Merge branch 'devel' into next
[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 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include "zfcp_ext.h"
13
14 struct ct_iu_gpn_ft_req {
15         struct ct_hdr header;
16         u8 flags;
17         u8 domain_id_scope;
18         u8 area_id_scope;
19         u8 fc4_type;
20 } __attribute__ ((packed));
21
22 struct gpn_ft_resp_acc {
23         u8 control;
24         u8 port_id[3];
25         u8 reserved[4];
26         u64 wwpn;
27 } __attribute__ ((packed));
28
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)
33
34 struct ct_iu_gpn_ft_resp {
35         struct ct_hdr header;
36         struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
37 } __attribute__ ((packed));
38
39 struct zfcp_gpn_ft {
40         struct zfcp_send_ct ct;
41         struct scatterlist sg_req;
42         struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
43 };
44
45 struct zfcp_fc_ns_handler_data {
46         struct completion done;
47         void (*handler)(unsigned long);
48         unsigned long handler_data;
49 };
50
51 static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
52 {
53         if (mutex_lock_interruptible(&wka_port->mutex))
54                 return -ERESTARTSYS;
55
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;
61         }
62
63         mutex_unlock(&wka_port->mutex);
64
65         wait_event_timeout(
66                 wka_port->completion_wq,
67                 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
68                 wka_port->status == ZFCP_WKA_PORT_OFFLINE,
69                 HZ >> 1);
70
71         if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
72                 atomic_inc(&wka_port->refcount);
73                 return 0;
74         }
75         return -EIO;
76 }
77
78 static void zfcp_wka_port_offline(struct work_struct *work)
79 {
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);
83
84         wait_event(wka_port->completion_wq,
85                         atomic_read(&wka_port->refcount) == 0);
86
87         mutex_lock(&wka_port->mutex);
88         if ((atomic_read(&wka_port->refcount) != 0) ||
89             (wka_port->status != ZFCP_WKA_PORT_ONLINE))
90                 goto out;
91
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);
96         }
97 out:
98         mutex_unlock(&wka_port->mutex);
99 }
100
101 static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
102 {
103         if (atomic_dec_return(&wka_port->refcount) != 0)
104                 return;
105         /* wait 10 miliseconds, other reqs might pop in */
106         schedule_delayed_work(&wka_port->work, HZ / 100);
107 }
108
109 void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
110 {
111         struct zfcp_wka_port *wka_port = &adapter->nsp;
112
113         init_waitqueue_head(&wka_port->completion_wq);
114
115         wka_port->adapter = adapter;
116         wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
117
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);
122 }
123
124 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
125                                    struct fcp_rscn_element *elem)
126 {
127         unsigned long flags;
128         struct zfcp_port *port;
129
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,
136                                              82, fsf_req);
137                 else if ((port->d_id & range) == (elem->nport_did & range))
138                         /* Check connection status for connected ports */
139                         zfcp_test_link(port);
140         }
141         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
142 }
143
144 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
145 {
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;
149         u16 i;
150         u16 no_entries;
151         u32 range_mask;
152
153         fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
154         fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
155
156         /* see FC-FS */
157         no_entries = fcp_rscn_head->payload_len /
158                         sizeof(struct fcp_rscn_element);
159
160         for (i = 1; i < no_entries; i++) {
161                 /* skip head and start with 1st element */
162                 fcp_rscn_element++;
163                 switch (fcp_rscn_element->addr_format) {
164                 case ZFCP_PORT_ADDRESS:
165                         range_mask = ZFCP_PORTS_RANGE_PORT;
166                         break;
167                 case ZFCP_AREA_ADDRESS:
168                         range_mask = ZFCP_PORTS_RANGE_AREA;
169                         break;
170                 case ZFCP_DOMAIN_ADDRESS:
171                         range_mask = ZFCP_PORTS_RANGE_DOMAIN;
172                         break;
173                 case ZFCP_FABRIC_ADDRESS:
174                         range_mask = ZFCP_PORTS_RANGE_FABRIC;
175                         break;
176                 default:
177                         continue;
178                 }
179                 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
180         }
181         schedule_work(&fsf_req->adapter->scan_work);
182 }
183
184 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
185 {
186         struct zfcp_adapter *adapter = req->adapter;
187         struct zfcp_port *port;
188         unsigned long flags;
189
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)
193                         break;
194         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
195
196         if (port && (port->wwpn == wwpn))
197                 zfcp_erp_port_forced_reopen(port, 0, 83, req);
198 }
199
200 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
201 {
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;
206
207         zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
208 }
209
210 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
211 {
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;
216
217         zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
218 }
219
220 /**
221  * zfcp_fc_incoming_els - handle incoming ELS
222  * @fsf_req - request which contains incoming ELS
223  */
224 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
225 {
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];
229
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);
237 }
238
239 static void zfcp_fc_ns_handler(unsigned long data)
240 {
241         struct zfcp_fc_ns_handler_data *compl_rec =
242                         (struct zfcp_fc_ns_handler_data *) data;
243
244         if (compl_rec->handler)
245                 compl_rec->handler(compl_rec->handler_data);
246
247         complete(&compl_rec->done);
248 }
249
250 static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
251 {
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;
257
258         if (ct->status)
259                 return;
260         if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
261                 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
262                 return;
263         }
264         /* paranoia */
265         if (ct_iu_req->wwpn != port->wwpn)
266                 return;
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);
270 }
271
272 int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
273                                      struct zfcp_gid_pn_data *gid_pn)
274 {
275         struct zfcp_adapter *adapter = erp_action->adapter;
276         struct zfcp_fc_ns_handler_data compl_rec;
277         int ret;
278
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));
293
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;
302
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,
307                                erp_action);
308         if (!ret)
309                 wait_for_completion(&compl_rec.done);
310         return ret;
311 }
312
313 /**
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
317  */
318 int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
319 {
320         int ret;
321         struct zfcp_gid_pn_data *gid_pn;
322         struct zfcp_adapter *adapter = erp_action->adapter;
323
324         gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
325         if (!gid_pn)
326                 return -ENOMEM;
327
328         memset(gid_pn, 0, sizeof(*gid_pn));
329
330         ret = zfcp_wka_port_get(&adapter->nsp);
331         if (ret)
332                 goto out;
333
334         ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
335
336         zfcp_wka_port_put(&adapter->nsp);
337 out:
338         mempool_free(gid_pn, adapter->pool.data_gid_pn);
339         return ret;
340 }
341
342 /**
343  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
344  * @port: zfcp_port structure
345  * @plogi: plogi payload
346  *
347  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
348  */
349 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
350 {
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;
361 }
362
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;
369 };
370
371 static void zfcp_fc_adisc_handler(unsigned long data)
372 {
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;
376
377         if (adisc->els.status) {
378                 /* request rejected or timed out */
379                 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
380                 goto out;
381         }
382
383         if (!port->wwnn)
384                 port->wwnn = ls_adisc->wwnn;
385
386         if (port->wwpn != ls_adisc->wwpn)
387                 zfcp_erp_port_reopen(port, 0, 64, NULL);
388
389  out:
390         zfcp_port_put(port);
391         kfree(adisc);
392 }
393
394 static int zfcp_fc_adisc(struct zfcp_port *port)
395 {
396         struct zfcp_els_adisc *adisc;
397         struct zfcp_adapter *adapter = port->adapter;
398
399         adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
400         if (!adisc)
401                 return -ENOMEM;
402
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));
409
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;
418
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);
424
425         return zfcp_fsf_send_els(&adisc->els);
426 }
427
428 /**
429  * zfcp_test_link - lightweight link test procedure
430  * @port: port to be tested
431  *
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
434  * will be triggered.
435  */
436 void zfcp_test_link(struct zfcp_port *port)
437 {
438         int retval;
439
440         zfcp_port_get(port);
441         retval = zfcp_fc_adisc(port);
442         if (retval == 0)
443                 return;
444
445         /* send of ADISC was not possible */
446         zfcp_port_put(port);
447         if (retval != -EBUSY)
448                 zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
449 }
450
451 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
452 {
453         struct scatterlist *sg = &gpn_ft->sg_req;
454
455         kfree(sg_virt(sg)); /* free request buffer */
456         zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
457
458         kfree(gpn_ft);
459 }
460
461 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
462 {
463         struct zfcp_gpn_ft *gpn_ft;
464         struct ct_iu_gpn_ft_req *req;
465
466         gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
467         if (!gpn_ft)
468                 return NULL;
469
470         req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
471         if (!req) {
472                 kfree(gpn_ft);
473                 gpn_ft = NULL;
474                 goto out;
475         }
476         sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
477
478         if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
479                 zfcp_free_sg_env(gpn_ft);
480                 gpn_ft = NULL;
481         }
482 out:
483         return gpn_ft;
484 }
485
486
487 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
488                                   struct zfcp_adapter *adapter)
489 {
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;
493         int ret;
494
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;
503         req->flags = 0;
504         req->domain_id_scope = 0;
505         req->area_id_scope = 0;
506         req->fc4_type = ZFCP_CT_SCSI_FCP;
507
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;
512         ct->timeout = 10;
513         ct->req = &gpn_ft->sg_req;
514         ct->resp = gpn_ft->sg_resp;
515         ct->req_count = 1;
516         ct->resp_count = ZFCP_GPN_FT_BUFFERS;
517
518         init_completion(&compl_rec.done);
519         compl_rec.handler = NULL;
520         ret = zfcp_fsf_send_ct(ct, NULL, NULL);
521         if (!ret)
522                 wait_for_completion(&compl_rec.done);
523         return ret;
524 }
525
526 static void zfcp_validate_port(struct zfcp_port *port)
527 {
528         struct zfcp_adapter *adapter = port->adapter;
529
530         atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
531
532         if ((port->supported_classes != 0) ||
533             !list_empty(&port->unit_list_head)) {
534                 zfcp_port_put(port);
535                 return;
536         }
537         zfcp_erp_port_shutdown(port, 0, 151, NULL);
538         zfcp_erp_wait(adapter);
539         zfcp_port_put(port);
540         zfcp_port_dequeue(port);
541 }
542
543 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
544 {
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;
551         u32 d_id;
552         int ret = 0, x, last = 0;
553
554         if (ct->status)
555                 return -EIO;
556
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 */
560                 return -EIO;
561         }
562
563         if (hdr->max_res_size)
564                 return -E2BIG;
565
566         down(&zfcp_data.config_sema);
567
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))
571                         acc++;
572                 else
573                         acc = sg_virt(++sg);
574
575                 last = acc->control & 0x80;
576                 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
577                        acc->port_id[2];
578
579                 /* don't attach ports with a well known address */
580                 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
581                         continue;
582                 /* skip the adapter's port and known remote ports */
583                 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
584                         continue;
585                 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
586                 if (port) {
587                         zfcp_port_get(port);
588                         continue;
589                 }
590
591                 port = zfcp_port_enqueue(adapter, acc->wwpn,
592                                          ZFCP_STATUS_PORT_DID_DID |
593                                          ZFCP_STATUS_COMMON_NOESC, d_id);
594                 if (IS_ERR(port))
595                         ret = PTR_ERR(port);
596                 else
597                         zfcp_erp_port_reopen(port, 0, 149, NULL);
598         }
599
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);
604         return ret;
605 }
606
607 /**
608  * zfcp_scan_ports - scan remote ports and attach new ports
609  * @adapter: pointer to struct zfcp_adapter
610  */
611 int zfcp_scan_ports(struct zfcp_adapter *adapter)
612 {
613         int ret, i;
614         struct zfcp_gpn_ft *gpn_ft;
615
616         if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
617                 return 0;
618
619         ret = zfcp_wka_port_get(&adapter->nsp);
620         if (ret)
621                 return ret;
622
623         gpn_ft = zfcp_alloc_sg_env();
624         if (!gpn_ft) {
625                 ret = -ENOMEM;
626                 goto out;
627         }
628
629         for (i = 0; i < 3; i++) {
630                 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
631                 if (!ret) {
632                         ret = zfcp_scan_eval_gpn_ft(gpn_ft);
633                         if (ret == -EAGAIN)
634                                 ssleep(1);
635                         else
636                                 break;
637                 }
638         }
639         zfcp_free_sg_env(gpn_ft);
640 out:
641         zfcp_wka_port_put(&adapter->nsp);
642         return ret;
643 }
644
645
646 void _zfcp_scan_ports_later(struct work_struct *work)
647 {
648         zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
649 }