[SCSI] zfcp: Replace status modifier functions.
[linux-2.6-block.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 *
615f59e0 6 * Copyright IBM Corporation 2008, 2010
24073b47
CS
7 */
8
ecf39d42
CS
9#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
9d05ce2c 12#include <linux/types.h>
5a0e3ad6 13#include <linux/slab.h>
9d05ce2c
CS
14#include <scsi/fc/fc_els.h>
15#include <scsi/libfc.h>
24073b47 16#include "zfcp_ext.h"
9d05ce2c 17#include "zfcp_fc.h"
24073b47 18
9d05ce2c
CS
19static u32 zfcp_fc_rscn_range_mask[] = {
20 [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
21 [ELS_ADDR_FMT_AREA] = 0xFFFF00,
22 [ELS_ADDR_FMT_DOM] = 0xFF0000,
23 [ELS_ADDR_FMT_FAB] = 0x000000,
e0d7fcb5
CS
24};
25
2d1e547f
SS
26/**
27 * zfcp_fc_post_event - post event to userspace via fc_transport
28 * @work: work struct with enqueued events
29 */
30void zfcp_fc_post_event(struct work_struct *work)
31{
32 struct zfcp_fc_event *event = NULL, *tmp = NULL;
33 LIST_HEAD(tmp_lh);
34 struct zfcp_fc_events *events = container_of(work,
35 struct zfcp_fc_events, work);
36 struct zfcp_adapter *adapter = container_of(events, struct zfcp_adapter,
37 events);
38
39 spin_lock_bh(&events->list_lock);
40 list_splice_init(&events->list, &tmp_lh);
41 spin_unlock_bh(&events->list_lock);
42
43 list_for_each_entry_safe(event, tmp, &tmp_lh, list) {
44 fc_host_post_event(adapter->scsi_host, fc_get_event_number(),
45 event->code, event->data);
46 list_del(&event->list);
47 kfree(event);
48 }
49
50}
51
52/**
53 * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context
54 * @adapter: The adapter where to enqueue the event
55 * @event_code: The event code (as defined in fc_host_event_code in
56 * scsi_transport_fc.h)
57 * @event_data: The event data (e.g. n_port page in case of els)
58 */
59void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
60 enum fc_host_event_code event_code, u32 event_data)
61{
62 struct zfcp_fc_event *event;
63
64 event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
65 if (!event)
66 return;
67
68 event->code = event_code;
69 event->data = event_data;
70
71 spin_lock(&adapter->events.list_lock);
72 list_add_tail(&event->list, &adapter->events.list);
73 spin_unlock(&adapter->events.list_lock);
74
75 queue_work(adapter->work_queue, &adapter->events.work);
76}
77
bd0072ec 78static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
5ab944f9
SS
79{
80 if (mutex_lock_interruptible(&wka_port->mutex))
81 return -ERESTARTSYS;
82
bd0072ec
CS
83 if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
84 wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
85 wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
5ab944f9 86 if (zfcp_fsf_open_wka_port(wka_port))
bd0072ec 87 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
88 }
89
90 mutex_unlock(&wka_port->mutex);
91
27f492cc 92 wait_event(wka_port->completion_wq,
bd0072ec
CS
93 wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
94 wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
5ab944f9 95
bd0072ec 96 if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
5ab944f9
SS
97 atomic_inc(&wka_port->refcount);
98 return 0;
99 }
100 return -EIO;
101}
102
6f53a2d2 103static void zfcp_fc_wka_port_offline(struct work_struct *work)
5ab944f9 104{
bf6aede7 105 struct delayed_work *dw = to_delayed_work(work);
bd0072ec
CS
106 struct zfcp_fc_wka_port *wka_port =
107 container_of(dw, struct zfcp_fc_wka_port, work);
5ab944f9 108
5ab944f9
SS
109 mutex_lock(&wka_port->mutex);
110 if ((atomic_read(&wka_port->refcount) != 0) ||
bd0072ec 111 (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
5ab944f9
SS
112 goto out;
113
bd0072ec 114 wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
5ab944f9 115 if (zfcp_fsf_close_wka_port(wka_port)) {
bd0072ec 116 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
117 wake_up(&wka_port->completion_wq);
118 }
119out:
120 mutex_unlock(&wka_port->mutex);
121}
122
bd0072ec 123static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
5ab944f9
SS
124{
125 if (atomic_dec_return(&wka_port->refcount) != 0)
126 return;
19af5cdb 127 /* wait 10 milliseconds, other reqs might pop in */
5ab944f9
SS
128 schedule_delayed_work(&wka_port->work, HZ / 100);
129}
130
bd0072ec 131static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
9d544f2b 132 struct zfcp_adapter *adapter)
5ab944f9 133{
5ab944f9
SS
134 init_waitqueue_head(&wka_port->completion_wq);
135
136 wka_port->adapter = adapter;
9d544f2b 137 wka_port->d_id = d_id;
5ab944f9 138
bd0072ec 139 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
140 atomic_set(&wka_port->refcount, 0);
141 mutex_init(&wka_port->mutex);
6f53a2d2 142 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
5ab944f9
SS
143}
144
bd0072ec 145static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
828bc121
SS
146{
147 cancel_delayed_work_sync(&wka->work);
148 mutex_lock(&wka->mutex);
bd0072ec 149 wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
828bc121
SS
150 mutex_unlock(&wka->mutex);
151}
152
bd0072ec 153void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
55c770fa 154{
f3450c7b
SS
155 if (!gs)
156 return;
55c770fa
CS
157 zfcp_fc_wka_port_force_offline(&gs->ms);
158 zfcp_fc_wka_port_force_offline(&gs->ts);
159 zfcp_fc_wka_port_force_offline(&gs->ds);
160 zfcp_fc_wka_port_force_offline(&gs->as);
55c770fa
CS
161}
162
24073b47 163static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
9d05ce2c 164 struct fc_els_rscn_page *page)
24073b47
CS
165{
166 unsigned long flags;
ecf0c772 167 struct zfcp_adapter *adapter = fsf_req->adapter;
24073b47
CS
168 struct zfcp_port *port;
169
ecf0c772
SS
170 read_lock_irqsave(&adapter->port_list_lock, flags);
171 list_for_each_entry(port, &adapter->port_list, list) {
9d05ce2c 172 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
6f53a2d2 173 zfcp_fc_test_link(port);
ea460a81
SS
174 if (!port->d_id)
175 zfcp_erp_port_reopen(port,
176 ZFCP_STATUS_COMMON_ERP_FAILED,
177 "fcrscn1", NULL);
178 }
ecf0c772 179 read_unlock_irqrestore(&adapter->port_list_lock, flags);
24073b47
CS
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;
9d05ce2c
CS
185 struct fc_els_rscn *head;
186 struct fc_els_rscn_page *page;
24073b47
CS
187 u16 i;
188 u16 no_entries;
9d05ce2c 189 unsigned int afmt;
24073b47 190
9d05ce2c
CS
191 head = (struct fc_els_rscn *) status_buffer->payload.data;
192 page = (struct fc_els_rscn_page *) head;
24073b47
CS
193
194 /* see FC-FS */
9d05ce2c 195 no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
24073b47
CS
196
197 for (i = 1; i < no_entries; i++) {
198 /* skip head and start with 1st element */
9d05ce2c
CS
199 page++;
200 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
201 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
202 page);
2d1e547f
SS
203 zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
204 *(u32 *)page);
24073b47 205 }
9eae07ef 206 queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
24073b47
CS
207}
208
7ba58c9c 209static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
24073b47 210{
ecf0c772 211 unsigned long flags;
24073b47
CS
212 struct zfcp_adapter *adapter = req->adapter;
213 struct zfcp_port *port;
24073b47 214
ecf0c772
SS
215 read_lock_irqsave(&adapter->port_list_lock, flags);
216 list_for_each_entry(port, &adapter->port_list, list)
217 if (port->wwpn == wwpn) {
218 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
24073b47 219 break;
ecf0c772
SS
220 }
221 read_unlock_irqrestore(&adapter->port_list_lock, flags);
24073b47
CS
222}
223
224static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
225{
9d05ce2c
CS
226 struct fsf_status_read_buffer *status_buffer;
227 struct fc_els_flogi *plogi;
24073b47 228
9d05ce2c
CS
229 status_buffer = (struct fsf_status_read_buffer *) req->data;
230 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
231 zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
24073b47
CS
232}
233
234static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
235{
236 struct fsf_status_read_buffer *status_buffer =
237 (struct fsf_status_read_buffer *)req->data;
9d05ce2c
CS
238 struct fc_els_logo *logo =
239 (struct fc_els_logo *) status_buffer->payload.data;
24073b47 240
9d05ce2c 241 zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
24073b47
CS
242}
243
244/**
245 * zfcp_fc_incoming_els - handle incoming ELS
246 * @fsf_req - request which contains incoming ELS
247 */
248void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
249{
250 struct fsf_status_read_buffer *status_buffer =
251 (struct fsf_status_read_buffer *) fsf_req->data;
c41f8cbd 252 unsigned int els_type = status_buffer->payload.data[0];
24073b47 253
5771710b 254 zfcp_dbf_san_incoming_els(fsf_req);
9d05ce2c 255 if (els_type == ELS_PLOGI)
24073b47 256 zfcp_fc_incoming_plogi(fsf_req);
9d05ce2c 257 else if (els_type == ELS_LOGO)
24073b47 258 zfcp_fc_incoming_logo(fsf_req);
9d05ce2c 259 else if (els_type == ELS_RSCN)
24073b47
CS
260 zfcp_fc_incoming_rscn(fsf_req);
261}
262
7c7dc196 263static void zfcp_fc_ns_gid_pn_eval(void *data)
24073b47 264{
7c7dc196
CS
265 struct zfcp_fc_gid_pn *gid_pn = data;
266 struct zfcp_fsf_ct_els *ct = &gid_pn->ct;
dbf5dfe9
CS
267 struct zfcp_fc_gid_pn_req *gid_pn_req = sg_virt(ct->req);
268 struct zfcp_fc_gid_pn_resp *gid_pn_resp = sg_virt(ct->resp);
24073b47
CS
269 struct zfcp_port *port = gid_pn->port;
270
271 if (ct->status)
5ab944f9 272 return;
dbf5dfe9 273 if (gid_pn_resp->ct_hdr.ct_cmd != FC_FS_ACC)
5ab944f9 274 return;
a5b11dda 275
24073b47 276 /* paranoia */
dbf5dfe9 277 if (gid_pn_req->gid_pn.fn_wwpn != port->wwpn)
5ab944f9 278 return;
24073b47 279 /* looks like a valid d_id */
dbf5dfe9 280 port->d_id = ntoh24(gid_pn_resp->gid_pn.fp_fid);
24073b47
CS
281}
282
7c7dc196
CS
283static void zfcp_fc_complete(void *data)
284{
285 complete(data);
286}
287
799b76d0 288static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
dbf5dfe9 289 struct zfcp_fc_gid_pn *gid_pn)
24073b47 290{
799b76d0 291 struct zfcp_adapter *adapter = port->adapter;
7c7dc196 292 DECLARE_COMPLETION_ONSTACK(completion);
5ab944f9 293 int ret;
24073b47
CS
294
295 /* setup parameters for send generic command */
799b76d0 296 gid_pn->port = port;
7c7dc196
CS
297 gid_pn->ct.handler = zfcp_fc_complete;
298 gid_pn->ct.handler_data = &completion;
dbf5dfe9
CS
299 gid_pn->ct.req = &gid_pn->sg_req;
300 gid_pn->ct.resp = &gid_pn->sg_resp;
301 sg_init_one(&gid_pn->sg_req, &gid_pn->gid_pn_req,
302 sizeof(struct zfcp_fc_gid_pn_req));
303 sg_init_one(&gid_pn->sg_resp, &gid_pn->gid_pn_resp,
304 sizeof(struct zfcp_fc_gid_pn_resp));
24073b47
CS
305
306 /* setup nameserver request */
dbf5dfe9
CS
307 gid_pn->gid_pn_req.ct_hdr.ct_rev = FC_CT_REV;
308 gid_pn->gid_pn_req.ct_hdr.ct_fs_type = FC_FST_DIR;
309 gid_pn->gid_pn_req.ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
310 gid_pn->gid_pn_req.ct_hdr.ct_options = 0;
311 gid_pn->gid_pn_req.ct_hdr.ct_cmd = FC_NS_GID_PN;
312 gid_pn->gid_pn_req.ct_hdr.ct_mr_size = ZFCP_FC_CT_SIZE_PAGE / 4;
313 gid_pn->gid_pn_req.gid_pn.fn_wwpn = port->wwpn;
24073b47 314
7c7dc196 315 ret = zfcp_fsf_send_ct(&adapter->gs->ds, &gid_pn->ct,
51375ee8
SS
316 adapter->pool.gid_pn_req,
317 ZFCP_FC_CTELS_TMO);
7c7dc196
CS
318 if (!ret) {
319 wait_for_completion(&completion);
320 zfcp_fc_ns_gid_pn_eval(gid_pn);
321 }
5ab944f9
SS
322 return ret;
323}
324
325/**
326 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
799b76d0 327 * @port: port where GID_PN request is needed
5ab944f9
SS
328 * return: -ENOMEM on error, 0 otherwise
329 */
799b76d0 330static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
5ab944f9
SS
331{
332 int ret;
dbf5dfe9 333 struct zfcp_fc_gid_pn *gid_pn;
799b76d0 334 struct zfcp_adapter *adapter = port->adapter;
5ab944f9 335
dbf5dfe9 336 gid_pn = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
5ab944f9
SS
337 if (!gid_pn)
338 return -ENOMEM;
339
340 memset(gid_pn, 0, sizeof(*gid_pn));
341
6f53a2d2 342 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
24073b47 343 if (ret)
5ab944f9
SS
344 goto out;
345
799b76d0 346 ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
5ab944f9 347
6f53a2d2 348 zfcp_fc_wka_port_put(&adapter->gs->ds);
5ab944f9 349out:
dbf5dfe9 350 mempool_free(gid_pn, adapter->pool.gid_pn);
24073b47
CS
351 return ret;
352}
353
799b76d0
CS
354void zfcp_fc_port_did_lookup(struct work_struct *work)
355{
356 int ret;
357 struct zfcp_port *port = container_of(work, struct zfcp_port,
358 gid_pn_work);
359
360 ret = zfcp_fc_ns_gid_pn(port);
361 if (ret) {
362 /* could not issue gid_pn for some reason */
363 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
364 goto out;
365 }
366
367 if (!port->d_id) {
edaed859 368 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
799b76d0
CS
369 goto out;
370 }
371
372 zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
373out:
615f59e0 374 put_device(&port->dev);
799b76d0
CS
375}
376
934aeb58
CS
377/**
378 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
379 * @port: The zfcp_port to lookup the d_id for.
380 */
381void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
382{
615f59e0 383 get_device(&port->dev);
934aeb58 384 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
615f59e0 385 put_device(&port->dev);
934aeb58
CS
386}
387
24073b47
CS
388/**
389 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
390 * @port: zfcp_port structure
391 * @plogi: plogi payload
392 *
393 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
394 */
9d05ce2c 395void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
24073b47 396{
9d05ce2c
CS
397 if (plogi->fl_wwpn != port->wwpn) {
398 port->d_id = 0;
399 dev_warn(&port->adapter->ccw_device->dev,
400 "A port opened with WWPN 0x%016Lx returned data that "
401 "identifies it as WWPN 0x%016Lx\n",
402 (unsigned long long) port->wwpn,
403 (unsigned long long) plogi->fl_wwpn);
404 return;
405 }
406
407 port->wwnn = plogi->fl_wwnn;
408 port->maxframe_size = plogi->fl_csp.sp_bb_data;
409
410 if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
24073b47 411 port->supported_classes |= FC_COS_CLASS1;
9d05ce2c 412 if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
24073b47 413 port->supported_classes |= FC_COS_CLASS2;
9d05ce2c 414 if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
24073b47 415 port->supported_classes |= FC_COS_CLASS3;
9d05ce2c 416 if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
24073b47
CS
417 port->supported_classes |= FC_COS_CLASS4;
418}
419
7c7dc196 420static void zfcp_fc_adisc_handler(void *data)
24073b47 421{
7c7dc196 422 struct zfcp_fc_els_adisc *adisc = data;
24073b47 423 struct zfcp_port *port = adisc->els.port;
9d05ce2c 424 struct fc_els_adisc *adisc_resp = &adisc->adisc_resp;
24073b47 425
3968ce80 426 if (adisc->els.status) {
24073b47 427 /* request rejected or timed out */
5b43e719
SS
428 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
429 "fcadh_1", NULL);
24073b47
CS
430 goto out;
431 }
432
433 if (!port->wwnn)
9d05ce2c 434 port->wwnn = adisc_resp->adisc_wwnn;
24073b47 435
9d05ce2c 436 if ((port->wwpn != adisc_resp->adisc_wwpn) ||
a2fa0aed 437 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
24095490
SS
438 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
439 "fcadh_2", NULL);
a2fa0aed
CS
440 goto out;
441 }
24073b47 442
a2fa0aed
CS
443 /* port is good, unblock rport without going through erp */
444 zfcp_scsi_schedule_rport_register(port);
24073b47 445 out:
14e242ea 446 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
615f59e0 447 put_device(&port->dev);
ee744622 448 kmem_cache_free(zfcp_data.adisc_cache, adisc);
24073b47
CS
449}
450
451static int zfcp_fc_adisc(struct zfcp_port *port)
452{
9d05ce2c 453 struct zfcp_fc_els_adisc *adisc;
24073b47 454 struct zfcp_adapter *adapter = port->adapter;
ee744622 455 int ret;
24073b47 456
6e51f085 457 adisc = kmem_cache_zalloc(zfcp_data.adisc_cache, GFP_ATOMIC);
24073b47
CS
458 if (!adisc)
459 return -ENOMEM;
460
7c7dc196 461 adisc->els.port = port;
24073b47
CS
462 adisc->els.req = &adisc->req;
463 adisc->els.resp = &adisc->resp;
9d05ce2c
CS
464 sg_init_one(adisc->els.req, &adisc->adisc_req,
465 sizeof(struct fc_els_adisc));
466 sg_init_one(adisc->els.resp, &adisc->adisc_resp,
467 sizeof(struct fc_els_adisc));
24073b47 468
24073b47 469 adisc->els.handler = zfcp_fc_adisc_handler;
7c7dc196 470 adisc->els.handler_data = adisc;
24073b47
CS
471
472 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
473 without FC-AL-2 capability, so we don't set it */
9d05ce2c
CS
474 adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
475 adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
7c7dc196 476 adisc->adisc_req.adisc_cmd = ELS_ADISC;
9d05ce2c
CS
477 hton24(adisc->adisc_req.adisc_port_id,
478 fc_host_port_id(adapter->scsi_host));
24073b47 479
51375ee8
SS
480 ret = zfcp_fsf_send_els(adapter, port->d_id, &adisc->els,
481 ZFCP_FC_CTELS_TMO);
ee744622
CS
482 if (ret)
483 kmem_cache_free(zfcp_data.adisc_cache, adisc);
484
485 return ret;
24073b47
CS
486}
487
8fdf30d5 488void zfcp_fc_link_test_work(struct work_struct *work)
24073b47 489{
8fdf30d5
CS
490 struct zfcp_port *port =
491 container_of(work, struct zfcp_port, test_link_work);
24073b47
CS
492 int retval;
493
615f59e0 494 get_device(&port->dev);
a2fa0aed
CS
495 port->rport_task = RPORT_DEL;
496 zfcp_scsi_rport_work(&port->rport_work);
497
14e242ea
CS
498 /* only issue one test command at one time per port */
499 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
500 goto out;
501
502 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
503
24073b47 504 retval = zfcp_fc_adisc(port);
9528539c 505 if (retval == 0)
24073b47
CS
506 return;
507
508 /* send of ADISC was not possible */
14e242ea 509 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
a2fa0aed
CS
510 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
511
14e242ea 512out:
615f59e0 513 put_device(&port->dev);
24073b47 514}
cc8c2829 515
8fdf30d5 516/**
6f53a2d2 517 * zfcp_fc_test_link - lightweight link test procedure
8fdf30d5
CS
518 * @port: port to be tested
519 *
520 * Test status of a link to a remote port using the ELS command ADISC.
521 * If there is a problem with the remote port, error recovery steps
522 * will be triggered.
523 */
6f53a2d2 524void zfcp_fc_test_link(struct zfcp_port *port)
8fdf30d5 525{
615f59e0 526 get_device(&port->dev);
4544683a 527 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
615f59e0 528 put_device(&port->dev);
8fdf30d5
CS
529}
530
dbf5dfe9 531static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
cc8c2829
SS
532{
533 struct scatterlist *sg = &gpn_ft->sg_req;
534
a4623c46 535 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
39eb7e9a 536 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
cc8c2829
SS
537
538 kfree(gpn_ft);
539}
540
dbf5dfe9 541static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
cc8c2829 542{
dbf5dfe9
CS
543 struct zfcp_fc_gpn_ft *gpn_ft;
544 struct zfcp_fc_gpn_ft_req *req;
cc8c2829
SS
545
546 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
547 if (!gpn_ft)
548 return NULL;
549
6e51f085 550 req = kmem_cache_zalloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
cc8c2829
SS
551 if (!req) {
552 kfree(gpn_ft);
553 gpn_ft = NULL;
554 goto out;
555 }
556 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
557
39eb7e9a
CS
558 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
559 zfcp_free_sg_env(gpn_ft, buf_num);
cc8c2829
SS
560 gpn_ft = NULL;
561 }
562out:
563 return gpn_ft;
564}
565
566
dbf5dfe9 567static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
6f53a2d2 568 struct zfcp_adapter *adapter, int max_bytes)
cc8c2829 569{
7c7dc196 570 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
dbf5dfe9 571 struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
7c7dc196 572 DECLARE_COMPLETION_ONSTACK(completion);
cc8c2829
SS
573 int ret;
574
575 /* prepare CT IU for GPN_FT */
dbf5dfe9
CS
576 req->ct_hdr.ct_rev = FC_CT_REV;
577 req->ct_hdr.ct_fs_type = FC_FST_DIR;
578 req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
579 req->ct_hdr.ct_options = 0;
580 req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
581 req->ct_hdr.ct_mr_size = max_bytes / 4;
582 req->gpn_ft.fn_domain_id_scope = 0;
583 req->gpn_ft.fn_area_id_scope = 0;
584 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
cc8c2829
SS
585
586 /* prepare zfcp_send_ct */
7c7dc196
CS
587 ct->handler = zfcp_fc_complete;
588 ct->handler_data = &completion;
cc8c2829
SS
589 ct->req = &gpn_ft->sg_req;
590 ct->resp = gpn_ft->sg_resp;
cc8c2829 591
51375ee8
SS
592 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct, NULL,
593 ZFCP_FC_CTELS_TMO);
cc8c2829 594 if (!ret)
7c7dc196 595 wait_for_completion(&completion);
cc8c2829
SS
596 return ret;
597}
598
f3450c7b 599static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
cc8c2829 600{
6ab35c07
MP
601 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
602 return;
603
cc8c2829
SS
604 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
605
0406289e 606 if ((port->supported_classes != 0) ||
f3450c7b 607 !list_empty(&port->unit_list))
cc8c2829 608 return;
f3450c7b 609
f3450c7b 610 list_move_tail(&port->list, lh);
cc8c2829
SS
611}
612
7c7dc196
CS
613static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
614 struct zfcp_adapter *adapter, int max_entries)
cc8c2829 615{
7c7dc196 616 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
cc8c2829 617 struct scatterlist *sg = gpn_ft->sg_resp;
dbf5dfe9
CS
618 struct fc_ct_hdr *hdr = sg_virt(sg);
619 struct fc_gpn_ft_resp *acc = sg_virt(sg);
cc8c2829 620 struct zfcp_port *port, *tmp;
ecf0c772 621 unsigned long flags;
f3450c7b 622 LIST_HEAD(remove_lh);
cc8c2829 623 u32 d_id;
47f7bba5 624 int ret = 0, x, last = 0;
cc8c2829
SS
625
626 if (ct->status)
627 return -EIO;
628
dbf5dfe9
CS
629 if (hdr->ct_cmd != FC_FS_ACC) {
630 if (hdr->ct_reason == FC_BA_RJT_UNABLE)
cc8c2829
SS
631 return -EAGAIN; /* might be a temporary condition */
632 return -EIO;
633 }
634
dbf5dfe9 635 if (hdr->ct_mr_size) {
39eb7e9a
CS
636 dev_warn(&adapter->ccw_device->dev,
637 "The name server reported %d words residual data\n",
dbf5dfe9 638 hdr->ct_mr_size);
cc8c2829 639 return -E2BIG;
39eb7e9a 640 }
cc8c2829 641
cc8c2829 642 /* first entry is the header */
39eb7e9a 643 for (x = 1; x < max_entries && !last; x++) {
dbf5dfe9 644 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
cc8c2829
SS
645 acc++;
646 else
647 acc = sg_virt(++sg);
648
dbf5dfe9
CS
649 last = acc->fp_flags & FC_NS_FID_LAST;
650 d_id = ntoh24(acc->fp_fid);
cc8c2829 651
5ab944f9 652 /* don't attach ports with a well known address */
dbf5dfe9 653 if (d_id >= FC_FID_WELL_KNOWN_BASE)
5ab944f9 654 continue;
cc8c2829 655 /* skip the adapter's port and known remote ports */
dbf5dfe9 656 if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
cc8c2829
SS
657 continue;
658
dbf5dfe9 659 port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
cc8c2829 660 ZFCP_STATUS_COMMON_NOESC, d_id);
ecf0c772 661 if (!IS_ERR(port))
5ffd51a5 662 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
ecf0c772
SS
663 else if (PTR_ERR(port) != -EEXIST)
664 ret = PTR_ERR(port);
cc8c2829
SS
665 }
666
667 zfcp_erp_wait(adapter);
ecf0c772
SS
668 write_lock_irqsave(&adapter->port_list_lock, flags);
669 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
f3450c7b 670 zfcp_fc_validate_port(port, &remove_lh);
ecf0c772 671 write_unlock_irqrestore(&adapter->port_list_lock, flags);
f3450c7b
SS
672
673 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
674 zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
615f59e0 675 zfcp_device_unregister(&port->dev, &zfcp_sysfs_port_attrs);
f3450c7b
SS
676 }
677
cc8c2829
SS
678 return ret;
679}
680
681/**
6f53a2d2 682 * zfcp_fc_scan_ports - scan remote ports and attach new ports
9eae07ef 683 * @work: reference to scheduled work
cc8c2829 684 */
9eae07ef 685void zfcp_fc_scan_ports(struct work_struct *work)
cc8c2829 686{
9eae07ef
SS
687 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
688 scan_work);
cc8c2829 689 int ret, i;
dbf5dfe9 690 struct zfcp_fc_gpn_ft *gpn_ft;
39eb7e9a
CS
691 int chain, max_entries, buf_num, max_bytes;
692
693 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
dbf5dfe9
CS
694 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
695 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
696 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
cc8c2829 697
306b6edc
SS
698 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
699 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
9eae07ef 700 return;
cc8c2829 701
9eae07ef
SS
702 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
703 return;
cc8c2829 704
39eb7e9a 705 gpn_ft = zfcp_alloc_sg_env(buf_num);
9eae07ef 706 if (!gpn_ft)
5ab944f9 707 goto out;
cc8c2829
SS
708
709 for (i = 0; i < 3; i++) {
6f53a2d2 710 ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
cc8c2829 711 if (!ret) {
7c7dc196 712 ret = zfcp_fc_eval_gpn_ft(gpn_ft, adapter, max_entries);
cc8c2829
SS
713 if (ret == -EAGAIN)
714 ssleep(1);
715 else
716 break;
717 }
718 }
39eb7e9a 719 zfcp_free_sg_env(gpn_ft, buf_num);
5ab944f9 720out:
6f53a2d2 721 zfcp_fc_wka_port_put(&adapter->gs->ds);
cc8c2829
SS
722}
723
7c7dc196 724static void zfcp_fc_ct_els_job_handler(void *data)
9d544f2b 725{
7c7dc196
CS
726 struct fc_bsg_job *job = data;
727 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
7dec9cf1 728 struct fc_bsg_reply *jr = job->reply;
9d544f2b 729
7dec9cf1
SS
730 jr->reply_payload_rcv_len = job->reply_payload.payload_len;
731 jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
732 jr->result = zfcp_ct_els->status ? -EIO : 0;
9d544f2b 733 job->job_done(job);
9d544f2b
SS
734}
735
f09d5454
CS
736static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct fc_bsg_job *job)
737{
738 u32 preamble_word1;
739 u8 gs_type;
740 struct zfcp_adapter *adapter;
741
742 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
743 gs_type = (preamble_word1 & 0xff000000) >> 24;
744
745 adapter = (struct zfcp_adapter *) job->shost->hostdata[0];
746
747 switch (gs_type) {
748 case FC_FST_ALIAS:
749 return &adapter->gs->as;
750 case FC_FST_MGMT:
751 return &adapter->gs->ms;
752 case FC_FST_TIME:
753 return &adapter->gs->ts;
754 break;
755 case FC_FST_DIR:
756 return &adapter->gs->ds;
757 break;
758 default:
759 return NULL;
760 }
761}
762
763static void zfcp_fc_ct_job_handler(void *data)
764{
765 struct fc_bsg_job *job = data;
766 struct zfcp_fc_wka_port *wka_port;
767
768 wka_port = zfcp_fc_job_wka_port(job);
769 zfcp_fc_wka_port_put(wka_port);
770
771 zfcp_fc_ct_els_job_handler(data);
772}
773
7c7dc196
CS
774static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
775 struct zfcp_adapter *adapter)
9d544f2b 776{
7c7dc196 777 struct zfcp_fsf_ct_els *els = job->dd_data;
9d544f2b 778 struct fc_rport *rport = job->rport;
9d544f2b 779 struct zfcp_port *port;
7c7dc196 780 u32 d_id;
9d544f2b 781
9d544f2b 782 if (rport) {
ea945ff8 783 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
7c7dc196 784 if (!port)
9d544f2b 785 return -EINVAL;
ecf0c772 786
7c7dc196 787 d_id = port->d_id;
615f59e0 788 put_device(&port->dev);
7c7dc196
CS
789 } else
790 d_id = ntoh24(job->request->rqst_data.h_els.port_id);
9d544f2b 791
f09d5454 792 els->handler = zfcp_fc_ct_els_job_handler;
51375ee8 793 return zfcp_fsf_send_els(adapter, d_id, els, job->req->timeout / HZ);
9d544f2b
SS
794}
795
7c7dc196
CS
796static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
797 struct zfcp_adapter *adapter)
9d544f2b
SS
798{
799 int ret;
7c7dc196
CS
800 struct zfcp_fsf_ct_els *ct = job->dd_data;
801 struct zfcp_fc_wka_port *wka_port;
9d544f2b 802
f09d5454
CS
803 wka_port = zfcp_fc_job_wka_port(job);
804 if (!wka_port)
805 return -EINVAL;
9d544f2b 806
7c7dc196
CS
807 ret = zfcp_fc_wka_port_get(wka_port);
808 if (ret)
9d544f2b 809 return ret;
9d544f2b 810
f09d5454 811 ct->handler = zfcp_fc_ct_job_handler;
51375ee8 812 ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->req->timeout / HZ);
7c7dc196
CS
813 if (ret)
814 zfcp_fc_wka_port_put(wka_port);
9d544f2b 815
9d544f2b
SS
816 return ret;
817}
d5a282a1 818
7c7dc196
CS
819int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
820{
821 struct Scsi_Host *shost;
822 struct zfcp_adapter *adapter;
823 struct zfcp_fsf_ct_els *ct_els = job->dd_data;
824
825 shost = job->rport ? rport_to_shost(job->rport) : job->shost;
826 adapter = (struct zfcp_adapter *)shost->hostdata[0];
827
828 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
829 return -EINVAL;
830
831 ct_els->req = job->request_payload.sg_list;
832 ct_els->resp = job->reply_payload.sg_list;
7c7dc196
CS
833 ct_els->handler_data = job;
834
835 switch (job->request->msgcode) {
836 case FC_BSG_RPT_ELS:
837 case FC_BSG_HST_ELS_NOLOGIN:
838 return zfcp_fc_exec_els_job(job, adapter);
839 case FC_BSG_RPT_CT:
840 case FC_BSG_HST_CT:
841 return zfcp_fc_exec_ct_job(job, adapter);
842 default:
843 return -EINVAL;
844 }
845}
846
491ca442
SS
847int zfcp_fc_timeout_bsg_job(struct fc_bsg_job *job)
848{
849 /* hardware tracks timeout, reset bsg timeout to not interfere */
850 return -EAGAIN;
851}
852
d5a282a1
SS
853int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
854{
bd0072ec 855 struct zfcp_fc_wka_ports *wka_ports;
d5a282a1 856
bd0072ec 857 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
d5a282a1
SS
858 if (!wka_ports)
859 return -ENOMEM;
860
861 adapter->gs = wka_ports;
862 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
863 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
864 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
865 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
d5a282a1
SS
866
867 return 0;
868}
869
870void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
871{
872 kfree(adapter->gs);
873 adapter->gs = NULL;
874}
875