1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Broadcom MPI3 Storage Controllers 4 * 5 * Copyright (C) 2017-2023 Broadcom Inc. 6 * (mailto: mpi3mr-linuxdrv.pdl@broadcom.com) 7 * 8 */ 9 10 #include "mpi3mr.h" 11 #include <linux/io-64-nonatomic-lo-hi.h> 12 #include <linux/sched/mm.h> 13 14 static int 15 mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type, u16 reset_reason); 16 static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc); 17 static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc, 18 struct mpi3_ioc_facts_data *facts_data); 19 static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc, 20 struct mpi3mr_drv_cmd *drv_cmd); 21 static int mpi3mr_check_op_admin_proc(struct mpi3mr_ioc *mrioc); 22 static int poll_queues; 23 module_param(poll_queues, int, 0444); 24 MODULE_PARM_DESC(poll_queues, "Number of queues for io_uring poll mode. (Range 1 - 126)"); 25 static bool threaded_isr_poll = true; 26 module_param(threaded_isr_poll, bool, 0444); 27 MODULE_PARM_DESC(threaded_isr_poll, 28 "Enablement of IRQ polling thread (default=true)"); 29 30 #if defined(writeq) && defined(CONFIG_64BIT) 31 static inline void mpi3mr_writeq(__u64 b, void __iomem *addr, 32 spinlock_t *write_queue_lock) 33 { 34 writeq(b, addr); 35 } 36 #else 37 static inline void mpi3mr_writeq(__u64 b, void __iomem *addr, 38 spinlock_t *write_queue_lock) 39 { 40 __u64 data_out = b; 41 unsigned long flags; 42 43 spin_lock_irqsave(write_queue_lock, flags); 44 writel((u32)(data_out), addr); 45 writel((u32)(data_out >> 32), (addr + 4)); 46 spin_unlock_irqrestore(write_queue_lock, flags); 47 } 48 #endif 49 50 static inline bool 51 mpi3mr_check_req_qfull(struct op_req_qinfo *op_req_q) 52 { 53 u16 pi, ci, max_entries; 54 bool is_qfull = false; 55 56 pi = op_req_q->pi; 57 ci = READ_ONCE(op_req_q->ci); 58 max_entries = op_req_q->num_requests; 59 60 if ((ci == (pi + 1)) || ((!ci) && (pi == (max_entries - 1)))) 61 is_qfull = true; 62 63 return is_qfull; 64 } 65 66 static void mpi3mr_sync_irqs(struct mpi3mr_ioc *mrioc) 67 { 68 u16 i, max_vectors; 69 70 max_vectors = mrioc->intr_info_count; 71 72 for (i = 0; i < max_vectors; i++) 73 synchronize_irq(pci_irq_vector(mrioc->pdev, i)); 74 } 75 76 void mpi3mr_ioc_disable_intr(struct mpi3mr_ioc *mrioc) 77 { 78 mrioc->intr_enabled = 0; 79 mpi3mr_sync_irqs(mrioc); 80 } 81 82 void mpi3mr_ioc_enable_intr(struct mpi3mr_ioc *mrioc) 83 { 84 mrioc->intr_enabled = 1; 85 } 86 87 static void mpi3mr_cleanup_isr(struct mpi3mr_ioc *mrioc) 88 { 89 u16 i; 90 91 mpi3mr_ioc_disable_intr(mrioc); 92 93 if (!mrioc->intr_info) 94 return; 95 96 for (i = 0; i < mrioc->intr_info_count; i++) 97 free_irq(pci_irq_vector(mrioc->pdev, i), 98 (mrioc->intr_info + i)); 99 100 kfree(mrioc->intr_info); 101 mrioc->intr_info = NULL; 102 mrioc->intr_info_count = 0; 103 mrioc->is_intr_info_set = false; 104 pci_free_irq_vectors(mrioc->pdev); 105 } 106 107 void mpi3mr_add_sg_single(void *paddr, u8 flags, u32 length, 108 dma_addr_t dma_addr) 109 { 110 struct mpi3_sge_common *sgel = paddr; 111 112 sgel->flags = flags; 113 sgel->length = cpu_to_le32(length); 114 sgel->address = cpu_to_le64(dma_addr); 115 } 116 117 void mpi3mr_build_zero_len_sge(void *paddr) 118 { 119 u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST; 120 121 mpi3mr_add_sg_single(paddr, sgl_flags, 0, -1); 122 } 123 124 void *mpi3mr_get_reply_virt_addr(struct mpi3mr_ioc *mrioc, 125 dma_addr_t phys_addr) 126 { 127 u64 offset; 128 129 if (!phys_addr) 130 return NULL; 131 132 offset = phys_addr - mrioc->reply_buf_dma; 133 134 if ((phys_addr < mrioc->reply_buf_dma) || 135 (phys_addr > mrioc->reply_buf_dma_max_address - mrioc->reply_sz) || 136 do_div(offset, mrioc->reply_sz)) 137 return NULL; 138 139 return mrioc->reply_buf + (phys_addr - mrioc->reply_buf_dma); 140 } 141 142 void *mpi3mr_get_sensebuf_virt_addr(struct mpi3mr_ioc *mrioc, 143 dma_addr_t phys_addr) 144 { 145 u64 offset; 146 147 if (!phys_addr) 148 return NULL; 149 150 offset = phys_addr - mrioc->sense_buf_dma; 151 152 if (phys_addr < mrioc->sense_buf_dma || 153 (phys_addr > mrioc->sense_buf_dma + 154 (mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ) - MPI3MR_SENSE_BUF_SZ) || 155 do_div(offset, MPI3MR_SENSE_BUF_SZ)) 156 return NULL; 157 158 return mrioc->sense_buf + (phys_addr - mrioc->sense_buf_dma); 159 } 160 161 static void mpi3mr_repost_reply_buf(struct mpi3mr_ioc *mrioc, 162 u64 reply_dma) 163 { 164 u32 old_idx = 0; 165 unsigned long flags; 166 167 spin_lock_irqsave(&mrioc->reply_free_queue_lock, flags); 168 old_idx = mrioc->reply_free_queue_host_index; 169 mrioc->reply_free_queue_host_index = ( 170 (mrioc->reply_free_queue_host_index == 171 (mrioc->reply_free_qsz - 1)) ? 0 : 172 (mrioc->reply_free_queue_host_index + 1)); 173 mrioc->reply_free_q[old_idx] = cpu_to_le64(reply_dma); 174 writel(mrioc->reply_free_queue_host_index, 175 &mrioc->sysif_regs->reply_free_host_index); 176 spin_unlock_irqrestore(&mrioc->reply_free_queue_lock, flags); 177 } 178 179 void mpi3mr_repost_sense_buf(struct mpi3mr_ioc *mrioc, 180 u64 sense_buf_dma) 181 { 182 u32 old_idx = 0; 183 unsigned long flags; 184 185 spin_lock_irqsave(&mrioc->sbq_lock, flags); 186 old_idx = mrioc->sbq_host_index; 187 mrioc->sbq_host_index = ((mrioc->sbq_host_index == 188 (mrioc->sense_buf_q_sz - 1)) ? 0 : 189 (mrioc->sbq_host_index + 1)); 190 mrioc->sense_buf_q[old_idx] = cpu_to_le64(sense_buf_dma); 191 writel(mrioc->sbq_host_index, 192 &mrioc->sysif_regs->sense_buffer_free_host_index); 193 spin_unlock_irqrestore(&mrioc->sbq_lock, flags); 194 } 195 196 static void mpi3mr_print_event_data(struct mpi3mr_ioc *mrioc, 197 struct mpi3_event_notification_reply *event_reply) 198 { 199 char *desc = NULL; 200 u16 event; 201 202 if (!(mrioc->logging_level & MPI3_DEBUG_EVENT)) 203 return; 204 205 event = event_reply->event; 206 207 switch (event) { 208 case MPI3_EVENT_LOG_DATA: 209 desc = "Log Data"; 210 break; 211 case MPI3_EVENT_CHANGE: 212 desc = "Event Change"; 213 break; 214 case MPI3_EVENT_GPIO_INTERRUPT: 215 desc = "GPIO Interrupt"; 216 break; 217 case MPI3_EVENT_CABLE_MGMT: 218 desc = "Cable Management"; 219 break; 220 case MPI3_EVENT_ENERGY_PACK_CHANGE: 221 desc = "Energy Pack Change"; 222 break; 223 case MPI3_EVENT_DEVICE_ADDED: 224 { 225 struct mpi3_device_page0 *event_data = 226 (struct mpi3_device_page0 *)event_reply->event_data; 227 ioc_info(mrioc, "Device Added: dev=0x%04x Form=0x%x\n", 228 event_data->dev_handle, event_data->device_form); 229 return; 230 } 231 case MPI3_EVENT_DEVICE_INFO_CHANGED: 232 { 233 struct mpi3_device_page0 *event_data = 234 (struct mpi3_device_page0 *)event_reply->event_data; 235 ioc_info(mrioc, "Device Info Changed: dev=0x%04x Form=0x%x\n", 236 event_data->dev_handle, event_data->device_form); 237 return; 238 } 239 case MPI3_EVENT_DEVICE_STATUS_CHANGE: 240 { 241 struct mpi3_event_data_device_status_change *event_data = 242 (struct mpi3_event_data_device_status_change *)event_reply->event_data; 243 ioc_info(mrioc, "Device status Change: dev=0x%04x RC=0x%x\n", 244 event_data->dev_handle, event_data->reason_code); 245 return; 246 } 247 case MPI3_EVENT_SAS_DISCOVERY: 248 { 249 struct mpi3_event_data_sas_discovery *event_data = 250 (struct mpi3_event_data_sas_discovery *)event_reply->event_data; 251 ioc_info(mrioc, "SAS Discovery: (%s) status (0x%08x)\n", 252 (event_data->reason_code == MPI3_EVENT_SAS_DISC_RC_STARTED) ? 253 "start" : "stop", 254 le32_to_cpu(event_data->discovery_status)); 255 return; 256 } 257 case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE: 258 desc = "SAS Broadcast Primitive"; 259 break; 260 case MPI3_EVENT_SAS_NOTIFY_PRIMITIVE: 261 desc = "SAS Notify Primitive"; 262 break; 263 case MPI3_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE: 264 desc = "SAS Init Device Status Change"; 265 break; 266 case MPI3_EVENT_SAS_INIT_TABLE_OVERFLOW: 267 desc = "SAS Init Table Overflow"; 268 break; 269 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST: 270 desc = "SAS Topology Change List"; 271 break; 272 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE: 273 desc = "Enclosure Device Status Change"; 274 break; 275 case MPI3_EVENT_ENCL_DEVICE_ADDED: 276 desc = "Enclosure Added"; 277 break; 278 case MPI3_EVENT_HARD_RESET_RECEIVED: 279 desc = "Hard Reset Received"; 280 break; 281 case MPI3_EVENT_SAS_PHY_COUNTER: 282 desc = "SAS PHY Counter"; 283 break; 284 case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR: 285 desc = "SAS Device Discovery Error"; 286 break; 287 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST: 288 desc = "PCIE Topology Change List"; 289 break; 290 case MPI3_EVENT_PCIE_ENUMERATION: 291 { 292 struct mpi3_event_data_pcie_enumeration *event_data = 293 (struct mpi3_event_data_pcie_enumeration *)event_reply->event_data; 294 ioc_info(mrioc, "PCIE Enumeration: (%s)", 295 (event_data->reason_code == 296 MPI3_EVENT_PCIE_ENUM_RC_STARTED) ? "start" : "stop"); 297 if (event_data->enumeration_status) 298 ioc_info(mrioc, "enumeration_status(0x%08x)\n", 299 le32_to_cpu(event_data->enumeration_status)); 300 return; 301 } 302 case MPI3_EVENT_PREPARE_FOR_RESET: 303 desc = "Prepare For Reset"; 304 break; 305 case MPI3_EVENT_DIAGNOSTIC_BUFFER_STATUS_CHANGE: 306 desc = "Diagnostic Buffer Status Change"; 307 break; 308 } 309 310 if (!desc) 311 return; 312 313 ioc_info(mrioc, "%s\n", desc); 314 } 315 316 static void mpi3mr_handle_events(struct mpi3mr_ioc *mrioc, 317 struct mpi3_default_reply *def_reply) 318 { 319 struct mpi3_event_notification_reply *event_reply = 320 (struct mpi3_event_notification_reply *)def_reply; 321 322 mrioc->change_count = le16_to_cpu(event_reply->ioc_change_count); 323 mpi3mr_print_event_data(mrioc, event_reply); 324 mpi3mr_os_handle_events(mrioc, event_reply); 325 } 326 327 static struct mpi3mr_drv_cmd * 328 mpi3mr_get_drv_cmd(struct mpi3mr_ioc *mrioc, u16 host_tag, 329 struct mpi3_default_reply *def_reply) 330 { 331 u16 idx; 332 333 switch (host_tag) { 334 case MPI3MR_HOSTTAG_INITCMDS: 335 return &mrioc->init_cmds; 336 case MPI3MR_HOSTTAG_CFG_CMDS: 337 return &mrioc->cfg_cmds; 338 case MPI3MR_HOSTTAG_BSG_CMDS: 339 return &mrioc->bsg_cmds; 340 case MPI3MR_HOSTTAG_BLK_TMS: 341 return &mrioc->host_tm_cmds; 342 case MPI3MR_HOSTTAG_PEL_ABORT: 343 return &mrioc->pel_abort_cmd; 344 case MPI3MR_HOSTTAG_PEL_WAIT: 345 return &mrioc->pel_cmds; 346 case MPI3MR_HOSTTAG_TRANSPORT_CMDS: 347 return &mrioc->transport_cmds; 348 case MPI3MR_HOSTTAG_INVALID: 349 if (def_reply && def_reply->function == 350 MPI3_FUNCTION_EVENT_NOTIFICATION) 351 mpi3mr_handle_events(mrioc, def_reply); 352 return NULL; 353 default: 354 break; 355 } 356 if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN && 357 host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX) { 358 idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN; 359 return &mrioc->dev_rmhs_cmds[idx]; 360 } 361 362 if (host_tag >= MPI3MR_HOSTTAG_EVTACKCMD_MIN && 363 host_tag <= MPI3MR_HOSTTAG_EVTACKCMD_MAX) { 364 idx = host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN; 365 return &mrioc->evtack_cmds[idx]; 366 } 367 368 return NULL; 369 } 370 371 static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc, 372 struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma) 373 { 374 u16 reply_desc_type, host_tag = 0; 375 u16 ioc_status = MPI3_IOCSTATUS_SUCCESS; 376 u16 masked_ioc_status = MPI3_IOCSTATUS_SUCCESS; 377 u32 ioc_loginfo = 0, sense_count = 0; 378 struct mpi3_status_reply_descriptor *status_desc; 379 struct mpi3_address_reply_descriptor *addr_desc; 380 struct mpi3_success_reply_descriptor *success_desc; 381 struct mpi3_default_reply *def_reply = NULL; 382 struct mpi3mr_drv_cmd *cmdptr = NULL; 383 struct mpi3_scsi_io_reply *scsi_reply; 384 struct scsi_sense_hdr sshdr; 385 u8 *sense_buf = NULL; 386 387 *reply_dma = 0; 388 reply_desc_type = le16_to_cpu(reply_desc->reply_flags) & 389 MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK; 390 switch (reply_desc_type) { 391 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS: 392 status_desc = (struct mpi3_status_reply_descriptor *)reply_desc; 393 host_tag = le16_to_cpu(status_desc->host_tag); 394 ioc_status = le16_to_cpu(status_desc->ioc_status); 395 if (ioc_status & 396 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL) 397 ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info); 398 masked_ioc_status = ioc_status & MPI3_IOCSTATUS_STATUS_MASK; 399 mpi3mr_reply_trigger(mrioc, masked_ioc_status, ioc_loginfo); 400 break; 401 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY: 402 addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc; 403 *reply_dma = le64_to_cpu(addr_desc->reply_frame_address); 404 def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma); 405 if (!def_reply) 406 goto out; 407 host_tag = le16_to_cpu(def_reply->host_tag); 408 ioc_status = le16_to_cpu(def_reply->ioc_status); 409 if (ioc_status & 410 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL) 411 ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info); 412 masked_ioc_status = ioc_status & MPI3_IOCSTATUS_STATUS_MASK; 413 if (def_reply->function == MPI3_FUNCTION_SCSI_IO) { 414 scsi_reply = (struct mpi3_scsi_io_reply *)def_reply; 415 sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc, 416 le64_to_cpu(scsi_reply->sense_data_buffer_address)); 417 sense_count = le32_to_cpu(scsi_reply->sense_count); 418 if (sense_buf) { 419 scsi_normalize_sense(sense_buf, sense_count, 420 &sshdr); 421 mpi3mr_scsisense_trigger(mrioc, sshdr.sense_key, 422 scsi_sense_asc(&sshdr), 423 scsi_sense_ascq(&sshdr)); 424 } 425 } 426 mpi3mr_reply_trigger(mrioc, masked_ioc_status, ioc_loginfo); 427 break; 428 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS: 429 success_desc = (struct mpi3_success_reply_descriptor *)reply_desc; 430 host_tag = le16_to_cpu(success_desc->host_tag); 431 break; 432 default: 433 break; 434 } 435 436 cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply); 437 if (cmdptr) { 438 if (cmdptr->state & MPI3MR_CMD_PENDING) { 439 cmdptr->state |= MPI3MR_CMD_COMPLETE; 440 cmdptr->ioc_loginfo = ioc_loginfo; 441 if (host_tag == MPI3MR_HOSTTAG_BSG_CMDS) 442 cmdptr->ioc_status = ioc_status; 443 else 444 cmdptr->ioc_status = masked_ioc_status; 445 cmdptr->state &= ~MPI3MR_CMD_PENDING; 446 if (def_reply) { 447 cmdptr->state |= MPI3MR_CMD_REPLY_VALID; 448 memcpy((u8 *)cmdptr->reply, (u8 *)def_reply, 449 mrioc->reply_sz); 450 } 451 if (sense_buf && cmdptr->sensebuf) { 452 cmdptr->is_sense = 1; 453 memcpy(cmdptr->sensebuf, sense_buf, 454 MPI3MR_SENSE_BUF_SZ); 455 } 456 if (cmdptr->is_waiting) { 457 cmdptr->is_waiting = 0; 458 complete(&cmdptr->done); 459 } else if (cmdptr->callback) 460 cmdptr->callback(mrioc, cmdptr); 461 } 462 } 463 out: 464 if (sense_buf) 465 mpi3mr_repost_sense_buf(mrioc, 466 le64_to_cpu(scsi_reply->sense_data_buffer_address)); 467 } 468 469 int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc) 470 { 471 u32 exp_phase = mrioc->admin_reply_ephase; 472 u32 admin_reply_ci = mrioc->admin_reply_ci; 473 u32 num_admin_replies = 0; 474 u64 reply_dma = 0; 475 u16 threshold_comps = 0; 476 struct mpi3_default_reply_descriptor *reply_desc; 477 478 if (!atomic_add_unless(&mrioc->admin_reply_q_in_use, 1, 1)) { 479 atomic_inc(&mrioc->admin_pend_isr); 480 return 0; 481 } 482 483 atomic_set(&mrioc->admin_pend_isr, 0); 484 reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base + 485 admin_reply_ci; 486 487 if ((le16_to_cpu(reply_desc->reply_flags) & 488 MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) { 489 atomic_dec(&mrioc->admin_reply_q_in_use); 490 return 0; 491 } 492 493 /* 494 * Ensure that the descriptor payload is read only after 495 * the phase bit check is complete. 496 */ 497 dma_rmb(); 498 499 do { 500 if (mrioc->unrecoverable || mrioc->io_admin_reset_sync) 501 break; 502 503 mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci); 504 mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma); 505 if (reply_dma) 506 mpi3mr_repost_reply_buf(mrioc, reply_dma); 507 num_admin_replies++; 508 threshold_comps++; 509 if (++admin_reply_ci == mrioc->num_admin_replies) { 510 admin_reply_ci = 0; 511 exp_phase ^= 1; 512 } 513 reply_desc = 514 (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base + 515 admin_reply_ci; 516 if ((le16_to_cpu(reply_desc->reply_flags) & 517 MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) 518 break; 519 520 /* 521 * Ensure that the descriptor payload is read only after 522 * the phase bit check is complete. 523 */ 524 dma_rmb(); 525 526 if (threshold_comps == MPI3MR_THRESHOLD_REPLY_COUNT) { 527 writel(admin_reply_ci, 528 &mrioc->sysif_regs->admin_reply_queue_ci); 529 threshold_comps = 0; 530 } 531 } while (1); 532 533 writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci); 534 mrioc->admin_reply_ci = admin_reply_ci; 535 mrioc->admin_reply_ephase = exp_phase; 536 atomic_dec(&mrioc->admin_reply_q_in_use); 537 538 return num_admin_replies; 539 } 540 541 /** 542 * mpi3mr_get_reply_desc - get reply descriptor frame corresponding to 543 * queue's consumer index from operational reply descriptor queue. 544 * @op_reply_q: op_reply_qinfo object 545 * @reply_ci: operational reply descriptor's queue consumer index 546 * 547 * Returns: reply descriptor frame address 548 */ 549 static inline struct mpi3_default_reply_descriptor * 550 mpi3mr_get_reply_desc(struct op_reply_qinfo *op_reply_q, u32 reply_ci) 551 { 552 void *segment_base_addr; 553 struct segments *segments = op_reply_q->q_segments; 554 struct mpi3_default_reply_descriptor *reply_desc = NULL; 555 556 segment_base_addr = 557 segments[reply_ci / op_reply_q->segment_qd].segment; 558 reply_desc = (struct mpi3_default_reply_descriptor *)segment_base_addr + 559 (reply_ci % op_reply_q->segment_qd); 560 return reply_desc; 561 } 562 563 /** 564 * mpi3mr_process_op_reply_q - Operational reply queue handler 565 * @mrioc: Adapter instance reference 566 * @op_reply_q: Operational reply queue info 567 * 568 * Checks the specific operational reply queue and drains the 569 * reply queue entries until the queue is empty and process the 570 * individual reply descriptors. 571 * 572 * Return: 0 if queue is already processed,or number of reply 573 * descriptors processed. 574 */ 575 int mpi3mr_process_op_reply_q(struct mpi3mr_ioc *mrioc, 576 struct op_reply_qinfo *op_reply_q) 577 { 578 struct op_req_qinfo *op_req_q; 579 u32 exp_phase; 580 u32 reply_ci; 581 u32 num_op_reply = 0; 582 u64 reply_dma = 0; 583 struct mpi3_default_reply_descriptor *reply_desc; 584 u16 req_q_idx = 0, reply_qidx, threshold_comps = 0; 585 586 if (!op_reply_q) 587 return 0; 588 589 reply_qidx = op_reply_q->qid - 1; 590 591 if (!atomic_add_unless(&op_reply_q->in_use, 1, 1)) 592 return 0; 593 594 exp_phase = op_reply_q->ephase; 595 reply_ci = op_reply_q->ci; 596 597 reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci); 598 if ((le16_to_cpu(reply_desc->reply_flags) & 599 MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) { 600 /* Recheck under in_use before releasing, to avoid a reclaim race */ 601 dma_rmb(); 602 if ((le16_to_cpu(reply_desc->reply_flags) & 603 MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) == exp_phase) 604 goto process_desc; 605 atomic_dec(&op_reply_q->in_use); 606 return 0; 607 } 608 process_desc: 609 /* 610 * Ensure that the descriptor payload is read only after 611 * the phase bit check is complete. 612 */ 613 dma_rmb(); 614 615 do { 616 if (mrioc->unrecoverable || mrioc->io_admin_reset_sync) 617 break; 618 619 req_q_idx = le16_to_cpu(reply_desc->request_queue_id) - 1; 620 621 if (unlikely(req_q_idx >= mrioc->num_op_req_q)) { 622 ioc_err(mrioc, "Invalid request queue id %d, skipping reply\n", 623 req_q_idx + 1); 624 goto next_reply; 625 } 626 627 op_req_q = &mrioc->req_qinfo[req_q_idx]; 628 629 WRITE_ONCE(op_req_q->ci, le16_to_cpu(reply_desc->request_queue_ci)); 630 mpi3mr_process_op_reply_desc(mrioc, reply_desc, &reply_dma, 631 reply_qidx); 632 633 if (reply_dma) 634 mpi3mr_repost_reply_buf(mrioc, reply_dma); 635 threshold_comps++; 636 next_reply: 637 num_op_reply++; 638 639 if (++reply_ci == op_reply_q->num_replies) { 640 reply_ci = 0; 641 exp_phase ^= 1; 642 } 643 644 reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci); 645 646 if ((le16_to_cpu(reply_desc->reply_flags) & 647 MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) { 648 dma_rmb(); 649 if ((le16_to_cpu(reply_desc->reply_flags) & 650 MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) == exp_phase) 651 goto reply_ready; 652 break; 653 } 654 reply_ready: 655 /* 656 * Ensure that the descriptor payload is read only after 657 * the phase bit check is complete. 658 */ 659 dma_rmb(); 660 #ifndef CONFIG_PREEMPT_RT 661 /* 662 * Exit completion loop to avoid CPU lockup 663 * Ensure remaining completion happens from threaded ISR. 664 */ 665 if ((num_op_reply > mrioc->max_host_ios) && 666 (threaded_isr_poll == true)) { 667 op_reply_q->enable_irq_poll = true; 668 break; 669 } 670 #endif 671 if (threshold_comps == MPI3MR_THRESHOLD_REPLY_COUNT) { 672 writel(reply_ci, 673 &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index); 674 atomic_sub(threshold_comps, &op_reply_q->pend_ios); 675 threshold_comps = 0; 676 } 677 } while (1); 678 679 writel(reply_ci, 680 &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index); 681 op_reply_q->ci = reply_ci; 682 op_reply_q->ephase = exp_phase; 683 atomic_sub(threshold_comps, &op_reply_q->pend_ios); 684 atomic_dec(&op_reply_q->in_use); 685 return num_op_reply; 686 } 687 688 /** 689 * mpi3mr_blk_mq_poll - Operational reply queue handler 690 * @shost: SCSI Host reference 691 * @queue_num: Request queue number (w.r.t OS it is hardware context number) 692 * 693 * Checks the specific operational reply queue and drains the 694 * reply queue entries until the queue is empty and process the 695 * individual reply descriptors. 696 * 697 * Return: 0 if queue is already processed,or number of reply 698 * descriptors processed. 699 */ 700 int mpi3mr_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num) 701 { 702 int num_entries = 0; 703 struct mpi3mr_ioc *mrioc; 704 705 mrioc = (struct mpi3mr_ioc *)shost->hostdata; 706 707 if ((mrioc->reset_in_progress || mrioc->prepare_for_reset || 708 mrioc->unrecoverable || mrioc->pci_err_recovery)) 709 return 0; 710 711 num_entries = mpi3mr_process_op_reply_q(mrioc, 712 &mrioc->op_reply_qinfo[queue_num]); 713 714 return num_entries; 715 } 716 717 static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata) 718 { 719 struct mpi3mr_intr_info *intr_info = privdata; 720 struct mpi3mr_ioc *mrioc; 721 struct op_reply_qinfo *op_reply_q; 722 u16 midx; 723 u32 num_admin_replies = 0, num_op_reply = 0; 724 725 if (!intr_info) 726 return IRQ_NONE; 727 728 mrioc = intr_info->mrioc; 729 730 if (!mrioc->intr_enabled) 731 return IRQ_NONE; 732 733 midx = intr_info->msix_index; 734 735 if (!midx) 736 num_admin_replies = mpi3mr_process_admin_reply_q(mrioc); 737 op_reply_q = READ_ONCE(intr_info->op_reply_q); 738 if (op_reply_q) 739 num_op_reply = mpi3mr_process_op_reply_q(mrioc, op_reply_q); 740 741 if (num_admin_replies || num_op_reply) 742 return IRQ_HANDLED; 743 else 744 return IRQ_NONE; 745 } 746 747 #ifndef CONFIG_PREEMPT_RT 748 749 static irqreturn_t mpi3mr_isr(int irq, void *privdata) 750 { 751 struct mpi3mr_intr_info *intr_info = privdata; 752 struct op_reply_qinfo *op_reply_q; 753 int ret; 754 755 if (!intr_info) 756 return IRQ_NONE; 757 758 /* Call primary ISR routine */ 759 ret = mpi3mr_isr_primary(irq, privdata); 760 761 /* 762 * If more IOs are expected, schedule IRQ polling thread. 763 * Otherwise exit from ISR. 764 */ 765 op_reply_q = READ_ONCE(intr_info->op_reply_q); 766 if ((threaded_isr_poll == false) || !op_reply_q) 767 return ret; 768 769 if (!op_reply_q->enable_irq_poll || 770 !atomic_read(&op_reply_q->pend_ios)) 771 return ret; 772 773 disable_irq_nosync(intr_info->os_irq); 774 775 return IRQ_WAKE_THREAD; 776 } 777 778 /** 779 * mpi3mr_isr_poll - Reply queue polling routine 780 * @irq: IRQ 781 * @privdata: Interrupt info 782 * 783 * poll for pending I/O completions in a loop until pending I/Os 784 * present or controller queue depth I/Os are processed. 785 * 786 * Return: IRQ_NONE or IRQ_HANDLED 787 */ 788 static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata) 789 { 790 struct mpi3mr_intr_info *intr_info = privdata; 791 struct mpi3mr_ioc *mrioc; 792 struct op_reply_qinfo *op_reply_q; 793 u16 midx; 794 u32 num_op_reply = 0; 795 796 if (!intr_info) 797 return IRQ_NONE; 798 799 op_reply_q = READ_ONCE(intr_info->op_reply_q); 800 if (!op_reply_q) { 801 enable_irq(intr_info->os_irq); 802 return IRQ_HANDLED; 803 } 804 805 mrioc = intr_info->mrioc; 806 midx = intr_info->msix_index; 807 808 /* Poll for pending IOs completions */ 809 do { 810 if (!mrioc->intr_enabled || mrioc->unrecoverable) 811 break; 812 813 op_reply_q = READ_ONCE(intr_info->op_reply_q); 814 if (!op_reply_q) 815 break; 816 817 if (!midx) 818 mpi3mr_process_admin_reply_q(mrioc); 819 num_op_reply += 820 mpi3mr_process_op_reply_q(mrioc, op_reply_q); 821 if (!atomic_read(&op_reply_q->pend_ios)) 822 break; 823 824 usleep_range(MPI3MR_IRQ_POLL_SLEEP, 10 * MPI3MR_IRQ_POLL_SLEEP); 825 826 } while (num_op_reply < mrioc->max_host_ios); 827 828 if (op_reply_q) 829 op_reply_q->enable_irq_poll = false; 830 enable_irq(intr_info->os_irq); 831 832 return IRQ_HANDLED; 833 } 834 835 #endif 836 837 /** 838 * mpi3mr_request_irq - Request IRQ and register ISR 839 * @mrioc: Adapter instance reference 840 * @index: IRQ vector index 841 * 842 * Request threaded ISR with primary ISR and secondary 843 * 844 * Return: 0 on success and non zero on failures. 845 */ 846 static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index) 847 { 848 struct pci_dev *pdev = mrioc->pdev; 849 struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index; 850 int retval = 0; 851 852 intr_info->mrioc = mrioc; 853 intr_info->msix_index = index; 854 intr_info->op_reply_q = NULL; 855 856 scnprintf(intr_info->name, MPI3MR_NAME_LENGTH, 857 "%.32s%d-msix%u", mrioc->driver_name, mrioc->id, index); 858 859 #ifndef CONFIG_PREEMPT_RT 860 retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr, 861 mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info); 862 #else 863 retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr_primary, 864 NULL, IRQF_SHARED, intr_info->name, intr_info); 865 #endif 866 if (retval) { 867 ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n", 868 intr_info->name, pci_irq_vector(pdev, index)); 869 return retval; 870 } 871 872 intr_info->os_irq = pci_irq_vector(pdev, index); 873 return retval; 874 } 875 876 static void mpi3mr_calc_poll_queues(struct mpi3mr_ioc *mrioc, u16 max_vectors) 877 { 878 if (!mrioc->requested_poll_qcount) 879 return; 880 881 /* Reserved for Admin and Default Queue */ 882 if (max_vectors > 2 && 883 (mrioc->requested_poll_qcount < max_vectors - 2)) { 884 ioc_info(mrioc, 885 "enabled polled queues (%d) msix (%d)\n", 886 mrioc->requested_poll_qcount, max_vectors); 887 } else { 888 ioc_info(mrioc, 889 "disabled polled queues (%d) msix (%d) because of no resources for default queue\n", 890 mrioc->requested_poll_qcount, max_vectors); 891 mrioc->requested_poll_qcount = 0; 892 } 893 } 894 895 /** 896 * mpi3mr_setup_isr - Setup ISR for the controller 897 * @mrioc: Adapter instance reference 898 * @setup_one: Request one IRQ or more 899 * 900 * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR 901 * 902 * Return: 0 on success and non zero on failures. 903 */ 904 static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one) 905 { 906 unsigned int irq_flags = PCI_IRQ_MSIX; 907 int max_vectors, min_vec; 908 int retval; 909 int i; 910 struct irq_affinity desc = { .pre_vectors = 1, .post_vectors = 1 }; 911 912 if (mrioc->is_intr_info_set) 913 return 0; 914 915 mpi3mr_cleanup_isr(mrioc); 916 917 if (setup_one || reset_devices) { 918 max_vectors = 1; 919 retval = pci_alloc_irq_vectors(mrioc->pdev, 920 1, max_vectors, irq_flags); 921 if (retval < 0) { 922 ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n", 923 retval); 924 goto out_failed; 925 } 926 } else { 927 max_vectors = 928 min_t(int, mrioc->cpu_count + 1 + 929 mrioc->requested_poll_qcount, mrioc->msix_count); 930 931 mpi3mr_calc_poll_queues(mrioc, max_vectors); 932 933 ioc_info(mrioc, 934 "MSI-X vectors supported: %d, no of cores: %d,", 935 mrioc->msix_count, mrioc->cpu_count); 936 ioc_info(mrioc, 937 "MSI-x vectors requested: %d poll_queues %d\n", 938 max_vectors, mrioc->requested_poll_qcount); 939 940 desc.post_vectors = mrioc->requested_poll_qcount; 941 min_vec = desc.pre_vectors + desc.post_vectors; 942 irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES; 943 944 retval = pci_alloc_irq_vectors_affinity(mrioc->pdev, 945 min_vec, max_vectors, irq_flags, &desc); 946 947 if (retval < 0) { 948 ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n", 949 retval); 950 goto out_failed; 951 } 952 953 954 /* 955 * If only one MSI-x is allocated, then MSI-x 0 will be shared 956 * between Admin queue and operational queue 957 */ 958 if (retval == min_vec) 959 mrioc->op_reply_q_offset = 0; 960 else if (retval != (max_vectors)) { 961 ioc_info(mrioc, 962 "allocated vectors (%d) are less than configured (%d)\n", 963 retval, max_vectors); 964 } 965 966 max_vectors = retval; 967 mrioc->op_reply_q_offset = (max_vectors > 1) ? 1 : 0; 968 969 mpi3mr_calc_poll_queues(mrioc, max_vectors); 970 971 } 972 973 mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors, 974 GFP_KERNEL); 975 if (!mrioc->intr_info) { 976 retval = -ENOMEM; 977 pci_free_irq_vectors(mrioc->pdev); 978 goto out_failed; 979 } 980 for (i = 0; i < max_vectors; i++) { 981 retval = mpi3mr_request_irq(mrioc, i); 982 if (retval) { 983 mrioc->intr_info_count = i; 984 goto out_failed; 985 } 986 } 987 if (reset_devices || !setup_one) 988 mrioc->is_intr_info_set = true; 989 mrioc->intr_info_count = max_vectors; 990 mpi3mr_ioc_enable_intr(mrioc); 991 return 0; 992 993 out_failed: 994 mpi3mr_cleanup_isr(mrioc); 995 996 return retval; 997 } 998 999 static const struct { 1000 enum mpi3mr_iocstate value; 1001 char *name; 1002 } mrioc_states[] = { 1003 { MRIOC_STATE_READY, "ready" }, 1004 { MRIOC_STATE_FAULT, "fault" }, 1005 { MRIOC_STATE_RESET, "reset" }, 1006 { MRIOC_STATE_BECOMING_READY, "becoming ready" }, 1007 { MRIOC_STATE_RESET_REQUESTED, "reset requested" }, 1008 { MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" }, 1009 }; 1010 1011 static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state) 1012 { 1013 int i; 1014 char *name = NULL; 1015 1016 for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) { 1017 if (mrioc_states[i].value == mrioc_state) { 1018 name = mrioc_states[i].name; 1019 break; 1020 } 1021 } 1022 return name; 1023 } 1024 1025 /* Reset reason to name mapper structure*/ 1026 static const struct { 1027 enum mpi3mr_reset_reason value; 1028 char *name; 1029 } mpi3mr_reset_reason_codes[] = { 1030 { MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" }, 1031 { MPI3MR_RESET_FROM_FAULT_WATCH, "fault" }, 1032 { MPI3MR_RESET_FROM_APP, "application invocation" }, 1033 { MPI3MR_RESET_FROM_EH_HOS, "error handling" }, 1034 { MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" }, 1035 { MPI3MR_RESET_FROM_APP_TIMEOUT, "application command timeout" }, 1036 { MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" }, 1037 { MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" }, 1038 { MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" }, 1039 { MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" }, 1040 { MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" }, 1041 { MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" }, 1042 { MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" }, 1043 { 1044 MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT, 1045 "create request queue timeout" 1046 }, 1047 { 1048 MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT, 1049 "create reply queue timeout" 1050 }, 1051 { MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" }, 1052 { MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" }, 1053 { MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" }, 1054 { MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" }, 1055 { 1056 MPI3MR_RESET_FROM_CIACTVRST_TIMER, 1057 "component image activation timeout" 1058 }, 1059 { 1060 MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT, 1061 "get package version timeout" 1062 }, 1063 { MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" }, 1064 { MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" }, 1065 { 1066 MPI3MR_RESET_FROM_DIAG_BUFFER_POST_TIMEOUT, 1067 "diagnostic buffer post timeout" 1068 }, 1069 { 1070 MPI3MR_RESET_FROM_DIAG_BUFFER_RELEASE_TIMEOUT, 1071 "diagnostic buffer release timeout" 1072 }, 1073 { MPI3MR_RESET_FROM_FIRMWARE, "firmware asynchronous reset" }, 1074 { MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT, "configuration request timeout"}, 1075 { MPI3MR_RESET_FROM_SAS_TRANSPORT_TIMEOUT, "timeout of a SAS transport layer request" }, 1076 { MPI3MR_RESET_FROM_INVALID_COMPLETION, "invalid cmd completion" }, 1077 }; 1078 1079 /** 1080 * mpi3mr_reset_rc_name - get reset reason code name 1081 * @reason_code: reset reason code value 1082 * 1083 * Map reset reason to an NULL terminated ASCII string 1084 * 1085 * Return: name corresponding to reset reason value or NULL. 1086 */ 1087 static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code) 1088 { 1089 int i; 1090 char *name = NULL; 1091 1092 for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_reason_codes); i++) { 1093 if (mpi3mr_reset_reason_codes[i].value == reason_code) { 1094 name = mpi3mr_reset_reason_codes[i].name; 1095 break; 1096 } 1097 } 1098 return name; 1099 } 1100 1101 /* Reset type to name mapper structure*/ 1102 static const struct { 1103 u16 reset_type; 1104 char *name; 1105 } mpi3mr_reset_types[] = { 1106 { MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" }, 1107 { MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" }, 1108 }; 1109 1110 /** 1111 * mpi3mr_reset_type_name - get reset type name 1112 * @reset_type: reset type value 1113 * 1114 * Map reset type to an NULL terminated ASCII string 1115 * 1116 * Return: name corresponding to reset type value or NULL. 1117 */ 1118 static const char *mpi3mr_reset_type_name(u16 reset_type) 1119 { 1120 int i; 1121 char *name = NULL; 1122 1123 for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_types); i++) { 1124 if (mpi3mr_reset_types[i].reset_type == reset_type) { 1125 name = mpi3mr_reset_types[i].name; 1126 break; 1127 } 1128 } 1129 return name; 1130 } 1131 1132 /** 1133 * mpi3mr_is_fault_recoverable - Read fault code and decide 1134 * whether the controller can be recoverable 1135 * @mrioc: Adapter instance reference 1136 * Return: true if fault is recoverable, false otherwise. 1137 */ 1138 static inline bool mpi3mr_is_fault_recoverable(struct mpi3mr_ioc *mrioc) 1139 { 1140 u32 fault; 1141 1142 fault = (readl(&mrioc->sysif_regs->fault) & 1143 MPI3_SYSIF_FAULT_CODE_MASK); 1144 1145 switch (fault) { 1146 case MPI3_SYSIF_FAULT_CODE_COMPLETE_RESET_NEEDED: 1147 case MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED: 1148 ioc_warn(mrioc, 1149 "controller requires system power cycle, marking controller as unrecoverable\n"); 1150 return false; 1151 case MPI3_SYSIF_FAULT_CODE_INSUFFICIENT_PCI_SLOT_POWER: 1152 ioc_warn(mrioc, 1153 "controller faulted due to insufficient power,\n" 1154 " try by connecting it to a different slot\n"); 1155 return false; 1156 default: 1157 break; 1158 } 1159 return true; 1160 } 1161 1162 /** 1163 * mpi3mr_print_fault_info - Display fault information 1164 * @mrioc: Adapter instance reference 1165 * 1166 * Display the controller fault information if there is a 1167 * controller fault. 1168 * 1169 * Return: Nothing. 1170 */ 1171 void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc) 1172 { 1173 u32 ioc_status, code, code1, code2, code3; 1174 1175 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1176 1177 if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) { 1178 code = readl(&mrioc->sysif_regs->fault); 1179 code1 = readl(&mrioc->sysif_regs->fault_info[0]); 1180 code2 = readl(&mrioc->sysif_regs->fault_info[1]); 1181 code3 = readl(&mrioc->sysif_regs->fault_info[2]); 1182 1183 ioc_info(mrioc, 1184 "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n", 1185 code, code1, code2, code3); 1186 } 1187 } 1188 1189 /** 1190 * mpi3mr_save_fault_info - Save fault information 1191 * @mrioc: Adapter instance reference 1192 * 1193 * Save the controller fault information if there is a 1194 * controller fault. 1195 * 1196 * Return: Nothing. 1197 */ 1198 static void mpi3mr_save_fault_info(struct mpi3mr_ioc *mrioc) 1199 { 1200 u32 ioc_status, i; 1201 1202 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1203 1204 if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) { 1205 mrioc->saved_fault_code = readl(&mrioc->sysif_regs->fault) & 1206 MPI3_SYSIF_FAULT_CODE_MASK; 1207 for (i = 0; i < 3; i++) { 1208 mrioc->saved_fault_info[i] = 1209 readl(&mrioc->sysif_regs->fault_info[i]); 1210 } 1211 } 1212 } 1213 1214 /** 1215 * mpi3mr_get_iocstate - Get IOC State 1216 * @mrioc: Adapter instance reference 1217 * 1218 * Return a proper IOC state enum based on the IOC status and 1219 * IOC configuration and unrcoverable state of the controller. 1220 * 1221 * Return: Current IOC state. 1222 */ 1223 enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc) 1224 { 1225 u32 ioc_status, ioc_config; 1226 u8 ready, enabled; 1227 1228 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1229 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 1230 1231 if (mrioc->unrecoverable) 1232 return MRIOC_STATE_UNRECOVERABLE; 1233 if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) 1234 return MRIOC_STATE_FAULT; 1235 1236 ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY); 1237 enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC); 1238 1239 if (ready && enabled) 1240 return MRIOC_STATE_READY; 1241 if ((!ready) && (!enabled)) 1242 return MRIOC_STATE_RESET; 1243 if ((!ready) && (enabled)) 1244 return MRIOC_STATE_BECOMING_READY; 1245 1246 return MRIOC_STATE_RESET_REQUESTED; 1247 } 1248 1249 /** 1250 * mpi3mr_free_ioctl_dma_memory - free memory for ioctl dma 1251 * @mrioc: Adapter instance reference 1252 * 1253 * Free the DMA memory allocated for IOCTL handling purpose. 1254 * 1255 * Return: None 1256 */ 1257 static void mpi3mr_free_ioctl_dma_memory(struct mpi3mr_ioc *mrioc) 1258 { 1259 struct dma_memory_desc *mem_desc; 1260 u16 i; 1261 1262 if (!mrioc->ioctl_dma_pool) 1263 return; 1264 1265 for (i = 0; i < MPI3MR_NUM_IOCTL_SGE; i++) { 1266 mem_desc = &mrioc->ioctl_sge[i]; 1267 if (mem_desc->addr) { 1268 dma_pool_free(mrioc->ioctl_dma_pool, 1269 mem_desc->addr, 1270 mem_desc->dma_addr); 1271 mem_desc->addr = NULL; 1272 } 1273 } 1274 dma_pool_destroy(mrioc->ioctl_dma_pool); 1275 mrioc->ioctl_dma_pool = NULL; 1276 mem_desc = &mrioc->ioctl_chain_sge; 1277 1278 if (mem_desc->addr) { 1279 dma_free_coherent(&mrioc->pdev->dev, mem_desc->size, 1280 mem_desc->addr, mem_desc->dma_addr); 1281 mem_desc->addr = NULL; 1282 } 1283 mem_desc = &mrioc->ioctl_resp_sge; 1284 if (mem_desc->addr) { 1285 dma_free_coherent(&mrioc->pdev->dev, mem_desc->size, 1286 mem_desc->addr, mem_desc->dma_addr); 1287 mem_desc->addr = NULL; 1288 } 1289 1290 mrioc->ioctl_sges_allocated = false; 1291 } 1292 1293 /** 1294 * mpi3mr_alloc_ioctl_dma_memory - Alloc memory for ioctl dma 1295 * @mrioc: Adapter instance reference 1296 * 1297 * This function allocates dmaable memory required to handle the 1298 * application issued MPI3 IOCTL requests. 1299 * 1300 * Return: None 1301 */ 1302 static void mpi3mr_alloc_ioctl_dma_memory(struct mpi3mr_ioc *mrioc) 1303 1304 { 1305 struct dma_memory_desc *mem_desc; 1306 u16 i; 1307 1308 mrioc->ioctl_dma_pool = dma_pool_create("ioctl dma pool", 1309 &mrioc->pdev->dev, 1310 MPI3MR_IOCTL_SGE_SIZE, 1311 MPI3MR_PAGE_SIZE_4K, 0); 1312 1313 if (!mrioc->ioctl_dma_pool) { 1314 ioc_err(mrioc, "ioctl_dma_pool: dma_pool_create failed\n"); 1315 goto out_failed; 1316 } 1317 1318 for (i = 0; i < MPI3MR_NUM_IOCTL_SGE; i++) { 1319 mem_desc = &mrioc->ioctl_sge[i]; 1320 mem_desc->size = MPI3MR_IOCTL_SGE_SIZE; 1321 mem_desc->addr = dma_pool_zalloc(mrioc->ioctl_dma_pool, 1322 GFP_KERNEL, 1323 &mem_desc->dma_addr); 1324 if (!mem_desc->addr) 1325 goto out_failed; 1326 } 1327 1328 mem_desc = &mrioc->ioctl_chain_sge; 1329 mem_desc->size = MPI3MR_PAGE_SIZE_4K; 1330 mem_desc->addr = dma_alloc_coherent(&mrioc->pdev->dev, 1331 mem_desc->size, 1332 &mem_desc->dma_addr, 1333 GFP_KERNEL); 1334 if (!mem_desc->addr) 1335 goto out_failed; 1336 1337 mem_desc = &mrioc->ioctl_resp_sge; 1338 mem_desc->size = MPI3MR_PAGE_SIZE_4K; 1339 mem_desc->addr = dma_alloc_coherent(&mrioc->pdev->dev, 1340 mem_desc->size, 1341 &mem_desc->dma_addr, 1342 GFP_KERNEL); 1343 if (!mem_desc->addr) 1344 goto out_failed; 1345 1346 mrioc->ioctl_sges_allocated = true; 1347 1348 return; 1349 out_failed: 1350 ioc_warn(mrioc, "cannot allocate DMA memory for the mpt commands\n" 1351 "from the applications, application interface for MPT command is disabled\n"); 1352 mpi3mr_free_ioctl_dma_memory(mrioc); 1353 } 1354 1355 /** 1356 * mpi3mr_fault_uevent_emit - Emit uevent for any controller 1357 * fault 1358 * @mrioc: Pointer to the mpi3mr_ioc structure for the controller instance 1359 * 1360 * This function is invoked when the controller undergoes any 1361 * type of fault. 1362 */ 1363 1364 static void mpi3mr_fault_uevent_emit(struct mpi3mr_ioc *mrioc) 1365 { 1366 struct kobj_uevent_env *env; 1367 unsigned int noio_flag; 1368 int ret; 1369 1370 noio_flag = memalloc_noio_save(); 1371 1372 env = kzalloc_obj(*env); 1373 if (!env) 1374 goto out_restore; 1375 1376 ret = add_uevent_var(env, "DRIVER=%s", mrioc->driver_name); 1377 if (ret) 1378 goto out_free; 1379 1380 ret = add_uevent_var(env, "IOC_ID=%u", mrioc->id); 1381 if (ret) 1382 goto out_free; 1383 1384 ret = add_uevent_var(env, "FAULT_CODE=0x%08x", 1385 mrioc->saved_fault_code); 1386 if (ret) 1387 goto out_free; 1388 1389 ret = add_uevent_var(env, "FAULT_INFO0=0x%08x", 1390 mrioc->saved_fault_info[0]); 1391 if (ret) 1392 goto out_free; 1393 1394 ret = add_uevent_var(env, "FAULT_INFO1=0x%08x", 1395 mrioc->saved_fault_info[1]); 1396 if (ret) 1397 goto out_free; 1398 1399 ret = add_uevent_var(env, "FAULT_INFO2=0x%08x", 1400 mrioc->saved_fault_info[2]); 1401 if (ret) 1402 goto out_free; 1403 1404 kobject_uevent_env(&mrioc->shost->shost_gendev.kobj, 1405 KOBJ_CHANGE, env->envp); 1406 1407 out_free: 1408 kfree(env); 1409 out_restore: 1410 memalloc_noio_restore(noio_flag); 1411 } 1412 1413 /** 1414 * mpi3mr_clear_reset_history - clear reset history 1415 * @mrioc: Adapter instance reference 1416 * 1417 * Write the reset history bit in IOC status to clear the bit, 1418 * if it is already set. 1419 * 1420 * Return: Nothing. 1421 */ 1422 static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc) 1423 { 1424 u32 ioc_status; 1425 1426 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1427 if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) 1428 writel(ioc_status, &mrioc->sysif_regs->ioc_status); 1429 } 1430 1431 /** 1432 * mpi3mr_issue_and_process_mur - Message unit Reset handler 1433 * @mrioc: Adapter instance reference 1434 * @reset_reason: Reset reason code 1435 * 1436 * Issue Message unit Reset to the controller and wait for it to 1437 * be complete. 1438 * 1439 * Return: 0 on success, -1 on failure. 1440 */ 1441 static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc, 1442 u32 reset_reason) 1443 { 1444 u32 ioc_config, timeout, ioc_status, scratch_pad0; 1445 int retval = -1; 1446 1447 ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n"); 1448 if (mrioc->unrecoverable) { 1449 ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n"); 1450 return retval; 1451 } 1452 mpi3mr_clear_reset_history(mrioc); 1453 scratch_pad0 = ((MPI3MR_RESET_REASON_OSTYPE_LINUX << 1454 MPI3MR_RESET_REASON_OSTYPE_SHIFT) | 1455 (mrioc->facts.ioc_num << 1456 MPI3MR_RESET_REASON_IOCNUM_SHIFT) | reset_reason); 1457 writel(scratch_pad0, &mrioc->sysif_regs->scratchpad[0]); 1458 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 1459 ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC; 1460 writel(ioc_config, &mrioc->sysif_regs->ioc_configuration); 1461 1462 timeout = MPI3MR_MUR_TIMEOUT * 10; 1463 do { 1464 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1465 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) { 1466 mpi3mr_clear_reset_history(mrioc); 1467 break; 1468 } 1469 if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) { 1470 mpi3mr_print_fault_info(mrioc); 1471 break; 1472 } 1473 msleep(100); 1474 } while (--timeout); 1475 1476 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 1477 if (timeout && !((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) || 1478 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) || 1479 (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC))) 1480 retval = 0; 1481 1482 ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%08x)/(0x%08x)\n", 1483 (!retval) ? "successful" : "failed", ioc_status, ioc_config); 1484 return retval; 1485 } 1486 1487 /** 1488 * mpi3mr_revalidate_factsdata - validate IOCFacts parameters 1489 * during reset/resume 1490 * @mrioc: Adapter instance reference 1491 * 1492 * Return: zero if the new IOCFacts parameters value is compatible with 1493 * older values else return -EPERM 1494 */ 1495 static int 1496 mpi3mr_revalidate_factsdata(struct mpi3mr_ioc *mrioc) 1497 { 1498 unsigned long *removepend_bitmap; 1499 1500 if (mrioc->facts.reply_sz > mrioc->reply_sz) { 1501 ioc_err(mrioc, 1502 "cannot increase reply size from %d to %d\n", 1503 mrioc->reply_sz, mrioc->facts.reply_sz); 1504 return -EPERM; 1505 } 1506 1507 if (mrioc->facts.max_op_reply_q < mrioc->num_op_reply_q) { 1508 ioc_err(mrioc, 1509 "cannot reduce number of operational reply queues from %d to %d\n", 1510 mrioc->num_op_reply_q, 1511 mrioc->facts.max_op_reply_q); 1512 return -EPERM; 1513 } 1514 1515 if (mrioc->facts.max_op_req_q < mrioc->num_op_req_q) { 1516 ioc_err(mrioc, 1517 "cannot reduce number of operational request queues from %d to %d\n", 1518 mrioc->num_op_req_q, mrioc->facts.max_op_req_q); 1519 return -EPERM; 1520 } 1521 1522 if (mrioc->shost->max_sectors != (mrioc->facts.max_data_length / 512)) 1523 ioc_err(mrioc, "Warning: The maximum data transfer length\n" 1524 "\tchanged after reset: previous(%d), new(%d),\n" 1525 "the driver cannot change this at run time\n", 1526 mrioc->shost->max_sectors * 512, mrioc->facts.max_data_length); 1527 1528 if ((mrioc->sas_transport_enabled) && (mrioc->facts.ioc_capabilities & 1529 MPI3_IOCFACTS_CAPABILITY_MULTIPATH_SUPPORTED)) 1530 ioc_err(mrioc, 1531 "critical error: multipath capability is enabled at the\n" 1532 "\tcontroller while sas transport support is enabled at the\n" 1533 "\tdriver, please reboot the system or reload the driver\n"); 1534 1535 if (mrioc->seg_tb_support) { 1536 if (!(mrioc->facts.ioc_capabilities & 1537 MPI3_IOCFACTS_CAPABILITY_SEG_DIAG_TRACE_SUPPORTED)) { 1538 ioc_err(mrioc, 1539 "critical error: previously enabled segmented trace\n" 1540 " buffer capability is disabled after reset. Please\n" 1541 " update the firmware or reboot the system or\n" 1542 " reload the driver to enable trace diag buffer\n"); 1543 mrioc->diag_buffers[0].disabled_after_reset = true; 1544 } else 1545 mrioc->diag_buffers[0].disabled_after_reset = false; 1546 } 1547 1548 if (mrioc->facts.max_devhandle > mrioc->dev_handle_bitmap_bits) { 1549 removepend_bitmap = bitmap_zalloc(mrioc->facts.max_devhandle, 1550 GFP_KERNEL); 1551 if (!removepend_bitmap) { 1552 ioc_err(mrioc, 1553 "failed to increase removepend_bitmap bits from %d to %d\n", 1554 mrioc->dev_handle_bitmap_bits, 1555 mrioc->facts.max_devhandle); 1556 return -EPERM; 1557 } 1558 bitmap_free(mrioc->removepend_bitmap); 1559 mrioc->removepend_bitmap = removepend_bitmap; 1560 ioc_info(mrioc, 1561 "increased bits of dev_handle_bitmap from %d to %d\n", 1562 mrioc->dev_handle_bitmap_bits, 1563 mrioc->facts.max_devhandle); 1564 mrioc->dev_handle_bitmap_bits = mrioc->facts.max_devhandle; 1565 } 1566 1567 return 0; 1568 } 1569 1570 /** 1571 * mpi3mr_bring_ioc_ready - Bring controller to ready state 1572 * @mrioc: Adapter instance reference 1573 * 1574 * Set Enable IOC bit in IOC configuration register and wait for 1575 * the controller to become ready. 1576 * 1577 * Return: 0 on success, appropriate error on failure. 1578 */ 1579 static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc) 1580 { 1581 u32 ioc_config, ioc_status, timeout, host_diagnostic; 1582 int retval = 0; 1583 enum mpi3mr_iocstate ioc_state; 1584 u64 base_info; 1585 u8 retry = 0; 1586 u64 start_time, elapsed_time_sec; 1587 1588 retry_bring_ioc_ready: 1589 1590 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1591 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 1592 base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information); 1593 ioc_info(mrioc, "ioc_status(0x%08x), ioc_config(0x%08x), ioc_info(0x%016llx) at the bringup\n", 1594 ioc_status, ioc_config, base_info); 1595 1596 if (!mpi3mr_is_fault_recoverable(mrioc)) { 1597 mrioc->unrecoverable = 1; 1598 goto out_device_not_present; 1599 } 1600 1601 /*The timeout value is in 2sec unit, changing it to seconds*/ 1602 mrioc->ready_timeout = 1603 ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >> 1604 MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2; 1605 1606 ioc_info(mrioc, "ready timeout: %d seconds\n", mrioc->ready_timeout); 1607 1608 ioc_state = mpi3mr_get_iocstate(mrioc); 1609 ioc_info(mrioc, "controller is in %s state during detection\n", 1610 mpi3mr_iocstate_name(ioc_state)); 1611 1612 timeout = mrioc->ready_timeout * 10; 1613 1614 do { 1615 ioc_state = mpi3mr_get_iocstate(mrioc); 1616 1617 if (ioc_state != MRIOC_STATE_BECOMING_READY && 1618 ioc_state != MRIOC_STATE_RESET_REQUESTED) 1619 break; 1620 1621 if (!pci_device_is_present(mrioc->pdev)) { 1622 mrioc->unrecoverable = 1; 1623 ioc_err(mrioc, "controller is not present while waiting to reset\n"); 1624 goto out_device_not_present; 1625 } 1626 1627 msleep(100); 1628 } while (--timeout); 1629 1630 if (ioc_state == MRIOC_STATE_READY) { 1631 ioc_info(mrioc, "issuing message unit reset (MUR) to bring to reset state\n"); 1632 retval = mpi3mr_issue_and_process_mur(mrioc, 1633 MPI3MR_RESET_FROM_BRINGUP); 1634 ioc_state = mpi3mr_get_iocstate(mrioc); 1635 if (retval) 1636 ioc_err(mrioc, 1637 "message unit reset failed with error %d current state %s\n", 1638 retval, mpi3mr_iocstate_name(ioc_state)); 1639 } 1640 if (ioc_state != MRIOC_STATE_RESET) { 1641 if (ioc_state == MRIOC_STATE_FAULT) { 1642 timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10; 1643 mpi3mr_print_fault_info(mrioc); 1644 mpi3mr_save_fault_info(mrioc); 1645 mrioc->fault_during_init = 1; 1646 mrioc->fwfault_counter++; 1647 1648 do { 1649 host_diagnostic = 1650 readl(&mrioc->sysif_regs->host_diagnostic); 1651 if (!(host_diagnostic & 1652 MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS)) 1653 break; 1654 if (!pci_device_is_present(mrioc->pdev)) { 1655 mrioc->unrecoverable = 1; 1656 ioc_err(mrioc, "controller is not present at the bringup\n"); 1657 goto out_device_not_present; 1658 } 1659 msleep(100); 1660 } while (--timeout); 1661 } 1662 mpi3mr_print_fault_info(mrioc); 1663 ioc_info(mrioc, "issuing soft reset to bring to reset state\n"); 1664 retval = mpi3mr_issue_reset(mrioc, 1665 MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, 1666 MPI3MR_RESET_FROM_BRINGUP); 1667 if (retval) { 1668 ioc_err(mrioc, 1669 "soft reset failed with error %d\n", retval); 1670 goto out_failed; 1671 } 1672 } 1673 ioc_state = mpi3mr_get_iocstate(mrioc); 1674 if (ioc_state != MRIOC_STATE_RESET) { 1675 ioc_err(mrioc, 1676 "cannot bring controller to reset state, current state: %s\n", 1677 mpi3mr_iocstate_name(ioc_state)); 1678 goto out_failed; 1679 } 1680 mpi3mr_clear_reset_history(mrioc); 1681 retval = mpi3mr_setup_admin_qpair(mrioc); 1682 if (retval) { 1683 ioc_err(mrioc, "failed to setup admin queues: error %d\n", 1684 retval); 1685 goto out_failed; 1686 } 1687 1688 ioc_info(mrioc, "bringing controller to ready state\n"); 1689 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 1690 ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC; 1691 writel(ioc_config, &mrioc->sysif_regs->ioc_configuration); 1692 1693 if (retry == 0) 1694 start_time = jiffies; 1695 1696 timeout = mrioc->ready_timeout * 10; 1697 do { 1698 ioc_state = mpi3mr_get_iocstate(mrioc); 1699 if (ioc_state == MRIOC_STATE_READY) { 1700 ioc_info(mrioc, 1701 "successfully transitioned to %s state\n", 1702 mpi3mr_iocstate_name(ioc_state)); 1703 mpi3mr_clear_reset_history(mrioc); 1704 return 0; 1705 } 1706 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1707 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) || 1708 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) { 1709 mpi3mr_print_fault_info(mrioc); 1710 goto out_failed; 1711 } 1712 if (!pci_device_is_present(mrioc->pdev)) { 1713 mrioc->unrecoverable = 1; 1714 ioc_err(mrioc, 1715 "controller is not present at the bringup\n"); 1716 retval = -1; 1717 goto out_device_not_present; 1718 } 1719 msleep(100); 1720 elapsed_time_sec = jiffies_to_msecs(jiffies - start_time)/1000; 1721 } while (elapsed_time_sec < mrioc->ready_timeout); 1722 1723 ioc_state = mpi3mr_get_iocstate(mrioc); 1724 if (ioc_state == MRIOC_STATE_READY) { 1725 ioc_info(mrioc, 1726 "successfully transitioned to %s state after %llu seconds\n", 1727 mpi3mr_iocstate_name(ioc_state), elapsed_time_sec); 1728 mpi3mr_clear_reset_history(mrioc); 1729 return 0; 1730 } 1731 1732 out_failed: 1733 elapsed_time_sec = jiffies_to_msecs(jiffies - start_time)/1000; 1734 if ((retry < 2) && (elapsed_time_sec < (mrioc->ready_timeout - 60))) { 1735 retry++; 1736 1737 ioc_warn(mrioc, "retrying to bring IOC ready, retry_count:%d\n" 1738 " elapsed time =%llu\n", retry, elapsed_time_sec); 1739 1740 goto retry_bring_ioc_ready; 1741 } 1742 ioc_state = mpi3mr_get_iocstate(mrioc); 1743 ioc_err(mrioc, 1744 "failed to bring to ready state, current state: %s\n", 1745 mpi3mr_iocstate_name(ioc_state)); 1746 out_device_not_present: 1747 return retval; 1748 } 1749 1750 /** 1751 * mpi3mr_soft_reset_success - Check softreset is success or not 1752 * @ioc_status: IOC status register value 1753 * @ioc_config: IOC config register value 1754 * 1755 * Check whether the soft reset is successful or not based on 1756 * IOC status and IOC config register values. 1757 * 1758 * Return: True when the soft reset is success, false otherwise. 1759 */ 1760 static inline bool 1761 mpi3mr_soft_reset_success(u32 ioc_status, u32 ioc_config) 1762 { 1763 if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) || 1764 (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC))) 1765 return true; 1766 return false; 1767 } 1768 1769 /** 1770 * mpi3mr_diagfault_success - Check diag fault is success or not 1771 * @mrioc: Adapter reference 1772 * @ioc_status: IOC status register value 1773 * 1774 * Check whether the controller hit diag reset fault code. 1775 * 1776 * Return: True when there is diag fault, false otherwise. 1777 */ 1778 static inline bool mpi3mr_diagfault_success(struct mpi3mr_ioc *mrioc, 1779 u32 ioc_status) 1780 { 1781 u32 fault; 1782 1783 if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) 1784 return false; 1785 fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK; 1786 if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) { 1787 mpi3mr_print_fault_info(mrioc); 1788 return true; 1789 } 1790 return false; 1791 } 1792 1793 /** 1794 * mpi3mr_set_diagsave - Set diag save bit for snapdump 1795 * @mrioc: Adapter reference 1796 * 1797 * Set diag save bit in IOC configuration register to enable 1798 * snapdump. 1799 * 1800 * Return: Nothing. 1801 */ 1802 static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc) 1803 { 1804 u32 ioc_config; 1805 1806 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 1807 ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE; 1808 writel(ioc_config, &mrioc->sysif_regs->ioc_configuration); 1809 } 1810 1811 /** 1812 * mpi3mr_issue_reset - Issue reset to the controller 1813 * @mrioc: Adapter reference 1814 * @reset_type: Reset type 1815 * @reset_reason: Reset reason code 1816 * 1817 * Unlock the host diagnostic registers and write the specific 1818 * reset type to that, wait for reset acknowledgment from the 1819 * controller, if the reset is not successful retry for the 1820 * predefined number of times. 1821 * 1822 * Return: 0 on success, non-zero on failure. 1823 */ 1824 static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type, 1825 u16 reset_reason) 1826 { 1827 int retval = -1; 1828 u8 unlock_retry_count = 0; 1829 u32 host_diagnostic, ioc_status, ioc_config, scratch_pad0; 1830 u32 timeout = MPI3MR_RESET_ACK_TIMEOUT * 10; 1831 1832 if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) && 1833 (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT)) 1834 return retval; 1835 if (mrioc->unrecoverable) 1836 return retval; 1837 if (reset_reason == MPI3MR_RESET_FROM_FIRMWARE) { 1838 retval = 0; 1839 return retval; 1840 } 1841 1842 ioc_info(mrioc, "%s reset due to %s(0x%x)\n", 1843 mpi3mr_reset_type_name(reset_type), 1844 mpi3mr_reset_rc_name(reset_reason), reset_reason); 1845 1846 mpi3mr_clear_reset_history(mrioc); 1847 do { 1848 ioc_info(mrioc, 1849 "Write magic sequence to unlock host diag register (retry=%d)\n", 1850 ++unlock_retry_count); 1851 if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) { 1852 ioc_err(mrioc, 1853 "%s reset failed due to unlock failure, host_diagnostic(0x%08x)\n", 1854 mpi3mr_reset_type_name(reset_type), 1855 host_diagnostic); 1856 mrioc->unrecoverable = 1; 1857 return retval; 1858 } 1859 1860 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH, 1861 &mrioc->sysif_regs->write_sequence); 1862 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST, 1863 &mrioc->sysif_regs->write_sequence); 1864 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND, 1865 &mrioc->sysif_regs->write_sequence); 1866 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD, 1867 &mrioc->sysif_regs->write_sequence); 1868 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH, 1869 &mrioc->sysif_regs->write_sequence); 1870 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH, 1871 &mrioc->sysif_regs->write_sequence); 1872 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH, 1873 &mrioc->sysif_regs->write_sequence); 1874 usleep_range(1000, 1100); 1875 host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic); 1876 ioc_info(mrioc, 1877 "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n", 1878 unlock_retry_count, host_diagnostic); 1879 } while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE)); 1880 1881 scratch_pad0 = ((MPI3MR_RESET_REASON_OSTYPE_LINUX << 1882 MPI3MR_RESET_REASON_OSTYPE_SHIFT) | (mrioc->facts.ioc_num << 1883 MPI3MR_RESET_REASON_IOCNUM_SHIFT) | reset_reason); 1884 writel(scratch_pad0, &mrioc->sysif_regs->scratchpad[0]); 1885 if (reset_type == MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT) 1886 mpi3mr_set_diagsave(mrioc); 1887 writel(host_diagnostic | reset_type, 1888 &mrioc->sysif_regs->host_diagnostic); 1889 switch (reset_type) { 1890 case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET: 1891 do { 1892 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1893 ioc_config = 1894 readl(&mrioc->sysif_regs->ioc_configuration); 1895 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) 1896 && mpi3mr_soft_reset_success(ioc_status, ioc_config) 1897 ) { 1898 mpi3mr_clear_reset_history(mrioc); 1899 retval = 0; 1900 break; 1901 } 1902 msleep(100); 1903 } while (--timeout); 1904 mpi3mr_print_fault_info(mrioc); 1905 break; 1906 case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT: 1907 do { 1908 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1909 if (mpi3mr_diagfault_success(mrioc, ioc_status)) { 1910 retval = 0; 1911 break; 1912 } 1913 msleep(100); 1914 } while (--timeout); 1915 break; 1916 default: 1917 break; 1918 } 1919 1920 writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND, 1921 &mrioc->sysif_regs->write_sequence); 1922 1923 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 1924 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 1925 ioc_info(mrioc, 1926 "ioc_status/ioc_config after %s reset is (0x%08x)/(0x%08x)\n", 1927 (!retval)?"successful":"failed", ioc_status, 1928 ioc_config); 1929 if (retval) 1930 mrioc->unrecoverable = 1; 1931 return retval; 1932 } 1933 1934 /** 1935 * mpi3mr_admin_request_post - Post request to admin queue 1936 * @mrioc: Adapter reference 1937 * @admin_req: MPI3 request 1938 * @admin_req_sz: Request size 1939 * @ignore_reset: Ignore reset in process 1940 * 1941 * Post the MPI3 request into admin request queue and 1942 * inform the controller, if the queue is full return 1943 * appropriate error. 1944 * 1945 * Return: 0 on success, non-zero on failure. 1946 */ 1947 int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req, 1948 u16 admin_req_sz, u8 ignore_reset) 1949 { 1950 u16 areq_pi = 0, areq_ci = 0, max_entries = 0; 1951 int retval = 0; 1952 unsigned long flags; 1953 u8 *areq_entry; 1954 1955 if (mrioc->unrecoverable) { 1956 ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__); 1957 return -EFAULT; 1958 } 1959 1960 spin_lock_irqsave(&mrioc->admin_req_lock, flags); 1961 areq_pi = mrioc->admin_req_pi; 1962 areq_ci = mrioc->admin_req_ci; 1963 max_entries = mrioc->num_admin_req; 1964 if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) && 1965 (areq_pi == (max_entries - 1)))) { 1966 ioc_err(mrioc, "AdminReqQ full condition detected\n"); 1967 retval = -EAGAIN; 1968 goto out; 1969 } 1970 if (!ignore_reset && mrioc->reset_in_progress) { 1971 ioc_err(mrioc, "AdminReqQ submit reset in progress\n"); 1972 retval = -EAGAIN; 1973 goto out; 1974 } 1975 if (mrioc->pci_err_recovery) { 1976 ioc_err(mrioc, "admin request queue submission failed due to pci error recovery in progress\n"); 1977 retval = -EAGAIN; 1978 goto out; 1979 } 1980 1981 areq_entry = (u8 *)mrioc->admin_req_base + 1982 (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ); 1983 memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ); 1984 memcpy(areq_entry, (u8 *)admin_req, admin_req_sz); 1985 1986 if (++areq_pi == max_entries) 1987 areq_pi = 0; 1988 mrioc->admin_req_pi = areq_pi; 1989 1990 writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi); 1991 1992 out: 1993 spin_unlock_irqrestore(&mrioc->admin_req_lock, flags); 1994 1995 return retval; 1996 } 1997 1998 /** 1999 * mpi3mr_free_op_req_q_segments - free request memory segments 2000 * @mrioc: Adapter instance reference 2001 * @q_idx: operational request queue index 2002 * 2003 * Free memory segments allocated for operational request queue 2004 * 2005 * Return: Nothing. 2006 */ 2007 static void mpi3mr_free_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx) 2008 { 2009 u16 j; 2010 int size; 2011 struct segments *segments; 2012 2013 if (mrioc->enable_segqueue) { 2014 size = MPI3MR_OP_REQ_Q_SEG_SIZE; 2015 if (mrioc->req_qinfo[q_idx].q_segment_list) { 2016 dma_free_coherent(&mrioc->pdev->dev, 2017 MPI3MR_MAX_SEG_LIST_SIZE, 2018 mrioc->req_qinfo[q_idx].q_segment_list, 2019 mrioc->req_qinfo[q_idx].q_segment_list_dma); 2020 mrioc->req_qinfo[q_idx].q_segment_list = NULL; 2021 } 2022 } else 2023 size = mrioc->req_qinfo[q_idx].segment_qd * 2024 mrioc->facts.op_req_sz; 2025 2026 segments = mrioc->req_qinfo[q_idx].q_segments; 2027 if (!segments) 2028 return; 2029 2030 for (j = 0; j < mrioc->req_qinfo[q_idx].num_segments; j++) { 2031 if (!segments[j].segment) 2032 continue; 2033 dma_free_coherent(&mrioc->pdev->dev, 2034 size, segments[j].segment, segments[j].segment_dma); 2035 segments[j].segment = NULL; 2036 } 2037 kfree(mrioc->req_qinfo[q_idx].q_segments); 2038 mrioc->req_qinfo[q_idx].q_segments = NULL; 2039 mrioc->req_qinfo[q_idx].qid = 0; 2040 } 2041 2042 /** 2043 * mpi3mr_free_op_reply_q_segments - free reply memory segments 2044 * @mrioc: Adapter instance reference 2045 * @q_idx: operational reply queue index 2046 * 2047 * Free memory segments allocated for operational reply queue 2048 * 2049 * Return: Nothing. 2050 */ 2051 static void mpi3mr_free_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx) 2052 { 2053 u16 j; 2054 int size; 2055 struct segments *segments; 2056 u16 midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(q_idx, mrioc->op_reply_q_offset); 2057 2058 /* 2059 * Stop the ISR/poll thread from picking up this queue before its 2060 * segments are freed below, and wait for any in-flight handler 2061 * that already has the old pointer to finish using it. 2062 */ 2063 if (midx < mrioc->intr_info_count) { 2064 WRITE_ONCE(mrioc->intr_info[midx].op_reply_q, NULL); 2065 synchronize_irq(pci_irq_vector(mrioc->pdev, midx)); 2066 } 2067 2068 if (mrioc->enable_segqueue) { 2069 size = MPI3MR_OP_REP_Q_SEG_SIZE; 2070 if (mrioc->op_reply_qinfo[q_idx].q_segment_list) { 2071 dma_free_coherent(&mrioc->pdev->dev, 2072 MPI3MR_MAX_SEG_LIST_SIZE, 2073 mrioc->op_reply_qinfo[q_idx].q_segment_list, 2074 mrioc->op_reply_qinfo[q_idx].q_segment_list_dma); 2075 mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL; 2076 } 2077 } else 2078 size = mrioc->op_reply_qinfo[q_idx].segment_qd * 2079 mrioc->op_reply_desc_sz; 2080 2081 segments = mrioc->op_reply_qinfo[q_idx].q_segments; 2082 if (!segments) 2083 return; 2084 2085 for (j = 0; j < mrioc->op_reply_qinfo[q_idx].num_segments; j++) { 2086 if (!segments[j].segment) 2087 continue; 2088 dma_free_coherent(&mrioc->pdev->dev, 2089 size, segments[j].segment, segments[j].segment_dma); 2090 segments[j].segment = NULL; 2091 } 2092 2093 kfree(mrioc->op_reply_qinfo[q_idx].q_segments); 2094 mrioc->op_reply_qinfo[q_idx].q_segments = NULL; 2095 mrioc->op_reply_qinfo[q_idx].qid = 0; 2096 } 2097 2098 /** 2099 * mpi3mr_delete_op_reply_q - delete operational reply queue 2100 * @mrioc: Adapter instance reference 2101 * @qidx: operational reply queue index 2102 * 2103 * Delete operatinal reply queue by issuing MPI request 2104 * through admin queue. 2105 * 2106 * Return: 0 on success, non-zero on failure. 2107 */ 2108 static int mpi3mr_delete_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx) 2109 { 2110 struct mpi3_delete_reply_queue_request delq_req; 2111 struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx; 2112 int retval = 0; 2113 u16 reply_qid = 0, midx; 2114 2115 reply_qid = op_reply_q->qid; 2116 2117 midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset); 2118 2119 if (!reply_qid) { 2120 retval = -1; 2121 ioc_err(mrioc, "Issue DelRepQ: called with invalid ReqQID\n"); 2122 goto out; 2123 } 2124 2125 (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount-- : 2126 mrioc->active_poll_qcount--; 2127 2128 memset(&delq_req, 0, sizeof(delq_req)); 2129 mutex_lock(&mrioc->init_cmds.mutex); 2130 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 2131 retval = -1; 2132 ioc_err(mrioc, "Issue DelRepQ: Init command is in use\n"); 2133 mutex_unlock(&mrioc->init_cmds.mutex); 2134 goto out; 2135 } 2136 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 2137 mrioc->init_cmds.is_waiting = 1; 2138 mrioc->init_cmds.callback = NULL; 2139 delq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 2140 delq_req.function = MPI3_FUNCTION_DELETE_REPLY_QUEUE; 2141 delq_req.queue_id = cpu_to_le16(reply_qid); 2142 2143 init_completion(&mrioc->init_cmds.done); 2144 retval = mpi3mr_admin_request_post(mrioc, &delq_req, sizeof(delq_req), 2145 1); 2146 if (retval) { 2147 ioc_err(mrioc, "Issue DelRepQ: Admin Post failed\n"); 2148 goto out_unlock; 2149 } 2150 wait_for_completion_timeout(&mrioc->init_cmds.done, 2151 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 2152 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 2153 ioc_err(mrioc, "delete reply queue timed out\n"); 2154 mpi3mr_check_rh_fault_ioc(mrioc, 2155 MPI3MR_RESET_FROM_DELREPQ_TIMEOUT); 2156 retval = -1; 2157 goto out_unlock; 2158 } 2159 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 2160 != MPI3_IOCSTATUS_SUCCESS) { 2161 ioc_err(mrioc, 2162 "Issue DelRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 2163 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 2164 mrioc->init_cmds.ioc_loginfo); 2165 retval = -1; 2166 goto out_unlock; 2167 } 2168 mrioc->intr_info[midx].op_reply_q = NULL; 2169 2170 mpi3mr_free_op_reply_q_segments(mrioc, qidx); 2171 out_unlock: 2172 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 2173 mutex_unlock(&mrioc->init_cmds.mutex); 2174 out: 2175 2176 return retval; 2177 } 2178 2179 /** 2180 * mpi3mr_alloc_op_reply_q_segments -Alloc segmented reply pool 2181 * @mrioc: Adapter instance reference 2182 * @qidx: request queue index 2183 * 2184 * Allocate segmented memory pools for operational reply 2185 * queue. 2186 * 2187 * Return: 0 on success, non-zero on failure. 2188 */ 2189 static int mpi3mr_alloc_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx) 2190 { 2191 struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx; 2192 int i, size; 2193 u64 *q_segment_list_entry = NULL; 2194 struct segments *segments; 2195 2196 if (mrioc->enable_segqueue) { 2197 op_reply_q->segment_qd = 2198 MPI3MR_OP_REP_Q_SEG_SIZE / mrioc->op_reply_desc_sz; 2199 2200 size = MPI3MR_OP_REP_Q_SEG_SIZE; 2201 2202 op_reply_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev, 2203 MPI3MR_MAX_SEG_LIST_SIZE, &op_reply_q->q_segment_list_dma, 2204 GFP_KERNEL); 2205 if (!op_reply_q->q_segment_list) 2206 return -ENOMEM; 2207 q_segment_list_entry = (u64 *)op_reply_q->q_segment_list; 2208 } else { 2209 op_reply_q->segment_qd = op_reply_q->num_replies; 2210 size = op_reply_q->num_replies * mrioc->op_reply_desc_sz; 2211 } 2212 2213 op_reply_q->num_segments = DIV_ROUND_UP(op_reply_q->num_replies, 2214 op_reply_q->segment_qd); 2215 2216 op_reply_q->q_segments = kzalloc_objs(struct segments, 2217 op_reply_q->num_segments); 2218 if (!op_reply_q->q_segments) 2219 return -ENOMEM; 2220 2221 segments = op_reply_q->q_segments; 2222 for (i = 0; i < op_reply_q->num_segments; i++) { 2223 segments[i].segment = 2224 dma_alloc_coherent(&mrioc->pdev->dev, 2225 size, &segments[i].segment_dma, GFP_KERNEL); 2226 if (!segments[i].segment) 2227 return -ENOMEM; 2228 if (mrioc->enable_segqueue) 2229 q_segment_list_entry[i] = 2230 (unsigned long)segments[i].segment_dma; 2231 } 2232 2233 return 0; 2234 } 2235 2236 /** 2237 * mpi3mr_alloc_op_req_q_segments - Alloc segmented req pool. 2238 * @mrioc: Adapter instance reference 2239 * @qidx: request queue index 2240 * 2241 * Allocate segmented memory pools for operational request 2242 * queue. 2243 * 2244 * Return: 0 on success, non-zero on failure. 2245 */ 2246 static int mpi3mr_alloc_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx) 2247 { 2248 struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx; 2249 int i, size; 2250 u64 *q_segment_list_entry = NULL; 2251 struct segments *segments; 2252 2253 if (mrioc->enable_segqueue) { 2254 op_req_q->segment_qd = 2255 MPI3MR_OP_REQ_Q_SEG_SIZE / mrioc->facts.op_req_sz; 2256 2257 size = MPI3MR_OP_REQ_Q_SEG_SIZE; 2258 2259 op_req_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev, 2260 MPI3MR_MAX_SEG_LIST_SIZE, &op_req_q->q_segment_list_dma, 2261 GFP_KERNEL); 2262 if (!op_req_q->q_segment_list) 2263 return -ENOMEM; 2264 q_segment_list_entry = (u64 *)op_req_q->q_segment_list; 2265 2266 } else { 2267 op_req_q->segment_qd = op_req_q->num_requests; 2268 size = op_req_q->num_requests * mrioc->facts.op_req_sz; 2269 } 2270 2271 op_req_q->num_segments = DIV_ROUND_UP(op_req_q->num_requests, 2272 op_req_q->segment_qd); 2273 2274 op_req_q->q_segments = kzalloc_objs(struct segments, 2275 op_req_q->num_segments); 2276 if (!op_req_q->q_segments) 2277 return -ENOMEM; 2278 2279 segments = op_req_q->q_segments; 2280 for (i = 0; i < op_req_q->num_segments; i++) { 2281 segments[i].segment = 2282 dma_alloc_coherent(&mrioc->pdev->dev, 2283 size, &segments[i].segment_dma, GFP_KERNEL); 2284 if (!segments[i].segment) 2285 return -ENOMEM; 2286 if (mrioc->enable_segqueue) 2287 q_segment_list_entry[i] = 2288 (unsigned long)segments[i].segment_dma; 2289 } 2290 2291 return 0; 2292 } 2293 2294 /** 2295 * mpi3mr_create_op_reply_q - create operational reply queue 2296 * @mrioc: Adapter instance reference 2297 * @qidx: operational reply queue index 2298 * 2299 * Create operatinal reply queue by issuing MPI request 2300 * through admin queue. 2301 * 2302 * Return: 0 on success, non-zero on failure. 2303 */ 2304 static int mpi3mr_create_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx) 2305 { 2306 struct mpi3_create_reply_queue_request create_req; 2307 struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx; 2308 int retval = 0; 2309 u16 reply_qid = 0, midx; 2310 2311 reply_qid = op_reply_q->qid; 2312 2313 midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset); 2314 2315 if (reply_qid) { 2316 retval = -1; 2317 ioc_err(mrioc, "CreateRepQ: called for duplicate qid %d\n", 2318 reply_qid); 2319 2320 return retval; 2321 } 2322 2323 reply_qid = qidx + 1; 2324 2325 if (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) { 2326 if (mrioc->pdev->revision) 2327 op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD; 2328 else 2329 op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD4K; 2330 } else 2331 op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD2K; 2332 2333 op_reply_q->ci = 0; 2334 op_reply_q->ephase = 1; 2335 atomic_set(&op_reply_q->pend_ios, 0); 2336 atomic_set(&op_reply_q->in_use, 0); 2337 op_reply_q->enable_irq_poll = false; 2338 op_reply_q->qfull_watermark = 2339 op_reply_q->num_replies - (MPI3MR_THRESHOLD_REPLY_COUNT * 2); 2340 2341 if (!op_reply_q->q_segments) { 2342 retval = mpi3mr_alloc_op_reply_q_segments(mrioc, qidx); 2343 if (retval) { 2344 mpi3mr_free_op_reply_q_segments(mrioc, qidx); 2345 goto out; 2346 } 2347 } 2348 2349 memset(&create_req, 0, sizeof(create_req)); 2350 mutex_lock(&mrioc->init_cmds.mutex); 2351 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 2352 retval = -1; 2353 ioc_err(mrioc, "CreateRepQ: Init command is in use\n"); 2354 goto out_unlock; 2355 } 2356 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 2357 mrioc->init_cmds.is_waiting = 1; 2358 mrioc->init_cmds.callback = NULL; 2359 create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 2360 create_req.function = MPI3_FUNCTION_CREATE_REPLY_QUEUE; 2361 create_req.queue_id = cpu_to_le16(reply_qid); 2362 2363 if (midx < (mrioc->intr_info_count - mrioc->requested_poll_qcount)) 2364 op_reply_q->qtype = MPI3MR_DEFAULT_QUEUE; 2365 else 2366 op_reply_q->qtype = MPI3MR_POLL_QUEUE; 2367 2368 if (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) { 2369 create_req.flags = 2370 MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE; 2371 create_req.msix_index = 2372 cpu_to_le16(mrioc->intr_info[midx].msix_index); 2373 } else { 2374 create_req.msix_index = cpu_to_le16(mrioc->intr_info_count - 1); 2375 ioc_info(mrioc, "create reply queue(polled): for qid(%d), midx(%d)\n", 2376 reply_qid, midx); 2377 if (!mrioc->active_poll_qcount) 2378 disable_irq_nosync(pci_irq_vector(mrioc->pdev, 2379 mrioc->intr_info_count - 1)); 2380 } 2381 2382 if (mrioc->enable_segqueue) { 2383 create_req.flags |= 2384 MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED; 2385 create_req.base_address = cpu_to_le64( 2386 op_reply_q->q_segment_list_dma); 2387 } else 2388 create_req.base_address = cpu_to_le64( 2389 op_reply_q->q_segments[0].segment_dma); 2390 2391 create_req.size = cpu_to_le16(op_reply_q->num_replies); 2392 2393 init_completion(&mrioc->init_cmds.done); 2394 retval = mpi3mr_admin_request_post(mrioc, &create_req, 2395 sizeof(create_req), 1); 2396 if (retval) { 2397 ioc_err(mrioc, "CreateRepQ: Admin Post failed\n"); 2398 goto out_unlock; 2399 } 2400 wait_for_completion_timeout(&mrioc->init_cmds.done, 2401 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 2402 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 2403 ioc_err(mrioc, "create reply queue timed out\n"); 2404 mpi3mr_check_rh_fault_ioc(mrioc, 2405 MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT); 2406 retval = -1; 2407 goto out_unlock; 2408 } 2409 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 2410 != MPI3_IOCSTATUS_SUCCESS) { 2411 ioc_err(mrioc, 2412 "CreateRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 2413 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 2414 mrioc->init_cmds.ioc_loginfo); 2415 retval = -1; 2416 goto out_unlock; 2417 } 2418 op_reply_q->qid = reply_qid; 2419 if (midx < mrioc->intr_info_count) 2420 mrioc->intr_info[midx].op_reply_q = op_reply_q; 2421 2422 (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount++ : 2423 mrioc->active_poll_qcount++; 2424 2425 out_unlock: 2426 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 2427 mutex_unlock(&mrioc->init_cmds.mutex); 2428 out: 2429 2430 return retval; 2431 } 2432 2433 /** 2434 * mpi3mr_create_op_req_q - create operational request queue 2435 * @mrioc: Adapter instance reference 2436 * @idx: operational request queue index 2437 * @reply_qid: Reply queue ID 2438 * 2439 * Create operatinal request queue by issuing MPI request 2440 * through admin queue. 2441 * 2442 * Return: 0 on success, non-zero on failure. 2443 */ 2444 static int mpi3mr_create_op_req_q(struct mpi3mr_ioc *mrioc, u16 idx, 2445 u16 reply_qid) 2446 { 2447 struct mpi3_create_request_queue_request create_req; 2448 struct op_req_qinfo *op_req_q = mrioc->req_qinfo + idx; 2449 int retval = 0; 2450 u16 req_qid = 0; 2451 2452 req_qid = op_req_q->qid; 2453 2454 if (req_qid) { 2455 retval = -1; 2456 ioc_err(mrioc, "CreateReqQ: called for duplicate qid %d\n", 2457 req_qid); 2458 2459 return retval; 2460 } 2461 req_qid = idx + 1; 2462 2463 op_req_q->num_requests = MPI3MR_OP_REQ_Q_QD; 2464 op_req_q->ci = 0; 2465 op_req_q->pi = 0; 2466 op_req_q->reply_qid = reply_qid; 2467 op_req_q->last_full_host_tag = MPI3MR_HOSTTAG_INVALID; 2468 op_req_q->qfull_io_count = 0; 2469 op_req_q->qfull_instances = 0; 2470 spin_lock_init(&op_req_q->q_lock); 2471 2472 if (!op_req_q->q_segments) { 2473 retval = mpi3mr_alloc_op_req_q_segments(mrioc, idx); 2474 if (retval) { 2475 mpi3mr_free_op_req_q_segments(mrioc, idx); 2476 goto out; 2477 } 2478 } 2479 2480 memset(&create_req, 0, sizeof(create_req)); 2481 mutex_lock(&mrioc->init_cmds.mutex); 2482 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 2483 retval = -1; 2484 ioc_err(mrioc, "CreateReqQ: Init command is in use\n"); 2485 goto out_unlock; 2486 } 2487 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 2488 mrioc->init_cmds.is_waiting = 1; 2489 mrioc->init_cmds.callback = NULL; 2490 create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 2491 create_req.function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE; 2492 create_req.queue_id = cpu_to_le16(req_qid); 2493 if (mrioc->enable_segqueue) { 2494 create_req.flags = 2495 MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED; 2496 create_req.base_address = cpu_to_le64( 2497 op_req_q->q_segment_list_dma); 2498 } else 2499 create_req.base_address = cpu_to_le64( 2500 op_req_q->q_segments[0].segment_dma); 2501 create_req.reply_queue_id = cpu_to_le16(reply_qid); 2502 create_req.size = cpu_to_le16(op_req_q->num_requests); 2503 2504 init_completion(&mrioc->init_cmds.done); 2505 retval = mpi3mr_admin_request_post(mrioc, &create_req, 2506 sizeof(create_req), 1); 2507 if (retval) { 2508 ioc_err(mrioc, "CreateReqQ: Admin Post failed\n"); 2509 goto out_unlock; 2510 } 2511 wait_for_completion_timeout(&mrioc->init_cmds.done, 2512 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 2513 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 2514 ioc_err(mrioc, "create request queue timed out\n"); 2515 mpi3mr_check_rh_fault_ioc(mrioc, 2516 MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT); 2517 retval = -1; 2518 goto out_unlock; 2519 } 2520 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 2521 != MPI3_IOCSTATUS_SUCCESS) { 2522 ioc_err(mrioc, 2523 "CreateReqQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 2524 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 2525 mrioc->init_cmds.ioc_loginfo); 2526 retval = -1; 2527 goto out_unlock; 2528 } 2529 op_req_q->qid = req_qid; 2530 2531 out_unlock: 2532 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 2533 mutex_unlock(&mrioc->init_cmds.mutex); 2534 out: 2535 2536 return retval; 2537 } 2538 2539 /** 2540 * mpi3mr_create_op_queues - create operational queue pairs 2541 * @mrioc: Adapter instance reference 2542 * 2543 * Allocate memory for operational queue meta data and call 2544 * create request and reply queue functions. 2545 * 2546 * Return: 0 on success, non-zero on failures. 2547 */ 2548 static int mpi3mr_create_op_queues(struct mpi3mr_ioc *mrioc) 2549 { 2550 int retval = 0; 2551 u16 num_queues = 0, i = 0, j = 0, msix_count_op_q = 1; 2552 u32 ioc_status; 2553 enum mpi3mr_iocstate ioc_state; 2554 2555 num_queues = min_t(int, mrioc->facts.max_op_reply_q, 2556 mrioc->facts.max_op_req_q); 2557 2558 msix_count_op_q = 2559 mrioc->intr_info_count - mrioc->op_reply_q_offset; 2560 if (!mrioc->num_queues) 2561 mrioc->num_queues = min_t(int, num_queues, msix_count_op_q); 2562 /* 2563 * During reset set the num_queues to the number of queues 2564 * that was set before the reset. 2565 */ 2566 num_queues = mrioc->num_op_reply_q ? 2567 mrioc->num_op_reply_q : mrioc->num_queues; 2568 ioc_info(mrioc, "trying to create %d operational queue pairs\n", 2569 num_queues); 2570 2571 if (!mrioc->req_qinfo) { 2572 mrioc->req_qinfo = kzalloc_objs(struct op_req_qinfo, num_queues); 2573 if (!mrioc->req_qinfo) { 2574 retval = -1; 2575 goto out_failed; 2576 } 2577 2578 mrioc->op_reply_qinfo = kzalloc(sizeof(struct op_reply_qinfo) * 2579 num_queues, GFP_KERNEL); 2580 if (!mrioc->op_reply_qinfo) { 2581 retval = -1; 2582 goto out_failed; 2583 } 2584 } 2585 2586 if (mrioc->enable_segqueue) 2587 ioc_info(mrioc, 2588 "allocating operational queues through segmented queues\n"); 2589 2590 for (i = 0; i < num_queues; i++) { 2591 if (mpi3mr_create_op_reply_q(mrioc, i)) { 2592 ioc_err(mrioc, "Cannot create OP RepQ %d\n", i); 2593 break; 2594 } 2595 if (mpi3mr_create_op_req_q(mrioc, i, 2596 mrioc->op_reply_qinfo[i].qid)) { 2597 ioc_err(mrioc, "Cannot create OP ReqQ %d\n", i); 2598 mpi3mr_delete_op_reply_q(mrioc, i); 2599 break; 2600 } 2601 } 2602 2603 if (i < num_queues) { 2604 for (j = i; j < num_queues; j++) { 2605 mpi3mr_free_op_req_q_segments(mrioc, j); 2606 mpi3mr_free_op_reply_q_segments(mrioc, j); 2607 } 2608 } 2609 2610 if (i == 0) { 2611 /* Not even one queue is created successfully*/ 2612 retval = -1; 2613 goto out_failed; 2614 } 2615 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 2616 ioc_state = mpi3mr_get_iocstate(mrioc); 2617 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) || 2618 ioc_state != MRIOC_STATE_READY) { 2619 mpi3mr_print_fault_info(mrioc); 2620 retval = -1; 2621 goto out_failed; 2622 } 2623 mrioc->num_op_reply_q = mrioc->num_op_req_q = i; 2624 ioc_info(mrioc, 2625 "successfully created %d operational queue pairs(default/polled) queue = (%d/%d)\n", 2626 mrioc->num_op_reply_q, mrioc->default_qcount, 2627 mrioc->active_poll_qcount); 2628 2629 return retval; 2630 out_failed: 2631 if (mrioc->req_qinfo) { 2632 for (j = 0; j < i; j++) { 2633 mpi3mr_free_op_req_q_segments(mrioc, j); 2634 mpi3mr_free_op_reply_q_segments(mrioc, j); 2635 } 2636 kfree(mrioc->req_qinfo); 2637 mrioc->req_qinfo = NULL; 2638 } 2639 mrioc->num_op_req_q = 0; 2640 2641 kfree(mrioc->op_reply_qinfo); 2642 mrioc->op_reply_qinfo = NULL; 2643 mrioc->num_op_reply_q = 0; 2644 2645 return retval; 2646 } 2647 2648 /** 2649 * mpi3mr_op_request_post - Post request to operational queue 2650 * @mrioc: Adapter reference 2651 * @op_req_q: Operational request queue info 2652 * @req: MPI3 request 2653 * 2654 * Post the MPI3 request into operational request queue and 2655 * inform the controller, if the queue is full return 2656 * appropriate error. 2657 * 2658 * Return: 0 on success, non-zero on failure. 2659 */ 2660 int mpi3mr_op_request_post(struct mpi3mr_ioc *mrioc, 2661 struct op_req_qinfo *op_req_q, u8 *req) 2662 { 2663 u16 pi = 0, max_entries, reply_qidx = 0, midx; 2664 int retval = 0; 2665 unsigned long flags; 2666 u8 *req_entry; 2667 void *segment_base_addr; 2668 u16 req_sz = mrioc->facts.op_req_sz; 2669 struct segments *segments = op_req_q->q_segments; 2670 struct op_reply_qinfo *op_reply_q = NULL; 2671 struct mpi3_scsi_io_request *scsiio_req = 2672 (struct mpi3_scsi_io_request *)req; 2673 2674 reply_qidx = op_req_q->reply_qid - 1; 2675 op_reply_q = mrioc->op_reply_qinfo + reply_qidx; 2676 2677 if (mrioc->unrecoverable) 2678 return -EFAULT; 2679 2680 spin_lock_irqsave(&op_req_q->q_lock, flags); 2681 pi = op_req_q->pi; 2682 max_entries = op_req_q->num_requests; 2683 2684 if (mpi3mr_check_req_qfull(op_req_q)) { 2685 midx = REPLY_QUEUE_IDX_TO_MSIX_IDX( 2686 reply_qidx, mrioc->op_reply_q_offset); 2687 mpi3mr_process_op_reply_q(mrioc, 2688 READ_ONCE(mrioc->intr_info[midx].op_reply_q)); 2689 2690 if (mpi3mr_check_req_qfull(op_req_q)) { 2691 2692 if (op_req_q->last_full_host_tag == 2693 MPI3MR_HOSTTAG_INVALID) 2694 op_req_q->qfull_instances++; 2695 2696 op_req_q->last_full_host_tag = scsiio_req->host_tag; 2697 op_req_q->qfull_io_count++; 2698 retval = -EAGAIN; 2699 goto out; 2700 } 2701 } 2702 2703 if (op_req_q->last_full_host_tag != MPI3MR_HOSTTAG_INVALID) 2704 op_req_q->last_full_host_tag = MPI3MR_HOSTTAG_INVALID; 2705 2706 if (mrioc->reset_in_progress) { 2707 ioc_err(mrioc, "OpReqQ submit reset in progress\n"); 2708 retval = -EAGAIN; 2709 goto out; 2710 } 2711 if (mrioc->pci_err_recovery) { 2712 ioc_err(mrioc, "operational request queue submission failed due to pci error recovery in progress\n"); 2713 retval = -EAGAIN; 2714 goto out; 2715 } 2716 2717 /* Reply queue is nearing to get full, push back IOs to SML */ 2718 if ((mrioc->prevent_reply_qfull == true) && 2719 (atomic_read(&op_reply_q->pend_ios) > 2720 (op_reply_q->qfull_watermark))) { 2721 atomic_inc(&mrioc->reply_qfull_count); 2722 retval = -EAGAIN; 2723 goto out; 2724 } 2725 2726 segment_base_addr = segments[pi / op_req_q->segment_qd].segment; 2727 req_entry = (u8 *)segment_base_addr + 2728 ((pi % op_req_q->segment_qd) * req_sz); 2729 2730 memset(req_entry, 0, req_sz); 2731 memcpy(req_entry, req, MPI3MR_ADMIN_REQ_FRAME_SZ); 2732 2733 if (++pi == max_entries) 2734 pi = 0; 2735 op_req_q->pi = pi; 2736 2737 #ifndef CONFIG_PREEMPT_RT 2738 if (atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios) 2739 > MPI3MR_IRQ_POLL_TRIGGER_IOCOUNT) 2740 mrioc->op_reply_qinfo[reply_qidx].enable_irq_poll = true; 2741 #else 2742 atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios); 2743 #endif 2744 2745 writel(op_req_q->pi, 2746 &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].producer_index); 2747 2748 out: 2749 spin_unlock_irqrestore(&op_req_q->q_lock, flags); 2750 return retval; 2751 } 2752 2753 /** 2754 * mpi3mr_check_rh_fault_ioc - check reset history and fault 2755 * controller 2756 * @mrioc: Adapter instance reference 2757 * @reason_code: reason code for the fault. 2758 * 2759 * This routine will save snapdump and fault the controller with 2760 * the given reason code if it is not already in the fault or 2761 * not asynchronosuly reset. This will be used to handle 2762 * initilaization time faults/resets/timeout as in those cases 2763 * immediate soft reset invocation is not required. 2764 * 2765 * Return: None. 2766 */ 2767 void mpi3mr_check_rh_fault_ioc(struct mpi3mr_ioc *mrioc, u32 reason_code) 2768 { 2769 u32 ioc_status, host_diagnostic, timeout; 2770 union mpi3mr_trigger_data trigger_data; 2771 2772 if (mrioc->unrecoverable) { 2773 ioc_err(mrioc, "controller is unrecoverable\n"); 2774 return; 2775 } 2776 2777 if (!pci_device_is_present(mrioc->pdev)) { 2778 mrioc->unrecoverable = 1; 2779 ioc_err(mrioc, "controller is not present\n"); 2780 return; 2781 } 2782 memset(&trigger_data, 0, sizeof(trigger_data)); 2783 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 2784 2785 if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) { 2786 mpi3mr_set_trigger_data_in_all_hdb(mrioc, 2787 MPI3MR_HDB_TRIGGER_TYPE_FW_RELEASED, NULL, 0); 2788 return; 2789 } else if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) { 2790 trigger_data.fault = (readl(&mrioc->sysif_regs->fault) & 2791 MPI3_SYSIF_FAULT_CODE_MASK); 2792 2793 mpi3mr_set_trigger_data_in_all_hdb(mrioc, 2794 MPI3MR_HDB_TRIGGER_TYPE_FAULT, &trigger_data, 0); 2795 mpi3mr_print_fault_info(mrioc); 2796 mpi3mr_save_fault_info(mrioc); 2797 mrioc->fault_during_init = 1; 2798 mrioc->fwfault_counter++; 2799 return; 2800 } 2801 2802 mpi3mr_set_diagsave(mrioc); 2803 mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, 2804 reason_code); 2805 trigger_data.fault = (readl(&mrioc->sysif_regs->fault) & 2806 MPI3_SYSIF_FAULT_CODE_MASK); 2807 mpi3mr_set_trigger_data_in_all_hdb(mrioc, MPI3MR_HDB_TRIGGER_TYPE_FAULT, 2808 &trigger_data, 0); 2809 timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10; 2810 do { 2811 host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic); 2812 if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS)) 2813 break; 2814 msleep(100); 2815 } while (--timeout); 2816 2817 mpi3mr_save_fault_info(mrioc); 2818 mrioc->fault_during_init = 1; 2819 mrioc->fwfault_counter++; 2820 } 2821 2822 /** 2823 * mpi3mr_sync_timestamp - Issue time stamp sync request 2824 * @mrioc: Adapter reference 2825 * 2826 * Issue IO unit control MPI request to synchronize firmware 2827 * timestamp with host time. 2828 * 2829 * Return: 0 on success, non-zero on failure. 2830 */ 2831 static int mpi3mr_sync_timestamp(struct mpi3mr_ioc *mrioc) 2832 { 2833 ktime_t current_time; 2834 struct mpi3_iounit_control_request iou_ctrl; 2835 int retval = 0; 2836 2837 memset(&iou_ctrl, 0, sizeof(iou_ctrl)); 2838 mutex_lock(&mrioc->init_cmds.mutex); 2839 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 2840 retval = -1; 2841 ioc_err(mrioc, "Issue IOUCTL time_stamp: command is in use\n"); 2842 mutex_unlock(&mrioc->init_cmds.mutex); 2843 goto out; 2844 } 2845 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 2846 mrioc->init_cmds.is_waiting = 1; 2847 mrioc->init_cmds.callback = NULL; 2848 iou_ctrl.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 2849 iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL; 2850 iou_ctrl.operation = MPI3_CTRL_OP_UPDATE_TIMESTAMP; 2851 current_time = ktime_get_real(); 2852 iou_ctrl.param64[0] = cpu_to_le64(ktime_to_ms(current_time)); 2853 2854 init_completion(&mrioc->init_cmds.done); 2855 retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, 2856 sizeof(iou_ctrl), 0); 2857 if (retval) { 2858 ioc_err(mrioc, "Issue IOUCTL time_stamp: Admin Post failed\n"); 2859 goto out_unlock; 2860 } 2861 2862 wait_for_completion_timeout(&mrioc->init_cmds.done, 2863 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 2864 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 2865 ioc_err(mrioc, "Issue IOUCTL time_stamp: command timed out\n"); 2866 mrioc->init_cmds.is_waiting = 0; 2867 if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET)) 2868 mpi3mr_check_rh_fault_ioc(mrioc, 2869 MPI3MR_RESET_FROM_TSU_TIMEOUT); 2870 retval = -1; 2871 goto out_unlock; 2872 } 2873 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 2874 != MPI3_IOCSTATUS_SUCCESS) { 2875 ioc_err(mrioc, 2876 "Issue IOUCTL time_stamp: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 2877 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 2878 mrioc->init_cmds.ioc_loginfo); 2879 retval = -1; 2880 goto out_unlock; 2881 } 2882 2883 out_unlock: 2884 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 2885 mutex_unlock(&mrioc->init_cmds.mutex); 2886 2887 out: 2888 return retval; 2889 } 2890 2891 /** 2892 * mpi3mr_print_pkg_ver - display controller fw package version 2893 * @mrioc: Adapter reference 2894 * 2895 * Retrieve firmware package version from the component image 2896 * header of the controller flash and display it. 2897 * 2898 * Return: 0 on success and non-zero on failure. 2899 */ 2900 static int mpi3mr_print_pkg_ver(struct mpi3mr_ioc *mrioc) 2901 { 2902 struct mpi3_ci_upload_request ci_upload; 2903 int retval = -1; 2904 void *data = NULL; 2905 dma_addr_t data_dma; 2906 struct mpi3_ci_manifest_mpi *manifest; 2907 u32 data_len = sizeof(struct mpi3_ci_manifest_mpi); 2908 u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST; 2909 2910 data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma, 2911 GFP_KERNEL); 2912 if (!data) 2913 return -ENOMEM; 2914 2915 memset(&ci_upload, 0, sizeof(ci_upload)); 2916 mutex_lock(&mrioc->init_cmds.mutex); 2917 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 2918 ioc_err(mrioc, "sending get package version failed due to command in use\n"); 2919 mutex_unlock(&mrioc->init_cmds.mutex); 2920 goto out; 2921 } 2922 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 2923 mrioc->init_cmds.is_waiting = 1; 2924 mrioc->init_cmds.callback = NULL; 2925 ci_upload.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 2926 ci_upload.function = MPI3_FUNCTION_CI_UPLOAD; 2927 ci_upload.msg_flags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY; 2928 ci_upload.signature1 = cpu_to_le32(MPI3_IMAGE_HEADER_SIGNATURE1_MANIFEST); 2929 ci_upload.image_offset = cpu_to_le32(MPI3_IMAGE_HEADER_SIZE); 2930 ci_upload.segment_size = cpu_to_le32(data_len); 2931 2932 mpi3mr_add_sg_single(&ci_upload.sgl, sgl_flags, data_len, 2933 data_dma); 2934 init_completion(&mrioc->init_cmds.done); 2935 retval = mpi3mr_admin_request_post(mrioc, &ci_upload, 2936 sizeof(ci_upload), 1); 2937 if (retval) { 2938 ioc_err(mrioc, "posting get package version failed\n"); 2939 goto out_unlock; 2940 } 2941 wait_for_completion_timeout(&mrioc->init_cmds.done, 2942 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 2943 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 2944 ioc_err(mrioc, "get package version timed out\n"); 2945 mpi3mr_check_rh_fault_ioc(mrioc, 2946 MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT); 2947 retval = -1; 2948 goto out_unlock; 2949 } 2950 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 2951 == MPI3_IOCSTATUS_SUCCESS) { 2952 manifest = (struct mpi3_ci_manifest_mpi *) data; 2953 if (manifest->manifest_type == MPI3_CI_MANIFEST_TYPE_MPI) { 2954 ioc_info(mrioc, 2955 "firmware package version(%d.%d.%d.%d.%05d-%05d)\n", 2956 manifest->package_version.gen_major, 2957 manifest->package_version.gen_minor, 2958 manifest->package_version.phase_major, 2959 manifest->package_version.phase_minor, 2960 manifest->package_version.customer_id, 2961 manifest->package_version.build_num); 2962 } 2963 } 2964 retval = 0; 2965 out_unlock: 2966 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 2967 mutex_unlock(&mrioc->init_cmds.mutex); 2968 2969 out: 2970 if (data) 2971 dma_free_coherent(&mrioc->pdev->dev, data_len, data, 2972 data_dma); 2973 return retval; 2974 } 2975 2976 /** 2977 * mpi3mr_watchdog_work - watchdog thread to monitor faults 2978 * @work: work struct 2979 * 2980 * Watch dog work periodically executed (1 second interval) to 2981 * monitor firmware fault and perform timestamp synchronization 2982 * to firmware, with an early sync 1 minute after load followed 2983 * by periodic updates at ts_update_interval seconds (default 15 minutes). 2984 * 2985 * Return: Nothing. 2986 */ 2987 static void mpi3mr_watchdog_work(struct work_struct *work) 2988 { 2989 struct mpi3mr_ioc *mrioc = 2990 container_of(work, struct mpi3mr_ioc, watchdog_work.work); 2991 unsigned long flags; 2992 enum mpi3mr_iocstate ioc_state; 2993 u32 host_diagnostic, ioc_status; 2994 union mpi3mr_trigger_data trigger_data; 2995 u16 reset_reason = MPI3MR_RESET_FROM_FAULT_WATCH; 2996 2997 if (mrioc->fault_during_init) { 2998 mpi3mr_fault_uevent_emit(mrioc); 2999 mrioc->fault_during_init = 0; 3000 } 3001 3002 if (mrioc->reset_in_progress || mrioc->pci_err_recovery) 3003 return; 3004 3005 if (!mrioc->unrecoverable && !pci_device_is_present(mrioc->pdev)) { 3006 ioc_err(mrioc, "watchdog could not detect the controller\n"); 3007 mrioc->unrecoverable = 1; 3008 } 3009 3010 if (mrioc->unrecoverable) { 3011 ioc_err(mrioc, 3012 "flush pending commands for unrecoverable controller\n"); 3013 mpi3mr_flush_cmds_for_unrecovered_controller(mrioc); 3014 return; 3015 } 3016 3017 if (mrioc->invalid_io_comp) { 3018 mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_INVALID_COMPLETION, 1); 3019 return; 3020 } 3021 3022 if (atomic_read(&mrioc->admin_pend_isr)) { 3023 ioc_err(mrioc, "Unprocessed admin ISR instance found\n" 3024 "flush admin replies\n"); 3025 mpi3mr_process_admin_reply_q(mrioc); 3026 } 3027 3028 if (!(mrioc->facts.ioc_capabilities & 3029 MPI3_IOCFACTS_CAPABILITY_NON_SUPERVISOR_IOC)) { 3030 if (!mrioc->early_ts_sync_done) { 3031 /* 3032 * Send time sync 1 min after load 3033 */ 3034 if (mrioc->ts_update_counter++ >= 3035 MPI3MR_EARLY_TSUPDATE_SECONDS) { 3036 mrioc->early_ts_sync_done = 1; 3037 mrioc->ts_update_counter = 0; 3038 mpi3mr_sync_timestamp(mrioc); 3039 } 3040 } else if (mrioc->ts_update_counter++ >= 3041 mrioc->ts_update_interval) { 3042 mrioc->ts_update_counter = 0; 3043 mpi3mr_sync_timestamp(mrioc); 3044 } 3045 3046 } 3047 3048 if ((mrioc->prepare_for_reset) && 3049 ((mrioc->prepare_for_reset_timeout_counter++) >= 3050 MPI3MR_PREPARE_FOR_RESET_TIMEOUT)) { 3051 mpi3mr_soft_reset_handler(mrioc, 3052 MPI3MR_RESET_FROM_CIACTVRST_TIMER, 1); 3053 return; 3054 } 3055 3056 memset(&trigger_data, 0, sizeof(trigger_data)); 3057 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 3058 if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) { 3059 mpi3mr_set_trigger_data_in_all_hdb(mrioc, 3060 MPI3MR_HDB_TRIGGER_TYPE_FW_RELEASED, NULL, 0); 3061 mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0); 3062 return; 3063 } 3064 3065 /*Check for fault state every one second and issue Soft reset*/ 3066 ioc_state = mpi3mr_get_iocstate(mrioc); 3067 if (ioc_state != MRIOC_STATE_FAULT) 3068 goto schedule_work; 3069 3070 trigger_data.fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK; 3071 mpi3mr_set_trigger_data_in_all_hdb(mrioc, 3072 MPI3MR_HDB_TRIGGER_TYPE_FAULT, &trigger_data, 0); 3073 host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic); 3074 if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) { 3075 if (!mrioc->diagsave_timeout) { 3076 mpi3mr_print_fault_info(mrioc); 3077 ioc_warn(mrioc, "diag save in progress\n"); 3078 } 3079 if ((mrioc->diagsave_timeout++) <= MPI3_SYSIF_DIAG_SAVE_TIMEOUT) 3080 goto schedule_work; 3081 } 3082 3083 mpi3mr_print_fault_info(mrioc); 3084 mrioc->diagsave_timeout = 0; 3085 3086 if (!mpi3mr_is_fault_recoverable(mrioc)) { 3087 mrioc->unrecoverable = 1; 3088 goto schedule_work; 3089 } 3090 3091 mpi3mr_save_fault_info(mrioc); 3092 mpi3mr_fault_uevent_emit(mrioc); 3093 mrioc->fwfault_counter++; 3094 3095 switch (trigger_data.fault) { 3096 case MPI3_SYSIF_FAULT_CODE_COMPLETE_RESET_NEEDED: 3097 case MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED: 3098 ioc_warn(mrioc, 3099 "controller requires system power cycle, marking controller as unrecoverable\n"); 3100 mrioc->unrecoverable = 1; 3101 goto schedule_work; 3102 case MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS: 3103 goto schedule_work; 3104 case MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET: 3105 reset_reason = MPI3MR_RESET_FROM_CIACTIV_FAULT; 3106 break; 3107 default: 3108 break; 3109 } 3110 mpi3mr_soft_reset_handler(mrioc, reset_reason, 0); 3111 return; 3112 3113 schedule_work: 3114 spin_lock_irqsave(&mrioc->watchdog_lock, flags); 3115 if (mrioc->watchdog_work_q) 3116 queue_delayed_work(mrioc->watchdog_work_q, 3117 &mrioc->watchdog_work, 3118 msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL)); 3119 spin_unlock_irqrestore(&mrioc->watchdog_lock, flags); 3120 return; 3121 } 3122 3123 /** 3124 * mpi3mr_start_watchdog - Start watchdog 3125 * @mrioc: Adapter instance reference 3126 * 3127 * Create and start the watchdog thread to monitor controller 3128 * faults. 3129 * 3130 * Return: Nothing. 3131 */ 3132 void mpi3mr_start_watchdog(struct mpi3mr_ioc *mrioc) 3133 { 3134 if (mrioc->watchdog_work_q) 3135 return; 3136 3137 INIT_DELAYED_WORK(&mrioc->watchdog_work, mpi3mr_watchdog_work); 3138 mrioc->watchdog_work_q = alloc_ordered_workqueue( 3139 "watchdog_%s%d", WQ_MEM_RECLAIM, mrioc->name, mrioc->id); 3140 if (!mrioc->watchdog_work_q) { 3141 ioc_err(mrioc, "%s: failed (line=%d)\n", __func__, __LINE__); 3142 return; 3143 } 3144 3145 if (mrioc->watchdog_work_q) 3146 queue_delayed_work(mrioc->watchdog_work_q, 3147 &mrioc->watchdog_work, 3148 msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL)); 3149 } 3150 3151 /** 3152 * mpi3mr_stop_watchdog - Stop watchdog 3153 * @mrioc: Adapter instance reference 3154 * 3155 * Stop the watchdog thread created to monitor controller 3156 * faults. 3157 * 3158 * Return: Nothing. 3159 */ 3160 void mpi3mr_stop_watchdog(struct mpi3mr_ioc *mrioc) 3161 { 3162 unsigned long flags; 3163 struct workqueue_struct *wq; 3164 3165 spin_lock_irqsave(&mrioc->watchdog_lock, flags); 3166 wq = mrioc->watchdog_work_q; 3167 mrioc->watchdog_work_q = NULL; 3168 spin_unlock_irqrestore(&mrioc->watchdog_lock, flags); 3169 if (wq) { 3170 if (!cancel_delayed_work_sync(&mrioc->watchdog_work)) 3171 flush_workqueue(wq); 3172 destroy_workqueue(wq); 3173 } 3174 } 3175 3176 /** 3177 * mpi3mr_setup_admin_qpair - Setup admin queue pair 3178 * @mrioc: Adapter instance reference 3179 * 3180 * Allocate memory for admin queue pair if required and register 3181 * the admin queue with the controller. 3182 * 3183 * Return: 0 on success, non-zero on failures. 3184 */ 3185 static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc) 3186 { 3187 int retval = 0; 3188 u32 num_admin_entries = 0; 3189 3190 mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE; 3191 mrioc->num_admin_req = mrioc->admin_req_q_sz / 3192 MPI3MR_ADMIN_REQ_FRAME_SZ; 3193 mrioc->admin_req_ci = mrioc->admin_req_pi = 0; 3194 3195 mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE; 3196 mrioc->num_admin_replies = mrioc->admin_reply_q_sz / 3197 MPI3MR_ADMIN_REPLY_FRAME_SZ; 3198 mrioc->admin_reply_ci = 0; 3199 mrioc->admin_reply_ephase = 1; 3200 atomic_set(&mrioc->admin_reply_q_in_use, 0); 3201 atomic_set(&mrioc->admin_pend_isr, 0); 3202 3203 if (!mrioc->admin_req_base) { 3204 mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev, 3205 mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL); 3206 3207 if (!mrioc->admin_req_base) { 3208 retval = -1; 3209 goto out_failed; 3210 } 3211 3212 mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev, 3213 mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma, 3214 GFP_KERNEL); 3215 3216 if (!mrioc->admin_reply_base) { 3217 retval = -1; 3218 goto out_failed; 3219 } 3220 } 3221 3222 num_admin_entries = (mrioc->num_admin_replies << 16) | 3223 (mrioc->num_admin_req); 3224 writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries); 3225 mpi3mr_writeq(mrioc->admin_req_dma, 3226 &mrioc->sysif_regs->admin_request_queue_address, 3227 &mrioc->adm_req_q_bar_writeq_lock); 3228 mpi3mr_writeq(mrioc->admin_reply_dma, 3229 &mrioc->sysif_regs->admin_reply_queue_address, 3230 &mrioc->adm_reply_q_bar_writeq_lock); 3231 writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi); 3232 writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci); 3233 return retval; 3234 3235 out_failed: 3236 3237 if (mrioc->admin_reply_base) { 3238 dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz, 3239 mrioc->admin_reply_base, mrioc->admin_reply_dma); 3240 mrioc->admin_reply_base = NULL; 3241 } 3242 if (mrioc->admin_req_base) { 3243 dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz, 3244 mrioc->admin_req_base, mrioc->admin_req_dma); 3245 mrioc->admin_req_base = NULL; 3246 } 3247 return retval; 3248 } 3249 3250 /** 3251 * mpi3mr_issue_iocfacts - Send IOC Facts 3252 * @mrioc: Adapter instance reference 3253 * @facts_data: Cached IOC facts data 3254 * 3255 * Issue IOC Facts MPI request through admin queue and wait for 3256 * the completion of it or time out. 3257 * 3258 * Return: 0 on success, non-zero on failures. 3259 */ 3260 static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc, 3261 struct mpi3_ioc_facts_data *facts_data) 3262 { 3263 struct mpi3_ioc_facts_request iocfacts_req; 3264 u16 reply_frame_size; 3265 void *data = NULL; 3266 dma_addr_t data_dma; 3267 u32 data_len = sizeof(*facts_data); 3268 int retval = 0; 3269 u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST; 3270 3271 data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma, 3272 GFP_KERNEL); 3273 3274 if (!data) { 3275 retval = -1; 3276 goto out; 3277 } 3278 3279 memset(&iocfacts_req, 0, sizeof(iocfacts_req)); 3280 mutex_lock(&mrioc->init_cmds.mutex); 3281 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 3282 retval = -1; 3283 ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n"); 3284 mutex_unlock(&mrioc->init_cmds.mutex); 3285 goto out; 3286 } 3287 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 3288 mrioc->init_cmds.is_waiting = 1; 3289 mrioc->init_cmds.callback = NULL; 3290 iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 3291 iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS; 3292 3293 mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len, 3294 data_dma); 3295 3296 init_completion(&mrioc->init_cmds.done); 3297 retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req, 3298 sizeof(iocfacts_req), 1); 3299 if (retval) { 3300 ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n"); 3301 goto out_unlock; 3302 } 3303 wait_for_completion_timeout(&mrioc->init_cmds.done, 3304 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 3305 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 3306 ioc_err(mrioc, "ioc_facts timed out\n"); 3307 mpi3mr_check_rh_fault_ioc(mrioc, 3308 MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT); 3309 retval = -1; 3310 goto out_unlock; 3311 } 3312 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 3313 != MPI3_IOCSTATUS_SUCCESS) { 3314 ioc_err(mrioc, 3315 "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 3316 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 3317 mrioc->init_cmds.ioc_loginfo); 3318 retval = -1; 3319 goto out_unlock; 3320 } 3321 memcpy(facts_data, (u8 *)data, data_len); 3322 3323 reply_frame_size = le16_to_cpu(facts_data->reply_frame_size); 3324 /* 3325 * Validate reply_frame_size: 3326 * - Must be at least 16 dwords (64 bytes) to accommodate fixed reply 3327 * structures and avoid underflow/divide-by-zero in logdata sizing. 3328 * - Must not exceed U16_MAX / 4 to prevent integer overflow. 3329 */ 3330 if (reply_frame_size < MPI3MR_MIN_REPLY_FRAME_SZ_DWORDS || 3331 reply_frame_size > (U16_MAX / 4)) { 3332 ioc_err(mrioc, "Invalid reply_frame_size %u (must be %u..%u dwords)\n", 3333 reply_frame_size, MPI3MR_MIN_REPLY_FRAME_SZ_DWORDS, 3334 (unsigned int)(U16_MAX / 4)); 3335 retval = -EINVAL; 3336 goto out_unlock; 3337 } 3338 3339 mpi3mr_process_factsdata(mrioc, facts_data); 3340 out_unlock: 3341 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 3342 mutex_unlock(&mrioc->init_cmds.mutex); 3343 3344 out: 3345 if (data) 3346 dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma); 3347 3348 return retval; 3349 } 3350 3351 /** 3352 * mpi3mr_check_reset_dma_mask - Process IOC facts data 3353 * @mrioc: Adapter instance reference 3354 * 3355 * Check whether the new DMA mask requested through IOCFacts by 3356 * firmware needs to be set, if so set it . 3357 * 3358 * Return: 0 on success, non-zero on failure. 3359 */ 3360 static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc) 3361 { 3362 struct pci_dev *pdev = mrioc->pdev; 3363 int r; 3364 u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask); 3365 3366 if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask)) 3367 return 0; 3368 3369 ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n", 3370 mrioc->dma_mask, facts_dma_mask); 3371 3372 r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask); 3373 if (r) { 3374 ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n", 3375 facts_dma_mask, r); 3376 return r; 3377 } 3378 mrioc->dma_mask = facts_dma_mask; 3379 return r; 3380 } 3381 3382 /** 3383 * mpi3mr_process_factsdata - Process IOC facts data 3384 * @mrioc: Adapter instance reference 3385 * @facts_data: Cached IOC facts data 3386 * 3387 * Convert IOC facts data into cpu endianness and cache it in 3388 * the driver . 3389 * 3390 * Return: Nothing. 3391 */ 3392 static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc, 3393 struct mpi3_ioc_facts_data *facts_data) 3394 { 3395 u32 ioc_config, req_sz, facts_flags; 3396 3397 if ((le16_to_cpu(facts_data->ioc_facts_data_length)) != 3398 (sizeof(*facts_data) / 4)) { 3399 ioc_warn(mrioc, 3400 "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n", 3401 sizeof(*facts_data), 3402 le16_to_cpu(facts_data->ioc_facts_data_length) * 4); 3403 } 3404 3405 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 3406 req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >> 3407 MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT); 3408 if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) { 3409 ioc_err(mrioc, 3410 "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n", 3411 req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size)); 3412 } 3413 3414 memset(&mrioc->facts, 0, sizeof(mrioc->facts)); 3415 3416 facts_flags = le32_to_cpu(facts_data->flags); 3417 mrioc->facts.op_req_sz = req_sz; 3418 mrioc->op_reply_desc_sz = 1 << ((ioc_config & 3419 MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >> 3420 MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT); 3421 3422 mrioc->facts.ioc_num = facts_data->ioc_number; 3423 mrioc->facts.who_init = facts_data->who_init; 3424 mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors); 3425 mrioc->facts.personality = (facts_flags & 3426 MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK); 3427 mrioc->facts.dma_mask = (facts_flags & 3428 MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >> 3429 MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT; 3430 mrioc->facts.dma_mask = (facts_flags & 3431 MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >> 3432 MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT; 3433 mrioc->facts.max_req_limit = (facts_flags & 3434 MPI3_IOCFACTS_FLAGS_MAX_REQ_PER_REPLY_QUEUE_LIMIT); 3435 mrioc->facts.protocol_flags = facts_data->protocol_flags; 3436 mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word); 3437 mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_requests); 3438 mrioc->facts.product_id = le16_to_cpu(facts_data->product_id); 3439 mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4; 3440 mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions); 3441 mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id); 3442 mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds); 3443 mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds); 3444 mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_adv_host_pds); 3445 mrioc->facts.max_raid_pds = le16_to_cpu(facts_data->max_raid_pds); 3446 mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme); 3447 mrioc->facts.max_pcie_switches = 3448 le16_to_cpu(facts_data->max_pcie_switches); 3449 mrioc->facts.max_sasexpanders = 3450 le16_to_cpu(facts_data->max_sas_expanders); 3451 mrioc->facts.max_data_length = le16_to_cpu(facts_data->max_data_length); 3452 mrioc->facts.max_sasinitiators = 3453 le16_to_cpu(facts_data->max_sas_initiators); 3454 mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures); 3455 mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle); 3456 mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle); 3457 mrioc->facts.max_op_req_q = 3458 le16_to_cpu(facts_data->max_operational_request_queues); 3459 mrioc->facts.max_op_reply_q = 3460 le16_to_cpu(facts_data->max_operational_reply_queues); 3461 mrioc->facts.ioc_capabilities = 3462 le32_to_cpu(facts_data->ioc_capabilities); 3463 mrioc->facts.fw_ver.build_num = 3464 le16_to_cpu(facts_data->fw_version.build_num); 3465 mrioc->facts.fw_ver.cust_id = 3466 le16_to_cpu(facts_data->fw_version.customer_id); 3467 mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor; 3468 mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major; 3469 mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor; 3470 mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major; 3471 mrioc->msix_count = min_t(int, mrioc->msix_count, 3472 mrioc->facts.max_msix_vectors); 3473 mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask; 3474 mrioc->facts.sge_mod_value = facts_data->sge_modifier_value; 3475 mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift; 3476 mrioc->facts.shutdown_timeout = 3477 le16_to_cpu(facts_data->shutdown_timeout); 3478 mrioc->facts.diag_trace_sz = 3479 le32_to_cpu(facts_data->diag_trace_size); 3480 mrioc->facts.diag_fw_sz = 3481 le32_to_cpu(facts_data->diag_fw_size); 3482 mrioc->facts.diag_drvr_sz = le32_to_cpu(facts_data->diag_driver_size); 3483 mrioc->facts.max_dev_per_tg = 3484 facts_data->max_devices_per_throttle_group; 3485 mrioc->facts.io_throttle_data_length = 3486 le16_to_cpu(facts_data->io_throttle_data_length); 3487 mrioc->facts.max_io_throttle_group = 3488 le16_to_cpu(facts_data->max_io_throttle_group); 3489 mrioc->facts.io_throttle_low = le16_to_cpu(facts_data->io_throttle_low); 3490 mrioc->facts.io_throttle_high = 3491 le16_to_cpu(facts_data->io_throttle_high); 3492 3493 if (mrioc->facts.max_data_length == 3494 MPI3_IOCFACTS_MAX_DATA_LENGTH_NOT_REPORTED) 3495 mrioc->facts.max_data_length = MPI3MR_DEFAULT_MAX_IO_SIZE; 3496 else 3497 mrioc->facts.max_data_length *= MPI3MR_PAGE_SIZE_4K; 3498 /* Store in 512b block count */ 3499 if (mrioc->facts.io_throttle_data_length) 3500 mrioc->io_throttle_data_length = 3501 (mrioc->facts.io_throttle_data_length * 2 * 4); 3502 else 3503 /* set the length to 1MB + 1K to disable throttle */ 3504 mrioc->io_throttle_data_length = (mrioc->facts.max_data_length / 512) + 2; 3505 3506 mrioc->io_throttle_high = (mrioc->facts.io_throttle_high * 2 * 1024); 3507 mrioc->io_throttle_low = (mrioc->facts.io_throttle_low * 2 * 1024); 3508 3509 ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),", 3510 mrioc->facts.ioc_num, mrioc->facts.max_op_req_q, 3511 mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle); 3512 ioc_info(mrioc, 3513 "maxreqs(%d), mindh(%d) maxvectors(%d) maxperids(%d)\n", 3514 mrioc->facts.max_reqs, mrioc->facts.min_devhandle, 3515 mrioc->facts.max_msix_vectors, mrioc->facts.max_perids); 3516 ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ", 3517 mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value, 3518 mrioc->facts.sge_mod_shift); 3519 ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x max_data_len (%d)\n", 3520 mrioc->facts.dma_mask, (facts_flags & 3521 MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK), mrioc->facts.max_data_length); 3522 ioc_info(mrioc, 3523 "max_dev_per_throttle_group(%d), max_throttle_groups(%d)\n", 3524 mrioc->facts.max_dev_per_tg, mrioc->facts.max_io_throttle_group); 3525 ioc_info(mrioc, 3526 "io_throttle_data_len(%dKiB), io_throttle_high(%dMiB), io_throttle_low(%dMiB)\n", 3527 mrioc->facts.io_throttle_data_length * 4, 3528 mrioc->facts.io_throttle_high, mrioc->facts.io_throttle_low); 3529 } 3530 3531 /** 3532 * mpi3mr_alloc_reply_sense_bufs - Send IOC Init 3533 * @mrioc: Adapter instance reference 3534 * 3535 * Allocate and initialize the reply free buffers, sense 3536 * buffers, reply free queue and sense buffer queue. 3537 * 3538 * Return: 0 on success, non-zero on failures. 3539 */ 3540 static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc) 3541 { 3542 int retval = 0; 3543 u32 sz, i; 3544 3545 if (mrioc->init_cmds.reply) 3546 return retval; 3547 3548 mrioc->init_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL); 3549 if (!mrioc->init_cmds.reply) 3550 goto out_failed; 3551 3552 mrioc->bsg_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL); 3553 if (!mrioc->bsg_cmds.reply) 3554 goto out_failed; 3555 3556 mrioc->transport_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL); 3557 if (!mrioc->transport_cmds.reply) 3558 goto out_failed; 3559 3560 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) { 3561 mrioc->dev_rmhs_cmds[i].reply = kzalloc(mrioc->reply_sz, 3562 GFP_KERNEL); 3563 if (!mrioc->dev_rmhs_cmds[i].reply) 3564 goto out_failed; 3565 } 3566 3567 for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) { 3568 mrioc->evtack_cmds[i].reply = kzalloc(mrioc->reply_sz, 3569 GFP_KERNEL); 3570 if (!mrioc->evtack_cmds[i].reply) 3571 goto out_failed; 3572 } 3573 3574 mrioc->host_tm_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL); 3575 if (!mrioc->host_tm_cmds.reply) 3576 goto out_failed; 3577 3578 mrioc->pel_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL); 3579 if (!mrioc->pel_cmds.reply) 3580 goto out_failed; 3581 3582 mrioc->pel_abort_cmd.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL); 3583 if (!mrioc->pel_abort_cmd.reply) 3584 goto out_failed; 3585 3586 mrioc->dev_handle_bitmap_bits = mrioc->facts.max_devhandle; 3587 mrioc->removepend_bitmap = bitmap_zalloc(mrioc->dev_handle_bitmap_bits, 3588 GFP_KERNEL); 3589 if (!mrioc->removepend_bitmap) 3590 goto out_failed; 3591 3592 mrioc->devrem_bitmap = bitmap_zalloc(MPI3MR_NUM_DEVRMCMD, GFP_KERNEL); 3593 if (!mrioc->devrem_bitmap) 3594 goto out_failed; 3595 3596 mrioc->evtack_cmds_bitmap = bitmap_zalloc(MPI3MR_NUM_EVTACKCMD, 3597 GFP_KERNEL); 3598 if (!mrioc->evtack_cmds_bitmap) 3599 goto out_failed; 3600 3601 mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES; 3602 mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1; 3603 mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR; 3604 mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1; 3605 3606 /* reply buffer pool, 16 byte align */ 3607 sz = mrioc->num_reply_bufs * mrioc->reply_sz; 3608 mrioc->reply_buf_pool = dma_pool_create("reply_buf pool", 3609 &mrioc->pdev->dev, sz, 16, 0); 3610 if (!mrioc->reply_buf_pool) { 3611 ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n"); 3612 goto out_failed; 3613 } 3614 3615 mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL, 3616 &mrioc->reply_buf_dma); 3617 if (!mrioc->reply_buf) 3618 goto out_failed; 3619 3620 mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz; 3621 3622 /* reply free queue, 8 byte align */ 3623 sz = mrioc->reply_free_qsz * 8; 3624 mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool", 3625 &mrioc->pdev->dev, sz, 8, 0); 3626 if (!mrioc->reply_free_q_pool) { 3627 ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n"); 3628 goto out_failed; 3629 } 3630 mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool, 3631 GFP_KERNEL, &mrioc->reply_free_q_dma); 3632 if (!mrioc->reply_free_q) 3633 goto out_failed; 3634 3635 /* sense buffer pool, 4 byte align */ 3636 sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ; 3637 mrioc->sense_buf_pool = dma_pool_create("sense_buf pool", 3638 &mrioc->pdev->dev, sz, 4, 0); 3639 if (!mrioc->sense_buf_pool) { 3640 ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n"); 3641 goto out_failed; 3642 } 3643 mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL, 3644 &mrioc->sense_buf_dma); 3645 if (!mrioc->sense_buf) 3646 goto out_failed; 3647 3648 /* sense buffer queue, 8 byte align */ 3649 sz = mrioc->sense_buf_q_sz * 8; 3650 mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool", 3651 &mrioc->pdev->dev, sz, 8, 0); 3652 if (!mrioc->sense_buf_q_pool) { 3653 ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n"); 3654 goto out_failed; 3655 } 3656 mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool, 3657 GFP_KERNEL, &mrioc->sense_buf_q_dma); 3658 if (!mrioc->sense_buf_q) 3659 goto out_failed; 3660 3661 return retval; 3662 3663 out_failed: 3664 retval = -1; 3665 return retval; 3666 } 3667 3668 /** 3669 * mpimr_initialize_reply_sbuf_queues - initialize reply sense 3670 * buffers 3671 * @mrioc: Adapter instance reference 3672 * 3673 * Helper function to initialize reply and sense buffers along 3674 * with some debug prints. 3675 * 3676 * Return: None. 3677 */ 3678 static void mpimr_initialize_reply_sbuf_queues(struct mpi3mr_ioc *mrioc) 3679 { 3680 u32 sz, i; 3681 dma_addr_t phy_addr; 3682 3683 sz = mrioc->num_reply_bufs * mrioc->reply_sz; 3684 ioc_info(mrioc, 3685 "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n", 3686 mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->reply_sz, 3687 (sz / 1024), (unsigned long long)mrioc->reply_buf_dma); 3688 sz = mrioc->reply_free_qsz * 8; 3689 ioc_info(mrioc, 3690 "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n", 3691 mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024), 3692 (unsigned long long)mrioc->reply_free_q_dma); 3693 sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ; 3694 ioc_info(mrioc, 3695 "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n", 3696 mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSE_BUF_SZ, 3697 (sz / 1024), (unsigned long long)mrioc->sense_buf_dma); 3698 sz = mrioc->sense_buf_q_sz * 8; 3699 ioc_info(mrioc, 3700 "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n", 3701 mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024), 3702 (unsigned long long)mrioc->sense_buf_q_dma); 3703 3704 /* initialize Reply buffer Queue */ 3705 for (i = 0, phy_addr = mrioc->reply_buf_dma; 3706 i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->reply_sz) 3707 mrioc->reply_free_q[i] = cpu_to_le64(phy_addr); 3708 mrioc->reply_free_q[i] = cpu_to_le64(0); 3709 3710 /* initialize Sense Buffer Queue */ 3711 for (i = 0, phy_addr = mrioc->sense_buf_dma; 3712 i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSE_BUF_SZ) 3713 mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr); 3714 mrioc->sense_buf_q[i] = cpu_to_le64(0); 3715 } 3716 3717 /** 3718 * mpi3mr_issue_iocinit - Send IOC Init 3719 * @mrioc: Adapter instance reference 3720 * 3721 * Issue IOC Init MPI request through admin queue and wait for 3722 * the completion of it or time out. 3723 * 3724 * Return: 0 on success, non-zero on failures. 3725 */ 3726 static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc) 3727 { 3728 struct mpi3_ioc_init_request iocinit_req; 3729 struct mpi3_driver_info_layout *drv_info; 3730 dma_addr_t data_dma; 3731 u32 data_len = sizeof(*drv_info); 3732 int retval = 0; 3733 ktime_t current_time; 3734 3735 drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma, 3736 GFP_KERNEL); 3737 if (!drv_info) { 3738 retval = -1; 3739 goto out; 3740 } 3741 mpimr_initialize_reply_sbuf_queues(mrioc); 3742 3743 drv_info->information_length = cpu_to_le32(data_len); 3744 strscpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature)); 3745 strscpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name)); 3746 strscpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version)); 3747 strscpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name)); 3748 strscpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version)); 3749 strscpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE, 3750 sizeof(drv_info->driver_release_date)); 3751 drv_info->driver_capabilities = 0; 3752 memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info, 3753 sizeof(mrioc->driver_info)); 3754 3755 memset(&iocinit_req, 0, sizeof(iocinit_req)); 3756 mutex_lock(&mrioc->init_cmds.mutex); 3757 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 3758 retval = -1; 3759 ioc_err(mrioc, "Issue IOCInit: Init command is in use\n"); 3760 mutex_unlock(&mrioc->init_cmds.mutex); 3761 goto out; 3762 } 3763 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 3764 mrioc->init_cmds.is_waiting = 1; 3765 mrioc->init_cmds.callback = NULL; 3766 iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 3767 iocinit_req.function = MPI3_FUNCTION_IOC_INIT; 3768 iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV; 3769 iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT; 3770 iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR; 3771 iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR; 3772 iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER; 3773 iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz); 3774 iocinit_req.reply_free_queue_address = 3775 cpu_to_le64(mrioc->reply_free_q_dma); 3776 iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSE_BUF_SZ); 3777 iocinit_req.sense_buffer_free_queue_depth = 3778 cpu_to_le16(mrioc->sense_buf_q_sz); 3779 iocinit_req.sense_buffer_free_queue_address = 3780 cpu_to_le64(mrioc->sense_buf_q_dma); 3781 iocinit_req.driver_information_address = cpu_to_le64(data_dma); 3782 3783 current_time = ktime_get_real(); 3784 iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time)); 3785 3786 iocinit_req.msg_flags |= 3787 MPI3_IOCINIT_MSGFLAGS_SCSIIOSTATUSREPLY_SUPPORTED; 3788 iocinit_req.msg_flags |= 3789 MPI3_IOCINIT_MSGFLAGS_WRITESAMEDIVERT_SUPPORTED; 3790 3791 init_completion(&mrioc->init_cmds.done); 3792 retval = mpi3mr_admin_request_post(mrioc, &iocinit_req, 3793 sizeof(iocinit_req), 1); 3794 if (retval) { 3795 ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n"); 3796 goto out_unlock; 3797 } 3798 wait_for_completion_timeout(&mrioc->init_cmds.done, 3799 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 3800 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 3801 mpi3mr_check_rh_fault_ioc(mrioc, 3802 MPI3MR_RESET_FROM_IOCINIT_TIMEOUT); 3803 ioc_err(mrioc, "ioc_init timed out\n"); 3804 retval = -1; 3805 goto out_unlock; 3806 } 3807 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 3808 != MPI3_IOCSTATUS_SUCCESS) { 3809 ioc_err(mrioc, 3810 "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 3811 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 3812 mrioc->init_cmds.ioc_loginfo); 3813 retval = -1; 3814 goto out_unlock; 3815 } 3816 3817 mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs; 3818 writel(mrioc->reply_free_queue_host_index, 3819 &mrioc->sysif_regs->reply_free_host_index); 3820 3821 mrioc->sbq_host_index = mrioc->num_sense_bufs; 3822 writel(mrioc->sbq_host_index, 3823 &mrioc->sysif_regs->sense_buffer_free_host_index); 3824 out_unlock: 3825 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 3826 mutex_unlock(&mrioc->init_cmds.mutex); 3827 3828 out: 3829 if (drv_info) 3830 dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info, 3831 data_dma); 3832 3833 return retval; 3834 } 3835 3836 /** 3837 * mpi3mr_unmask_events - Unmask events in event mask bitmap 3838 * @mrioc: Adapter instance reference 3839 * @event: MPI event ID 3840 * 3841 * Un mask the specific event by resetting the event_mask 3842 * bitmap. 3843 * 3844 * Return: 0 on success, non-zero on failures. 3845 */ 3846 static void mpi3mr_unmask_events(struct mpi3mr_ioc *mrioc, u16 event) 3847 { 3848 u32 desired_event; 3849 u8 word; 3850 3851 if (event >= 128) 3852 return; 3853 3854 desired_event = (1 << (event % 32)); 3855 word = event / 32; 3856 3857 mrioc->event_masks[word] &= ~desired_event; 3858 } 3859 3860 /** 3861 * mpi3mr_issue_event_notification - Send event notification 3862 * @mrioc: Adapter instance reference 3863 * 3864 * Issue event notification MPI request through admin queue and 3865 * wait for the completion of it or time out. 3866 * 3867 * Return: 0 on success, non-zero on failures. 3868 */ 3869 static int mpi3mr_issue_event_notification(struct mpi3mr_ioc *mrioc) 3870 { 3871 struct mpi3_event_notification_request evtnotify_req; 3872 int retval = 0; 3873 u8 i; 3874 3875 memset(&evtnotify_req, 0, sizeof(evtnotify_req)); 3876 mutex_lock(&mrioc->init_cmds.mutex); 3877 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 3878 retval = -1; 3879 ioc_err(mrioc, "Issue EvtNotify: Init command is in use\n"); 3880 mutex_unlock(&mrioc->init_cmds.mutex); 3881 goto out; 3882 } 3883 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 3884 mrioc->init_cmds.is_waiting = 1; 3885 mrioc->init_cmds.callback = NULL; 3886 evtnotify_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 3887 evtnotify_req.function = MPI3_FUNCTION_EVENT_NOTIFICATION; 3888 for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 3889 evtnotify_req.event_masks[i] = 3890 cpu_to_le32(mrioc->event_masks[i]); 3891 init_completion(&mrioc->init_cmds.done); 3892 retval = mpi3mr_admin_request_post(mrioc, &evtnotify_req, 3893 sizeof(evtnotify_req), 1); 3894 if (retval) { 3895 ioc_err(mrioc, "Issue EvtNotify: Admin Post failed\n"); 3896 goto out_unlock; 3897 } 3898 wait_for_completion_timeout(&mrioc->init_cmds.done, 3899 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 3900 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 3901 ioc_err(mrioc, "event notification timed out\n"); 3902 mpi3mr_check_rh_fault_ioc(mrioc, 3903 MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT); 3904 retval = -1; 3905 goto out_unlock; 3906 } 3907 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 3908 != MPI3_IOCSTATUS_SUCCESS) { 3909 ioc_err(mrioc, 3910 "Issue EvtNotify: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 3911 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 3912 mrioc->init_cmds.ioc_loginfo); 3913 retval = -1; 3914 goto out_unlock; 3915 } 3916 3917 out_unlock: 3918 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 3919 mutex_unlock(&mrioc->init_cmds.mutex); 3920 out: 3921 return retval; 3922 } 3923 3924 /** 3925 * mpi3mr_process_event_ack - Process event acknowledgment 3926 * @mrioc: Adapter instance reference 3927 * @event: MPI3 event ID 3928 * @event_ctx: event context 3929 * 3930 * Send event acknowledgment through admin queue and wait for 3931 * it to complete. 3932 * 3933 * Return: 0 on success, non-zero on failures. 3934 */ 3935 int mpi3mr_process_event_ack(struct mpi3mr_ioc *mrioc, u8 event, 3936 u32 event_ctx) 3937 { 3938 struct mpi3_event_ack_request evtack_req; 3939 int retval = 0; 3940 3941 memset(&evtack_req, 0, sizeof(evtack_req)); 3942 mutex_lock(&mrioc->init_cmds.mutex); 3943 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 3944 retval = -1; 3945 ioc_err(mrioc, "Send EvtAck: Init command is in use\n"); 3946 mutex_unlock(&mrioc->init_cmds.mutex); 3947 goto out; 3948 } 3949 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 3950 mrioc->init_cmds.is_waiting = 1; 3951 mrioc->init_cmds.callback = NULL; 3952 evtack_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 3953 evtack_req.function = MPI3_FUNCTION_EVENT_ACK; 3954 evtack_req.event = event; 3955 evtack_req.event_context = cpu_to_le32(event_ctx); 3956 3957 init_completion(&mrioc->init_cmds.done); 3958 retval = mpi3mr_admin_request_post(mrioc, &evtack_req, 3959 sizeof(evtack_req), 1); 3960 if (retval) { 3961 ioc_err(mrioc, "Send EvtAck: Admin Post failed\n"); 3962 goto out_unlock; 3963 } 3964 wait_for_completion_timeout(&mrioc->init_cmds.done, 3965 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 3966 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 3967 ioc_err(mrioc, "Issue EvtNotify: command timed out\n"); 3968 if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET)) 3969 mpi3mr_check_rh_fault_ioc(mrioc, 3970 MPI3MR_RESET_FROM_EVTACK_TIMEOUT); 3971 retval = -1; 3972 goto out_unlock; 3973 } 3974 if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 3975 != MPI3_IOCSTATUS_SUCCESS) { 3976 ioc_err(mrioc, 3977 "Send EvtAck: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 3978 (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 3979 mrioc->init_cmds.ioc_loginfo); 3980 retval = -1; 3981 goto out_unlock; 3982 } 3983 3984 out_unlock: 3985 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 3986 mutex_unlock(&mrioc->init_cmds.mutex); 3987 out: 3988 return retval; 3989 } 3990 3991 /** 3992 * mpi3mr_alloc_chain_bufs - Allocate chain buffers 3993 * @mrioc: Adapter instance reference 3994 * 3995 * Allocate chain buffers and set a bitmap to indicate free 3996 * chain buffers. Chain buffers are used to pass the SGE 3997 * information along with MPI3 SCSI IO requests for host I/O. 3998 * 3999 * Return: 0 on success, non-zero on failure 4000 */ 4001 static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc) 4002 { 4003 int retval = 0; 4004 u32 sz, i; 4005 u16 num_chains; 4006 4007 if (mrioc->chain_sgl_list) 4008 return retval; 4009 4010 num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR; 4011 4012 if (prot_mask & (SHOST_DIX_TYPE0_PROTECTION 4013 | SHOST_DIX_TYPE1_PROTECTION 4014 | SHOST_DIX_TYPE2_PROTECTION 4015 | SHOST_DIX_TYPE3_PROTECTION)) 4016 num_chains += (num_chains / MPI3MR_CHAINBUFDIX_FACTOR); 4017 4018 mrioc->chain_buf_count = num_chains; 4019 sz = sizeof(struct chain_element) * num_chains; 4020 mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL); 4021 if (!mrioc->chain_sgl_list) 4022 goto out_failed; 4023 4024 if (mrioc->max_sgl_entries > (mrioc->facts.max_data_length / 4025 MPI3MR_PAGE_SIZE_4K)) 4026 mrioc->max_sgl_entries = mrioc->facts.max_data_length / 4027 MPI3MR_PAGE_SIZE_4K; 4028 sz = mrioc->max_sgl_entries * sizeof(struct mpi3_sge_common); 4029 ioc_info(mrioc, "number of sgl entries=%d chain buffer size=%dKB\n", 4030 mrioc->max_sgl_entries, sz/1024); 4031 4032 mrioc->chain_buf_pool = dma_pool_create("chain_buf pool", 4033 &mrioc->pdev->dev, sz, 16, 0); 4034 if (!mrioc->chain_buf_pool) { 4035 ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n"); 4036 goto out_failed; 4037 } 4038 4039 for (i = 0; i < num_chains; i++) { 4040 mrioc->chain_sgl_list[i].addr = 4041 dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL, 4042 &mrioc->chain_sgl_list[i].dma_addr); 4043 4044 if (!mrioc->chain_sgl_list[i].addr) 4045 goto out_failed; 4046 } 4047 mrioc->chain_bitmap = bitmap_zalloc(num_chains, GFP_KERNEL); 4048 if (!mrioc->chain_bitmap) 4049 goto out_failed; 4050 return retval; 4051 out_failed: 4052 retval = -1; 4053 return retval; 4054 } 4055 4056 /** 4057 * mpi3mr_port_enable_complete - Mark port enable complete 4058 * @mrioc: Adapter instance reference 4059 * @drv_cmd: Internal command tracker 4060 * 4061 * Call back for asynchronous port enable request sets the 4062 * driver command to indicate port enable request is complete. 4063 * 4064 * Return: Nothing 4065 */ 4066 static void mpi3mr_port_enable_complete(struct mpi3mr_ioc *mrioc, 4067 struct mpi3mr_drv_cmd *drv_cmd) 4068 { 4069 drv_cmd->callback = NULL; 4070 mrioc->scan_started = 0; 4071 if (drv_cmd->state & MPI3MR_CMD_RESET) 4072 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR; 4073 else 4074 mrioc->scan_failed = drv_cmd->ioc_status; 4075 drv_cmd->state = MPI3MR_CMD_NOTUSED; 4076 } 4077 4078 /** 4079 * mpi3mr_issue_port_enable - Issue Port Enable 4080 * @mrioc: Adapter instance reference 4081 * @async: Flag to wait for completion or not 4082 * 4083 * Issue Port Enable MPI request through admin queue and if the 4084 * async flag is not set wait for the completion of the port 4085 * enable or time out. 4086 * 4087 * Return: 0 on success, non-zero on failures. 4088 */ 4089 int mpi3mr_issue_port_enable(struct mpi3mr_ioc *mrioc, u8 async) 4090 { 4091 struct mpi3_port_enable_request pe_req; 4092 int retval = 0; 4093 u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT; 4094 4095 memset(&pe_req, 0, sizeof(pe_req)); 4096 mutex_lock(&mrioc->init_cmds.mutex); 4097 if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) { 4098 retval = -1; 4099 ioc_err(mrioc, "Issue PortEnable: Init command is in use\n"); 4100 mutex_unlock(&mrioc->init_cmds.mutex); 4101 goto out; 4102 } 4103 mrioc->init_cmds.state = MPI3MR_CMD_PENDING; 4104 if (async) { 4105 mrioc->init_cmds.is_waiting = 0; 4106 mrioc->init_cmds.callback = mpi3mr_port_enable_complete; 4107 } else { 4108 mrioc->init_cmds.is_waiting = 1; 4109 mrioc->init_cmds.callback = NULL; 4110 init_completion(&mrioc->init_cmds.done); 4111 } 4112 pe_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS); 4113 pe_req.function = MPI3_FUNCTION_PORT_ENABLE; 4114 4115 retval = mpi3mr_admin_request_post(mrioc, &pe_req, sizeof(pe_req), 1); 4116 if (retval) { 4117 ioc_err(mrioc, "Issue PortEnable: Admin Post failed\n"); 4118 goto out_unlock; 4119 } 4120 if (async) { 4121 mutex_unlock(&mrioc->init_cmds.mutex); 4122 goto out; 4123 } 4124 4125 wait_for_completion_timeout(&mrioc->init_cmds.done, (pe_timeout * HZ)); 4126 if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) { 4127 ioc_err(mrioc, "port enable timed out\n"); 4128 retval = -1; 4129 mpi3mr_check_rh_fault_ioc(mrioc, MPI3MR_RESET_FROM_PE_TIMEOUT); 4130 goto out_unlock; 4131 } 4132 mpi3mr_port_enable_complete(mrioc, &mrioc->init_cmds); 4133 4134 out_unlock: 4135 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 4136 mutex_unlock(&mrioc->init_cmds.mutex); 4137 out: 4138 return retval; 4139 } 4140 4141 /* Protocol type to name mapper structure */ 4142 static const struct { 4143 u8 protocol; 4144 char *name; 4145 } mpi3mr_protocols[] = { 4146 { MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR, "Initiator" }, 4147 { MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET, "Target" }, 4148 { MPI3_IOCFACTS_PROTOCOL_NVME, "NVMe attachment" }, 4149 }; 4150 4151 /* Capability to name mapper structure*/ 4152 static const struct { 4153 u32 capability; 4154 char *name; 4155 } mpi3mr_capabilities[] = { 4156 { MPI3_IOCFACTS_CAPABILITY_RAID_SUPPORTED, "RAID" }, 4157 { MPI3_IOCFACTS_CAPABILITY_MULTIPATH_SUPPORTED, "MultiPath" }, 4158 }; 4159 4160 /** 4161 * mpi3mr_repost_diag_bufs - repost host diag buffers 4162 * @mrioc: Adapter instance reference 4163 * 4164 * repost firmware and trace diag buffers based on global 4165 * trigger flag from driver page 2 4166 * 4167 * Return: 0 on success, non-zero on failures. 4168 */ 4169 static int mpi3mr_repost_diag_bufs(struct mpi3mr_ioc *mrioc) 4170 { 4171 u64 global_trigger; 4172 union mpi3mr_trigger_data prev_trigger_data; 4173 struct diag_buffer_desc *trace_hdb = NULL; 4174 struct diag_buffer_desc *fw_hdb = NULL; 4175 int retval = 0; 4176 bool trace_repost_needed = false; 4177 bool fw_repost_needed = false; 4178 u8 prev_trigger_type; 4179 4180 retval = mpi3mr_refresh_trigger(mrioc, MPI3_CONFIG_ACTION_READ_CURRENT); 4181 if (retval) 4182 return -1; 4183 4184 trace_hdb = mpi3mr_diag_buffer_for_type(mrioc, 4185 MPI3_DIAG_BUFFER_TYPE_TRACE); 4186 4187 if (trace_hdb && 4188 trace_hdb->status != MPI3MR_HDB_BUFSTATUS_NOT_ALLOCATED && 4189 trace_hdb->trigger_type != MPI3MR_HDB_TRIGGER_TYPE_GLOBAL && 4190 trace_hdb->trigger_type != MPI3MR_HDB_TRIGGER_TYPE_ELEMENT) 4191 trace_repost_needed = true; 4192 4193 fw_hdb = mpi3mr_diag_buffer_for_type(mrioc, MPI3_DIAG_BUFFER_TYPE_FW); 4194 4195 if (fw_hdb && fw_hdb->status != MPI3MR_HDB_BUFSTATUS_NOT_ALLOCATED && 4196 fw_hdb->trigger_type != MPI3MR_HDB_TRIGGER_TYPE_GLOBAL && 4197 fw_hdb->trigger_type != MPI3MR_HDB_TRIGGER_TYPE_ELEMENT) 4198 fw_repost_needed = true; 4199 4200 if (trace_repost_needed || fw_repost_needed) { 4201 global_trigger = le64_to_cpu(mrioc->driver_pg2->global_trigger); 4202 if (global_trigger & 4203 MPI3_DRIVER2_GLOBALTRIGGER_POST_DIAG_TRACE_DISABLED) 4204 trace_repost_needed = false; 4205 if (global_trigger & 4206 MPI3_DRIVER2_GLOBALTRIGGER_POST_DIAG_FW_DISABLED) 4207 fw_repost_needed = false; 4208 } 4209 4210 if (trace_repost_needed) { 4211 prev_trigger_type = trace_hdb->trigger_type; 4212 memcpy(&prev_trigger_data, &trace_hdb->trigger_data, 4213 sizeof(trace_hdb->trigger_data)); 4214 retval = mpi3mr_issue_diag_buf_post(mrioc, trace_hdb); 4215 if (!retval) { 4216 dprint_init(mrioc, "trace diag buffer reposted"); 4217 mpi3mr_set_trigger_data_in_hdb(trace_hdb, 4218 MPI3MR_HDB_TRIGGER_TYPE_UNKNOWN, NULL, 1); 4219 } else { 4220 trace_hdb->trigger_type = prev_trigger_type; 4221 memcpy(&trace_hdb->trigger_data, &prev_trigger_data, 4222 sizeof(prev_trigger_data)); 4223 ioc_err(mrioc, "trace diag buffer repost failed"); 4224 return -1; 4225 } 4226 } 4227 4228 if (fw_repost_needed) { 4229 prev_trigger_type = fw_hdb->trigger_type; 4230 memcpy(&prev_trigger_data, &fw_hdb->trigger_data, 4231 sizeof(fw_hdb->trigger_data)); 4232 retval = mpi3mr_issue_diag_buf_post(mrioc, fw_hdb); 4233 if (!retval) { 4234 dprint_init(mrioc, "firmware diag buffer reposted"); 4235 mpi3mr_set_trigger_data_in_hdb(fw_hdb, 4236 MPI3MR_HDB_TRIGGER_TYPE_UNKNOWN, NULL, 1); 4237 } else { 4238 fw_hdb->trigger_type = prev_trigger_type; 4239 memcpy(&fw_hdb->trigger_data, &prev_trigger_data, 4240 sizeof(prev_trigger_data)); 4241 ioc_err(mrioc, "firmware diag buffer repost failed"); 4242 return -1; 4243 } 4244 } 4245 return retval; 4246 } 4247 4248 /** 4249 * mpi3mr_read_driver_page1 - Read Driver Page 1 parameters 4250 * @mrioc: Adapter instance reference 4251 * 4252 * Reads and caches Driver Page 1 parameters such as 4253 * timestamp update interval and driver behavior flags. 4254 * 4255 * Return: Nothing 4256 */ 4257 static void 4258 mpi3mr_read_driver_page1(struct mpi3mr_ioc *mrioc) 4259 { 4260 struct mpi3_driver_page1 driver_pg1; 4261 u16 pg_sz = sizeof(driver_pg1); 4262 int retval = 0; 4263 4264 mrioc->ts_update_interval = MPI3MR_TSUPDATE_INTERVAL; 4265 mrioc->skip_dev_shutdown_on_unload = 0; 4266 4267 retval = mpi3mr_cfg_get_driver_pg1(mrioc, &driver_pg1, pg_sz); 4268 4269 if (retval) 4270 return; 4271 4272 if (driver_pg1.time_stamp_update) 4273 mrioc->ts_update_interval = (driver_pg1.time_stamp_update * 60); 4274 4275 mrioc->skip_dev_shutdown_on_unload = 4276 (le32_to_cpu(driver_pg1.flags) & 4277 MPI3_DRIVER1_FLAGS_DEVICE_SHUTDOWN_ON_UNLOAD_DISABLE) ? 1 : 0; 4278 } 4279 4280 /** 4281 * mpi3mr_print_ioc_info - Display controller information 4282 * @mrioc: Adapter instance reference 4283 * 4284 * Display controller personality, capability, supported 4285 * protocols etc. 4286 * 4287 * Return: Nothing 4288 */ 4289 static void 4290 mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc) 4291 { 4292 int i = 0, bytes_written = 0; 4293 const char *personality; 4294 char protocol[50] = {0}; 4295 char capabilities[100] = {0}; 4296 struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver; 4297 4298 switch (mrioc->facts.personality) { 4299 case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA: 4300 personality = "Enhanced HBA"; 4301 break; 4302 case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR: 4303 personality = "RAID"; 4304 break; 4305 default: 4306 personality = "Unknown"; 4307 break; 4308 } 4309 4310 ioc_info(mrioc, "Running in %s Personality", personality); 4311 4312 ioc_info(mrioc, "FW version(%d.%d.%d.%d.%d.%d)\n", 4313 fwver->gen_major, fwver->gen_minor, fwver->ph_major, 4314 fwver->ph_minor, fwver->cust_id, fwver->build_num); 4315 4316 for (i = 0; i < ARRAY_SIZE(mpi3mr_protocols); i++) { 4317 if (mrioc->facts.protocol_flags & 4318 mpi3mr_protocols[i].protocol) { 4319 bytes_written += scnprintf(protocol + bytes_written, 4320 sizeof(protocol) - bytes_written, "%s%s", 4321 bytes_written ? "," : "", 4322 mpi3mr_protocols[i].name); 4323 } 4324 } 4325 4326 bytes_written = 0; 4327 for (i = 0; i < ARRAY_SIZE(mpi3mr_capabilities); i++) { 4328 if (mrioc->facts.protocol_flags & 4329 mpi3mr_capabilities[i].capability) { 4330 bytes_written += scnprintf(capabilities + bytes_written, 4331 sizeof(capabilities) - bytes_written, "%s%s", 4332 bytes_written ? "," : "", 4333 mpi3mr_capabilities[i].name); 4334 } 4335 } 4336 4337 ioc_info(mrioc, "Protocol=(%s), Capabilities=(%s)\n", 4338 protocol, capabilities); 4339 } 4340 4341 /** 4342 * mpi3mr_cleanup_resources - Free PCI resources 4343 * @mrioc: Adapter instance reference 4344 * 4345 * Unmap PCI device memory and disable PCI device. 4346 * 4347 * Return: 0 on success and non-zero on failure. 4348 */ 4349 void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc) 4350 { 4351 struct pci_dev *pdev = mrioc->pdev; 4352 4353 mpi3mr_cleanup_isr(mrioc); 4354 4355 if (mrioc->sysif_regs) { 4356 iounmap((void __iomem *)mrioc->sysif_regs); 4357 mrioc->sysif_regs = NULL; 4358 } 4359 4360 if (pci_is_enabled(pdev)) { 4361 if (mrioc->bars) 4362 pci_release_selected_regions(pdev, mrioc->bars); 4363 pci_disable_device(pdev); 4364 } 4365 } 4366 4367 /** 4368 * mpi3mr_setup_resources - Enable PCI resources 4369 * @mrioc: Adapter instance reference 4370 * 4371 * Enable PCI device memory, MSI-x registers and set DMA mask. 4372 * 4373 * Return: 0 on success and non-zero on failure. 4374 */ 4375 int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc) 4376 { 4377 struct pci_dev *pdev = mrioc->pdev; 4378 u32 memap_sz = 0; 4379 int i, retval = 0, capb = 0; 4380 u16 message_control; 4381 u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask : 4382 ((sizeof(dma_addr_t) > 4) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32)); 4383 4384 if (pci_enable_device_mem(pdev)) { 4385 ioc_err(mrioc, "pci_enable_device_mem: failed\n"); 4386 retval = -ENODEV; 4387 goto out_failed; 4388 } 4389 4390 capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX); 4391 if (!capb) { 4392 ioc_err(mrioc, "Unable to find MSI-X Capabilities\n"); 4393 retval = -ENODEV; 4394 goto out_failed; 4395 } 4396 mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM); 4397 4398 if (pci_request_selected_regions(pdev, mrioc->bars, 4399 mrioc->driver_name)) { 4400 ioc_err(mrioc, "pci_request_selected_regions: failed\n"); 4401 retval = -ENODEV; 4402 goto out_failed; 4403 } 4404 4405 for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) { 4406 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) { 4407 mrioc->sysif_regs_phys = pci_resource_start(pdev, i); 4408 memap_sz = pci_resource_len(pdev, i); 4409 mrioc->sysif_regs = 4410 ioremap(mrioc->sysif_regs_phys, memap_sz); 4411 break; 4412 } 4413 } 4414 4415 pci_set_master(pdev); 4416 4417 retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask); 4418 if (retval) { 4419 if (dma_mask != DMA_BIT_MASK(32)) { 4420 ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n"); 4421 dma_mask = DMA_BIT_MASK(32); 4422 retval = dma_set_mask_and_coherent(&pdev->dev, 4423 dma_mask); 4424 } 4425 if (retval) { 4426 mrioc->dma_mask = 0; 4427 ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n"); 4428 goto out_failed; 4429 } 4430 } 4431 mrioc->dma_mask = dma_mask; 4432 4433 if (!mrioc->sysif_regs) { 4434 ioc_err(mrioc, 4435 "Unable to map adapter memory or resource not found\n"); 4436 retval = -EINVAL; 4437 goto out_failed; 4438 } 4439 4440 pci_read_config_word(pdev, capb + 2, &message_control); 4441 mrioc->msix_count = (message_control & 0x3FF) + 1; 4442 4443 pci_save_state(pdev); 4444 4445 pci_set_drvdata(pdev, mrioc->shost); 4446 4447 mpi3mr_ioc_disable_intr(mrioc); 4448 4449 ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n", 4450 (unsigned long long)mrioc->sysif_regs_phys, 4451 mrioc->sysif_regs, memap_sz); 4452 ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n", 4453 mrioc->msix_count); 4454 4455 if (!reset_devices && poll_queues > 0) 4456 mrioc->requested_poll_qcount = min_t(int, poll_queues, 4457 mrioc->msix_count - 2); 4458 return retval; 4459 4460 out_failed: 4461 mpi3mr_cleanup_resources(mrioc); 4462 return retval; 4463 } 4464 4465 /** 4466 * mpi3mr_enable_events - Enable required events 4467 * @mrioc: Adapter instance reference 4468 * 4469 * This routine unmasks the events required by the driver by 4470 * sennding appropriate event mask bitmapt through an event 4471 * notification request. 4472 * 4473 * Return: 0 on success and non-zero on failure. 4474 */ 4475 static int mpi3mr_enable_events(struct mpi3mr_ioc *mrioc) 4476 { 4477 int retval = 0; 4478 u32 i; 4479 4480 for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 4481 mrioc->event_masks[i] = -1; 4482 4483 mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_ADDED); 4484 mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_INFO_CHANGED); 4485 mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_STATUS_CHANGE); 4486 mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE); 4487 mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_ADDED); 4488 mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST); 4489 mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DISCOVERY); 4490 mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR); 4491 mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE); 4492 mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST); 4493 mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_ENUMERATION); 4494 mpi3mr_unmask_events(mrioc, MPI3_EVENT_PREPARE_FOR_RESET); 4495 mpi3mr_unmask_events(mrioc, MPI3_EVENT_CABLE_MGMT); 4496 mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENERGY_PACK_CHANGE); 4497 mpi3mr_unmask_events(mrioc, MPI3_EVENT_DIAGNOSTIC_BUFFER_STATUS_CHANGE); 4498 4499 retval = mpi3mr_issue_event_notification(mrioc); 4500 if (retval) 4501 ioc_err(mrioc, "failed to issue event notification %d\n", 4502 retval); 4503 return retval; 4504 } 4505 4506 /** 4507 * mpi3mr_init_ioc - Initialize the controller 4508 * @mrioc: Adapter instance reference 4509 * 4510 * This the controller initialization routine, executed either 4511 * after soft reset or from pci probe callback. 4512 * Setup the required resources, memory map the controller 4513 * registers, create admin and operational reply queue pairs, 4514 * allocate required memory for reply pool, sense buffer pool, 4515 * issue IOC init request to the firmware, unmask the events and 4516 * issue port enable to discover SAS/SATA/NVMe devies and RAID 4517 * volumes. 4518 * 4519 * Return: 0 on success and non-zero on failure. 4520 */ 4521 int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc) 4522 { 4523 int retval = 0; 4524 u8 retry = 0; 4525 struct mpi3_ioc_facts_data facts_data; 4526 u32 sz; 4527 4528 retry_init: 4529 retval = mpi3mr_bring_ioc_ready(mrioc); 4530 if (retval) { 4531 ioc_err(mrioc, "Failed to bring ioc ready: error %d\n", 4532 retval); 4533 goto out_failed_noretry; 4534 } 4535 4536 retval = mpi3mr_setup_isr(mrioc, 1); 4537 if (retval) { 4538 ioc_err(mrioc, "Failed to setup ISR error %d\n", 4539 retval); 4540 goto out_failed_noretry; 4541 } 4542 4543 retval = mpi3mr_issue_iocfacts(mrioc, &facts_data); 4544 if (retval) { 4545 ioc_err(mrioc, "Failed to Issue IOC Facts %d\n", 4546 retval); 4547 goto out_failed; 4548 } 4549 4550 mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD; 4551 mrioc->shost->max_sectors = mrioc->facts.max_data_length / 512; 4552 mrioc->num_io_throttle_group = mrioc->facts.max_io_throttle_group; 4553 atomic_set(&mrioc->pend_large_data_sz, 0); 4554 4555 if (reset_devices) 4556 mrioc->max_host_ios = min_t(int, mrioc->max_host_ios, 4557 MPI3MR_HOST_IOS_KDUMP); 4558 4559 if (!(mrioc->facts.ioc_capabilities & 4560 MPI3_IOCFACTS_CAPABILITY_MULTIPATH_SUPPORTED)) { 4561 mrioc->sas_transport_enabled = 1; 4562 mrioc->scsi_device_channel = 1; 4563 mrioc->shost->max_channel = 1; 4564 mrioc->shost->transportt = mpi3mr_transport_template; 4565 } 4566 4567 if (mrioc->facts.max_req_limit) 4568 mrioc->prevent_reply_qfull = true; 4569 4570 if (mrioc->facts.ioc_capabilities & 4571 MPI3_IOCFACTS_CAPABILITY_SEG_DIAG_TRACE_SUPPORTED) 4572 mrioc->seg_tb_support = true; 4573 4574 mrioc->reply_sz = mrioc->facts.reply_sz; 4575 4576 retval = mpi3mr_check_reset_dma_mask(mrioc); 4577 if (retval) { 4578 ioc_err(mrioc, "Resetting dma mask failed %d\n", 4579 retval); 4580 goto out_failed_noretry; 4581 } 4582 4583 mpi3mr_read_driver_page1(mrioc); 4584 mpi3mr_print_ioc_info(mrioc); 4585 4586 dprint_init(mrioc, "allocating host diag buffers\n"); 4587 mpi3mr_alloc_diag_bufs(mrioc); 4588 4589 dprint_init(mrioc, "allocating ioctl dma buffers\n"); 4590 mpi3mr_alloc_ioctl_dma_memory(mrioc); 4591 4592 dprint_init(mrioc, "posting host diag buffers\n"); 4593 retval = mpi3mr_post_diag_bufs(mrioc); 4594 4595 if (retval) 4596 ioc_warn(mrioc, "failed to post host diag buffers\n"); 4597 4598 if (!mrioc->init_cmds.reply) { 4599 retval = mpi3mr_alloc_reply_sense_bufs(mrioc); 4600 if (retval) { 4601 ioc_err(mrioc, 4602 "%s :Failed to allocated reply sense buffers %d\n", 4603 __func__, retval); 4604 goto out_failed_noretry; 4605 } 4606 } 4607 4608 if (!mrioc->chain_sgl_list) { 4609 retval = mpi3mr_alloc_chain_bufs(mrioc); 4610 if (retval) { 4611 ioc_err(mrioc, "Failed to allocated chain buffers %d\n", 4612 retval); 4613 goto out_failed_noretry; 4614 } 4615 } 4616 4617 retval = mpi3mr_issue_iocinit(mrioc); 4618 if (retval) { 4619 ioc_err(mrioc, "Failed to Issue IOC Init %d\n", 4620 retval); 4621 goto out_failed; 4622 } 4623 4624 retval = mpi3mr_print_pkg_ver(mrioc); 4625 if (retval) { 4626 ioc_err(mrioc, "failed to get package version\n"); 4627 goto out_failed; 4628 } 4629 4630 retval = mpi3mr_setup_isr(mrioc, 0); 4631 if (retval) { 4632 ioc_err(mrioc, "Failed to re-setup ISR, error %d\n", 4633 retval); 4634 goto out_failed_noretry; 4635 } 4636 4637 retval = mpi3mr_create_op_queues(mrioc); 4638 if (retval) { 4639 ioc_err(mrioc, "Failed to create OpQueues error %d\n", 4640 retval); 4641 goto out_failed; 4642 } 4643 4644 if (!mrioc->pel_seqnum_virt) { 4645 dprint_init(mrioc, "allocating memory for pel_seqnum_virt\n"); 4646 mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq); 4647 mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev, 4648 mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma, 4649 GFP_KERNEL); 4650 if (!mrioc->pel_seqnum_virt) { 4651 retval = -ENOMEM; 4652 goto out_failed_noretry; 4653 } 4654 } 4655 4656 if (!mrioc->throttle_groups && mrioc->num_io_throttle_group) { 4657 dprint_init(mrioc, "allocating memory for throttle groups\n"); 4658 sz = sizeof(struct mpi3mr_throttle_group_info); 4659 mrioc->throttle_groups = kcalloc(mrioc->num_io_throttle_group, sz, GFP_KERNEL); 4660 if (!mrioc->throttle_groups) { 4661 retval = -1; 4662 goto out_failed_noretry; 4663 } 4664 } 4665 4666 retval = mpi3mr_enable_events(mrioc); 4667 if (retval) { 4668 ioc_err(mrioc, "failed to enable events %d\n", 4669 retval); 4670 goto out_failed; 4671 } 4672 4673 retval = mpi3mr_refresh_trigger(mrioc, MPI3_CONFIG_ACTION_READ_CURRENT); 4674 if (retval) { 4675 ioc_err(mrioc, "failed to refresh triggers\n"); 4676 goto out_failed; 4677 } 4678 4679 ioc_info(mrioc, "controller initialization completed successfully\n"); 4680 return retval; 4681 out_failed: 4682 if (retry < 2) { 4683 retry++; 4684 ioc_warn(mrioc, "retrying controller initialization, retry_count:%d\n", 4685 retry); 4686 mpi3mr_memset_buffers(mrioc); 4687 goto retry_init; 4688 } 4689 retval = -1; 4690 out_failed_noretry: 4691 ioc_err(mrioc, "controller initialization failed\n"); 4692 mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, 4693 MPI3MR_RESET_FROM_CTLR_CLEANUP); 4694 mrioc->unrecoverable = 1; 4695 return retval; 4696 } 4697 4698 /** 4699 * mpi3mr_reinit_ioc - Re-Initialize the controller 4700 * @mrioc: Adapter instance reference 4701 * @is_resume: Called from resume or reset path 4702 * 4703 * This the controller re-initialization routine, executed from 4704 * the soft reset handler or resume callback. Creates 4705 * operational reply queue pairs, allocate required memory for 4706 * reply pool, sense buffer pool, issue IOC init request to the 4707 * firmware, unmask the events and issue port enable to discover 4708 * SAS/SATA/NVMe devices and RAID volumes. 4709 * 4710 * Return: 0 on success and non-zero on failure. 4711 */ 4712 int mpi3mr_reinit_ioc(struct mpi3mr_ioc *mrioc, u8 is_resume) 4713 { 4714 int retval = 0; 4715 u8 retry = 0; 4716 struct mpi3_ioc_facts_data facts_data; 4717 u32 pe_timeout, ioc_status; 4718 4719 retry_init: 4720 pe_timeout = 4721 (MPI3MR_PORTENABLE_TIMEOUT / MPI3MR_PORTENABLE_POLL_INTERVAL); 4722 4723 dprint_reset(mrioc, "bringing up the controller to ready state\n"); 4724 retval = mpi3mr_bring_ioc_ready(mrioc); 4725 if (retval) { 4726 ioc_err(mrioc, "failed to bring to ready state\n"); 4727 goto out_failed_noretry; 4728 } 4729 4730 mrioc->io_admin_reset_sync = 0; 4731 if (is_resume || mrioc->block_on_pci_err) { 4732 dprint_reset(mrioc, "setting up single ISR\n"); 4733 retval = mpi3mr_setup_isr(mrioc, 1); 4734 if (retval) { 4735 ioc_err(mrioc, "failed to setup ISR\n"); 4736 goto out_failed_noretry; 4737 } 4738 } else 4739 mpi3mr_ioc_enable_intr(mrioc); 4740 4741 dprint_reset(mrioc, "getting ioc_facts\n"); 4742 retval = mpi3mr_issue_iocfacts(mrioc, &facts_data); 4743 if (retval) { 4744 ioc_err(mrioc, "failed to get ioc_facts\n"); 4745 goto out_failed; 4746 } 4747 4748 dprint_reset(mrioc, "validating ioc_facts\n"); 4749 retval = mpi3mr_revalidate_factsdata(mrioc); 4750 if (retval) { 4751 ioc_err(mrioc, "failed to revalidate ioc_facts data\n"); 4752 goto out_failed_noretry; 4753 } 4754 4755 mpi3mr_read_driver_page1(mrioc); 4756 mpi3mr_print_ioc_info(mrioc); 4757 4758 if (is_resume) { 4759 dprint_reset(mrioc, "posting host diag buffers\n"); 4760 retval = mpi3mr_post_diag_bufs(mrioc); 4761 if (retval) 4762 ioc_warn(mrioc, "failed to post host diag buffers\n"); 4763 } else { 4764 retval = mpi3mr_repost_diag_bufs(mrioc); 4765 if (retval) 4766 ioc_warn(mrioc, "failed to re post host diag buffers\n"); 4767 } 4768 4769 dprint_reset(mrioc, "sending ioc_init\n"); 4770 retval = mpi3mr_issue_iocinit(mrioc); 4771 if (retval) { 4772 ioc_err(mrioc, "failed to send ioc_init\n"); 4773 goto out_failed; 4774 } 4775 4776 dprint_reset(mrioc, "getting package version\n"); 4777 retval = mpi3mr_print_pkg_ver(mrioc); 4778 if (retval) { 4779 ioc_err(mrioc, "failed to get package version\n"); 4780 goto out_failed; 4781 } 4782 4783 if (is_resume || mrioc->block_on_pci_err) { 4784 dprint_reset(mrioc, "setting up multiple ISR\n"); 4785 retval = mpi3mr_setup_isr(mrioc, 0); 4786 if (retval) { 4787 ioc_err(mrioc, "failed to re-setup ISR\n"); 4788 goto out_failed_noretry; 4789 } 4790 } 4791 4792 dprint_reset(mrioc, "creating operational queue pairs\n"); 4793 retval = mpi3mr_create_op_queues(mrioc); 4794 if (retval) { 4795 ioc_err(mrioc, "failed to create operational queue pairs\n"); 4796 goto out_failed; 4797 } 4798 4799 if (!mrioc->pel_seqnum_virt) { 4800 dprint_reset(mrioc, "allocating memory for pel_seqnum_virt\n"); 4801 mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq); 4802 mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev, 4803 mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma, 4804 GFP_KERNEL); 4805 if (!mrioc->pel_seqnum_virt) { 4806 retval = -ENOMEM; 4807 goto out_failed_noretry; 4808 } 4809 } 4810 4811 if (mrioc->shost->nr_hw_queues > mrioc->num_op_reply_q) { 4812 ioc_err(mrioc, 4813 "cannot create minimum number of operational queues expected:%d created:%d\n", 4814 mrioc->shost->nr_hw_queues, mrioc->num_op_reply_q); 4815 retval = -1; 4816 goto out_failed_noretry; 4817 } 4818 4819 dprint_reset(mrioc, "enabling events\n"); 4820 retval = mpi3mr_enable_events(mrioc); 4821 if (retval) { 4822 ioc_err(mrioc, "failed to enable events\n"); 4823 goto out_failed; 4824 } 4825 4826 mrioc->device_refresh_on = 1; 4827 mpi3mr_add_event_wait_for_device_refresh(mrioc); 4828 4829 ioc_info(mrioc, "sending port enable\n"); 4830 retval = mpi3mr_issue_port_enable(mrioc, 1); 4831 if (retval) { 4832 ioc_err(mrioc, "failed to issue port enable\n"); 4833 goto out_failed; 4834 } 4835 do { 4836 ssleep(MPI3MR_PORTENABLE_POLL_INTERVAL); 4837 if (mrioc->init_cmds.state == MPI3MR_CMD_NOTUSED) 4838 break; 4839 if (!pci_device_is_present(mrioc->pdev)) 4840 mrioc->unrecoverable = 1; 4841 if (mrioc->unrecoverable) { 4842 retval = -1; 4843 goto out_failed_noretry; 4844 } 4845 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 4846 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) || 4847 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) { 4848 mpi3mr_print_fault_info(mrioc); 4849 mrioc->init_cmds.is_waiting = 0; 4850 mrioc->init_cmds.callback = NULL; 4851 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 4852 goto out_failed; 4853 } 4854 } while (--pe_timeout); 4855 4856 if (!pe_timeout) { 4857 ioc_err(mrioc, "port enable timed out\n"); 4858 mpi3mr_check_rh_fault_ioc(mrioc, 4859 MPI3MR_RESET_FROM_PE_TIMEOUT); 4860 mrioc->init_cmds.is_waiting = 0; 4861 mrioc->init_cmds.callback = NULL; 4862 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED; 4863 goto out_failed; 4864 } else if (mrioc->scan_failed) { 4865 ioc_err(mrioc, 4866 "port enable failed with status=0x%04x\n", 4867 mrioc->scan_failed); 4868 } else 4869 ioc_info(mrioc, "port enable completed successfully\n"); 4870 4871 ioc_info(mrioc, "controller %s completed successfully\n", 4872 (is_resume)?"resume":"re-initialization"); 4873 return retval; 4874 out_failed: 4875 if (retry < 2) { 4876 retry++; 4877 ioc_warn(mrioc, "retrying controller %s, retry_count:%d\n", 4878 (is_resume)?"resume":"re-initialization", retry); 4879 mpi3mr_memset_buffers(mrioc); 4880 goto retry_init; 4881 } 4882 retval = -1; 4883 out_failed_noretry: 4884 ioc_err(mrioc, "controller %s is failed\n", 4885 (is_resume)?"resume":"re-initialization"); 4886 mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, 4887 MPI3MR_RESET_FROM_CTLR_CLEANUP); 4888 mrioc->unrecoverable = 1; 4889 return retval; 4890 } 4891 4892 /** 4893 * mpi3mr_memset_op_reply_q_buffers - memset the operational reply queue's 4894 * segments 4895 * @mrioc: Adapter instance reference 4896 * @qidx: Operational reply queue index 4897 * 4898 * Return: Nothing. 4899 */ 4900 static void mpi3mr_memset_op_reply_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx) 4901 { 4902 struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx; 4903 struct segments *segments; 4904 int i, size; 4905 4906 if (!op_reply_q->q_segments) 4907 return; 4908 4909 size = op_reply_q->segment_qd * mrioc->op_reply_desc_sz; 4910 segments = op_reply_q->q_segments; 4911 for (i = 0; i < op_reply_q->num_segments; i++) 4912 memset(segments[i].segment, 0, size); 4913 } 4914 4915 /** 4916 * mpi3mr_memset_op_req_q_buffers - memset the operational request queue's 4917 * segments 4918 * @mrioc: Adapter instance reference 4919 * @qidx: Operational request queue index 4920 * 4921 * Return: Nothing. 4922 */ 4923 static void mpi3mr_memset_op_req_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx) 4924 { 4925 struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx; 4926 struct segments *segments; 4927 int i, size; 4928 4929 if (!op_req_q->q_segments) 4930 return; 4931 4932 size = op_req_q->segment_qd * mrioc->facts.op_req_sz; 4933 segments = op_req_q->q_segments; 4934 for (i = 0; i < op_req_q->num_segments; i++) 4935 memset(segments[i].segment, 0, size); 4936 } 4937 4938 /** 4939 * mpi3mr_memset_buffers - memset memory for a controller 4940 * @mrioc: Adapter instance reference 4941 * 4942 * clear all the memory allocated for a controller, typically 4943 * called post reset to reuse the memory allocated during the 4944 * controller init. 4945 * 4946 * Return: Nothing. 4947 */ 4948 void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc) 4949 { 4950 u16 i; 4951 struct mpi3mr_throttle_group_info *tg; 4952 4953 mrioc->change_count = 0; 4954 mrioc->active_poll_qcount = 0; 4955 mrioc->default_qcount = 0; 4956 if (mrioc->admin_req_base) 4957 memset(mrioc->admin_req_base, 0, mrioc->admin_req_q_sz); 4958 if (mrioc->admin_reply_base) 4959 memset(mrioc->admin_reply_base, 0, mrioc->admin_reply_q_sz); 4960 atomic_set(&mrioc->admin_reply_q_in_use, 0); 4961 atomic_set(&mrioc->admin_pend_isr, 0); 4962 4963 if (mrioc->init_cmds.reply) { 4964 memset(mrioc->init_cmds.reply, 0, sizeof(*mrioc->init_cmds.reply)); 4965 memset(mrioc->bsg_cmds.reply, 0, 4966 sizeof(*mrioc->bsg_cmds.reply)); 4967 memset(mrioc->host_tm_cmds.reply, 0, 4968 sizeof(*mrioc->host_tm_cmds.reply)); 4969 memset(mrioc->pel_cmds.reply, 0, 4970 sizeof(*mrioc->pel_cmds.reply)); 4971 memset(mrioc->pel_abort_cmd.reply, 0, 4972 sizeof(*mrioc->pel_abort_cmd.reply)); 4973 memset(mrioc->transport_cmds.reply, 0, 4974 sizeof(*mrioc->transport_cmds.reply)); 4975 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) 4976 memset(mrioc->dev_rmhs_cmds[i].reply, 0, 4977 sizeof(*mrioc->dev_rmhs_cmds[i].reply)); 4978 for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) 4979 memset(mrioc->evtack_cmds[i].reply, 0, 4980 sizeof(*mrioc->evtack_cmds[i].reply)); 4981 bitmap_clear(mrioc->removepend_bitmap, 0, 4982 mrioc->dev_handle_bitmap_bits); 4983 bitmap_clear(mrioc->devrem_bitmap, 0, MPI3MR_NUM_DEVRMCMD); 4984 bitmap_clear(mrioc->evtack_cmds_bitmap, 0, 4985 MPI3MR_NUM_EVTACKCMD); 4986 } 4987 4988 for (i = 0; i < mrioc->num_queues; i++) { 4989 if (mrioc->op_reply_qinfo) { 4990 mrioc->op_reply_qinfo[i].qid = 0; 4991 mrioc->op_reply_qinfo[i].ci = 0; 4992 mrioc->op_reply_qinfo[i].num_replies = 0; 4993 mrioc->op_reply_qinfo[i].ephase = 0; 4994 atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0); 4995 atomic_set(&mrioc->op_reply_qinfo[i].in_use, 0); 4996 mpi3mr_memset_op_reply_q_buffers(mrioc, i); 4997 } 4998 4999 if (mrioc->req_qinfo) { 5000 mrioc->req_qinfo[i].ci = 0; 5001 mrioc->req_qinfo[i].pi = 0; 5002 mrioc->req_qinfo[i].num_requests = 0; 5003 mrioc->req_qinfo[i].qid = 0; 5004 mrioc->req_qinfo[i].reply_qid = 0; 5005 spin_lock_init(&mrioc->req_qinfo[i].q_lock); 5006 mrioc->req_qinfo[i].last_full_host_tag = 0; 5007 mpi3mr_memset_op_req_q_buffers(mrioc, i); 5008 } 5009 } 5010 5011 atomic_set(&mrioc->pend_large_data_sz, 0); 5012 if (mrioc->throttle_groups) { 5013 tg = mrioc->throttle_groups; 5014 for (i = 0; i < mrioc->num_io_throttle_group; i++, tg++) { 5015 tg->id = 0; 5016 tg->fw_qd = 0; 5017 tg->modified_qd = 0; 5018 tg->io_divert = 0; 5019 tg->need_qd_reduction = 0; 5020 tg->high = 0; 5021 tg->low = 0; 5022 tg->qd_reduction = 0; 5023 atomic_set(&tg->pend_large_data_sz, 0); 5024 } 5025 } 5026 } 5027 5028 /** 5029 * mpi3mr_free_mem - Free memory allocated for a controller 5030 * @mrioc: Adapter instance reference 5031 * 5032 * Free all the memory allocated for a controller. 5033 * 5034 * Return: Nothing. 5035 */ 5036 void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc) 5037 { 5038 u16 i, j; 5039 struct mpi3mr_intr_info *intr_info; 5040 struct diag_buffer_desc *diag_buffer; 5041 5042 mpi3mr_free_enclosure_list(mrioc); 5043 mpi3mr_free_ioctl_dma_memory(mrioc); 5044 5045 if (mrioc->sense_buf_pool) { 5046 if (mrioc->sense_buf) 5047 dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf, 5048 mrioc->sense_buf_dma); 5049 dma_pool_destroy(mrioc->sense_buf_pool); 5050 mrioc->sense_buf = NULL; 5051 mrioc->sense_buf_pool = NULL; 5052 } 5053 if (mrioc->sense_buf_q_pool) { 5054 if (mrioc->sense_buf_q) 5055 dma_pool_free(mrioc->sense_buf_q_pool, 5056 mrioc->sense_buf_q, mrioc->sense_buf_q_dma); 5057 dma_pool_destroy(mrioc->sense_buf_q_pool); 5058 mrioc->sense_buf_q = NULL; 5059 mrioc->sense_buf_q_pool = NULL; 5060 } 5061 5062 if (mrioc->reply_buf_pool) { 5063 if (mrioc->reply_buf) 5064 dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf, 5065 mrioc->reply_buf_dma); 5066 dma_pool_destroy(mrioc->reply_buf_pool); 5067 mrioc->reply_buf = NULL; 5068 mrioc->reply_buf_pool = NULL; 5069 } 5070 if (mrioc->reply_free_q_pool) { 5071 if (mrioc->reply_free_q) 5072 dma_pool_free(mrioc->reply_free_q_pool, 5073 mrioc->reply_free_q, mrioc->reply_free_q_dma); 5074 dma_pool_destroy(mrioc->reply_free_q_pool); 5075 mrioc->reply_free_q = NULL; 5076 mrioc->reply_free_q_pool = NULL; 5077 } 5078 5079 for (i = 0; i < mrioc->num_op_req_q; i++) 5080 mpi3mr_free_op_req_q_segments(mrioc, i); 5081 5082 for (i = 0; i < mrioc->num_op_reply_q; i++) 5083 mpi3mr_free_op_reply_q_segments(mrioc, i); 5084 5085 for (i = 0; i < mrioc->intr_info_count; i++) { 5086 intr_info = mrioc->intr_info + i; 5087 intr_info->op_reply_q = NULL; 5088 } 5089 5090 kfree(mrioc->req_qinfo); 5091 mrioc->req_qinfo = NULL; 5092 mrioc->num_op_req_q = 0; 5093 5094 kfree(mrioc->op_reply_qinfo); 5095 mrioc->op_reply_qinfo = NULL; 5096 mrioc->num_op_reply_q = 0; 5097 5098 kfree(mrioc->init_cmds.reply); 5099 mrioc->init_cmds.reply = NULL; 5100 5101 kfree(mrioc->bsg_cmds.reply); 5102 mrioc->bsg_cmds.reply = NULL; 5103 5104 kfree(mrioc->host_tm_cmds.reply); 5105 mrioc->host_tm_cmds.reply = NULL; 5106 5107 kfree(mrioc->pel_cmds.reply); 5108 mrioc->pel_cmds.reply = NULL; 5109 5110 kfree(mrioc->pel_abort_cmd.reply); 5111 mrioc->pel_abort_cmd.reply = NULL; 5112 5113 for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) { 5114 kfree(mrioc->evtack_cmds[i].reply); 5115 mrioc->evtack_cmds[i].reply = NULL; 5116 } 5117 5118 bitmap_free(mrioc->removepend_bitmap); 5119 mrioc->removepend_bitmap = NULL; 5120 5121 bitmap_free(mrioc->devrem_bitmap); 5122 mrioc->devrem_bitmap = NULL; 5123 5124 bitmap_free(mrioc->evtack_cmds_bitmap); 5125 mrioc->evtack_cmds_bitmap = NULL; 5126 5127 bitmap_free(mrioc->chain_bitmap); 5128 mrioc->chain_bitmap = NULL; 5129 5130 kfree(mrioc->transport_cmds.reply); 5131 mrioc->transport_cmds.reply = NULL; 5132 5133 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) { 5134 kfree(mrioc->dev_rmhs_cmds[i].reply); 5135 mrioc->dev_rmhs_cmds[i].reply = NULL; 5136 } 5137 5138 if (mrioc->chain_buf_pool) { 5139 for (i = 0; i < mrioc->chain_buf_count; i++) { 5140 if (mrioc->chain_sgl_list[i].addr) { 5141 dma_pool_free(mrioc->chain_buf_pool, 5142 mrioc->chain_sgl_list[i].addr, 5143 mrioc->chain_sgl_list[i].dma_addr); 5144 mrioc->chain_sgl_list[i].addr = NULL; 5145 } 5146 } 5147 dma_pool_destroy(mrioc->chain_buf_pool); 5148 mrioc->chain_buf_pool = NULL; 5149 } 5150 5151 kfree(mrioc->chain_sgl_list); 5152 mrioc->chain_sgl_list = NULL; 5153 5154 if (mrioc->admin_reply_base) { 5155 dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz, 5156 mrioc->admin_reply_base, mrioc->admin_reply_dma); 5157 mrioc->admin_reply_base = NULL; 5158 } 5159 if (mrioc->admin_req_base) { 5160 dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz, 5161 mrioc->admin_req_base, mrioc->admin_req_dma); 5162 mrioc->admin_req_base = NULL; 5163 } 5164 5165 if (mrioc->pel_seqnum_virt) { 5166 dma_free_coherent(&mrioc->pdev->dev, mrioc->pel_seqnum_sz, 5167 mrioc->pel_seqnum_virt, mrioc->pel_seqnum_dma); 5168 mrioc->pel_seqnum_virt = NULL; 5169 } 5170 5171 for (i = 0; i < MPI3MR_MAX_NUM_HDB; i++) { 5172 diag_buffer = &mrioc->diag_buffers[i]; 5173 if ((i == 0) && mrioc->seg_tb_support) { 5174 if (mrioc->trace_buf_pool) { 5175 for (j = 0; j < mrioc->num_tb_segs; j++) { 5176 if (mrioc->trace_buf[j].segment) { 5177 dma_pool_free(mrioc->trace_buf_pool, 5178 mrioc->trace_buf[j].segment, 5179 mrioc->trace_buf[j].segment_dma); 5180 mrioc->trace_buf[j].segment = NULL; 5181 } 5182 5183 mrioc->trace_buf[j].segment = NULL; 5184 } 5185 dma_pool_destroy(mrioc->trace_buf_pool); 5186 mrioc->trace_buf_pool = NULL; 5187 } 5188 5189 kfree(mrioc->trace_buf); 5190 mrioc->trace_buf = NULL; 5191 diag_buffer->size = sizeof(u64) * mrioc->num_tb_segs; 5192 } 5193 if (diag_buffer->addr) { 5194 dma_free_coherent(&mrioc->pdev->dev, 5195 diag_buffer->size, diag_buffer->addr, 5196 diag_buffer->dma_addr); 5197 diag_buffer->addr = NULL; 5198 diag_buffer->size = 0; 5199 diag_buffer->type = 0; 5200 diag_buffer->status = 0; 5201 } 5202 } 5203 5204 kfree(mrioc->throttle_groups); 5205 mrioc->throttle_groups = NULL; 5206 5207 kfree(mrioc->logdata_buf); 5208 mrioc->logdata_buf = NULL; 5209 5210 } 5211 5212 /** 5213 * mpi3mr_issue_ioc_shutdown - shutdown controller 5214 * @mrioc: Adapter instance reference 5215 * 5216 * Send shutodwn notification to the controller and wait for the 5217 * shutdown_timeout for it to be completed. 5218 * 5219 * Return: Nothing. 5220 */ 5221 static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc) 5222 { 5223 u32 ioc_config, ioc_status, shutdown_action; 5224 u8 retval = 1, retry = 0; 5225 u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10, timeout_remaining = 0; 5226 5227 ioc_info(mrioc, "Issuing shutdown Notification\n"); 5228 if (mrioc->unrecoverable) { 5229 ioc_warn(mrioc, 5230 "IOC is unrecoverable shutdown is not issued\n"); 5231 return; 5232 } 5233 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 5234 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK) 5235 == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) { 5236 ioc_info(mrioc, "shutdown already in progress\n"); 5237 return; 5238 } 5239 5240 shutdown_action = MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL; 5241 5242 if (!(mrioc->is_unload && mrioc->skip_dev_shutdown_on_unload)) 5243 shutdown_action |= 5244 MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ; 5245 else 5246 ioc_info(mrioc, 5247 "The shutdown request is issued without the device shutdown bit set\n" 5248 "as indicated by the controller configuration\n"); 5249 5250 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 5251 ioc_config |= shutdown_action; 5252 5253 writel(ioc_config, &mrioc->sysif_regs->ioc_configuration); 5254 5255 if (mrioc->facts.shutdown_timeout) 5256 timeout = mrioc->facts.shutdown_timeout * 10; 5257 timeout_remaining = timeout; 5258 5259 do { 5260 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 5261 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK) 5262 == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) { 5263 retval = 0; 5264 break; 5265 } 5266 if (mrioc->unrecoverable) 5267 break; 5268 if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) { 5269 mpi3mr_print_fault_info(mrioc); 5270 if (retry >= MPI3MR_MAX_SHUTDOWN_RETRY_COUNT) 5271 break; 5272 if (mpi3mr_issue_reset(mrioc, 5273 MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, 5274 MPI3MR_RESET_FROM_CTLR_CLEANUP)) 5275 break; 5276 ioc_config = 5277 readl(&mrioc->sysif_regs->ioc_configuration); 5278 ioc_config |= shutdown_action; 5279 writel(ioc_config, 5280 &mrioc->sysif_regs->ioc_configuration); 5281 timeout_remaining = timeout; 5282 retry++; 5283 } 5284 msleep(100); 5285 } while (--timeout_remaining); 5286 5287 ioc_status = readl(&mrioc->sysif_regs->ioc_status); 5288 ioc_config = readl(&mrioc->sysif_regs->ioc_configuration); 5289 5290 if (retval) { 5291 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK) 5292 == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) 5293 ioc_warn(mrioc, 5294 "shutdown still in progress after timeout\n"); 5295 } 5296 5297 ioc_info(mrioc, 5298 "Base IOC Sts/Config after %s shutdown is (0x%08x)/(0x%08x)\n", 5299 (!retval) ? "successful" : "failed", ioc_status, 5300 ioc_config); 5301 } 5302 5303 /** 5304 * mpi3mr_cleanup_ioc - Cleanup controller 5305 * @mrioc: Adapter instance reference 5306 * 5307 * controller cleanup handler, Message unit reset or soft reset 5308 * and shutdown notification is issued to the controller. 5309 * 5310 * Return: Nothing. 5311 */ 5312 void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc) 5313 { 5314 enum mpi3mr_iocstate ioc_state; 5315 5316 dprint_exit(mrioc, "cleaning up the controller\n"); 5317 mpi3mr_ioc_disable_intr(mrioc); 5318 5319 ioc_state = mpi3mr_get_iocstate(mrioc); 5320 5321 if (!mrioc->unrecoverable && !mrioc->reset_in_progress && 5322 !mrioc->pci_err_recovery && 5323 (ioc_state == MRIOC_STATE_READY)) { 5324 if (mpi3mr_issue_and_process_mur(mrioc, 5325 MPI3MR_RESET_FROM_CTLR_CLEANUP)) 5326 mpi3mr_issue_reset(mrioc, 5327 MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, 5328 MPI3MR_RESET_FROM_MUR_FAILURE); 5329 mpi3mr_issue_ioc_shutdown(mrioc); 5330 } 5331 dprint_exit(mrioc, "controller cleanup completed\n"); 5332 } 5333 5334 /** 5335 * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command 5336 * @mrioc: Adapter instance reference 5337 * @cmdptr: Internal command tracker 5338 * 5339 * Complete an internal driver commands with state indicating it 5340 * is completed due to reset. 5341 * 5342 * Return: Nothing. 5343 */ 5344 static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc, 5345 struct mpi3mr_drv_cmd *cmdptr) 5346 { 5347 if (cmdptr->state & MPI3MR_CMD_PENDING) { 5348 cmdptr->state |= MPI3MR_CMD_RESET; 5349 cmdptr->state &= ~MPI3MR_CMD_PENDING; 5350 if (cmdptr->is_waiting) { 5351 complete(&cmdptr->done); 5352 cmdptr->is_waiting = 0; 5353 } else if (cmdptr->callback) 5354 cmdptr->callback(mrioc, cmdptr); 5355 } 5356 } 5357 5358 /** 5359 * mpi3mr_flush_drv_cmds - Flush internaldriver commands 5360 * @mrioc: Adapter instance reference 5361 * 5362 * Flush all internal driver commands post reset 5363 * 5364 * Return: Nothing. 5365 */ 5366 void mpi3mr_flush_drv_cmds(struct mpi3mr_ioc *mrioc) 5367 { 5368 struct mpi3mr_drv_cmd *cmdptr; 5369 u8 i; 5370 5371 cmdptr = &mrioc->init_cmds; 5372 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5373 5374 cmdptr = &mrioc->cfg_cmds; 5375 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5376 5377 cmdptr = &mrioc->bsg_cmds; 5378 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5379 cmdptr = &mrioc->host_tm_cmds; 5380 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5381 5382 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) { 5383 cmdptr = &mrioc->dev_rmhs_cmds[i]; 5384 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5385 } 5386 5387 for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) { 5388 cmdptr = &mrioc->evtack_cmds[i]; 5389 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5390 } 5391 5392 cmdptr = &mrioc->pel_cmds; 5393 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5394 5395 cmdptr = &mrioc->pel_abort_cmd; 5396 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5397 5398 cmdptr = &mrioc->transport_cmds; 5399 mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr); 5400 } 5401 5402 /** 5403 * mpi3mr_pel_wait_post - Issue PEL Wait 5404 * @mrioc: Adapter instance reference 5405 * @drv_cmd: Internal command tracker 5406 * 5407 * Issue PEL Wait MPI request through admin queue and return. 5408 * 5409 * Return: Nothing. 5410 */ 5411 static void mpi3mr_pel_wait_post(struct mpi3mr_ioc *mrioc, 5412 struct mpi3mr_drv_cmd *drv_cmd) 5413 { 5414 struct mpi3_pel_req_action_wait pel_wait; 5415 5416 mrioc->pel_abort_requested = false; 5417 5418 memset(&pel_wait, 0, sizeof(pel_wait)); 5419 drv_cmd->state = MPI3MR_CMD_PENDING; 5420 drv_cmd->is_waiting = 0; 5421 drv_cmd->callback = mpi3mr_pel_wait_complete; 5422 drv_cmd->ioc_status = 0; 5423 drv_cmd->ioc_loginfo = 0; 5424 pel_wait.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT); 5425 pel_wait.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG; 5426 pel_wait.action = MPI3_PEL_ACTION_WAIT; 5427 pel_wait.starting_sequence_number = cpu_to_le32(mrioc->pel_newest_seqnum); 5428 pel_wait.locale = cpu_to_le16(mrioc->pel_locale); 5429 pel_wait.class = cpu_to_le16(mrioc->pel_class); 5430 pel_wait.wait_time = MPI3_PEL_WAITTIME_INFINITE_WAIT; 5431 dprint_bsg_info(mrioc, "sending pel_wait seqnum(%d), class(%d), locale(0x%08x)\n", 5432 mrioc->pel_newest_seqnum, mrioc->pel_class, mrioc->pel_locale); 5433 5434 if (mpi3mr_admin_request_post(mrioc, &pel_wait, sizeof(pel_wait), 0)) { 5435 dprint_bsg_err(mrioc, 5436 "Issuing PELWait: Admin post failed\n"); 5437 drv_cmd->state = MPI3MR_CMD_NOTUSED; 5438 drv_cmd->callback = NULL; 5439 drv_cmd->retry_count = 0; 5440 mrioc->pel_enabled = false; 5441 } 5442 } 5443 5444 /** 5445 * mpi3mr_pel_get_seqnum_post - Issue PEL Get Sequence number 5446 * @mrioc: Adapter instance reference 5447 * @drv_cmd: Internal command tracker 5448 * 5449 * Issue PEL get sequence number MPI request through admin queue 5450 * and return. 5451 * 5452 * Return: 0 on success, non-zero on failure. 5453 */ 5454 int mpi3mr_pel_get_seqnum_post(struct mpi3mr_ioc *mrioc, 5455 struct mpi3mr_drv_cmd *drv_cmd) 5456 { 5457 struct mpi3_pel_req_action_get_sequence_numbers pel_getseq_req; 5458 u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST; 5459 int retval = 0; 5460 5461 memset(&pel_getseq_req, 0, sizeof(pel_getseq_req)); 5462 mrioc->pel_cmds.state = MPI3MR_CMD_PENDING; 5463 mrioc->pel_cmds.is_waiting = 0; 5464 mrioc->pel_cmds.ioc_status = 0; 5465 mrioc->pel_cmds.ioc_loginfo = 0; 5466 mrioc->pel_cmds.callback = mpi3mr_pel_get_seqnum_complete; 5467 pel_getseq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT); 5468 pel_getseq_req.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG; 5469 pel_getseq_req.action = MPI3_PEL_ACTION_GET_SEQNUM; 5470 mpi3mr_add_sg_single(&pel_getseq_req.sgl, sgl_flags, 5471 mrioc->pel_seqnum_sz, mrioc->pel_seqnum_dma); 5472 5473 retval = mpi3mr_admin_request_post(mrioc, &pel_getseq_req, 5474 sizeof(pel_getseq_req), 0); 5475 if (retval) { 5476 if (drv_cmd) { 5477 drv_cmd->state = MPI3MR_CMD_NOTUSED; 5478 drv_cmd->callback = NULL; 5479 drv_cmd->retry_count = 0; 5480 } 5481 mrioc->pel_enabled = false; 5482 } 5483 5484 return retval; 5485 } 5486 5487 /** 5488 * mpi3mr_pel_wait_complete - PELWait Completion callback 5489 * @mrioc: Adapter instance reference 5490 * @drv_cmd: Internal command tracker 5491 * 5492 * This is a callback handler for the PELWait request and 5493 * firmware completes a PELWait request when it is aborted or a 5494 * new PEL entry is available. This sends AEN to the application 5495 * and if the PELwait completion is not due to PELAbort then 5496 * this will send a request for new PEL Sequence number 5497 * 5498 * Return: Nothing. 5499 */ 5500 static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc, 5501 struct mpi3mr_drv_cmd *drv_cmd) 5502 { 5503 struct mpi3_pel_reply *pel_reply = NULL; 5504 u16 ioc_status, pe_log_status; 5505 bool do_retry = false; 5506 5507 if (drv_cmd->state & MPI3MR_CMD_RESET) 5508 goto cleanup_drv_cmd; 5509 5510 ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK; 5511 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 5512 ioc_err(mrioc, "%s: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n", 5513 __func__, ioc_status, drv_cmd->ioc_loginfo); 5514 dprint_bsg_err(mrioc, 5515 "pel_wait: failed with ioc_status(0x%04x), log_info(0x%08x)\n", 5516 ioc_status, drv_cmd->ioc_loginfo); 5517 do_retry = true; 5518 } 5519 5520 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID) 5521 pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply; 5522 5523 if (!pel_reply) { 5524 dprint_bsg_err(mrioc, 5525 "pel_wait: failed due to no reply\n"); 5526 goto out_failed; 5527 } 5528 5529 pe_log_status = le16_to_cpu(pel_reply->pe_log_status); 5530 if ((pe_log_status != MPI3_PEL_STATUS_SUCCESS) && 5531 (pe_log_status != MPI3_PEL_STATUS_ABORTED)) { 5532 ioc_err(mrioc, "%s: Failed pe_log_status(0x%04x)\n", 5533 __func__, pe_log_status); 5534 dprint_bsg_err(mrioc, 5535 "pel_wait: failed due to pel_log_status(0x%04x)\n", 5536 pe_log_status); 5537 do_retry = true; 5538 } 5539 5540 if (do_retry) { 5541 if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) { 5542 drv_cmd->retry_count++; 5543 dprint_bsg_err(mrioc, "pel_wait: retrying(%d)\n", 5544 drv_cmd->retry_count); 5545 mpi3mr_pel_wait_post(mrioc, drv_cmd); 5546 return; 5547 } 5548 dprint_bsg_err(mrioc, 5549 "pel_wait: failed after all retries(%d)\n", 5550 drv_cmd->retry_count); 5551 goto out_failed; 5552 } 5553 atomic64_inc(&event_counter); 5554 if (!mrioc->pel_abort_requested) { 5555 mrioc->pel_cmds.retry_count = 0; 5556 mpi3mr_pel_get_seqnum_post(mrioc, &mrioc->pel_cmds); 5557 } 5558 5559 return; 5560 out_failed: 5561 mrioc->pel_enabled = false; 5562 cleanup_drv_cmd: 5563 drv_cmd->state = MPI3MR_CMD_NOTUSED; 5564 drv_cmd->callback = NULL; 5565 drv_cmd->retry_count = 0; 5566 } 5567 5568 /** 5569 * mpi3mr_pel_get_seqnum_complete - PELGetSeqNum Completion callback 5570 * @mrioc: Adapter instance reference 5571 * @drv_cmd: Internal command tracker 5572 * 5573 * This is a callback handler for the PEL get sequence number 5574 * request and a new PEL wait request will be issued to the 5575 * firmware from this 5576 * 5577 * Return: Nothing. 5578 */ 5579 void mpi3mr_pel_get_seqnum_complete(struct mpi3mr_ioc *mrioc, 5580 struct mpi3mr_drv_cmd *drv_cmd) 5581 { 5582 struct mpi3_pel_reply *pel_reply = NULL; 5583 struct mpi3_pel_seq *pel_seqnum_virt; 5584 u16 ioc_status; 5585 bool do_retry = false; 5586 5587 pel_seqnum_virt = (struct mpi3_pel_seq *)mrioc->pel_seqnum_virt; 5588 5589 if (drv_cmd->state & MPI3MR_CMD_RESET) 5590 goto cleanup_drv_cmd; 5591 5592 ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK; 5593 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 5594 dprint_bsg_err(mrioc, 5595 "pel_get_seqnum: failed with ioc_status(0x%04x), log_info(0x%08x)\n", 5596 ioc_status, drv_cmd->ioc_loginfo); 5597 do_retry = true; 5598 } 5599 5600 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID) 5601 pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply; 5602 if (!pel_reply) { 5603 dprint_bsg_err(mrioc, 5604 "pel_get_seqnum: failed due to no reply\n"); 5605 goto out_failed; 5606 } 5607 5608 if (le16_to_cpu(pel_reply->pe_log_status) != MPI3_PEL_STATUS_SUCCESS) { 5609 dprint_bsg_err(mrioc, 5610 "pel_get_seqnum: failed due to pel_log_status(0x%04x)\n", 5611 le16_to_cpu(pel_reply->pe_log_status)); 5612 do_retry = true; 5613 } 5614 5615 if (do_retry) { 5616 if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) { 5617 drv_cmd->retry_count++; 5618 dprint_bsg_err(mrioc, 5619 "pel_get_seqnum: retrying(%d)\n", 5620 drv_cmd->retry_count); 5621 mpi3mr_pel_get_seqnum_post(mrioc, drv_cmd); 5622 return; 5623 } 5624 5625 dprint_bsg_err(mrioc, 5626 "pel_get_seqnum: failed after all retries(%d)\n", 5627 drv_cmd->retry_count); 5628 goto out_failed; 5629 } 5630 mrioc->pel_newest_seqnum = le32_to_cpu(pel_seqnum_virt->newest) + 1; 5631 drv_cmd->retry_count = 0; 5632 mpi3mr_pel_wait_post(mrioc, drv_cmd); 5633 5634 return; 5635 out_failed: 5636 mrioc->pel_enabled = false; 5637 cleanup_drv_cmd: 5638 drv_cmd->state = MPI3MR_CMD_NOTUSED; 5639 drv_cmd->callback = NULL; 5640 drv_cmd->retry_count = 0; 5641 } 5642 5643 /** 5644 * mpi3mr_check_op_admin_proc - 5645 * @mrioc: Adapter instance reference 5646 * 5647 * Check if any of the operation reply queues 5648 * or the admin reply queue are currently in use. 5649 * If any queue is in use, this function waits for 5650 * a maximum of 10 seconds for them to become available. 5651 * 5652 * Return: 0 on success, non-zero on failure. 5653 */ 5654 static int mpi3mr_check_op_admin_proc(struct mpi3mr_ioc *mrioc) 5655 { 5656 5657 u16 timeout = 10 * 10; 5658 u16 elapsed_time = 0; 5659 bool op_admin_in_use = false; 5660 5661 do { 5662 op_admin_in_use = false; 5663 5664 /* Check admin_reply queue first to exit early */ 5665 if (atomic_read(&mrioc->admin_reply_q_in_use) == 1) 5666 op_admin_in_use = true; 5667 else { 5668 /* Check op_reply queues */ 5669 int i; 5670 5671 for (i = 0; i < mrioc->num_queues; i++) { 5672 if (atomic_read(&mrioc->op_reply_qinfo[i].in_use) == 1) { 5673 op_admin_in_use = true; 5674 break; 5675 } 5676 } 5677 } 5678 5679 if (!op_admin_in_use) 5680 break; 5681 5682 msleep(100); 5683 5684 } while (++elapsed_time < timeout); 5685 5686 if (op_admin_in_use) 5687 return 1; 5688 5689 return 0; 5690 } 5691 5692 /** 5693 * mpi3mr_soft_reset_handler - Reset the controller 5694 * @mrioc: Adapter instance reference 5695 * @reset_reason: Reset reason code 5696 * @snapdump: Flag to generate snapdump in firmware or not 5697 * 5698 * This is an handler for recovering controller by issuing soft 5699 * reset are diag fault reset. This is a blocking function and 5700 * when one reset is executed if any other resets they will be 5701 * blocked. All BSG requests will be blocked during the reset. If 5702 * controller reset is successful then the controller will be 5703 * reinitalized, otherwise the controller will be marked as not 5704 * recoverable 5705 * 5706 * In snapdump bit is set, the controller is issued with diag 5707 * fault reset so that the firmware can create a snap dump and 5708 * post that the firmware will result in F000 fault and the 5709 * driver will issue soft reset to recover from that. 5710 * 5711 * Return: 0 on success, non-zero on failure. 5712 */ 5713 int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc, 5714 u16 reset_reason, u8 snapdump) 5715 { 5716 int retval = 0, i; 5717 unsigned long flags; 5718 enum mpi3mr_iocstate ioc_state; 5719 u32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10; 5720 union mpi3mr_trigger_data trigger_data; 5721 5722 /* Block the reset handler until diag save in progress*/ 5723 dprint_reset(mrioc, 5724 "soft_reset_handler: check and block on diagsave_timeout(%d)\n", 5725 mrioc->diagsave_timeout); 5726 while (mrioc->diagsave_timeout) 5727 ssleep(1); 5728 /* 5729 * Block new resets until the currently executing one is finished and 5730 * return the status of the existing reset for all blocked resets 5731 */ 5732 dprint_reset(mrioc, "soft_reset_handler: acquiring reset_mutex\n"); 5733 if (!mutex_trylock(&mrioc->reset_mutex)) { 5734 ioc_info(mrioc, 5735 "controller reset triggered by %s is blocked due to another reset in progress\n", 5736 mpi3mr_reset_rc_name(reset_reason)); 5737 do { 5738 ssleep(1); 5739 } while (mrioc->reset_in_progress == 1); 5740 ioc_info(mrioc, 5741 "returning previous reset result(%d) for the reset triggered by %s\n", 5742 mrioc->prev_reset_result, 5743 mpi3mr_reset_rc_name(reset_reason)); 5744 return mrioc->prev_reset_result; 5745 } 5746 ioc_info(mrioc, "controller reset is triggered by %s\n", 5747 mpi3mr_reset_rc_name(reset_reason)); 5748 5749 mrioc->device_refresh_on = 0; 5750 scsi_block_requests(mrioc->shost); 5751 mrioc->reset_in_progress = 1; 5752 mrioc->stop_bsgs = 1; 5753 mrioc->prev_reset_result = -1; 5754 memset(&trigger_data, 0, sizeof(trigger_data)); 5755 5756 if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) && 5757 (reset_reason != MPI3MR_RESET_FROM_FIRMWARE) && 5758 (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) { 5759 mpi3mr_set_trigger_data_in_all_hdb(mrioc, 5760 MPI3MR_HDB_TRIGGER_TYPE_SOFT_RESET, NULL, 0); 5761 dprint_reset(mrioc, 5762 "soft_reset_handler: releasing host diagnostic buffers\n"); 5763 mpi3mr_release_diag_bufs(mrioc, 0); 5764 for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 5765 mrioc->event_masks[i] = -1; 5766 5767 dprint_reset(mrioc, "soft_reset_handler: masking events\n"); 5768 mpi3mr_issue_event_notification(mrioc); 5769 } 5770 5771 mpi3mr_wait_for_host_io(mrioc, MPI3MR_RESET_HOST_IOWAIT_TIMEOUT); 5772 5773 mpi3mr_ioc_disable_intr(mrioc); 5774 mrioc->io_admin_reset_sync = 1; 5775 5776 if (snapdump) { 5777 retval = mpi3mr_issue_reset(mrioc, 5778 MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason); 5779 if (!retval) { 5780 trigger_data.fault = (readl(&mrioc->sysif_regs->fault) & 5781 MPI3_SYSIF_FAULT_CODE_MASK); 5782 do { 5783 host_diagnostic = 5784 readl(&mrioc->sysif_regs->host_diagnostic); 5785 if (!(host_diagnostic & 5786 MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS)) 5787 break; 5788 msleep(100); 5789 } while (--timeout); 5790 5791 mpi3mr_save_fault_info(mrioc); 5792 mpi3mr_fault_uevent_emit(mrioc); 5793 mrioc->fwfault_counter++; 5794 mpi3mr_set_trigger_data_in_all_hdb(mrioc, 5795 MPI3MR_HDB_TRIGGER_TYPE_FAULT, &trigger_data, 0); 5796 } 5797 } 5798 5799 retval = mpi3mr_issue_reset(mrioc, 5800 MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason); 5801 if (retval) { 5802 ioc_err(mrioc, "Failed to issue soft reset to the ioc\n"); 5803 goto out; 5804 } 5805 5806 retval = mpi3mr_check_op_admin_proc(mrioc); 5807 if (retval) { 5808 ioc_err(mrioc, "Soft reset failed due to an Admin or I/O queue polling\n" 5809 "thread still processing replies even after a 10 second\n" 5810 "timeout. Marking the controller as unrecoverable!\n"); 5811 5812 goto out; 5813 } 5814 5815 if (mrioc->num_io_throttle_group != 5816 mrioc->facts.max_io_throttle_group) { 5817 ioc_err(mrioc, 5818 "max io throttle group doesn't match old(%d), new(%d)\n", 5819 mrioc->num_io_throttle_group, 5820 mrioc->facts.max_io_throttle_group); 5821 retval = -EPERM; 5822 goto out; 5823 } 5824 5825 mpi3mr_flush_delayed_cmd_lists(mrioc); 5826 mpi3mr_flush_drv_cmds(mrioc); 5827 bitmap_clear(mrioc->devrem_bitmap, 0, MPI3MR_NUM_DEVRMCMD); 5828 bitmap_clear(mrioc->removepend_bitmap, 0, 5829 mrioc->dev_handle_bitmap_bits); 5830 bitmap_clear(mrioc->evtack_cmds_bitmap, 0, MPI3MR_NUM_EVTACKCMD); 5831 mpi3mr_flush_host_io(mrioc); 5832 mpi3mr_cleanup_fwevt_list(mrioc); 5833 mpi3mr_invalidate_devhandles(mrioc); 5834 mpi3mr_free_enclosure_list(mrioc); 5835 5836 if (mrioc->prepare_for_reset) { 5837 mrioc->prepare_for_reset = 0; 5838 mrioc->prepare_for_reset_timeout_counter = 0; 5839 } 5840 mpi3mr_memset_buffers(mrioc); 5841 mpi3mr_release_diag_bufs(mrioc, 1); 5842 mrioc->fw_release_trigger_active = false; 5843 mrioc->trace_release_trigger_active = false; 5844 mrioc->snapdump_trigger_active = false; 5845 mpi3mr_set_trigger_data_in_all_hdb(mrioc, 5846 MPI3MR_HDB_TRIGGER_TYPE_SOFT_RESET, NULL, 0); 5847 5848 dprint_reset(mrioc, 5849 "soft_reset_handler: reinitializing the controller\n"); 5850 retval = mpi3mr_reinit_ioc(mrioc, 0); 5851 if (retval) { 5852 pr_err(IOCNAME "reinit after soft reset failed: reason %d\n", 5853 mrioc->name, reset_reason); 5854 goto out; 5855 } 5856 ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME); 5857 5858 out: 5859 mrioc->invalid_io_comp = 0; 5860 if (!retval) { 5861 mrioc->diagsave_timeout = 0; 5862 mrioc->reset_in_progress = 0; 5863 scsi_unblock_requests(mrioc->shost); 5864 mrioc->pel_abort_requested = 0; 5865 if (mrioc->pel_enabled) { 5866 mrioc->pel_cmds.retry_count = 0; 5867 mpi3mr_pel_wait_post(mrioc, &mrioc->pel_cmds); 5868 } 5869 5870 mrioc->device_refresh_on = 0; 5871 5872 mrioc->ts_update_counter = 0; 5873 spin_lock_irqsave(&mrioc->watchdog_lock, flags); 5874 if (mrioc->watchdog_work_q) 5875 queue_delayed_work(mrioc->watchdog_work_q, 5876 &mrioc->watchdog_work, 5877 msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL)); 5878 spin_unlock_irqrestore(&mrioc->watchdog_lock, flags); 5879 mrioc->stop_bsgs = 0; 5880 if (mrioc->pel_enabled) 5881 atomic64_inc(&event_counter); 5882 } else { 5883 dprint_reset(mrioc, 5884 "soft_reset_handler failed, marking controller as unrecoverable\n"); 5885 ioc_state = mpi3mr_get_iocstate(mrioc); 5886 5887 if (ioc_state != MRIOC_STATE_FAULT) 5888 mpi3mr_issue_reset(mrioc, 5889 MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason); 5890 mrioc->device_refresh_on = 0; 5891 mrioc->unrecoverable = 1; 5892 mrioc->reset_in_progress = 0; 5893 scsi_unblock_requests(mrioc->shost); 5894 mrioc->stop_bsgs = 0; 5895 retval = -1; 5896 mpi3mr_flush_cmds_for_unrecovered_controller(mrioc); 5897 } 5898 mrioc->prev_reset_result = retval; 5899 mutex_unlock(&mrioc->reset_mutex); 5900 ioc_info(mrioc, "controller reset is %s\n", 5901 ((retval == 0) ? "successful" : "failed")); 5902 return retval; 5903 } 5904 5905 /** 5906 * mpi3mr_post_cfg_req - Issue config requests and wait 5907 * @mrioc: Adapter instance reference 5908 * @cfg_req: Configuration request 5909 * @timeout: Timeout in seconds 5910 * @ioc_status: Pointer to return ioc status 5911 * 5912 * A generic function for posting MPI3 configuration request to 5913 * the firmware. This blocks for the completion of request for 5914 * timeout seconds and if the request times out this function 5915 * faults the controller with proper reason code. 5916 * 5917 * On successful completion of the request this function returns 5918 * appropriate ioc status from the firmware back to the caller. 5919 * 5920 * Return: 0 on success, non-zero on failure. 5921 */ 5922 static int mpi3mr_post_cfg_req(struct mpi3mr_ioc *mrioc, 5923 struct mpi3_config_request *cfg_req, int timeout, u16 *ioc_status) 5924 { 5925 int retval = 0; 5926 5927 mutex_lock(&mrioc->cfg_cmds.mutex); 5928 if (mrioc->cfg_cmds.state & MPI3MR_CMD_PENDING) { 5929 retval = -1; 5930 ioc_err(mrioc, "sending config request failed due to command in use\n"); 5931 mutex_unlock(&mrioc->cfg_cmds.mutex); 5932 goto out; 5933 } 5934 mrioc->cfg_cmds.state = MPI3MR_CMD_PENDING; 5935 mrioc->cfg_cmds.is_waiting = 1; 5936 mrioc->cfg_cmds.callback = NULL; 5937 mrioc->cfg_cmds.ioc_status = 0; 5938 mrioc->cfg_cmds.ioc_loginfo = 0; 5939 5940 cfg_req->host_tag = cpu_to_le16(MPI3MR_HOSTTAG_CFG_CMDS); 5941 cfg_req->function = MPI3_FUNCTION_CONFIG; 5942 5943 init_completion(&mrioc->cfg_cmds.done); 5944 dprint_cfg_info(mrioc, "posting config request\n"); 5945 if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO) 5946 dprint_dump(cfg_req, sizeof(struct mpi3_config_request), 5947 "mpi3_cfg_req"); 5948 retval = mpi3mr_admin_request_post(mrioc, cfg_req, sizeof(*cfg_req), 1); 5949 if (retval) { 5950 ioc_err(mrioc, "posting config request failed\n"); 5951 goto out_unlock; 5952 } 5953 wait_for_completion_timeout(&mrioc->cfg_cmds.done, (timeout * HZ)); 5954 if (!(mrioc->cfg_cmds.state & MPI3MR_CMD_COMPLETE)) { 5955 mpi3mr_check_rh_fault_ioc(mrioc, 5956 MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT); 5957 ioc_err(mrioc, "config request timed out\n"); 5958 retval = -1; 5959 goto out_unlock; 5960 } 5961 *ioc_status = mrioc->cfg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK; 5962 if ((*ioc_status) != MPI3_IOCSTATUS_SUCCESS) 5963 dprint_cfg_err(mrioc, 5964 "cfg_page request returned with ioc_status(0x%04x), log_info(0x%08x)\n", 5965 *ioc_status, mrioc->cfg_cmds.ioc_loginfo); 5966 5967 out_unlock: 5968 mrioc->cfg_cmds.state = MPI3MR_CMD_NOTUSED; 5969 mutex_unlock(&mrioc->cfg_cmds.mutex); 5970 5971 out: 5972 return retval; 5973 } 5974 5975 /** 5976 * mpi3mr_process_cfg_req - config page request processor 5977 * @mrioc: Adapter instance reference 5978 * @cfg_req: Configuration request 5979 * @cfg_hdr: Configuration page header 5980 * @timeout: Timeout in seconds 5981 * @ioc_status: Pointer to return ioc status 5982 * @cfg_buf: Memory pointer to copy config page or header 5983 * @cfg_buf_sz: Size of the memory to get config page or header 5984 * 5985 * This is handler for config page read, write and config page 5986 * header read operations. 5987 * 5988 * This function expects the cfg_req to be populated with page 5989 * type, page number, action for the header read and with page 5990 * address for all other operations. 5991 * 5992 * The cfg_hdr can be passed as null for reading required header 5993 * details for read/write pages the cfg_hdr should point valid 5994 * configuration page header. 5995 * 5996 * This allocates dmaable memory based on the size of the config 5997 * buffer and set the SGE of the cfg_req. 5998 * 5999 * For write actions, the config page data has to be passed in 6000 * the cfg_buf and size of the data has to be mentioned in the 6001 * cfg_buf_sz. 6002 * 6003 * For read/header actions, on successful completion of the 6004 * request with successful ioc_status the data will be copied 6005 * into the cfg_buf limited to a minimum of actual page size and 6006 * cfg_buf_sz 6007 * 6008 * 6009 * Return: 0 on success, non-zero on failure. 6010 */ 6011 static int mpi3mr_process_cfg_req(struct mpi3mr_ioc *mrioc, 6012 struct mpi3_config_request *cfg_req, 6013 struct mpi3_config_page_header *cfg_hdr, int timeout, u16 *ioc_status, 6014 void *cfg_buf, u32 cfg_buf_sz) 6015 { 6016 struct dma_memory_desc mem_desc; 6017 int retval = -1; 6018 u8 invalid_action = 0; 6019 u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST; 6020 6021 memset(&mem_desc, 0, sizeof(struct dma_memory_desc)); 6022 6023 if (cfg_req->action == MPI3_CONFIG_ACTION_PAGE_HEADER) 6024 mem_desc.size = sizeof(struct mpi3_config_page_header); 6025 else { 6026 if (!cfg_hdr) { 6027 ioc_err(mrioc, "null config header passed for config action(%d), page_type(0x%02x), page_num(%d)\n", 6028 cfg_req->action, cfg_req->page_type, 6029 cfg_req->page_number); 6030 goto out; 6031 } 6032 switch (cfg_hdr->page_attribute & MPI3_CONFIG_PAGEATTR_MASK) { 6033 case MPI3_CONFIG_PAGEATTR_READ_ONLY: 6034 if (cfg_req->action 6035 != MPI3_CONFIG_ACTION_READ_CURRENT) 6036 invalid_action = 1; 6037 break; 6038 case MPI3_CONFIG_PAGEATTR_CHANGEABLE: 6039 if ((cfg_req->action == 6040 MPI3_CONFIG_ACTION_READ_PERSISTENT) || 6041 (cfg_req->action == 6042 MPI3_CONFIG_ACTION_WRITE_PERSISTENT)) 6043 invalid_action = 1; 6044 break; 6045 case MPI3_CONFIG_PAGEATTR_PERSISTENT: 6046 default: 6047 break; 6048 } 6049 if (invalid_action) { 6050 ioc_err(mrioc, 6051 "config action(%d) is not allowed for page_type(0x%02x), page_num(%d) with page_attribute(0x%02x)\n", 6052 cfg_req->action, cfg_req->page_type, 6053 cfg_req->page_number, cfg_hdr->page_attribute); 6054 goto out; 6055 } 6056 mem_desc.size = le16_to_cpu(cfg_hdr->page_length) * 4; 6057 cfg_req->page_length = cfg_hdr->page_length; 6058 cfg_req->page_version = cfg_hdr->page_version; 6059 } 6060 6061 mem_desc.addr = dma_alloc_coherent(&mrioc->pdev->dev, 6062 mem_desc.size, &mem_desc.dma_addr, GFP_KERNEL); 6063 6064 if (!mem_desc.addr) 6065 return retval; 6066 6067 mpi3mr_add_sg_single(&cfg_req->sgl, sgl_flags, mem_desc.size, 6068 mem_desc.dma_addr); 6069 6070 if ((cfg_req->action == MPI3_CONFIG_ACTION_WRITE_PERSISTENT) || 6071 (cfg_req->action == MPI3_CONFIG_ACTION_WRITE_CURRENT)) { 6072 memcpy(mem_desc.addr, cfg_buf, min_t(u16, mem_desc.size, 6073 cfg_buf_sz)); 6074 dprint_cfg_info(mrioc, "config buffer to be written\n"); 6075 if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO) 6076 dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf"); 6077 } 6078 6079 if (mpi3mr_post_cfg_req(mrioc, cfg_req, timeout, ioc_status)) 6080 goto out; 6081 6082 retval = 0; 6083 if ((*ioc_status == MPI3_IOCSTATUS_SUCCESS) && 6084 (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_PERSISTENT) && 6085 (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_CURRENT)) { 6086 memcpy(cfg_buf, mem_desc.addr, min_t(u16, mem_desc.size, 6087 cfg_buf_sz)); 6088 dprint_cfg_info(mrioc, "config buffer read\n"); 6089 if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO) 6090 dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf"); 6091 } 6092 6093 out: 6094 if (mem_desc.addr) { 6095 dma_free_coherent(&mrioc->pdev->dev, mem_desc.size, 6096 mem_desc.addr, mem_desc.dma_addr); 6097 mem_desc.addr = NULL; 6098 } 6099 6100 return retval; 6101 } 6102 6103 /** 6104 * mpi3mr_cfg_get_dev_pg0 - Read current device page0 6105 * @mrioc: Adapter instance reference 6106 * @ioc_status: Pointer to return ioc status 6107 * @dev_pg0: Pointer to return device page 0 6108 * @pg_sz: Size of the memory allocated to the page pointer 6109 * @form: The form to be used for addressing the page 6110 * @form_spec: Form specific information like device handle 6111 * 6112 * This is handler for config page read for a specific device 6113 * page0. The ioc_status has the controller returned ioc_status. 6114 * This routine doesn't check ioc_status to decide whether the 6115 * page read is success or not and it is the callers 6116 * responsibility. 6117 * 6118 * Return: 0 on success, non-zero on failure. 6119 */ 6120 int mpi3mr_cfg_get_dev_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status, 6121 struct mpi3_device_page0 *dev_pg0, u16 pg_sz, u32 form, u32 form_spec) 6122 { 6123 struct mpi3_config_page_header cfg_hdr; 6124 struct mpi3_config_request cfg_req; 6125 u32 page_address; 6126 6127 memset(dev_pg0, 0, pg_sz); 6128 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6129 memset(&cfg_req, 0, sizeof(cfg_req)); 6130 6131 cfg_req.function = MPI3_FUNCTION_CONFIG; 6132 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6133 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_DEVICE; 6134 cfg_req.page_number = 0; 6135 cfg_req.page_address = 0; 6136 6137 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6138 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6139 ioc_err(mrioc, "device page0 header read failed\n"); 6140 goto out_failed; 6141 } 6142 if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6143 ioc_err(mrioc, "device page0 header read failed with ioc_status(0x%04x)\n", 6144 *ioc_status); 6145 goto out_failed; 6146 } 6147 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6148 page_address = ((form & MPI3_DEVICE_PGAD_FORM_MASK) | 6149 (form_spec & MPI3_DEVICE_PGAD_HANDLE_MASK)); 6150 cfg_req.page_address = cpu_to_le32(page_address); 6151 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6152 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, dev_pg0, pg_sz)) { 6153 ioc_err(mrioc, "device page0 read failed\n"); 6154 goto out_failed; 6155 } 6156 return 0; 6157 out_failed: 6158 return -1; 6159 } 6160 6161 6162 /** 6163 * mpi3mr_cfg_get_sas_phy_pg0 - Read current SAS Phy page0 6164 * @mrioc: Adapter instance reference 6165 * @ioc_status: Pointer to return ioc status 6166 * @phy_pg0: Pointer to return SAS Phy page 0 6167 * @pg_sz: Size of the memory allocated to the page pointer 6168 * @form: The form to be used for addressing the page 6169 * @form_spec: Form specific information like phy number 6170 * 6171 * This is handler for config page read for a specific SAS Phy 6172 * page0. The ioc_status has the controller returned ioc_status. 6173 * This routine doesn't check ioc_status to decide whether the 6174 * page read is success or not and it is the callers 6175 * responsibility. 6176 * 6177 * Return: 0 on success, non-zero on failure. 6178 */ 6179 int mpi3mr_cfg_get_sas_phy_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status, 6180 struct mpi3_sas_phy_page0 *phy_pg0, u16 pg_sz, u32 form, 6181 u32 form_spec) 6182 { 6183 struct mpi3_config_page_header cfg_hdr; 6184 struct mpi3_config_request cfg_req; 6185 u32 page_address; 6186 6187 memset(phy_pg0, 0, pg_sz); 6188 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6189 memset(&cfg_req, 0, sizeof(cfg_req)); 6190 6191 cfg_req.function = MPI3_FUNCTION_CONFIG; 6192 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6193 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_PHY; 6194 cfg_req.page_number = 0; 6195 cfg_req.page_address = 0; 6196 6197 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6198 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6199 ioc_err(mrioc, "sas phy page0 header read failed\n"); 6200 goto out_failed; 6201 } 6202 if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6203 ioc_err(mrioc, "sas phy page0 header read failed with ioc_status(0x%04x)\n", 6204 *ioc_status); 6205 goto out_failed; 6206 } 6207 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6208 page_address = ((form & MPI3_SAS_PHY_PGAD_FORM_MASK) | 6209 (form_spec & MPI3_SAS_PHY_PGAD_PHY_NUMBER_MASK)); 6210 cfg_req.page_address = cpu_to_le32(page_address); 6211 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6212 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, phy_pg0, pg_sz)) { 6213 ioc_err(mrioc, "sas phy page0 read failed\n"); 6214 goto out_failed; 6215 } 6216 return 0; 6217 out_failed: 6218 return -1; 6219 } 6220 6221 /** 6222 * mpi3mr_cfg_get_sas_phy_pg1 - Read current SAS Phy page1 6223 * @mrioc: Adapter instance reference 6224 * @ioc_status: Pointer to return ioc status 6225 * @phy_pg1: Pointer to return SAS Phy page 1 6226 * @pg_sz: Size of the memory allocated to the page pointer 6227 * @form: The form to be used for addressing the page 6228 * @form_spec: Form specific information like phy number 6229 * 6230 * This is handler for config page read for a specific SAS Phy 6231 * page1. The ioc_status has the controller returned ioc_status. 6232 * This routine doesn't check ioc_status to decide whether the 6233 * page read is success or not and it is the callers 6234 * responsibility. 6235 * 6236 * Return: 0 on success, non-zero on failure. 6237 */ 6238 int mpi3mr_cfg_get_sas_phy_pg1(struct mpi3mr_ioc *mrioc, u16 *ioc_status, 6239 struct mpi3_sas_phy_page1 *phy_pg1, u16 pg_sz, u32 form, 6240 u32 form_spec) 6241 { 6242 struct mpi3_config_page_header cfg_hdr; 6243 struct mpi3_config_request cfg_req; 6244 u32 page_address; 6245 6246 memset(phy_pg1, 0, pg_sz); 6247 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6248 memset(&cfg_req, 0, sizeof(cfg_req)); 6249 6250 cfg_req.function = MPI3_FUNCTION_CONFIG; 6251 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6252 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_PHY; 6253 cfg_req.page_number = 1; 6254 cfg_req.page_address = 0; 6255 6256 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6257 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6258 ioc_err(mrioc, "sas phy page1 header read failed\n"); 6259 goto out_failed; 6260 } 6261 if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6262 ioc_err(mrioc, "sas phy page1 header read failed with ioc_status(0x%04x)\n", 6263 *ioc_status); 6264 goto out_failed; 6265 } 6266 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6267 page_address = ((form & MPI3_SAS_PHY_PGAD_FORM_MASK) | 6268 (form_spec & MPI3_SAS_PHY_PGAD_PHY_NUMBER_MASK)); 6269 cfg_req.page_address = cpu_to_le32(page_address); 6270 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6271 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, phy_pg1, pg_sz)) { 6272 ioc_err(mrioc, "sas phy page1 read failed\n"); 6273 goto out_failed; 6274 } 6275 return 0; 6276 out_failed: 6277 return -1; 6278 } 6279 6280 6281 /** 6282 * mpi3mr_cfg_get_sas_exp_pg0 - Read current SAS Expander page0 6283 * @mrioc: Adapter instance reference 6284 * @ioc_status: Pointer to return ioc status 6285 * @exp_pg0: Pointer to return SAS Expander page 0 6286 * @pg_sz: Size of the memory allocated to the page pointer 6287 * @form: The form to be used for addressing the page 6288 * @form_spec: Form specific information like device handle 6289 * 6290 * This is handler for config page read for a specific SAS 6291 * Expander page0. The ioc_status has the controller returned 6292 * ioc_status. This routine doesn't check ioc_status to decide 6293 * whether the page read is success or not and it is the callers 6294 * responsibility. 6295 * 6296 * Return: 0 on success, non-zero on failure. 6297 */ 6298 int mpi3mr_cfg_get_sas_exp_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status, 6299 struct mpi3_sas_expander_page0 *exp_pg0, u16 pg_sz, u32 form, 6300 u32 form_spec) 6301 { 6302 struct mpi3_config_page_header cfg_hdr; 6303 struct mpi3_config_request cfg_req; 6304 u32 page_address; 6305 6306 memset(exp_pg0, 0, pg_sz); 6307 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6308 memset(&cfg_req, 0, sizeof(cfg_req)); 6309 6310 cfg_req.function = MPI3_FUNCTION_CONFIG; 6311 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6312 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_EXPANDER; 6313 cfg_req.page_number = 0; 6314 cfg_req.page_address = 0; 6315 6316 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6317 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6318 ioc_err(mrioc, "expander page0 header read failed\n"); 6319 goto out_failed; 6320 } 6321 if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6322 ioc_err(mrioc, "expander page0 header read failed with ioc_status(0x%04x)\n", 6323 *ioc_status); 6324 goto out_failed; 6325 } 6326 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6327 page_address = ((form & MPI3_SAS_EXPAND_PGAD_FORM_MASK) | 6328 (form_spec & (MPI3_SAS_EXPAND_PGAD_PHYNUM_MASK | 6329 MPI3_SAS_EXPAND_PGAD_HANDLE_MASK))); 6330 cfg_req.page_address = cpu_to_le32(page_address); 6331 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6332 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, exp_pg0, pg_sz)) { 6333 ioc_err(mrioc, "expander page0 read failed\n"); 6334 goto out_failed; 6335 } 6336 return 0; 6337 out_failed: 6338 return -1; 6339 } 6340 6341 /** 6342 * mpi3mr_cfg_get_sas_exp_pg1 - Read current SAS Expander page1 6343 * @mrioc: Adapter instance reference 6344 * @ioc_status: Pointer to return ioc status 6345 * @exp_pg1: Pointer to return SAS Expander page 1 6346 * @pg_sz: Size of the memory allocated to the page pointer 6347 * @form: The form to be used for addressing the page 6348 * @form_spec: Form specific information like phy number 6349 * 6350 * This is handler for config page read for a specific SAS 6351 * Expander page1. The ioc_status has the controller returned 6352 * ioc_status. This routine doesn't check ioc_status to decide 6353 * whether the page read is success or not and it is the callers 6354 * responsibility. 6355 * 6356 * Return: 0 on success, non-zero on failure. 6357 */ 6358 int mpi3mr_cfg_get_sas_exp_pg1(struct mpi3mr_ioc *mrioc, u16 *ioc_status, 6359 struct mpi3_sas_expander_page1 *exp_pg1, u16 pg_sz, u32 form, 6360 u32 form_spec) 6361 { 6362 struct mpi3_config_page_header cfg_hdr; 6363 struct mpi3_config_request cfg_req; 6364 u32 page_address; 6365 6366 memset(exp_pg1, 0, pg_sz); 6367 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6368 memset(&cfg_req, 0, sizeof(cfg_req)); 6369 6370 cfg_req.function = MPI3_FUNCTION_CONFIG; 6371 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6372 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_EXPANDER; 6373 cfg_req.page_number = 1; 6374 cfg_req.page_address = 0; 6375 6376 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6377 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6378 ioc_err(mrioc, "expander page1 header read failed\n"); 6379 goto out_failed; 6380 } 6381 if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6382 ioc_err(mrioc, "expander page1 header read failed with ioc_status(0x%04x)\n", 6383 *ioc_status); 6384 goto out_failed; 6385 } 6386 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6387 page_address = ((form & MPI3_SAS_EXPAND_PGAD_FORM_MASK) | 6388 (form_spec & (MPI3_SAS_EXPAND_PGAD_PHYNUM_MASK | 6389 MPI3_SAS_EXPAND_PGAD_HANDLE_MASK))); 6390 cfg_req.page_address = cpu_to_le32(page_address); 6391 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6392 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, exp_pg1, pg_sz)) { 6393 ioc_err(mrioc, "expander page1 read failed\n"); 6394 goto out_failed; 6395 } 6396 return 0; 6397 out_failed: 6398 return -1; 6399 } 6400 6401 /** 6402 * mpi3mr_cfg_get_enclosure_pg0 - Read current Enclosure page0 6403 * @mrioc: Adapter instance reference 6404 * @ioc_status: Pointer to return ioc status 6405 * @encl_pg0: Pointer to return Enclosure page 0 6406 * @pg_sz: Size of the memory allocated to the page pointer 6407 * @form: The form to be used for addressing the page 6408 * @form_spec: Form specific information like device handle 6409 * 6410 * This is handler for config page read for a specific Enclosure 6411 * page0. The ioc_status has the controller returned ioc_status. 6412 * This routine doesn't check ioc_status to decide whether the 6413 * page read is success or not and it is the callers 6414 * responsibility. 6415 * 6416 * Return: 0 on success, non-zero on failure. 6417 */ 6418 int mpi3mr_cfg_get_enclosure_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status, 6419 struct mpi3_enclosure_page0 *encl_pg0, u16 pg_sz, u32 form, 6420 u32 form_spec) 6421 { 6422 struct mpi3_config_page_header cfg_hdr; 6423 struct mpi3_config_request cfg_req; 6424 u32 page_address; 6425 6426 memset(encl_pg0, 0, pg_sz); 6427 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6428 memset(&cfg_req, 0, sizeof(cfg_req)); 6429 6430 cfg_req.function = MPI3_FUNCTION_CONFIG; 6431 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6432 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_ENCLOSURE; 6433 cfg_req.page_number = 0; 6434 cfg_req.page_address = 0; 6435 6436 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6437 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6438 ioc_err(mrioc, "enclosure page0 header read failed\n"); 6439 goto out_failed; 6440 } 6441 if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6442 ioc_err(mrioc, "enclosure page0 header read failed with ioc_status(0x%04x)\n", 6443 *ioc_status); 6444 goto out_failed; 6445 } 6446 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6447 page_address = ((form & MPI3_ENCLOS_PGAD_FORM_MASK) | 6448 (form_spec & MPI3_ENCLOS_PGAD_HANDLE_MASK)); 6449 cfg_req.page_address = cpu_to_le32(page_address); 6450 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6451 MPI3MR_INTADMCMD_TIMEOUT, ioc_status, encl_pg0, pg_sz)) { 6452 ioc_err(mrioc, "enclosure page0 read failed\n"); 6453 goto out_failed; 6454 } 6455 return 0; 6456 out_failed: 6457 return -1; 6458 } 6459 6460 6461 /** 6462 * mpi3mr_cfg_get_sas_io_unit_pg0 - Read current SASIOUnit page0 6463 * @mrioc: Adapter instance reference 6464 * @sas_io_unit_pg0: Pointer to return SAS IO Unit page 0 6465 * @pg_sz: Size of the memory allocated to the page pointer 6466 * 6467 * This is handler for config page read for the SAS IO Unit 6468 * page0. This routine checks ioc_status to decide whether the 6469 * page read is success or not. 6470 * 6471 * Return: 0 on success, non-zero on failure. 6472 */ 6473 int mpi3mr_cfg_get_sas_io_unit_pg0(struct mpi3mr_ioc *mrioc, 6474 struct mpi3_sas_io_unit_page0 *sas_io_unit_pg0, u16 pg_sz) 6475 { 6476 struct mpi3_config_page_header cfg_hdr; 6477 struct mpi3_config_request cfg_req; 6478 u16 ioc_status = 0; 6479 6480 memset(sas_io_unit_pg0, 0, pg_sz); 6481 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6482 memset(&cfg_req, 0, sizeof(cfg_req)); 6483 6484 cfg_req.function = MPI3_FUNCTION_CONFIG; 6485 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6486 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT; 6487 cfg_req.page_number = 0; 6488 cfg_req.page_address = 0; 6489 6490 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6491 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6492 ioc_err(mrioc, "sas io unit page0 header read failed\n"); 6493 goto out_failed; 6494 } 6495 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6496 ioc_err(mrioc, "sas io unit page0 header read failed with ioc_status(0x%04x)\n", 6497 ioc_status); 6498 goto out_failed; 6499 } 6500 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6501 6502 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6503 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg0, pg_sz)) { 6504 ioc_err(mrioc, "sas io unit page0 read failed\n"); 6505 goto out_failed; 6506 } 6507 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6508 ioc_err(mrioc, "sas io unit page0 read failed with ioc_status(0x%04x)\n", 6509 ioc_status); 6510 goto out_failed; 6511 } 6512 return 0; 6513 out_failed: 6514 return -1; 6515 } 6516 6517 /** 6518 * mpi3mr_cfg_get_sas_io_unit_pg1 - Read current SASIOUnit page1 6519 * @mrioc: Adapter instance reference 6520 * @sas_io_unit_pg1: Pointer to return SAS IO Unit page 1 6521 * @pg_sz: Size of the memory allocated to the page pointer 6522 * 6523 * This is handler for config page read for the SAS IO Unit 6524 * page1. This routine checks ioc_status to decide whether the 6525 * page read is success or not. 6526 * 6527 * Return: 0 on success, non-zero on failure. 6528 */ 6529 int mpi3mr_cfg_get_sas_io_unit_pg1(struct mpi3mr_ioc *mrioc, 6530 struct mpi3_sas_io_unit_page1 *sas_io_unit_pg1, u16 pg_sz) 6531 { 6532 struct mpi3_config_page_header cfg_hdr; 6533 struct mpi3_config_request cfg_req; 6534 u16 ioc_status = 0; 6535 6536 memset(sas_io_unit_pg1, 0, pg_sz); 6537 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6538 memset(&cfg_req, 0, sizeof(cfg_req)); 6539 6540 cfg_req.function = MPI3_FUNCTION_CONFIG; 6541 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6542 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT; 6543 cfg_req.page_number = 1; 6544 cfg_req.page_address = 0; 6545 6546 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6547 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6548 ioc_err(mrioc, "sas io unit page1 header read failed\n"); 6549 goto out_failed; 6550 } 6551 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6552 ioc_err(mrioc, "sas io unit page1 header read failed with ioc_status(0x%04x)\n", 6553 ioc_status); 6554 goto out_failed; 6555 } 6556 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6557 6558 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6559 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) { 6560 ioc_err(mrioc, "sas io unit page1 read failed\n"); 6561 goto out_failed; 6562 } 6563 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6564 ioc_err(mrioc, "sas io unit page1 read failed with ioc_status(0x%04x)\n", 6565 ioc_status); 6566 goto out_failed; 6567 } 6568 return 0; 6569 out_failed: 6570 return -1; 6571 } 6572 6573 /** 6574 * mpi3mr_cfg_set_sas_io_unit_pg1 - Write SASIOUnit page1 6575 * @mrioc: Adapter instance reference 6576 * @sas_io_unit_pg1: Pointer to the SAS IO Unit page 1 to write 6577 * @pg_sz: Size of the memory allocated to the page pointer 6578 * 6579 * This is handler for config page write for the SAS IO Unit 6580 * page1. This routine checks ioc_status to decide whether the 6581 * page read is success or not. This will modify both current 6582 * and persistent page. 6583 * 6584 * Return: 0 on success, non-zero on failure. 6585 */ 6586 int mpi3mr_cfg_set_sas_io_unit_pg1(struct mpi3mr_ioc *mrioc, 6587 struct mpi3_sas_io_unit_page1 *sas_io_unit_pg1, u16 pg_sz) 6588 { 6589 struct mpi3_config_page_header cfg_hdr; 6590 struct mpi3_config_request cfg_req; 6591 u16 ioc_status = 0; 6592 6593 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6594 memset(&cfg_req, 0, sizeof(cfg_req)); 6595 6596 cfg_req.function = MPI3_FUNCTION_CONFIG; 6597 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6598 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT; 6599 cfg_req.page_number = 1; 6600 cfg_req.page_address = 0; 6601 6602 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6603 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6604 ioc_err(mrioc, "sas io unit page1 header read failed\n"); 6605 goto out_failed; 6606 } 6607 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6608 ioc_err(mrioc, "sas io unit page1 header read failed with ioc_status(0x%04x)\n", 6609 ioc_status); 6610 goto out_failed; 6611 } 6612 cfg_req.action = MPI3_CONFIG_ACTION_WRITE_CURRENT; 6613 6614 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6615 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) { 6616 ioc_err(mrioc, "sas io unit page1 write current failed\n"); 6617 goto out_failed; 6618 } 6619 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6620 ioc_err(mrioc, "sas io unit page1 write current failed with ioc_status(0x%04x)\n", 6621 ioc_status); 6622 goto out_failed; 6623 } 6624 6625 cfg_req.action = MPI3_CONFIG_ACTION_WRITE_PERSISTENT; 6626 6627 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6628 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) { 6629 ioc_err(mrioc, "sas io unit page1 write persistent failed\n"); 6630 goto out_failed; 6631 } 6632 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6633 ioc_err(mrioc, "sas io unit page1 write persistent failed with ioc_status(0x%04x)\n", 6634 ioc_status); 6635 goto out_failed; 6636 } 6637 return 0; 6638 out_failed: 6639 return -1; 6640 } 6641 6642 /** 6643 * mpi3mr_cfg_get_driver_pg1 - Read current Driver page1 6644 * @mrioc: Adapter instance reference 6645 * @driver_pg1: Pointer to return Driver page 1 6646 * @pg_sz: Size of the memory allocated to the page pointer 6647 * 6648 * This is handler for config page read for the Driver page1. 6649 * This routine checks ioc_status to decide whether the page 6650 * read is success or not. 6651 * 6652 * Return: 0 on success, non-zero on failure. 6653 */ 6654 int mpi3mr_cfg_get_driver_pg1(struct mpi3mr_ioc *mrioc, 6655 struct mpi3_driver_page1 *driver_pg1, u16 pg_sz) 6656 { 6657 struct mpi3_config_page_header cfg_hdr; 6658 struct mpi3_config_request cfg_req; 6659 u16 ioc_status = 0; 6660 6661 memset(driver_pg1, 0, pg_sz); 6662 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6663 memset(&cfg_req, 0, sizeof(cfg_req)); 6664 6665 cfg_req.function = MPI3_FUNCTION_CONFIG; 6666 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6667 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_DRIVER; 6668 cfg_req.page_number = 1; 6669 cfg_req.page_address = 0; 6670 6671 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6672 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6673 ioc_err(mrioc, "driver page1 header read failed\n"); 6674 goto out_failed; 6675 } 6676 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6677 ioc_err(mrioc, "driver page1 header read failed with ioc_status(0x%04x)\n", 6678 ioc_status); 6679 goto out_failed; 6680 } 6681 cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT; 6682 6683 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6684 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, driver_pg1, pg_sz)) { 6685 ioc_err(mrioc, "driver page1 read failed\n"); 6686 goto out_failed; 6687 } 6688 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6689 ioc_err(mrioc, "driver page1 read failed with ioc_status(0x%04x)\n", 6690 ioc_status); 6691 goto out_failed; 6692 } 6693 return 0; 6694 out_failed: 6695 return -1; 6696 } 6697 6698 /** 6699 * mpi3mr_cfg_get_driver_pg2 - Read current driver page2 6700 * @mrioc: Adapter instance reference 6701 * @driver_pg2: Pointer to return driver page 2 6702 * @pg_sz: Size of the memory allocated to the page pointer 6703 * @page_action: Page action 6704 * 6705 * This is handler for config page read for the driver page2. 6706 * This routine checks ioc_status to decide whether the page 6707 * read is success or not. 6708 * 6709 * Return: 0 on success, non-zero on failure. 6710 */ 6711 int mpi3mr_cfg_get_driver_pg2(struct mpi3mr_ioc *mrioc, 6712 struct mpi3_driver_page2 *driver_pg2, u16 pg_sz, u8 page_action) 6713 { 6714 struct mpi3_config_page_header cfg_hdr; 6715 struct mpi3_config_request cfg_req; 6716 u16 ioc_status = 0; 6717 6718 memset(driver_pg2, 0, pg_sz); 6719 memset(&cfg_hdr, 0, sizeof(cfg_hdr)); 6720 memset(&cfg_req, 0, sizeof(cfg_req)); 6721 6722 cfg_req.function = MPI3_FUNCTION_CONFIG; 6723 cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER; 6724 cfg_req.page_type = MPI3_CONFIG_PAGETYPE_DRIVER; 6725 cfg_req.page_number = 2; 6726 cfg_req.page_address = 0; 6727 cfg_req.page_version = MPI3_DRIVER2_PAGEVERSION; 6728 6729 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL, 6730 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) { 6731 ioc_err(mrioc, "driver page2 header read failed\n"); 6732 goto out_failed; 6733 } 6734 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6735 ioc_err(mrioc, "driver page2 header read failed with\n" 6736 "ioc_status(0x%04x)\n", 6737 ioc_status); 6738 goto out_failed; 6739 } 6740 cfg_req.action = page_action; 6741 6742 if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr, 6743 MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, driver_pg2, pg_sz)) { 6744 ioc_err(mrioc, "driver page2 read failed\n"); 6745 goto out_failed; 6746 } 6747 if (ioc_status != MPI3_IOCSTATUS_SUCCESS) { 6748 ioc_err(mrioc, "driver page2 read failed with\n" 6749 "ioc_status(0x%04x)\n", 6750 ioc_status); 6751 goto out_failed; 6752 } 6753 return 0; 6754 out_failed: 6755 return -1; 6756 } 6757 6758