lio_vf_main.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more details.
  17. ***********************************************************************/
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pci.h>
  21. #include <net/vxlan.h>
  22. #include "liquidio_common.h"
  23. #include "octeon_droq.h"
  24. #include "octeon_iq.h"
  25. #include "response_manager.h"
  26. #include "octeon_device.h"
  27. #include "octeon_nic.h"
  28. #include "octeon_main.h"
  29. #include "octeon_network.h"
  30. #include "cn23xx_vf_device.h"
  31. MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
  32. MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Virtual Function Driver");
  33. MODULE_LICENSE("GPL");
  34. MODULE_VERSION(LIQUIDIO_VERSION);
  35. static int debug = -1;
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
  38. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  39. struct liquidio_if_cfg_context {
  40. int octeon_id;
  41. wait_queue_head_t wc;
  42. int cond;
  43. };
  44. struct liquidio_if_cfg_resp {
  45. u64 rh;
  46. struct liquidio_if_cfg_info cfg_info;
  47. u64 status;
  48. };
  49. struct liquidio_rx_ctl_context {
  50. int octeon_id;
  51. wait_queue_head_t wc;
  52. int cond;
  53. };
  54. struct oct_timestamp_resp {
  55. u64 rh;
  56. u64 timestamp;
  57. u64 status;
  58. };
  59. union tx_info {
  60. u64 u64;
  61. struct {
  62. #ifdef __BIG_ENDIAN_BITFIELD
  63. u16 gso_size;
  64. u16 gso_segs;
  65. u32 reserved;
  66. #else
  67. u32 reserved;
  68. u16 gso_segs;
  69. u16 gso_size;
  70. #endif
  71. } s;
  72. };
  73. #define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
  74. #define OCTNIC_GSO_MAX_HEADER_SIZE 128
  75. #define OCTNIC_GSO_MAX_SIZE \
  76. (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
  77. struct octnic_gather {
  78. /* List manipulation. Next and prev pointers. */
  79. struct list_head list;
  80. /* Size of the gather component at sg in bytes. */
  81. int sg_size;
  82. /* Number of bytes that sg was adjusted to make it 8B-aligned. */
  83. int adjust;
  84. /* Gather component that can accommodate max sized fragment list
  85. * received from the IP layer.
  86. */
  87. struct octeon_sg_entry *sg;
  88. dma_addr_t sg_dma_ptr;
  89. };
  90. struct octeon_device_priv {
  91. /* Tasklet structures for this device. */
  92. struct tasklet_struct droq_tasklet;
  93. unsigned long napi_mask;
  94. };
  95. static int
  96. liquidio_vf_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  97. static void liquidio_vf_remove(struct pci_dev *pdev);
  98. static int octeon_device_init(struct octeon_device *oct);
  99. static int liquidio_stop(struct net_device *netdev);
  100. static int lio_wait_for_oq_pkts(struct octeon_device *oct)
  101. {
  102. struct octeon_device_priv *oct_priv =
  103. (struct octeon_device_priv *)oct->priv;
  104. int retry = MAX_VF_IP_OP_PENDING_PKT_COUNT;
  105. int pkt_cnt = 0, pending_pkts;
  106. int i;
  107. do {
  108. pending_pkts = 0;
  109. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
  110. if (!(oct->io_qmask.oq & BIT_ULL(i)))
  111. continue;
  112. pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
  113. }
  114. if (pkt_cnt > 0) {
  115. pending_pkts += pkt_cnt;
  116. tasklet_schedule(&oct_priv->droq_tasklet);
  117. }
  118. pkt_cnt = 0;
  119. schedule_timeout_uninterruptible(1);
  120. } while (retry-- && pending_pkts);
  121. return pkt_cnt;
  122. }
  123. /**
  124. * \brief wait for all pending requests to complete
  125. * @param oct Pointer to Octeon device
  126. *
  127. * Called during shutdown sequence
  128. */
  129. static int wait_for_pending_requests(struct octeon_device *oct)
  130. {
  131. int i, pcount = 0;
  132. for (i = 0; i < MAX_VF_IP_OP_PENDING_PKT_COUNT; i++) {
  133. pcount = atomic_read(
  134. &oct->response_list[OCTEON_ORDERED_SC_LIST]
  135. .pending_req_count);
  136. if (pcount)
  137. schedule_timeout_uninterruptible(HZ / 10);
  138. else
  139. break;
  140. }
  141. if (pcount)
  142. return 1;
  143. return 0;
  144. }
  145. /**
  146. * \brief Cause device to go quiet so it can be safely removed/reset/etc
  147. * @param oct Pointer to Octeon device
  148. */
  149. static void pcierror_quiesce_device(struct octeon_device *oct)
  150. {
  151. int i;
  152. /* Disable the input and output queues now. No more packets will
  153. * arrive from Octeon, but we should wait for all packet processing
  154. * to finish.
  155. */
  156. /* To allow for in-flight requests */
  157. schedule_timeout_uninterruptible(100);
  158. if (wait_for_pending_requests(oct))
  159. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  160. /* Force all requests waiting to be fetched by OCTEON to complete. */
  161. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  162. struct octeon_instr_queue *iq;
  163. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  164. continue;
  165. iq = oct->instr_queue[i];
  166. if (atomic_read(&iq->instr_pending)) {
  167. spin_lock_bh(&iq->lock);
  168. iq->fill_cnt = 0;
  169. iq->octeon_read_index = iq->host_write_index;
  170. iq->stats.instr_processed +=
  171. atomic_read(&iq->instr_pending);
  172. lio_process_iq_request_list(oct, iq, 0);
  173. spin_unlock_bh(&iq->lock);
  174. }
  175. }
  176. /* Force all pending ordered list requests to time out. */
  177. lio_process_ordered_list(oct, 1);
  178. /* We do not need to wait for output queue packets to be processed. */
  179. }
  180. /**
  181. * \brief Cleanup PCI AER uncorrectable error status
  182. * @param dev Pointer to PCI device
  183. */
  184. static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
  185. {
  186. u32 status, mask;
  187. int pos = 0x100;
  188. pr_info("%s :\n", __func__);
  189. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  190. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  191. if (dev->error_state == pci_channel_io_normal)
  192. status &= ~mask; /* Clear corresponding nonfatal bits */
  193. else
  194. status &= mask; /* Clear corresponding fatal bits */
  195. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  196. }
  197. /**
  198. * \brief Stop all PCI IO to a given device
  199. * @param dev Pointer to Octeon device
  200. */
  201. static void stop_pci_io(struct octeon_device *oct)
  202. {
  203. struct msix_entry *msix_entries;
  204. int i;
  205. /* No more instructions will be forwarded. */
  206. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  207. for (i = 0; i < oct->ifcount; i++)
  208. netif_device_detach(oct->props[i].netdev);
  209. /* Disable interrupts */
  210. oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
  211. pcierror_quiesce_device(oct);
  212. if (oct->msix_on) {
  213. msix_entries = (struct msix_entry *)oct->msix_entries;
  214. for (i = 0; i < oct->num_msix_irqs; i++) {
  215. /* clear the affinity_cpumask */
  216. irq_set_affinity_hint(msix_entries[i].vector,
  217. NULL);
  218. free_irq(msix_entries[i].vector,
  219. &oct->ioq_vector[i]);
  220. }
  221. pci_disable_msix(oct->pci_dev);
  222. kfree(oct->msix_entries);
  223. oct->msix_entries = NULL;
  224. octeon_free_ioq_vector(oct);
  225. }
  226. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  227. lio_get_state_string(&oct->status));
  228. /* making it a common function for all OCTEON models */
  229. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  230. pci_disable_device(oct->pci_dev);
  231. }
  232. /**
  233. * \brief called when PCI error is detected
  234. * @param pdev Pointer to PCI device
  235. * @param state The current pci connection state
  236. *
  237. * This function is called after a PCI bus error affecting
  238. * this device has been detected.
  239. */
  240. static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
  241. pci_channel_state_t state)
  242. {
  243. struct octeon_device *oct = pci_get_drvdata(pdev);
  244. /* Non-correctable Non-fatal errors */
  245. if (state == pci_channel_io_normal) {
  246. dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
  247. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  248. return PCI_ERS_RESULT_CAN_RECOVER;
  249. }
  250. /* Non-correctable Fatal errors */
  251. dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
  252. stop_pci_io(oct);
  253. return PCI_ERS_RESULT_DISCONNECT;
  254. }
  255. /* For PCI-E Advanced Error Recovery (AER) Interface */
  256. static const struct pci_error_handlers liquidio_vf_err_handler = {
  257. .error_detected = liquidio_pcie_error_detected,
  258. };
  259. static const struct pci_device_id liquidio_vf_pci_tbl[] = {
  260. {
  261. PCI_VENDOR_ID_CAVIUM, OCTEON_CN23XX_VF_VID,
  262. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  263. },
  264. {
  265. 0, 0, 0, 0, 0, 0, 0
  266. }
  267. };
  268. MODULE_DEVICE_TABLE(pci, liquidio_vf_pci_tbl);
  269. static struct pci_driver liquidio_vf_pci_driver = {
  270. .name = "LiquidIO_VF",
  271. .id_table = liquidio_vf_pci_tbl,
  272. .probe = liquidio_vf_probe,
  273. .remove = liquidio_vf_remove,
  274. .err_handler = &liquidio_vf_err_handler, /* For AER */
  275. };
  276. /**
  277. * \brief Stop Tx queues
  278. * @param netdev network device
  279. */
  280. static void txqs_stop(struct net_device *netdev)
  281. {
  282. if (netif_is_multiqueue(netdev)) {
  283. int i;
  284. for (i = 0; i < netdev->num_tx_queues; i++)
  285. netif_stop_subqueue(netdev, i);
  286. } else {
  287. netif_stop_queue(netdev);
  288. }
  289. }
  290. /**
  291. * \brief Start Tx queues
  292. * @param netdev network device
  293. */
  294. static void txqs_start(struct net_device *netdev)
  295. {
  296. if (netif_is_multiqueue(netdev)) {
  297. int i;
  298. for (i = 0; i < netdev->num_tx_queues; i++)
  299. netif_start_subqueue(netdev, i);
  300. } else {
  301. netif_start_queue(netdev);
  302. }
  303. }
  304. /**
  305. * \brief Wake Tx queues
  306. * @param netdev network device
  307. */
  308. static void txqs_wake(struct net_device *netdev)
  309. {
  310. struct lio *lio = GET_LIO(netdev);
  311. if (netif_is_multiqueue(netdev)) {
  312. int i;
  313. for (i = 0; i < netdev->num_tx_queues; i++) {
  314. int qno = lio->linfo.txpciq[i % (lio->linfo.num_txpciq)]
  315. .s.q_no;
  316. if (__netif_subqueue_stopped(netdev, i)) {
  317. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
  318. tx_restart, 1);
  319. netif_wake_subqueue(netdev, i);
  320. }
  321. }
  322. } else {
  323. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
  324. tx_restart, 1);
  325. netif_wake_queue(netdev);
  326. }
  327. }
  328. /**
  329. * \brief Start Tx queue
  330. * @param netdev network device
  331. */
  332. static void start_txq(struct net_device *netdev)
  333. {
  334. struct lio *lio = GET_LIO(netdev);
  335. if (lio->linfo.link.s.link_up) {
  336. txqs_start(netdev);
  337. return;
  338. }
  339. }
  340. /**
  341. * \brief Wake a queue
  342. * @param netdev network device
  343. * @param q which queue to wake
  344. */
  345. static void wake_q(struct net_device *netdev, int q)
  346. {
  347. if (netif_is_multiqueue(netdev))
  348. netif_wake_subqueue(netdev, q);
  349. else
  350. netif_wake_queue(netdev);
  351. }
  352. /**
  353. * \brief Stop a queue
  354. * @param netdev network device
  355. * @param q which queue to stop
  356. */
  357. static void stop_q(struct net_device *netdev, int q)
  358. {
  359. if (netif_is_multiqueue(netdev))
  360. netif_stop_subqueue(netdev, q);
  361. else
  362. netif_stop_queue(netdev);
  363. }
  364. /**
  365. * Remove the node at the head of the list. The list would be empty at
  366. * the end of this call if there are no more nodes in the list.
  367. */
  368. static struct list_head *list_delete_head(struct list_head *root)
  369. {
  370. struct list_head *node;
  371. if ((root->prev == root) && (root->next == root))
  372. node = NULL;
  373. else
  374. node = root->next;
  375. if (node)
  376. list_del(node);
  377. return node;
  378. }
  379. /**
  380. * \brief Delete gather lists
  381. * @param lio per-network private data
  382. */
  383. static void delete_glists(struct lio *lio)
  384. {
  385. struct octnic_gather *g;
  386. int i;
  387. kfree(lio->glist_lock);
  388. lio->glist_lock = NULL;
  389. if (!lio->glist)
  390. return;
  391. for (i = 0; i < lio->linfo.num_txpciq; i++) {
  392. do {
  393. g = (struct octnic_gather *)
  394. list_delete_head(&lio->glist[i]);
  395. if (g)
  396. kfree(g);
  397. } while (g);
  398. if (lio->glists_virt_base && lio->glists_virt_base[i] &&
  399. lio->glists_dma_base && lio->glists_dma_base[i]) {
  400. lio_dma_free(lio->oct_dev,
  401. lio->glist_entry_size * lio->tx_qsize,
  402. lio->glists_virt_base[i],
  403. lio->glists_dma_base[i]);
  404. }
  405. }
  406. kfree(lio->glists_virt_base);
  407. lio->glists_virt_base = NULL;
  408. kfree(lio->glists_dma_base);
  409. lio->glists_dma_base = NULL;
  410. kfree(lio->glist);
  411. lio->glist = NULL;
  412. }
  413. /**
  414. * \brief Setup gather lists
  415. * @param lio per-network private data
  416. */
  417. static int setup_glists(struct lio *lio, int num_iqs)
  418. {
  419. struct octnic_gather *g;
  420. int i, j;
  421. lio->glist_lock =
  422. kzalloc(sizeof(*lio->glist_lock) * num_iqs, GFP_KERNEL);
  423. if (!lio->glist_lock)
  424. return -ENOMEM;
  425. lio->glist =
  426. kzalloc(sizeof(*lio->glist) * num_iqs, GFP_KERNEL);
  427. if (!lio->glist) {
  428. kfree(lio->glist_lock);
  429. lio->glist_lock = NULL;
  430. return -ENOMEM;
  431. }
  432. lio->glist_entry_size =
  433. ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
  434. /* allocate memory to store virtual and dma base address of
  435. * per glist consistent memory
  436. */
  437. lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
  438. GFP_KERNEL);
  439. lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
  440. GFP_KERNEL);
  441. if (!lio->glists_virt_base || !lio->glists_dma_base) {
  442. delete_glists(lio);
  443. return -ENOMEM;
  444. }
  445. for (i = 0; i < num_iqs; i++) {
  446. spin_lock_init(&lio->glist_lock[i]);
  447. INIT_LIST_HEAD(&lio->glist[i]);
  448. lio->glists_virt_base[i] =
  449. lio_dma_alloc(lio->oct_dev,
  450. lio->glist_entry_size * lio->tx_qsize,
  451. &lio->glists_dma_base[i]);
  452. if (!lio->glists_virt_base[i]) {
  453. delete_glists(lio);
  454. return -ENOMEM;
  455. }
  456. for (j = 0; j < lio->tx_qsize; j++) {
  457. g = kzalloc(sizeof(*g), GFP_KERNEL);
  458. if (!g)
  459. break;
  460. g->sg = lio->glists_virt_base[i] +
  461. (j * lio->glist_entry_size);
  462. g->sg_dma_ptr = lio->glists_dma_base[i] +
  463. (j * lio->glist_entry_size);
  464. list_add_tail(&g->list, &lio->glist[i]);
  465. }
  466. if (j != lio->tx_qsize) {
  467. delete_glists(lio);
  468. return -ENOMEM;
  469. }
  470. }
  471. return 0;
  472. }
  473. /**
  474. * \brief Print link information
  475. * @param netdev network device
  476. */
  477. static void print_link_info(struct net_device *netdev)
  478. {
  479. struct lio *lio = GET_LIO(netdev);
  480. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
  481. struct oct_link_info *linfo = &lio->linfo;
  482. if (linfo->link.s.link_up) {
  483. netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
  484. linfo->link.s.speed,
  485. (linfo->link.s.duplex) ? "Full" : "Half");
  486. } else {
  487. netif_info(lio, link, lio->netdev, "Link Down\n");
  488. }
  489. }
  490. }
  491. /**
  492. * \brief Routine to notify MTU change
  493. * @param work work_struct data structure
  494. */
  495. static void octnet_link_status_change(struct work_struct *work)
  496. {
  497. struct cavium_wk *wk = (struct cavium_wk *)work;
  498. struct lio *lio = (struct lio *)wk->ctxptr;
  499. rtnl_lock();
  500. call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
  501. rtnl_unlock();
  502. }
  503. /**
  504. * \brief Sets up the mtu status change work
  505. * @param netdev network device
  506. */
  507. static int setup_link_status_change_wq(struct net_device *netdev)
  508. {
  509. struct lio *lio = GET_LIO(netdev);
  510. struct octeon_device *oct = lio->oct_dev;
  511. lio->link_status_wq.wq = alloc_workqueue("link-status",
  512. WQ_MEM_RECLAIM, 0);
  513. if (!lio->link_status_wq.wq) {
  514. dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
  515. return -1;
  516. }
  517. INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
  518. octnet_link_status_change);
  519. lio->link_status_wq.wk.ctxptr = lio;
  520. return 0;
  521. }
  522. static void cleanup_link_status_change_wq(struct net_device *netdev)
  523. {
  524. struct lio *lio = GET_LIO(netdev);
  525. if (lio->link_status_wq.wq) {
  526. cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
  527. destroy_workqueue(lio->link_status_wq.wq);
  528. }
  529. }
  530. /**
  531. * \brief Update link status
  532. * @param netdev network device
  533. * @param ls link status structure
  534. *
  535. * Called on receipt of a link status response from the core application to
  536. * update each interface's link status.
  537. */
  538. static void update_link_status(struct net_device *netdev,
  539. union oct_link_status *ls)
  540. {
  541. struct lio *lio = GET_LIO(netdev);
  542. struct octeon_device *oct = lio->oct_dev;
  543. if ((lio->intf_open) && (lio->linfo.link.u64 != ls->u64)) {
  544. lio->linfo.link.u64 = ls->u64;
  545. print_link_info(netdev);
  546. lio->link_changes++;
  547. if (lio->linfo.link.s.link_up) {
  548. netif_carrier_on(netdev);
  549. txqs_wake(netdev);
  550. } else {
  551. netif_carrier_off(netdev);
  552. txqs_stop(netdev);
  553. }
  554. if (lio->linfo.link.s.mtu < netdev->mtu) {
  555. dev_warn(&oct->pci_dev->dev,
  556. "PF has changed the MTU for gmx port. Reducing the mtu from %d to %d\n",
  557. netdev->mtu, lio->linfo.link.s.mtu);
  558. lio->mtu = lio->linfo.link.s.mtu;
  559. netdev->mtu = lio->linfo.link.s.mtu;
  560. queue_delayed_work(lio->link_status_wq.wq,
  561. &lio->link_status_wq.wk.work, 0);
  562. }
  563. }
  564. }
  565. static void update_txq_status(struct octeon_device *oct, int iq_num)
  566. {
  567. struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
  568. struct net_device *netdev;
  569. struct lio *lio;
  570. netdev = oct->props[iq->ifidx].netdev;
  571. lio = GET_LIO(netdev);
  572. if (netif_is_multiqueue(netdev)) {
  573. if (__netif_subqueue_stopped(netdev, iq->q_index) &&
  574. lio->linfo.link.s.link_up &&
  575. (!octnet_iq_is_full(oct, iq_num))) {
  576. netif_wake_subqueue(netdev, iq->q_index);
  577. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
  578. tx_restart, 1);
  579. }
  580. } else if (netif_queue_stopped(netdev) && lio->linfo.link.s.link_up &&
  581. (!octnet_iq_is_full(oct, lio->txq))) {
  582. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
  583. lio->txq, tx_restart, 1);
  584. netif_wake_queue(netdev);
  585. }
  586. }
  587. static
  588. int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
  589. {
  590. struct octeon_device *oct = droq->oct_dev;
  591. struct octeon_device_priv *oct_priv =
  592. (struct octeon_device_priv *)oct->priv;
  593. if (droq->ops.poll_mode) {
  594. droq->ops.napi_fn(droq);
  595. } else {
  596. if (ret & MSIX_PO_INT) {
  597. dev_err(&oct->pci_dev->dev,
  598. "should not come here should not get rx when poll mode = 0 for vf\n");
  599. tasklet_schedule(&oct_priv->droq_tasklet);
  600. return 1;
  601. }
  602. /* this will be flushed periodically by check iq db */
  603. if (ret & MSIX_PI_INT)
  604. return 0;
  605. }
  606. return 0;
  607. }
  608. static irqreturn_t
  609. liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
  610. {
  611. struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
  612. struct octeon_device *oct = ioq_vector->oct_dev;
  613. struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
  614. u64 ret;
  615. ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
  616. if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
  617. liquidio_schedule_msix_droq_pkt_handler(droq, ret);
  618. return IRQ_HANDLED;
  619. }
  620. /**
  621. * \brief Setup interrupt for octeon device
  622. * @param oct octeon device
  623. *
  624. * Enable interrupt in Octeon device as given in the PCI interrupt mask.
  625. */
  626. static int octeon_setup_interrupt(struct octeon_device *oct)
  627. {
  628. struct msix_entry *msix_entries;
  629. char *queue_irq_names = NULL;
  630. int num_alloc_ioq_vectors;
  631. int num_ioq_vectors;
  632. int irqret;
  633. int i;
  634. if (oct->msix_on) {
  635. oct->num_msix_irqs = oct->sriov_info.rings_per_vf;
  636. /* allocate storage for the names assigned to each irq */
  637. oct->irq_name_storage =
  638. kcalloc(MAX_IOQ_INTERRUPTS_PER_VF, INTRNAMSIZ,
  639. GFP_KERNEL);
  640. if (!oct->irq_name_storage) {
  641. dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
  642. return -ENOMEM;
  643. }
  644. queue_irq_names = oct->irq_name_storage;
  645. oct->msix_entries = kcalloc(
  646. oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
  647. if (!oct->msix_entries) {
  648. dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
  649. kfree(oct->irq_name_storage);
  650. oct->irq_name_storage = NULL;
  651. return -ENOMEM;
  652. }
  653. msix_entries = (struct msix_entry *)oct->msix_entries;
  654. for (i = 0; i < oct->num_msix_irqs; i++)
  655. msix_entries[i].entry = i;
  656. num_alloc_ioq_vectors = pci_enable_msix_range(
  657. oct->pci_dev, msix_entries,
  658. oct->num_msix_irqs,
  659. oct->num_msix_irqs);
  660. if (num_alloc_ioq_vectors < 0) {
  661. dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
  662. kfree(oct->msix_entries);
  663. oct->msix_entries = NULL;
  664. kfree(oct->irq_name_storage);
  665. oct->irq_name_storage = NULL;
  666. return num_alloc_ioq_vectors;
  667. }
  668. dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
  669. num_ioq_vectors = oct->num_msix_irqs;
  670. for (i = 0; i < num_ioq_vectors; i++) {
  671. snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
  672. "LiquidIO%u-vf%u-rxtx-%u",
  673. oct->octeon_id, oct->vf_num, i);
  674. irqret = request_irq(msix_entries[i].vector,
  675. liquidio_msix_intr_handler, 0,
  676. &queue_irq_names[IRQ_NAME_OFF(i)],
  677. &oct->ioq_vector[i]);
  678. if (irqret) {
  679. dev_err(&oct->pci_dev->dev,
  680. "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
  681. irqret);
  682. while (i) {
  683. i--;
  684. irq_set_affinity_hint(
  685. msix_entries[i].vector, NULL);
  686. free_irq(msix_entries[i].vector,
  687. &oct->ioq_vector[i]);
  688. }
  689. pci_disable_msix(oct->pci_dev);
  690. kfree(oct->msix_entries);
  691. oct->msix_entries = NULL;
  692. kfree(oct->irq_name_storage);
  693. oct->irq_name_storage = NULL;
  694. return irqret;
  695. }
  696. oct->ioq_vector[i].vector = msix_entries[i].vector;
  697. /* assign the cpu mask for this msix interrupt vector */
  698. irq_set_affinity_hint(
  699. msix_entries[i].vector,
  700. (&oct->ioq_vector[i].affinity_mask));
  701. }
  702. dev_dbg(&oct->pci_dev->dev,
  703. "OCTEON[%d]: MSI-X enabled\n", oct->octeon_id);
  704. }
  705. return 0;
  706. }
  707. /**
  708. * \brief PCI probe handler
  709. * @param pdev PCI device structure
  710. * @param ent unused
  711. */
  712. static int
  713. liquidio_vf_probe(struct pci_dev *pdev,
  714. const struct pci_device_id *ent __attribute__((unused)))
  715. {
  716. struct octeon_device *oct_dev = NULL;
  717. oct_dev = octeon_allocate_device(pdev->device,
  718. sizeof(struct octeon_device_priv));
  719. if (!oct_dev) {
  720. dev_err(&pdev->dev, "Unable to allocate device\n");
  721. return -ENOMEM;
  722. }
  723. oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
  724. dev_info(&pdev->dev, "Initializing device %x:%x.\n",
  725. (u32)pdev->vendor, (u32)pdev->device);
  726. /* Assign octeon_device for this device to the private data area. */
  727. pci_set_drvdata(pdev, oct_dev);
  728. /* set linux specific device pointer */
  729. oct_dev->pci_dev = pdev;
  730. if (octeon_device_init(oct_dev)) {
  731. liquidio_vf_remove(pdev);
  732. return -ENOMEM;
  733. }
  734. dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
  735. return 0;
  736. }
  737. /**
  738. * \brief PCI FLR for each Octeon device.
  739. * @param oct octeon device
  740. */
  741. static void octeon_pci_flr(struct octeon_device *oct)
  742. {
  743. u16 status;
  744. pci_save_state(oct->pci_dev);
  745. pci_cfg_access_lock(oct->pci_dev);
  746. /* Quiesce the device completely */
  747. pci_write_config_word(oct->pci_dev, PCI_COMMAND,
  748. PCI_COMMAND_INTX_DISABLE);
  749. /* Wait for Transaction Pending bit clean */
  750. msleep(100);
  751. pcie_capability_read_word(oct->pci_dev, PCI_EXP_DEVSTA, &status);
  752. if (status & PCI_EXP_DEVSTA_TRPND) {
  753. dev_info(&oct->pci_dev->dev, "Function reset incomplete after 100ms, sleeping for 5 seconds\n");
  754. ssleep(5);
  755. pcie_capability_read_word(oct->pci_dev, PCI_EXP_DEVSTA,
  756. &status);
  757. if (status & PCI_EXP_DEVSTA_TRPND)
  758. dev_info(&oct->pci_dev->dev, "Function reset still incomplete after 5s, reset anyway\n");
  759. }
  760. pcie_capability_set_word(oct->pci_dev, PCI_EXP_DEVCTL,
  761. PCI_EXP_DEVCTL_BCR_FLR);
  762. mdelay(100);
  763. pci_cfg_access_unlock(oct->pci_dev);
  764. pci_restore_state(oct->pci_dev);
  765. }
  766. /**
  767. *\brief Destroy resources associated with octeon device
  768. * @param pdev PCI device structure
  769. * @param ent unused
  770. */
  771. static void octeon_destroy_resources(struct octeon_device *oct)
  772. {
  773. struct msix_entry *msix_entries;
  774. int i;
  775. switch (atomic_read(&oct->status)) {
  776. case OCT_DEV_RUNNING:
  777. case OCT_DEV_CORE_OK:
  778. /* No more instructions will be forwarded. */
  779. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  780. oct->app_mode = CVM_DRV_INVALID_APP;
  781. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  782. lio_get_state_string(&oct->status));
  783. schedule_timeout_uninterruptible(HZ / 10);
  784. /* fallthrough */
  785. case OCT_DEV_HOST_OK:
  786. /* fallthrough */
  787. case OCT_DEV_IO_QUEUES_DONE:
  788. if (wait_for_pending_requests(oct))
  789. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  790. if (lio_wait_for_instr_fetch(oct))
  791. dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
  792. /* Disable the input and output queues now. No more packets will
  793. * arrive from Octeon, but we should wait for all packet
  794. * processing to finish.
  795. */
  796. oct->fn_list.disable_io_queues(oct);
  797. if (lio_wait_for_oq_pkts(oct))
  798. dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
  799. case OCT_DEV_INTR_SET_DONE:
  800. /* Disable interrupts */
  801. oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
  802. if (oct->msix_on) {
  803. msix_entries = (struct msix_entry *)oct->msix_entries;
  804. for (i = 0; i < oct->num_msix_irqs; i++) {
  805. irq_set_affinity_hint(msix_entries[i].vector,
  806. NULL);
  807. free_irq(msix_entries[i].vector,
  808. &oct->ioq_vector[i]);
  809. }
  810. pci_disable_msix(oct->pci_dev);
  811. kfree(oct->msix_entries);
  812. oct->msix_entries = NULL;
  813. kfree(oct->irq_name_storage);
  814. oct->irq_name_storage = NULL;
  815. }
  816. /* Soft reset the octeon device before exiting */
  817. if (oct->pci_dev->reset_fn)
  818. octeon_pci_flr(oct);
  819. else
  820. cn23xx_vf_ask_pf_to_do_flr(oct);
  821. /* fallthrough */
  822. case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
  823. octeon_free_ioq_vector(oct);
  824. /* fallthrough */
  825. case OCT_DEV_MBOX_SETUP_DONE:
  826. oct->fn_list.free_mbox(oct);
  827. /* fallthrough */
  828. case OCT_DEV_IN_RESET:
  829. case OCT_DEV_DROQ_INIT_DONE:
  830. mdelay(100);
  831. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
  832. if (!(oct->io_qmask.oq & BIT_ULL(i)))
  833. continue;
  834. octeon_delete_droq(oct, i);
  835. }
  836. /* fallthrough */
  837. case OCT_DEV_RESP_LIST_INIT_DONE:
  838. octeon_delete_response_list(oct);
  839. /* fallthrough */
  840. case OCT_DEV_INSTR_QUEUE_INIT_DONE:
  841. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  842. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  843. continue;
  844. octeon_delete_instr_queue(oct, i);
  845. }
  846. /* fallthrough */
  847. case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
  848. octeon_free_sc_buffer_pool(oct);
  849. /* fallthrough */
  850. case OCT_DEV_DISPATCH_INIT_DONE:
  851. octeon_delete_dispatch_list(oct);
  852. cancel_delayed_work_sync(&oct->nic_poll_work.work);
  853. /* fallthrough */
  854. case OCT_DEV_PCI_MAP_DONE:
  855. octeon_unmap_pci_barx(oct, 0);
  856. octeon_unmap_pci_barx(oct, 1);
  857. /* fallthrough */
  858. case OCT_DEV_PCI_ENABLE_DONE:
  859. pci_clear_master(oct->pci_dev);
  860. /* Disable the device, releasing the PCI INT */
  861. pci_disable_device(oct->pci_dev);
  862. /* fallthrough */
  863. case OCT_DEV_BEGIN_STATE:
  864. /* Nothing to be done here either */
  865. break;
  866. }
  867. }
  868. /**
  869. * \brief Callback for rx ctrl
  870. * @param status status of request
  871. * @param buf pointer to resp structure
  872. */
  873. static void rx_ctl_callback(struct octeon_device *oct,
  874. u32 status, void *buf)
  875. {
  876. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  877. struct liquidio_rx_ctl_context *ctx;
  878. ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
  879. oct = lio_get_device(ctx->octeon_id);
  880. if (status)
  881. dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
  882. CVM_CAST64(status));
  883. WRITE_ONCE(ctx->cond, 1);
  884. /* This barrier is required to be sure that the response has been
  885. * written fully before waking up the handler
  886. */
  887. wmb();
  888. wake_up_interruptible(&ctx->wc);
  889. }
  890. /**
  891. * \brief Send Rx control command
  892. * @param lio per-network private data
  893. * @param start_stop whether to start or stop
  894. */
  895. static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
  896. {
  897. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  898. int ctx_size = sizeof(struct liquidio_rx_ctl_context);
  899. struct liquidio_rx_ctl_context *ctx;
  900. struct octeon_soft_command *sc;
  901. union octnet_cmd *ncmd;
  902. int retval;
  903. if (oct->props[lio->ifidx].rx_on == start_stop)
  904. return;
  905. sc = (struct octeon_soft_command *)
  906. octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
  907. 16, ctx_size);
  908. ncmd = (union octnet_cmd *)sc->virtdptr;
  909. ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
  910. WRITE_ONCE(ctx->cond, 0);
  911. ctx->octeon_id = lio_get_device_id(oct);
  912. init_waitqueue_head(&ctx->wc);
  913. ncmd->u64 = 0;
  914. ncmd->s.cmd = OCTNET_CMD_RX_CTL;
  915. ncmd->s.param1 = start_stop;
  916. octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
  917. sc->iq_no = lio->linfo.txpciq[0].s.q_no;
  918. octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
  919. OPCODE_NIC_CMD, 0, 0, 0);
  920. sc->callback = rx_ctl_callback;
  921. sc->callback_arg = sc;
  922. sc->wait_time = 5000;
  923. retval = octeon_send_soft_command(oct, sc);
  924. if (retval == IQ_SEND_FAILED) {
  925. netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
  926. } else {
  927. /* Sleep on a wait queue till the cond flag indicates that the
  928. * response arrived or timed-out.
  929. */
  930. if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
  931. return;
  932. oct->props[lio->ifidx].rx_on = start_stop;
  933. }
  934. octeon_free_soft_command(oct, sc);
  935. }
  936. /**
  937. * \brief Destroy NIC device interface
  938. * @param oct octeon device
  939. * @param ifidx which interface to destroy
  940. *
  941. * Cleanup associated with each interface for an Octeon device when NIC
  942. * module is being unloaded or if initialization fails during load.
  943. */
  944. static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
  945. {
  946. struct net_device *netdev = oct->props[ifidx].netdev;
  947. struct napi_struct *napi, *n;
  948. struct lio *lio;
  949. if (!netdev) {
  950. dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
  951. __func__, ifidx);
  952. return;
  953. }
  954. lio = GET_LIO(netdev);
  955. dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
  956. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
  957. liquidio_stop(netdev);
  958. if (oct->props[lio->ifidx].napi_enabled == 1) {
  959. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  960. napi_disable(napi);
  961. oct->props[lio->ifidx].napi_enabled = 0;
  962. oct->droq[0]->ops.poll_mode = 0;
  963. }
  964. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
  965. unregister_netdev(netdev);
  966. cleanup_rx_oom_poll_fn(netdev);
  967. cleanup_link_status_change_wq(netdev);
  968. delete_glists(lio);
  969. free_netdev(netdev);
  970. oct->props[ifidx].gmxport = -1;
  971. oct->props[ifidx].netdev = NULL;
  972. }
  973. /**
  974. * \brief Stop complete NIC functionality
  975. * @param oct octeon device
  976. */
  977. static int liquidio_stop_nic_module(struct octeon_device *oct)
  978. {
  979. struct lio *lio;
  980. int i, j;
  981. dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
  982. if (!oct->ifcount) {
  983. dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
  984. return 1;
  985. }
  986. spin_lock_bh(&oct->cmd_resp_wqlock);
  987. oct->cmd_resp_state = OCT_DRV_OFFLINE;
  988. spin_unlock_bh(&oct->cmd_resp_wqlock);
  989. for (i = 0; i < oct->ifcount; i++) {
  990. lio = GET_LIO(oct->props[i].netdev);
  991. for (j = 0; j < lio->linfo.num_rxpciq; j++)
  992. octeon_unregister_droq_ops(oct,
  993. lio->linfo.rxpciq[j].s.q_no);
  994. }
  995. for (i = 0; i < oct->ifcount; i++)
  996. liquidio_destroy_nic_device(oct, i);
  997. dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
  998. return 0;
  999. }
  1000. /**
  1001. * \brief Cleans up resources at unload time
  1002. * @param pdev PCI device structure
  1003. */
  1004. static void liquidio_vf_remove(struct pci_dev *pdev)
  1005. {
  1006. struct octeon_device *oct_dev = pci_get_drvdata(pdev);
  1007. dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
  1008. if (oct_dev->app_mode == CVM_DRV_NIC_APP)
  1009. liquidio_stop_nic_module(oct_dev);
  1010. /* Reset the octeon device and cleanup all memory allocated for
  1011. * the octeon device by driver.
  1012. */
  1013. octeon_destroy_resources(oct_dev);
  1014. dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
  1015. /* This octeon device has been removed. Update the global
  1016. * data structure to reflect this. Free the device structure.
  1017. */
  1018. octeon_free_device_mem(oct_dev);
  1019. }
  1020. /**
  1021. * \brief PCI initialization for each Octeon device.
  1022. * @param oct octeon device
  1023. */
  1024. static int octeon_pci_os_setup(struct octeon_device *oct)
  1025. {
  1026. #ifdef CONFIG_PCI_IOV
  1027. /* setup PCI stuff first */
  1028. if (!oct->pci_dev->physfn)
  1029. octeon_pci_flr(oct);
  1030. #endif
  1031. if (pci_enable_device(oct->pci_dev)) {
  1032. dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
  1033. return 1;
  1034. }
  1035. if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
  1036. dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
  1037. pci_disable_device(oct->pci_dev);
  1038. return 1;
  1039. }
  1040. /* Enable PCI DMA Master. */
  1041. pci_set_master(oct->pci_dev);
  1042. return 0;
  1043. }
  1044. static int skb_iq(struct lio *lio, struct sk_buff *skb)
  1045. {
  1046. int q = 0;
  1047. if (netif_is_multiqueue(lio->netdev))
  1048. q = skb->queue_mapping % lio->linfo.num_txpciq;
  1049. return q;
  1050. }
  1051. /**
  1052. * \brief Check Tx queue state for a given network buffer
  1053. * @param lio per-network private data
  1054. * @param skb network buffer
  1055. */
  1056. static int check_txq_state(struct lio *lio, struct sk_buff *skb)
  1057. {
  1058. int q = 0, iq = 0;
  1059. if (netif_is_multiqueue(lio->netdev)) {
  1060. q = skb->queue_mapping;
  1061. iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
  1062. } else {
  1063. iq = lio->txq;
  1064. q = iq;
  1065. }
  1066. if (octnet_iq_is_full(lio->oct_dev, iq))
  1067. return 0;
  1068. if (__netif_subqueue_stopped(lio->netdev, q)) {
  1069. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
  1070. wake_q(lio->netdev, q);
  1071. }
  1072. return 1;
  1073. }
  1074. /**
  1075. * \brief Unmap and free network buffer
  1076. * @param buf buffer
  1077. */
  1078. static void free_netbuf(void *buf)
  1079. {
  1080. struct octnet_buf_free_info *finfo;
  1081. struct sk_buff *skb;
  1082. struct lio *lio;
  1083. finfo = (struct octnet_buf_free_info *)buf;
  1084. skb = finfo->skb;
  1085. lio = finfo->lio;
  1086. dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
  1087. DMA_TO_DEVICE);
  1088. check_txq_state(lio, skb);
  1089. tx_buffer_free(skb);
  1090. }
  1091. /**
  1092. * \brief Unmap and free gather buffer
  1093. * @param buf buffer
  1094. */
  1095. static void free_netsgbuf(void *buf)
  1096. {
  1097. struct octnet_buf_free_info *finfo;
  1098. struct octnic_gather *g;
  1099. struct sk_buff *skb;
  1100. int i, frags, iq;
  1101. struct lio *lio;
  1102. finfo = (struct octnet_buf_free_info *)buf;
  1103. skb = finfo->skb;
  1104. lio = finfo->lio;
  1105. g = finfo->g;
  1106. frags = skb_shinfo(skb)->nr_frags;
  1107. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1108. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1109. DMA_TO_DEVICE);
  1110. i = 1;
  1111. while (frags--) {
  1112. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1113. pci_unmap_page((lio->oct_dev)->pci_dev,
  1114. g->sg[(i >> 2)].ptr[(i & 3)],
  1115. frag->size, DMA_TO_DEVICE);
  1116. i++;
  1117. }
  1118. iq = skb_iq(lio, skb);
  1119. spin_lock(&lio->glist_lock[iq]);
  1120. list_add_tail(&g->list, &lio->glist[iq]);
  1121. spin_unlock(&lio->glist_lock[iq]);
  1122. check_txq_state(lio, skb); /* mq support: sub-queue state check */
  1123. tx_buffer_free(skb);
  1124. }
  1125. /**
  1126. * \brief Unmap and free gather buffer with response
  1127. * @param buf buffer
  1128. */
  1129. static void free_netsgbuf_with_resp(void *buf)
  1130. {
  1131. struct octnet_buf_free_info *finfo;
  1132. struct octeon_soft_command *sc;
  1133. struct octnic_gather *g;
  1134. struct sk_buff *skb;
  1135. int i, frags, iq;
  1136. struct lio *lio;
  1137. sc = (struct octeon_soft_command *)buf;
  1138. skb = (struct sk_buff *)sc->callback_arg;
  1139. finfo = (struct octnet_buf_free_info *)&skb->cb;
  1140. lio = finfo->lio;
  1141. g = finfo->g;
  1142. frags = skb_shinfo(skb)->nr_frags;
  1143. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1144. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1145. DMA_TO_DEVICE);
  1146. i = 1;
  1147. while (frags--) {
  1148. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1149. pci_unmap_page((lio->oct_dev)->pci_dev,
  1150. g->sg[(i >> 2)].ptr[(i & 3)],
  1151. frag->size, DMA_TO_DEVICE);
  1152. i++;
  1153. }
  1154. iq = skb_iq(lio, skb);
  1155. spin_lock(&lio->glist_lock[iq]);
  1156. list_add_tail(&g->list, &lio->glist[iq]);
  1157. spin_unlock(&lio->glist_lock[iq]);
  1158. /* Don't free the skb yet */
  1159. check_txq_state(lio, skb);
  1160. }
  1161. /**
  1162. * \brief Setup output queue
  1163. * @param oct octeon device
  1164. * @param q_no which queue
  1165. * @param num_descs how many descriptors
  1166. * @param desc_size size of each descriptor
  1167. * @param app_ctx application context
  1168. */
  1169. static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
  1170. int desc_size, void *app_ctx)
  1171. {
  1172. int ret_val;
  1173. dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
  1174. /* droq creation and local register settings. */
  1175. ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
  1176. if (ret_val < 0)
  1177. return ret_val;
  1178. if (ret_val == 1) {
  1179. dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
  1180. return 0;
  1181. }
  1182. /* Enable the droq queues */
  1183. octeon_set_droq_pkt_op(oct, q_no, 1);
  1184. /* Send Credit for Octeon Output queues. Credits are always
  1185. * sent after the output queue is enabled.
  1186. */
  1187. writel(oct->droq[q_no]->max_count, oct->droq[q_no]->pkts_credit_reg);
  1188. return ret_val;
  1189. }
  1190. /**
  1191. * \brief Callback for getting interface configuration
  1192. * @param status status of request
  1193. * @param buf pointer to resp structure
  1194. */
  1195. static void if_cfg_callback(struct octeon_device *oct,
  1196. u32 status __attribute__((unused)), void *buf)
  1197. {
  1198. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  1199. struct liquidio_if_cfg_context *ctx;
  1200. struct liquidio_if_cfg_resp *resp;
  1201. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  1202. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  1203. oct = lio_get_device(ctx->octeon_id);
  1204. if (resp->status)
  1205. dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
  1206. CVM_CAST64(resp->status));
  1207. WRITE_ONCE(ctx->cond, 1);
  1208. snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
  1209. resp->cfg_info.liquidio_firmware_version);
  1210. /* This barrier is required to be sure that the response has been
  1211. * written fully before waking up the handler
  1212. */
  1213. wmb();
  1214. wake_up_interruptible(&ctx->wc);
  1215. }
  1216. /** Routine to push packets arriving on Octeon interface upto network layer.
  1217. * @param oct_id - octeon device id.
  1218. * @param skbuff - skbuff struct to be passed to network layer.
  1219. * @param len - size of total data received.
  1220. * @param rh - Control header associated with the packet
  1221. * @param param - additional control data with the packet
  1222. * @param arg - farg registered in droq_ops
  1223. */
  1224. static void
  1225. liquidio_push_packet(u32 octeon_id __attribute__((unused)),
  1226. void *skbuff,
  1227. u32 len,
  1228. union octeon_rh *rh,
  1229. void *param,
  1230. void *arg)
  1231. {
  1232. struct napi_struct *napi = param;
  1233. struct octeon_droq *droq =
  1234. container_of(param, struct octeon_droq, napi);
  1235. struct net_device *netdev = (struct net_device *)arg;
  1236. struct sk_buff *skb = (struct sk_buff *)skbuff;
  1237. u16 vtag = 0;
  1238. u32 r_dh_off;
  1239. if (netdev) {
  1240. struct lio *lio = GET_LIO(netdev);
  1241. int packet_was_received;
  1242. /* Do not proceed if the interface is not in RUNNING state. */
  1243. if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
  1244. recv_buffer_free(skb);
  1245. droq->stats.rx_dropped++;
  1246. return;
  1247. }
  1248. skb->dev = netdev;
  1249. skb_record_rx_queue(skb, droq->q_no);
  1250. if (likely(len > MIN_SKB_SIZE)) {
  1251. struct octeon_skb_page_info *pg_info;
  1252. unsigned char *va;
  1253. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  1254. if (pg_info->page) {
  1255. /* For Paged allocation use the frags */
  1256. va = page_address(pg_info->page) +
  1257. pg_info->page_offset;
  1258. memcpy(skb->data, va, MIN_SKB_SIZE);
  1259. skb_put(skb, MIN_SKB_SIZE);
  1260. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  1261. pg_info->page,
  1262. pg_info->page_offset +
  1263. MIN_SKB_SIZE,
  1264. len - MIN_SKB_SIZE,
  1265. LIO_RXBUFFER_SZ);
  1266. }
  1267. } else {
  1268. struct octeon_skb_page_info *pg_info =
  1269. ((struct octeon_skb_page_info *)(skb->cb));
  1270. skb_copy_to_linear_data(skb,
  1271. page_address(pg_info->page) +
  1272. pg_info->page_offset, len);
  1273. skb_put(skb, len);
  1274. put_page(pg_info->page);
  1275. }
  1276. r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
  1277. if (rh->r_dh.has_hwtstamp)
  1278. r_dh_off -= BYTES_PER_DHLEN_UNIT;
  1279. if (rh->r_dh.has_hash) {
  1280. __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
  1281. u32 hash = be32_to_cpu(*hash_be);
  1282. skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
  1283. r_dh_off -= BYTES_PER_DHLEN_UNIT;
  1284. }
  1285. skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
  1286. skb->protocol = eth_type_trans(skb, skb->dev);
  1287. if ((netdev->features & NETIF_F_RXCSUM) &&
  1288. (((rh->r_dh.encap_on) &&
  1289. (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
  1290. (!(rh->r_dh.encap_on) &&
  1291. (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
  1292. /* checksum has already been verified */
  1293. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1294. else
  1295. skb->ip_summed = CHECKSUM_NONE;
  1296. /* Setting Encapsulation field on basis of status received
  1297. * from the firmware
  1298. */
  1299. if (rh->r_dh.encap_on) {
  1300. skb->encapsulation = 1;
  1301. skb->csum_level = 1;
  1302. droq->stats.rx_vxlan++;
  1303. }
  1304. /* inbound VLAN tag */
  1305. if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1306. rh->r_dh.vlan) {
  1307. u16 priority = rh->r_dh.priority;
  1308. u16 vid = rh->r_dh.vlan;
  1309. vtag = (priority << VLAN_PRIO_SHIFT) | vid;
  1310. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
  1311. }
  1312. packet_was_received = (napi_gro_receive(napi, skb) != GRO_DROP);
  1313. if (packet_was_received) {
  1314. droq->stats.rx_bytes_received += len;
  1315. droq->stats.rx_pkts_received++;
  1316. } else {
  1317. droq->stats.rx_dropped++;
  1318. netif_info(lio, rx_err, lio->netdev,
  1319. "droq:%d error rx_dropped:%llu\n",
  1320. droq->q_no, droq->stats.rx_dropped);
  1321. }
  1322. } else {
  1323. recv_buffer_free(skb);
  1324. }
  1325. }
  1326. /**
  1327. * \brief callback when receive interrupt occurs and we are in NAPI mode
  1328. * @param arg pointer to octeon output queue
  1329. */
  1330. static void liquidio_vf_napi_drv_callback(void *arg)
  1331. {
  1332. struct octeon_droq *droq = arg;
  1333. napi_schedule_irqoff(&droq->napi);
  1334. }
  1335. /**
  1336. * \brief Entry point for NAPI polling
  1337. * @param napi NAPI structure
  1338. * @param budget maximum number of items to process
  1339. */
  1340. static int liquidio_napi_poll(struct napi_struct *napi, int budget)
  1341. {
  1342. struct octeon_instr_queue *iq;
  1343. struct octeon_device *oct;
  1344. struct octeon_droq *droq;
  1345. int tx_done = 0, iq_no;
  1346. int work_done;
  1347. droq = container_of(napi, struct octeon_droq, napi);
  1348. oct = droq->oct_dev;
  1349. iq_no = droq->q_no;
  1350. /* Handle Droq descriptors */
  1351. work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
  1352. POLL_EVENT_PROCESS_PKTS,
  1353. budget);
  1354. /* Flush the instruction queue */
  1355. iq = oct->instr_queue[iq_no];
  1356. if (iq) {
  1357. if (atomic_read(&iq->instr_pending))
  1358. /* Process iq buffers with in the budget limits */
  1359. tx_done = octeon_flush_iq(oct, iq, budget);
  1360. else
  1361. tx_done = 1;
  1362. /* Update iq read-index rather than waiting for next interrupt.
  1363. * Return back if tx_done is false.
  1364. */
  1365. update_txq_status(oct, iq_no);
  1366. } else {
  1367. dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
  1368. __func__, iq_no);
  1369. }
  1370. /* force enable interrupt if reg cnts are high to avoid wraparound */
  1371. if ((work_done < budget && tx_done) ||
  1372. (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
  1373. (droq->pkt_count >= MAX_REG_CNT)) {
  1374. tx_done = 1;
  1375. napi_complete_done(napi, work_done);
  1376. octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
  1377. POLL_EVENT_ENABLE_INTR, 0);
  1378. return 0;
  1379. }
  1380. return (!tx_done) ? (budget) : (work_done);
  1381. }
  1382. /**
  1383. * \brief Setup input and output queues
  1384. * @param octeon_dev octeon device
  1385. * @param ifidx Interface index
  1386. *
  1387. * Note: Queues are with respect to the octeon device. Thus
  1388. * an input queue is for egress packets, and output queues
  1389. * are for ingress packets.
  1390. */
  1391. static int setup_io_queues(struct octeon_device *octeon_dev, int ifidx)
  1392. {
  1393. struct octeon_droq_ops droq_ops;
  1394. struct net_device *netdev;
  1395. static int cpu_id_modulus;
  1396. struct octeon_droq *droq;
  1397. struct napi_struct *napi;
  1398. static int cpu_id;
  1399. int num_tx_descs;
  1400. struct lio *lio;
  1401. int retval = 0;
  1402. int q, q_no;
  1403. netdev = octeon_dev->props[ifidx].netdev;
  1404. lio = GET_LIO(netdev);
  1405. memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
  1406. droq_ops.fptr = liquidio_push_packet;
  1407. droq_ops.farg = netdev;
  1408. droq_ops.poll_mode = 1;
  1409. droq_ops.napi_fn = liquidio_vf_napi_drv_callback;
  1410. cpu_id = 0;
  1411. cpu_id_modulus = num_present_cpus();
  1412. /* set up DROQs. */
  1413. for (q = 0; q < lio->linfo.num_rxpciq; q++) {
  1414. q_no = lio->linfo.rxpciq[q].s.q_no;
  1415. retval = octeon_setup_droq(
  1416. octeon_dev, q_no,
  1417. CFG_GET_NUM_RX_DESCS_NIC_IF(octeon_get_conf(octeon_dev),
  1418. lio->ifidx),
  1419. CFG_GET_NUM_RX_BUF_SIZE_NIC_IF(octeon_get_conf(octeon_dev),
  1420. lio->ifidx),
  1421. NULL);
  1422. if (retval) {
  1423. dev_err(&octeon_dev->pci_dev->dev,
  1424. "%s : Runtime DROQ(RxQ) creation failed.\n",
  1425. __func__);
  1426. return 1;
  1427. }
  1428. droq = octeon_dev->droq[q_no];
  1429. napi = &droq->napi;
  1430. netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
  1431. /* designate a CPU for this droq */
  1432. droq->cpu_id = cpu_id;
  1433. cpu_id++;
  1434. if (cpu_id >= cpu_id_modulus)
  1435. cpu_id = 0;
  1436. octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
  1437. }
  1438. /* 23XX VF can send/recv control messages (via the first VF-owned
  1439. * droq) from the firmware even if the ethX interface is down,
  1440. * so that's why poll_mode must be off for the first droq.
  1441. */
  1442. octeon_dev->droq[0]->ops.poll_mode = 0;
  1443. /* set up IQs. */
  1444. for (q = 0; q < lio->linfo.num_txpciq; q++) {
  1445. num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(
  1446. octeon_get_conf(octeon_dev), lio->ifidx);
  1447. retval = octeon_setup_iq(octeon_dev, ifidx, q,
  1448. lio->linfo.txpciq[q], num_tx_descs,
  1449. netdev_get_tx_queue(netdev, q));
  1450. if (retval) {
  1451. dev_err(&octeon_dev->pci_dev->dev,
  1452. " %s : Runtime IQ(TxQ) creation failed.\n",
  1453. __func__);
  1454. return 1;
  1455. }
  1456. }
  1457. return 0;
  1458. }
  1459. /**
  1460. * \brief Net device open for LiquidIO
  1461. * @param netdev network device
  1462. */
  1463. static int liquidio_open(struct net_device *netdev)
  1464. {
  1465. struct lio *lio = GET_LIO(netdev);
  1466. struct octeon_device *oct = lio->oct_dev;
  1467. struct napi_struct *napi, *n;
  1468. if (!oct->props[lio->ifidx].napi_enabled) {
  1469. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1470. napi_enable(napi);
  1471. oct->props[lio->ifidx].napi_enabled = 1;
  1472. oct->droq[0]->ops.poll_mode = 1;
  1473. }
  1474. ifstate_set(lio, LIO_IFSTATE_RUNNING);
  1475. /* Ready for link status updates */
  1476. lio->intf_open = 1;
  1477. netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
  1478. start_txq(netdev);
  1479. /* tell Octeon to start forwarding packets to host */
  1480. send_rx_ctrl_cmd(lio, 1);
  1481. dev_info(&oct->pci_dev->dev, "%s interface is opened\n", netdev->name);
  1482. return 0;
  1483. }
  1484. /**
  1485. * \brief Net device stop for LiquidIO
  1486. * @param netdev network device
  1487. */
  1488. static int liquidio_stop(struct net_device *netdev)
  1489. {
  1490. struct lio *lio = GET_LIO(netdev);
  1491. struct octeon_device *oct = lio->oct_dev;
  1492. netif_info(lio, ifdown, lio->netdev, "Stopping interface!\n");
  1493. /* Inform that netif carrier is down */
  1494. lio->intf_open = 0;
  1495. lio->linfo.link.s.link_up = 0;
  1496. netif_carrier_off(netdev);
  1497. lio->link_changes++;
  1498. /* tell Octeon to stop forwarding packets to host */
  1499. send_rx_ctrl_cmd(lio, 0);
  1500. ifstate_reset(lio, LIO_IFSTATE_RUNNING);
  1501. txqs_stop(netdev);
  1502. dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
  1503. return 0;
  1504. }
  1505. /**
  1506. * \brief Converts a mask based on net device flags
  1507. * @param netdev network device
  1508. *
  1509. * This routine generates a octnet_ifflags mask from the net device flags
  1510. * received from the OS.
  1511. */
  1512. static enum octnet_ifflags get_new_flags(struct net_device *netdev)
  1513. {
  1514. enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
  1515. if (netdev->flags & IFF_PROMISC)
  1516. f |= OCTNET_IFFLAG_PROMISC;
  1517. if (netdev->flags & IFF_ALLMULTI)
  1518. f |= OCTNET_IFFLAG_ALLMULTI;
  1519. if (netdev->flags & IFF_MULTICAST) {
  1520. f |= OCTNET_IFFLAG_MULTICAST;
  1521. /* Accept all multicast addresses if there are more than we
  1522. * can handle
  1523. */
  1524. if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
  1525. f |= OCTNET_IFFLAG_ALLMULTI;
  1526. }
  1527. if (netdev->flags & IFF_BROADCAST)
  1528. f |= OCTNET_IFFLAG_BROADCAST;
  1529. return f;
  1530. }
  1531. static void liquidio_set_uc_list(struct net_device *netdev)
  1532. {
  1533. struct lio *lio = GET_LIO(netdev);
  1534. struct octeon_device *oct = lio->oct_dev;
  1535. struct octnic_ctrl_pkt nctrl;
  1536. struct netdev_hw_addr *ha;
  1537. u64 *mac;
  1538. if (lio->netdev_uc_count == netdev_uc_count(netdev))
  1539. return;
  1540. if (netdev_uc_count(netdev) > MAX_NCTRL_UDD) {
  1541. dev_err(&oct->pci_dev->dev, "too many MAC addresses in netdev uc list\n");
  1542. return;
  1543. }
  1544. lio->netdev_uc_count = netdev_uc_count(netdev);
  1545. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1546. nctrl.ncmd.s.cmd = OCTNET_CMD_SET_UC_LIST;
  1547. nctrl.ncmd.s.more = lio->netdev_uc_count;
  1548. nctrl.ncmd.s.param1 = oct->vf_num;
  1549. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  1550. nctrl.netpndev = (u64)netdev;
  1551. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  1552. /* copy all the addresses into the udd */
  1553. mac = &nctrl.udd[0];
  1554. netdev_for_each_uc_addr(ha, netdev) {
  1555. ether_addr_copy(((u8 *)mac) + 2, ha->addr);
  1556. mac++;
  1557. }
  1558. octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  1559. }
  1560. /**
  1561. * \brief Net device set_multicast_list
  1562. * @param netdev network device
  1563. */
  1564. static void liquidio_set_mcast_list(struct net_device *netdev)
  1565. {
  1566. int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
  1567. struct lio *lio = GET_LIO(netdev);
  1568. struct octeon_device *oct = lio->oct_dev;
  1569. struct octnic_ctrl_pkt nctrl;
  1570. struct netdev_hw_addr *ha;
  1571. u64 *mc;
  1572. int ret;
  1573. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1574. /* Create a ctrl pkt command to be sent to core app. */
  1575. nctrl.ncmd.u64 = 0;
  1576. nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
  1577. nctrl.ncmd.s.param1 = get_new_flags(netdev);
  1578. nctrl.ncmd.s.param2 = mc_count;
  1579. nctrl.ncmd.s.more = mc_count;
  1580. nctrl.netpndev = (u64)netdev;
  1581. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  1582. /* copy all the addresses into the udd */
  1583. mc = &nctrl.udd[0];
  1584. netdev_for_each_mc_addr(ha, netdev) {
  1585. *mc = 0;
  1586. ether_addr_copy(((u8 *)mc) + 2, ha->addr);
  1587. /* no need to swap bytes */
  1588. if (++mc > &nctrl.udd[mc_count])
  1589. break;
  1590. }
  1591. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  1592. /* Apparently, any activity in this call from the kernel has to
  1593. * be atomic. So we won't wait for response.
  1594. */
  1595. nctrl.wait_time = 0;
  1596. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  1597. if (ret < 0) {
  1598. dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
  1599. ret);
  1600. }
  1601. liquidio_set_uc_list(netdev);
  1602. }
  1603. /**
  1604. * \brief Net device set_mac_address
  1605. * @param netdev network device
  1606. */
  1607. static int liquidio_set_mac(struct net_device *netdev, void *p)
  1608. {
  1609. struct sockaddr *addr = (struct sockaddr *)p;
  1610. struct lio *lio = GET_LIO(netdev);
  1611. struct octeon_device *oct = lio->oct_dev;
  1612. struct octnic_ctrl_pkt nctrl;
  1613. int ret = 0;
  1614. if (!is_valid_ether_addr(addr->sa_data))
  1615. return -EADDRNOTAVAIL;
  1616. if (ether_addr_equal(addr->sa_data, netdev->dev_addr))
  1617. return 0;
  1618. if (lio->linfo.macaddr_is_admin_asgnd)
  1619. return -EPERM;
  1620. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1621. nctrl.ncmd.u64 = 0;
  1622. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
  1623. nctrl.ncmd.s.param1 = 0;
  1624. nctrl.ncmd.s.more = 1;
  1625. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  1626. nctrl.netpndev = (u64)netdev;
  1627. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  1628. nctrl.wait_time = 100;
  1629. nctrl.udd[0] = 0;
  1630. /* The MAC Address is presented in network byte order. */
  1631. ether_addr_copy((u8 *)&nctrl.udd[0] + 2, addr->sa_data);
  1632. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  1633. if (ret < 0) {
  1634. dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
  1635. return -ENOMEM;
  1636. }
  1637. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1638. ether_addr_copy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data);
  1639. return 0;
  1640. }
  1641. /**
  1642. * \brief Net device get_stats
  1643. * @param netdev network device
  1644. */
  1645. static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
  1646. {
  1647. struct lio *lio = GET_LIO(netdev);
  1648. struct net_device_stats *stats = &netdev->stats;
  1649. u64 pkts = 0, drop = 0, bytes = 0;
  1650. struct oct_droq_stats *oq_stats;
  1651. struct oct_iq_stats *iq_stats;
  1652. struct octeon_device *oct;
  1653. int i, iq_no, oq_no;
  1654. oct = lio->oct_dev;
  1655. for (i = 0; i < lio->linfo.num_txpciq; i++) {
  1656. iq_no = lio->linfo.txpciq[i].s.q_no;
  1657. iq_stats = &oct->instr_queue[iq_no]->stats;
  1658. pkts += iq_stats->tx_done;
  1659. drop += iq_stats->tx_dropped;
  1660. bytes += iq_stats->tx_tot_bytes;
  1661. }
  1662. stats->tx_packets = pkts;
  1663. stats->tx_bytes = bytes;
  1664. stats->tx_dropped = drop;
  1665. pkts = 0;
  1666. drop = 0;
  1667. bytes = 0;
  1668. for (i = 0; i < lio->linfo.num_rxpciq; i++) {
  1669. oq_no = lio->linfo.rxpciq[i].s.q_no;
  1670. oq_stats = &oct->droq[oq_no]->stats;
  1671. pkts += oq_stats->rx_pkts_received;
  1672. drop += (oq_stats->rx_dropped +
  1673. oq_stats->dropped_nodispatch +
  1674. oq_stats->dropped_toomany +
  1675. oq_stats->dropped_nomem);
  1676. bytes += oq_stats->rx_bytes_received;
  1677. }
  1678. stats->rx_bytes = bytes;
  1679. stats->rx_packets = pkts;
  1680. stats->rx_dropped = drop;
  1681. return stats;
  1682. }
  1683. /**
  1684. * \brief Net device change_mtu
  1685. * @param netdev network device
  1686. */
  1687. static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
  1688. {
  1689. struct lio *lio = GET_LIO(netdev);
  1690. struct octeon_device *oct = lio->oct_dev;
  1691. lio->mtu = new_mtu;
  1692. netif_info(lio, probe, lio->netdev, "MTU Changed from %d to %d\n",
  1693. netdev->mtu, new_mtu);
  1694. dev_info(&oct->pci_dev->dev, "%s MTU Changed from %d to %d\n",
  1695. netdev->name, netdev->mtu, new_mtu);
  1696. netdev->mtu = new_mtu;
  1697. return 0;
  1698. }
  1699. /**
  1700. * \brief Handler for SIOCSHWTSTAMP ioctl
  1701. * @param netdev network device
  1702. * @param ifr interface request
  1703. * @param cmd command
  1704. */
  1705. static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
  1706. {
  1707. struct lio *lio = GET_LIO(netdev);
  1708. struct hwtstamp_config conf;
  1709. if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
  1710. return -EFAULT;
  1711. if (conf.flags)
  1712. return -EINVAL;
  1713. switch (conf.tx_type) {
  1714. case HWTSTAMP_TX_ON:
  1715. case HWTSTAMP_TX_OFF:
  1716. break;
  1717. default:
  1718. return -ERANGE;
  1719. }
  1720. switch (conf.rx_filter) {
  1721. case HWTSTAMP_FILTER_NONE:
  1722. break;
  1723. case HWTSTAMP_FILTER_ALL:
  1724. case HWTSTAMP_FILTER_SOME:
  1725. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1726. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1727. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1728. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1729. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1730. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1731. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1732. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1733. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1734. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1735. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1736. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1737. conf.rx_filter = HWTSTAMP_FILTER_ALL;
  1738. break;
  1739. default:
  1740. return -ERANGE;
  1741. }
  1742. if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
  1743. ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  1744. else
  1745. ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  1746. return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
  1747. }
  1748. /**
  1749. * \brief ioctl handler
  1750. * @param netdev network device
  1751. * @param ifr interface request
  1752. * @param cmd command
  1753. */
  1754. static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1755. {
  1756. switch (cmd) {
  1757. case SIOCSHWTSTAMP:
  1758. return hwtstamp_ioctl(netdev, ifr);
  1759. default:
  1760. return -EOPNOTSUPP;
  1761. }
  1762. }
  1763. static void handle_timestamp(struct octeon_device *oct, u32 status, void *buf)
  1764. {
  1765. struct sk_buff *skb = (struct sk_buff *)buf;
  1766. struct octnet_buf_free_info *finfo;
  1767. struct oct_timestamp_resp *resp;
  1768. struct octeon_soft_command *sc;
  1769. struct lio *lio;
  1770. finfo = (struct octnet_buf_free_info *)skb->cb;
  1771. lio = finfo->lio;
  1772. sc = finfo->sc;
  1773. oct = lio->oct_dev;
  1774. resp = (struct oct_timestamp_resp *)sc->virtrptr;
  1775. if (status != OCTEON_REQUEST_DONE) {
  1776. dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
  1777. CVM_CAST64(status));
  1778. resp->timestamp = 0;
  1779. }
  1780. octeon_swap_8B_data(&resp->timestamp, 1);
  1781. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
  1782. struct skb_shared_hwtstamps ts;
  1783. u64 ns = resp->timestamp;
  1784. netif_info(lio, tx_done, lio->netdev,
  1785. "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
  1786. skb, (unsigned long long)ns);
  1787. ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
  1788. skb_tstamp_tx(skb, &ts);
  1789. }
  1790. octeon_free_soft_command(oct, sc);
  1791. tx_buffer_free(skb);
  1792. }
  1793. /* \brief Send a data packet that will be timestamped
  1794. * @param oct octeon device
  1795. * @param ndata pointer to network data
  1796. * @param finfo pointer to private network data
  1797. */
  1798. static int send_nic_timestamp_pkt(struct octeon_device *oct,
  1799. struct octnic_data_pkt *ndata,
  1800. struct octnet_buf_free_info *finfo)
  1801. {
  1802. struct octeon_soft_command *sc;
  1803. int ring_doorbell;
  1804. struct lio *lio;
  1805. int retval;
  1806. u32 len;
  1807. lio = finfo->lio;
  1808. sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
  1809. sizeof(struct oct_timestamp_resp));
  1810. finfo->sc = sc;
  1811. if (!sc) {
  1812. dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
  1813. return IQ_SEND_FAILED;
  1814. }
  1815. if (ndata->reqtype == REQTYPE_NORESP_NET)
  1816. ndata->reqtype = REQTYPE_RESP_NET;
  1817. else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
  1818. ndata->reqtype = REQTYPE_RESP_NET_SG;
  1819. sc->callback = handle_timestamp;
  1820. sc->callback_arg = finfo->skb;
  1821. sc->iq_no = ndata->q_no;
  1822. len = (u32)((struct octeon_instr_ih3 *)(&sc->cmd.cmd3.ih3))->dlengsz;
  1823. ring_doorbell = 1;
  1824. retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
  1825. sc, len, ndata->reqtype);
  1826. if (retval == IQ_SEND_FAILED) {
  1827. dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
  1828. retval);
  1829. octeon_free_soft_command(oct, sc);
  1830. } else {
  1831. netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
  1832. }
  1833. return retval;
  1834. }
  1835. /** \brief Transmit networks packets to the Octeon interface
  1836. * @param skbuff skbuff struct to be passed to network layer.
  1837. * @param netdev pointer to network device
  1838. * @returns whether the packet was transmitted to the device okay or not
  1839. * (NETDEV_TX_OK or NETDEV_TX_BUSY)
  1840. */
  1841. static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
  1842. {
  1843. struct octnet_buf_free_info *finfo;
  1844. union octnic_cmd_setup cmdsetup;
  1845. struct octnic_data_pkt ndata;
  1846. struct octeon_instr_irh *irh;
  1847. struct oct_iq_stats *stats;
  1848. struct octeon_device *oct;
  1849. int q_idx = 0, iq_no = 0;
  1850. union tx_info *tx_info;
  1851. struct lio *lio;
  1852. int status = 0;
  1853. u64 dptr = 0;
  1854. u32 tag = 0;
  1855. int j;
  1856. lio = GET_LIO(netdev);
  1857. oct = lio->oct_dev;
  1858. if (netif_is_multiqueue(netdev)) {
  1859. q_idx = skb->queue_mapping;
  1860. q_idx = (q_idx % (lio->linfo.num_txpciq));
  1861. tag = q_idx;
  1862. iq_no = lio->linfo.txpciq[q_idx].s.q_no;
  1863. } else {
  1864. iq_no = lio->txq;
  1865. }
  1866. stats = &oct->instr_queue[iq_no]->stats;
  1867. /* Check for all conditions in which the current packet cannot be
  1868. * transmitted.
  1869. */
  1870. if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
  1871. (!lio->linfo.link.s.link_up) || (skb->len <= 0)) {
  1872. netif_info(lio, tx_err, lio->netdev, "Transmit failed link_status : %d\n",
  1873. lio->linfo.link.s.link_up);
  1874. goto lio_xmit_failed;
  1875. }
  1876. /* Use space in skb->cb to store info used to unmap and
  1877. * free the buffers.
  1878. */
  1879. finfo = (struct octnet_buf_free_info *)skb->cb;
  1880. finfo->lio = lio;
  1881. finfo->skb = skb;
  1882. finfo->sc = NULL;
  1883. /* Prepare the attributes for the data to be passed to OSI. */
  1884. memset(&ndata, 0, sizeof(struct octnic_data_pkt));
  1885. ndata.buf = finfo;
  1886. ndata.q_no = iq_no;
  1887. if (netif_is_multiqueue(netdev)) {
  1888. if (octnet_iq_is_full(oct, ndata.q_no)) {
  1889. /* defer sending if queue is full */
  1890. netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
  1891. ndata.q_no);
  1892. stats->tx_iq_busy++;
  1893. return NETDEV_TX_BUSY;
  1894. }
  1895. } else {
  1896. if (octnet_iq_is_full(oct, lio->txq)) {
  1897. /* defer sending if queue is full */
  1898. stats->tx_iq_busy++;
  1899. netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
  1900. ndata.q_no);
  1901. return NETDEV_TX_BUSY;
  1902. }
  1903. }
  1904. ndata.datasize = skb->len;
  1905. cmdsetup.u64 = 0;
  1906. cmdsetup.s.iq_no = iq_no;
  1907. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1908. if (skb->encapsulation) {
  1909. cmdsetup.s.tnl_csum = 1;
  1910. stats->tx_vxlan++;
  1911. } else {
  1912. cmdsetup.s.transport_csum = 1;
  1913. }
  1914. }
  1915. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  1916. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1917. cmdsetup.s.timestamp = 1;
  1918. }
  1919. if (!skb_shinfo(skb)->nr_frags) {
  1920. cmdsetup.s.u.datasize = skb->len;
  1921. octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
  1922. /* Offload checksum calculation for TCP/UDP packets */
  1923. dptr = dma_map_single(&oct->pci_dev->dev,
  1924. skb->data,
  1925. skb->len,
  1926. DMA_TO_DEVICE);
  1927. if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
  1928. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
  1929. __func__);
  1930. return NETDEV_TX_BUSY;
  1931. }
  1932. ndata.cmd.cmd3.dptr = dptr;
  1933. finfo->dptr = dptr;
  1934. ndata.reqtype = REQTYPE_NORESP_NET;
  1935. } else {
  1936. struct skb_frag_struct *frag;
  1937. struct octnic_gather *g;
  1938. int i, frags;
  1939. spin_lock(&lio->glist_lock[q_idx]);
  1940. g = (struct octnic_gather *)list_delete_head(
  1941. &lio->glist[q_idx]);
  1942. spin_unlock(&lio->glist_lock[q_idx]);
  1943. if (!g) {
  1944. netif_info(lio, tx_err, lio->netdev,
  1945. "Transmit scatter gather: glist null!\n");
  1946. goto lio_xmit_failed;
  1947. }
  1948. cmdsetup.s.gather = 1;
  1949. cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
  1950. octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
  1951. memset(g->sg, 0, g->sg_size);
  1952. g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
  1953. skb->data,
  1954. (skb->len - skb->data_len),
  1955. DMA_TO_DEVICE);
  1956. if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
  1957. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
  1958. __func__);
  1959. return NETDEV_TX_BUSY;
  1960. }
  1961. add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
  1962. frags = skb_shinfo(skb)->nr_frags;
  1963. i = 1;
  1964. while (frags--) {
  1965. frag = &skb_shinfo(skb)->frags[i - 1];
  1966. g->sg[(i >> 2)].ptr[(i & 3)] =
  1967. dma_map_page(&oct->pci_dev->dev,
  1968. frag->page.p,
  1969. frag->page_offset,
  1970. frag->size,
  1971. DMA_TO_DEVICE);
  1972. if (dma_mapping_error(&oct->pci_dev->dev,
  1973. g->sg[i >> 2].ptr[i & 3])) {
  1974. dma_unmap_single(&oct->pci_dev->dev,
  1975. g->sg[0].ptr[0],
  1976. skb->len - skb->data_len,
  1977. DMA_TO_DEVICE);
  1978. for (j = 1; j < i; j++) {
  1979. frag = &skb_shinfo(skb)->frags[j - 1];
  1980. dma_unmap_page(&oct->pci_dev->dev,
  1981. g->sg[j >> 2].ptr[j & 3],
  1982. frag->size,
  1983. DMA_TO_DEVICE);
  1984. }
  1985. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
  1986. __func__);
  1987. return NETDEV_TX_BUSY;
  1988. }
  1989. add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
  1990. i++;
  1991. }
  1992. dptr = g->sg_dma_ptr;
  1993. ndata.cmd.cmd3.dptr = dptr;
  1994. finfo->dptr = dptr;
  1995. finfo->g = g;
  1996. ndata.reqtype = REQTYPE_NORESP_NET_SG;
  1997. }
  1998. irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
  1999. tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
  2000. if (skb_shinfo(skb)->gso_size) {
  2001. tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
  2002. tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
  2003. }
  2004. /* HW insert VLAN tag */
  2005. if (skb_vlan_tag_present(skb)) {
  2006. irh->priority = skb_vlan_tag_get(skb) >> VLAN_PRIO_SHIFT;
  2007. irh->vlan = skb_vlan_tag_get(skb) & VLAN_VID_MASK;
  2008. }
  2009. if (unlikely(cmdsetup.s.timestamp))
  2010. status = send_nic_timestamp_pkt(oct, &ndata, finfo);
  2011. else
  2012. status = octnet_send_nic_data_pkt(oct, &ndata);
  2013. if (status == IQ_SEND_FAILED)
  2014. goto lio_xmit_failed;
  2015. netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
  2016. if (status == IQ_SEND_STOP) {
  2017. dev_err(&oct->pci_dev->dev, "Rcvd IQ_SEND_STOP signal; stopping IQ-%d\n",
  2018. iq_no);
  2019. stop_q(lio->netdev, q_idx);
  2020. }
  2021. netif_trans_update(netdev);
  2022. if (tx_info->s.gso_segs)
  2023. stats->tx_done += tx_info->s.gso_segs;
  2024. else
  2025. stats->tx_done++;
  2026. stats->tx_tot_bytes += ndata.datasize;
  2027. return NETDEV_TX_OK;
  2028. lio_xmit_failed:
  2029. stats->tx_dropped++;
  2030. netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
  2031. iq_no, stats->tx_dropped);
  2032. if (dptr)
  2033. dma_unmap_single(&oct->pci_dev->dev, dptr,
  2034. ndata.datasize, DMA_TO_DEVICE);
  2035. tx_buffer_free(skb);
  2036. return NETDEV_TX_OK;
  2037. }
  2038. /** \brief Network device Tx timeout
  2039. * @param netdev pointer to network device
  2040. */
  2041. static void liquidio_tx_timeout(struct net_device *netdev)
  2042. {
  2043. struct lio *lio;
  2044. lio = GET_LIO(netdev);
  2045. netif_info(lio, tx_err, lio->netdev,
  2046. "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
  2047. netdev->stats.tx_dropped);
  2048. netif_trans_update(netdev);
  2049. txqs_wake(netdev);
  2050. }
  2051. static int
  2052. liquidio_vlan_rx_add_vid(struct net_device *netdev,
  2053. __be16 proto __attribute__((unused)), u16 vid)
  2054. {
  2055. struct lio *lio = GET_LIO(netdev);
  2056. struct octeon_device *oct = lio->oct_dev;
  2057. struct octnic_ctrl_pkt nctrl;
  2058. struct completion compl;
  2059. u16 response_code;
  2060. int ret = 0;
  2061. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2062. nctrl.ncmd.u64 = 0;
  2063. nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
  2064. nctrl.ncmd.s.param1 = vid;
  2065. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2066. nctrl.wait_time = 100;
  2067. nctrl.netpndev = (u64)netdev;
  2068. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2069. init_completion(&compl);
  2070. nctrl.completion = &compl;
  2071. nctrl.response_code = &response_code;
  2072. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2073. if (ret < 0) {
  2074. dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
  2075. ret);
  2076. return -EIO;
  2077. }
  2078. if (!wait_for_completion_timeout(&compl,
  2079. msecs_to_jiffies(nctrl.wait_time)))
  2080. return -EPERM;
  2081. if (READ_ONCE(response_code))
  2082. return -EPERM;
  2083. return 0;
  2084. }
  2085. static int
  2086. liquidio_vlan_rx_kill_vid(struct net_device *netdev,
  2087. __be16 proto __attribute__((unused)), u16 vid)
  2088. {
  2089. struct lio *lio = GET_LIO(netdev);
  2090. struct octeon_device *oct = lio->oct_dev;
  2091. struct octnic_ctrl_pkt nctrl;
  2092. int ret = 0;
  2093. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2094. nctrl.ncmd.u64 = 0;
  2095. nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
  2096. nctrl.ncmd.s.param1 = vid;
  2097. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2098. nctrl.wait_time = 100;
  2099. nctrl.netpndev = (u64)netdev;
  2100. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2101. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2102. if (ret < 0) {
  2103. dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
  2104. ret);
  2105. }
  2106. return ret;
  2107. }
  2108. /** Sending command to enable/disable RX checksum offload
  2109. * @param netdev pointer to network device
  2110. * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
  2111. * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
  2112. * OCTNET_CMD_RXCSUM_DISABLE
  2113. * @returns SUCCESS or FAILURE
  2114. */
  2115. static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
  2116. u8 rx_cmd)
  2117. {
  2118. struct lio *lio = GET_LIO(netdev);
  2119. struct octeon_device *oct = lio->oct_dev;
  2120. struct octnic_ctrl_pkt nctrl;
  2121. int ret = 0;
  2122. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2123. nctrl.ncmd.u64 = 0;
  2124. nctrl.ncmd.s.cmd = command;
  2125. nctrl.ncmd.s.param1 = rx_cmd;
  2126. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2127. nctrl.wait_time = 100;
  2128. nctrl.netpndev = (u64)netdev;
  2129. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2130. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2131. if (ret < 0) {
  2132. dev_err(&oct->pci_dev->dev, "DEVFLAGS RXCSUM change failed in core (ret:0x%x)\n",
  2133. ret);
  2134. }
  2135. return ret;
  2136. }
  2137. /** Sending command to add/delete VxLAN UDP port to firmware
  2138. * @param netdev pointer to network device
  2139. * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
  2140. * @param vxlan_port VxLAN port to be added or deleted
  2141. * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
  2142. * OCTNET_CMD_VXLAN_PORT_DEL
  2143. * @returns SUCCESS or FAILURE
  2144. */
  2145. static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
  2146. u16 vxlan_port, u8 vxlan_cmd_bit)
  2147. {
  2148. struct lio *lio = GET_LIO(netdev);
  2149. struct octeon_device *oct = lio->oct_dev;
  2150. struct octnic_ctrl_pkt nctrl;
  2151. int ret = 0;
  2152. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2153. nctrl.ncmd.u64 = 0;
  2154. nctrl.ncmd.s.cmd = command;
  2155. nctrl.ncmd.s.more = vxlan_cmd_bit;
  2156. nctrl.ncmd.s.param1 = vxlan_port;
  2157. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2158. nctrl.wait_time = 100;
  2159. nctrl.netpndev = (u64)netdev;
  2160. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2161. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2162. if (ret < 0) {
  2163. dev_err(&oct->pci_dev->dev,
  2164. "DEVFLAGS VxLAN port add/delete failed in core (ret : 0x%x)\n",
  2165. ret);
  2166. }
  2167. return ret;
  2168. }
  2169. /** \brief Net device fix features
  2170. * @param netdev pointer to network device
  2171. * @param request features requested
  2172. * @returns updated features list
  2173. */
  2174. static netdev_features_t liquidio_fix_features(struct net_device *netdev,
  2175. netdev_features_t request)
  2176. {
  2177. struct lio *lio = netdev_priv(netdev);
  2178. if ((request & NETIF_F_RXCSUM) &&
  2179. !(lio->dev_capability & NETIF_F_RXCSUM))
  2180. request &= ~NETIF_F_RXCSUM;
  2181. if ((request & NETIF_F_HW_CSUM) &&
  2182. !(lio->dev_capability & NETIF_F_HW_CSUM))
  2183. request &= ~NETIF_F_HW_CSUM;
  2184. if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
  2185. request &= ~NETIF_F_TSO;
  2186. if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
  2187. request &= ~NETIF_F_TSO6;
  2188. if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
  2189. request &= ~NETIF_F_LRO;
  2190. /* Disable LRO if RXCSUM is off */
  2191. if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
  2192. (lio->dev_capability & NETIF_F_LRO))
  2193. request &= ~NETIF_F_LRO;
  2194. return request;
  2195. }
  2196. /** \brief Net device set features
  2197. * @param netdev pointer to network device
  2198. * @param features features to enable/disable
  2199. */
  2200. static int liquidio_set_features(struct net_device *netdev,
  2201. netdev_features_t features)
  2202. {
  2203. struct lio *lio = netdev_priv(netdev);
  2204. if (!((netdev->features ^ features) & NETIF_F_LRO))
  2205. return 0;
  2206. if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
  2207. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
  2208. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  2209. else if (!(features & NETIF_F_LRO) &&
  2210. (lio->dev_capability & NETIF_F_LRO))
  2211. liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
  2212. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  2213. if (!(netdev->features & NETIF_F_RXCSUM) &&
  2214. (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
  2215. (features & NETIF_F_RXCSUM))
  2216. liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
  2217. OCTNET_CMD_RXCSUM_ENABLE);
  2218. else if ((netdev->features & NETIF_F_RXCSUM) &&
  2219. (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
  2220. !(features & NETIF_F_RXCSUM))
  2221. liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
  2222. OCTNET_CMD_RXCSUM_DISABLE);
  2223. return 0;
  2224. }
  2225. static void liquidio_add_vxlan_port(struct net_device *netdev,
  2226. struct udp_tunnel_info *ti)
  2227. {
  2228. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2229. return;
  2230. liquidio_vxlan_port_command(netdev,
  2231. OCTNET_CMD_VXLAN_PORT_CONFIG,
  2232. htons(ti->port),
  2233. OCTNET_CMD_VXLAN_PORT_ADD);
  2234. }
  2235. static void liquidio_del_vxlan_port(struct net_device *netdev,
  2236. struct udp_tunnel_info *ti)
  2237. {
  2238. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2239. return;
  2240. liquidio_vxlan_port_command(netdev,
  2241. OCTNET_CMD_VXLAN_PORT_CONFIG,
  2242. htons(ti->port),
  2243. OCTNET_CMD_VXLAN_PORT_DEL);
  2244. }
  2245. static const struct net_device_ops lionetdevops = {
  2246. .ndo_open = liquidio_open,
  2247. .ndo_stop = liquidio_stop,
  2248. .ndo_start_xmit = liquidio_xmit,
  2249. .ndo_get_stats = liquidio_get_stats,
  2250. .ndo_set_mac_address = liquidio_set_mac,
  2251. .ndo_set_rx_mode = liquidio_set_mcast_list,
  2252. .ndo_tx_timeout = liquidio_tx_timeout,
  2253. .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
  2254. .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
  2255. .ndo_change_mtu = liquidio_change_mtu,
  2256. .ndo_do_ioctl = liquidio_ioctl,
  2257. .ndo_fix_features = liquidio_fix_features,
  2258. .ndo_set_features = liquidio_set_features,
  2259. .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
  2260. .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
  2261. };
  2262. static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
  2263. {
  2264. struct octeon_device *oct = (struct octeon_device *)buf;
  2265. struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
  2266. union oct_link_status *ls;
  2267. int gmxport = 0;
  2268. int i;
  2269. if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
  2270. dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
  2271. recv_pkt->buffer_size[0],
  2272. recv_pkt->rh.r_nic_info.gmxport);
  2273. goto nic_info_err;
  2274. }
  2275. gmxport = recv_pkt->rh.r_nic_info.gmxport;
  2276. ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
  2277. octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
  2278. for (i = 0; i < oct->ifcount; i++) {
  2279. if (oct->props[i].gmxport == gmxport) {
  2280. update_link_status(oct->props[i].netdev, ls);
  2281. break;
  2282. }
  2283. }
  2284. nic_info_err:
  2285. for (i = 0; i < recv_pkt->buffer_count; i++)
  2286. recv_buffer_free(recv_pkt->buffer_ptr[i]);
  2287. octeon_free_recv_info(recv_info);
  2288. return 0;
  2289. }
  2290. /**
  2291. * \brief Setup network interfaces
  2292. * @param octeon_dev octeon device
  2293. *
  2294. * Called during init time for each device. It assumes the NIC
  2295. * is already up and running. The link information for each
  2296. * interface is passed in link_info.
  2297. */
  2298. static int setup_nic_devices(struct octeon_device *octeon_dev)
  2299. {
  2300. int retval, num_iqueues, num_oqueues;
  2301. struct liquidio_if_cfg_context *ctx;
  2302. u32 resp_size, ctx_size, data_size;
  2303. struct liquidio_if_cfg_resp *resp;
  2304. struct octeon_soft_command *sc;
  2305. union oct_nic_if_cfg if_cfg;
  2306. struct octdev_props *props;
  2307. struct net_device *netdev;
  2308. struct lio_version *vdata;
  2309. struct lio *lio = NULL;
  2310. u8 mac[ETH_ALEN], i, j;
  2311. u32 ifidx_or_pfnum;
  2312. ifidx_or_pfnum = octeon_dev->pf_num;
  2313. /* This is to handle link status changes */
  2314. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC, OPCODE_NIC_INFO,
  2315. lio_nic_info, octeon_dev);
  2316. /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
  2317. * They are handled directly.
  2318. */
  2319. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
  2320. free_netbuf);
  2321. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
  2322. free_netsgbuf);
  2323. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
  2324. free_netsgbuf_with_resp);
  2325. for (i = 0; i < octeon_dev->ifcount; i++) {
  2326. resp_size = sizeof(struct liquidio_if_cfg_resp);
  2327. ctx_size = sizeof(struct liquidio_if_cfg_context);
  2328. data_size = sizeof(struct lio_version);
  2329. sc = (struct octeon_soft_command *)
  2330. octeon_alloc_soft_command(octeon_dev, data_size,
  2331. resp_size, ctx_size);
  2332. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  2333. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  2334. vdata = (struct lio_version *)sc->virtdptr;
  2335. *((u64 *)vdata) = 0;
  2336. vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
  2337. vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
  2338. vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
  2339. WRITE_ONCE(ctx->cond, 0);
  2340. ctx->octeon_id = lio_get_device_id(octeon_dev);
  2341. init_waitqueue_head(&ctx->wc);
  2342. if_cfg.u64 = 0;
  2343. if_cfg.s.num_iqueues = octeon_dev->sriov_info.rings_per_vf;
  2344. if_cfg.s.num_oqueues = octeon_dev->sriov_info.rings_per_vf;
  2345. if_cfg.s.base_queue = 0;
  2346. sc->iq_no = 0;
  2347. octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
  2348. OPCODE_NIC_IF_CFG, 0, if_cfg.u64,
  2349. 0);
  2350. sc->callback = if_cfg_callback;
  2351. sc->callback_arg = sc;
  2352. sc->wait_time = 5000;
  2353. retval = octeon_send_soft_command(octeon_dev, sc);
  2354. if (retval == IQ_SEND_FAILED) {
  2355. dev_err(&octeon_dev->pci_dev->dev,
  2356. "iq/oq config failed status: %x\n", retval);
  2357. /* Soft instr is freed by driver in case of failure. */
  2358. goto setup_nic_dev_fail;
  2359. }
  2360. /* Sleep on a wait queue till the cond flag indicates that the
  2361. * response arrived or timed-out.
  2362. */
  2363. if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
  2364. dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
  2365. goto setup_nic_wait_intr;
  2366. }
  2367. retval = resp->status;
  2368. if (retval) {
  2369. dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
  2370. goto setup_nic_dev_fail;
  2371. }
  2372. octeon_swap_8B_data((u64 *)(&resp->cfg_info),
  2373. (sizeof(struct liquidio_if_cfg_info)) >> 3);
  2374. num_iqueues = hweight64(resp->cfg_info.iqmask);
  2375. num_oqueues = hweight64(resp->cfg_info.oqmask);
  2376. if (!(num_iqueues) || !(num_oqueues)) {
  2377. dev_err(&octeon_dev->pci_dev->dev,
  2378. "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
  2379. resp->cfg_info.iqmask, resp->cfg_info.oqmask);
  2380. goto setup_nic_dev_fail;
  2381. }
  2382. dev_dbg(&octeon_dev->pci_dev->dev,
  2383. "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
  2384. i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
  2385. num_iqueues, num_oqueues);
  2386. netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
  2387. if (!netdev) {
  2388. dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
  2389. goto setup_nic_dev_fail;
  2390. }
  2391. SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
  2392. /* Associate the routines that will handle different
  2393. * netdev tasks.
  2394. */
  2395. netdev->netdev_ops = &lionetdevops;
  2396. lio = GET_LIO(netdev);
  2397. memset(lio, 0, sizeof(struct lio));
  2398. lio->ifidx = ifidx_or_pfnum;
  2399. props = &octeon_dev->props[i];
  2400. props->gmxport = resp->cfg_info.linfo.gmxport;
  2401. props->netdev = netdev;
  2402. lio->linfo.num_rxpciq = num_oqueues;
  2403. lio->linfo.num_txpciq = num_iqueues;
  2404. for (j = 0; j < num_oqueues; j++) {
  2405. lio->linfo.rxpciq[j].u64 =
  2406. resp->cfg_info.linfo.rxpciq[j].u64;
  2407. }
  2408. for (j = 0; j < num_iqueues; j++) {
  2409. lio->linfo.txpciq[j].u64 =
  2410. resp->cfg_info.linfo.txpciq[j].u64;
  2411. }
  2412. lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
  2413. lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
  2414. lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
  2415. lio->linfo.macaddr_is_admin_asgnd =
  2416. resp->cfg_info.linfo.macaddr_is_admin_asgnd;
  2417. lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2418. lio->dev_capability = NETIF_F_HIGHDMA
  2419. | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  2420. | NETIF_F_SG | NETIF_F_RXCSUM
  2421. | NETIF_F_TSO | NETIF_F_TSO6
  2422. | NETIF_F_GRO
  2423. | NETIF_F_LRO;
  2424. netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
  2425. /* Copy of transmit encapsulation capabilities:
  2426. * TSO, TSO6, Checksums for this device
  2427. */
  2428. lio->enc_dev_capability = NETIF_F_IP_CSUM
  2429. | NETIF_F_IPV6_CSUM
  2430. | NETIF_F_GSO_UDP_TUNNEL
  2431. | NETIF_F_HW_CSUM | NETIF_F_SG
  2432. | NETIF_F_RXCSUM
  2433. | NETIF_F_TSO | NETIF_F_TSO6
  2434. | NETIF_F_LRO;
  2435. netdev->hw_enc_features =
  2436. (lio->enc_dev_capability & ~NETIF_F_LRO);
  2437. netdev->vlan_features = lio->dev_capability;
  2438. /* Add any unchangeable hw features */
  2439. lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
  2440. NETIF_F_HW_VLAN_CTAG_RX |
  2441. NETIF_F_HW_VLAN_CTAG_TX;
  2442. netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
  2443. netdev->hw_features = lio->dev_capability;
  2444. /* MTU range: 68 - 16000 */
  2445. netdev->min_mtu = LIO_MIN_MTU_SIZE;
  2446. netdev->max_mtu = LIO_MAX_MTU_SIZE;
  2447. /* Point to the properties for octeon device to which this
  2448. * interface belongs.
  2449. */
  2450. lio->oct_dev = octeon_dev;
  2451. lio->octprops = props;
  2452. lio->netdev = netdev;
  2453. dev_dbg(&octeon_dev->pci_dev->dev,
  2454. "if%d gmx: %d hw_addr: 0x%llx\n", i,
  2455. lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
  2456. /* 64-bit swap required on LE machines */
  2457. octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
  2458. for (j = 0; j < ETH_ALEN; j++)
  2459. mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
  2460. /* Copy MAC Address to OS network device structure */
  2461. ether_addr_copy(netdev->dev_addr, mac);
  2462. if (setup_io_queues(octeon_dev, i)) {
  2463. dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
  2464. goto setup_nic_dev_fail;
  2465. }
  2466. ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
  2467. /* For VFs, enable Octeon device interrupts here,
  2468. * as this is contingent upon IO queue setup
  2469. */
  2470. octeon_dev->fn_list.enable_interrupt(octeon_dev,
  2471. OCTEON_ALL_INTR);
  2472. /* By default all interfaces on a single Octeon uses the same
  2473. * tx and rx queues
  2474. */
  2475. lio->txq = lio->linfo.txpciq[0].s.q_no;
  2476. lio->rxq = lio->linfo.rxpciq[0].s.q_no;
  2477. lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
  2478. lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
  2479. if (setup_glists(lio, num_iqueues)) {
  2480. dev_err(&octeon_dev->pci_dev->dev,
  2481. "Gather list allocation failed\n");
  2482. goto setup_nic_dev_fail;
  2483. }
  2484. /* Register ethtool support */
  2485. liquidio_set_ethtool_ops(netdev);
  2486. if (lio->oct_dev->chip_id == OCTEON_CN23XX_VF_VID)
  2487. octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
  2488. else
  2489. octeon_dev->priv_flags = 0x0;
  2490. if (netdev->features & NETIF_F_LRO)
  2491. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
  2492. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  2493. if ((debug != -1) && (debug & NETIF_MSG_HW))
  2494. liquidio_set_feature(netdev, OCTNET_CMD_VERBOSE_ENABLE,
  2495. 0);
  2496. if (setup_link_status_change_wq(netdev))
  2497. goto setup_nic_dev_fail;
  2498. if (setup_rx_oom_poll_fn(netdev))
  2499. goto setup_nic_dev_fail;
  2500. /* Register the network device with the OS */
  2501. if (register_netdev(netdev)) {
  2502. dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
  2503. goto setup_nic_dev_fail;
  2504. }
  2505. dev_dbg(&octeon_dev->pci_dev->dev,
  2506. "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
  2507. i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  2508. netif_carrier_off(netdev);
  2509. lio->link_changes++;
  2510. ifstate_set(lio, LIO_IFSTATE_REGISTERED);
  2511. /* Sending command to firmware to enable Rx checksum offload
  2512. * by default at the time of setup of Liquidio driver for
  2513. * this device
  2514. */
  2515. liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
  2516. OCTNET_CMD_RXCSUM_ENABLE);
  2517. liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
  2518. OCTNET_CMD_TXCSUM_ENABLE);
  2519. dev_dbg(&octeon_dev->pci_dev->dev,
  2520. "NIC ifidx:%d Setup successful\n", i);
  2521. octeon_free_soft_command(octeon_dev, sc);
  2522. }
  2523. return 0;
  2524. setup_nic_dev_fail:
  2525. octeon_free_soft_command(octeon_dev, sc);
  2526. setup_nic_wait_intr:
  2527. while (i--) {
  2528. dev_err(&octeon_dev->pci_dev->dev,
  2529. "NIC ifidx:%d Setup failed\n", i);
  2530. liquidio_destroy_nic_device(octeon_dev, i);
  2531. }
  2532. return -ENODEV;
  2533. }
  2534. /**
  2535. * \brief initialize the NIC
  2536. * @param oct octeon device
  2537. *
  2538. * This initialization routine is called once the Octeon device application is
  2539. * up and running
  2540. */
  2541. static int liquidio_init_nic_module(struct octeon_device *oct)
  2542. {
  2543. int num_nic_ports = 1;
  2544. int i, retval = 0;
  2545. dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
  2546. /* only default iq and oq were initialized
  2547. * initialize the rest as well run port_config command for each port
  2548. */
  2549. oct->ifcount = num_nic_ports;
  2550. memset(oct->props, 0,
  2551. sizeof(struct octdev_props) * num_nic_ports);
  2552. for (i = 0; i < MAX_OCTEON_LINKS; i++)
  2553. oct->props[i].gmxport = -1;
  2554. retval = setup_nic_devices(oct);
  2555. if (retval) {
  2556. dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
  2557. goto octnet_init_failure;
  2558. }
  2559. dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
  2560. return retval;
  2561. octnet_init_failure:
  2562. oct->ifcount = 0;
  2563. return retval;
  2564. }
  2565. /**
  2566. * \brief Device initialization for each Octeon device that is probed
  2567. * @param octeon_dev octeon device
  2568. */
  2569. static int octeon_device_init(struct octeon_device *oct)
  2570. {
  2571. u32 rev_id;
  2572. int j;
  2573. atomic_set(&oct->status, OCT_DEV_BEGIN_STATE);
  2574. /* Enable access to the octeon device and make its DMA capability
  2575. * known to the OS.
  2576. */
  2577. if (octeon_pci_os_setup(oct))
  2578. return 1;
  2579. atomic_set(&oct->status, OCT_DEV_PCI_ENABLE_DONE);
  2580. oct->chip_id = OCTEON_CN23XX_VF_VID;
  2581. pci_read_config_dword(oct->pci_dev, 8, &rev_id);
  2582. oct->rev_id = rev_id & 0xff;
  2583. if (cn23xx_setup_octeon_vf_device(oct))
  2584. return 1;
  2585. atomic_set(&oct->status, OCT_DEV_PCI_MAP_DONE);
  2586. oct->app_mode = CVM_DRV_NIC_APP;
  2587. /* Initialize the dispatch mechanism used to push packets arriving on
  2588. * Octeon Output queues.
  2589. */
  2590. if (octeon_init_dispatch_list(oct))
  2591. return 1;
  2592. atomic_set(&oct->status, OCT_DEV_DISPATCH_INIT_DONE);
  2593. if (octeon_set_io_queues_off(oct)) {
  2594. dev_err(&oct->pci_dev->dev, "setting io queues off failed\n");
  2595. return 1;
  2596. }
  2597. if (oct->fn_list.setup_device_regs(oct)) {
  2598. dev_err(&oct->pci_dev->dev, "device registers configuration failed\n");
  2599. return 1;
  2600. }
  2601. /* Initialize soft command buffer pool */
  2602. if (octeon_setup_sc_buffer_pool(oct)) {
  2603. dev_err(&oct->pci_dev->dev, "sc buffer pool allocation failed\n");
  2604. return 1;
  2605. }
  2606. atomic_set(&oct->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
  2607. /* Setup the data structures that manage this Octeon's Input queues. */
  2608. if (octeon_setup_instr_queues(oct)) {
  2609. dev_err(&oct->pci_dev->dev, "instruction queue initialization failed\n");
  2610. return 1;
  2611. }
  2612. atomic_set(&oct->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
  2613. /* Initialize lists to manage the requests of different types that
  2614. * arrive from user & kernel applications for this octeon device.
  2615. */
  2616. if (octeon_setup_response_list(oct)) {
  2617. dev_err(&oct->pci_dev->dev, "Response list allocation failed\n");
  2618. return 1;
  2619. }
  2620. atomic_set(&oct->status, OCT_DEV_RESP_LIST_INIT_DONE);
  2621. if (octeon_setup_output_queues(oct)) {
  2622. dev_err(&oct->pci_dev->dev, "Output queue initialization failed\n");
  2623. return 1;
  2624. }
  2625. atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);
  2626. if (oct->fn_list.setup_mbox(oct)) {
  2627. dev_err(&oct->pci_dev->dev, "Mailbox setup failed\n");
  2628. return 1;
  2629. }
  2630. atomic_set(&oct->status, OCT_DEV_MBOX_SETUP_DONE);
  2631. if (octeon_allocate_ioq_vector(oct)) {
  2632. dev_err(&oct->pci_dev->dev, "ioq vector allocation failed\n");
  2633. return 1;
  2634. }
  2635. atomic_set(&oct->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
  2636. dev_info(&oct->pci_dev->dev, "OCTEON_CN23XX VF Version: %s, %d ioqs\n",
  2637. LIQUIDIO_VERSION, oct->sriov_info.rings_per_vf);
  2638. /* Setup the interrupt handler and record the INT SUM register address*/
  2639. if (octeon_setup_interrupt(oct))
  2640. return 1;
  2641. if (cn23xx_octeon_pfvf_handshake(oct))
  2642. return 1;
  2643. /* Enable Octeon device interrupts */
  2644. oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
  2645. atomic_set(&oct->status, OCT_DEV_INTR_SET_DONE);
  2646. /* Enable the input and output queues for this Octeon device */
  2647. if (oct->fn_list.enable_io_queues(oct)) {
  2648. dev_err(&oct->pci_dev->dev, "enabling io queues failed\n");
  2649. return 1;
  2650. }
  2651. atomic_set(&oct->status, OCT_DEV_IO_QUEUES_DONE);
  2652. atomic_set(&oct->status, OCT_DEV_HOST_OK);
  2653. /* Send Credit for Octeon Output queues. Credits are always sent after
  2654. * the output queue is enabled.
  2655. */
  2656. for (j = 0; j < oct->num_oqs; j++)
  2657. writel(oct->droq[j]->max_count, oct->droq[j]->pkts_credit_reg);
  2658. /* Packets can start arriving on the output queues from this point. */
  2659. atomic_set(&oct->status, OCT_DEV_CORE_OK);
  2660. atomic_set(&oct->status, OCT_DEV_RUNNING);
  2661. if (liquidio_init_nic_module(oct))
  2662. return 1;
  2663. return 0;
  2664. }
  2665. static int __init liquidio_vf_init(void)
  2666. {
  2667. octeon_init_device_list(0);
  2668. return pci_register_driver(&liquidio_vf_pci_driver);
  2669. }
  2670. static void __exit liquidio_vf_exit(void)
  2671. {
  2672. pci_unregister_driver(&liquidio_vf_pci_driver);
  2673. pr_info("LiquidIO_VF network module is now unloaded\n");
  2674. }
  2675. module_init(liquidio_vf_init);
  2676. module_exit(liquidio_vf_exit);