lio_vf_main.c 84 KB

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