Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Thomas Wu | fc4d9fd | 2017-03-22 10:15:30 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [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 | #include "nss_ppe.h" |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 18 | #include "nss_ppe_stats.h" |
Amit Gupta | 79c1c20 | 2017-06-30 15:28:13 +0530 | [diff] [blame] | 19 | |
Amit Gupta | 6c0f641 | 2017-11-20 15:42:43 +0530 | [diff] [blame] | 20 | DEFINE_SPINLOCK(nss_ppe_stats_lock); |
| 21 | |
Amit Gupta | cb1696b | 2017-11-13 13:09:06 +0530 | [diff] [blame] | 22 | struct nss_ppe_stats_debug nss_ppe_debug_stats; |
Amit Gupta | 6c0f641 | 2017-11-20 15:42:43 +0530 | [diff] [blame] | 23 | struct nss_ppe_pvt ppe_pvt; |
Amit Gupta | cb1696b | 2017-11-13 13:09:06 +0530 | [diff] [blame] | 24 | |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 25 | /* |
| 26 | * nss_ppe_verify_ifnum() |
| 27 | * Verify PPE interface number. |
| 28 | */ |
| 29 | static inline bool nss_ppe_verify_ifnum(int if_num) |
| 30 | { |
| 31 | return nss_is_dynamic_interface(if_num) || (if_num == NSS_PPE_INTERFACE); |
| 32 | } |
| 33 | |
| 34 | /* |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 35 | * nss_ppe_callback() |
| 36 | * Callback to handle the completion of NSS->HLOS messages. |
| 37 | */ |
| 38 | static void nss_ppe_callback(void *app_data, struct nss_ppe_msg *npm) |
| 39 | { |
| 40 | nss_ppe_msg_callback_t callback = (nss_ppe_msg_callback_t)ppe_pvt.cb; |
| 41 | void *data = ppe_pvt.app_data; |
| 42 | |
| 43 | ppe_pvt.response = NSS_TX_SUCCESS; |
| 44 | ppe_pvt.cb = NULL; |
| 45 | ppe_pvt.app_data = NULL; |
| 46 | |
| 47 | if (npm->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 48 | nss_warning("ppe error response %d\n", npm->cm.response); |
| 49 | ppe_pvt.response = npm->cm.response; |
| 50 | } |
| 51 | |
| 52 | if (callback) { |
| 53 | callback(data, npm); |
| 54 | } |
| 55 | complete(&ppe_pvt.complete); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * nss_ppe_tx_msg() |
| 60 | * Transmit a ppe message to NSSFW |
| 61 | */ |
| 62 | nss_tx_status_t nss_ppe_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_ppe_msg *msg) |
| 63 | { |
| 64 | struct nss_ppe_msg *nm; |
| 65 | struct nss_cmn_msg *ncm = &msg->cm; |
| 66 | struct sk_buff *nbuf; |
| 67 | int32_t status; |
| 68 | |
| 69 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 70 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 71 | nss_warning("%p: ppe msg dropped as core not ready\n", nss_ctx); |
| 72 | return NSS_TX_FAILURE_NOT_READY; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Sanity check the message |
| 77 | */ |
| 78 | if (ncm->type >= NSS_PPE_MSG_MAX) { |
| 79 | nss_warning("%p: message type out of range: %d\n", nss_ctx, ncm->type); |
| 80 | return NSS_TX_FAILURE; |
| 81 | } |
| 82 | |
| 83 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ppe_msg)) { |
| 84 | nss_warning("%p: message length is invalid: %d\n", nss_ctx, nss_cmn_get_msg_len(ncm)); |
| 85 | return NSS_TX_FAILURE; |
| 86 | } |
| 87 | |
| 88 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 89 | if (unlikely(!nbuf)) { |
| 90 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
| 91 | nss_warning("%p: msg dropped as command allocation failed\n", nss_ctx); |
| 92 | return NSS_TX_FAILURE; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Copy the message to our skb |
| 97 | */ |
| 98 | nm = (struct nss_ppe_msg *)skb_put(nbuf, sizeof(struct nss_ppe_msg)); |
| 99 | memcpy(nm, msg, sizeof(struct nss_ppe_msg)); |
| 100 | |
| 101 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 102 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 103 | dev_kfree_skb_any(nbuf); |
| 104 | nss_warning("%p: Unable to enqueue 'ppe message'\n", nss_ctx); |
| 105 | return NSS_TX_FAILURE; |
| 106 | } |
| 107 | |
| 108 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
| 109 | |
| 110 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 111 | return NSS_TX_SUCCESS; |
| 112 | } |
| 113 | EXPORT_SYMBOL(nss_ppe_tx_msg); |
| 114 | |
| 115 | /* |
| 116 | * nss_ppe_tx_msg_sync() |
| 117 | * Transmit a ppe message to NSS firmware synchronously. |
| 118 | */ |
| 119 | nss_tx_status_t nss_ppe_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_ppe_msg *npm) |
| 120 | { |
| 121 | nss_tx_status_t status; |
| 122 | int ret = 0; |
| 123 | |
| 124 | down(&ppe_pvt.sem); |
| 125 | ppe_pvt.cb = (void *)npm->cm.cb; |
| 126 | ppe_pvt.app_data = (void *)npm->cm.app_data; |
| 127 | |
| 128 | npm->cm.cb = (nss_ptr_t)nss_ppe_callback; |
| 129 | npm->cm.app_data = (nss_ptr_t)NULL; |
| 130 | |
| 131 | status = nss_ppe_tx_msg(nss_ctx, npm); |
| 132 | if (status != NSS_TX_SUCCESS) { |
| 133 | nss_warning("%p: ppe_tx_msg failed\n", nss_ctx); |
| 134 | up(&ppe_pvt.sem); |
| 135 | return status; |
| 136 | } |
| 137 | |
| 138 | ret = wait_for_completion_timeout(&ppe_pvt.complete, msecs_to_jiffies(NSS_PPE_TX_TIMEOUT)); |
| 139 | if (!ret) { |
| 140 | nss_warning("%p: ppe msg tx failed due to timeout\n", nss_ctx); |
| 141 | ppe_pvt.response = NSS_TX_FAILURE; |
| 142 | } |
| 143 | |
| 144 | status = ppe_pvt.response; |
| 145 | up(&ppe_pvt.sem); |
| 146 | return status; |
| 147 | } |
| 148 | EXPORT_SYMBOL(nss_ppe_tx_msg_sync); |
| 149 | |
| 150 | /* |
| 151 | * nss_ppe_get_context() |
| 152 | * Get NSS context instance for ppe |
| 153 | */ |
| 154 | struct nss_ctx_instance *nss_ppe_get_context(void) |
| 155 | { |
| 156 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.ppe_handler_id]; |
| 157 | } |
| 158 | EXPORT_SYMBOL(nss_ppe_get_context); |
| 159 | |
| 160 | /* |
| 161 | * nss_ppe_msg_init() |
| 162 | * Initialize nss_ppe_msg. |
| 163 | */ |
| 164 | void nss_ppe_msg_init(struct nss_ppe_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data) |
| 165 | { |
| 166 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 167 | } |
| 168 | EXPORT_SYMBOL(nss_ppe_msg_init); |
| 169 | |
| 170 | /* |
| 171 | * nss_ppe_tx_l2_exception_msg |
| 172 | * API to send vsi assign message to NSS FW |
| 173 | */ |
| 174 | nss_tx_status_t nss_ppe_tx_l2_exception_msg(uint32_t if_num, bool exception_enable) |
| 175 | { |
| 176 | struct nss_ctx_instance *nss_ctx = nss_ppe_get_context(); |
| 177 | struct nss_ppe_msg npm; |
| 178 | |
| 179 | if (!nss_ctx) { |
| 180 | nss_warning("Can't get nss context\n"); |
| 181 | return NSS_TX_FAILURE; |
| 182 | } |
| 183 | |
| 184 | if (!nss_ppe_verify_ifnum(if_num)) { |
| 185 | nss_warning("%p: invalid interface %d\n", nss_ctx, if_num); |
| 186 | return NSS_TX_FAILURE; |
| 187 | } |
| 188 | |
| 189 | nss_ppe_msg_init(&npm, if_num, NSS_PPE_MSG_L2_EXCEPTION, |
| 190 | sizeof(struct nss_ppe_l2_exception_msg), NULL, NULL); |
| 191 | |
| 192 | npm.msg.l2_exception.l2_exception_enable = exception_enable; |
| 193 | |
| 194 | return nss_ppe_tx_msg_sync(nss_ctx, &npm); |
| 195 | } |
| 196 | EXPORT_SYMBOL(nss_ppe_tx_l2_exception_msg); |
| 197 | |
| 198 | /* |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 199 | * nss_ppe_handler() |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 200 | * Handle NSS -> HLOS messages for ppe |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 201 | */ |
| 202 | static void nss_ppe_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 203 | { |
| 204 | struct nss_ppe_msg *msg = (struct nss_ppe_msg *)ncm; |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 205 | void *ctx; |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 206 | |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 207 | nss_ppe_msg_callback_t cb; |
| 208 | |
| 209 | nss_trace("nss_ctx: %p ppe msg: %p\n", nss_ctx, msg); |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 210 | BUG_ON(!nss_ppe_verify_ifnum(ncm->interface)); |
| 211 | |
| 212 | /* |
| 213 | * Is this a valid request/response packet? |
| 214 | */ |
| 215 | if (ncm->type >= NSS_PPE_MSG_MAX) { |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 216 | nss_warning("%p: received invalid message %d for PPE interface\n", nss_ctx, ncm->type); |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ppe_msg)) { |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 221 | nss_warning("%p: Length of message is greater than required: %d\n", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
| 225 | switch (msg->cm.type) { |
| 226 | case NSS_PPE_MSG_SYNC_STATS: |
| 227 | /* |
| 228 | * session debug stats embeded in session stats msg |
| 229 | */ |
| 230 | nss_ppe_stats_sync(nss_ctx, &msg->msg.stats, ncm->interface); |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 231 | return; |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 232 | } |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 233 | |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 234 | /* |
| 235 | * Log failures |
| 236 | */ |
| 237 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 238 | |
| 239 | /* |
| 240 | * Do we have a call back |
| 241 | */ |
| 242 | if (!ncm->cb) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * callback |
| 248 | */ |
| 249 | cb = (nss_ppe_msg_callback_t)ncm->cb; |
| 250 | ctx = (void *)ncm->app_data; |
| 251 | |
| 252 | cb(ctx, msg); |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 253 | } |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 254 | |
| 255 | /* |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 256 | * nss_ppe_register_handler() |
| 257 | * debugfs stats msg handler received on static ppe interface |
| 258 | * |
| 259 | * TODO: Export API so that others can also read PPE stats. |
| 260 | */ |
| 261 | void nss_ppe_register_handler(void) |
| 262 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 263 | struct nss_ctx_instance *nss_ctx = nss_ppe_get_context(); |
| 264 | |
| 265 | nss_core_register_handler(nss_ctx, NSS_PPE_INTERFACE, nss_ppe_handler, NULL); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 266 | |
| 267 | nss_ppe_stats_dentry_create(); |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
| 271 | * nss_ppe_free() |
| 272 | * Uninitialize PPE base |
| 273 | */ |
| 274 | void nss_ppe_free(void) |
| 275 | { |
| 276 | /* |
| 277 | * Check if PPE base is already uninitialized. |
| 278 | */ |
| 279 | if (!ppe_pvt.ppe_base) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Unmap PPE base address |
| 285 | */ |
| 286 | iounmap(ppe_pvt.ppe_base); |
| 287 | ppe_pvt.ppe_base = NULL; |
| 288 | |
| 289 | spin_lock_bh(&nss_ppe_stats_lock); |
| 290 | nss_ppe_debug_stats.valid = false; |
| 291 | nss_ppe_debug_stats.if_num = 0; |
| 292 | nss_ppe_debug_stats.if_index = 0; |
| 293 | spin_unlock_bh(&nss_ppe_stats_lock); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * nss_ppe_init() |
| 298 | * Initialize PPE base |
| 299 | */ |
| 300 | void nss_ppe_init(void) |
| 301 | { |
| 302 | /* |
| 303 | * Check if PPE base is already initialized. |
| 304 | */ |
| 305 | if (ppe_pvt.ppe_base) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Get the PPE base address |
| 311 | */ |
| 312 | ppe_pvt.ppe_base = ioremap_nocache(PPE_BASE_ADDR, PPE_REG_SIZE); |
| 313 | if (!ppe_pvt.ppe_base) { |
| 314 | nss_warning("DRV can't get PPE base address\n"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | spin_lock_bh(&nss_ppe_stats_lock); |
| 319 | nss_ppe_debug_stats.valid = true; |
| 320 | nss_ppe_debug_stats.if_num = 0; |
| 321 | nss_ppe_debug_stats.if_index = 0; |
| 322 | spin_unlock_bh(&nss_ppe_stats_lock); |
Amit Gupta | 1263b08 | 2017-07-06 14:22:57 +0530 | [diff] [blame] | 323 | |
| 324 | sema_init(&ppe_pvt.sem, 1); |
| 325 | init_completion(&ppe_pvt.complete); |
Amit Gupta | 316729b | 2016-08-12 12:21:15 +0530 | [diff] [blame] | 326 | } |