1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2026 LeapIO Tech Inc. 4 * 5 * LeapRAID storage and RAID controller driver. 6 */ 7 #include <linux/module.h> 8 9 #include "leapraid_func.h" 10 #include "leapraid.h" 11 12 LIST_HEAD(leapraid_adapter_list); 13 DEFINE_SPINLOCK(leapraid_adapter_lock); 14 15 MODULE_AUTHOR(LEAPRAID_AUTHOR); 16 MODULE_DESCRIPTION(LEAPRAID_DESCRIPTION); 17 MODULE_LICENSE("GPL"); 18 MODULE_VERSION(LEAPRAID_DRIVER_VERSION); 19 20 static atomic_t leapraid_ids = ATOMIC_INIT(0); 21 22 static int open_pcie_trace = 1; 23 module_param(open_pcie_trace, int, 0644); 24 MODULE_PARM_DESC(open_pcie_trace, "Default=1(open)/0(close)"); 25 26 static int enable_mp = 1; 27 module_param(enable_mp, int, 0444); 28 MODULE_PARM_DESC(enable_mp, 29 "Enable multipath on target device. default=1(enable)"); 30 31 static inline void leapraid_get_sense_data(char *sense, 32 struct sense_info *data) 33 { 34 bool desc_format = (sense[0] & SCSI_SENSE_RESPONSE_CODE_MASK) >= 35 DESC_FORMAT_THRESHOLD; 36 37 if (desc_format) { 38 data->sense_key = sense[1] & SENSE_KEY_MASK; 39 data->asc = sense[2]; 40 data->ascq = sense[3]; 41 } else { 42 data->sense_key = sense[2] & SENSE_KEY_MASK; 43 data->asc = sense[12]; 44 data->ascq = sense[13]; 45 } 46 } 47 48 static struct leapraid_adapter *pdev_to_adapter(struct pci_dev *pdev) 49 { 50 struct Scsi_Host *shost = pci_get_drvdata(pdev); 51 52 if (!shost) 53 return NULL; 54 55 return shost_priv(shost); 56 } 57 58 void leapraid_set_tm_flg(struct leapraid_adapter *adapter, u16 hdl) 59 { 60 struct leapraid_sdev_priv *sdev_priv; 61 struct scsi_device *sdev; 62 bool skip = false; 63 64 /* Iterate over all devices. */ 65 shost_for_each_device(sdev, adapter->shost) { 66 if (skip) 67 continue; 68 69 sdev_priv = sdev->hostdata; 70 if (!sdev_priv) 71 continue; 72 73 if (sdev_priv->starget_priv->hdl == hdl) { 74 sdev_priv->starget_priv->tm_busy = 1; 75 skip = true; 76 } 77 } 78 } 79 80 void leapraid_clear_tm_flg(struct leapraid_adapter *adapter, u16 hdl) 81 { 82 struct leapraid_sdev_priv *sdev_priv; 83 struct scsi_device *sdev; 84 bool skip = false; 85 86 /* Iterate over all devices. */ 87 shost_for_each_device(sdev, adapter->shost) { 88 if (skip) 89 continue; 90 91 sdev_priv = sdev->hostdata; 92 if (!sdev_priv) 93 continue; 94 95 if (sdev_priv->starget_priv->hdl == hdl) { 96 sdev_priv->starget_priv->tm_busy = 0; 97 skip = true; 98 } 99 } 100 } 101 102 static int leapraid_tm_cmd_map_status(struct leapraid_adapter *adapter, 103 uint channel, 104 uint id, 105 uint lun, 106 u8 type, 107 u16 taskid_task) 108 { 109 int rc = FAILED; 110 111 if (taskid_task <= adapter->shost->can_queue) { 112 switch (type) { 113 case LEAPRAID_TM_TASKTYPE_ABRT_TASK_SET: 114 case LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET: 115 if (!leapraid_scmd_find_by_lun(adapter, id, lun, 116 channel)) 117 rc = SUCCESS; 118 break; 119 case LEAPRAID_TM_TASKTYPE_TARGET_RESET: 120 if (!leapraid_scmd_find_by_tgt(adapter, id, channel)) 121 rc = SUCCESS; 122 break; 123 default: 124 rc = SUCCESS; 125 } 126 } 127 128 if (taskid_task == adapter->driver_cmds.ctl_cmd.hp_taskid && 129 (adapter->driver_cmds.ctl_cmd.status & 130 LEAPRAID_CMD_DONE || 131 adapter->driver_cmds.ctl_cmd.status & 132 LEAPRAID_CMD_NOT_USED)) 133 rc = SUCCESS; 134 135 return rc; 136 } 137 138 static int leapraid_tm_post_processing(struct leapraid_adapter *adapter, 139 u16 hdl, uint channel, uint id, 140 uint lun, u8 type, u16 taskid_task) 141 { 142 int rc; 143 144 rc = leapraid_tm_cmd_map_status(adapter, channel, id, lun, 145 type, taskid_task); 146 if (rc == SUCCESS) 147 return rc; 148 149 leapraid_mask_int(adapter); 150 leapraid_sync_irqs(adapter, true); 151 leapraid_unmask_int(adapter); 152 153 return leapraid_tm_cmd_map_status(adapter, channel, id, lun, type, 154 taskid_task); 155 } 156 157 static void leapraid_build_tm_req(struct leapraid_scsi_tm_req *scsi_tm_req, 158 u16 hdl, uint lun, u8 type, u8 tr_method, 159 u16 target_taskid) 160 { 161 memset(scsi_tm_req, 0, sizeof(*scsi_tm_req)); 162 scsi_tm_req->func = LEAPRAID_FUNC_SCSI_TMF; 163 scsi_tm_req->dev_hdl = cpu_to_le16(hdl); 164 scsi_tm_req->task_type = type; 165 scsi_tm_req->msg_flg = tr_method; 166 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK || 167 type == LEAPRAID_TM_TASKTYPE_QUERY_TASK) 168 scsi_tm_req->task_mid = cpu_to_le16(target_taskid); 169 int_to_scsilun(lun, (struct scsi_lun *)scsi_tm_req->lun); 170 } 171 172 int leapraid_issue_tm(struct leapraid_adapter *adapter, u16 hdl, uint channel, 173 uint id, uint lun, u8 type, 174 u16 target_taskid, u8 tr_method) 175 { 176 struct leapraid_scsi_tm_req *scsi_tm_req; 177 struct leapraid_scsiio_req *scsiio_req; 178 struct leapraid_io_req_tracker *io_req_tracker = NULL; 179 u16 msix_task; 180 u16 taskid; 181 bool issue_reset = false; 182 u32 db; 183 int rc; 184 185 lockdep_assert_held(&adapter->driver_cmds.tm_cmd.mutex); 186 187 if (adapter->access_ctrl.shost_recovering || 188 adapter->access_ctrl.host_removing || 189 adapter->access_ctrl.pcie_recovering) { 190 dev_info(&adapter->pdev->dev, 191 "%s %s: Host is recovering, skip tm command!\n", 192 __func__, adapter->adapter_attr.name); 193 return FAILED; 194 } 195 196 db = leapraid_readl(&adapter->iomem_base->db); 197 if (db & LEAPRAID_DB_USED) { 198 dev_info(&adapter->pdev->dev, 199 "%s Unexpected db status, issuing hard reset!\n", 200 adapter->adapter_attr.name); 201 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n", 202 __func__, __LINE__); 203 rc = leapraid_hard_reset_handler(adapter, FULL_RESET); 204 return !rc ? SUCCESS : FAILED; 205 } 206 207 if ((db & LEAPRAID_DB_MASK) == LEAPRAID_DB_FAULT) { 208 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n", 209 __func__, __LINE__); 210 rc = leapraid_hard_reset_handler(adapter, FULL_RESET); 211 return !rc ? SUCCESS : FAILED; 212 } 213 214 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) 215 io_req_tracker = 216 leapraid_get_io_tracker_from_taskid(adapter, 217 target_taskid); 218 219 adapter->driver_cmds.tm_cmd.status = LEAPRAID_CMD_PENDING; 220 scsi_tm_req = 221 leapraid_get_task_desc(adapter, 222 adapter->driver_cmds.tm_cmd.hp_taskid); 223 leapraid_build_tm_req(scsi_tm_req, hdl, lun, type, tr_method, 224 target_taskid); 225 memset(&adapter->driver_cmds.tm_cmd.reply, 0, 226 sizeof(struct leapraid_scsi_tm_rep)); 227 leapraid_set_tm_flg(adapter, hdl); 228 init_completion(&adapter->driver_cmds.tm_cmd.done); 229 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK && 230 io_req_tracker && 231 io_req_tracker->msix_io < adapter->adapter_attr.rq_cnt) 232 msix_task = io_req_tracker->msix_io; 233 else 234 msix_task = 0; 235 taskid = adapter->driver_cmds.tm_cmd.hp_taskid; 236 leapraid_fire_hpr_task(adapter, taskid, msix_task); 237 wait_for_completion_timeout(&adapter->driver_cmds.tm_cmd.done, 238 LEAPRAID_TM_CMD_TIMEOUT * HZ); 239 if (!(adapter->driver_cmds.tm_cmd.status & LEAPRAID_CMD_DONE)) { 240 dev_err(&adapter->pdev->dev, 241 "%s: TM cmd timeout, status=0x%x\n", 242 __func__, adapter->driver_cmds.tm_cmd.status); 243 leapraid_log_req_context(adapter, taskid, scsi_tm_req); 244 issue_reset = 245 leapraid_check_reset( 246 adapter->driver_cmds.tm_cmd.status); 247 if (issue_reset) { 248 dev_info(&adapter->pdev->dev, 249 "%s:%d: call hard_reset\n", 250 __func__, __LINE__); 251 rc = leapraid_hard_reset_handler(adapter, FULL_RESET); 252 rc = !rc ? SUCCESS : FAILED; 253 goto out_cleanup; 254 } 255 } 256 257 leapraid_sync_irqs(adapter, false); 258 259 switch (type) { 260 case LEAPRAID_TM_TASKTYPE_TARGET_RESET: 261 case LEAPRAID_TM_TASKTYPE_ABRT_TASK_SET: 262 case LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET: 263 rc = leapraid_tm_post_processing(adapter, hdl, channel, id, 264 lun, type, target_taskid); 265 break; 266 case LEAPRAID_TM_TASKTYPE_ABORT_TASK: 267 rc = SUCCESS; 268 scsiio_req = leapraid_get_task_desc(adapter, target_taskid); 269 if (le16_to_cpu(scsiio_req->dev_hdl) != hdl) 270 break; 271 dev_err(&adapter->pdev->dev, "%s: Abort failed, hdl=0x%04x\n", 272 adapter->adapter_attr.name, hdl); 273 rc = FAILED; 274 break; 275 case LEAPRAID_TM_TASKTYPE_QUERY_TASK: 276 rc = SUCCESS; 277 break; 278 default: 279 rc = FAILED; 280 break; 281 } 282 283 out_cleanup: 284 leapraid_clear_tm_flg(adapter, hdl); 285 adapter->driver_cmds.tm_cmd.status = LEAPRAID_CMD_NOT_USED; 286 return rc; 287 } 288 289 int leapraid_issue_locked_tm(struct leapraid_adapter *adapter, u16 hdl, 290 uint channel, uint id, uint lun, u8 type, 291 u16 target_taskid, u8 tr_method) 292 { 293 int rc; 294 295 mutex_lock(&adapter->driver_cmds.tm_cmd.mutex); 296 rc = leapraid_issue_tm(adapter, hdl, channel, id, lun, type, 297 target_taskid, tr_method); 298 mutex_unlock(&adapter->driver_cmds.tm_cmd.mutex); 299 300 return rc; 301 } 302 303 void leapraid_smart_fault_detect(struct leapraid_adapter *adapter, u16 hdl) 304 { 305 struct leapraid_starget_priv *starget_priv; 306 struct leapraid_sas_dev *sas_dev; 307 struct scsi_target *starget; 308 unsigned long flags; 309 310 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 311 sas_dev = leapraid_hold_lock_get_sas_dev_by_hdl(adapter, hdl); 312 if (!sas_dev) { 313 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 314 return; 315 } 316 317 starget = sas_dev->starget; 318 starget_priv = starget ? starget->hostdata : NULL; 319 if (!starget_priv) { 320 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 321 goto release_sdev; 322 } 323 324 if (starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER || 325 starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) { 326 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 327 goto release_sdev; 328 } 329 330 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 331 leapraid_async_turn_on_led(adapter, hdl); 332 release_sdev: 333 if (sas_dev) 334 leapraid_sdev_put(sas_dev); 335 } 336 337 static void leapraid_process_sense_data(struct leapraid_adapter *adapter, 338 struct leapraid_scsiio_rep *scsiio_rep, 339 struct scsi_cmnd *scmd, u16 taskid) 340 { 341 struct sense_info data; 342 const void *sense_data; 343 u32 sz; 344 345 if (!(scsiio_rep->scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID)) 346 return; 347 348 sense_data = leapraid_get_sense_buffer(adapter, taskid); 349 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, 350 le32_to_cpu(scsiio_rep->sense_count)); 351 352 memcpy(scmd->sense_buffer, sense_data, sz); 353 leapraid_get_sense_data(scmd->sense_buffer, &data); 354 if (data.asc == ASC_FAILURE_PREDICTION_THRESHOLD_EXCEEDED) 355 leapraid_smart_fault_detect(adapter, 356 le16_to_cpu(scsiio_rep->dev_hdl)); 357 } 358 359 static void leapraid_handle_data_underrun( 360 struct leapraid_scsiio_rep *scsiio_rep, 361 struct scsi_cmnd *scmd, u32 xfer_cnt) 362 { 363 u8 scsi_status = scsiio_rep->scsi_status; 364 u8 scsi_state = scsiio_rep->scsi_state; 365 366 scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | scsi_status; 367 368 if (scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID) 369 return; 370 371 if (xfer_cnt < scmd->underflow) { 372 if (scsi_status == SAM_STAT_BUSY) 373 scmd->result = SAM_STAT_BUSY; 374 else 375 scmd->result = DID_SOFT_ERROR << 376 LEAPRAID_SCSI_HOST_SHIFT; 377 } else if (scsi_state & (LEAPRAID_SCSI_STATE_AUTOSENSE_FAILED | 378 LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)) { 379 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT; 380 } else if (scsi_state & LEAPRAID_SCSI_STATE_TERMINATED) { 381 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT; 382 } else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) { 383 scsiio_rep->scsi_state = LEAPRAID_SCSI_STATE_AUTOSENSE_VALID; 384 scsiio_rep->scsi_status = SAM_STAT_CHECK_CONDITION; 385 scsi_set_sense(scmd, 0, ILLEGAL_REQUEST, 386 INVALID_COMMAND_OP_CODE); 387 } 388 } 389 390 static void leapraid_handle_success_status( 391 struct leapraid_scsiio_rep *scsiio_rep, 392 struct scsi_cmnd *scmd, 393 u32 response_code) 394 { 395 u8 scsi_status = scsiio_rep->scsi_status; 396 u8 scsi_state = scsiio_rep->scsi_state; 397 398 scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | scsi_status; 399 400 if (response_code == LEAPRAID_TM_RSP_INVALID_FRAME || 401 (scsi_state & (LEAPRAID_SCSI_STATE_AUTOSENSE_FAILED | 402 LEAPRAID_SCSI_STATE_NO_SCSI_STATUS))) 403 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT; 404 else if (scsi_state & LEAPRAID_SCSI_STATE_TERMINATED) 405 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT; 406 } 407 408 static void leapraid_scsiio_done_dispatch( 409 struct leapraid_adapter *adapter, 410 struct leapraid_scsiio_rep *scsiio_rep, 411 struct leapraid_sdev_priv *sdev_priv, 412 struct scsi_cmnd *scmd, 413 u16 taskid, u32 response_code) 414 { 415 u8 scsi_status = scsiio_rep->scsi_status; 416 u8 scsi_state = scsiio_rep->scsi_state; 417 u16 adapter_status; 418 u32 xfer_cnt; 419 u32 sz; 420 421 adapter_status = le16_to_cpu(scsiio_rep->adapter_status) & 422 LEAPRAID_ADAPTER_STATUS_MASK; 423 424 xfer_cnt = le32_to_cpu(scsiio_rep->transfer_count); 425 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt); 426 427 if (adapter_status == LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN && 428 xfer_cnt == 0 && 429 (scsi_status == LEAPRAID_SCSI_STATUS_BUSY || 430 scsi_status == LEAPRAID_SCSI_STATUS_RESERVATION_CONFLICT || 431 scsi_status == LEAPRAID_SCSI_STATUS_TASK_SET_FULL)) 432 adapter_status = LEAPRAID_ADAPTER_STATUS_SUCCESS; 433 434 switch (adapter_status) { 435 case LEAPRAID_ADAPTER_STATUS_SCSI_DEVICE_NOT_THERE: 436 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT; 437 break; 438 439 case LEAPRAID_ADAPTER_STATUS_BUSY: 440 case LEAPRAID_ADAPTER_STATUS_INSUFFICIENT_RESOURCES: 441 scmd->result = SAM_STAT_BUSY; 442 break; 443 444 case LEAPRAID_ADAPTER_STATUS_SCSI_RESIDUAL_MISMATCH: 445 if (xfer_cnt == 0 || scmd->underflow > xfer_cnt) 446 scmd->result = DID_SOFT_ERROR << 447 LEAPRAID_SCSI_HOST_SHIFT; 448 else 449 scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | 450 scsi_status; 451 break; 452 453 case LEAPRAID_ADAPTER_STATUS_SCSI_ADAPTER_TERMINATED: 454 if (sdev_priv->block) { 455 scmd->result = DID_TRANSPORT_DISRUPTED << 456 LEAPRAID_SCSI_HOST_SHIFT; 457 return; 458 } 459 460 if (scmd->device->channel == RAID_CHANNEL && 461 scsi_state == (LEAPRAID_SCSI_STATE_TERMINATED | 462 LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)) { 463 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT; 464 break; 465 } 466 467 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT; 468 break; 469 470 case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_TERMINATED: 471 case LEAPRAID_ADAPTER_STATUS_SCSI_EXT_TERMINATED: 472 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT; 473 break; 474 475 case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN: 476 leapraid_handle_data_underrun(scsiio_rep, scmd, xfer_cnt); 477 break; 478 479 case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_OVERRUN: 480 scsi_set_resid(scmd, 0); 481 leapraid_handle_success_status(scsiio_rep, scmd, 482 response_code); 483 break; 484 case LEAPRAID_ADAPTER_STATUS_SCSI_RECOVERED_ERROR: 485 case LEAPRAID_ADAPTER_STATUS_SUCCESS: 486 leapraid_handle_success_status(scsiio_rep, scmd, 487 response_code); 488 break; 489 490 case LEAPRAID_ADAPTER_STATUS_SCSI_PROTOCOL_ERROR: 491 case LEAPRAID_ADAPTER_STATUS_INTERNAL_ERROR: 492 case LEAPRAID_ADAPTER_STATUS_SCSI_IO_DATA_ERROR: 493 case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_MGMT_FAILED: 494 default: 495 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT; 496 break; 497 } 498 499 if (!scmd->result) 500 return; 501 502 scsi_print_command(scmd); 503 dev_warn(&adapter->pdev->dev, 504 "SCSI I/O: hdl=0x%x, status: 0x%x, 0x%x, 0x%x\n", 505 le16_to_cpu(scsiio_rep->dev_hdl), adapter_status, 506 scsi_status, scsi_state); 507 508 if (scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID) { 509 struct scsi_sense_hdr sshdr; 510 511 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, 512 le32_to_cpu(scsiio_rep->sense_count)); 513 if (scsi_normalize_sense(scmd->sense_buffer, sz, 514 &sshdr)) 515 dev_warn(&adapter->pdev->dev, 516 "Sense: key=0x%x asc=0x%x ascq=0x%x\n", 517 sshdr.sense_key, 518 scsi_sense_asc(&sshdr), 519 scsi_sense_ascq(&sshdr)); 520 else 521 dev_warn(&adapter->pdev->dev, 522 "Sense: Invalid sense data\n"); 523 } 524 } 525 526 bool leapraid_scsiio_done(struct leapraid_adapter *adapter, u16 taskid, 527 u8 msix_index, u32 rep) 528 { 529 struct leapraid_scsiio_rep *scsiio_rep; 530 struct leapraid_sdev_priv *sdev_priv; 531 struct scsi_cmnd *scmd; 532 u32 response_code = 0; 533 534 scmd = leapraid_get_scmd_from_taskid(adapter, taskid); 535 if (!scmd) 536 return true; 537 538 scsiio_rep = leapraid_get_reply_vaddr(adapter, rep); 539 if (!scsiio_rep) { 540 scmd->result = DID_OK << LEAPRAID_SCSI_HOST_SHIFT; 541 goto out_scsiio_done; 542 } 543 544 sdev_priv = scmd->device->hostdata; 545 if (!sdev_priv || 546 !sdev_priv->starget_priv || 547 sdev_priv->starget_priv->deleted) { 548 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT; 549 goto out_scsiio_done; 550 } 551 552 if (scsiio_rep->scsi_state & LEAPRAID_SCSI_STATE_RESPONSE_INFO_VALID) 553 response_code = le32_to_cpu(scsiio_rep->resp_info) & 0xFF; 554 555 leapraid_process_sense_data(adapter, scsiio_rep, scmd, taskid); 556 leapraid_scsiio_done_dispatch(adapter, scsiio_rep, sdev_priv, scmd, 557 taskid, response_code); 558 559 out_scsiio_done: 560 scsi_dma_unmap(scmd); 561 leapraid_free_taskid(adapter, taskid); 562 scsi_done(scmd); 563 return false; 564 } 565 566 static void leapraid_probe_raid(struct leapraid_adapter *adapter) 567 { 568 struct leapraid_raid_volume *raid_volume, *next_raid_volume; 569 unsigned long flags; 570 LIST_HEAD(head); 571 int rc; 572 573 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags); 574 list_splice_init(&adapter->dev_topo.raid_volume_list, &head); 575 spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock, flags); 576 577 list_for_each_entry_safe(raid_volume, next_raid_volume, &head, list) { 578 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags); 579 list_move_tail(&raid_volume->list, 580 &adapter->dev_topo.raid_volume_list); 581 if (raid_volume->starget) { 582 spin_unlock_irqrestore( 583 &adapter->dev_topo.raid_volume_lock, flags); 584 continue; 585 } 586 587 leapraid_raid_volume_get(raid_volume); 588 spin_unlock_irqrestore( 589 &adapter->dev_topo.raid_volume_lock, flags); 590 591 rc = scsi_add_device(adapter->shost, RAID_CHANNEL, 592 raid_volume->id, 0); 593 if (rc) 594 leapraid_raid_volume_remove(adapter, raid_volume); 595 596 leapraid_raid_volume_put(raid_volume); 597 } 598 } 599 600 static void leapraid_sas_dev_make_active(struct leapraid_adapter *adapter, 601 struct leapraid_sas_dev *sas_dev) 602 { 603 unsigned long flags; 604 605 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 606 if (!list_empty(&sas_dev->list)) { 607 list_del_init(&sas_dev->list); 608 leapraid_sdev_put(sas_dev); 609 } 610 611 leapraid_sdev_get(sas_dev); 612 list_add_tail(&sas_dev->list, &adapter->dev_topo.sas_dev_list); 613 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 614 } 615 616 static void leapraid_probe_sas(struct leapraid_adapter *adapter) 617 { 618 struct leapraid_sas_dev *sas_dev; 619 bool added; 620 621 for (;;) { 622 sas_dev = leapraid_get_next_sas_dev_from_init_list(adapter); 623 if (!sas_dev) 624 break; 625 added = leapraid_transport_port_add(adapter, 626 sas_dev->hdl, 627 sas_dev->parent_sas_addr, 628 sas_dev->card_port); 629 if (!added) 630 goto remove_dev; 631 632 if (!sas_dev->starget && 633 !adapter->scan_dev_desc.driver_loading) { 634 leapraid_transport_port_remove(adapter, 635 sas_dev->sas_addr, 636 sas_dev->parent_sas_addr, 637 sas_dev->card_port); 638 goto remove_dev; 639 } 640 641 leapraid_sas_dev_make_active(adapter, sas_dev); 642 leapraid_sdev_put(sas_dev); 643 continue; 644 645 remove_dev: 646 leapraid_sas_dev_remove(adapter, sas_dev); 647 leapraid_sdev_put(sas_dev); 648 } 649 } 650 651 static bool leapraid_get_boot_dev(struct leapraid_adapter *adapter, 652 struct leapraid_boot_dev *boot_dev, 653 void **pdev, u32 *pchnl) 654 { 655 unsigned long flags; 656 void *dev; 657 u32 chnl; 658 659 spin_lock_irqsave(&adapter->boot_devs.lock, flags); 660 if (!boot_dev->dev) { 661 spin_unlock_irqrestore(&adapter->boot_devs.lock, flags); 662 return false; 663 } 664 665 dev = boot_dev->dev; 666 chnl = boot_dev->chnl; 667 leapraid_boot_dev_get(dev, chnl); 668 spin_unlock_irqrestore(&adapter->boot_devs.lock, flags); 669 670 *pdev = dev; 671 *pchnl = chnl; 672 return true; 673 } 674 675 static void leapraid_probe_boot_dev(struct leapraid_adapter *adapter) 676 { 677 struct leapraid_raid_volume *raid_volume; 678 struct leapraid_sas_dev *sas_dev; 679 struct leapraid_sas_port *sport; 680 unsigned long flags; 681 void *dev = NULL; 682 u32 chnl; 683 684 if (leapraid_get_boot_dev(adapter, 685 &adapter->boot_devs.requested_boot_dev, 686 &dev, &chnl)) 687 goto boot_dev_found; 688 689 if (leapraid_get_boot_dev(adapter, 690 &adapter->boot_devs.requested_alt_boot_dev, 691 &dev, &chnl)) 692 goto boot_dev_found; 693 694 if (leapraid_get_boot_dev(adapter, 695 &adapter->boot_devs.current_boot_dev, 696 &dev, &chnl)) 697 goto boot_dev_found; 698 699 return; 700 701 boot_dev_found: 702 switch (chnl) { 703 case RAID_CHANNEL: 704 raid_volume = dev; 705 706 if (raid_volume->starget) 707 break; 708 709 /* TODO eedp */ 710 711 if (scsi_add_device(adapter->shost, RAID_CHANNEL, 712 raid_volume->id, 0)) 713 leapraid_raid_volume_remove(adapter, raid_volume); 714 break; 715 default: 716 sas_dev = dev; 717 718 if (sas_dev->starget) 719 break; 720 721 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 722 list_move_tail(&sas_dev->list, 723 &adapter->dev_topo.sas_dev_list); 724 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 725 726 if (!sas_dev->card_port) 727 break; 728 729 sport = leapraid_transport_port_add(adapter, sas_dev->hdl, 730 sas_dev->parent_sas_addr, 731 sas_dev->card_port); 732 if (!sport) 733 leapraid_sas_dev_remove(adapter, sas_dev); 734 break; 735 } 736 737 leapraid_boot_dev_put(dev, chnl); 738 } 739 740 static void leapraid_probe_devices(struct leapraid_adapter *adapter) 741 { 742 leapraid_probe_boot_dev(adapter); 743 744 if (adapter->adapter_attr.raid_support) { 745 leapraid_probe_raid(adapter); 746 leapraid_probe_sas(adapter); 747 } else { 748 leapraid_probe_sas(adapter); 749 } 750 } 751 752 void leapraid_scan_dev_done(struct leapraid_adapter *adapter) 753 { 754 if (adapter->scan_dev_desc.wait_scan_dev_done) { 755 adapter->scan_dev_desc.wait_scan_dev_done = 0; 756 leapraid_probe_devices(adapter); 757 } 758 759 adapter->scan_dev_desc.scan_start = 0; 760 761 leapraid_check_scheduled_fault_start(adapter); 762 leapraid_fw_log_start(adapter); 763 adapter->scan_dev_desc.driver_loading = 0; 764 wake_up(&adapter->scan_dev_desc.wait_driver_loading); 765 } 766 767 static const struct pci_device_id leapraid_pci_table[] = { 768 { PCI_DEVICE_SUB(LEAPRAID_VENDOR_ID, LEAPRAID_DEVID_HBA, 769 LEAPRAID_SUBVENDOR_ID, 770 LEAPRAID_SUBDEVID_HBA) }, 771 { 0, } 772 }; 773 774 static inline bool leapraid_is_scmd_permitted(struct leapraid_adapter *adapter, 775 struct scsi_cmnd *scmd) 776 { 777 u8 opcode; 778 779 if (adapter->access_ctrl.pcie_recovering || 780 atomic_read(&adapter->overheat_desc.thermal_alert)) 781 return false; 782 783 if (adapter->access_ctrl.host_removing) { 784 if (leapraid_pci_removed(adapter)) 785 return false; 786 787 opcode = scmd->cmnd[0]; 788 return opcode == SYNCHRONIZE_CACHE || opcode == START_STOP; 789 } 790 return true; 791 } 792 793 static bool leapraid_should_queuecommand(struct leapraid_adapter *adapter, 794 struct leapraid_sdev_priv *sdev_priv, 795 struct scsi_cmnd *scmd, 796 enum scsi_qc_status *rc) 797 { 798 struct leapraid_starget_priv *starget_priv; 799 800 if (!sdev_priv || !sdev_priv->starget_priv) 801 goto no_connect; 802 803 if (!leapraid_is_scmd_permitted(adapter, scmd)) 804 goto no_connect; 805 806 starget_priv = sdev_priv->starget_priv; 807 if (starget_priv->hdl == LEAPRAID_INVALID_DEV_HANDLE) 808 goto no_connect; 809 810 if (sdev_priv->block && 811 scsi_get_host_state(scmd->device->host) == SHOST_RECOVERY && 812 scmd->cmnd[0] == TEST_UNIT_READY) { 813 scsi_set_sense(scmd, 0, UNIT_ATTENTION, 814 I_T_NEXUS_LOSS_OCCURRED); 815 goto scsiio_done; 816 } 817 818 if (adapter->access_ctrl.shost_recovering || 819 adapter->reset_desc.adapter_link_resetting) { 820 *rc = SCSI_MLQUEUE_HOST_BUSY; 821 return false; 822 } 823 824 if (starget_priv->deleted || sdev_priv->deleted) 825 goto no_connect; 826 827 if (starget_priv->tm_busy || sdev_priv->block) { 828 *rc = SCSI_MLQUEUE_DEVICE_BUSY; 829 return false; 830 } 831 832 return true; 833 834 no_connect: 835 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT; 836 scsiio_done: 837 scsi_done(scmd); 838 839 return false; 840 } 841 842 static u32 build_scsiio_req_control(struct scsi_cmnd *scmd, 843 struct leapraid_sdev_priv *sdev_priv) 844 { 845 u32 control; 846 847 switch (scmd->sc_data_direction) { 848 case DMA_FROM_DEVICE: 849 control = LEAPRAID_SCSIIO_CTRL_READ; 850 break; 851 case DMA_TO_DEVICE: 852 control = LEAPRAID_SCSIIO_CTRL_WRITE; 853 break; 854 default: 855 control = LEAPRAID_SCSIIO_CTRL_NODATATRANSFER; 856 break; 857 } 858 859 control |= LEAPRAID_SCSIIO_CTRL_SIMPLEQ; 860 861 if (sdev_priv->ncq_prio_enable && 862 (IOPRIO_PRIO_CLASS(req_get_ioprio(scsi_cmd_to_rq(scmd))) == 863 IOPRIO_CLASS_RT)) 864 control |= LEAPRAID_SCSIIO_CTRL_CMDPRI; 865 if (scmd->cmd_len == 32) 866 control |= LEAPRAID_SCSIIO_CTRL_CDB_32BYTE << 867 LEAPRAID_SCSIIO_CTRL_CDB_LEN_SHIFT; 868 869 return control; 870 } 871 872 enum scsi_qc_status leapraid_queuecommand(struct Scsi_Host *shost, 873 struct scsi_cmnd *scmd) 874 { 875 struct leapraid_adapter *adapter = shost_priv(scmd->device->host); 876 struct leapraid_sdev_priv *sdev_priv = scmd->device->hostdata; 877 struct leapraid_starget_priv *starget_priv; 878 struct leapraid_scsiio_req *scsiio_req; 879 u32 control; 880 u16 taskid; 881 u16 hdl; 882 enum scsi_qc_status rc = 0; 883 884 if (!leapraid_should_queuecommand(adapter, sdev_priv, scmd, &rc)) 885 return rc; 886 887 starget_priv = sdev_priv->starget_priv; 888 hdl = starget_priv->hdl; 889 control = build_scsiio_req_control(scmd, sdev_priv); 890 891 taskid = leapraid_alloc_scsiio_taskid(adapter, scmd); 892 scsiio_req = leapraid_get_task_desc(adapter, taskid); 893 894 scsiio_req->func = LEAPRAID_FUNC_SCSIIO; 895 if (sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER) 896 scsiio_req->func = LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH; 897 else 898 scsiio_req->func = LEAPRAID_FUNC_SCSIIO; 899 900 scsiio_req->dev_hdl = cpu_to_le16(hdl); 901 scsiio_req->data_len = cpu_to_le32(scsi_bufflen(scmd)); 902 scsiio_req->ctrl = cpu_to_le32(control); 903 scsiio_req->io_flg = cpu_to_le16(scmd->cmd_len); 904 scsiio_req->msg_flg = 0; 905 scsiio_req->sense_buffer_len = SCSI_SENSE_BUFFERSIZE; 906 scsiio_req->sense_buffer_low_add = 907 leapraid_get_sense_buffer_dma(adapter, taskid); 908 scsiio_req->sgl_offset0 = 909 offsetof(struct leapraid_scsiio_req, sgl) / 910 LEAPRAID_DWORDS_BYTE_SIZE; 911 int_to_scsilun(sdev_priv->lun, (struct scsi_lun *)scsiio_req->lun); 912 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len); 913 if (scsiio_req->data_len) { 914 if (leapraid_build_scmd_ieee_sg(adapter, scmd, taskid)) { 915 leapraid_free_taskid(adapter, taskid); 916 return SCSI_MLQUEUE_HOST_BUSY; 917 } 918 } else { 919 leapraid_build_ieee_nodata_sg(adapter, &scsiio_req->sgl); 920 } 921 922 if (likely(scsiio_req->func == LEAPRAID_FUNC_SCSIIO)) 923 leapraid_fire_scsi_io(adapter, taskid, 924 le16_to_cpu(scsiio_req->dev_hdl)); 925 else 926 leapraid_fire_task(adapter, taskid); 927 928 dev_dbg(&adapter->pdev->dev, 929 "LEAPRAID_SCSIIO: Send Descriptor taskid %d, req type 0x%x\n", 930 taskid, scsiio_req->func); 931 return rc; 932 } 933 934 static int leapraid_init_cmd_priv(struct Scsi_Host *shost, 935 struct scsi_cmnd *scmd) 936 { 937 struct leapraid_adapter *adapter = shost_priv(shost); 938 struct leapraid_io_req_tracker *io_tracker; 939 940 io_tracker = scsi_cmd_priv(scmd); 941 leapraid_internal_init_cmd_priv(adapter, io_tracker); 942 943 return 0; 944 } 945 946 static int leapraid_exit_cmd_priv(struct Scsi_Host *shost, 947 struct scsi_cmnd *scmd) 948 { 949 struct leapraid_adapter *adapter = shost_priv(shost); 950 struct leapraid_io_req_tracker *io_tracker; 951 952 io_tracker = scsi_cmd_priv(scmd); 953 leapraid_internal_exit_cmd_priv(adapter, io_tracker); 954 955 return 0; 956 } 957 958 static int leapraid_error_handler(struct scsi_cmnd *scmd, 959 const char *str, u8 type) 960 { 961 struct leapraid_adapter *adapter = shost_priv(scmd->device->host); 962 struct scsi_target *starget = scmd->device->sdev_target; 963 struct leapraid_starget_priv *starget_priv = starget->hostdata; 964 struct leapraid_io_req_tracker *io_req_tracker = NULL; 965 struct leapraid_sdev_priv *sdev_priv; 966 struct leapraid_sas_dev *sas_dev = NULL; 967 u16 hdl; 968 int rc; 969 970 dev_info(&adapter->pdev->dev, 971 "EH enter: type=%s, scmd=0x%p, req tag=%d\n", str, scmd, 972 scsi_cmd_to_rq(scmd)->tag); 973 scsi_print_command(scmd); 974 975 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) { 976 io_req_tracker = scsi_cmd_priv(scmd); 977 dev_info(&adapter->pdev->dev, 978 "EH ABORT: scmd=0x%p, pend=%ums, tout=%ums, tag=%d\n", 979 scmd, 980 jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc), 981 (scsi_cmd_to_rq(scmd)->timeout / HZ) * 1000, 982 scsi_cmd_to_rq(scmd)->tag); 983 } 984 985 if (leapraid_pci_removed(adapter) || 986 adapter->access_ctrl.host_removing) { 987 dev_err(&adapter->pdev->dev, 988 "EH %s failed: %s scmd=0x%p\n", str, 989 (adapter->access_ctrl.host_removing ? 990 "shost removing!" : "pci_dev removed!"), scmd); 991 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK && 992 io_req_tracker && io_req_tracker->taskid) 993 leapraid_free_taskid(adapter, io_req_tracker->taskid); 994 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT; 995 #ifdef FAST_IO_FAIL 996 rc = FAST_IO_FAIL; 997 #else 998 rc = FAILED; 999 #endif 1000 goto out_eh_done; 1001 } 1002 1003 sdev_priv = scmd->device->hostdata; 1004 if (!sdev_priv || !sdev_priv->starget_priv) { 1005 dev_warn(&adapter->pdev->dev, 1006 "EH %s: SAS dev or starget gone, scmd=0x%p\n", 1007 str, scmd); 1008 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT; 1009 scsi_done(scmd); 1010 rc = SUCCESS; 1011 goto out_eh_done; 1012 } 1013 1014 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) { 1015 if (!io_req_tracker) { 1016 dev_warn(&adapter->pdev->dev, 1017 "EH ABORT: No I/O tracker, scmd 0x%p\n", scmd); 1018 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT; 1019 rc = SUCCESS; 1020 goto out_eh_done; 1021 } 1022 1023 if (sdev_priv->starget_priv->flg & 1024 LEAPRAID_TGT_FLG_RAID_MEMBER || 1025 sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) { 1026 dev_err(&adapter->pdev->dev, 1027 "EH ABORT: Skip RAID/VOLUME, scmd=0x%p\n", 1028 scmd); 1029 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT; 1030 rc = FAILED; 1031 goto out_eh_done; 1032 } 1033 1034 hdl = sdev_priv->starget_priv->hdl; 1035 } else { 1036 hdl = 0; 1037 if (sdev_priv->starget_priv->flg & 1038 LEAPRAID_TGT_FLG_RAID_MEMBER) { 1039 sas_dev = leapraid_get_sas_dev_from_tgt(adapter, 1040 starget_priv); 1041 if (sas_dev) 1042 hdl = sas_dev->volume_hdl; 1043 } else { 1044 hdl = sdev_priv->starget_priv->hdl; 1045 } 1046 1047 if (!hdl) { 1048 dev_err(&adapter->pdev->dev, 1049 "EH %s failed: Target handle 0, scmd=0x%p\n", 1050 str, scmd); 1051 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT; 1052 rc = FAILED; 1053 goto out_eh_done; 1054 } 1055 } 1056 1057 dev_info(&adapter->pdev->dev, 1058 "EH issue TM: type=%s, scmd=0x%p, hdl=0x%x\n", 1059 str, scmd, hdl); 1060 1061 rc = leapraid_issue_locked_tm( 1062 adapter, 1063 hdl, 1064 scmd->device->channel, 1065 scmd->device->id, 1066 (type == LEAPRAID_TM_TASKTYPE_TARGET_RESET ? 1067 0 : scmd->device->lun), 1068 type, 1069 (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK ? 1070 io_req_tracker->taskid : 0), 1071 LEAPRAID_TM_MSGFLAGS_LINK_RESET); 1072 1073 out_eh_done: 1074 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) { 1075 if (rc != SUCCESS) 1076 dev_err(&adapter->pdev->dev, 1077 "EH ABORT result: failed, scmd=0x%p\n", 1078 scmd); 1079 } else { 1080 if (rc != SUCCESS) 1081 dev_err(&adapter->pdev->dev, 1082 "EH %s result: failed, scmd=0x%p\n", 1083 str, scmd); 1084 if (sas_dev) 1085 leapraid_sdev_put(sas_dev); 1086 } 1087 return rc; 1088 } 1089 1090 static int leapraid_eh_abort_handler(struct scsi_cmnd *scmd) 1091 { 1092 return leapraid_error_handler(scmd, "ABORT TASK", 1093 LEAPRAID_TM_TASKTYPE_ABORT_TASK); 1094 } 1095 1096 static int leapraid_eh_device_reset_handler(struct scsi_cmnd *scmd) 1097 { 1098 return leapraid_error_handler(scmd, "UNIT RESET", 1099 LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET); 1100 } 1101 1102 static int leapraid_eh_target_reset_handler(struct scsi_cmnd *scmd) 1103 { 1104 return leapraid_error_handler(scmd, "TARGET RESET", 1105 LEAPRAID_TM_TASKTYPE_TARGET_RESET); 1106 } 1107 1108 static int leapraid_eh_host_reset_handler(struct scsi_cmnd *scmd) 1109 { 1110 struct leapraid_adapter *adapter = shost_priv(scmd->device->host); 1111 int rc; 1112 1113 dev_info(&adapter->pdev->dev, 1114 "EH HOST RESET enter: scmd=%p, req tag=%d\n", 1115 scmd, 1116 scsi_cmd_to_rq(scmd)->tag); 1117 scsi_print_command(scmd); 1118 1119 if (adapter->scan_dev_desc.driver_loading || 1120 adapter->access_ctrl.host_removing) { 1121 dev_err(&adapter->pdev->dev, 1122 "EH HOST RESET failed: %s scmd=0x%p\n", 1123 (adapter->access_ctrl.host_removing ? 1124 "shost removing!" : "driver loading!"), scmd); 1125 rc = FAILED; 1126 goto out_host_reset_done; 1127 } 1128 1129 dev_info(&adapter->pdev->dev, "%s:%d: Issuing hard reset\n", 1130 __func__, __LINE__); 1131 if (leapraid_hard_reset_handler(adapter, FULL_RESET) < 0) 1132 rc = FAILED; 1133 else 1134 rc = SUCCESS; 1135 1136 out_host_reset_done: 1137 if (rc != SUCCESS) 1138 dev_err(&adapter->pdev->dev, 1139 "EH HOST RESET result: failed, scmd=0x%p\n", 1140 scmd); 1141 return rc; 1142 } 1143 1144 static int leapraid_sdev_init(struct scsi_device *sdev) 1145 { 1146 struct leapraid_raid_volume *raid_volume; 1147 struct leapraid_starget_priv *stgt_priv; 1148 struct leapraid_sdev_priv *sdev_priv; 1149 struct leapraid_adapter *adapter; 1150 struct leapraid_sas_dev *sas_dev; 1151 struct scsi_target *tgt; 1152 struct Scsi_Host *shost; 1153 unsigned long flags; 1154 1155 sdev_priv = kzalloc_obj(*sdev_priv); 1156 if (!sdev_priv) 1157 return -ENOMEM; 1158 1159 sdev_priv->lun = sdev->lun; 1160 sdev_priv->flg = LEAPRAID_DEVICE_FLG_INIT; 1161 tgt = scsi_target(sdev); 1162 stgt_priv = tgt->hostdata; 1163 stgt_priv->num_luns++; 1164 sdev_priv->starget_priv = stgt_priv; 1165 sdev->hostdata = sdev_priv; 1166 if (stgt_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER) 1167 sdev->no_uld_attach = LEAPRAID_NO_ULD_ATTACH; 1168 1169 shost = dev_to_shost(&tgt->dev); 1170 adapter = shost_priv(shost); 1171 if (tgt->channel == RAID_CHANNEL) { 1172 raid_volume = leapraid_raid_volume_find_by_id(adapter, 1173 tgt->id, 1174 tgt->channel); 1175 if (raid_volume) { 1176 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, 1177 flags); 1178 raid_volume->sdev = sdev; 1179 spin_unlock_irqrestore( 1180 &adapter->dev_topo.raid_volume_lock, flags); 1181 leapraid_raid_volume_put(raid_volume); 1182 } 1183 } 1184 1185 if (!(stgt_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) { 1186 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 1187 sas_dev = leapraid_hold_lock_get_sas_dev_by_addr( 1188 adapter, 1189 stgt_priv->sas_address, 1190 stgt_priv->card_port); 1191 if (sas_dev && !sas_dev->starget) { 1192 sdev_printk(KERN_INFO, sdev, 1193 "%s: Assign starget to sas_dev\n", 1194 __func__); 1195 sas_dev->starget = tgt; 1196 } 1197 1198 if (sas_dev) 1199 leapraid_sdev_put(sas_dev); 1200 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 1201 } 1202 return 0; 1203 } 1204 1205 static bool leapraid_slave_cfg_volume(struct scsi_device *sdev, 1206 struct queue_limits *lim) 1207 { 1208 struct Scsi_Host *shost = sdev->host; 1209 struct leapraid_adapter *adapter = shost_priv(shost); 1210 struct leapraid_raid_volume *raid_volume; 1211 struct leapraid_starget_priv *starget_priv; 1212 struct leapraid_sdev_priv *sdev_priv; 1213 int qd; 1214 u16 hdl; 1215 1216 sdev_priv = sdev->hostdata; 1217 starget_priv = sdev_priv->starget_priv; 1218 hdl = starget_priv->hdl; 1219 1220 raid_volume = leapraid_raid_volume_find_by_hdl(adapter, hdl); 1221 if (!raid_volume) { 1222 sdev_printk(KERN_WARNING, sdev, 1223 "%s: RAID volume not found, hdl=0x%x\n", 1224 __func__, hdl); 1225 return 1; 1226 } 1227 1228 if (leapraid_get_volume_cap(adapter, raid_volume)) { 1229 sdev_printk(KERN_ERR, sdev, 1230 "%s: Failed to get volume cap, hdl=0x%x\n", 1231 __func__, hdl); 1232 leapraid_raid_volume_put(raid_volume); 1233 return 1; 1234 } 1235 1236 qd = (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT) ? 1237 adapter->adapter_attr.narrowport_max_queue_depth : 1238 adapter->adapter_attr.sata_max_queue_depth; 1239 if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0) 1240 qd = adapter->adapter_attr.raid_volume_max_queue_depth; 1241 1242 sdev_printk(KERN_INFO, sdev, 1243 "RAID volume: hdl=0x%04x, wwid=0x%016llx\n", 1244 raid_volume->hdl, (unsigned long long)raid_volume->wwid); 1245 1246 if (shost->max_sectors > LEAPRAID_MAX_SECTORS) 1247 lim->max_hw_sectors = LEAPRAID_MAX_SECTORS; 1248 1249 leapraid_change_queue_depth(sdev, qd); 1250 leapraid_raid_volume_put(raid_volume); 1251 return 0; 1252 } 1253 1254 static bool leapraid_slave_configure_extra(struct scsi_device *sdev, 1255 struct leapraid_sas_dev **psas_dev, 1256 u16 vol_hdl, u64 volume_wwid, 1257 bool *is_target_ssp, int *qd) 1258 { 1259 struct leapraid_sas_dev *sas_dev; 1260 struct leapraid_sdev_priv *sdev_priv; 1261 struct Scsi_Host *shost = sdev->host; 1262 struct leapraid_adapter *adapter = shost_priv(shost); 1263 unsigned long flags; 1264 1265 sdev_priv = sdev->hostdata; 1266 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 1267 *is_target_ssp = false; 1268 sas_dev = leapraid_hold_lock_get_sas_dev_by_addr( 1269 adapter, 1270 sdev_priv->starget_priv->sas_address, 1271 sdev_priv->starget_priv->card_port); 1272 if (!sas_dev) { 1273 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 1274 sdev_printk(KERN_WARNING, sdev, 1275 "%s: SAS dev not found, sas=0x%llx\n", 1276 __func__, sdev_priv->starget_priv->sas_address); 1277 return 1; 1278 } 1279 1280 *psas_dev = sas_dev; 1281 sas_dev->volume_hdl = vol_hdl; 1282 sas_dev->volume_wwid = volume_wwid; 1283 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) { 1284 *qd = (sas_dev->port_connection > 1) ? 1285 adapter->adapter_attr.wideport_max_queue_depth : 1286 adapter->adapter_attr.narrowport_max_queue_depth; 1287 *is_target_ssp = true; 1288 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SEP) 1289 sdev_priv->sep = 1; 1290 } else { 1291 *qd = adapter->adapter_attr.sata_max_queue_depth; 1292 } 1293 1294 sdev_printk(KERN_INFO, sdev, 1295 "device name=0x%016llx, SAS addr=0x%016llx\n", 1296 (unsigned long long)sas_dev->dev_name, 1297 (unsigned long long)sas_dev->sas_addr); 1298 leapraid_sdev_put(sas_dev); 1299 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 1300 return 0; 1301 } 1302 1303 static int leapraid_sdev_configure(struct scsi_device *sdev, 1304 struct queue_limits *lim) 1305 { 1306 struct leapraid_sas_dev *sas_dev; 1307 struct leapraid_sdev_priv *sdev_priv; 1308 struct Scsi_Host *shost = sdev->host; 1309 struct leapraid_starget_priv *starget_priv; 1310 struct leapraid_adapter *adapter; 1311 u16 hdl, vol_hdl = 0; 1312 bool is_target_ssp = false; 1313 u64 volume_wwid = 0; 1314 int qd = 1; 1315 1316 adapter = shost_priv(shost); 1317 sdev_priv = sdev->hostdata; 1318 sdev_priv->flg &= ~LEAPRAID_DEVICE_FLG_INIT; 1319 starget_priv = sdev_priv->starget_priv; 1320 hdl = starget_priv->hdl; 1321 if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) 1322 return leapraid_slave_cfg_volume(sdev, lim); 1323 1324 if (starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER) { 1325 if (leapraid_cfg_get_volume_hdl(adapter, hdl, &vol_hdl)) { 1326 sdev_printk(KERN_WARNING, sdev, 1327 "%s: Get volume hdl failed, hdl=0x%x\n", 1328 __func__, hdl); 1329 return 1; 1330 } 1331 1332 if (vol_hdl && leapraid_cfg_get_volume_wwid(adapter, vol_hdl, 1333 &volume_wwid)) { 1334 sdev_printk(KERN_WARNING, sdev, 1335 "%s: Get wwid failed, volume_hdl=0x%x\n", 1336 __func__, vol_hdl); 1337 return 1; 1338 } 1339 } 1340 1341 if (leapraid_slave_configure_extra(sdev, &sas_dev, vol_hdl, 1342 volume_wwid, &is_target_ssp, &qd)) { 1343 sdev_printk(KERN_WARNING, sdev, 1344 "%s: slave_configure_extra failed\n", __func__); 1345 return 1; 1346 } 1347 1348 leapraid_change_queue_depth(sdev, qd); 1349 if (is_target_ssp) 1350 sas_read_port_mode_page(sdev); 1351 1352 return 0; 1353 } 1354 1355 static void leapraid_sdev_destroy(struct scsi_device *sdev) 1356 { 1357 struct leapraid_adapter *adapter; 1358 struct Scsi_Host *shost; 1359 struct leapraid_sas_dev *sas_dev; 1360 struct leapraid_starget_priv *starget_priv; 1361 struct scsi_target *stgt; 1362 unsigned long flags; 1363 1364 if (!sdev->hostdata) 1365 return; 1366 1367 stgt = scsi_target(sdev); 1368 starget_priv = stgt->hostdata; 1369 starget_priv->num_luns--; 1370 shost = dev_to_shost(&stgt->dev); 1371 adapter = shost_priv(shost); 1372 if (!(starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) { 1373 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 1374 sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter, 1375 starget_priv); 1376 if (sas_dev && !starget_priv->num_luns) 1377 sas_dev->starget = NULL; 1378 if (sas_dev) 1379 leapraid_sdev_put(sas_dev); 1380 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 1381 } 1382 1383 kfree(sdev->hostdata); 1384 sdev->hostdata = NULL; 1385 } 1386 1387 static int leapraid_target_alloc_raid(struct scsi_target *tgt) 1388 { 1389 struct leapraid_starget_priv *starget_priv; 1390 struct leapraid_raid_volume *raid_volume; 1391 struct Scsi_Host *shost = dev_to_shost(&tgt->dev); 1392 struct leapraid_adapter *adapter = shost_priv(shost); 1393 unsigned long flags; 1394 1395 starget_priv = tgt->hostdata; 1396 raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id, 1397 tgt->channel); 1398 if (raid_volume) { 1399 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags); 1400 starget_priv->hdl = raid_volume->hdl; 1401 starget_priv->sas_address = raid_volume->wwid; 1402 starget_priv->flg |= LEAPRAID_TGT_FLG_VOLUME; 1403 raid_volume->starget = tgt; 1404 spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock, 1405 flags); 1406 leapraid_raid_volume_put(raid_volume); 1407 } 1408 return 0; 1409 } 1410 1411 static int leapraid_target_alloc_sas(struct scsi_target *tgt) 1412 { 1413 struct sas_rphy *rphy; 1414 struct Scsi_Host *shost; 1415 struct leapraid_sas_dev *sas_dev; 1416 struct leapraid_adapter *adapter; 1417 struct leapraid_starget_priv *starget_priv; 1418 unsigned long flags; 1419 1420 shost = dev_to_shost(&tgt->dev); 1421 adapter = shost_priv(shost); 1422 starget_priv = tgt->hostdata; 1423 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 1424 rphy = dev_to_rphy(tgt->dev.parent); 1425 sas_dev = leapraid_hold_lock_get_sas_dev_by_addr_and_rphy( 1426 adapter, 1427 rphy->identify.sas_address, 1428 rphy); 1429 if (sas_dev) { 1430 starget_priv->sas_dev = sas_dev; 1431 starget_priv->card_port = sas_dev->card_port; 1432 starget_priv->sas_address = sas_dev->sas_addr; 1433 starget_priv->hdl = sas_dev->hdl; 1434 sas_dev->channel = tgt->channel; 1435 sas_dev->id = tgt->id; 1436 sas_dev->starget = tgt; 1437 if (sas_dev->hdl && 1438 sas_dev->hdl <= 1439 adapter->adapter_attr.features.max_dev_handle && 1440 test_bit(sas_dev->hdl, adapter->dev_topo.pd_hdls)) 1441 starget_priv->flg |= LEAPRAID_TGT_FLG_RAID_MEMBER; 1442 } 1443 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 1444 1445 return 0; 1446 } 1447 1448 static int leapraid_target_alloc(struct scsi_target *tgt) 1449 { 1450 struct leapraid_starget_priv *starget_priv; 1451 1452 starget_priv = kzalloc_obj(*starget_priv); 1453 if (!starget_priv) 1454 return -ENOMEM; 1455 1456 tgt->hostdata = starget_priv; 1457 starget_priv->starget = tgt; 1458 starget_priv->hdl = LEAPRAID_INVALID_DEV_HANDLE; 1459 if (tgt->channel == RAID_CHANNEL) 1460 return leapraid_target_alloc_raid(tgt); 1461 1462 return leapraid_target_alloc_sas(tgt); 1463 } 1464 1465 static void leapraid_target_destroy_raid(struct scsi_target *tgt) 1466 { 1467 struct leapraid_raid_volume *raid_volume; 1468 struct Scsi_Host *shost = dev_to_shost(&tgt->dev); 1469 struct leapraid_adapter *adapter = shost_priv(shost); 1470 unsigned long flags; 1471 1472 raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id, 1473 tgt->channel); 1474 if (raid_volume) { 1475 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags); 1476 raid_volume->starget = NULL; 1477 raid_volume->sdev = NULL; 1478 spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock, 1479 flags); 1480 leapraid_raid_volume_put(raid_volume); 1481 } 1482 } 1483 1484 static void leapraid_target_destroy_sas(struct scsi_target *tgt) 1485 { 1486 struct leapraid_adapter *adapter; 1487 struct leapraid_sas_dev *sas_dev; 1488 struct leapraid_starget_priv *starget_priv; 1489 struct Scsi_Host *shost; 1490 unsigned long flags; 1491 1492 shost = dev_to_shost(&tgt->dev); 1493 adapter = shost_priv(shost); 1494 starget_priv = tgt->hostdata; 1495 1496 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags); 1497 sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter, 1498 starget_priv); 1499 if (sas_dev && 1500 sas_dev->starget == tgt && 1501 sas_dev->id == tgt->id && 1502 sas_dev->channel == tgt->channel) 1503 sas_dev->starget = NULL; 1504 1505 if (sas_dev) { 1506 starget_priv->sas_dev = NULL; 1507 leapraid_sdev_put(sas_dev); 1508 leapraid_sdev_put(sas_dev); 1509 } 1510 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags); 1511 } 1512 1513 static void leapraid_target_destroy(struct scsi_target *tgt) 1514 { 1515 struct leapraid_starget_priv *starget_priv; 1516 1517 starget_priv = tgt->hostdata; 1518 if (!starget_priv) 1519 return; 1520 1521 if (tgt->channel == RAID_CHANNEL) { 1522 leapraid_target_destroy_raid(tgt); 1523 goto out_free; 1524 } 1525 1526 leapraid_target_destroy_sas(tgt); 1527 1528 out_free: 1529 kfree(starget_priv); 1530 tgt->hostdata = NULL; 1531 } 1532 1533 static bool leapraid_scan_check_status(struct leapraid_adapter *adapter, 1534 bool *need_hard_reset) 1535 { 1536 u32 adapter_state; 1537 1538 if (adapter->scan_dev_desc.scan_start) { 1539 adapter_state = leapraid_get_adapter_state(adapter); 1540 if (adapter_state == LEAPRAID_DB_FAULT) { 1541 *need_hard_reset = true; 1542 return true; 1543 } 1544 return false; 1545 } 1546 1547 if (adapter->driver_cmds.scan_dev_cmd.status & LEAPRAID_CMD_RESET) { 1548 dev_err(&adapter->pdev->dev, 1549 "Device scan: Aborted due to reset\n"); 1550 adapter->driver_cmds.scan_dev_cmd.status = 1551 LEAPRAID_CMD_NOT_USED; 1552 adapter->scan_dev_desc.driver_loading = 0; 1553 wake_up(&adapter->scan_dev_desc.wait_driver_loading); 1554 return true; 1555 } 1556 1557 if (adapter->scan_dev_desc.scan_start_failed) { 1558 dev_err(&adapter->pdev->dev, 1559 "Device scan: Failed with adapter_status=0x%08x\n", 1560 adapter->scan_dev_desc.scan_start_failed); 1561 adapter->scan_dev_desc.driver_loading = 0; 1562 wake_up(&adapter->scan_dev_desc.wait_driver_loading); 1563 adapter->scan_dev_desc.wait_scan_dev_done = 0; 1564 adapter->access_ctrl.host_removing = 1; 1565 return true; 1566 } 1567 1568 adapter->driver_cmds.scan_dev_cmd.status = LEAPRAID_CMD_NOT_USED; 1569 leapraid_scan_dev_done(adapter); 1570 return true; 1571 } 1572 1573 static int leapraid_scan_finished(struct Scsi_Host *shost, unsigned long time) 1574 { 1575 struct leapraid_adapter *adapter = shost_priv(shost); 1576 bool need_hard_reset = false; 1577 1578 if (time >= (LEAPRAID_SCAN_DEV_CMD_TIMEOUT * HZ)) { 1579 adapter->driver_cmds.scan_dev_cmd.status = 1580 LEAPRAID_CMD_NOT_USED; 1581 dev_err(&adapter->pdev->dev, 1582 "Device scan: Failed with timeout 300s\n"); 1583 adapter->scan_dev_desc.driver_loading = 0; 1584 wake_up(&adapter->scan_dev_desc.wait_driver_loading); 1585 return 1; 1586 } 1587 1588 if (!leapraid_scan_check_status(adapter, &need_hard_reset)) 1589 return 0; 1590 1591 if (need_hard_reset) { 1592 adapter->driver_cmds.scan_dev_cmd.status = 1593 LEAPRAID_CMD_NOT_USED; 1594 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n", 1595 __func__, __LINE__); 1596 if (leapraid_hard_reset_handler(adapter, PART_RESET)) { 1597 adapter->scan_dev_desc.driver_loading = 0; 1598 wake_up(&adapter->scan_dev_desc.wait_driver_loading); 1599 } 1600 } 1601 1602 return 1; 1603 } 1604 1605 static void leapraid_scan_start(struct Scsi_Host *shost) 1606 { 1607 struct leapraid_adapter *adapter = shost_priv(shost); 1608 1609 adapter->scan_dev_desc.scan_start = 1; 1610 leapraid_scan_dev(adapter, true); 1611 } 1612 1613 static u32 leapraid_get_raid_qd(struct leapraid_adapter *adapter, 1614 const struct leapraid_raid_volume *raid_volume, 1615 bool default_qd) 1616 { 1617 const struct leapraid_adapter_attr *attr = &adapter->adapter_attr; 1618 1619 if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0) 1620 return default_qd ? LEAPRAID_RAID_QUEUE_DEPTH : 1621 attr->raid_volume_max_queue_depth; 1622 1623 if (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT) 1624 return default_qd ? LEAPRAID_SAS_QUEUE_DEPTH : 1625 attr->narrowport_max_queue_depth; 1626 1627 return default_qd ? LEAPRAID_SATA_QUEUE_DEPTH : 1628 attr->sata_max_queue_depth; 1629 } 1630 1631 static int leapraid_calc_max_queue_depth(struct scsi_device *sdev, int qdepth) 1632 { 1633 struct Scsi_Host *shost; 1634 struct leapraid_adapter *adapter; 1635 struct leapraid_starget_priv *starget_priv; 1636 struct leapraid_sdev_priv *sdev_priv; 1637 struct leapraid_raid_volume *raid_volume; 1638 struct leapraid_sas_dev *sas_dev; 1639 int max_depth; 1640 u32 default_qdepth = 0; 1641 u32 fw_qdepth = 0; 1642 1643 shost = sdev->host; 1644 adapter = shost_priv(shost); 1645 max_depth = shost->can_queue; 1646 1647 sdev_priv = sdev->hostdata; 1648 if (!sdev_priv) 1649 goto out_tag_check; 1650 1651 starget_priv = sdev_priv->starget_priv; 1652 if (!starget_priv) 1653 goto out_tag_check; 1654 1655 if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) { 1656 raid_volume = leapraid_raid_volume_find_by_hdl( 1657 adapter, starget_priv->hdl); 1658 if (raid_volume) { 1659 default_qdepth = leapraid_get_raid_qd( 1660 adapter, raid_volume, true); 1661 fw_qdepth = leapraid_get_raid_qd( 1662 adapter, raid_volume, false); 1663 leapraid_raid_volume_put(raid_volume); 1664 } 1665 goto out_limit_check; 1666 } 1667 1668 sas_dev = leapraid_get_sas_dev_from_tgt(adapter, starget_priv); 1669 if (sas_dev) { 1670 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) { 1671 default_qdepth = LEAPRAID_SAS_QUEUE_DEPTH; 1672 fw_qdepth = (sas_dev->port_connection > 1) ? 1673 adapter->adapter_attr.wideport_max_queue_depth : 1674 adapter->adapter_attr.narrowport_max_queue_depth; 1675 } 1676 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SATA_DEV) { 1677 default_qdepth = LEAPRAID_SATA_QUEUE_DEPTH; 1678 fw_qdepth = adapter->adapter_attr.sata_max_queue_depth; 1679 } 1680 leapraid_sdev_put(sas_dev); 1681 } 1682 1683 out_limit_check: 1684 if (fw_qdepth > shost->can_queue && default_qdepth) 1685 fw_qdepth = default_qdepth; 1686 1687 if (fw_qdepth) 1688 max_depth = min_t(int, max_depth, fw_qdepth); 1689 1690 out_tag_check: 1691 if (!sdev->tagged_supported) 1692 max_depth = 1; 1693 1694 if (qdepth > max_depth) 1695 qdepth = max_depth; 1696 1697 return qdepth; 1698 } 1699 1700 int leapraid_change_queue_depth(struct scsi_device *sdev, int qdepth) 1701 { 1702 qdepth = leapraid_calc_max_queue_depth(sdev, qdepth); 1703 scsi_change_queue_depth(sdev, qdepth); 1704 return sdev->queue_depth; 1705 } 1706 1707 static void leapraid_map_queues(struct Scsi_Host *shost) 1708 { 1709 struct leapraid_adapter *adapter; 1710 struct blk_mq_queue_map *queue_map; 1711 int msix_queue_count; 1712 int poll_queue_count; 1713 int queue_offset; 1714 int map_index; 1715 1716 adapter = (struct leapraid_adapter *)shost->hostdata; 1717 if (shost->nr_hw_queues == 1) 1718 return; 1719 1720 msix_queue_count = adapter->notification_desc.iopoll_qdex; 1721 poll_queue_count = adapter->adapter_attr.rq_cnt - msix_queue_count; 1722 1723 queue_offset = 0; 1724 for (map_index = 0; map_index < shost->nr_maps; map_index++) { 1725 queue_map = &shost->tag_set.map[map_index]; 1726 queue_map->nr_queues = 0; 1727 1728 switch (map_index) { 1729 case HCTX_TYPE_DEFAULT: 1730 queue_map->nr_queues = msix_queue_count; 1731 queue_map->queue_offset = queue_offset; 1732 WARN_ON_ONCE(!queue_map->nr_queues); 1733 blk_mq_map_hw_queues(queue_map, 1734 &adapter->pdev->dev, 0); 1735 break; 1736 case HCTX_TYPE_POLL: 1737 queue_map->nr_queues = poll_queue_count; 1738 queue_map->queue_offset = queue_offset; 1739 blk_mq_map_queues(queue_map); 1740 break; 1741 default: 1742 queue_map->queue_offset = queue_offset; 1743 blk_mq_map_hw_queues(queue_map, 1744 &adapter->pdev->dev, 0); 1745 break; 1746 } 1747 queue_offset += queue_map->nr_queues; 1748 } 1749 } 1750 1751 int leapraid_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num) 1752 { 1753 struct leapraid_adapter *adapter = 1754 (struct leapraid_adapter *)shost->hostdata; 1755 struct leapraid_blk_mq_poll_rq *blk_mq_poll_rq; 1756 int num_entries; 1757 int qid = queue_num - adapter->notification_desc.iopoll_qdex; 1758 1759 blk_mq_poll_rq = &adapter->notification_desc.blk_mq_poll_rqs[qid]; 1760 if (atomic_read(&blk_mq_poll_rq->pause) || 1761 !atomic_add_unless(&blk_mq_poll_rq->busy, 1, 1)) 1762 return 0; 1763 1764 num_entries = leapraid_rep_queue_handler(&blk_mq_poll_rq->rq); 1765 atomic_dec(&blk_mq_poll_rq->busy); 1766 return num_entries; 1767 } 1768 1769 static int leapraid_bios_param(struct scsi_device *sdev, 1770 struct gendisk *disk, sector_t capacity, 1771 int geom[]) 1772 { 1773 int heads; 1774 int sectors; 1775 sector_t cylinders; 1776 1777 if (scsi_partsize(disk, capacity, geom)) 1778 return 0; 1779 1780 if ((ulong)capacity >= LEAPRAID_LARGE_DISK_THRESHOLD) { 1781 heads = LEAPRAID_LARGE_DISK_HEADS; 1782 sectors = LEAPRAID_LARGE_DISK_SECTORS; 1783 } else { 1784 heads = LEAPRAID_SMALL_DISK_HEADS; 1785 sectors = LEAPRAID_SMALL_DISK_SECTORS; 1786 } 1787 1788 cylinders = capacity; 1789 sector_div(cylinders, heads * sectors); 1790 1791 geom[0] = heads; 1792 geom[1] = sectors; 1793 geom[2] = cylinders; 1794 return 0; 1795 } 1796 1797 static ssize_t fw_queue_depth_show(struct device *cdev, 1798 struct device_attribute *attr, 1799 char *buf) 1800 { 1801 struct Scsi_Host *shost = class_to_shost(cdev); 1802 struct leapraid_adapter *adapter = shost_priv(shost); 1803 1804 return sysfs_emit(buf, "%02d\n", 1805 adapter->adapter_attr.features.req_slot); 1806 } 1807 1808 static ssize_t host_sas_address_show(struct device *cdev, 1809 struct device_attribute *attr, char *buf) 1810 { 1811 struct Scsi_Host *shost = class_to_shost(cdev); 1812 struct leapraid_adapter *adapter = shost_priv(shost); 1813 1814 return sysfs_emit(buf, "0x%016llx\n", 1815 (unsigned long long)adapter->dev_topo.card.sas_address); 1816 } 1817 1818 static ssize_t board_name_show(struct device *cdev, 1819 struct device_attribute *attr, char *buf) 1820 { 1821 struct Scsi_Host *shost = class_to_shost(cdev); 1822 struct leapraid_adapter *adapter = shost_priv(shost); 1823 1824 return sysfs_emit(buf, "%s\n", adapter->adapter_attr.board_name); 1825 } 1826 1827 static DEVICE_ATTR_RO(fw_queue_depth); 1828 static DEVICE_ATTR_RO(host_sas_address); 1829 static DEVICE_ATTR_RO(board_name); 1830 1831 static struct attribute *leapraid_shost_attrs[] = { 1832 &dev_attr_fw_queue_depth.attr, 1833 &dev_attr_host_sas_address.attr, 1834 &dev_attr_board_name.attr, 1835 NULL, 1836 }; 1837 1838 ATTRIBUTE_GROUPS(leapraid_shost); 1839 1840 static ssize_t sas_device_handle_show(struct device *dev, 1841 struct device_attribute *attr, char *buf) 1842 { 1843 struct scsi_device *sdev = to_scsi_device(dev); 1844 struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata; 1845 1846 if (!sas_device_priv_data || !sas_device_priv_data->starget_priv) { 1847 dev_err(&sdev->sdev_gendev, 1848 "%s: Invalid sdev_priv or starget_priv\n", __func__); 1849 return -EINVAL; 1850 } 1851 1852 return sysfs_emit(buf, "0x%04x\n", 1853 sas_device_priv_data->starget_priv->hdl); 1854 } 1855 1856 static ssize_t sas_ncq_prio_supported_show(struct device *dev, 1857 struct device_attribute *attr, 1858 char *buf) 1859 { 1860 struct scsi_device *sdev = to_scsi_device(dev); 1861 1862 return sysfs_emit(buf, "%d\n", sas_ata_ncq_prio_supported(sdev)); 1863 } 1864 1865 static ssize_t sas_ncq_prio_enable_show(struct device *dev, 1866 struct device_attribute *attr, 1867 char *buf) 1868 { 1869 struct scsi_device *sdev = to_scsi_device(dev); 1870 struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata; 1871 1872 if (!sas_device_priv_data) { 1873 dev_err(&sdev->sdev_gendev, 1874 "%s: Invalid sdev_priv\n", __func__); 1875 return -EINVAL; 1876 } 1877 1878 return sysfs_emit(buf, "%d\n", sas_device_priv_data->ncq_prio_enable); 1879 } 1880 1881 static ssize_t sas_ncq_prio_enable_store(struct device *dev, 1882 struct device_attribute *attr, 1883 const char *buf, size_t count) 1884 { 1885 struct scsi_device *sdev = to_scsi_device(dev); 1886 struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata; 1887 bool enable; 1888 1889 if (!sas_device_priv_data) { 1890 dev_err(&sdev->sdev_gendev, 1891 "%s: Invalid sdev_priv\n", __func__); 1892 return -EINVAL; 1893 } 1894 1895 if (kstrtobool(buf, &enable)) 1896 return -EINVAL; 1897 1898 if (!sas_ata_ncq_prio_supported(sdev)) 1899 return -EINVAL; 1900 1901 sas_device_priv_data->ncq_prio_enable = enable; 1902 return count; 1903 } 1904 1905 static DEVICE_ATTR_RO(sas_device_handle); 1906 static DEVICE_ATTR_RO(sas_ncq_prio_supported); 1907 static DEVICE_ATTR_RW(sas_ncq_prio_enable); 1908 1909 static bool leapraid_sdev_is_sata(struct scsi_device *sdev) 1910 { 1911 struct scsi_target *starget = sdev->sdev_target; 1912 struct leapraid_starget_priv *starget_priv = starget->hostdata; 1913 struct leapraid_sas_dev *sas_dev; 1914 1915 if (!starget_priv) 1916 return false; 1917 1918 sas_dev = starget_priv->sas_dev; 1919 return sas_dev && (sas_dev->dev_info & LEAPRAID_DEVTYP_SATA_DEV); 1920 } 1921 1922 static struct attribute *leapraid_sdev_attrs[] = { 1923 &dev_attr_sas_device_handle.attr, 1924 &dev_attr_sas_ncq_prio_supported.attr, 1925 &dev_attr_sas_ncq_prio_enable.attr, 1926 NULL, 1927 }; 1928 1929 static umode_t leapraid_sdev_attr_is_visible(struct kobject *kobj, 1930 struct attribute *attr, int i) 1931 { 1932 struct device *dev = kobj_to_dev(kobj); 1933 struct scsi_device *sdev = to_scsi_device(dev); 1934 1935 if (attr == &dev_attr_sas_ncq_prio_supported.attr || 1936 attr == &dev_attr_sas_ncq_prio_enable.attr) 1937 if (!leapraid_sdev_is_sata(sdev)) 1938 return 0; 1939 1940 return attr->mode; 1941 } 1942 1943 static const struct attribute_group leapraid_sdev_attr_group = { 1944 .attrs = leapraid_sdev_attrs, 1945 .is_visible = leapraid_sdev_attr_is_visible, 1946 }; 1947 1948 static const struct attribute_group *leapraid_sdev_groups[] = { 1949 &leapraid_sdev_attr_group, 1950 NULL, 1951 }; 1952 1953 static struct scsi_host_template leapraid_driver_template = { 1954 .module = THIS_MODULE, 1955 .name = "LEAPIO RAID Host", 1956 .proc_name = LEAPRAID_DRIVER_NAME, 1957 .queuecommand = leapraid_queuecommand, 1958 .cmd_size = sizeof(struct leapraid_io_req_tracker), 1959 .init_cmd_priv = leapraid_init_cmd_priv, 1960 .exit_cmd_priv = leapraid_exit_cmd_priv, 1961 .eh_abort_handler = leapraid_eh_abort_handler, 1962 .eh_device_reset_handler = leapraid_eh_device_reset_handler, 1963 .eh_target_reset_handler = leapraid_eh_target_reset_handler, 1964 .eh_host_reset_handler = leapraid_eh_host_reset_handler, 1965 .sdev_init = leapraid_sdev_init, 1966 .sdev_destroy = leapraid_sdev_destroy, 1967 .sdev_configure = leapraid_sdev_configure, 1968 .target_alloc = leapraid_target_alloc, 1969 .target_destroy = leapraid_target_destroy, 1970 .scan_finished = leapraid_scan_finished, 1971 .scan_start = leapraid_scan_start, 1972 .change_queue_depth = leapraid_change_queue_depth, 1973 .map_queues = leapraid_map_queues, 1974 .mq_poll = leapraid_blk_mq_poll, 1975 .bios_param = leapraid_bios_param, 1976 .can_queue = LEAPRAID_CAN_QUEUE_MIN, 1977 .this_id = LEAPRAID_THIS_ID_NONE, 1978 .sg_tablesize = LEAPRAID_SG_DEPTH, 1979 .max_sectors = LEAPRAID_MAX_SECTORS, 1980 .max_segment_size = LEAPRAID_MAX_SEGMENT_SIZE, 1981 .cmd_per_lun = LEAPRAID_CMD_PER_LUN, 1982 .shost_groups = leapraid_shost_groups, 1983 .sdev_groups = leapraid_sdev_groups, 1984 .track_queue_depth = 1, 1985 }; 1986 1987 static void leapraid_lock_init(struct leapraid_adapter *adapter) 1988 { 1989 mutex_init(&adapter->reset_desc.adapter_reset_mutex); 1990 mutex_init(&adapter->reset_desc.host_diag_mutex); 1991 mutex_init(&adapter->access_ctrl.pci_access_lock); 1992 1993 spin_lock_init(&adapter->reset_desc.adapter_reset_lock); 1994 spin_lock_init(&adapter->dynamic_task_desc.task_lock); 1995 spin_lock_init(&adapter->dev_topo.sas_dev_lock); 1996 spin_lock_init(&adapter->dev_topo.topo_node_lock); 1997 spin_lock_init(&adapter->fw_evt_s.fw_evt_lock); 1998 spin_lock_init(&adapter->dev_topo.raid_volume_lock); 1999 spin_lock_init(&adapter->dev_topo.enc_lock); 2000 spin_lock_init(&adapter->boot_devs.lock); 2001 } 2002 2003 static void leapraid_list_init(struct leapraid_adapter *adapter) 2004 { 2005 INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_list); 2006 INIT_LIST_HEAD(&adapter->dev_topo.card_port_list); 2007 INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_init_list); 2008 INIT_LIST_HEAD(&adapter->dev_topo.exp_list); 2009 INIT_LIST_HEAD(&adapter->dev_topo.enc_list); 2010 INIT_LIST_HEAD(&adapter->fw_evt_s.fw_evt_list); 2011 INIT_LIST_HEAD(&adapter->dev_topo.raid_volume_list); 2012 INIT_LIST_HEAD(&adapter->dev_topo.card.sas_port_list); 2013 } 2014 2015 static int leapraid_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2016 { 2017 struct leapraid_adapter *adapter; 2018 struct Scsi_Host *shost; 2019 int iopoll_q_count; 2020 int rc; 2021 2022 shost = scsi_host_alloc(&leapraid_driver_template, 2023 sizeof(struct leapraid_adapter)); 2024 if (!shost) { 2025 dev_err(&pdev->dev, 2026 "%s: SCSI host alloc failed\n", __func__); 2027 return -ENODEV; 2028 } 2029 2030 adapter = shost_priv(shost); 2031 memset(adapter, 0, sizeof(struct leapraid_adapter)); 2032 2033 init_waitqueue_head(&adapter->access_ctrl.recovery_waitq); 2034 adapter->adapter_attr.id = atomic_inc_return(&leapraid_ids) - 1; 2035 2036 adapter->adapter_attr.enable_mp = enable_mp; 2037 2038 adapter = shost_priv(shost); 2039 INIT_LIST_HEAD(&adapter->list); 2040 2041 adapter->shost = shost; 2042 adapter->pdev = pdev; 2043 adapter->fw_log_desc.open_pcie_trace = open_pcie_trace; 2044 atomic_set(&adapter->fw_log_desc.mmap_refcnt, 0); 2045 init_waitqueue_head(&adapter->fw_log_desc.mmap_waitq); 2046 leapraid_lock_init(adapter); 2047 leapraid_list_init(adapter); 2048 snprintf(adapter->adapter_attr.name, LEAPRAID_NAME_LENGTH, "%s%d", 2049 LEAPRAID_DRIVER_NAME, adapter->adapter_attr.id); 2050 2051 shost->max_cmd_len = LEAPRAID_MAX_CDB_LEN; 2052 shost->max_lun = LEAPRAID_MAX_LUNS; 2053 shost->transportt = leapraid_transport_template; 2054 shost->unique_id = adapter->adapter_attr.id; 2055 2056 adapter->fw_evt_s.fw_evt_thread = 2057 alloc_ordered_workqueue("fw_event_%s%d", 0, 2058 LEAPRAID_DRIVER_NAME, 2059 adapter->adapter_attr.id); 2060 if (!adapter->fw_evt_s.fw_evt_thread) { 2061 dev_err(&adapter->pdev->dev, 2062 "%s: Failed to create fw event workqueue\n", __func__); 2063 rc = -ENODEV; 2064 goto evt_wq_fail; 2065 } 2066 2067 shost->host_tagset = 1; 2068 init_waitqueue_head(&adapter->scan_dev_desc.wait_driver_loading); 2069 adapter->scan_dev_desc.driver_loading = 1; 2070 if (leapraid_ctrl_init(adapter)) { 2071 dev_err(&adapter->pdev->dev, 2072 "%s: Adapter init failed\n", __func__); 2073 rc = -ENODEV; 2074 goto ctrl_init_fail; 2075 } 2076 2077 shost->nr_hw_queues = 1; 2078 if (shost->host_tagset) { 2079 shost->nr_hw_queues = adapter->adapter_attr.rq_cnt; 2080 iopoll_q_count = adapter->adapter_attr.rq_cnt - 2081 adapter->notification_desc.iopoll_qdex; 2082 shost->nr_maps = iopoll_q_count ? 3 : 1; 2083 dev_info(&adapter->pdev->dev, 2084 "Max scsi I/O cmds %d shared with nr_hw_queues=%d\n", 2085 shost->can_queue, shost->nr_hw_queues); 2086 } 2087 2088 rc = scsi_add_host(shost, &pdev->dev); 2089 if (rc) { 2090 dev_err(&pdev->dev, 2091 "%s: SCSI host add failed\n", __func__); 2092 goto scsi_add_shost_fail; 2093 } 2094 2095 spin_lock(&leapraid_adapter_lock); 2096 list_add_tail(&adapter->list, &leapraid_adapter_list); 2097 spin_unlock(&leapraid_adapter_lock); 2098 2099 scsi_scan_host(shost); 2100 return 0; 2101 2102 scsi_add_shost_fail: 2103 leapraid_remove_ctrl(adapter); 2104 ctrl_init_fail: 2105 leapraid_overheat_cleanup(adapter); 2106 destroy_workqueue(adapter->fw_evt_s.fw_evt_thread); 2107 evt_wq_fail: 2108 scsi_host_put(shost); 2109 return rc; 2110 } 2111 2112 void leapraid_cleanup_lists(struct leapraid_adapter *adapter) 2113 { 2114 struct leapraid_raid_volume *raid_volume, *next_raid_volume; 2115 struct leapraid_starget_priv *starget_priv_data; 2116 struct leapraid_sas_port *leapraid_port, *next_port; 2117 struct leapraid_card_port *port, *port_next; 2118 struct leapraid_vphy *vphy, *vphy_next; 2119 2120 list_for_each_entry_safe(raid_volume, next_raid_volume, 2121 &adapter->dev_topo.raid_volume_list, list) { 2122 if (raid_volume->starget) { 2123 starget_priv_data = raid_volume->starget->hostdata; 2124 if (starget_priv_data) 2125 starget_priv_data->deleted = 1; 2126 scsi_remove_target(&raid_volume->starget->dev); 2127 } 2128 dev_info(&adapter->pdev->dev, 2129 "removing hdl=0x%04x, wwid=0x%016llx\n", 2130 raid_volume->hdl, 2131 (unsigned long long)raid_volume->wwid); 2132 leapraid_raid_volume_remove(adapter, raid_volume); 2133 } 2134 2135 list_for_each_entry_safe(leapraid_port, next_port, 2136 &adapter->dev_topo.card.sas_port_list, 2137 port_list) { 2138 if (leapraid_port->remote_identify.device_type == 2139 SAS_END_DEVICE) 2140 leapraid_sas_dev_remove_by_sas_address( 2141 adapter, 2142 leapraid_port->remote_identify.sas_address, 2143 leapraid_port->card_port); 2144 else if (leapraid_port->remote_identify.device_type == 2145 SAS_EDGE_EXPANDER_DEVICE || 2146 leapraid_port->remote_identify.device_type == 2147 SAS_FANOUT_EXPANDER_DEVICE) 2148 leapraid_exp_rm( 2149 adapter, 2150 leapraid_port->remote_identify.sas_address, 2151 leapraid_port->card_port); 2152 } 2153 2154 list_for_each_entry_safe(port, port_next, 2155 &adapter->dev_topo.card_port_list, list) { 2156 if (port->vphys_mask) 2157 list_for_each_entry_safe(vphy, vphy_next, 2158 &port->vphys_list, list) { 2159 list_del(&vphy->list); 2160 kfree(vphy); 2161 } 2162 list_del(&port->list); 2163 kfree(port); 2164 } 2165 2166 if (adapter->dev_topo.card.phys_num) { 2167 kfree(adapter->dev_topo.card.card_phy); 2168 adapter->dev_topo.card.card_phy = NULL; 2169 adapter->dev_topo.card.phys_num = 0; 2170 } 2171 } 2172 2173 static void leapraid_remove(struct pci_dev *pdev) 2174 { 2175 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2176 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2177 struct workqueue_struct *wq; 2178 unsigned long flags; 2179 2180 if (!shost || !adapter) { 2181 dev_err(&pdev->dev, "Unable to remove!\n"); 2182 return; 2183 } 2184 2185 wait_event(adapter->scan_dev_desc.wait_driver_loading, 2186 !adapter->scan_dev_desc.driver_loading); 2187 wait_event(adapter->scan_dev_desc.wait_driver_loading, 2188 !atomic_read(&adapter->overheat_desc.thermal_alert)); 2189 2190 WRITE_ONCE(adapter->access_ctrl.host_removing, 1); 2191 spin_lock(&leapraid_adapter_lock); 2192 list_del(&adapter->list); 2193 spin_unlock(&leapraid_adapter_lock); 2194 2195 leapraid_wait_cmds_done(adapter); 2196 2197 if (leapraid_pci_removed(adapter)) { 2198 leapraid_mq_polling_pause(adapter); 2199 leapraid_clean_active_scsi_cmds(adapter); 2200 } 2201 leapraid_clean_active_fw_evt(adapter); 2202 2203 spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags); 2204 wq = adapter->fw_evt_s.fw_evt_thread; 2205 adapter->fw_evt_s.fw_evt_thread = NULL; 2206 spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags); 2207 if (wq) 2208 destroy_workqueue(wq); 2209 2210 sas_remove_host(shost); 2211 leapraid_cleanup_lists(adapter); 2212 leapraid_remove_ctrl(adapter); 2213 scsi_host_put(shost); 2214 } 2215 2216 static void leapraid_shutdown(struct pci_dev *pdev) 2217 { 2218 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2219 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2220 struct workqueue_struct *wq; 2221 unsigned long flags; 2222 2223 if (!shost || !adapter) { 2224 dev_err(&pdev->dev, "Unable to shutdown!\n"); 2225 return; 2226 } 2227 2228 adapter->access_ctrl.host_removing = 1; 2229 leapraid_wait_cmds_done(adapter); 2230 leapraid_clean_active_fw_evt(adapter); 2231 leapraid_overheat_cleanup(adapter); 2232 leapraid_fw_log_stop(adapter); 2233 spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags); 2234 wq = adapter->fw_evt_s.fw_evt_thread; 2235 adapter->fw_evt_s.fw_evt_thread = NULL; 2236 spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags); 2237 if (wq) 2238 destroy_workqueue(wq); 2239 2240 leapraid_disable_controller(adapter); 2241 } 2242 2243 static pci_ers_result_t leapraid_pci_error_detected(struct pci_dev *pdev, 2244 pci_channel_state_t state) 2245 { 2246 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2247 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2248 2249 if (!shost || !adapter) { 2250 dev_err(&pdev->dev, "Failed to error detected for device\n"); 2251 return PCI_ERS_RESULT_DISCONNECT; 2252 } 2253 2254 dev_err(&pdev->dev, "%s: PCI error detected, state=%d\n", 2255 adapter->adapter_attr.name, state); 2256 2257 switch (state) { 2258 case pci_channel_io_normal: 2259 return PCI_ERS_RESULT_CAN_RECOVER; 2260 case pci_channel_io_frozen: 2261 adapter->access_ctrl.pcie_recovering = 1; 2262 scsi_block_requests(adapter->shost); 2263 leapraid_overheat_cleanup(adapter); 2264 leapraid_check_scheduled_fault_stop(adapter); 2265 leapraid_fw_log_stop(adapter); 2266 leapraid_disable_controller(adapter); 2267 return PCI_ERS_RESULT_NEED_RESET; 2268 case pci_channel_io_perm_failure: 2269 adapter->access_ctrl.pcie_recovering = 1; 2270 leapraid_overheat_cleanup(adapter); 2271 leapraid_check_scheduled_fault_stop(adapter); 2272 leapraid_fw_log_stop(adapter); 2273 leapraid_mq_polling_pause(adapter); 2274 leapraid_clean_active_scsi_cmds(adapter); 2275 return PCI_ERS_RESULT_DISCONNECT; 2276 } 2277 2278 return PCI_ERS_RESULT_NEED_RESET; 2279 } 2280 2281 static pci_ers_result_t leapraid_pci_mmio_enabled(struct pci_dev *pdev) 2282 { 2283 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2284 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2285 2286 if (!shost || !adapter) { 2287 dev_err(&pdev->dev, 2288 "Failed to enable mmio for device\n"); 2289 return PCI_ERS_RESULT_DISCONNECT; 2290 } 2291 2292 dev_info(&pdev->dev, "%s: PCI error mmio enabled\n", 2293 adapter->adapter_attr.name); 2294 2295 return PCI_ERS_RESULT_RECOVERED; 2296 } 2297 2298 static pci_ers_result_t leapraid_pci_slot_reset(struct pci_dev *pdev) 2299 { 2300 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2301 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2302 int rc; 2303 2304 if (!shost || !adapter) { 2305 dev_err(&pdev->dev, 2306 "Failed to slot reset for device\n"); 2307 return PCI_ERS_RESULT_DISCONNECT; 2308 } 2309 2310 dev_err(&pdev->dev, "%s PCI error slot reset\n", 2311 adapter->adapter_attr.name); 2312 2313 adapter->pdev = pdev; 2314 pci_restore_state(pdev); 2315 if (leapraid_set_pcie_and_notification(adapter)) { 2316 dev_err(&pdev->dev, 2317 "%s: Failed to set PCIe state and notification\n", 2318 __func__); 2319 return PCI_ERS_RESULT_DISCONNECT; 2320 } 2321 2322 adapter->access_ctrl.pcie_recovering = 0; 2323 dev_info(&pdev->dev, "%s: Hard reset triggered by PCI slot reset\n", 2324 adapter->adapter_attr.name); 2325 dev_info(&adapter->pdev->dev, "%s: %d: call hard_reset\n", 2326 __func__, __LINE__); 2327 rc = leapraid_hard_reset_handler(adapter, FULL_RESET); 2328 if (rc) 2329 dev_err(&pdev->dev, "%s hard reset: failed\n", 2330 adapter->adapter_attr.name); 2331 2332 return rc == 0 ? PCI_ERS_RESULT_RECOVERED : 2333 PCI_ERS_RESULT_DISCONNECT; 2334 } 2335 2336 static void leapraid_pci_resume(struct pci_dev *pdev) 2337 { 2338 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2339 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2340 2341 if (!shost || !adapter) { 2342 dev_err(&pdev->dev, "Failed to resume\n"); 2343 return; 2344 } 2345 2346 dev_err(&pdev->dev, "PCI error resume!\n"); 2347 2348 pci_aer_clear_nonfatal_status(pdev); 2349 leapraid_check_scheduled_fault_start(adapter); 2350 leapraid_fw_log_start(adapter); 2351 scsi_unblock_requests(adapter->shost); 2352 } 2353 2354 MODULE_DEVICE_TABLE(pci, leapraid_pci_table); 2355 static struct pci_error_handlers leapraid_err_handler = { 2356 .error_detected = leapraid_pci_error_detected, 2357 .mmio_enabled = leapraid_pci_mmio_enabled, 2358 .slot_reset = leapraid_pci_slot_reset, 2359 .resume = leapraid_pci_resume, 2360 }; 2361 2362 #ifdef CONFIG_PM 2363 static int leapraid_suspend(struct pci_dev *pdev, pm_message_t state) 2364 { 2365 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2366 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2367 pci_power_t device_state; 2368 2369 if (!shost || !adapter) { 2370 dev_err(&pdev->dev, 2371 "Suspend failed, invalid host or adapter\n"); 2372 return -ENXIO; 2373 } 2374 2375 leapraid_overheat_cleanup(adapter); 2376 leapraid_check_scheduled_fault_stop(adapter); 2377 leapraid_fw_log_stop(adapter); 2378 scsi_block_requests(shost); 2379 device_state = pci_choose_state(pdev, state); 2380 2381 dev_info(&pdev->dev, "Entering PCI power state D%d, (slot=%s)\n", 2382 device_state, pci_name(pdev)); 2383 2384 pci_save_state(pdev); 2385 leapraid_disable_controller(adapter); 2386 pci_set_power_state(pdev, device_state); 2387 return 0; 2388 } 2389 2390 static int leapraid_resume(struct pci_dev *pdev) 2391 { 2392 struct leapraid_adapter *adapter = pdev_to_adapter(pdev); 2393 struct Scsi_Host *shost = pci_get_drvdata(pdev); 2394 pci_power_t device_state = pdev->current_state; 2395 int rc; 2396 2397 if (!shost || !adapter) { 2398 dev_err(&pdev->dev, 2399 "Resume failed, invalid host or adapter\n"); 2400 return -ENXIO; 2401 } 2402 2403 dev_info(&pdev->dev, 2404 "Resuming device %s, previous state D%d\n", 2405 pci_name(pdev), device_state); 2406 2407 pci_set_power_state(pdev, PCI_D0); 2408 pci_enable_wake(pdev, PCI_D0, 0); 2409 pci_restore_state(pdev); 2410 adapter->pdev = pdev; 2411 rc = leapraid_set_pcie_and_notification(adapter); 2412 if (rc) { 2413 dev_err(&pdev->dev, 2414 "%s: Failed to set PCIe state and notification\n", 2415 __func__); 2416 return rc; 2417 } 2418 2419 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n", 2420 __func__, __LINE__); 2421 rc = leapraid_hard_reset_handler(adapter, PART_RESET); 2422 if (rc) { 2423 dev_err(&adapter->pdev->dev, 2424 "%s: Hard reset failed during resume, rc=%d\n", 2425 __func__, rc); 2426 return rc; 2427 } 2428 scsi_unblock_requests(shost); 2429 leapraid_check_scheduled_fault_start(adapter); 2430 leapraid_fw_log_start(adapter); 2431 return 0; 2432 } 2433 #endif /* CONFIG_PM */ 2434 2435 static struct pci_driver leapraid_driver = { 2436 .name = LEAPRAID_DRIVER_NAME, 2437 .id_table = leapraid_pci_table, 2438 .probe = leapraid_probe, 2439 .remove = leapraid_remove, 2440 .shutdown = leapraid_shutdown, 2441 .err_handler = &leapraid_err_handler, 2442 #ifdef CONFIG_PM 2443 .suspend = leapraid_suspend, 2444 .resume = leapraid_resume, 2445 #endif /* CONFIG_PM */ 2446 }; 2447 2448 static int __init leapraid_init(void) 2449 { 2450 int error; 2451 2452 pr_info("%s initializing\n", LEAPRAID_DRIVER_NAME); 2453 2454 leapraid_transport_template = 2455 sas_attach_transport(&leapraid_transport_functions); 2456 if (!leapraid_transport_template) { 2457 pr_err("%s: Failed to attach SAS transport\n", 2458 LEAPRAID_DRIVER_NAME); 2459 return -ENODEV; 2460 } 2461 2462 error = pci_register_driver(&leapraid_driver); 2463 if (error) { 2464 pr_err("%s: PCI driver registration failed: %d\n", 2465 LEAPRAID_DRIVER_NAME, error); 2466 sas_release_transport(leapraid_transport_template); 2467 return error; 2468 } 2469 2470 error = leapraid_ctl_init(); 2471 if (error) { 2472 pci_unregister_driver(&leapraid_driver); 2473 sas_release_transport(leapraid_transport_template); 2474 return error; 2475 } 2476 2477 return 0; 2478 } 2479 2480 static void __exit leapraid_exit(void) 2481 { 2482 pr_info("%s exiting\n", LEAPRAID_DRIVER_NAME); 2483 2484 leapraid_ctl_exit(); 2485 pci_unregister_driver(&leapraid_driver); 2486 sas_release_transport(leapraid_transport_template); 2487 } 2488 2489 module_init(leapraid_init); 2490 module_exit(leapraid_exit); 2491