1 /* 2 * Copyright (c) 2017-2018 Cavium, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 /* 28 * File : ecore_spq.c 29 */ 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 34 #include "bcm_osal.h" 35 #include "reg_addr.h" 36 #include "ecore_gtt_reg_addr.h" 37 #include "ecore_hsi_common.h" 38 #include "ecore.h" 39 #include "ecore_sp_api.h" 40 #include "ecore_spq.h" 41 #include "ecore_iro.h" 42 #include "ecore_init_fw_funcs.h" 43 #include "ecore_cxt.h" 44 #include "ecore_int.h" 45 #include "ecore_dev_api.h" 46 #include "ecore_mcp.h" 47 #ifdef CONFIG_ECORE_ROCE 48 #include "ecore_roce.h" 49 #endif 50 #include "ecore_hw.h" 51 #include "ecore_sriov.h" 52 #ifdef CONFIG_ECORE_ISCSI 53 #include "ecore_iscsi.h" 54 #include "ecore_ooo.h" 55 #endif 56 57 /*************************************************************************** 58 * Structures & Definitions 59 ***************************************************************************/ 60 61 #define SPQ_HIGH_PRI_RESERVE_DEFAULT (1) 62 63 #define SPQ_BLOCK_DELAY_MAX_ITER (10) 64 #define SPQ_BLOCK_DELAY_US (10) 65 #define SPQ_BLOCK_SLEEP_MAX_ITER (1000) 66 #define SPQ_BLOCK_SLEEP_MS (5) 67 68 #ifndef REMOVE_DBG 69 /*************************************************************************** 70 * Debug [iSCSI] tool 71 ***************************************************************************/ 72 static void ecore_iscsi_eq_dump(struct ecore_hwfn *p_hwfn, 73 struct event_ring_entry *p_eqe) 74 { 75 if (p_eqe->opcode >= MAX_ISCSI_EQE_OPCODE) { 76 DP_NOTICE(p_hwfn, false, "Unknown iSCSI EQ: %x\n", 77 p_eqe->opcode); 78 } 79 80 switch (p_eqe->opcode) { 81 case ISCSI_EVENT_TYPE_INIT_FUNC: 82 case ISCSI_EVENT_TYPE_DESTROY_FUNC: 83 /* NOPE */ 84 break; 85 case ISCSI_EVENT_TYPE_OFFLOAD_CONN: 86 case ISCSI_EVENT_TYPE_TERMINATE_CONN: 87 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE, 88 "iSCSI EQE: Port %x, Op %x, echo %x, FWret %x, CID %x, ConnID %x, ERR %x\n", 89 p_hwfn->port_id, p_eqe->opcode, 90 OSAL_LE16_TO_CPU(p_eqe->echo), 91 p_eqe->fw_return_code, 92 OSAL_LE32_TO_CPU(p_eqe->data.iscsi_info.cid), 93 OSAL_LE16_TO_CPU(p_eqe->data.iscsi_info.conn_id), 94 p_eqe->data.iscsi_info.error_code); 95 break; 96 case ISCSI_EVENT_TYPE_UPDATE_CONN: 97 case ISCSI_EVENT_TYPE_CLEAR_SQ: 98 case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE: 99 case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE: 100 case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD: 101 case ISCSI_EVENT_TYPE_ASYN_CLOSE_RCVD: 102 case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD: 103 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME: 104 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT: 105 case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT: 106 case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2: 107 case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR: 108 case ISCSI_EVENT_TYPE_TCP_CONN_ERROR: 109 default: 110 /* NOPE */ 111 break; 112 } 113 } 114 #endif 115 116 /*************************************************************************** 117 * Blocking Imp. (BLOCK/EBLOCK mode) 118 ***************************************************************************/ 119 static void ecore_spq_blocking_cb(struct ecore_hwfn *p_hwfn, void *cookie, 120 union event_ring_data OSAL_UNUSED *data, 121 u8 fw_return_code) 122 { 123 struct ecore_spq_comp_done *comp_done; 124 125 comp_done = (struct ecore_spq_comp_done *)cookie; 126 127 comp_done->done = 0x1; 128 comp_done->fw_return_code = fw_return_code; 129 130 /* make update visible to waiting thread */ 131 OSAL_SMP_WMB(p_hwfn->p_dev); 132 } 133 134 static enum _ecore_status_t __ecore_spq_block(struct ecore_hwfn *p_hwfn, 135 struct ecore_spq_entry *p_ent, 136 u8 *p_fw_ret, 137 bool sleep_between_iter) 138 { 139 struct ecore_spq_comp_done *comp_done; 140 u32 iter_cnt; 141 142 comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie; 143 iter_cnt = sleep_between_iter ? SPQ_BLOCK_SLEEP_MAX_ITER 144 : SPQ_BLOCK_DELAY_MAX_ITER; 145 146 while (iter_cnt--) { 147 OSAL_POLL_MODE_DPC(p_hwfn); 148 OSAL_SMP_RMB(p_hwfn->p_dev); 149 if (comp_done->done == 1) { 150 if (p_fw_ret) 151 *p_fw_ret = comp_done->fw_return_code; 152 return ECORE_SUCCESS; 153 } 154 155 if (sleep_between_iter) { 156 OSAL_MSLEEP(SPQ_BLOCK_SLEEP_MS); 157 } else { 158 OSAL_UDELAY(SPQ_BLOCK_DELAY_US); 159 } 160 } 161 162 return ECORE_TIMEOUT; 163 } 164 165 static enum _ecore_status_t ecore_spq_block(struct ecore_hwfn *p_hwfn, 166 struct ecore_spq_entry *p_ent, 167 u8 *p_fw_ret, bool skip_quick_poll) 168 { 169 struct ecore_spq_comp_done *comp_done; 170 struct ecore_ptt *p_ptt; 171 enum _ecore_status_t rc; 172 173 /* A relatively short polling period w/o sleeping, to allow the FW to 174 * complete the ramrod and thus possibly to avoid the following sleeps. 175 */ 176 if (!skip_quick_poll) { 177 rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, false); 178 if (rc == ECORE_SUCCESS) 179 return ECORE_SUCCESS; 180 } 181 182 /* Move to polling with a sleeping period between iterations */ 183 rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true); 184 if (rc == ECORE_SUCCESS) 185 return ECORE_SUCCESS; 186 187 p_ptt = ecore_ptt_acquire(p_hwfn); 188 if (!p_ptt) { 189 DP_NOTICE(p_hwfn, true, "ptt, failed to acquire\n"); 190 return ECORE_AGAIN; 191 } 192 193 DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n"); 194 rc = ecore_mcp_drain(p_hwfn, p_ptt); 195 if (rc != ECORE_SUCCESS) { 196 DP_NOTICE(p_hwfn, true, "MCP drain failed\n"); 197 goto err; 198 } 199 200 /* Retry after drain */ 201 rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true); 202 if (rc == ECORE_SUCCESS) 203 goto out; 204 205 comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie; 206 if (comp_done->done == 1) { 207 if (p_fw_ret) 208 *p_fw_ret = comp_done->fw_return_code; 209 } 210 out: 211 ecore_ptt_release(p_hwfn, p_ptt); 212 213 return ECORE_SUCCESS; 214 215 err: 216 ecore_ptt_release(p_hwfn, p_ptt); 217 DP_NOTICE(p_hwfn, true, 218 "Ramrod is stuck [CID %08x cmd %02x protocol %02x echo %04x]\n", 219 OSAL_LE32_TO_CPU(p_ent->elem.hdr.cid), 220 p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id, 221 OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo)); 222 223 ecore_hw_err_notify(p_hwfn, ECORE_HW_ERR_RAMROD_FAIL); 224 225 return ECORE_BUSY; 226 } 227 228 /*************************************************************************** 229 * SPQ entries inner API 230 ***************************************************************************/ 231 static enum _ecore_status_t ecore_spq_fill_entry(struct ecore_hwfn *p_hwfn, 232 struct ecore_spq_entry *p_ent) 233 { 234 p_ent->flags = 0; 235 236 switch (p_ent->comp_mode) { 237 case ECORE_SPQ_MODE_EBLOCK: 238 case ECORE_SPQ_MODE_BLOCK: 239 p_ent->comp_cb.function = ecore_spq_blocking_cb; 240 break; 241 case ECORE_SPQ_MODE_CB: 242 break; 243 default: 244 DP_NOTICE(p_hwfn, true, "Unknown SPQE completion mode %d\n", 245 p_ent->comp_mode); 246 return ECORE_INVAL; 247 } 248 249 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, 250 "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x] Data pointer: [%08x:%08x] Completion Mode: %s\n", 251 p_ent->elem.hdr.cid, p_ent->elem.hdr.cmd_id, 252 p_ent->elem.hdr.protocol_id, 253 p_ent->elem.data_ptr.hi, p_ent->elem.data_ptr.lo, 254 D_TRINE(p_ent->comp_mode, ECORE_SPQ_MODE_EBLOCK, 255 ECORE_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK", 256 "MODE_CB")); 257 258 return ECORE_SUCCESS; 259 } 260 261 /*************************************************************************** 262 * HSI access 263 ***************************************************************************/ 264 static void ecore_spq_hw_initialize(struct ecore_hwfn *p_hwfn, 265 struct ecore_spq *p_spq) 266 { 267 struct e4_core_conn_context *p_cxt; 268 struct ecore_cxt_info cxt_info; 269 u16 physical_q; 270 enum _ecore_status_t rc; 271 272 cxt_info.iid = p_spq->cid; 273 274 rc = ecore_cxt_get_cid_info(p_hwfn, &cxt_info); 275 276 if (rc < 0) { 277 DP_NOTICE(p_hwfn, true, "Cannot find context info for cid=%d\n", 278 p_spq->cid); 279 return; 280 } 281 282 p_cxt = cxt_info.p_cxt; 283 284 /* @@@TBD we zero the context until we have ilt_reset implemented. */ 285 OSAL_MEM_ZERO(p_cxt, sizeof(*p_cxt)); 286 287 if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev)) { 288 SET_FIELD(p_cxt->xstorm_ag_context.flags10, 289 E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1); 290 SET_FIELD(p_cxt->xstorm_ag_context.flags1, 291 E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1); 292 /*SET_FIELD(p_cxt->xstorm_ag_context.flags10, 293 E4_XSTORM_CORE_CONN_AG_CTX_SLOW_PATH_EN, 1);*/ 294 SET_FIELD(p_cxt->xstorm_ag_context.flags9, 295 E4_XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1); 296 } else { /* E5 */ 297 ECORE_E5_MISSING_CODE; 298 } 299 300 /* CDU validation - FIXME currently disabled */ 301 302 /* QM physical queue */ 303 physical_q = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB); 304 p_cxt->xstorm_ag_context.physical_q0 = OSAL_CPU_TO_LE16(physical_q); 305 306 p_cxt->xstorm_st_context.spq_base_lo = 307 DMA_LO_LE(p_spq->chain.p_phys_addr); 308 p_cxt->xstorm_st_context.spq_base_hi = 309 DMA_HI_LE(p_spq->chain.p_phys_addr); 310 311 DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr, 312 p_hwfn->p_consq->chain.p_phys_addr); 313 } 314 315 static enum _ecore_status_t ecore_spq_hw_post(struct ecore_hwfn *p_hwfn, 316 struct ecore_spq *p_spq, 317 struct ecore_spq_entry *p_ent) 318 { 319 struct ecore_chain *p_chain = &p_hwfn->p_spq->chain; 320 u16 echo = ecore_chain_get_prod_idx(p_chain); 321 struct slow_path_element *elem; 322 struct core_db_data db; 323 324 p_ent->elem.hdr.echo = OSAL_CPU_TO_LE16(echo); 325 elem = ecore_chain_produce(p_chain); 326 if (!elem) { 327 DP_NOTICE(p_hwfn, true, "Failed to produce from SPQ chain\n"); 328 return ECORE_INVAL; 329 } 330 331 *elem = p_ent->elem; /* struct assignment */ 332 333 /* send a doorbell on the slow hwfn session */ 334 OSAL_MEMSET(&db, 0, sizeof(db)); 335 SET_FIELD(db.params, CORE_DB_DATA_DEST, DB_DEST_XCM); 336 SET_FIELD(db.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET); 337 SET_FIELD(db.params, CORE_DB_DATA_AGG_VAL_SEL, DQ_XCM_CORE_SPQ_PROD_CMD); 338 db.agg_flags = DQ_XCM_CORE_DQ_CF_CMD; 339 db.spq_prod = OSAL_CPU_TO_LE16(ecore_chain_get_prod_idx(p_chain)); 340 341 /* make sure the SPQE is updated before the doorbell */ 342 OSAL_WMB(p_hwfn->p_dev); 343 344 DOORBELL(p_hwfn, DB_ADDR(p_spq->cid, DQ_DEMS_LEGACY), *(u32 *)&db); 345 346 /* make sure doorbell is rang */ 347 OSAL_WMB(p_hwfn->p_dev); 348 349 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, 350 "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n", 351 DB_ADDR(p_spq->cid, DQ_DEMS_LEGACY), p_spq->cid, db.params, 352 db.agg_flags, ecore_chain_get_prod_idx(p_chain)); 353 354 return ECORE_SUCCESS; 355 } 356 357 /*************************************************************************** 358 * Asynchronous events 359 ***************************************************************************/ 360 361 static enum _ecore_status_t 362 ecore_async_event_completion(struct ecore_hwfn *p_hwfn, 363 struct event_ring_entry *p_eqe) 364 { 365 switch (p_eqe->protocol_id) { 366 #ifdef CONFIG_ECORE_ROCE 367 case PROTOCOLID_ROCE: 368 { 369 ecore_roce_async_event(p_hwfn, 370 p_eqe->opcode, 371 &p_eqe->data.rdma_data); 372 return ECORE_SUCCESS; 373 } 374 #ifdef CONFIG_ECORE_IWARP 375 case PROTOCOLID_IWARP: 376 { 377 ecore_iwarp_async_event(p_hwfn, 378 p_eqe->opcode, 379 &p_eqe->data.rdma_data.async_handle, 380 p_eqe->fw_return_code); 381 return ECORE_SUCCESS; 382 } 383 #endif 384 #endif 385 case PROTOCOLID_COMMON: 386 return ecore_sriov_eqe_event(p_hwfn, 387 p_eqe->opcode, 388 p_eqe->echo, 389 &p_eqe->data); 390 #ifdef CONFIG_ECORE_ISCSI 391 case PROTOCOLID_ISCSI: 392 if (p_hwfn->p_iscsi_info->event_cb != OSAL_NULL) { 393 struct ecore_iscsi_info *p_iscsi = p_hwfn->p_iscsi_info; 394 395 return p_iscsi->event_cb(p_iscsi->event_context, 396 p_eqe->opcode, &p_eqe->data); 397 } else { 398 DP_NOTICE(p_hwfn, 399 false, "iSCSI async completion is not set\n"); 400 return ECORE_NOTIMPL; 401 } 402 #endif 403 default: 404 DP_NOTICE(p_hwfn, 405 true, "Unknown Async completion for protocol: %d\n", 406 p_eqe->protocol_id); 407 return ECORE_INVAL; 408 } 409 } 410 411 /*************************************************************************** 412 * EQ API 413 ***************************************************************************/ 414 void ecore_eq_prod_update(struct ecore_hwfn *p_hwfn, 415 u16 prod) 416 { 417 u32 addr = GTT_BAR0_MAP_REG_USDM_RAM + 418 USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id); 419 420 REG_WR16(p_hwfn, addr, prod); 421 422 /* keep prod updates ordered */ 423 OSAL_MMIOWB(p_hwfn->p_dev); 424 } 425 426 enum _ecore_status_t ecore_eq_completion(struct ecore_hwfn *p_hwfn, 427 void *cookie) 428 429 { 430 struct ecore_eq *p_eq = cookie; 431 struct ecore_chain *p_chain = &p_eq->chain; 432 enum _ecore_status_t rc = 0; 433 434 /* take a snapshot of the FW consumer */ 435 u16 fw_cons_idx = OSAL_LE16_TO_CPU(*p_eq->p_fw_cons); 436 437 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx); 438 439 /* Need to guarantee the fw_cons index we use points to a usuable 440 * element (to comply with our chain), so our macros would comply 441 */ 442 if ((fw_cons_idx & ecore_chain_get_usable_per_page(p_chain)) == 443 ecore_chain_get_usable_per_page(p_chain)) { 444 fw_cons_idx += ecore_chain_get_unusable_per_page(p_chain); 445 } 446 447 /* Complete current segment of eq entries */ 448 while (fw_cons_idx != ecore_chain_get_cons_idx(p_chain)) { 449 struct event_ring_entry *p_eqe = ecore_chain_consume(p_chain); 450 if (!p_eqe) { 451 rc = ECORE_INVAL; 452 break; 453 } 454 455 DP_VERBOSE(p_hwfn, 456 ECORE_MSG_SPQ, 457 "op %x prot %x res0 %x echo %x fwret %x flags %x\n", 458 p_eqe->opcode, /* Event Opcode */ 459 p_eqe->protocol_id, /* Event Protocol ID */ 460 p_eqe->reserved0, /* Reserved */ 461 OSAL_LE16_TO_CPU(p_eqe->echo),/* Echo value from 462 ramrod data on the host 463 */ 464 p_eqe->fw_return_code, /* FW return code for SP 465 ramrods 466 */ 467 p_eqe->flags); 468 #ifndef REMOVE_DBG 469 if (p_eqe->protocol_id == PROTOCOLID_ISCSI) 470 ecore_iscsi_eq_dump(p_hwfn, p_eqe); 471 #endif 472 473 if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) { 474 if (ecore_async_event_completion(p_hwfn, p_eqe)) 475 rc = ECORE_INVAL; 476 } else if (ecore_spq_completion(p_hwfn, 477 p_eqe->echo, 478 p_eqe->fw_return_code, 479 &p_eqe->data)) { 480 rc = ECORE_INVAL; 481 } 482 483 ecore_chain_recycle_consumed(p_chain); 484 } 485 486 ecore_eq_prod_update(p_hwfn, ecore_chain_get_prod_idx(p_chain)); 487 488 return rc; 489 } 490 491 enum _ecore_status_t ecore_eq_alloc(struct ecore_hwfn *p_hwfn, u16 num_elem) 492 { 493 struct ecore_eq *p_eq; 494 495 /* Allocate EQ struct */ 496 p_eq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_eq)); 497 if (!p_eq) { 498 DP_NOTICE(p_hwfn, true, 499 "Failed to allocate `struct ecore_eq'\n"); 500 return ECORE_NOMEM; 501 } 502 503 /* Allocate and initialize EQ chain*/ 504 if (ecore_chain_alloc(p_hwfn->p_dev, 505 ECORE_CHAIN_USE_TO_PRODUCE, 506 ECORE_CHAIN_MODE_PBL, 507 ECORE_CHAIN_CNT_TYPE_U16, 508 num_elem, 509 sizeof(union event_ring_element), 510 &p_eq->chain, OSAL_NULL) != ECORE_SUCCESS) { 511 DP_NOTICE(p_hwfn, true, "Failed to allocate eq chain\n"); 512 goto eq_allocate_fail; 513 } 514 515 /* register EQ completion on the SP SB */ 516 ecore_int_register_cb(p_hwfn, ecore_eq_completion, 517 p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons); 518 519 p_hwfn->p_eq = p_eq; 520 return ECORE_SUCCESS; 521 522 eq_allocate_fail: 523 OSAL_FREE(p_hwfn->p_dev, p_eq); 524 return ECORE_NOMEM; 525 } 526 527 void ecore_eq_setup(struct ecore_hwfn *p_hwfn) 528 { 529 ecore_chain_reset(&p_hwfn->p_eq->chain); 530 } 531 532 void ecore_eq_free(struct ecore_hwfn *p_hwfn) 533 { 534 if (!p_hwfn->p_eq) 535 return; 536 537 ecore_chain_free(p_hwfn->p_dev, &p_hwfn->p_eq->chain); 538 539 OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_eq); 540 p_hwfn->p_eq = OSAL_NULL; 541 } 542 543 /*************************************************************************** 544 * CQE API - manipulate EQ functionallity 545 ***************************************************************************/ 546 static enum _ecore_status_t ecore_cqe_completion(struct ecore_hwfn *p_hwfn, 547 struct eth_slow_path_rx_cqe *cqe, 548 enum protocol_type protocol) 549 { 550 if (IS_VF(p_hwfn->p_dev)) 551 return OSAL_VF_CQE_COMPLETION(p_hwfn, cqe, protocol); 552 553 /* @@@tmp - it's possible we'll eventually want to handle some 554 * actual commands that can arrive here, but for now this is only 555 * used to complete the ramrod using the echo value on the cqe 556 */ 557 return ecore_spq_completion(p_hwfn, cqe->echo, 0, OSAL_NULL); 558 } 559 560 enum _ecore_status_t ecore_eth_cqe_completion(struct ecore_hwfn *p_hwfn, 561 struct eth_slow_path_rx_cqe *cqe) 562 { 563 enum _ecore_status_t rc; 564 565 rc = ecore_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH); 566 if (rc) { 567 DP_NOTICE(p_hwfn, true, 568 "Failed to handle RXQ CQE [cmd 0x%02x]\n", 569 cqe->ramrod_cmd_id); 570 } 571 572 return rc; 573 } 574 575 /*************************************************************************** 576 * Slow hwfn Queue (spq) 577 ***************************************************************************/ 578 void ecore_spq_setup(struct ecore_hwfn *p_hwfn) 579 { 580 struct ecore_spq *p_spq = p_hwfn->p_spq; 581 struct ecore_spq_entry *p_virt = OSAL_NULL; 582 dma_addr_t p_phys = 0; 583 u32 i, capacity; 584 585 OSAL_LIST_INIT(&p_spq->pending); 586 OSAL_LIST_INIT(&p_spq->completion_pending); 587 OSAL_LIST_INIT(&p_spq->free_pool); 588 OSAL_LIST_INIT(&p_spq->unlimited_pending); 589 OSAL_SPIN_LOCK_INIT(&p_spq->lock); 590 591 /* SPQ empty pool */ 592 p_phys = p_spq->p_phys + OFFSETOF(struct ecore_spq_entry, ramrod); 593 p_virt = p_spq->p_virt; 594 595 capacity = ecore_chain_get_capacity(&p_spq->chain); 596 for (i = 0; i < capacity; i++) { 597 DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys); 598 599 OSAL_LIST_PUSH_TAIL(&p_virt->list, &p_spq->free_pool); 600 601 p_virt++; 602 p_phys += sizeof(struct ecore_spq_entry); 603 } 604 605 /* Statistics */ 606 p_spq->normal_count = 0; 607 p_spq->comp_count = 0; 608 p_spq->comp_sent_count = 0; 609 p_spq->unlimited_pending_count = 0; 610 611 OSAL_MEM_ZERO(p_spq->p_comp_bitmap, 612 SPQ_COMP_BMAP_SIZE * sizeof(unsigned long)); 613 p_spq->comp_bitmap_idx = 0; 614 615 /* SPQ cid, cannot fail */ 616 ecore_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid); 617 ecore_spq_hw_initialize(p_hwfn, p_spq); 618 619 /* reset the chain itself */ 620 ecore_chain_reset(&p_spq->chain); 621 } 622 623 enum _ecore_status_t ecore_spq_alloc(struct ecore_hwfn *p_hwfn) 624 { 625 struct ecore_spq_entry *p_virt = OSAL_NULL; 626 struct ecore_spq *p_spq = OSAL_NULL; 627 dma_addr_t p_phys = 0; 628 u32 capacity; 629 630 /* SPQ struct */ 631 p_spq = 632 OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(struct ecore_spq)); 633 if (!p_spq) { 634 DP_NOTICE(p_hwfn, true, "Failed to allocate `struct ecore_spq'\n"); 635 return ECORE_NOMEM; 636 } 637 638 /* SPQ ring */ 639 if (ecore_chain_alloc(p_hwfn->p_dev, 640 ECORE_CHAIN_USE_TO_PRODUCE, 641 ECORE_CHAIN_MODE_SINGLE, 642 ECORE_CHAIN_CNT_TYPE_U16, 643 0, /* N/A when the mode is SINGLE */ 644 sizeof(struct slow_path_element), 645 &p_spq->chain, OSAL_NULL)) { 646 DP_NOTICE(p_hwfn, true, "Failed to allocate spq chain\n"); 647 goto spq_allocate_fail; 648 } 649 650 /* allocate and fill the SPQ elements (incl. ramrod data list) */ 651 capacity = ecore_chain_get_capacity(&p_spq->chain); 652 p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, &p_phys, 653 capacity * 654 sizeof(struct ecore_spq_entry)); 655 if (!p_virt) { 656 goto spq_allocate_fail; 657 } 658 659 p_spq->p_virt = p_virt; 660 p_spq->p_phys = p_phys; 661 662 #ifdef CONFIG_ECORE_LOCK_ALLOC 663 OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_spq->lock); 664 #endif 665 666 p_hwfn->p_spq = p_spq; 667 return ECORE_SUCCESS; 668 669 spq_allocate_fail: 670 ecore_chain_free(p_hwfn->p_dev, &p_spq->chain); 671 OSAL_FREE(p_hwfn->p_dev, p_spq); 672 return ECORE_NOMEM; 673 } 674 675 void ecore_spq_free(struct ecore_hwfn *p_hwfn) 676 { 677 struct ecore_spq *p_spq = p_hwfn->p_spq; 678 u32 capacity; 679 680 if (!p_spq) 681 return; 682 683 if (p_spq->p_virt) { 684 capacity = ecore_chain_get_capacity(&p_spq->chain); 685 OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev, 686 p_spq->p_virt, 687 p_spq->p_phys, 688 capacity * 689 sizeof(struct ecore_spq_entry)); 690 } 691 692 ecore_chain_free(p_hwfn->p_dev, &p_spq->chain); 693 #ifdef CONFIG_ECORE_LOCK_ALLOC 694 OSAL_SPIN_LOCK_DEALLOC(&p_spq->lock); 695 #endif 696 697 OSAL_FREE(p_hwfn->p_dev, p_spq); 698 p_hwfn->p_spq = OSAL_NULL; 699 } 700 701 enum _ecore_status_t ecore_spq_get_entry(struct ecore_hwfn *p_hwfn, 702 struct ecore_spq_entry **pp_ent) 703 { 704 struct ecore_spq *p_spq = p_hwfn->p_spq; 705 struct ecore_spq_entry *p_ent = OSAL_NULL; 706 enum _ecore_status_t rc = ECORE_SUCCESS; 707 708 OSAL_SPIN_LOCK(&p_spq->lock); 709 710 if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) { 711 712 p_ent = OSAL_ZALLOC(p_hwfn->p_dev, GFP_ATOMIC, sizeof(*p_ent)); 713 if (!p_ent) { 714 DP_NOTICE(p_hwfn, true, "Failed to allocate an SPQ entry for a pending ramrod\n"); 715 rc = ECORE_NOMEM; 716 goto out_unlock; 717 } 718 p_ent->queue = &p_spq->unlimited_pending; 719 } else { 720 p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool, 721 struct ecore_spq_entry, 722 list); 723 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->free_pool); 724 p_ent->queue = &p_spq->pending; 725 } 726 727 *pp_ent = p_ent; 728 729 out_unlock: 730 OSAL_SPIN_UNLOCK(&p_spq->lock); 731 return rc; 732 } 733 734 /* Locked variant; Should be called while the SPQ lock is taken */ 735 static void __ecore_spq_return_entry(struct ecore_hwfn *p_hwfn, 736 struct ecore_spq_entry *p_ent) 737 { 738 OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_hwfn->p_spq->free_pool); 739 } 740 741 void ecore_spq_return_entry(struct ecore_hwfn *p_hwfn, 742 struct ecore_spq_entry *p_ent) 743 { 744 OSAL_SPIN_LOCK(&p_hwfn->p_spq->lock); 745 __ecore_spq_return_entry(p_hwfn, p_ent); 746 OSAL_SPIN_UNLOCK(&p_hwfn->p_spq->lock); 747 } 748 749 /** 750 * @brief ecore_spq_add_entry - adds a new entry to the pending 751 * list. Should be used while lock is being held. 752 * 753 * Addes an entry to the pending list is there is room (en empty 754 * element is avaliable in the free_pool), or else places the 755 * entry in the unlimited_pending pool. 756 * 757 * @param p_hwfn 758 * @param p_ent 759 * @param priority 760 * 761 * @return enum _ecore_status_t 762 */ 763 static enum _ecore_status_t ecore_spq_add_entry(struct ecore_hwfn *p_hwfn, 764 struct ecore_spq_entry *p_ent, 765 enum spq_priority priority) 766 { 767 struct ecore_spq *p_spq = p_hwfn->p_spq; 768 769 if (p_ent->queue == &p_spq->unlimited_pending) { 770 if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) { 771 772 OSAL_LIST_PUSH_TAIL(&p_ent->list, 773 &p_spq->unlimited_pending); 774 p_spq->unlimited_pending_count++; 775 776 return ECORE_SUCCESS; 777 778 } else { 779 struct ecore_spq_entry *p_en2; 780 781 p_en2 = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool, 782 struct ecore_spq_entry, 783 list); 784 OSAL_LIST_REMOVE_ENTRY(&p_en2->list, &p_spq->free_pool); 785 786 /* Copy the ring element physical pointer to the new 787 * entry, since we are about to override the entire ring 788 * entry and don't want to lose the pointer. 789 */ 790 p_ent->elem.data_ptr = p_en2->elem.data_ptr; 791 792 *p_en2 = *p_ent; 793 794 /* EBLOCK responsible to free the allocated p_ent */ 795 if (p_ent->comp_mode != ECORE_SPQ_MODE_EBLOCK) 796 OSAL_FREE(p_hwfn->p_dev, p_ent); 797 798 p_ent = p_en2; 799 } 800 } 801 802 /* entry is to be placed in 'pending' queue */ 803 switch (priority) { 804 case ECORE_SPQ_PRIORITY_NORMAL: 805 OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_spq->pending); 806 p_spq->normal_count++; 807 break; 808 case ECORE_SPQ_PRIORITY_HIGH: 809 OSAL_LIST_PUSH_HEAD(&p_ent->list, &p_spq->pending); 810 p_spq->high_count++; 811 break; 812 default: 813 return ECORE_INVAL; 814 } 815 816 return ECORE_SUCCESS; 817 } 818 819 /*************************************************************************** 820 * Accessor 821 ***************************************************************************/ 822 823 u32 ecore_spq_get_cid(struct ecore_hwfn *p_hwfn) 824 { 825 if (!p_hwfn->p_spq) { 826 return 0xffffffff; /* illegal */ 827 } 828 return p_hwfn->p_spq->cid; 829 } 830 831 /*************************************************************************** 832 * Posting new Ramrods 833 ***************************************************************************/ 834 835 static enum _ecore_status_t ecore_spq_post_list(struct ecore_hwfn *p_hwfn, 836 osal_list_t *head, 837 u32 keep_reserve) 838 { 839 struct ecore_spq *p_spq = p_hwfn->p_spq; 840 enum _ecore_status_t rc; 841 842 /* TODO - implementation might be wasteful; will always keep room 843 * for an additional high priority ramrod (even if one is already 844 * pending FW) 845 */ 846 while (ecore_chain_get_elem_left(&p_spq->chain) > keep_reserve && 847 !OSAL_LIST_IS_EMPTY(head)) { 848 struct ecore_spq_entry *p_ent = 849 OSAL_LIST_FIRST_ENTRY(head, struct ecore_spq_entry, list); 850 if (p_ent != OSAL_NULL) { 851 #if defined(_NTDDK_) 852 #pragma warning(suppress : 6011 28182) 853 #endif 854 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, head); 855 OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_spq->completion_pending); 856 p_spq->comp_sent_count++; 857 858 rc = ecore_spq_hw_post(p_hwfn, p_spq, p_ent); 859 if (rc) { 860 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, 861 &p_spq->completion_pending); 862 __ecore_spq_return_entry(p_hwfn, p_ent); 863 return rc; 864 } 865 } 866 } 867 868 return ECORE_SUCCESS; 869 } 870 871 static enum _ecore_status_t ecore_spq_pend_post(struct ecore_hwfn *p_hwfn) 872 { 873 struct ecore_spq *p_spq = p_hwfn->p_spq; 874 struct ecore_spq_entry *p_ent = OSAL_NULL; 875 876 while (!OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) 877 { 878 if (OSAL_LIST_IS_EMPTY(&p_spq->unlimited_pending)) 879 break; 880 881 p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->unlimited_pending, 882 struct ecore_spq_entry, 883 list); 884 if (!p_ent) 885 return ECORE_INVAL; 886 887 #if defined(_NTDDK_) 888 #pragma warning(suppress : 6011) 889 #endif 890 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->unlimited_pending); 891 892 ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority); 893 } 894 895 return ecore_spq_post_list(p_hwfn, &p_spq->pending, 896 SPQ_HIGH_PRI_RESERVE_DEFAULT); 897 } 898 899 enum _ecore_status_t ecore_spq_post(struct ecore_hwfn *p_hwfn, 900 struct ecore_spq_entry *p_ent, 901 u8 *fw_return_code) 902 { 903 enum _ecore_status_t rc = ECORE_SUCCESS; 904 struct ecore_spq *p_spq = p_hwfn ? p_hwfn->p_spq : OSAL_NULL; 905 bool b_ret_ent = true; 906 907 if (!p_hwfn) 908 return ECORE_INVAL; 909 910 if (!p_ent) { 911 DP_NOTICE(p_hwfn, true, "Got a NULL pointer\n"); 912 return ECORE_INVAL; 913 } 914 915 if (p_hwfn->p_dev->recov_in_prog) { 916 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, 917 "Recovery is in progress -> skip spq post [cmd %02x protocol %02x]\n", 918 p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id); 919 /* Return success to let the flows to be completed successfully 920 * w/o any error handling. 921 */ 922 return ECORE_SUCCESS; 923 } 924 925 OSAL_SPIN_LOCK(&p_spq->lock); 926 927 /* Complete the entry */ 928 rc = ecore_spq_fill_entry(p_hwfn, p_ent); 929 930 /* Check return value after LOCK is taken for cleaner error flow */ 931 if (rc) 932 goto spq_post_fail; 933 934 /* Add the request to the pending queue */ 935 rc = ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority); 936 if (rc) 937 goto spq_post_fail; 938 939 rc = ecore_spq_pend_post(p_hwfn); 940 if (rc) { 941 /* Since it's possible that pending failed for a different 942 * entry [although unlikely], the failed entry was already 943 * dealt with; No need to return it here. 944 */ 945 b_ret_ent = false; 946 goto spq_post_fail; 947 } 948 949 OSAL_SPIN_UNLOCK(&p_spq->lock); 950 951 if (p_ent->comp_mode == ECORE_SPQ_MODE_EBLOCK) { 952 /* For entries in ECORE BLOCK mode, the completion code cannot 953 * perform the necessary cleanup - if it did, we couldn't 954 * access p_ent here to see whether it's successful or not. 955 * Thus, after gaining the answer perform the cleanup here. 956 */ 957 rc = ecore_spq_block(p_hwfn, p_ent, fw_return_code, 958 p_ent->queue == &p_spq->unlimited_pending); 959 960 if (p_ent->queue == &p_spq->unlimited_pending) { 961 /* This is an allocated p_ent which does not need to 962 * return to pool. 963 */ 964 OSAL_FREE(p_hwfn->p_dev, p_ent); 965 966 /* TBD: handle error flow and remove p_ent from 967 * completion pending 968 */ 969 return rc; 970 } 971 972 if (rc) 973 goto spq_post_fail2; 974 975 /* return to pool */ 976 ecore_spq_return_entry(p_hwfn, p_ent); 977 } 978 return rc; 979 980 spq_post_fail2: 981 OSAL_SPIN_LOCK(&p_spq->lock); 982 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->completion_pending); 983 ecore_chain_return_produced(&p_spq->chain); 984 985 spq_post_fail: 986 /* return to the free pool */ 987 if (b_ret_ent) 988 __ecore_spq_return_entry(p_hwfn, p_ent); 989 OSAL_SPIN_UNLOCK(&p_spq->lock); 990 991 return rc; 992 } 993 994 enum _ecore_status_t ecore_spq_completion(struct ecore_hwfn *p_hwfn, 995 __le16 echo, 996 u8 fw_return_code, 997 union event_ring_data *p_data) 998 { 999 struct ecore_spq *p_spq; 1000 struct ecore_spq_entry *p_ent = OSAL_NULL; 1001 struct ecore_spq_entry *tmp; 1002 struct ecore_spq_entry *found = OSAL_NULL; 1003 enum _ecore_status_t rc; 1004 1005 if (!p_hwfn) { 1006 return ECORE_INVAL; 1007 } 1008 1009 p_spq = p_hwfn->p_spq; 1010 if (!p_spq) { 1011 return ECORE_INVAL; 1012 } 1013 1014 OSAL_SPIN_LOCK(&p_spq->lock); 1015 OSAL_LIST_FOR_EACH_ENTRY_SAFE(p_ent, 1016 tmp, 1017 &p_spq->completion_pending, 1018 list, 1019 struct ecore_spq_entry) { 1020 1021 if (p_ent->elem.hdr.echo == echo) { 1022 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, 1023 &p_spq->completion_pending); 1024 1025 /* Avoid overriding of SPQ entries when getting 1026 * out-of-order completions, by marking the completions 1027 * in a bitmap and increasing the chain consumer only 1028 * for the first successive completed entries. 1029 */ 1030 SPQ_COMP_BMAP_SET_BIT(p_spq, echo); 1031 while (SPQ_COMP_BMAP_TEST_BIT(p_spq, 1032 p_spq->comp_bitmap_idx)) { 1033 SPQ_COMP_BMAP_CLEAR_BIT(p_spq, 1034 p_spq->comp_bitmap_idx); 1035 p_spq->comp_bitmap_idx++; 1036 ecore_chain_return_produced(&p_spq->chain); 1037 } 1038 1039 p_spq->comp_count++; 1040 found = p_ent; 1041 break; 1042 } 1043 1044 /* This is debug and should be relatively uncommon - depends 1045 * on scenarios which have mutliple per-PF sent ramrods. 1046 */ 1047 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, 1048 "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n", 1049 OSAL_LE16_TO_CPU(echo), 1050 OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo)); 1051 } 1052 1053 /* Release lock before callback, as callback may post 1054 * an additional ramrod. 1055 */ 1056 OSAL_SPIN_UNLOCK(&p_spq->lock); 1057 1058 if (!found) { 1059 DP_NOTICE(p_hwfn, true, 1060 "Failed to find an entry this EQE [echo %04x] completes\n", 1061 OSAL_LE16_TO_CPU(echo)); 1062 return ECORE_EXISTS; 1063 } 1064 1065 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, 1066 "Complete EQE [echo %04x]: func %p cookie %p)\n", 1067 OSAL_LE16_TO_CPU(echo), 1068 p_ent->comp_cb.function, p_ent->comp_cb.cookie); 1069 if (found->comp_cb.function) 1070 found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data, 1071 fw_return_code); 1072 else 1073 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "Got a completion without a callback function\n"); 1074 1075 if ((found->comp_mode != ECORE_SPQ_MODE_EBLOCK) || 1076 (found->queue == &p_spq->unlimited_pending)) 1077 /* EBLOCK is responsible for returning its own entry into the 1078 * free list, unless it originally added the entry into the 1079 * unlimited pending list. 1080 */ 1081 ecore_spq_return_entry(p_hwfn, found); 1082 1083 /* Attempt to post pending requests */ 1084 OSAL_SPIN_LOCK(&p_spq->lock); 1085 rc = ecore_spq_pend_post(p_hwfn); 1086 OSAL_SPIN_UNLOCK(&p_spq->lock); 1087 1088 return rc; 1089 } 1090 1091 enum _ecore_status_t ecore_consq_alloc(struct ecore_hwfn *p_hwfn) 1092 { 1093 struct ecore_consq *p_consq; 1094 1095 /* Allocate ConsQ struct */ 1096 p_consq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_consq)); 1097 if (!p_consq) { 1098 DP_NOTICE(p_hwfn, true, 1099 "Failed to allocate `struct ecore_consq'\n"); 1100 return ECORE_NOMEM; 1101 } 1102 1103 /* Allocate and initialize EQ chain*/ 1104 if (ecore_chain_alloc(p_hwfn->p_dev, 1105 ECORE_CHAIN_USE_TO_PRODUCE, 1106 ECORE_CHAIN_MODE_PBL, 1107 ECORE_CHAIN_CNT_TYPE_U16, 1108 ECORE_CHAIN_PAGE_SIZE/0x80, 1109 0x80, 1110 &p_consq->chain, OSAL_NULL) != ECORE_SUCCESS) { 1111 DP_NOTICE(p_hwfn, true, "Failed to allocate consq chain"); 1112 goto consq_allocate_fail; 1113 } 1114 1115 p_hwfn->p_consq = p_consq; 1116 return ECORE_SUCCESS; 1117 1118 consq_allocate_fail: 1119 OSAL_FREE(p_hwfn->p_dev, p_consq); 1120 return ECORE_NOMEM; 1121 } 1122 1123 void ecore_consq_setup(struct ecore_hwfn *p_hwfn) 1124 { 1125 ecore_chain_reset(&p_hwfn->p_consq->chain); 1126 } 1127 1128 void ecore_consq_free(struct ecore_hwfn *p_hwfn) 1129 { 1130 if (!p_hwfn->p_consq) 1131 return; 1132 1133 ecore_chain_free(p_hwfn->p_dev, &p_hwfn->p_consq->chain); 1134 1135 OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_consq); 1136 p_hwfn->p_consq = OSAL_NULL; 1137 } 1138