GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / s390 / scsi / zfcp_fc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * zfcp device driver
4  *
5  * Fibre Channel related functions for the zfcp device driver.
6  *
7  * Copyright IBM Corp. 2008, 2017
8  */
9
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/utsname.h>
16 #include <linux/random.h>
17 #include <linux/bsg-lib.h>
18 #include <scsi/fc/fc_els.h>
19 #include <scsi/libfc.h>
20 #include "zfcp_ext.h"
21 #include "zfcp_fc.h"
22
23 struct kmem_cache *zfcp_fc_req_cache;
24
25 static u32 zfcp_fc_rscn_range_mask[] = {
26         [ELS_ADDR_FMT_PORT]             = 0xFFFFFF,
27         [ELS_ADDR_FMT_AREA]             = 0xFFFF00,
28         [ELS_ADDR_FMT_DOM]              = 0xFF0000,
29         [ELS_ADDR_FMT_FAB]              = 0x000000,
30 };
31
32 static bool no_auto_port_rescan;
33 module_param(no_auto_port_rescan, bool, 0600);
34 MODULE_PARM_DESC(no_auto_port_rescan,
35                  "no automatic port_rescan (default off)");
36
37 static unsigned int port_scan_backoff = 500;
38 module_param(port_scan_backoff, uint, 0600);
39 MODULE_PARM_DESC(port_scan_backoff,
40         "upper limit of port scan random backoff in msecs (default 500)");
41
42 static unsigned int port_scan_ratelimit = 60000;
43 module_param(port_scan_ratelimit, uint, 0600);
44 MODULE_PARM_DESC(port_scan_ratelimit,
45         "minimum interval between port scans in msecs (default 60000)");
46
47 unsigned int zfcp_fc_port_scan_backoff(void)
48 {
49         if (!port_scan_backoff)
50                 return 0;
51         return get_random_int() % port_scan_backoff;
52 }
53
54 static void zfcp_fc_port_scan_time(struct zfcp_adapter *adapter)
55 {
56         unsigned long interval = msecs_to_jiffies(port_scan_ratelimit);
57         unsigned long backoff = msecs_to_jiffies(zfcp_fc_port_scan_backoff());
58
59         adapter->next_port_scan = jiffies + interval + backoff;
60 }
61
62 static void zfcp_fc_port_scan(struct zfcp_adapter *adapter)
63 {
64         unsigned long now = jiffies;
65         unsigned long next = adapter->next_port_scan;
66         unsigned long delay = 0, max;
67
68         /* delay only needed within waiting period */
69         if (time_before(now, next)) {
70                 delay = next - now;
71                 /* paranoia: never ever delay scans longer than specified */
72                 max = msecs_to_jiffies(port_scan_ratelimit + port_scan_backoff);
73                 delay = min(delay, max);
74         }
75
76         queue_delayed_work(adapter->work_queue, &adapter->scan_work, delay);
77 }
78
79 void zfcp_fc_conditional_port_scan(struct zfcp_adapter *adapter)
80 {
81         if (no_auto_port_rescan)
82                 return;
83
84         zfcp_fc_port_scan(adapter);
85 }
86
87 void zfcp_fc_inverse_conditional_port_scan(struct zfcp_adapter *adapter)
88 {
89         if (!no_auto_port_rescan)
90                 return;
91
92         zfcp_fc_port_scan(adapter);
93 }
94
95 /**
96  * zfcp_fc_post_event - post event to userspace via fc_transport
97  * @work: work struct with enqueued events
98  */
99 void zfcp_fc_post_event(struct work_struct *work)
100 {
101         struct zfcp_fc_event *event = NULL, *tmp = NULL;
102         LIST_HEAD(tmp_lh);
103         struct zfcp_fc_events *events = container_of(work,
104                                         struct zfcp_fc_events, work);
105         struct zfcp_adapter *adapter = container_of(events, struct zfcp_adapter,
106                                                 events);
107
108         spin_lock_bh(&events->list_lock);
109         list_splice_init(&events->list, &tmp_lh);
110         spin_unlock_bh(&events->list_lock);
111
112         list_for_each_entry_safe(event, tmp, &tmp_lh, list) {
113                 fc_host_post_event(adapter->scsi_host, fc_get_event_number(),
114                                 event->code, event->data);
115                 list_del(&event->list);
116                 kfree(event);
117         }
118
119 }
120
121 /**
122  * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context
123  * @adapter: The adapter where to enqueue the event
124  * @event_code: The event code (as defined in fc_host_event_code in
125  *              scsi_transport_fc.h)
126  * @event_data: The event data (e.g. n_port page in case of els)
127  */
128 void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
129                         enum fc_host_event_code event_code, u32 event_data)
130 {
131         struct zfcp_fc_event *event;
132
133         event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
134         if (!event)
135                 return;
136
137         event->code = event_code;
138         event->data = event_data;
139
140         spin_lock(&adapter->events.list_lock);
141         list_add_tail(&event->list, &adapter->events.list);
142         spin_unlock(&adapter->events.list_lock);
143
144         queue_work(adapter->work_queue, &adapter->events.work);
145 }
146
147 static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
148 {
149         int ret = -EIO;
150
151         if (mutex_lock_interruptible(&wka_port->mutex))
152                 return -ERESTARTSYS;
153
154         if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
155             wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
156                 wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
157                 if (zfcp_fsf_open_wka_port(wka_port)) {
158                         /* could not even send request, nothing to wait for */
159                         wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
160                         goto out;
161                 }
162         }
163
164         wait_event(wka_port->opened,
165                    wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
166                    wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
167
168         if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
169                 atomic_inc(&wka_port->refcount);
170                 ret = 0;
171                 goto out;
172         }
173 out:
174         mutex_unlock(&wka_port->mutex);
175         return ret;
176 }
177
178 static void zfcp_fc_wka_port_offline(struct work_struct *work)
179 {
180         struct delayed_work *dw = to_delayed_work(work);
181         struct zfcp_fc_wka_port *wka_port =
182                         container_of(dw, struct zfcp_fc_wka_port, work);
183
184         mutex_lock(&wka_port->mutex);
185         if ((atomic_read(&wka_port->refcount) != 0) ||
186             (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
187                 goto out;
188
189         wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
190         if (zfcp_fsf_close_wka_port(wka_port)) {
191                 /* could not even send request, nothing to wait for */
192                 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
193                 goto out;
194         }
195         wait_event(wka_port->closed,
196                    wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
197 out:
198         mutex_unlock(&wka_port->mutex);
199 }
200
201 static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
202 {
203         if (atomic_dec_return(&wka_port->refcount) != 0)
204                 return;
205         /* wait 10 milliseconds, other reqs might pop in */
206         queue_delayed_work(wka_port->adapter->work_queue, &wka_port->work,
207                            msecs_to_jiffies(10));
208 }
209
210 static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
211                                   struct zfcp_adapter *adapter)
212 {
213         init_waitqueue_head(&wka_port->opened);
214         init_waitqueue_head(&wka_port->closed);
215
216         wka_port->adapter = adapter;
217         wka_port->d_id = d_id;
218
219         wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
220         atomic_set(&wka_port->refcount, 0);
221         mutex_init(&wka_port->mutex);
222         INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
223 }
224
225 static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
226 {
227         cancel_delayed_work_sync(&wka->work);
228         mutex_lock(&wka->mutex);
229         wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
230         mutex_unlock(&wka->mutex);
231 }
232
233 void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
234 {
235         if (!gs)
236                 return;
237         zfcp_fc_wka_port_force_offline(&gs->ms);
238         zfcp_fc_wka_port_force_offline(&gs->ts);
239         zfcp_fc_wka_port_force_offline(&gs->ds);
240         zfcp_fc_wka_port_force_offline(&gs->as);
241 }
242
243 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
244                                    struct fc_els_rscn_page *page)
245 {
246         unsigned long flags;
247         struct zfcp_adapter *adapter = fsf_req->adapter;
248         struct zfcp_port *port;
249
250         read_lock_irqsave(&adapter->port_list_lock, flags);
251         list_for_each_entry(port, &adapter->port_list, list) {
252                 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
253                         zfcp_fc_test_link(port);
254         }
255         read_unlock_irqrestore(&adapter->port_list_lock, flags);
256 }
257
258 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
259 {
260         struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
261         struct zfcp_adapter *adapter = fsf_req->adapter;
262         struct fc_els_rscn *head;
263         struct fc_els_rscn_page *page;
264         u16 i;
265         u16 no_entries;
266         unsigned int afmt;
267
268         head = (struct fc_els_rscn *) status_buffer->payload.data;
269         page = (struct fc_els_rscn_page *) head;
270
271         /* see FC-FS */
272         no_entries = be16_to_cpu(head->rscn_plen) /
273                 sizeof(struct fc_els_rscn_page);
274
275         if (no_entries > 1) {
276                 /* handle failed ports */
277                 unsigned long flags;
278                 struct zfcp_port *port;
279
280                 read_lock_irqsave(&adapter->port_list_lock, flags);
281                 list_for_each_entry(port, &adapter->port_list, list) {
282                         if (port->d_id)
283                                 continue;
284                         zfcp_erp_port_reopen(port,
285                                              ZFCP_STATUS_COMMON_ERP_FAILED,
286                                              "fcrscn1");
287                 }
288                 read_unlock_irqrestore(&adapter->port_list_lock, flags);
289         }
290
291         for (i = 1; i < no_entries; i++) {
292                 /* skip head and start with 1st element */
293                 page++;
294                 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
295                 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
296                                        page);
297                 zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
298                                       *(u32 *)page);
299         }
300         zfcp_fc_conditional_port_scan(fsf_req->adapter);
301 }
302
303 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
304 {
305         unsigned long flags;
306         struct zfcp_adapter *adapter = req->adapter;
307         struct zfcp_port *port;
308
309         read_lock_irqsave(&adapter->port_list_lock, flags);
310         list_for_each_entry(port, &adapter->port_list, list)
311                 if (port->wwpn == wwpn) {
312                         zfcp_erp_port_forced_reopen(port, 0, "fciwwp1");
313                         break;
314                 }
315         read_unlock_irqrestore(&adapter->port_list_lock, flags);
316 }
317
318 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
319 {
320         struct fsf_status_read_buffer *status_buffer;
321         struct fc_els_flogi *plogi;
322
323         status_buffer = (struct fsf_status_read_buffer *) req->data;
324         plogi = (struct fc_els_flogi *) status_buffer->payload.data;
325         zfcp_fc_incoming_wwpn(req, be64_to_cpu(plogi->fl_wwpn));
326 }
327
328 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
329 {
330         struct fsf_status_read_buffer *status_buffer =
331                 (struct fsf_status_read_buffer *)req->data;
332         struct fc_els_logo *logo =
333                 (struct fc_els_logo *) status_buffer->payload.data;
334
335         zfcp_fc_incoming_wwpn(req, be64_to_cpu(logo->fl_n_port_wwn));
336 }
337
338 /**
339  * zfcp_fc_incoming_els - handle incoming ELS
340  * @fsf_req - request which contains incoming ELS
341  */
342 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
343 {
344         struct fsf_status_read_buffer *status_buffer =
345                 (struct fsf_status_read_buffer *) fsf_req->data;
346         unsigned int els_type = status_buffer->payload.data[0];
347
348         zfcp_dbf_san_in_els("fciels1", fsf_req);
349         if (els_type == ELS_PLOGI)
350                 zfcp_fc_incoming_plogi(fsf_req);
351         else if (els_type == ELS_LOGO)
352                 zfcp_fc_incoming_logo(fsf_req);
353         else if (els_type == ELS_RSCN)
354                 zfcp_fc_incoming_rscn(fsf_req);
355 }
356
357 static void zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req *fc_req)
358 {
359         struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
360         struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
361
362         if (ct_els->status)
363                 return;
364         if (gid_pn_rsp->ct_hdr.ct_cmd != cpu_to_be16(FC_FS_ACC))
365                 return;
366
367         /* looks like a valid d_id */
368         ct_els->port->d_id = ntoh24(gid_pn_rsp->gid_pn.fp_fid);
369 }
370
371 static void zfcp_fc_complete(void *data)
372 {
373         complete(data);
374 }
375
376 static void zfcp_fc_ct_ns_init(struct fc_ct_hdr *ct_hdr, u16 cmd, u16 mr_size)
377 {
378         ct_hdr->ct_rev = FC_CT_REV;
379         ct_hdr->ct_fs_type = FC_FST_DIR;
380         ct_hdr->ct_fs_subtype = FC_NS_SUBTYPE;
381         ct_hdr->ct_cmd = cpu_to_be16(cmd);
382         ct_hdr->ct_mr_size = cpu_to_be16(mr_size / 4);
383 }
384
385 static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
386                                      struct zfcp_fc_req *fc_req)
387 {
388         struct zfcp_adapter *adapter = port->adapter;
389         DECLARE_COMPLETION_ONSTACK(completion);
390         struct zfcp_fc_gid_pn_req *gid_pn_req = &fc_req->u.gid_pn.req;
391         struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
392         int ret;
393
394         /* setup parameters for send generic command */
395         fc_req->ct_els.port = port;
396         fc_req->ct_els.handler = zfcp_fc_complete;
397         fc_req->ct_els.handler_data = &completion;
398         fc_req->ct_els.req = &fc_req->sg_req;
399         fc_req->ct_els.resp = &fc_req->sg_rsp;
400         sg_init_one(&fc_req->sg_req, gid_pn_req, sizeof(*gid_pn_req));
401         sg_init_one(&fc_req->sg_rsp, gid_pn_rsp, sizeof(*gid_pn_rsp));
402
403         zfcp_fc_ct_ns_init(&gid_pn_req->ct_hdr,
404                            FC_NS_GID_PN, ZFCP_FC_CT_SIZE_PAGE);
405         gid_pn_req->gid_pn.fn_wwpn = cpu_to_be64(port->wwpn);
406
407         ret = zfcp_fsf_send_ct(&adapter->gs->ds, &fc_req->ct_els,
408                                adapter->pool.gid_pn_req,
409                                ZFCP_FC_CTELS_TMO);
410         if (!ret) {
411                 wait_for_completion(&completion);
412                 zfcp_fc_ns_gid_pn_eval(fc_req);
413         }
414         return ret;
415 }
416
417 /**
418  * zfcp_fc_ns_gid_pn - initiate GID_PN nameserver request
419  * @port: port where GID_PN request is needed
420  * return: -ENOMEM on error, 0 otherwise
421  */
422 static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
423 {
424         int ret;
425         struct zfcp_fc_req *fc_req;
426         struct zfcp_adapter *adapter = port->adapter;
427
428         fc_req = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
429         if (!fc_req)
430                 return -ENOMEM;
431
432         memset(fc_req, 0, sizeof(*fc_req));
433
434         ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
435         if (ret)
436                 goto out;
437
438         ret = zfcp_fc_ns_gid_pn_request(port, fc_req);
439
440         zfcp_fc_wka_port_put(&adapter->gs->ds);
441 out:
442         mempool_free(fc_req, adapter->pool.gid_pn);
443         return ret;
444 }
445
446 void zfcp_fc_port_did_lookup(struct work_struct *work)
447 {
448         int ret;
449         struct zfcp_port *port = container_of(work, struct zfcp_port,
450                                               gid_pn_work);
451
452         ret = zfcp_fc_ns_gid_pn(port);
453         if (ret) {
454                 /* could not issue gid_pn for some reason */
455                 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1");
456                 goto out;
457         }
458
459         if (!port->d_id) {
460                 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
461                 goto out;
462         }
463
464         zfcp_erp_port_reopen(port, 0, "fcgpn_3");
465 out:
466         put_device(&port->dev);
467 }
468
469 /**
470  * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
471  * @port: The zfcp_port to lookup the d_id for.
472  */
473 void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
474 {
475         get_device(&port->dev);
476         if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
477                 put_device(&port->dev);
478 }
479
480 /**
481  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
482  * @port: zfcp_port structure
483  * @plogi: plogi payload
484  *
485  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
486  */
487 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
488 {
489         if (be64_to_cpu(plogi->fl_wwpn) != port->wwpn) {
490                 port->d_id = 0;
491                 dev_warn(&port->adapter->ccw_device->dev,
492                          "A port opened with WWPN 0x%016Lx returned data that "
493                          "identifies it as WWPN 0x%016Lx\n",
494                          (unsigned long long) port->wwpn,
495                          (unsigned long long) be64_to_cpu(plogi->fl_wwpn));
496                 return;
497         }
498
499         port->wwnn = be64_to_cpu(plogi->fl_wwnn);
500         port->maxframe_size = be16_to_cpu(plogi->fl_csp.sp_bb_data);
501
502         if (plogi->fl_cssp[0].cp_class & cpu_to_be16(FC_CPC_VALID))
503                 port->supported_classes |= FC_COS_CLASS1;
504         if (plogi->fl_cssp[1].cp_class & cpu_to_be16(FC_CPC_VALID))
505                 port->supported_classes |= FC_COS_CLASS2;
506         if (plogi->fl_cssp[2].cp_class & cpu_to_be16(FC_CPC_VALID))
507                 port->supported_classes |= FC_COS_CLASS3;
508         if (plogi->fl_cssp[3].cp_class & cpu_to_be16(FC_CPC_VALID))
509                 port->supported_classes |= FC_COS_CLASS4;
510 }
511
512 static void zfcp_fc_adisc_handler(void *data)
513 {
514         struct zfcp_fc_req *fc_req = data;
515         struct zfcp_port *port = fc_req->ct_els.port;
516         struct fc_els_adisc *adisc_resp = &fc_req->u.adisc.rsp;
517
518         if (fc_req->ct_els.status) {
519                 /* request rejected or timed out */
520                 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
521                                             "fcadh_1");
522                 goto out;
523         }
524
525         if (!port->wwnn)
526                 port->wwnn = be64_to_cpu(adisc_resp->adisc_wwnn);
527
528         if ((port->wwpn != be64_to_cpu(adisc_resp->adisc_wwpn)) ||
529             !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
530                 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
531                                      "fcadh_2");
532                 goto out;
533         }
534
535         /* re-init to undo drop from zfcp_fc_adisc() */
536         port->d_id = ntoh24(adisc_resp->adisc_port_id);
537         /* port is good, unblock rport without going through erp */
538         zfcp_scsi_schedule_rport_register(port);
539  out:
540         atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
541         put_device(&port->dev);
542         kmem_cache_free(zfcp_fc_req_cache, fc_req);
543 }
544
545 static int zfcp_fc_adisc(struct zfcp_port *port)
546 {
547         struct zfcp_fc_req *fc_req;
548         struct zfcp_adapter *adapter = port->adapter;
549         struct Scsi_Host *shost = adapter->scsi_host;
550         u32 d_id;
551         int ret;
552
553         fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC);
554         if (!fc_req)
555                 return -ENOMEM;
556
557         fc_req->ct_els.port = port;
558         fc_req->ct_els.req = &fc_req->sg_req;
559         fc_req->ct_els.resp = &fc_req->sg_rsp;
560         sg_init_one(&fc_req->sg_req, &fc_req->u.adisc.req,
561                     sizeof(struct fc_els_adisc));
562         sg_init_one(&fc_req->sg_rsp, &fc_req->u.adisc.rsp,
563                     sizeof(struct fc_els_adisc));
564
565         fc_req->ct_els.handler = zfcp_fc_adisc_handler;
566         fc_req->ct_els.handler_data = fc_req;
567
568         /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
569            without FC-AL-2 capability, so we don't set it */
570         fc_req->u.adisc.req.adisc_wwpn = cpu_to_be64(fc_host_port_name(shost));
571         fc_req->u.adisc.req.adisc_wwnn = cpu_to_be64(fc_host_node_name(shost));
572         fc_req->u.adisc.req.adisc_cmd = ELS_ADISC;
573         hton24(fc_req->u.adisc.req.adisc_port_id, fc_host_port_id(shost));
574
575         d_id = port->d_id; /* remember as destination for send els below */
576         /*
577          * Force fresh GID_PN lookup on next port recovery.
578          * Must happen after request setup and before sending request,
579          * to prevent race with port->d_id re-init in zfcp_fc_adisc_handler().
580          */
581         port->d_id = 0;
582
583         ret = zfcp_fsf_send_els(adapter, d_id, &fc_req->ct_els,
584                                 ZFCP_FC_CTELS_TMO);
585         if (ret)
586                 kmem_cache_free(zfcp_fc_req_cache, fc_req);
587
588         return ret;
589 }
590
591 void zfcp_fc_link_test_work(struct work_struct *work)
592 {
593         struct zfcp_port *port =
594                 container_of(work, struct zfcp_port, test_link_work);
595         int retval;
596
597         get_device(&port->dev);
598         port->rport_task = RPORT_DEL;
599         zfcp_scsi_rport_work(&port->rport_work);
600
601         /* only issue one test command at one time per port */
602         if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
603                 goto out;
604
605         atomic_or(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
606
607         retval = zfcp_fc_adisc(port);
608         if (retval == 0)
609                 return;
610
611         /* send of ADISC was not possible */
612         atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
613         zfcp_erp_port_forced_reopen(port, 0, "fcltwk1");
614
615 out:
616         put_device(&port->dev);
617 }
618
619 /**
620  * zfcp_fc_test_link - lightweight link test procedure
621  * @port: port to be tested
622  *
623  * Test status of a link to a remote port using the ELS command ADISC.
624  * If there is a problem with the remote port, error recovery steps
625  * will be triggered.
626  */
627 void zfcp_fc_test_link(struct zfcp_port *port)
628 {
629         get_device(&port->dev);
630         if (!queue_work(port->adapter->work_queue, &port->test_link_work))
631                 put_device(&port->dev);
632 }
633
634 static struct zfcp_fc_req *zfcp_alloc_sg_env(int buf_num)
635 {
636         struct zfcp_fc_req *fc_req;
637
638         fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
639         if (!fc_req)
640                 return NULL;
641
642         if (zfcp_sg_setup_table(&fc_req->sg_rsp, buf_num)) {
643                 kmem_cache_free(zfcp_fc_req_cache, fc_req);
644                 return NULL;
645         }
646
647         sg_init_one(&fc_req->sg_req, &fc_req->u.gpn_ft.req,
648                     sizeof(struct zfcp_fc_gpn_ft_req));
649
650         return fc_req;
651 }
652
653 static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req,
654                                struct zfcp_adapter *adapter, int max_bytes)
655 {
656         struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
657         struct zfcp_fc_gpn_ft_req *req = &fc_req->u.gpn_ft.req;
658         DECLARE_COMPLETION_ONSTACK(completion);
659         int ret;
660
661         zfcp_fc_ct_ns_init(&req->ct_hdr, FC_NS_GPN_FT, max_bytes);
662         req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
663
664         ct_els->handler = zfcp_fc_complete;
665         ct_els->handler_data = &completion;
666         ct_els->req = &fc_req->sg_req;
667         ct_els->resp = &fc_req->sg_rsp;
668
669         ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
670                                ZFCP_FC_CTELS_TMO);
671         if (!ret)
672                 wait_for_completion(&completion);
673         return ret;
674 }
675
676 static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
677 {
678         if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
679                 return;
680
681         atomic_andnot(ZFCP_STATUS_COMMON_NOESC, &port->status);
682
683         if ((port->supported_classes != 0) ||
684             !list_empty(&port->unit_list))
685                 return;
686
687         list_move_tail(&port->list, lh);
688 }
689
690 static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_req *fc_req,
691                                struct zfcp_adapter *adapter, int max_entries)
692 {
693         struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
694         struct scatterlist *sg = &fc_req->sg_rsp;
695         struct fc_ct_hdr *hdr = sg_virt(sg);
696         struct fc_gpn_ft_resp *acc = sg_virt(sg);
697         struct zfcp_port *port, *tmp;
698         unsigned long flags;
699         LIST_HEAD(remove_lh);
700         u32 d_id;
701         int ret = 0, x, last = 0;
702
703         if (ct_els->status)
704                 return -EIO;
705
706         if (hdr->ct_cmd != cpu_to_be16(FC_FS_ACC)) {
707                 if (hdr->ct_reason == FC_FS_RJT_UNABL)
708                         return -EAGAIN; /* might be a temporary condition */
709                 return -EIO;
710         }
711
712         if (hdr->ct_mr_size) {
713                 dev_warn(&adapter->ccw_device->dev,
714                          "The name server reported %d words residual data\n",
715                          hdr->ct_mr_size);
716                 return -E2BIG;
717         }
718
719         /* first entry is the header */
720         for (x = 1; x < max_entries && !last; x++) {
721                 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
722                         acc++;
723                 else
724                         acc = sg_virt(++sg);
725
726                 last = acc->fp_flags & FC_NS_FID_LAST;
727                 d_id = ntoh24(acc->fp_fid);
728
729                 /* don't attach ports with a well known address */
730                 if (d_id >= FC_FID_WELL_KNOWN_BASE)
731                         continue;
732                 /* skip the adapter's port and known remote ports */
733                 if (be64_to_cpu(acc->fp_wwpn) ==
734                     fc_host_port_name(adapter->scsi_host))
735                         continue;
736
737                 port = zfcp_port_enqueue(adapter, be64_to_cpu(acc->fp_wwpn),
738                                          ZFCP_STATUS_COMMON_NOESC, d_id);
739                 if (!IS_ERR(port))
740                         zfcp_erp_port_reopen(port, 0, "fcegpf1");
741                 else if (PTR_ERR(port) != -EEXIST)
742                         ret = PTR_ERR(port);
743         }
744
745         zfcp_erp_wait(adapter);
746         write_lock_irqsave(&adapter->port_list_lock, flags);
747         list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
748                 zfcp_fc_validate_port(port, &remove_lh);
749         write_unlock_irqrestore(&adapter->port_list_lock, flags);
750
751         list_for_each_entry_safe(port, tmp, &remove_lh, list) {
752                 zfcp_erp_port_shutdown(port, 0, "fcegpf2");
753                 device_unregister(&port->dev);
754         }
755
756         return ret;
757 }
758
759 /**
760  * zfcp_fc_scan_ports - scan remote ports and attach new ports
761  * @work: reference to scheduled work
762  */
763 void zfcp_fc_scan_ports(struct work_struct *work)
764 {
765         struct delayed_work *dw = to_delayed_work(work);
766         struct zfcp_adapter *adapter = container_of(dw, struct zfcp_adapter,
767                                                     scan_work);
768         int ret, i;
769         struct zfcp_fc_req *fc_req;
770         int chain, max_entries, buf_num, max_bytes;
771
772         zfcp_fc_port_scan_time(adapter);
773
774         chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
775         buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
776         max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
777         max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
778
779         if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
780             fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
781                 return;
782
783         if (zfcp_fc_wka_port_get(&adapter->gs->ds))
784                 return;
785
786         fc_req = zfcp_alloc_sg_env(buf_num);
787         if (!fc_req)
788                 goto out;
789
790         for (i = 0; i < 3; i++) {
791                 ret = zfcp_fc_send_gpn_ft(fc_req, adapter, max_bytes);
792                 if (!ret) {
793                         ret = zfcp_fc_eval_gpn_ft(fc_req, adapter, max_entries);
794                         if (ret == -EAGAIN)
795                                 ssleep(1);
796                         else
797                                 break;
798                 }
799         }
800         zfcp_sg_free_table(&fc_req->sg_rsp, buf_num);
801         kmem_cache_free(zfcp_fc_req_cache, fc_req);
802 out:
803         zfcp_fc_wka_port_put(&adapter->gs->ds);
804 }
805
806 static int zfcp_fc_gspn(struct zfcp_adapter *adapter,
807                         struct zfcp_fc_req *fc_req)
808 {
809         DECLARE_COMPLETION_ONSTACK(completion);
810         char devno[] = "DEVNO:";
811         struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
812         struct zfcp_fc_gspn_req *gspn_req = &fc_req->u.gspn.req;
813         struct zfcp_fc_gspn_rsp *gspn_rsp = &fc_req->u.gspn.rsp;
814         int ret;
815
816         zfcp_fc_ct_ns_init(&gspn_req->ct_hdr, FC_NS_GSPN_ID,
817                            FC_SYMBOLIC_NAME_SIZE);
818         hton24(gspn_req->gspn.fp_fid, fc_host_port_id(adapter->scsi_host));
819
820         sg_init_one(&fc_req->sg_req, gspn_req, sizeof(*gspn_req));
821         sg_init_one(&fc_req->sg_rsp, gspn_rsp, sizeof(*gspn_rsp));
822
823         ct_els->handler = zfcp_fc_complete;
824         ct_els->handler_data = &completion;
825         ct_els->req = &fc_req->sg_req;
826         ct_els->resp = &fc_req->sg_rsp;
827
828         ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
829                                ZFCP_FC_CTELS_TMO);
830         if (ret)
831                 return ret;
832
833         wait_for_completion(&completion);
834         if (ct_els->status)
835                 return ct_els->status;
836
837         if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_NPIV &&
838             !(strstr(gspn_rsp->gspn.fp_name, devno)))
839                 snprintf(fc_host_symbolic_name(adapter->scsi_host),
840                          FC_SYMBOLIC_NAME_SIZE, "%s%s %s NAME: %s",
841                          gspn_rsp->gspn.fp_name, devno,
842                          dev_name(&adapter->ccw_device->dev),
843                          init_utsname()->nodename);
844         else
845                 strlcpy(fc_host_symbolic_name(adapter->scsi_host),
846                         gspn_rsp->gspn.fp_name, FC_SYMBOLIC_NAME_SIZE);
847
848         return 0;
849 }
850
851 static void zfcp_fc_rspn(struct zfcp_adapter *adapter,
852                          struct zfcp_fc_req *fc_req)
853 {
854         DECLARE_COMPLETION_ONSTACK(completion);
855         struct Scsi_Host *shost = adapter->scsi_host;
856         struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
857         struct zfcp_fc_rspn_req *rspn_req = &fc_req->u.rspn.req;
858         struct fc_ct_hdr *rspn_rsp = &fc_req->u.rspn.rsp;
859         int ret, len;
860
861         zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID,
862                            FC_SYMBOLIC_NAME_SIZE);
863         hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost));
864         len = strlcpy(rspn_req->rspn.fr_name, fc_host_symbolic_name(shost),
865                       FC_SYMBOLIC_NAME_SIZE);
866         rspn_req->rspn.fr_name_len = len;
867
868         sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req));
869         sg_init_one(&fc_req->sg_rsp, rspn_rsp, sizeof(*rspn_rsp));
870
871         ct_els->handler = zfcp_fc_complete;
872         ct_els->handler_data = &completion;
873         ct_els->req = &fc_req->sg_req;
874         ct_els->resp = &fc_req->sg_rsp;
875
876         ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
877                                ZFCP_FC_CTELS_TMO);
878         if (!ret)
879                 wait_for_completion(&completion);
880 }
881
882 /**
883  * zfcp_fc_sym_name_update - Retrieve and update the symbolic port name
884  * @work: ns_up_work of the adapter where to update the symbolic port name
885  *
886  * Retrieve the current symbolic port name that may have been set by
887  * the hardware using the GSPN request and update the fc_host
888  * symbolic_name sysfs attribute. When running in NPIV mode (and hence
889  * the port name is unique for this system), update the symbolic port
890  * name to add Linux specific information and update the FC nameserver
891  * using the RSPN request.
892  */
893 void zfcp_fc_sym_name_update(struct work_struct *work)
894 {
895         struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
896                                                     ns_up_work);
897         int ret;
898         struct zfcp_fc_req *fc_req;
899
900         if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
901             fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
902                 return;
903
904         fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
905         if (!fc_req)
906                 return;
907
908         ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
909         if (ret)
910                 goto out_free;
911
912         ret = zfcp_fc_gspn(adapter, fc_req);
913         if (ret || fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
914                 goto out_ds_put;
915
916         memset(fc_req, 0, sizeof(*fc_req));
917         zfcp_fc_rspn(adapter, fc_req);
918
919 out_ds_put:
920         zfcp_fc_wka_port_put(&adapter->gs->ds);
921 out_free:
922         kmem_cache_free(zfcp_fc_req_cache, fc_req);
923 }
924
925 static void zfcp_fc_ct_els_job_handler(void *data)
926 {
927         struct bsg_job *job = data;
928         struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
929         struct fc_bsg_reply *jr = job->reply;
930
931         jr->reply_payload_rcv_len = job->reply_payload.payload_len;
932         jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
933         jr->result = zfcp_ct_els->status ? -EIO : 0;
934         bsg_job_done(job, jr->result, jr->reply_payload_rcv_len);
935 }
936
937 static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct bsg_job *job)
938 {
939         u32 preamble_word1;
940         u8 gs_type;
941         struct zfcp_adapter *adapter;
942         struct fc_bsg_request *bsg_request = job->request;
943         struct fc_rport *rport = fc_bsg_to_rport(job);
944         struct Scsi_Host *shost;
945
946         preamble_word1 = bsg_request->rqst_data.r_ct.preamble_word1;
947         gs_type = (preamble_word1 & 0xff000000) >> 24;
948
949         shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
950         adapter = (struct zfcp_adapter *) shost->hostdata[0];
951
952         switch (gs_type) {
953         case FC_FST_ALIAS:
954                 return &adapter->gs->as;
955         case FC_FST_MGMT:
956                 return &adapter->gs->ms;
957         case FC_FST_TIME:
958                 return &adapter->gs->ts;
959                 break;
960         case FC_FST_DIR:
961                 return &adapter->gs->ds;
962                 break;
963         default:
964                 return NULL;
965         }
966 }
967
968 static void zfcp_fc_ct_job_handler(void *data)
969 {
970         struct bsg_job *job = data;
971         struct zfcp_fc_wka_port *wka_port;
972
973         wka_port = zfcp_fc_job_wka_port(job);
974         zfcp_fc_wka_port_put(wka_port);
975
976         zfcp_fc_ct_els_job_handler(data);
977 }
978
979 static int zfcp_fc_exec_els_job(struct bsg_job *job,
980                                 struct zfcp_adapter *adapter)
981 {
982         struct zfcp_fsf_ct_els *els = job->dd_data;
983         struct fc_rport *rport = fc_bsg_to_rport(job);
984         struct fc_bsg_request *bsg_request = job->request;
985         struct zfcp_port *port;
986         u32 d_id;
987
988         if (rport) {
989                 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
990                 if (!port)
991                         return -EINVAL;
992
993                 d_id = port->d_id;
994                 put_device(&port->dev);
995         } else
996                 d_id = ntoh24(bsg_request->rqst_data.h_els.port_id);
997
998         els->handler = zfcp_fc_ct_els_job_handler;
999         return zfcp_fsf_send_els(adapter, d_id, els, job->req->timeout / HZ);
1000 }
1001
1002 static int zfcp_fc_exec_ct_job(struct bsg_job *job,
1003                                struct zfcp_adapter *adapter)
1004 {
1005         int ret;
1006         struct zfcp_fsf_ct_els *ct = job->dd_data;
1007         struct zfcp_fc_wka_port *wka_port;
1008
1009         wka_port = zfcp_fc_job_wka_port(job);
1010         if (!wka_port)
1011                 return -EINVAL;
1012
1013         ret = zfcp_fc_wka_port_get(wka_port);
1014         if (ret)
1015                 return ret;
1016
1017         ct->handler = zfcp_fc_ct_job_handler;
1018         ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->req->timeout / HZ);
1019         if (ret)
1020                 zfcp_fc_wka_port_put(wka_port);
1021
1022         return ret;
1023 }
1024
1025 int zfcp_fc_exec_bsg_job(struct bsg_job *job)
1026 {
1027         struct Scsi_Host *shost;
1028         struct zfcp_adapter *adapter;
1029         struct zfcp_fsf_ct_els *ct_els = job->dd_data;
1030         struct fc_bsg_request *bsg_request = job->request;
1031         struct fc_rport *rport = fc_bsg_to_rport(job);
1032
1033         shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
1034         adapter = (struct zfcp_adapter *)shost->hostdata[0];
1035
1036         if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
1037                 return -EINVAL;
1038
1039         ct_els->req = job->request_payload.sg_list;
1040         ct_els->resp = job->reply_payload.sg_list;
1041         ct_els->handler_data = job;
1042
1043         switch (bsg_request->msgcode) {
1044         case FC_BSG_RPT_ELS:
1045         case FC_BSG_HST_ELS_NOLOGIN:
1046                 return zfcp_fc_exec_els_job(job, adapter);
1047         case FC_BSG_RPT_CT:
1048         case FC_BSG_HST_CT:
1049                 return zfcp_fc_exec_ct_job(job, adapter);
1050         default:
1051                 return -EINVAL;
1052         }
1053 }
1054
1055 int zfcp_fc_timeout_bsg_job(struct bsg_job *job)
1056 {
1057         /* hardware tracks timeout, reset bsg timeout to not interfere */
1058         return -EAGAIN;
1059 }
1060
1061 int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
1062 {
1063         struct zfcp_fc_wka_ports *wka_ports;
1064
1065         wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
1066         if (!wka_ports)
1067                 return -ENOMEM;
1068
1069         adapter->gs = wka_ports;
1070         zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
1071         zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
1072         zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
1073         zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
1074
1075         return 0;
1076 }
1077
1078 void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
1079 {
1080         kfree(adapter->gs);
1081         adapter->gs = NULL;
1082 }
1083