Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved. |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +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_tx_rx_common.h" |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 18 | #include "nss_sjack_stats.h" |
Ankit Dhanuka | 1499999 | 2014-11-12 15:35:11 +0530 | [diff] [blame] | 19 | |
| 20 | /* |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 21 | * nss_sjack_handler() |
| 22 | * Handle NSS -> HLOS messages for sjack |
| 23 | */ |
| 24 | static void nss_sjack_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, |
| 25 | __attribute__((unused))void *app_data) |
| 26 | { |
| 27 | void *ctx; |
| 28 | nss_sjack_msg_callback_t cb; |
Ankit Dhanuka | 1499999 | 2014-11-12 15:35:11 +0530 | [diff] [blame] | 29 | struct nss_sjack_msg *nsm = (struct nss_sjack_msg *)ncm; |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 30 | |
| 31 | BUG_ON(ncm->interface != NSS_SJACK_INTERFACE); |
| 32 | |
| 33 | /* |
| 34 | * Is this a valid request/response packet? |
| 35 | */ |
| 36 | if (ncm->type >= NSS_SJACK_MAX_MSG_TYPE) { |
| 37 | nss_warning("%p: received invalid message %d for sjack interface", nss_ctx, ncm->type); |
| 38 | return; |
| 39 | } |
| 40 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 41 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_sjack_msg)) { |
| 42 | nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Update the callback and app_data for NOTIFY messages, sjack sends all notify messages |
| 48 | * to the same callback/app_data. |
| 49 | */ |
| 50 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 51 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->if_rx_msg_callback[ncm->interface]; |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Log failures |
| 56 | */ |
| 57 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 58 | |
Ankit Dhanuka | 1499999 | 2014-11-12 15:35:11 +0530 | [diff] [blame] | 59 | switch (ncm->type) { |
| 60 | case NSS_SJACK_STATS_SYNC_MSG: |
| 61 | /* |
| 62 | * Update sjack statistics on node sync. |
| 63 | */ |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 64 | nss_sjack_stats_node_sync(nss_ctx, &nsm->msg.stats_sync); |
Ankit Dhanuka | 1499999 | 2014-11-12 15:35:11 +0530 | [diff] [blame] | 65 | break; |
| 66 | } |
| 67 | |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 68 | /* |
| 69 | * Do we have a call back |
| 70 | */ |
| 71 | if (!ncm->cb) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * callback |
| 77 | */ |
| 78 | cb = (nss_sjack_msg_callback_t)ncm->cb; |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 79 | ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev; |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 80 | |
| 81 | cb(ctx, ncm); |
| 82 | } |
| 83 | |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 84 | /* |
| 85 | * nss_sjack_tx_msg() |
| 86 | * Transmit a sjack message to NSSFW |
| 87 | */ |
| 88 | nss_tx_status_t nss_sjack_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_sjack_msg *msg) |
| 89 | { |
| 90 | struct nss_sjack_msg *nm; |
| 91 | struct nss_cmn_msg *ncm = &msg->cm; |
| 92 | struct sk_buff *nbuf; |
| 93 | int32_t status; |
| 94 | |
| 95 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 96 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 97 | nss_warning("%p: sjack msg dropped as core not ready", nss_ctx); |
| 98 | return NSS_TX_FAILURE_NOT_READY; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Sanity check the message |
| 103 | */ |
| 104 | if (ncm->interface != NSS_SJACK_INTERFACE) { |
| 105 | nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface); |
| 106 | return NSS_TX_FAILURE; |
| 107 | } |
| 108 | |
| 109 | if (ncm->type > NSS_SJACK_MAX_MSG_TYPE) { |
| 110 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 111 | return NSS_TX_FAILURE; |
| 112 | } |
| 113 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 114 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_sjack_msg)) { |
| 115 | nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 116 | return NSS_TX_FAILURE; |
| 117 | } |
| 118 | |
| 119 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 120 | if (unlikely(!nbuf)) { |
Sundarajan Srinivasan | 62fee7e | 2015-01-22 11:13:10 -0800 | [diff] [blame] | 121 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 122 | nss_warning("%p: msg dropped as command allocation failed", nss_ctx); |
| 123 | return NSS_TX_FAILURE; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Copy the message to our skb |
| 128 | */ |
| 129 | nm = (struct nss_sjack_msg *)skb_put(nbuf, sizeof(struct nss_sjack_msg)); |
| 130 | memcpy(nm, msg, sizeof(struct nss_sjack_msg)); |
| 131 | |
| 132 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 133 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 134 | dev_kfree_skb_any(nbuf); |
| 135 | nss_warning("%p: Unable to enqueue 'sjack message' \n", nss_ctx); |
| 136 | return NSS_TX_FAILURE; |
| 137 | } |
| 138 | |
Stephen Wang | 90c67de | 2016-04-26 15:15:59 -0700 | [diff] [blame] | 139 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 140 | |
| 141 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 142 | return NSS_TX_SUCCESS; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * nss_sjack_register_if() |
| 147 | */ |
| 148 | struct nss_ctx_instance *nss_sjack_register_if(uint32_t if_num, struct net_device *netdev, |
| 149 | nss_sjack_msg_callback_t event_callback) |
| 150 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 151 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.sjack_handler_id]; |
| 152 | |
| 153 | nss_assert(nss_ctx); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 154 | nss_assert(if_num == NSS_SJACK_INTERFACE); |
| 155 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 156 | nss_core_register_subsys_dp(nss_ctx, if_num, NULL, NULL, NULL, netdev, 0); |
Sundarajan Srinivasan | 7037484 | 2014-11-19 15:22:52 -0800 | [diff] [blame] | 157 | |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 158 | nss_top_main.if_rx_msg_callback[if_num] = event_callback; |
| 159 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 160 | return nss_ctx; |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* |
| 164 | * nss_sjack_unregister_if() |
| 165 | */ |
| 166 | void nss_sjack_unregister_if(uint32_t if_num) |
| 167 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 168 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.sjack_handler_id]; |
| 169 | |
| 170 | nss_assert(nss_ctx); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 171 | nss_assert(if_num == NSS_SJACK_INTERFACE); |
| 172 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 173 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 174 | nss_top_main.if_rx_msg_callback[if_num] = NULL; |
| 175 | |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | /* |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 180 | * nss_sjack_get_context() |
| 181 | * get NSS context instance for sjack |
| 182 | */ |
| 183 | struct nss_ctx_instance *nss_sjack_get_context(void) |
| 184 | { |
| 185 | return &nss_top_main.nss[nss_top_main.sjack_handler_id]; |
| 186 | } |
| 187 | EXPORT_SYMBOL(nss_sjack_get_context); |
| 188 | |
| 189 | /* |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 190 | * nss_sjack_register_handler() |
| 191 | * Registering handler for sending msg to sjack node on NSS. |
| 192 | */ |
| 193 | void nss_sjack_register_handler(void) |
| 194 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 195 | struct nss_ctx_instance *nss_ctx = nss_sjack_get_context(); |
| 196 | |
| 197 | nss_core_register_handler(nss_ctx, NSS_SJACK_INTERFACE, nss_sjack_handler, NULL); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 198 | |
| 199 | nss_sjack_stats_dentry_create(); |
Ankit Dhanuka | 2ae20a8 | 2014-08-25 18:04:00 +0530 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | EXPORT_SYMBOL(nss_sjack_register_if); |
| 203 | EXPORT_SYMBOL(nss_sjack_unregister_if); |
| 204 | EXPORT_SYMBOL(nss_sjack_tx_msg); |