dpio-service.c 19 KB

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