blob: 511bf8e0a436c2eb40f2601c6a68edf1724ea887 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christof Schmitt24073b42008-06-10 18:20:54 +02002/*
3 * zfcp device driver
4 *
5 * Fibre Channel related functions for the zfcp device driver.
6 *
Steffen Maierab8ab4b2017-07-28 12:30:59 +02007 * Copyright IBM Corp. 2008, 2017
Christof Schmitt24073b42008-06-10 18:20:54 +02008 */
9
Christof Schmittecf39d42008-12-25 13:39:53 +010010#define KMSG_COMPONENT "zfcp"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Christof Schmitt9d05ce22009-11-24 16:54:09 +010013#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Christof Schmitt038d9442011-02-22 19:54:48 +010015#include <linux/utsname.h>
Martin Peschke18f87a62014-11-13 14:59:48 +010016#include <linux/random.h>
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +010017#include <linux/bsg-lib.h>
Christof Schmitt9d05ce22009-11-24 16:54:09 +010018#include <scsi/fc/fc_els.h>
19#include <scsi/libfc.h>
Christof Schmitt24073b42008-06-10 18:20:54 +020020#include "zfcp_ext.h"
Christof Schmitt9d05ce22009-11-24 16:54:09 +010021#include "zfcp_fc.h"
Christof Schmitt24073b42008-06-10 18:20:54 +020022
Christof Schmitt087897e2011-02-22 19:54:41 +010023struct kmem_cache *zfcp_fc_req_cache;
24
Christof Schmitt9d05ce22009-11-24 16:54:09 +010025static 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,
Christof Schmitte0d7fcb2008-12-19 16:56:58 +010030};
31
Steffen Maier43f60cb2012-09-04 15:23:35 +020032static bool no_auto_port_rescan;
Martin Peschkeb096ef82017-07-28 12:31:07 +020033module_param(no_auto_port_rescan, bool, 0600);
Steffen Maier43f60cb2012-09-04 15:23:35 +020034MODULE_PARM_DESC(no_auto_port_rescan,
35 "no automatic port_rescan (default off)");
36
Martin Peschke18f87a62014-11-13 14:59:48 +010037static unsigned int port_scan_backoff = 500;
38module_param(port_scan_backoff, uint, 0600);
39MODULE_PARM_DESC(port_scan_backoff,
40 "upper limit of port scan random backoff in msecs (default 500)");
41
42static unsigned int port_scan_ratelimit = 60000;
43module_param(port_scan_ratelimit, uint, 0600);
44MODULE_PARM_DESC(port_scan_ratelimit,
45 "minimum interval between port scans in msecs (default 60000)");
46
47unsigned int zfcp_fc_port_scan_backoff(void)
48{
49 if (!port_scan_backoff)
50 return 0;
George Spelvin0cd0e572020-07-03 15:19:57 +020051 return prandom_u32_max(port_scan_backoff);
Martin Peschke18f87a62014-11-13 14:59:48 +010052}
53
54static 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
62static 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
Steffen Maier43f60cb2012-09-04 15:23:35 +020079void zfcp_fc_conditional_port_scan(struct zfcp_adapter *adapter)
80{
81 if (no_auto_port_rescan)
82 return;
83
Martin Peschke18f87a62014-11-13 14:59:48 +010084 zfcp_fc_port_scan(adapter);
Steffen Maier43f60cb2012-09-04 15:23:35 +020085}
86
87void zfcp_fc_inverse_conditional_port_scan(struct zfcp_adapter *adapter)
88{
89 if (!no_auto_port_rescan)
90 return;
91
Martin Peschke18f87a62014-11-13 14:59:48 +010092 zfcp_fc_port_scan(adapter);
Steffen Maier43f60cb2012-09-04 15:23:35 +020093}
94
Sven Schuetz2d1e5472010-07-16 15:37:39 +020095/**
96 * zfcp_fc_post_event - post event to userspace via fc_transport
97 * @work: work struct with enqueued events
98 */
99void 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(),
Steffen Maier69192982018-05-17 19:15:04 +0200114 event->code, event->data);
Sven Schuetz2d1e5472010-07-16 15:37:39 +0200115 list_del(&event->list);
116 kfree(event);
117 }
Sven Schuetz2d1e5472010-07-16 15:37:39 +0200118}
119
120/**
121 * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context
122 * @adapter: The adapter where to enqueue the event
123 * @event_code: The event code (as defined in fc_host_event_code in
124 * scsi_transport_fc.h)
125 * @event_data: The event data (e.g. n_port page in case of els)
126 */
127void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
Steffen Maier69192982018-05-17 19:15:04 +0200128 enum fc_host_event_code event_code, u32 event_data)
Sven Schuetz2d1e5472010-07-16 15:37:39 +0200129{
130 struct zfcp_fc_event *event;
131
132 event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
133 if (!event)
134 return;
135
136 event->code = event_code;
137 event->data = event_data;
138
139 spin_lock(&adapter->events.list_lock);
140 list_add_tail(&event->list, &adapter->events.list);
141 spin_unlock(&adapter->events.list_lock);
142
143 queue_work(adapter->work_queue, &adapter->events.work);
144}
145
Christof Schmittbd0072e2009-11-24 16:54:11 +0100146static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200147{
148 if (mutex_lock_interruptible(&wka_port->mutex))
149 return -ERESTARTSYS;
150
Christof Schmittbd0072e2009-11-24 16:54:11 +0100151 if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
152 wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
153 wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200154 if (zfcp_fsf_open_wka_port(wka_port))
Christof Schmittbd0072e2009-11-24 16:54:11 +0100155 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200156 }
157
158 mutex_unlock(&wka_port->mutex);
159
Swen Schillig27f492c2009-07-13 15:06:13 +0200160 wait_event(wka_port->completion_wq,
Christof Schmittbd0072e2009-11-24 16:54:11 +0100161 wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
162 wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200163
Christof Schmittbd0072e2009-11-24 16:54:11 +0100164 if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
Swen Schillig5ab944f2008-10-01 12:42:17 +0200165 atomic_inc(&wka_port->refcount);
166 return 0;
167 }
168 return -EIO;
169}
170
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200171static void zfcp_fc_wka_port_offline(struct work_struct *work)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200172{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700173 struct delayed_work *dw = to_delayed_work(work);
Christof Schmittbd0072e2009-11-24 16:54:11 +0100174 struct zfcp_fc_wka_port *wka_port =
175 container_of(dw, struct zfcp_fc_wka_port, work);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200176
Swen Schillig5ab944f2008-10-01 12:42:17 +0200177 mutex_lock(&wka_port->mutex);
178 if ((atomic_read(&wka_port->refcount) != 0) ||
Christof Schmittbd0072e2009-11-24 16:54:11 +0100179 (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200180 goto out;
181
Christof Schmittbd0072e2009-11-24 16:54:11 +0100182 wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200183 if (zfcp_fsf_close_wka_port(wka_port)) {
Christof Schmittbd0072e2009-11-24 16:54:11 +0100184 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200185 wake_up(&wka_port->completion_wq);
186 }
187out:
188 mutex_unlock(&wka_port->mutex);
189}
190
Christof Schmittbd0072e2009-11-24 16:54:11 +0100191static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200192{
193 if (atomic_dec_return(&wka_port->refcount) != 0)
194 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200195 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200196 schedule_delayed_work(&wka_port->work, HZ / 100);
197}
198
Christof Schmittbd0072e2009-11-24 16:54:11 +0100199static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
Sven Schuetz9d544f22009-04-06 18:31:47 +0200200 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200201{
Swen Schillig5ab944f2008-10-01 12:42:17 +0200202 init_waitqueue_head(&wka_port->completion_wq);
203
204 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200205 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200206
Christof Schmittbd0072e2009-11-24 16:54:11 +0100207 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200208 atomic_set(&wka_port->refcount, 0);
209 mutex_init(&wka_port->mutex);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200210 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200211}
212
Christof Schmittbd0072e2009-11-24 16:54:11 +0100213static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
Swen Schillig828bc122009-04-17 15:08:05 +0200214{
215 cancel_delayed_work_sync(&wka->work);
216 mutex_lock(&wka->mutex);
Christof Schmittbd0072e2009-11-24 16:54:11 +0100217 wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig828bc122009-04-17 15:08:05 +0200218 mutex_unlock(&wka->mutex);
219}
220
Christof Schmittbd0072e2009-11-24 16:54:11 +0100221void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
Christof Schmitt55c770f2009-08-18 15:43:12 +0200222{
Swen Schilligf3450c72009-11-24 16:53:59 +0100223 if (!gs)
224 return;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200225 zfcp_fc_wka_port_force_offline(&gs->ms);
226 zfcp_fc_wka_port_force_offline(&gs->ts);
227 zfcp_fc_wka_port_force_offline(&gs->ds);
228 zfcp_fc_wka_port_force_offline(&gs->as);
Christof Schmitt55c770f2009-08-18 15:43:12 +0200229}
230
Christof Schmitt24073b42008-06-10 18:20:54 +0200231static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100232 struct fc_els_rscn_page *page)
Christof Schmitt24073b42008-06-10 18:20:54 +0200233{
234 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +0100235 struct zfcp_adapter *adapter = fsf_req->adapter;
Christof Schmitt24073b42008-06-10 18:20:54 +0200236 struct zfcp_port *port;
237
Swen Schilligecf0c772009-11-24 16:53:58 +0100238 read_lock_irqsave(&adapter->port_list_lock, flags);
239 list_for_each_entry(port, &adapter->port_list, list) {
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100240 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200241 zfcp_fc_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200242 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100243 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200244}
245
246static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
247{
248 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
Steffen Maierc8206572019-03-26 14:37:00 +0100249 struct zfcp_adapter *adapter = fsf_req->adapter;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100250 struct fc_els_rscn *head;
251 struct fc_els_rscn_page *page;
Christof Schmitt24073b42008-06-10 18:20:54 +0200252 u16 i;
253 u16 no_entries;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100254 unsigned int afmt;
Christof Schmitt24073b42008-06-10 18:20:54 +0200255
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100256 head = (struct fc_els_rscn *) status_buffer->payload.data;
257 page = (struct fc_els_rscn_page *) head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200258
259 /* see FC-FS */
Steffen Maier9d464fc2017-07-28 12:31:02 +0200260 no_entries = be16_to_cpu(head->rscn_plen) /
261 sizeof(struct fc_els_rscn_page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200262
Steffen Maierc8206572019-03-26 14:37:00 +0100263 if (no_entries > 1) {
264 /* handle failed ports */
265 unsigned long flags;
266 struct zfcp_port *port;
267
268 read_lock_irqsave(&adapter->port_list_lock, flags);
269 list_for_each_entry(port, &adapter->port_list, list) {
270 if (port->d_id)
271 continue;
272 zfcp_erp_port_reopen(port,
273 ZFCP_STATUS_COMMON_ERP_FAILED,
274 "fcrscn1");
275 }
276 read_unlock_irqrestore(&adapter->port_list_lock, flags);
277 }
278
Christof Schmitt24073b42008-06-10 18:20:54 +0200279 for (i = 1; i < no_entries; i++) {
280 /* skip head and start with 1st element */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100281 page++;
282 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
283 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
284 page);
Sven Schuetz2d1e5472010-07-16 15:37:39 +0200285 zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
286 *(u32 *)page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200287 }
Steffen Maier43f60cb2012-09-04 15:23:35 +0200288 zfcp_fc_conditional_port_scan(fsf_req->adapter);
Christof Schmitt24073b42008-06-10 18:20:54 +0200289}
290
Swen Schillig7ba58c92008-10-01 12:42:18 +0200291static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200292{
Swen Schilligecf0c772009-11-24 16:53:58 +0100293 unsigned long flags;
Christof Schmitt24073b42008-06-10 18:20:54 +0200294 struct zfcp_adapter *adapter = req->adapter;
295 struct zfcp_port *port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200296
Swen Schilligecf0c772009-11-24 16:53:58 +0100297 read_lock_irqsave(&adapter->port_list_lock, flags);
298 list_for_each_entry(port, &adapter->port_list, list)
299 if (port->wwpn == wwpn) {
Swen Schilligea4a3a62010-12-02 15:16:16 +0100300 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1");
Christof Schmitt24073b42008-06-10 18:20:54 +0200301 break;
Swen Schilligecf0c772009-11-24 16:53:58 +0100302 }
303 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200304}
305
306static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
307{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100308 struct fsf_status_read_buffer *status_buffer;
309 struct fc_els_flogi *plogi;
Christof Schmitt24073b42008-06-10 18:20:54 +0200310
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100311 status_buffer = (struct fsf_status_read_buffer *) req->data;
312 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
Steffen Maier9d464fc2017-07-28 12:31:02 +0200313 zfcp_fc_incoming_wwpn(req, be64_to_cpu(plogi->fl_wwpn));
Christof Schmitt24073b42008-06-10 18:20:54 +0200314}
315
316static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
317{
318 struct fsf_status_read_buffer *status_buffer =
319 (struct fsf_status_read_buffer *)req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100320 struct fc_els_logo *logo =
321 (struct fc_els_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200322
Steffen Maier9d464fc2017-07-28 12:31:02 +0200323 zfcp_fc_incoming_wwpn(req, be64_to_cpu(logo->fl_n_port_wwn));
Christof Schmitt24073b42008-06-10 18:20:54 +0200324}
325
326/**
327 * zfcp_fc_incoming_els - handle incoming ELS
Steffen Maier8684d612018-11-08 15:44:54 +0100328 * @fsf_req: request which contains incoming ELS
Christof Schmitt24073b42008-06-10 18:20:54 +0200329 */
330void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
331{
332 struct fsf_status_read_buffer *status_buffer =
333 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200334 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200335
Swen Schillig2c55b752010-12-02 15:16:13 +0100336 zfcp_dbf_san_in_els("fciels1", fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100337 if (els_type == ELS_PLOGI)
Christof Schmitt24073b42008-06-10 18:20:54 +0200338 zfcp_fc_incoming_plogi(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100339 else if (els_type == ELS_LOGO)
Christof Schmitt24073b42008-06-10 18:20:54 +0200340 zfcp_fc_incoming_logo(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100341 else if (els_type == ELS_RSCN)
Christof Schmitt24073b42008-06-10 18:20:54 +0200342 zfcp_fc_incoming_rscn(fsf_req);
343}
344
Christof Schmittfcf7e612011-02-22 19:54:42 +0100345static void zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req *fc_req)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200346{
Christof Schmittfcf7e612011-02-22 19:54:42 +0100347 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
348 struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200349
Christof Schmittfcf7e612011-02-22 19:54:42 +0100350 if (ct_els->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200351 return;
Steffen Maier9d464fc2017-07-28 12:31:02 +0200352 if (gid_pn_rsp->ct_hdr.ct_cmd != cpu_to_be16(FC_FS_ACC))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200353 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100354
Christof Schmitt24073b42008-06-10 18:20:54 +0200355 /* looks like a valid d_id */
Christof Schmittfcf7e612011-02-22 19:54:42 +0100356 ct_els->port->d_id = ntoh24(gid_pn_rsp->gid_pn.fp_fid);
Christof Schmitt24073b42008-06-10 18:20:54 +0200357}
358
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100359static void zfcp_fc_complete(void *data)
360{
361 complete(data);
362}
363
Christof Schmittfcf7e612011-02-22 19:54:42 +0100364static void zfcp_fc_ct_ns_init(struct fc_ct_hdr *ct_hdr, u16 cmd, u16 mr_size)
365{
366 ct_hdr->ct_rev = FC_CT_REV;
367 ct_hdr->ct_fs_type = FC_FST_DIR;
368 ct_hdr->ct_fs_subtype = FC_NS_SUBTYPE;
Steffen Maier9d464fc2017-07-28 12:31:02 +0200369 ct_hdr->ct_cmd = cpu_to_be16(cmd);
370 ct_hdr->ct_mr_size = cpu_to_be16(mr_size / 4);
Christof Schmittfcf7e612011-02-22 19:54:42 +0100371}
372
Christof Schmitt799b76d2009-08-18 15:43:20 +0200373static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
Christof Schmittfcf7e612011-02-22 19:54:42 +0100374 struct zfcp_fc_req *fc_req)
Christof Schmitt24073b42008-06-10 18:20:54 +0200375{
Christof Schmitt799b76d2009-08-18 15:43:20 +0200376 struct zfcp_adapter *adapter = port->adapter;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100377 DECLARE_COMPLETION_ONSTACK(completion);
Christof Schmittfcf7e612011-02-22 19:54:42 +0100378 struct zfcp_fc_gid_pn_req *gid_pn_req = &fc_req->u.gid_pn.req;
379 struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200380 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200381
382 /* setup parameters for send generic command */
Christof Schmittfcf7e612011-02-22 19:54:42 +0100383 fc_req->ct_els.port = port;
384 fc_req->ct_els.handler = zfcp_fc_complete;
385 fc_req->ct_els.handler_data = &completion;
386 fc_req->ct_els.req = &fc_req->sg_req;
387 fc_req->ct_els.resp = &fc_req->sg_rsp;
388 sg_init_one(&fc_req->sg_req, gid_pn_req, sizeof(*gid_pn_req));
389 sg_init_one(&fc_req->sg_rsp, gid_pn_rsp, sizeof(*gid_pn_rsp));
Christof Schmitt24073b42008-06-10 18:20:54 +0200390
Christof Schmittfcf7e612011-02-22 19:54:42 +0100391 zfcp_fc_ct_ns_init(&gid_pn_req->ct_hdr,
392 FC_NS_GID_PN, ZFCP_FC_CT_SIZE_PAGE);
Steffen Maier9d464fc2017-07-28 12:31:02 +0200393 gid_pn_req->gid_pn.fn_wwpn = cpu_to_be64(port->wwpn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200394
Christof Schmittfcf7e612011-02-22 19:54:42 +0100395 ret = zfcp_fsf_send_ct(&adapter->gs->ds, &fc_req->ct_els,
Swen Schillig51375ee2010-01-14 17:19:02 +0100396 adapter->pool.gid_pn_req,
397 ZFCP_FC_CTELS_TMO);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100398 if (!ret) {
399 wait_for_completion(&completion);
Christof Schmittfcf7e612011-02-22 19:54:42 +0100400 zfcp_fc_ns_gid_pn_eval(fc_req);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100401 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200402 return ret;
403}
404
405/**
Christof Schmittfcf7e612011-02-22 19:54:42 +0100406 * zfcp_fc_ns_gid_pn - initiate GID_PN nameserver request
Christof Schmitt799b76d2009-08-18 15:43:20 +0200407 * @port: port where GID_PN request is needed
Swen Schillig5ab944f2008-10-01 12:42:17 +0200408 * return: -ENOMEM on error, 0 otherwise
409 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200410static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200411{
412 int ret;
Christof Schmittfcf7e612011-02-22 19:54:42 +0100413 struct zfcp_fc_req *fc_req;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200414 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200415
Christof Schmittfcf7e612011-02-22 19:54:42 +0100416 fc_req = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
417 if (!fc_req)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200418 return -ENOMEM;
419
Christof Schmittfcf7e612011-02-22 19:54:42 +0100420 memset(fc_req, 0, sizeof(*fc_req));
Swen Schillig5ab944f2008-10-01 12:42:17 +0200421
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200422 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200423 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200424 goto out;
425
Christof Schmittfcf7e612011-02-22 19:54:42 +0100426 ret = zfcp_fc_ns_gid_pn_request(port, fc_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200427
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200428 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200429out:
Christof Schmittfcf7e612011-02-22 19:54:42 +0100430 mempool_free(fc_req, adapter->pool.gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200431 return ret;
432}
433
Christof Schmitt799b76d2009-08-18 15:43:20 +0200434void zfcp_fc_port_did_lookup(struct work_struct *work)
435{
436 int ret;
437 struct zfcp_port *port = container_of(work, struct zfcp_port,
438 gid_pn_work);
439
Steffen Maier5c750d52018-05-17 19:14:57 +0200440 set_worker_desc("zgidpn%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200441 ret = zfcp_fc_ns_gid_pn(port);
442 if (ret) {
443 /* could not issue gid_pn for some reason */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100444 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1");
Christof Schmitt799b76d2009-08-18 15:43:20 +0200445 goto out;
446 }
447
448 if (!port->d_id) {
Swen Schilligedaed852010-09-08 14:40:01 +0200449 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200450 goto out;
451 }
452
Swen Schilligea4a3a62010-12-02 15:16:16 +0100453 zfcp_erp_port_reopen(port, 0, "fcgpn_3");
Christof Schmitt799b76d2009-08-18 15:43:20 +0200454out:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100455 put_device(&port->dev);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200456}
457
Christof Schmitt24073b42008-06-10 18:20:54 +0200458/**
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200459 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
460 * @port: The zfcp_port to lookup the d_id for.
461 */
462void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
463{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100464 get_device(&port->dev);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200465 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
Christof Schmitt615f59e2010-02-17 11:18:56 +0100466 put_device(&port->dev);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200467}
468
469/**
Christof Schmitt24073b42008-06-10 18:20:54 +0200470 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
471 * @port: zfcp_port structure
472 * @plogi: plogi payload
473 *
474 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
475 */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100476void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
Christof Schmitt24073b42008-06-10 18:20:54 +0200477{
Steffen Maier9d464fc2017-07-28 12:31:02 +0200478 if (be64_to_cpu(plogi->fl_wwpn) != port->wwpn) {
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100479 port->d_id = 0;
480 dev_warn(&port->adapter->ccw_device->dev,
481 "A port opened with WWPN 0x%016Lx returned data that "
482 "identifies it as WWPN 0x%016Lx\n",
483 (unsigned long long) port->wwpn,
Steffen Maier9d464fc2017-07-28 12:31:02 +0200484 (unsigned long long) be64_to_cpu(plogi->fl_wwpn));
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100485 return;
486 }
487
Steffen Maier9d464fc2017-07-28 12:31:02 +0200488 port->wwnn = be64_to_cpu(plogi->fl_wwnn);
489 port->maxframe_size = be16_to_cpu(plogi->fl_csp.sp_bb_data);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100490
Steffen Maier9d464fc2017-07-28 12:31:02 +0200491 if (plogi->fl_cssp[0].cp_class & cpu_to_be16(FC_CPC_VALID))
Christof Schmitt24073b42008-06-10 18:20:54 +0200492 port->supported_classes |= FC_COS_CLASS1;
Steffen Maier9d464fc2017-07-28 12:31:02 +0200493 if (plogi->fl_cssp[1].cp_class & cpu_to_be16(FC_CPC_VALID))
Christof Schmitt24073b42008-06-10 18:20:54 +0200494 port->supported_classes |= FC_COS_CLASS2;
Steffen Maier9d464fc2017-07-28 12:31:02 +0200495 if (plogi->fl_cssp[2].cp_class & cpu_to_be16(FC_CPC_VALID))
Christof Schmitt24073b42008-06-10 18:20:54 +0200496 port->supported_classes |= FC_COS_CLASS3;
Steffen Maier9d464fc2017-07-28 12:31:02 +0200497 if (plogi->fl_cssp[3].cp_class & cpu_to_be16(FC_CPC_VALID))
Christof Schmitt24073b42008-06-10 18:20:54 +0200498 port->supported_classes |= FC_COS_CLASS4;
499}
500
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100501static void zfcp_fc_adisc_handler(void *data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200502{
Christof Schmitt087897e2011-02-22 19:54:41 +0100503 struct zfcp_fc_req *fc_req = data;
504 struct zfcp_port *port = fc_req->ct_els.port;
505 struct fc_els_adisc *adisc_resp = &fc_req->u.adisc.rsp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200506
Christof Schmitt087897e2011-02-22 19:54:41 +0100507 if (fc_req->ct_els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200508 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200509 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100510 "fcadh_1");
Christof Schmitt24073b42008-06-10 18:20:54 +0200511 goto out;
512 }
513
514 if (!port->wwnn)
Steffen Maier9d464fc2017-07-28 12:31:02 +0200515 port->wwnn = be64_to_cpu(adisc_resp->adisc_wwnn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200516
Steffen Maier9d464fc2017-07-28 12:31:02 +0200517 if ((port->wwpn != be64_to_cpu(adisc_resp->adisc_wwpn)) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100518 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100519 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100520 "fcadh_2");
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100521 goto out;
522 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200523
Steffen Maier8c9db662022-01-18 17:58:03 +0100524 /* re-init to undo drop from zfcp_fc_adisc() */
525 port->d_id = ntoh24(adisc_resp->adisc_port_id);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100526 /* port is good, unblock rport without going through erp */
527 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200528 out:
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200529 atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitt615f59e2010-02-17 11:18:56 +0100530 put_device(&port->dev);
Christof Schmitt087897e2011-02-22 19:54:41 +0100531 kmem_cache_free(zfcp_fc_req_cache, fc_req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200532}
533
534static int zfcp_fc_adisc(struct zfcp_port *port)
535{
Christof Schmitt087897e2011-02-22 19:54:41 +0100536 struct zfcp_fc_req *fc_req;
Christof Schmitt24073b42008-06-10 18:20:54 +0200537 struct zfcp_adapter *adapter = port->adapter;
Christof Schmitt087897e2011-02-22 19:54:41 +0100538 struct Scsi_Host *shost = adapter->scsi_host;
Steffen Maier8c9db662022-01-18 17:58:03 +0100539 u32 d_id;
Christof Schmittee744622009-11-24 16:54:14 +0100540 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200541
Christof Schmitt087897e2011-02-22 19:54:41 +0100542 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC);
543 if (!fc_req)
Christof Schmitt24073b42008-06-10 18:20:54 +0200544 return -ENOMEM;
545
Christof Schmitt087897e2011-02-22 19:54:41 +0100546 fc_req->ct_els.port = port;
547 fc_req->ct_els.req = &fc_req->sg_req;
548 fc_req->ct_els.resp = &fc_req->sg_rsp;
549 sg_init_one(&fc_req->sg_req, &fc_req->u.adisc.req,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100550 sizeof(struct fc_els_adisc));
Christof Schmitt087897e2011-02-22 19:54:41 +0100551 sg_init_one(&fc_req->sg_rsp, &fc_req->u.adisc.rsp,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100552 sizeof(struct fc_els_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200553
Christof Schmitt087897e2011-02-22 19:54:41 +0100554 fc_req->ct_els.handler = zfcp_fc_adisc_handler;
555 fc_req->ct_els.handler_data = fc_req;
Christof Schmitt24073b42008-06-10 18:20:54 +0200556
557 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
558 without FC-AL-2 capability, so we don't set it */
Steffen Maier9d464fc2017-07-28 12:31:02 +0200559 fc_req->u.adisc.req.adisc_wwpn = cpu_to_be64(fc_host_port_name(shost));
560 fc_req->u.adisc.req.adisc_wwnn = cpu_to_be64(fc_host_node_name(shost));
Christof Schmitt087897e2011-02-22 19:54:41 +0100561 fc_req->u.adisc.req.adisc_cmd = ELS_ADISC;
562 hton24(fc_req->u.adisc.req.adisc_port_id, fc_host_port_id(shost));
Christof Schmitt24073b42008-06-10 18:20:54 +0200563
Steffen Maier8c9db662022-01-18 17:58:03 +0100564 d_id = port->d_id; /* remember as destination for send els below */
565 /*
566 * Force fresh GID_PN lookup on next port recovery.
567 * Must happen after request setup and before sending request,
568 * to prevent race with port->d_id re-init in zfcp_fc_adisc_handler().
569 */
570 port->d_id = 0;
571
572 ret = zfcp_fsf_send_els(adapter, d_id, &fc_req->ct_els,
Swen Schillig51375ee2010-01-14 17:19:02 +0100573 ZFCP_FC_CTELS_TMO);
Christof Schmittee744622009-11-24 16:54:14 +0100574 if (ret)
Christof Schmitt087897e2011-02-22 19:54:41 +0100575 kmem_cache_free(zfcp_fc_req_cache, fc_req);
Christof Schmittee744622009-11-24 16:54:14 +0100576
577 return ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200578}
579
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100580void zfcp_fc_link_test_work(struct work_struct *work)
581{
582 struct zfcp_port *port =
583 container_of(work, struct zfcp_port, test_link_work);
584 int retval;
585
Steffen Maier5c750d52018-05-17 19:14:57 +0200586 set_worker_desc("zadisc%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */
Christof Schmitt615f59e2010-02-17 11:18:56 +0100587 get_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100588 port->rport_task = RPORT_DEL;
589 zfcp_scsi_rport_work(&port->rport_work);
590
Christof Schmitt14e242e2009-08-18 15:43:11 +0200591 /* only issue one test command at one time per port */
592 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
593 goto out;
594
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200595 atomic_or(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitt14e242e2009-08-18 15:43:11 +0200596
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100597 retval = zfcp_fc_adisc(port);
598 if (retval == 0)
599 return;
600
601 /* send of ADISC was not possible */
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200602 atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100603 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1");
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100604
Christof Schmitt14e242e2009-08-18 15:43:11 +0200605out:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100606 put_device(&port->dev);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100607}
608
Christof Schmitt24073b42008-06-10 18:20:54 +0200609/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200610 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200611 * @port: port to be tested
612 *
613 * Test status of a link to a remote port using the ELS command ADISC.
614 * If there is a problem with the remote port, error recovery steps
615 * will be triggered.
616 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200617void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200618{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100619 get_device(&port->dev);
Swen Schillig45446832009-08-18 15:43:17 +0200620 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Christof Schmitt615f59e2010-02-17 11:18:56 +0100621 put_device(&port->dev);
Christof Schmitt24073b42008-06-10 18:20:54 +0200622}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200623
Steffen Maier58f3ead2018-11-08 15:44:38 +0100624/**
625 * zfcp_fc_sg_free_table - free memory used by scatterlists
626 * @sg: pointer to scatterlist
627 * @count: number of scatterlist which are to be free'ed
628 * the scatterlist are expected to reference pages always
629 */
630static void zfcp_fc_sg_free_table(struct scatterlist *sg, int count)
631{
632 int i;
633
Ming Lei013be032019-06-18 09:37:51 +0800634 for (i = 0; i < count; i++, sg = sg_next(sg))
Steffen Maier58f3ead2018-11-08 15:44:38 +0100635 if (sg)
636 free_page((unsigned long) sg_virt(sg));
637 else
638 break;
639}
640
641/**
642 * zfcp_fc_sg_setup_table - init scatterlist and allocate, assign buffers
643 * @sg: pointer to struct scatterlist
644 * @count: number of scatterlists which should be assigned with buffers
645 * of size page
646 *
647 * Returns: 0 on success, -ENOMEM otherwise
648 */
649static int zfcp_fc_sg_setup_table(struct scatterlist *sg, int count)
650{
651 void *addr;
652 int i;
653
654 sg_init_table(sg, count);
Ming Lei013be032019-06-18 09:37:51 +0800655 for (i = 0; i < count; i++, sg = sg_next(sg)) {
Steffen Maier58f3ead2018-11-08 15:44:38 +0100656 addr = (void *) get_zeroed_page(GFP_KERNEL);
657 if (!addr) {
658 zfcp_fc_sg_free_table(sg, i);
659 return -ENOMEM;
660 }
661 sg_set_buf(sg, addr, PAGE_SIZE);
662 }
663 return 0;
664}
665
Steffen Maierd39eda52018-05-17 19:14:58 +0200666static struct zfcp_fc_req *zfcp_fc_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200667{
Christof Schmittf9773222011-02-22 19:54:43 +0100668 struct zfcp_fc_req *fc_req;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200669
Christof Schmittf9773222011-02-22 19:54:43 +0100670 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
671 if (!fc_req)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200672 return NULL;
673
Steffen Maier58f3ead2018-11-08 15:44:38 +0100674 if (zfcp_fc_sg_setup_table(&fc_req->sg_rsp, buf_num)) {
Christof Schmittf9773222011-02-22 19:54:43 +0100675 kmem_cache_free(zfcp_fc_req_cache, fc_req);
676 return NULL;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200677 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200678
Christof Schmittf9773222011-02-22 19:54:43 +0100679 sg_init_one(&fc_req->sg_req, &fc_req->u.gpn_ft.req,
680 sizeof(struct zfcp_fc_gpn_ft_req));
681
682 return fc_req;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200683}
684
Christof Schmittf9773222011-02-22 19:54:43 +0100685static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req,
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200686 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200687{
Christof Schmittf9773222011-02-22 19:54:43 +0100688 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
689 struct zfcp_fc_gpn_ft_req *req = &fc_req->u.gpn_ft.req;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100690 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200691 int ret;
692
Christof Schmittf9773222011-02-22 19:54:43 +0100693 zfcp_fc_ct_ns_init(&req->ct_hdr, FC_NS_GPN_FT, max_bytes);
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100694 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200695
Christof Schmittf9773222011-02-22 19:54:43 +0100696 ct_els->handler = zfcp_fc_complete;
697 ct_els->handler_data = &completion;
698 ct_els->req = &fc_req->sg_req;
699 ct_els->resp = &fc_req->sg_rsp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200700
Christof Schmittf9773222011-02-22 19:54:43 +0100701 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
Swen Schillig51375ee2010-01-14 17:19:02 +0100702 ZFCP_FC_CTELS_TMO);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200703 if (!ret)
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100704 wait_for_completion(&completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200705 return ret;
706}
707
Swen Schilligf3450c72009-11-24 16:53:59 +0100708static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200709{
Martin Petermann6ab35c02009-04-17 15:08:13 +0200710 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
711 return;
712
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200713 atomic_andnot(ZFCP_STATUS_COMMON_NOESC, &port->status);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200714
Christof Schmitt04062892008-10-01 12:42:20 +0200715 if ((port->supported_classes != 0) ||
Swen Schilligf3450c72009-11-24 16:53:59 +0100716 !list_empty(&port->unit_list))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200717 return;
Swen Schilligf3450c72009-11-24 16:53:59 +0100718
Swen Schilligf3450c72009-11-24 16:53:59 +0100719 list_move_tail(&port->list, lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200720}
721
Christof Schmittf9773222011-02-22 19:54:43 +0100722static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_req *fc_req,
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100723 struct zfcp_adapter *adapter, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200724{
Christof Schmittf9773222011-02-22 19:54:43 +0100725 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
726 struct scatterlist *sg = &fc_req->sg_rsp;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100727 struct fc_ct_hdr *hdr = sg_virt(sg);
728 struct fc_gpn_ft_resp *acc = sg_virt(sg);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200729 struct zfcp_port *port, *tmp;
Swen Schilligecf0c772009-11-24 16:53:58 +0100730 unsigned long flags;
Swen Schilligf3450c72009-11-24 16:53:59 +0100731 LIST_HEAD(remove_lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200732 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200733 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200734
Christof Schmittf9773222011-02-22 19:54:43 +0100735 if (ct_els->status)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200736 return -EIO;
737
Steffen Maier9d464fc2017-07-28 12:31:02 +0200738 if (hdr->ct_cmd != cpu_to_be16(FC_FS_ACC)) {
Steffen Maierab8ab4b2017-07-28 12:30:59 +0200739 if (hdr->ct_reason == FC_FS_RJT_UNABL)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200740 return -EAGAIN; /* might be a temporary condition */
741 return -EIO;
742 }
743
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100744 if (hdr->ct_mr_size) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100745 dev_warn(&adapter->ccw_device->dev,
746 "The name server reported %d words residual data\n",
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100747 hdr->ct_mr_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200748 return -E2BIG;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100749 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200750
Swen Schilligcc8c2822008-06-10 18:21:00 +0200751 /* first entry is the header */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100752 for (x = 1; x < max_entries && !last; x++) {
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100753 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200754 acc++;
755 else
756 acc = sg_virt(++sg);
757
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100758 last = acc->fp_flags & FC_NS_FID_LAST;
759 d_id = ntoh24(acc->fp_fid);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200760
Swen Schillig5ab944f2008-10-01 12:42:17 +0200761 /* don't attach ports with a well known address */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100762 if (d_id >= FC_FID_WELL_KNOWN_BASE)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200763 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200764 /* skip the adapter's port and known remote ports */
Steffen Maier9d464fc2017-07-28 12:31:02 +0200765 if (be64_to_cpu(acc->fp_wwpn) ==
766 fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200767 continue;
768
Steffen Maier9d464fc2017-07-28 12:31:02 +0200769 port = zfcp_port_enqueue(adapter, be64_to_cpu(acc->fp_wwpn),
Swen Schilligcc8c2822008-06-10 18:21:00 +0200770 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100771 if (!IS_ERR(port))
Swen Schilligea4a3a62010-12-02 15:16:16 +0100772 zfcp_erp_port_reopen(port, 0, "fcegpf1");
Swen Schilligecf0c772009-11-24 16:53:58 +0100773 else if (PTR_ERR(port) != -EEXIST)
774 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200775 }
776
777 zfcp_erp_wait(adapter);
Swen Schilligecf0c772009-11-24 16:53:58 +0100778 write_lock_irqsave(&adapter->port_list_lock, flags);
779 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100780 zfcp_fc_validate_port(port, &remove_lh);
Swen Schilligecf0c772009-11-24 16:53:58 +0100781 write_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schilligf3450c72009-11-24 16:53:59 +0100782
783 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
Swen Schilligea4a3a62010-12-02 15:16:16 +0100784 zfcp_erp_port_shutdown(port, 0, "fcegpf2");
Sebastian Ott83d4e1c2013-04-26 16:13:48 +0200785 device_unregister(&port->dev);
Swen Schilligf3450c72009-11-24 16:53:59 +0100786 }
787
Swen Schilligcc8c2822008-06-10 18:21:00 +0200788 return ret;
789}
790
791/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200792 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schillig9eae07e2009-11-24 16:54:06 +0100793 * @work: reference to scheduled work
Swen Schilligcc8c2822008-06-10 18:21:00 +0200794 */
Swen Schillig9eae07e2009-11-24 16:54:06 +0100795void zfcp_fc_scan_ports(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200796{
Martin Peschke18f87a62014-11-13 14:59:48 +0100797 struct delayed_work *dw = to_delayed_work(work);
798 struct zfcp_adapter *adapter = container_of(dw, struct zfcp_adapter,
Swen Schillig9eae07e2009-11-24 16:54:06 +0100799 scan_work);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200800 int ret, i;
Christof Schmittf9773222011-02-22 19:54:43 +0100801 struct zfcp_fc_req *fc_req;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100802 int chain, max_entries, buf_num, max_bytes;
803
Martin Peschke18f87a62014-11-13 14:59:48 +0100804 zfcp_fc_port_scan_time(adapter);
805
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100806 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100807 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
808 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
809 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200810
Swen Schillig306b6ed2009-04-17 15:08:02 +0200811 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
812 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schillig9eae07e2009-11-24 16:54:06 +0100813 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200814
Swen Schillig9eae07e2009-11-24 16:54:06 +0100815 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
816 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200817
Steffen Maierd39eda52018-05-17 19:14:58 +0200818 fc_req = zfcp_fc_alloc_sg_env(buf_num);
Christof Schmittf9773222011-02-22 19:54:43 +0100819 if (!fc_req)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200820 goto out;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200821
822 for (i = 0; i < 3; i++) {
Christof Schmittf9773222011-02-22 19:54:43 +0100823 ret = zfcp_fc_send_gpn_ft(fc_req, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200824 if (!ret) {
Christof Schmittf9773222011-02-22 19:54:43 +0100825 ret = zfcp_fc_eval_gpn_ft(fc_req, adapter, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200826 if (ret == -EAGAIN)
827 ssleep(1);
828 else
829 break;
830 }
831 }
Steffen Maier58f3ead2018-11-08 15:44:38 +0100832 zfcp_fc_sg_free_table(&fc_req->sg_rsp, buf_num);
Christof Schmittf9773222011-02-22 19:54:43 +0100833 kmem_cache_free(zfcp_fc_req_cache, fc_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200834out:
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200835 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200836}
837
Christof Schmitt038d9442011-02-22 19:54:48 +0100838static int zfcp_fc_gspn(struct zfcp_adapter *adapter,
839 struct zfcp_fc_req *fc_req)
840{
841 DECLARE_COMPLETION_ONSTACK(completion);
842 char devno[] = "DEVNO:";
843 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
844 struct zfcp_fc_gspn_req *gspn_req = &fc_req->u.gspn.req;
845 struct zfcp_fc_gspn_rsp *gspn_rsp = &fc_req->u.gspn.rsp;
846 int ret;
847
848 zfcp_fc_ct_ns_init(&gspn_req->ct_hdr, FC_NS_GSPN_ID,
849 FC_SYMBOLIC_NAME_SIZE);
850 hton24(gspn_req->gspn.fp_fid, fc_host_port_id(adapter->scsi_host));
851
852 sg_init_one(&fc_req->sg_req, gspn_req, sizeof(*gspn_req));
853 sg_init_one(&fc_req->sg_rsp, gspn_rsp, sizeof(*gspn_rsp));
854
855 ct_els->handler = zfcp_fc_complete;
856 ct_els->handler_data = &completion;
857 ct_els->req = &fc_req->sg_req;
858 ct_els->resp = &fc_req->sg_rsp;
859
860 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
861 ZFCP_FC_CTELS_TMO);
862 if (ret)
863 return ret;
864
865 wait_for_completion(&completion);
866 if (ct_els->status)
867 return ct_els->status;
868
869 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_NPIV &&
870 !(strstr(gspn_rsp->gspn.fp_name, devno)))
871 snprintf(fc_host_symbolic_name(adapter->scsi_host),
872 FC_SYMBOLIC_NAME_SIZE, "%s%s %s NAME: %s",
873 gspn_rsp->gspn.fp_name, devno,
874 dev_name(&adapter->ccw_device->dev),
875 init_utsname()->nodename);
876 else
877 strlcpy(fc_host_symbolic_name(adapter->scsi_host),
878 gspn_rsp->gspn.fp_name, FC_SYMBOLIC_NAME_SIZE);
879
880 return 0;
881}
882
883static void zfcp_fc_rspn(struct zfcp_adapter *adapter,
884 struct zfcp_fc_req *fc_req)
885{
886 DECLARE_COMPLETION_ONSTACK(completion);
887 struct Scsi_Host *shost = adapter->scsi_host;
888 struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
889 struct zfcp_fc_rspn_req *rspn_req = &fc_req->u.rspn.req;
890 struct fc_ct_hdr *rspn_rsp = &fc_req->u.rspn.rsp;
891 int ret, len;
892
893 zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID,
894 FC_SYMBOLIC_NAME_SIZE);
895 hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost));
896 len = strlcpy(rspn_req->rspn.fr_name, fc_host_symbolic_name(shost),
897 FC_SYMBOLIC_NAME_SIZE);
898 rspn_req->rspn.fr_name_len = len;
899
900 sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req));
901 sg_init_one(&fc_req->sg_rsp, rspn_rsp, sizeof(*rspn_rsp));
902
903 ct_els->handler = zfcp_fc_complete;
904 ct_els->handler_data = &completion;
905 ct_els->req = &fc_req->sg_req;
906 ct_els->resp = &fc_req->sg_rsp;
907
908 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
909 ZFCP_FC_CTELS_TMO);
910 if (!ret)
911 wait_for_completion(&completion);
912}
913
914/**
915 * zfcp_fc_sym_name_update - Retrieve and update the symbolic port name
916 * @work: ns_up_work of the adapter where to update the symbolic port name
917 *
918 * Retrieve the current symbolic port name that may have been set by
919 * the hardware using the GSPN request and update the fc_host
920 * symbolic_name sysfs attribute. When running in NPIV mode (and hence
921 * the port name is unique for this system), update the symbolic port
922 * name to add Linux specific information and update the FC nameserver
923 * using the RSPN request.
924 */
925void zfcp_fc_sym_name_update(struct work_struct *work)
926{
927 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
928 ns_up_work);
929 int ret;
930 struct zfcp_fc_req *fc_req;
931
932 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
933 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
934 return;
935
936 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
937 if (!fc_req)
938 return;
939
940 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
941 if (ret)
942 goto out_free;
943
944 ret = zfcp_fc_gspn(adapter, fc_req);
945 if (ret || fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
946 goto out_ds_put;
947
948 memset(fc_req, 0, sizeof(*fc_req));
949 zfcp_fc_rspn(adapter, fc_req);
950
951out_ds_put:
952 zfcp_fc_wka_port_put(&adapter->gs->ds);
953out_free:
954 kmem_cache_free(zfcp_fc_req_cache, fc_req);
955}
956
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100957static void zfcp_fc_ct_els_job_handler(void *data)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200958{
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100959 struct bsg_job *job = data;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100960 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
Swen Schillig7dec9cf2010-01-26 17:49:19 +0100961 struct fc_bsg_reply *jr = job->reply;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200962
Swen Schillig7dec9cf2010-01-26 17:49:19 +0100963 jr->reply_payload_rcv_len = job->reply_payload.payload_len;
964 jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
965 jr->result = zfcp_ct_els->status ? -EIO : 0;
Johannes Thumshirn06548162016-11-17 10:31:22 +0100966 bsg_job_done(job, jr->result, jr->reply_payload_rcv_len);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200967}
968
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100969static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct bsg_job *job)
Christof Schmittf09d5452010-01-13 17:52:36 +0100970{
971 u32 preamble_word1;
972 u8 gs_type;
973 struct zfcp_adapter *adapter;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100974 struct fc_bsg_request *bsg_request = job->request;
Johannes Thumshirn1d69b122016-11-17 10:31:15 +0100975 struct fc_rport *rport = fc_bsg_to_rport(job);
Johannes Thumshirncd21c602016-11-17 10:31:14 +0100976 struct Scsi_Host *shost;
Christof Schmittf09d5452010-01-13 17:52:36 +0100977
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100978 preamble_word1 = bsg_request->rqst_data.r_ct.preamble_word1;
Christof Schmittf09d5452010-01-13 17:52:36 +0100979 gs_type = (preamble_word1 & 0xff000000) >> 24;
980
Johannes Thumshirn1d69b122016-11-17 10:31:15 +0100981 shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
Johannes Thumshirncd21c602016-11-17 10:31:14 +0100982 adapter = (struct zfcp_adapter *) shost->hostdata[0];
Christof Schmittf09d5452010-01-13 17:52:36 +0100983
984 switch (gs_type) {
985 case FC_FST_ALIAS:
986 return &adapter->gs->as;
987 case FC_FST_MGMT:
988 return &adapter->gs->ms;
989 case FC_FST_TIME:
990 return &adapter->gs->ts;
991 break;
992 case FC_FST_DIR:
993 return &adapter->gs->ds;
994 break;
995 default:
996 return NULL;
997 }
998}
999
1000static void zfcp_fc_ct_job_handler(void *data)
1001{
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001002 struct bsg_job *job = data;
Christof Schmittf09d5452010-01-13 17:52:36 +01001003 struct zfcp_fc_wka_port *wka_port;
1004
1005 wka_port = zfcp_fc_job_wka_port(job);
1006 zfcp_fc_wka_port_put(wka_port);
1007
1008 zfcp_fc_ct_els_job_handler(data);
1009}
1010
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001011static int zfcp_fc_exec_els_job(struct bsg_job *job,
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001012 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +02001013{
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001014 struct zfcp_fsf_ct_els *els = job->dd_data;
Johannes Thumshirn1d69b122016-11-17 10:31:15 +01001015 struct fc_rport *rport = fc_bsg_to_rport(job);
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001016 struct fc_bsg_request *bsg_request = job->request;
Sven Schuetz9d544f22009-04-06 18:31:47 +02001017 struct zfcp_port *port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001018 u32 d_id;
Sven Schuetz9d544f22009-04-06 18:31:47 +02001019
Sven Schuetz9d544f22009-04-06 18:31:47 +02001020 if (rport) {
Swen Schilligea945ff2009-08-18 15:43:24 +02001021 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001022 if (!port)
Sven Schuetz9d544f22009-04-06 18:31:47 +02001023 return -EINVAL;
Swen Schilligecf0c772009-11-24 16:53:58 +01001024
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001025 d_id = port->d_id;
Christof Schmitt615f59e2010-02-17 11:18:56 +01001026 put_device(&port->dev);
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001027 } else
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001028 d_id = ntoh24(bsg_request->rqst_data.h_els.port_id);
Sven Schuetz9d544f22009-04-06 18:31:47 +02001029
Christof Schmittf09d5452010-01-13 17:52:36 +01001030 els->handler = zfcp_fc_ct_els_job_handler;
Christoph Hellwig31156ec2018-03-13 17:28:39 +01001031 return zfcp_fsf_send_els(adapter, d_id, els, job->timeout / HZ);
Sven Schuetz9d544f22009-04-06 18:31:47 +02001032}
1033
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001034static int zfcp_fc_exec_ct_job(struct bsg_job *job,
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001035 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +02001036{
1037 int ret;
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001038 struct zfcp_fsf_ct_els *ct = job->dd_data;
1039 struct zfcp_fc_wka_port *wka_port;
Sven Schuetz9d544f22009-04-06 18:31:47 +02001040
Christof Schmittf09d5452010-01-13 17:52:36 +01001041 wka_port = zfcp_fc_job_wka_port(job);
1042 if (!wka_port)
1043 return -EINVAL;
Sven Schuetz9d544f22009-04-06 18:31:47 +02001044
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001045 ret = zfcp_fc_wka_port_get(wka_port);
1046 if (ret)
Sven Schuetz9d544f22009-04-06 18:31:47 +02001047 return ret;
Sven Schuetz9d544f22009-04-06 18:31:47 +02001048
Christof Schmittf09d5452010-01-13 17:52:36 +01001049 ct->handler = zfcp_fc_ct_job_handler;
Christoph Hellwig31156ec2018-03-13 17:28:39 +01001050 ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->timeout / HZ);
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001051 if (ret)
1052 zfcp_fc_wka_port_put(wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +02001053
Sven Schuetz9d544f22009-04-06 18:31:47 +02001054 return ret;
1055}
Swen Schilligd5a282a2009-08-18 15:43:22 +02001056
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001057int zfcp_fc_exec_bsg_job(struct bsg_job *job)
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001058{
1059 struct Scsi_Host *shost;
1060 struct zfcp_adapter *adapter;
1061 struct zfcp_fsf_ct_els *ct_els = job->dd_data;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001062 struct fc_bsg_request *bsg_request = job->request;
Johannes Thumshirn1d69b122016-11-17 10:31:15 +01001063 struct fc_rport *rport = fc_bsg_to_rport(job);
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001064
Johannes Thumshirn1d69b122016-11-17 10:31:15 +01001065 shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001066 adapter = (struct zfcp_adapter *)shost->hostdata[0];
1067
1068 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
1069 return -EINVAL;
1070
1071 ct_els->req = job->request_payload.sg_list;
1072 ct_els->resp = job->reply_payload.sg_list;
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001073 ct_els->handler_data = job;
1074
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001075 switch (bsg_request->msgcode) {
Christof Schmitt7c7dc192009-11-24 16:54:13 +01001076 case FC_BSG_RPT_ELS:
1077 case FC_BSG_HST_ELS_NOLOGIN:
1078 return zfcp_fc_exec_els_job(job, adapter);
1079 case FC_BSG_RPT_CT:
1080 case FC_BSG_HST_CT:
1081 return zfcp_fc_exec_ct_job(job, adapter);
1082 default:
1083 return -EINVAL;
1084 }
1085}
1086
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001087int zfcp_fc_timeout_bsg_job(struct bsg_job *job)
Swen Schillig491ca442010-01-14 17:19:01 +01001088{
1089 /* hardware tracks timeout, reset bsg timeout to not interfere */
1090 return -EAGAIN;
1091}
1092
Swen Schilligd5a282a2009-08-18 15:43:22 +02001093int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
1094{
Christof Schmittbd0072e2009-11-24 16:54:11 +01001095 struct zfcp_fc_wka_ports *wka_ports;
Swen Schilligd5a282a2009-08-18 15:43:22 +02001096
Christof Schmittbd0072e2009-11-24 16:54:11 +01001097 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
Swen Schilligd5a282a2009-08-18 15:43:22 +02001098 if (!wka_ports)
1099 return -ENOMEM;
1100
1101 adapter->gs = wka_ports;
1102 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
1103 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
1104 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
1105 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
Swen Schilligd5a282a2009-08-18 15:43:22 +02001106
1107 return 0;
1108}
1109
1110void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
1111{
1112 kfree(adapter->gs);
1113 adapter->gs = NULL;
1114}
1115