Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Mellanox Technologies inc. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | #include <asm-generic/kmap_types.h> |
| 34 | #include <linux/module.h> |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 35 | #include <linux/errno.h> |
| 36 | #include <linux/pci.h> |
| 37 | #include <linux/dma-mapping.h> |
| 38 | #include <linux/slab.h> |
| 39 | #include <linux/delay.h> |
| 40 | #include <linux/random.h> |
| 41 | #include <linux/io-mapping.h> |
| 42 | #include <linux/mlx5/driver.h> |
| 43 | #include <linux/debugfs.h> |
| 44 | |
| 45 | #include "mlx5_core.h" |
| 46 | |
| 47 | enum { |
Moshe Lazer | 0a324f31 | 2013-08-14 17:46:48 +0300 | [diff] [blame] | 48 | CMD_IF_REV = 5, |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | enum { |
| 52 | CMD_MODE_POLLING, |
| 53 | CMD_MODE_EVENTS |
| 54 | }; |
| 55 | |
| 56 | enum { |
| 57 | NUM_LONG_LISTS = 2, |
| 58 | NUM_MED_LISTS = 64, |
| 59 | LONG_LIST_SIZE = (2ULL * 1024 * 1024 * 1024 / PAGE_SIZE) * 8 + 16 + |
| 60 | MLX5_CMD_DATA_BLOCK_SIZE, |
| 61 | MED_LIST_SIZE = 16 + MLX5_CMD_DATA_BLOCK_SIZE, |
| 62 | }; |
| 63 | |
| 64 | enum { |
| 65 | MLX5_CMD_DELIVERY_STAT_OK = 0x0, |
| 66 | MLX5_CMD_DELIVERY_STAT_SIGNAT_ERR = 0x1, |
| 67 | MLX5_CMD_DELIVERY_STAT_TOK_ERR = 0x2, |
| 68 | MLX5_CMD_DELIVERY_STAT_BAD_BLK_NUM_ERR = 0x3, |
| 69 | MLX5_CMD_DELIVERY_STAT_OUT_PTR_ALIGN_ERR = 0x4, |
| 70 | MLX5_CMD_DELIVERY_STAT_IN_PTR_ALIGN_ERR = 0x5, |
| 71 | MLX5_CMD_DELIVERY_STAT_FW_ERR = 0x6, |
| 72 | MLX5_CMD_DELIVERY_STAT_IN_LENGTH_ERR = 0x7, |
| 73 | MLX5_CMD_DELIVERY_STAT_OUT_LENGTH_ERR = 0x8, |
| 74 | MLX5_CMD_DELIVERY_STAT_RES_FLD_NOT_CLR_ERR = 0x9, |
| 75 | MLX5_CMD_DELIVERY_STAT_CMD_DESCR_ERR = 0x10, |
| 76 | }; |
| 77 | |
| 78 | enum { |
| 79 | MLX5_CMD_STAT_OK = 0x0, |
| 80 | MLX5_CMD_STAT_INT_ERR = 0x1, |
| 81 | MLX5_CMD_STAT_BAD_OP_ERR = 0x2, |
| 82 | MLX5_CMD_STAT_BAD_PARAM_ERR = 0x3, |
| 83 | MLX5_CMD_STAT_BAD_SYS_STATE_ERR = 0x4, |
| 84 | MLX5_CMD_STAT_BAD_RES_ERR = 0x5, |
| 85 | MLX5_CMD_STAT_RES_BUSY = 0x6, |
| 86 | MLX5_CMD_STAT_LIM_ERR = 0x8, |
| 87 | MLX5_CMD_STAT_BAD_RES_STATE_ERR = 0x9, |
| 88 | MLX5_CMD_STAT_IX_ERR = 0xa, |
| 89 | MLX5_CMD_STAT_NO_RES_ERR = 0xf, |
| 90 | MLX5_CMD_STAT_BAD_INP_LEN_ERR = 0x50, |
| 91 | MLX5_CMD_STAT_BAD_OUTP_LEN_ERR = 0x51, |
| 92 | MLX5_CMD_STAT_BAD_QP_STATE_ERR = 0x10, |
| 93 | MLX5_CMD_STAT_BAD_PKT_ERR = 0x30, |
| 94 | MLX5_CMD_STAT_BAD_SIZE_OUTS_CQES_ERR = 0x40, |
| 95 | }; |
| 96 | |
| 97 | static struct mlx5_cmd_work_ent *alloc_cmd(struct mlx5_cmd *cmd, |
| 98 | struct mlx5_cmd_msg *in, |
| 99 | struct mlx5_cmd_msg *out, |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 100 | void *uout, int uout_size, |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 101 | mlx5_cmd_cbk_t cbk, |
| 102 | void *context, int page_queue) |
| 103 | { |
| 104 | gfp_t alloc_flags = cbk ? GFP_ATOMIC : GFP_KERNEL; |
| 105 | struct mlx5_cmd_work_ent *ent; |
| 106 | |
| 107 | ent = kzalloc(sizeof(*ent), alloc_flags); |
| 108 | if (!ent) |
| 109 | return ERR_PTR(-ENOMEM); |
| 110 | |
| 111 | ent->in = in; |
| 112 | ent->out = out; |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 113 | ent->uout = uout; |
| 114 | ent->uout_size = uout_size; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 115 | ent->callback = cbk; |
| 116 | ent->context = context; |
| 117 | ent->cmd = cmd; |
| 118 | ent->page_queue = page_queue; |
| 119 | |
| 120 | return ent; |
| 121 | } |
| 122 | |
| 123 | static u8 alloc_token(struct mlx5_cmd *cmd) |
| 124 | { |
| 125 | u8 token; |
| 126 | |
| 127 | spin_lock(&cmd->token_lock); |
| 128 | token = cmd->token++ % 255 + 1; |
| 129 | spin_unlock(&cmd->token_lock); |
| 130 | |
| 131 | return token; |
| 132 | } |
| 133 | |
| 134 | static int alloc_ent(struct mlx5_cmd *cmd) |
| 135 | { |
| 136 | unsigned long flags; |
| 137 | int ret; |
| 138 | |
| 139 | spin_lock_irqsave(&cmd->alloc_lock, flags); |
| 140 | ret = find_first_bit(&cmd->bitmask, cmd->max_reg_cmds); |
| 141 | if (ret < cmd->max_reg_cmds) |
| 142 | clear_bit(ret, &cmd->bitmask); |
| 143 | spin_unlock_irqrestore(&cmd->alloc_lock, flags); |
| 144 | |
| 145 | return ret < cmd->max_reg_cmds ? ret : -ENOMEM; |
| 146 | } |
| 147 | |
| 148 | static void free_ent(struct mlx5_cmd *cmd, int idx) |
| 149 | { |
| 150 | unsigned long flags; |
| 151 | |
| 152 | spin_lock_irqsave(&cmd->alloc_lock, flags); |
| 153 | set_bit(idx, &cmd->bitmask); |
| 154 | spin_unlock_irqrestore(&cmd->alloc_lock, flags); |
| 155 | } |
| 156 | |
| 157 | static struct mlx5_cmd_layout *get_inst(struct mlx5_cmd *cmd, int idx) |
| 158 | { |
| 159 | return cmd->cmd_buf + (idx << cmd->log_stride); |
| 160 | } |
| 161 | |
| 162 | static u8 xor8_buf(void *buf, int len) |
| 163 | { |
| 164 | u8 *ptr = buf; |
| 165 | u8 sum = 0; |
| 166 | int i; |
| 167 | |
| 168 | for (i = 0; i < len; i++) |
| 169 | sum ^= ptr[i]; |
| 170 | |
| 171 | return sum; |
| 172 | } |
| 173 | |
| 174 | static int verify_block_sig(struct mlx5_cmd_prot_block *block) |
| 175 | { |
| 176 | if (xor8_buf(block->rsvd0, sizeof(*block) - sizeof(block->data) - 1) != 0xff) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | if (xor8_buf(block, sizeof(*block)) != 0xff) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 185 | static void calc_block_sig(struct mlx5_cmd_prot_block *block, u8 token, |
| 186 | int csum) |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 187 | { |
| 188 | block->token = token; |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 189 | if (csum) { |
| 190 | block->ctrl_sig = ~xor8_buf(block->rsvd0, sizeof(*block) - |
| 191 | sizeof(block->data) - 2); |
| 192 | block->sig = ~xor8_buf(block, sizeof(*block) - 1); |
| 193 | } |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 194 | } |
| 195 | |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 196 | static void calc_chain_sig(struct mlx5_cmd_msg *msg, u8 token, int csum) |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 197 | { |
| 198 | struct mlx5_cmd_mailbox *next = msg->next; |
| 199 | |
| 200 | while (next) { |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 201 | calc_block_sig(next->buf, token, csum); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 202 | next = next->next; |
| 203 | } |
| 204 | } |
| 205 | |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 206 | static void set_signature(struct mlx5_cmd_work_ent *ent, int csum) |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 207 | { |
| 208 | ent->lay->sig = ~xor8_buf(ent->lay, sizeof(*ent->lay)); |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 209 | calc_chain_sig(ent->in, ent->token, csum); |
| 210 | calc_chain_sig(ent->out, ent->token, csum); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void poll_timeout(struct mlx5_cmd_work_ent *ent) |
| 214 | { |
| 215 | unsigned long poll_end = jiffies + msecs_to_jiffies(MLX5_CMD_TIMEOUT_MSEC + 1000); |
| 216 | u8 own; |
| 217 | |
| 218 | do { |
| 219 | own = ent->lay->status_own; |
| 220 | if (!(own & CMD_OWNER_HW)) { |
| 221 | ent->ret = 0; |
| 222 | return; |
| 223 | } |
| 224 | usleep_range(5000, 10000); |
| 225 | } while (time_before(jiffies, poll_end)); |
| 226 | |
| 227 | ent->ret = -ETIMEDOUT; |
| 228 | } |
| 229 | |
| 230 | static void free_cmd(struct mlx5_cmd_work_ent *ent) |
| 231 | { |
| 232 | kfree(ent); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | static int verify_signature(struct mlx5_cmd_work_ent *ent) |
| 237 | { |
| 238 | struct mlx5_cmd_mailbox *next = ent->out->next; |
| 239 | int err; |
| 240 | u8 sig; |
| 241 | |
| 242 | sig = xor8_buf(ent->lay, sizeof(*ent->lay)); |
| 243 | if (sig != 0xff) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | while (next) { |
| 247 | err = verify_block_sig(next->buf); |
| 248 | if (err) |
| 249 | return err; |
| 250 | |
| 251 | next = next->next; |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static void dump_buf(void *buf, int size, int data_only, int offset) |
| 258 | { |
| 259 | __be32 *p = buf; |
| 260 | int i; |
| 261 | |
| 262 | for (i = 0; i < size; i += 16) { |
| 263 | pr_debug("%03x: %08x %08x %08x %08x\n", offset, be32_to_cpu(p[0]), |
| 264 | be32_to_cpu(p[1]), be32_to_cpu(p[2]), |
| 265 | be32_to_cpu(p[3])); |
| 266 | p += 4; |
| 267 | offset += 16; |
| 268 | } |
| 269 | if (!data_only) |
| 270 | pr_debug("\n"); |
| 271 | } |
| 272 | |
| 273 | const char *mlx5_command_str(int command) |
| 274 | { |
| 275 | switch (command) { |
| 276 | case MLX5_CMD_OP_QUERY_HCA_CAP: |
| 277 | return "QUERY_HCA_CAP"; |
| 278 | |
| 279 | case MLX5_CMD_OP_SET_HCA_CAP: |
| 280 | return "SET_HCA_CAP"; |
| 281 | |
| 282 | case MLX5_CMD_OP_QUERY_ADAPTER: |
| 283 | return "QUERY_ADAPTER"; |
| 284 | |
| 285 | case MLX5_CMD_OP_INIT_HCA: |
| 286 | return "INIT_HCA"; |
| 287 | |
| 288 | case MLX5_CMD_OP_TEARDOWN_HCA: |
| 289 | return "TEARDOWN_HCA"; |
| 290 | |
Eli Cohen | cd23b14 | 2013-07-18 15:31:08 +0300 | [diff] [blame] | 291 | case MLX5_CMD_OP_ENABLE_HCA: |
| 292 | return "MLX5_CMD_OP_ENABLE_HCA"; |
| 293 | |
| 294 | case MLX5_CMD_OP_DISABLE_HCA: |
| 295 | return "MLX5_CMD_OP_DISABLE_HCA"; |
| 296 | |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 297 | case MLX5_CMD_OP_QUERY_PAGES: |
| 298 | return "QUERY_PAGES"; |
| 299 | |
| 300 | case MLX5_CMD_OP_MANAGE_PAGES: |
| 301 | return "MANAGE_PAGES"; |
| 302 | |
| 303 | case MLX5_CMD_OP_CREATE_MKEY: |
| 304 | return "CREATE_MKEY"; |
| 305 | |
| 306 | case MLX5_CMD_OP_QUERY_MKEY: |
| 307 | return "QUERY_MKEY"; |
| 308 | |
| 309 | case MLX5_CMD_OP_DESTROY_MKEY: |
| 310 | return "DESTROY_MKEY"; |
| 311 | |
| 312 | case MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS: |
| 313 | return "QUERY_SPECIAL_CONTEXTS"; |
| 314 | |
| 315 | case MLX5_CMD_OP_CREATE_EQ: |
| 316 | return "CREATE_EQ"; |
| 317 | |
| 318 | case MLX5_CMD_OP_DESTROY_EQ: |
| 319 | return "DESTROY_EQ"; |
| 320 | |
| 321 | case MLX5_CMD_OP_QUERY_EQ: |
| 322 | return "QUERY_EQ"; |
| 323 | |
| 324 | case MLX5_CMD_OP_CREATE_CQ: |
| 325 | return "CREATE_CQ"; |
| 326 | |
| 327 | case MLX5_CMD_OP_DESTROY_CQ: |
| 328 | return "DESTROY_CQ"; |
| 329 | |
| 330 | case MLX5_CMD_OP_QUERY_CQ: |
| 331 | return "QUERY_CQ"; |
| 332 | |
| 333 | case MLX5_CMD_OP_MODIFY_CQ: |
| 334 | return "MODIFY_CQ"; |
| 335 | |
| 336 | case MLX5_CMD_OP_CREATE_QP: |
| 337 | return "CREATE_QP"; |
| 338 | |
| 339 | case MLX5_CMD_OP_DESTROY_QP: |
| 340 | return "DESTROY_QP"; |
| 341 | |
| 342 | case MLX5_CMD_OP_RST2INIT_QP: |
| 343 | return "RST2INIT_QP"; |
| 344 | |
| 345 | case MLX5_CMD_OP_INIT2RTR_QP: |
| 346 | return "INIT2RTR_QP"; |
| 347 | |
| 348 | case MLX5_CMD_OP_RTR2RTS_QP: |
| 349 | return "RTR2RTS_QP"; |
| 350 | |
| 351 | case MLX5_CMD_OP_RTS2RTS_QP: |
| 352 | return "RTS2RTS_QP"; |
| 353 | |
| 354 | case MLX5_CMD_OP_SQERR2RTS_QP: |
| 355 | return "SQERR2RTS_QP"; |
| 356 | |
| 357 | case MLX5_CMD_OP_2ERR_QP: |
| 358 | return "2ERR_QP"; |
| 359 | |
| 360 | case MLX5_CMD_OP_RTS2SQD_QP: |
| 361 | return "RTS2SQD_QP"; |
| 362 | |
| 363 | case MLX5_CMD_OP_SQD2RTS_QP: |
| 364 | return "SQD2RTS_QP"; |
| 365 | |
| 366 | case MLX5_CMD_OP_2RST_QP: |
| 367 | return "2RST_QP"; |
| 368 | |
| 369 | case MLX5_CMD_OP_QUERY_QP: |
| 370 | return "QUERY_QP"; |
| 371 | |
| 372 | case MLX5_CMD_OP_CONF_SQP: |
| 373 | return "CONF_SQP"; |
| 374 | |
| 375 | case MLX5_CMD_OP_MAD_IFC: |
| 376 | return "MAD_IFC"; |
| 377 | |
| 378 | case MLX5_CMD_OP_INIT2INIT_QP: |
| 379 | return "INIT2INIT_QP"; |
| 380 | |
| 381 | case MLX5_CMD_OP_SUSPEND_QP: |
| 382 | return "SUSPEND_QP"; |
| 383 | |
| 384 | case MLX5_CMD_OP_UNSUSPEND_QP: |
| 385 | return "UNSUSPEND_QP"; |
| 386 | |
| 387 | case MLX5_CMD_OP_SQD2SQD_QP: |
| 388 | return "SQD2SQD_QP"; |
| 389 | |
| 390 | case MLX5_CMD_OP_ALLOC_QP_COUNTER_SET: |
| 391 | return "ALLOC_QP_COUNTER_SET"; |
| 392 | |
| 393 | case MLX5_CMD_OP_DEALLOC_QP_COUNTER_SET: |
| 394 | return "DEALLOC_QP_COUNTER_SET"; |
| 395 | |
| 396 | case MLX5_CMD_OP_QUERY_QP_COUNTER_SET: |
| 397 | return "QUERY_QP_COUNTER_SET"; |
| 398 | |
| 399 | case MLX5_CMD_OP_CREATE_PSV: |
| 400 | return "CREATE_PSV"; |
| 401 | |
| 402 | case MLX5_CMD_OP_DESTROY_PSV: |
| 403 | return "DESTROY_PSV"; |
| 404 | |
| 405 | case MLX5_CMD_OP_QUERY_PSV: |
| 406 | return "QUERY_PSV"; |
| 407 | |
| 408 | case MLX5_CMD_OP_QUERY_SIG_RULE_TABLE: |
| 409 | return "QUERY_SIG_RULE_TABLE"; |
| 410 | |
| 411 | case MLX5_CMD_OP_QUERY_BLOCK_SIZE_TABLE: |
| 412 | return "QUERY_BLOCK_SIZE_TABLE"; |
| 413 | |
| 414 | case MLX5_CMD_OP_CREATE_SRQ: |
| 415 | return "CREATE_SRQ"; |
| 416 | |
| 417 | case MLX5_CMD_OP_DESTROY_SRQ: |
| 418 | return "DESTROY_SRQ"; |
| 419 | |
| 420 | case MLX5_CMD_OP_QUERY_SRQ: |
| 421 | return "QUERY_SRQ"; |
| 422 | |
| 423 | case MLX5_CMD_OP_ARM_RQ: |
| 424 | return "ARM_RQ"; |
| 425 | |
| 426 | case MLX5_CMD_OP_RESIZE_SRQ: |
| 427 | return "RESIZE_SRQ"; |
| 428 | |
| 429 | case MLX5_CMD_OP_ALLOC_PD: |
| 430 | return "ALLOC_PD"; |
| 431 | |
| 432 | case MLX5_CMD_OP_DEALLOC_PD: |
| 433 | return "DEALLOC_PD"; |
| 434 | |
| 435 | case MLX5_CMD_OP_ALLOC_UAR: |
| 436 | return "ALLOC_UAR"; |
| 437 | |
| 438 | case MLX5_CMD_OP_DEALLOC_UAR: |
| 439 | return "DEALLOC_UAR"; |
| 440 | |
| 441 | case MLX5_CMD_OP_ATTACH_TO_MCG: |
| 442 | return "ATTACH_TO_MCG"; |
| 443 | |
| 444 | case MLX5_CMD_OP_DETACH_FROM_MCG: |
| 445 | return "DETACH_FROM_MCG"; |
| 446 | |
| 447 | case MLX5_CMD_OP_ALLOC_XRCD: |
| 448 | return "ALLOC_XRCD"; |
| 449 | |
| 450 | case MLX5_CMD_OP_DEALLOC_XRCD: |
| 451 | return "DEALLOC_XRCD"; |
| 452 | |
| 453 | case MLX5_CMD_OP_ACCESS_REG: |
| 454 | return "MLX5_CMD_OP_ACCESS_REG"; |
| 455 | |
| 456 | default: return "unknown command opcode"; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | static void dump_command(struct mlx5_core_dev *dev, |
| 461 | struct mlx5_cmd_work_ent *ent, int input) |
| 462 | { |
| 463 | u16 op = be16_to_cpu(((struct mlx5_inbox_hdr *)(ent->lay->in))->opcode); |
| 464 | struct mlx5_cmd_msg *msg = input ? ent->in : ent->out; |
| 465 | struct mlx5_cmd_mailbox *next = msg->next; |
| 466 | int data_only; |
Jack Morgenstein | f241e74 | 2014-07-28 23:30:23 +0300 | [diff] [blame] | 467 | u32 offset = 0; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 468 | int dump_len; |
| 469 | |
| 470 | data_only = !!(mlx5_core_debug_mask & (1 << MLX5_CMD_DATA)); |
| 471 | |
| 472 | if (data_only) |
| 473 | mlx5_core_dbg_mask(dev, 1 << MLX5_CMD_DATA, |
| 474 | "dump command data %s(0x%x) %s\n", |
| 475 | mlx5_command_str(op), op, |
| 476 | input ? "INPUT" : "OUTPUT"); |
| 477 | else |
| 478 | mlx5_core_dbg(dev, "dump command %s(0x%x) %s\n", |
| 479 | mlx5_command_str(op), op, |
| 480 | input ? "INPUT" : "OUTPUT"); |
| 481 | |
| 482 | if (data_only) { |
| 483 | if (input) { |
| 484 | dump_buf(ent->lay->in, sizeof(ent->lay->in), 1, offset); |
| 485 | offset += sizeof(ent->lay->in); |
| 486 | } else { |
| 487 | dump_buf(ent->lay->out, sizeof(ent->lay->out), 1, offset); |
| 488 | offset += sizeof(ent->lay->out); |
| 489 | } |
| 490 | } else { |
| 491 | dump_buf(ent->lay, sizeof(*ent->lay), 0, offset); |
| 492 | offset += sizeof(*ent->lay); |
| 493 | } |
| 494 | |
| 495 | while (next && offset < msg->len) { |
| 496 | if (data_only) { |
| 497 | dump_len = min_t(int, MLX5_CMD_DATA_BLOCK_SIZE, msg->len - offset); |
| 498 | dump_buf(next->buf, dump_len, 1, offset); |
| 499 | offset += MLX5_CMD_DATA_BLOCK_SIZE; |
| 500 | } else { |
| 501 | mlx5_core_dbg(dev, "command block:\n"); |
| 502 | dump_buf(next->buf, sizeof(struct mlx5_cmd_prot_block), 0, offset); |
| 503 | offset += sizeof(struct mlx5_cmd_prot_block); |
| 504 | } |
| 505 | next = next->next; |
| 506 | } |
| 507 | |
| 508 | if (data_only) |
| 509 | pr_debug("\n"); |
| 510 | } |
| 511 | |
| 512 | static void cmd_work_handler(struct work_struct *work) |
| 513 | { |
| 514 | struct mlx5_cmd_work_ent *ent = container_of(work, struct mlx5_cmd_work_ent, work); |
| 515 | struct mlx5_cmd *cmd = ent->cmd; |
| 516 | struct mlx5_core_dev *dev = container_of(cmd, struct mlx5_core_dev, cmd); |
| 517 | struct mlx5_cmd_layout *lay; |
| 518 | struct semaphore *sem; |
| 519 | |
| 520 | sem = ent->page_queue ? &cmd->pages_sem : &cmd->sem; |
| 521 | down(sem); |
| 522 | if (!ent->page_queue) { |
| 523 | ent->idx = alloc_ent(cmd); |
| 524 | if (ent->idx < 0) { |
| 525 | mlx5_core_err(dev, "failed to allocate command entry\n"); |
| 526 | up(sem); |
| 527 | return; |
| 528 | } |
| 529 | } else { |
| 530 | ent->idx = cmd->max_reg_cmds; |
| 531 | } |
| 532 | |
| 533 | ent->token = alloc_token(cmd); |
| 534 | cmd->ent_arr[ent->idx] = ent; |
| 535 | lay = get_inst(cmd, ent->idx); |
| 536 | ent->lay = lay; |
| 537 | memset(lay, 0, sizeof(*lay)); |
| 538 | memcpy(lay->in, ent->in->first.data, sizeof(lay->in)); |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 539 | ent->op = be32_to_cpu(lay->in[0]) >> 16; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 540 | if (ent->in->next) |
| 541 | lay->in_ptr = cpu_to_be64(ent->in->next->dma); |
| 542 | lay->inlen = cpu_to_be32(ent->in->len); |
| 543 | if (ent->out->next) |
| 544 | lay->out_ptr = cpu_to_be64(ent->out->next->dma); |
| 545 | lay->outlen = cpu_to_be32(ent->out->len); |
| 546 | lay->type = MLX5_PCI_CMD_XPORT; |
| 547 | lay->token = ent->token; |
| 548 | lay->status_own = CMD_OWNER_HW; |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 549 | set_signature(ent, !cmd->checksum_disabled); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 550 | dump_command(dev, ent, 1); |
Thomas Gleixner | 14a7004 | 2014-07-16 21:04:44 +0000 | [diff] [blame] | 551 | ent->ts1 = ktime_get_ns(); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 552 | |
| 553 | /* ring doorbell after the descriptor is valid */ |
| 554 | wmb(); |
| 555 | iowrite32be(1 << ent->idx, &dev->iseg->cmd_dbell); |
| 556 | mlx5_core_dbg(dev, "write 0x%x to command doorbell\n", 1 << ent->idx); |
| 557 | mmiowb(); |
| 558 | if (cmd->mode == CMD_MODE_POLLING) { |
| 559 | poll_timeout(ent); |
| 560 | /* make sure we read the descriptor after ownership is SW */ |
| 561 | rmb(); |
| 562 | mlx5_cmd_comp_handler(dev, 1UL << ent->idx); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | static const char *deliv_status_to_str(u8 status) |
| 567 | { |
| 568 | switch (status) { |
| 569 | case MLX5_CMD_DELIVERY_STAT_OK: |
| 570 | return "no errors"; |
| 571 | case MLX5_CMD_DELIVERY_STAT_SIGNAT_ERR: |
| 572 | return "signature error"; |
| 573 | case MLX5_CMD_DELIVERY_STAT_TOK_ERR: |
| 574 | return "token error"; |
| 575 | case MLX5_CMD_DELIVERY_STAT_BAD_BLK_NUM_ERR: |
| 576 | return "bad block number"; |
| 577 | case MLX5_CMD_DELIVERY_STAT_OUT_PTR_ALIGN_ERR: |
| 578 | return "output pointer not aligned to block size"; |
| 579 | case MLX5_CMD_DELIVERY_STAT_IN_PTR_ALIGN_ERR: |
| 580 | return "input pointer not aligned to block size"; |
| 581 | case MLX5_CMD_DELIVERY_STAT_FW_ERR: |
| 582 | return "firmware internal error"; |
| 583 | case MLX5_CMD_DELIVERY_STAT_IN_LENGTH_ERR: |
| 584 | return "command input length error"; |
| 585 | case MLX5_CMD_DELIVERY_STAT_OUT_LENGTH_ERR: |
| 586 | return "command ouput length error"; |
| 587 | case MLX5_CMD_DELIVERY_STAT_RES_FLD_NOT_CLR_ERR: |
| 588 | return "reserved fields not cleared"; |
| 589 | case MLX5_CMD_DELIVERY_STAT_CMD_DESCR_ERR: |
| 590 | return "bad command descriptor type"; |
| 591 | default: |
| 592 | return "unknown status code"; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static u16 msg_to_opcode(struct mlx5_cmd_msg *in) |
| 597 | { |
| 598 | struct mlx5_inbox_hdr *hdr = (struct mlx5_inbox_hdr *)(in->first.data); |
| 599 | |
| 600 | return be16_to_cpu(hdr->opcode); |
| 601 | } |
| 602 | |
| 603 | static int wait_func(struct mlx5_core_dev *dev, struct mlx5_cmd_work_ent *ent) |
| 604 | { |
| 605 | unsigned long timeout = msecs_to_jiffies(MLX5_CMD_TIMEOUT_MSEC); |
| 606 | struct mlx5_cmd *cmd = &dev->cmd; |
| 607 | int err; |
| 608 | |
| 609 | if (cmd->mode == CMD_MODE_POLLING) { |
| 610 | wait_for_completion(&ent->done); |
| 611 | err = ent->ret; |
| 612 | } else { |
| 613 | if (!wait_for_completion_timeout(&ent->done, timeout)) |
| 614 | err = -ETIMEDOUT; |
| 615 | else |
| 616 | err = 0; |
| 617 | } |
| 618 | if (err == -ETIMEDOUT) { |
| 619 | mlx5_core_warn(dev, "%s(0x%x) timeout. Will cause a leak of a command resource\n", |
| 620 | mlx5_command_str(msg_to_opcode(ent->in)), |
| 621 | msg_to_opcode(ent->in)); |
| 622 | } |
Joe Perches | 1a91de2 | 2014-05-07 12:52:57 -0700 | [diff] [blame] | 623 | mlx5_core_dbg(dev, "err %d, delivery status %s(%d)\n", |
| 624 | err, deliv_status_to_str(ent->status), ent->status); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 625 | |
| 626 | return err; |
| 627 | } |
| 628 | |
| 629 | /* Notes: |
| 630 | * 1. Callback functions may not sleep |
| 631 | * 2. page queue commands do not support asynchrous completion |
| 632 | */ |
| 633 | static int mlx5_cmd_invoke(struct mlx5_core_dev *dev, struct mlx5_cmd_msg *in, |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 634 | struct mlx5_cmd_msg *out, void *uout, int uout_size, |
| 635 | mlx5_cmd_cbk_t callback, |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 636 | void *context, int page_queue, u8 *status) |
| 637 | { |
| 638 | struct mlx5_cmd *cmd = &dev->cmd; |
| 639 | struct mlx5_cmd_work_ent *ent; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 640 | struct mlx5_cmd_stats *stats; |
| 641 | int err = 0; |
| 642 | s64 ds; |
| 643 | u16 op; |
| 644 | |
| 645 | if (callback && page_queue) |
| 646 | return -EINVAL; |
| 647 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 648 | ent = alloc_cmd(cmd, in, out, uout, uout_size, callback, context, |
| 649 | page_queue); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 650 | if (IS_ERR(ent)) |
| 651 | return PTR_ERR(ent); |
| 652 | |
| 653 | if (!callback) |
| 654 | init_completion(&ent->done); |
| 655 | |
| 656 | INIT_WORK(&ent->work, cmd_work_handler); |
| 657 | if (page_queue) { |
| 658 | cmd_work_handler(&ent->work); |
| 659 | } else if (!queue_work(cmd->wq, &ent->work)) { |
| 660 | mlx5_core_warn(dev, "failed to queue work\n"); |
| 661 | err = -ENOMEM; |
| 662 | goto out_free; |
| 663 | } |
| 664 | |
| 665 | if (!callback) { |
| 666 | err = wait_func(dev, ent); |
| 667 | if (err == -ETIMEDOUT) |
| 668 | goto out; |
| 669 | |
Thomas Gleixner | 14a7004 | 2014-07-16 21:04:44 +0000 | [diff] [blame] | 670 | ds = ent->ts2 - ent->ts1; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 671 | op = be16_to_cpu(((struct mlx5_inbox_hdr *)in->first.data)->opcode); |
| 672 | if (op < ARRAY_SIZE(cmd->stats)) { |
| 673 | stats = &cmd->stats[op]; |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 674 | spin_lock_irq(&stats->lock); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 675 | stats->sum += ds; |
| 676 | ++stats->n; |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 677 | spin_unlock_irq(&stats->lock); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 678 | } |
| 679 | mlx5_core_dbg_mask(dev, 1 << MLX5_CMD_TIME, |
| 680 | "fw exec time for %s is %lld nsec\n", |
| 681 | mlx5_command_str(op), ds); |
| 682 | *status = ent->status; |
| 683 | free_cmd(ent); |
| 684 | } |
| 685 | |
| 686 | return err; |
| 687 | |
| 688 | out_free: |
| 689 | free_cmd(ent); |
| 690 | out: |
| 691 | return err; |
| 692 | } |
| 693 | |
| 694 | static ssize_t dbg_write(struct file *filp, const char __user *buf, |
| 695 | size_t count, loff_t *pos) |
| 696 | { |
| 697 | struct mlx5_core_dev *dev = filp->private_data; |
| 698 | struct mlx5_cmd_debug *dbg = &dev->cmd.dbg; |
| 699 | char lbuf[3]; |
| 700 | int err; |
| 701 | |
| 702 | if (!dbg->in_msg || !dbg->out_msg) |
| 703 | return -ENOMEM; |
| 704 | |
| 705 | if (copy_from_user(lbuf, buf, sizeof(lbuf))) |
Dan Carpenter | 5e631a0 | 2013-07-10 13:58:59 +0300 | [diff] [blame] | 706 | return -EFAULT; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 707 | |
| 708 | lbuf[sizeof(lbuf) - 1] = 0; |
| 709 | |
| 710 | if (strcmp(lbuf, "go")) |
| 711 | return -EINVAL; |
| 712 | |
| 713 | err = mlx5_cmd_exec(dev, dbg->in_msg, dbg->inlen, dbg->out_msg, dbg->outlen); |
| 714 | |
| 715 | return err ? err : count; |
| 716 | } |
| 717 | |
| 718 | |
| 719 | static const struct file_operations fops = { |
| 720 | .owner = THIS_MODULE, |
| 721 | .open = simple_open, |
| 722 | .write = dbg_write, |
| 723 | }; |
| 724 | |
| 725 | static int mlx5_copy_to_msg(struct mlx5_cmd_msg *to, void *from, int size) |
| 726 | { |
| 727 | struct mlx5_cmd_prot_block *block; |
| 728 | struct mlx5_cmd_mailbox *next; |
| 729 | int copy; |
| 730 | |
| 731 | if (!to || !from) |
| 732 | return -ENOMEM; |
| 733 | |
| 734 | copy = min_t(int, size, sizeof(to->first.data)); |
| 735 | memcpy(to->first.data, from, copy); |
| 736 | size -= copy; |
| 737 | from += copy; |
| 738 | |
| 739 | next = to->next; |
| 740 | while (size) { |
| 741 | if (!next) { |
| 742 | /* this is a BUG */ |
| 743 | return -ENOMEM; |
| 744 | } |
| 745 | |
| 746 | copy = min_t(int, size, MLX5_CMD_DATA_BLOCK_SIZE); |
| 747 | block = next->buf; |
| 748 | memcpy(block->data, from, copy); |
| 749 | from += copy; |
| 750 | size -= copy; |
| 751 | next = next->next; |
| 752 | } |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int mlx5_copy_from_msg(void *to, struct mlx5_cmd_msg *from, int size) |
| 758 | { |
| 759 | struct mlx5_cmd_prot_block *block; |
| 760 | struct mlx5_cmd_mailbox *next; |
| 761 | int copy; |
| 762 | |
| 763 | if (!to || !from) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | copy = min_t(int, size, sizeof(from->first.data)); |
| 767 | memcpy(to, from->first.data, copy); |
| 768 | size -= copy; |
| 769 | to += copy; |
| 770 | |
| 771 | next = from->next; |
| 772 | while (size) { |
| 773 | if (!next) { |
| 774 | /* this is a BUG */ |
| 775 | return -ENOMEM; |
| 776 | } |
| 777 | |
| 778 | copy = min_t(int, size, MLX5_CMD_DATA_BLOCK_SIZE); |
| 779 | block = next->buf; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 780 | |
| 781 | memcpy(to, block->data, copy); |
| 782 | to += copy; |
| 783 | size -= copy; |
| 784 | next = next->next; |
| 785 | } |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static struct mlx5_cmd_mailbox *alloc_cmd_box(struct mlx5_core_dev *dev, |
| 791 | gfp_t flags) |
| 792 | { |
| 793 | struct mlx5_cmd_mailbox *mailbox; |
| 794 | |
| 795 | mailbox = kmalloc(sizeof(*mailbox), flags); |
| 796 | if (!mailbox) |
| 797 | return ERR_PTR(-ENOMEM); |
| 798 | |
| 799 | mailbox->buf = pci_pool_alloc(dev->cmd.pool, flags, |
| 800 | &mailbox->dma); |
| 801 | if (!mailbox->buf) { |
| 802 | mlx5_core_dbg(dev, "failed allocation\n"); |
| 803 | kfree(mailbox); |
| 804 | return ERR_PTR(-ENOMEM); |
| 805 | } |
| 806 | memset(mailbox->buf, 0, sizeof(struct mlx5_cmd_prot_block)); |
| 807 | mailbox->next = NULL; |
| 808 | |
| 809 | return mailbox; |
| 810 | } |
| 811 | |
| 812 | static void free_cmd_box(struct mlx5_core_dev *dev, |
| 813 | struct mlx5_cmd_mailbox *mailbox) |
| 814 | { |
| 815 | pci_pool_free(dev->cmd.pool, mailbox->buf, mailbox->dma); |
| 816 | kfree(mailbox); |
| 817 | } |
| 818 | |
| 819 | static struct mlx5_cmd_msg *mlx5_alloc_cmd_msg(struct mlx5_core_dev *dev, |
| 820 | gfp_t flags, int size) |
| 821 | { |
| 822 | struct mlx5_cmd_mailbox *tmp, *head = NULL; |
| 823 | struct mlx5_cmd_prot_block *block; |
| 824 | struct mlx5_cmd_msg *msg; |
| 825 | int blen; |
| 826 | int err; |
| 827 | int n; |
| 828 | int i; |
| 829 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 830 | msg = kzalloc(sizeof(*msg), flags); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 831 | if (!msg) |
| 832 | return ERR_PTR(-ENOMEM); |
| 833 | |
| 834 | blen = size - min_t(int, sizeof(msg->first.data), size); |
| 835 | n = (blen + MLX5_CMD_DATA_BLOCK_SIZE - 1) / MLX5_CMD_DATA_BLOCK_SIZE; |
| 836 | |
| 837 | for (i = 0; i < n; i++) { |
| 838 | tmp = alloc_cmd_box(dev, flags); |
| 839 | if (IS_ERR(tmp)) { |
| 840 | mlx5_core_warn(dev, "failed allocating block\n"); |
| 841 | err = PTR_ERR(tmp); |
| 842 | goto err_alloc; |
| 843 | } |
| 844 | |
| 845 | block = tmp->buf; |
| 846 | tmp->next = head; |
| 847 | block->next = cpu_to_be64(tmp->next ? tmp->next->dma : 0); |
| 848 | block->block_num = cpu_to_be32(n - i - 1); |
| 849 | head = tmp; |
| 850 | } |
| 851 | msg->next = head; |
| 852 | msg->len = size; |
| 853 | return msg; |
| 854 | |
| 855 | err_alloc: |
| 856 | while (head) { |
| 857 | tmp = head->next; |
| 858 | free_cmd_box(dev, head); |
| 859 | head = tmp; |
| 860 | } |
| 861 | kfree(msg); |
| 862 | |
| 863 | return ERR_PTR(err); |
| 864 | } |
| 865 | |
| 866 | static void mlx5_free_cmd_msg(struct mlx5_core_dev *dev, |
| 867 | struct mlx5_cmd_msg *msg) |
| 868 | { |
| 869 | struct mlx5_cmd_mailbox *head = msg->next; |
| 870 | struct mlx5_cmd_mailbox *next; |
| 871 | |
| 872 | while (head) { |
| 873 | next = head->next; |
| 874 | free_cmd_box(dev, head); |
| 875 | head = next; |
| 876 | } |
| 877 | kfree(msg); |
| 878 | } |
| 879 | |
| 880 | static ssize_t data_write(struct file *filp, const char __user *buf, |
| 881 | size_t count, loff_t *pos) |
| 882 | { |
| 883 | struct mlx5_core_dev *dev = filp->private_data; |
| 884 | struct mlx5_cmd_debug *dbg = &dev->cmd.dbg; |
| 885 | void *ptr; |
| 886 | int err; |
| 887 | |
| 888 | if (*pos != 0) |
| 889 | return -EINVAL; |
| 890 | |
| 891 | kfree(dbg->in_msg); |
| 892 | dbg->in_msg = NULL; |
| 893 | dbg->inlen = 0; |
| 894 | |
| 895 | ptr = kzalloc(count, GFP_KERNEL); |
| 896 | if (!ptr) |
| 897 | return -ENOMEM; |
| 898 | |
| 899 | if (copy_from_user(ptr, buf, count)) { |
Dan Carpenter | 5e631a0 | 2013-07-10 13:58:59 +0300 | [diff] [blame] | 900 | err = -EFAULT; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 901 | goto out; |
| 902 | } |
| 903 | dbg->in_msg = ptr; |
| 904 | dbg->inlen = count; |
| 905 | |
| 906 | *pos = count; |
| 907 | |
| 908 | return count; |
| 909 | |
| 910 | out: |
| 911 | kfree(ptr); |
| 912 | return err; |
| 913 | } |
| 914 | |
| 915 | static ssize_t data_read(struct file *filp, char __user *buf, size_t count, |
| 916 | loff_t *pos) |
| 917 | { |
| 918 | struct mlx5_core_dev *dev = filp->private_data; |
| 919 | struct mlx5_cmd_debug *dbg = &dev->cmd.dbg; |
| 920 | int copy; |
| 921 | |
| 922 | if (*pos) |
| 923 | return 0; |
| 924 | |
| 925 | if (!dbg->out_msg) |
| 926 | return -ENOMEM; |
| 927 | |
| 928 | copy = min_t(int, count, dbg->outlen); |
| 929 | if (copy_to_user(buf, dbg->out_msg, copy)) |
Dan Carpenter | 5e631a0 | 2013-07-10 13:58:59 +0300 | [diff] [blame] | 930 | return -EFAULT; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 931 | |
| 932 | *pos += copy; |
| 933 | |
| 934 | return copy; |
| 935 | } |
| 936 | |
| 937 | static const struct file_operations dfops = { |
| 938 | .owner = THIS_MODULE, |
| 939 | .open = simple_open, |
| 940 | .write = data_write, |
| 941 | .read = data_read, |
| 942 | }; |
| 943 | |
| 944 | static ssize_t outlen_read(struct file *filp, char __user *buf, size_t count, |
| 945 | loff_t *pos) |
| 946 | { |
| 947 | struct mlx5_core_dev *dev = filp->private_data; |
| 948 | struct mlx5_cmd_debug *dbg = &dev->cmd.dbg; |
| 949 | char outlen[8]; |
| 950 | int err; |
| 951 | |
| 952 | if (*pos) |
| 953 | return 0; |
| 954 | |
| 955 | err = snprintf(outlen, sizeof(outlen), "%d", dbg->outlen); |
| 956 | if (err < 0) |
| 957 | return err; |
| 958 | |
| 959 | if (copy_to_user(buf, &outlen, err)) |
Dan Carpenter | 5e631a0 | 2013-07-10 13:58:59 +0300 | [diff] [blame] | 960 | return -EFAULT; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 961 | |
| 962 | *pos += err; |
| 963 | |
| 964 | return err; |
| 965 | } |
| 966 | |
| 967 | static ssize_t outlen_write(struct file *filp, const char __user *buf, |
| 968 | size_t count, loff_t *pos) |
| 969 | { |
| 970 | struct mlx5_core_dev *dev = filp->private_data; |
| 971 | struct mlx5_cmd_debug *dbg = &dev->cmd.dbg; |
| 972 | char outlen_str[8]; |
| 973 | int outlen; |
| 974 | void *ptr; |
| 975 | int err; |
| 976 | |
| 977 | if (*pos != 0 || count > 6) |
| 978 | return -EINVAL; |
| 979 | |
| 980 | kfree(dbg->out_msg); |
| 981 | dbg->out_msg = NULL; |
| 982 | dbg->outlen = 0; |
| 983 | |
| 984 | if (copy_from_user(outlen_str, buf, count)) |
Dan Carpenter | 5e631a0 | 2013-07-10 13:58:59 +0300 | [diff] [blame] | 985 | return -EFAULT; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 986 | |
| 987 | outlen_str[7] = 0; |
| 988 | |
| 989 | err = sscanf(outlen_str, "%d", &outlen); |
| 990 | if (err < 0) |
| 991 | return err; |
| 992 | |
| 993 | ptr = kzalloc(outlen, GFP_KERNEL); |
| 994 | if (!ptr) |
| 995 | return -ENOMEM; |
| 996 | |
| 997 | dbg->out_msg = ptr; |
| 998 | dbg->outlen = outlen; |
| 999 | |
| 1000 | *pos = count; |
| 1001 | |
| 1002 | return count; |
| 1003 | } |
| 1004 | |
| 1005 | static const struct file_operations olfops = { |
| 1006 | .owner = THIS_MODULE, |
| 1007 | .open = simple_open, |
| 1008 | .write = outlen_write, |
| 1009 | .read = outlen_read, |
| 1010 | }; |
| 1011 | |
| 1012 | static void set_wqname(struct mlx5_core_dev *dev) |
| 1013 | { |
| 1014 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1015 | |
| 1016 | snprintf(cmd->wq_name, sizeof(cmd->wq_name), "mlx5_cmd_%s", |
| 1017 | dev_name(&dev->pdev->dev)); |
| 1018 | } |
| 1019 | |
| 1020 | static void clean_debug_files(struct mlx5_core_dev *dev) |
| 1021 | { |
| 1022 | struct mlx5_cmd_debug *dbg = &dev->cmd.dbg; |
| 1023 | |
| 1024 | if (!mlx5_debugfs_root) |
| 1025 | return; |
| 1026 | |
| 1027 | mlx5_cmdif_debugfs_cleanup(dev); |
| 1028 | debugfs_remove_recursive(dbg->dbg_root); |
| 1029 | } |
| 1030 | |
| 1031 | static int create_debugfs_files(struct mlx5_core_dev *dev) |
| 1032 | { |
| 1033 | struct mlx5_cmd_debug *dbg = &dev->cmd.dbg; |
| 1034 | int err = -ENOMEM; |
| 1035 | |
| 1036 | if (!mlx5_debugfs_root) |
| 1037 | return 0; |
| 1038 | |
| 1039 | dbg->dbg_root = debugfs_create_dir("cmd", dev->priv.dbg_root); |
| 1040 | if (!dbg->dbg_root) |
| 1041 | return err; |
| 1042 | |
| 1043 | dbg->dbg_in = debugfs_create_file("in", 0400, dbg->dbg_root, |
| 1044 | dev, &dfops); |
| 1045 | if (!dbg->dbg_in) |
| 1046 | goto err_dbg; |
| 1047 | |
| 1048 | dbg->dbg_out = debugfs_create_file("out", 0200, dbg->dbg_root, |
| 1049 | dev, &dfops); |
| 1050 | if (!dbg->dbg_out) |
| 1051 | goto err_dbg; |
| 1052 | |
| 1053 | dbg->dbg_outlen = debugfs_create_file("out_len", 0600, dbg->dbg_root, |
| 1054 | dev, &olfops); |
| 1055 | if (!dbg->dbg_outlen) |
| 1056 | goto err_dbg; |
| 1057 | |
| 1058 | dbg->dbg_status = debugfs_create_u8("status", 0600, dbg->dbg_root, |
| 1059 | &dbg->status); |
| 1060 | if (!dbg->dbg_status) |
| 1061 | goto err_dbg; |
| 1062 | |
| 1063 | dbg->dbg_run = debugfs_create_file("run", 0200, dbg->dbg_root, dev, &fops); |
| 1064 | if (!dbg->dbg_run) |
| 1065 | goto err_dbg; |
| 1066 | |
| 1067 | mlx5_cmdif_debugfs_init(dev); |
| 1068 | |
| 1069 | return 0; |
| 1070 | |
| 1071 | err_dbg: |
| 1072 | clean_debug_files(dev); |
| 1073 | return err; |
| 1074 | } |
| 1075 | |
| 1076 | void mlx5_cmd_use_events(struct mlx5_core_dev *dev) |
| 1077 | { |
| 1078 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1079 | int i; |
| 1080 | |
| 1081 | for (i = 0; i < cmd->max_reg_cmds; i++) |
| 1082 | down(&cmd->sem); |
| 1083 | |
| 1084 | down(&cmd->pages_sem); |
| 1085 | |
| 1086 | flush_workqueue(cmd->wq); |
| 1087 | |
| 1088 | cmd->mode = CMD_MODE_EVENTS; |
| 1089 | |
| 1090 | up(&cmd->pages_sem); |
| 1091 | for (i = 0; i < cmd->max_reg_cmds; i++) |
| 1092 | up(&cmd->sem); |
| 1093 | } |
| 1094 | |
| 1095 | void mlx5_cmd_use_polling(struct mlx5_core_dev *dev) |
| 1096 | { |
| 1097 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1098 | int i; |
| 1099 | |
| 1100 | for (i = 0; i < cmd->max_reg_cmds; i++) |
| 1101 | down(&cmd->sem); |
| 1102 | |
| 1103 | down(&cmd->pages_sem); |
| 1104 | |
| 1105 | flush_workqueue(cmd->wq); |
| 1106 | cmd->mode = CMD_MODE_POLLING; |
| 1107 | |
| 1108 | up(&cmd->pages_sem); |
| 1109 | for (i = 0; i < cmd->max_reg_cmds; i++) |
| 1110 | up(&cmd->sem); |
| 1111 | } |
| 1112 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1113 | static void free_msg(struct mlx5_core_dev *dev, struct mlx5_cmd_msg *msg) |
| 1114 | { |
| 1115 | unsigned long flags; |
| 1116 | |
| 1117 | if (msg->cache) { |
| 1118 | spin_lock_irqsave(&msg->cache->lock, flags); |
| 1119 | list_add_tail(&msg->list, &msg->cache->head); |
| 1120 | spin_unlock_irqrestore(&msg->cache->lock, flags); |
| 1121 | } else { |
| 1122 | mlx5_free_cmd_msg(dev, msg); |
| 1123 | } |
| 1124 | } |
| 1125 | |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1126 | void mlx5_cmd_comp_handler(struct mlx5_core_dev *dev, unsigned long vector) |
| 1127 | { |
| 1128 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1129 | struct mlx5_cmd_work_ent *ent; |
| 1130 | mlx5_cmd_cbk_t callback; |
| 1131 | void *context; |
| 1132 | int err; |
| 1133 | int i; |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1134 | s64 ds; |
| 1135 | struct mlx5_cmd_stats *stats; |
| 1136 | unsigned long flags; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1137 | |
| 1138 | for (i = 0; i < (1 << cmd->log_sz); i++) { |
| 1139 | if (test_bit(i, &vector)) { |
Dan Carpenter | 11940c8 | 2013-07-22 11:02:01 +0300 | [diff] [blame] | 1140 | struct semaphore *sem; |
| 1141 | |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1142 | ent = cmd->ent_arr[i]; |
Dan Carpenter | 11940c8 | 2013-07-22 11:02:01 +0300 | [diff] [blame] | 1143 | if (ent->page_queue) |
| 1144 | sem = &cmd->pages_sem; |
| 1145 | else |
| 1146 | sem = &cmd->sem; |
Thomas Gleixner | 14a7004 | 2014-07-16 21:04:44 +0000 | [diff] [blame] | 1147 | ent->ts2 = ktime_get_ns(); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1148 | memcpy(ent->out->first.data, ent->lay->out, sizeof(ent->lay->out)); |
| 1149 | dump_command(dev, ent, 0); |
| 1150 | if (!ent->ret) { |
| 1151 | if (!cmd->checksum_disabled) |
| 1152 | ent->ret = verify_signature(ent); |
| 1153 | else |
| 1154 | ent->ret = 0; |
| 1155 | ent->status = ent->lay->status_own >> 1; |
| 1156 | mlx5_core_dbg(dev, "command completed. ret 0x%x, delivery status %s(0x%x)\n", |
| 1157 | ent->ret, deliv_status_to_str(ent->status), ent->status); |
| 1158 | } |
| 1159 | free_ent(cmd, ent->idx); |
| 1160 | if (ent->callback) { |
Thomas Gleixner | 14a7004 | 2014-07-16 21:04:44 +0000 | [diff] [blame] | 1161 | ds = ent->ts2 - ent->ts1; |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1162 | if (ent->op < ARRAY_SIZE(cmd->stats)) { |
| 1163 | stats = &cmd->stats[ent->op]; |
| 1164 | spin_lock_irqsave(&stats->lock, flags); |
| 1165 | stats->sum += ds; |
| 1166 | ++stats->n; |
| 1167 | spin_unlock_irqrestore(&stats->lock, flags); |
| 1168 | } |
| 1169 | |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1170 | callback = ent->callback; |
| 1171 | context = ent->context; |
| 1172 | err = ent->ret; |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1173 | if (!err) |
| 1174 | err = mlx5_copy_from_msg(ent->uout, |
| 1175 | ent->out, |
| 1176 | ent->uout_size); |
| 1177 | |
| 1178 | mlx5_free_cmd_msg(dev, ent->out); |
| 1179 | free_msg(dev, ent->in); |
| 1180 | |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1181 | free_cmd(ent); |
| 1182 | callback(err, context); |
| 1183 | } else { |
| 1184 | complete(&ent->done); |
| 1185 | } |
Dan Carpenter | 11940c8 | 2013-07-22 11:02:01 +0300 | [diff] [blame] | 1186 | up(sem); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | EXPORT_SYMBOL(mlx5_cmd_comp_handler); |
| 1191 | |
| 1192 | static int status_to_err(u8 status) |
| 1193 | { |
| 1194 | return status ? -1 : 0; /* TBD more meaningful codes */ |
| 1195 | } |
| 1196 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1197 | static struct mlx5_cmd_msg *alloc_msg(struct mlx5_core_dev *dev, int in_size, |
| 1198 | gfp_t gfp) |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1199 | { |
| 1200 | struct mlx5_cmd_msg *msg = ERR_PTR(-ENOMEM); |
| 1201 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1202 | struct cache_ent *ent = NULL; |
| 1203 | |
| 1204 | if (in_size > MED_LIST_SIZE && in_size <= LONG_LIST_SIZE) |
| 1205 | ent = &cmd->cache.large; |
| 1206 | else if (in_size > 16 && in_size <= MED_LIST_SIZE) |
| 1207 | ent = &cmd->cache.med; |
| 1208 | |
| 1209 | if (ent) { |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1210 | spin_lock_irq(&ent->lock); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1211 | if (!list_empty(&ent->head)) { |
| 1212 | msg = list_entry(ent->head.next, typeof(*msg), list); |
| 1213 | /* For cached lists, we must explicitly state what is |
| 1214 | * the real size |
| 1215 | */ |
| 1216 | msg->len = in_size; |
| 1217 | list_del(&msg->list); |
| 1218 | } |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1219 | spin_unlock_irq(&ent->lock); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | if (IS_ERR(msg)) |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1223 | msg = mlx5_alloc_cmd_msg(dev, gfp, in_size); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1224 | |
| 1225 | return msg; |
| 1226 | } |
| 1227 | |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1228 | static int is_manage_pages(struct mlx5_inbox_hdr *in) |
| 1229 | { |
| 1230 | return be16_to_cpu(in->opcode) == MLX5_CMD_OP_MANAGE_PAGES; |
| 1231 | } |
| 1232 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1233 | static int cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out, |
| 1234 | int out_size, mlx5_cmd_cbk_t callback, void *context) |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1235 | { |
| 1236 | struct mlx5_cmd_msg *inb; |
| 1237 | struct mlx5_cmd_msg *outb; |
| 1238 | int pages_queue; |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1239 | gfp_t gfp; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1240 | int err; |
| 1241 | u8 status = 0; |
| 1242 | |
| 1243 | pages_queue = is_manage_pages(in); |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1244 | gfp = callback ? GFP_ATOMIC : GFP_KERNEL; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1245 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1246 | inb = alloc_msg(dev, in_size, gfp); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1247 | if (IS_ERR(inb)) { |
| 1248 | err = PTR_ERR(inb); |
| 1249 | return err; |
| 1250 | } |
| 1251 | |
| 1252 | err = mlx5_copy_to_msg(inb, in, in_size); |
| 1253 | if (err) { |
| 1254 | mlx5_core_warn(dev, "err %d\n", err); |
| 1255 | goto out_in; |
| 1256 | } |
| 1257 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1258 | outb = mlx5_alloc_cmd_msg(dev, gfp, out_size); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1259 | if (IS_ERR(outb)) { |
| 1260 | err = PTR_ERR(outb); |
| 1261 | goto out_in; |
| 1262 | } |
| 1263 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1264 | err = mlx5_cmd_invoke(dev, inb, outb, out, out_size, callback, context, |
| 1265 | pages_queue, &status); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1266 | if (err) |
| 1267 | goto out_out; |
| 1268 | |
| 1269 | mlx5_core_dbg(dev, "err %d, status %d\n", err, status); |
| 1270 | if (status) { |
| 1271 | err = status_to_err(status); |
| 1272 | goto out_out; |
| 1273 | } |
| 1274 | |
| 1275 | err = mlx5_copy_from_msg(out, outb, out_size); |
| 1276 | |
| 1277 | out_out: |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1278 | if (!callback) |
| 1279 | mlx5_free_cmd_msg(dev, outb); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1280 | |
| 1281 | out_in: |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1282 | if (!callback) |
| 1283 | free_msg(dev, inb); |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1284 | return err; |
| 1285 | } |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1286 | |
| 1287 | int mlx5_cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out, |
| 1288 | int out_size) |
| 1289 | { |
| 1290 | return cmd_exec(dev, in, in_size, out, out_size, NULL, NULL); |
| 1291 | } |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1292 | EXPORT_SYMBOL(mlx5_cmd_exec); |
| 1293 | |
Eli Cohen | 746b558 | 2013-10-23 09:53:14 +0300 | [diff] [blame] | 1294 | int mlx5_cmd_exec_cb(struct mlx5_core_dev *dev, void *in, int in_size, |
| 1295 | void *out, int out_size, mlx5_cmd_cbk_t callback, |
| 1296 | void *context) |
| 1297 | { |
| 1298 | return cmd_exec(dev, in, in_size, out, out_size, callback, context); |
| 1299 | } |
| 1300 | EXPORT_SYMBOL(mlx5_cmd_exec_cb); |
| 1301 | |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1302 | static void destroy_msg_cache(struct mlx5_core_dev *dev) |
| 1303 | { |
| 1304 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1305 | struct mlx5_cmd_msg *msg; |
| 1306 | struct mlx5_cmd_msg *n; |
| 1307 | |
| 1308 | list_for_each_entry_safe(msg, n, &cmd->cache.large.head, list) { |
| 1309 | list_del(&msg->list); |
| 1310 | mlx5_free_cmd_msg(dev, msg); |
| 1311 | } |
| 1312 | |
| 1313 | list_for_each_entry_safe(msg, n, &cmd->cache.med.head, list) { |
| 1314 | list_del(&msg->list); |
| 1315 | mlx5_free_cmd_msg(dev, msg); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | static int create_msg_cache(struct mlx5_core_dev *dev) |
| 1320 | { |
| 1321 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1322 | struct mlx5_cmd_msg *msg; |
| 1323 | int err; |
| 1324 | int i; |
| 1325 | |
| 1326 | spin_lock_init(&cmd->cache.large.lock); |
| 1327 | INIT_LIST_HEAD(&cmd->cache.large.head); |
| 1328 | spin_lock_init(&cmd->cache.med.lock); |
| 1329 | INIT_LIST_HEAD(&cmd->cache.med.head); |
| 1330 | |
| 1331 | for (i = 0; i < NUM_LONG_LISTS; i++) { |
| 1332 | msg = mlx5_alloc_cmd_msg(dev, GFP_KERNEL, LONG_LIST_SIZE); |
| 1333 | if (IS_ERR(msg)) { |
| 1334 | err = PTR_ERR(msg); |
| 1335 | goto ex_err; |
| 1336 | } |
| 1337 | msg->cache = &cmd->cache.large; |
| 1338 | list_add_tail(&msg->list, &cmd->cache.large.head); |
| 1339 | } |
| 1340 | |
| 1341 | for (i = 0; i < NUM_MED_LISTS; i++) { |
| 1342 | msg = mlx5_alloc_cmd_msg(dev, GFP_KERNEL, MED_LIST_SIZE); |
| 1343 | if (IS_ERR(msg)) { |
| 1344 | err = PTR_ERR(msg); |
| 1345 | goto ex_err; |
| 1346 | } |
| 1347 | msg->cache = &cmd->cache.med; |
| 1348 | list_add_tail(&msg->list, &cmd->cache.med.head); |
| 1349 | } |
| 1350 | |
| 1351 | return 0; |
| 1352 | |
| 1353 | ex_err: |
| 1354 | destroy_msg_cache(dev); |
| 1355 | return err; |
| 1356 | } |
| 1357 | |
| 1358 | int mlx5_cmd_init(struct mlx5_core_dev *dev) |
| 1359 | { |
| 1360 | int size = sizeof(struct mlx5_cmd_prot_block); |
| 1361 | int align = roundup_pow_of_two(size); |
| 1362 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1363 | u32 cmd_h, cmd_l; |
| 1364 | u16 cmd_if_rev; |
| 1365 | int err; |
| 1366 | int i; |
| 1367 | |
| 1368 | cmd_if_rev = cmdif_rev(dev); |
| 1369 | if (cmd_if_rev != CMD_IF_REV) { |
| 1370 | dev_err(&dev->pdev->dev, |
| 1371 | "Driver cmdif rev(%d) differs from firmware's(%d)\n", |
| 1372 | CMD_IF_REV, cmd_if_rev); |
| 1373 | return -EINVAL; |
| 1374 | } |
| 1375 | |
| 1376 | cmd->pool = pci_pool_create("mlx5_cmd", dev->pdev, size, align, 0); |
| 1377 | if (!cmd->pool) |
| 1378 | return -ENOMEM; |
| 1379 | |
| 1380 | cmd->cmd_buf = (void *)__get_free_pages(GFP_ATOMIC, 0); |
| 1381 | if (!cmd->cmd_buf) { |
| 1382 | err = -ENOMEM; |
| 1383 | goto err_free_pool; |
| 1384 | } |
| 1385 | cmd->dma = dma_map_single(&dev->pdev->dev, cmd->cmd_buf, PAGE_SIZE, |
| 1386 | DMA_BIDIRECTIONAL); |
| 1387 | if (dma_mapping_error(&dev->pdev->dev, cmd->dma)) { |
| 1388 | err = -ENOMEM; |
| 1389 | goto err_free; |
| 1390 | } |
| 1391 | |
| 1392 | cmd_l = ioread32be(&dev->iseg->cmdq_addr_l_sz) & 0xff; |
| 1393 | cmd->log_sz = cmd_l >> 4 & 0xf; |
| 1394 | cmd->log_stride = cmd_l & 0xf; |
| 1395 | if (1 << cmd->log_sz > MLX5_MAX_COMMANDS) { |
| 1396 | dev_err(&dev->pdev->dev, "firmware reports too many outstanding commands %d\n", |
| 1397 | 1 << cmd->log_sz); |
| 1398 | err = -EINVAL; |
| 1399 | goto err_map; |
| 1400 | } |
| 1401 | |
| 1402 | if (cmd->log_sz + cmd->log_stride > PAGE_SHIFT) { |
| 1403 | dev_err(&dev->pdev->dev, "command queue size overflow\n"); |
| 1404 | err = -EINVAL; |
| 1405 | goto err_map; |
| 1406 | } |
| 1407 | |
Eli Cohen | c1868b8 | 2013-09-11 16:35:25 +0300 | [diff] [blame] | 1408 | cmd->checksum_disabled = 1; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1409 | cmd->max_reg_cmds = (1 << cmd->log_sz) - 1; |
| 1410 | cmd->bitmask = (1 << cmd->max_reg_cmds) - 1; |
| 1411 | |
| 1412 | cmd->cmdif_rev = ioread32be(&dev->iseg->cmdif_rev_fw_sub) >> 16; |
| 1413 | if (cmd->cmdif_rev > CMD_IF_REV) { |
| 1414 | dev_err(&dev->pdev->dev, "driver does not support command interface version. driver %d, firmware %d\n", |
| 1415 | CMD_IF_REV, cmd->cmdif_rev); |
| 1416 | err = -ENOTSUPP; |
| 1417 | goto err_map; |
| 1418 | } |
| 1419 | |
| 1420 | spin_lock_init(&cmd->alloc_lock); |
| 1421 | spin_lock_init(&cmd->token_lock); |
| 1422 | for (i = 0; i < ARRAY_SIZE(cmd->stats); i++) |
| 1423 | spin_lock_init(&cmd->stats[i].lock); |
| 1424 | |
| 1425 | sema_init(&cmd->sem, cmd->max_reg_cmds); |
| 1426 | sema_init(&cmd->pages_sem, 1); |
| 1427 | |
| 1428 | cmd_h = (u32)((u64)(cmd->dma) >> 32); |
| 1429 | cmd_l = (u32)(cmd->dma); |
| 1430 | if (cmd_l & 0xfff) { |
| 1431 | dev_err(&dev->pdev->dev, "invalid command queue address\n"); |
| 1432 | err = -ENOMEM; |
| 1433 | goto err_map; |
| 1434 | } |
| 1435 | |
| 1436 | iowrite32be(cmd_h, &dev->iseg->cmdq_addr_h); |
| 1437 | iowrite32be(cmd_l, &dev->iseg->cmdq_addr_l_sz); |
| 1438 | |
| 1439 | /* Make sure firmware sees the complete address before we proceed */ |
| 1440 | wmb(); |
| 1441 | |
| 1442 | mlx5_core_dbg(dev, "descriptor at dma 0x%llx\n", (unsigned long long)(cmd->dma)); |
| 1443 | |
| 1444 | cmd->mode = CMD_MODE_POLLING; |
| 1445 | |
| 1446 | err = create_msg_cache(dev); |
| 1447 | if (err) { |
| 1448 | dev_err(&dev->pdev->dev, "failed to create command cache\n"); |
| 1449 | goto err_map; |
| 1450 | } |
| 1451 | |
| 1452 | set_wqname(dev); |
| 1453 | cmd->wq = create_singlethread_workqueue(cmd->wq_name); |
| 1454 | if (!cmd->wq) { |
| 1455 | dev_err(&dev->pdev->dev, "failed to create command workqueue\n"); |
| 1456 | err = -ENOMEM; |
| 1457 | goto err_cache; |
| 1458 | } |
| 1459 | |
| 1460 | err = create_debugfs_files(dev); |
| 1461 | if (err) { |
| 1462 | err = -ENOMEM; |
| 1463 | goto err_wq; |
| 1464 | } |
| 1465 | |
| 1466 | return 0; |
| 1467 | |
| 1468 | err_wq: |
| 1469 | destroy_workqueue(cmd->wq); |
| 1470 | |
| 1471 | err_cache: |
| 1472 | destroy_msg_cache(dev); |
| 1473 | |
| 1474 | err_map: |
| 1475 | dma_unmap_single(&dev->pdev->dev, cmd->dma, PAGE_SIZE, |
| 1476 | DMA_BIDIRECTIONAL); |
| 1477 | err_free: |
| 1478 | free_pages((unsigned long)cmd->cmd_buf, 0); |
| 1479 | |
| 1480 | err_free_pool: |
| 1481 | pci_pool_destroy(cmd->pool); |
| 1482 | |
| 1483 | return err; |
| 1484 | } |
| 1485 | EXPORT_SYMBOL(mlx5_cmd_init); |
| 1486 | |
| 1487 | void mlx5_cmd_cleanup(struct mlx5_core_dev *dev) |
| 1488 | { |
| 1489 | struct mlx5_cmd *cmd = &dev->cmd; |
| 1490 | |
| 1491 | clean_debug_files(dev); |
| 1492 | destroy_workqueue(cmd->wq); |
| 1493 | destroy_msg_cache(dev); |
| 1494 | dma_unmap_single(&dev->pdev->dev, cmd->dma, PAGE_SIZE, |
| 1495 | DMA_BIDIRECTIONAL); |
| 1496 | free_pages((unsigned long)cmd->cmd_buf, 0); |
| 1497 | pci_pool_destroy(cmd->pool); |
| 1498 | } |
| 1499 | EXPORT_SYMBOL(mlx5_cmd_cleanup); |
| 1500 | |
| 1501 | static const char *cmd_status_str(u8 status) |
| 1502 | { |
| 1503 | switch (status) { |
| 1504 | case MLX5_CMD_STAT_OK: |
| 1505 | return "OK"; |
| 1506 | case MLX5_CMD_STAT_INT_ERR: |
| 1507 | return "internal error"; |
| 1508 | case MLX5_CMD_STAT_BAD_OP_ERR: |
| 1509 | return "bad operation"; |
| 1510 | case MLX5_CMD_STAT_BAD_PARAM_ERR: |
| 1511 | return "bad parameter"; |
| 1512 | case MLX5_CMD_STAT_BAD_SYS_STATE_ERR: |
| 1513 | return "bad system state"; |
| 1514 | case MLX5_CMD_STAT_BAD_RES_ERR: |
| 1515 | return "bad resource"; |
| 1516 | case MLX5_CMD_STAT_RES_BUSY: |
| 1517 | return "resource busy"; |
| 1518 | case MLX5_CMD_STAT_LIM_ERR: |
| 1519 | return "limits exceeded"; |
| 1520 | case MLX5_CMD_STAT_BAD_RES_STATE_ERR: |
| 1521 | return "bad resource state"; |
| 1522 | case MLX5_CMD_STAT_IX_ERR: |
| 1523 | return "bad index"; |
| 1524 | case MLX5_CMD_STAT_NO_RES_ERR: |
| 1525 | return "no resources"; |
| 1526 | case MLX5_CMD_STAT_BAD_INP_LEN_ERR: |
| 1527 | return "bad input length"; |
| 1528 | case MLX5_CMD_STAT_BAD_OUTP_LEN_ERR: |
| 1529 | return "bad output length"; |
| 1530 | case MLX5_CMD_STAT_BAD_QP_STATE_ERR: |
| 1531 | return "bad QP state"; |
| 1532 | case MLX5_CMD_STAT_BAD_PKT_ERR: |
| 1533 | return "bad packet (discarded)"; |
| 1534 | case MLX5_CMD_STAT_BAD_SIZE_OUTS_CQES_ERR: |
| 1535 | return "bad size too many outstanding CQEs"; |
| 1536 | default: |
| 1537 | return "unknown status"; |
| 1538 | } |
| 1539 | } |
| 1540 | |
Eli Cohen | c7a08ac | 2014-10-02 12:19:42 +0300 | [diff] [blame^] | 1541 | static int cmd_status_to_err(u8 status) |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1542 | { |
Eli Cohen | c7a08ac | 2014-10-02 12:19:42 +0300 | [diff] [blame^] | 1543 | switch (status) { |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1544 | case MLX5_CMD_STAT_OK: return 0; |
| 1545 | case MLX5_CMD_STAT_INT_ERR: return -EIO; |
| 1546 | case MLX5_CMD_STAT_BAD_OP_ERR: return -EINVAL; |
| 1547 | case MLX5_CMD_STAT_BAD_PARAM_ERR: return -EINVAL; |
| 1548 | case MLX5_CMD_STAT_BAD_SYS_STATE_ERR: return -EIO; |
| 1549 | case MLX5_CMD_STAT_BAD_RES_ERR: return -EINVAL; |
| 1550 | case MLX5_CMD_STAT_RES_BUSY: return -EBUSY; |
Eli Cohen | 9c86513 | 2013-09-11 16:35:33 +0300 | [diff] [blame] | 1551 | case MLX5_CMD_STAT_LIM_ERR: return -ENOMEM; |
Eli Cohen | e126ba9 | 2013-07-07 17:25:49 +0300 | [diff] [blame] | 1552 | case MLX5_CMD_STAT_BAD_RES_STATE_ERR: return -EINVAL; |
| 1553 | case MLX5_CMD_STAT_IX_ERR: return -EINVAL; |
| 1554 | case MLX5_CMD_STAT_NO_RES_ERR: return -EAGAIN; |
| 1555 | case MLX5_CMD_STAT_BAD_INP_LEN_ERR: return -EIO; |
| 1556 | case MLX5_CMD_STAT_BAD_OUTP_LEN_ERR: return -EIO; |
| 1557 | case MLX5_CMD_STAT_BAD_QP_STATE_ERR: return -EINVAL; |
| 1558 | case MLX5_CMD_STAT_BAD_PKT_ERR: return -EINVAL; |
| 1559 | case MLX5_CMD_STAT_BAD_SIZE_OUTS_CQES_ERR: return -EINVAL; |
| 1560 | default: return -EIO; |
| 1561 | } |
| 1562 | } |
Eli Cohen | c7a08ac | 2014-10-02 12:19:42 +0300 | [diff] [blame^] | 1563 | |
| 1564 | /* this will be available till all the commands use set/get macros */ |
| 1565 | int mlx5_cmd_status_to_err(struct mlx5_outbox_hdr *hdr) |
| 1566 | { |
| 1567 | if (!hdr->status) |
| 1568 | return 0; |
| 1569 | |
| 1570 | pr_warn("command failed, status %s(0x%x), syndrome 0x%x\n", |
| 1571 | cmd_status_str(hdr->status), hdr->status, |
| 1572 | be32_to_cpu(hdr->syndrome)); |
| 1573 | |
| 1574 | return cmd_status_to_err(hdr->status); |
| 1575 | } |