[SCSI] zfcp: Replace fsf_req wait_queue with completion
[deliverable/linux.git] / drivers / s390 / scsi / zfcp_fc.c
CommitLineData
24073b47
CS
1/*
2 * zfcp device driver
3 *
4 * Fibre Channel related functions for the zfcp device driver.
5 *
a2fa0aed 6 * Copyright IBM Corporation 2008, 2009
24073b47
CS
7 */
8
ecf39d42
CS
9#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
24073b47
CS
12#include "zfcp_ext.h"
13
e0d7fcb5
CS
14enum 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
21static 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
cc8c2829
SS
28struct 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
36struct gpn_ft_resp_acc {
37 u8 control;
38 u8 port_id[3];
39 u8 reserved[4];
40 u64 wwpn;
41} __attribute__ ((packed));
42
39eb7e9a
CS
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))
cc8c2829 46#define ZFCP_GPN_FT_BUFFERS 4
39eb7e9a
CS
47#define ZFCP_GPN_FT_MAX_SIZE (ZFCP_GPN_FT_BUFFERS * PAGE_SIZE \
48 - sizeof(struct ct_hdr))
cc8c2829
SS
49#define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
50
51struct 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
56struct 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
5ab944f9
SS
62struct zfcp_fc_ns_handler_data {
63 struct completion done;
64 void (*handler)(unsigned long);
65 unsigned long handler_data;
66};
67
68static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
69{
70 if (mutex_lock_interruptible(&wka_port->mutex))
71 return -ERESTARTSYS;
72
1c1cba17
CS
73 if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
74 wka_port->status == ZFCP_WKA_PORT_CLOSING) {
5ab944f9
SS
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
27f492cc
SS
82 wait_event(wka_port->completion_wq,
83 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
84 wka_port->status == ZFCP_WKA_PORT_OFFLINE);
5ab944f9
SS
85
86 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
87 atomic_inc(&wka_port->refcount);
88 return 0;
89 }
90 return -EIO;
91}
92
93static void zfcp_wka_port_offline(struct work_struct *work)
94{
bf6aede7 95 struct delayed_work *dw = to_delayed_work(work);
5ab944f9
SS
96 struct zfcp_wka_port *wka_port =
97 container_of(dw, struct zfcp_wka_port, work);
98
5ab944f9
SS
99 mutex_lock(&wka_port->mutex);
100 if ((atomic_read(&wka_port->refcount) != 0) ||
101 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
102 goto out;
103
104 wka_port->status = ZFCP_WKA_PORT_CLOSING;
105 if (zfcp_fsf_close_wka_port(wka_port)) {
106 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
107 wake_up(&wka_port->completion_wq);
108 }
109out:
110 mutex_unlock(&wka_port->mutex);
111}
112
113static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
114{
115 if (atomic_dec_return(&wka_port->refcount) != 0)
116 return;
19af5cdb 117 /* wait 10 milliseconds, other reqs might pop in */
5ab944f9
SS
118 schedule_delayed_work(&wka_port->work, HZ / 100);
119}
120
9d544f2b
SS
121static void zfcp_fc_wka_port_init(struct zfcp_wka_port *wka_port, u32 d_id,
122 struct zfcp_adapter *adapter)
5ab944f9 123{
5ab944f9
SS
124 init_waitqueue_head(&wka_port->completion_wq);
125
126 wka_port->adapter = adapter;
9d544f2b 127 wka_port->d_id = d_id;
5ab944f9
SS
128
129 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
130 atomic_set(&wka_port->refcount, 0);
131 mutex_init(&wka_port->mutex);
132 INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
133}
134
55c770fa 135static void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
828bc121
SS
136{
137 cancel_delayed_work_sync(&wka->work);
138 mutex_lock(&wka->mutex);
139 wka->status = ZFCP_WKA_PORT_OFFLINE;
140 mutex_unlock(&wka->mutex);
141}
142
55c770fa
CS
143void zfcp_fc_wka_ports_force_offline(struct zfcp_wka_ports *gs)
144{
145 zfcp_fc_wka_port_force_offline(&gs->ms);
146 zfcp_fc_wka_port_force_offline(&gs->ts);
147 zfcp_fc_wka_port_force_offline(&gs->ds);
148 zfcp_fc_wka_port_force_offline(&gs->as);
149 zfcp_fc_wka_port_force_offline(&gs->ks);
150}
151
9d544f2b
SS
152void zfcp_fc_wka_ports_init(struct zfcp_adapter *adapter)
153{
154 struct zfcp_wka_ports *gs = adapter->gs;
155
156 zfcp_fc_wka_port_init(&gs->ms, FC_FID_MGMT_SERV, adapter);
157 zfcp_fc_wka_port_init(&gs->ts, FC_FID_TIME_SERV, adapter);
158 zfcp_fc_wka_port_init(&gs->ds, FC_FID_DIR_SERV, adapter);
159 zfcp_fc_wka_port_init(&gs->as, FC_FID_ALIASES, adapter);
160 zfcp_fc_wka_port_init(&gs->ks, FC_FID_SEC_KEY, adapter);
161}
162
24073b47
CS
163static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
164 struct fcp_rscn_element *elem)
165{
166 unsigned long flags;
167 struct zfcp_port *port;
168
169 read_lock_irqsave(&zfcp_data.config_lock, flags);
ea460a81 170 list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
24095490 171 if ((port->d_id & range) == (elem->nport_did & range))
24073b47 172 zfcp_test_link(port);
ea460a81
SS
173 if (!port->d_id)
174 zfcp_erp_port_reopen(port,
175 ZFCP_STATUS_COMMON_ERP_FAILED,
176 "fcrscn1", NULL);
177 }
24095490 178
24073b47
CS
179 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
180}
181
182static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
183{
184 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
185 struct fcp_rscn_head *fcp_rscn_head;
186 struct fcp_rscn_element *fcp_rscn_element;
187 u16 i;
188 u16 no_entries;
189 u32 range_mask;
190
c41f8cbd
SS
191 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
192 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
24073b47
CS
193
194 /* see FC-FS */
195 no_entries = fcp_rscn_head->payload_len /
196 sizeof(struct fcp_rscn_element);
197
198 for (i = 1; i < no_entries; i++) {
199 /* skip head and start with 1st element */
200 fcp_rscn_element++;
e0d7fcb5 201 range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
24073b47
CS
202 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
203 }
cc8c2829 204 schedule_work(&fsf_req->adapter->scan_work);
24073b47
CS
205}
206
7ba58c9c 207static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
24073b47
CS
208{
209 struct zfcp_adapter *adapter = req->adapter;
210 struct zfcp_port *port;
211 unsigned long flags;
212
213 read_lock_irqsave(&zfcp_data.config_lock, flags);
214 list_for_each_entry(port, &adapter->port_list_head, list)
215 if (port->wwpn == wwpn)
216 break;
217 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
218
219 if (port && (port->wwpn == wwpn))
5ffd51a5 220 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
24073b47
CS
221}
222
223static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
224{
225 struct fsf_status_read_buffer *status_buffer =
226 (struct fsf_status_read_buffer *)req->data;
227 struct fsf_plogi *els_plogi =
c41f8cbd 228 (struct fsf_plogi *) status_buffer->payload.data;
24073b47
CS
229
230 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
231}
232
233static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
234{
235 struct fsf_status_read_buffer *status_buffer =
236 (struct fsf_status_read_buffer *)req->data;
c41f8cbd
SS
237 struct fcp_logo *els_logo =
238 (struct fcp_logo *) status_buffer->payload.data;
24073b47
CS
239
240 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
241}
242
243/**
244 * zfcp_fc_incoming_els - handle incoming ELS
245 * @fsf_req - request which contains incoming ELS
246 */
247void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
248{
249 struct fsf_status_read_buffer *status_buffer =
250 (struct fsf_status_read_buffer *) fsf_req->data;
c41f8cbd 251 unsigned int els_type = status_buffer->payload.data[0];
24073b47
CS
252
253 zfcp_san_dbf_event_incoming_els(fsf_req);
254 if (els_type == LS_PLOGI)
255 zfcp_fc_incoming_plogi(fsf_req);
256 else if (els_type == LS_LOGO)
257 zfcp_fc_incoming_logo(fsf_req);
258 else if (els_type == LS_RSCN)
259 zfcp_fc_incoming_rscn(fsf_req);
260}
261
5ab944f9
SS
262static void zfcp_fc_ns_handler(unsigned long data)
263{
264 struct zfcp_fc_ns_handler_data *compl_rec =
265 (struct zfcp_fc_ns_handler_data *) data;
266
267 if (compl_rec->handler)
268 compl_rec->handler(compl_rec->handler_data);
269
270 complete(&compl_rec->done);
271}
272
273static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
24073b47
CS
274{
275 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
276 struct zfcp_send_ct *ct = &gid_pn->ct;
277 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
278 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
279 struct zfcp_port *port = gid_pn->port;
280
281 if (ct->status)
5ab944f9 282 return;
a5b11dda 283 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
5ab944f9 284 return;
a5b11dda 285
24073b47
CS
286 /* paranoia */
287 if (ct_iu_req->wwpn != port->wwpn)
5ab944f9 288 return;
24073b47
CS
289 /* looks like a valid d_id */
290 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
24073b47
CS
291}
292
5ab944f9
SS
293int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
294 struct zfcp_gid_pn_data *gid_pn)
24073b47 295{
24073b47 296 struct zfcp_adapter *adapter = erp_action->adapter;
5ab944f9
SS
297 struct zfcp_fc_ns_handler_data compl_rec;
298 int ret;
24073b47
CS
299
300 /* setup parameters for send generic command */
301 gid_pn->port = erp_action->port;
9d544f2b 302 gid_pn->ct.wka_port = &adapter->gs->ds;
5ab944f9
SS
303 gid_pn->ct.handler = zfcp_fc_ns_handler;
304 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
24073b47
CS
305 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
306 gid_pn->ct.req = &gid_pn->req;
307 gid_pn->ct.resp = &gid_pn->resp;
24073b47
CS
308 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
309 sizeof(struct ct_iu_gid_pn_req));
310 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
311 sizeof(struct ct_iu_gid_pn_resp));
312
313 /* setup nameserver request */
314 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
315 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
316 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
317 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
318 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
39eb7e9a 319 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
24073b47
CS
320 gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
321
5ab944f9
SS
322 init_completion(&compl_rec.done);
323 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
324 compl_rec.handler_data = (unsigned long) gid_pn;
24073b47
CS
325 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
326 erp_action);
5ab944f9
SS
327 if (!ret)
328 wait_for_completion(&compl_rec.done);
329 return ret;
330}
331
332/**
333 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
334 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
335 * return: -ENOMEM on error, 0 otherwise
336 */
337int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
338{
339 int ret;
340 struct zfcp_gid_pn_data *gid_pn;
341 struct zfcp_adapter *adapter = erp_action->adapter;
342
343 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
344 if (!gid_pn)
345 return -ENOMEM;
346
347 memset(gid_pn, 0, sizeof(*gid_pn));
348
9d544f2b 349 ret = zfcp_wka_port_get(&adapter->gs->ds);
24073b47 350 if (ret)
5ab944f9
SS
351 goto out;
352
353 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
354
9d544f2b 355 zfcp_wka_port_put(&adapter->gs->ds);
5ab944f9
SS
356out:
357 mempool_free(gid_pn, adapter->pool.data_gid_pn);
24073b47
CS
358 return ret;
359}
360
361/**
362 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
363 * @port: zfcp_port structure
364 * @plogi: plogi payload
365 *
366 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
367 */
368void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
369{
370 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
371 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
372 if (plogi->serv_param.class1_serv_param[0] & 0x80)
373 port->supported_classes |= FC_COS_CLASS1;
374 if (plogi->serv_param.class2_serv_param[0] & 0x80)
375 port->supported_classes |= FC_COS_CLASS2;
376 if (plogi->serv_param.class3_serv_param[0] & 0x80)
377 port->supported_classes |= FC_COS_CLASS3;
378 if (plogi->serv_param.class4_serv_param[0] & 0x80)
379 port->supported_classes |= FC_COS_CLASS4;
380}
381
382struct zfcp_els_adisc {
383 struct zfcp_send_els els;
384 struct scatterlist req;
385 struct scatterlist resp;
386 struct zfcp_ls_adisc ls_adisc;
44cc76f2 387 struct zfcp_ls_adisc ls_adisc_acc;
24073b47
CS
388};
389
390static void zfcp_fc_adisc_handler(unsigned long data)
391{
392 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
393 struct zfcp_port *port = adisc->els.port;
44cc76f2 394 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
24073b47 395
3968ce80 396 if (adisc->els.status) {
24073b47 397 /* request rejected or timed out */
5b43e719
SS
398 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
399 "fcadh_1", NULL);
24073b47
CS
400 goto out;
401 }
402
403 if (!port->wwnn)
404 port->wwnn = ls_adisc->wwnn;
405
24095490 406 if ((port->wwpn != ls_adisc->wwpn) ||
a2fa0aed 407 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
24095490
SS
408 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
409 "fcadh_2", NULL);
a2fa0aed
CS
410 goto out;
411 }
24073b47 412
a2fa0aed
CS
413 /* port is good, unblock rport without going through erp */
414 zfcp_scsi_schedule_rport_register(port);
24073b47 415 out:
14e242ea 416 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
24073b47
CS
417 zfcp_port_put(port);
418 kfree(adisc);
419}
420
421static int zfcp_fc_adisc(struct zfcp_port *port)
422{
423 struct zfcp_els_adisc *adisc;
424 struct zfcp_adapter *adapter = port->adapter;
425
426 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
427 if (!adisc)
428 return -ENOMEM;
429
430 adisc->els.req = &adisc->req;
431 adisc->els.resp = &adisc->resp;
432 sg_init_one(adisc->els.req, &adisc->ls_adisc,
433 sizeof(struct zfcp_ls_adisc));
434 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
44cc76f2 435 sizeof(struct zfcp_ls_adisc));
24073b47 436
24073b47
CS
437 adisc->els.adapter = adapter;
438 adisc->els.port = port;
439 adisc->els.d_id = port->d_id;
440 adisc->els.handler = zfcp_fc_adisc_handler;
441 adisc->els.handler_data = (unsigned long) adisc;
442 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
443
444 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
445 without FC-AL-2 capability, so we don't set it */
446 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
447 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
448 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
449
450 return zfcp_fsf_send_els(&adisc->els);
451}
452
8fdf30d5 453void zfcp_fc_link_test_work(struct work_struct *work)
24073b47 454{
8fdf30d5
CS
455 struct zfcp_port *port =
456 container_of(work, struct zfcp_port, test_link_work);
24073b47
CS
457 int retval;
458
a2fa0aed
CS
459 zfcp_port_get(port);
460 port->rport_task = RPORT_DEL;
461 zfcp_scsi_rport_work(&port->rport_work);
462
14e242ea
CS
463 /* only issue one test command at one time per port */
464 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
465 goto out;
466
467 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
468
24073b47 469 retval = zfcp_fc_adisc(port);
9528539c 470 if (retval == 0)
24073b47
CS
471 return;
472
473 /* send of ADISC was not possible */
14e242ea 474 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
a2fa0aed
CS
475 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
476
14e242ea 477out:
24073b47 478 zfcp_port_put(port);
24073b47 479}
cc8c2829 480
8fdf30d5
CS
481/**
482 * zfcp_test_link - lightweight link test procedure
483 * @port: port to be tested
484 *
485 * Test status of a link to a remote port using the ELS command ADISC.
486 * If there is a problem with the remote port, error recovery steps
487 * will be triggered.
488 */
489void zfcp_test_link(struct zfcp_port *port)
490{
491 zfcp_port_get(port);
492 if (!queue_work(zfcp_data.work_queue, &port->test_link_work))
493 zfcp_port_put(port);
494}
495
39eb7e9a 496static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
cc8c2829
SS
497{
498 struct scatterlist *sg = &gpn_ft->sg_req;
499
500 kfree(sg_virt(sg)); /* free request buffer */
39eb7e9a 501 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
cc8c2829
SS
502
503 kfree(gpn_ft);
504}
505
39eb7e9a 506static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
cc8c2829
SS
507{
508 struct zfcp_gpn_ft *gpn_ft;
509 struct ct_iu_gpn_ft_req *req;
510
511 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
512 if (!gpn_ft)
513 return NULL;
514
515 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
516 if (!req) {
517 kfree(gpn_ft);
518 gpn_ft = NULL;
519 goto out;
520 }
521 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
522
39eb7e9a
CS
523 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
524 zfcp_free_sg_env(gpn_ft, buf_num);
cc8c2829
SS
525 gpn_ft = NULL;
526 }
527out:
528 return gpn_ft;
529}
530
531
532static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
39eb7e9a
CS
533 struct zfcp_adapter *adapter,
534 int max_bytes)
cc8c2829
SS
535{
536 struct zfcp_send_ct *ct = &gpn_ft->ct;
537 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
5ab944f9 538 struct zfcp_fc_ns_handler_data compl_rec;
cc8c2829
SS
539 int ret;
540
541 /* prepare CT IU for GPN_FT */
542 req->header.revision = ZFCP_CT_REVISION;
543 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
544 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
545 req->header.options = ZFCP_CT_SYNCHRONOUS;
546 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
39eb7e9a 547 req->header.max_res_size = max_bytes / 4;
cc8c2829
SS
548 req->flags = 0;
549 req->domain_id_scope = 0;
550 req->area_id_scope = 0;
551 req->fc4_type = ZFCP_CT_SCSI_FCP;
552
553 /* prepare zfcp_send_ct */
9d544f2b 554 ct->wka_port = &adapter->gs->ds;
5ab944f9
SS
555 ct->handler = zfcp_fc_ns_handler;
556 ct->handler_data = (unsigned long)&compl_rec;
cc8c2829
SS
557 ct->timeout = 10;
558 ct->req = &gpn_ft->sg_req;
559 ct->resp = gpn_ft->sg_resp;
cc8c2829 560
5ab944f9
SS
561 init_completion(&compl_rec.done);
562 compl_rec.handler = NULL;
cc8c2829
SS
563 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
564 if (!ret)
5ab944f9 565 wait_for_completion(&compl_rec.done);
cc8c2829
SS
566 return ret;
567}
568
569static void zfcp_validate_port(struct zfcp_port *port)
570{
571 struct zfcp_adapter *adapter = port->adapter;
572
6ab35c07
MP
573 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
574 return;
575
cc8c2829
SS
576 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
577
0406289e
CS
578 if ((port->supported_classes != 0) ||
579 !list_empty(&port->unit_list_head)) {
cc8c2829
SS
580 zfcp_port_put(port);
581 return;
582 }
5ffd51a5 583 zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
cc8c2829
SS
584 zfcp_erp_wait(adapter);
585 zfcp_port_put(port);
586 zfcp_port_dequeue(port);
587}
588
39eb7e9a 589static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
cc8c2829
SS
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);
5ab944f9 595 struct zfcp_adapter *adapter = ct->wka_port->adapter;
cc8c2829
SS
596 struct zfcp_port *port, *tmp;
597 u32 d_id;
47f7bba5 598 int ret = 0, x, last = 0;
cc8c2829
SS
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
39eb7e9a
CS
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);
cc8c2829 613 return -E2BIG;
39eb7e9a 614 }
cc8c2829
SS
615
616 down(&zfcp_data.config_sema);
617
618 /* first entry is the header */
39eb7e9a 619 for (x = 1; x < max_entries && !last; x++) {
cc8c2829
SS
620 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
621 acc++;
622 else
623 acc = sg_virt(++sg);
624
47f7bba5 625 last = acc->control & 0x80;
cc8c2829
SS
626 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
627 acc->port_id[2];
628
5ab944f9
SS
629 /* don't attach ports with a well known address */
630 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
631 continue;
cc8c2829 632 /* skip the adapter's port and known remote ports */
9528539c 633 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
cc8c2829 634 continue;
9528539c 635 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
6ab35c07 636 if (port)
9528539c 637 continue;
cc8c2829
SS
638
639 port = zfcp_port_enqueue(adapter, acc->wwpn,
cc8c2829 640 ZFCP_STATUS_COMMON_NOESC, d_id);
317e6b65
SS
641 if (IS_ERR(port))
642 ret = PTR_ERR(port);
cc8c2829 643 else
5ffd51a5 644 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
cc8c2829
SS
645 }
646
647 zfcp_erp_wait(adapter);
648 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
649 zfcp_validate_port(port);
650 up(&zfcp_data.config_sema);
651 return ret;
652}
653
654/**
655 * zfcp_scan_ports - scan remote ports and attach new ports
656 * @adapter: pointer to struct zfcp_adapter
657 */
658int zfcp_scan_ports(struct zfcp_adapter *adapter)
659{
660 int ret, i;
661 struct zfcp_gpn_ft *gpn_ft;
39eb7e9a
CS
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;
cc8c2829 668
306b6edc
SS
669 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
670 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
cc8c2829
SS
671 return 0;
672
9d544f2b 673 ret = zfcp_wka_port_get(&adapter->gs->ds);
cc8c2829
SS
674 if (ret)
675 return ret;
676
39eb7e9a 677 gpn_ft = zfcp_alloc_sg_env(buf_num);
5ab944f9
SS
678 if (!gpn_ft) {
679 ret = -ENOMEM;
680 goto out;
681 }
cc8c2829
SS
682
683 for (i = 0; i < 3; i++) {
39eb7e9a 684 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter, max_bytes);
cc8c2829 685 if (!ret) {
39eb7e9a 686 ret = zfcp_scan_eval_gpn_ft(gpn_ft, max_entries);
cc8c2829
SS
687 if (ret == -EAGAIN)
688 ssleep(1);
689 else
690 break;
691 }
692 }
39eb7e9a 693 zfcp_free_sg_env(gpn_ft, buf_num);
5ab944f9 694out:
9d544f2b 695 zfcp_wka_port_put(&adapter->gs->ds);
cc8c2829
SS
696 return ret;
697}
698
699
700void _zfcp_scan_ports_later(struct work_struct *work)
701{
702 zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
703}
9d544f2b
SS
704
705struct zfcp_els_fc_job {
706 struct zfcp_send_els els;
707 struct fc_bsg_job *job;
708};
709
710static 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;
dc577d55 723 reply->reply_payload_rcv_len = job->reply_payload.payload_len;
9d544f2b
SS
724
725out:
726 job->state_flags = FC_RQST_STATE_DONE;
727 job->job_done(job);
728 kfree(els_fc_job);
729}
730
731int 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 = rport->dd_data;
754 if (port)
dc577d55 755 els_fc_job->els.d_id = port->d_id;
9d544f2b
SS
756 read_unlock_irq(&zfcp_data.config_lock);
757 if (!port) {
758 kfree(els_fc_job);
759 return -EINVAL;
760 }
9d544f2b
SS
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
776struct zfcp_ct_fc_job {
777 struct zfcp_send_ct ct;
778 struct fc_bsg_job *job;
779};
780
781static 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;
dc577d55 788 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
9d544f2b 789 job->state_flags = FC_RQST_STATE_DONE;
9d544f2b
SS
790 job->job_done(job);
791
792 zfcp_wka_port_put(ct_fc_job->ct.wka_port);
793
794 kfree(ct_fc_job);
795}
796
797int 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_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.timeout = ZFCP_FSF_REQUEST_TIMEOUT;
847 ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
848 ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
849 ct_fc_job->ct.completion = NULL;
850 ct_fc_job->job = job;
851
852 ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL, NULL);
853 if (ret) {
854 kfree(ct_fc_job);
855 zfcp_wka_port_put(ct_fc_job->ct.wka_port);
856 }
857 return ret;
858}
This page took 0.207333 seconds and 5 git commands to generate.