4 * Fibre Channel related functions for the zfcp device driver.
6 * Copyright IBM Corporation 2008
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 struct ct_iu_gpn_ft_req {
20 } __attribute__ ((packed));
22 struct gpn_ft_resp_acc {
27 } __attribute__ ((packed));
29 #define ZFCP_GPN_FT_ENTRIES ((PAGE_SIZE - sizeof(struct ct_hdr)) \
30 / sizeof(struct gpn_ft_resp_acc))
31 #define ZFCP_GPN_FT_BUFFERS 4
32 #define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
34 struct ct_iu_gpn_ft_resp {
36 struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
37 } __attribute__ ((packed));
40 struct zfcp_send_ct ct;
41 struct scatterlist sg_req;
42 struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
45 struct zfcp_fc_ns_handler_data {
46 struct completion done;
47 void (*handler)(unsigned long);
48 unsigned long handler_data;
51 static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
53 if (mutex_lock_interruptible(&wka_port->mutex))
56 if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
57 wka_port->status == ZFCP_WKA_PORT_CLOSING) {
58 wka_port->status = ZFCP_WKA_PORT_OPENING;
59 if (zfcp_fsf_open_wka_port(wka_port))
60 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
63 mutex_unlock(&wka_port->mutex);
66 wka_port->completion_wq,
67 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
68 wka_port->status == ZFCP_WKA_PORT_OFFLINE,
71 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
72 atomic_inc(&wka_port->refcount);
78 static void zfcp_wka_port_offline(struct work_struct *work)
80 struct delayed_work *dw = container_of(work, struct delayed_work, work);
81 struct zfcp_wka_port *wka_port =
82 container_of(dw, struct zfcp_wka_port, work);
84 wait_event(wka_port->completion_wq,
85 atomic_read(&wka_port->refcount) == 0);
87 mutex_lock(&wka_port->mutex);
88 if ((atomic_read(&wka_port->refcount) != 0) ||
89 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
92 wka_port->status = ZFCP_WKA_PORT_CLOSING;
93 if (zfcp_fsf_close_wka_port(wka_port)) {
94 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
95 wake_up(&wka_port->completion_wq);
98 mutex_unlock(&wka_port->mutex);
101 static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
103 if (atomic_dec_return(&wka_port->refcount) != 0)
105 /* wait 10 miliseconds, other reqs might pop in */
106 schedule_delayed_work(&wka_port->work, HZ / 100);
109 void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
111 struct zfcp_wka_port *wka_port = &adapter->nsp;
113 init_waitqueue_head(&wka_port->completion_wq);
115 wka_port->adapter = adapter;
116 wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
118 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
119 atomic_set(&wka_port->refcount, 0);
120 mutex_init(&wka_port->mutex);
121 INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
124 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
125 struct fcp_rscn_element *elem)
128 struct zfcp_port *port;
130 read_lock_irqsave(&zfcp_data.config_lock, flags);
131 list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
132 if (!(atomic_read(&port->status) & ZFCP_STATUS_PORT_PHYS_OPEN))
133 /* Try to connect to unused ports anyway. */
134 zfcp_erp_port_reopen(port,
135 ZFCP_STATUS_COMMON_ERP_FAILED,
137 else if ((port->d_id & range) == (elem->nport_did & range))
138 /* Check connection status for connected ports */
139 zfcp_test_link(port);
141 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
144 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
146 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
147 struct fcp_rscn_head *fcp_rscn_head;
148 struct fcp_rscn_element *fcp_rscn_element;
153 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
154 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
157 no_entries = fcp_rscn_head->payload_len /
158 sizeof(struct fcp_rscn_element);
160 for (i = 1; i < no_entries; i++) {
161 /* skip head and start with 1st element */
163 switch (fcp_rscn_element->addr_format) {
164 case ZFCP_PORT_ADDRESS:
165 range_mask = ZFCP_PORTS_RANGE_PORT;
167 case ZFCP_AREA_ADDRESS:
168 range_mask = ZFCP_PORTS_RANGE_AREA;
170 case ZFCP_DOMAIN_ADDRESS:
171 range_mask = ZFCP_PORTS_RANGE_DOMAIN;
173 case ZFCP_FABRIC_ADDRESS:
174 range_mask = ZFCP_PORTS_RANGE_FABRIC;
179 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
181 schedule_work(&fsf_req->adapter->scan_work);
184 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
186 struct zfcp_adapter *adapter = req->adapter;
187 struct zfcp_port *port;
190 read_lock_irqsave(&zfcp_data.config_lock, flags);
191 list_for_each_entry(port, &adapter->port_list_head, list)
192 if (port->wwpn == wwpn)
194 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
196 if (port && (port->wwpn == wwpn))
197 zfcp_erp_port_forced_reopen(port, 0, 83, req);
200 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
202 struct fsf_status_read_buffer *status_buffer =
203 (struct fsf_status_read_buffer *)req->data;
204 struct fsf_plogi *els_plogi =
205 (struct fsf_plogi *) status_buffer->payload.data;
207 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
210 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
212 struct fsf_status_read_buffer *status_buffer =
213 (struct fsf_status_read_buffer *)req->data;
214 struct fcp_logo *els_logo =
215 (struct fcp_logo *) status_buffer->payload.data;
217 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
221 * zfcp_fc_incoming_els - handle incoming ELS
222 * @fsf_req - request which contains incoming ELS
224 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
226 struct fsf_status_read_buffer *status_buffer =
227 (struct fsf_status_read_buffer *) fsf_req->data;
228 unsigned int els_type = status_buffer->payload.data[0];
230 zfcp_san_dbf_event_incoming_els(fsf_req);
231 if (els_type == LS_PLOGI)
232 zfcp_fc_incoming_plogi(fsf_req);
233 else if (els_type == LS_LOGO)
234 zfcp_fc_incoming_logo(fsf_req);
235 else if (els_type == LS_RSCN)
236 zfcp_fc_incoming_rscn(fsf_req);
239 static void zfcp_fc_ns_handler(unsigned long data)
241 struct zfcp_fc_ns_handler_data *compl_rec =
242 (struct zfcp_fc_ns_handler_data *) data;
244 if (compl_rec->handler)
245 compl_rec->handler(compl_rec->handler_data);
247 complete(&compl_rec->done);
250 static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
252 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
253 struct zfcp_send_ct *ct = &gid_pn->ct;
254 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
255 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
256 struct zfcp_port *port = gid_pn->port;
260 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
261 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
265 if (ct_iu_req->wwpn != port->wwpn)
267 /* looks like a valid d_id */
268 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
269 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
272 int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
273 struct zfcp_gid_pn_data *gid_pn)
275 struct zfcp_adapter *adapter = erp_action->adapter;
276 struct zfcp_fc_ns_handler_data compl_rec;
279 /* setup parameters for send generic command */
280 gid_pn->port = erp_action->port;
281 gid_pn->ct.wka_port = &adapter->nsp;
282 gid_pn->ct.handler = zfcp_fc_ns_handler;
283 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
284 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
285 gid_pn->ct.req = &gid_pn->req;
286 gid_pn->ct.resp = &gid_pn->resp;
287 gid_pn->ct.req_count = 1;
288 gid_pn->ct.resp_count = 1;
289 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
290 sizeof(struct ct_iu_gid_pn_req));
291 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
292 sizeof(struct ct_iu_gid_pn_resp));
294 /* setup nameserver request */
295 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
296 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
297 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
298 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
299 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
300 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_MAX_SIZE;
301 gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
303 init_completion(&compl_rec.done);
304 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
305 compl_rec.handler_data = (unsigned long) gid_pn;
306 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
309 wait_for_completion(&compl_rec.done);
314 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
315 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
316 * return: -ENOMEM on error, 0 otherwise
318 int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
321 struct zfcp_gid_pn_data *gid_pn;
322 struct zfcp_adapter *adapter = erp_action->adapter;
324 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
328 memset(gid_pn, 0, sizeof(*gid_pn));
330 ret = zfcp_wka_port_get(&adapter->nsp);
334 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
336 zfcp_wka_port_put(&adapter->nsp);
338 mempool_free(gid_pn, adapter->pool.data_gid_pn);
343 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
344 * @port: zfcp_port structure
345 * @plogi: plogi payload
347 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
349 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
351 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
352 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
353 if (plogi->serv_param.class1_serv_param[0] & 0x80)
354 port->supported_classes |= FC_COS_CLASS1;
355 if (plogi->serv_param.class2_serv_param[0] & 0x80)
356 port->supported_classes |= FC_COS_CLASS2;
357 if (plogi->serv_param.class3_serv_param[0] & 0x80)
358 port->supported_classes |= FC_COS_CLASS3;
359 if (plogi->serv_param.class4_serv_param[0] & 0x80)
360 port->supported_classes |= FC_COS_CLASS4;
363 struct zfcp_els_adisc {
364 struct zfcp_send_els els;
365 struct scatterlist req;
366 struct scatterlist resp;
367 struct zfcp_ls_adisc ls_adisc;
368 struct zfcp_ls_adisc ls_adisc_acc;
371 static void zfcp_fc_adisc_handler(unsigned long data)
373 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
374 struct zfcp_port *port = adisc->els.port;
375 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
377 if (adisc->els.status) {
378 /* request rejected or timed out */
379 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
384 port->wwnn = ls_adisc->wwnn;
386 if (port->wwpn != ls_adisc->wwpn)
387 zfcp_erp_port_reopen(port, 0, 64, NULL);
394 static int zfcp_fc_adisc(struct zfcp_port *port)
396 struct zfcp_els_adisc *adisc;
397 struct zfcp_adapter *adapter = port->adapter;
399 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
403 adisc->els.req = &adisc->req;
404 adisc->els.resp = &adisc->resp;
405 sg_init_one(adisc->els.req, &adisc->ls_adisc,
406 sizeof(struct zfcp_ls_adisc));
407 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
408 sizeof(struct zfcp_ls_adisc));
410 adisc->els.req_count = 1;
411 adisc->els.resp_count = 1;
412 adisc->els.adapter = adapter;
413 adisc->els.port = port;
414 adisc->els.d_id = port->d_id;
415 adisc->els.handler = zfcp_fc_adisc_handler;
416 adisc->els.handler_data = (unsigned long) adisc;
417 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
419 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
420 without FC-AL-2 capability, so we don't set it */
421 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
422 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
423 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
425 return zfcp_fsf_send_els(&adisc->els);
429 * zfcp_test_link - lightweight link test procedure
430 * @port: port to be tested
432 * Test status of a link to a remote port using the ELS command ADISC.
433 * If there is a problem with the remote port, error recovery steps
436 void zfcp_test_link(struct zfcp_port *port)
441 retval = zfcp_fc_adisc(port);
445 /* send of ADISC was not possible */
447 if (retval != -EBUSY)
448 zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
451 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
453 struct scatterlist *sg = &gpn_ft->sg_req;
455 kfree(sg_virt(sg)); /* free request buffer */
456 zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
461 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
463 struct zfcp_gpn_ft *gpn_ft;
464 struct ct_iu_gpn_ft_req *req;
466 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
470 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
476 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
478 if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
479 zfcp_free_sg_env(gpn_ft);
487 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
488 struct zfcp_adapter *adapter)
490 struct zfcp_send_ct *ct = &gpn_ft->ct;
491 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
492 struct zfcp_fc_ns_handler_data compl_rec;
495 /* prepare CT IU for GPN_FT */
496 req->header.revision = ZFCP_CT_REVISION;
497 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
498 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
499 req->header.options = ZFCP_CT_SYNCHRONOUS;
500 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
501 req->header.max_res_size = (sizeof(struct gpn_ft_resp_acc) *
502 (ZFCP_GPN_FT_MAX_ENTRIES - 1)) >> 2;
504 req->domain_id_scope = 0;
505 req->area_id_scope = 0;
506 req->fc4_type = ZFCP_CT_SCSI_FCP;
508 /* prepare zfcp_send_ct */
509 ct->wka_port = &adapter->nsp;
510 ct->handler = zfcp_fc_ns_handler;
511 ct->handler_data = (unsigned long)&compl_rec;
513 ct->req = &gpn_ft->sg_req;
514 ct->resp = gpn_ft->sg_resp;
516 ct->resp_count = ZFCP_GPN_FT_BUFFERS;
518 init_completion(&compl_rec.done);
519 compl_rec.handler = NULL;
520 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
522 wait_for_completion(&compl_rec.done);
526 static void zfcp_validate_port(struct zfcp_port *port)
528 struct zfcp_adapter *adapter = port->adapter;
530 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
532 if ((port->supported_classes != 0) ||
533 !list_empty(&port->unit_list_head)) {
537 zfcp_erp_port_shutdown(port, 0, 151, NULL);
538 zfcp_erp_wait(adapter);
540 zfcp_port_dequeue(port);
543 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
545 struct zfcp_send_ct *ct = &gpn_ft->ct;
546 struct scatterlist *sg = gpn_ft->sg_resp;
547 struct ct_hdr *hdr = sg_virt(sg);
548 struct gpn_ft_resp_acc *acc = sg_virt(sg);
549 struct zfcp_adapter *adapter = ct->wka_port->adapter;
550 struct zfcp_port *port, *tmp;
552 int ret = 0, x, last = 0;
557 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
558 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
559 return -EAGAIN; /* might be a temporary condition */
563 if (hdr->max_res_size)
566 down(&zfcp_data.config_sema);
568 /* first entry is the header */
569 for (x = 1; x < ZFCP_GPN_FT_MAX_ENTRIES && !last; x++) {
570 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
575 last = acc->control & 0x80;
576 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
579 /* don't attach ports with a well known address */
580 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
582 /* skip the adapter's port and known remote ports */
583 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
585 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
591 port = zfcp_port_enqueue(adapter, acc->wwpn,
592 ZFCP_STATUS_PORT_DID_DID |
593 ZFCP_STATUS_COMMON_NOESC, d_id);
597 zfcp_erp_port_reopen(port, 0, 149, NULL);
600 zfcp_erp_wait(adapter);
601 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
602 zfcp_validate_port(port);
603 up(&zfcp_data.config_sema);
608 * zfcp_scan_ports - scan remote ports and attach new ports
609 * @adapter: pointer to struct zfcp_adapter
611 int zfcp_scan_ports(struct zfcp_adapter *adapter)
614 struct zfcp_gpn_ft *gpn_ft;
616 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
619 ret = zfcp_wka_port_get(&adapter->nsp);
623 gpn_ft = zfcp_alloc_sg_env();
629 for (i = 0; i < 3; i++) {
630 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
632 ret = zfcp_scan_eval_gpn_ft(gpn_ft);
639 zfcp_free_sg_env(gpn_ft);
641 zfcp_wka_port_put(&adapter->nsp);
646 void _zfcp_scan_ports_later(struct work_struct *work)
648 zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));