of/platform: Initialise default DMA masks
[linux/fpc-iii.git] / drivers / s390 / scsi / zfcp_fc.c
blobf6c415d6ef4875c8e856a11720c975b6575d63a7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * zfcp device driver
5 * Fibre Channel related functions for the zfcp device driver.
7 * Copyright IBM Corp. 2008, 2017
8 */
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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"
23 struct kmem_cache *zfcp_fc_req_cache;
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,
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)");
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)");
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)");
47 unsigned int zfcp_fc_port_scan_backoff(void)
49 if (!port_scan_backoff)
50 return 0;
51 return get_random_int() % port_scan_backoff;
54 static void zfcp_fc_port_scan_time(struct zfcp_adapter *adapter)
56 unsigned long interval = msecs_to_jiffies(port_scan_ratelimit);
57 unsigned long backoff = msecs_to_jiffies(zfcp_fc_port_scan_backoff());
59 adapter->next_port_scan = jiffies + interval + backoff;
62 static void zfcp_fc_port_scan(struct zfcp_adapter *adapter)
64 unsigned long now = jiffies;
65 unsigned long next = adapter->next_port_scan;
66 unsigned long delay = 0, max;
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);
76 queue_delayed_work(adapter->work_queue, &adapter->scan_work, delay);
79 void zfcp_fc_conditional_port_scan(struct zfcp_adapter *adapter)
81 if (no_auto_port_rescan)
82 return;
84 zfcp_fc_port_scan(adapter);
87 void zfcp_fc_inverse_conditional_port_scan(struct zfcp_adapter *adapter)
89 if (!no_auto_port_rescan)
90 return;
92 zfcp_fc_port_scan(adapter);
95 /**
96 * zfcp_fc_post_event - post event to userspace via fc_transport
97 * @work: work struct with enqueued events
99 void zfcp_fc_post_event(struct work_struct *work)
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);
108 spin_lock_bh(&events->list_lock);
109 list_splice_init(&events->list, &tmp_lh);
110 spin_unlock_bh(&events->list_lock);
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);
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)
127 void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
128 enum fc_host_event_code event_code, u32 event_data)
130 struct zfcp_fc_event *event;
132 event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
133 if (!event)
134 return;
136 event->code = event_code;
137 event->data = event_data;
139 spin_lock(&adapter->events.list_lock);
140 list_add_tail(&event->list, &adapter->events.list);
141 spin_unlock(&adapter->events.list_lock);
143 queue_work(adapter->work_queue, &adapter->events.work);
146 static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
148 if (mutex_lock_interruptible(&wka_port->mutex))
149 return -ERESTARTSYS;
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;
158 mutex_unlock(&wka_port->mutex);
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);
164 if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
165 atomic_inc(&wka_port->refcount);
166 return 0;
168 return -EIO;
171 static void zfcp_fc_wka_port_offline(struct work_struct *work)
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);
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;
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);
187 out:
188 mutex_unlock(&wka_port->mutex);
191 static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
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);
199 static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
200 struct zfcp_adapter *adapter)
202 init_waitqueue_head(&wka_port->completion_wq);
204 wka_port->adapter = adapter;
205 wka_port->d_id = d_id;
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);
213 static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
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);
221 void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
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);
231 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
232 struct fc_els_rscn_page *page)
234 unsigned long flags;
235 struct zfcp_adapter *adapter = fsf_req->adapter;
236 struct zfcp_port *port;
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 if (!port->d_id)
243 zfcp_erp_port_reopen(port,
244 ZFCP_STATUS_COMMON_ERP_FAILED,
245 "fcrscn1");
247 read_unlock_irqrestore(&adapter->port_list_lock, flags);
250 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
252 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
253 struct fc_els_rscn *head;
254 struct fc_els_rscn_page *page;
255 u16 i;
256 u16 no_entries;
257 unsigned int afmt;
259 head = (struct fc_els_rscn *) status_buffer->payload.data;
260 page = (struct fc_els_rscn_page *) head;
262 /* see FC-FS */
263 no_entries = be16_to_cpu(head->rscn_plen) /
264 sizeof(struct fc_els_rscn_page);
266 for (i = 1; i < no_entries; i++) {
267 /* skip head and start with 1st element */
268 page++;
269 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
270 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
271 page);
272 zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
273 *(u32 *)page);
275 zfcp_fc_conditional_port_scan(fsf_req->adapter);
278 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
280 unsigned long flags;
281 struct zfcp_adapter *adapter = req->adapter;
282 struct zfcp_port *port;
284 read_lock_irqsave(&adapter->port_list_lock, flags);
285 list_for_each_entry(port, &adapter->port_list, list)
286 if (port->wwpn == wwpn) {
287 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1");
288 break;
290 read_unlock_irqrestore(&adapter->port_list_lock, flags);
293 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
295 struct fsf_status_read_buffer *status_buffer;
296 struct fc_els_flogi *plogi;
298 status_buffer = (struct fsf_status_read_buffer *) req->data;
299 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
300 zfcp_fc_incoming_wwpn(req, be64_to_cpu(plogi->fl_wwpn));
303 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
305 struct fsf_status_read_buffer *status_buffer =
306 (struct fsf_status_read_buffer *)req->data;
307 struct fc_els_logo *logo =
308 (struct fc_els_logo *) status_buffer->payload.data;
310 zfcp_fc_incoming_wwpn(req, be64_to_cpu(logo->fl_n_port_wwn));
314 * zfcp_fc_incoming_els - handle incoming ELS
315 * @fsf_req - request which contains incoming ELS
317 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
319 struct fsf_status_read_buffer *status_buffer =
320 (struct fsf_status_read_buffer *) fsf_req->data;
321 unsigned int els_type = status_buffer->payload.data[0];
323 zfcp_dbf_san_in_els("fciels1", fsf_req);
324 if (els_type == ELS_PLOGI)
325 zfcp_fc_incoming_plogi(fsf_req);
326 else if (els_type == ELS_LOGO)
327 zfcp_fc_incoming_logo(fsf_req);
328 else if (els_type == ELS_RSCN)
329 zfcp_fc_incoming_rscn(fsf_req);
332 static void zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req *fc_req)
334 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
335 struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
337 if (ct_els->status)
338 return;
339 if (gid_pn_rsp->ct_hdr.ct_cmd != cpu_to_be16(FC_FS_ACC))
340 return;
342 /* looks like a valid d_id */
343 ct_els->port->d_id = ntoh24(gid_pn_rsp->gid_pn.fp_fid);
346 static void zfcp_fc_complete(void *data)
348 complete(data);
351 static void zfcp_fc_ct_ns_init(struct fc_ct_hdr *ct_hdr, u16 cmd, u16 mr_size)
353 ct_hdr->ct_rev = FC_CT_REV;
354 ct_hdr->ct_fs_type = FC_FST_DIR;
355 ct_hdr->ct_fs_subtype = FC_NS_SUBTYPE;
356 ct_hdr->ct_cmd = cpu_to_be16(cmd);
357 ct_hdr->ct_mr_size = cpu_to_be16(mr_size / 4);
360 static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
361 struct zfcp_fc_req *fc_req)
363 struct zfcp_adapter *adapter = port->adapter;
364 DECLARE_COMPLETION_ONSTACK(completion);
365 struct zfcp_fc_gid_pn_req *gid_pn_req = &fc_req->u.gid_pn.req;
366 struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
367 int ret;
369 /* setup parameters for send generic command */
370 fc_req->ct_els.port = port;
371 fc_req->ct_els.handler = zfcp_fc_complete;
372 fc_req->ct_els.handler_data = &completion;
373 fc_req->ct_els.req = &fc_req->sg_req;
374 fc_req->ct_els.resp = &fc_req->sg_rsp;
375 sg_init_one(&fc_req->sg_req, gid_pn_req, sizeof(*gid_pn_req));
376 sg_init_one(&fc_req->sg_rsp, gid_pn_rsp, sizeof(*gid_pn_rsp));
378 zfcp_fc_ct_ns_init(&gid_pn_req->ct_hdr,
379 FC_NS_GID_PN, ZFCP_FC_CT_SIZE_PAGE);
380 gid_pn_req->gid_pn.fn_wwpn = cpu_to_be64(port->wwpn);
382 ret = zfcp_fsf_send_ct(&adapter->gs->ds, &fc_req->ct_els,
383 adapter->pool.gid_pn_req,
384 ZFCP_FC_CTELS_TMO);
385 if (!ret) {
386 wait_for_completion(&completion);
387 zfcp_fc_ns_gid_pn_eval(fc_req);
389 return ret;
393 * zfcp_fc_ns_gid_pn - initiate GID_PN nameserver request
394 * @port: port where GID_PN request is needed
395 * return: -ENOMEM on error, 0 otherwise
397 static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
399 int ret;
400 struct zfcp_fc_req *fc_req;
401 struct zfcp_adapter *adapter = port->adapter;
403 fc_req = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
404 if (!fc_req)
405 return -ENOMEM;
407 memset(fc_req, 0, sizeof(*fc_req));
409 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
410 if (ret)
411 goto out;
413 ret = zfcp_fc_ns_gid_pn_request(port, fc_req);
415 zfcp_fc_wka_port_put(&adapter->gs->ds);
416 out:
417 mempool_free(fc_req, adapter->pool.gid_pn);
418 return ret;
421 void zfcp_fc_port_did_lookup(struct work_struct *work)
423 int ret;
424 struct zfcp_port *port = container_of(work, struct zfcp_port,
425 gid_pn_work);
427 set_worker_desc("zgidpn%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */
428 ret = zfcp_fc_ns_gid_pn(port);
429 if (ret) {
430 /* could not issue gid_pn for some reason */
431 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1");
432 goto out;
435 if (!port->d_id) {
436 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
437 goto out;
440 zfcp_erp_port_reopen(port, 0, "fcgpn_3");
441 out:
442 put_device(&port->dev);
446 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
447 * @port: The zfcp_port to lookup the d_id for.
449 void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
451 get_device(&port->dev);
452 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
453 put_device(&port->dev);
457 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
458 * @port: zfcp_port structure
459 * @plogi: plogi payload
461 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
463 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
465 if (be64_to_cpu(plogi->fl_wwpn) != port->wwpn) {
466 port->d_id = 0;
467 dev_warn(&port->adapter->ccw_device->dev,
468 "A port opened with WWPN 0x%016Lx returned data that "
469 "identifies it as WWPN 0x%016Lx\n",
470 (unsigned long long) port->wwpn,
471 (unsigned long long) be64_to_cpu(plogi->fl_wwpn));
472 return;
475 port->wwnn = be64_to_cpu(plogi->fl_wwnn);
476 port->maxframe_size = be16_to_cpu(plogi->fl_csp.sp_bb_data);
478 if (plogi->fl_cssp[0].cp_class & cpu_to_be16(FC_CPC_VALID))
479 port->supported_classes |= FC_COS_CLASS1;
480 if (plogi->fl_cssp[1].cp_class & cpu_to_be16(FC_CPC_VALID))
481 port->supported_classes |= FC_COS_CLASS2;
482 if (plogi->fl_cssp[2].cp_class & cpu_to_be16(FC_CPC_VALID))
483 port->supported_classes |= FC_COS_CLASS3;
484 if (plogi->fl_cssp[3].cp_class & cpu_to_be16(FC_CPC_VALID))
485 port->supported_classes |= FC_COS_CLASS4;
488 static void zfcp_fc_adisc_handler(void *data)
490 struct zfcp_fc_req *fc_req = data;
491 struct zfcp_port *port = fc_req->ct_els.port;
492 struct fc_els_adisc *adisc_resp = &fc_req->u.adisc.rsp;
494 if (fc_req->ct_els.status) {
495 /* request rejected or timed out */
496 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
497 "fcadh_1");
498 goto out;
501 if (!port->wwnn)
502 port->wwnn = be64_to_cpu(adisc_resp->adisc_wwnn);
504 if ((port->wwpn != be64_to_cpu(adisc_resp->adisc_wwpn)) ||
505 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
506 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
507 "fcadh_2");
508 goto out;
511 /* port is good, unblock rport without going through erp */
512 zfcp_scsi_schedule_rport_register(port);
513 out:
514 atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
515 put_device(&port->dev);
516 kmem_cache_free(zfcp_fc_req_cache, fc_req);
519 static int zfcp_fc_adisc(struct zfcp_port *port)
521 struct zfcp_fc_req *fc_req;
522 struct zfcp_adapter *adapter = port->adapter;
523 struct Scsi_Host *shost = adapter->scsi_host;
524 int ret;
526 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC);
527 if (!fc_req)
528 return -ENOMEM;
530 fc_req->ct_els.port = port;
531 fc_req->ct_els.req = &fc_req->sg_req;
532 fc_req->ct_els.resp = &fc_req->sg_rsp;
533 sg_init_one(&fc_req->sg_req, &fc_req->u.adisc.req,
534 sizeof(struct fc_els_adisc));
535 sg_init_one(&fc_req->sg_rsp, &fc_req->u.adisc.rsp,
536 sizeof(struct fc_els_adisc));
538 fc_req->ct_els.handler = zfcp_fc_adisc_handler;
539 fc_req->ct_els.handler_data = fc_req;
541 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
542 without FC-AL-2 capability, so we don't set it */
543 fc_req->u.adisc.req.adisc_wwpn = cpu_to_be64(fc_host_port_name(shost));
544 fc_req->u.adisc.req.adisc_wwnn = cpu_to_be64(fc_host_node_name(shost));
545 fc_req->u.adisc.req.adisc_cmd = ELS_ADISC;
546 hton24(fc_req->u.adisc.req.adisc_port_id, fc_host_port_id(shost));
548 ret = zfcp_fsf_send_els(adapter, port->d_id, &fc_req->ct_els,
549 ZFCP_FC_CTELS_TMO);
550 if (ret)
551 kmem_cache_free(zfcp_fc_req_cache, fc_req);
553 return ret;
556 void zfcp_fc_link_test_work(struct work_struct *work)
558 struct zfcp_port *port =
559 container_of(work, struct zfcp_port, test_link_work);
560 int retval;
562 set_worker_desc("zadisc%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */
563 get_device(&port->dev);
564 port->rport_task = RPORT_DEL;
565 zfcp_scsi_rport_work(&port->rport_work);
567 /* only issue one test command at one time per port */
568 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
569 goto out;
571 atomic_or(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
573 retval = zfcp_fc_adisc(port);
574 if (retval == 0)
575 return;
577 /* send of ADISC was not possible */
578 atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
579 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1");
581 out:
582 put_device(&port->dev);
586 * zfcp_fc_test_link - lightweight link test procedure
587 * @port: port to be tested
589 * Test status of a link to a remote port using the ELS command ADISC.
590 * If there is a problem with the remote port, error recovery steps
591 * will be triggered.
593 void zfcp_fc_test_link(struct zfcp_port *port)
595 get_device(&port->dev);
596 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
597 put_device(&port->dev);
600 static struct zfcp_fc_req *zfcp_fc_alloc_sg_env(int buf_num)
602 struct zfcp_fc_req *fc_req;
604 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
605 if (!fc_req)
606 return NULL;
608 if (zfcp_sg_setup_table(&fc_req->sg_rsp, buf_num)) {
609 kmem_cache_free(zfcp_fc_req_cache, fc_req);
610 return NULL;
613 sg_init_one(&fc_req->sg_req, &fc_req->u.gpn_ft.req,
614 sizeof(struct zfcp_fc_gpn_ft_req));
616 return fc_req;
619 static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req,
620 struct zfcp_adapter *adapter, int max_bytes)
622 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
623 struct zfcp_fc_gpn_ft_req *req = &fc_req->u.gpn_ft.req;
624 DECLARE_COMPLETION_ONSTACK(completion);
625 int ret;
627 zfcp_fc_ct_ns_init(&req->ct_hdr, FC_NS_GPN_FT, max_bytes);
628 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
630 ct_els->handler = zfcp_fc_complete;
631 ct_els->handler_data = &completion;
632 ct_els->req = &fc_req->sg_req;
633 ct_els->resp = &fc_req->sg_rsp;
635 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
636 ZFCP_FC_CTELS_TMO);
637 if (!ret)
638 wait_for_completion(&completion);
639 return ret;
642 static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
644 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
645 return;
647 atomic_andnot(ZFCP_STATUS_COMMON_NOESC, &port->status);
649 if ((port->supported_classes != 0) ||
650 !list_empty(&port->unit_list))
651 return;
653 list_move_tail(&port->list, lh);
656 static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_req *fc_req,
657 struct zfcp_adapter *adapter, int max_entries)
659 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
660 struct scatterlist *sg = &fc_req->sg_rsp;
661 struct fc_ct_hdr *hdr = sg_virt(sg);
662 struct fc_gpn_ft_resp *acc = sg_virt(sg);
663 struct zfcp_port *port, *tmp;
664 unsigned long flags;
665 LIST_HEAD(remove_lh);
666 u32 d_id;
667 int ret = 0, x, last = 0;
669 if (ct_els->status)
670 return -EIO;
672 if (hdr->ct_cmd != cpu_to_be16(FC_FS_ACC)) {
673 if (hdr->ct_reason == FC_FS_RJT_UNABL)
674 return -EAGAIN; /* might be a temporary condition */
675 return -EIO;
678 if (hdr->ct_mr_size) {
679 dev_warn(&adapter->ccw_device->dev,
680 "The name server reported %d words residual data\n",
681 hdr->ct_mr_size);
682 return -E2BIG;
685 /* first entry is the header */
686 for (x = 1; x < max_entries && !last; x++) {
687 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
688 acc++;
689 else
690 acc = sg_virt(++sg);
692 last = acc->fp_flags & FC_NS_FID_LAST;
693 d_id = ntoh24(acc->fp_fid);
695 /* don't attach ports with a well known address */
696 if (d_id >= FC_FID_WELL_KNOWN_BASE)
697 continue;
698 /* skip the adapter's port and known remote ports */
699 if (be64_to_cpu(acc->fp_wwpn) ==
700 fc_host_port_name(adapter->scsi_host))
701 continue;
703 port = zfcp_port_enqueue(adapter, be64_to_cpu(acc->fp_wwpn),
704 ZFCP_STATUS_COMMON_NOESC, d_id);
705 if (!IS_ERR(port))
706 zfcp_erp_port_reopen(port, 0, "fcegpf1");
707 else if (PTR_ERR(port) != -EEXIST)
708 ret = PTR_ERR(port);
711 zfcp_erp_wait(adapter);
712 write_lock_irqsave(&adapter->port_list_lock, flags);
713 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
714 zfcp_fc_validate_port(port, &remove_lh);
715 write_unlock_irqrestore(&adapter->port_list_lock, flags);
717 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
718 zfcp_erp_port_shutdown(port, 0, "fcegpf2");
719 device_unregister(&port->dev);
722 return ret;
726 * zfcp_fc_scan_ports - scan remote ports and attach new ports
727 * @work: reference to scheduled work
729 void zfcp_fc_scan_ports(struct work_struct *work)
731 struct delayed_work *dw = to_delayed_work(work);
732 struct zfcp_adapter *adapter = container_of(dw, struct zfcp_adapter,
733 scan_work);
734 int ret, i;
735 struct zfcp_fc_req *fc_req;
736 int chain, max_entries, buf_num, max_bytes;
738 zfcp_fc_port_scan_time(adapter);
740 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
741 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
742 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
743 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
745 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
746 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
747 return;
749 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
750 return;
752 fc_req = zfcp_fc_alloc_sg_env(buf_num);
753 if (!fc_req)
754 goto out;
756 for (i = 0; i < 3; i++) {
757 ret = zfcp_fc_send_gpn_ft(fc_req, adapter, max_bytes);
758 if (!ret) {
759 ret = zfcp_fc_eval_gpn_ft(fc_req, adapter, max_entries);
760 if (ret == -EAGAIN)
761 ssleep(1);
762 else
763 break;
766 zfcp_sg_free_table(&fc_req->sg_rsp, buf_num);
767 kmem_cache_free(zfcp_fc_req_cache, fc_req);
768 out:
769 zfcp_fc_wka_port_put(&adapter->gs->ds);
772 static int zfcp_fc_gspn(struct zfcp_adapter *adapter,
773 struct zfcp_fc_req *fc_req)
775 DECLARE_COMPLETION_ONSTACK(completion);
776 char devno[] = "DEVNO:";
777 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
778 struct zfcp_fc_gspn_req *gspn_req = &fc_req->u.gspn.req;
779 struct zfcp_fc_gspn_rsp *gspn_rsp = &fc_req->u.gspn.rsp;
780 int ret;
782 zfcp_fc_ct_ns_init(&gspn_req->ct_hdr, FC_NS_GSPN_ID,
783 FC_SYMBOLIC_NAME_SIZE);
784 hton24(gspn_req->gspn.fp_fid, fc_host_port_id(adapter->scsi_host));
786 sg_init_one(&fc_req->sg_req, gspn_req, sizeof(*gspn_req));
787 sg_init_one(&fc_req->sg_rsp, gspn_rsp, sizeof(*gspn_rsp));
789 ct_els->handler = zfcp_fc_complete;
790 ct_els->handler_data = &completion;
791 ct_els->req = &fc_req->sg_req;
792 ct_els->resp = &fc_req->sg_rsp;
794 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
795 ZFCP_FC_CTELS_TMO);
796 if (ret)
797 return ret;
799 wait_for_completion(&completion);
800 if (ct_els->status)
801 return ct_els->status;
803 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_NPIV &&
804 !(strstr(gspn_rsp->gspn.fp_name, devno)))
805 snprintf(fc_host_symbolic_name(adapter->scsi_host),
806 FC_SYMBOLIC_NAME_SIZE, "%s%s %s NAME: %s",
807 gspn_rsp->gspn.fp_name, devno,
808 dev_name(&adapter->ccw_device->dev),
809 init_utsname()->nodename);
810 else
811 strlcpy(fc_host_symbolic_name(adapter->scsi_host),
812 gspn_rsp->gspn.fp_name, FC_SYMBOLIC_NAME_SIZE);
814 return 0;
817 static void zfcp_fc_rspn(struct zfcp_adapter *adapter,
818 struct zfcp_fc_req *fc_req)
820 DECLARE_COMPLETION_ONSTACK(completion);
821 struct Scsi_Host *shost = adapter->scsi_host;
822 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
823 struct zfcp_fc_rspn_req *rspn_req = &fc_req->u.rspn.req;
824 struct fc_ct_hdr *rspn_rsp = &fc_req->u.rspn.rsp;
825 int ret, len;
827 zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID,
828 FC_SYMBOLIC_NAME_SIZE);
829 hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost));
830 len = strlcpy(rspn_req->rspn.fr_name, fc_host_symbolic_name(shost),
831 FC_SYMBOLIC_NAME_SIZE);
832 rspn_req->rspn.fr_name_len = len;
834 sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req));
835 sg_init_one(&fc_req->sg_rsp, rspn_rsp, sizeof(*rspn_rsp));
837 ct_els->handler = zfcp_fc_complete;
838 ct_els->handler_data = &completion;
839 ct_els->req = &fc_req->sg_req;
840 ct_els->resp = &fc_req->sg_rsp;
842 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
843 ZFCP_FC_CTELS_TMO);
844 if (!ret)
845 wait_for_completion(&completion);
849 * zfcp_fc_sym_name_update - Retrieve and update the symbolic port name
850 * @work: ns_up_work of the adapter where to update the symbolic port name
852 * Retrieve the current symbolic port name that may have been set by
853 * the hardware using the GSPN request and update the fc_host
854 * symbolic_name sysfs attribute. When running in NPIV mode (and hence
855 * the port name is unique for this system), update the symbolic port
856 * name to add Linux specific information and update the FC nameserver
857 * using the RSPN request.
859 void zfcp_fc_sym_name_update(struct work_struct *work)
861 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
862 ns_up_work);
863 int ret;
864 struct zfcp_fc_req *fc_req;
866 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
867 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
868 return;
870 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
871 if (!fc_req)
872 return;
874 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
875 if (ret)
876 goto out_free;
878 ret = zfcp_fc_gspn(adapter, fc_req);
879 if (ret || fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
880 goto out_ds_put;
882 memset(fc_req, 0, sizeof(*fc_req));
883 zfcp_fc_rspn(adapter, fc_req);
885 out_ds_put:
886 zfcp_fc_wka_port_put(&adapter->gs->ds);
887 out_free:
888 kmem_cache_free(zfcp_fc_req_cache, fc_req);
891 static void zfcp_fc_ct_els_job_handler(void *data)
893 struct bsg_job *job = data;
894 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
895 struct fc_bsg_reply *jr = job->reply;
897 jr->reply_payload_rcv_len = job->reply_payload.payload_len;
898 jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
899 jr->result = zfcp_ct_els->status ? -EIO : 0;
900 bsg_job_done(job, jr->result, jr->reply_payload_rcv_len);
903 static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct bsg_job *job)
905 u32 preamble_word1;
906 u8 gs_type;
907 struct zfcp_adapter *adapter;
908 struct fc_bsg_request *bsg_request = job->request;
909 struct fc_rport *rport = fc_bsg_to_rport(job);
910 struct Scsi_Host *shost;
912 preamble_word1 = bsg_request->rqst_data.r_ct.preamble_word1;
913 gs_type = (preamble_word1 & 0xff000000) >> 24;
915 shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
916 adapter = (struct zfcp_adapter *) shost->hostdata[0];
918 switch (gs_type) {
919 case FC_FST_ALIAS:
920 return &adapter->gs->as;
921 case FC_FST_MGMT:
922 return &adapter->gs->ms;
923 case FC_FST_TIME:
924 return &adapter->gs->ts;
925 break;
926 case FC_FST_DIR:
927 return &adapter->gs->ds;
928 break;
929 default:
930 return NULL;
934 static void zfcp_fc_ct_job_handler(void *data)
936 struct bsg_job *job = data;
937 struct zfcp_fc_wka_port *wka_port;
939 wka_port = zfcp_fc_job_wka_port(job);
940 zfcp_fc_wka_port_put(wka_port);
942 zfcp_fc_ct_els_job_handler(data);
945 static int zfcp_fc_exec_els_job(struct bsg_job *job,
946 struct zfcp_adapter *adapter)
948 struct zfcp_fsf_ct_els *els = job->dd_data;
949 struct fc_rport *rport = fc_bsg_to_rport(job);
950 struct fc_bsg_request *bsg_request = job->request;
951 struct zfcp_port *port;
952 u32 d_id;
954 if (rport) {
955 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
956 if (!port)
957 return -EINVAL;
959 d_id = port->d_id;
960 put_device(&port->dev);
961 } else
962 d_id = ntoh24(bsg_request->rqst_data.h_els.port_id);
964 els->handler = zfcp_fc_ct_els_job_handler;
965 return zfcp_fsf_send_els(adapter, d_id, els, job->timeout / HZ);
968 static int zfcp_fc_exec_ct_job(struct bsg_job *job,
969 struct zfcp_adapter *adapter)
971 int ret;
972 struct zfcp_fsf_ct_els *ct = job->dd_data;
973 struct zfcp_fc_wka_port *wka_port;
975 wka_port = zfcp_fc_job_wka_port(job);
976 if (!wka_port)
977 return -EINVAL;
979 ret = zfcp_fc_wka_port_get(wka_port);
980 if (ret)
981 return ret;
983 ct->handler = zfcp_fc_ct_job_handler;
984 ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->timeout / HZ);
985 if (ret)
986 zfcp_fc_wka_port_put(wka_port);
988 return ret;
991 int zfcp_fc_exec_bsg_job(struct bsg_job *job)
993 struct Scsi_Host *shost;
994 struct zfcp_adapter *adapter;
995 struct zfcp_fsf_ct_els *ct_els = job->dd_data;
996 struct fc_bsg_request *bsg_request = job->request;
997 struct fc_rport *rport = fc_bsg_to_rport(job);
999 shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
1000 adapter = (struct zfcp_adapter *)shost->hostdata[0];
1002 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
1003 return -EINVAL;
1005 ct_els->req = job->request_payload.sg_list;
1006 ct_els->resp = job->reply_payload.sg_list;
1007 ct_els->handler_data = job;
1009 switch (bsg_request->msgcode) {
1010 case FC_BSG_RPT_ELS:
1011 case FC_BSG_HST_ELS_NOLOGIN:
1012 return zfcp_fc_exec_els_job(job, adapter);
1013 case FC_BSG_RPT_CT:
1014 case FC_BSG_HST_CT:
1015 return zfcp_fc_exec_ct_job(job, adapter);
1016 default:
1017 return -EINVAL;
1021 int zfcp_fc_timeout_bsg_job(struct bsg_job *job)
1023 /* hardware tracks timeout, reset bsg timeout to not interfere */
1024 return -EAGAIN;
1027 int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
1029 struct zfcp_fc_wka_ports *wka_ports;
1031 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
1032 if (!wka_ports)
1033 return -ENOMEM;
1035 adapter->gs = wka_ports;
1036 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
1037 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
1038 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
1039 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
1041 return 0;
1044 void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
1046 kfree(adapter->gs);
1047 adapter->gs = NULL;