Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[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, 2009
7  */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include "zfcp_ext.h"
13
14 enum rscn_address_format {
15         RSCN_PORT_ADDRESS       = 0x0,
16         RSCN_AREA_ADDRESS       = 0x1,
17         RSCN_DOMAIN_ADDRESS     = 0x2,
18         RSCN_FABRIC_ADDRESS     = 0x3,
19 };
20
21 static u32 rscn_range_mask[] = {
22         [RSCN_PORT_ADDRESS]             = 0xFFFFFF,
23         [RSCN_AREA_ADDRESS]             = 0xFFFF00,
24         [RSCN_DOMAIN_ADDRESS]           = 0xFF0000,
25         [RSCN_FABRIC_ADDRESS]           = 0x000000,
26 };
27
28 struct ct_iu_gpn_ft_req {
29         struct ct_hdr header;
30         u8 flags;
31         u8 domain_id_scope;
32         u8 area_id_scope;
33         u8 fc4_type;
34 } __attribute__ ((packed));
35
36 struct gpn_ft_resp_acc {
37         u8 control;
38         u8 port_id[3];
39         u8 reserved[4];
40         u64 wwpn;
41 } __attribute__ ((packed));
42
43 #define ZFCP_CT_SIZE_ONE_PAGE   (PAGE_SIZE - sizeof(struct ct_hdr))
44 #define ZFCP_GPN_FT_ENTRIES     (ZFCP_CT_SIZE_ONE_PAGE \
45                                         / sizeof(struct gpn_ft_resp_acc))
46 #define ZFCP_GPN_FT_BUFFERS 4
47 #define ZFCP_GPN_FT_MAX_SIZE (ZFCP_GPN_FT_BUFFERS * PAGE_SIZE \
48                                 - sizeof(struct ct_hdr))
49 #define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
50
51 struct ct_iu_gpn_ft_resp {
52         struct ct_hdr header;
53         struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
54 } __attribute__ ((packed));
55
56 struct zfcp_gpn_ft {
57         struct zfcp_send_ct ct;
58         struct scatterlist sg_req;
59         struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
60 };
61
62 struct zfcp_fc_ns_handler_data {
63         struct completion done;
64         void (*handler)(unsigned long);
65         unsigned long handler_data;
66 };
67
68 static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
69 {
70         if (mutex_lock_interruptible(&wka_port->mutex))
71                 return -ERESTARTSYS;
72
73         if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
74             wka_port->status == ZFCP_WKA_PORT_CLOSING) {
75                 wka_port->status = ZFCP_WKA_PORT_OPENING;
76                 if (zfcp_fsf_open_wka_port(wka_port))
77                         wka_port->status = ZFCP_WKA_PORT_OFFLINE;
78         }
79
80         mutex_unlock(&wka_port->mutex);
81
82         wait_event_timeout(
83                 wka_port->completion_wq,
84                 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
85                 wka_port->status == ZFCP_WKA_PORT_OFFLINE,
86                 HZ >> 1);
87
88         if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
89                 atomic_inc(&wka_port->refcount);
90                 return 0;
91         }
92         return -EIO;
93 }
94
95 static void zfcp_wka_port_offline(struct work_struct *work)
96 {
97         struct delayed_work *dw = to_delayed_work(work);
98         struct zfcp_wka_port *wka_port =
99                         container_of(dw, struct zfcp_wka_port, work);
100
101         mutex_lock(&wka_port->mutex);
102         if ((atomic_read(&wka_port->refcount) != 0) ||
103             (wka_port->status != ZFCP_WKA_PORT_ONLINE))
104                 goto out;
105
106         wka_port->status = ZFCP_WKA_PORT_CLOSING;
107         if (zfcp_fsf_close_wka_port(wka_port)) {
108                 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
109                 wake_up(&wka_port->completion_wq);
110         }
111 out:
112         mutex_unlock(&wka_port->mutex);
113 }
114
115 static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
116 {
117         if (atomic_dec_return(&wka_port->refcount) != 0)
118                 return;
119         /* wait 10 milliseconds, other reqs might pop in */
120         schedule_delayed_work(&wka_port->work, HZ / 100);
121 }
122
123 static void zfcp_fc_wka_port_init(struct zfcp_wka_port *wka_port, u32 d_id,
124                                   struct zfcp_adapter *adapter)
125 {
126         init_waitqueue_head(&wka_port->completion_wq);
127
128         wka_port->adapter = adapter;
129         wka_port->d_id = d_id;
130
131         wka_port->status = ZFCP_WKA_PORT_OFFLINE;
132         atomic_set(&wka_port->refcount, 0);
133         mutex_init(&wka_port->mutex);
134         INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
135 }
136
137 void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
138 {
139         cancel_delayed_work_sync(&wka->work);
140         mutex_lock(&wka->mutex);
141         wka->status = ZFCP_WKA_PORT_OFFLINE;
142         mutex_unlock(&wka->mutex);
143 }
144
145 void zfcp_fc_wka_ports_init(struct zfcp_adapter *adapter)
146 {
147         struct zfcp_wka_ports *gs = adapter->gs;
148
149         zfcp_fc_wka_port_init(&gs->ms, FC_FID_MGMT_SERV, adapter);
150         zfcp_fc_wka_port_init(&gs->ts, FC_FID_TIME_SERV, adapter);
151         zfcp_fc_wka_port_init(&gs->ds, FC_FID_DIR_SERV, adapter);
152         zfcp_fc_wka_port_init(&gs->as, FC_FID_ALIASES, adapter);
153         zfcp_fc_wka_port_init(&gs->ks, FC_FID_SEC_KEY, adapter);
154 }
155
156 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
157                                    struct fcp_rscn_element *elem)
158 {
159         unsigned long flags;
160         struct zfcp_port *port;
161
162         read_lock_irqsave(&zfcp_data.config_lock, flags);
163         list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
164                 if ((port->d_id & range) == (elem->nport_did & range))
165                         zfcp_test_link(port);
166                 if (!port->d_id)
167                         zfcp_erp_port_reopen(port,
168                                              ZFCP_STATUS_COMMON_ERP_FAILED,
169                                              "fcrscn1", NULL);
170         }
171
172         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
173 }
174
175 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
176 {
177         struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
178         struct fcp_rscn_head *fcp_rscn_head;
179         struct fcp_rscn_element *fcp_rscn_element;
180         u16 i;
181         u16 no_entries;
182         u32 range_mask;
183
184         fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
185         fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
186
187         /* see FC-FS */
188         no_entries = fcp_rscn_head->payload_len /
189                         sizeof(struct fcp_rscn_element);
190
191         for (i = 1; i < no_entries; i++) {
192                 /* skip head and start with 1st element */
193                 fcp_rscn_element++;
194                 range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
195                 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
196         }
197         schedule_work(&fsf_req->adapter->scan_work);
198 }
199
200 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
201 {
202         struct zfcp_adapter *adapter = req->adapter;
203         struct zfcp_port *port;
204         unsigned long flags;
205
206         read_lock_irqsave(&zfcp_data.config_lock, flags);
207         list_for_each_entry(port, &adapter->port_list_head, list)
208                 if (port->wwpn == wwpn)
209                         break;
210         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
211
212         if (port && (port->wwpn == wwpn))
213                 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
214 }
215
216 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
217 {
218         struct fsf_status_read_buffer *status_buffer =
219                 (struct fsf_status_read_buffer *)req->data;
220         struct fsf_plogi *els_plogi =
221                 (struct fsf_plogi *) status_buffer->payload.data;
222
223         zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
224 }
225
226 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
227 {
228         struct fsf_status_read_buffer *status_buffer =
229                 (struct fsf_status_read_buffer *)req->data;
230         struct fcp_logo *els_logo =
231                 (struct fcp_logo *) status_buffer->payload.data;
232
233         zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
234 }
235
236 /**
237  * zfcp_fc_incoming_els - handle incoming ELS
238  * @fsf_req - request which contains incoming ELS
239  */
240 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
241 {
242         struct fsf_status_read_buffer *status_buffer =
243                 (struct fsf_status_read_buffer *) fsf_req->data;
244         unsigned int els_type = status_buffer->payload.data[0];
245
246         zfcp_san_dbf_event_incoming_els(fsf_req);
247         if (els_type == LS_PLOGI)
248                 zfcp_fc_incoming_plogi(fsf_req);
249         else if (els_type == LS_LOGO)
250                 zfcp_fc_incoming_logo(fsf_req);
251         else if (els_type == LS_RSCN)
252                 zfcp_fc_incoming_rscn(fsf_req);
253 }
254
255 static void zfcp_fc_ns_handler(unsigned long data)
256 {
257         struct zfcp_fc_ns_handler_data *compl_rec =
258                         (struct zfcp_fc_ns_handler_data *) data;
259
260         if (compl_rec->handler)
261                 compl_rec->handler(compl_rec->handler_data);
262
263         complete(&compl_rec->done);
264 }
265
266 static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
267 {
268         struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
269         struct zfcp_send_ct *ct = &gid_pn->ct;
270         struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
271         struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
272         struct zfcp_port *port = gid_pn->port;
273
274         if (ct->status)
275                 return;
276         if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
277                 return;
278
279         /* paranoia */
280         if (ct_iu_req->wwpn != port->wwpn)
281                 return;
282         /* looks like a valid d_id */
283         port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
284 }
285
286 int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
287                                      struct zfcp_gid_pn_data *gid_pn)
288 {
289         struct zfcp_adapter *adapter = erp_action->adapter;
290         struct zfcp_fc_ns_handler_data compl_rec;
291         int ret;
292
293         /* setup parameters for send generic command */
294         gid_pn->port = erp_action->port;
295         gid_pn->ct.wka_port = &adapter->gs->ds;
296         gid_pn->ct.handler = zfcp_fc_ns_handler;
297         gid_pn->ct.handler_data = (unsigned long) &compl_rec;
298         gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
299         gid_pn->ct.req = &gid_pn->req;
300         gid_pn->ct.resp = &gid_pn->resp;
301         sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
302                     sizeof(struct ct_iu_gid_pn_req));
303         sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
304                     sizeof(struct ct_iu_gid_pn_resp));
305
306         /* setup nameserver request */
307         gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
308         gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
309         gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
310         gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
311         gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
312         gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
313         gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
314
315         init_completion(&compl_rec.done);
316         compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
317         compl_rec.handler_data = (unsigned long) gid_pn;
318         ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
319                                erp_action);
320         if (!ret)
321                 wait_for_completion(&compl_rec.done);
322         return ret;
323 }
324
325 /**
326  * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
327  * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
328  * return: -ENOMEM on error, 0 otherwise
329  */
330 int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
331 {
332         int ret;
333         struct zfcp_gid_pn_data *gid_pn;
334         struct zfcp_adapter *adapter = erp_action->adapter;
335
336         gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
337         if (!gid_pn)
338                 return -ENOMEM;
339
340         memset(gid_pn, 0, sizeof(*gid_pn));
341
342         ret = zfcp_wka_port_get(&adapter->gs->ds);
343         if (ret)
344                 goto out;
345
346         ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
347
348         zfcp_wka_port_put(&adapter->gs->ds);
349 out:
350         mempool_free(gid_pn, adapter->pool.data_gid_pn);
351         return ret;
352 }
353
354 /**
355  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
356  * @port: zfcp_port structure
357  * @plogi: plogi payload
358  *
359  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
360  */
361 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
362 {
363         port->maxframe_size = plogi->serv_param.common_serv_param[7] |
364                 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
365         if (plogi->serv_param.class1_serv_param[0] & 0x80)
366                 port->supported_classes |= FC_COS_CLASS1;
367         if (plogi->serv_param.class2_serv_param[0] & 0x80)
368                 port->supported_classes |= FC_COS_CLASS2;
369         if (plogi->serv_param.class3_serv_param[0] & 0x80)
370                 port->supported_classes |= FC_COS_CLASS3;
371         if (plogi->serv_param.class4_serv_param[0] & 0x80)
372                 port->supported_classes |= FC_COS_CLASS4;
373 }
374
375 struct zfcp_els_adisc {
376         struct zfcp_send_els els;
377         struct scatterlist req;
378         struct scatterlist resp;
379         struct zfcp_ls_adisc ls_adisc;
380         struct zfcp_ls_adisc ls_adisc_acc;
381 };
382
383 static void zfcp_fc_adisc_handler(unsigned long data)
384 {
385         struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
386         struct zfcp_port *port = adisc->els.port;
387         struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
388
389         if (adisc->els.status) {
390                 /* request rejected or timed out */
391                 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
392                                             "fcadh_1", NULL);
393                 goto out;
394         }
395
396         if (!port->wwnn)
397                 port->wwnn = ls_adisc->wwnn;
398
399         if ((port->wwpn != ls_adisc->wwpn) ||
400             !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
401                 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
402                                      "fcadh_2", NULL);
403                 goto out;
404         }
405
406         /* port is good, unblock rport without going through erp */
407         zfcp_scsi_schedule_rport_register(port);
408  out:
409         zfcp_port_put(port);
410         kfree(adisc);
411 }
412
413 static int zfcp_fc_adisc(struct zfcp_port *port)
414 {
415         struct zfcp_els_adisc *adisc;
416         struct zfcp_adapter *adapter = port->adapter;
417
418         adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
419         if (!adisc)
420                 return -ENOMEM;
421
422         adisc->els.req = &adisc->req;
423         adisc->els.resp = &adisc->resp;
424         sg_init_one(adisc->els.req, &adisc->ls_adisc,
425                     sizeof(struct zfcp_ls_adisc));
426         sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
427                     sizeof(struct zfcp_ls_adisc));
428
429         adisc->els.adapter = adapter;
430         adisc->els.port = port;
431         adisc->els.d_id = port->d_id;
432         adisc->els.handler = zfcp_fc_adisc_handler;
433         adisc->els.handler_data = (unsigned long) adisc;
434         adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
435
436         /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
437            without FC-AL-2 capability, so we don't set it */
438         adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
439         adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
440         adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
441
442         return zfcp_fsf_send_els(&adisc->els);
443 }
444
445 void zfcp_fc_link_test_work(struct work_struct *work)
446 {
447         struct zfcp_port *port =
448                 container_of(work, struct zfcp_port, test_link_work);
449         int retval;
450
451         zfcp_port_get(port);
452         port->rport_task = RPORT_DEL;
453         zfcp_scsi_rport_work(&port->rport_work);
454
455         retval = zfcp_fc_adisc(port);
456         if (retval == 0)
457                 return;
458
459         /* send of ADISC was not possible */
460         zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
461
462         zfcp_port_put(port);
463 }
464
465 /**
466  * zfcp_test_link - lightweight link test procedure
467  * @port: port to be tested
468  *
469  * Test status of a link to a remote port using the ELS command ADISC.
470  * If there is a problem with the remote port, error recovery steps
471  * will be triggered.
472  */
473 void zfcp_test_link(struct zfcp_port *port)
474 {
475         zfcp_port_get(port);
476         if (!queue_work(zfcp_data.work_queue, &port->test_link_work))
477                 zfcp_port_put(port);
478 }
479
480 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
481 {
482         struct scatterlist *sg = &gpn_ft->sg_req;
483
484         kfree(sg_virt(sg)); /* free request buffer */
485         zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
486
487         kfree(gpn_ft);
488 }
489
490 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
491 {
492         struct zfcp_gpn_ft *gpn_ft;
493         struct ct_iu_gpn_ft_req *req;
494
495         gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
496         if (!gpn_ft)
497                 return NULL;
498
499         req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
500         if (!req) {
501                 kfree(gpn_ft);
502                 gpn_ft = NULL;
503                 goto out;
504         }
505         sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
506
507         if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
508                 zfcp_free_sg_env(gpn_ft, buf_num);
509                 gpn_ft = NULL;
510         }
511 out:
512         return gpn_ft;
513 }
514
515
516 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
517                                   struct zfcp_adapter *adapter,
518                                   int max_bytes)
519 {
520         struct zfcp_send_ct *ct = &gpn_ft->ct;
521         struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
522         struct zfcp_fc_ns_handler_data compl_rec;
523         int ret;
524
525         /* prepare CT IU for GPN_FT */
526         req->header.revision = ZFCP_CT_REVISION;
527         req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
528         req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
529         req->header.options = ZFCP_CT_SYNCHRONOUS;
530         req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
531         req->header.max_res_size = max_bytes / 4;
532         req->flags = 0;
533         req->domain_id_scope = 0;
534         req->area_id_scope = 0;
535         req->fc4_type = ZFCP_CT_SCSI_FCP;
536
537         /* prepare zfcp_send_ct */
538         ct->wka_port = &adapter->gs->ds;
539         ct->handler = zfcp_fc_ns_handler;
540         ct->handler_data = (unsigned long)&compl_rec;
541         ct->timeout = 10;
542         ct->req = &gpn_ft->sg_req;
543         ct->resp = gpn_ft->sg_resp;
544
545         init_completion(&compl_rec.done);
546         compl_rec.handler = NULL;
547         ret = zfcp_fsf_send_ct(ct, NULL, NULL);
548         if (!ret)
549                 wait_for_completion(&compl_rec.done);
550         return ret;
551 }
552
553 static void zfcp_validate_port(struct zfcp_port *port)
554 {
555         struct zfcp_adapter *adapter = port->adapter;
556
557         if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
558                 return;
559
560         atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
561
562         if ((port->supported_classes != 0) ||
563             !list_empty(&port->unit_list_head)) {
564                 zfcp_port_put(port);
565                 return;
566         }
567         zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
568         zfcp_erp_wait(adapter);
569         zfcp_port_put(port);
570         zfcp_port_dequeue(port);
571 }
572
573 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
574 {
575         struct zfcp_send_ct *ct = &gpn_ft->ct;
576         struct scatterlist *sg = gpn_ft->sg_resp;
577         struct ct_hdr *hdr = sg_virt(sg);
578         struct gpn_ft_resp_acc *acc = sg_virt(sg);
579         struct zfcp_adapter *adapter = ct->wka_port->adapter;
580         struct zfcp_port *port, *tmp;
581         u32 d_id;
582         int ret = 0, x, last = 0;
583
584         if (ct->status)
585                 return -EIO;
586
587         if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
588                 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
589                         return -EAGAIN; /* might be a temporary condition */
590                 return -EIO;
591         }
592
593         if (hdr->max_res_size) {
594                 dev_warn(&adapter->ccw_device->dev,
595                          "The name server reported %d words residual data\n",
596                          hdr->max_res_size);
597                 return -E2BIG;
598         }
599
600         down(&zfcp_data.config_sema);
601
602         /* first entry is the header */
603         for (x = 1; x < max_entries && !last; x++) {
604                 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
605                         acc++;
606                 else
607                         acc = sg_virt(++sg);
608
609                 last = acc->control & 0x80;
610                 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
611                        acc->port_id[2];
612
613                 /* don't attach ports with a well known address */
614                 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
615                         continue;
616                 /* skip the adapter's port and known remote ports */
617                 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
618                         continue;
619                 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
620                 if (port)
621                         continue;
622
623                 port = zfcp_port_enqueue(adapter, acc->wwpn,
624                                          ZFCP_STATUS_COMMON_NOESC, d_id);
625                 if (IS_ERR(port))
626                         ret = PTR_ERR(port);
627                 else
628                         zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
629         }
630
631         zfcp_erp_wait(adapter);
632         list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
633                 zfcp_validate_port(port);
634         up(&zfcp_data.config_sema);
635         return ret;
636 }
637
638 /**
639  * zfcp_scan_ports - scan remote ports and attach new ports
640  * @adapter: pointer to struct zfcp_adapter
641  */
642 int zfcp_scan_ports(struct zfcp_adapter *adapter)
643 {
644         int ret, i;
645         struct zfcp_gpn_ft *gpn_ft;
646         int chain, max_entries, buf_num, max_bytes;
647
648         chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
649         buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
650         max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
651         max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
652
653         if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
654             fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
655                 return 0;
656
657         ret = zfcp_wka_port_get(&adapter->gs->ds);
658         if (ret)
659                 return ret;
660
661         gpn_ft = zfcp_alloc_sg_env(buf_num);
662         if (!gpn_ft) {
663                 ret = -ENOMEM;
664                 goto out;
665         }
666
667         for (i = 0; i < 3; i++) {
668                 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter, max_bytes);
669                 if (!ret) {
670                         ret = zfcp_scan_eval_gpn_ft(gpn_ft, max_entries);
671                         if (ret == -EAGAIN)
672                                 ssleep(1);
673                         else
674                                 break;
675                 }
676         }
677         zfcp_free_sg_env(gpn_ft, buf_num);
678 out:
679         zfcp_wka_port_put(&adapter->gs->ds);
680         return ret;
681 }
682
683
684 void _zfcp_scan_ports_later(struct work_struct *work)
685 {
686         zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
687 }
688
689 struct zfcp_els_fc_job {
690         struct zfcp_send_els els;
691         struct fc_bsg_job *job;
692 };
693
694 static void zfcp_fc_generic_els_handler(unsigned long data)
695 {
696         struct zfcp_els_fc_job *els_fc_job = (struct zfcp_els_fc_job *) data;
697         struct fc_bsg_job *job = els_fc_job->job;
698         struct fc_bsg_reply *reply = job->reply;
699
700         if (els_fc_job->els.status) {
701                 /* request rejected or timed out */
702                 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_REJECT;
703                 goto out;
704         }
705
706         reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
707         reply->reply_payload_rcv_len = job->reply_payload.payload_len;
708
709 out:
710         job->state_flags = FC_RQST_STATE_DONE;
711         job->job_done(job);
712         kfree(els_fc_job);
713 }
714
715 int zfcp_fc_execute_els_fc_job(struct fc_bsg_job *job)
716 {
717         struct zfcp_els_fc_job *els_fc_job;
718         struct fc_rport *rport = job->rport;
719         struct Scsi_Host *shost;
720         struct zfcp_adapter *adapter;
721         struct zfcp_port *port;
722         u8 *port_did;
723
724         shost = rport ? rport_to_shost(rport) : job->shost;
725         adapter = (struct zfcp_adapter *)shost->hostdata[0];
726
727         if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
728                 return -EINVAL;
729
730         els_fc_job = kzalloc(sizeof(struct zfcp_els_fc_job), GFP_KERNEL);
731         if (!els_fc_job)
732                 return -ENOMEM;
733
734         els_fc_job->els.adapter = adapter;
735         if (rport) {
736                 read_lock_irq(&zfcp_data.config_lock);
737                 port = rport->dd_data;
738                 if (port)
739                         els_fc_job->els.d_id = port->d_id;
740                 read_unlock_irq(&zfcp_data.config_lock);
741                 if (!port) {
742                         kfree(els_fc_job);
743                         return -EINVAL;
744                 }
745         } else {
746                 port_did = job->request->rqst_data.h_els.port_id;
747                 els_fc_job->els.d_id = (port_did[0] << 16) +
748                                         (port_did[1] << 8) + port_did[2];
749         }
750
751         els_fc_job->els.req = job->request_payload.sg_list;
752         els_fc_job->els.resp = job->reply_payload.sg_list;
753         els_fc_job->els.handler = zfcp_fc_generic_els_handler;
754         els_fc_job->els.handler_data = (unsigned long) els_fc_job;
755         els_fc_job->job = job;
756
757         return zfcp_fsf_send_els(&els_fc_job->els);
758 }
759
760 struct zfcp_ct_fc_job {
761         struct zfcp_send_ct ct;
762         struct fc_bsg_job *job;
763 };
764
765 static void zfcp_fc_generic_ct_handler(unsigned long data)
766 {
767         struct zfcp_ct_fc_job *ct_fc_job = (struct zfcp_ct_fc_job *) data;
768         struct fc_bsg_job *job = ct_fc_job->job;
769
770         job->reply->reply_data.ctels_reply.status = ct_fc_job->ct.status ?
771                                 FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
772         job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
773         job->state_flags = FC_RQST_STATE_DONE;
774         job->job_done(job);
775
776         zfcp_wka_port_put(ct_fc_job->ct.wka_port);
777
778         kfree(ct_fc_job);
779 }
780
781 int zfcp_fc_execute_ct_fc_job(struct fc_bsg_job *job)
782 {
783         int ret;
784         u8 gs_type;
785         struct fc_rport *rport = job->rport;
786         struct Scsi_Host *shost;
787         struct zfcp_adapter *adapter;
788         struct zfcp_ct_fc_job *ct_fc_job;
789         u32 preamble_word1;
790
791         shost = rport ? rport_to_shost(rport) : job->shost;
792
793         adapter = (struct zfcp_adapter *)shost->hostdata[0];
794         if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
795                 return -EINVAL;
796
797         ct_fc_job = kzalloc(sizeof(struct zfcp_ct_fc_job), GFP_KERNEL);
798         if (!ct_fc_job)
799                 return -ENOMEM;
800
801         preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
802         gs_type = (preamble_word1 & 0xff000000) >> 24;
803
804         switch (gs_type) {
805         case FC_FST_ALIAS:
806                 ct_fc_job->ct.wka_port = &adapter->gs->as;
807                 break;
808         case FC_FST_MGMT:
809                 ct_fc_job->ct.wka_port = &adapter->gs->ms;
810                 break;
811         case FC_FST_TIME:
812                 ct_fc_job->ct.wka_port = &adapter->gs->ts;
813                 break;
814         case FC_FST_DIR:
815                 ct_fc_job->ct.wka_port = &adapter->gs->ds;
816                 break;
817         default:
818                 kfree(ct_fc_job);
819                 return -EINVAL; /* no such service */
820         }
821
822         ret = zfcp_wka_port_get(ct_fc_job->ct.wka_port);
823         if (ret) {
824                 kfree(ct_fc_job);
825                 return ret;
826         }
827
828         ct_fc_job->ct.req = job->request_payload.sg_list;
829         ct_fc_job->ct.resp = job->reply_payload.sg_list;
830         ct_fc_job->ct.timeout = ZFCP_FSF_REQUEST_TIMEOUT;
831         ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
832         ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
833         ct_fc_job->ct.completion = NULL;
834         ct_fc_job->job = job;
835
836         ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL, NULL);
837         if (ret) {
838                 kfree(ct_fc_job);
839                 zfcp_wka_port_put(ct_fc_job->ct.wka_port);
840         }
841         return ret;
842 }