1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * zfcp device driver 4 * 5 * Fibre Channel related functions for the zfcp device driver. 6 * 7 * Copyright IBM Corp. 2008, 2017 8 */ 9 10 #define KMSG_COMPONENT "zfcp" 11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 12 13 #include <linux/types.h> 14 #include <linux/slab.h> 15 #include <linux/utsname.h> 16 #include <linux/random.h> 17 #include <linux/bsg-lib.h> 18 #include <scsi/fc/fc_els.h> 19 #include <scsi/libfc.h> 20 #include "zfcp_ext.h" 21 #include "zfcp_fc.h" 22 23 struct kmem_cache *zfcp_fc_req_cache; 24 25 static u32 zfcp_fc_rscn_range_mask[] = { 26 [ELS_ADDR_FMT_PORT] = 0xFFFFFF, 27 [ELS_ADDR_FMT_AREA] = 0xFFFF00, 28 [ELS_ADDR_FMT_DOM] = 0xFF0000, 29 [ELS_ADDR_FMT_FAB] = 0x000000, 30 }; 31 32 static bool no_auto_port_rescan; 33 module_param(no_auto_port_rescan, bool, 0600); 34 MODULE_PARM_DESC(no_auto_port_rescan, 35 "no automatic port_rescan (default off)"); 36 37 static unsigned int port_scan_backoff = 500; 38 module_param(port_scan_backoff, uint, 0600); 39 MODULE_PARM_DESC(port_scan_backoff, 40 "upper limit of port scan random backoff in msecs (default 500)"); 41 42 static unsigned int port_scan_ratelimit = 60000; 43 module_param(port_scan_ratelimit, uint, 0600); 44 MODULE_PARM_DESC(port_scan_ratelimit, 45 "minimum interval between port scans in msecs (default 60000)"); 46 47 unsigned int zfcp_fc_port_scan_backoff(void) 48 { 49 if (!port_scan_backoff) 50 return 0; 51 return prandom_u32_max(port_scan_backoff); 52 } 53 54 static void zfcp_fc_port_scan_time(struct zfcp_adapter *adapter) 55 { 56 unsigned long interval = msecs_to_jiffies(port_scan_ratelimit); 57 unsigned long backoff = msecs_to_jiffies(zfcp_fc_port_scan_backoff()); 58 59 adapter->next_port_scan = jiffies + interval + backoff; 60 } 61 62 static void zfcp_fc_port_scan(struct zfcp_adapter *adapter) 63 { 64 unsigned long now = jiffies; 65 unsigned long next = adapter->next_port_scan; 66 unsigned long delay = 0, max; 67 68 /* delay only needed within waiting period */ 69 if (time_before(now, next)) { 70 delay = next - now; 71 /* paranoia: never ever delay scans longer than specified */ 72 max = msecs_to_jiffies(port_scan_ratelimit + port_scan_backoff); 73 delay = min(delay, max); 74 } 75 76 queue_delayed_work(adapter->work_queue, &adapter->scan_work, delay); 77 } 78 79 void zfcp_fc_conditional_port_scan(struct zfcp_adapter *adapter) 80 { 81 if (no_auto_port_rescan) 82 return; 83 84 zfcp_fc_port_scan(adapter); 85 } 86 87 void zfcp_fc_inverse_conditional_port_scan(struct zfcp_adapter *adapter) 88 { 89 if (!no_auto_port_rescan) 90 return; 91 92 zfcp_fc_port_scan(adapter); 93 } 94 95 /** 96 * zfcp_fc_post_event - post event to userspace via fc_transport 97 * @work: work struct with enqueued events 98 */ 99 void zfcp_fc_post_event(struct work_struct *work) 100 { 101 struct zfcp_fc_event *event = NULL, *tmp = NULL; 102 LIST_HEAD(tmp_lh); 103 struct zfcp_fc_events *events = container_of(work, 104 struct zfcp_fc_events, work); 105 struct zfcp_adapter *adapter = container_of(events, struct zfcp_adapter, 106 events); 107 108 spin_lock_bh(&events->list_lock); 109 list_splice_init(&events->list, &tmp_lh); 110 spin_unlock_bh(&events->list_lock); 111 112 list_for_each_entry_safe(event, tmp, &tmp_lh, list) { 113 fc_host_post_event(adapter->scsi_host, fc_get_event_number(), 114 event->code, event->data); 115 list_del(&event->list); 116 kfree(event); 117 } 118 } 119 120 /** 121 * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context 122 * @adapter: The adapter where to enqueue the event 123 * @event_code: The event code (as defined in fc_host_event_code in 124 * scsi_transport_fc.h) 125 * @event_data: The event data (e.g. n_port page in case of els) 126 */ 127 void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter, 128 enum fc_host_event_code event_code, u32 event_data) 129 { 130 struct zfcp_fc_event *event; 131 132 event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC); 133 if (!event) 134 return; 135 136 event->code = event_code; 137 event->data = event_data; 138 139 spin_lock(&adapter->events.list_lock); 140 list_add_tail(&event->list, &adapter->events.list); 141 spin_unlock(&adapter->events.list_lock); 142 143 queue_work(adapter->work_queue, &adapter->events.work); 144 } 145 146 static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port) 147 { 148 if (mutex_lock_interruptible(&wka_port->mutex)) 149 return -ERESTARTSYS; 150 151 if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE || 152 wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) { 153 wka_port->status = ZFCP_FC_WKA_PORT_OPENING; 154 if (zfcp_fsf_open_wka_port(wka_port)) 155 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE; 156 } 157 158 mutex_unlock(&wka_port->mutex); 159 160 wait_event(wka_port->completion_wq, 161 wka_port->status == ZFCP_FC_WKA_PORT_ONLINE || 162 wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE); 163 164 if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) { 165 atomic_inc(&wka_port->refcount); 166 return 0; 167 } 168 return -EIO; 169 } 170 171 static void zfcp_fc_wka_port_offline(struct work_struct *work) 172 { 173 struct delayed_work *dw = to_delayed_work(work); 174 struct zfcp_fc_wka_port *wka_port = 175 container_of(dw, struct zfcp_fc_wka_port, work); 176 177 mutex_lock(&wka_port->mutex); 178 if ((atomic_read(&wka_port->refcount) != 0) || 179 (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE)) 180 goto out; 181 182 wka_port->status = ZFCP_FC_WKA_PORT_CLOSING; 183 if (zfcp_fsf_close_wka_port(wka_port)) { 184 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE; 185 wake_up(&wka_port->completion_wq); 186 } 187 out: 188 mutex_unlock(&wka_port->mutex); 189 } 190 191 static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port) 192 { 193 if (atomic_dec_return(&wka_port->refcount) != 0) 194 return; 195 /* wait 10 milliseconds, other reqs might pop in */ 196 schedule_delayed_work(&wka_port->work, HZ / 100); 197 } 198 199 static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id, 200 struct zfcp_adapter *adapter) 201 { 202 init_waitqueue_head(&wka_port->completion_wq); 203 204 wka_port->adapter = adapter; 205 wka_port->d_id = d_id; 206 207 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE; 208 atomic_set(&wka_port->refcount, 0); 209 mutex_init(&wka_port->mutex); 210 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline); 211 } 212 213 static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka) 214 { 215 cancel_delayed_work_sync(&wka->work); 216 mutex_lock(&wka->mutex); 217 wka->status = ZFCP_FC_WKA_PORT_OFFLINE; 218 mutex_unlock(&wka->mutex); 219 } 220 221 void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs) 222 { 223 if (!gs) 224 return; 225 zfcp_fc_wka_port_force_offline(&gs->ms); 226 zfcp_fc_wka_port_force_offline(&gs->ts); 227 zfcp_fc_wka_port_force_offline(&gs->ds); 228 zfcp_fc_wka_port_force_offline(&gs->as); 229 } 230 231 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range, 232 struct fc_els_rscn_page *page) 233 { 234 unsigned long flags; 235 struct zfcp_adapter *adapter = fsf_req->adapter; 236 struct zfcp_port *port; 237 238 read_lock_irqsave(&adapter->port_list_lock, flags); 239 list_for_each_entry(port, &adapter->port_list, list) { 240 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range)) 241 zfcp_fc_test_link(port); 242 } 243 read_unlock_irqrestore(&adapter->port_list_lock, flags); 244 } 245 246 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req) 247 { 248 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data; 249 struct zfcp_adapter *adapter = fsf_req->adapter; 250 struct fc_els_rscn *head; 251 struct fc_els_rscn_page *page; 252 u16 i; 253 u16 no_entries; 254 unsigned int afmt; 255 256 head = (struct fc_els_rscn *) status_buffer->payload.data; 257 page = (struct fc_els_rscn_page *) head; 258 259 /* see FC-FS */ 260 no_entries = be16_to_cpu(head->rscn_plen) / 261 sizeof(struct fc_els_rscn_page); 262 263 if (no_entries > 1) { 264 /* handle failed ports */ 265 unsigned long flags; 266 struct zfcp_port *port; 267 268 read_lock_irqsave(&adapter->port_list_lock, flags); 269 list_for_each_entry(port, &adapter->port_list, list) { 270 if (port->d_id) 271 continue; 272 zfcp_erp_port_reopen(port, 273 ZFCP_STATUS_COMMON_ERP_FAILED, 274 "fcrscn1"); 275 } 276 read_unlock_irqrestore(&adapter->port_list_lock, flags); 277 } 278 279 for (i = 1; i < no_entries; i++) { 280 /* skip head and start with 1st element */ 281 page++; 282 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK; 283 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt], 284 page); 285 zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN, 286 *(u32 *)page); 287 } 288 zfcp_fc_conditional_port_scan(fsf_req->adapter); 289 } 290 291 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn) 292 { 293 unsigned long flags; 294 struct zfcp_adapter *adapter = req->adapter; 295 struct zfcp_port *port; 296 297 read_lock_irqsave(&adapter->port_list_lock, flags); 298 list_for_each_entry(port, &adapter->port_list, list) 299 if (port->wwpn == wwpn) { 300 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1"); 301 break; 302 } 303 read_unlock_irqrestore(&adapter->port_list_lock, flags); 304 } 305 306 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req) 307 { 308 struct fsf_status_read_buffer *status_buffer; 309 struct fc_els_flogi *plogi; 310 311 status_buffer = (struct fsf_status_read_buffer *) req->data; 312 plogi = (struct fc_els_flogi *) status_buffer->payload.data; 313 zfcp_fc_incoming_wwpn(req, be64_to_cpu(plogi->fl_wwpn)); 314 } 315 316 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req) 317 { 318 struct fsf_status_read_buffer *status_buffer = 319 (struct fsf_status_read_buffer *)req->data; 320 struct fc_els_logo *logo = 321 (struct fc_els_logo *) status_buffer->payload.data; 322 323 zfcp_fc_incoming_wwpn(req, be64_to_cpu(logo->fl_n_port_wwn)); 324 } 325 326 /** 327 * zfcp_fc_incoming_els - handle incoming ELS 328 * @fsf_req: request which contains incoming ELS 329 */ 330 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req) 331 { 332 struct fsf_status_read_buffer *status_buffer = 333 (struct fsf_status_read_buffer *) fsf_req->data; 334 unsigned int els_type = status_buffer->payload.data[0]; 335 336 zfcp_dbf_san_in_els("fciels1", fsf_req); 337 if (els_type == ELS_PLOGI) 338 zfcp_fc_incoming_plogi(fsf_req); 339 else if (els_type == ELS_LOGO) 340 zfcp_fc_incoming_logo(fsf_req); 341 else if (els_type == ELS_RSCN) 342 zfcp_fc_incoming_rscn(fsf_req); 343 } 344 345 static void zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req *fc_req) 346 { 347 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els; 348 struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp; 349 350 if (ct_els->status) 351 return; 352 if (gid_pn_rsp->ct_hdr.ct_cmd != cpu_to_be16(FC_FS_ACC)) 353 return; 354 355 /* looks like a valid d_id */ 356 ct_els->port->d_id = ntoh24(gid_pn_rsp->gid_pn.fp_fid); 357 } 358 359 static void zfcp_fc_complete(void *data) 360 { 361 complete(data); 362 } 363 364 static void zfcp_fc_ct_ns_init(struct fc_ct_hdr *ct_hdr, u16 cmd, u16 mr_size) 365 { 366 ct_hdr->ct_rev = FC_CT_REV; 367 ct_hdr->ct_fs_type = FC_FST_DIR; 368 ct_hdr->ct_fs_subtype = FC_NS_SUBTYPE; 369 ct_hdr->ct_cmd = cpu_to_be16(cmd); 370 ct_hdr->ct_mr_size = cpu_to_be16(mr_size / 4); 371 } 372 373 static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port, 374 struct zfcp_fc_req *fc_req) 375 { 376 struct zfcp_adapter *adapter = port->adapter; 377 DECLARE_COMPLETION_ONSTACK(completion); 378 struct zfcp_fc_gid_pn_req *gid_pn_req = &fc_req->u.gid_pn.req; 379 struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp; 380 int ret; 381 382 /* setup parameters for send generic command */ 383 fc_req->ct_els.port = port; 384 fc_req->ct_els.handler = zfcp_fc_complete; 385 fc_req->ct_els.handler_data = &completion; 386 fc_req->ct_els.req = &fc_req->sg_req; 387 fc_req->ct_els.resp = &fc_req->sg_rsp; 388 sg_init_one(&fc_req->sg_req, gid_pn_req, sizeof(*gid_pn_req)); 389 sg_init_one(&fc_req->sg_rsp, gid_pn_rsp, sizeof(*gid_pn_rsp)); 390 391 zfcp_fc_ct_ns_init(&gid_pn_req->ct_hdr, 392 FC_NS_GID_PN, ZFCP_FC_CT_SIZE_PAGE); 393 gid_pn_req->gid_pn.fn_wwpn = cpu_to_be64(port->wwpn); 394 395 ret = zfcp_fsf_send_ct(&adapter->gs->ds, &fc_req->ct_els, 396 adapter->pool.gid_pn_req, 397 ZFCP_FC_CTELS_TMO); 398 if (!ret) { 399 wait_for_completion(&completion); 400 zfcp_fc_ns_gid_pn_eval(fc_req); 401 } 402 return ret; 403 } 404 405 /** 406 * zfcp_fc_ns_gid_pn - initiate GID_PN nameserver request 407 * @port: port where GID_PN request is needed 408 * return: -ENOMEM on error, 0 otherwise 409 */ 410 static int zfcp_fc_ns_gid_pn(struct zfcp_port *port) 411 { 412 int ret; 413 struct zfcp_fc_req *fc_req; 414 struct zfcp_adapter *adapter = port->adapter; 415 416 fc_req = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC); 417 if (!fc_req) 418 return -ENOMEM; 419 420 memset(fc_req, 0, sizeof(*fc_req)); 421 422 ret = zfcp_fc_wka_port_get(&adapter->gs->ds); 423 if (ret) 424 goto out; 425 426 ret = zfcp_fc_ns_gid_pn_request(port, fc_req); 427 428 zfcp_fc_wka_port_put(&adapter->gs->ds); 429 out: 430 mempool_free(fc_req, adapter->pool.gid_pn); 431 return ret; 432 } 433 434 void zfcp_fc_port_did_lookup(struct work_struct *work) 435 { 436 int ret; 437 struct zfcp_port *port = container_of(work, struct zfcp_port, 438 gid_pn_work); 439 440 set_worker_desc("zgidpn%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */ 441 ret = zfcp_fc_ns_gid_pn(port); 442 if (ret) { 443 /* could not issue gid_pn for some reason */ 444 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1"); 445 goto out; 446 } 447 448 if (!port->d_id) { 449 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED); 450 goto out; 451 } 452 453 zfcp_erp_port_reopen(port, 0, "fcgpn_3"); 454 out: 455 put_device(&port->dev); 456 } 457 458 /** 459 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request 460 * @port: The zfcp_port to lookup the d_id for. 461 */ 462 void zfcp_fc_trigger_did_lookup(struct zfcp_port *port) 463 { 464 get_device(&port->dev); 465 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work)) 466 put_device(&port->dev); 467 } 468 469 /** 470 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload 471 * @port: zfcp_port structure 472 * @plogi: plogi payload 473 * 474 * Evaluate PLOGI playload and copy important fields into zfcp_port structure 475 */ 476 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi) 477 { 478 if (be64_to_cpu(plogi->fl_wwpn) != port->wwpn) { 479 port->d_id = 0; 480 dev_warn(&port->adapter->ccw_device->dev, 481 "A port opened with WWPN 0x%016Lx returned data that " 482 "identifies it as WWPN 0x%016Lx\n", 483 (unsigned long long) port->wwpn, 484 (unsigned long long) be64_to_cpu(plogi->fl_wwpn)); 485 return; 486 } 487 488 port->wwnn = be64_to_cpu(plogi->fl_wwnn); 489 port->maxframe_size = be16_to_cpu(plogi->fl_csp.sp_bb_data); 490 491 if (plogi->fl_cssp[0].cp_class & cpu_to_be16(FC_CPC_VALID)) 492 port->supported_classes |= FC_COS_CLASS1; 493 if (plogi->fl_cssp[1].cp_class & cpu_to_be16(FC_CPC_VALID)) 494 port->supported_classes |= FC_COS_CLASS2; 495 if (plogi->fl_cssp[2].cp_class & cpu_to_be16(FC_CPC_VALID)) 496 port->supported_classes |= FC_COS_CLASS3; 497 if (plogi->fl_cssp[3].cp_class & cpu_to_be16(FC_CPC_VALID)) 498 port->supported_classes |= FC_COS_CLASS4; 499 } 500 501 static void zfcp_fc_adisc_handler(void *data) 502 { 503 struct zfcp_fc_req *fc_req = data; 504 struct zfcp_port *port = fc_req->ct_els.port; 505 struct fc_els_adisc *adisc_resp = &fc_req->u.adisc.rsp; 506 507 if (fc_req->ct_els.status) { 508 /* request rejected or timed out */ 509 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, 510 "fcadh_1"); 511 goto out; 512 } 513 514 if (!port->wwnn) 515 port->wwnn = be64_to_cpu(adisc_resp->adisc_wwnn); 516 517 if ((port->wwpn != be64_to_cpu(adisc_resp->adisc_wwpn)) || 518 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) { 519 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, 520 "fcadh_2"); 521 goto out; 522 } 523 524 /* port is good, unblock rport without going through erp */ 525 zfcp_scsi_schedule_rport_register(port); 526 out: 527 atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status); 528 put_device(&port->dev); 529 kmem_cache_free(zfcp_fc_req_cache, fc_req); 530 } 531 532 static int zfcp_fc_adisc(struct zfcp_port *port) 533 { 534 struct zfcp_fc_req *fc_req; 535 struct zfcp_adapter *adapter = port->adapter; 536 struct Scsi_Host *shost = adapter->scsi_host; 537 int ret; 538 539 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC); 540 if (!fc_req) 541 return -ENOMEM; 542 543 fc_req->ct_els.port = port; 544 fc_req->ct_els.req = &fc_req->sg_req; 545 fc_req->ct_els.resp = &fc_req->sg_rsp; 546 sg_init_one(&fc_req->sg_req, &fc_req->u.adisc.req, 547 sizeof(struct fc_els_adisc)); 548 sg_init_one(&fc_req->sg_rsp, &fc_req->u.adisc.rsp, 549 sizeof(struct fc_els_adisc)); 550 551 fc_req->ct_els.handler = zfcp_fc_adisc_handler; 552 fc_req->ct_els.handler_data = fc_req; 553 554 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports 555 without FC-AL-2 capability, so we don't set it */ 556 fc_req->u.adisc.req.adisc_wwpn = cpu_to_be64(fc_host_port_name(shost)); 557 fc_req->u.adisc.req.adisc_wwnn = cpu_to_be64(fc_host_node_name(shost)); 558 fc_req->u.adisc.req.adisc_cmd = ELS_ADISC; 559 hton24(fc_req->u.adisc.req.adisc_port_id, fc_host_port_id(shost)); 560 561 ret = zfcp_fsf_send_els(adapter, port->d_id, &fc_req->ct_els, 562 ZFCP_FC_CTELS_TMO); 563 if (ret) 564 kmem_cache_free(zfcp_fc_req_cache, fc_req); 565 566 return ret; 567 } 568 569 void zfcp_fc_link_test_work(struct work_struct *work) 570 { 571 struct zfcp_port *port = 572 container_of(work, struct zfcp_port, test_link_work); 573 int retval; 574 575 set_worker_desc("zadisc%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */ 576 get_device(&port->dev); 577 port->rport_task = RPORT_DEL; 578 zfcp_scsi_rport_work(&port->rport_work); 579 580 /* only issue one test command at one time per port */ 581 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST) 582 goto out; 583 584 atomic_or(ZFCP_STATUS_PORT_LINK_TEST, &port->status); 585 586 retval = zfcp_fc_adisc(port); 587 if (retval == 0) 588 return; 589 590 /* send of ADISC was not possible */ 591 atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status); 592 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1"); 593 594 out: 595 put_device(&port->dev); 596 } 597 598 /** 599 * zfcp_fc_test_link - lightweight link test procedure 600 * @port: port to be tested 601 * 602 * Test status of a link to a remote port using the ELS command ADISC. 603 * If there is a problem with the remote port, error recovery steps 604 * will be triggered. 605 */ 606 void zfcp_fc_test_link(struct zfcp_port *port) 607 { 608 get_device(&port->dev); 609 if (!queue_work(port->adapter->work_queue, &port->test_link_work)) 610 put_device(&port->dev); 611 } 612 613 /** 614 * zfcp_fc_sg_free_table - free memory used by scatterlists 615 * @sg: pointer to scatterlist 616 * @count: number of scatterlist which are to be free'ed 617 * the scatterlist are expected to reference pages always 618 */ 619 static void zfcp_fc_sg_free_table(struct scatterlist *sg, int count) 620 { 621 int i; 622 623 for (i = 0; i < count; i++, sg = sg_next(sg)) 624 if (sg) 625 free_page((unsigned long) sg_virt(sg)); 626 else 627 break; 628 } 629 630 /** 631 * zfcp_fc_sg_setup_table - init scatterlist and allocate, assign buffers 632 * @sg: pointer to struct scatterlist 633 * @count: number of scatterlists which should be assigned with buffers 634 * of size page 635 * 636 * Returns: 0 on success, -ENOMEM otherwise 637 */ 638 static int zfcp_fc_sg_setup_table(struct scatterlist *sg, int count) 639 { 640 void *addr; 641 int i; 642 643 sg_init_table(sg, count); 644 for (i = 0; i < count; i++, sg = sg_next(sg)) { 645 addr = (void *) get_zeroed_page(GFP_KERNEL); 646 if (!addr) { 647 zfcp_fc_sg_free_table(sg, i); 648 return -ENOMEM; 649 } 650 sg_set_buf(sg, addr, PAGE_SIZE); 651 } 652 return 0; 653 } 654 655 static struct zfcp_fc_req *zfcp_fc_alloc_sg_env(int buf_num) 656 { 657 struct zfcp_fc_req *fc_req; 658 659 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL); 660 if (!fc_req) 661 return NULL; 662 663 if (zfcp_fc_sg_setup_table(&fc_req->sg_rsp, buf_num)) { 664 kmem_cache_free(zfcp_fc_req_cache, fc_req); 665 return NULL; 666 } 667 668 sg_init_one(&fc_req->sg_req, &fc_req->u.gpn_ft.req, 669 sizeof(struct zfcp_fc_gpn_ft_req)); 670 671 return fc_req; 672 } 673 674 static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req, 675 struct zfcp_adapter *adapter, int max_bytes) 676 { 677 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els; 678 struct zfcp_fc_gpn_ft_req *req = &fc_req->u.gpn_ft.req; 679 DECLARE_COMPLETION_ONSTACK(completion); 680 int ret; 681 682 zfcp_fc_ct_ns_init(&req->ct_hdr, FC_NS_GPN_FT, max_bytes); 683 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP; 684 685 ct_els->handler = zfcp_fc_complete; 686 ct_els->handler_data = &completion; 687 ct_els->req = &fc_req->sg_req; 688 ct_els->resp = &fc_req->sg_rsp; 689 690 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL, 691 ZFCP_FC_CTELS_TMO); 692 if (!ret) 693 wait_for_completion(&completion); 694 return ret; 695 } 696 697 static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh) 698 { 699 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)) 700 return; 701 702 atomic_andnot(ZFCP_STATUS_COMMON_NOESC, &port->status); 703 704 if ((port->supported_classes != 0) || 705 !list_empty(&port->unit_list)) 706 return; 707 708 list_move_tail(&port->list, lh); 709 } 710 711 static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_req *fc_req, 712 struct zfcp_adapter *adapter, int max_entries) 713 { 714 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els; 715 struct scatterlist *sg = &fc_req->sg_rsp; 716 struct fc_ct_hdr *hdr = sg_virt(sg); 717 struct fc_gpn_ft_resp *acc = sg_virt(sg); 718 struct zfcp_port *port, *tmp; 719 unsigned long flags; 720 LIST_HEAD(remove_lh); 721 u32 d_id; 722 int ret = 0, x, last = 0; 723 724 if (ct_els->status) 725 return -EIO; 726 727 if (hdr->ct_cmd != cpu_to_be16(FC_FS_ACC)) { 728 if (hdr->ct_reason == FC_FS_RJT_UNABL) 729 return -EAGAIN; /* might be a temporary condition */ 730 return -EIO; 731 } 732 733 if (hdr->ct_mr_size) { 734 dev_warn(&adapter->ccw_device->dev, 735 "The name server reported %d words residual data\n", 736 hdr->ct_mr_size); 737 return -E2BIG; 738 } 739 740 /* first entry is the header */ 741 for (x = 1; x < max_entries && !last; x++) { 742 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1)) 743 acc++; 744 else 745 acc = sg_virt(++sg); 746 747 last = acc->fp_flags & FC_NS_FID_LAST; 748 d_id = ntoh24(acc->fp_fid); 749 750 /* don't attach ports with a well known address */ 751 if (d_id >= FC_FID_WELL_KNOWN_BASE) 752 continue; 753 /* skip the adapter's port and known remote ports */ 754 if (be64_to_cpu(acc->fp_wwpn) == 755 fc_host_port_name(adapter->scsi_host)) 756 continue; 757 758 port = zfcp_port_enqueue(adapter, be64_to_cpu(acc->fp_wwpn), 759 ZFCP_STATUS_COMMON_NOESC, d_id); 760 if (!IS_ERR(port)) 761 zfcp_erp_port_reopen(port, 0, "fcegpf1"); 762 else if (PTR_ERR(port) != -EEXIST) 763 ret = PTR_ERR(port); 764 } 765 766 zfcp_erp_wait(adapter); 767 write_lock_irqsave(&adapter->port_list_lock, flags); 768 list_for_each_entry_safe(port, tmp, &adapter->port_list, list) 769 zfcp_fc_validate_port(port, &remove_lh); 770 write_unlock_irqrestore(&adapter->port_list_lock, flags); 771 772 list_for_each_entry_safe(port, tmp, &remove_lh, list) { 773 zfcp_erp_port_shutdown(port, 0, "fcegpf2"); 774 device_unregister(&port->dev); 775 } 776 777 return ret; 778 } 779 780 /** 781 * zfcp_fc_scan_ports - scan remote ports and attach new ports 782 * @work: reference to scheduled work 783 */ 784 void zfcp_fc_scan_ports(struct work_struct *work) 785 { 786 struct delayed_work *dw = to_delayed_work(work); 787 struct zfcp_adapter *adapter = container_of(dw, struct zfcp_adapter, 788 scan_work); 789 int ret, i; 790 struct zfcp_fc_req *fc_req; 791 int chain, max_entries, buf_num, max_bytes; 792 793 zfcp_fc_port_scan_time(adapter); 794 795 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS; 796 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1; 797 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE; 798 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE; 799 800 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT && 801 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV) 802 return; 803 804 if (zfcp_fc_wka_port_get(&adapter->gs->ds)) 805 return; 806 807 fc_req = zfcp_fc_alloc_sg_env(buf_num); 808 if (!fc_req) 809 goto out; 810 811 for (i = 0; i < 3; i++) { 812 ret = zfcp_fc_send_gpn_ft(fc_req, adapter, max_bytes); 813 if (!ret) { 814 ret = zfcp_fc_eval_gpn_ft(fc_req, adapter, max_entries); 815 if (ret == -EAGAIN) 816 ssleep(1); 817 else 818 break; 819 } 820 } 821 zfcp_fc_sg_free_table(&fc_req->sg_rsp, buf_num); 822 kmem_cache_free(zfcp_fc_req_cache, fc_req); 823 out: 824 zfcp_fc_wka_port_put(&adapter->gs->ds); 825 } 826 827 static int zfcp_fc_gspn(struct zfcp_adapter *adapter, 828 struct zfcp_fc_req *fc_req) 829 { 830 DECLARE_COMPLETION_ONSTACK(completion); 831 char devno[] = "DEVNO:"; 832 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els; 833 struct zfcp_fc_gspn_req *gspn_req = &fc_req->u.gspn.req; 834 struct zfcp_fc_gspn_rsp *gspn_rsp = &fc_req->u.gspn.rsp; 835 int ret; 836 837 zfcp_fc_ct_ns_init(&gspn_req->ct_hdr, FC_NS_GSPN_ID, 838 FC_SYMBOLIC_NAME_SIZE); 839 hton24(gspn_req->gspn.fp_fid, fc_host_port_id(adapter->scsi_host)); 840 841 sg_init_one(&fc_req->sg_req, gspn_req, sizeof(*gspn_req)); 842 sg_init_one(&fc_req->sg_rsp, gspn_rsp, sizeof(*gspn_rsp)); 843 844 ct_els->handler = zfcp_fc_complete; 845 ct_els->handler_data = &completion; 846 ct_els->req = &fc_req->sg_req; 847 ct_els->resp = &fc_req->sg_rsp; 848 849 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL, 850 ZFCP_FC_CTELS_TMO); 851 if (ret) 852 return ret; 853 854 wait_for_completion(&completion); 855 if (ct_els->status) 856 return ct_els->status; 857 858 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_NPIV && 859 !(strstr(gspn_rsp->gspn.fp_name, devno))) 860 snprintf(fc_host_symbolic_name(adapter->scsi_host), 861 FC_SYMBOLIC_NAME_SIZE, "%s%s %s NAME: %s", 862 gspn_rsp->gspn.fp_name, devno, 863 dev_name(&adapter->ccw_device->dev), 864 init_utsname()->nodename); 865 else 866 strlcpy(fc_host_symbolic_name(adapter->scsi_host), 867 gspn_rsp->gspn.fp_name, FC_SYMBOLIC_NAME_SIZE); 868 869 return 0; 870 } 871 872 static void zfcp_fc_rspn(struct zfcp_adapter *adapter, 873 struct zfcp_fc_req *fc_req) 874 { 875 DECLARE_COMPLETION_ONSTACK(completion); 876 struct Scsi_Host *shost = adapter->scsi_host; 877 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els; 878 struct zfcp_fc_rspn_req *rspn_req = &fc_req->u.rspn.req; 879 struct fc_ct_hdr *rspn_rsp = &fc_req->u.rspn.rsp; 880 int ret, len; 881 882 zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID, 883 FC_SYMBOLIC_NAME_SIZE); 884 hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost)); 885 len = strlcpy(rspn_req->rspn.fr_name, fc_host_symbolic_name(shost), 886 FC_SYMBOLIC_NAME_SIZE); 887 rspn_req->rspn.fr_name_len = len; 888 889 sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req)); 890 sg_init_one(&fc_req->sg_rsp, rspn_rsp, sizeof(*rspn_rsp)); 891 892 ct_els->handler = zfcp_fc_complete; 893 ct_els->handler_data = &completion; 894 ct_els->req = &fc_req->sg_req; 895 ct_els->resp = &fc_req->sg_rsp; 896 897 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL, 898 ZFCP_FC_CTELS_TMO); 899 if (!ret) 900 wait_for_completion(&completion); 901 } 902 903 /** 904 * zfcp_fc_sym_name_update - Retrieve and update the symbolic port name 905 * @work: ns_up_work of the adapter where to update the symbolic port name 906 * 907 * Retrieve the current symbolic port name that may have been set by 908 * the hardware using the GSPN request and update the fc_host 909 * symbolic_name sysfs attribute. When running in NPIV mode (and hence 910 * the port name is unique for this system), update the symbolic port 911 * name to add Linux specific information and update the FC nameserver 912 * using the RSPN request. 913 */ 914 void zfcp_fc_sym_name_update(struct work_struct *work) 915 { 916 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter, 917 ns_up_work); 918 int ret; 919 struct zfcp_fc_req *fc_req; 920 921 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT && 922 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV) 923 return; 924 925 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL); 926 if (!fc_req) 927 return; 928 929 ret = zfcp_fc_wka_port_get(&adapter->gs->ds); 930 if (ret) 931 goto out_free; 932 933 ret = zfcp_fc_gspn(adapter, fc_req); 934 if (ret || fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV) 935 goto out_ds_put; 936 937 memset(fc_req, 0, sizeof(*fc_req)); 938 zfcp_fc_rspn(adapter, fc_req); 939 940 out_ds_put: 941 zfcp_fc_wka_port_put(&adapter->gs->ds); 942 out_free: 943 kmem_cache_free(zfcp_fc_req_cache, fc_req); 944 } 945 946 static void zfcp_fc_ct_els_job_handler(void *data) 947 { 948 struct bsg_job *job = data; 949 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data; 950 struct fc_bsg_reply *jr = job->reply; 951 952 jr->reply_payload_rcv_len = job->reply_payload.payload_len; 953 jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK; 954 jr->result = zfcp_ct_els->status ? -EIO : 0; 955 bsg_job_done(job, jr->result, jr->reply_payload_rcv_len); 956 } 957 958 static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct bsg_job *job) 959 { 960 u32 preamble_word1; 961 u8 gs_type; 962 struct zfcp_adapter *adapter; 963 struct fc_bsg_request *bsg_request = job->request; 964 struct fc_rport *rport = fc_bsg_to_rport(job); 965 struct Scsi_Host *shost; 966 967 preamble_word1 = bsg_request->rqst_data.r_ct.preamble_word1; 968 gs_type = (preamble_word1 & 0xff000000) >> 24; 969 970 shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job); 971 adapter = (struct zfcp_adapter *) shost->hostdata[0]; 972 973 switch (gs_type) { 974 case FC_FST_ALIAS: 975 return &adapter->gs->as; 976 case FC_FST_MGMT: 977 return &adapter->gs->ms; 978 case FC_FST_TIME: 979 return &adapter->gs->ts; 980 break; 981 case FC_FST_DIR: 982 return &adapter->gs->ds; 983 break; 984 default: 985 return NULL; 986 } 987 } 988 989 static void zfcp_fc_ct_job_handler(void *data) 990 { 991 struct bsg_job *job = data; 992 struct zfcp_fc_wka_port *wka_port; 993 994 wka_port = zfcp_fc_job_wka_port(job); 995 zfcp_fc_wka_port_put(wka_port); 996 997 zfcp_fc_ct_els_job_handler(data); 998 } 999 1000 static int zfcp_fc_exec_els_job(struct bsg_job *job, 1001 struct zfcp_adapter *adapter) 1002 { 1003 struct zfcp_fsf_ct_els *els = job->dd_data; 1004 struct fc_rport *rport = fc_bsg_to_rport(job); 1005 struct fc_bsg_request *bsg_request = job->request; 1006 struct zfcp_port *port; 1007 u32 d_id; 1008 1009 if (rport) { 1010 port = zfcp_get_port_by_wwpn(adapter, rport->port_name); 1011 if (!port) 1012 return -EINVAL; 1013 1014 d_id = port->d_id; 1015 put_device(&port->dev); 1016 } else 1017 d_id = ntoh24(bsg_request->rqst_data.h_els.port_id); 1018 1019 els->handler = zfcp_fc_ct_els_job_handler; 1020 return zfcp_fsf_send_els(adapter, d_id, els, job->timeout / HZ); 1021 } 1022 1023 static int zfcp_fc_exec_ct_job(struct bsg_job *job, 1024 struct zfcp_adapter *adapter) 1025 { 1026 int ret; 1027 struct zfcp_fsf_ct_els *ct = job->dd_data; 1028 struct zfcp_fc_wka_port *wka_port; 1029 1030 wka_port = zfcp_fc_job_wka_port(job); 1031 if (!wka_port) 1032 return -EINVAL; 1033 1034 ret = zfcp_fc_wka_port_get(wka_port); 1035 if (ret) 1036 return ret; 1037 1038 ct->handler = zfcp_fc_ct_job_handler; 1039 ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->timeout / HZ); 1040 if (ret) 1041 zfcp_fc_wka_port_put(wka_port); 1042 1043 return ret; 1044 } 1045 1046 int zfcp_fc_exec_bsg_job(struct bsg_job *job) 1047 { 1048 struct Scsi_Host *shost; 1049 struct zfcp_adapter *adapter; 1050 struct zfcp_fsf_ct_els *ct_els = job->dd_data; 1051 struct fc_bsg_request *bsg_request = job->request; 1052 struct fc_rport *rport = fc_bsg_to_rport(job); 1053 1054 shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job); 1055 adapter = (struct zfcp_adapter *)shost->hostdata[0]; 1056 1057 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN)) 1058 return -EINVAL; 1059 1060 ct_els->req = job->request_payload.sg_list; 1061 ct_els->resp = job->reply_payload.sg_list; 1062 ct_els->handler_data = job; 1063 1064 switch (bsg_request->msgcode) { 1065 case FC_BSG_RPT_ELS: 1066 case FC_BSG_HST_ELS_NOLOGIN: 1067 return zfcp_fc_exec_els_job(job, adapter); 1068 case FC_BSG_RPT_CT: 1069 case FC_BSG_HST_CT: 1070 return zfcp_fc_exec_ct_job(job, adapter); 1071 default: 1072 return -EINVAL; 1073 } 1074 } 1075 1076 int zfcp_fc_timeout_bsg_job(struct bsg_job *job) 1077 { 1078 /* hardware tracks timeout, reset bsg timeout to not interfere */ 1079 return -EAGAIN; 1080 } 1081 1082 int zfcp_fc_gs_setup(struct zfcp_adapter *adapter) 1083 { 1084 struct zfcp_fc_wka_ports *wka_ports; 1085 1086 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL); 1087 if (!wka_ports) 1088 return -ENOMEM; 1089 1090 adapter->gs = wka_ports; 1091 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter); 1092 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter); 1093 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter); 1094 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter); 1095 1096 return 0; 1097 } 1098 1099 void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter) 1100 { 1101 kfree(adapter->gs); 1102 adapter->gs = NULL; 1103 } 1104 1105