return 0;
}
+static struct dlb_ldb_port *
+dlb_get_domain_used_ldb_port(u32 id, struct dlb_domain *domain)
+{
+ struct dlb_list_entry *iter;
+ struct dlb_ldb_port *port;
+ RTE_SET_USED(iter);
+
+ if (id >= DLB_MAX_NUM_LDB_PORTS)
+ return NULL;
+
+ DLB_DOM_LIST_FOR(domain->used_ldb_ports, port, iter)
+ if (port->id == id)
+ return port;
+
+ DLB_DOM_LIST_FOR(domain->avail_ldb_ports, port, iter)
+ if (port->id == id)
+ return port;
+
+ return NULL;
+}
+
+static void
+dlb_log_pending_port_unmaps_args(struct dlb_hw *hw,
+ struct dlb_pending_port_unmaps_args *args)
+{
+ DLB_HW_INFO(hw, "DLB pending port unmaps arguments:\n");
+ DLB_HW_INFO(hw, "\tPort ID: %d\n", args->port_id);
+}
+
+int dlb_hw_pending_port_unmaps(struct dlb_hw *hw,
+ u32 domain_id,
+ struct dlb_pending_port_unmaps_args *args,
+ struct dlb_cmd_response *resp)
+{
+ struct dlb_domain *domain;
+ struct dlb_ldb_port *port;
+
+ dlb_log_pending_port_unmaps_args(hw, args);
+
+ domain = dlb_get_domain_from_id(hw, domain_id);
+
+ if (domain == NULL) {
+ resp->status = DLB_ST_INVALID_DOMAIN_ID;
+ return -EINVAL;
+ }
+
+ port = dlb_get_domain_used_ldb_port(args->port_id, domain);
+ if (port == NULL || !port->configured) {
+ resp->status = DLB_ST_INVALID_PORT_ID;
+ return -EINVAL;
+ }
+
+ resp->id = port->num_pending_removals;
+
+ return 0;
+}
+
+static void dlb_log_unmap_qid(struct dlb_hw *hw,
+ u32 domain_id,
+ struct dlb_unmap_qid_args *args)
+{
+ DLB_HW_INFO(hw, "DLB unmap QID arguments:\n");
+ DLB_HW_INFO(hw, "\tDomain ID: %d\n",
+ domain_id);
+ DLB_HW_INFO(hw, "\tPort ID: %d\n",
+ args->port_id);
+ DLB_HW_INFO(hw, "\tQueue ID: %d\n",
+ args->qid);
+ if (args->qid < DLB_MAX_NUM_LDB_QUEUES)
+ DLB_HW_INFO(hw, "\tQueue's num mappings: %d\n",
+ hw->rsrcs.ldb_queues[args->qid].num_mappings);
+}
+
+static struct dlb_ldb_queue *dlb_get_domain_ldb_queue(u32 id,
+ struct dlb_domain *domain)
+{
+ struct dlb_list_entry *iter;
+ struct dlb_ldb_queue *queue;
+ RTE_SET_USED(iter);
+
+ if (id >= DLB_MAX_NUM_LDB_QUEUES)
+ return NULL;
+
+ DLB_DOM_LIST_FOR(domain->used_ldb_queues, queue, iter)
+ if (queue->id == id)
+ return queue;
+
+ return NULL;
+}
+
+static bool
+dlb_port_find_slot_with_pending_map_queue(struct dlb_ldb_port *port,
+ struct dlb_ldb_queue *queue,
+ int *slot)
+{
+ int i;
+
+ for (i = 0; i < DLB_MAX_NUM_QIDS_PER_LDB_CQ; i++) {
+ struct dlb_ldb_port_qid_map *map = &port->qid_map[i];
+
+ if (map->state == DLB_QUEUE_UNMAP_IN_PROGRESS_PENDING_MAP &&
+ map->pending_qid == queue->id)
+ break;
+ }
+
+ *slot = i;
+
+ return (i < DLB_MAX_NUM_QIDS_PER_LDB_CQ);
+}
+
+static int dlb_verify_unmap_qid_args(struct dlb_hw *hw,
+ u32 domain_id,
+ struct dlb_unmap_qid_args *args,
+ struct dlb_cmd_response *resp)
+{
+ enum dlb_qid_map_state state;
+ struct dlb_domain *domain;
+ struct dlb_ldb_port *port;
+ struct dlb_ldb_queue *queue;
+ int slot;
+ int id;
+
+ domain = dlb_get_domain_from_id(hw, domain_id);
+
+ if (domain == NULL) {
+ resp->status = DLB_ST_INVALID_DOMAIN_ID;
+ return -1;
+ }
+
+ if (!domain->configured) {
+ resp->status = DLB_ST_DOMAIN_NOT_CONFIGURED;
+ return -1;
+ }
+
+ id = args->port_id;
+
+ port = dlb_get_domain_used_ldb_port(id, domain);
+
+ if (port == NULL || !port->configured) {
+ resp->status = DLB_ST_INVALID_PORT_ID;
+ return -1;
+ }
+
+ if (port->domain_id != domain->id) {
+ resp->status = DLB_ST_INVALID_PORT_ID;
+ return -1;
+ }
+
+ queue = dlb_get_domain_ldb_queue(args->qid, domain);
+
+ if (queue == NULL || !queue->configured) {
+ DLB_HW_ERR(hw, "[%s()] Can't unmap unconfigured queue %d\n",
+ __func__, args->qid);
+ resp->status = DLB_ST_INVALID_QID;
+ return -1;
+ }
+
+ /* Verify that the port has the queue mapped. From the application's
+ * perspective a queue is mapped if it is actually mapped, the map is
+ * in progress, or the map is blocked pending an unmap.
+ */
+ state = DLB_QUEUE_MAPPED;
+ if (dlb_port_find_slot_queue(port, state, queue, &slot))
+ return 0;
+
+ state = DLB_QUEUE_MAP_IN_PROGRESS;
+ if (dlb_port_find_slot_queue(port, state, queue, &slot))
+ return 0;
+
+ if (dlb_port_find_slot_with_pending_map_queue(port, queue, &slot))
+ return 0;
+
+ resp->status = DLB_ST_INVALID_QID;
+ return -1;
+}
+
+int dlb_hw_unmap_qid(struct dlb_hw *hw,
+ u32 domain_id,
+ struct dlb_unmap_qid_args *args,
+ struct dlb_cmd_response *resp)
+{
+ enum dlb_qid_map_state state;
+ struct dlb_ldb_queue *queue;
+ struct dlb_ldb_port *port;
+ struct dlb_domain *domain;
+ bool unmap_complete;
+ int i, ret, id;
+
+ dlb_log_unmap_qid(hw, domain_id, args);
+
+ /* Verify that hardware resources are available before attempting to
+ * satisfy the request. This simplifies the error unwinding code.
+ */
+ if (dlb_verify_unmap_qid_args(hw, domain_id, args, resp))
+ return -EINVAL;
+
+ domain = dlb_get_domain_from_id(hw, domain_id);
+ if (domain == NULL) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: domain not found\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ id = args->port_id;
+
+ port = dlb_get_domain_used_ldb_port(id, domain);
+ if (port == NULL) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port not found\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ queue = dlb_get_domain_ldb_queue(args->qid, domain);
+ if (queue == NULL) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: queue not found\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ /* If the queue hasn't been mapped yet, we need to update the slot's
+ * state and re-enable the queue's inflights.
+ */
+ state = DLB_QUEUE_MAP_IN_PROGRESS;
+ if (dlb_port_find_slot_queue(port, state, queue, &i)) {
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ /* Since the in-progress map was aborted, re-enable the QID's
+ * inflights.
+ */
+ if (queue->num_pending_additions == 0)
+ dlb_ldb_queue_set_inflight_limit(hw, queue);
+
+ state = DLB_QUEUE_UNMAPPED;
+ ret = dlb_port_slot_state_transition(hw, port, queue, i, state);
+ if (ret)
+ return ret;
+
+ goto unmap_qid_done;
+ }
+
+ /* If the queue mapping is on hold pending an unmap, we simply need to
+ * update the slot's state.
+ */
+ if (dlb_port_find_slot_with_pending_map_queue(port, queue, &i)) {
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ state = DLB_QUEUE_UNMAP_IN_PROGRESS;
+ ret = dlb_port_slot_state_transition(hw, port, queue, i, state);
+ if (ret)
+ return ret;
+
+ goto unmap_qid_done;
+ }
+
+ state = DLB_QUEUE_MAPPED;
+ if (!dlb_port_find_slot_queue(port, state, queue, &i)) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: no available CQ slots\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ /* QID->CQ mapping removal is an asynchronous procedure. It requires
+ * stopping the DLB from scheduling this CQ, draining all inflights
+ * from the CQ, then unmapping the queue from the CQ. This function
+ * simply marks the port as needing the queue unmapped, and (if
+ * necessary) starts the unmapping worker thread.
+ */
+ dlb_ldb_port_cq_disable(hw, port);
+
+ state = DLB_QUEUE_UNMAP_IN_PROGRESS;
+ ret = dlb_port_slot_state_transition(hw, port, queue, i, state);
+ if (ret)
+ return ret;
+
+ /* Attempt to finish the unmapping now, in case the port has no
+ * outstanding inflights. If that's not the case, this will fail and
+ * the unmapping will be completed at a later time.
+ */
+ unmap_complete = dlb_domain_finish_unmap_port(hw, domain, port);
+
+ /* If the unmapping couldn't complete immediately, launch the worker
+ * thread (if it isn't already launched) to finish it later.
+ */
+ if (!unmap_complete && !os_worker_active(hw))
+ os_schedule_work(hw);
+
+unmap_qid_done:
+ resp->status = 0;
+
+ return 0;
+}
+
+static void dlb_log_map_qid(struct dlb_hw *hw,
+ u32 domain_id,
+ struct dlb_map_qid_args *args)
+{
+ DLB_HW_INFO(hw, "DLB map QID arguments:\n");
+ DLB_HW_INFO(hw, "\tDomain ID: %d\n", domain_id);
+ DLB_HW_INFO(hw, "\tPort ID: %d\n", args->port_id);
+ DLB_HW_INFO(hw, "\tQueue ID: %d\n", args->qid);
+ DLB_HW_INFO(hw, "\tPriority: %d\n", args->priority);
+}
+
+static int dlb_verify_map_qid_args(struct dlb_hw *hw,
+ u32 domain_id,
+ struct dlb_map_qid_args *args,
+ struct dlb_cmd_response *resp)
+{
+ struct dlb_domain *domain;
+ struct dlb_ldb_port *port;
+ struct dlb_ldb_queue *queue;
+ int id;
+
+ domain = dlb_get_domain_from_id(hw, domain_id);
+
+ if (domain == NULL) {
+ resp->status = DLB_ST_INVALID_DOMAIN_ID;
+ return -1;
+ }
+
+ if (!domain->configured) {
+ resp->status = DLB_ST_DOMAIN_NOT_CONFIGURED;
+ return -1;
+ }
+
+ id = args->port_id;
+
+ port = dlb_get_domain_used_ldb_port(id, domain);
+
+ if (port == NULL || !port->configured) {
+ resp->status = DLB_ST_INVALID_PORT_ID;
+ return -1;
+ }
+
+ if (args->priority >= DLB_QID_PRIORITIES) {
+ resp->status = DLB_ST_INVALID_PRIORITY;
+ return -1;
+ }
+
+ queue = dlb_get_domain_ldb_queue(args->qid, domain);
+
+ if (queue == NULL || !queue->configured) {
+ resp->status = DLB_ST_INVALID_QID;
+ return -1;
+ }
+
+ if (queue->domain_id != domain->id) {
+ resp->status = DLB_ST_INVALID_QID;
+ return -1;
+ }
+
+ if (port->domain_id != domain->id) {
+ resp->status = DLB_ST_INVALID_PORT_ID;
+ return -1;
+ }
+
+ return 0;
+}
+
+static int dlb_verify_map_qid_slot_available(struct dlb_ldb_port *port,
+ struct dlb_ldb_queue *queue,
+ struct dlb_cmd_response *resp)
+{
+ enum dlb_qid_map_state state;
+ int i;
+
+ /* Unused slot available? */
+ if (port->num_mappings < DLB_MAX_NUM_QIDS_PER_LDB_CQ)
+ return 0;
+
+ /* If the queue is already mapped (from the application's perspective),
+ * this is simply a priority update.
+ */
+ state = DLB_QUEUE_MAPPED;
+ if (dlb_port_find_slot_queue(port, state, queue, &i))
+ return 0;
+
+ state = DLB_QUEUE_MAP_IN_PROGRESS;
+ if (dlb_port_find_slot_queue(port, state, queue, &i))
+ return 0;
+
+ if (dlb_port_find_slot_with_pending_map_queue(port, queue, &i))
+ return 0;
+
+ /* If the slot contains an unmap in progress, it's considered
+ * available.
+ */
+ state = DLB_QUEUE_UNMAP_IN_PROGRESS;
+ if (dlb_port_find_slot(port, state, &i))
+ return 0;
+
+ state = DLB_QUEUE_UNMAPPED;
+ if (dlb_port_find_slot(port, state, &i))
+ return 0;
+
+ resp->status = DLB_ST_NO_QID_SLOTS_AVAILABLE;
+ return -EINVAL;
+}
+
+static void dlb_ldb_port_change_qid_priority(struct dlb_hw *hw,
+ struct dlb_ldb_port *port,
+ int slot,
+ struct dlb_map_qid_args *args)
+{
+ union dlb_lsp_cq2priov r0;
+
+ /* Read-modify-write the priority and valid bit register */
+ r0.val = DLB_CSR_RD(hw, DLB_LSP_CQ2PRIOV(port->id));
+
+ r0.field.v |= 1 << slot;
+ r0.field.prio |= (args->priority & 0x7) << slot * 3;
+
+ DLB_CSR_WR(hw, DLB_LSP_CQ2PRIOV(port->id), r0.val);
+
+ dlb_flush_csr(hw);
+
+ port->qid_map[slot].priority = args->priority;
+}
+
+int dlb_hw_map_qid(struct dlb_hw *hw,
+ u32 domain_id,
+ struct dlb_map_qid_args *args,
+ struct dlb_cmd_response *resp)
+{
+ enum dlb_qid_map_state state;
+ struct dlb_ldb_queue *queue;
+ struct dlb_ldb_port *port;
+ struct dlb_domain *domain;
+ int ret, i, id;
+ u8 prio;
+
+ dlb_log_map_qid(hw, domain_id, args);
+
+ /* Verify that hardware resources are available before attempting to
+ * satisfy the request. This simplifies the error unwinding code.
+ */
+ if (dlb_verify_map_qid_args(hw, domain_id, args, resp))
+ return -EINVAL;
+
+ prio = args->priority;
+
+ domain = dlb_get_domain_from_id(hw, domain_id);
+ if (domain == NULL) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: domain not found\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ id = args->port_id;
+
+ port = dlb_get_domain_used_ldb_port(id, domain);
+ if (port == NULL) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port not found\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ queue = dlb_get_domain_ldb_queue(args->qid, domain);
+ if (queue == NULL) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: queue not found\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ /* If there are any outstanding detach operations for this port,
+ * attempt to complete them. This may be necessary to free up a QID
+ * slot for this requested mapping.
+ */
+ if (port->num_pending_removals)
+ dlb_domain_finish_unmap_port(hw, domain, port);
+
+ ret = dlb_verify_map_qid_slot_available(port, queue, resp);
+ if (ret)
+ return ret;
+
+ /* Hardware requires disabling the CQ before mapping QIDs. */
+ if (port->enabled)
+ dlb_ldb_port_cq_disable(hw, port);
+
+ /* If this is only a priority change, don't perform the full QID->CQ
+ * mapping procedure
+ */
+ state = DLB_QUEUE_MAPPED;
+ if (dlb_port_find_slot_queue(port, state, queue, &i)) {
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ if (prio != port->qid_map[i].priority) {
+ dlb_ldb_port_change_qid_priority(hw, port, i, args);
+ DLB_HW_INFO(hw, "DLB map: priority change only\n");
+ }
+
+ state = DLB_QUEUE_MAPPED;
+ ret = dlb_port_slot_state_transition(hw, port, queue, i, state);
+ if (ret)
+ return ret;
+
+ goto map_qid_done;
+ }
+
+ state = DLB_QUEUE_UNMAP_IN_PROGRESS;
+ if (dlb_port_find_slot_queue(port, state, queue, &i)) {
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ if (prio != port->qid_map[i].priority) {
+ dlb_ldb_port_change_qid_priority(hw, port, i, args);
+ DLB_HW_INFO(hw, "DLB map: priority change only\n");
+ }
+
+ state = DLB_QUEUE_MAPPED;
+ ret = dlb_port_slot_state_transition(hw, port, queue, i, state);
+ if (ret)
+ return ret;
+
+ goto map_qid_done;
+ }
+
+ /* If this is a priority change on an in-progress mapping, don't
+ * perform the full QID->CQ mapping procedure.
+ */
+ state = DLB_QUEUE_MAP_IN_PROGRESS;
+ if (dlb_port_find_slot_queue(port, state, queue, &i)) {
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ port->qid_map[i].priority = prio;
+
+ DLB_HW_INFO(hw, "DLB map: priority change only\n");
+
+ goto map_qid_done;
+ }
+
+ /* If this is a priority change on a pending mapping, update the
+ * pending priority
+ */
+ if (dlb_port_find_slot_with_pending_map_queue(port, queue, &i)) {
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ port->qid_map[i].pending_priority = prio;
+
+ DLB_HW_INFO(hw, "DLB map: priority change only\n");
+
+ goto map_qid_done;
+ }
+
+ /* If all the CQ's slots are in use, then there's an unmap in progress
+ * (guaranteed by dlb_verify_map_qid_slot_available()), so add this
+ * mapping to pending_map and return. When the removal is completed for
+ * the slot's current occupant, this mapping will be performed.
+ */
+ if (!dlb_port_find_slot(port, DLB_QUEUE_UNMAPPED, &i)) {
+ if (dlb_port_find_slot(port, DLB_QUEUE_UNMAP_IN_PROGRESS, &i)) {
+ enum dlb_qid_map_state state;
+
+ if (i >= DLB_MAX_NUM_QIDS_PER_LDB_CQ) {
+ DLB_HW_ERR(hw,
+ "[%s():%d] Internal error: port slot tracking failed\n",
+ __func__, __LINE__);
+ return -EFAULT;
+ }
+
+ port->qid_map[i].pending_qid = queue->id;
+ port->qid_map[i].pending_priority = prio;
+
+ state = DLB_QUEUE_UNMAP_IN_PROGRESS_PENDING_MAP;
+
+ ret = dlb_port_slot_state_transition(hw, port, queue,
+ i, state);
+ if (ret)
+ return ret;
+
+ DLB_HW_INFO(hw, "DLB map: map pending removal\n");
+
+ goto map_qid_done;
+ }
+ }
+
+ /* If the domain has started, a special "dynamic" CQ->queue mapping
+ * procedure is required in order to safely update the CQ<->QID tables.
+ * The "static" procedure cannot be used when traffic is flowing,
+ * because the CQ<->QID tables cannot be updated atomically and the
+ * scheduler won't see the new mapping unless the queue's if_status
+ * changes, which isn't guaranteed.
+ */
+ ret = dlb_ldb_port_map_qid(hw, domain, port, queue, prio);
+
+ /* If ret is less than zero, it's due to an internal error */
+ if (ret < 0)
+ return ret;
+
+map_qid_done:
+ if (port->enabled)
+ dlb_ldb_port_cq_enable(hw, port);
+
+ resp->status = 0;
+
+ return 0;
+}
+