1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Broadcom MPI3 Storage Controllers 4 * 5 * Copyright (C) 2017-2022 Broadcom Inc. 6 * (mailto: mpi3mr-linuxdrv.pdl@broadcom.com) 7 * 8 */ 9 10 #include "mpi3mr.h" 11 #include <linux/bsg-lib.h> 12 #include <uapi/scsi/scsi_bsg_mpi3mr.h> 13 14 /** 15 * mpi3mr_bsg_pel_abort - sends PEL abort request 16 * @mrioc: Adapter instance reference 17 * 18 * This function sends PEL abort request to the firmware through 19 * admin request queue. 20 * 21 * Return: 0 on success, -1 on failure 22 */ 23 static int mpi3mr_bsg_pel_abort(struct mpi3mr_ioc *mrioc) 24 { 25 struct mpi3_pel_req_action_abort pel_abort_req; 26 struct mpi3_pel_reply *pel_reply; 27 int retval = 0; 28 u16 pe_log_status; 29 30 if (mrioc->reset_in_progress) { 31 dprint_bsg_err(mrioc, "%s: reset in progress\n", __func__); 32 return -1; 33 } 34 if (mrioc->stop_bsgs) { 35 dprint_bsg_err(mrioc, "%s: bsgs are blocked\n", __func__); 36 return -1; 37 } 38 39 memset(&pel_abort_req, 0, sizeof(pel_abort_req)); 40 mutex_lock(&mrioc->pel_abort_cmd.mutex); 41 if (mrioc->pel_abort_cmd.state & MPI3MR_CMD_PENDING) { 42 dprint_bsg_err(mrioc, "%s: command is in use\n", __func__); 43 mutex_unlock(&mrioc->pel_abort_cmd.mutex); 44 return -1; 45 } 46 mrioc->pel_abort_cmd.state = MPI3MR_CMD_PENDING; 47 mrioc->pel_abort_cmd.is_waiting = 1; 48 mrioc->pel_abort_cmd.callback = NULL; 49 pel_abort_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_ABORT); 50 pel_abort_req.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG; 51 pel_abort_req.action = MPI3_PEL_ACTION_ABORT; 52 pel_abort_req.abort_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT); 53 54 mrioc->pel_abort_requested = 1; 55 init_completion(&mrioc->pel_abort_cmd.done); 56 retval = mpi3mr_admin_request_post(mrioc, &pel_abort_req, 57 sizeof(pel_abort_req), 0); 58 if (retval) { 59 retval = -1; 60 dprint_bsg_err(mrioc, "%s: admin request post failed\n", 61 __func__); 62 mrioc->pel_abort_requested = 0; 63 goto out_unlock; 64 } 65 66 wait_for_completion_timeout(&mrioc->pel_abort_cmd.done, 67 (MPI3MR_INTADMCMD_TIMEOUT * HZ)); 68 if (!(mrioc->pel_abort_cmd.state & MPI3MR_CMD_COMPLETE)) { 69 mrioc->pel_abort_cmd.is_waiting = 0; 70 dprint_bsg_err(mrioc, "%s: command timedout\n", __func__); 71 if (!(mrioc->pel_abort_cmd.state & MPI3MR_CMD_RESET)) 72 mpi3mr_soft_reset_handler(mrioc, 73 MPI3MR_RESET_FROM_PELABORT_TIMEOUT, 1); 74 retval = -1; 75 goto out_unlock; 76 } 77 if ((mrioc->pel_abort_cmd.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 78 != MPI3_IOCSTATUS_SUCCESS) { 79 dprint_bsg_err(mrioc, 80 "%s: command failed, ioc_status(0x%04x) log_info(0x%08x)\n", 81 __func__, (mrioc->pel_abort_cmd.ioc_status & 82 MPI3_IOCSTATUS_STATUS_MASK), 83 mrioc->pel_abort_cmd.ioc_loginfo); 84 retval = -1; 85 goto out_unlock; 86 } 87 if (mrioc->pel_abort_cmd.state & MPI3MR_CMD_REPLY_VALID) { 88 pel_reply = (struct mpi3_pel_reply *)mrioc->pel_abort_cmd.reply; 89 pe_log_status = le16_to_cpu(pel_reply->pe_log_status); 90 if (pe_log_status != MPI3_PEL_STATUS_SUCCESS) { 91 dprint_bsg_err(mrioc, 92 "%s: command failed, pel_status(0x%04x)\n", 93 __func__, pe_log_status); 94 retval = -1; 95 } 96 } 97 98 out_unlock: 99 mrioc->pel_abort_cmd.state = MPI3MR_CMD_NOTUSED; 100 mutex_unlock(&mrioc->pel_abort_cmd.mutex); 101 return retval; 102 } 103 /** 104 * mpi3mr_bsg_verify_adapter - verify adapter number is valid 105 * @ioc_number: Adapter number 106 * 107 * This function returns the adapter instance pointer of given 108 * adapter number. If adapter number does not match with the 109 * driver's adapter list, driver returns NULL. 110 * 111 * Return: adapter instance reference 112 */ 113 static struct mpi3mr_ioc *mpi3mr_bsg_verify_adapter(int ioc_number) 114 { 115 struct mpi3mr_ioc *mrioc = NULL; 116 117 spin_lock(&mrioc_list_lock); 118 list_for_each_entry(mrioc, &mrioc_list, list) { 119 if (mrioc->id == ioc_number) { 120 spin_unlock(&mrioc_list_lock); 121 return mrioc; 122 } 123 } 124 spin_unlock(&mrioc_list_lock); 125 return NULL; 126 } 127 128 /** 129 * mpi3mr_enable_logdata - Handler for log data enable 130 * @mrioc: Adapter instance reference 131 * @job: BSG job reference 132 * 133 * This function enables log data caching in the driver if not 134 * already enabled and return the maximum number of log data 135 * entries that can be cached in the driver. 136 * 137 * Return: 0 on success and proper error codes on failure 138 */ 139 static long mpi3mr_enable_logdata(struct mpi3mr_ioc *mrioc, 140 struct bsg_job *job) 141 { 142 struct mpi3mr_logdata_enable logdata_enable; 143 144 if (!mrioc->logdata_buf) { 145 mrioc->logdata_entry_sz = 146 (mrioc->reply_sz - (sizeof(struct mpi3_event_notification_reply) - 4)) 147 + MPI3MR_BSG_LOGDATA_ENTRY_HEADER_SZ; 148 mrioc->logdata_buf_idx = 0; 149 mrioc->logdata_buf = kcalloc(MPI3MR_BSG_LOGDATA_MAX_ENTRIES, 150 mrioc->logdata_entry_sz, GFP_KERNEL); 151 152 if (!mrioc->logdata_buf) 153 return -ENOMEM; 154 } 155 156 memset(&logdata_enable, 0, sizeof(logdata_enable)); 157 logdata_enable.max_entries = 158 MPI3MR_BSG_LOGDATA_MAX_ENTRIES; 159 if (job->request_payload.payload_len >= sizeof(logdata_enable)) { 160 sg_copy_from_buffer(job->request_payload.sg_list, 161 job->request_payload.sg_cnt, 162 &logdata_enable, sizeof(logdata_enable)); 163 return 0; 164 } 165 166 return -EINVAL; 167 } 168 /** 169 * mpi3mr_get_logdata - Handler for get log data 170 * @mrioc: Adapter instance reference 171 * @job: BSG job pointer 172 * This function copies the log data entries to the user buffer 173 * when log caching is enabled in the driver. 174 * 175 * Return: 0 on success and proper error codes on failure 176 */ 177 static long mpi3mr_get_logdata(struct mpi3mr_ioc *mrioc, 178 struct bsg_job *job) 179 { 180 u16 num_entries, sz, entry_sz = mrioc->logdata_entry_sz; 181 182 if ((!mrioc->logdata_buf) || (job->request_payload.payload_len < entry_sz)) 183 return -EINVAL; 184 185 num_entries = job->request_payload.payload_len / entry_sz; 186 if (num_entries > MPI3MR_BSG_LOGDATA_MAX_ENTRIES) 187 num_entries = MPI3MR_BSG_LOGDATA_MAX_ENTRIES; 188 sz = num_entries * entry_sz; 189 190 if (job->request_payload.payload_len >= sz) { 191 sg_copy_from_buffer(job->request_payload.sg_list, 192 job->request_payload.sg_cnt, 193 mrioc->logdata_buf, sz); 194 return 0; 195 } 196 return -EINVAL; 197 } 198 199 /** 200 * mpi3mr_bsg_pel_enable - Handler for PEL enable driver 201 * @mrioc: Adapter instance reference 202 * @job: BSG job pointer 203 * 204 * This function is the handler for PEL enable driver. 205 * Validates the application given class and locale and if 206 * requires aborts the existing PEL wait request and/or issues 207 * new PEL wait request to the firmware and returns. 208 * 209 * Return: 0 on success and proper error codes on failure. 210 */ 211 static long mpi3mr_bsg_pel_enable(struct mpi3mr_ioc *mrioc, 212 struct bsg_job *job) 213 { 214 long rval = -EINVAL; 215 struct mpi3mr_bsg_out_pel_enable pel_enable; 216 u8 issue_pel_wait; 217 u8 tmp_class; 218 u16 tmp_locale; 219 220 if (job->request_payload.payload_len != sizeof(pel_enable)) { 221 dprint_bsg_err(mrioc, "%s: invalid size argument\n", 222 __func__); 223 return rval; 224 } 225 226 sg_copy_to_buffer(job->request_payload.sg_list, 227 job->request_payload.sg_cnt, 228 &pel_enable, sizeof(pel_enable)); 229 230 if (pel_enable.pel_class > MPI3_PEL_CLASS_FAULT) { 231 dprint_bsg_err(mrioc, "%s: out of range class %d sent\n", 232 __func__, pel_enable.pel_class); 233 rval = 0; 234 goto out; 235 } 236 if (!mrioc->pel_enabled) 237 issue_pel_wait = 1; 238 else { 239 if ((mrioc->pel_class <= pel_enable.pel_class) && 240 !((mrioc->pel_locale & pel_enable.pel_locale) ^ 241 pel_enable.pel_locale)) { 242 issue_pel_wait = 0; 243 rval = 0; 244 } else { 245 pel_enable.pel_locale |= mrioc->pel_locale; 246 247 if (mrioc->pel_class < pel_enable.pel_class) 248 pel_enable.pel_class = mrioc->pel_class; 249 250 rval = mpi3mr_bsg_pel_abort(mrioc); 251 if (rval) { 252 dprint_bsg_err(mrioc, 253 "%s: pel_abort failed, status(%ld)\n", 254 __func__, rval); 255 goto out; 256 } 257 issue_pel_wait = 1; 258 } 259 } 260 if (issue_pel_wait) { 261 tmp_class = mrioc->pel_class; 262 tmp_locale = mrioc->pel_locale; 263 mrioc->pel_class = pel_enable.pel_class; 264 mrioc->pel_locale = pel_enable.pel_locale; 265 mrioc->pel_enabled = 1; 266 rval = mpi3mr_pel_get_seqnum_post(mrioc, NULL); 267 if (rval) { 268 mrioc->pel_class = tmp_class; 269 mrioc->pel_locale = tmp_locale; 270 mrioc->pel_enabled = 0; 271 dprint_bsg_err(mrioc, 272 "%s: pel get sequence number failed, status(%ld)\n", 273 __func__, rval); 274 } 275 } 276 277 out: 278 return rval; 279 } 280 /** 281 * mpi3mr_get_all_tgt_info - Get all target information 282 * @mrioc: Adapter instance reference 283 * @job: BSG job reference 284 * 285 * This function copies the driver managed target devices device 286 * handle, persistent ID, bus ID and taret ID to the user 287 * provided buffer for the specific controller. This function 288 * also provides the number of devices managed by the driver for 289 * the specific controller. 290 * 291 * Return: 0 on success and proper error codes on failure 292 */ 293 static long mpi3mr_get_all_tgt_info(struct mpi3mr_ioc *mrioc, 294 struct bsg_job *job) 295 { 296 long rval = -EINVAL; 297 u16 num_devices = 0, i = 0, size; 298 unsigned long flags; 299 struct mpi3mr_tgt_dev *tgtdev; 300 struct mpi3mr_device_map_info *devmap_info = NULL; 301 struct mpi3mr_all_tgt_info *alltgt_info = NULL; 302 uint32_t min_entrylen = 0, kern_entrylen = 0, usr_entrylen = 0; 303 304 if (job->request_payload.payload_len < sizeof(u32)) { 305 dprint_bsg_err(mrioc, "%s: invalid size argument\n", 306 __func__); 307 return rval; 308 } 309 310 spin_lock_irqsave(&mrioc->tgtdev_lock, flags); 311 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) 312 num_devices++; 313 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags); 314 315 if ((job->request_payload.payload_len == sizeof(u32)) || 316 list_empty(&mrioc->tgtdev_list)) { 317 sg_copy_from_buffer(job->request_payload.sg_list, 318 job->request_payload.sg_cnt, 319 &num_devices, sizeof(num_devices)); 320 return 0; 321 } 322 323 kern_entrylen = (num_devices - 1) * sizeof(*devmap_info); 324 size = sizeof(*alltgt_info) + kern_entrylen; 325 alltgt_info = kzalloc(size, GFP_KERNEL); 326 if (!alltgt_info) 327 return -ENOMEM; 328 329 devmap_info = alltgt_info->dmi; 330 memset((u8 *)devmap_info, 0xFF, (kern_entrylen + sizeof(*devmap_info))); 331 spin_lock_irqsave(&mrioc->tgtdev_lock, flags); 332 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) { 333 if (i < num_devices) { 334 devmap_info[i].handle = tgtdev->dev_handle; 335 devmap_info[i].perst_id = tgtdev->perst_id; 336 if (tgtdev->host_exposed && tgtdev->starget) { 337 devmap_info[i].target_id = tgtdev->starget->id; 338 devmap_info[i].bus_id = 339 tgtdev->starget->channel; 340 } 341 i++; 342 } 343 } 344 num_devices = i; 345 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags); 346 347 memcpy(&alltgt_info->num_devices, &num_devices, sizeof(num_devices)); 348 349 usr_entrylen = (job->request_payload.payload_len - sizeof(u32)) / sizeof(*devmap_info); 350 usr_entrylen *= sizeof(*devmap_info); 351 min_entrylen = min(usr_entrylen, kern_entrylen); 352 if (min_entrylen && (!memcpy(&alltgt_info->dmi, devmap_info, min_entrylen))) { 353 dprint_bsg_err(mrioc, "%s:%d: device map info copy failed\n", 354 __func__, __LINE__); 355 rval = -EFAULT; 356 goto out; 357 } 358 359 sg_copy_from_buffer(job->request_payload.sg_list, 360 job->request_payload.sg_cnt, 361 alltgt_info, job->request_payload.payload_len); 362 rval = 0; 363 out: 364 kfree(alltgt_info); 365 return rval; 366 } 367 /** 368 * mpi3mr_get_change_count - Get topology change count 369 * @mrioc: Adapter instance reference 370 * @job: BSG job reference 371 * 372 * This function copies the toplogy change count provided by the 373 * driver in events and cached in the driver to the user 374 * provided buffer for the specific controller. 375 * 376 * Return: 0 on success and proper error codes on failure 377 */ 378 static long mpi3mr_get_change_count(struct mpi3mr_ioc *mrioc, 379 struct bsg_job *job) 380 { 381 struct mpi3mr_change_count chgcnt; 382 383 memset(&chgcnt, 0, sizeof(chgcnt)); 384 chgcnt.change_count = mrioc->change_count; 385 if (job->request_payload.payload_len >= sizeof(chgcnt)) { 386 sg_copy_from_buffer(job->request_payload.sg_list, 387 job->request_payload.sg_cnt, 388 &chgcnt, sizeof(chgcnt)); 389 return 0; 390 } 391 return -EINVAL; 392 } 393 394 /** 395 * mpi3mr_bsg_adp_reset - Issue controller reset 396 * @mrioc: Adapter instance reference 397 * @job: BSG job reference 398 * 399 * This function identifies the user provided reset type and 400 * issues approporiate reset to the controller and wait for that 401 * to complete and reinitialize the controller and then returns 402 * 403 * Return: 0 on success and proper error codes on failure 404 */ 405 static long mpi3mr_bsg_adp_reset(struct mpi3mr_ioc *mrioc, 406 struct bsg_job *job) 407 { 408 long rval = -EINVAL; 409 u8 save_snapdump; 410 struct mpi3mr_bsg_adp_reset adpreset; 411 412 if (job->request_payload.payload_len != 413 sizeof(adpreset)) { 414 dprint_bsg_err(mrioc, "%s: invalid size argument\n", 415 __func__); 416 goto out; 417 } 418 419 sg_copy_to_buffer(job->request_payload.sg_list, 420 job->request_payload.sg_cnt, 421 &adpreset, sizeof(adpreset)); 422 423 switch (adpreset.reset_type) { 424 case MPI3MR_BSG_ADPRESET_SOFT: 425 save_snapdump = 0; 426 break; 427 case MPI3MR_BSG_ADPRESET_DIAG_FAULT: 428 save_snapdump = 1; 429 break; 430 default: 431 dprint_bsg_err(mrioc, "%s: unknown reset_type(%d)\n", 432 __func__, adpreset.reset_type); 433 goto out; 434 } 435 436 rval = mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_APP, 437 save_snapdump); 438 439 if (rval) 440 dprint_bsg_err(mrioc, 441 "%s: reset handler returned error(%ld) for reset type %d\n", 442 __func__, rval, adpreset.reset_type); 443 out: 444 return rval; 445 } 446 447 /** 448 * mpi3mr_bsg_populate_adpinfo - Get adapter info command handler 449 * @mrioc: Adapter instance reference 450 * @job: BSG job reference 451 * 452 * This function provides adapter information for the given 453 * controller 454 * 455 * Return: 0 on success and proper error codes on failure 456 */ 457 static long mpi3mr_bsg_populate_adpinfo(struct mpi3mr_ioc *mrioc, 458 struct bsg_job *job) 459 { 460 enum mpi3mr_iocstate ioc_state; 461 struct mpi3mr_bsg_in_adpinfo adpinfo; 462 463 memset(&adpinfo, 0, sizeof(adpinfo)); 464 adpinfo.adp_type = MPI3MR_BSG_ADPTYPE_AVGFAMILY; 465 adpinfo.pci_dev_id = mrioc->pdev->device; 466 adpinfo.pci_dev_hw_rev = mrioc->pdev->revision; 467 adpinfo.pci_subsys_dev_id = mrioc->pdev->subsystem_device; 468 adpinfo.pci_subsys_ven_id = mrioc->pdev->subsystem_vendor; 469 adpinfo.pci_bus = mrioc->pdev->bus->number; 470 adpinfo.pci_dev = PCI_SLOT(mrioc->pdev->devfn); 471 adpinfo.pci_func = PCI_FUNC(mrioc->pdev->devfn); 472 adpinfo.pci_seg_id = pci_domain_nr(mrioc->pdev->bus); 473 adpinfo.app_intfc_ver = MPI3MR_IOCTL_VERSION; 474 475 ioc_state = mpi3mr_get_iocstate(mrioc); 476 if (ioc_state == MRIOC_STATE_UNRECOVERABLE) 477 adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_UNRECOVERABLE; 478 else if ((mrioc->reset_in_progress) || (mrioc->stop_bsgs)) 479 adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_IN_RESET; 480 else if (ioc_state == MRIOC_STATE_FAULT) 481 adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_FAULT; 482 else 483 adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_OPERATIONAL; 484 485 memcpy((u8 *)&adpinfo.driver_info, (u8 *)&mrioc->driver_info, 486 sizeof(adpinfo.driver_info)); 487 488 if (job->request_payload.payload_len >= sizeof(adpinfo)) { 489 sg_copy_from_buffer(job->request_payload.sg_list, 490 job->request_payload.sg_cnt, 491 &adpinfo, sizeof(adpinfo)); 492 return 0; 493 } 494 return -EINVAL; 495 } 496 497 /** 498 * mpi3mr_bsg_process_drv_cmds - Driver Command handler 499 * @job: BSG job reference 500 * 501 * This function is the top level handler for driver commands, 502 * this does basic validation of the buffer and identifies the 503 * opcode and switches to correct sub handler. 504 * 505 * Return: 0 on success and proper error codes on failure 506 */ 507 static long mpi3mr_bsg_process_drv_cmds(struct bsg_job *job) 508 { 509 long rval = -EINVAL; 510 struct mpi3mr_ioc *mrioc = NULL; 511 struct mpi3mr_bsg_packet *bsg_req = NULL; 512 struct mpi3mr_bsg_drv_cmd *drvrcmd = NULL; 513 514 bsg_req = job->request; 515 drvrcmd = &bsg_req->cmd.drvrcmd; 516 517 mrioc = mpi3mr_bsg_verify_adapter(drvrcmd->mrioc_id); 518 if (!mrioc) 519 return -ENODEV; 520 521 if (drvrcmd->opcode == MPI3MR_DRVBSG_OPCODE_ADPINFO) { 522 rval = mpi3mr_bsg_populate_adpinfo(mrioc, job); 523 return rval; 524 } 525 526 if (mutex_lock_interruptible(&mrioc->bsg_cmds.mutex)) 527 return -ERESTARTSYS; 528 529 switch (drvrcmd->opcode) { 530 case MPI3MR_DRVBSG_OPCODE_ADPRESET: 531 rval = mpi3mr_bsg_adp_reset(mrioc, job); 532 break; 533 case MPI3MR_DRVBSG_OPCODE_ALLTGTDEVINFO: 534 rval = mpi3mr_get_all_tgt_info(mrioc, job); 535 break; 536 case MPI3MR_DRVBSG_OPCODE_GETCHGCNT: 537 rval = mpi3mr_get_change_count(mrioc, job); 538 break; 539 case MPI3MR_DRVBSG_OPCODE_LOGDATAENABLE: 540 rval = mpi3mr_enable_logdata(mrioc, job); 541 break; 542 case MPI3MR_DRVBSG_OPCODE_GETLOGDATA: 543 rval = mpi3mr_get_logdata(mrioc, job); 544 break; 545 case MPI3MR_DRVBSG_OPCODE_PELENABLE: 546 rval = mpi3mr_bsg_pel_enable(mrioc, job); 547 break; 548 case MPI3MR_DRVBSG_OPCODE_UNKNOWN: 549 default: 550 pr_err("%s: unsupported driver command opcode %d\n", 551 MPI3MR_DRIVER_NAME, drvrcmd->opcode); 552 break; 553 } 554 mutex_unlock(&mrioc->bsg_cmds.mutex); 555 return rval; 556 } 557 558 /** 559 * mpi3mr_bsg_build_sgl - SGL construction for MPI commands 560 * @mpi_req: MPI request 561 * @sgl_offset: offset to start sgl in the MPI request 562 * @drv_bufs: DMA address of the buffers to be placed in sgl 563 * @bufcnt: Number of DMA buffers 564 * @is_rmc: Does the buffer list has management command buffer 565 * @is_rmr: Does the buffer list has management response buffer 566 * @num_datasges: Number of data buffers in the list 567 * 568 * This function places the DMA address of the given buffers in 569 * proper format as SGEs in the given MPI request. 570 * 571 * Return: Nothing 572 */ 573 static void mpi3mr_bsg_build_sgl(u8 *mpi_req, uint32_t sgl_offset, 574 struct mpi3mr_buf_map *drv_bufs, u8 bufcnt, u8 is_rmc, 575 u8 is_rmr, u8 num_datasges) 576 { 577 u8 *sgl = (mpi_req + sgl_offset), count = 0; 578 struct mpi3_mgmt_passthrough_request *rmgmt_req = 579 (struct mpi3_mgmt_passthrough_request *)mpi_req; 580 struct mpi3mr_buf_map *drv_buf_iter = drv_bufs; 581 u8 sgl_flags, sgl_flags_last; 582 583 sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE | 584 MPI3_SGE_FLAGS_DLAS_SYSTEM | MPI3_SGE_FLAGS_END_OF_BUFFER; 585 sgl_flags_last = sgl_flags | MPI3_SGE_FLAGS_END_OF_LIST; 586 587 if (is_rmc) { 588 mpi3mr_add_sg_single(&rmgmt_req->command_sgl, 589 sgl_flags_last, drv_buf_iter->kern_buf_len, 590 drv_buf_iter->kern_buf_dma); 591 sgl = (u8 *)drv_buf_iter->kern_buf + drv_buf_iter->bsg_buf_len; 592 drv_buf_iter++; 593 count++; 594 if (is_rmr) { 595 mpi3mr_add_sg_single(&rmgmt_req->response_sgl, 596 sgl_flags_last, drv_buf_iter->kern_buf_len, 597 drv_buf_iter->kern_buf_dma); 598 drv_buf_iter++; 599 count++; 600 } else 601 mpi3mr_build_zero_len_sge( 602 &rmgmt_req->response_sgl); 603 } 604 if (!num_datasges) { 605 mpi3mr_build_zero_len_sge(sgl); 606 return; 607 } 608 for (; count < bufcnt; count++, drv_buf_iter++) { 609 if (drv_buf_iter->data_dir == DMA_NONE) 610 continue; 611 if (num_datasges == 1 || !is_rmc) 612 mpi3mr_add_sg_single(sgl, sgl_flags_last, 613 drv_buf_iter->kern_buf_len, drv_buf_iter->kern_buf_dma); 614 else 615 mpi3mr_add_sg_single(sgl, sgl_flags, 616 drv_buf_iter->kern_buf_len, drv_buf_iter->kern_buf_dma); 617 sgl += sizeof(struct mpi3_sge_common); 618 num_datasges--; 619 } 620 } 621 622 /** 623 * mpi3mr_bsg_process_mpt_cmds - MPI Pass through BSG handler 624 * @job: BSG job reference 625 * 626 * This function is the top level handler for MPI Pass through 627 * command, this does basic validation of the input data buffers, 628 * identifies the given buffer types and MPI command, allocates 629 * DMAable memory for user given buffers, construstcs SGL 630 * properly and passes the command to the firmware. 631 * 632 * Once the MPI command is completed the driver copies the data 633 * if any and reply, sense information to user provided buffers. 634 * If the command is timed out then issues controller reset 635 * prior to returning. 636 * 637 * Return: 0 on success and proper error codes on failure 638 */ 639 640 static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job, unsigned int *reply_payload_rcv_len) 641 { 642 long rval = -EINVAL; 643 644 struct mpi3mr_ioc *mrioc = NULL; 645 u8 *mpi_req = NULL, *sense_buff_k = NULL; 646 u8 mpi_msg_size = 0; 647 struct mpi3mr_bsg_packet *bsg_req = NULL; 648 struct mpi3mr_bsg_mptcmd *karg; 649 struct mpi3mr_buf_entry *buf_entries = NULL; 650 struct mpi3mr_buf_map *drv_bufs = NULL, *drv_buf_iter = NULL; 651 u8 count, bufcnt = 0, is_rmcb = 0, is_rmrb = 0, din_cnt = 0, dout_cnt = 0; 652 u8 invalid_be = 0, erb_offset = 0xFF, mpirep_offset = 0xFF, sg_entries = 0; 653 u8 block_io = 0, resp_code = 0; 654 struct mpi3_request_header *mpi_header = NULL; 655 struct mpi3_status_reply_descriptor *status_desc; 656 struct mpi3_scsi_task_mgmt_request *tm_req; 657 u32 erbsz = MPI3MR_SENSE_BUF_SZ, tmplen; 658 u16 dev_handle; 659 struct mpi3mr_tgt_dev *tgtdev; 660 struct mpi3mr_stgt_priv_data *stgt_priv = NULL; 661 struct mpi3mr_bsg_in_reply_buf *bsg_reply_buf = NULL; 662 u32 din_size = 0, dout_size = 0; 663 u8 *din_buf = NULL, *dout_buf = NULL; 664 u8 *sgl_iter = NULL, *sgl_din_iter = NULL, *sgl_dout_iter = NULL; 665 666 bsg_req = job->request; 667 karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd; 668 669 mrioc = mpi3mr_bsg_verify_adapter(karg->mrioc_id); 670 if (!mrioc) 671 return -ENODEV; 672 673 if (karg->timeout < MPI3MR_APP_DEFAULT_TIMEOUT) 674 karg->timeout = MPI3MR_APP_DEFAULT_TIMEOUT; 675 676 mpi_req = kzalloc(MPI3MR_ADMIN_REQ_FRAME_SZ, GFP_KERNEL); 677 if (!mpi_req) 678 return -ENOMEM; 679 mpi_header = (struct mpi3_request_header *)mpi_req; 680 681 bufcnt = karg->buf_entry_list.num_of_entries; 682 drv_bufs = kzalloc((sizeof(*drv_bufs) * bufcnt), GFP_KERNEL); 683 if (!drv_bufs) { 684 rval = -ENOMEM; 685 goto out; 686 } 687 688 dout_buf = kzalloc(job->request_payload.payload_len, 689 GFP_KERNEL); 690 if (!dout_buf) { 691 rval = -ENOMEM; 692 goto out; 693 } 694 695 din_buf = kzalloc(job->reply_payload.payload_len, 696 GFP_KERNEL); 697 if (!din_buf) { 698 rval = -ENOMEM; 699 goto out; 700 } 701 702 sg_copy_to_buffer(job->request_payload.sg_list, 703 job->request_payload.sg_cnt, 704 dout_buf, job->request_payload.payload_len); 705 706 buf_entries = karg->buf_entry_list.buf_entry; 707 sgl_din_iter = din_buf; 708 sgl_dout_iter = dout_buf; 709 drv_buf_iter = drv_bufs; 710 711 for (count = 0; count < bufcnt; count++, buf_entries++, drv_buf_iter++) { 712 713 if (sgl_dout_iter > (dout_buf + job->request_payload.payload_len)) { 714 dprint_bsg_err(mrioc, "%s: data_out buffer length mismatch\n", 715 __func__); 716 rval = -EINVAL; 717 goto out; 718 } 719 if (sgl_din_iter > (din_buf + job->reply_payload.payload_len)) { 720 dprint_bsg_err(mrioc, "%s: data_in buffer length mismatch\n", 721 __func__); 722 rval = -EINVAL; 723 goto out; 724 } 725 726 switch (buf_entries->buf_type) { 727 case MPI3MR_BSG_BUFTYPE_RAIDMGMT_CMD: 728 sgl_iter = sgl_dout_iter; 729 sgl_dout_iter += buf_entries->buf_len; 730 drv_buf_iter->data_dir = DMA_TO_DEVICE; 731 is_rmcb = 1; 732 if (count != 0) 733 invalid_be = 1; 734 break; 735 case MPI3MR_BSG_BUFTYPE_RAIDMGMT_RESP: 736 sgl_iter = sgl_din_iter; 737 sgl_din_iter += buf_entries->buf_len; 738 drv_buf_iter->data_dir = DMA_FROM_DEVICE; 739 is_rmrb = 1; 740 if (count != 1 || !is_rmcb) 741 invalid_be = 1; 742 break; 743 case MPI3MR_BSG_BUFTYPE_DATA_IN: 744 sgl_iter = sgl_din_iter; 745 sgl_din_iter += buf_entries->buf_len; 746 drv_buf_iter->data_dir = DMA_FROM_DEVICE; 747 din_cnt++; 748 din_size += drv_buf_iter->bsg_buf_len; 749 if ((din_cnt > 1) && !is_rmcb) 750 invalid_be = 1; 751 break; 752 case MPI3MR_BSG_BUFTYPE_DATA_OUT: 753 sgl_iter = sgl_dout_iter; 754 sgl_dout_iter += buf_entries->buf_len; 755 drv_buf_iter->data_dir = DMA_TO_DEVICE; 756 dout_cnt++; 757 dout_size += drv_buf_iter->bsg_buf_len; 758 if ((dout_cnt > 1) && !is_rmcb) 759 invalid_be = 1; 760 break; 761 case MPI3MR_BSG_BUFTYPE_MPI_REPLY: 762 sgl_iter = sgl_din_iter; 763 sgl_din_iter += buf_entries->buf_len; 764 drv_buf_iter->data_dir = DMA_NONE; 765 mpirep_offset = count; 766 break; 767 case MPI3MR_BSG_BUFTYPE_ERR_RESPONSE: 768 sgl_iter = sgl_din_iter; 769 sgl_din_iter += buf_entries->buf_len; 770 drv_buf_iter->data_dir = DMA_NONE; 771 erb_offset = count; 772 break; 773 case MPI3MR_BSG_BUFTYPE_MPI_REQUEST: 774 sgl_iter = sgl_dout_iter; 775 sgl_dout_iter += buf_entries->buf_len; 776 drv_buf_iter->data_dir = DMA_NONE; 777 mpi_msg_size = buf_entries->buf_len; 778 if ((!mpi_msg_size || (mpi_msg_size % 4)) || 779 (mpi_msg_size > MPI3MR_ADMIN_REQ_FRAME_SZ)) { 780 dprint_bsg_err(mrioc, "%s: invalid MPI message size\n", 781 __func__); 782 rval = -EINVAL; 783 goto out; 784 } 785 memcpy(mpi_req, sgl_iter, buf_entries->buf_len); 786 break; 787 default: 788 invalid_be = 1; 789 break; 790 } 791 if (invalid_be) { 792 dprint_bsg_err(mrioc, "%s: invalid buffer entries passed\n", 793 __func__); 794 rval = -EINVAL; 795 goto out; 796 } 797 798 drv_buf_iter->bsg_buf = sgl_iter; 799 drv_buf_iter->bsg_buf_len = buf_entries->buf_len; 800 801 } 802 if (!is_rmcb && (dout_cnt || din_cnt)) { 803 sg_entries = dout_cnt + din_cnt; 804 if (((mpi_msg_size) + (sg_entries * 805 sizeof(struct mpi3_sge_common))) > MPI3MR_ADMIN_REQ_FRAME_SZ) { 806 dprint_bsg_err(mrioc, 807 "%s:%d: invalid message size passed\n", 808 __func__, __LINE__); 809 rval = -EINVAL; 810 goto out; 811 } 812 } 813 if (din_size > MPI3MR_MAX_APP_XFER_SIZE) { 814 dprint_bsg_err(mrioc, 815 "%s:%d: invalid data transfer size passed for function 0x%x din_size=%d\n", 816 __func__, __LINE__, mpi_header->function, din_size); 817 rval = -EINVAL; 818 goto out; 819 } 820 if (dout_size > MPI3MR_MAX_APP_XFER_SIZE) { 821 dprint_bsg_err(mrioc, 822 "%s:%d: invalid data transfer size passed for function 0x%x dout_size = %d\n", 823 __func__, __LINE__, mpi_header->function, dout_size); 824 rval = -EINVAL; 825 goto out; 826 } 827 828 drv_buf_iter = drv_bufs; 829 for (count = 0; count < bufcnt; count++, drv_buf_iter++) { 830 if (drv_buf_iter->data_dir == DMA_NONE) 831 continue; 832 833 drv_buf_iter->kern_buf_len = drv_buf_iter->bsg_buf_len; 834 if (is_rmcb && !count) 835 drv_buf_iter->kern_buf_len += ((dout_cnt + din_cnt) * 836 sizeof(struct mpi3_sge_common)); 837 838 if (!drv_buf_iter->kern_buf_len) 839 continue; 840 841 drv_buf_iter->kern_buf = dma_alloc_coherent(&mrioc->pdev->dev, 842 drv_buf_iter->kern_buf_len, &drv_buf_iter->kern_buf_dma, 843 GFP_KERNEL); 844 if (!drv_buf_iter->kern_buf) { 845 rval = -ENOMEM; 846 goto out; 847 } 848 if (drv_buf_iter->data_dir == DMA_TO_DEVICE) { 849 tmplen = min(drv_buf_iter->kern_buf_len, 850 drv_buf_iter->bsg_buf_len); 851 memcpy(drv_buf_iter->kern_buf, drv_buf_iter->bsg_buf, tmplen); 852 } 853 } 854 855 if (erb_offset != 0xFF) { 856 sense_buff_k = kzalloc(erbsz, GFP_KERNEL); 857 if (!sense_buff_k) { 858 rval = -ENOMEM; 859 goto out; 860 } 861 } 862 863 if (mutex_lock_interruptible(&mrioc->bsg_cmds.mutex)) { 864 rval = -ERESTARTSYS; 865 goto out; 866 } 867 if (mrioc->bsg_cmds.state & MPI3MR_CMD_PENDING) { 868 rval = -EAGAIN; 869 dprint_bsg_err(mrioc, "%s: command is in use\n", __func__); 870 mutex_unlock(&mrioc->bsg_cmds.mutex); 871 goto out; 872 } 873 if (mrioc->unrecoverable) { 874 dprint_bsg_err(mrioc, "%s: unrecoverable controller\n", 875 __func__); 876 rval = -EFAULT; 877 mutex_unlock(&mrioc->bsg_cmds.mutex); 878 goto out; 879 } 880 if (mrioc->reset_in_progress) { 881 dprint_bsg_err(mrioc, "%s: reset in progress\n", __func__); 882 rval = -EAGAIN; 883 mutex_unlock(&mrioc->bsg_cmds.mutex); 884 goto out; 885 } 886 if (mrioc->stop_bsgs) { 887 dprint_bsg_err(mrioc, "%s: bsgs are blocked\n", __func__); 888 rval = -EAGAIN; 889 mutex_unlock(&mrioc->bsg_cmds.mutex); 890 goto out; 891 } 892 893 if (mpi_header->function != MPI3_BSG_FUNCTION_NVME_ENCAPSULATED) { 894 mpi3mr_bsg_build_sgl(mpi_req, (mpi_msg_size), 895 drv_bufs, bufcnt, is_rmcb, is_rmrb, 896 (dout_cnt + din_cnt)); 897 } 898 899 if (mpi_header->function == MPI3_BSG_FUNCTION_SCSI_TASK_MGMT) { 900 tm_req = (struct mpi3_scsi_task_mgmt_request *)mpi_req; 901 if (tm_req->task_type != 902 MPI3_SCSITASKMGMT_TASKTYPE_ABORT_TASK) { 903 dev_handle = tm_req->dev_handle; 904 block_io = 1; 905 } 906 } 907 if (block_io) { 908 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle); 909 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata) { 910 stgt_priv = (struct mpi3mr_stgt_priv_data *) 911 tgtdev->starget->hostdata; 912 atomic_inc(&stgt_priv->block_io); 913 mpi3mr_tgtdev_put(tgtdev); 914 } 915 } 916 917 mrioc->bsg_cmds.state = MPI3MR_CMD_PENDING; 918 mrioc->bsg_cmds.is_waiting = 1; 919 mrioc->bsg_cmds.callback = NULL; 920 mrioc->bsg_cmds.is_sense = 0; 921 mrioc->bsg_cmds.sensebuf = sense_buff_k; 922 memset(mrioc->bsg_cmds.reply, 0, mrioc->reply_sz); 923 mpi_header->host_tag = cpu_to_le16(MPI3MR_HOSTTAG_BSG_CMDS); 924 if (mrioc->logging_level & MPI3_DEBUG_BSG_INFO) { 925 dprint_bsg_info(mrioc, 926 "%s: posting bsg request to the controller\n", __func__); 927 dprint_dump(mpi_req, MPI3MR_ADMIN_REQ_FRAME_SZ, 928 "bsg_mpi3_req"); 929 if (mpi_header->function == MPI3_BSG_FUNCTION_MGMT_PASSTHROUGH) { 930 drv_buf_iter = &drv_bufs[0]; 931 dprint_dump(drv_buf_iter->kern_buf, 932 drv_buf_iter->kern_buf_len, "mpi3_mgmt_req"); 933 } 934 } 935 936 init_completion(&mrioc->bsg_cmds.done); 937 rval = mpi3mr_admin_request_post(mrioc, mpi_req, 938 MPI3MR_ADMIN_REQ_FRAME_SZ, 0); 939 940 941 if (rval) { 942 mrioc->bsg_cmds.is_waiting = 0; 943 dprint_bsg_err(mrioc, 944 "%s: posting bsg request is failed\n", __func__); 945 rval = -EAGAIN; 946 goto out_unlock; 947 } 948 wait_for_completion_timeout(&mrioc->bsg_cmds.done, 949 (karg->timeout * HZ)); 950 if (block_io && stgt_priv) 951 atomic_dec(&stgt_priv->block_io); 952 if (!(mrioc->bsg_cmds.state & MPI3MR_CMD_COMPLETE)) { 953 mrioc->bsg_cmds.is_waiting = 0; 954 rval = -EAGAIN; 955 if (mrioc->bsg_cmds.state & MPI3MR_CMD_RESET) 956 goto out_unlock; 957 dprint_bsg_err(mrioc, 958 "%s: bsg request timedout after %d seconds\n", __func__, 959 karg->timeout); 960 if (mrioc->logging_level & MPI3_DEBUG_BSG_ERROR) { 961 dprint_dump(mpi_req, MPI3MR_ADMIN_REQ_FRAME_SZ, 962 "bsg_mpi3_req"); 963 if (mpi_header->function == 964 MPI3_BSG_FUNCTION_MGMT_PASSTHROUGH) { 965 drv_buf_iter = &drv_bufs[0]; 966 dprint_dump(drv_buf_iter->kern_buf, 967 drv_buf_iter->kern_buf_len, "mpi3_mgmt_req"); 968 } 969 } 970 971 if (mpi_header->function == MPI3_BSG_FUNCTION_SCSI_IO) 972 mpi3mr_issue_tm(mrioc, 973 MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 974 mpi_header->function_dependent, 0, 975 MPI3MR_HOSTTAG_BLK_TMS, MPI3MR_RESETTM_TIMEOUT, 976 &mrioc->host_tm_cmds, &resp_code, NULL); 977 if (!(mrioc->bsg_cmds.state & MPI3MR_CMD_COMPLETE) && 978 !(mrioc->bsg_cmds.state & MPI3MR_CMD_RESET)) 979 mpi3mr_soft_reset_handler(mrioc, 980 MPI3MR_RESET_FROM_APP_TIMEOUT, 1); 981 goto out_unlock; 982 } 983 dprint_bsg_info(mrioc, "%s: bsg request is completed\n", __func__); 984 985 if ((mrioc->bsg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK) 986 != MPI3_IOCSTATUS_SUCCESS) { 987 dprint_bsg_info(mrioc, 988 "%s: command failed, ioc_status(0x%04x) log_info(0x%08x)\n", 989 __func__, 990 (mrioc->bsg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK), 991 mrioc->bsg_cmds.ioc_loginfo); 992 } 993 994 if ((mpirep_offset != 0xFF) && 995 drv_bufs[mpirep_offset].bsg_buf_len) { 996 drv_buf_iter = &drv_bufs[mpirep_offset]; 997 drv_buf_iter->kern_buf_len = (sizeof(*bsg_reply_buf) - 1 + 998 mrioc->reply_sz); 999 bsg_reply_buf = kzalloc(drv_buf_iter->kern_buf_len, GFP_KERNEL); 1000 1001 if (!bsg_reply_buf) { 1002 rval = -ENOMEM; 1003 goto out_unlock; 1004 } 1005 if (mrioc->bsg_cmds.state & MPI3MR_CMD_REPLY_VALID) { 1006 bsg_reply_buf->mpi_reply_type = 1007 MPI3MR_BSG_MPI_REPLY_BUFTYPE_ADDRESS; 1008 memcpy(bsg_reply_buf->reply_buf, 1009 mrioc->bsg_cmds.reply, mrioc->reply_sz); 1010 } else { 1011 bsg_reply_buf->mpi_reply_type = 1012 MPI3MR_BSG_MPI_REPLY_BUFTYPE_STATUS; 1013 status_desc = (struct mpi3_status_reply_descriptor *) 1014 bsg_reply_buf->reply_buf; 1015 status_desc->ioc_status = mrioc->bsg_cmds.ioc_status; 1016 status_desc->ioc_log_info = mrioc->bsg_cmds.ioc_loginfo; 1017 } 1018 tmplen = min(drv_buf_iter->kern_buf_len, 1019 drv_buf_iter->bsg_buf_len); 1020 memcpy(drv_buf_iter->bsg_buf, bsg_reply_buf, tmplen); 1021 } 1022 1023 if (erb_offset != 0xFF && mrioc->bsg_cmds.sensebuf && 1024 mrioc->bsg_cmds.is_sense) { 1025 drv_buf_iter = &drv_bufs[erb_offset]; 1026 tmplen = min(erbsz, drv_buf_iter->bsg_buf_len); 1027 memcpy(drv_buf_iter->bsg_buf, sense_buff_k, tmplen); 1028 } 1029 1030 drv_buf_iter = drv_bufs; 1031 for (count = 0; count < bufcnt; count++, drv_buf_iter++) { 1032 if (drv_buf_iter->data_dir == DMA_NONE) 1033 continue; 1034 if (drv_buf_iter->data_dir == DMA_FROM_DEVICE) { 1035 tmplen = min(drv_buf_iter->kern_buf_len, 1036 drv_buf_iter->bsg_buf_len); 1037 memcpy(drv_buf_iter->bsg_buf, 1038 drv_buf_iter->kern_buf, tmplen); 1039 } 1040 } 1041 1042 out_unlock: 1043 if (din_buf) { 1044 *reply_payload_rcv_len = 1045 sg_copy_from_buffer(job->reply_payload.sg_list, 1046 job->reply_payload.sg_cnt, 1047 din_buf, job->reply_payload.payload_len); 1048 } 1049 mrioc->bsg_cmds.is_sense = 0; 1050 mrioc->bsg_cmds.sensebuf = NULL; 1051 mrioc->bsg_cmds.state = MPI3MR_CMD_NOTUSED; 1052 mutex_unlock(&mrioc->bsg_cmds.mutex); 1053 out: 1054 kfree(sense_buff_k); 1055 kfree(dout_buf); 1056 kfree(din_buf); 1057 kfree(mpi_req); 1058 if (drv_bufs) { 1059 drv_buf_iter = drv_bufs; 1060 for (count = 0; count < bufcnt; count++, drv_buf_iter++) { 1061 if (drv_buf_iter->kern_buf && drv_buf_iter->kern_buf_dma) 1062 dma_free_coherent(&mrioc->pdev->dev, 1063 drv_buf_iter->kern_buf_len, 1064 drv_buf_iter->kern_buf, 1065 drv_buf_iter->kern_buf_dma); 1066 } 1067 kfree(drv_bufs); 1068 } 1069 kfree(bsg_reply_buf); 1070 return rval; 1071 } 1072 1073 /** 1074 * mpi3mr_app_save_logdata - Save Log Data events 1075 * @mrioc: Adapter instance reference 1076 * @event_data: event data associated with log data event 1077 * @event_data_size: event data size to copy 1078 * 1079 * If log data event caching is enabled by the applicatiobns, 1080 * then this function saves the log data in the circular queue 1081 * and Sends async signal SIGIO to indicate there is an async 1082 * event from the firmware to the event monitoring applications. 1083 * 1084 * Return:Nothing 1085 */ 1086 void mpi3mr_app_save_logdata(struct mpi3mr_ioc *mrioc, char *event_data, 1087 u16 event_data_size) 1088 { 1089 u32 index = mrioc->logdata_buf_idx, sz; 1090 struct mpi3mr_logdata_entry *entry; 1091 1092 if (!(mrioc->logdata_buf)) 1093 return; 1094 1095 entry = (struct mpi3mr_logdata_entry *) 1096 (mrioc->logdata_buf + (index * mrioc->logdata_entry_sz)); 1097 entry->valid_entry = 1; 1098 sz = min(mrioc->logdata_entry_sz, event_data_size); 1099 memcpy(entry->data, event_data, sz); 1100 mrioc->logdata_buf_idx = 1101 ((++index) % MPI3MR_BSG_LOGDATA_MAX_ENTRIES); 1102 atomic64_inc(&event_counter); 1103 } 1104 1105 /** 1106 * mpi3mr_bsg_request - bsg request entry point 1107 * @job: BSG job reference 1108 * 1109 * This is driver's entry point for bsg requests 1110 * 1111 * Return: 0 on success and proper error codes on failure 1112 */ 1113 static int mpi3mr_bsg_request(struct bsg_job *job) 1114 { 1115 long rval = -EINVAL; 1116 unsigned int reply_payload_rcv_len = 0; 1117 1118 struct mpi3mr_bsg_packet *bsg_req = job->request; 1119 1120 switch (bsg_req->cmd_type) { 1121 case MPI3MR_DRV_CMD: 1122 rval = mpi3mr_bsg_process_drv_cmds(job); 1123 break; 1124 case MPI3MR_MPT_CMD: 1125 rval = mpi3mr_bsg_process_mpt_cmds(job, &reply_payload_rcv_len); 1126 break; 1127 default: 1128 pr_err("%s: unsupported BSG command(0x%08x)\n", 1129 MPI3MR_DRIVER_NAME, bsg_req->cmd_type); 1130 break; 1131 } 1132 1133 bsg_job_done(job, rval, reply_payload_rcv_len); 1134 1135 return 0; 1136 } 1137 1138 /** 1139 * mpi3mr_bsg_exit - de-registration from bsg layer 1140 * 1141 * This will be called during driver unload and all 1142 * bsg resources allocated during load will be freed. 1143 * 1144 * Return:Nothing 1145 */ 1146 void mpi3mr_bsg_exit(struct mpi3mr_ioc *mrioc) 1147 { 1148 if (!mrioc->bsg_queue) 1149 return; 1150 1151 bsg_remove_queue(mrioc->bsg_queue); 1152 mrioc->bsg_queue = NULL; 1153 1154 device_del(mrioc->bsg_dev); 1155 put_device(mrioc->bsg_dev); 1156 kfree(mrioc->bsg_dev); 1157 } 1158 1159 /** 1160 * mpi3mr_bsg_node_release -release bsg device node 1161 * @dev: bsg device node 1162 * 1163 * decrements bsg dev reference count 1164 * 1165 * Return:Nothing 1166 */ 1167 static void mpi3mr_bsg_node_release(struct device *dev) 1168 { 1169 put_device(dev); 1170 } 1171 1172 /** 1173 * mpi3mr_bsg_init - registration with bsg layer 1174 * 1175 * This will be called during driver load and it will 1176 * register driver with bsg layer 1177 * 1178 * Return:Nothing 1179 */ 1180 void mpi3mr_bsg_init(struct mpi3mr_ioc *mrioc) 1181 { 1182 mrioc->bsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1183 if (!mrioc->bsg_dev) { 1184 ioc_err(mrioc, "bsg device mem allocation failed\n"); 1185 return; 1186 } 1187 1188 device_initialize(mrioc->bsg_dev); 1189 dev_set_name(mrioc->bsg_dev, "mpi3mrctl%u", mrioc->id); 1190 1191 if (device_add(mrioc->bsg_dev)) { 1192 ioc_err(mrioc, "%s: bsg device add failed\n", 1193 dev_name(mrioc->bsg_dev)); 1194 goto err_device_add; 1195 } 1196 1197 mrioc->bsg_dev->release = mpi3mr_bsg_node_release; 1198 1199 mrioc->bsg_queue = bsg_setup_queue(mrioc->bsg_dev, dev_name(mrioc->bsg_dev), 1200 mpi3mr_bsg_request, NULL, 0); 1201 if (!mrioc->bsg_queue) { 1202 ioc_err(mrioc, "%s: bsg registration failed\n", 1203 dev_name(mrioc->bsg_dev)); 1204 goto err_setup_queue; 1205 } 1206 1207 blk_queue_max_segments(mrioc->bsg_queue, MPI3MR_MAX_APP_XFER_SEGMENTS); 1208 blk_queue_max_hw_sectors(mrioc->bsg_queue, MPI3MR_MAX_APP_XFER_SECTORS); 1209 1210 return; 1211 1212 err_setup_queue: 1213 device_del(mrioc->bsg_dev); 1214 put_device(mrioc->bsg_dev); 1215 err_device_add: 1216 kfree(mrioc->bsg_dev); 1217 } 1218 1219 /** 1220 * adapter_state_show - SysFS callback for adapter state show 1221 * @dev: class device 1222 * @attr: Device attributes 1223 * @buf: Buffer to copy 1224 * 1225 * Return: snprintf() return after copying adapter state 1226 */ 1227 static ssize_t 1228 adp_state_show(struct device *dev, struct device_attribute *attr, 1229 char *buf) 1230 { 1231 struct Scsi_Host *shost = class_to_shost(dev); 1232 struct mpi3mr_ioc *mrioc = shost_priv(shost); 1233 enum mpi3mr_iocstate ioc_state; 1234 uint8_t adp_state; 1235 1236 ioc_state = mpi3mr_get_iocstate(mrioc); 1237 if (ioc_state == MRIOC_STATE_UNRECOVERABLE) 1238 adp_state = MPI3MR_BSG_ADPSTATE_UNRECOVERABLE; 1239 else if ((mrioc->reset_in_progress) || (mrioc->stop_bsgs)) 1240 adp_state = MPI3MR_BSG_ADPSTATE_IN_RESET; 1241 else if (ioc_state == MRIOC_STATE_FAULT) 1242 adp_state = MPI3MR_BSG_ADPSTATE_FAULT; 1243 else 1244 adp_state = MPI3MR_BSG_ADPSTATE_OPERATIONAL; 1245 1246 return snprintf(buf, PAGE_SIZE, "%u\n", adp_state); 1247 } 1248 1249 static DEVICE_ATTR_RO(adp_state); 1250 1251 static struct attribute *mpi3mr_host_attrs[] = { 1252 &dev_attr_adp_state.attr, 1253 NULL, 1254 }; 1255 1256 static const struct attribute_group mpi3mr_host_attr_group = { 1257 .attrs = mpi3mr_host_attrs 1258 }; 1259 1260 const struct attribute_group *mpi3mr_host_groups[] = { 1261 &mpi3mr_host_attr_group, 1262 NULL, 1263 }; 1264