dpio-service.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright 2014-2016 Freescale Semiconductor Inc.
  4. * Copyright 2016-2019 NXP
  5. *
  6. */
  7. #include <linux/types.h>
  8. #include <linux/fsl/mc.h>
  9. #include <soc/fsl/dpaa2-io.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dim.h>
  16. #include <linux/slab.h>
  17. #include "dpio.h"
  18. #include "qbman-portal.h"
  19. struct dpaa2_io {
  20. struct dpaa2_io_desc dpio_desc;
  21. struct qbman_swp_desc swp_desc;
  22. struct qbman_swp *swp;
  23. struct list_head node;
  24. /* protect against multiple management commands */
  25. spinlock_t lock_mgmt_cmd;
  26. /* protect notifications list */
  27. spinlock_t lock_notifications;
  28. struct list_head notifications;
  29. struct device *dev;
  30. /* Net DIM */
  31. struct dim rx_dim;
  32. /* protect against concurrent Net DIM updates */
  33. spinlock_t dim_lock;
  34. u16 event_ctr;
  35. u64 bytes;
  36. u64 frames;
  37. };
  38. struct dpaa2_io_store {
  39. unsigned int max;
  40. dma_addr_t paddr;
  41. struct dpaa2_dq *vaddr;
  42. void *alloced_addr; /* unaligned value from kmalloc() */
  43. unsigned int idx; /* position of the next-to-be-returned entry */
  44. struct qbman_swp *swp; /* portal used to issue VDQCR */
  45. struct device *dev; /* device used for DMA mapping */
  46. };
  47. /* keep a per cpu array of DPIOs for fast access */
  48. static struct dpaa2_io *dpio_by_cpu[NR_CPUS];
  49. static struct list_head dpio_list = LIST_HEAD_INIT(dpio_list);
  50. static DEFINE_SPINLOCK(dpio_list_lock);
  51. static inline struct dpaa2_io *service_select_by_cpu(struct dpaa2_io *d,
  52. int cpu)
  53. {
  54. if (d)
  55. return d;
  56. if (cpu != DPAA2_IO_ANY_CPU && cpu >= num_possible_cpus())
  57. return NULL;
  58. /*
  59. * If cpu == -1, choose the current cpu, with no guarantees about
  60. * potentially being migrated away.
  61. */
  62. if (cpu < 0)
  63. cpu = raw_smp_processor_id();
  64. /* If a specific cpu was requested, pick it up immediately */
  65. return dpio_by_cpu[cpu];
  66. }
  67. static inline struct dpaa2_io *service_select(struct dpaa2_io *d)
  68. {
  69. if (d)
  70. return d;
  71. d = service_select_by_cpu(d, -1);
  72. if (d)
  73. return d;
  74. spin_lock(&dpio_list_lock);
  75. d = list_entry(dpio_list.next, struct dpaa2_io, node);
  76. list_del(&d->node);
  77. list_add_tail(&d->node, &dpio_list);
  78. spin_unlock(&dpio_list_lock);
  79. return d;
  80. }
  81. /**
  82. * dpaa2_io_service_select() - return a dpaa2_io service affined to this cpu
  83. * @cpu: the cpu id
  84. *
  85. * Return the affine dpaa2_io service, or NULL if there is no service affined
  86. * to the specified cpu. If DPAA2_IO_ANY_CPU is used, return the next available
  87. * service.
  88. */
  89. struct dpaa2_io *dpaa2_io_service_select(int cpu)
  90. {
  91. if (cpu == DPAA2_IO_ANY_CPU)
  92. return service_select(NULL);
  93. return service_select_by_cpu(NULL, cpu);
  94. }
  95. EXPORT_SYMBOL_GPL(dpaa2_io_service_select);
  96. static void dpaa2_io_dim_work(struct work_struct *w)
  97. {
  98. struct dim *dim = container_of(w, struct dim, work);
  99. struct dim_cq_moder moder =
  100. net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
  101. struct dpaa2_io *d = container_of(dim, struct dpaa2_io, rx_dim);
  102. dpaa2_io_set_irq_coalescing(d, moder.usec);
  103. dim->state = DIM_START_MEASURE;
  104. }
  105. /**
  106. * dpaa2_io_create() - create a dpaa2_io object.
  107. * @desc: the dpaa2_io descriptor
  108. * @dev: the actual DPIO device
  109. *
  110. * Activates a "struct dpaa2_io" corresponding to the given config of an actual
  111. * DPIO object.
  112. *
  113. * Return a valid dpaa2_io object for success, or NULL for failure.
  114. */
  115. struct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc,
  116. struct device *dev)
  117. {
  118. struct dpaa2_io *obj = kmalloc(sizeof(*obj), GFP_KERNEL);
  119. u32 qman_256_cycles_per_ns;
  120. if (!obj)
  121. return NULL;
  122. /* check if CPU is out of range (-1 means any cpu) */
  123. if (desc->cpu != DPAA2_IO_ANY_CPU && desc->cpu >= num_possible_cpus()) {
  124. kfree(obj);
  125. return NULL;
  126. }
  127. obj->dpio_desc = *desc;
  128. obj->swp_desc.cena_bar = obj->dpio_desc.regs_cena;
  129. obj->swp_desc.cinh_bar = obj->dpio_desc.regs_cinh;
  130. obj->swp_desc.qman_clk = obj->dpio_desc.qman_clk;
  131. obj->swp_desc.qman_version = obj->dpio_desc.qman_version;
  132. /* Compute how many 256 QBMAN cycles fit into one ns. This is because
  133. * the interrupt timeout period register needs to be specified in QBMAN
  134. * clock cycles in increments of 256.
  135. */
  136. qman_256_cycles_per_ns = 256000 / (obj->swp_desc.qman_clk / 1000000);
  137. obj->swp_desc.qman_256_cycles_per_ns = qman_256_cycles_per_ns;
  138. obj->swp = qbman_swp_init(&obj->swp_desc);
  139. if (!obj->swp) {
  140. kfree(obj);
  141. return NULL;
  142. }
  143. INIT_LIST_HEAD(&obj->node);
  144. spin_lock_init(&obj->lock_mgmt_cmd);
  145. spin_lock_init(&obj->lock_notifications);
  146. spin_lock_init(&obj->dim_lock);
  147. INIT_LIST_HEAD(&obj->notifications);
  148. /* For now only enable DQRR interrupts */
  149. qbman_swp_interrupt_set_trigger(obj->swp,
  150. QBMAN_SWP_INTERRUPT_DQRI);
  151. qbman_swp_interrupt_clear_status(obj->swp, 0xffffffff);
  152. if (obj->dpio_desc.receives_notifications)
  153. qbman_swp_push_set(obj->swp, 0, 1);
  154. spin_lock(&dpio_list_lock);
  155. list_add_tail(&obj->node, &dpio_list);
  156. if (desc->cpu >= 0 && !dpio_by_cpu[desc->cpu])
  157. dpio_by_cpu[desc->cpu] = obj;
  158. spin_unlock(&dpio_list_lock);
  159. obj->dev = dev;
  160. memset(&obj->rx_dim, 0, sizeof(obj->rx_dim));
  161. INIT_WORK(&obj->rx_dim.work, dpaa2_io_dim_work);
  162. obj->event_ctr = 0;
  163. obj->bytes = 0;
  164. obj->frames = 0;
  165. return obj;
  166. }
  167. /**
  168. * dpaa2_io_down() - release the dpaa2_io object.
  169. * @d: the dpaa2_io object to be released.
  170. *
  171. * The "struct dpaa2_io" type can represent an individual DPIO object (as
  172. * described by "struct dpaa2_io_desc") or an instance of a "DPIO service",
  173. * which can be used to group/encapsulate multiple DPIO objects. In all cases,
  174. * each handle obtained should be released using this function.
  175. */
  176. void dpaa2_io_down(struct dpaa2_io *d)
  177. {
  178. spin_lock(&dpio_list_lock);
  179. dpio_by_cpu[d->dpio_desc.cpu] = NULL;
  180. list_del(&d->node);
  181. spin_unlock(&dpio_list_lock);
  182. kfree(d);
  183. }
  184. #define DPAA_POLL_MAX 32
  185. /**
  186. * dpaa2_io_irq() - ISR for DPIO interrupts
  187. *
  188. * @obj: the given DPIO object.
  189. *
  190. * Return IRQ_HANDLED for success or IRQ_NONE if there
  191. * were no pending interrupts.
  192. */
  193. irqreturn_t dpaa2_io_irq(struct dpaa2_io *obj)
  194. {
  195. const struct dpaa2_dq *dq;
  196. int max = 0;
  197. struct qbman_swp *swp;
  198. u32 status;
  199. obj->event_ctr++;
  200. swp = obj->swp;
  201. status = qbman_swp_interrupt_read_status(swp);
  202. if (!status)
  203. return IRQ_NONE;
  204. dq = qbman_swp_dqrr_next(swp);
  205. while (dq) {
  206. if (qbman_result_is_SCN(dq)) {
  207. struct dpaa2_io_notification_ctx *ctx;
  208. u64 q64;
  209. q64 = qbman_result_SCN_ctx(dq);
  210. ctx = (void *)(uintptr_t)q64;
  211. ctx->cb(ctx);
  212. } else {
  213. pr_crit("fsl-mc-dpio: Unrecognised/ignored DQRR entry\n");
  214. }
  215. qbman_swp_dqrr_consume(swp, dq);
  216. ++max;
  217. if (max > DPAA_POLL_MAX)
  218. goto done;
  219. dq = qbman_swp_dqrr_next(swp);
  220. }
  221. done:
  222. qbman_swp_interrupt_clear_status(swp, status);
  223. qbman_swp_interrupt_set_inhibit(swp, 0);
  224. return IRQ_HANDLED;
  225. }
  226. /**
  227. * dpaa2_io_get_cpu() - get the cpu associated with a given DPIO object
  228. *
  229. * @d: the given DPIO object.
  230. *
  231. * Return the cpu associated with the DPIO object
  232. */
  233. int dpaa2_io_get_cpu(struct dpaa2_io *d)
  234. {
  235. return d->dpio_desc.cpu;
  236. }
  237. EXPORT_SYMBOL(dpaa2_io_get_cpu);
  238. /**
  239. * dpaa2_io_service_register() - Prepare for servicing of FQDAN or CDAN
  240. * notifications on the given DPIO service.
  241. * @d: the given DPIO service.
  242. * @ctx: the notification context.
  243. * @dev: the device that requests the register
  244. *
  245. * The caller should make the MC command to attach a DPAA2 object to
  246. * a DPIO after this function completes successfully. In that way:
  247. * (a) The DPIO service is "ready" to handle a notification arrival
  248. * (which might happen before the "attach" command to MC has
  249. * returned control of execution back to the caller)
  250. * (b) The DPIO service can provide back to the caller the 'dpio_id' and
  251. * 'qman64' parameters that it should pass along in the MC command
  252. * in order for the object to be configured to produce the right
  253. * notification fields to the DPIO service.
  254. *
  255. * Return 0 for success, or -ENODEV for failure.
  256. */
  257. int dpaa2_io_service_register(struct dpaa2_io *d,
  258. struct dpaa2_io_notification_ctx *ctx,
  259. struct device *dev)
  260. {
  261. struct device_link *link;
  262. unsigned long irqflags;
  263. d = service_select_by_cpu(d, ctx->desired_cpu);
  264. if (!d)
  265. return -ENODEV;
  266. link = device_link_add(dev, d->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
  267. if (!link)
  268. return -EINVAL;
  269. ctx->dpio_id = d->dpio_desc.dpio_id;
  270. ctx->qman64 = (u64)(uintptr_t)ctx;
  271. ctx->dpio_private = d;
  272. spin_lock_irqsave(&d->lock_notifications, irqflags);
  273. list_add(&ctx->node, &d->notifications);
  274. spin_unlock_irqrestore(&d->lock_notifications, irqflags);
  275. /* Enable the generation of CDAN notifications */
  276. if (ctx->is_cdan)
  277. return qbman_swp_CDAN_set_context_enable(d->swp,
  278. (u16)ctx->id,
  279. ctx->qman64);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(dpaa2_io_service_register);
  283. /**
  284. * dpaa2_io_service_deregister - The opposite of 'register'.
  285. * @service: the given DPIO service.
  286. * @ctx: the notification context.
  287. * @dev: the device that requests to be deregistered
  288. *
  289. * This function should be called only after sending the MC command to
  290. * to detach the notification-producing device from the DPIO.
  291. */
  292. void dpaa2_io_service_deregister(struct dpaa2_io *service,
  293. struct dpaa2_io_notification_ctx *ctx,
  294. struct device *dev)
  295. {
  296. struct dpaa2_io *d = ctx->dpio_private;
  297. unsigned long irqflags;
  298. if (ctx->is_cdan)
  299. qbman_swp_CDAN_disable(d->swp, (u16)ctx->id);
  300. spin_lock_irqsave(&d->lock_notifications, irqflags);
  301. list_del(&ctx->node);
  302. spin_unlock_irqrestore(&d->lock_notifications, irqflags);
  303. }
  304. EXPORT_SYMBOL_GPL(dpaa2_io_service_deregister);
  305. /**
  306. * dpaa2_io_service_rearm() - Rearm the notification for the given DPIO service.
  307. * @d: the given DPIO service.
  308. * @ctx: the notification context.
  309. *
  310. * Once a FQDAN/CDAN has been produced, the corresponding FQ/channel is
  311. * considered "disarmed". Ie. the user can issue pull dequeue operations on that
  312. * traffic source for as long as it likes. Eventually it may wish to "rearm"
  313. * that source to allow it to produce another FQDAN/CDAN, that's what this
  314. * function achieves.
  315. *
  316. * Return 0 for success.
  317. */
  318. int dpaa2_io_service_rearm(struct dpaa2_io *d,
  319. struct dpaa2_io_notification_ctx *ctx)
  320. {
  321. unsigned long irqflags;
  322. int err;
  323. d = service_select_by_cpu(d, ctx->desired_cpu);
  324. if (!unlikely(d))
  325. return -ENODEV;
  326. spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
  327. if (ctx->is_cdan)
  328. err = qbman_swp_CDAN_enable(d->swp, (u16)ctx->id);
  329. else
  330. err = qbman_swp_fq_schedule(d->swp, ctx->id);
  331. spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
  332. return err;
  333. }
  334. EXPORT_SYMBOL_GPL(dpaa2_io_service_rearm);
  335. /**
  336. * dpaa2_io_service_pull_fq() - pull dequeue functions from a fq.
  337. * @d: the given DPIO service.
  338. * @fqid: the given frame queue id.
  339. * @s: the dpaa2_io_store object for the result.
  340. *
  341. * Return 0 for success, or error code for failure.
  342. */
  343. int dpaa2_io_service_pull_fq(struct dpaa2_io *d, u32 fqid,
  344. struct dpaa2_io_store *s)
  345. {
  346. struct qbman_pull_desc pd;
  347. int err;
  348. qbman_pull_desc_clear(&pd);
  349. qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
  350. qbman_pull_desc_set_numframes(&pd, (u8)s->max);
  351. qbman_pull_desc_set_fq(&pd, fqid);
  352. d = service_select(d);
  353. if (!d)
  354. return -ENODEV;
  355. s->swp = d->swp;
  356. err = qbman_swp_pull(d->swp, &pd);
  357. if (err)
  358. s->swp = NULL;
  359. return err;
  360. }
  361. EXPORT_SYMBOL(dpaa2_io_service_pull_fq);
  362. /**
  363. * dpaa2_io_service_pull_channel() - pull dequeue functions from a channel.
  364. * @d: the given DPIO service.
  365. * @channelid: the given channel id.
  366. * @s: the dpaa2_io_store object for the result.
  367. *
  368. * Return 0 for success, or error code for failure.
  369. */
  370. int dpaa2_io_service_pull_channel(struct dpaa2_io *d, u32 channelid,
  371. struct dpaa2_io_store *s)
  372. {
  373. struct qbman_pull_desc pd;
  374. int err;
  375. qbman_pull_desc_clear(&pd);
  376. qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
  377. qbman_pull_desc_set_numframes(&pd, (u8)s->max);
  378. qbman_pull_desc_set_channel(&pd, channelid, qbman_pull_type_prio);
  379. d = service_select(d);
  380. if (!d)
  381. return -ENODEV;
  382. s->swp = d->swp;
  383. err = qbman_swp_pull(d->swp, &pd);
  384. if (err)
  385. s->swp = NULL;
  386. return err;
  387. }
  388. EXPORT_SYMBOL_GPL(dpaa2_io_service_pull_channel);
  389. /**
  390. * dpaa2_io_service_enqueue_fq() - Enqueue a frame to a frame queue.
  391. * @d: the given DPIO service.
  392. * @fqid: the given frame queue id.
  393. * @fd: the frame descriptor which is enqueued.
  394. *
  395. * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
  396. * or -ENODEV if there is no dpio service.
  397. */
  398. int dpaa2_io_service_enqueue_fq(struct dpaa2_io *d,
  399. u32 fqid,
  400. const struct dpaa2_fd *fd)
  401. {
  402. struct qbman_eq_desc ed;
  403. d = service_select(d);
  404. if (!d)
  405. return -ENODEV;
  406. qbman_eq_desc_clear(&ed);
  407. qbman_eq_desc_set_no_orp(&ed, 0);
  408. qbman_eq_desc_set_fq(&ed, fqid);
  409. return qbman_swp_enqueue(d->swp, &ed, fd);
  410. }
  411. EXPORT_SYMBOL(dpaa2_io_service_enqueue_fq);
  412. /**
  413. * dpaa2_io_service_enqueue_multiple_fq() - Enqueue multiple frames
  414. * to a frame queue using one fqid.
  415. * @d: the given DPIO service.
  416. * @fqid: the given frame queue id.
  417. * @fd: the frame descriptor which is enqueued.
  418. * @nb: number of frames to be enqueud
  419. *
  420. * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
  421. * or -ENODEV if there is no dpio service.
  422. */
  423. int dpaa2_io_service_enqueue_multiple_fq(struct dpaa2_io *d,
  424. u32 fqid,
  425. const struct dpaa2_fd *fd,
  426. int nb)
  427. {
  428. struct qbman_eq_desc ed;
  429. d = service_select(d);
  430. if (!d)
  431. return -ENODEV;
  432. qbman_eq_desc_clear(&ed);
  433. qbman_eq_desc_set_no_orp(&ed, 0);
  434. qbman_eq_desc_set_fq(&ed, fqid);
  435. return qbman_swp_enqueue_multiple(d->swp, &ed, fd, NULL, nb);
  436. }
  437. EXPORT_SYMBOL(dpaa2_io_service_enqueue_multiple_fq);
  438. /**
  439. * dpaa2_io_service_enqueue_multiple_desc_fq() - Enqueue multiple frames
  440. * to different frame queue using a list of fqids.
  441. * @d: the given DPIO service.
  442. * @fqid: the given list of frame queue ids.
  443. * @fd: the frame descriptor which is enqueued.
  444. * @nb: number of frames to be enqueud
  445. *
  446. * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
  447. * or -ENODEV if there is no dpio service.
  448. */
  449. int dpaa2_io_service_enqueue_multiple_desc_fq(struct dpaa2_io *d,
  450. u32 *fqid,
  451. const struct dpaa2_fd *fd,
  452. int nb)
  453. {
  454. struct qbman_eq_desc *ed;
  455. int i, ret;
  456. ed = kcalloc(32, sizeof(struct qbman_eq_desc), GFP_KERNEL);
  457. if (!ed)
  458. return -ENOMEM;
  459. d = service_select(d);
  460. if (!d) {
  461. ret = -ENODEV;
  462. goto out;
  463. }
  464. for (i = 0; i < nb; i++) {
  465. qbman_eq_desc_clear(&ed[i]);
  466. qbman_eq_desc_set_no_orp(&ed[i], 0);
  467. qbman_eq_desc_set_fq(&ed[i], fqid[i]);
  468. }
  469. ret = qbman_swp_enqueue_multiple_desc(d->swp, &ed[0], fd, nb);
  470. out:
  471. kfree(ed);
  472. return ret;
  473. }
  474. EXPORT_SYMBOL(dpaa2_io_service_enqueue_multiple_desc_fq);
  475. /**
  476. * dpaa2_io_service_enqueue_qd() - Enqueue a frame to a QD.
  477. * @d: the given DPIO service.
  478. * @qdid: the given queuing destination id.
  479. * @prio: the given queuing priority.
  480. * @qdbin: the given queuing destination bin.
  481. * @fd: the frame descriptor which is enqueued.
  482. *
  483. * Return 0 for successful enqueue, or -EBUSY if the enqueue ring is not ready,
  484. * or -ENODEV if there is no dpio service.
  485. */
  486. int dpaa2_io_service_enqueue_qd(struct dpaa2_io *d,
  487. u32 qdid, u8 prio, u16 qdbin,
  488. const struct dpaa2_fd *fd)
  489. {
  490. struct qbman_eq_desc ed;
  491. d = service_select(d);
  492. if (!d)
  493. return -ENODEV;
  494. qbman_eq_desc_clear(&ed);
  495. qbman_eq_desc_set_no_orp(&ed, 0);
  496. qbman_eq_desc_set_qd(&ed, qdid, qdbin, prio);
  497. return qbman_swp_enqueue(d->swp, &ed, fd);
  498. }
  499. EXPORT_SYMBOL_GPL(dpaa2_io_service_enqueue_qd);
  500. /**
  501. * dpaa2_io_service_release() - Release buffers to a buffer pool.
  502. * @d: the given DPIO object.
  503. * @bpid: the buffer pool id.
  504. * @buffers: the buffers to be released.
  505. * @num_buffers: the number of the buffers to be released.
  506. *
  507. * Return 0 for success, and negative error code for failure.
  508. */
  509. int dpaa2_io_service_release(struct dpaa2_io *d,
  510. u16 bpid,
  511. const u64 *buffers,
  512. unsigned int num_buffers)
  513. {
  514. struct qbman_release_desc rd;
  515. d = service_select(d);
  516. if (!d)
  517. return -ENODEV;
  518. qbman_release_desc_clear(&rd);
  519. qbman_release_desc_set_bpid(&rd, bpid);
  520. return qbman_swp_release(d->swp, &rd, buffers, num_buffers);
  521. }
  522. EXPORT_SYMBOL_GPL(dpaa2_io_service_release);
  523. /**
  524. * dpaa2_io_service_acquire() - Acquire buffers from a buffer pool.
  525. * @d: the given DPIO object.
  526. * @bpid: the buffer pool id.
  527. * @buffers: the buffer addresses for acquired buffers.
  528. * @num_buffers: the expected number of the buffers to acquire.
  529. *
  530. * Return a negative error code if the command failed, otherwise it returns
  531. * the number of buffers acquired, which may be less than the number requested.
  532. * Eg. if the buffer pool is empty, this will return zero.
  533. */
  534. int dpaa2_io_service_acquire(struct dpaa2_io *d,
  535. u16 bpid,
  536. u64 *buffers,
  537. unsigned int num_buffers)
  538. {
  539. unsigned long irqflags;
  540. int err;
  541. d = service_select(d);
  542. if (!d)
  543. return -ENODEV;
  544. spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
  545. err = qbman_swp_acquire(d->swp, bpid, buffers, num_buffers);
  546. spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
  547. return err;
  548. }
  549. EXPORT_SYMBOL_GPL(dpaa2_io_service_acquire);
  550. /*
  551. * 'Stores' are reusable memory blocks for holding dequeue results, and to
  552. * assist with parsing those results.
  553. */
  554. /**
  555. * dpaa2_io_store_create() - Create the dma memory storage for dequeue result.
  556. * @max_frames: the maximum number of dequeued result for frames, must be <= 32.
  557. * @dev: the device to allow mapping/unmapping the DMAable region.
  558. *
  559. * The size of the storage is "max_frames*sizeof(struct dpaa2_dq)".
  560. * The 'dpaa2_io_store' returned is a DPIO service managed object.
  561. *
  562. * Return pointer to dpaa2_io_store struct for successfully created storage
  563. * memory, or NULL on error.
  564. */
  565. struct dpaa2_io_store *dpaa2_io_store_create(unsigned int max_frames,
  566. struct device *dev)
  567. {
  568. struct dpaa2_io_store *ret;
  569. size_t size;
  570. if (!max_frames || (max_frames > 32))
  571. return NULL;
  572. ret = kmalloc(sizeof(*ret), GFP_KERNEL);
  573. if (!ret)
  574. return NULL;
  575. ret->max = max_frames;
  576. size = max_frames * sizeof(struct dpaa2_dq) + 64;
  577. ret->alloced_addr = kzalloc(size, GFP_KERNEL);
  578. if (!ret->alloced_addr) {
  579. kfree(ret);
  580. return NULL;
  581. }
  582. ret->vaddr = PTR_ALIGN(ret->alloced_addr, 64);
  583. ret->paddr = dma_map_single(dev, ret->vaddr,
  584. sizeof(struct dpaa2_dq) * max_frames,
  585. DMA_FROM_DEVICE);
  586. if (dma_mapping_error(dev, ret->paddr)) {
  587. kfree(ret->alloced_addr);
  588. kfree(ret);
  589. return NULL;
  590. }
  591. ret->idx = 0;
  592. ret->dev = dev;
  593. return ret;
  594. }
  595. EXPORT_SYMBOL_GPL(dpaa2_io_store_create);
  596. /**
  597. * dpaa2_io_store_destroy() - Frees the dma memory storage for dequeue
  598. * result.
  599. * @s: the storage memory to be destroyed.
  600. */
  601. void dpaa2_io_store_destroy(struct dpaa2_io_store *s)
  602. {
  603. dma_unmap_single(s->dev, s->paddr, sizeof(struct dpaa2_dq) * s->max,
  604. DMA_FROM_DEVICE);
  605. kfree(s->alloced_addr);
  606. kfree(s);
  607. }
  608. EXPORT_SYMBOL_GPL(dpaa2_io_store_destroy);
  609. /**
  610. * dpaa2_io_store_next() - Determine when the next dequeue result is available.
  611. * @s: the dpaa2_io_store object.
  612. * @is_last: indicate whether this is the last frame in the pull command.
  613. *
  614. * When an object driver performs dequeues to a dpaa2_io_store, this function
  615. * can be used to determine when the next frame result is available. Once
  616. * this function returns non-NULL, a subsequent call to it will try to find
  617. * the next dequeue result.
  618. *
  619. * Note that if a pull-dequeue has a NULL result because the target FQ/channel
  620. * was empty, then this function will also return NULL (rather than expecting
  621. * the caller to always check for this. As such, "is_last" can be used to
  622. * differentiate between "end-of-empty-dequeue" and "still-waiting".
  623. *
  624. * Return dequeue result for a valid dequeue result, or NULL for empty dequeue.
  625. */
  626. struct dpaa2_dq *dpaa2_io_store_next(struct dpaa2_io_store *s, int *is_last)
  627. {
  628. int match;
  629. struct dpaa2_dq *ret = &s->vaddr[s->idx];
  630. match = qbman_result_has_new_result(s->swp, ret);
  631. if (!match) {
  632. *is_last = 0;
  633. return NULL;
  634. }
  635. s->idx++;
  636. if (dpaa2_dq_is_pull_complete(ret)) {
  637. *is_last = 1;
  638. s->idx = 0;
  639. /*
  640. * If we get an empty dequeue result to terminate a zero-results
  641. * vdqcr, return NULL to the caller rather than expecting him to
  642. * check non-NULL results every time.
  643. */
  644. if (!(dpaa2_dq_flags(ret) & DPAA2_DQ_STAT_VALIDFRAME))
  645. ret = NULL;
  646. } else {
  647. prefetch(&s->vaddr[s->idx]);
  648. *is_last = 0;
  649. }
  650. return ret;
  651. }
  652. EXPORT_SYMBOL_GPL(dpaa2_io_store_next);
  653. /**
  654. * dpaa2_io_query_fq_count() - Get the frame and byte count for a given fq.
  655. * @d: the given DPIO object.
  656. * @fqid: the id of frame queue to be queried.
  657. * @fcnt: the queried frame count.
  658. * @bcnt: the queried byte count.
  659. *
  660. * Knowing the FQ count at run-time can be useful in debugging situations.
  661. * The instantaneous frame- and byte-count are hereby returned.
  662. *
  663. * Return 0 for a successful query, and negative error code if query fails.
  664. */
  665. int dpaa2_io_query_fq_count(struct dpaa2_io *d, u32 fqid,
  666. u32 *fcnt, u32 *bcnt)
  667. {
  668. struct qbman_fq_query_np_rslt state;
  669. struct qbman_swp *swp;
  670. unsigned long irqflags;
  671. int ret;
  672. d = service_select(d);
  673. if (!d)
  674. return -ENODEV;
  675. swp = d->swp;
  676. spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
  677. ret = qbman_fq_query_state(swp, fqid, &state);
  678. spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
  679. if (ret)
  680. return ret;
  681. *fcnt = qbman_fq_state_frame_count(&state);
  682. *bcnt = qbman_fq_state_byte_count(&state);
  683. return 0;
  684. }
  685. EXPORT_SYMBOL_GPL(dpaa2_io_query_fq_count);
  686. /**
  687. * dpaa2_io_query_bp_count() - Query the number of buffers currently in a
  688. * buffer pool.
  689. * @d: the given DPIO object.
  690. * @bpid: the index of buffer pool to be queried.
  691. * @num: the queried number of buffers in the buffer pool.
  692. *
  693. * Return 0 for a successful query, and negative error code if query fails.
  694. */
  695. int dpaa2_io_query_bp_count(struct dpaa2_io *d, u16 bpid, u32 *num)
  696. {
  697. struct qbman_bp_query_rslt state;
  698. struct qbman_swp *swp;
  699. unsigned long irqflags;
  700. int ret;
  701. d = service_select(d);
  702. if (!d)
  703. return -ENODEV;
  704. swp = d->swp;
  705. spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
  706. ret = qbman_bp_query(swp, bpid, &state);
  707. spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
  708. if (ret)
  709. return ret;
  710. *num = qbman_bp_info_num_free_bufs(&state);
  711. return 0;
  712. }
  713. EXPORT_SYMBOL_GPL(dpaa2_io_query_bp_count);
  714. /**
  715. * dpaa2_io_set_irq_coalescing() - Set new IRQ coalescing values
  716. * @d: the given DPIO object
  717. * @irq_holdoff: interrupt holdoff (timeout) period in us
  718. *
  719. * Return 0 for success, or negative error code on error.
  720. */
  721. int dpaa2_io_set_irq_coalescing(struct dpaa2_io *d, u32 irq_holdoff)
  722. {
  723. struct qbman_swp *swp = d->swp;
  724. return qbman_swp_set_irq_coalescing(swp, swp->dqrr.dqrr_size - 1,
  725. irq_holdoff);
  726. }
  727. EXPORT_SYMBOL(dpaa2_io_set_irq_coalescing);
  728. /**
  729. * dpaa2_io_get_irq_coalescing() - Get the current IRQ coalescing parameters
  730. * @d: the given DPIO object
  731. * @irq_holdoff: interrupt holdoff (timeout) period in us
  732. */
  733. void dpaa2_io_get_irq_coalescing(struct dpaa2_io *d, u32 *irq_holdoff)
  734. {
  735. struct qbman_swp *swp = d->swp;
  736. qbman_swp_get_irq_coalescing(swp, NULL, irq_holdoff);
  737. }
  738. EXPORT_SYMBOL(dpaa2_io_get_irq_coalescing);
  739. /**
  740. * dpaa2_io_set_adaptive_coalescing() - Enable/disable adaptive coalescing
  741. * @d: the given DPIO object
  742. * @use_adaptive_rx_coalesce: adaptive coalescing state
  743. */
  744. void dpaa2_io_set_adaptive_coalescing(struct dpaa2_io *d,
  745. int use_adaptive_rx_coalesce)
  746. {
  747. d->swp->use_adaptive_rx_coalesce = use_adaptive_rx_coalesce;
  748. }
  749. EXPORT_SYMBOL(dpaa2_io_set_adaptive_coalescing);
  750. /**
  751. * dpaa2_io_get_adaptive_coalescing() - Query adaptive coalescing state
  752. * @d: the given DPIO object
  753. *
  754. * Return 1 when adaptive coalescing is enabled on the DPIO object and 0
  755. * otherwise.
  756. */
  757. int dpaa2_io_get_adaptive_coalescing(struct dpaa2_io *d)
  758. {
  759. return d->swp->use_adaptive_rx_coalesce;
  760. }
  761. EXPORT_SYMBOL(dpaa2_io_get_adaptive_coalescing);
  762. /**
  763. * dpaa2_io_update_net_dim() - Update Net DIM
  764. * @d: the given DPIO object
  765. * @frames: how many frames have been dequeued by the user since the last call
  766. * @bytes: how many bytes have been dequeued by the user since the last call
  767. */
  768. void dpaa2_io_update_net_dim(struct dpaa2_io *d, __u64 frames, __u64 bytes)
  769. {
  770. struct dim_sample dim_sample = {};
  771. if (!d->swp->use_adaptive_rx_coalesce)
  772. return;
  773. spin_lock(&d->dim_lock);
  774. d->bytes += bytes;
  775. d->frames += frames;
  776. dim_update_sample(d->event_ctr, d->frames, d->bytes, &dim_sample);
  777. net_dim(&d->rx_dim, dim_sample);
  778. spin_unlock(&d->dim_lock);
  779. }
  780. EXPORT_SYMBOL(dpaa2_io_update_net_dim);