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