idpf_lib.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2023 Intel Corporation */
  3. #include "idpf.h"
  4. #include "idpf_virtchnl.h"
  5. static const struct net_device_ops idpf_netdev_ops;
  6. /**
  7. * idpf_init_vector_stack - Fill the MSIX vector stack with vector index
  8. * @adapter: private data struct
  9. *
  10. * Return 0 on success, error on failure
  11. */
  12. static int idpf_init_vector_stack(struct idpf_adapter *adapter)
  13. {
  14. struct idpf_vector_lifo *stack;
  15. u16 min_vec;
  16. u32 i;
  17. mutex_lock(&adapter->vector_lock);
  18. min_vec = adapter->num_msix_entries - adapter->num_avail_msix;
  19. stack = &adapter->vector_stack;
  20. stack->size = adapter->num_msix_entries;
  21. /* set the base and top to point at start of the 'free pool' to
  22. * distribute the unused vectors on-demand basis
  23. */
  24. stack->base = min_vec;
  25. stack->top = min_vec;
  26. stack->vec_idx = kcalloc(stack->size, sizeof(u16), GFP_KERNEL);
  27. if (!stack->vec_idx) {
  28. mutex_unlock(&adapter->vector_lock);
  29. return -ENOMEM;
  30. }
  31. for (i = 0; i < stack->size; i++)
  32. stack->vec_idx[i] = i;
  33. mutex_unlock(&adapter->vector_lock);
  34. return 0;
  35. }
  36. /**
  37. * idpf_deinit_vector_stack - zero out the MSIX vector stack
  38. * @adapter: private data struct
  39. */
  40. static void idpf_deinit_vector_stack(struct idpf_adapter *adapter)
  41. {
  42. struct idpf_vector_lifo *stack;
  43. mutex_lock(&adapter->vector_lock);
  44. stack = &adapter->vector_stack;
  45. kfree(stack->vec_idx);
  46. stack->vec_idx = NULL;
  47. mutex_unlock(&adapter->vector_lock);
  48. }
  49. /**
  50. * idpf_mb_intr_rel_irq - Free the IRQ association with the OS
  51. * @adapter: adapter structure
  52. *
  53. * This will also disable interrupt mode and queue up mailbox task. Mailbox
  54. * task will reschedule itself if not in interrupt mode.
  55. */
  56. static void idpf_mb_intr_rel_irq(struct idpf_adapter *adapter)
  57. {
  58. clear_bit(IDPF_MB_INTR_MODE, adapter->flags);
  59. kfree(free_irq(adapter->msix_entries[0].vector, adapter));
  60. queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
  61. }
  62. /**
  63. * idpf_intr_rel - Release interrupt capabilities and free memory
  64. * @adapter: adapter to disable interrupts on
  65. */
  66. void idpf_intr_rel(struct idpf_adapter *adapter)
  67. {
  68. if (!adapter->msix_entries)
  69. return;
  70. idpf_mb_intr_rel_irq(adapter);
  71. pci_free_irq_vectors(adapter->pdev);
  72. idpf_send_dealloc_vectors_msg(adapter);
  73. idpf_deinit_vector_stack(adapter);
  74. kfree(adapter->msix_entries);
  75. adapter->msix_entries = NULL;
  76. }
  77. /**
  78. * idpf_mb_intr_clean - Interrupt handler for the mailbox
  79. * @irq: interrupt number
  80. * @data: pointer to the adapter structure
  81. */
  82. static irqreturn_t idpf_mb_intr_clean(int __always_unused irq, void *data)
  83. {
  84. struct idpf_adapter *adapter = (struct idpf_adapter *)data;
  85. queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
  86. return IRQ_HANDLED;
  87. }
  88. /**
  89. * idpf_mb_irq_enable - Enable MSIX interrupt for the mailbox
  90. * @adapter: adapter to get the hardware address for register write
  91. */
  92. static void idpf_mb_irq_enable(struct idpf_adapter *adapter)
  93. {
  94. struct idpf_intr_reg *intr = &adapter->mb_vector.intr_reg;
  95. u32 val;
  96. val = intr->dyn_ctl_intena_m | intr->dyn_ctl_itridx_m;
  97. writel(val, intr->dyn_ctl);
  98. writel(intr->icr_ena_ctlq_m, intr->icr_ena);
  99. }
  100. /**
  101. * idpf_mb_intr_req_irq - Request irq for the mailbox interrupt
  102. * @adapter: adapter structure to pass to the mailbox irq handler
  103. */
  104. static int idpf_mb_intr_req_irq(struct idpf_adapter *adapter)
  105. {
  106. int irq_num, mb_vidx = 0, err;
  107. char *name;
  108. irq_num = adapter->msix_entries[mb_vidx].vector;
  109. name = kasprintf(GFP_KERNEL, "%s-%s-%d",
  110. dev_driver_string(&adapter->pdev->dev),
  111. "Mailbox", mb_vidx);
  112. err = request_irq(irq_num, adapter->irq_mb_handler, 0, name, adapter);
  113. if (err) {
  114. dev_err(&adapter->pdev->dev,
  115. "IRQ request for mailbox failed, error: %d\n", err);
  116. return err;
  117. }
  118. set_bit(IDPF_MB_INTR_MODE, adapter->flags);
  119. return 0;
  120. }
  121. /**
  122. * idpf_set_mb_vec_id - Set vector index for mailbox
  123. * @adapter: adapter structure to access the vector chunks
  124. *
  125. * The first vector id in the requested vector chunks from the CP is for
  126. * the mailbox
  127. */
  128. static void idpf_set_mb_vec_id(struct idpf_adapter *adapter)
  129. {
  130. if (adapter->req_vec_chunks)
  131. adapter->mb_vector.v_idx =
  132. le16_to_cpu(adapter->caps.mailbox_vector_id);
  133. else
  134. adapter->mb_vector.v_idx = 0;
  135. }
  136. /**
  137. * idpf_mb_intr_init - Initialize the mailbox interrupt
  138. * @adapter: adapter structure to store the mailbox vector
  139. */
  140. static int idpf_mb_intr_init(struct idpf_adapter *adapter)
  141. {
  142. adapter->dev_ops.reg_ops.mb_intr_reg_init(adapter);
  143. adapter->irq_mb_handler = idpf_mb_intr_clean;
  144. return idpf_mb_intr_req_irq(adapter);
  145. }
  146. /**
  147. * idpf_vector_lifo_push - push MSIX vector index onto stack
  148. * @adapter: private data struct
  149. * @vec_idx: vector index to store
  150. */
  151. static int idpf_vector_lifo_push(struct idpf_adapter *adapter, u16 vec_idx)
  152. {
  153. struct idpf_vector_lifo *stack = &adapter->vector_stack;
  154. lockdep_assert_held(&adapter->vector_lock);
  155. if (stack->top == stack->base) {
  156. dev_err(&adapter->pdev->dev, "Exceeded the vector stack limit: %d\n",
  157. stack->top);
  158. return -EINVAL;
  159. }
  160. stack->vec_idx[--stack->top] = vec_idx;
  161. return 0;
  162. }
  163. /**
  164. * idpf_vector_lifo_pop - pop MSIX vector index from stack
  165. * @adapter: private data struct
  166. */
  167. static int idpf_vector_lifo_pop(struct idpf_adapter *adapter)
  168. {
  169. struct idpf_vector_lifo *stack = &adapter->vector_stack;
  170. lockdep_assert_held(&adapter->vector_lock);
  171. if (stack->top == stack->size) {
  172. dev_err(&adapter->pdev->dev, "No interrupt vectors are available to distribute!\n");
  173. return -EINVAL;
  174. }
  175. return stack->vec_idx[stack->top++];
  176. }
  177. /**
  178. * idpf_vector_stash - Store the vector indexes onto the stack
  179. * @adapter: private data struct
  180. * @q_vector_idxs: vector index array
  181. * @vec_info: info related to the number of vectors
  182. *
  183. * This function is a no-op if there are no vectors indexes to be stashed
  184. */
  185. static void idpf_vector_stash(struct idpf_adapter *adapter, u16 *q_vector_idxs,
  186. struct idpf_vector_info *vec_info)
  187. {
  188. int i, base = 0;
  189. u16 vec_idx;
  190. lockdep_assert_held(&adapter->vector_lock);
  191. if (!vec_info->num_curr_vecs)
  192. return;
  193. /* For default vports, no need to stash vector allocated from the
  194. * default pool onto the stack
  195. */
  196. if (vec_info->default_vport)
  197. base = IDPF_MIN_Q_VEC;
  198. for (i = vec_info->num_curr_vecs - 1; i >= base ; i--) {
  199. vec_idx = q_vector_idxs[i];
  200. idpf_vector_lifo_push(adapter, vec_idx);
  201. adapter->num_avail_msix++;
  202. }
  203. }
  204. /**
  205. * idpf_req_rel_vector_indexes - Request or release MSIX vector indexes
  206. * @adapter: driver specific private structure
  207. * @q_vector_idxs: vector index array
  208. * @vec_info: info related to the number of vectors
  209. *
  210. * This is the core function to distribute the MSIX vectors acquired from the
  211. * OS. It expects the caller to pass the number of vectors required and
  212. * also previously allocated. First, it stashes previously allocated vector
  213. * indexes on to the stack and then figures out if it can allocate requested
  214. * vectors. It can wait on acquiring the mutex lock. If the caller passes 0 as
  215. * requested vectors, then this function just stashes the already allocated
  216. * vectors and returns 0.
  217. *
  218. * Returns actual number of vectors allocated on success, error value on failure
  219. * If 0 is returned, implies the stack has no vectors to allocate which is also
  220. * a failure case for the caller
  221. */
  222. int idpf_req_rel_vector_indexes(struct idpf_adapter *adapter,
  223. u16 *q_vector_idxs,
  224. struct idpf_vector_info *vec_info)
  225. {
  226. u16 num_req_vecs, num_alloc_vecs = 0, max_vecs;
  227. struct idpf_vector_lifo *stack;
  228. int i, j, vecid;
  229. mutex_lock(&adapter->vector_lock);
  230. stack = &adapter->vector_stack;
  231. num_req_vecs = vec_info->num_req_vecs;
  232. /* Stash interrupt vector indexes onto the stack if required */
  233. idpf_vector_stash(adapter, q_vector_idxs, vec_info);
  234. if (!num_req_vecs)
  235. goto rel_lock;
  236. if (vec_info->default_vport) {
  237. /* As IDPF_MIN_Q_VEC per default vport is put aside in the
  238. * default pool of the stack, use them for default vports
  239. */
  240. j = vec_info->index * IDPF_MIN_Q_VEC + IDPF_MBX_Q_VEC;
  241. for (i = 0; i < IDPF_MIN_Q_VEC; i++) {
  242. q_vector_idxs[num_alloc_vecs++] = stack->vec_idx[j++];
  243. num_req_vecs--;
  244. }
  245. }
  246. /* Find if stack has enough vector to allocate */
  247. max_vecs = min(adapter->num_avail_msix, num_req_vecs);
  248. for (j = 0; j < max_vecs; j++) {
  249. vecid = idpf_vector_lifo_pop(adapter);
  250. q_vector_idxs[num_alloc_vecs++] = vecid;
  251. }
  252. adapter->num_avail_msix -= max_vecs;
  253. rel_lock:
  254. mutex_unlock(&adapter->vector_lock);
  255. return num_alloc_vecs;
  256. }
  257. /**
  258. * idpf_intr_req - Request interrupt capabilities
  259. * @adapter: adapter to enable interrupts on
  260. *
  261. * Returns 0 on success, negative on failure
  262. */
  263. int idpf_intr_req(struct idpf_adapter *adapter)
  264. {
  265. u16 default_vports = idpf_get_default_vports(adapter);
  266. int num_q_vecs, total_vecs, num_vec_ids;
  267. int min_vectors, v_actual, err;
  268. unsigned int vector;
  269. u16 *vecids;
  270. total_vecs = idpf_get_reserved_vecs(adapter);
  271. num_q_vecs = total_vecs - IDPF_MBX_Q_VEC;
  272. err = idpf_send_alloc_vectors_msg(adapter, num_q_vecs);
  273. if (err) {
  274. dev_err(&adapter->pdev->dev,
  275. "Failed to allocate %d vectors: %d\n", num_q_vecs, err);
  276. return -EAGAIN;
  277. }
  278. min_vectors = IDPF_MBX_Q_VEC + IDPF_MIN_Q_VEC * default_vports;
  279. v_actual = pci_alloc_irq_vectors(adapter->pdev, min_vectors,
  280. total_vecs, PCI_IRQ_MSIX);
  281. if (v_actual < min_vectors) {
  282. dev_err(&adapter->pdev->dev, "Failed to allocate MSIX vectors: %d\n",
  283. v_actual);
  284. err = -EAGAIN;
  285. goto send_dealloc_vecs;
  286. }
  287. adapter->msix_entries = kcalloc(v_actual, sizeof(struct msix_entry),
  288. GFP_KERNEL);
  289. if (!adapter->msix_entries) {
  290. err = -ENOMEM;
  291. goto free_irq;
  292. }
  293. idpf_set_mb_vec_id(adapter);
  294. vecids = kcalloc(total_vecs, sizeof(u16), GFP_KERNEL);
  295. if (!vecids) {
  296. err = -ENOMEM;
  297. goto free_msix;
  298. }
  299. num_vec_ids = idpf_get_vec_ids(adapter, vecids, total_vecs,
  300. &adapter->req_vec_chunks->vchunks);
  301. if (num_vec_ids < v_actual) {
  302. err = -EINVAL;
  303. goto free_vecids;
  304. }
  305. for (vector = 0; vector < v_actual; vector++) {
  306. adapter->msix_entries[vector].entry = vecids[vector];
  307. adapter->msix_entries[vector].vector =
  308. pci_irq_vector(adapter->pdev, vector);
  309. }
  310. adapter->num_req_msix = total_vecs;
  311. adapter->num_msix_entries = v_actual;
  312. /* 'num_avail_msix' is used to distribute excess vectors to the vports
  313. * after considering the minimum vectors required per each default
  314. * vport
  315. */
  316. adapter->num_avail_msix = v_actual - min_vectors;
  317. /* Fill MSIX vector lifo stack with vector indexes */
  318. err = idpf_init_vector_stack(adapter);
  319. if (err)
  320. goto free_vecids;
  321. err = idpf_mb_intr_init(adapter);
  322. if (err)
  323. goto deinit_vec_stack;
  324. idpf_mb_irq_enable(adapter);
  325. kfree(vecids);
  326. return 0;
  327. deinit_vec_stack:
  328. idpf_deinit_vector_stack(adapter);
  329. free_vecids:
  330. kfree(vecids);
  331. free_msix:
  332. kfree(adapter->msix_entries);
  333. adapter->msix_entries = NULL;
  334. free_irq:
  335. pci_free_irq_vectors(adapter->pdev);
  336. send_dealloc_vecs:
  337. idpf_send_dealloc_vectors_msg(adapter);
  338. return err;
  339. }
  340. /**
  341. * idpf_find_mac_filter - Search filter list for specific mac filter
  342. * @vconfig: Vport config structure
  343. * @macaddr: The MAC address
  344. *
  345. * Returns ptr to the filter object or NULL. Must be called while holding the
  346. * mac_filter_list_lock.
  347. **/
  348. static struct idpf_mac_filter *idpf_find_mac_filter(struct idpf_vport_config *vconfig,
  349. const u8 *macaddr)
  350. {
  351. struct idpf_mac_filter *f;
  352. if (!macaddr)
  353. return NULL;
  354. list_for_each_entry(f, &vconfig->user_config.mac_filter_list, list) {
  355. if (ether_addr_equal(macaddr, f->macaddr))
  356. return f;
  357. }
  358. return NULL;
  359. }
  360. /**
  361. * __idpf_del_mac_filter - Delete a MAC filter from the filter list
  362. * @vport_config: Vport config structure
  363. * @macaddr: The MAC address
  364. *
  365. * Returns 0 on success, error value on failure
  366. **/
  367. static int __idpf_del_mac_filter(struct idpf_vport_config *vport_config,
  368. const u8 *macaddr)
  369. {
  370. struct idpf_mac_filter *f;
  371. spin_lock_bh(&vport_config->mac_filter_list_lock);
  372. f = idpf_find_mac_filter(vport_config, macaddr);
  373. if (f) {
  374. list_del(&f->list);
  375. kfree(f);
  376. }
  377. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  378. return 0;
  379. }
  380. /**
  381. * idpf_del_mac_filter - Delete a MAC filter from the filter list
  382. * @vport: Main vport structure
  383. * @np: Netdev private structure
  384. * @macaddr: The MAC address
  385. * @async: Don't wait for return message
  386. *
  387. * Removes filter from list and if interface is up, tells hardware about the
  388. * removed filter.
  389. **/
  390. static int idpf_del_mac_filter(struct idpf_vport *vport,
  391. struct idpf_netdev_priv *np,
  392. const u8 *macaddr, bool async)
  393. {
  394. struct idpf_vport_config *vport_config;
  395. struct idpf_mac_filter *f;
  396. vport_config = np->adapter->vport_config[np->vport_idx];
  397. spin_lock_bh(&vport_config->mac_filter_list_lock);
  398. f = idpf_find_mac_filter(vport_config, macaddr);
  399. if (f) {
  400. f->remove = true;
  401. } else {
  402. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  403. return -EINVAL;
  404. }
  405. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  406. if (np->state == __IDPF_VPORT_UP) {
  407. int err;
  408. err = idpf_add_del_mac_filters(vport, np, false, async);
  409. if (err)
  410. return err;
  411. }
  412. return __idpf_del_mac_filter(vport_config, macaddr);
  413. }
  414. /**
  415. * __idpf_add_mac_filter - Add mac filter helper function
  416. * @vport_config: Vport config structure
  417. * @macaddr: Address to add
  418. *
  419. * Takes mac_filter_list_lock spinlock to add new filter to list.
  420. */
  421. static int __idpf_add_mac_filter(struct idpf_vport_config *vport_config,
  422. const u8 *macaddr)
  423. {
  424. struct idpf_mac_filter *f;
  425. spin_lock_bh(&vport_config->mac_filter_list_lock);
  426. f = idpf_find_mac_filter(vport_config, macaddr);
  427. if (f) {
  428. f->remove = false;
  429. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  430. return 0;
  431. }
  432. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  433. if (!f) {
  434. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  435. return -ENOMEM;
  436. }
  437. ether_addr_copy(f->macaddr, macaddr);
  438. list_add_tail(&f->list, &vport_config->user_config.mac_filter_list);
  439. f->add = true;
  440. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  441. return 0;
  442. }
  443. /**
  444. * idpf_add_mac_filter - Add a mac filter to the filter list
  445. * @vport: Main vport structure
  446. * @np: Netdev private structure
  447. * @macaddr: The MAC address
  448. * @async: Don't wait for return message
  449. *
  450. * Returns 0 on success or error on failure. If interface is up, we'll also
  451. * send the virtchnl message to tell hardware about the filter.
  452. **/
  453. static int idpf_add_mac_filter(struct idpf_vport *vport,
  454. struct idpf_netdev_priv *np,
  455. const u8 *macaddr, bool async)
  456. {
  457. struct idpf_vport_config *vport_config;
  458. int err;
  459. vport_config = np->adapter->vport_config[np->vport_idx];
  460. err = __idpf_add_mac_filter(vport_config, macaddr);
  461. if (err)
  462. return err;
  463. if (np->state == __IDPF_VPORT_UP)
  464. err = idpf_add_del_mac_filters(vport, np, true, async);
  465. return err;
  466. }
  467. /**
  468. * idpf_del_all_mac_filters - Delete all MAC filters in list
  469. * @vport: main vport struct
  470. *
  471. * Takes mac_filter_list_lock spinlock. Deletes all filters
  472. */
  473. static void idpf_del_all_mac_filters(struct idpf_vport *vport)
  474. {
  475. struct idpf_vport_config *vport_config;
  476. struct idpf_mac_filter *f, *ftmp;
  477. vport_config = vport->adapter->vport_config[vport->idx];
  478. spin_lock_bh(&vport_config->mac_filter_list_lock);
  479. list_for_each_entry_safe(f, ftmp, &vport_config->user_config.mac_filter_list,
  480. list) {
  481. list_del(&f->list);
  482. kfree(f);
  483. }
  484. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  485. }
  486. /**
  487. * idpf_restore_mac_filters - Re-add all MAC filters in list
  488. * @vport: main vport struct
  489. *
  490. * Takes mac_filter_list_lock spinlock. Sets add field to true for filters to
  491. * resync filters back to HW.
  492. */
  493. static void idpf_restore_mac_filters(struct idpf_vport *vport)
  494. {
  495. struct idpf_vport_config *vport_config;
  496. struct idpf_mac_filter *f;
  497. vport_config = vport->adapter->vport_config[vport->idx];
  498. spin_lock_bh(&vport_config->mac_filter_list_lock);
  499. list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list)
  500. f->add = true;
  501. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  502. idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev),
  503. true, false);
  504. }
  505. /**
  506. * idpf_remove_mac_filters - Remove all MAC filters in list
  507. * @vport: main vport struct
  508. *
  509. * Takes mac_filter_list_lock spinlock. Sets remove field to true for filters
  510. * to remove filters in HW.
  511. */
  512. static void idpf_remove_mac_filters(struct idpf_vport *vport)
  513. {
  514. struct idpf_vport_config *vport_config;
  515. struct idpf_mac_filter *f;
  516. vport_config = vport->adapter->vport_config[vport->idx];
  517. spin_lock_bh(&vport_config->mac_filter_list_lock);
  518. list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list)
  519. f->remove = true;
  520. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  521. idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev),
  522. false, false);
  523. }
  524. /**
  525. * idpf_deinit_mac_addr - deinitialize mac address for vport
  526. * @vport: main vport structure
  527. */
  528. static void idpf_deinit_mac_addr(struct idpf_vport *vport)
  529. {
  530. struct idpf_vport_config *vport_config;
  531. struct idpf_mac_filter *f;
  532. vport_config = vport->adapter->vport_config[vport->idx];
  533. spin_lock_bh(&vport_config->mac_filter_list_lock);
  534. f = idpf_find_mac_filter(vport_config, vport->default_mac_addr);
  535. if (f) {
  536. list_del(&f->list);
  537. kfree(f);
  538. }
  539. spin_unlock_bh(&vport_config->mac_filter_list_lock);
  540. }
  541. /**
  542. * idpf_init_mac_addr - initialize mac address for vport
  543. * @vport: main vport structure
  544. * @netdev: pointer to netdev struct associated with this vport
  545. */
  546. static int idpf_init_mac_addr(struct idpf_vport *vport,
  547. struct net_device *netdev)
  548. {
  549. struct idpf_netdev_priv *np = netdev_priv(netdev);
  550. struct idpf_adapter *adapter = vport->adapter;
  551. int err;
  552. if (is_valid_ether_addr(vport->default_mac_addr)) {
  553. eth_hw_addr_set(netdev, vport->default_mac_addr);
  554. ether_addr_copy(netdev->perm_addr, vport->default_mac_addr);
  555. return idpf_add_mac_filter(vport, np, vport->default_mac_addr,
  556. false);
  557. }
  558. if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS,
  559. VIRTCHNL2_CAP_MACFILTER)) {
  560. dev_err(&adapter->pdev->dev,
  561. "MAC address is not provided and capability is not set\n");
  562. return -EINVAL;
  563. }
  564. eth_hw_addr_random(netdev);
  565. err = idpf_add_mac_filter(vport, np, netdev->dev_addr, false);
  566. if (err)
  567. return err;
  568. dev_info(&adapter->pdev->dev, "Invalid MAC address %pM, using random %pM\n",
  569. vport->default_mac_addr, netdev->dev_addr);
  570. ether_addr_copy(vport->default_mac_addr, netdev->dev_addr);
  571. return 0;
  572. }
  573. /**
  574. * idpf_cfg_netdev - Allocate, configure and register a netdev
  575. * @vport: main vport structure
  576. *
  577. * Returns 0 on success, negative value on failure.
  578. */
  579. static int idpf_cfg_netdev(struct idpf_vport *vport)
  580. {
  581. struct idpf_adapter *adapter = vport->adapter;
  582. struct idpf_vport_config *vport_config;
  583. netdev_features_t other_offloads = 0;
  584. netdev_features_t csum_offloads = 0;
  585. netdev_features_t tso_offloads = 0;
  586. netdev_features_t dflt_features;
  587. struct idpf_netdev_priv *np;
  588. struct net_device *netdev;
  589. u16 idx = vport->idx;
  590. int err;
  591. vport_config = adapter->vport_config[idx];
  592. /* It's possible we already have a netdev allocated and registered for
  593. * this vport
  594. */
  595. if (test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags)) {
  596. netdev = adapter->netdevs[idx];
  597. np = netdev_priv(netdev);
  598. np->vport = vport;
  599. np->vport_idx = vport->idx;
  600. np->vport_id = vport->vport_id;
  601. np->max_tx_hdr_size = idpf_get_max_tx_hdr_size(adapter);
  602. vport->netdev = netdev;
  603. return idpf_init_mac_addr(vport, netdev);
  604. }
  605. netdev = alloc_etherdev_mqs(sizeof(struct idpf_netdev_priv),
  606. vport_config->max_q.max_txq,
  607. vport_config->max_q.max_rxq);
  608. if (!netdev)
  609. return -ENOMEM;
  610. vport->netdev = netdev;
  611. np = netdev_priv(netdev);
  612. np->vport = vport;
  613. np->adapter = adapter;
  614. np->vport_idx = vport->idx;
  615. np->vport_id = vport->vport_id;
  616. np->max_tx_hdr_size = idpf_get_max_tx_hdr_size(adapter);
  617. spin_lock_init(&np->stats_lock);
  618. err = idpf_init_mac_addr(vport, netdev);
  619. if (err) {
  620. free_netdev(vport->netdev);
  621. vport->netdev = NULL;
  622. return err;
  623. }
  624. /* assign netdev_ops */
  625. netdev->netdev_ops = &idpf_netdev_ops;
  626. /* setup watchdog timeout value to be 5 second */
  627. netdev->watchdog_timeo = 5 * HZ;
  628. netdev->dev_port = idx;
  629. /* configure default MTU size */
  630. netdev->min_mtu = ETH_MIN_MTU;
  631. netdev->max_mtu = vport->max_mtu;
  632. dflt_features = NETIF_F_SG |
  633. NETIF_F_HIGHDMA;
  634. if (idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS))
  635. dflt_features |= NETIF_F_RXHASH;
  636. if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V4))
  637. csum_offloads |= NETIF_F_IP_CSUM;
  638. if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V6))
  639. csum_offloads |= NETIF_F_IPV6_CSUM;
  640. if (idpf_is_cap_ena(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM))
  641. csum_offloads |= NETIF_F_RXCSUM;
  642. if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_SCTP_CSUM))
  643. csum_offloads |= NETIF_F_SCTP_CRC;
  644. if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV4_TCP))
  645. tso_offloads |= NETIF_F_TSO;
  646. if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV6_TCP))
  647. tso_offloads |= NETIF_F_TSO6;
  648. if (idpf_is_cap_ena_all(adapter, IDPF_SEG_CAPS,
  649. VIRTCHNL2_CAP_SEG_IPV4_UDP |
  650. VIRTCHNL2_CAP_SEG_IPV6_UDP))
  651. tso_offloads |= NETIF_F_GSO_UDP_L4;
  652. if (idpf_is_cap_ena_all(adapter, IDPF_RSC_CAPS, IDPF_CAP_RSC))
  653. other_offloads |= NETIF_F_GRO_HW;
  654. if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_LOOPBACK))
  655. other_offloads |= NETIF_F_LOOPBACK;
  656. netdev->features |= dflt_features | csum_offloads | tso_offloads;
  657. netdev->hw_features |= netdev->features | other_offloads;
  658. netdev->vlan_features |= netdev->features | other_offloads;
  659. netdev->hw_enc_features |= dflt_features | other_offloads;
  660. idpf_set_ethtool_ops(netdev);
  661. SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
  662. /* carrier off on init to avoid Tx hangs */
  663. netif_carrier_off(netdev);
  664. /* make sure transmit queues start off as stopped */
  665. netif_tx_stop_all_queues(netdev);
  666. /* The vport can be arbitrarily released so we need to also track
  667. * netdevs in the adapter struct
  668. */
  669. adapter->netdevs[idx] = netdev;
  670. return 0;
  671. }
  672. /**
  673. * idpf_get_free_slot - get the next non-NULL location index in array
  674. * @adapter: adapter in which to look for a free vport slot
  675. */
  676. static int idpf_get_free_slot(struct idpf_adapter *adapter)
  677. {
  678. unsigned int i;
  679. for (i = 0; i < adapter->max_vports; i++) {
  680. if (!adapter->vports[i])
  681. return i;
  682. }
  683. return IDPF_NO_FREE_SLOT;
  684. }
  685. /**
  686. * idpf_remove_features - Turn off feature configs
  687. * @vport: virtual port structure
  688. */
  689. static void idpf_remove_features(struct idpf_vport *vport)
  690. {
  691. struct idpf_adapter *adapter = vport->adapter;
  692. if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER))
  693. idpf_remove_mac_filters(vport);
  694. }
  695. /**
  696. * idpf_vport_stop - Disable a vport
  697. * @vport: vport to disable
  698. */
  699. static void idpf_vport_stop(struct idpf_vport *vport)
  700. {
  701. struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
  702. if (np->state <= __IDPF_VPORT_DOWN)
  703. return;
  704. netif_carrier_off(vport->netdev);
  705. netif_tx_disable(vport->netdev);
  706. idpf_send_disable_vport_msg(vport);
  707. idpf_send_disable_queues_msg(vport);
  708. idpf_send_map_unmap_queue_vector_msg(vport, false);
  709. /* Normally we ask for queues in create_vport, but if the number of
  710. * initially requested queues have changed, for example via ethtool
  711. * set channels, we do delete queues and then add the queues back
  712. * instead of deleting and reallocating the vport.
  713. */
  714. if (test_and_clear_bit(IDPF_VPORT_DEL_QUEUES, vport->flags))
  715. idpf_send_delete_queues_msg(vport);
  716. idpf_remove_features(vport);
  717. vport->link_up = false;
  718. idpf_vport_intr_deinit(vport);
  719. idpf_vport_queues_rel(vport);
  720. idpf_vport_intr_rel(vport);
  721. np->state = __IDPF_VPORT_DOWN;
  722. }
  723. /**
  724. * idpf_stop - Disables a network interface
  725. * @netdev: network interface device structure
  726. *
  727. * The stop entry point is called when an interface is de-activated by the OS,
  728. * and the netdevice enters the DOWN state. The hardware is still under the
  729. * driver's control, but the netdev interface is disabled.
  730. *
  731. * Returns success only - not allowed to fail
  732. */
  733. static int idpf_stop(struct net_device *netdev)
  734. {
  735. struct idpf_netdev_priv *np = netdev_priv(netdev);
  736. struct idpf_vport *vport;
  737. if (test_bit(IDPF_REMOVE_IN_PROG, np->adapter->flags))
  738. return 0;
  739. idpf_vport_ctrl_lock(netdev);
  740. vport = idpf_netdev_to_vport(netdev);
  741. idpf_vport_stop(vport);
  742. idpf_vport_ctrl_unlock(netdev);
  743. return 0;
  744. }
  745. /**
  746. * idpf_decfg_netdev - Unregister the netdev
  747. * @vport: vport for which netdev to be unregistered
  748. */
  749. static void idpf_decfg_netdev(struct idpf_vport *vport)
  750. {
  751. struct idpf_adapter *adapter = vport->adapter;
  752. kfree(vport->rx_ptype_lkup);
  753. vport->rx_ptype_lkup = NULL;
  754. unregister_netdev(vport->netdev);
  755. free_netdev(vport->netdev);
  756. vport->netdev = NULL;
  757. adapter->netdevs[vport->idx] = NULL;
  758. }
  759. /**
  760. * idpf_vport_rel - Delete a vport and free its resources
  761. * @vport: the vport being removed
  762. */
  763. static void idpf_vport_rel(struct idpf_vport *vport)
  764. {
  765. struct idpf_adapter *adapter = vport->adapter;
  766. struct idpf_vport_config *vport_config;
  767. struct idpf_vector_info vec_info;
  768. struct idpf_rss_data *rss_data;
  769. struct idpf_vport_max_q max_q;
  770. u16 idx = vport->idx;
  771. vport_config = adapter->vport_config[vport->idx];
  772. idpf_deinit_rss(vport);
  773. rss_data = &vport_config->user_config.rss_data;
  774. kfree(rss_data->rss_key);
  775. rss_data->rss_key = NULL;
  776. idpf_send_destroy_vport_msg(vport);
  777. /* Release all max queues allocated to the adapter's pool */
  778. max_q.max_rxq = vport_config->max_q.max_rxq;
  779. max_q.max_txq = vport_config->max_q.max_txq;
  780. max_q.max_bufq = vport_config->max_q.max_bufq;
  781. max_q.max_complq = vport_config->max_q.max_complq;
  782. idpf_vport_dealloc_max_qs(adapter, &max_q);
  783. /* Release all the allocated vectors on the stack */
  784. vec_info.num_req_vecs = 0;
  785. vec_info.num_curr_vecs = vport->num_q_vectors;
  786. vec_info.default_vport = vport->default_vport;
  787. idpf_req_rel_vector_indexes(adapter, vport->q_vector_idxs, &vec_info);
  788. kfree(vport->q_vector_idxs);
  789. vport->q_vector_idxs = NULL;
  790. kfree(adapter->vport_params_recvd[idx]);
  791. adapter->vport_params_recvd[idx] = NULL;
  792. kfree(adapter->vport_params_reqd[idx]);
  793. adapter->vport_params_reqd[idx] = NULL;
  794. if (adapter->vport_config[idx]) {
  795. kfree(adapter->vport_config[idx]->req_qs_chunks);
  796. adapter->vport_config[idx]->req_qs_chunks = NULL;
  797. }
  798. kfree(vport);
  799. adapter->num_alloc_vports--;
  800. }
  801. /**
  802. * idpf_vport_dealloc - cleanup and release a given vport
  803. * @vport: pointer to idpf vport structure
  804. *
  805. * returns nothing
  806. */
  807. static void idpf_vport_dealloc(struct idpf_vport *vport)
  808. {
  809. struct idpf_adapter *adapter = vport->adapter;
  810. unsigned int i = vport->idx;
  811. idpf_deinit_mac_addr(vport);
  812. idpf_vport_stop(vport);
  813. if (!test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
  814. idpf_decfg_netdev(vport);
  815. if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
  816. idpf_del_all_mac_filters(vport);
  817. if (adapter->netdevs[i]) {
  818. struct idpf_netdev_priv *np = netdev_priv(adapter->netdevs[i]);
  819. np->vport = NULL;
  820. }
  821. idpf_vport_rel(vport);
  822. adapter->vports[i] = NULL;
  823. adapter->next_vport = idpf_get_free_slot(adapter);
  824. }
  825. /**
  826. * idpf_is_hsplit_supported - check whether the header split is supported
  827. * @vport: virtual port to check the capability for
  828. *
  829. * Return: true if it's supported by the HW/FW, false if not.
  830. */
  831. static bool idpf_is_hsplit_supported(const struct idpf_vport *vport)
  832. {
  833. return idpf_is_queue_model_split(vport->rxq_model) &&
  834. idpf_is_cap_ena_all(vport->adapter, IDPF_HSPLIT_CAPS,
  835. IDPF_CAP_HSPLIT);
  836. }
  837. /**
  838. * idpf_vport_get_hsplit - get the current header split feature state
  839. * @vport: virtual port to query the state for
  840. *
  841. * Return: ``ETHTOOL_TCP_DATA_SPLIT_UNKNOWN`` if not supported,
  842. * ``ETHTOOL_TCP_DATA_SPLIT_DISABLED`` if disabled,
  843. * ``ETHTOOL_TCP_DATA_SPLIT_ENABLED`` if active.
  844. */
  845. u8 idpf_vport_get_hsplit(const struct idpf_vport *vport)
  846. {
  847. const struct idpf_vport_user_config_data *config;
  848. if (!idpf_is_hsplit_supported(vport))
  849. return ETHTOOL_TCP_DATA_SPLIT_UNKNOWN;
  850. config = &vport->adapter->vport_config[vport->idx]->user_config;
  851. return test_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags) ?
  852. ETHTOOL_TCP_DATA_SPLIT_ENABLED :
  853. ETHTOOL_TCP_DATA_SPLIT_DISABLED;
  854. }
  855. /**
  856. * idpf_vport_set_hsplit - enable or disable header split on a given vport
  857. * @vport: virtual port to configure
  858. * @val: Ethtool flag controlling the header split state
  859. *
  860. * Return: true on success, false if not supported by the HW.
  861. */
  862. bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val)
  863. {
  864. struct idpf_vport_user_config_data *config;
  865. if (!idpf_is_hsplit_supported(vport))
  866. return val == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN;
  867. config = &vport->adapter->vport_config[vport->idx]->user_config;
  868. switch (val) {
  869. case ETHTOOL_TCP_DATA_SPLIT_UNKNOWN:
  870. /* Default is to enable */
  871. case ETHTOOL_TCP_DATA_SPLIT_ENABLED:
  872. __set_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags);
  873. return true;
  874. case ETHTOOL_TCP_DATA_SPLIT_DISABLED:
  875. __clear_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags);
  876. return true;
  877. default:
  878. return false;
  879. }
  880. }
  881. /**
  882. * idpf_vport_alloc - Allocates the next available struct vport in the adapter
  883. * @adapter: board private structure
  884. * @max_q: vport max queue info
  885. *
  886. * returns a pointer to a vport on success, NULL on failure.
  887. */
  888. static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
  889. struct idpf_vport_max_q *max_q)
  890. {
  891. struct idpf_rss_data *rss_data;
  892. u16 idx = adapter->next_vport;
  893. struct idpf_vport *vport;
  894. u16 num_max_q;
  895. if (idx == IDPF_NO_FREE_SLOT)
  896. return NULL;
  897. vport = kzalloc(sizeof(*vport), GFP_KERNEL);
  898. if (!vport)
  899. return vport;
  900. num_max_q = max(max_q->max_txq, max_q->max_rxq);
  901. if (!adapter->vport_config[idx]) {
  902. struct idpf_vport_config *vport_config;
  903. struct idpf_q_coalesce *q_coal;
  904. vport_config = kzalloc(sizeof(*vport_config), GFP_KERNEL);
  905. if (!vport_config) {
  906. kfree(vport);
  907. return NULL;
  908. }
  909. q_coal = kcalloc(num_max_q, sizeof(*q_coal), GFP_KERNEL);
  910. if (!q_coal) {
  911. kfree(vport_config);
  912. kfree(vport);
  913. return NULL;
  914. }
  915. for (int i = 0; i < num_max_q; i++) {
  916. q_coal[i].tx_intr_mode = IDPF_ITR_DYNAMIC;
  917. q_coal[i].tx_coalesce_usecs = IDPF_ITR_TX_DEF;
  918. q_coal[i].rx_intr_mode = IDPF_ITR_DYNAMIC;
  919. q_coal[i].rx_coalesce_usecs = IDPF_ITR_RX_DEF;
  920. }
  921. vport_config->user_config.q_coalesce = q_coal;
  922. adapter->vport_config[idx] = vport_config;
  923. }
  924. vport->idx = idx;
  925. vport->adapter = adapter;
  926. vport->compln_clean_budget = IDPF_TX_COMPLQ_CLEAN_BUDGET;
  927. vport->default_vport = adapter->num_alloc_vports <
  928. idpf_get_default_vports(adapter);
  929. vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL);
  930. if (!vport->q_vector_idxs)
  931. goto free_vport;
  932. idpf_vport_init(vport, max_q);
  933. /* This alloc is done separate from the LUT because it's not strictly
  934. * dependent on how many queues we have. If we change number of queues
  935. * and soft reset we'll need a new LUT but the key can remain the same
  936. * for as long as the vport exists.
  937. */
  938. rss_data = &adapter->vport_config[idx]->user_config.rss_data;
  939. rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL);
  940. if (!rss_data->rss_key)
  941. goto free_vector_idxs;
  942. /* Initialize default rss key */
  943. netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size);
  944. /* fill vport slot in the adapter struct */
  945. adapter->vports[idx] = vport;
  946. adapter->vport_ids[idx] = idpf_get_vport_id(vport);
  947. adapter->num_alloc_vports++;
  948. /* prepare adapter->next_vport for next use */
  949. adapter->next_vport = idpf_get_free_slot(adapter);
  950. return vport;
  951. free_vector_idxs:
  952. kfree(vport->q_vector_idxs);
  953. free_vport:
  954. kfree(vport);
  955. return NULL;
  956. }
  957. /**
  958. * idpf_get_stats64 - get statistics for network device structure
  959. * @netdev: network interface device structure
  960. * @stats: main device statistics structure
  961. */
  962. static void idpf_get_stats64(struct net_device *netdev,
  963. struct rtnl_link_stats64 *stats)
  964. {
  965. struct idpf_netdev_priv *np = netdev_priv(netdev);
  966. spin_lock_bh(&np->stats_lock);
  967. *stats = np->netstats;
  968. spin_unlock_bh(&np->stats_lock);
  969. }
  970. /**
  971. * idpf_statistics_task - Delayed task to get statistics over mailbox
  972. * @work: work_struct handle to our data
  973. */
  974. void idpf_statistics_task(struct work_struct *work)
  975. {
  976. struct idpf_adapter *adapter;
  977. int i;
  978. adapter = container_of(work, struct idpf_adapter, stats_task.work);
  979. for (i = 0; i < adapter->max_vports; i++) {
  980. struct idpf_vport *vport = adapter->vports[i];
  981. if (vport && !test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
  982. idpf_send_get_stats_msg(vport);
  983. }
  984. queue_delayed_work(adapter->stats_wq, &adapter->stats_task,
  985. msecs_to_jiffies(10000));
  986. }
  987. /**
  988. * idpf_mbx_task - Delayed task to handle mailbox responses
  989. * @work: work_struct handle
  990. */
  991. void idpf_mbx_task(struct work_struct *work)
  992. {
  993. struct idpf_adapter *adapter;
  994. adapter = container_of(work, struct idpf_adapter, mbx_task.work);
  995. if (test_bit(IDPF_MB_INTR_MODE, adapter->flags))
  996. idpf_mb_irq_enable(adapter);
  997. else
  998. queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task,
  999. msecs_to_jiffies(300));
  1000. idpf_recv_mb_msg(adapter);
  1001. }
  1002. /**
  1003. * idpf_service_task - Delayed task for handling mailbox responses
  1004. * @work: work_struct handle to our data
  1005. *
  1006. */
  1007. void idpf_service_task(struct work_struct *work)
  1008. {
  1009. struct idpf_adapter *adapter;
  1010. adapter = container_of(work, struct idpf_adapter, serv_task.work);
  1011. if (idpf_is_reset_detected(adapter) &&
  1012. !idpf_is_reset_in_prog(adapter) &&
  1013. !test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) {
  1014. dev_info(&adapter->pdev->dev, "HW reset detected\n");
  1015. set_bit(IDPF_HR_FUNC_RESET, adapter->flags);
  1016. queue_delayed_work(adapter->vc_event_wq,
  1017. &adapter->vc_event_task,
  1018. msecs_to_jiffies(10));
  1019. }
  1020. queue_delayed_work(adapter->serv_wq, &adapter->serv_task,
  1021. msecs_to_jiffies(300));
  1022. }
  1023. /**
  1024. * idpf_restore_features - Restore feature configs
  1025. * @vport: virtual port structure
  1026. */
  1027. static void idpf_restore_features(struct idpf_vport *vport)
  1028. {
  1029. struct idpf_adapter *adapter = vport->adapter;
  1030. if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER))
  1031. idpf_restore_mac_filters(vport);
  1032. }
  1033. /**
  1034. * idpf_set_real_num_queues - set number of queues for netdev
  1035. * @vport: virtual port structure
  1036. *
  1037. * Returns 0 on success, negative on failure.
  1038. */
  1039. static int idpf_set_real_num_queues(struct idpf_vport *vport)
  1040. {
  1041. int err;
  1042. err = netif_set_real_num_rx_queues(vport->netdev, vport->num_rxq);
  1043. if (err)
  1044. return err;
  1045. return netif_set_real_num_tx_queues(vport->netdev, vport->num_txq);
  1046. }
  1047. /**
  1048. * idpf_up_complete - Complete interface up sequence
  1049. * @vport: virtual port structure
  1050. *
  1051. * Returns 0 on success, negative on failure.
  1052. */
  1053. static int idpf_up_complete(struct idpf_vport *vport)
  1054. {
  1055. struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
  1056. if (vport->link_up && !netif_carrier_ok(vport->netdev)) {
  1057. netif_carrier_on(vport->netdev);
  1058. netif_tx_start_all_queues(vport->netdev);
  1059. }
  1060. np->state = __IDPF_VPORT_UP;
  1061. return 0;
  1062. }
  1063. /**
  1064. * idpf_rx_init_buf_tail - Write initial buffer ring tail value
  1065. * @vport: virtual port struct
  1066. */
  1067. static void idpf_rx_init_buf_tail(struct idpf_vport *vport)
  1068. {
  1069. int i, j;
  1070. for (i = 0; i < vport->num_rxq_grp; i++) {
  1071. struct idpf_rxq_group *grp = &vport->rxq_grps[i];
  1072. if (idpf_is_queue_model_split(vport->rxq_model)) {
  1073. for (j = 0; j < vport->num_bufqs_per_qgrp; j++) {
  1074. const struct idpf_buf_queue *q =
  1075. &grp->splitq.bufq_sets[j].bufq;
  1076. writel(q->next_to_alloc, q->tail);
  1077. }
  1078. } else {
  1079. for (j = 0; j < grp->singleq.num_rxq; j++) {
  1080. const struct idpf_rx_queue *q =
  1081. grp->singleq.rxqs[j];
  1082. writel(q->next_to_alloc, q->tail);
  1083. }
  1084. }
  1085. }
  1086. }
  1087. /**
  1088. * idpf_vport_open - Bring up a vport
  1089. * @vport: vport to bring up
  1090. */
  1091. static int idpf_vport_open(struct idpf_vport *vport)
  1092. {
  1093. struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
  1094. struct idpf_adapter *adapter = vport->adapter;
  1095. struct idpf_vport_config *vport_config;
  1096. int err;
  1097. if (np->state != __IDPF_VPORT_DOWN)
  1098. return -EBUSY;
  1099. /* we do not allow interface up just yet */
  1100. netif_carrier_off(vport->netdev);
  1101. err = idpf_vport_intr_alloc(vport);
  1102. if (err) {
  1103. dev_err(&adapter->pdev->dev, "Failed to allocate interrupts for vport %u: %d\n",
  1104. vport->vport_id, err);
  1105. return err;
  1106. }
  1107. err = idpf_vport_queues_alloc(vport);
  1108. if (err)
  1109. goto intr_rel;
  1110. err = idpf_vport_queue_ids_init(vport);
  1111. if (err) {
  1112. dev_err(&adapter->pdev->dev, "Failed to initialize queue ids for vport %u: %d\n",
  1113. vport->vport_id, err);
  1114. goto queues_rel;
  1115. }
  1116. err = idpf_vport_intr_init(vport);
  1117. if (err) {
  1118. dev_err(&adapter->pdev->dev, "Failed to initialize interrupts for vport %u: %d\n",
  1119. vport->vport_id, err);
  1120. goto queues_rel;
  1121. }
  1122. err = idpf_rx_bufs_init_all(vport);
  1123. if (err) {
  1124. dev_err(&adapter->pdev->dev, "Failed to initialize RX buffers for vport %u: %d\n",
  1125. vport->vport_id, err);
  1126. goto queues_rel;
  1127. }
  1128. err = idpf_queue_reg_init(vport);
  1129. if (err) {
  1130. dev_err(&adapter->pdev->dev, "Failed to initialize queue registers for vport %u: %d\n",
  1131. vport->vport_id, err);
  1132. goto queues_rel;
  1133. }
  1134. idpf_rx_init_buf_tail(vport);
  1135. idpf_vport_intr_ena(vport);
  1136. err = idpf_send_config_queues_msg(vport);
  1137. if (err) {
  1138. dev_err(&adapter->pdev->dev, "Failed to configure queues for vport %u, %d\n",
  1139. vport->vport_id, err);
  1140. goto intr_deinit;
  1141. }
  1142. err = idpf_send_map_unmap_queue_vector_msg(vport, true);
  1143. if (err) {
  1144. dev_err(&adapter->pdev->dev, "Failed to map queue vectors for vport %u: %d\n",
  1145. vport->vport_id, err);
  1146. goto intr_deinit;
  1147. }
  1148. err = idpf_send_enable_queues_msg(vport);
  1149. if (err) {
  1150. dev_err(&adapter->pdev->dev, "Failed to enable queues for vport %u: %d\n",
  1151. vport->vport_id, err);
  1152. goto unmap_queue_vectors;
  1153. }
  1154. err = idpf_send_enable_vport_msg(vport);
  1155. if (err) {
  1156. dev_err(&adapter->pdev->dev, "Failed to enable vport %u: %d\n",
  1157. vport->vport_id, err);
  1158. err = -EAGAIN;
  1159. goto disable_queues;
  1160. }
  1161. idpf_restore_features(vport);
  1162. vport_config = adapter->vport_config[vport->idx];
  1163. if (vport_config->user_config.rss_data.rss_lut)
  1164. err = idpf_config_rss(vport);
  1165. else
  1166. err = idpf_init_rss(vport);
  1167. if (err) {
  1168. dev_err(&adapter->pdev->dev, "Failed to initialize RSS for vport %u: %d\n",
  1169. vport->vport_id, err);
  1170. goto disable_vport;
  1171. }
  1172. err = idpf_up_complete(vport);
  1173. if (err) {
  1174. dev_err(&adapter->pdev->dev, "Failed to complete interface up for vport %u: %d\n",
  1175. vport->vport_id, err);
  1176. goto deinit_rss;
  1177. }
  1178. return 0;
  1179. deinit_rss:
  1180. idpf_deinit_rss(vport);
  1181. disable_vport:
  1182. idpf_send_disable_vport_msg(vport);
  1183. disable_queues:
  1184. idpf_send_disable_queues_msg(vport);
  1185. unmap_queue_vectors:
  1186. idpf_send_map_unmap_queue_vector_msg(vport, false);
  1187. intr_deinit:
  1188. idpf_vport_intr_deinit(vport);
  1189. queues_rel:
  1190. idpf_vport_queues_rel(vport);
  1191. intr_rel:
  1192. idpf_vport_intr_rel(vport);
  1193. return err;
  1194. }
  1195. /**
  1196. * idpf_init_task - Delayed initialization task
  1197. * @work: work_struct handle to our data
  1198. *
  1199. * Init task finishes up pending work started in probe. Due to the asynchronous
  1200. * nature in which the device communicates with hardware, we may have to wait
  1201. * several milliseconds to get a response. Instead of busy polling in probe,
  1202. * pulling it out into a delayed work task prevents us from bogging down the
  1203. * whole system waiting for a response from hardware.
  1204. */
  1205. void idpf_init_task(struct work_struct *work)
  1206. {
  1207. struct idpf_vport_config *vport_config;
  1208. struct idpf_vport_max_q max_q;
  1209. struct idpf_adapter *adapter;
  1210. struct idpf_netdev_priv *np;
  1211. struct idpf_vport *vport;
  1212. u16 num_default_vports;
  1213. struct pci_dev *pdev;
  1214. bool default_vport;
  1215. int index, err;
  1216. adapter = container_of(work, struct idpf_adapter, init_task.work);
  1217. num_default_vports = idpf_get_default_vports(adapter);
  1218. if (adapter->num_alloc_vports < num_default_vports)
  1219. default_vport = true;
  1220. else
  1221. default_vport = false;
  1222. err = idpf_vport_alloc_max_qs(adapter, &max_q);
  1223. if (err)
  1224. goto unwind_vports;
  1225. err = idpf_send_create_vport_msg(adapter, &max_q);
  1226. if (err) {
  1227. idpf_vport_dealloc_max_qs(adapter, &max_q);
  1228. goto unwind_vports;
  1229. }
  1230. pdev = adapter->pdev;
  1231. vport = idpf_vport_alloc(adapter, &max_q);
  1232. if (!vport) {
  1233. err = -EFAULT;
  1234. dev_err(&pdev->dev, "failed to allocate vport: %d\n",
  1235. err);
  1236. idpf_vport_dealloc_max_qs(adapter, &max_q);
  1237. goto unwind_vports;
  1238. }
  1239. index = vport->idx;
  1240. vport_config = adapter->vport_config[index];
  1241. init_waitqueue_head(&vport->sw_marker_wq);
  1242. spin_lock_init(&vport_config->mac_filter_list_lock);
  1243. INIT_LIST_HEAD(&vport_config->user_config.mac_filter_list);
  1244. err = idpf_check_supported_desc_ids(vport);
  1245. if (err) {
  1246. dev_err(&pdev->dev, "failed to get required descriptor ids\n");
  1247. goto cfg_netdev_err;
  1248. }
  1249. if (idpf_cfg_netdev(vport))
  1250. goto cfg_netdev_err;
  1251. err = idpf_send_get_rx_ptype_msg(vport);
  1252. if (err)
  1253. goto handle_err;
  1254. /* Once state is put into DOWN, driver is ready for dev_open */
  1255. np = netdev_priv(vport->netdev);
  1256. np->state = __IDPF_VPORT_DOWN;
  1257. if (test_and_clear_bit(IDPF_VPORT_UP_REQUESTED, vport_config->flags))
  1258. idpf_vport_open(vport);
  1259. /* Spawn and return 'idpf_init_task' work queue until all the
  1260. * default vports are created
  1261. */
  1262. if (adapter->num_alloc_vports < num_default_vports) {
  1263. queue_delayed_work(adapter->init_wq, &adapter->init_task,
  1264. msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07)));
  1265. return;
  1266. }
  1267. for (index = 0; index < adapter->max_vports; index++) {
  1268. if (adapter->netdevs[index] &&
  1269. !test_bit(IDPF_VPORT_REG_NETDEV,
  1270. adapter->vport_config[index]->flags)) {
  1271. register_netdev(adapter->netdevs[index]);
  1272. set_bit(IDPF_VPORT_REG_NETDEV,
  1273. adapter->vport_config[index]->flags);
  1274. }
  1275. }
  1276. /* As all the required vports are created, clear the reset flag
  1277. * unconditionally here in case we were in reset and the link was down.
  1278. */
  1279. clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
  1280. /* Start the statistics task now */
  1281. queue_delayed_work(adapter->stats_wq, &adapter->stats_task,
  1282. msecs_to_jiffies(10 * (pdev->devfn & 0x07)));
  1283. return;
  1284. handle_err:
  1285. idpf_decfg_netdev(vport);
  1286. cfg_netdev_err:
  1287. idpf_vport_rel(vport);
  1288. adapter->vports[index] = NULL;
  1289. unwind_vports:
  1290. if (default_vport) {
  1291. for (index = 0; index < adapter->max_vports; index++) {
  1292. if (adapter->vports[index])
  1293. idpf_vport_dealloc(adapter->vports[index]);
  1294. }
  1295. }
  1296. clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
  1297. }
  1298. /**
  1299. * idpf_sriov_ena - Enable or change number of VFs
  1300. * @adapter: private data struct
  1301. * @num_vfs: number of VFs to allocate
  1302. */
  1303. static int idpf_sriov_ena(struct idpf_adapter *adapter, int num_vfs)
  1304. {
  1305. struct device *dev = &adapter->pdev->dev;
  1306. int err;
  1307. err = idpf_send_set_sriov_vfs_msg(adapter, num_vfs);
  1308. if (err) {
  1309. dev_err(dev, "Failed to allocate VFs: %d\n", err);
  1310. return err;
  1311. }
  1312. err = pci_enable_sriov(adapter->pdev, num_vfs);
  1313. if (err) {
  1314. idpf_send_set_sriov_vfs_msg(adapter, 0);
  1315. dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
  1316. return err;
  1317. }
  1318. adapter->num_vfs = num_vfs;
  1319. return num_vfs;
  1320. }
  1321. /**
  1322. * idpf_sriov_configure - Configure the requested VFs
  1323. * @pdev: pointer to a pci_dev structure
  1324. * @num_vfs: number of vfs to allocate
  1325. *
  1326. * Enable or change the number of VFs. Called when the user updates the number
  1327. * of VFs in sysfs.
  1328. **/
  1329. int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs)
  1330. {
  1331. struct idpf_adapter *adapter = pci_get_drvdata(pdev);
  1332. if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_SRIOV)) {
  1333. dev_info(&pdev->dev, "SR-IOV is not supported on this device\n");
  1334. return -EOPNOTSUPP;
  1335. }
  1336. if (num_vfs)
  1337. return idpf_sriov_ena(adapter, num_vfs);
  1338. if (pci_vfs_assigned(pdev)) {
  1339. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs\n");
  1340. return -EBUSY;
  1341. }
  1342. pci_disable_sriov(adapter->pdev);
  1343. idpf_send_set_sriov_vfs_msg(adapter, 0);
  1344. adapter->num_vfs = 0;
  1345. return 0;
  1346. }
  1347. /**
  1348. * idpf_deinit_task - Device deinit routine
  1349. * @adapter: Driver specific private structure
  1350. *
  1351. * Extended remove logic which will be used for
  1352. * hard reset as well
  1353. */
  1354. void idpf_deinit_task(struct idpf_adapter *adapter)
  1355. {
  1356. unsigned int i;
  1357. /* Wait until the init_task is done else this thread might release
  1358. * the resources first and the other thread might end up in a bad state
  1359. */
  1360. cancel_delayed_work_sync(&adapter->init_task);
  1361. if (!adapter->vports)
  1362. return;
  1363. cancel_delayed_work_sync(&adapter->stats_task);
  1364. for (i = 0; i < adapter->max_vports; i++) {
  1365. if (adapter->vports[i])
  1366. idpf_vport_dealloc(adapter->vports[i]);
  1367. }
  1368. }
  1369. /**
  1370. * idpf_check_reset_complete - check that reset is complete
  1371. * @hw: pointer to hw struct
  1372. * @reset_reg: struct with reset registers
  1373. *
  1374. * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
  1375. **/
  1376. static int idpf_check_reset_complete(struct idpf_hw *hw,
  1377. struct idpf_reset_reg *reset_reg)
  1378. {
  1379. struct idpf_adapter *adapter = hw->back;
  1380. int i;
  1381. for (i = 0; i < 2000; i++) {
  1382. u32 reg_val = readl(reset_reg->rstat);
  1383. /* 0xFFFFFFFF might be read if other side hasn't cleared the
  1384. * register for us yet and 0xFFFFFFFF is not a valid value for
  1385. * the register, so treat that as invalid.
  1386. */
  1387. if (reg_val != 0xFFFFFFFF && (reg_val & reset_reg->rstat_m))
  1388. return 0;
  1389. usleep_range(5000, 10000);
  1390. }
  1391. dev_warn(&adapter->pdev->dev, "Device reset timeout!\n");
  1392. /* Clear the reset flag unconditionally here since the reset
  1393. * technically isn't in progress anymore from the driver's perspective
  1394. */
  1395. clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
  1396. return -EBUSY;
  1397. }
  1398. /**
  1399. * idpf_set_vport_state - Set the vport state to be after the reset
  1400. * @adapter: Driver specific private structure
  1401. */
  1402. static void idpf_set_vport_state(struct idpf_adapter *adapter)
  1403. {
  1404. u16 i;
  1405. for (i = 0; i < adapter->max_vports; i++) {
  1406. struct idpf_netdev_priv *np;
  1407. if (!adapter->netdevs[i])
  1408. continue;
  1409. np = netdev_priv(adapter->netdevs[i]);
  1410. if (np->state == __IDPF_VPORT_UP)
  1411. set_bit(IDPF_VPORT_UP_REQUESTED,
  1412. adapter->vport_config[i]->flags);
  1413. }
  1414. }
  1415. /**
  1416. * idpf_init_hard_reset - Initiate a hardware reset
  1417. * @adapter: Driver specific private structure
  1418. *
  1419. * Deallocate the vports and all the resources associated with them and
  1420. * reallocate. Also reinitialize the mailbox. Return 0 on success,
  1421. * negative on failure.
  1422. */
  1423. static int idpf_init_hard_reset(struct idpf_adapter *adapter)
  1424. {
  1425. struct idpf_reg_ops *reg_ops = &adapter->dev_ops.reg_ops;
  1426. struct device *dev = &adapter->pdev->dev;
  1427. struct net_device *netdev;
  1428. int err;
  1429. u16 i;
  1430. mutex_lock(&adapter->vport_ctrl_lock);
  1431. dev_info(dev, "Device HW Reset initiated\n");
  1432. /* Avoid TX hangs on reset */
  1433. for (i = 0; i < adapter->max_vports; i++) {
  1434. netdev = adapter->netdevs[i];
  1435. if (!netdev)
  1436. continue;
  1437. netif_carrier_off(netdev);
  1438. netif_tx_disable(netdev);
  1439. }
  1440. /* Prepare for reset */
  1441. if (test_and_clear_bit(IDPF_HR_DRV_LOAD, adapter->flags)) {
  1442. reg_ops->trigger_reset(adapter, IDPF_HR_DRV_LOAD);
  1443. } else if (test_and_clear_bit(IDPF_HR_FUNC_RESET, adapter->flags)) {
  1444. bool is_reset = idpf_is_reset_detected(adapter);
  1445. idpf_set_vport_state(adapter);
  1446. idpf_vc_core_deinit(adapter);
  1447. if (!is_reset)
  1448. reg_ops->trigger_reset(adapter, IDPF_HR_FUNC_RESET);
  1449. idpf_deinit_dflt_mbx(adapter);
  1450. } else {
  1451. dev_err(dev, "Unhandled hard reset cause\n");
  1452. err = -EBADRQC;
  1453. goto unlock_mutex;
  1454. }
  1455. /* Wait for reset to complete */
  1456. err = idpf_check_reset_complete(&adapter->hw, &adapter->reset_reg);
  1457. if (err) {
  1458. dev_err(dev, "The driver was unable to contact the device's firmware. Check that the FW is running. Driver state= 0x%x\n",
  1459. adapter->state);
  1460. goto unlock_mutex;
  1461. }
  1462. /* Reset is complete and so start building the driver resources again */
  1463. err = idpf_init_dflt_mbx(adapter);
  1464. if (err) {
  1465. dev_err(dev, "Failed to initialize default mailbox: %d\n", err);
  1466. goto unlock_mutex;
  1467. }
  1468. queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
  1469. /* Initialize the state machine, also allocate memory and request
  1470. * resources
  1471. */
  1472. err = idpf_vc_core_init(adapter);
  1473. if (err) {
  1474. cancel_delayed_work_sync(&adapter->mbx_task);
  1475. idpf_deinit_dflt_mbx(adapter);
  1476. goto unlock_mutex;
  1477. }
  1478. /* Wait till all the vports are initialized to release the reset lock,
  1479. * else user space callbacks may access uninitialized vports
  1480. */
  1481. while (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
  1482. msleep(100);
  1483. unlock_mutex:
  1484. mutex_unlock(&adapter->vport_ctrl_lock);
  1485. return err;
  1486. }
  1487. /**
  1488. * idpf_vc_event_task - Handle virtchannel event logic
  1489. * @work: work queue struct
  1490. */
  1491. void idpf_vc_event_task(struct work_struct *work)
  1492. {
  1493. struct idpf_adapter *adapter;
  1494. adapter = container_of(work, struct idpf_adapter, vc_event_task.work);
  1495. if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
  1496. return;
  1497. if (test_bit(IDPF_HR_FUNC_RESET, adapter->flags))
  1498. goto func_reset;
  1499. if (test_bit(IDPF_HR_DRV_LOAD, adapter->flags))
  1500. goto drv_load;
  1501. return;
  1502. func_reset:
  1503. idpf_vc_xn_shutdown(adapter->vcxn_mngr);
  1504. drv_load:
  1505. set_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
  1506. idpf_init_hard_reset(adapter);
  1507. }
  1508. /**
  1509. * idpf_initiate_soft_reset - Initiate a software reset
  1510. * @vport: virtual port data struct
  1511. * @reset_cause: reason for the soft reset
  1512. *
  1513. * Soft reset only reallocs vport queue resources. Returns 0 on success,
  1514. * negative on failure.
  1515. */
  1516. int idpf_initiate_soft_reset(struct idpf_vport *vport,
  1517. enum idpf_vport_reset_cause reset_cause)
  1518. {
  1519. struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
  1520. enum idpf_vport_state current_state = np->state;
  1521. struct idpf_adapter *adapter = vport->adapter;
  1522. struct idpf_vport *new_vport;
  1523. int err;
  1524. /* If the system is low on memory, we can end up in bad state if we
  1525. * free all the memory for queue resources and try to allocate them
  1526. * again. Instead, we can pre-allocate the new resources before doing
  1527. * anything and bailing if the alloc fails.
  1528. *
  1529. * Make a clone of the existing vport to mimic its current
  1530. * configuration, then modify the new structure with any requested
  1531. * changes. Once the allocation of the new resources is done, stop the
  1532. * existing vport and copy the configuration to the main vport. If an
  1533. * error occurred, the existing vport will be untouched.
  1534. *
  1535. */
  1536. new_vport = kzalloc(sizeof(*vport), GFP_KERNEL);
  1537. if (!new_vport)
  1538. return -ENOMEM;
  1539. /* This purposely avoids copying the end of the struct because it
  1540. * contains wait_queues and mutexes and other stuff we don't want to
  1541. * mess with. Nothing below should use those variables from new_vport
  1542. * and should instead always refer to them in vport if they need to.
  1543. */
  1544. memcpy(new_vport, vport, offsetof(struct idpf_vport, link_up));
  1545. /* Adjust resource parameters prior to reallocating resources */
  1546. switch (reset_cause) {
  1547. case IDPF_SR_Q_CHANGE:
  1548. err = idpf_vport_adjust_qs(new_vport);
  1549. if (err)
  1550. goto free_vport;
  1551. break;
  1552. case IDPF_SR_Q_DESC_CHANGE:
  1553. /* Update queue parameters before allocating resources */
  1554. idpf_vport_calc_num_q_desc(new_vport);
  1555. break;
  1556. case IDPF_SR_MTU_CHANGE:
  1557. case IDPF_SR_RSC_CHANGE:
  1558. break;
  1559. default:
  1560. dev_err(&adapter->pdev->dev, "Unhandled soft reset cause\n");
  1561. err = -EINVAL;
  1562. goto free_vport;
  1563. }
  1564. if (current_state <= __IDPF_VPORT_DOWN) {
  1565. idpf_send_delete_queues_msg(vport);
  1566. } else {
  1567. set_bit(IDPF_VPORT_DEL_QUEUES, vport->flags);
  1568. idpf_vport_stop(vport);
  1569. }
  1570. idpf_deinit_rss(vport);
  1571. /* We're passing in vport here because we need its wait_queue
  1572. * to send a message and it should be getting all the vport
  1573. * config data out of the adapter but we need to be careful not
  1574. * to add code to add_queues to change the vport config within
  1575. * vport itself as it will be wiped with a memcpy later.
  1576. */
  1577. err = idpf_send_add_queues_msg(vport, new_vport->num_txq,
  1578. new_vport->num_complq,
  1579. new_vport->num_rxq,
  1580. new_vport->num_bufq);
  1581. if (err)
  1582. goto err_reset;
  1583. /* Same comment as above regarding avoiding copying the wait_queues and
  1584. * mutexes applies here. We do not want to mess with those if possible.
  1585. */
  1586. memcpy(vport, new_vport, offsetof(struct idpf_vport, link_up));
  1587. if (reset_cause == IDPF_SR_Q_CHANGE)
  1588. idpf_vport_alloc_vec_indexes(vport);
  1589. err = idpf_set_real_num_queues(vport);
  1590. if (err)
  1591. goto err_open;
  1592. if (current_state == __IDPF_VPORT_UP)
  1593. err = idpf_vport_open(vport);
  1594. kfree(new_vport);
  1595. return err;
  1596. err_reset:
  1597. idpf_send_add_queues_msg(vport, vport->num_txq, vport->num_complq,
  1598. vport->num_rxq, vport->num_bufq);
  1599. err_open:
  1600. if (current_state == __IDPF_VPORT_UP)
  1601. idpf_vport_open(vport);
  1602. free_vport:
  1603. kfree(new_vport);
  1604. return err;
  1605. }
  1606. /**
  1607. * idpf_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1608. * @netdev: the netdevice
  1609. * @addr: address to add
  1610. *
  1611. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1612. * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock
  1613. * meaning we cannot sleep in this context. Due to this, we have to add the
  1614. * filter and send the virtchnl message asynchronously without waiting for the
  1615. * response from the other side. We won't know whether or not the operation
  1616. * actually succeeded until we get the message back. Returns 0 on success,
  1617. * negative on failure.
  1618. */
  1619. static int idpf_addr_sync(struct net_device *netdev, const u8 *addr)
  1620. {
  1621. struct idpf_netdev_priv *np = netdev_priv(netdev);
  1622. return idpf_add_mac_filter(np->vport, np, addr, true);
  1623. }
  1624. /**
  1625. * idpf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1626. * @netdev: the netdevice
  1627. * @addr: address to add
  1628. *
  1629. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1630. * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock
  1631. * meaning we cannot sleep in this context. Due to this we have to delete the
  1632. * filter and send the virtchnl message asynchronously without waiting for the
  1633. * return from the other side. We won't know whether or not the operation
  1634. * actually succeeded until we get the message back. Returns 0 on success,
  1635. * negative on failure.
  1636. */
  1637. static int idpf_addr_unsync(struct net_device *netdev, const u8 *addr)
  1638. {
  1639. struct idpf_netdev_priv *np = netdev_priv(netdev);
  1640. /* Under some circumstances, we might receive a request to delete
  1641. * our own device address from our uc list. Because we store the
  1642. * device address in the VSI's MAC filter list, we need to ignore
  1643. * such requests and not delete our device address from this list.
  1644. */
  1645. if (ether_addr_equal(addr, netdev->dev_addr))
  1646. return 0;
  1647. idpf_del_mac_filter(np->vport, np, addr, true);
  1648. return 0;
  1649. }
  1650. /**
  1651. * idpf_set_rx_mode - NDO callback to set the netdev filters
  1652. * @netdev: network interface device structure
  1653. *
  1654. * Stack takes addr_list_lock spinlock before calling our .set_rx_mode. We
  1655. * cannot sleep in this context.
  1656. */
  1657. static void idpf_set_rx_mode(struct net_device *netdev)
  1658. {
  1659. struct idpf_netdev_priv *np = netdev_priv(netdev);
  1660. struct idpf_vport_user_config_data *config_data;
  1661. struct idpf_adapter *adapter;
  1662. bool changed = false;
  1663. struct device *dev;
  1664. int err;
  1665. adapter = np->adapter;
  1666. dev = &adapter->pdev->dev;
  1667. if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) {
  1668. __dev_uc_sync(netdev, idpf_addr_sync, idpf_addr_unsync);
  1669. __dev_mc_sync(netdev, idpf_addr_sync, idpf_addr_unsync);
  1670. }
  1671. if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PROMISC))
  1672. return;
  1673. config_data = &adapter->vport_config[np->vport_idx]->user_config;
  1674. /* IFF_PROMISC enables both unicast and multicast promiscuous,
  1675. * while IFF_ALLMULTI only enables multicast such that:
  1676. *
  1677. * promisc + allmulti = unicast | multicast
  1678. * promisc + !allmulti = unicast | multicast
  1679. * !promisc + allmulti = multicast
  1680. */
  1681. if ((netdev->flags & IFF_PROMISC) &&
  1682. !test_and_set_bit(__IDPF_PROMISC_UC, config_data->user_flags)) {
  1683. changed = true;
  1684. dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n");
  1685. if (!test_and_set_bit(__IDPF_PROMISC_MC, adapter->flags))
  1686. dev_info(dev, "Entering multicast promiscuous mode\n");
  1687. }
  1688. if (!(netdev->flags & IFF_PROMISC) &&
  1689. test_and_clear_bit(__IDPF_PROMISC_UC, config_data->user_flags)) {
  1690. changed = true;
  1691. dev_info(dev, "Leaving promiscuous mode\n");
  1692. }
  1693. if (netdev->flags & IFF_ALLMULTI &&
  1694. !test_and_set_bit(__IDPF_PROMISC_MC, config_data->user_flags)) {
  1695. changed = true;
  1696. dev_info(dev, "Entering multicast promiscuous mode\n");
  1697. }
  1698. if (!(netdev->flags & (IFF_ALLMULTI | IFF_PROMISC)) &&
  1699. test_and_clear_bit(__IDPF_PROMISC_MC, config_data->user_flags)) {
  1700. changed = true;
  1701. dev_info(dev, "Leaving multicast promiscuous mode\n");
  1702. }
  1703. if (!changed)
  1704. return;
  1705. err = idpf_set_promiscuous(adapter, config_data, np->vport_id);
  1706. if (err)
  1707. dev_err(dev, "Failed to set promiscuous mode: %d\n", err);
  1708. }
  1709. /**
  1710. * idpf_vport_manage_rss_lut - disable/enable RSS
  1711. * @vport: the vport being changed
  1712. *
  1713. * In the event of disable request for RSS, this function will zero out RSS
  1714. * LUT, while in the event of enable request for RSS, it will reconfigure RSS
  1715. * LUT with the default LUT configuration.
  1716. */
  1717. static int idpf_vport_manage_rss_lut(struct idpf_vport *vport)
  1718. {
  1719. bool ena = idpf_is_feature_ena(vport, NETIF_F_RXHASH);
  1720. struct idpf_rss_data *rss_data;
  1721. u16 idx = vport->idx;
  1722. int lut_size;
  1723. rss_data = &vport->adapter->vport_config[idx]->user_config.rss_data;
  1724. lut_size = rss_data->rss_lut_size * sizeof(u32);
  1725. if (ena) {
  1726. /* This will contain the default or user configured LUT */
  1727. memcpy(rss_data->rss_lut, rss_data->cached_lut, lut_size);
  1728. } else {
  1729. /* Save a copy of the current LUT to be restored later if
  1730. * requested.
  1731. */
  1732. memcpy(rss_data->cached_lut, rss_data->rss_lut, lut_size);
  1733. /* Zero out the current LUT to disable */
  1734. memset(rss_data->rss_lut, 0, lut_size);
  1735. }
  1736. return idpf_config_rss(vport);
  1737. }
  1738. /**
  1739. * idpf_set_features - set the netdev feature flags
  1740. * @netdev: ptr to the netdev being adjusted
  1741. * @features: the feature set that the stack is suggesting
  1742. */
  1743. static int idpf_set_features(struct net_device *netdev,
  1744. netdev_features_t features)
  1745. {
  1746. netdev_features_t changed = netdev->features ^ features;
  1747. struct idpf_adapter *adapter;
  1748. struct idpf_vport *vport;
  1749. int err = 0;
  1750. idpf_vport_ctrl_lock(netdev);
  1751. vport = idpf_netdev_to_vport(netdev);
  1752. adapter = vport->adapter;
  1753. if (idpf_is_reset_in_prog(adapter)) {
  1754. dev_err(&adapter->pdev->dev, "Device is resetting, changing netdev features temporarily unavailable.\n");
  1755. err = -EBUSY;
  1756. goto unlock_mutex;
  1757. }
  1758. if (changed & NETIF_F_RXHASH) {
  1759. netdev->features ^= NETIF_F_RXHASH;
  1760. err = idpf_vport_manage_rss_lut(vport);
  1761. if (err)
  1762. goto unlock_mutex;
  1763. }
  1764. if (changed & NETIF_F_GRO_HW) {
  1765. netdev->features ^= NETIF_F_GRO_HW;
  1766. err = idpf_initiate_soft_reset(vport, IDPF_SR_RSC_CHANGE);
  1767. if (err)
  1768. goto unlock_mutex;
  1769. }
  1770. if (changed & NETIF_F_LOOPBACK) {
  1771. netdev->features ^= NETIF_F_LOOPBACK;
  1772. err = idpf_send_ena_dis_loopback_msg(vport);
  1773. }
  1774. unlock_mutex:
  1775. idpf_vport_ctrl_unlock(netdev);
  1776. return err;
  1777. }
  1778. /**
  1779. * idpf_open - Called when a network interface becomes active
  1780. * @netdev: network interface device structure
  1781. *
  1782. * The open entry point is called when a network interface is made
  1783. * active by the system (IFF_UP). At this point all resources needed
  1784. * for transmit and receive operations are allocated, the interrupt
  1785. * handler is registered with the OS, the netdev watchdog is enabled,
  1786. * and the stack is notified that the interface is ready.
  1787. *
  1788. * Returns 0 on success, negative value on failure
  1789. */
  1790. static int idpf_open(struct net_device *netdev)
  1791. {
  1792. struct idpf_vport *vport;
  1793. int err;
  1794. idpf_vport_ctrl_lock(netdev);
  1795. vport = idpf_netdev_to_vport(netdev);
  1796. err = idpf_set_real_num_queues(vport);
  1797. if (err)
  1798. goto unlock;
  1799. err = idpf_vport_open(vport);
  1800. unlock:
  1801. idpf_vport_ctrl_unlock(netdev);
  1802. return err;
  1803. }
  1804. /**
  1805. * idpf_change_mtu - NDO callback to change the MTU
  1806. * @netdev: network interface device structure
  1807. * @new_mtu: new value for maximum frame size
  1808. *
  1809. * Returns 0 on success, negative on failure
  1810. */
  1811. static int idpf_change_mtu(struct net_device *netdev, int new_mtu)
  1812. {
  1813. struct idpf_vport *vport;
  1814. int err;
  1815. idpf_vport_ctrl_lock(netdev);
  1816. vport = idpf_netdev_to_vport(netdev);
  1817. WRITE_ONCE(netdev->mtu, new_mtu);
  1818. err = idpf_initiate_soft_reset(vport, IDPF_SR_MTU_CHANGE);
  1819. idpf_vport_ctrl_unlock(netdev);
  1820. return err;
  1821. }
  1822. /**
  1823. * idpf_features_check - Validate packet conforms to limits
  1824. * @skb: skb buffer
  1825. * @netdev: This port's netdev
  1826. * @features: Offload features that the stack believes apply
  1827. */
  1828. static netdev_features_t idpf_features_check(struct sk_buff *skb,
  1829. struct net_device *netdev,
  1830. netdev_features_t features)
  1831. {
  1832. struct idpf_netdev_priv *np = netdev_priv(netdev);
  1833. u16 max_tx_hdr_size = np->max_tx_hdr_size;
  1834. size_t len;
  1835. /* No point in doing any of this if neither checksum nor GSO are
  1836. * being requested for this frame. We can rule out both by just
  1837. * checking for CHECKSUM_PARTIAL
  1838. */
  1839. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1840. return features;
  1841. /* We cannot support GSO if the MSS is going to be less than
  1842. * 88 bytes. If it is then we need to drop support for GSO.
  1843. */
  1844. if (skb_is_gso(skb) &&
  1845. (skb_shinfo(skb)->gso_size < IDPF_TX_TSO_MIN_MSS))
  1846. features &= ~NETIF_F_GSO_MASK;
  1847. /* Ensure MACLEN is <= 126 bytes (63 words) and not an odd size */
  1848. len = skb_network_offset(skb);
  1849. if (unlikely(len & ~(126)))
  1850. goto unsupported;
  1851. len = skb_network_header_len(skb);
  1852. if (unlikely(len > max_tx_hdr_size))
  1853. goto unsupported;
  1854. if (!skb->encapsulation)
  1855. return features;
  1856. /* L4TUNLEN can support 127 words */
  1857. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  1858. if (unlikely(len & ~(127 * 2)))
  1859. goto unsupported;
  1860. /* IPLEN can support at most 127 dwords */
  1861. len = skb_inner_network_header_len(skb);
  1862. if (unlikely(len > max_tx_hdr_size))
  1863. goto unsupported;
  1864. /* No need to validate L4LEN as TCP is the only protocol with a
  1865. * a flexible value and we support all possible values supported
  1866. * by TCP, which is at most 15 dwords
  1867. */
  1868. return features;
  1869. unsupported:
  1870. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  1871. }
  1872. /**
  1873. * idpf_set_mac - NDO callback to set port mac address
  1874. * @netdev: network interface device structure
  1875. * @p: pointer to an address structure
  1876. *
  1877. * Returns 0 on success, negative on failure
  1878. **/
  1879. static int idpf_set_mac(struct net_device *netdev, void *p)
  1880. {
  1881. struct idpf_netdev_priv *np = netdev_priv(netdev);
  1882. struct idpf_vport_config *vport_config;
  1883. struct sockaddr *addr = p;
  1884. u8 old_mac_addr[ETH_ALEN];
  1885. struct idpf_vport *vport;
  1886. int err = 0;
  1887. idpf_vport_ctrl_lock(netdev);
  1888. vport = idpf_netdev_to_vport(netdev);
  1889. if (!idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS,
  1890. VIRTCHNL2_CAP_MACFILTER)) {
  1891. dev_info(&vport->adapter->pdev->dev, "Setting MAC address is not supported\n");
  1892. err = -EOPNOTSUPP;
  1893. goto unlock_mutex;
  1894. }
  1895. if (!is_valid_ether_addr(addr->sa_data)) {
  1896. dev_info(&vport->adapter->pdev->dev, "Invalid MAC address: %pM\n",
  1897. addr->sa_data);
  1898. err = -EADDRNOTAVAIL;
  1899. goto unlock_mutex;
  1900. }
  1901. if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
  1902. goto unlock_mutex;
  1903. ether_addr_copy(old_mac_addr, vport->default_mac_addr);
  1904. ether_addr_copy(vport->default_mac_addr, addr->sa_data);
  1905. vport_config = vport->adapter->vport_config[vport->idx];
  1906. err = idpf_add_mac_filter(vport, np, addr->sa_data, false);
  1907. if (err) {
  1908. __idpf_del_mac_filter(vport_config, addr->sa_data);
  1909. ether_addr_copy(vport->default_mac_addr, netdev->dev_addr);
  1910. goto unlock_mutex;
  1911. }
  1912. if (is_valid_ether_addr(old_mac_addr))
  1913. __idpf_del_mac_filter(vport_config, old_mac_addr);
  1914. eth_hw_addr_set(netdev, addr->sa_data);
  1915. unlock_mutex:
  1916. idpf_vport_ctrl_unlock(netdev);
  1917. return err;
  1918. }
  1919. /**
  1920. * idpf_alloc_dma_mem - Allocate dma memory
  1921. * @hw: pointer to hw struct
  1922. * @mem: pointer to dma_mem struct
  1923. * @size: size of the memory to allocate
  1924. */
  1925. void *idpf_alloc_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem, u64 size)
  1926. {
  1927. struct idpf_adapter *adapter = hw->back;
  1928. size_t sz = ALIGN(size, 4096);
  1929. /* The control queue resources are freed under a spinlock, contiguous
  1930. * pages will avoid IOMMU remapping and the use vmap (and vunmap in
  1931. * dma_free_*() path.
  1932. */
  1933. mem->va = dma_alloc_attrs(&adapter->pdev->dev, sz, &mem->pa,
  1934. GFP_KERNEL, DMA_ATTR_FORCE_CONTIGUOUS);
  1935. mem->size = sz;
  1936. return mem->va;
  1937. }
  1938. /**
  1939. * idpf_free_dma_mem - Free the allocated dma memory
  1940. * @hw: pointer to hw struct
  1941. * @mem: pointer to dma_mem struct
  1942. */
  1943. void idpf_free_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem)
  1944. {
  1945. struct idpf_adapter *adapter = hw->back;
  1946. dma_free_attrs(&adapter->pdev->dev, mem->size,
  1947. mem->va, mem->pa, DMA_ATTR_FORCE_CONTIGUOUS);
  1948. mem->size = 0;
  1949. mem->va = NULL;
  1950. mem->pa = 0;
  1951. }
  1952. static const struct net_device_ops idpf_netdev_ops = {
  1953. .ndo_open = idpf_open,
  1954. .ndo_stop = idpf_stop,
  1955. .ndo_start_xmit = idpf_tx_start,
  1956. .ndo_features_check = idpf_features_check,
  1957. .ndo_set_rx_mode = idpf_set_rx_mode,
  1958. .ndo_validate_addr = eth_validate_addr,
  1959. .ndo_set_mac_address = idpf_set_mac,
  1960. .ndo_change_mtu = idpf_change_mtu,
  1961. .ndo_get_stats64 = idpf_get_stats64,
  1962. .ndo_set_features = idpf_set_features,
  1963. .ndo_tx_timeout = idpf_tx_timeout,
  1964. };