1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * zfcp device driver 4 * 5 * Interface to Linux SCSI midlayer. 6 * 7 * Copyright IBM Corp. 2002, 2020 8 */ 9 10 #define KMSG_COMPONENT "zfcp" 11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 12 13 #include <linux/module.h> 14 #include <linux/types.h> 15 #include <linux/slab.h> 16 #include <scsi/fc/fc_fcp.h> 17 #include <scsi/scsi_eh.h> 18 #include <linux/atomic.h> 19 #include "zfcp_ext.h" 20 #include "zfcp_dbf.h" 21 #include "zfcp_fc.h" 22 #include "zfcp_reqlist.h" 23 24 static unsigned int default_depth = 32; 25 module_param_named(queue_depth, default_depth, uint, 0600); 26 MODULE_PARM_DESC(queue_depth, "Default queue depth for new SCSI devices"); 27 28 static bool enable_dif; 29 module_param_named(dif, enable_dif, bool, 0400); 30 MODULE_PARM_DESC(dif, "Enable DIF data integrity support (default off)"); 31 32 bool zfcp_experimental_dix; 33 module_param_named(dix, zfcp_experimental_dix, bool, 0400); 34 MODULE_PARM_DESC(dix, "Enable experimental DIX (data integrity extension) support which implies DIF support (default off)"); 35 36 static bool allow_lun_scan = true; 37 module_param(allow_lun_scan, bool, 0600); 38 MODULE_PARM_DESC(allow_lun_scan, "For NPIV, scan and attach all storage LUNs"); 39 40 static void zfcp_scsi_sdev_destroy(struct scsi_device *sdev) 41 { 42 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev); 43 44 /* if previous sdev_init returned early, there is nothing to do */ 45 if (!zfcp_sdev->port) 46 return; 47 48 zfcp_erp_lun_shutdown_wait(sdev, "scssd_1"); 49 put_device(&zfcp_sdev->port->dev); 50 } 51 52 static int zfcp_scsi_sdev_configure(struct scsi_device *sdp, 53 struct queue_limits *lim) 54 { 55 if (sdp->tagged_supported) 56 scsi_change_queue_depth(sdp, default_depth); 57 return 0; 58 } 59 60 static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result) 61 { 62 set_host_byte(scpnt, result); 63 zfcp_dbf_scsi_fail_send(scpnt); 64 scsi_done(scpnt); 65 } 66 67 static 68 int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt) 69 { 70 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device); 71 struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device)); 72 int status, scsi_result, ret; 73 74 /* reset the status for this request */ 75 scpnt->result = 0; 76 scpnt->host_scribble = NULL; 77 78 scsi_result = fc_remote_port_chkready(rport); 79 if (unlikely(scsi_result)) { 80 scpnt->result = scsi_result; 81 zfcp_dbf_scsi_fail_send(scpnt); 82 scsi_done(scpnt); 83 return 0; 84 } 85 86 status = atomic_read(&zfcp_sdev->status); 87 if (unlikely(status & ZFCP_STATUS_COMMON_ERP_FAILED) && 88 !(atomic_read(&zfcp_sdev->port->status) & 89 ZFCP_STATUS_COMMON_ERP_FAILED)) { 90 /* only LUN access denied, but port is good 91 * not covered by FC transport, have to fail here */ 92 zfcp_scsi_command_fail(scpnt, DID_ERROR); 93 return 0; 94 } 95 96 if (unlikely(!(status & ZFCP_STATUS_COMMON_UNBLOCKED))) { 97 /* This could be 98 * call to rport_delete pending: mimic retry from 99 * fc_remote_port_chkready until rport is BLOCKED 100 */ 101 zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY); 102 return 0; 103 } 104 105 ret = zfcp_fsf_fcp_cmnd(scpnt); 106 if (unlikely(ret == -EBUSY)) 107 return SCSI_MLQUEUE_DEVICE_BUSY; 108 else if (unlikely(ret < 0)) 109 return SCSI_MLQUEUE_HOST_BUSY; 110 111 return ret; 112 } 113 114 static int zfcp_scsi_sdev_init(struct scsi_device *sdev) 115 { 116 struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); 117 struct zfcp_adapter *adapter = 118 (struct zfcp_adapter *) sdev->host->hostdata[0]; 119 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev); 120 struct zfcp_port *port; 121 struct zfcp_unit *unit; 122 int npiv = adapter->connection_features & FSF_FEATURE_NPIV_MODE; 123 124 zfcp_sdev->erp_action.adapter = adapter; 125 zfcp_sdev->erp_action.sdev = sdev; 126 127 port = zfcp_get_port_by_wwpn(adapter, rport->port_name); 128 if (!port) 129 return -ENXIO; 130 131 zfcp_sdev->erp_action.port = port; 132 133 mutex_lock(&zfcp_sysfs_port_units_mutex); 134 if (zfcp_sysfs_port_is_removing(port)) { 135 /* port is already gone */ 136 mutex_unlock(&zfcp_sysfs_port_units_mutex); 137 put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */ 138 return -ENXIO; 139 } 140 mutex_unlock(&zfcp_sysfs_port_units_mutex); 141 142 unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev)); 143 if (unit) 144 put_device(&unit->dev); 145 146 if (!unit && !(allow_lun_scan && npiv)) { 147 put_device(&port->dev); 148 return -ENXIO; 149 } 150 151 zfcp_sdev->port = port; 152 zfcp_sdev->latencies.write.channel.min = 0xFFFFFFFF; 153 zfcp_sdev->latencies.write.fabric.min = 0xFFFFFFFF; 154 zfcp_sdev->latencies.read.channel.min = 0xFFFFFFFF; 155 zfcp_sdev->latencies.read.fabric.min = 0xFFFFFFFF; 156 zfcp_sdev->latencies.cmd.channel.min = 0xFFFFFFFF; 157 zfcp_sdev->latencies.cmd.fabric.min = 0xFFFFFFFF; 158 spin_lock_init(&zfcp_sdev->latencies.lock); 159 160 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING); 161 zfcp_erp_lun_reopen(sdev, 0, "scsla_1"); 162 zfcp_erp_wait(port->adapter); 163 164 return 0; 165 } 166 167 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt) 168 { 169 struct Scsi_Host *scsi_host = scpnt->device->host; 170 struct zfcp_adapter *adapter = 171 (struct zfcp_adapter *) scsi_host->hostdata[0]; 172 struct zfcp_fsf_req *old_req, *abrt_req; 173 unsigned long flags; 174 u64 old_reqid = (u64) scpnt->host_scribble; 175 int retval = SUCCESS, ret; 176 int retry = 3; 177 char *dbf_tag; 178 179 /* avoid race condition between late normal completion and abort */ 180 write_lock_irqsave(&adapter->abort_lock, flags); 181 182 old_req = zfcp_reqlist_find(adapter->req_list, old_reqid); 183 if (!old_req) { 184 write_unlock_irqrestore(&adapter->abort_lock, flags); 185 zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL); 186 return FAILED; /* completion could be in progress */ 187 } 188 old_req->data = NULL; 189 190 /* don't access old fsf_req after releasing the abort_lock */ 191 write_unlock_irqrestore(&adapter->abort_lock, flags); 192 193 while (retry--) { 194 abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt); 195 if (abrt_req) 196 break; 197 198 zfcp_dbf_scsi_abort("abrt_wt", scpnt, NULL); 199 zfcp_erp_wait(adapter); 200 ret = fc_block_scsi_eh(scpnt); 201 if (ret) { 202 zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL); 203 return ret; 204 } 205 if (!(atomic_read(&adapter->status) & 206 ZFCP_STATUS_COMMON_RUNNING)) { 207 zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL); 208 return SUCCESS; 209 } 210 } 211 if (!abrt_req) { 212 zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL); 213 return FAILED; 214 } 215 216 wait_for_completion(&abrt_req->completion); 217 218 if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) 219 dbf_tag = "abrt_ok"; 220 else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) 221 dbf_tag = "abrt_nn"; 222 else { 223 dbf_tag = "abrt_fa"; 224 retval = FAILED; 225 } 226 zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req); 227 zfcp_fsf_req_free(abrt_req); 228 return retval; 229 } 230 231 struct zfcp_scsi_req_filter { 232 u8 tmf_scope; 233 u32 lun_handle; 234 u32 port_handle; 235 }; 236 237 static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data) 238 { 239 struct zfcp_scsi_req_filter *filter = 240 (struct zfcp_scsi_req_filter *)data; 241 242 /* already aborted - prevent side-effects - or not a SCSI command */ 243 if (old_req->data == NULL || 244 zfcp_fsf_req_is_status_read_buffer(old_req) || 245 old_req->qtcb->header.fsf_command != FSF_QTCB_FCP_CMND) 246 return; 247 248 /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */ 249 if (old_req->qtcb->header.port_handle != filter->port_handle) 250 return; 251 252 if (filter->tmf_scope == FCP_TMF_LUN_RESET && 253 old_req->qtcb->header.lun_handle != filter->lun_handle) 254 return; 255 256 zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req); 257 old_req->data = NULL; 258 } 259 260 static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags) 261 { 262 struct zfcp_adapter *adapter = zsdev->port->adapter; 263 struct zfcp_scsi_req_filter filter = { 264 .tmf_scope = FCP_TMF_TGT_RESET, 265 .port_handle = zsdev->port->handle, 266 }; 267 unsigned long flags; 268 269 if (tm_flags == FCP_TMF_LUN_RESET) { 270 filter.tmf_scope = FCP_TMF_LUN_RESET; 271 filter.lun_handle = zsdev->lun_handle; 272 } 273 274 /* 275 * abort_lock secures against other processings - in the abort-function 276 * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data 277 */ 278 write_lock_irqsave(&adapter->abort_lock, flags); 279 zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd, 280 &filter); 281 write_unlock_irqrestore(&adapter->abort_lock, flags); 282 } 283 284 /** 285 * zfcp_scsi_task_mgmt_function() - Send a task management function (sync). 286 * @sdev: Pointer to SCSI device to send the task management command to. 287 * @tm_flags: Task management flags, 288 * here we only handle %FCP_TMF_TGT_RESET or %FCP_TMF_LUN_RESET. 289 */ 290 static int zfcp_scsi_task_mgmt_function(struct scsi_device *sdev, u8 tm_flags) 291 { 292 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev); 293 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter; 294 struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); 295 struct zfcp_fsf_req *fsf_req = NULL; 296 int retval = SUCCESS, ret; 297 int retry = 3; 298 299 while (retry--) { 300 fsf_req = zfcp_fsf_fcp_task_mgmt(sdev, tm_flags); 301 if (fsf_req) 302 break; 303 304 zfcp_dbf_scsi_devreset("wait", sdev, tm_flags, NULL); 305 zfcp_erp_wait(adapter); 306 ret = fc_block_rport(rport); 307 if (ret) { 308 zfcp_dbf_scsi_devreset("fiof", sdev, tm_flags, NULL); 309 return ret; 310 } 311 312 if (!(atomic_read(&adapter->status) & 313 ZFCP_STATUS_COMMON_RUNNING)) { 314 zfcp_dbf_scsi_devreset("nres", sdev, tm_flags, NULL); 315 return SUCCESS; 316 } 317 } 318 if (!fsf_req) { 319 zfcp_dbf_scsi_devreset("reqf", sdev, tm_flags, NULL); 320 return FAILED; 321 } 322 323 wait_for_completion(&fsf_req->completion); 324 325 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) { 326 zfcp_dbf_scsi_devreset("fail", sdev, tm_flags, fsf_req); 327 retval = FAILED; 328 } else { 329 zfcp_dbf_scsi_devreset("okay", sdev, tm_flags, fsf_req); 330 zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags); 331 } 332 333 zfcp_fsf_req_free(fsf_req); 334 return retval; 335 } 336 337 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt) 338 { 339 struct scsi_device *sdev = scpnt->device; 340 341 return zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_LUN_RESET); 342 } 343 344 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt) 345 { 346 struct scsi_target *starget = scsi_target(scpnt->device); 347 struct fc_rport *rport = starget_to_rport(starget); 348 struct Scsi_Host *shost = rport_to_shost(rport); 349 struct scsi_device *sdev = NULL, *tmp_sdev; 350 struct zfcp_adapter *adapter = 351 (struct zfcp_adapter *)shost->hostdata[0]; 352 int ret; 353 354 shost_for_each_device(tmp_sdev, shost) { 355 if (tmp_sdev->id == starget->id) { 356 sdev = tmp_sdev; 357 break; 358 } 359 } 360 if (!sdev) { 361 ret = FAILED; 362 zfcp_dbf_scsi_eh("tr_nosd", adapter, starget->id, ret); 363 return ret; 364 } 365 366 ret = zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_TGT_RESET); 367 368 /* release reference from above shost_for_each_device */ 369 if (sdev) 370 scsi_device_put(tmp_sdev); 371 372 return ret; 373 } 374 375 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt) 376 { 377 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device); 378 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter; 379 int ret = SUCCESS, fc_ret; 380 381 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE)) { 382 zfcp_erp_port_forced_reopen_all(adapter, 0, "schrh_p"); 383 zfcp_erp_wait(adapter); 384 } 385 zfcp_erp_adapter_reopen(adapter, 0, "schrh_1"); 386 zfcp_erp_wait(adapter); 387 fc_ret = fc_block_scsi_eh(scpnt); 388 if (fc_ret) 389 ret = fc_ret; 390 391 zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret); 392 return ret; 393 } 394 395 /** 396 * zfcp_scsi_sysfs_host_reset() - Support scsi_host sysfs attribute host_reset. 397 * @shost: Pointer to Scsi_Host to perform action on. 398 * @reset_type: We support %SCSI_ADAPTER_RESET but not %SCSI_FIRMWARE_RESET. 399 * 400 * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise. 401 * 402 * This is similar to zfcp_sysfs_adapter_failed_store(). 403 */ 404 static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host *shost, int reset_type) 405 { 406 struct zfcp_adapter *adapter = 407 (struct zfcp_adapter *)shost->hostdata[0]; 408 int ret = 0; 409 410 if (reset_type != SCSI_ADAPTER_RESET) { 411 ret = -EOPNOTSUPP; 412 zfcp_dbf_scsi_eh("scshr_n", adapter, ~0, ret); 413 return ret; 414 } 415 416 zfcp_erp_adapter_reset_sync(adapter, "scshr_y"); 417 return ret; 418 } 419 420 struct scsi_transport_template *zfcp_scsi_transport_template; 421 422 static const struct scsi_host_template zfcp_scsi_host_template = { 423 .module = THIS_MODULE, 424 .name = "zfcp", 425 .queuecommand = zfcp_scsi_queuecommand, 426 .eh_timed_out = fc_eh_timed_out, 427 .eh_abort_handler = zfcp_scsi_eh_abort_handler, 428 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler, 429 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler, 430 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler, 431 .sdev_init = zfcp_scsi_sdev_init, 432 .sdev_configure = zfcp_scsi_sdev_configure, 433 .sdev_destroy = zfcp_scsi_sdev_destroy, 434 .change_queue_depth = scsi_change_queue_depth, 435 .host_reset = zfcp_scsi_sysfs_host_reset, 436 .proc_name = "zfcp", 437 .can_queue = 4096, 438 .this_id = -1, 439 .sg_tablesize = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1) 440 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2), 441 /* GCD, adjusted later */ 442 .max_sectors = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1) 443 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8, 444 /* GCD, adjusted later */ 445 /* report size limit per scatter-gather segment */ 446 .max_segment_size = ZFCP_QDIO_SBALE_LEN, 447 .dma_boundary = ZFCP_QDIO_SBALE_LEN - 1, 448 .shost_groups = zfcp_sysfs_shost_attr_groups, 449 .sdev_groups = zfcp_sysfs_sdev_attr_groups, 450 .track_queue_depth = 1, 451 .supported_mode = MODE_INITIATOR, 452 }; 453 454 /** 455 * zfcp_scsi_adapter_register() - Allocate and register SCSI and FC host with 456 * SCSI midlayer 457 * @adapter: The zfcp adapter to register with the SCSI midlayer 458 * 459 * Allocates the SCSI host object for the given adapter, sets basic properties 460 * (such as the transport template, QDIO limits, ...), and registers it with 461 * the midlayer. 462 * 463 * During registration with the midlayer the corresponding FC host object for 464 * the referenced transport class is also implicitely allocated. 465 * 466 * Upon success adapter->scsi_host is set, and upon failure it remains NULL. If 467 * adapter->scsi_host is already set, nothing is done. 468 * 469 * Return: 470 * * 0 - Allocation and registration was successful 471 * * -EEXIST - SCSI and FC host did already exist, nothing was done, nothing 472 * was changed 473 * * -EIO - Allocation or registration failed 474 */ 475 int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter) 476 { 477 struct ccw_dev_id dev_id; 478 479 if (adapter->scsi_host) 480 return -EEXIST; 481 482 ccw_device_get_id(adapter->ccw_device, &dev_id); 483 /* register adapter as SCSI host with mid layer of SCSI stack */ 484 adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template, 485 sizeof (struct zfcp_adapter *)); 486 if (!adapter->scsi_host) 487 goto err_out; 488 489 /* tell the SCSI stack some characteristics of this adapter */ 490 adapter->scsi_host->max_id = 511; 491 adapter->scsi_host->max_lun = 0xFFFFFFFF; 492 adapter->scsi_host->max_channel = 0; 493 adapter->scsi_host->unique_id = dev_id.devno; 494 adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */ 495 adapter->scsi_host->transportt = zfcp_scsi_transport_template; 496 497 /* make all basic properties known at registration time */ 498 zfcp_qdio_shost_update(adapter, adapter->qdio); 499 zfcp_scsi_set_prot(adapter); 500 501 adapter->scsi_host->hostdata[0] = (unsigned long) adapter; 502 503 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) { 504 scsi_host_put(adapter->scsi_host); 505 goto err_out; 506 } 507 508 return 0; 509 err_out: 510 adapter->scsi_host = NULL; 511 dev_err(&adapter->ccw_device->dev, 512 "Registering the FCP device with the SCSI stack failed\n"); 513 return -EIO; 514 } 515 516 /** 517 * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer 518 * @adapter: The zfcp adapter to unregister. 519 */ 520 void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter) 521 { 522 struct Scsi_Host *shost; 523 struct zfcp_port *port; 524 525 shost = adapter->scsi_host; 526 if (!shost) 527 return; 528 529 read_lock_irq(&adapter->port_list_lock); 530 list_for_each_entry(port, &adapter->port_list, list) 531 port->rport = NULL; 532 read_unlock_irq(&adapter->port_list_lock); 533 534 fc_remove_host(shost); 535 scsi_remove_host(shost); 536 scsi_host_put(shost); 537 adapter->scsi_host = NULL; 538 } 539 540 static struct fc_host_statistics* 541 zfcp_scsi_init_fc_host_stats(struct zfcp_adapter *adapter) 542 { 543 struct fc_host_statistics *fc_stats; 544 545 if (!adapter->fc_stats) { 546 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL); 547 if (!fc_stats) 548 return NULL; 549 adapter->fc_stats = fc_stats; /* freed in adapter_release */ 550 } 551 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats)); 552 return adapter->fc_stats; 553 } 554 555 static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics *fc_stats, 556 struct fsf_qtcb_bottom_port *data, 557 struct fsf_qtcb_bottom_port *old) 558 { 559 fc_stats->seconds_since_last_reset = 560 data->seconds_since_last_reset - old->seconds_since_last_reset; 561 fc_stats->tx_frames = data->tx_frames - old->tx_frames; 562 fc_stats->tx_words = data->tx_words - old->tx_words; 563 fc_stats->rx_frames = data->rx_frames - old->rx_frames; 564 fc_stats->rx_words = data->rx_words - old->rx_words; 565 fc_stats->lip_count = data->lip - old->lip; 566 fc_stats->nos_count = data->nos - old->nos; 567 fc_stats->error_frames = data->error_frames - old->error_frames; 568 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames; 569 fc_stats->link_failure_count = data->link_failure - old->link_failure; 570 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync; 571 fc_stats->loss_of_signal_count = 572 data->loss_of_signal - old->loss_of_signal; 573 fc_stats->prim_seq_protocol_err_count = 574 data->psp_error_counts - old->psp_error_counts; 575 fc_stats->invalid_tx_word_count = 576 data->invalid_tx_words - old->invalid_tx_words; 577 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs; 578 fc_stats->fcp_input_requests = 579 data->input_requests - old->input_requests; 580 fc_stats->fcp_output_requests = 581 data->output_requests - old->output_requests; 582 fc_stats->fcp_control_requests = 583 data->control_requests - old->control_requests; 584 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb; 585 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb; 586 } 587 588 static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics *fc_stats, 589 struct fsf_qtcb_bottom_port *data) 590 { 591 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset; 592 fc_stats->tx_frames = data->tx_frames; 593 fc_stats->tx_words = data->tx_words; 594 fc_stats->rx_frames = data->rx_frames; 595 fc_stats->rx_words = data->rx_words; 596 fc_stats->lip_count = data->lip; 597 fc_stats->nos_count = data->nos; 598 fc_stats->error_frames = data->error_frames; 599 fc_stats->dumped_frames = data->dumped_frames; 600 fc_stats->link_failure_count = data->link_failure; 601 fc_stats->loss_of_sync_count = data->loss_of_sync; 602 fc_stats->loss_of_signal_count = data->loss_of_signal; 603 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts; 604 fc_stats->invalid_tx_word_count = data->invalid_tx_words; 605 fc_stats->invalid_crc_count = data->invalid_crcs; 606 fc_stats->fcp_input_requests = data->input_requests; 607 fc_stats->fcp_output_requests = data->output_requests; 608 fc_stats->fcp_control_requests = data->control_requests; 609 fc_stats->fcp_input_megabytes = data->input_mb; 610 fc_stats->fcp_output_megabytes = data->output_mb; 611 } 612 613 static struct fc_host_statistics * 614 zfcp_scsi_get_fc_host_stats(struct Scsi_Host *host) 615 { 616 struct zfcp_adapter *adapter; 617 struct fc_host_statistics *fc_stats; 618 struct fsf_qtcb_bottom_port *data; 619 int ret; 620 621 adapter = (struct zfcp_adapter *)host->hostdata[0]; 622 fc_stats = zfcp_scsi_init_fc_host_stats(adapter); 623 if (!fc_stats) 624 return NULL; 625 626 data = kzalloc(sizeof(*data), GFP_KERNEL); 627 if (!data) 628 return NULL; 629 630 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data); 631 if (ret != 0 && ret != -EAGAIN) { 632 kfree(data); 633 return NULL; 634 } 635 636 if (adapter->stats_reset && 637 ((jiffies/HZ - adapter->stats_reset) < 638 data->seconds_since_last_reset)) 639 zfcp_scsi_adjust_fc_host_stats(fc_stats, data, 640 adapter->stats_reset_data); 641 else 642 zfcp_scsi_set_fc_host_stats(fc_stats, data); 643 644 kfree(data); 645 return fc_stats; 646 } 647 648 static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host *shost) 649 { 650 struct zfcp_adapter *adapter; 651 struct fsf_qtcb_bottom_port *data; 652 int ret; 653 654 adapter = (struct zfcp_adapter *)shost->hostdata[0]; 655 data = kzalloc(sizeof(*data), GFP_KERNEL); 656 if (!data) 657 return; 658 659 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data); 660 if (ret != 0 && ret != -EAGAIN) 661 kfree(data); 662 else { 663 adapter->stats_reset = jiffies/HZ; 664 kfree(adapter->stats_reset_data); 665 adapter->stats_reset_data = data; /* finally freed in 666 adapter_release */ 667 } 668 } 669 670 static void zfcp_scsi_get_host_port_state(struct Scsi_Host *shost) 671 { 672 struct zfcp_adapter *adapter = 673 (struct zfcp_adapter *)shost->hostdata[0]; 674 int status = atomic_read(&adapter->status); 675 676 if ((status & ZFCP_STATUS_COMMON_RUNNING) && 677 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)) 678 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE; 679 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED) 680 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN; 681 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED) 682 fc_host_port_state(shost) = FC_PORTSTATE_ERROR; 683 else 684 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN; 685 } 686 687 static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport *rport, 688 u32 timeout) 689 { 690 rport->dev_loss_tmo = timeout; 691 } 692 693 /** 694 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport 695 * @rport: The FC rport where to teminate I/O 696 * 697 * Abort all pending SCSI commands for a port by closing the 698 * port. Using a reopen avoids a conflict with a shutdown 699 * overwriting a reopen. The "forced" ensures that a disappeared port 700 * is not opened again as valid due to the cached plogi data in 701 * non-NPIV mode. 702 */ 703 static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport) 704 { 705 struct zfcp_port *port; 706 struct Scsi_Host *shost = rport_to_shost(rport); 707 struct zfcp_adapter *adapter = 708 (struct zfcp_adapter *)shost->hostdata[0]; 709 710 port = zfcp_get_port_by_wwpn(adapter, rport->port_name); 711 712 if (port) { 713 zfcp_erp_port_forced_reopen(port, 0, "sctrpi1"); 714 put_device(&port->dev); 715 } else { 716 zfcp_erp_port_forced_no_port_dbf( 717 "sctrpin", adapter, 718 rport->port_name /* zfcp_scsi_rport_register */, 719 rport->port_id /* zfcp_scsi_rport_register */); 720 } 721 } 722 723 static void zfcp_scsi_rport_register(struct zfcp_port *port) 724 { 725 struct fc_rport_identifiers ids; 726 struct fc_rport *rport; 727 728 if (port->rport) 729 return; 730 731 ids.node_name = port->wwnn; 732 ids.port_name = port->wwpn; 733 ids.port_id = port->d_id; 734 ids.roles = FC_RPORT_ROLE_FCP_TARGET; 735 736 zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL, 737 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD, 738 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD); 739 rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids); 740 if (!rport) { 741 dev_err(&port->adapter->ccw_device->dev, 742 "Registering port 0x%016Lx failed\n", 743 (unsigned long long)port->wwpn); 744 return; 745 } 746 747 rport->maxframe_size = port->maxframe_size; 748 rport->supported_classes = port->supported_classes; 749 port->rport = rport; 750 port->starget_id = rport->scsi_target_id; 751 752 zfcp_unit_queue_scsi_scan(port); 753 } 754 755 static void zfcp_scsi_rport_block(struct zfcp_port *port) 756 { 757 struct fc_rport *rport = port->rport; 758 759 if (rport) { 760 zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL, 761 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL, 762 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL); 763 fc_remote_port_delete(rport); 764 port->rport = NULL; 765 } 766 } 767 768 void zfcp_scsi_schedule_rport_register(struct zfcp_port *port) 769 { 770 get_device(&port->dev); 771 port->rport_task = RPORT_ADD; 772 773 if (!queue_work(port->adapter->work_queue, &port->rport_work)) 774 put_device(&port->dev); 775 } 776 777 void zfcp_scsi_schedule_rport_block(struct zfcp_port *port) 778 { 779 get_device(&port->dev); 780 port->rport_task = RPORT_DEL; 781 782 if (port->rport && queue_work(port->adapter->work_queue, 783 &port->rport_work)) 784 return; 785 786 put_device(&port->dev); 787 } 788 789 void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter) 790 { 791 unsigned long flags; 792 struct zfcp_port *port; 793 794 read_lock_irqsave(&adapter->port_list_lock, flags); 795 list_for_each_entry(port, &adapter->port_list, list) 796 zfcp_scsi_schedule_rport_block(port); 797 read_unlock_irqrestore(&adapter->port_list_lock, flags); 798 } 799 800 void zfcp_scsi_rport_work(struct work_struct *work) 801 { 802 struct zfcp_port *port = container_of(work, struct zfcp_port, 803 rport_work); 804 805 set_worker_desc("zrp%c-%16llx", 806 (port->rport_task == RPORT_ADD) ? 'a' : 'd', 807 port->wwpn); /* < WORKER_DESC_LEN=24 */ 808 while (port->rport_task) { 809 if (port->rport_task == RPORT_ADD) { 810 port->rport_task = RPORT_NONE; 811 zfcp_scsi_rport_register(port); 812 } else { 813 port->rport_task = RPORT_NONE; 814 zfcp_scsi_rport_block(port); 815 } 816 } 817 818 put_device(&port->dev); 819 } 820 821 /** 822 * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host 823 * @adapter: The adapter where to configure DIF/DIX for the SCSI host 824 */ 825 void zfcp_scsi_set_prot(struct zfcp_adapter *adapter) 826 { 827 unsigned int mask = 0; 828 unsigned int data_div; 829 struct Scsi_Host *shost = adapter->scsi_host; 830 831 data_div = atomic_read(&adapter->status) & 832 ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED; 833 834 if ((enable_dif || zfcp_experimental_dix) && 835 adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1) 836 mask |= SHOST_DIF_TYPE1_PROTECTION; 837 838 if (zfcp_experimental_dix && data_div && 839 adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) { 840 mask |= SHOST_DIX_TYPE1_PROTECTION; 841 scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP); 842 shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2; 843 shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2; 844 shost->max_sectors = shost->sg_tablesize * 8; 845 } 846 847 scsi_host_set_prot(shost, mask); 848 } 849 850 /** 851 * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error 852 * @scmd: The SCSI command to report the error for 853 * @ascq: The ASCQ to put in the sense buffer 854 * 855 * See the error handling in sd_done for the sense codes used here. 856 * Set DID_SOFT_ERROR to retry the request, if possible. 857 */ 858 void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq) 859 { 860 scsi_build_sense(scmd, 1, ILLEGAL_REQUEST, 0x10, ascq); 861 set_host_byte(scmd, DID_SOFT_ERROR); 862 } 863 864 void zfcp_scsi_shost_update_config_data( 865 struct zfcp_adapter *const adapter, 866 const struct fsf_qtcb_bottom_config *const bottom, 867 const bool bottom_incomplete) 868 { 869 struct Scsi_Host *const shost = adapter->scsi_host; 870 const struct fc_els_flogi *nsp, *plogi; 871 872 if (shost == NULL) 873 return; 874 875 snprintf(fc_host_firmware_version(shost), FC_VERSION_STRING_SIZE, 876 "0x%08x", bottom->lic_version); 877 878 if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) { 879 snprintf(fc_host_hardware_version(shost), 880 FC_VERSION_STRING_SIZE, 881 "0x%08x", bottom->hardware_version); 882 memcpy(fc_host_serial_number(shost), bottom->serial_number, 883 min(FC_SERIAL_NUMBER_SIZE, 17)); 884 EBCASC(fc_host_serial_number(shost), 885 min(FC_SERIAL_NUMBER_SIZE, 17)); 886 } 887 888 /* adjust pointers for missing command code */ 889 nsp = (struct fc_els_flogi *) ((u8 *)&bottom->nport_serv_param 890 - sizeof(u32)); 891 plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload 892 - sizeof(u32)); 893 894 snprintf(fc_host_manufacturer(shost), FC_SERIAL_NUMBER_SIZE, "%s", 895 "IBM"); 896 fc_host_port_name(shost) = be64_to_cpu(nsp->fl_wwpn); 897 fc_host_node_name(shost) = be64_to_cpu(nsp->fl_wwnn); 898 fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3; 899 900 zfcp_scsi_set_prot(adapter); 901 902 /* do not evaluate invalid fields */ 903 if (bottom_incomplete) 904 return; 905 906 fc_host_port_id(shost) = ntoh24(bottom->s_id); 907 fc_host_speed(shost) = 908 zfcp_fsf_convert_portspeed(bottom->fc_link_speed); 909 910 snprintf(fc_host_model(shost), FC_SYMBOLIC_NAME_SIZE, "0x%04x", 911 bottom->adapter_type); 912 913 switch (bottom->fc_topology) { 914 case FSF_TOPO_P2P: 915 fc_host_port_type(shost) = FC_PORTTYPE_PTP; 916 fc_host_fabric_name(shost) = 0; 917 break; 918 case FSF_TOPO_FABRIC: 919 fc_host_fabric_name(shost) = be64_to_cpu(plogi->fl_wwnn); 920 if (bottom->connection_features & FSF_FEATURE_NPIV_MODE) 921 fc_host_port_type(shost) = FC_PORTTYPE_NPIV; 922 else 923 fc_host_port_type(shost) = FC_PORTTYPE_NPORT; 924 break; 925 case FSF_TOPO_AL: 926 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT; 927 fallthrough; 928 default: 929 fc_host_fabric_name(shost) = 0; 930 break; 931 } 932 } 933 934 void zfcp_scsi_shost_update_port_data( 935 struct zfcp_adapter *const adapter, 936 const struct fsf_qtcb_bottom_port *const bottom) 937 { 938 struct Scsi_Host *const shost = adapter->scsi_host; 939 940 if (shost == NULL) 941 return; 942 943 fc_host_permanent_port_name(shost) = bottom->wwpn; 944 fc_host_maxframe_size(shost) = bottom->maximum_frame_size; 945 fc_host_supported_speeds(shost) = 946 zfcp_fsf_convert_portspeed(bottom->supported_speed); 947 memcpy(fc_host_supported_fc4s(shost), bottom->supported_fc4_types, 948 FC_FC4_LIST_SIZE); 949 memcpy(fc_host_active_fc4s(shost), bottom->active_fc4_types, 950 FC_FC4_LIST_SIZE); 951 } 952 953 struct fc_function_template zfcp_transport_functions = { 954 .show_starget_port_id = 1, 955 .show_starget_port_name = 1, 956 .show_starget_node_name = 1, 957 .show_rport_supported_classes = 1, 958 .show_rport_maxframe_size = 1, 959 .show_rport_dev_loss_tmo = 1, 960 .show_host_node_name = 1, 961 .show_host_port_name = 1, 962 .show_host_permanent_port_name = 1, 963 .show_host_supported_classes = 1, 964 .show_host_supported_fc4s = 1, 965 .show_host_supported_speeds = 1, 966 .show_host_maxframe_size = 1, 967 .show_host_serial_number = 1, 968 .show_host_manufacturer = 1, 969 .show_host_model = 1, 970 .show_host_hardware_version = 1, 971 .show_host_firmware_version = 1, 972 .get_fc_host_stats = zfcp_scsi_get_fc_host_stats, 973 .reset_fc_host_stats = zfcp_scsi_reset_fc_host_stats, 974 .set_rport_dev_loss_tmo = zfcp_scsi_set_rport_dev_loss_tmo, 975 .get_host_port_state = zfcp_scsi_get_host_port_state, 976 .terminate_rport_io = zfcp_scsi_terminate_rport_io, 977 .show_host_port_state = 1, 978 .show_host_active_fc4s = 1, 979 .bsg_request = zfcp_fc_exec_bsg_job, 980 .bsg_timeout = zfcp_fc_timeout_bsg_job, 981 /* no functions registered for following dynamic attributes but 982 directly set by LLDD */ 983 .show_host_port_type = 1, 984 .show_host_symbolic_name = 1, 985 .show_host_speed = 1, 986 .show_host_port_id = 1, 987 .show_host_fabric_name = 1, 988 .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els), 989 }; 990