xref: /openbmc/linux/drivers/s390/scsi/zfcp_fc.c (revision e3b9f1e8)
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 get_random_int() % 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 /**
122  * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context
123  * @adapter: The adapter where to enqueue the event
124  * @event_code: The event code (as defined in fc_host_event_code in
125  *		scsi_transport_fc.h)
126  * @event_data: The event data (e.g. n_port page in case of els)
127  */
128 void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
129 			enum fc_host_event_code event_code, u32 event_data)
130 {
131 	struct zfcp_fc_event *event;
132 
133 	event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
134 	if (!event)
135 		return;
136 
137 	event->code = event_code;
138 	event->data = event_data;
139 
140 	spin_lock(&adapter->events.list_lock);
141 	list_add_tail(&event->list, &adapter->events.list);
142 	spin_unlock(&adapter->events.list_lock);
143 
144 	queue_work(adapter->work_queue, &adapter->events.work);
145 }
146 
147 static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
148 {
149 	if (mutex_lock_interruptible(&wka_port->mutex))
150 		return -ERESTARTSYS;
151 
152 	if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
153 	    wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
154 		wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
155 		if (zfcp_fsf_open_wka_port(wka_port))
156 			wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
157 	}
158 
159 	mutex_unlock(&wka_port->mutex);
160 
161 	wait_event(wka_port->completion_wq,
162 		   wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
163 		   wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
164 
165 	if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
166 		atomic_inc(&wka_port->refcount);
167 		return 0;
168 	}
169 	return -EIO;
170 }
171 
172 static void zfcp_fc_wka_port_offline(struct work_struct *work)
173 {
174 	struct delayed_work *dw = to_delayed_work(work);
175 	struct zfcp_fc_wka_port *wka_port =
176 			container_of(dw, struct zfcp_fc_wka_port, work);
177 
178 	mutex_lock(&wka_port->mutex);
179 	if ((atomic_read(&wka_port->refcount) != 0) ||
180 	    (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
181 		goto out;
182 
183 	wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
184 	if (zfcp_fsf_close_wka_port(wka_port)) {
185 		wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
186 		wake_up(&wka_port->completion_wq);
187 	}
188 out:
189 	mutex_unlock(&wka_port->mutex);
190 }
191 
192 static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
193 {
194 	if (atomic_dec_return(&wka_port->refcount) != 0)
195 		return;
196 	/* wait 10 milliseconds, other reqs might pop in */
197 	schedule_delayed_work(&wka_port->work, HZ / 100);
198 }
199 
200 static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
201 				  struct zfcp_adapter *adapter)
202 {
203 	init_waitqueue_head(&wka_port->completion_wq);
204 
205 	wka_port->adapter = adapter;
206 	wka_port->d_id = d_id;
207 
208 	wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
209 	atomic_set(&wka_port->refcount, 0);
210 	mutex_init(&wka_port->mutex);
211 	INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
212 }
213 
214 static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
215 {
216 	cancel_delayed_work_sync(&wka->work);
217 	mutex_lock(&wka->mutex);
218 	wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
219 	mutex_unlock(&wka->mutex);
220 }
221 
222 void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
223 {
224 	if (!gs)
225 		return;
226 	zfcp_fc_wka_port_force_offline(&gs->ms);
227 	zfcp_fc_wka_port_force_offline(&gs->ts);
228 	zfcp_fc_wka_port_force_offline(&gs->ds);
229 	zfcp_fc_wka_port_force_offline(&gs->as);
230 }
231 
232 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
233 				   struct fc_els_rscn_page *page)
234 {
235 	unsigned long flags;
236 	struct zfcp_adapter *adapter = fsf_req->adapter;
237 	struct zfcp_port *port;
238 
239 	read_lock_irqsave(&adapter->port_list_lock, flags);
240 	list_for_each_entry(port, &adapter->port_list, list) {
241 		if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
242 			zfcp_fc_test_link(port);
243 		if (!port->d_id)
244 			zfcp_erp_port_reopen(port,
245 					     ZFCP_STATUS_COMMON_ERP_FAILED,
246 					     "fcrscn1");
247 	}
248 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
249 }
250 
251 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
252 {
253 	struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
254 	struct fc_els_rscn *head;
255 	struct fc_els_rscn_page *page;
256 	u16 i;
257 	u16 no_entries;
258 	unsigned int afmt;
259 
260 	head = (struct fc_els_rscn *) status_buffer->payload.data;
261 	page = (struct fc_els_rscn_page *) head;
262 
263 	/* see FC-FS */
264 	no_entries = be16_to_cpu(head->rscn_plen) /
265 		sizeof(struct fc_els_rscn_page);
266 
267 	for (i = 1; i < no_entries; i++) {
268 		/* skip head and start with 1st element */
269 		page++;
270 		afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
271 		_zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
272 				       page);
273 		zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
274 				      *(u32 *)page);
275 	}
276 	zfcp_fc_conditional_port_scan(fsf_req->adapter);
277 }
278 
279 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
280 {
281 	unsigned long flags;
282 	struct zfcp_adapter *adapter = req->adapter;
283 	struct zfcp_port *port;
284 
285 	read_lock_irqsave(&adapter->port_list_lock, flags);
286 	list_for_each_entry(port, &adapter->port_list, list)
287 		if (port->wwpn == wwpn) {
288 			zfcp_erp_port_forced_reopen(port, 0, "fciwwp1");
289 			break;
290 		}
291 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
292 }
293 
294 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
295 {
296 	struct fsf_status_read_buffer *status_buffer;
297 	struct fc_els_flogi *plogi;
298 
299 	status_buffer = (struct fsf_status_read_buffer *) req->data;
300 	plogi = (struct fc_els_flogi *) status_buffer->payload.data;
301 	zfcp_fc_incoming_wwpn(req, be64_to_cpu(plogi->fl_wwpn));
302 }
303 
304 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
305 {
306 	struct fsf_status_read_buffer *status_buffer =
307 		(struct fsf_status_read_buffer *)req->data;
308 	struct fc_els_logo *logo =
309 		(struct fc_els_logo *) status_buffer->payload.data;
310 
311 	zfcp_fc_incoming_wwpn(req, be64_to_cpu(logo->fl_n_port_wwn));
312 }
313 
314 /**
315  * zfcp_fc_incoming_els - handle incoming ELS
316  * @fsf_req - request which contains incoming ELS
317  */
318 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
319 {
320 	struct fsf_status_read_buffer *status_buffer =
321 		(struct fsf_status_read_buffer *) fsf_req->data;
322 	unsigned int els_type = status_buffer->payload.data[0];
323 
324 	zfcp_dbf_san_in_els("fciels1", fsf_req);
325 	if (els_type == ELS_PLOGI)
326 		zfcp_fc_incoming_plogi(fsf_req);
327 	else if (els_type == ELS_LOGO)
328 		zfcp_fc_incoming_logo(fsf_req);
329 	else if (els_type == ELS_RSCN)
330 		zfcp_fc_incoming_rscn(fsf_req);
331 }
332 
333 static void zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req *fc_req)
334 {
335 	struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
336 	struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
337 
338 	if (ct_els->status)
339 		return;
340 	if (gid_pn_rsp->ct_hdr.ct_cmd != cpu_to_be16(FC_FS_ACC))
341 		return;
342 
343 	/* looks like a valid d_id */
344 	ct_els->port->d_id = ntoh24(gid_pn_rsp->gid_pn.fp_fid);
345 }
346 
347 static void zfcp_fc_complete(void *data)
348 {
349 	complete(data);
350 }
351 
352 static void zfcp_fc_ct_ns_init(struct fc_ct_hdr *ct_hdr, u16 cmd, u16 mr_size)
353 {
354 	ct_hdr->ct_rev = FC_CT_REV;
355 	ct_hdr->ct_fs_type = FC_FST_DIR;
356 	ct_hdr->ct_fs_subtype = FC_NS_SUBTYPE;
357 	ct_hdr->ct_cmd = cpu_to_be16(cmd);
358 	ct_hdr->ct_mr_size = cpu_to_be16(mr_size / 4);
359 }
360 
361 static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
362 				     struct zfcp_fc_req *fc_req)
363 {
364 	struct zfcp_adapter *adapter = port->adapter;
365 	DECLARE_COMPLETION_ONSTACK(completion);
366 	struct zfcp_fc_gid_pn_req *gid_pn_req = &fc_req->u.gid_pn.req;
367 	struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
368 	int ret;
369 
370 	/* setup parameters for send generic command */
371 	fc_req->ct_els.port = port;
372 	fc_req->ct_els.handler = zfcp_fc_complete;
373 	fc_req->ct_els.handler_data = &completion;
374 	fc_req->ct_els.req = &fc_req->sg_req;
375 	fc_req->ct_els.resp = &fc_req->sg_rsp;
376 	sg_init_one(&fc_req->sg_req, gid_pn_req, sizeof(*gid_pn_req));
377 	sg_init_one(&fc_req->sg_rsp, gid_pn_rsp, sizeof(*gid_pn_rsp));
378 
379 	zfcp_fc_ct_ns_init(&gid_pn_req->ct_hdr,
380 			   FC_NS_GID_PN, ZFCP_FC_CT_SIZE_PAGE);
381 	gid_pn_req->gid_pn.fn_wwpn = cpu_to_be64(port->wwpn);
382 
383 	ret = zfcp_fsf_send_ct(&adapter->gs->ds, &fc_req->ct_els,
384 			       adapter->pool.gid_pn_req,
385 			       ZFCP_FC_CTELS_TMO);
386 	if (!ret) {
387 		wait_for_completion(&completion);
388 		zfcp_fc_ns_gid_pn_eval(fc_req);
389 	}
390 	return ret;
391 }
392 
393 /**
394  * zfcp_fc_ns_gid_pn - initiate GID_PN nameserver request
395  * @port: port where GID_PN request is needed
396  * return: -ENOMEM on error, 0 otherwise
397  */
398 static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
399 {
400 	int ret;
401 	struct zfcp_fc_req *fc_req;
402 	struct zfcp_adapter *adapter = port->adapter;
403 
404 	fc_req = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
405 	if (!fc_req)
406 		return -ENOMEM;
407 
408 	memset(fc_req, 0, sizeof(*fc_req));
409 
410 	ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
411 	if (ret)
412 		goto out;
413 
414 	ret = zfcp_fc_ns_gid_pn_request(port, fc_req);
415 
416 	zfcp_fc_wka_port_put(&adapter->gs->ds);
417 out:
418 	mempool_free(fc_req, adapter->pool.gid_pn);
419 	return ret;
420 }
421 
422 void zfcp_fc_port_did_lookup(struct work_struct *work)
423 {
424 	int ret;
425 	struct zfcp_port *port = container_of(work, struct zfcp_port,
426 					      gid_pn_work);
427 
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;
433 	}
434 
435 	if (!port->d_id) {
436 		zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
437 		goto out;
438 	}
439 
440 	zfcp_erp_port_reopen(port, 0, "fcgpn_3");
441 out:
442 	put_device(&port->dev);
443 }
444 
445 /**
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.
448  */
449 void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
450 {
451 	get_device(&port->dev);
452 	if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
453 		put_device(&port->dev);
454 }
455 
456 /**
457  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
458  * @port: zfcp_port structure
459  * @plogi: plogi payload
460  *
461  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
462  */
463 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
464 {
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;
473 	}
474 
475 	port->wwnn = be64_to_cpu(plogi->fl_wwnn);
476 	port->maxframe_size = be16_to_cpu(plogi->fl_csp.sp_bb_data);
477 
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;
486 }
487 
488 static void zfcp_fc_adisc_handler(void *data)
489 {
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;
493 
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;
499 	}
500 
501 	if (!port->wwnn)
502 		port->wwnn = be64_to_cpu(adisc_resp->adisc_wwnn);
503 
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;
509 	}
510 
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);
517 }
518 
519 static int zfcp_fc_adisc(struct zfcp_port *port)
520 {
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;
525 
526 	fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC);
527 	if (!fc_req)
528 		return -ENOMEM;
529 
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));
537 
538 	fc_req->ct_els.handler = zfcp_fc_adisc_handler;
539 	fc_req->ct_els.handler_data = fc_req;
540 
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));
547 
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);
552 
553 	return ret;
554 }
555 
556 void zfcp_fc_link_test_work(struct work_struct *work)
557 {
558 	struct zfcp_port *port =
559 		container_of(work, struct zfcp_port, test_link_work);
560 	int retval;
561 
562 	get_device(&port->dev);
563 	port->rport_task = RPORT_DEL;
564 	zfcp_scsi_rport_work(&port->rport_work);
565 
566 	/* only issue one test command at one time per port */
567 	if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
568 		goto out;
569 
570 	atomic_or(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
571 
572 	retval = zfcp_fc_adisc(port);
573 	if (retval == 0)
574 		return;
575 
576 	/* send of ADISC was not possible */
577 	atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
578 	zfcp_erp_port_forced_reopen(port, 0, "fcltwk1");
579 
580 out:
581 	put_device(&port->dev);
582 }
583 
584 /**
585  * zfcp_fc_test_link - lightweight link test procedure
586  * @port: port to be tested
587  *
588  * Test status of a link to a remote port using the ELS command ADISC.
589  * If there is a problem with the remote port, error recovery steps
590  * will be triggered.
591  */
592 void zfcp_fc_test_link(struct zfcp_port *port)
593 {
594 	get_device(&port->dev);
595 	if (!queue_work(port->adapter->work_queue, &port->test_link_work))
596 		put_device(&port->dev);
597 }
598 
599 static struct zfcp_fc_req *zfcp_alloc_sg_env(int buf_num)
600 {
601 	struct zfcp_fc_req *fc_req;
602 
603 	fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
604 	if (!fc_req)
605 		return NULL;
606 
607 	if (zfcp_sg_setup_table(&fc_req->sg_rsp, buf_num)) {
608 		kmem_cache_free(zfcp_fc_req_cache, fc_req);
609 		return NULL;
610 	}
611 
612 	sg_init_one(&fc_req->sg_req, &fc_req->u.gpn_ft.req,
613 		    sizeof(struct zfcp_fc_gpn_ft_req));
614 
615 	return fc_req;
616 }
617 
618 static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req,
619 			       struct zfcp_adapter *adapter, int max_bytes)
620 {
621 	struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
622 	struct zfcp_fc_gpn_ft_req *req = &fc_req->u.gpn_ft.req;
623 	DECLARE_COMPLETION_ONSTACK(completion);
624 	int ret;
625 
626 	zfcp_fc_ct_ns_init(&req->ct_hdr, FC_NS_GPN_FT, max_bytes);
627 	req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
628 
629 	ct_els->handler = zfcp_fc_complete;
630 	ct_els->handler_data = &completion;
631 	ct_els->req = &fc_req->sg_req;
632 	ct_els->resp = &fc_req->sg_rsp;
633 
634 	ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
635 			       ZFCP_FC_CTELS_TMO);
636 	if (!ret)
637 		wait_for_completion(&completion);
638 	return ret;
639 }
640 
641 static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
642 {
643 	if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
644 		return;
645 
646 	atomic_andnot(ZFCP_STATUS_COMMON_NOESC, &port->status);
647 
648 	if ((port->supported_classes != 0) ||
649 	    !list_empty(&port->unit_list))
650 		return;
651 
652 	list_move_tail(&port->list, lh);
653 }
654 
655 static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_req *fc_req,
656 			       struct zfcp_adapter *adapter, int max_entries)
657 {
658 	struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
659 	struct scatterlist *sg = &fc_req->sg_rsp;
660 	struct fc_ct_hdr *hdr = sg_virt(sg);
661 	struct fc_gpn_ft_resp *acc = sg_virt(sg);
662 	struct zfcp_port *port, *tmp;
663 	unsigned long flags;
664 	LIST_HEAD(remove_lh);
665 	u32 d_id;
666 	int ret = 0, x, last = 0;
667 
668 	if (ct_els->status)
669 		return -EIO;
670 
671 	if (hdr->ct_cmd != cpu_to_be16(FC_FS_ACC)) {
672 		if (hdr->ct_reason == FC_FS_RJT_UNABL)
673 			return -EAGAIN; /* might be a temporary condition */
674 		return -EIO;
675 	}
676 
677 	if (hdr->ct_mr_size) {
678 		dev_warn(&adapter->ccw_device->dev,
679 			 "The name server reported %d words residual data\n",
680 			 hdr->ct_mr_size);
681 		return -E2BIG;
682 	}
683 
684 	/* first entry is the header */
685 	for (x = 1; x < max_entries && !last; x++) {
686 		if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
687 			acc++;
688 		else
689 			acc = sg_virt(++sg);
690 
691 		last = acc->fp_flags & FC_NS_FID_LAST;
692 		d_id = ntoh24(acc->fp_fid);
693 
694 		/* don't attach ports with a well known address */
695 		if (d_id >= FC_FID_WELL_KNOWN_BASE)
696 			continue;
697 		/* skip the adapter's port and known remote ports */
698 		if (be64_to_cpu(acc->fp_wwpn) ==
699 		    fc_host_port_name(adapter->scsi_host))
700 			continue;
701 
702 		port = zfcp_port_enqueue(adapter, be64_to_cpu(acc->fp_wwpn),
703 					 ZFCP_STATUS_COMMON_NOESC, d_id);
704 		if (!IS_ERR(port))
705 			zfcp_erp_port_reopen(port, 0, "fcegpf1");
706 		else if (PTR_ERR(port) != -EEXIST)
707 			ret = PTR_ERR(port);
708 	}
709 
710 	zfcp_erp_wait(adapter);
711 	write_lock_irqsave(&adapter->port_list_lock, flags);
712 	list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
713 		zfcp_fc_validate_port(port, &remove_lh);
714 	write_unlock_irqrestore(&adapter->port_list_lock, flags);
715 
716 	list_for_each_entry_safe(port, tmp, &remove_lh, list) {
717 		zfcp_erp_port_shutdown(port, 0, "fcegpf2");
718 		device_unregister(&port->dev);
719 	}
720 
721 	return ret;
722 }
723 
724 /**
725  * zfcp_fc_scan_ports - scan remote ports and attach new ports
726  * @work: reference to scheduled work
727  */
728 void zfcp_fc_scan_ports(struct work_struct *work)
729 {
730 	struct delayed_work *dw = to_delayed_work(work);
731 	struct zfcp_adapter *adapter = container_of(dw, struct zfcp_adapter,
732 						    scan_work);
733 	int ret, i;
734 	struct zfcp_fc_req *fc_req;
735 	int chain, max_entries, buf_num, max_bytes;
736 
737 	zfcp_fc_port_scan_time(adapter);
738 
739 	chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
740 	buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
741 	max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
742 	max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
743 
744 	if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
745 	    fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
746 		return;
747 
748 	if (zfcp_fc_wka_port_get(&adapter->gs->ds))
749 		return;
750 
751 	fc_req = zfcp_alloc_sg_env(buf_num);
752 	if (!fc_req)
753 		goto out;
754 
755 	for (i = 0; i < 3; i++) {
756 		ret = zfcp_fc_send_gpn_ft(fc_req, adapter, max_bytes);
757 		if (!ret) {
758 			ret = zfcp_fc_eval_gpn_ft(fc_req, adapter, max_entries);
759 			if (ret == -EAGAIN)
760 				ssleep(1);
761 			else
762 				break;
763 		}
764 	}
765 	zfcp_sg_free_table(&fc_req->sg_rsp, buf_num);
766 	kmem_cache_free(zfcp_fc_req_cache, fc_req);
767 out:
768 	zfcp_fc_wka_port_put(&adapter->gs->ds);
769 }
770 
771 static int zfcp_fc_gspn(struct zfcp_adapter *adapter,
772 			struct zfcp_fc_req *fc_req)
773 {
774 	DECLARE_COMPLETION_ONSTACK(completion);
775 	char devno[] = "DEVNO:";
776 	struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
777 	struct zfcp_fc_gspn_req *gspn_req = &fc_req->u.gspn.req;
778 	struct zfcp_fc_gspn_rsp *gspn_rsp = &fc_req->u.gspn.rsp;
779 	int ret;
780 
781 	zfcp_fc_ct_ns_init(&gspn_req->ct_hdr, FC_NS_GSPN_ID,
782 			   FC_SYMBOLIC_NAME_SIZE);
783 	hton24(gspn_req->gspn.fp_fid, fc_host_port_id(adapter->scsi_host));
784 
785 	sg_init_one(&fc_req->sg_req, gspn_req, sizeof(*gspn_req));
786 	sg_init_one(&fc_req->sg_rsp, gspn_rsp, sizeof(*gspn_rsp));
787 
788 	ct_els->handler = zfcp_fc_complete;
789 	ct_els->handler_data = &completion;
790 	ct_els->req = &fc_req->sg_req;
791 	ct_els->resp = &fc_req->sg_rsp;
792 
793 	ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
794 			       ZFCP_FC_CTELS_TMO);
795 	if (ret)
796 		return ret;
797 
798 	wait_for_completion(&completion);
799 	if (ct_els->status)
800 		return ct_els->status;
801 
802 	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_NPIV &&
803 	    !(strstr(gspn_rsp->gspn.fp_name, devno)))
804 		snprintf(fc_host_symbolic_name(adapter->scsi_host),
805 			 FC_SYMBOLIC_NAME_SIZE, "%s%s %s NAME: %s",
806 			 gspn_rsp->gspn.fp_name, devno,
807 			 dev_name(&adapter->ccw_device->dev),
808 			 init_utsname()->nodename);
809 	else
810 		strlcpy(fc_host_symbolic_name(adapter->scsi_host),
811 			gspn_rsp->gspn.fp_name, FC_SYMBOLIC_NAME_SIZE);
812 
813 	return 0;
814 }
815 
816 static void zfcp_fc_rspn(struct zfcp_adapter *adapter,
817 			 struct zfcp_fc_req *fc_req)
818 {
819 	DECLARE_COMPLETION_ONSTACK(completion);
820 	struct Scsi_Host *shost = adapter->scsi_host;
821 	struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
822 	struct zfcp_fc_rspn_req *rspn_req = &fc_req->u.rspn.req;
823 	struct fc_ct_hdr *rspn_rsp = &fc_req->u.rspn.rsp;
824 	int ret, len;
825 
826 	zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID,
827 			   FC_SYMBOLIC_NAME_SIZE);
828 	hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost));
829 	len = strlcpy(rspn_req->rspn.fr_name, fc_host_symbolic_name(shost),
830 		      FC_SYMBOLIC_NAME_SIZE);
831 	rspn_req->rspn.fr_name_len = len;
832 
833 	sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req));
834 	sg_init_one(&fc_req->sg_rsp, rspn_rsp, sizeof(*rspn_rsp));
835 
836 	ct_els->handler = zfcp_fc_complete;
837 	ct_els->handler_data = &completion;
838 	ct_els->req = &fc_req->sg_req;
839 	ct_els->resp = &fc_req->sg_rsp;
840 
841 	ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
842 			       ZFCP_FC_CTELS_TMO);
843 	if (!ret)
844 		wait_for_completion(&completion);
845 }
846 
847 /**
848  * zfcp_fc_sym_name_update - Retrieve and update the symbolic port name
849  * @work: ns_up_work of the adapter where to update the symbolic port name
850  *
851  * Retrieve the current symbolic port name that may have been set by
852  * the hardware using the GSPN request and update the fc_host
853  * symbolic_name sysfs attribute. When running in NPIV mode (and hence
854  * the port name is unique for this system), update the symbolic port
855  * name to add Linux specific information and update the FC nameserver
856  * using the RSPN request.
857  */
858 void zfcp_fc_sym_name_update(struct work_struct *work)
859 {
860 	struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
861 						    ns_up_work);
862 	int ret;
863 	struct zfcp_fc_req *fc_req;
864 
865 	if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
866 	    fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
867 		return;
868 
869 	fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
870 	if (!fc_req)
871 		return;
872 
873 	ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
874 	if (ret)
875 		goto out_free;
876 
877 	ret = zfcp_fc_gspn(adapter, fc_req);
878 	if (ret || fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
879 		goto out_ds_put;
880 
881 	memset(fc_req, 0, sizeof(*fc_req));
882 	zfcp_fc_rspn(adapter, fc_req);
883 
884 out_ds_put:
885 	zfcp_fc_wka_port_put(&adapter->gs->ds);
886 out_free:
887 	kmem_cache_free(zfcp_fc_req_cache, fc_req);
888 }
889 
890 static void zfcp_fc_ct_els_job_handler(void *data)
891 {
892 	struct bsg_job *job = data;
893 	struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
894 	struct fc_bsg_reply *jr = job->reply;
895 
896 	jr->reply_payload_rcv_len = job->reply_payload.payload_len;
897 	jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
898 	jr->result = zfcp_ct_els->status ? -EIO : 0;
899 	bsg_job_done(job, jr->result, jr->reply_payload_rcv_len);
900 }
901 
902 static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct bsg_job *job)
903 {
904 	u32 preamble_word1;
905 	u8 gs_type;
906 	struct zfcp_adapter *adapter;
907 	struct fc_bsg_request *bsg_request = job->request;
908 	struct fc_rport *rport = fc_bsg_to_rport(job);
909 	struct Scsi_Host *shost;
910 
911 	preamble_word1 = bsg_request->rqst_data.r_ct.preamble_word1;
912 	gs_type = (preamble_word1 & 0xff000000) >> 24;
913 
914 	shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
915 	adapter = (struct zfcp_adapter *) shost->hostdata[0];
916 
917 	switch (gs_type) {
918 	case FC_FST_ALIAS:
919 		return &adapter->gs->as;
920 	case FC_FST_MGMT:
921 		return &adapter->gs->ms;
922 	case FC_FST_TIME:
923 		return &adapter->gs->ts;
924 		break;
925 	case FC_FST_DIR:
926 		return &adapter->gs->ds;
927 		break;
928 	default:
929 		return NULL;
930 	}
931 }
932 
933 static void zfcp_fc_ct_job_handler(void *data)
934 {
935 	struct bsg_job *job = data;
936 	struct zfcp_fc_wka_port *wka_port;
937 
938 	wka_port = zfcp_fc_job_wka_port(job);
939 	zfcp_fc_wka_port_put(wka_port);
940 
941 	zfcp_fc_ct_els_job_handler(data);
942 }
943 
944 static int zfcp_fc_exec_els_job(struct bsg_job *job,
945 				struct zfcp_adapter *adapter)
946 {
947 	struct zfcp_fsf_ct_els *els = job->dd_data;
948 	struct fc_rport *rport = fc_bsg_to_rport(job);
949 	struct fc_bsg_request *bsg_request = job->request;
950 	struct zfcp_port *port;
951 	u32 d_id;
952 
953 	if (rport) {
954 		port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
955 		if (!port)
956 			return -EINVAL;
957 
958 		d_id = port->d_id;
959 		put_device(&port->dev);
960 	} else
961 		d_id = ntoh24(bsg_request->rqst_data.h_els.port_id);
962 
963 	els->handler = zfcp_fc_ct_els_job_handler;
964 	return zfcp_fsf_send_els(adapter, d_id, els, job->req->timeout / HZ);
965 }
966 
967 static int zfcp_fc_exec_ct_job(struct bsg_job *job,
968 			       struct zfcp_adapter *adapter)
969 {
970 	int ret;
971 	struct zfcp_fsf_ct_els *ct = job->dd_data;
972 	struct zfcp_fc_wka_port *wka_port;
973 
974 	wka_port = zfcp_fc_job_wka_port(job);
975 	if (!wka_port)
976 		return -EINVAL;
977 
978 	ret = zfcp_fc_wka_port_get(wka_port);
979 	if (ret)
980 		return ret;
981 
982 	ct->handler = zfcp_fc_ct_job_handler;
983 	ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->req->timeout / HZ);
984 	if (ret)
985 		zfcp_fc_wka_port_put(wka_port);
986 
987 	return ret;
988 }
989 
990 int zfcp_fc_exec_bsg_job(struct bsg_job *job)
991 {
992 	struct Scsi_Host *shost;
993 	struct zfcp_adapter *adapter;
994 	struct zfcp_fsf_ct_els *ct_els = job->dd_data;
995 	struct fc_bsg_request *bsg_request = job->request;
996 	struct fc_rport *rport = fc_bsg_to_rport(job);
997 
998 	shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
999 	adapter = (struct zfcp_adapter *)shost->hostdata[0];
1000 
1001 	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
1002 		return -EINVAL;
1003 
1004 	ct_els->req = job->request_payload.sg_list;
1005 	ct_els->resp = job->reply_payload.sg_list;
1006 	ct_els->handler_data = job;
1007 
1008 	switch (bsg_request->msgcode) {
1009 	case FC_BSG_RPT_ELS:
1010 	case FC_BSG_HST_ELS_NOLOGIN:
1011 		return zfcp_fc_exec_els_job(job, adapter);
1012 	case FC_BSG_RPT_CT:
1013 	case FC_BSG_HST_CT:
1014 		return zfcp_fc_exec_ct_job(job, adapter);
1015 	default:
1016 		return -EINVAL;
1017 	}
1018 }
1019 
1020 int zfcp_fc_timeout_bsg_job(struct bsg_job *job)
1021 {
1022 	/* hardware tracks timeout, reset bsg timeout to not interfere */
1023 	return -EAGAIN;
1024 }
1025 
1026 int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
1027 {
1028 	struct zfcp_fc_wka_ports *wka_ports;
1029 
1030 	wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
1031 	if (!wka_ports)
1032 		return -ENOMEM;
1033 
1034 	adapter->gs = wka_ports;
1035 	zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
1036 	zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
1037 	zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
1038 	zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
1039 
1040 	return 0;
1041 }
1042 
1043 void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
1044 {
1045 	kfree(adapter->gs);
1046 	adapter->gs = NULL;
1047 }
1048 
1049