xref: /openbmc/linux/drivers/s390/scsi/zfcp_fc.c (revision 8fa5723aa7e053d498336b48448b292fc2e0458b)
1 /*
2  * zfcp device driver
3  *
4  * Fibre Channel related functions for the zfcp device driver.
5  *
6  * Copyright IBM Corporation 2008
7  */
8 
9 #include "zfcp_ext.h"
10 
11 struct ct_iu_gpn_ft_req {
12 	struct ct_hdr header;
13 	u8 flags;
14 	u8 domain_id_scope;
15 	u8 area_id_scope;
16 	u8 fc4_type;
17 } __attribute__ ((packed));
18 
19 struct gpn_ft_resp_acc {
20 	u8 control;
21 	u8 port_id[3];
22 	u8 reserved[4];
23 	u64 wwpn;
24 } __attribute__ ((packed));
25 
26 #define ZFCP_GPN_FT_ENTRIES ((PAGE_SIZE - sizeof(struct ct_hdr)) \
27 				/ sizeof(struct gpn_ft_resp_acc))
28 #define ZFCP_GPN_FT_BUFFERS 4
29 #define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
30 
31 struct ct_iu_gpn_ft_resp {
32 	struct ct_hdr header;
33 	struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
34 } __attribute__ ((packed));
35 
36 struct zfcp_gpn_ft {
37 	struct zfcp_send_ct ct;
38 	struct scatterlist sg_req;
39 	struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
40 };
41 
42 struct zfcp_fc_ns_handler_data {
43 	struct completion done;
44 	void (*handler)(unsigned long);
45 	unsigned long handler_data;
46 };
47 
48 static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
49 {
50 	if (mutex_lock_interruptible(&wka_port->mutex))
51 		return -ERESTARTSYS;
52 
53 	if (wka_port->status != ZFCP_WKA_PORT_ONLINE) {
54 		wka_port->status = ZFCP_WKA_PORT_OPENING;
55 		if (zfcp_fsf_open_wka_port(wka_port))
56 			wka_port->status = ZFCP_WKA_PORT_OFFLINE;
57 	}
58 
59 	mutex_unlock(&wka_port->mutex);
60 
61 	wait_event_timeout(
62 		wka_port->completion_wq,
63 		wka_port->status == ZFCP_WKA_PORT_ONLINE ||
64 		wka_port->status == ZFCP_WKA_PORT_OFFLINE,
65 		HZ >> 1);
66 
67 	if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
68 		atomic_inc(&wka_port->refcount);
69 		return 0;
70 	}
71 	return -EIO;
72 }
73 
74 static void zfcp_wka_port_offline(struct work_struct *work)
75 {
76 	struct delayed_work *dw = container_of(work, struct delayed_work, work);
77 	struct zfcp_wka_port *wka_port =
78 			container_of(dw, struct zfcp_wka_port, work);
79 
80 	wait_event(wka_port->completion_wq,
81 			atomic_read(&wka_port->refcount) == 0);
82 
83 	mutex_lock(&wka_port->mutex);
84 	if ((atomic_read(&wka_port->refcount) != 0) ||
85 	    (wka_port->status != ZFCP_WKA_PORT_ONLINE))
86 		goto out;
87 
88 	wka_port->status = ZFCP_WKA_PORT_CLOSING;
89 	if (zfcp_fsf_close_wka_port(wka_port)) {
90 		wka_port->status = ZFCP_WKA_PORT_OFFLINE;
91 		wake_up(&wka_port->completion_wq);
92 	}
93 out:
94 	mutex_unlock(&wka_port->mutex);
95 }
96 
97 static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
98 {
99 	if (atomic_dec_return(&wka_port->refcount) != 0)
100 		return;
101 	/* wait 10 miliseconds, other reqs might pop in */
102 	schedule_delayed_work(&wka_port->work, HZ / 100);
103 }
104 
105 void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
106 {
107 	struct zfcp_wka_port *wka_port = &adapter->nsp;
108 
109 	init_waitqueue_head(&wka_port->completion_wq);
110 
111 	wka_port->adapter = adapter;
112 	wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
113 
114 	wka_port->status = ZFCP_WKA_PORT_OFFLINE;
115 	atomic_set(&wka_port->refcount, 0);
116 	mutex_init(&wka_port->mutex);
117 	INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
118 }
119 
120 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
121 				   struct fcp_rscn_element *elem)
122 {
123 	unsigned long flags;
124 	struct zfcp_port *port;
125 
126 	read_lock_irqsave(&zfcp_data.config_lock, flags);
127 	list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
128 		/* FIXME: ZFCP_STATUS_PORT_DID_DID check is racy */
129 		if (!(atomic_read(&port->status) & ZFCP_STATUS_PORT_DID_DID))
130 			/* Try to connect to unused ports anyway. */
131 			zfcp_erp_port_reopen(port,
132 					     ZFCP_STATUS_COMMON_ERP_FAILED,
133 					     82, fsf_req);
134 		else if ((port->d_id & range) == (elem->nport_did & range))
135 			/* Check connection status for connected ports */
136 			zfcp_test_link(port);
137 	}
138 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
139 }
140 
141 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
142 {
143 	struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
144 	struct fcp_rscn_head *fcp_rscn_head;
145 	struct fcp_rscn_element *fcp_rscn_element;
146 	u16 i;
147 	u16 no_entries;
148 	u32 range_mask;
149 
150 	fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
151 	fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
152 
153 	/* see FC-FS */
154 	no_entries = fcp_rscn_head->payload_len /
155 			sizeof(struct fcp_rscn_element);
156 
157 	for (i = 1; i < no_entries; i++) {
158 		/* skip head and start with 1st element */
159 		fcp_rscn_element++;
160 		switch (fcp_rscn_element->addr_format) {
161 		case ZFCP_PORT_ADDRESS:
162 			range_mask = ZFCP_PORTS_RANGE_PORT;
163 			break;
164 		case ZFCP_AREA_ADDRESS:
165 			range_mask = ZFCP_PORTS_RANGE_AREA;
166 			break;
167 		case ZFCP_DOMAIN_ADDRESS:
168 			range_mask = ZFCP_PORTS_RANGE_DOMAIN;
169 			break;
170 		case ZFCP_FABRIC_ADDRESS:
171 			range_mask = ZFCP_PORTS_RANGE_FABRIC;
172 			break;
173 		default:
174 			continue;
175 		}
176 		_zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
177 	}
178 	schedule_work(&fsf_req->adapter->scan_work);
179 }
180 
181 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
182 {
183 	struct zfcp_adapter *adapter = req->adapter;
184 	struct zfcp_port *port;
185 	unsigned long flags;
186 
187 	read_lock_irqsave(&zfcp_data.config_lock, flags);
188 	list_for_each_entry(port, &adapter->port_list_head, list)
189 		if (port->wwpn == wwpn)
190 			break;
191 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
192 
193 	if (port && (port->wwpn == wwpn))
194 		zfcp_erp_port_forced_reopen(port, 0, 83, req);
195 }
196 
197 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
198 {
199 	struct fsf_status_read_buffer *status_buffer =
200 		(struct fsf_status_read_buffer *)req->data;
201 	struct fsf_plogi *els_plogi =
202 		(struct fsf_plogi *) status_buffer->payload.data;
203 
204 	zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
205 }
206 
207 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
208 {
209 	struct fsf_status_read_buffer *status_buffer =
210 		(struct fsf_status_read_buffer *)req->data;
211 	struct fcp_logo *els_logo =
212 		(struct fcp_logo *) status_buffer->payload.data;
213 
214 	zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
215 }
216 
217 /**
218  * zfcp_fc_incoming_els - handle incoming ELS
219  * @fsf_req - request which contains incoming ELS
220  */
221 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
222 {
223 	struct fsf_status_read_buffer *status_buffer =
224 		(struct fsf_status_read_buffer *) fsf_req->data;
225 	unsigned int els_type = status_buffer->payload.data[0];
226 
227 	zfcp_san_dbf_event_incoming_els(fsf_req);
228 	if (els_type == LS_PLOGI)
229 		zfcp_fc_incoming_plogi(fsf_req);
230 	else if (els_type == LS_LOGO)
231 		zfcp_fc_incoming_logo(fsf_req);
232 	else if (els_type == LS_RSCN)
233 		zfcp_fc_incoming_rscn(fsf_req);
234 }
235 
236 static void zfcp_fc_ns_handler(unsigned long data)
237 {
238 	struct zfcp_fc_ns_handler_data *compl_rec =
239 			(struct zfcp_fc_ns_handler_data *) data;
240 
241 	if (compl_rec->handler)
242 		compl_rec->handler(compl_rec->handler_data);
243 
244 	complete(&compl_rec->done);
245 }
246 
247 static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
248 {
249 	struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
250 	struct zfcp_send_ct *ct = &gid_pn->ct;
251 	struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
252 	struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
253 	struct zfcp_port *port = gid_pn->port;
254 
255 	if (ct->status)
256 		return;
257 	if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
258 		atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
259 		return;
260 	}
261 	/* paranoia */
262 	if (ct_iu_req->wwpn != port->wwpn)
263 		return;
264 	/* looks like a valid d_id */
265 	port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
266 	atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
267 }
268 
269 int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
270 				     struct zfcp_gid_pn_data *gid_pn)
271 {
272 	struct zfcp_adapter *adapter = erp_action->adapter;
273 	struct zfcp_fc_ns_handler_data compl_rec;
274 	int ret;
275 
276 	/* setup parameters for send generic command */
277 	gid_pn->port = erp_action->port;
278 	gid_pn->ct.wka_port = &adapter->nsp;
279 	gid_pn->ct.handler = zfcp_fc_ns_handler;
280 	gid_pn->ct.handler_data = (unsigned long) &compl_rec;
281 	gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
282 	gid_pn->ct.req = &gid_pn->req;
283 	gid_pn->ct.resp = &gid_pn->resp;
284 	gid_pn->ct.req_count = 1;
285 	gid_pn->ct.resp_count = 1;
286 	sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
287 		    sizeof(struct ct_iu_gid_pn_req));
288 	sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
289 		    sizeof(struct ct_iu_gid_pn_resp));
290 
291 	/* setup nameserver request */
292 	gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
293 	gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
294 	gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
295 	gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
296 	gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
297 	gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_MAX_SIZE;
298 	gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
299 
300 	init_completion(&compl_rec.done);
301 	compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
302 	compl_rec.handler_data = (unsigned long) gid_pn;
303 	ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
304 			       erp_action);
305 	if (!ret)
306 		wait_for_completion(&compl_rec.done);
307 	return ret;
308 }
309 
310 /**
311  * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
312  * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
313  * return: -ENOMEM on error, 0 otherwise
314  */
315 int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
316 {
317 	int ret;
318 	struct zfcp_gid_pn_data *gid_pn;
319 	struct zfcp_adapter *adapter = erp_action->adapter;
320 
321 	gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
322 	if (!gid_pn)
323 		return -ENOMEM;
324 
325 	memset(gid_pn, 0, sizeof(*gid_pn));
326 
327 	ret = zfcp_wka_port_get(&adapter->nsp);
328 	if (ret)
329 		goto out;
330 
331 	ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
332 
333 	zfcp_wka_port_put(&adapter->nsp);
334 out:
335 	mempool_free(gid_pn, adapter->pool.data_gid_pn);
336 	return ret;
337 }
338 
339 /**
340  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
341  * @port: zfcp_port structure
342  * @plogi: plogi payload
343  *
344  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
345  */
346 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
347 {
348 	port->maxframe_size = plogi->serv_param.common_serv_param[7] |
349 		((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
350 	if (plogi->serv_param.class1_serv_param[0] & 0x80)
351 		port->supported_classes |= FC_COS_CLASS1;
352 	if (plogi->serv_param.class2_serv_param[0] & 0x80)
353 		port->supported_classes |= FC_COS_CLASS2;
354 	if (plogi->serv_param.class3_serv_param[0] & 0x80)
355 		port->supported_classes |= FC_COS_CLASS3;
356 	if (plogi->serv_param.class4_serv_param[0] & 0x80)
357 		port->supported_classes |= FC_COS_CLASS4;
358 }
359 
360 struct zfcp_els_adisc {
361 	struct zfcp_send_els els;
362 	struct scatterlist req;
363 	struct scatterlist resp;
364 	struct zfcp_ls_adisc ls_adisc;
365 	struct zfcp_ls_adisc ls_adisc_acc;
366 };
367 
368 static void zfcp_fc_adisc_handler(unsigned long data)
369 {
370 	struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
371 	struct zfcp_port *port = adisc->els.port;
372 	struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
373 
374 	if (adisc->els.status) {
375 		/* request rejected or timed out */
376 		zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
377 		goto out;
378 	}
379 
380 	if (!port->wwnn)
381 		port->wwnn = ls_adisc->wwnn;
382 
383 	if (port->wwpn != ls_adisc->wwpn)
384 		zfcp_erp_port_reopen(port, 0, 64, NULL);
385 
386  out:
387 	zfcp_port_put(port);
388 	kfree(adisc);
389 }
390 
391 static int zfcp_fc_adisc(struct zfcp_port *port)
392 {
393 	struct zfcp_els_adisc *adisc;
394 	struct zfcp_adapter *adapter = port->adapter;
395 
396 	adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
397 	if (!adisc)
398 		return -ENOMEM;
399 
400 	adisc->els.req = &adisc->req;
401 	adisc->els.resp = &adisc->resp;
402 	sg_init_one(adisc->els.req, &adisc->ls_adisc,
403 		    sizeof(struct zfcp_ls_adisc));
404 	sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
405 		    sizeof(struct zfcp_ls_adisc));
406 
407 	adisc->els.req_count = 1;
408 	adisc->els.resp_count = 1;
409 	adisc->els.adapter = adapter;
410 	adisc->els.port = port;
411 	adisc->els.d_id = port->d_id;
412 	adisc->els.handler = zfcp_fc_adisc_handler;
413 	adisc->els.handler_data = (unsigned long) adisc;
414 	adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
415 
416 	/* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
417 	   without FC-AL-2 capability, so we don't set it */
418 	adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
419 	adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
420 	adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
421 
422 	return zfcp_fsf_send_els(&adisc->els);
423 }
424 
425 /**
426  * zfcp_test_link - lightweight link test procedure
427  * @port: port to be tested
428  *
429  * Test status of a link to a remote port using the ELS command ADISC.
430  * If there is a problem with the remote port, error recovery steps
431  * will be triggered.
432  */
433 void zfcp_test_link(struct zfcp_port *port)
434 {
435 	int retval;
436 
437 	zfcp_port_get(port);
438 	retval = zfcp_fc_adisc(port);
439 	if (retval == 0)
440 		return;
441 
442 	/* send of ADISC was not possible */
443 	zfcp_port_put(port);
444 	if (retval != -EBUSY)
445 		zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
446 }
447 
448 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
449 {
450 	struct scatterlist *sg = &gpn_ft->sg_req;
451 
452 	kfree(sg_virt(sg)); /* free request buffer */
453 	zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
454 
455 	kfree(gpn_ft);
456 }
457 
458 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
459 {
460 	struct zfcp_gpn_ft *gpn_ft;
461 	struct ct_iu_gpn_ft_req *req;
462 
463 	gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
464 	if (!gpn_ft)
465 		return NULL;
466 
467 	req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
468 	if (!req) {
469 		kfree(gpn_ft);
470 		gpn_ft = NULL;
471 		goto out;
472 	}
473 	sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
474 
475 	if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
476 		zfcp_free_sg_env(gpn_ft);
477 		gpn_ft = NULL;
478 	}
479 out:
480 	return gpn_ft;
481 }
482 
483 
484 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
485 				  struct zfcp_adapter *adapter)
486 {
487 	struct zfcp_send_ct *ct = &gpn_ft->ct;
488 	struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
489 	struct zfcp_fc_ns_handler_data compl_rec;
490 	int ret;
491 
492 	/* prepare CT IU for GPN_FT */
493 	req->header.revision = ZFCP_CT_REVISION;
494 	req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
495 	req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
496 	req->header.options = ZFCP_CT_SYNCHRONOUS;
497 	req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
498 	req->header.max_res_size = (sizeof(struct gpn_ft_resp_acc) *
499 					(ZFCP_GPN_FT_MAX_ENTRIES - 1)) >> 2;
500 	req->flags = 0;
501 	req->domain_id_scope = 0;
502 	req->area_id_scope = 0;
503 	req->fc4_type = ZFCP_CT_SCSI_FCP;
504 
505 	/* prepare zfcp_send_ct */
506 	ct->wka_port = &adapter->nsp;
507 	ct->handler = zfcp_fc_ns_handler;
508 	ct->handler_data = (unsigned long)&compl_rec;
509 	ct->timeout = 10;
510 	ct->req = &gpn_ft->sg_req;
511 	ct->resp = gpn_ft->sg_resp;
512 	ct->req_count = 1;
513 	ct->resp_count = ZFCP_GPN_FT_BUFFERS;
514 
515 	init_completion(&compl_rec.done);
516 	compl_rec.handler = NULL;
517 	ret = zfcp_fsf_send_ct(ct, NULL, NULL);
518 	if (!ret)
519 		wait_for_completion(&compl_rec.done);
520 	return ret;
521 }
522 
523 static void zfcp_validate_port(struct zfcp_port *port)
524 {
525 	struct zfcp_adapter *adapter = port->adapter;
526 
527 	atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
528 
529 	if ((port->supported_classes != 0) ||
530 	    !list_empty(&port->unit_list_head)) {
531 		zfcp_port_put(port);
532 		return;
533 	}
534 	zfcp_erp_port_shutdown(port, 0, 151, NULL);
535 	zfcp_erp_wait(adapter);
536 	zfcp_port_put(port);
537 	zfcp_port_dequeue(port);
538 }
539 
540 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
541 {
542 	struct zfcp_send_ct *ct = &gpn_ft->ct;
543 	struct scatterlist *sg = gpn_ft->sg_resp;
544 	struct ct_hdr *hdr = sg_virt(sg);
545 	struct gpn_ft_resp_acc *acc = sg_virt(sg);
546 	struct zfcp_adapter *adapter = ct->wka_port->adapter;
547 	struct zfcp_port *port, *tmp;
548 	u32 d_id;
549 	int ret = 0, x, last = 0;
550 
551 	if (ct->status)
552 		return -EIO;
553 
554 	if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
555 		if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
556 			return -EAGAIN; /* might be a temporary condition */
557 		return -EIO;
558 	}
559 
560 	if (hdr->max_res_size)
561 		return -E2BIG;
562 
563 	down(&zfcp_data.config_sema);
564 
565 	/* first entry is the header */
566 	for (x = 1; x < ZFCP_GPN_FT_MAX_ENTRIES && !last; x++) {
567 		if (x % (ZFCP_GPN_FT_ENTRIES + 1))
568 			acc++;
569 		else
570 			acc = sg_virt(++sg);
571 
572 		last = acc->control & 0x80;
573 		d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
574 		       acc->port_id[2];
575 
576 		/* don't attach ports with a well known address */
577 		if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
578 			continue;
579 		/* skip the adapter's port and known remote ports */
580 		if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
581 			continue;
582 		port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
583 		if (port) {
584 			zfcp_port_get(port);
585 			continue;
586 		}
587 
588 		port = zfcp_port_enqueue(adapter, acc->wwpn,
589 					 ZFCP_STATUS_PORT_DID_DID |
590 					 ZFCP_STATUS_COMMON_NOESC, d_id);
591 		if (IS_ERR(port))
592 			ret = PTR_ERR(port);
593 		else
594 			zfcp_erp_port_reopen(port, 0, 149, NULL);
595 	}
596 
597 	zfcp_erp_wait(adapter);
598 	list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
599 		zfcp_validate_port(port);
600 	up(&zfcp_data.config_sema);
601 	return ret;
602 }
603 
604 /**
605  * zfcp_scan_ports - scan remote ports and attach new ports
606  * @adapter: pointer to struct zfcp_adapter
607  */
608 int zfcp_scan_ports(struct zfcp_adapter *adapter)
609 {
610 	int ret, i;
611 	struct zfcp_gpn_ft *gpn_ft;
612 
613 	zfcp_erp_wait(adapter); /* wait until adapter is finished with ERP */
614 	if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
615 		return 0;
616 
617 	ret = zfcp_wka_port_get(&adapter->nsp);
618 	if (ret)
619 		return ret;
620 
621 	gpn_ft = zfcp_alloc_sg_env();
622 	if (!gpn_ft) {
623 		ret = -ENOMEM;
624 		goto out;
625 	}
626 
627 	for (i = 0; i < 3; i++) {
628 		ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
629 		if (!ret) {
630 			ret = zfcp_scan_eval_gpn_ft(gpn_ft);
631 			if (ret == -EAGAIN)
632 				ssleep(1);
633 			else
634 				break;
635 		}
636 	}
637 	zfcp_free_sg_env(gpn_ft);
638 out:
639 	zfcp_wka_port_put(&adapter->nsp);
640 	return ret;
641 }
642 
643 
644 void _zfcp_scan_ports_later(struct work_struct *work)
645 {
646 	zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
647 }
648