4 * Fibre Channel related functions for the zfcp device driver.
6 * Copyright IBM Corporation 2008
11 struct ct_iu_gpn_ft_req {
17 } __attribute__ ((packed));
19 struct gpn_ft_resp_acc {
24 } __attribute__ ((packed));
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)
31 struct ct_iu_gpn_ft_resp {
33 struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
34 } __attribute__ ((packed));
37 struct zfcp_send_ct ct;
38 struct scatterlist sg_req;
39 struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
42 struct zfcp_fc_ns_handler_data {
43 struct completion done;
44 void (*handler)(unsigned long);
45 unsigned long handler_data;
48 static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
50 if (mutex_lock_interruptible(&wka_port->mutex))
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;
59 mutex_unlock(&wka_port->mutex);
62 wka_port->completion_wq,
63 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
64 wka_port->status == ZFCP_WKA_PORT_OFFLINE,
67 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
68 atomic_inc(&wka_port->refcount);
74 static void zfcp_wka_port_offline(struct work_struct *work)
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);
80 wait_event(wka_port->completion_wq,
81 atomic_read(&wka_port->refcount) == 0);
83 mutex_lock(&wka_port->mutex);
84 if ((atomic_read(&wka_port->refcount) != 0) ||
85 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
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);
94 mutex_unlock(&wka_port->mutex);
97 static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
99 if (atomic_dec_return(&wka_port->refcount) != 0)
101 /* wait 10 miliseconds, other reqs might pop in */
102 schedule_delayed_work(&wka_port->work, HZ / 100);
105 void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
107 struct zfcp_wka_port *wka_port = &adapter->nsp;
109 init_waitqueue_head(&wka_port->completion_wq);
111 wka_port->adapter = adapter;
112 wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
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);
120 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
121 struct fcp_rscn_element *elem)
124 struct zfcp_port *port;
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,
134 else if ((port->d_id & range) == (elem->nport_did & range))
135 /* Check connection status for connected ports */
136 zfcp_test_link(port);
138 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
141 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
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;
150 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
151 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
154 no_entries = fcp_rscn_head->payload_len /
155 sizeof(struct fcp_rscn_element);
157 for (i = 1; i < no_entries; i++) {
158 /* skip head and start with 1st element */
160 switch (fcp_rscn_element->addr_format) {
161 case ZFCP_PORT_ADDRESS:
162 range_mask = ZFCP_PORTS_RANGE_PORT;
164 case ZFCP_AREA_ADDRESS:
165 range_mask = ZFCP_PORTS_RANGE_AREA;
167 case ZFCP_DOMAIN_ADDRESS:
168 range_mask = ZFCP_PORTS_RANGE_DOMAIN;
170 case ZFCP_FABRIC_ADDRESS:
171 range_mask = ZFCP_PORTS_RANGE_FABRIC;
176 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
178 schedule_work(&fsf_req->adapter->scan_work);
181 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
183 struct zfcp_adapter *adapter = req->adapter;
184 struct zfcp_port *port;
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)
191 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
193 if (port && (port->wwpn == wwpn))
194 zfcp_erp_port_forced_reopen(port, 0, 83, req);
197 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
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;
204 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
207 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
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;
214 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
218 * zfcp_fc_incoming_els - handle incoming ELS
219 * @fsf_req - request which contains incoming ELS
221 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
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];
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);
236 static void zfcp_fc_ns_handler(unsigned long data)
238 struct zfcp_fc_ns_handler_data *compl_rec =
239 (struct zfcp_fc_ns_handler_data *) data;
241 if (compl_rec->handler)
242 compl_rec->handler(compl_rec->handler_data);
244 complete(&compl_rec->done);
247 static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
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;
257 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
258 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
262 if (ct_iu_req->wwpn != port->wwpn)
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);
269 int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
270 struct zfcp_gid_pn_data *gid_pn)
272 struct zfcp_adapter *adapter = erp_action->adapter;
273 struct zfcp_fc_ns_handler_data compl_rec;
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));
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;
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,
306 wait_for_completion(&compl_rec.done);
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
315 int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
318 struct zfcp_gid_pn_data *gid_pn;
319 struct zfcp_adapter *adapter = erp_action->adapter;
321 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
325 memset(gid_pn, 0, sizeof(*gid_pn));
327 ret = zfcp_wka_port_get(&adapter->nsp);
331 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
333 zfcp_wka_port_put(&adapter->nsp);
335 mempool_free(gid_pn, adapter->pool.data_gid_pn);
340 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
341 * @port: zfcp_port structure
342 * @plogi: plogi payload
344 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
346 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
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;
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;
368 static void zfcp_fc_adisc_handler(unsigned long data)
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;
374 if (adisc->els.status) {
375 /* request rejected or timed out */
376 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
381 port->wwnn = ls_adisc->wwnn;
383 if (port->wwpn != ls_adisc->wwpn)
384 zfcp_erp_port_reopen(port, 0, 64, NULL);
391 static int zfcp_fc_adisc(struct zfcp_port *port)
393 struct zfcp_els_adisc *adisc;
394 struct zfcp_adapter *adapter = port->adapter;
396 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
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));
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;
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);
422 return zfcp_fsf_send_els(&adisc->els);
426 * zfcp_test_link - lightweight link test procedure
427 * @port: port to be tested
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
433 void zfcp_test_link(struct zfcp_port *port)
438 retval = zfcp_fc_adisc(port);
442 /* send of ADISC was not possible */
444 if (retval != -EBUSY)
445 zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
448 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
450 struct scatterlist *sg = &gpn_ft->sg_req;
452 kfree(sg_virt(sg)); /* free request buffer */
453 zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
458 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
460 struct zfcp_gpn_ft *gpn_ft;
461 struct ct_iu_gpn_ft_req *req;
463 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
467 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
473 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
475 if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
476 zfcp_free_sg_env(gpn_ft);
484 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
485 struct zfcp_adapter *adapter)
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;
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;
501 req->domain_id_scope = 0;
502 req->area_id_scope = 0;
503 req->fc4_type = ZFCP_CT_SCSI_FCP;
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;
510 ct->req = &gpn_ft->sg_req;
511 ct->resp = gpn_ft->sg_resp;
513 ct->resp_count = ZFCP_GPN_FT_BUFFERS;
515 init_completion(&compl_rec.done);
516 compl_rec.handler = NULL;
517 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
519 wait_for_completion(&compl_rec.done);
523 static void zfcp_validate_port(struct zfcp_port *port)
525 struct zfcp_adapter *adapter = port->adapter;
527 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
529 if ((port->supported_classes != 0) || (port->units != 0)) {
533 zfcp_erp_port_shutdown(port, 0, 151, NULL);
534 zfcp_erp_wait(adapter);
536 zfcp_port_dequeue(port);
539 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
541 struct zfcp_send_ct *ct = &gpn_ft->ct;
542 struct scatterlist *sg = gpn_ft->sg_resp;
543 struct ct_hdr *hdr = sg_virt(sg);
544 struct gpn_ft_resp_acc *acc = sg_virt(sg);
545 struct zfcp_adapter *adapter = ct->wka_port->adapter;
546 struct zfcp_port *port, *tmp;
548 int ret = 0, x, last = 0;
553 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
554 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
555 return -EAGAIN; /* might be a temporary condition */
559 if (hdr->max_res_size)
562 down(&zfcp_data.config_sema);
564 /* first entry is the header */
565 for (x = 1; x < ZFCP_GPN_FT_MAX_ENTRIES && !last; x++) {
566 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
571 last = acc->control & 0x80;
572 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
575 /* don't attach ports with a well known address */
576 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
578 /* skip the adapter's port and known remote ports */
579 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
581 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
587 port = zfcp_port_enqueue(adapter, acc->wwpn,
588 ZFCP_STATUS_PORT_DID_DID |
589 ZFCP_STATUS_COMMON_NOESC, d_id);
593 zfcp_erp_port_reopen(port, 0, 149, NULL);
596 zfcp_erp_wait(adapter);
597 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
598 zfcp_validate_port(port);
599 up(&zfcp_data.config_sema);
604 * zfcp_scan_ports - scan remote ports and attach new ports
605 * @adapter: pointer to struct zfcp_adapter
607 int zfcp_scan_ports(struct zfcp_adapter *adapter)
610 struct zfcp_gpn_ft *gpn_ft;
612 zfcp_erp_wait(adapter); /* wait until adapter is finished with ERP */
613 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
616 ret = zfcp_wka_port_get(&adapter->nsp);
620 gpn_ft = zfcp_alloc_sg_env();
626 for (i = 0; i < 3; i++) {
627 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
629 ret = zfcp_scan_eval_gpn_ft(gpn_ft);
636 zfcp_free_sg_env(gpn_ft);
638 zfcp_wka_port_put(&adapter->nsp);
643 void _zfcp_scan_ports_later(struct work_struct *work)
645 zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));