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 if (!(atomic_read(&port->status) & ZFCP_STATUS_PORT_PHYS_OPEN))
129 /* Try to connect to unused ports anyway. */
130 zfcp_erp_port_reopen(port,
131 ZFCP_STATUS_COMMON_ERP_FAILED,
133 else if ((port->d_id & range) == (elem->nport_did & range))
134 /* Check connection status for connected ports */
135 zfcp_test_link(port);
137 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
140 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
142 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
143 struct fcp_rscn_head *fcp_rscn_head;
144 struct fcp_rscn_element *fcp_rscn_element;
149 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
150 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
153 no_entries = fcp_rscn_head->payload_len /
154 sizeof(struct fcp_rscn_element);
156 for (i = 1; i < no_entries; i++) {
157 /* skip head and start with 1st element */
159 switch (fcp_rscn_element->addr_format) {
160 case ZFCP_PORT_ADDRESS:
161 range_mask = ZFCP_PORTS_RANGE_PORT;
163 case ZFCP_AREA_ADDRESS:
164 range_mask = ZFCP_PORTS_RANGE_AREA;
166 case ZFCP_DOMAIN_ADDRESS:
167 range_mask = ZFCP_PORTS_RANGE_DOMAIN;
169 case ZFCP_FABRIC_ADDRESS:
170 range_mask = ZFCP_PORTS_RANGE_FABRIC;
175 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
177 schedule_work(&fsf_req->adapter->scan_work);
180 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
182 struct zfcp_adapter *adapter = req->adapter;
183 struct zfcp_port *port;
186 read_lock_irqsave(&zfcp_data.config_lock, flags);
187 list_for_each_entry(port, &adapter->port_list_head, list)
188 if (port->wwpn == wwpn)
190 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
192 if (port && (port->wwpn == wwpn))
193 zfcp_erp_port_forced_reopen(port, 0, 83, req);
196 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
198 struct fsf_status_read_buffer *status_buffer =
199 (struct fsf_status_read_buffer *)req->data;
200 struct fsf_plogi *els_plogi =
201 (struct fsf_plogi *) status_buffer->payload.data;
203 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
206 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
208 struct fsf_status_read_buffer *status_buffer =
209 (struct fsf_status_read_buffer *)req->data;
210 struct fcp_logo *els_logo =
211 (struct fcp_logo *) status_buffer->payload.data;
213 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
217 * zfcp_fc_incoming_els - handle incoming ELS
218 * @fsf_req - request which contains incoming ELS
220 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
222 struct fsf_status_read_buffer *status_buffer =
223 (struct fsf_status_read_buffer *) fsf_req->data;
224 unsigned int els_type = status_buffer->payload.data[0];
226 zfcp_san_dbf_event_incoming_els(fsf_req);
227 if (els_type == LS_PLOGI)
228 zfcp_fc_incoming_plogi(fsf_req);
229 else if (els_type == LS_LOGO)
230 zfcp_fc_incoming_logo(fsf_req);
231 else if (els_type == LS_RSCN)
232 zfcp_fc_incoming_rscn(fsf_req);
235 static void zfcp_fc_ns_handler(unsigned long data)
237 struct zfcp_fc_ns_handler_data *compl_rec =
238 (struct zfcp_fc_ns_handler_data *) data;
240 if (compl_rec->handler)
241 compl_rec->handler(compl_rec->handler_data);
243 complete(&compl_rec->done);
246 static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
248 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
249 struct zfcp_send_ct *ct = &gid_pn->ct;
250 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
251 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
252 struct zfcp_port *port = gid_pn->port;
256 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
257 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
261 if (ct_iu_req->wwpn != port->wwpn)
263 /* looks like a valid d_id */
264 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
265 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
268 int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
269 struct zfcp_gid_pn_data *gid_pn)
271 struct zfcp_adapter *adapter = erp_action->adapter;
272 struct zfcp_fc_ns_handler_data compl_rec;
275 /* setup parameters for send generic command */
276 gid_pn->port = erp_action->port;
277 gid_pn->ct.wka_port = &adapter->nsp;
278 gid_pn->ct.handler = zfcp_fc_ns_handler;
279 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
280 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
281 gid_pn->ct.req = &gid_pn->req;
282 gid_pn->ct.resp = &gid_pn->resp;
283 gid_pn->ct.req_count = 1;
284 gid_pn->ct.resp_count = 1;
285 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
286 sizeof(struct ct_iu_gid_pn_req));
287 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
288 sizeof(struct ct_iu_gid_pn_resp));
290 /* setup nameserver request */
291 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
292 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
293 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
294 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
295 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
296 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_MAX_SIZE;
297 gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
299 init_completion(&compl_rec.done);
300 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
301 compl_rec.handler_data = (unsigned long) gid_pn;
302 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
305 wait_for_completion(&compl_rec.done);
310 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
311 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
312 * return: -ENOMEM on error, 0 otherwise
314 int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
317 struct zfcp_gid_pn_data *gid_pn;
318 struct zfcp_adapter *adapter = erp_action->adapter;
320 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
324 memset(gid_pn, 0, sizeof(*gid_pn));
326 ret = zfcp_wka_port_get(&adapter->nsp);
330 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
332 zfcp_wka_port_put(&adapter->nsp);
334 mempool_free(gid_pn, adapter->pool.data_gid_pn);
339 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
340 * @port: zfcp_port structure
341 * @plogi: plogi payload
343 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
345 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
347 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
348 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
349 if (plogi->serv_param.class1_serv_param[0] & 0x80)
350 port->supported_classes |= FC_COS_CLASS1;
351 if (plogi->serv_param.class2_serv_param[0] & 0x80)
352 port->supported_classes |= FC_COS_CLASS2;
353 if (plogi->serv_param.class3_serv_param[0] & 0x80)
354 port->supported_classes |= FC_COS_CLASS3;
355 if (plogi->serv_param.class4_serv_param[0] & 0x80)
356 port->supported_classes |= FC_COS_CLASS4;
359 struct zfcp_els_adisc {
360 struct zfcp_send_els els;
361 struct scatterlist req;
362 struct scatterlist resp;
363 struct zfcp_ls_adisc ls_adisc;
364 struct zfcp_ls_adisc ls_adisc_acc;
367 static void zfcp_fc_adisc_handler(unsigned long data)
369 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
370 struct zfcp_port *port = adisc->els.port;
371 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
373 if (adisc->els.status) {
374 /* request rejected or timed out */
375 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
380 port->wwnn = ls_adisc->wwnn;
382 if (port->wwpn != ls_adisc->wwpn)
383 zfcp_erp_port_reopen(port, 0, 64, NULL);
390 static int zfcp_fc_adisc(struct zfcp_port *port)
392 struct zfcp_els_adisc *adisc;
393 struct zfcp_adapter *adapter = port->adapter;
395 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
399 adisc->els.req = &adisc->req;
400 adisc->els.resp = &adisc->resp;
401 sg_init_one(adisc->els.req, &adisc->ls_adisc,
402 sizeof(struct zfcp_ls_adisc));
403 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
404 sizeof(struct zfcp_ls_adisc));
406 adisc->els.req_count = 1;
407 adisc->els.resp_count = 1;
408 adisc->els.adapter = adapter;
409 adisc->els.port = port;
410 adisc->els.d_id = port->d_id;
411 adisc->els.handler = zfcp_fc_adisc_handler;
412 adisc->els.handler_data = (unsigned long) adisc;
413 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
415 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
416 without FC-AL-2 capability, so we don't set it */
417 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
418 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
419 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
421 return zfcp_fsf_send_els(&adisc->els);
425 * zfcp_test_link - lightweight link test procedure
426 * @port: port to be tested
428 * Test status of a link to a remote port using the ELS command ADISC.
429 * If there is a problem with the remote port, error recovery steps
432 void zfcp_test_link(struct zfcp_port *port)
437 retval = zfcp_fc_adisc(port);
441 /* send of ADISC was not possible */
443 if (retval != -EBUSY)
444 zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
447 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
449 struct scatterlist *sg = &gpn_ft->sg_req;
451 kfree(sg_virt(sg)); /* free request buffer */
452 zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
457 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
459 struct zfcp_gpn_ft *gpn_ft;
460 struct ct_iu_gpn_ft_req *req;
462 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
466 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
472 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
474 if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
475 zfcp_free_sg_env(gpn_ft);
483 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
484 struct zfcp_adapter *adapter)
486 struct zfcp_send_ct *ct = &gpn_ft->ct;
487 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
488 struct zfcp_fc_ns_handler_data compl_rec;
491 /* prepare CT IU for GPN_FT */
492 req->header.revision = ZFCP_CT_REVISION;
493 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
494 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
495 req->header.options = ZFCP_CT_SYNCHRONOUS;
496 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
497 req->header.max_res_size = (sizeof(struct gpn_ft_resp_acc) *
498 (ZFCP_GPN_FT_MAX_ENTRIES - 1)) >> 2;
500 req->domain_id_scope = 0;
501 req->area_id_scope = 0;
502 req->fc4_type = ZFCP_CT_SCSI_FCP;
504 /* prepare zfcp_send_ct */
505 ct->wka_port = &adapter->nsp;
506 ct->handler = zfcp_fc_ns_handler;
507 ct->handler_data = (unsigned long)&compl_rec;
509 ct->req = &gpn_ft->sg_req;
510 ct->resp = gpn_ft->sg_resp;
512 ct->resp_count = ZFCP_GPN_FT_BUFFERS;
514 init_completion(&compl_rec.done);
515 compl_rec.handler = NULL;
516 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
518 wait_for_completion(&compl_rec.done);
522 static void zfcp_validate_port(struct zfcp_port *port)
524 struct zfcp_adapter *adapter = port->adapter;
526 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
528 if ((port->supported_classes != 0) ||
529 !list_empty(&port->unit_list_head)) {
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));