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