funeth_main.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. #include <linux/bpf.h>
  3. #include <linux/crash_dump.h>
  4. #include <linux/etherdevice.h>
  5. #include <linux/ethtool.h>
  6. #include <linux/filter.h>
  7. #include <linux/idr.h>
  8. #include <linux/if_vlan.h>
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/pci.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/inetdevice.h>
  14. #include "funeth.h"
  15. #include "funeth_devlink.h"
  16. #include "funeth_ktls.h"
  17. #include "fun_port.h"
  18. #include "fun_queue.h"
  19. #include "funeth_txrx.h"
  20. #define ADMIN_SQ_DEPTH 32
  21. #define ADMIN_CQ_DEPTH 64
  22. #define ADMIN_RQ_DEPTH 16
  23. /* Default number of Tx/Rx queues. */
  24. #define FUN_DFLT_QUEUES 16U
  25. enum {
  26. FUN_SERV_RES_CHANGE = FUN_SERV_FIRST_AVAIL,
  27. FUN_SERV_DEL_PORTS,
  28. };
  29. static const struct pci_device_id funeth_id_table[] = {
  30. { PCI_VDEVICE(FUNGIBLE, 0x0101) },
  31. { PCI_VDEVICE(FUNGIBLE, 0x0181) },
  32. { 0, }
  33. };
  34. /* Issue a port write admin command with @n key/value pairs. */
  35. static int fun_port_write_cmds(struct funeth_priv *fp, unsigned int n,
  36. const int *keys, const u64 *data)
  37. {
  38. unsigned int cmd_size, i;
  39. union {
  40. struct fun_admin_port_req req;
  41. struct fun_admin_port_rsp rsp;
  42. u8 v[ADMIN_SQE_SIZE];
  43. } cmd;
  44. cmd_size = offsetof(struct fun_admin_port_req, u.write.write48) +
  45. n * sizeof(struct fun_admin_write48_req);
  46. if (cmd_size > sizeof(cmd) || cmd_size > ADMIN_RSP_MAX_LEN)
  47. return -EINVAL;
  48. cmd.req.common = FUN_ADMIN_REQ_COMMON_INIT2(FUN_ADMIN_OP_PORT,
  49. cmd_size);
  50. cmd.req.u.write =
  51. FUN_ADMIN_PORT_WRITE_REQ_INIT(FUN_ADMIN_SUBOP_WRITE, 0,
  52. fp->netdev->dev_port);
  53. for (i = 0; i < n; i++)
  54. cmd.req.u.write.write48[i] =
  55. FUN_ADMIN_WRITE48_REQ_INIT(keys[i], data[i]);
  56. return fun_submit_admin_sync_cmd(fp->fdev, &cmd.req.common,
  57. &cmd.rsp, cmd_size, 0);
  58. }
  59. int fun_port_write_cmd(struct funeth_priv *fp, int key, u64 data)
  60. {
  61. return fun_port_write_cmds(fp, 1, &key, &data);
  62. }
  63. /* Issue a port read admin command with @n key/value pairs. */
  64. static int fun_port_read_cmds(struct funeth_priv *fp, unsigned int n,
  65. const int *keys, u64 *data)
  66. {
  67. const struct fun_admin_read48_rsp *r48rsp;
  68. unsigned int cmd_size, i;
  69. int rc;
  70. union {
  71. struct fun_admin_port_req req;
  72. struct fun_admin_port_rsp rsp;
  73. u8 v[ADMIN_SQE_SIZE];
  74. } cmd;
  75. cmd_size = offsetof(struct fun_admin_port_req, u.read.read48) +
  76. n * sizeof(struct fun_admin_read48_req);
  77. if (cmd_size > sizeof(cmd) || cmd_size > ADMIN_RSP_MAX_LEN)
  78. return -EINVAL;
  79. cmd.req.common = FUN_ADMIN_REQ_COMMON_INIT2(FUN_ADMIN_OP_PORT,
  80. cmd_size);
  81. cmd.req.u.read =
  82. FUN_ADMIN_PORT_READ_REQ_INIT(FUN_ADMIN_SUBOP_READ, 0,
  83. fp->netdev->dev_port);
  84. for (i = 0; i < n; i++)
  85. cmd.req.u.read.read48[i] = FUN_ADMIN_READ48_REQ_INIT(keys[i]);
  86. rc = fun_submit_admin_sync_cmd(fp->fdev, &cmd.req.common,
  87. &cmd.rsp, cmd_size, 0);
  88. if (rc)
  89. return rc;
  90. for (r48rsp = cmd.rsp.u.read.read48, i = 0; i < n; i++, r48rsp++) {
  91. data[i] = FUN_ADMIN_READ48_RSP_DATA_G(r48rsp->key_to_data);
  92. dev_dbg(fp->fdev->dev,
  93. "port_read_rsp lport=%u (key_to_data=0x%llx) key=%d data:%lld retval:%lld",
  94. fp->lport, r48rsp->key_to_data, keys[i], data[i],
  95. FUN_ADMIN_READ48_RSP_RET_G(r48rsp->key_to_data));
  96. }
  97. return 0;
  98. }
  99. int fun_port_read_cmd(struct funeth_priv *fp, int key, u64 *data)
  100. {
  101. return fun_port_read_cmds(fp, 1, &key, data);
  102. }
  103. static void fun_report_link(struct net_device *netdev)
  104. {
  105. if (netif_carrier_ok(netdev)) {
  106. const struct funeth_priv *fp = netdev_priv(netdev);
  107. const char *fec = "", *pause = "";
  108. int speed = fp->link_speed;
  109. char unit = 'M';
  110. if (fp->link_speed >= SPEED_1000) {
  111. speed /= 1000;
  112. unit = 'G';
  113. }
  114. if (fp->active_fec & FUN_PORT_FEC_RS)
  115. fec = ", RS-FEC";
  116. else if (fp->active_fec & FUN_PORT_FEC_FC)
  117. fec = ", BASER-FEC";
  118. if ((fp->active_fc & FUN_PORT_CAP_PAUSE_MASK) == FUN_PORT_CAP_PAUSE_MASK)
  119. pause = ", Tx/Rx PAUSE";
  120. else if (fp->active_fc & FUN_PORT_CAP_RX_PAUSE)
  121. pause = ", Rx PAUSE";
  122. else if (fp->active_fc & FUN_PORT_CAP_TX_PAUSE)
  123. pause = ", Tx PAUSE";
  124. netdev_info(netdev, "Link up at %d %cb/s full-duplex%s%s\n",
  125. speed, unit, pause, fec);
  126. } else {
  127. netdev_info(netdev, "Link down\n");
  128. }
  129. }
  130. static int fun_adi_write(struct fun_dev *fdev, enum fun_admin_adi_attr attr,
  131. unsigned int adi_id, const struct fun_adi_param *param)
  132. {
  133. struct fun_admin_adi_req req = {
  134. .common = FUN_ADMIN_REQ_COMMON_INIT2(FUN_ADMIN_OP_ADI,
  135. sizeof(req)),
  136. .u.write.subop = FUN_ADMIN_SUBOP_WRITE,
  137. .u.write.attribute = attr,
  138. .u.write.id = cpu_to_be32(adi_id),
  139. .u.write.param = *param
  140. };
  141. return fun_submit_admin_sync_cmd(fdev, &req.common, NULL, 0, 0);
  142. }
  143. /* Configure RSS for the given port. @op determines whether a new RSS context
  144. * is to be created or whether an existing one should be reconfigured. The
  145. * remaining parameters specify the hashing algorithm, key, and indirection
  146. * table.
  147. *
  148. * This initiates packet delivery to the Rx queues set in the indirection
  149. * table.
  150. */
  151. int fun_config_rss(struct net_device *dev, int algo, const u8 *key,
  152. const u32 *qtable, u8 op)
  153. {
  154. struct funeth_priv *fp = netdev_priv(dev);
  155. unsigned int table_len = fp->indir_table_nentries;
  156. unsigned int len = FUN_ETH_RSS_MAX_KEY_SIZE + sizeof(u32) * table_len;
  157. struct funeth_rxq **rxqs = rtnl_dereference(fp->rxqs);
  158. union {
  159. struct {
  160. struct fun_admin_rss_req req;
  161. struct fun_dataop_gl gl;
  162. };
  163. struct fun_admin_generic_create_rsp rsp;
  164. } cmd;
  165. __be32 *indir_tab;
  166. u16 flags;
  167. int rc;
  168. if (op != FUN_ADMIN_SUBOP_CREATE && fp->rss_hw_id == FUN_HCI_ID_INVALID)
  169. return -EINVAL;
  170. flags = op == FUN_ADMIN_SUBOP_CREATE ?
  171. FUN_ADMIN_RES_CREATE_FLAG_ALLOCATOR : 0;
  172. cmd.req.common = FUN_ADMIN_REQ_COMMON_INIT2(FUN_ADMIN_OP_RSS,
  173. sizeof(cmd));
  174. cmd.req.u.create =
  175. FUN_ADMIN_RSS_CREATE_REQ_INIT(op, flags, fp->rss_hw_id,
  176. dev->dev_port, algo,
  177. FUN_ETH_RSS_MAX_KEY_SIZE,
  178. table_len, 0,
  179. FUN_ETH_RSS_MAX_KEY_SIZE);
  180. cmd.req.u.create.dataop = FUN_DATAOP_HDR_INIT(1, 0, 1, 0, len);
  181. fun_dataop_gl_init(&cmd.gl, 0, 0, len, fp->rss_dma_addr);
  182. /* write the key and indirection table into the RSS DMA area */
  183. memcpy(fp->rss_cfg, key, FUN_ETH_RSS_MAX_KEY_SIZE);
  184. indir_tab = fp->rss_cfg + FUN_ETH_RSS_MAX_KEY_SIZE;
  185. for (rc = 0; rc < table_len; rc++)
  186. *indir_tab++ = cpu_to_be32(rxqs[*qtable++]->hw_cqid);
  187. rc = fun_submit_admin_sync_cmd(fp->fdev, &cmd.req.common,
  188. &cmd.rsp, sizeof(cmd.rsp), 0);
  189. if (!rc && op == FUN_ADMIN_SUBOP_CREATE)
  190. fp->rss_hw_id = be32_to_cpu(cmd.rsp.id);
  191. return rc;
  192. }
  193. /* Destroy the HW RSS conntext associated with the given port. This also stops
  194. * all packet delivery to our Rx queues.
  195. */
  196. static void fun_destroy_rss(struct funeth_priv *fp)
  197. {
  198. if (fp->rss_hw_id != FUN_HCI_ID_INVALID) {
  199. fun_res_destroy(fp->fdev, FUN_ADMIN_OP_RSS, 0, fp->rss_hw_id);
  200. fp->rss_hw_id = FUN_HCI_ID_INVALID;
  201. }
  202. }
  203. static void fun_irq_aff_notify(struct irq_affinity_notify *notify,
  204. const cpumask_t *mask)
  205. {
  206. struct fun_irq *p = container_of(notify, struct fun_irq, aff_notify);
  207. cpumask_copy(&p->affinity_mask, mask);
  208. }
  209. static void fun_irq_aff_release(struct kref __always_unused *ref)
  210. {
  211. }
  212. /* Allocate an IRQ structure, assign an MSI-X index and initial affinity to it,
  213. * and add it to the IRQ XArray.
  214. */
  215. static struct fun_irq *fun_alloc_qirq(struct funeth_priv *fp, unsigned int idx,
  216. int node, unsigned int xa_idx_offset)
  217. {
  218. struct fun_irq *irq;
  219. int cpu, res;
  220. cpu = cpumask_local_spread(idx, node);
  221. node = cpu_to_mem(cpu);
  222. irq = kzalloc_node(sizeof(*irq), GFP_KERNEL, node);
  223. if (!irq)
  224. return ERR_PTR(-ENOMEM);
  225. res = fun_reserve_irqs(fp->fdev, 1, &irq->irq_idx);
  226. if (res != 1)
  227. goto free_irq;
  228. res = xa_insert(&fp->irqs, idx + xa_idx_offset, irq, GFP_KERNEL);
  229. if (res)
  230. goto release_irq;
  231. irq->irq = pci_irq_vector(fp->pdev, irq->irq_idx);
  232. cpumask_set_cpu(cpu, &irq->affinity_mask);
  233. irq->aff_notify.notify = fun_irq_aff_notify;
  234. irq->aff_notify.release = fun_irq_aff_release;
  235. irq->state = FUN_IRQ_INIT;
  236. return irq;
  237. release_irq:
  238. fun_release_irqs(fp->fdev, 1, &irq->irq_idx);
  239. free_irq:
  240. kfree(irq);
  241. return ERR_PTR(res);
  242. }
  243. static void fun_free_qirq(struct funeth_priv *fp, struct fun_irq *irq)
  244. {
  245. netif_napi_del(&irq->napi);
  246. fun_release_irqs(fp->fdev, 1, &irq->irq_idx);
  247. kfree(irq);
  248. }
  249. /* Release the IRQs reserved for Tx/Rx queues that aren't being used. */
  250. static void fun_prune_queue_irqs(struct net_device *dev)
  251. {
  252. struct funeth_priv *fp = netdev_priv(dev);
  253. unsigned int nreleased = 0;
  254. struct fun_irq *irq;
  255. unsigned long idx;
  256. xa_for_each(&fp->irqs, idx, irq) {
  257. if (irq->txq || irq->rxq) /* skip those in use */
  258. continue;
  259. xa_erase(&fp->irqs, idx);
  260. fun_free_qirq(fp, irq);
  261. nreleased++;
  262. if (idx < fp->rx_irq_ofst)
  263. fp->num_tx_irqs--;
  264. else
  265. fp->num_rx_irqs--;
  266. }
  267. netif_info(fp, intr, dev, "Released %u queue IRQs\n", nreleased);
  268. }
  269. /* Reserve IRQs, one per queue, to acommodate the requested queue numbers @ntx
  270. * and @nrx. IRQs are added incrementally to those we already have.
  271. * We hold on to allocated IRQs until garbage collection of unused IRQs is
  272. * separately requested.
  273. */
  274. static int fun_alloc_queue_irqs(struct net_device *dev, unsigned int ntx,
  275. unsigned int nrx)
  276. {
  277. struct funeth_priv *fp = netdev_priv(dev);
  278. int node = dev_to_node(&fp->pdev->dev);
  279. struct fun_irq *irq;
  280. unsigned int i;
  281. for (i = fp->num_tx_irqs; i < ntx; i++) {
  282. irq = fun_alloc_qirq(fp, i, node, 0);
  283. if (IS_ERR(irq))
  284. return PTR_ERR(irq);
  285. fp->num_tx_irqs++;
  286. netif_napi_add_tx(dev, &irq->napi, fun_txq_napi_poll);
  287. }
  288. for (i = fp->num_rx_irqs; i < nrx; i++) {
  289. irq = fun_alloc_qirq(fp, i, node, fp->rx_irq_ofst);
  290. if (IS_ERR(irq))
  291. return PTR_ERR(irq);
  292. fp->num_rx_irqs++;
  293. netif_napi_add(dev, &irq->napi, fun_rxq_napi_poll);
  294. }
  295. netif_info(fp, intr, dev, "Reserved %u/%u IRQs for Tx/Rx queues\n",
  296. ntx, nrx);
  297. return 0;
  298. }
  299. static void free_txqs(struct funeth_txq **txqs, unsigned int nqs,
  300. unsigned int start, int state)
  301. {
  302. unsigned int i;
  303. for (i = start; i < nqs && txqs[i]; i++)
  304. txqs[i] = funeth_txq_free(txqs[i], state);
  305. }
  306. static int alloc_txqs(struct net_device *dev, struct funeth_txq **txqs,
  307. unsigned int nqs, unsigned int depth, unsigned int start,
  308. int state)
  309. {
  310. struct funeth_priv *fp = netdev_priv(dev);
  311. unsigned int i;
  312. int err;
  313. for (i = start; i < nqs; i++) {
  314. err = funeth_txq_create(dev, i, depth, xa_load(&fp->irqs, i),
  315. state, &txqs[i]);
  316. if (err) {
  317. free_txqs(txqs, nqs, start, FUN_QSTATE_DESTROYED);
  318. return err;
  319. }
  320. }
  321. return 0;
  322. }
  323. static void free_rxqs(struct funeth_rxq **rxqs, unsigned int nqs,
  324. unsigned int start, int state)
  325. {
  326. unsigned int i;
  327. for (i = start; i < nqs && rxqs[i]; i++)
  328. rxqs[i] = funeth_rxq_free(rxqs[i], state);
  329. }
  330. static int alloc_rxqs(struct net_device *dev, struct funeth_rxq **rxqs,
  331. unsigned int nqs, unsigned int ncqe, unsigned int nrqe,
  332. unsigned int start, int state)
  333. {
  334. struct funeth_priv *fp = netdev_priv(dev);
  335. unsigned int i;
  336. int err;
  337. for (i = start; i < nqs; i++) {
  338. err = funeth_rxq_create(dev, i, ncqe, nrqe,
  339. xa_load(&fp->irqs, i + fp->rx_irq_ofst),
  340. state, &rxqs[i]);
  341. if (err) {
  342. free_rxqs(rxqs, nqs, start, FUN_QSTATE_DESTROYED);
  343. return err;
  344. }
  345. }
  346. return 0;
  347. }
  348. static void free_xdpqs(struct funeth_txq **xdpqs, unsigned int nqs,
  349. unsigned int start, int state)
  350. {
  351. unsigned int i;
  352. for (i = start; i < nqs && xdpqs[i]; i++)
  353. xdpqs[i] = funeth_txq_free(xdpqs[i], state);
  354. if (state == FUN_QSTATE_DESTROYED)
  355. kfree(xdpqs);
  356. }
  357. static struct funeth_txq **alloc_xdpqs(struct net_device *dev, unsigned int nqs,
  358. unsigned int depth, unsigned int start,
  359. int state)
  360. {
  361. struct funeth_txq **xdpqs;
  362. unsigned int i;
  363. int err;
  364. xdpqs = kcalloc(nqs, sizeof(*xdpqs), GFP_KERNEL);
  365. if (!xdpqs)
  366. return ERR_PTR(-ENOMEM);
  367. for (i = start; i < nqs; i++) {
  368. err = funeth_txq_create(dev, i, depth, NULL, state, &xdpqs[i]);
  369. if (err) {
  370. free_xdpqs(xdpqs, nqs, start, FUN_QSTATE_DESTROYED);
  371. return ERR_PTR(err);
  372. }
  373. }
  374. return xdpqs;
  375. }
  376. static void fun_free_rings(struct net_device *netdev, struct fun_qset *qset)
  377. {
  378. struct funeth_priv *fp = netdev_priv(netdev);
  379. struct funeth_txq **xdpqs = qset->xdpqs;
  380. struct funeth_rxq **rxqs = qset->rxqs;
  381. /* qset may not specify any queues to operate on. In that case the
  382. * currently installed queues are implied.
  383. */
  384. if (!rxqs) {
  385. rxqs = rtnl_dereference(fp->rxqs);
  386. xdpqs = rtnl_dereference(fp->xdpqs);
  387. qset->txqs = fp->txqs;
  388. qset->nrxqs = netdev->real_num_rx_queues;
  389. qset->ntxqs = netdev->real_num_tx_queues;
  390. qset->nxdpqs = fp->num_xdpqs;
  391. }
  392. if (!rxqs)
  393. return;
  394. if (rxqs == rtnl_dereference(fp->rxqs)) {
  395. rcu_assign_pointer(fp->rxqs, NULL);
  396. rcu_assign_pointer(fp->xdpqs, NULL);
  397. synchronize_net();
  398. fp->txqs = NULL;
  399. }
  400. free_rxqs(rxqs, qset->nrxqs, qset->rxq_start, qset->state);
  401. free_txqs(qset->txqs, qset->ntxqs, qset->txq_start, qset->state);
  402. free_xdpqs(xdpqs, qset->nxdpqs, qset->xdpq_start, qset->state);
  403. if (qset->state == FUN_QSTATE_DESTROYED)
  404. kfree(rxqs);
  405. /* Tell the caller which queues were operated on. */
  406. qset->rxqs = rxqs;
  407. qset->xdpqs = xdpqs;
  408. }
  409. static int fun_alloc_rings(struct net_device *netdev, struct fun_qset *qset)
  410. {
  411. struct funeth_txq **xdpqs = NULL, **txqs;
  412. struct funeth_rxq **rxqs;
  413. int err;
  414. err = fun_alloc_queue_irqs(netdev, qset->ntxqs, qset->nrxqs);
  415. if (err)
  416. return err;
  417. rxqs = kcalloc(qset->ntxqs + qset->nrxqs, sizeof(*rxqs), GFP_KERNEL);
  418. if (!rxqs)
  419. return -ENOMEM;
  420. if (qset->nxdpqs) {
  421. xdpqs = alloc_xdpqs(netdev, qset->nxdpqs, qset->sq_depth,
  422. qset->xdpq_start, qset->state);
  423. if (IS_ERR(xdpqs)) {
  424. err = PTR_ERR(xdpqs);
  425. goto free_qvec;
  426. }
  427. }
  428. txqs = (struct funeth_txq **)&rxqs[qset->nrxqs];
  429. err = alloc_txqs(netdev, txqs, qset->ntxqs, qset->sq_depth,
  430. qset->txq_start, qset->state);
  431. if (err)
  432. goto free_xdpqs;
  433. err = alloc_rxqs(netdev, rxqs, qset->nrxqs, qset->cq_depth,
  434. qset->rq_depth, qset->rxq_start, qset->state);
  435. if (err)
  436. goto free_txqs;
  437. qset->rxqs = rxqs;
  438. qset->txqs = txqs;
  439. qset->xdpqs = xdpqs;
  440. return 0;
  441. free_txqs:
  442. free_txqs(txqs, qset->ntxqs, qset->txq_start, FUN_QSTATE_DESTROYED);
  443. free_xdpqs:
  444. free_xdpqs(xdpqs, qset->nxdpqs, qset->xdpq_start, FUN_QSTATE_DESTROYED);
  445. free_qvec:
  446. kfree(rxqs);
  447. return err;
  448. }
  449. /* Take queues to the next level. Presently this means creating them on the
  450. * device.
  451. */
  452. static int fun_advance_ring_state(struct net_device *dev, struct fun_qset *qset)
  453. {
  454. struct funeth_priv *fp = netdev_priv(dev);
  455. int i, err;
  456. for (i = 0; i < qset->nrxqs; i++) {
  457. err = fun_rxq_create_dev(qset->rxqs[i],
  458. xa_load(&fp->irqs,
  459. i + fp->rx_irq_ofst));
  460. if (err)
  461. goto out;
  462. }
  463. for (i = 0; i < qset->ntxqs; i++) {
  464. err = fun_txq_create_dev(qset->txqs[i], xa_load(&fp->irqs, i));
  465. if (err)
  466. goto out;
  467. }
  468. for (i = 0; i < qset->nxdpqs; i++) {
  469. err = fun_txq_create_dev(qset->xdpqs[i], NULL);
  470. if (err)
  471. goto out;
  472. }
  473. return 0;
  474. out:
  475. fun_free_rings(dev, qset);
  476. return err;
  477. }
  478. static int fun_port_create(struct net_device *netdev)
  479. {
  480. struct funeth_priv *fp = netdev_priv(netdev);
  481. union {
  482. struct fun_admin_port_req req;
  483. struct fun_admin_port_rsp rsp;
  484. } cmd;
  485. int rc;
  486. if (fp->lport != INVALID_LPORT)
  487. return 0;
  488. cmd.req.common = FUN_ADMIN_REQ_COMMON_INIT2(FUN_ADMIN_OP_PORT,
  489. sizeof(cmd.req));
  490. cmd.req.u.create =
  491. FUN_ADMIN_PORT_CREATE_REQ_INIT(FUN_ADMIN_SUBOP_CREATE, 0,
  492. netdev->dev_port);
  493. rc = fun_submit_admin_sync_cmd(fp->fdev, &cmd.req.common, &cmd.rsp,
  494. sizeof(cmd.rsp), 0);
  495. if (!rc)
  496. fp->lport = be16_to_cpu(cmd.rsp.u.create.lport);
  497. return rc;
  498. }
  499. static int fun_port_destroy(struct net_device *netdev)
  500. {
  501. struct funeth_priv *fp = netdev_priv(netdev);
  502. if (fp->lport == INVALID_LPORT)
  503. return 0;
  504. fp->lport = INVALID_LPORT;
  505. return fun_res_destroy(fp->fdev, FUN_ADMIN_OP_PORT, 0,
  506. netdev->dev_port);
  507. }
  508. static int fun_eth_create(struct funeth_priv *fp)
  509. {
  510. union {
  511. struct fun_admin_eth_req req;
  512. struct fun_admin_generic_create_rsp rsp;
  513. } cmd;
  514. int rc;
  515. cmd.req.common = FUN_ADMIN_REQ_COMMON_INIT2(FUN_ADMIN_OP_ETH,
  516. sizeof(cmd.req));
  517. cmd.req.u.create = FUN_ADMIN_ETH_CREATE_REQ_INIT(
  518. FUN_ADMIN_SUBOP_CREATE,
  519. FUN_ADMIN_RES_CREATE_FLAG_ALLOCATOR,
  520. 0, fp->netdev->dev_port);
  521. rc = fun_submit_admin_sync_cmd(fp->fdev, &cmd.req.common, &cmd.rsp,
  522. sizeof(cmd.rsp), 0);
  523. return rc ? rc : be32_to_cpu(cmd.rsp.id);
  524. }
  525. static int fun_vi_create(struct funeth_priv *fp)
  526. {
  527. struct fun_admin_vi_req req = {
  528. .common = FUN_ADMIN_REQ_COMMON_INIT2(FUN_ADMIN_OP_VI,
  529. sizeof(req)),
  530. .u.create = FUN_ADMIN_VI_CREATE_REQ_INIT(FUN_ADMIN_SUBOP_CREATE,
  531. 0,
  532. fp->netdev->dev_port,
  533. fp->netdev->dev_port)
  534. };
  535. return fun_submit_admin_sync_cmd(fp->fdev, &req.common, NULL, 0, 0);
  536. }
  537. /* Helper to create an ETH flow and bind an SQ to it.
  538. * Returns the ETH id (>= 0) on success or a negative error.
  539. */
  540. int fun_create_and_bind_tx(struct funeth_priv *fp, u32 sqid)
  541. {
  542. int rc, ethid;
  543. ethid = fun_eth_create(fp);
  544. if (ethid >= 0) {
  545. rc = fun_bind(fp->fdev, FUN_ADMIN_BIND_TYPE_EPSQ, sqid,
  546. FUN_ADMIN_BIND_TYPE_ETH, ethid);
  547. if (rc) {
  548. fun_res_destroy(fp->fdev, FUN_ADMIN_OP_ETH, 0, ethid);
  549. ethid = rc;
  550. }
  551. }
  552. return ethid;
  553. }
  554. static irqreturn_t fun_queue_irq_handler(int irq, void *data)
  555. {
  556. struct fun_irq *p = data;
  557. if (p->rxq) {
  558. prefetch(p->rxq->next_cqe_info);
  559. p->rxq->irq_cnt++;
  560. }
  561. napi_schedule_irqoff(&p->napi);
  562. return IRQ_HANDLED;
  563. }
  564. static int fun_enable_irqs(struct net_device *dev)
  565. {
  566. struct funeth_priv *fp = netdev_priv(dev);
  567. unsigned long idx, last;
  568. unsigned int qidx;
  569. struct fun_irq *p;
  570. const char *qtype;
  571. int err;
  572. xa_for_each(&fp->irqs, idx, p) {
  573. if (p->txq) {
  574. qtype = "tx";
  575. qidx = p->txq->qidx;
  576. } else if (p->rxq) {
  577. qtype = "rx";
  578. qidx = p->rxq->qidx;
  579. } else {
  580. continue;
  581. }
  582. if (p->state != FUN_IRQ_INIT)
  583. continue;
  584. snprintf(p->name, sizeof(p->name) - 1, "%s-%s-%u", dev->name,
  585. qtype, qidx);
  586. err = request_irq(p->irq, fun_queue_irq_handler, 0, p->name, p);
  587. if (err) {
  588. netdev_err(dev, "Failed to allocate IRQ %u, err %d\n",
  589. p->irq, err);
  590. goto unroll;
  591. }
  592. p->state = FUN_IRQ_REQUESTED;
  593. }
  594. xa_for_each(&fp->irqs, idx, p) {
  595. if (p->state != FUN_IRQ_REQUESTED)
  596. continue;
  597. irq_set_affinity_notifier(p->irq, &p->aff_notify);
  598. irq_set_affinity_and_hint(p->irq, &p->affinity_mask);
  599. napi_enable(&p->napi);
  600. p->state = FUN_IRQ_ENABLED;
  601. }
  602. return 0;
  603. unroll:
  604. last = idx - 1;
  605. xa_for_each_range(&fp->irqs, idx, p, 0, last)
  606. if (p->state == FUN_IRQ_REQUESTED) {
  607. free_irq(p->irq, p);
  608. p->state = FUN_IRQ_INIT;
  609. }
  610. return err;
  611. }
  612. static void fun_disable_one_irq(struct fun_irq *irq)
  613. {
  614. napi_disable(&irq->napi);
  615. irq_set_affinity_notifier(irq->irq, NULL);
  616. irq_update_affinity_hint(irq->irq, NULL);
  617. free_irq(irq->irq, irq);
  618. irq->state = FUN_IRQ_INIT;
  619. }
  620. static void fun_disable_irqs(struct net_device *dev)
  621. {
  622. struct funeth_priv *fp = netdev_priv(dev);
  623. struct fun_irq *p;
  624. unsigned long idx;
  625. xa_for_each(&fp->irqs, idx, p)
  626. if (p->state == FUN_IRQ_ENABLED)
  627. fun_disable_one_irq(p);
  628. }
  629. static void fun_down(struct net_device *dev, struct fun_qset *qset)
  630. {
  631. struct funeth_priv *fp = netdev_priv(dev);
  632. /* If we don't have queues the data path is already down.
  633. * Note netif_running(dev) may be true.
  634. */
  635. if (!rcu_access_pointer(fp->rxqs))
  636. return;
  637. /* It is also down if the queues aren't on the device. */
  638. if (fp->txqs[0]->init_state >= FUN_QSTATE_INIT_FULL) {
  639. netif_info(fp, ifdown, dev,
  640. "Tearing down data path on device\n");
  641. fun_port_write_cmd(fp, FUN_ADMIN_PORT_KEY_DISABLE, 0);
  642. netif_carrier_off(dev);
  643. netif_tx_disable(dev);
  644. fun_destroy_rss(fp);
  645. fun_res_destroy(fp->fdev, FUN_ADMIN_OP_VI, 0, dev->dev_port);
  646. fun_disable_irqs(dev);
  647. }
  648. fun_free_rings(dev, qset);
  649. }
  650. static int fun_up(struct net_device *dev, struct fun_qset *qset)
  651. {
  652. static const int port_keys[] = {
  653. FUN_ADMIN_PORT_KEY_STATS_DMA_LOW,
  654. FUN_ADMIN_PORT_KEY_STATS_DMA_HIGH,
  655. FUN_ADMIN_PORT_KEY_ENABLE
  656. };
  657. struct funeth_priv *fp = netdev_priv(dev);
  658. u64 vals[] = {
  659. lower_32_bits(fp->stats_dma_addr),
  660. upper_32_bits(fp->stats_dma_addr),
  661. FUN_PORT_FLAG_ENABLE_NOTIFY
  662. };
  663. int err;
  664. netif_info(fp, ifup, dev, "Setting up data path on device\n");
  665. if (qset->rxqs[0]->init_state < FUN_QSTATE_INIT_FULL) {
  666. err = fun_advance_ring_state(dev, qset);
  667. if (err)
  668. return err;
  669. }
  670. err = fun_vi_create(fp);
  671. if (err)
  672. goto free_queues;
  673. fp->txqs = qset->txqs;
  674. rcu_assign_pointer(fp->rxqs, qset->rxqs);
  675. rcu_assign_pointer(fp->xdpqs, qset->xdpqs);
  676. err = fun_enable_irqs(dev);
  677. if (err)
  678. goto destroy_vi;
  679. if (fp->rss_cfg) {
  680. err = fun_config_rss(dev, fp->hash_algo, fp->rss_key,
  681. fp->indir_table, FUN_ADMIN_SUBOP_CREATE);
  682. } else {
  683. /* The non-RSS case has only 1 queue. */
  684. err = fun_bind(fp->fdev, FUN_ADMIN_BIND_TYPE_VI, dev->dev_port,
  685. FUN_ADMIN_BIND_TYPE_EPCQ,
  686. qset->rxqs[0]->hw_cqid);
  687. }
  688. if (err)
  689. goto disable_irqs;
  690. err = fun_port_write_cmds(fp, 3, port_keys, vals);
  691. if (err)
  692. goto free_rss;
  693. netif_tx_start_all_queues(dev);
  694. return 0;
  695. free_rss:
  696. fun_destroy_rss(fp);
  697. disable_irqs:
  698. fun_disable_irqs(dev);
  699. destroy_vi:
  700. fun_res_destroy(fp->fdev, FUN_ADMIN_OP_VI, 0, dev->dev_port);
  701. free_queues:
  702. fun_free_rings(dev, qset);
  703. return err;
  704. }
  705. static int funeth_open(struct net_device *netdev)
  706. {
  707. struct funeth_priv *fp = netdev_priv(netdev);
  708. struct fun_qset qset = {
  709. .nrxqs = netdev->real_num_rx_queues,
  710. .ntxqs = netdev->real_num_tx_queues,
  711. .nxdpqs = fp->num_xdpqs,
  712. .cq_depth = fp->cq_depth,
  713. .rq_depth = fp->rq_depth,
  714. .sq_depth = fp->sq_depth,
  715. .state = FUN_QSTATE_INIT_FULL,
  716. };
  717. int rc;
  718. rc = fun_alloc_rings(netdev, &qset);
  719. if (rc)
  720. return rc;
  721. rc = fun_up(netdev, &qset);
  722. if (rc) {
  723. qset.state = FUN_QSTATE_DESTROYED;
  724. fun_free_rings(netdev, &qset);
  725. }
  726. return rc;
  727. }
  728. static int funeth_close(struct net_device *netdev)
  729. {
  730. struct fun_qset qset = { .state = FUN_QSTATE_DESTROYED };
  731. fun_down(netdev, &qset);
  732. return 0;
  733. }
  734. static void fun_get_stats64(struct net_device *netdev,
  735. struct rtnl_link_stats64 *stats)
  736. {
  737. struct funeth_priv *fp = netdev_priv(netdev);
  738. struct funeth_txq **xdpqs;
  739. struct funeth_rxq **rxqs;
  740. unsigned int i, start;
  741. stats->tx_packets = fp->tx_packets;
  742. stats->tx_bytes = fp->tx_bytes;
  743. stats->tx_dropped = fp->tx_dropped;
  744. stats->rx_packets = fp->rx_packets;
  745. stats->rx_bytes = fp->rx_bytes;
  746. stats->rx_dropped = fp->rx_dropped;
  747. rcu_read_lock();
  748. rxqs = rcu_dereference(fp->rxqs);
  749. if (!rxqs)
  750. goto unlock;
  751. for (i = 0; i < netdev->real_num_tx_queues; i++) {
  752. struct funeth_txq_stats txs;
  753. FUN_QSTAT_READ(fp->txqs[i], start, txs);
  754. stats->tx_packets += txs.tx_pkts;
  755. stats->tx_bytes += txs.tx_bytes;
  756. stats->tx_dropped += txs.tx_map_err;
  757. }
  758. for (i = 0; i < netdev->real_num_rx_queues; i++) {
  759. struct funeth_rxq_stats rxs;
  760. FUN_QSTAT_READ(rxqs[i], start, rxs);
  761. stats->rx_packets += rxs.rx_pkts;
  762. stats->rx_bytes += rxs.rx_bytes;
  763. stats->rx_dropped += rxs.rx_map_err + rxs.rx_mem_drops;
  764. }
  765. xdpqs = rcu_dereference(fp->xdpqs);
  766. if (!xdpqs)
  767. goto unlock;
  768. for (i = 0; i < fp->num_xdpqs; i++) {
  769. struct funeth_txq_stats txs;
  770. FUN_QSTAT_READ(xdpqs[i], start, txs);
  771. stats->tx_packets += txs.tx_pkts;
  772. stats->tx_bytes += txs.tx_bytes;
  773. }
  774. unlock:
  775. rcu_read_unlock();
  776. }
  777. static int fun_change_mtu(struct net_device *netdev, int new_mtu)
  778. {
  779. struct funeth_priv *fp = netdev_priv(netdev);
  780. int rc;
  781. rc = fun_port_write_cmd(fp, FUN_ADMIN_PORT_KEY_MTU, new_mtu);
  782. if (!rc)
  783. WRITE_ONCE(netdev->mtu, new_mtu);
  784. return rc;
  785. }
  786. static int fun_set_macaddr(struct net_device *netdev, void *addr)
  787. {
  788. struct funeth_priv *fp = netdev_priv(netdev);
  789. struct sockaddr *saddr = addr;
  790. int rc;
  791. if (!is_valid_ether_addr(saddr->sa_data))
  792. return -EADDRNOTAVAIL;
  793. if (ether_addr_equal(netdev->dev_addr, saddr->sa_data))
  794. return 0;
  795. rc = fun_port_write_cmd(fp, FUN_ADMIN_PORT_KEY_MACADDR,
  796. ether_addr_to_u64(saddr->sa_data));
  797. if (!rc)
  798. eth_hw_addr_set(netdev, saddr->sa_data);
  799. return rc;
  800. }
  801. static int fun_get_port_attributes(struct net_device *netdev)
  802. {
  803. static const int keys[] = {
  804. FUN_ADMIN_PORT_KEY_MACADDR, FUN_ADMIN_PORT_KEY_CAPABILITIES,
  805. FUN_ADMIN_PORT_KEY_ADVERT, FUN_ADMIN_PORT_KEY_MTU
  806. };
  807. static const int phys_keys[] = {
  808. FUN_ADMIN_PORT_KEY_LANE_ATTRS,
  809. };
  810. struct funeth_priv *fp = netdev_priv(netdev);
  811. u64 data[ARRAY_SIZE(keys)];
  812. u8 mac[ETH_ALEN];
  813. int i, rc;
  814. rc = fun_port_read_cmds(fp, ARRAY_SIZE(keys), keys, data);
  815. if (rc)
  816. return rc;
  817. for (i = 0; i < ARRAY_SIZE(keys); i++) {
  818. switch (keys[i]) {
  819. case FUN_ADMIN_PORT_KEY_MACADDR:
  820. u64_to_ether_addr(data[i], mac);
  821. if (is_zero_ether_addr(mac)) {
  822. eth_hw_addr_random(netdev);
  823. } else if (is_valid_ether_addr(mac)) {
  824. eth_hw_addr_set(netdev, mac);
  825. } else {
  826. netdev_err(netdev,
  827. "device provided a bad MAC address %pM\n",
  828. mac);
  829. return -EINVAL;
  830. }
  831. break;
  832. case FUN_ADMIN_PORT_KEY_CAPABILITIES:
  833. fp->port_caps = data[i];
  834. break;
  835. case FUN_ADMIN_PORT_KEY_ADVERT:
  836. fp->advertising = data[i];
  837. break;
  838. case FUN_ADMIN_PORT_KEY_MTU:
  839. netdev->mtu = data[i];
  840. break;
  841. }
  842. }
  843. if (!(fp->port_caps & FUN_PORT_CAP_VPORT)) {
  844. rc = fun_port_read_cmds(fp, ARRAY_SIZE(phys_keys), phys_keys,
  845. data);
  846. if (rc)
  847. return rc;
  848. fp->lane_attrs = data[0];
  849. }
  850. if (netdev->addr_assign_type == NET_ADDR_RANDOM)
  851. return fun_port_write_cmd(fp, FUN_ADMIN_PORT_KEY_MACADDR,
  852. ether_addr_to_u64(netdev->dev_addr));
  853. return 0;
  854. }
  855. static int fun_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  856. {
  857. const struct funeth_priv *fp = netdev_priv(dev);
  858. return copy_to_user(ifr->ifr_data, &fp->hwtstamp_cfg,
  859. sizeof(fp->hwtstamp_cfg)) ? -EFAULT : 0;
  860. }
  861. static int fun_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  862. {
  863. struct funeth_priv *fp = netdev_priv(dev);
  864. struct hwtstamp_config cfg;
  865. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  866. return -EFAULT;
  867. /* no TX HW timestamps */
  868. cfg.tx_type = HWTSTAMP_TX_OFF;
  869. switch (cfg.rx_filter) {
  870. case HWTSTAMP_FILTER_NONE:
  871. break;
  872. case HWTSTAMP_FILTER_ALL:
  873. case HWTSTAMP_FILTER_SOME:
  874. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  875. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  876. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  877. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  878. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  879. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  880. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  881. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  882. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  883. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  884. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  885. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  886. case HWTSTAMP_FILTER_NTP_ALL:
  887. cfg.rx_filter = HWTSTAMP_FILTER_ALL;
  888. break;
  889. default:
  890. return -ERANGE;
  891. }
  892. fp->hwtstamp_cfg = cfg;
  893. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  894. }
  895. static int fun_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  896. {
  897. switch (cmd) {
  898. case SIOCSHWTSTAMP:
  899. return fun_hwtstamp_set(dev, ifr);
  900. case SIOCGHWTSTAMP:
  901. return fun_hwtstamp_get(dev, ifr);
  902. default:
  903. return -EOPNOTSUPP;
  904. }
  905. }
  906. /* Prepare the queues for XDP. */
  907. static int fun_enter_xdp(struct net_device *dev, struct bpf_prog *prog)
  908. {
  909. struct funeth_priv *fp = netdev_priv(dev);
  910. unsigned int i, nqs = num_online_cpus();
  911. struct funeth_txq **xdpqs;
  912. struct funeth_rxq **rxqs;
  913. int err;
  914. xdpqs = alloc_xdpqs(dev, nqs, fp->sq_depth, 0, FUN_QSTATE_INIT_FULL);
  915. if (IS_ERR(xdpqs))
  916. return PTR_ERR(xdpqs);
  917. rxqs = rtnl_dereference(fp->rxqs);
  918. for (i = 0; i < dev->real_num_rx_queues; i++) {
  919. err = fun_rxq_set_bpf(rxqs[i], prog);
  920. if (err)
  921. goto out;
  922. }
  923. fp->num_xdpqs = nqs;
  924. rcu_assign_pointer(fp->xdpqs, xdpqs);
  925. return 0;
  926. out:
  927. while (i--)
  928. fun_rxq_set_bpf(rxqs[i], NULL);
  929. free_xdpqs(xdpqs, nqs, 0, FUN_QSTATE_DESTROYED);
  930. return err;
  931. }
  932. /* Set the queues for non-XDP operation. */
  933. static void fun_end_xdp(struct net_device *dev)
  934. {
  935. struct funeth_priv *fp = netdev_priv(dev);
  936. struct funeth_txq **xdpqs;
  937. struct funeth_rxq **rxqs;
  938. unsigned int i;
  939. xdpqs = rtnl_dereference(fp->xdpqs);
  940. rcu_assign_pointer(fp->xdpqs, NULL);
  941. synchronize_net();
  942. /* at this point both Rx and Tx XDP processing has ended */
  943. free_xdpqs(xdpqs, fp->num_xdpqs, 0, FUN_QSTATE_DESTROYED);
  944. fp->num_xdpqs = 0;
  945. rxqs = rtnl_dereference(fp->rxqs);
  946. for (i = 0; i < dev->real_num_rx_queues; i++)
  947. fun_rxq_set_bpf(rxqs[i], NULL);
  948. }
  949. #define XDP_MAX_MTU \
  950. (PAGE_SIZE - FUN_XDP_HEADROOM - VLAN_ETH_HLEN - FUN_RX_TAILROOM)
  951. static int fun_xdp_setup(struct net_device *dev, struct netdev_bpf *xdp)
  952. {
  953. struct bpf_prog *old_prog, *prog = xdp->prog;
  954. struct funeth_priv *fp = netdev_priv(dev);
  955. int i, err;
  956. /* XDP uses at most one buffer */
  957. if (prog && dev->mtu > XDP_MAX_MTU) {
  958. netdev_err(dev, "device MTU %u too large for XDP\n", dev->mtu);
  959. NL_SET_ERR_MSG_MOD(xdp->extack,
  960. "Device MTU too large for XDP");
  961. return -EINVAL;
  962. }
  963. if (!netif_running(dev)) {
  964. fp->num_xdpqs = prog ? num_online_cpus() : 0;
  965. } else if (prog && !fp->xdp_prog) {
  966. err = fun_enter_xdp(dev, prog);
  967. if (err) {
  968. NL_SET_ERR_MSG_MOD(xdp->extack,
  969. "Failed to set queues for XDP.");
  970. return err;
  971. }
  972. } else if (!prog && fp->xdp_prog) {
  973. fun_end_xdp(dev);
  974. } else {
  975. struct funeth_rxq **rxqs = rtnl_dereference(fp->rxqs);
  976. for (i = 0; i < dev->real_num_rx_queues; i++)
  977. WRITE_ONCE(rxqs[i]->xdp_prog, prog);
  978. }
  979. if (prog)
  980. xdp_features_set_redirect_target(dev, true);
  981. else
  982. xdp_features_clear_redirect_target(dev);
  983. dev->max_mtu = prog ? XDP_MAX_MTU : FUN_MAX_MTU;
  984. old_prog = xchg(&fp->xdp_prog, prog);
  985. if (old_prog)
  986. bpf_prog_put(old_prog);
  987. return 0;
  988. }
  989. static int fun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  990. {
  991. switch (xdp->command) {
  992. case XDP_SETUP_PROG:
  993. return fun_xdp_setup(dev, xdp);
  994. default:
  995. return -EINVAL;
  996. }
  997. }
  998. static int fun_init_vports(struct fun_ethdev *ed, unsigned int n)
  999. {
  1000. if (ed->num_vports)
  1001. return -EINVAL;
  1002. ed->vport_info = kvcalloc(n, sizeof(*ed->vport_info), GFP_KERNEL);
  1003. if (!ed->vport_info)
  1004. return -ENOMEM;
  1005. ed->num_vports = n;
  1006. return 0;
  1007. }
  1008. static void fun_free_vports(struct fun_ethdev *ed)
  1009. {
  1010. kvfree(ed->vport_info);
  1011. ed->vport_info = NULL;
  1012. ed->num_vports = 0;
  1013. }
  1014. static struct fun_vport_info *fun_get_vport(struct fun_ethdev *ed,
  1015. unsigned int vport)
  1016. {
  1017. if (!ed->vport_info || vport >= ed->num_vports)
  1018. return NULL;
  1019. return ed->vport_info + vport;
  1020. }
  1021. static int fun_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
  1022. {
  1023. struct funeth_priv *fp = netdev_priv(dev);
  1024. struct fun_adi_param mac_param = {};
  1025. struct fun_dev *fdev = fp->fdev;
  1026. struct fun_ethdev *ed = to_fun_ethdev(fdev);
  1027. struct fun_vport_info *vi;
  1028. int rc = -EINVAL;
  1029. if (is_multicast_ether_addr(mac))
  1030. return -EINVAL;
  1031. mutex_lock(&ed->state_mutex);
  1032. vi = fun_get_vport(ed, vf);
  1033. if (!vi)
  1034. goto unlock;
  1035. mac_param.u.mac = FUN_ADI_MAC_INIT(ether_addr_to_u64(mac));
  1036. rc = fun_adi_write(fdev, FUN_ADMIN_ADI_ATTR_MACADDR, vf + 1,
  1037. &mac_param);
  1038. if (!rc)
  1039. ether_addr_copy(vi->mac, mac);
  1040. unlock:
  1041. mutex_unlock(&ed->state_mutex);
  1042. return rc;
  1043. }
  1044. static int fun_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
  1045. __be16 vlan_proto)
  1046. {
  1047. struct funeth_priv *fp = netdev_priv(dev);
  1048. struct fun_adi_param vlan_param = {};
  1049. struct fun_dev *fdev = fp->fdev;
  1050. struct fun_ethdev *ed = to_fun_ethdev(fdev);
  1051. struct fun_vport_info *vi;
  1052. int rc = -EINVAL;
  1053. if (vlan > 4095 || qos > 7)
  1054. return -EINVAL;
  1055. if (vlan_proto && vlan_proto != htons(ETH_P_8021Q) &&
  1056. vlan_proto != htons(ETH_P_8021AD))
  1057. return -EINVAL;
  1058. mutex_lock(&ed->state_mutex);
  1059. vi = fun_get_vport(ed, vf);
  1060. if (!vi)
  1061. goto unlock;
  1062. vlan_param.u.vlan = FUN_ADI_VLAN_INIT(be16_to_cpu(vlan_proto),
  1063. ((u16)qos << VLAN_PRIO_SHIFT) | vlan);
  1064. rc = fun_adi_write(fdev, FUN_ADMIN_ADI_ATTR_VLAN, vf + 1, &vlan_param);
  1065. if (!rc) {
  1066. vi->vlan = vlan;
  1067. vi->qos = qos;
  1068. vi->vlan_proto = vlan_proto;
  1069. }
  1070. unlock:
  1071. mutex_unlock(&ed->state_mutex);
  1072. return rc;
  1073. }
  1074. static int fun_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
  1075. int max_tx_rate)
  1076. {
  1077. struct funeth_priv *fp = netdev_priv(dev);
  1078. struct fun_adi_param rate_param = {};
  1079. struct fun_dev *fdev = fp->fdev;
  1080. struct fun_ethdev *ed = to_fun_ethdev(fdev);
  1081. struct fun_vport_info *vi;
  1082. int rc = -EINVAL;
  1083. if (min_tx_rate)
  1084. return -EINVAL;
  1085. mutex_lock(&ed->state_mutex);
  1086. vi = fun_get_vport(ed, vf);
  1087. if (!vi)
  1088. goto unlock;
  1089. rate_param.u.rate = FUN_ADI_RATE_INIT(max_tx_rate);
  1090. rc = fun_adi_write(fdev, FUN_ADMIN_ADI_ATTR_RATE, vf + 1, &rate_param);
  1091. if (!rc)
  1092. vi->max_rate = max_tx_rate;
  1093. unlock:
  1094. mutex_unlock(&ed->state_mutex);
  1095. return rc;
  1096. }
  1097. static int fun_get_vf_config(struct net_device *dev, int vf,
  1098. struct ifla_vf_info *ivi)
  1099. {
  1100. struct funeth_priv *fp = netdev_priv(dev);
  1101. struct fun_ethdev *ed = to_fun_ethdev(fp->fdev);
  1102. const struct fun_vport_info *vi;
  1103. mutex_lock(&ed->state_mutex);
  1104. vi = fun_get_vport(ed, vf);
  1105. if (!vi)
  1106. goto unlock;
  1107. memset(ivi, 0, sizeof(*ivi));
  1108. ivi->vf = vf;
  1109. ether_addr_copy(ivi->mac, vi->mac);
  1110. ivi->vlan = vi->vlan;
  1111. ivi->qos = vi->qos;
  1112. ivi->vlan_proto = vi->vlan_proto;
  1113. ivi->max_tx_rate = vi->max_rate;
  1114. ivi->spoofchk = vi->spoofchk;
  1115. unlock:
  1116. mutex_unlock(&ed->state_mutex);
  1117. return vi ? 0 : -EINVAL;
  1118. }
  1119. static void fun_uninit(struct net_device *dev)
  1120. {
  1121. struct funeth_priv *fp = netdev_priv(dev);
  1122. fun_prune_queue_irqs(dev);
  1123. xa_destroy(&fp->irqs);
  1124. }
  1125. static const struct net_device_ops fun_netdev_ops = {
  1126. .ndo_open = funeth_open,
  1127. .ndo_stop = funeth_close,
  1128. .ndo_start_xmit = fun_start_xmit,
  1129. .ndo_get_stats64 = fun_get_stats64,
  1130. .ndo_change_mtu = fun_change_mtu,
  1131. .ndo_set_mac_address = fun_set_macaddr,
  1132. .ndo_validate_addr = eth_validate_addr,
  1133. .ndo_eth_ioctl = fun_ioctl,
  1134. .ndo_uninit = fun_uninit,
  1135. .ndo_bpf = fun_xdp,
  1136. .ndo_xdp_xmit = fun_xdp_xmit_frames,
  1137. .ndo_set_vf_mac = fun_set_vf_mac,
  1138. .ndo_set_vf_vlan = fun_set_vf_vlan,
  1139. .ndo_set_vf_rate = fun_set_vf_rate,
  1140. .ndo_get_vf_config = fun_get_vf_config,
  1141. };
  1142. #define GSO_ENCAP_FLAGS (NETIF_F_GSO_GRE | NETIF_F_GSO_IPXIP4 | \
  1143. NETIF_F_GSO_IPXIP6 | NETIF_F_GSO_UDP_TUNNEL | \
  1144. NETIF_F_GSO_UDP_TUNNEL_CSUM)
  1145. #define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN | \
  1146. NETIF_F_GSO_UDP_L4)
  1147. #define VLAN_FEAT (NETIF_F_SG | NETIF_F_HW_CSUM | TSO_FLAGS | \
  1148. GSO_ENCAP_FLAGS | NETIF_F_HIGHDMA)
  1149. static void fun_dflt_rss_indir(struct funeth_priv *fp, unsigned int nrx)
  1150. {
  1151. unsigned int i;
  1152. for (i = 0; i < fp->indir_table_nentries; i++)
  1153. fp->indir_table[i] = ethtool_rxfh_indir_default(i, nrx);
  1154. }
  1155. /* Reset the RSS indirection table to equal distribution across the current
  1156. * number of Rx queues. Called at init time and whenever the number of Rx
  1157. * queues changes subsequently. Note that this may also resize the indirection
  1158. * table.
  1159. */
  1160. static void fun_reset_rss_indir(struct net_device *dev, unsigned int nrx)
  1161. {
  1162. struct funeth_priv *fp = netdev_priv(dev);
  1163. if (!fp->rss_cfg)
  1164. return;
  1165. /* Set the table size to the max possible that allows an equal number
  1166. * of occurrences of each CQ.
  1167. */
  1168. fp->indir_table_nentries = rounddown(FUN_ETH_RSS_MAX_INDIR_ENT, nrx);
  1169. fun_dflt_rss_indir(fp, nrx);
  1170. }
  1171. /* Update the RSS LUT to contain only queues in [0, nrx). Normally this will
  1172. * update the LUT to an equal distribution among nrx queues, If @only_if_needed
  1173. * is set the LUT is left unchanged if it already does not reference any queues
  1174. * >= nrx.
  1175. */
  1176. static int fun_rss_set_qnum(struct net_device *dev, unsigned int nrx,
  1177. bool only_if_needed)
  1178. {
  1179. struct funeth_priv *fp = netdev_priv(dev);
  1180. u32 old_lut[FUN_ETH_RSS_MAX_INDIR_ENT];
  1181. unsigned int i, oldsz;
  1182. int err;
  1183. if (!fp->rss_cfg)
  1184. return 0;
  1185. if (only_if_needed) {
  1186. for (i = 0; i < fp->indir_table_nentries; i++)
  1187. if (fp->indir_table[i] >= nrx)
  1188. break;
  1189. if (i >= fp->indir_table_nentries)
  1190. return 0;
  1191. }
  1192. memcpy(old_lut, fp->indir_table, sizeof(old_lut));
  1193. oldsz = fp->indir_table_nentries;
  1194. fun_reset_rss_indir(dev, nrx);
  1195. err = fun_config_rss(dev, fp->hash_algo, fp->rss_key,
  1196. fp->indir_table, FUN_ADMIN_SUBOP_MODIFY);
  1197. if (!err)
  1198. return 0;
  1199. memcpy(fp->indir_table, old_lut, sizeof(old_lut));
  1200. fp->indir_table_nentries = oldsz;
  1201. return err;
  1202. }
  1203. /* Allocate the DMA area for the RSS configuration commands to the device, and
  1204. * initialize the hash, hash key, indirection table size and its entries to
  1205. * their defaults. The indirection table defaults to equal distribution across
  1206. * the Rx queues.
  1207. */
  1208. static int fun_init_rss(struct net_device *dev)
  1209. {
  1210. struct funeth_priv *fp = netdev_priv(dev);
  1211. size_t size = sizeof(fp->rss_key) + sizeof(fp->indir_table);
  1212. fp->rss_hw_id = FUN_HCI_ID_INVALID;
  1213. if (!(fp->port_caps & FUN_PORT_CAP_OFFLOADS))
  1214. return 0;
  1215. fp->rss_cfg = dma_alloc_coherent(&fp->pdev->dev, size,
  1216. &fp->rss_dma_addr, GFP_KERNEL);
  1217. if (!fp->rss_cfg)
  1218. return -ENOMEM;
  1219. fp->hash_algo = FUN_ETH_RSS_ALG_TOEPLITZ;
  1220. netdev_rss_key_fill(fp->rss_key, sizeof(fp->rss_key));
  1221. fun_reset_rss_indir(dev, dev->real_num_rx_queues);
  1222. return 0;
  1223. }
  1224. static void fun_free_rss(struct funeth_priv *fp)
  1225. {
  1226. if (fp->rss_cfg) {
  1227. dma_free_coherent(&fp->pdev->dev,
  1228. sizeof(fp->rss_key) + sizeof(fp->indir_table),
  1229. fp->rss_cfg, fp->rss_dma_addr);
  1230. fp->rss_cfg = NULL;
  1231. }
  1232. }
  1233. void fun_set_ring_count(struct net_device *netdev, unsigned int ntx,
  1234. unsigned int nrx)
  1235. {
  1236. netif_set_real_num_tx_queues(netdev, ntx);
  1237. if (nrx != netdev->real_num_rx_queues) {
  1238. netif_set_real_num_rx_queues(netdev, nrx);
  1239. fun_reset_rss_indir(netdev, nrx);
  1240. }
  1241. }
  1242. static int fun_init_stats_area(struct funeth_priv *fp)
  1243. {
  1244. unsigned int nstats;
  1245. if (!(fp->port_caps & FUN_PORT_CAP_STATS))
  1246. return 0;
  1247. nstats = PORT_MAC_RX_STATS_MAX + PORT_MAC_TX_STATS_MAX +
  1248. PORT_MAC_FEC_STATS_MAX;
  1249. fp->stats = dma_alloc_coherent(&fp->pdev->dev, nstats * sizeof(u64),
  1250. &fp->stats_dma_addr, GFP_KERNEL);
  1251. if (!fp->stats)
  1252. return -ENOMEM;
  1253. return 0;
  1254. }
  1255. static void fun_free_stats_area(struct funeth_priv *fp)
  1256. {
  1257. unsigned int nstats;
  1258. if (fp->stats) {
  1259. nstats = PORT_MAC_RX_STATS_MAX + PORT_MAC_TX_STATS_MAX;
  1260. dma_free_coherent(&fp->pdev->dev, nstats * sizeof(u64),
  1261. fp->stats, fp->stats_dma_addr);
  1262. fp->stats = NULL;
  1263. }
  1264. }
  1265. static int fun_dl_port_register(struct net_device *netdev)
  1266. {
  1267. struct funeth_priv *fp = netdev_priv(netdev);
  1268. struct devlink *dl = priv_to_devlink(fp->fdev);
  1269. struct devlink_port_attrs attrs = {};
  1270. unsigned int idx;
  1271. if (fp->port_caps & FUN_PORT_CAP_VPORT) {
  1272. attrs.flavour = DEVLINK_PORT_FLAVOUR_VIRTUAL;
  1273. idx = fp->lport;
  1274. } else {
  1275. idx = netdev->dev_port;
  1276. attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
  1277. attrs.lanes = fp->lane_attrs & 7;
  1278. if (fp->lane_attrs & FUN_PORT_LANE_SPLIT) {
  1279. attrs.split = 1;
  1280. attrs.phys.port_number = fp->lport & ~3;
  1281. attrs.phys.split_subport_number = fp->lport & 3;
  1282. } else {
  1283. attrs.phys.port_number = fp->lport;
  1284. }
  1285. }
  1286. devlink_port_attrs_set(&fp->dl_port, &attrs);
  1287. return devlink_port_register(dl, &fp->dl_port, idx);
  1288. }
  1289. /* Determine the max Tx/Rx queues for a port. */
  1290. static int fun_max_qs(struct fun_ethdev *ed, unsigned int *ntx,
  1291. unsigned int *nrx)
  1292. {
  1293. int neth;
  1294. if (ed->num_ports > 1 || is_kdump_kernel()) {
  1295. *ntx = 1;
  1296. *nrx = 1;
  1297. return 0;
  1298. }
  1299. neth = fun_get_res_count(&ed->fdev, FUN_ADMIN_OP_ETH);
  1300. if (neth < 0)
  1301. return neth;
  1302. /* We determine the max number of queues based on the CPU
  1303. * cores, device interrupts and queues, RSS size, and device Tx flows.
  1304. *
  1305. * - At least 1 Rx and 1 Tx queues.
  1306. * - At most 1 Rx/Tx queue per core.
  1307. * - Each Rx/Tx queue needs 1 SQ.
  1308. */
  1309. *ntx = min(ed->nsqs_per_port - 1, num_online_cpus());
  1310. *nrx = *ntx;
  1311. if (*ntx > neth)
  1312. *ntx = neth;
  1313. if (*nrx > FUN_ETH_RSS_MAX_INDIR_ENT)
  1314. *nrx = FUN_ETH_RSS_MAX_INDIR_ENT;
  1315. return 0;
  1316. }
  1317. static void fun_queue_defaults(struct net_device *dev, unsigned int nsqs)
  1318. {
  1319. unsigned int ntx, nrx;
  1320. ntx = min(dev->num_tx_queues, FUN_DFLT_QUEUES);
  1321. nrx = min(dev->num_rx_queues, FUN_DFLT_QUEUES);
  1322. if (ntx <= nrx) {
  1323. ntx = min(ntx, nsqs / 2);
  1324. nrx = min(nrx, nsqs - ntx);
  1325. } else {
  1326. nrx = min(nrx, nsqs / 2);
  1327. ntx = min(ntx, nsqs - nrx);
  1328. }
  1329. netif_set_real_num_tx_queues(dev, ntx);
  1330. netif_set_real_num_rx_queues(dev, nrx);
  1331. }
  1332. /* Replace the existing Rx/Tx/XDP queues with equal number of queues with
  1333. * different settings, e.g. depth. This is a disruptive replacement that
  1334. * temporarily shuts down the data path and should be limited to changes that
  1335. * can't be applied to live queues. The old queues are always discarded.
  1336. */
  1337. int fun_replace_queues(struct net_device *dev, struct fun_qset *newqs,
  1338. struct netlink_ext_ack *extack)
  1339. {
  1340. struct fun_qset oldqs = { .state = FUN_QSTATE_DESTROYED };
  1341. struct funeth_priv *fp = netdev_priv(dev);
  1342. int err;
  1343. newqs->nrxqs = dev->real_num_rx_queues;
  1344. newqs->ntxqs = dev->real_num_tx_queues;
  1345. newqs->nxdpqs = fp->num_xdpqs;
  1346. newqs->state = FUN_QSTATE_INIT_SW;
  1347. err = fun_alloc_rings(dev, newqs);
  1348. if (err) {
  1349. NL_SET_ERR_MSG_MOD(extack,
  1350. "Unable to allocate memory for new queues, keeping current settings");
  1351. return err;
  1352. }
  1353. fun_down(dev, &oldqs);
  1354. err = fun_up(dev, newqs);
  1355. if (!err)
  1356. return 0;
  1357. /* The new queues couldn't be installed. We do not retry the old queues
  1358. * as they are the same to the device as the new queues and would
  1359. * similarly fail.
  1360. */
  1361. newqs->state = FUN_QSTATE_DESTROYED;
  1362. fun_free_rings(dev, newqs);
  1363. NL_SET_ERR_MSG_MOD(extack, "Unable to restore the data path with the new queues.");
  1364. return err;
  1365. }
  1366. /* Change the number of Rx/Tx queues of a device while it is up. This is done
  1367. * by incrementally adding/removing queues to meet the new requirements while
  1368. * handling ongoing traffic.
  1369. */
  1370. int fun_change_num_queues(struct net_device *dev, unsigned int ntx,
  1371. unsigned int nrx)
  1372. {
  1373. unsigned int keep_tx = min(dev->real_num_tx_queues, ntx);
  1374. unsigned int keep_rx = min(dev->real_num_rx_queues, nrx);
  1375. struct funeth_priv *fp = netdev_priv(dev);
  1376. struct fun_qset oldqs = {
  1377. .rxqs = rtnl_dereference(fp->rxqs),
  1378. .txqs = fp->txqs,
  1379. .nrxqs = dev->real_num_rx_queues,
  1380. .ntxqs = dev->real_num_tx_queues,
  1381. .rxq_start = keep_rx,
  1382. .txq_start = keep_tx,
  1383. .state = FUN_QSTATE_DESTROYED
  1384. };
  1385. struct fun_qset newqs = {
  1386. .nrxqs = nrx,
  1387. .ntxqs = ntx,
  1388. .rxq_start = keep_rx,
  1389. .txq_start = keep_tx,
  1390. .cq_depth = fp->cq_depth,
  1391. .rq_depth = fp->rq_depth,
  1392. .sq_depth = fp->sq_depth,
  1393. .state = FUN_QSTATE_INIT_FULL
  1394. };
  1395. int i, err;
  1396. err = fun_alloc_rings(dev, &newqs);
  1397. if (err)
  1398. goto free_irqs;
  1399. err = fun_enable_irqs(dev); /* of any newly added queues */
  1400. if (err)
  1401. goto free_rings;
  1402. /* copy the queues we are keeping to the new set */
  1403. memcpy(newqs.rxqs, oldqs.rxqs, keep_rx * sizeof(*oldqs.rxqs));
  1404. memcpy(newqs.txqs, fp->txqs, keep_tx * sizeof(*fp->txqs));
  1405. if (nrx < dev->real_num_rx_queues) {
  1406. err = fun_rss_set_qnum(dev, nrx, true);
  1407. if (err)
  1408. goto disable_tx_irqs;
  1409. for (i = nrx; i < dev->real_num_rx_queues; i++)
  1410. fun_disable_one_irq(container_of(oldqs.rxqs[i]->napi,
  1411. struct fun_irq, napi));
  1412. netif_set_real_num_rx_queues(dev, nrx);
  1413. }
  1414. if (ntx < dev->real_num_tx_queues)
  1415. netif_set_real_num_tx_queues(dev, ntx);
  1416. rcu_assign_pointer(fp->rxqs, newqs.rxqs);
  1417. fp->txqs = newqs.txqs;
  1418. synchronize_net();
  1419. if (ntx > dev->real_num_tx_queues)
  1420. netif_set_real_num_tx_queues(dev, ntx);
  1421. if (nrx > dev->real_num_rx_queues) {
  1422. netif_set_real_num_rx_queues(dev, nrx);
  1423. fun_rss_set_qnum(dev, nrx, false);
  1424. }
  1425. /* disable interrupts of any excess Tx queues */
  1426. for (i = keep_tx; i < oldqs.ntxqs; i++)
  1427. fun_disable_one_irq(oldqs.txqs[i]->irq);
  1428. fun_free_rings(dev, &oldqs);
  1429. fun_prune_queue_irqs(dev);
  1430. return 0;
  1431. disable_tx_irqs:
  1432. for (i = oldqs.ntxqs; i < ntx; i++)
  1433. fun_disable_one_irq(newqs.txqs[i]->irq);
  1434. free_rings:
  1435. newqs.state = FUN_QSTATE_DESTROYED;
  1436. fun_free_rings(dev, &newqs);
  1437. free_irqs:
  1438. fun_prune_queue_irqs(dev);
  1439. return err;
  1440. }
  1441. static int fun_create_netdev(struct fun_ethdev *ed, unsigned int portid)
  1442. {
  1443. struct fun_dev *fdev = &ed->fdev;
  1444. struct net_device *netdev;
  1445. struct funeth_priv *fp;
  1446. unsigned int ntx, nrx;
  1447. int rc;
  1448. rc = fun_max_qs(ed, &ntx, &nrx);
  1449. if (rc)
  1450. return rc;
  1451. netdev = alloc_etherdev_mqs(sizeof(*fp), ntx, nrx);
  1452. if (!netdev) {
  1453. rc = -ENOMEM;
  1454. goto done;
  1455. }
  1456. netdev->dev_port = portid;
  1457. fun_queue_defaults(netdev, ed->nsqs_per_port);
  1458. fp = netdev_priv(netdev);
  1459. fp->fdev = fdev;
  1460. fp->pdev = to_pci_dev(fdev->dev);
  1461. fp->netdev = netdev;
  1462. xa_init(&fp->irqs);
  1463. fp->rx_irq_ofst = ntx;
  1464. seqcount_init(&fp->link_seq);
  1465. fp->lport = INVALID_LPORT;
  1466. rc = fun_port_create(netdev);
  1467. if (rc)
  1468. goto free_netdev;
  1469. /* bind port to admin CQ for async events */
  1470. rc = fun_bind(fdev, FUN_ADMIN_BIND_TYPE_PORT, portid,
  1471. FUN_ADMIN_BIND_TYPE_EPCQ, 0);
  1472. if (rc)
  1473. goto destroy_port;
  1474. rc = fun_get_port_attributes(netdev);
  1475. if (rc)
  1476. goto destroy_port;
  1477. rc = fun_init_rss(netdev);
  1478. if (rc)
  1479. goto destroy_port;
  1480. rc = fun_init_stats_area(fp);
  1481. if (rc)
  1482. goto free_rss;
  1483. SET_NETDEV_DEV(netdev, fdev->dev);
  1484. SET_NETDEV_DEVLINK_PORT(netdev, &fp->dl_port);
  1485. netdev->netdev_ops = &fun_netdev_ops;
  1486. netdev->hw_features = NETIF_F_SG | NETIF_F_RXHASH | NETIF_F_RXCSUM;
  1487. if (fp->port_caps & FUN_PORT_CAP_OFFLOADS)
  1488. netdev->hw_features |= NETIF_F_HW_CSUM | TSO_FLAGS;
  1489. if (fp->port_caps & FUN_PORT_CAP_ENCAP_OFFLOADS)
  1490. netdev->hw_features |= GSO_ENCAP_FLAGS;
  1491. netdev->features |= netdev->hw_features | NETIF_F_HIGHDMA;
  1492. netdev->vlan_features = netdev->features & VLAN_FEAT;
  1493. netdev->mpls_features = netdev->vlan_features;
  1494. netdev->hw_enc_features = netdev->hw_features;
  1495. netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
  1496. netdev->min_mtu = ETH_MIN_MTU;
  1497. netdev->max_mtu = FUN_MAX_MTU;
  1498. fun_set_ethtool_ops(netdev);
  1499. /* configurable parameters */
  1500. fp->sq_depth = min(SQ_DEPTH, fdev->q_depth);
  1501. fp->cq_depth = min(CQ_DEPTH, fdev->q_depth);
  1502. fp->rq_depth = min_t(unsigned int, RQ_DEPTH, fdev->q_depth);
  1503. fp->rx_coal_usec = CQ_INTCOAL_USEC;
  1504. fp->rx_coal_count = CQ_INTCOAL_NPKT;
  1505. fp->tx_coal_usec = SQ_INTCOAL_USEC;
  1506. fp->tx_coal_count = SQ_INTCOAL_NPKT;
  1507. fp->cq_irq_db = FUN_IRQ_CQ_DB(fp->rx_coal_usec, fp->rx_coal_count);
  1508. rc = fun_dl_port_register(netdev);
  1509. if (rc)
  1510. goto free_stats;
  1511. fp->ktls_id = FUN_HCI_ID_INVALID;
  1512. fun_ktls_init(netdev); /* optional, failure OK */
  1513. netif_carrier_off(netdev);
  1514. ed->netdevs[portid] = netdev;
  1515. rc = register_netdev(netdev);
  1516. if (rc)
  1517. goto unreg_devlink;
  1518. return 0;
  1519. unreg_devlink:
  1520. ed->netdevs[portid] = NULL;
  1521. fun_ktls_cleanup(fp);
  1522. devlink_port_unregister(&fp->dl_port);
  1523. free_stats:
  1524. fun_free_stats_area(fp);
  1525. free_rss:
  1526. fun_free_rss(fp);
  1527. destroy_port:
  1528. fun_port_destroy(netdev);
  1529. free_netdev:
  1530. free_netdev(netdev);
  1531. done:
  1532. dev_err(fdev->dev, "couldn't allocate port %u, error %d", portid, rc);
  1533. return rc;
  1534. }
  1535. static void fun_destroy_netdev(struct net_device *netdev)
  1536. {
  1537. struct funeth_priv *fp;
  1538. fp = netdev_priv(netdev);
  1539. unregister_netdev(netdev);
  1540. devlink_port_unregister(&fp->dl_port);
  1541. fun_ktls_cleanup(fp);
  1542. fun_free_stats_area(fp);
  1543. fun_free_rss(fp);
  1544. fun_port_destroy(netdev);
  1545. free_netdev(netdev);
  1546. }
  1547. static int fun_create_ports(struct fun_ethdev *ed, unsigned int nports)
  1548. {
  1549. struct fun_dev *fd = &ed->fdev;
  1550. int i, rc;
  1551. /* The admin queue takes 1 IRQ and 2 SQs. */
  1552. ed->nsqs_per_port = min(fd->num_irqs - 1,
  1553. fd->kern_end_qid - 2) / nports;
  1554. if (ed->nsqs_per_port < 2) {
  1555. dev_err(fd->dev, "Too few SQs for %u ports", nports);
  1556. return -EINVAL;
  1557. }
  1558. ed->netdevs = kcalloc(nports, sizeof(*ed->netdevs), GFP_KERNEL);
  1559. if (!ed->netdevs)
  1560. return -ENOMEM;
  1561. ed->num_ports = nports;
  1562. for (i = 0; i < nports; i++) {
  1563. rc = fun_create_netdev(ed, i);
  1564. if (rc)
  1565. goto free_netdevs;
  1566. }
  1567. return 0;
  1568. free_netdevs:
  1569. while (i)
  1570. fun_destroy_netdev(ed->netdevs[--i]);
  1571. kfree(ed->netdevs);
  1572. ed->netdevs = NULL;
  1573. ed->num_ports = 0;
  1574. return rc;
  1575. }
  1576. static void fun_destroy_ports(struct fun_ethdev *ed)
  1577. {
  1578. unsigned int i;
  1579. for (i = 0; i < ed->num_ports; i++)
  1580. fun_destroy_netdev(ed->netdevs[i]);
  1581. kfree(ed->netdevs);
  1582. ed->netdevs = NULL;
  1583. ed->num_ports = 0;
  1584. }
  1585. static void fun_update_link_state(const struct fun_ethdev *ed,
  1586. const struct fun_admin_port_notif *notif)
  1587. {
  1588. unsigned int port_idx = be16_to_cpu(notif->id);
  1589. struct net_device *netdev;
  1590. struct funeth_priv *fp;
  1591. if (port_idx >= ed->num_ports)
  1592. return;
  1593. netdev = ed->netdevs[port_idx];
  1594. fp = netdev_priv(netdev);
  1595. write_seqcount_begin(&fp->link_seq);
  1596. fp->link_speed = be32_to_cpu(notif->speed) * 10; /* 10 Mbps->Mbps */
  1597. fp->active_fc = notif->flow_ctrl;
  1598. fp->active_fec = notif->fec;
  1599. fp->xcvr_type = notif->xcvr_type;
  1600. fp->link_down_reason = notif->link_down_reason;
  1601. fp->lp_advertising = be64_to_cpu(notif->lp_advertising);
  1602. if ((notif->link_state | notif->missed_events) & FUN_PORT_FLAG_MAC_DOWN)
  1603. netif_carrier_off(netdev);
  1604. if (notif->link_state & FUN_PORT_FLAG_MAC_UP)
  1605. netif_carrier_on(netdev);
  1606. write_seqcount_end(&fp->link_seq);
  1607. fun_report_link(netdev);
  1608. }
  1609. /* handler for async events delivered through the admin CQ */
  1610. static void fun_event_cb(struct fun_dev *fdev, void *entry)
  1611. {
  1612. u8 op = ((struct fun_admin_rsp_common *)entry)->op;
  1613. if (op == FUN_ADMIN_OP_PORT) {
  1614. const struct fun_admin_port_notif *rsp = entry;
  1615. if (rsp->subop == FUN_ADMIN_SUBOP_NOTIFY) {
  1616. fun_update_link_state(to_fun_ethdev(fdev), rsp);
  1617. } else if (rsp->subop == FUN_ADMIN_SUBOP_RES_COUNT) {
  1618. const struct fun_admin_res_count_rsp *r = entry;
  1619. if (r->count.data)
  1620. set_bit(FUN_SERV_RES_CHANGE, &fdev->service_flags);
  1621. else
  1622. set_bit(FUN_SERV_DEL_PORTS, &fdev->service_flags);
  1623. fun_serv_sched(fdev);
  1624. } else {
  1625. dev_info(fdev->dev, "adminq event unexpected op %u subop %u",
  1626. op, rsp->subop);
  1627. }
  1628. } else {
  1629. dev_info(fdev->dev, "adminq event unexpected op %u", op);
  1630. }
  1631. }
  1632. /* handler for pending work managed by the service task */
  1633. static void fun_service_cb(struct fun_dev *fdev)
  1634. {
  1635. struct fun_ethdev *ed = to_fun_ethdev(fdev);
  1636. int rc;
  1637. if (test_and_clear_bit(FUN_SERV_DEL_PORTS, &fdev->service_flags))
  1638. fun_destroy_ports(ed);
  1639. if (!test_and_clear_bit(FUN_SERV_RES_CHANGE, &fdev->service_flags))
  1640. return;
  1641. rc = fun_get_res_count(fdev, FUN_ADMIN_OP_PORT);
  1642. if (rc < 0 || rc == ed->num_ports)
  1643. return;
  1644. if (ed->num_ports)
  1645. fun_destroy_ports(ed);
  1646. if (rc)
  1647. fun_create_ports(ed, rc);
  1648. }
  1649. static int funeth_sriov_configure(struct pci_dev *pdev, int nvfs)
  1650. {
  1651. struct fun_dev *fdev = pci_get_drvdata(pdev);
  1652. struct fun_ethdev *ed = to_fun_ethdev(fdev);
  1653. int rc;
  1654. if (nvfs == 0) {
  1655. if (pci_vfs_assigned(pdev)) {
  1656. dev_warn(&pdev->dev,
  1657. "Cannot disable SR-IOV while VFs are assigned\n");
  1658. return -EPERM;
  1659. }
  1660. mutex_lock(&ed->state_mutex);
  1661. fun_free_vports(ed);
  1662. mutex_unlock(&ed->state_mutex);
  1663. pci_disable_sriov(pdev);
  1664. return 0;
  1665. }
  1666. rc = pci_enable_sriov(pdev, nvfs);
  1667. if (rc)
  1668. return rc;
  1669. mutex_lock(&ed->state_mutex);
  1670. rc = fun_init_vports(ed, nvfs);
  1671. mutex_unlock(&ed->state_mutex);
  1672. if (rc) {
  1673. pci_disable_sriov(pdev);
  1674. return rc;
  1675. }
  1676. return nvfs;
  1677. }
  1678. static int funeth_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1679. {
  1680. struct fun_dev_params aqreq = {
  1681. .cqe_size_log2 = ilog2(ADMIN_CQE_SIZE),
  1682. .sqe_size_log2 = ilog2(ADMIN_SQE_SIZE),
  1683. .cq_depth = ADMIN_CQ_DEPTH,
  1684. .sq_depth = ADMIN_SQ_DEPTH,
  1685. .rq_depth = ADMIN_RQ_DEPTH,
  1686. .min_msix = 2, /* 1 Rx + 1 Tx */
  1687. .event_cb = fun_event_cb,
  1688. .serv_cb = fun_service_cb,
  1689. };
  1690. struct devlink *devlink;
  1691. struct fun_ethdev *ed;
  1692. struct fun_dev *fdev;
  1693. int rc;
  1694. devlink = fun_devlink_alloc(&pdev->dev);
  1695. if (!devlink) {
  1696. dev_err(&pdev->dev, "devlink alloc failed\n");
  1697. return -ENOMEM;
  1698. }
  1699. ed = devlink_priv(devlink);
  1700. mutex_init(&ed->state_mutex);
  1701. fdev = &ed->fdev;
  1702. rc = fun_dev_enable(fdev, pdev, &aqreq, KBUILD_MODNAME);
  1703. if (rc)
  1704. goto free_devlink;
  1705. rc = fun_get_res_count(fdev, FUN_ADMIN_OP_PORT);
  1706. if (rc > 0)
  1707. rc = fun_create_ports(ed, rc);
  1708. if (rc < 0)
  1709. goto disable_dev;
  1710. fun_serv_restart(fdev);
  1711. fun_devlink_register(devlink);
  1712. return 0;
  1713. disable_dev:
  1714. fun_dev_disable(fdev);
  1715. free_devlink:
  1716. mutex_destroy(&ed->state_mutex);
  1717. fun_devlink_free(devlink);
  1718. return rc;
  1719. }
  1720. static void funeth_remove(struct pci_dev *pdev)
  1721. {
  1722. struct fun_dev *fdev = pci_get_drvdata(pdev);
  1723. struct devlink *devlink;
  1724. struct fun_ethdev *ed;
  1725. ed = to_fun_ethdev(fdev);
  1726. devlink = priv_to_devlink(ed);
  1727. fun_devlink_unregister(devlink);
  1728. #ifdef CONFIG_PCI_IOV
  1729. funeth_sriov_configure(pdev, 0);
  1730. #endif
  1731. fun_serv_stop(fdev);
  1732. fun_destroy_ports(ed);
  1733. fun_dev_disable(fdev);
  1734. mutex_destroy(&ed->state_mutex);
  1735. fun_devlink_free(devlink);
  1736. }
  1737. static struct pci_driver funeth_driver = {
  1738. .name = KBUILD_MODNAME,
  1739. .id_table = funeth_id_table,
  1740. .probe = funeth_probe,
  1741. .remove = funeth_remove,
  1742. .shutdown = funeth_remove,
  1743. .sriov_configure = funeth_sriov_configure,
  1744. };
  1745. module_pci_driver(funeth_driver);
  1746. MODULE_AUTHOR("Dimitris Michailidis <dmichail@fungible.com>");
  1747. MODULE_DESCRIPTION("Fungible Ethernet Network Driver");
  1748. MODULE_LICENSE("Dual BSD/GPL");
  1749. MODULE_DEVICE_TABLE(pci, funeth_id_table);