lio_vf_main.c 71 KB

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