lio_vf_main.c 83 KB

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