1 /*- 2 * Copyright 2016-2025 Microchip Technology, Inc. and/or its subsidiaries. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 27 #include "smartpqi_includes.h" 28 29 /* 30 * Request the adapter to get PQI capabilities supported. 31 */ 32 static int 33 pqisrc_report_pqi_capability(pqisrc_softstate_t *softs) 34 { 35 int ret = PQI_STATUS_SUCCESS; 36 37 DBG_FUNC("IN\n"); 38 39 gen_adm_req_iu_t admin_req; 40 gen_adm_resp_iu_t admin_resp; 41 dma_mem_t pqi_cap_dma_buf; 42 pqi_dev_cap_t *capability = NULL; 43 pqi_iu_layer_desc_t *iu_layer_desc = NULL; 44 45 /* Allocate Non DMA memory */ 46 capability = os_mem_alloc(softs, sizeof(*capability)); 47 if (!capability) { 48 DBG_ERR("Failed to allocate memory for capability\n"); 49 goto err_out; 50 } 51 52 memset(&admin_req, 0, sizeof(admin_req)); 53 memset(&admin_resp, 0, sizeof(admin_resp)); 54 55 memset(&pqi_cap_dma_buf, 0, sizeof(struct dma_mem)); 56 os_strlcpy(pqi_cap_dma_buf.tag, "pqi_cap_buf", sizeof(pqi_cap_dma_buf.tag)); 57 pqi_cap_dma_buf.size = REPORT_PQI_DEV_CAP_DATA_BUF_SIZE; 58 pqi_cap_dma_buf.align = PQISRC_DEFAULT_DMA_ALIGN; 59 60 ret = os_dma_mem_alloc(softs, &pqi_cap_dma_buf); 61 if (ret) { 62 DBG_ERR("Failed to allocate capability DMA buffer : %d\n", ret); 63 goto err_dma_alloc; 64 } 65 66 admin_req.fn_code = PQI_FUNCTION_REPORT_DEV_CAP; 67 admin_req.req_type.general_func.buf_size = pqi_cap_dma_buf.size; 68 admin_req.req_type.general_func.sg_desc.length = pqi_cap_dma_buf.size; 69 admin_req.req_type.general_func.sg_desc.addr = pqi_cap_dma_buf.dma_addr; 70 admin_req.req_type.general_func.sg_desc.type = SGL_DESCRIPTOR_CODE_DATA_BLOCK; 71 72 ret = pqisrc_submit_admin_req(softs, &admin_req, &admin_resp); 73 if( PQI_STATUS_SUCCESS == ret) { 74 memcpy(capability, 75 pqi_cap_dma_buf.virt_addr, 76 pqi_cap_dma_buf.size); 77 } else { 78 DBG_ERR("Failed to send admin req report pqi device capability\n"); 79 goto err_admin_req; 80 81 } 82 83 softs->pqi_dev_cap.max_iqs = capability->max_iqs; 84 softs->pqi_dev_cap.max_iq_elements = capability->max_iq_elements; 85 softs->pqi_dev_cap.max_iq_elem_len = capability->max_iq_elem_len; 86 softs->pqi_dev_cap.min_iq_elem_len = capability->min_iq_elem_len; 87 softs->pqi_dev_cap.max_oqs = capability->max_oqs; 88 softs->pqi_dev_cap.max_oq_elements = capability->max_oq_elements; 89 softs->pqi_dev_cap.max_oq_elem_len = capability->max_oq_elem_len; 90 softs->pqi_dev_cap.intr_coales_time_granularity = capability->intr_coales_time_granularity; 91 92 iu_layer_desc = &capability->iu_layer_desc[PQI_PROTOCOL_SOP]; 93 softs->max_ib_iu_length_per_fw = iu_layer_desc->max_ib_iu_len; 94 softs->ib_spanning_supported = iu_layer_desc->ib_spanning_supported; 95 softs->ob_spanning_supported = iu_layer_desc->ob_spanning_supported; 96 97 DBG_INIT("softs->pqi_dev_cap.max_iqs: %d\n", softs->pqi_dev_cap.max_iqs); 98 DBG_INIT("softs->pqi_dev_cap.max_iq_elements: %d\n", softs->pqi_dev_cap.max_iq_elements); 99 DBG_INIT("softs->pqi_dev_cap.max_iq_elem_len: %d\n", softs->pqi_dev_cap.max_iq_elem_len); 100 DBG_INIT("softs->pqi_dev_cap.min_iq_elem_len: %d\n", softs->pqi_dev_cap.min_iq_elem_len); 101 DBG_INIT("softs->pqi_dev_cap.max_oqs: %d\n", softs->pqi_dev_cap.max_oqs); 102 DBG_INIT("softs->pqi_dev_cap.max_oq_elements: %d\n", softs->pqi_dev_cap.max_oq_elements); 103 DBG_INIT("softs->pqi_dev_cap.max_oq_elem_len: %d\n", softs->pqi_dev_cap.max_oq_elem_len); 104 DBG_INIT("softs->pqi_dev_cap.intr_coales_time_granularity: %d\n", softs->pqi_dev_cap.intr_coales_time_granularity); 105 DBG_INIT("softs->max_ib_iu_length_per_fw: %d\n", softs->max_ib_iu_length_per_fw); 106 DBG_INIT("softs->ib_spanning_supported: %d\n", softs->ib_spanning_supported); 107 DBG_INIT("softs->ob_spanning_supported: %d\n", softs->ob_spanning_supported); 108 109 /* Not expecting these to change, could cause problems if they do */ 110 ASSERT(softs->pqi_dev_cap.max_iq_elem_len == PQISRC_OP_MAX_ELEM_SIZE); 111 ASSERT(softs->pqi_dev_cap.min_iq_elem_len == PQISRC_OP_MIN_ELEM_SIZE); 112 ASSERT(softs->max_ib_iu_length_per_fw == PQISRC_MAX_SPANNING_IU_LENGTH); 113 ASSERT(softs->ib_spanning_supported == true); 114 115 116 os_mem_free(softs, (void *)capability, 117 REPORT_PQI_DEV_CAP_DATA_BUF_SIZE); 118 os_dma_mem_free(softs, &pqi_cap_dma_buf); 119 120 DBG_FUNC("OUT\n"); 121 return ret; 122 123 err_admin_req: 124 os_dma_mem_free(softs, &pqi_cap_dma_buf); 125 err_dma_alloc: 126 if (capability) 127 os_mem_free(softs, (void *)capability, 128 REPORT_PQI_DEV_CAP_DATA_BUF_SIZE); 129 err_out: 130 DBG_FUNC("failed OUT\n"); 131 return PQI_STATUS_FAILURE; 132 } 133 134 /* 135 * Function used to deallocate the used rcb. 136 */ 137 void 138 pqisrc_free_rcb(pqisrc_softstate_t *softs, int req_count) 139 { 140 141 uint32_t num_req; 142 size_t size; 143 int i; 144 145 DBG_FUNC("IN\n"); 146 num_req = softs->max_outstanding_io + 1; 147 size = num_req * sizeof(rcb_t); 148 for (i = 1; i < req_count; i++) 149 os_dma_mem_free(softs, &softs->sg_dma_desc[i]); 150 os_mem_free(softs, (void *)softs->rcb, size); 151 softs->rcb = NULL; 152 DBG_FUNC("OUT\n"); 153 } 154 155 156 /* 157 * Allocate memory for rcb and SG descriptors. 158 * TODO : Sg list should be created separately 159 */ 160 static int 161 pqisrc_allocate_rcb(pqisrc_softstate_t *softs) 162 { 163 int ret = PQI_STATUS_SUCCESS; 164 int i = 0; 165 uint32_t num_req = 0; 166 uint32_t sg_buf_size = 0; 167 uint64_t alloc_size = 0; 168 rcb_t *rcb = NULL; 169 rcb_t *prcb = NULL; 170 DBG_FUNC("IN\n"); 171 172 /* Set maximum outstanding requests */ 173 /* The valid tag values are from 1, 2, ..., softs->max_outstanding_io 174 * The rcb will be accessed by using the tag as index 175 * As 0 tag index is not used, we need to allocate one extra. 176 */ 177 softs->max_outstanding_io = softs->pqi_cap.max_outstanding_io; 178 num_req = softs->max_outstanding_io + 1; 179 DBG_INIT("Max Outstanding IO reset to %u\n", num_req); 180 181 alloc_size = num_req * sizeof(rcb_t); 182 183 /* Allocate Non DMA memory */ 184 rcb = os_mem_alloc(softs, alloc_size); 185 if (!rcb) { 186 DBG_ERR("Failed to allocate memory for rcb\n"); 187 ret = PQI_STATUS_FAILURE; 188 goto err_out; 189 } 190 softs->rcb = rcb; 191 192 /* Allocate sg dma memory for sg chain */ 193 sg_buf_size = softs->pqi_cap.max_sg_elem * 194 sizeof(sgt_t); 195 196 prcb = &softs->rcb[1]; 197 /* Initialize rcb */ 198 for(i=1; i < num_req; i++) { 199 /* TODO:Here tag is local variable */ 200 char tag[15]; 201 sprintf(tag, "sg_dma_buf%d", i); 202 os_strlcpy(softs->sg_dma_desc[i].tag, tag, sizeof(softs->sg_dma_desc[i].tag)); 203 softs->sg_dma_desc[i].size = sg_buf_size; 204 softs->sg_dma_desc[i].align = PQISRC_DEFAULT_DMA_ALIGN; 205 206 ret = os_dma_mem_alloc(softs, &softs->sg_dma_desc[i]); 207 if (ret) { 208 DBG_ERR("Failed to Allocate sg desc %d\n", ret); 209 ret = PQI_STATUS_FAILURE; 210 goto error; 211 } 212 prcb->sg_chain_virt = (sgt_t *)(softs->sg_dma_desc[i].virt_addr); 213 prcb->sg_chain_dma = (dma_addr_t)(softs->sg_dma_desc[i].dma_addr); 214 prcb ++; 215 } 216 217 DBG_FUNC("OUT\n"); 218 return ret; 219 error: 220 pqisrc_free_rcb(softs, i); 221 err_out: 222 DBG_FUNC("failed OUT\n"); 223 return ret; 224 } 225 226 /* 227 * Function used to decide the operational queue configuration params 228 * - no of ibq/obq, shared/non-shared interrupt resource, IU spanning support 229 */ 230 void 231 pqisrc_decide_opq_config(pqisrc_softstate_t *softs) 232 { 233 uint16_t total_iq_elements; 234 235 DBG_FUNC("IN\n"); 236 237 DBG_INIT("softs->intr_count : %d softs->num_cpus_online : %d\n", 238 softs->intr_count, softs->num_cpus_online); 239 240 /* TODO : Get the number of IB and OB queues from OS layer */ 241 242 if (softs->intr_count == 1 || softs->num_cpus_online == 1) { 243 /* Share the event and Operational queue. */ 244 softs->num_op_obq = 1; 245 softs->share_opq_and_eventq = true; 246 } 247 else { 248 /* Note : One OBQ (OBQ0) reserved for event queue */ 249 softs->num_op_obq = MIN(softs->num_cpus_online, 250 softs->intr_count) - 1; 251 softs->share_opq_and_eventq = false; 252 } 253 /* If the available interrupt count is more than one, 254 we don’t need to share the interrupt for IO and event queue */ 255 if (softs->intr_count > 1) 256 softs->share_opq_and_eventq = false; 257 258 DBG_INIT("softs->num_op_obq : %u\n",softs->num_op_obq); 259 260 /* TODO : Reset the interrupt count based on number of queues*/ 261 262 softs->num_op_raid_ibq = softs->num_op_obq; 263 softs->num_op_aio_ibq = softs->num_op_raid_ibq; 264 softs->max_ibq_elem_size = softs->pqi_dev_cap.max_iq_elem_len * 16; 265 softs->max_obq_elem_size = softs->pqi_dev_cap.max_oq_elem_len * 16; 266 if (softs->max_ib_iu_length_per_fw == 256 && 267 softs->ob_spanning_supported) { 268 /* older f/w that doesn't actually support spanning. */ 269 softs->max_ib_iu_length = softs->max_ibq_elem_size; 270 } else { 271 /* max. inbound IU length is an multiple of our inbound element size. */ 272 softs->max_ib_iu_length = PQISRC_ROUND_DOWN(softs->max_ib_iu_length_per_fw, 273 softs->max_ibq_elem_size); 274 } 275 276 /* If Max. Outstanding IO came with Max. Spanning element count then, 277 needed elements per IO are multiplication of 278 Max.Outstanding IO and Max.Spanning element */ 279 total_iq_elements = (softs->max_outstanding_io * 280 (softs->max_ib_iu_length / softs->max_ibq_elem_size)); 281 282 softs->num_elem_per_op_ibq = total_iq_elements / softs->num_op_raid_ibq; 283 softs->num_elem_per_op_ibq = MIN(softs->num_elem_per_op_ibq, 284 softs->pqi_dev_cap.max_iq_elements); 285 286 softs->num_elem_per_op_obq = softs->max_outstanding_io / softs->num_op_obq; 287 softs->num_elem_per_op_obq = MIN(softs->num_elem_per_op_obq, 288 softs->pqi_dev_cap.max_oq_elements); 289 290 /* spanning elements should be 9 (1152/128) */ 291 softs->max_spanning_elems = softs->max_ib_iu_length/softs->max_ibq_elem_size; 292 ASSERT(softs->max_spanning_elems == PQISRC_MAX_SPANNING_ELEMS); 293 294 /* max SGs should be 8 (128/16) */ 295 softs->max_sg_per_single_iu_element = softs->max_ibq_elem_size / sizeof(sgt_t); 296 ASSERT(softs->max_sg_per_single_iu_element == MAX_EMBEDDED_SG_IN_IU); 297 298 /* max SGs for spanning cmd should be 68*/ 299 softs->max_sg_per_spanning_cmd = (softs->max_spanning_elems - 1) * softs->max_sg_per_single_iu_element; 300 softs->max_sg_per_spanning_cmd += MAX_EMBEDDED_SG_IN_FIRST_IU_DEFAULT; 301 302 DBG_INIT("softs->max_ib_iu_length: %d\n", softs->max_ib_iu_length); /* 1152 per FW advertisement */ 303 DBG_INIT("softs->num_elem_per_op_ibq: %u\n", softs->num_elem_per_op_ibq); /* 32 for xcal */ 304 DBG_INIT("softs->num_elem_per_op_obq: %u\n", softs->num_elem_per_op_obq); /* 256 for xcal */ 305 DBG_INIT("softs->max_spanning_elems: %d\n", softs->max_spanning_elems); /* 9 */ 306 DBG_INIT("softs->max_sg_per_spanning_cmd: %u\n", softs->max_sg_per_spanning_cmd); /* 68 until we add AIO writes */ 307 308 DBG_FUNC("OUT\n"); 309 } 310 311 /* 312 * Configure the operational queue parameters. 313 */ 314 int 315 pqisrc_configure_op_queues(pqisrc_softstate_t *softs) 316 { 317 int ret = PQI_STATUS_SUCCESS; 318 319 /* Get the PQI capability, 320 REPORT PQI DEVICE CAPABILITY request */ 321 ret = pqisrc_report_pqi_capability(softs); 322 if (ret) { 323 DBG_ERR("Failed to send report pqi dev capability request : %d\n", 324 ret); 325 goto err_out; 326 } 327 328 /* Reserve required no of slots for internal requests */ 329 softs->max_io_for_scsi_ml = softs->max_outstanding_io - PQI_RESERVED_IO_SLOTS_CNT; 330 331 /* Decide the Op queue configuration */ 332 pqisrc_decide_opq_config(softs); 333 334 DBG_FUNC("OUT\n"); 335 return ret; 336 337 err_out: 338 DBG_FUNC("OUT failed\n"); 339 return ret; 340 } 341 342 /* 343 * Validate the PQI mode of adapter. 344 */ 345 int 346 pqisrc_check_pqimode(pqisrc_softstate_t *softs) 347 { 348 int ret = PQI_STATUS_FAILURE; 349 int tmo = 0; 350 uint64_t signature = 0; 351 352 DBG_FUNC("IN\n"); 353 354 /* Check the PQI device signature */ 355 tmo = PQISRC_PQIMODE_READY_TIMEOUT; 356 do { 357 signature = LE_64(PCI_MEM_GET64(softs, &softs->pqi_reg->signature, PQI_SIGNATURE)); 358 359 if (memcmp(&signature, PQISRC_PQI_DEVICE_SIGNATURE, 360 sizeof(uint64_t)) == 0) { 361 ret = PQI_STATUS_SUCCESS; 362 break; 363 } 364 OS_SLEEP(PQISRC_MODE_READY_POLL_INTERVAL); 365 } while (tmo--); 366 367 PRINT_PQI_SIGNATURE(signature); 368 369 if (tmo <= 0) { 370 DBG_ERR("PQI Signature is invalid\n"); 371 ret = PQI_STATUS_TIMEOUT; 372 goto err_out; 373 } 374 375 tmo = PQISRC_PQIMODE_READY_TIMEOUT; 376 /* Check function and status code for the device */ 377 COND_WAIT((PCI_MEM_GET64(softs, &softs->pqi_reg->admin_q_config, 378 PQI_ADMINQ_CONFIG) == PQI_ADMIN_QUEUE_CONF_FUNC_STATUS_IDLE), tmo); 379 if (!tmo) { 380 DBG_ERR("PQI device is not in IDLE state\n"); 381 ret = PQI_STATUS_TIMEOUT; 382 goto err_out; 383 } 384 385 386 tmo = PQISRC_PQIMODE_READY_TIMEOUT; 387 /* Check the PQI device status register */ 388 COND_WAIT(LE_32(PCI_MEM_GET32(softs, &softs->pqi_reg->pqi_dev_status, PQI_DEV_STATUS)) & 389 PQI_DEV_STATE_AT_INIT, tmo); 390 if (!tmo) { 391 DBG_ERR("PQI Registers are not ready\n"); 392 ret = PQI_STATUS_TIMEOUT; 393 goto err_out; 394 } 395 396 DBG_FUNC("OUT\n"); 397 return ret; 398 err_out: 399 DBG_FUNC("OUT failed\n"); 400 return ret; 401 } 402 403 /* Wait for PQI reset completion for the adapter*/ 404 int 405 pqisrc_wait_for_pqi_reset_completion(pqisrc_softstate_t *softs) 406 { 407 int ret = PQI_STATUS_SUCCESS; 408 pqi_reset_reg_t reset_reg; 409 int pqi_reset_timeout = 0; 410 uint64_t val = 0; 411 uint32_t max_timeout = 0; 412 413 val = PCI_MEM_GET64(softs, &softs->pqi_reg->pqi_dev_adminq_cap, PQI_ADMINQ_CAP); 414 415 max_timeout = (val & 0xFFFF00000000) >> 32; 416 417 DBG_INIT("max_timeout for PQI reset completion in 100 msec units = %u\n", max_timeout); 418 419 while(1) { 420 if (pqi_reset_timeout++ == max_timeout) { 421 return PQI_STATUS_TIMEOUT; 422 } 423 OS_SLEEP(PQI_RESET_POLL_INTERVAL);/* 100 msec */ 424 reset_reg.all_bits = PCI_MEM_GET32(softs, 425 &softs->pqi_reg->dev_reset, PQI_DEV_RESET); 426 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED) 427 break; 428 } 429 430 return ret; 431 } 432 433 /* 434 * Function used to perform PQI hard reset. 435 */ 436 int 437 pqi_reset(pqisrc_softstate_t *softs) 438 { 439 int ret = PQI_STATUS_SUCCESS; 440 pqi_reset_reg_t pqi_reset_reg; 441 442 DBG_FUNC("IN\n"); 443 444 if (true == softs->ctrl_in_pqi_mode) { 445 446 if (softs->pqi_reset_quiesce_allowed) { 447 int val = PCI_MEM_GET32(softs, &softs->ioa_reg->host_to_ioa_db, 448 LEGACY_SIS_IDBR); 449 val |= SIS_PQI_RESET_QUIESCE; 450 PCI_MEM_PUT32(softs, &softs->ioa_reg->host_to_ioa_db, 451 LEGACY_SIS_IDBR, LE_32(val)); 452 OS_SLEEP(1000); /* 1 ms delay for PCI W/R ordering issue */ 453 ret = pqisrc_sis_wait_for_db_bit_to_clear(softs, SIS_PQI_RESET_QUIESCE); 454 if (ret) { 455 DBG_ERR("failed with error %d during quiesce\n", ret); 456 return ret; 457 } 458 } 459 460 pqi_reset_reg.all_bits = 0; 461 pqi_reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET; 462 pqi_reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET; 463 464 PCI_MEM_PUT32(softs, &softs->pqi_reg->dev_reset, PQI_DEV_RESET, 465 LE_32(pqi_reset_reg.all_bits)); 466 OS_SLEEP(1000); /* 1 ms delay for PCI W/R ordering issue */ 467 468 ret = pqisrc_wait_for_pqi_reset_completion(softs); 469 if (ret) { 470 DBG_ERR("PQI reset timed out: ret = %d!\n", ret); 471 return ret; 472 } 473 } 474 softs->ctrl_in_pqi_mode = false; 475 DBG_FUNC("OUT\n"); 476 return ret; 477 } 478 479 /* 480 * Initialize the adapter with supported PQI configuration. 481 */ 482 int 483 pqisrc_pqi_init(pqisrc_softstate_t *softs) 484 { 485 int ret = PQI_STATUS_SUCCESS; 486 487 DBG_FUNC("IN\n"); 488 489 /* Check the PQI signature */ 490 ret = pqisrc_check_pqimode(softs); 491 if(ret) { 492 DBG_ERR("failed to switch to pqi\n"); 493 goto err_out; 494 } 495 496 PQI_SAVE_CTRL_MODE(softs, CTRL_PQI_MODE); 497 softs->ctrl_in_pqi_mode = true; 498 499 /* Get the No. of Online CPUs,NUMA/Processor config from OS */ 500 ret = os_get_processor_config(softs); 501 if (ret) { 502 DBG_ERR("Failed to get processor config from OS %d\n", 503 ret); 504 goto err_out; 505 } 506 507 softs->intr_type = INTR_TYPE_NONE; 508 509 /* Get the interrupt count, type, priority available from OS */ 510 ret = os_get_intr_config(softs); 511 if (ret) { 512 DBG_ERR("Failed to get interrupt config from OS %d\n", 513 ret); 514 goto err_out; 515 } 516 517 /*Enable/Set Legacy INTx Interrupt mask clear pqi register, 518 *if allocated interrupt is legacy type. 519 */ 520 if (INTR_TYPE_FIXED == softs->intr_type) { 521 pqisrc_configure_legacy_intx(softs, true); 522 sis_enable_intx(softs); 523 } 524 525 /* Create Admin Queue pair*/ 526 ret = pqisrc_create_admin_queue(softs); 527 if(ret) { 528 DBG_ERR("Failed to configure admin queue\n"); 529 goto err_admin_queue; 530 } 531 532 /* For creating event and IO operational queues we have to submit 533 admin IU requests.So Allocate resources for submitting IUs */ 534 535 /* Allocate the request container block (rcb) */ 536 ret = pqisrc_allocate_rcb(softs); 537 if (ret == PQI_STATUS_FAILURE) { 538 DBG_ERR("Failed to allocate rcb \n"); 539 goto err_rcb; 540 } 541 542 /* Allocate & initialize request id queue */ 543 ret = pqisrc_init_taglist(softs,&softs->taglist, 544 softs->max_outstanding_io); 545 if (ret) { 546 DBG_ERR("Failed to allocate memory for request id q : %d\n", 547 ret); 548 goto err_taglist; 549 } 550 551 ret = pqisrc_configure_op_queues(softs); 552 if (ret) { 553 DBG_ERR("Failed to configure op queue\n"); 554 goto err_config_opq; 555 } 556 557 /* Create Operational queues */ 558 ret = pqisrc_create_op_queues(softs); 559 if(ret) { 560 DBG_ERR("Failed to create op queue\n"); 561 goto err_create_opq; 562 } 563 564 softs->ctrl_online = true; 565 566 DBG_FUNC("OUT\n"); 567 return ret; 568 569 err_create_opq: 570 err_config_opq: 571 pqisrc_destroy_taglist(softs,&softs->taglist); 572 err_taglist: 573 pqisrc_free_rcb(softs, softs->max_outstanding_io + 1); 574 err_rcb: 575 pqisrc_destroy_admin_queue(softs); 576 err_admin_queue: 577 os_free_intr_config(softs); 578 err_out: 579 DBG_FUNC("OUT failed\n"); 580 return PQI_STATUS_FAILURE; 581 } 582 583 /* */ 584 int 585 pqisrc_force_sis(pqisrc_softstate_t *softs) 586 { 587 int ret = PQI_STATUS_SUCCESS; 588 589 if (SIS_IS_KERNEL_PANIC(softs)) { 590 DBG_ERR("Controller FW is not running\n"); 591 return PQI_STATUS_FAILURE; 592 } 593 594 if (PQI_GET_CTRL_MODE(softs) == CTRL_SIS_MODE) { 595 return ret; 596 } 597 598 if (SIS_IS_KERNEL_UP(softs)) { 599 PQI_SAVE_CTRL_MODE(softs, CTRL_SIS_MODE); 600 return ret; 601 } 602 /* Disable interrupts ? */ 603 sis_disable_interrupt(softs); 604 605 /* reset pqi, this will delete queues */ 606 ret = pqi_reset(softs); 607 if (ret) { 608 return ret; 609 } 610 /* Re enable SIS */ 611 ret = pqisrc_reenable_sis(softs); 612 if (ret) { 613 return ret; 614 } 615 616 PQI_SAVE_CTRL_MODE(softs, CTRL_SIS_MODE); 617 618 return ret; 619 } 620 621 /* 5 mins timeout for quiesce */ 622 #define PQI_QUIESCE_TIMEOUT 300000 623 624 int 625 pqisrc_wait_for_cmnd_complete(pqisrc_softstate_t *softs) 626 { 627 628 int count = 0; 629 int ret = PQI_STATUS_SUCCESS; 630 631 DBG_NOTE("softs->taglist.num_elem : %u\n",softs->taglist.num_elem); 632 633 if (softs->taglist.num_elem == softs->max_outstanding_io) 634 return ret; 635 else { 636 DBG_WARN("%u commands pending\n", 637 softs->max_outstanding_io - softs->taglist.num_elem); 638 639 while(1) { 640 641 /* Since heartbeat timer stopped ,check for firmware status*/ 642 if (SIS_IS_KERNEL_PANIC(softs)) { 643 DBG_ERR("Controller FW is not running\n"); 644 return PQI_STATUS_FAILURE; 645 } 646 647 if (softs->taglist.num_elem != softs->max_outstanding_io) { 648 /* Sleep for 1 msec */ 649 OS_SLEEP(1000); 650 count++; 651 if(count % 1000 == 0) { 652 DBG_WARN("Waited for %d seconds\n", count/1000); 653 } 654 if (count >= PQI_QUIESCE_TIMEOUT) { 655 return PQI_STATUS_FAILURE; 656 } 657 continue; 658 } 659 break; 660 } 661 } 662 return ret; 663 } 664 665 void 666 pqisrc_complete_internal_cmds(pqisrc_softstate_t *softs) 667 { 668 669 int tag = 0; 670 rcb_t *rcb; 671 672 for (tag = 1; tag <= softs->max_outstanding_io; tag++) { 673 rcb = &softs->rcb[tag]; 674 if(rcb->req_pending && is_internal_req(rcb)) { 675 rcb->status = PQI_STATUS_TIMEOUT; 676 rcb->req_pending = false; 677 } 678 } 679 } 680 681 682 /* 683 * Uninitialize the resources used during PQI initialization. 684 */ 685 void 686 pqisrc_pqi_uninit(pqisrc_softstate_t *softs) 687 { 688 int ret; 689 690 DBG_FUNC("IN\n"); 691 692 /* Wait for any rescan to finish */ 693 pqisrc_wait_for_rescan_complete(softs); 694 695 /* Wait for commands to complete */ 696 ret = pqisrc_wait_for_cmnd_complete(softs); 697 698 /* disable and free the interrupt resources */ 699 os_destroy_intr(softs); 700 701 /* Complete all pending commands. */ 702 if(ret != PQI_STATUS_SUCCESS) { 703 pqisrc_complete_internal_cmds(softs); 704 os_complete_outstanding_cmds_nodevice(softs); 705 } 706 707 if(softs->devlist_lockcreated==true){ 708 os_uninit_spinlock(&softs->devlist_lock); 709 softs->devlist_lockcreated = false; 710 } 711 712 /* Free all queues */ 713 pqisrc_destroy_op_ib_queues(softs); 714 pqisrc_destroy_op_ob_queues(softs); 715 pqisrc_destroy_event_queue(softs); 716 717 /* Free rcb */ 718 pqisrc_free_rcb(softs, softs->max_outstanding_io + 1); 719 720 /* Free request id lists */ 721 pqisrc_destroy_taglist(softs,&softs->taglist); 722 723 /* Free Admin Queue */ 724 pqisrc_destroy_admin_queue(softs); 725 726 /* Switch back to SIS mode */ 727 if (pqisrc_force_sis(softs)) { 728 DBG_ERR("Failed to switch back the adapter to SIS mode!\n"); 729 } 730 731 DBG_FUNC("OUT\n"); 732 } 733 734 735 /* 736 * Function to do any sanity checks for OS macros 737 */ 738 void 739 sanity_check_os_behavior(pqisrc_softstate_t *softs) 740 { 741 #ifdef OS_ATOMIC64_INC 742 OS_ATOMIC64_T atomic_test_var = 0; 743 OS_ATOMIC64_T atomic_ret = 0; 744 745 atomic_ret = OS_ATOMIC64_INC(&atomic_test_var); 746 ASSERT(atomic_ret == 1); 747 748 atomic_ret = OS_ATOMIC64_INC(&atomic_test_var); 749 ASSERT(atomic_ret == 2); 750 751 atomic_ret = OS_ATOMIC64_DEC(&atomic_test_var); 752 ASSERT(atomic_ret == 1); 753 #else 754 DBG_INIT("OS needs to define/implement atomic macros\n"); 755 #endif 756 } 757 758 /* 759 * Function to initialize the adapter settings. 760 */ 761 int 762 pqisrc_init(pqisrc_softstate_t *softs) 763 { 764 int ret = 0; 765 uint32_t ctrl_type; 766 767 DBG_FUNC("IN\n"); 768 769 sanity_check_os_behavior(softs); 770 771 check_struct_sizes(); 772 773 /*Get verbose flags, defined in OS code XX_debug.h or so*/ 774 #ifdef DISABLE_ERR_RESP_VERBOSE 775 softs->err_resp_verbose = false; 776 #else 777 softs->err_resp_verbose = true; 778 #endif 779 780 /* prevent attachment of revA hardware. */ 781 ctrl_type = PQI_GET_CTRL_TYPE(softs); 782 if (ctrl_type == PQI_CTRL_PRODUCT_ID_GEN2_REV_A) { 783 DBG_ERR("adapter at B.D.F=%u.%u.%u: unsupported RevA card.\n", 784 softs->bus_id, softs->device_id, softs->func_id); 785 ret = PQI_STATUS_FAILURE; 786 goto err_out; 787 } 788 789 /* Increment the global adapter ID and tie it to this BDF */ 790 #ifdef OS_ATOMIC64_INC 791 static OS_ATOMIC64_T g_adapter_cnt = 0; 792 softs->adapter_num = (uint8_t)OS_ATOMIC64_INC(&g_adapter_cnt); 793 #else 794 static uint64_t g_adapter_cnt = 0; 795 softs->adapter_num = (uint8_t)++g_adapter_cnt; 796 #endif 797 DBG_NOTE("Initializing adapter %u\n", (uint32_t)softs->adapter_num); 798 799 ret = os_create_semaphore("scan_lock", 1, &softs->scan_lock); 800 if(ret != PQI_STATUS_SUCCESS){ 801 DBG_ERR(" Failed to initialize scan lock\n"); 802 goto err_out; 803 } 804 805 /* Init the Sync interface */ 806 ret = pqisrc_sis_init(softs); 807 if (ret) { 808 DBG_ERR("SIS Init failed with error %d\n", ret); 809 goto err_sis; 810 } 811 812 813 /* Init the PQI interface */ 814 ret = pqisrc_pqi_init(softs); 815 if (ret) { 816 DBG_ERR("PQI Init failed with error %d\n", ret); 817 goto err_pqi; 818 } 819 820 /* Setup interrupt */ 821 ret = os_setup_intr(softs); 822 if (ret) { 823 DBG_ERR("Interrupt setup failed with error %d\n", ret); 824 goto err_intr; 825 } 826 827 /* Report event configuration */ 828 ret = pqisrc_report_event_config(softs); 829 if(ret){ 830 DBG_ERR(" Failed to configure Report events\n"); 831 goto err_event; 832 } 833 834 /* Set event configuration*/ 835 ret = pqisrc_set_event_config(softs); 836 if(ret){ 837 DBG_ERR(" Failed to configure Set events\n"); 838 goto err_event; 839 } 840 841 /* Check for For PQI spanning */ 842 ret = pqisrc_get_ctrl_fw_version(softs); 843 if(ret){ 844 DBG_ERR(" Failed to get ctrl fw version\n"); 845 goto err_fw_version; 846 } 847 848 /* update driver version in to FW */ 849 ret = pqisrc_write_driver_version_to_host_wellness(softs); 850 if (ret) { 851 DBG_ERR(" Failed to update driver version in to FW\n"); 852 goto err_host_wellness; 853 } 854 855 /* Setup sense features */ 856 ret = pqisrc_QuerySenseFeatures(softs); 857 if (ret) { 858 DBG_ERR("Failed to get sense features\n"); 859 goto err_sense; 860 } 861 862 os_strlcpy(softs->devlist_lock_name, "devlist_lock", LOCKNAME_SIZE); 863 ret = os_init_spinlock(softs, &softs->devlist_lock, softs->devlist_lock_name); 864 if(ret){ 865 DBG_ERR(" Failed to initialize devlist_lock\n"); 866 softs->devlist_lockcreated=false; 867 goto err_lock; 868 } 869 softs->devlist_lockcreated = true; 870 871 /* Get the PQI configuration table to read heart-beat counter*/ 872 ret = pqisrc_process_config_table(softs); 873 if (ret) { 874 DBG_ERR("Failed to process PQI configuration table %d\n", ret); 875 goto err_config_tab; 876 } 877 878 softs->prev_heartbeat_count = CTRLR_HEARTBEAT_CNT(softs) - OS_FW_HEARTBEAT_TIMER_INTERVAL; 879 880 memset(softs->dev_list, 0, sizeof(*softs->dev_list)); 881 pqisrc_init_bitmap(softs); 882 883 DBG_FUNC("OUT\n"); 884 return ret; 885 886 err_config_tab: 887 if(softs->devlist_lockcreated==true){ 888 os_uninit_spinlock(&softs->devlist_lock); 889 softs->devlist_lockcreated = false; 890 } 891 err_lock: 892 err_fw_version: 893 err_event: 894 err_host_wellness: 895 err_intr: 896 err_sense: 897 pqisrc_pqi_uninit(softs); 898 err_pqi: 899 pqisrc_sis_uninit(softs); 900 err_sis: 901 os_destroy_semaphore(&softs->scan_lock); 902 err_out: 903 DBG_FUNC("OUT failed\n"); 904 return ret; 905 } 906 907 /* 908 * Write all data in the adapter's battery-backed cache to 909 * storage. 910 */ 911 int 912 pqisrc_flush_cache( pqisrc_softstate_t *softs, 913 enum pqisrc_flush_cache_event_type event_type) 914 { 915 int rval = PQI_STATUS_SUCCESS; 916 pqisrc_raid_req_t request; 917 pqisrc_bmic_flush_cache_t *flush_buff = NULL; 918 919 DBG_FUNC("IN\n"); 920 921 if (pqisrc_ctrl_offline(softs)) 922 return PQI_STATUS_FAILURE; 923 924 flush_buff = os_mem_alloc(softs, sizeof(pqisrc_bmic_flush_cache_t)); 925 if (!flush_buff) { 926 DBG_ERR("Failed to allocate memory for flush cache params\n"); 927 rval = PQI_STATUS_FAILURE; 928 return rval; 929 } 930 931 flush_buff->halt_event = event_type; 932 933 memset(&request, 0, sizeof(request)); 934 935 request.data_direction = SOP_DATA_DIR_FROM_DEVICE; 936 request.cmd.bmic_cdb.op_code = BMIC_WRITE; 937 request.cmd.bmic_cdb.cmd = BMIC_CACHE_FLUSH; 938 request.cmd.bmic_cdb.xfer_len = BE_16(sizeof(*flush_buff)); 939 940 rval = pqisrc_prepare_send_ctrlr_request(softs, &request, flush_buff, sizeof(*flush_buff)); 941 942 if (rval) { 943 DBG_ERR("error in build send raid req ret=%d\n", rval); 944 } 945 946 os_mem_free(softs, (void *)flush_buff, sizeof(pqisrc_bmic_flush_cache_t)); 947 948 DBG_FUNC("OUT\n"); 949 950 return rval; 951 } 952 953 /* 954 * Uninitialize the adapter. 955 */ 956 void 957 pqisrc_uninit(pqisrc_softstate_t *softs) 958 { 959 DBG_FUNC("IN\n"); 960 961 pqisrc_pqi_uninit(softs); 962 963 pqisrc_sis_uninit(softs); 964 965 os_destroy_semaphore(&softs->scan_lock); 966 967 pqisrc_cleanup_devices(softs); 968 969 DBG_FUNC("OUT\n"); 970 } 971