[SCSI] zfcp: fix remote port status check
[safe/jmp/linux-2.6] / drivers / s390 / scsi / zfcp_fc.c
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                 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,
132                                              82, fsf_req);
133                 else if ((port->d_id & range) == (elem->nport_did & range))
134                         /* Check connection status for connected ports */
135                         zfcp_test_link(port);
136         }
137         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
138 }
139
140 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
141 {
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;
145         u16 i;
146         u16 no_entries;
147         u32 range_mask;
148
149         fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
150         fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
151
152         /* see FC-FS */
153         no_entries = fcp_rscn_head->payload_len /
154                         sizeof(struct fcp_rscn_element);
155
156         for (i = 1; i < no_entries; i++) {
157                 /* skip head and start with 1st element */
158                 fcp_rscn_element++;
159                 switch (fcp_rscn_element->addr_format) {
160                 case ZFCP_PORT_ADDRESS:
161                         range_mask = ZFCP_PORTS_RANGE_PORT;
162                         break;
163                 case ZFCP_AREA_ADDRESS:
164                         range_mask = ZFCP_PORTS_RANGE_AREA;
165                         break;
166                 case ZFCP_DOMAIN_ADDRESS:
167                         range_mask = ZFCP_PORTS_RANGE_DOMAIN;
168                         break;
169                 case ZFCP_FABRIC_ADDRESS:
170                         range_mask = ZFCP_PORTS_RANGE_FABRIC;
171                         break;
172                 default:
173                         continue;
174                 }
175                 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
176         }
177         schedule_work(&fsf_req->adapter->scan_work);
178 }
179
180 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
181 {
182         struct zfcp_adapter *adapter = req->adapter;
183         struct zfcp_port *port;
184         unsigned long flags;
185
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)
189                         break;
190         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
191
192         if (port && (port->wwpn == wwpn))
193                 zfcp_erp_port_forced_reopen(port, 0, 83, req);
194 }
195
196 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
197 {
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;
202
203         zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
204 }
205
206 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
207 {
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;
212
213         zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
214 }
215
216 /**
217  * zfcp_fc_incoming_els - handle incoming ELS
218  * @fsf_req - request which contains incoming ELS
219  */
220 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
221 {
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];
225
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);
233 }
234
235 static void zfcp_fc_ns_handler(unsigned long data)
236 {
237         struct zfcp_fc_ns_handler_data *compl_rec =
238                         (struct zfcp_fc_ns_handler_data *) data;
239
240         if (compl_rec->handler)
241                 compl_rec->handler(compl_rec->handler_data);
242
243         complete(&compl_rec->done);
244 }
245
246 static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
247 {
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;
253
254         if (ct->status)
255                 return;
256         if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
257                 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
258                 return;
259         }
260         /* paranoia */
261         if (ct_iu_req->wwpn != port->wwpn)
262                 return;
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);
266 }
267
268 int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
269                                      struct zfcp_gid_pn_data *gid_pn)
270 {
271         struct zfcp_adapter *adapter = erp_action->adapter;
272         struct zfcp_fc_ns_handler_data compl_rec;
273         int ret;
274
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));
289
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;
298
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,
303                                erp_action);
304         if (!ret)
305                 wait_for_completion(&compl_rec.done);
306         return ret;
307 }
308
309 /**
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
313  */
314 int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
315 {
316         int ret;
317         struct zfcp_gid_pn_data *gid_pn;
318         struct zfcp_adapter *adapter = erp_action->adapter;
319
320         gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
321         if (!gid_pn)
322                 return -ENOMEM;
323
324         memset(gid_pn, 0, sizeof(*gid_pn));
325
326         ret = zfcp_wka_port_get(&adapter->nsp);
327         if (ret)
328                 goto out;
329
330         ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
331
332         zfcp_wka_port_put(&adapter->nsp);
333 out:
334         mempool_free(gid_pn, adapter->pool.data_gid_pn);
335         return ret;
336 }
337
338 /**
339  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
340  * @port: zfcp_port structure
341  * @plogi: plogi payload
342  *
343  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
344  */
345 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
346 {
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;
357 }
358
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;
365 };
366
367 static void zfcp_fc_adisc_handler(unsigned long data)
368 {
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;
372
373         if (adisc->els.status) {
374                 /* request rejected or timed out */
375                 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
376                 goto out;
377         }
378
379         if (!port->wwnn)
380                 port->wwnn = ls_adisc->wwnn;
381
382         if (port->wwpn != ls_adisc->wwpn)
383                 zfcp_erp_port_reopen(port, 0, 64, NULL);
384
385  out:
386         zfcp_port_put(port);
387         kfree(adisc);
388 }
389
390 static int zfcp_fc_adisc(struct zfcp_port *port)
391 {
392         struct zfcp_els_adisc *adisc;
393         struct zfcp_adapter *adapter = port->adapter;
394
395         adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
396         if (!adisc)
397                 return -ENOMEM;
398
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));
405
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;
414
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);
420
421         return zfcp_fsf_send_els(&adisc->els);
422 }
423
424 /**
425  * zfcp_test_link - lightweight link test procedure
426  * @port: port to be tested
427  *
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
430  * will be triggered.
431  */
432 void zfcp_test_link(struct zfcp_port *port)
433 {
434         int retval;
435
436         zfcp_port_get(port);
437         retval = zfcp_fc_adisc(port);
438         if (retval == 0)
439                 return;
440
441         /* send of ADISC was not possible */
442         zfcp_port_put(port);
443         if (retval != -EBUSY)
444                 zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
445 }
446
447 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
448 {
449         struct scatterlist *sg = &gpn_ft->sg_req;
450
451         kfree(sg_virt(sg)); /* free request buffer */
452         zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
453
454         kfree(gpn_ft);
455 }
456
457 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
458 {
459         struct zfcp_gpn_ft *gpn_ft;
460         struct ct_iu_gpn_ft_req *req;
461
462         gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
463         if (!gpn_ft)
464                 return NULL;
465
466         req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
467         if (!req) {
468                 kfree(gpn_ft);
469                 gpn_ft = NULL;
470                 goto out;
471         }
472         sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
473
474         if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
475                 zfcp_free_sg_env(gpn_ft);
476                 gpn_ft = NULL;
477         }
478 out:
479         return gpn_ft;
480 }
481
482
483 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
484                                   struct zfcp_adapter *adapter)
485 {
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;
489         int ret;
490
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;
499         req->flags = 0;
500         req->domain_id_scope = 0;
501         req->area_id_scope = 0;
502         req->fc4_type = ZFCP_CT_SCSI_FCP;
503
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;
508         ct->timeout = 10;
509         ct->req = &gpn_ft->sg_req;
510         ct->resp = gpn_ft->sg_resp;
511         ct->req_count = 1;
512         ct->resp_count = ZFCP_GPN_FT_BUFFERS;
513
514         init_completion(&compl_rec.done);
515         compl_rec.handler = NULL;
516         ret = zfcp_fsf_send_ct(ct, NULL, NULL);
517         if (!ret)
518                 wait_for_completion(&compl_rec.done);
519         return ret;
520 }
521
522 static void zfcp_validate_port(struct zfcp_port *port)
523 {
524         struct zfcp_adapter *adapter = port->adapter;
525
526         atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
527
528         if ((port->supported_classes != 0) ||
529             !list_empty(&port->unit_list_head)) {
530                 zfcp_port_put(port);
531                 return;
532         }
533         zfcp_erp_port_shutdown(port, 0, 151, NULL);
534         zfcp_erp_wait(adapter);
535         zfcp_port_put(port);
536         zfcp_port_dequeue(port);
537 }
538
539 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
540 {
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;
547         u32 d_id;
548         int ret = 0, x, last = 0;
549
550         if (ct->status)
551                 return -EIO;
552
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 */
556                 return -EIO;
557         }
558
559         if (hdr->max_res_size)
560                 return -E2BIG;
561
562         down(&zfcp_data.config_sema);
563
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))
567                         acc++;
568                 else
569                         acc = sg_virt(++sg);
570
571                 last = acc->control & 0x80;
572                 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
573                        acc->port_id[2];
574
575                 /* don't attach ports with a well known address */
576                 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
577                         continue;
578                 /* skip the adapter's port and known remote ports */
579                 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
580                         continue;
581                 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
582                 if (port) {
583                         zfcp_port_get(port);
584                         continue;
585                 }
586
587                 port = zfcp_port_enqueue(adapter, acc->wwpn,
588                                          ZFCP_STATUS_PORT_DID_DID |
589                                          ZFCP_STATUS_COMMON_NOESC, d_id);
590                 if (IS_ERR(port))
591                         ret = PTR_ERR(port);
592                 else
593                         zfcp_erp_port_reopen(port, 0, 149, NULL);
594         }
595
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);
600         return ret;
601 }
602
603 /**
604  * zfcp_scan_ports - scan remote ports and attach new ports
605  * @adapter: pointer to struct zfcp_adapter
606  */
607 int zfcp_scan_ports(struct zfcp_adapter *adapter)
608 {
609         int ret, i;
610         struct zfcp_gpn_ft *gpn_ft;
611
612         zfcp_erp_wait(adapter); /* wait until adapter is finished with ERP */
613         if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
614                 return 0;
615
616         ret = zfcp_wka_port_get(&adapter->nsp);
617         if (ret)
618                 return ret;
619
620         gpn_ft = zfcp_alloc_sg_env();
621         if (!gpn_ft) {
622                 ret = -ENOMEM;
623                 goto out;
624         }
625
626         for (i = 0; i < 3; i++) {
627                 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
628                 if (!ret) {
629                         ret = zfcp_scan_eval_gpn_ft(gpn_ft);
630                         if (ret == -EAGAIN)
631                                 ssleep(1);
632                         else
633                                 break;
634                 }
635         }
636         zfcp_free_sg_env(gpn_ft);
637 out:
638         zfcp_wka_port_put(&adapter->nsp);
639         return ret;
640 }
641
642
643 void _zfcp_scan_ports_later(struct work_struct *work)
644 {
645         zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
646 }