Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 4 | * Permission to use, copy, modify, and/or distribute this software for |
| 5 | * any purpose with or without fee is hereby granted, provided that the |
| 6 | * above copyright notice and this permission notice appear in all copies. |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 13 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | ************************************************************************** |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * nss_if.c |
| 19 | * NSS base interfaces |
| 20 | */ |
| 21 | |
| 22 | #include "nss_tx_rx_common.h" |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * nss_if_msg_handler() |
| 26 | * Handle NSS -> HLOS messages for base class interfaces |
| 27 | */ |
| 28 | void nss_if_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, |
| 29 | __attribute__((unused))void *app_data) |
| 30 | { |
| 31 | struct nss_if_msg *nim = (struct nss_if_msg *)ncm; |
| 32 | nss_if_msg_callback_t cb; |
| 33 | |
| 34 | /* |
| 35 | * We only support base class messages with this interface |
| 36 | */ |
| 37 | if (ncm->type > NSS_IF_MAX_MSG_TYPES) { |
| 38 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 39 | return; |
| 40 | } |
| 41 | |
Sundarajan Srinivasan | cd1631b | 2015-06-18 01:23:30 -0700 | [diff] [blame] | 42 | if (!nss_is_dynamic_interface(ncm->interface) && |
| 43 | !((ncm->interface >= NSS_PHYSICAL_IF_START) && (ncm->interface < NSS_VIRTUAL_IF_START))) { |
| 44 | nss_warning("%p: interface %d not in physical or dynamic if range\n", nss_ctx, ncm->interface); |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame^] | 48 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_if_msg)) { |
| 49 | nss_warning("%p: message length too big: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Log failures |
| 55 | */ |
| 56 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 57 | |
| 58 | /* |
| 59 | * Do we have a callback? |
| 60 | */ |
| 61 | if (!ncm->cb) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Callback |
| 67 | */ |
| 68 | cb = (nss_if_msg_callback_t)ncm->cb; |
| 69 | cb((void *)ncm->app_data, nim); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * nss_if_tx_buf() |
| 74 | * Send packet to interface owned by NSS |
| 75 | */ |
| 76 | nss_tx_status_t nss_if_tx_buf(struct nss_ctx_instance *nss_ctx, struct sk_buff *os_buf, uint32_t if_num) |
| 77 | { |
| 78 | int32_t status; |
| 79 | |
| 80 | nss_trace("%p: If Tx packet, id:%d, data=%p", nss_ctx, if_num, os_buf->data); |
| 81 | |
| 82 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 83 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 84 | nss_warning("%p: 'Phys If Tx' packet dropped as core not ready", nss_ctx); |
| 85 | return NSS_TX_FAILURE_NOT_READY; |
| 86 | } |
| 87 | |
Sundarajan Srinivasan | cd1631b | 2015-06-18 01:23:30 -0700 | [diff] [blame] | 88 | if (!nss_is_dynamic_interface(if_num) && |
| 89 | !((if_num >= NSS_PHYSICAL_IF_START) && (if_num < NSS_VIRTUAL_IF_START))) { |
| 90 | nss_warning("%p: interface %d not in physical or dynamic if range\n", nss_ctx, if_num); |
| 91 | return NSS_TX_FAILURE_BAD_PARAM; |
| 92 | } |
| 93 | |
Pamidipati, Vijay | efcc469 | 2014-05-09 14:47:38 +0530 | [diff] [blame] | 94 | status = nss_core_send_buffer(nss_ctx, if_num, os_buf, NSS_IF_DATA_QUEUE_0, H2N_BUFFER_PACKET, 0); |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 95 | if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) { |
| 96 | nss_warning("%p: Unable to enqueue 'Phys If Tx' packet\n", nss_ctx); |
| 97 | if (status == NSS_CORE_STATUS_FAILURE_QUEUE) { |
| 98 | return NSS_TX_FAILURE_QUEUE; |
| 99 | } |
| 100 | return NSS_TX_FAILURE; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Kick the NSS awake so it can process our new entry. |
| 105 | */ |
Pamidipati, Vijay | efcc469 | 2014-05-09 14:47:38 +0530 | [diff] [blame] | 106 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_DATA_QUEUE_0].desc_ring.int_bit, |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 107 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 108 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_PACKET]); |
| 109 | return NSS_TX_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * nss_if_tx_msg() |
| 114 | * Transmit a message to the specific interface on this core. |
| 115 | */ |
| 116 | nss_tx_status_t nss_if_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_if_msg *nim) |
| 117 | { |
| 118 | struct nss_cmn_msg *ncm = &nim->cm; |
| 119 | struct nss_if_msg *nim2; |
| 120 | struct net_device *dev; |
| 121 | struct sk_buff *nbuf; |
| 122 | uint32_t if_num; |
| 123 | int32_t status; |
| 124 | |
| 125 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 126 | nss_warning("Interface could not be created as core not ready"); |
| 127 | return NSS_TX_FAILURE; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Sanity check the message |
| 132 | */ |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 133 | |
| 134 | if (ncm->type > NSS_IF_MAX_MSG_TYPES) { |
| 135 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 136 | return NSS_TX_FAILURE; |
| 137 | } |
| 138 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame^] | 139 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_if_msg)) { |
| 140 | nss_warning("%p: invalid length: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 141 | return NSS_TX_FAILURE; |
| 142 | } |
| 143 | |
| 144 | if_num = ncm->interface; |
Sundarajan Srinivasan | 7037484 | 2014-11-19 15:22:52 -0800 | [diff] [blame] | 145 | dev = nss_top_main.subsys_dp_register[if_num].ndev; |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 146 | if (!dev) { |
| 147 | nss_warning("%p: Unregister interface %d: no context", nss_ctx, if_num); |
| 148 | return NSS_TX_FAILURE_BAD_PARAM; |
| 149 | } |
| 150 | |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 151 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 152 | if (unlikely(!nbuf)) { |
Sundarajan Srinivasan | 62fee7e | 2015-01-22 11:13:10 -0800 | [diff] [blame] | 153 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 154 | nss_warning("%p: interface %p: command allocation failed", nss_ctx, dev); |
| 155 | return NSS_TX_FAILURE; |
| 156 | } |
| 157 | |
| 158 | nim2 = (struct nss_if_msg *)skb_put(nbuf, sizeof(struct nss_if_msg)); |
| 159 | memcpy(nim2, nim, sizeof(struct nss_if_msg)); |
| 160 | |
| 161 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 162 | if (status != NSS_CORE_STATUS_SUCCESS) { |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 163 | dev_kfree_skb_any(nbuf); |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 164 | nss_warning("%p: Unable to enqueue 'interface' command\n", nss_ctx); |
| 165 | return NSS_TX_FAILURE; |
| 166 | } |
| 167 | |
| 168 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit, |
| 169 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 170 | return NSS_TX_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * nss_if_register() |
| 175 | * Primary registration for receiving data and msgs from an interface. |
| 176 | */ |
| 177 | struct nss_ctx_instance *nss_if_register(uint32_t if_num, |
Sakthi Vignesh Radhakrishnan | ef91849 | 2014-04-07 14:24:19 -0700 | [diff] [blame] | 178 | nss_if_rx_callback_t rx_callback, |
| 179 | nss_if_msg_callback_t msg_callback, |
Sol Kavy | a55c9a5 | 2014-04-08 14:33:16 -0700 | [diff] [blame] | 180 | struct net_device *if_ctx) |
| 181 | { |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * nss_if_unregister() |
| 187 | * Unregisteer the callback for this interface |
| 188 | */ |
| 189 | void nss_if_unregister(uint32_t if_num) |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | EXPORT_SYMBOL(nss_if_tx_msg); |
| 194 | EXPORT_SYMBOL(nss_if_register); |
| 195 | EXPORT_SYMBOL(nss_if_unregister); |