1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
224073b47SChristof Schmitt /*
324073b47SChristof Schmitt * zfcp device driver
424073b47SChristof Schmitt *
524073b47SChristof Schmitt * Fibre Channel related functions for the zfcp device driver.
624073b47SChristof Schmitt *
7ab8ab4beSSteffen Maier * Copyright IBM Corp. 2008, 2017
824073b47SChristof Schmitt */
924073b47SChristof Schmitt
10ecf39d42SChristof Schmitt #define KMSG_COMPONENT "zfcp"
11ecf39d42SChristof Schmitt #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12ecf39d42SChristof Schmitt
139d05ce2cSChristof Schmitt #include <linux/types.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15038d9446SChristof Schmitt #include <linux/utsname.h>
1618f87a67SMartin Peschke #include <linux/random.h>
1775cc8cfcSJohannes Thumshirn #include <linux/bsg-lib.h>
189d05ce2cSChristof Schmitt #include <scsi/fc/fc_els.h>
199d05ce2cSChristof Schmitt #include <scsi/libfc.h>
2024073b47SChristof Schmitt #include "zfcp_ext.h"
219d05ce2cSChristof Schmitt #include "zfcp_fc.h"
2224073b47SChristof Schmitt
23087897e3SChristof Schmitt struct kmem_cache *zfcp_fc_req_cache;
24087897e3SChristof Schmitt
259d05ce2cSChristof Schmitt static u32 zfcp_fc_rscn_range_mask[] = {
269d05ce2cSChristof Schmitt [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
279d05ce2cSChristof Schmitt [ELS_ADDR_FMT_AREA] = 0xFFFF00,
289d05ce2cSChristof Schmitt [ELS_ADDR_FMT_DOM] = 0xFF0000,
299d05ce2cSChristof Schmitt [ELS_ADDR_FMT_FAB] = 0x000000,
30e0d7fcb5SChristof Schmitt };
31e0d7fcb5SChristof Schmitt
3243f60cbdSSteffen Maier static bool no_auto_port_rescan;
33b096ef86SMartin Peschke module_param(no_auto_port_rescan, bool, 0600);
3443f60cbdSSteffen Maier MODULE_PARM_DESC(no_auto_port_rescan,
3543f60cbdSSteffen Maier "no automatic port_rescan (default off)");
3643f60cbdSSteffen Maier
3718f87a67SMartin Peschke static unsigned int port_scan_backoff = 500;
3818f87a67SMartin Peschke module_param(port_scan_backoff, uint, 0600);
3918f87a67SMartin Peschke MODULE_PARM_DESC(port_scan_backoff,
4018f87a67SMartin Peschke "upper limit of port scan random backoff in msecs (default 500)");
4118f87a67SMartin Peschke
4218f87a67SMartin Peschke static unsigned int port_scan_ratelimit = 60000;
4318f87a67SMartin Peschke module_param(port_scan_ratelimit, uint, 0600);
4418f87a67SMartin Peschke MODULE_PARM_DESC(port_scan_ratelimit,
4518f87a67SMartin Peschke "minimum interval between port scans in msecs (default 60000)");
4618f87a67SMartin Peschke
zfcp_fc_port_scan_backoff(void)4718f87a67SMartin Peschke unsigned int zfcp_fc_port_scan_backoff(void)
4818f87a67SMartin Peschke {
4918f87a67SMartin Peschke if (!port_scan_backoff)
5018f87a67SMartin Peschke return 0;
518032bf12SJason A. Donenfeld return get_random_u32_below(port_scan_backoff);
5218f87a67SMartin Peschke }
5318f87a67SMartin Peschke
zfcp_fc_port_scan_time(struct zfcp_adapter * adapter)5418f87a67SMartin Peschke static void zfcp_fc_port_scan_time(struct zfcp_adapter *adapter)
5518f87a67SMartin Peschke {
5618f87a67SMartin Peschke unsigned long interval = msecs_to_jiffies(port_scan_ratelimit);
5718f87a67SMartin Peschke unsigned long backoff = msecs_to_jiffies(zfcp_fc_port_scan_backoff());
5818f87a67SMartin Peschke
5918f87a67SMartin Peschke adapter->next_port_scan = jiffies + interval + backoff;
6018f87a67SMartin Peschke }
6118f87a67SMartin Peschke
zfcp_fc_port_scan(struct zfcp_adapter * adapter)6218f87a67SMartin Peschke static void zfcp_fc_port_scan(struct zfcp_adapter *adapter)
6318f87a67SMartin Peschke {
6418f87a67SMartin Peschke unsigned long now = jiffies;
6518f87a67SMartin Peschke unsigned long next = adapter->next_port_scan;
6618f87a67SMartin Peschke unsigned long delay = 0, max;
6718f87a67SMartin Peschke
6818f87a67SMartin Peschke /* delay only needed within waiting period */
6918f87a67SMartin Peschke if (time_before(now, next)) {
7018f87a67SMartin Peschke delay = next - now;
7118f87a67SMartin Peschke /* paranoia: never ever delay scans longer than specified */
7218f87a67SMartin Peschke max = msecs_to_jiffies(port_scan_ratelimit + port_scan_backoff);
7318f87a67SMartin Peschke delay = min(delay, max);
7418f87a67SMartin Peschke }
7518f87a67SMartin Peschke
7618f87a67SMartin Peschke queue_delayed_work(adapter->work_queue, &adapter->scan_work, delay);
7718f87a67SMartin Peschke }
7818f87a67SMartin Peschke
zfcp_fc_conditional_port_scan(struct zfcp_adapter * adapter)7943f60cbdSSteffen Maier void zfcp_fc_conditional_port_scan(struct zfcp_adapter *adapter)
8043f60cbdSSteffen Maier {
8143f60cbdSSteffen Maier if (no_auto_port_rescan)
8243f60cbdSSteffen Maier return;
8343f60cbdSSteffen Maier
8418f87a67SMartin Peschke zfcp_fc_port_scan(adapter);
8543f60cbdSSteffen Maier }
8643f60cbdSSteffen Maier
zfcp_fc_inverse_conditional_port_scan(struct zfcp_adapter * adapter)8743f60cbdSSteffen Maier void zfcp_fc_inverse_conditional_port_scan(struct zfcp_adapter *adapter)
8843f60cbdSSteffen Maier {
8943f60cbdSSteffen Maier if (!no_auto_port_rescan)
9043f60cbdSSteffen Maier return;
9143f60cbdSSteffen Maier
9218f87a67SMartin Peschke zfcp_fc_port_scan(adapter);
9343f60cbdSSteffen Maier }
9443f60cbdSSteffen Maier
952d1e547fSSven Schuetz /**
962d1e547fSSven Schuetz * zfcp_fc_post_event - post event to userspace via fc_transport
972d1e547fSSven Schuetz * @work: work struct with enqueued events
982d1e547fSSven Schuetz */
zfcp_fc_post_event(struct work_struct * work)992d1e547fSSven Schuetz void zfcp_fc_post_event(struct work_struct *work)
1002d1e547fSSven Schuetz {
1012d1e547fSSven Schuetz struct zfcp_fc_event *event = NULL, *tmp = NULL;
1022d1e547fSSven Schuetz LIST_HEAD(tmp_lh);
1032d1e547fSSven Schuetz struct zfcp_fc_events *events = container_of(work,
1042d1e547fSSven Schuetz struct zfcp_fc_events, work);
1052d1e547fSSven Schuetz struct zfcp_adapter *adapter = container_of(events, struct zfcp_adapter,
1062d1e547fSSven Schuetz events);
1072d1e547fSSven Schuetz
1082d1e547fSSven Schuetz spin_lock_bh(&events->list_lock);
1092d1e547fSSven Schuetz list_splice_init(&events->list, &tmp_lh);
1102d1e547fSSven Schuetz spin_unlock_bh(&events->list_lock);
1112d1e547fSSven Schuetz
1122d1e547fSSven Schuetz list_for_each_entry_safe(event, tmp, &tmp_lh, list) {
1132d1e547fSSven Schuetz fc_host_post_event(adapter->scsi_host, fc_get_event_number(),
1142d1e547fSSven Schuetz event->code, event->data);
1152d1e547fSSven Schuetz list_del(&event->list);
1162d1e547fSSven Schuetz kfree(event);
1172d1e547fSSven Schuetz }
1182d1e547fSSven Schuetz }
1192d1e547fSSven Schuetz
1202d1e547fSSven Schuetz /**
1212d1e547fSSven Schuetz * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context
1222d1e547fSSven Schuetz * @adapter: The adapter where to enqueue the event
1232d1e547fSSven Schuetz * @event_code: The event code (as defined in fc_host_event_code in
1242d1e547fSSven Schuetz * scsi_transport_fc.h)
1252d1e547fSSven Schuetz * @event_data: The event data (e.g. n_port page in case of els)
1262d1e547fSSven Schuetz */
zfcp_fc_enqueue_event(struct zfcp_adapter * adapter,enum fc_host_event_code event_code,u32 event_data)1272d1e547fSSven Schuetz void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
1282d1e547fSSven Schuetz enum fc_host_event_code event_code, u32 event_data)
1292d1e547fSSven Schuetz {
1302d1e547fSSven Schuetz struct zfcp_fc_event *event;
1312d1e547fSSven Schuetz
1322d1e547fSSven Schuetz event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
1332d1e547fSSven Schuetz if (!event)
1342d1e547fSSven Schuetz return;
1352d1e547fSSven Schuetz
1362d1e547fSSven Schuetz event->code = event_code;
1372d1e547fSSven Schuetz event->data = event_data;
1382d1e547fSSven Schuetz
1392d1e547fSSven Schuetz spin_lock(&adapter->events.list_lock);
1402d1e547fSSven Schuetz list_add_tail(&event->list, &adapter->events.list);
1412d1e547fSSven Schuetz spin_unlock(&adapter->events.list_lock);
1422d1e547fSSven Schuetz
1432d1e547fSSven Schuetz queue_work(adapter->work_queue, &adapter->events.work);
1442d1e547fSSven Schuetz }
1452d1e547fSSven Schuetz
zfcp_fc_wka_port_get(struct zfcp_fc_wka_port * wka_port)146bd0072ecSChristof Schmitt static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
1475ab944f9SSwen Schillig {
1484da8c5f7SSteffen Maier int ret = -EIO;
1494da8c5f7SSteffen Maier
1505ab944f9SSwen Schillig if (mutex_lock_interruptible(&wka_port->mutex))
1515ab944f9SSwen Schillig return -ERESTARTSYS;
1525ab944f9SSwen Schillig
153bd0072ecSChristof Schmitt if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
154bd0072ecSChristof Schmitt wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
155bd0072ecSChristof Schmitt wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
1564da8c5f7SSteffen Maier if (zfcp_fsf_open_wka_port(wka_port)) {
1574da8c5f7SSteffen Maier /* could not even send request, nothing to wait for */
158bd0072ecSChristof Schmitt wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1594da8c5f7SSteffen Maier goto out;
1604da8c5f7SSteffen Maier }
1615ab944f9SSwen Schillig }
1625ab944f9SSwen Schillig
1634da8c5f7SSteffen Maier wait_event(wka_port->opened,
164bd0072ecSChristof Schmitt wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
165bd0072ecSChristof Schmitt wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
1665ab944f9SSwen Schillig
167bd0072ecSChristof Schmitt if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
1685ab944f9SSwen Schillig atomic_inc(&wka_port->refcount);
1694da8c5f7SSteffen Maier ret = 0;
1704da8c5f7SSteffen Maier goto out;
1715ab944f9SSwen Schillig }
1724da8c5f7SSteffen Maier out:
1734da8c5f7SSteffen Maier mutex_unlock(&wka_port->mutex);
1744da8c5f7SSteffen Maier return ret;
1755ab944f9SSwen Schillig }
1765ab944f9SSwen Schillig
zfcp_fc_wka_port_offline(struct work_struct * work)1776f53a2d2SSwen Schillig static void zfcp_fc_wka_port_offline(struct work_struct *work)
1785ab944f9SSwen Schillig {
179bf6aede7SJean Delvare struct delayed_work *dw = to_delayed_work(work);
180bd0072ecSChristof Schmitt struct zfcp_fc_wka_port *wka_port =
181bd0072ecSChristof Schmitt container_of(dw, struct zfcp_fc_wka_port, work);
1825ab944f9SSwen Schillig
1835ab944f9SSwen Schillig mutex_lock(&wka_port->mutex);
1845ab944f9SSwen Schillig if ((atomic_read(&wka_port->refcount) != 0) ||
185bd0072ecSChristof Schmitt (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
1865ab944f9SSwen Schillig goto out;
1875ab944f9SSwen Schillig
188bd0072ecSChristof Schmitt wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
1895ab944f9SSwen Schillig if (zfcp_fsf_close_wka_port(wka_port)) {
1904da8c5f7SSteffen Maier /* could not even send request, nothing to wait for */
191bd0072ecSChristof Schmitt wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1924da8c5f7SSteffen Maier goto out;
1935ab944f9SSwen Schillig }
1944da8c5f7SSteffen Maier wait_event(wka_port->closed,
1954da8c5f7SSteffen Maier wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
1965ab944f9SSwen Schillig out:
1975ab944f9SSwen Schillig mutex_unlock(&wka_port->mutex);
1985ab944f9SSwen Schillig }
1995ab944f9SSwen Schillig
zfcp_fc_wka_port_put(struct zfcp_fc_wka_port * wka_port)200bd0072ecSChristof Schmitt static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
2015ab944f9SSwen Schillig {
2025ab944f9SSwen Schillig if (atomic_dec_return(&wka_port->refcount) != 0)
2035ab944f9SSwen Schillig return;
20419af5cdbSMartin Olsson /* wait 10 milliseconds, other reqs might pop in */
2054da8c5f7SSteffen Maier queue_delayed_work(wka_port->adapter->work_queue, &wka_port->work,
2064da8c5f7SSteffen Maier msecs_to_jiffies(10));
2075ab944f9SSwen Schillig }
2085ab944f9SSwen Schillig
zfcp_fc_wka_port_init(struct zfcp_fc_wka_port * wka_port,u32 d_id,struct zfcp_adapter * adapter)209bd0072ecSChristof Schmitt static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
2109d544f2bSSven Schuetz struct zfcp_adapter *adapter)
2115ab944f9SSwen Schillig {
2124da8c5f7SSteffen Maier init_waitqueue_head(&wka_port->opened);
2134da8c5f7SSteffen Maier init_waitqueue_head(&wka_port->closed);
2145ab944f9SSwen Schillig
2155ab944f9SSwen Schillig wka_port->adapter = adapter;
2169d544f2bSSven Schuetz wka_port->d_id = d_id;
2175ab944f9SSwen Schillig
218bd0072ecSChristof Schmitt wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
2195ab944f9SSwen Schillig atomic_set(&wka_port->refcount, 0);
2205ab944f9SSwen Schillig mutex_init(&wka_port->mutex);
2216f53a2d2SSwen Schillig INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
2225ab944f9SSwen Schillig }
2235ab944f9SSwen Schillig
zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port * wka)224bd0072ecSChristof Schmitt static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
225828bc121SSwen Schillig {
226828bc121SSwen Schillig cancel_delayed_work_sync(&wka->work);
227828bc121SSwen Schillig mutex_lock(&wka->mutex);
228bd0072ecSChristof Schmitt wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
229828bc121SSwen Schillig mutex_unlock(&wka->mutex);
230828bc121SSwen Schillig }
231828bc121SSwen Schillig
zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports * gs)232bd0072ecSChristof Schmitt void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
23355c770faSChristof Schmitt {
234f3450c7bSSwen Schillig if (!gs)
235f3450c7bSSwen Schillig return;
23655c770faSChristof Schmitt zfcp_fc_wka_port_force_offline(&gs->ms);
23755c770faSChristof Schmitt zfcp_fc_wka_port_force_offline(&gs->ts);
23855c770faSChristof Schmitt zfcp_fc_wka_port_force_offline(&gs->ds);
23955c770faSChristof Schmitt zfcp_fc_wka_port_force_offline(&gs->as);
24055c770faSChristof Schmitt }
24155c770faSChristof Schmitt
_zfcp_fc_incoming_rscn(struct zfcp_fsf_req * fsf_req,u32 range,struct fc_els_rscn_page * page)24224073b47SChristof Schmitt static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
2439d05ce2cSChristof Schmitt struct fc_els_rscn_page *page)
24424073b47SChristof Schmitt {
24524073b47SChristof Schmitt unsigned long flags;
246ecf0c772SSwen Schillig struct zfcp_adapter *adapter = fsf_req->adapter;
24724073b47SChristof Schmitt struct zfcp_port *port;
24824073b47SChristof Schmitt
249ecf0c772SSwen Schillig read_lock_irqsave(&adapter->port_list_lock, flags);
250ecf0c772SSwen Schillig list_for_each_entry(port, &adapter->port_list, list) {
2519d05ce2cSChristof Schmitt if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
2526f53a2d2SSwen Schillig zfcp_fc_test_link(port);
253ea460a81SSwen Schillig }
254ecf0c772SSwen Schillig read_unlock_irqrestore(&adapter->port_list_lock, flags);
25524073b47SChristof Schmitt }
25624073b47SChristof Schmitt
zfcp_fc_incoming_rscn(struct zfcp_fsf_req * fsf_req)25724073b47SChristof Schmitt static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
25824073b47SChristof Schmitt {
25924073b47SChristof Schmitt struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
260c8206579SSteffen Maier struct zfcp_adapter *adapter = fsf_req->adapter;
2619d05ce2cSChristof Schmitt struct fc_els_rscn *head;
2629d05ce2cSChristof Schmitt struct fc_els_rscn_page *page;
26324073b47SChristof Schmitt u16 i;
26424073b47SChristof Schmitt u16 no_entries;
2659d05ce2cSChristof Schmitt unsigned int afmt;
26624073b47SChristof Schmitt
2679d05ce2cSChristof Schmitt head = (struct fc_els_rscn *) status_buffer->payload.data;
2689d05ce2cSChristof Schmitt page = (struct fc_els_rscn_page *) head;
26924073b47SChristof Schmitt
27024073b47SChristof Schmitt /* see FC-FS */
2719d464fc1SSteffen Maier no_entries = be16_to_cpu(head->rscn_plen) /
2729d464fc1SSteffen Maier sizeof(struct fc_els_rscn_page);
27324073b47SChristof Schmitt
274c8206579SSteffen Maier if (no_entries > 1) {
275c8206579SSteffen Maier /* handle failed ports */
276c8206579SSteffen Maier unsigned long flags;
277c8206579SSteffen Maier struct zfcp_port *port;
278c8206579SSteffen Maier
279c8206579SSteffen Maier read_lock_irqsave(&adapter->port_list_lock, flags);
280c8206579SSteffen Maier list_for_each_entry(port, &adapter->port_list, list) {
281c8206579SSteffen Maier if (port->d_id)
282c8206579SSteffen Maier continue;
283c8206579SSteffen Maier zfcp_erp_port_reopen(port,
284c8206579SSteffen Maier ZFCP_STATUS_COMMON_ERP_FAILED,
285c8206579SSteffen Maier "fcrscn1");
286c8206579SSteffen Maier }
287c8206579SSteffen Maier read_unlock_irqrestore(&adapter->port_list_lock, flags);
288c8206579SSteffen Maier }
289c8206579SSteffen Maier
29024073b47SChristof Schmitt for (i = 1; i < no_entries; i++) {
29124073b47SChristof Schmitt /* skip head and start with 1st element */
2929d05ce2cSChristof Schmitt page++;
2939d05ce2cSChristof Schmitt afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
2949d05ce2cSChristof Schmitt _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
2959d05ce2cSChristof Schmitt page);
2962d1e547fSSven Schuetz zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
2972d1e547fSSven Schuetz *(u32 *)page);
29824073b47SChristof Schmitt }
29943f60cbdSSteffen Maier zfcp_fc_conditional_port_scan(fsf_req->adapter);
30024073b47SChristof Schmitt }
30124073b47SChristof Schmitt
zfcp_fc_incoming_wwpn(struct zfcp_fsf_req * req,u64 wwpn)3027ba58c9cSSwen Schillig static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
30324073b47SChristof Schmitt {
304ecf0c772SSwen Schillig unsigned long flags;
30524073b47SChristof Schmitt struct zfcp_adapter *adapter = req->adapter;
30624073b47SChristof Schmitt struct zfcp_port *port;
30724073b47SChristof Schmitt
308ecf0c772SSwen Schillig read_lock_irqsave(&adapter->port_list_lock, flags);
309ecf0c772SSwen Schillig list_for_each_entry(port, &adapter->port_list, list)
310ecf0c772SSwen Schillig if (port->wwpn == wwpn) {
311ea4a3a6aSSwen Schillig zfcp_erp_port_forced_reopen(port, 0, "fciwwp1");
312ecf0c772SSwen Schillig break;
313ecf0c772SSwen Schillig }
314ecf0c772SSwen Schillig read_unlock_irqrestore(&adapter->port_list_lock, flags);
31524073b47SChristof Schmitt }
31624073b47SChristof Schmitt
zfcp_fc_incoming_plogi(struct zfcp_fsf_req * req)31724073b47SChristof Schmitt static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
31824073b47SChristof Schmitt {
3199d05ce2cSChristof Schmitt struct fsf_status_read_buffer *status_buffer;
3209d05ce2cSChristof Schmitt struct fc_els_flogi *plogi;
32124073b47SChristof Schmitt
3229d05ce2cSChristof Schmitt status_buffer = (struct fsf_status_read_buffer *) req->data;
3239d05ce2cSChristof Schmitt plogi = (struct fc_els_flogi *) status_buffer->payload.data;
3249d464fc1SSteffen Maier zfcp_fc_incoming_wwpn(req, be64_to_cpu(plogi->fl_wwpn));
32524073b47SChristof Schmitt }
32624073b47SChristof Schmitt
zfcp_fc_incoming_logo(struct zfcp_fsf_req * req)32724073b47SChristof Schmitt static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
32824073b47SChristof Schmitt {
32924073b47SChristof Schmitt struct fsf_status_read_buffer *status_buffer =
33024073b47SChristof Schmitt (struct fsf_status_read_buffer *)req->data;
3319d05ce2cSChristof Schmitt struct fc_els_logo *logo =
3329d05ce2cSChristof Schmitt (struct fc_els_logo *) status_buffer->payload.data;
33324073b47SChristof Schmitt
3349d464fc1SSteffen Maier zfcp_fc_incoming_wwpn(req, be64_to_cpu(logo->fl_n_port_wwn));
33524073b47SChristof Schmitt }
33624073b47SChristof Schmitt
33724073b47SChristof Schmitt /**
33824073b47SChristof Schmitt * zfcp_fc_incoming_els - handle incoming ELS
3398684d614SSteffen Maier * @fsf_req: request which contains incoming ELS
34024073b47SChristof Schmitt */
zfcp_fc_incoming_els(struct zfcp_fsf_req * fsf_req)34124073b47SChristof Schmitt void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
34224073b47SChristof Schmitt {
34324073b47SChristof Schmitt struct fsf_status_read_buffer *status_buffer =
34424073b47SChristof Schmitt (struct fsf_status_read_buffer *) fsf_req->data;
345c41f8cbdSSwen Schillig unsigned int els_type = status_buffer->payload.data[0];
34624073b47SChristof Schmitt
3472c55b750SSwen Schillig zfcp_dbf_san_in_els("fciels1", fsf_req);
3489d05ce2cSChristof Schmitt if (els_type == ELS_PLOGI)
34924073b47SChristof Schmitt zfcp_fc_incoming_plogi(fsf_req);
3509d05ce2cSChristof Schmitt else if (els_type == ELS_LOGO)
35124073b47SChristof Schmitt zfcp_fc_incoming_logo(fsf_req);
3529d05ce2cSChristof Schmitt else if (els_type == ELS_RSCN)
35324073b47SChristof Schmitt zfcp_fc_incoming_rscn(fsf_req);
35424073b47SChristof Schmitt }
35524073b47SChristof Schmitt
zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req * fc_req)356fcf7e614SChristof Schmitt static void zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req *fc_req)
3575ab944f9SSwen Schillig {
358fcf7e614SChristof Schmitt struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
359fcf7e614SChristof Schmitt struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
36024073b47SChristof Schmitt
361fcf7e614SChristof Schmitt if (ct_els->status)
3625ab944f9SSwen Schillig return;
3639d464fc1SSteffen Maier if (gid_pn_rsp->ct_hdr.ct_cmd != cpu_to_be16(FC_FS_ACC))
3645ab944f9SSwen Schillig return;
365a5b11ddaSChristof Schmitt
36624073b47SChristof Schmitt /* looks like a valid d_id */
367fcf7e614SChristof Schmitt ct_els->port->d_id = ntoh24(gid_pn_rsp->gid_pn.fp_fid);
36824073b47SChristof Schmitt }
36924073b47SChristof Schmitt
zfcp_fc_complete(void * data)3707c7dc196SChristof Schmitt static void zfcp_fc_complete(void *data)
3717c7dc196SChristof Schmitt {
3727c7dc196SChristof Schmitt complete(data);
3737c7dc196SChristof Schmitt }
3747c7dc196SChristof Schmitt
zfcp_fc_ct_ns_init(struct fc_ct_hdr * ct_hdr,u16 cmd,u16 mr_size)375fcf7e614SChristof Schmitt static void zfcp_fc_ct_ns_init(struct fc_ct_hdr *ct_hdr, u16 cmd, u16 mr_size)
376fcf7e614SChristof Schmitt {
377fcf7e614SChristof Schmitt ct_hdr->ct_rev = FC_CT_REV;
378fcf7e614SChristof Schmitt ct_hdr->ct_fs_type = FC_FST_DIR;
379fcf7e614SChristof Schmitt ct_hdr->ct_fs_subtype = FC_NS_SUBTYPE;
3809d464fc1SSteffen Maier ct_hdr->ct_cmd = cpu_to_be16(cmd);
3819d464fc1SSteffen Maier ct_hdr->ct_mr_size = cpu_to_be16(mr_size / 4);
382fcf7e614SChristof Schmitt }
383fcf7e614SChristof Schmitt
zfcp_fc_ns_gid_pn_request(struct zfcp_port * port,struct zfcp_fc_req * fc_req)384799b76d0SChristof Schmitt static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
385fcf7e614SChristof Schmitt struct zfcp_fc_req *fc_req)
38624073b47SChristof Schmitt {
387799b76d0SChristof Schmitt struct zfcp_adapter *adapter = port->adapter;
3887c7dc196SChristof Schmitt DECLARE_COMPLETION_ONSTACK(completion);
389fcf7e614SChristof Schmitt struct zfcp_fc_gid_pn_req *gid_pn_req = &fc_req->u.gid_pn.req;
390fcf7e614SChristof Schmitt struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
3915ab944f9SSwen Schillig int ret;
39224073b47SChristof Schmitt
39324073b47SChristof Schmitt /* setup parameters for send generic command */
394fcf7e614SChristof Schmitt fc_req->ct_els.port = port;
395fcf7e614SChristof Schmitt fc_req->ct_els.handler = zfcp_fc_complete;
396fcf7e614SChristof Schmitt fc_req->ct_els.handler_data = &completion;
397fcf7e614SChristof Schmitt fc_req->ct_els.req = &fc_req->sg_req;
398fcf7e614SChristof Schmitt fc_req->ct_els.resp = &fc_req->sg_rsp;
399fcf7e614SChristof Schmitt sg_init_one(&fc_req->sg_req, gid_pn_req, sizeof(*gid_pn_req));
400fcf7e614SChristof Schmitt sg_init_one(&fc_req->sg_rsp, gid_pn_rsp, sizeof(*gid_pn_rsp));
40124073b47SChristof Schmitt
402fcf7e614SChristof Schmitt zfcp_fc_ct_ns_init(&gid_pn_req->ct_hdr,
403fcf7e614SChristof Schmitt FC_NS_GID_PN, ZFCP_FC_CT_SIZE_PAGE);
4049d464fc1SSteffen Maier gid_pn_req->gid_pn.fn_wwpn = cpu_to_be64(port->wwpn);
40524073b47SChristof Schmitt
406fcf7e614SChristof Schmitt ret = zfcp_fsf_send_ct(&adapter->gs->ds, &fc_req->ct_els,
40751375ee8SSwen Schillig adapter->pool.gid_pn_req,
40851375ee8SSwen Schillig ZFCP_FC_CTELS_TMO);
4097c7dc196SChristof Schmitt if (!ret) {
4107c7dc196SChristof Schmitt wait_for_completion(&completion);
411fcf7e614SChristof Schmitt zfcp_fc_ns_gid_pn_eval(fc_req);
4127c7dc196SChristof Schmitt }
4135ab944f9SSwen Schillig return ret;
4145ab944f9SSwen Schillig }
4155ab944f9SSwen Schillig
4165ab944f9SSwen Schillig /**
417fcf7e614SChristof Schmitt * zfcp_fc_ns_gid_pn - initiate GID_PN nameserver request
418799b76d0SChristof Schmitt * @port: port where GID_PN request is needed
4195ab944f9SSwen Schillig * return: -ENOMEM on error, 0 otherwise
4205ab944f9SSwen Schillig */
zfcp_fc_ns_gid_pn(struct zfcp_port * port)421799b76d0SChristof Schmitt static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
4225ab944f9SSwen Schillig {
4235ab944f9SSwen Schillig int ret;
424fcf7e614SChristof Schmitt struct zfcp_fc_req *fc_req;
425799b76d0SChristof Schmitt struct zfcp_adapter *adapter = port->adapter;
4265ab944f9SSwen Schillig
427fcf7e614SChristof Schmitt fc_req = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
428fcf7e614SChristof Schmitt if (!fc_req)
4295ab944f9SSwen Schillig return -ENOMEM;
4305ab944f9SSwen Schillig
431fcf7e614SChristof Schmitt memset(fc_req, 0, sizeof(*fc_req));
4325ab944f9SSwen Schillig
4336f53a2d2SSwen Schillig ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
43424073b47SChristof Schmitt if (ret)
4355ab944f9SSwen Schillig goto out;
4365ab944f9SSwen Schillig
437fcf7e614SChristof Schmitt ret = zfcp_fc_ns_gid_pn_request(port, fc_req);
4385ab944f9SSwen Schillig
4396f53a2d2SSwen Schillig zfcp_fc_wka_port_put(&adapter->gs->ds);
4405ab944f9SSwen Schillig out:
441fcf7e614SChristof Schmitt mempool_free(fc_req, adapter->pool.gid_pn);
44224073b47SChristof Schmitt return ret;
44324073b47SChristof Schmitt }
44424073b47SChristof Schmitt
zfcp_fc_port_did_lookup(struct work_struct * work)445799b76d0SChristof Schmitt void zfcp_fc_port_did_lookup(struct work_struct *work)
446799b76d0SChristof Schmitt {
447799b76d0SChristof Schmitt int ret;
448799b76d0SChristof Schmitt struct zfcp_port *port = container_of(work, struct zfcp_port,
449799b76d0SChristof Schmitt gid_pn_work);
450799b76d0SChristof Schmitt
4515c750d58SSteffen Maier set_worker_desc("zgidpn%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */
452799b76d0SChristof Schmitt ret = zfcp_fc_ns_gid_pn(port);
453799b76d0SChristof Schmitt if (ret) {
454799b76d0SChristof Schmitt /* could not issue gid_pn for some reason */
455ea4a3a6aSSwen Schillig zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1");
456799b76d0SChristof Schmitt goto out;
457799b76d0SChristof Schmitt }
458799b76d0SChristof Schmitt
459799b76d0SChristof Schmitt if (!port->d_id) {
460edaed859SSwen Schillig zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
461799b76d0SChristof Schmitt goto out;
462799b76d0SChristof Schmitt }
463799b76d0SChristof Schmitt
464ea4a3a6aSSwen Schillig zfcp_erp_port_reopen(port, 0, "fcgpn_3");
465799b76d0SChristof Schmitt out:
466615f59e0SChristof Schmitt put_device(&port->dev);
467799b76d0SChristof Schmitt }
468799b76d0SChristof Schmitt
46924073b47SChristof Schmitt /**
470934aeb58SChristof Schmitt * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
471934aeb58SChristof Schmitt * @port: The zfcp_port to lookup the d_id for.
472934aeb58SChristof Schmitt */
zfcp_fc_trigger_did_lookup(struct zfcp_port * port)473934aeb58SChristof Schmitt void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
474934aeb58SChristof Schmitt {
475615f59e0SChristof Schmitt get_device(&port->dev);
476934aeb58SChristof Schmitt if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
477615f59e0SChristof Schmitt put_device(&port->dev);
478934aeb58SChristof Schmitt }
479934aeb58SChristof Schmitt
480934aeb58SChristof Schmitt /**
48124073b47SChristof Schmitt * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
48224073b47SChristof Schmitt * @port: zfcp_port structure
48324073b47SChristof Schmitt * @plogi: plogi payload
48424073b47SChristof Schmitt *
48524073b47SChristof Schmitt * Evaluate PLOGI playload and copy important fields into zfcp_port structure
48624073b47SChristof Schmitt */
zfcp_fc_plogi_evaluate(struct zfcp_port * port,struct fc_els_flogi * plogi)4879d05ce2cSChristof Schmitt void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
48824073b47SChristof Schmitt {
4899d464fc1SSteffen Maier if (be64_to_cpu(plogi->fl_wwpn) != port->wwpn) {
4909d05ce2cSChristof Schmitt port->d_id = 0;
4919d05ce2cSChristof Schmitt dev_warn(&port->adapter->ccw_device->dev,
4929d05ce2cSChristof Schmitt "A port opened with WWPN 0x%016Lx returned data that "
4939d05ce2cSChristof Schmitt "identifies it as WWPN 0x%016Lx\n",
4949d05ce2cSChristof Schmitt (unsigned long long) port->wwpn,
4959d464fc1SSteffen Maier (unsigned long long) be64_to_cpu(plogi->fl_wwpn));
4969d05ce2cSChristof Schmitt return;
4979d05ce2cSChristof Schmitt }
4989d05ce2cSChristof Schmitt
4999d464fc1SSteffen Maier port->wwnn = be64_to_cpu(plogi->fl_wwnn);
5009d464fc1SSteffen Maier port->maxframe_size = be16_to_cpu(plogi->fl_csp.sp_bb_data);
5019d05ce2cSChristof Schmitt
5029d464fc1SSteffen Maier if (plogi->fl_cssp[0].cp_class & cpu_to_be16(FC_CPC_VALID))
50324073b47SChristof Schmitt port->supported_classes |= FC_COS_CLASS1;
5049d464fc1SSteffen Maier if (plogi->fl_cssp[1].cp_class & cpu_to_be16(FC_CPC_VALID))
50524073b47SChristof Schmitt port->supported_classes |= FC_COS_CLASS2;
5069d464fc1SSteffen Maier if (plogi->fl_cssp[2].cp_class & cpu_to_be16(FC_CPC_VALID))
50724073b47SChristof Schmitt port->supported_classes |= FC_COS_CLASS3;
5089d464fc1SSteffen Maier if (plogi->fl_cssp[3].cp_class & cpu_to_be16(FC_CPC_VALID))
50924073b47SChristof Schmitt port->supported_classes |= FC_COS_CLASS4;
51024073b47SChristof Schmitt }
51124073b47SChristof Schmitt
zfcp_fc_adisc_handler(void * data)5127c7dc196SChristof Schmitt static void zfcp_fc_adisc_handler(void *data)
51324073b47SChristof Schmitt {
514087897e3SChristof Schmitt struct zfcp_fc_req *fc_req = data;
515087897e3SChristof Schmitt struct zfcp_port *port = fc_req->ct_els.port;
516087897e3SChristof Schmitt struct fc_els_adisc *adisc_resp = &fc_req->u.adisc.rsp;
51724073b47SChristof Schmitt
518087897e3SChristof Schmitt if (fc_req->ct_els.status) {
51924073b47SChristof Schmitt /* request rejected or timed out */
5205b43e719SSwen Schillig zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
521ea4a3a6aSSwen Schillig "fcadh_1");
52224073b47SChristof Schmitt goto out;
52324073b47SChristof Schmitt }
52424073b47SChristof Schmitt
52524073b47SChristof Schmitt if (!port->wwnn)
5269d464fc1SSteffen Maier port->wwnn = be64_to_cpu(adisc_resp->adisc_wwnn);
52724073b47SChristof Schmitt
5289d464fc1SSteffen Maier if ((port->wwpn != be64_to_cpu(adisc_resp->adisc_wwpn)) ||
529a2fa0aedSChristof Schmitt !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
53024095490SSwen Schillig zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
531ea4a3a6aSSwen Schillig "fcadh_2");
532a2fa0aedSChristof Schmitt goto out;
533a2fa0aedSChristof Schmitt }
53424073b47SChristof Schmitt
5358c9db667SSteffen Maier /* re-init to undo drop from zfcp_fc_adisc() */
5368c9db667SSteffen Maier port->d_id = ntoh24(adisc_resp->adisc_port_id);
537e6585198SSteffen Maier /* port is still good, nothing to do */
53824073b47SChristof Schmitt out:
539805de8f4SPeter Zijlstra atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
540*32574fe6SSteffen Maier /*
541*32574fe6SSteffen Maier * port ref comes from get_device() in zfcp_fc_test_link() and
542*32574fe6SSteffen Maier * work item zfcp_fc_link_test_work() passes ref via
543*32574fe6SSteffen Maier * zfcp_fc_adisc() to here, if zfcp_fc_adisc() could send ADISC
544*32574fe6SSteffen Maier */
545615f59e0SChristof Schmitt put_device(&port->dev);
546087897e3SChristof Schmitt kmem_cache_free(zfcp_fc_req_cache, fc_req);
54724073b47SChristof Schmitt }
54824073b47SChristof Schmitt
zfcp_fc_adisc(struct zfcp_port * port)54924073b47SChristof Schmitt static int zfcp_fc_adisc(struct zfcp_port *port)
55024073b47SChristof Schmitt {
551087897e3SChristof Schmitt struct zfcp_fc_req *fc_req;
55224073b47SChristof Schmitt struct zfcp_adapter *adapter = port->adapter;
553087897e3SChristof Schmitt struct Scsi_Host *shost = adapter->scsi_host;
5548c9db667SSteffen Maier u32 d_id;
555ee744622SChristof Schmitt int ret;
55624073b47SChristof Schmitt
557087897e3SChristof Schmitt fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC);
558087897e3SChristof Schmitt if (!fc_req)
55924073b47SChristof Schmitt return -ENOMEM;
56024073b47SChristof Schmitt
561087897e3SChristof Schmitt fc_req->ct_els.port = port;
562087897e3SChristof Schmitt fc_req->ct_els.req = &fc_req->sg_req;
563087897e3SChristof Schmitt fc_req->ct_els.resp = &fc_req->sg_rsp;
564087897e3SChristof Schmitt sg_init_one(&fc_req->sg_req, &fc_req->u.adisc.req,
5659d05ce2cSChristof Schmitt sizeof(struct fc_els_adisc));
566087897e3SChristof Schmitt sg_init_one(&fc_req->sg_rsp, &fc_req->u.adisc.rsp,
5679d05ce2cSChristof Schmitt sizeof(struct fc_els_adisc));
56824073b47SChristof Schmitt
569087897e3SChristof Schmitt fc_req->ct_els.handler = zfcp_fc_adisc_handler;
570087897e3SChristof Schmitt fc_req->ct_els.handler_data = fc_req;
57124073b47SChristof Schmitt
57224073b47SChristof Schmitt /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
57324073b47SChristof Schmitt without FC-AL-2 capability, so we don't set it */
5749d464fc1SSteffen Maier fc_req->u.adisc.req.adisc_wwpn = cpu_to_be64(fc_host_port_name(shost));
5759d464fc1SSteffen Maier fc_req->u.adisc.req.adisc_wwnn = cpu_to_be64(fc_host_node_name(shost));
576087897e3SChristof Schmitt fc_req->u.adisc.req.adisc_cmd = ELS_ADISC;
577087897e3SChristof Schmitt hton24(fc_req->u.adisc.req.adisc_port_id, fc_host_port_id(shost));
57824073b47SChristof Schmitt
5798c9db667SSteffen Maier d_id = port->d_id; /* remember as destination for send els below */
5808c9db667SSteffen Maier /*
5818c9db667SSteffen Maier * Force fresh GID_PN lookup on next port recovery.
5828c9db667SSteffen Maier * Must happen after request setup and before sending request,
5838c9db667SSteffen Maier * to prevent race with port->d_id re-init in zfcp_fc_adisc_handler().
5848c9db667SSteffen Maier */
5858c9db667SSteffen Maier port->d_id = 0;
5868c9db667SSteffen Maier
5878c9db667SSteffen Maier ret = zfcp_fsf_send_els(adapter, d_id, &fc_req->ct_els,
58851375ee8SSwen Schillig ZFCP_FC_CTELS_TMO);
589ee744622SChristof Schmitt if (ret)
590087897e3SChristof Schmitt kmem_cache_free(zfcp_fc_req_cache, fc_req);
591ee744622SChristof Schmitt
592ee744622SChristof Schmitt return ret;
59324073b47SChristof Schmitt }
59424073b47SChristof Schmitt
zfcp_fc_link_test_work(struct work_struct * work)5958fdf30d5SChristof Schmitt void zfcp_fc_link_test_work(struct work_struct *work)
5968fdf30d5SChristof Schmitt {
5978fdf30d5SChristof Schmitt struct zfcp_port *port =
5988fdf30d5SChristof Schmitt container_of(work, struct zfcp_port, test_link_work);
5998fdf30d5SChristof Schmitt int retval;
6008fdf30d5SChristof Schmitt
6015c750d58SSteffen Maier set_worker_desc("zadisc%16llx", port->wwpn); /* < WORKER_DESC_LEN=24 */
602a2fa0aedSChristof Schmitt
60314e242eaSChristof Schmitt /* only issue one test command at one time per port */
60414e242eaSChristof Schmitt if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
60514e242eaSChristof Schmitt goto out;
60614e242eaSChristof Schmitt
607805de8f4SPeter Zijlstra atomic_or(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
60814e242eaSChristof Schmitt
6098fdf30d5SChristof Schmitt retval = zfcp_fc_adisc(port);
6108fdf30d5SChristof Schmitt if (retval == 0)
611*32574fe6SSteffen Maier return; /* port ref passed to zfcp_fc_adisc(), no put here */
6128fdf30d5SChristof Schmitt
6138fdf30d5SChristof Schmitt /* send of ADISC was not possible */
614805de8f4SPeter Zijlstra atomic_andnot(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
615ea4a3a6aSSwen Schillig zfcp_erp_port_forced_reopen(port, 0, "fcltwk1");
616a2fa0aedSChristof Schmitt
61714e242eaSChristof Schmitt out:
618615f59e0SChristof Schmitt put_device(&port->dev);
6198fdf30d5SChristof Schmitt }
6208fdf30d5SChristof Schmitt
62124073b47SChristof Schmitt /**
6226f53a2d2SSwen Schillig * zfcp_fc_test_link - lightweight link test procedure
62324073b47SChristof Schmitt * @port: port to be tested
62424073b47SChristof Schmitt *
62524073b47SChristof Schmitt * Test status of a link to a remote port using the ELS command ADISC.
62624073b47SChristof Schmitt * If there is a problem with the remote port, error recovery steps
62724073b47SChristof Schmitt * will be triggered.
62824073b47SChristof Schmitt */
zfcp_fc_test_link(struct zfcp_port * port)6296f53a2d2SSwen Schillig void zfcp_fc_test_link(struct zfcp_port *port)
63024073b47SChristof Schmitt {
631615f59e0SChristof Schmitt get_device(&port->dev);
6324544683aSSwen Schillig if (!queue_work(port->adapter->work_queue, &port->test_link_work))
633615f59e0SChristof Schmitt put_device(&port->dev);
63424073b47SChristof Schmitt }
635cc8c2829SSwen Schillig
63658f3ead5SSteffen Maier /**
63758f3ead5SSteffen Maier * zfcp_fc_sg_free_table - free memory used by scatterlists
63858f3ead5SSteffen Maier * @sg: pointer to scatterlist
63958f3ead5SSteffen Maier * @count: number of scatterlist which are to be free'ed
64058f3ead5SSteffen Maier * the scatterlist are expected to reference pages always
64158f3ead5SSteffen Maier */
zfcp_fc_sg_free_table(struct scatterlist * sg,int count)64258f3ead5SSteffen Maier static void zfcp_fc_sg_free_table(struct scatterlist *sg, int count)
64358f3ead5SSteffen Maier {
64458f3ead5SSteffen Maier int i;
64558f3ead5SSteffen Maier
646013be038SMing Lei for (i = 0; i < count; i++, sg = sg_next(sg))
64758f3ead5SSteffen Maier if (sg)
64858f3ead5SSteffen Maier free_page((unsigned long) sg_virt(sg));
64958f3ead5SSteffen Maier else
65058f3ead5SSteffen Maier break;
65158f3ead5SSteffen Maier }
65258f3ead5SSteffen Maier
65358f3ead5SSteffen Maier /**
65458f3ead5SSteffen Maier * zfcp_fc_sg_setup_table - init scatterlist and allocate, assign buffers
65558f3ead5SSteffen Maier * @sg: pointer to struct scatterlist
65658f3ead5SSteffen Maier * @count: number of scatterlists which should be assigned with buffers
65758f3ead5SSteffen Maier * of size page
65858f3ead5SSteffen Maier *
65958f3ead5SSteffen Maier * Returns: 0 on success, -ENOMEM otherwise
66058f3ead5SSteffen Maier */
zfcp_fc_sg_setup_table(struct scatterlist * sg,int count)66158f3ead5SSteffen Maier static int zfcp_fc_sg_setup_table(struct scatterlist *sg, int count)
66258f3ead5SSteffen Maier {
66358f3ead5SSteffen Maier void *addr;
66458f3ead5SSteffen Maier int i;
66558f3ead5SSteffen Maier
66658f3ead5SSteffen Maier sg_init_table(sg, count);
667013be038SMing Lei for (i = 0; i < count; i++, sg = sg_next(sg)) {
66858f3ead5SSteffen Maier addr = (void *) get_zeroed_page(GFP_KERNEL);
66958f3ead5SSteffen Maier if (!addr) {
67058f3ead5SSteffen Maier zfcp_fc_sg_free_table(sg, i);
67158f3ead5SSteffen Maier return -ENOMEM;
67258f3ead5SSteffen Maier }
67358f3ead5SSteffen Maier sg_set_buf(sg, addr, PAGE_SIZE);
67458f3ead5SSteffen Maier }
67558f3ead5SSteffen Maier return 0;
67658f3ead5SSteffen Maier }
67758f3ead5SSteffen Maier
zfcp_fc_alloc_sg_env(int buf_num)678d39eda54SSteffen Maier static struct zfcp_fc_req *zfcp_fc_alloc_sg_env(int buf_num)
679cc8c2829SSwen Schillig {
680f9773229SChristof Schmitt struct zfcp_fc_req *fc_req;
681cc8c2829SSwen Schillig
682f9773229SChristof Schmitt fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
683f9773229SChristof Schmitt if (!fc_req)
684cc8c2829SSwen Schillig return NULL;
685cc8c2829SSwen Schillig
68658f3ead5SSteffen Maier if (zfcp_fc_sg_setup_table(&fc_req->sg_rsp, buf_num)) {
687f9773229SChristof Schmitt kmem_cache_free(zfcp_fc_req_cache, fc_req);
688f9773229SChristof Schmitt return NULL;
689cc8c2829SSwen Schillig }
690cc8c2829SSwen Schillig
691f9773229SChristof Schmitt sg_init_one(&fc_req->sg_req, &fc_req->u.gpn_ft.req,
692f9773229SChristof Schmitt sizeof(struct zfcp_fc_gpn_ft_req));
693cc8c2829SSwen Schillig
694f9773229SChristof Schmitt return fc_req;
695f9773229SChristof Schmitt }
696f9773229SChristof Schmitt
zfcp_fc_send_gpn_ft(struct zfcp_fc_req * fc_req,struct zfcp_adapter * adapter,int max_bytes)697f9773229SChristof Schmitt static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req,
6986f53a2d2SSwen Schillig struct zfcp_adapter *adapter, int max_bytes)
699cc8c2829SSwen Schillig {
700f9773229SChristof Schmitt struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
701f9773229SChristof Schmitt struct zfcp_fc_gpn_ft_req *req = &fc_req->u.gpn_ft.req;
7027c7dc196SChristof Schmitt DECLARE_COMPLETION_ONSTACK(completion);
703cc8c2829SSwen Schillig int ret;
704cc8c2829SSwen Schillig
705f9773229SChristof Schmitt zfcp_fc_ct_ns_init(&req->ct_hdr, FC_NS_GPN_FT, max_bytes);
706dbf5dfe9SChristof Schmitt req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
707cc8c2829SSwen Schillig
708f9773229SChristof Schmitt ct_els->handler = zfcp_fc_complete;
709f9773229SChristof Schmitt ct_els->handler_data = &completion;
710f9773229SChristof Schmitt ct_els->req = &fc_req->sg_req;
711f9773229SChristof Schmitt ct_els->resp = &fc_req->sg_rsp;
712cc8c2829SSwen Schillig
713f9773229SChristof Schmitt ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
71451375ee8SSwen Schillig ZFCP_FC_CTELS_TMO);
715cc8c2829SSwen Schillig if (!ret)
7167c7dc196SChristof Schmitt wait_for_completion(&completion);
717cc8c2829SSwen Schillig return ret;
718cc8c2829SSwen Schillig }
719cc8c2829SSwen Schillig
zfcp_fc_validate_port(struct zfcp_port * port,struct list_head * lh)720f3450c7bSSwen Schillig static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
721cc8c2829SSwen Schillig {
7226ab35c07SMartin Petermann if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
7236ab35c07SMartin Petermann return;
7246ab35c07SMartin Petermann
725805de8f4SPeter Zijlstra atomic_andnot(ZFCP_STATUS_COMMON_NOESC, &port->status);
726cc8c2829SSwen Schillig
7270406289eSChristof Schmitt if ((port->supported_classes != 0) ||
728f3450c7bSSwen Schillig !list_empty(&port->unit_list))
729cc8c2829SSwen Schillig return;
730f3450c7bSSwen Schillig
731f3450c7bSSwen Schillig list_move_tail(&port->list, lh);
732cc8c2829SSwen Schillig }
733cc8c2829SSwen Schillig
zfcp_fc_eval_gpn_ft(struct zfcp_fc_req * fc_req,struct zfcp_adapter * adapter,int max_entries)734f9773229SChristof Schmitt static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_req *fc_req,
7357c7dc196SChristof Schmitt struct zfcp_adapter *adapter, int max_entries)
736cc8c2829SSwen Schillig {
737f9773229SChristof Schmitt struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
738f9773229SChristof Schmitt struct scatterlist *sg = &fc_req->sg_rsp;
739dbf5dfe9SChristof Schmitt struct fc_ct_hdr *hdr = sg_virt(sg);
740dbf5dfe9SChristof Schmitt struct fc_gpn_ft_resp *acc = sg_virt(sg);
741cc8c2829SSwen Schillig struct zfcp_port *port, *tmp;
742ecf0c772SSwen Schillig unsigned long flags;
743f3450c7bSSwen Schillig LIST_HEAD(remove_lh);
744cc8c2829SSwen Schillig u32 d_id;
74547f7bba5SChristof Schmitt int ret = 0, x, last = 0;
746cc8c2829SSwen Schillig
747f9773229SChristof Schmitt if (ct_els->status)
748cc8c2829SSwen Schillig return -EIO;
749cc8c2829SSwen Schillig
7509d464fc1SSteffen Maier if (hdr->ct_cmd != cpu_to_be16(FC_FS_ACC)) {
751ab8ab4beSSteffen Maier if (hdr->ct_reason == FC_FS_RJT_UNABL)
752cc8c2829SSwen Schillig return -EAGAIN; /* might be a temporary condition */
753cc8c2829SSwen Schillig return -EIO;
754cc8c2829SSwen Schillig }
755cc8c2829SSwen Schillig
756dbf5dfe9SChristof Schmitt if (hdr->ct_mr_size) {
75739eb7e9aSChristof Schmitt dev_warn(&adapter->ccw_device->dev,
75839eb7e9aSChristof Schmitt "The name server reported %d words residual data\n",
759dbf5dfe9SChristof Schmitt hdr->ct_mr_size);
760cc8c2829SSwen Schillig return -E2BIG;
76139eb7e9aSChristof Schmitt }
762cc8c2829SSwen Schillig
763cc8c2829SSwen Schillig /* first entry is the header */
76439eb7e9aSChristof Schmitt for (x = 1; x < max_entries && !last; x++) {
765dbf5dfe9SChristof Schmitt if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
766cc8c2829SSwen Schillig acc++;
767cc8c2829SSwen Schillig else
768cc8c2829SSwen Schillig acc = sg_virt(++sg);
769cc8c2829SSwen Schillig
770dbf5dfe9SChristof Schmitt last = acc->fp_flags & FC_NS_FID_LAST;
771dbf5dfe9SChristof Schmitt d_id = ntoh24(acc->fp_fid);
772cc8c2829SSwen Schillig
7735ab944f9SSwen Schillig /* don't attach ports with a well known address */
774dbf5dfe9SChristof Schmitt if (d_id >= FC_FID_WELL_KNOWN_BASE)
7755ab944f9SSwen Schillig continue;
776cc8c2829SSwen Schillig /* skip the adapter's port and known remote ports */
7779d464fc1SSteffen Maier if (be64_to_cpu(acc->fp_wwpn) ==
7789d464fc1SSteffen Maier fc_host_port_name(adapter->scsi_host))
779cc8c2829SSwen Schillig continue;
780cc8c2829SSwen Schillig
7819d464fc1SSteffen Maier port = zfcp_port_enqueue(adapter, be64_to_cpu(acc->fp_wwpn),
782cc8c2829SSwen Schillig ZFCP_STATUS_COMMON_NOESC, d_id);
783ecf0c772SSwen Schillig if (!IS_ERR(port))
784ea4a3a6aSSwen Schillig zfcp_erp_port_reopen(port, 0, "fcegpf1");
785ecf0c772SSwen Schillig else if (PTR_ERR(port) != -EEXIST)
786ecf0c772SSwen Schillig ret = PTR_ERR(port);
787cc8c2829SSwen Schillig }
788cc8c2829SSwen Schillig
789cc8c2829SSwen Schillig zfcp_erp_wait(adapter);
790ecf0c772SSwen Schillig write_lock_irqsave(&adapter->port_list_lock, flags);
791ecf0c772SSwen Schillig list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
792f3450c7bSSwen Schillig zfcp_fc_validate_port(port, &remove_lh);
793ecf0c772SSwen Schillig write_unlock_irqrestore(&adapter->port_list_lock, flags);
794f3450c7bSSwen Schillig
795f3450c7bSSwen Schillig list_for_each_entry_safe(port, tmp, &remove_lh, list) {
796ea4a3a6aSSwen Schillig zfcp_erp_port_shutdown(port, 0, "fcegpf2");
79783d4e1c3SSebastian Ott device_unregister(&port->dev);
798f3450c7bSSwen Schillig }
799f3450c7bSSwen Schillig
800cc8c2829SSwen Schillig return ret;
801cc8c2829SSwen Schillig }
802cc8c2829SSwen Schillig
803cc8c2829SSwen Schillig /**
8046f53a2d2SSwen Schillig * zfcp_fc_scan_ports - scan remote ports and attach new ports
8059eae07efSSwen Schillig * @work: reference to scheduled work
806cc8c2829SSwen Schillig */
zfcp_fc_scan_ports(struct work_struct * work)8079eae07efSSwen Schillig void zfcp_fc_scan_ports(struct work_struct *work)
808cc8c2829SSwen Schillig {
80918f87a67SMartin Peschke struct delayed_work *dw = to_delayed_work(work);
81018f87a67SMartin Peschke struct zfcp_adapter *adapter = container_of(dw, struct zfcp_adapter,
8119eae07efSSwen Schillig scan_work);
812cc8c2829SSwen Schillig int ret, i;
813f9773229SChristof Schmitt struct zfcp_fc_req *fc_req;
81439eb7e9aSChristof Schmitt int chain, max_entries, buf_num, max_bytes;
81539eb7e9aSChristof Schmitt
81618f87a67SMartin Peschke zfcp_fc_port_scan_time(adapter);
81718f87a67SMartin Peschke
81839eb7e9aSChristof Schmitt chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
819dbf5dfe9SChristof Schmitt buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
820dbf5dfe9SChristof Schmitt max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
821dbf5dfe9SChristof Schmitt max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
822cc8c2829SSwen Schillig
823306b6edcSSwen Schillig if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
824306b6edcSSwen Schillig fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
8259eae07efSSwen Schillig return;
826cc8c2829SSwen Schillig
8279eae07efSSwen Schillig if (zfcp_fc_wka_port_get(&adapter->gs->ds))
8289eae07efSSwen Schillig return;
829cc8c2829SSwen Schillig
830d39eda54SSteffen Maier fc_req = zfcp_fc_alloc_sg_env(buf_num);
831f9773229SChristof Schmitt if (!fc_req)
8325ab944f9SSwen Schillig goto out;
833cc8c2829SSwen Schillig
834cc8c2829SSwen Schillig for (i = 0; i < 3; i++) {
835f9773229SChristof Schmitt ret = zfcp_fc_send_gpn_ft(fc_req, adapter, max_bytes);
836cc8c2829SSwen Schillig if (!ret) {
837f9773229SChristof Schmitt ret = zfcp_fc_eval_gpn_ft(fc_req, adapter, max_entries);
838cc8c2829SSwen Schillig if (ret == -EAGAIN)
839cc8c2829SSwen Schillig ssleep(1);
840cc8c2829SSwen Schillig else
841cc8c2829SSwen Schillig break;
842cc8c2829SSwen Schillig }
843cc8c2829SSwen Schillig }
84458f3ead5SSteffen Maier zfcp_fc_sg_free_table(&fc_req->sg_rsp, buf_num);
845f9773229SChristof Schmitt kmem_cache_free(zfcp_fc_req_cache, fc_req);
8465ab944f9SSwen Schillig out:
8476f53a2d2SSwen Schillig zfcp_fc_wka_port_put(&adapter->gs->ds);
848cc8c2829SSwen Schillig }
849cc8c2829SSwen Schillig
zfcp_fc_gspn(struct zfcp_adapter * adapter,struct zfcp_fc_req * fc_req)850038d9446SChristof Schmitt static int zfcp_fc_gspn(struct zfcp_adapter *adapter,
851038d9446SChristof Schmitt struct zfcp_fc_req *fc_req)
852038d9446SChristof Schmitt {
853038d9446SChristof Schmitt DECLARE_COMPLETION_ONSTACK(completion);
854038d9446SChristof Schmitt char devno[] = "DEVNO:";
855038d9446SChristof Schmitt struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
856038d9446SChristof Schmitt struct zfcp_fc_gspn_req *gspn_req = &fc_req->u.gspn.req;
857038d9446SChristof Schmitt struct zfcp_fc_gspn_rsp *gspn_rsp = &fc_req->u.gspn.rsp;
858038d9446SChristof Schmitt int ret;
859038d9446SChristof Schmitt
860038d9446SChristof Schmitt zfcp_fc_ct_ns_init(&gspn_req->ct_hdr, FC_NS_GSPN_ID,
861038d9446SChristof Schmitt FC_SYMBOLIC_NAME_SIZE);
862038d9446SChristof Schmitt hton24(gspn_req->gspn.fp_fid, fc_host_port_id(adapter->scsi_host));
863038d9446SChristof Schmitt
864038d9446SChristof Schmitt sg_init_one(&fc_req->sg_req, gspn_req, sizeof(*gspn_req));
865038d9446SChristof Schmitt sg_init_one(&fc_req->sg_rsp, gspn_rsp, sizeof(*gspn_rsp));
866038d9446SChristof Schmitt
867038d9446SChristof Schmitt ct_els->handler = zfcp_fc_complete;
868038d9446SChristof Schmitt ct_els->handler_data = &completion;
869038d9446SChristof Schmitt ct_els->req = &fc_req->sg_req;
870038d9446SChristof Schmitt ct_els->resp = &fc_req->sg_rsp;
871038d9446SChristof Schmitt
872038d9446SChristof Schmitt ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
873038d9446SChristof Schmitt ZFCP_FC_CTELS_TMO);
874038d9446SChristof Schmitt if (ret)
875038d9446SChristof Schmitt return ret;
876038d9446SChristof Schmitt
877038d9446SChristof Schmitt wait_for_completion(&completion);
878038d9446SChristof Schmitt if (ct_els->status)
879038d9446SChristof Schmitt return ct_els->status;
880038d9446SChristof Schmitt
881038d9446SChristof Schmitt if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_NPIV &&
882038d9446SChristof Schmitt !(strstr(gspn_rsp->gspn.fp_name, devno)))
883038d9446SChristof Schmitt snprintf(fc_host_symbolic_name(adapter->scsi_host),
884038d9446SChristof Schmitt FC_SYMBOLIC_NAME_SIZE, "%s%s %s NAME: %s",
885038d9446SChristof Schmitt gspn_rsp->gspn.fp_name, devno,
886038d9446SChristof Schmitt dev_name(&adapter->ccw_device->dev),
887038d9446SChristof Schmitt init_utsname()->nodename);
888038d9446SChristof Schmitt else
889820109fbSWolfram Sang strscpy(fc_host_symbolic_name(adapter->scsi_host),
890038d9446SChristof Schmitt gspn_rsp->gspn.fp_name, FC_SYMBOLIC_NAME_SIZE);
891038d9446SChristof Schmitt
892038d9446SChristof Schmitt return 0;
893038d9446SChristof Schmitt }
894038d9446SChristof Schmitt
zfcp_fc_rspn(struct zfcp_adapter * adapter,struct zfcp_fc_req * fc_req)895038d9446SChristof Schmitt static void zfcp_fc_rspn(struct zfcp_adapter *adapter,
896038d9446SChristof Schmitt struct zfcp_fc_req *fc_req)
897038d9446SChristof Schmitt {
898038d9446SChristof Schmitt DECLARE_COMPLETION_ONSTACK(completion);
899038d9446SChristof Schmitt struct Scsi_Host *shost = adapter->scsi_host;
900038d9446SChristof Schmitt struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
901038d9446SChristof Schmitt struct zfcp_fc_rspn_req *rspn_req = &fc_req->u.rspn.req;
902038d9446SChristof Schmitt struct fc_ct_hdr *rspn_rsp = &fc_req->u.rspn.rsp;
903038d9446SChristof Schmitt int ret, len;
904038d9446SChristof Schmitt
905038d9446SChristof Schmitt zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID,
906038d9446SChristof Schmitt FC_SYMBOLIC_NAME_SIZE);
907038d9446SChristof Schmitt hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost));
9080d224b10SKees Cook
9090d224b10SKees Cook BUILD_BUG_ON(sizeof(rspn_req->name) !=
9100d224b10SKees Cook sizeof(fc_host_symbolic_name(shost)));
9110d224b10SKees Cook BUILD_BUG_ON(sizeof(rspn_req->name) !=
9120d224b10SKees Cook type_max(typeof(rspn_req->rspn.fr_name_len)) + 1);
9130d224b10SKees Cook len = strscpy(rspn_req->name, fc_host_symbolic_name(shost),
9140d224b10SKees Cook sizeof(rspn_req->name));
9150d224b10SKees Cook /*
9160d224b10SKees Cook * It should be impossible for this to truncate (see BUILD_BUG_ON()
9170d224b10SKees Cook * above), but be robust anyway.
9180d224b10SKees Cook */
9190d224b10SKees Cook if (WARN_ON(len < 0))
9200d224b10SKees Cook len = sizeof(rspn_req->name) - 1;
921038d9446SChristof Schmitt rspn_req->rspn.fr_name_len = len;
922038d9446SChristof Schmitt
923038d9446SChristof Schmitt sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req));
924038d9446SChristof Schmitt sg_init_one(&fc_req->sg_rsp, rspn_rsp, sizeof(*rspn_rsp));
925038d9446SChristof Schmitt
926038d9446SChristof Schmitt ct_els->handler = zfcp_fc_complete;
927038d9446SChristof Schmitt ct_els->handler_data = &completion;
928038d9446SChristof Schmitt ct_els->req = &fc_req->sg_req;
929038d9446SChristof Schmitt ct_els->resp = &fc_req->sg_rsp;
930038d9446SChristof Schmitt
931038d9446SChristof Schmitt ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
932038d9446SChristof Schmitt ZFCP_FC_CTELS_TMO);
933038d9446SChristof Schmitt if (!ret)
934038d9446SChristof Schmitt wait_for_completion(&completion);
935038d9446SChristof Schmitt }
936038d9446SChristof Schmitt
937038d9446SChristof Schmitt /**
938038d9446SChristof Schmitt * zfcp_fc_sym_name_update - Retrieve and update the symbolic port name
939038d9446SChristof Schmitt * @work: ns_up_work of the adapter where to update the symbolic port name
940038d9446SChristof Schmitt *
941038d9446SChristof Schmitt * Retrieve the current symbolic port name that may have been set by
942038d9446SChristof Schmitt * the hardware using the GSPN request and update the fc_host
943038d9446SChristof Schmitt * symbolic_name sysfs attribute. When running in NPIV mode (and hence
944038d9446SChristof Schmitt * the port name is unique for this system), update the symbolic port
945038d9446SChristof Schmitt * name to add Linux specific information and update the FC nameserver
946038d9446SChristof Schmitt * using the RSPN request.
947038d9446SChristof Schmitt */
zfcp_fc_sym_name_update(struct work_struct * work)948038d9446SChristof Schmitt void zfcp_fc_sym_name_update(struct work_struct *work)
949038d9446SChristof Schmitt {
950038d9446SChristof Schmitt struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
951038d9446SChristof Schmitt ns_up_work);
952038d9446SChristof Schmitt int ret;
953038d9446SChristof Schmitt struct zfcp_fc_req *fc_req;
954038d9446SChristof Schmitt
955038d9446SChristof Schmitt if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
956038d9446SChristof Schmitt fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
957038d9446SChristof Schmitt return;
958038d9446SChristof Schmitt
959038d9446SChristof Schmitt fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
960038d9446SChristof Schmitt if (!fc_req)
961038d9446SChristof Schmitt return;
962038d9446SChristof Schmitt
963038d9446SChristof Schmitt ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
964038d9446SChristof Schmitt if (ret)
965038d9446SChristof Schmitt goto out_free;
966038d9446SChristof Schmitt
967038d9446SChristof Schmitt ret = zfcp_fc_gspn(adapter, fc_req);
968038d9446SChristof Schmitt if (ret || fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
969038d9446SChristof Schmitt goto out_ds_put;
970038d9446SChristof Schmitt
971038d9446SChristof Schmitt memset(fc_req, 0, sizeof(*fc_req));
972038d9446SChristof Schmitt zfcp_fc_rspn(adapter, fc_req);
973038d9446SChristof Schmitt
974038d9446SChristof Schmitt out_ds_put:
975038d9446SChristof Schmitt zfcp_fc_wka_port_put(&adapter->gs->ds);
976038d9446SChristof Schmitt out_free:
977038d9446SChristof Schmitt kmem_cache_free(zfcp_fc_req_cache, fc_req);
978038d9446SChristof Schmitt }
979038d9446SChristof Schmitt
zfcp_fc_ct_els_job_handler(void * data)9807c7dc196SChristof Schmitt static void zfcp_fc_ct_els_job_handler(void *data)
9819d544f2bSSven Schuetz {
98275cc8cfcSJohannes Thumshirn struct bsg_job *job = data;
9837c7dc196SChristof Schmitt struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
9847dec9cf1SSwen Schillig struct fc_bsg_reply *jr = job->reply;
9859d544f2bSSven Schuetz
9867dec9cf1SSwen Schillig jr->reply_payload_rcv_len = job->reply_payload.payload_len;
9877dec9cf1SSwen Schillig jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
9887dec9cf1SSwen Schillig jr->result = zfcp_ct_els->status ? -EIO : 0;
98906548160SJohannes Thumshirn bsg_job_done(job, jr->result, jr->reply_payload_rcv_len);
9909d544f2bSSven Schuetz }
9919d544f2bSSven Schuetz
zfcp_fc_job_wka_port(struct bsg_job * job)99275cc8cfcSJohannes Thumshirn static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct bsg_job *job)
993f09d5454SChristof Schmitt {
994f09d5454SChristof Schmitt u32 preamble_word1;
995f09d5454SChristof Schmitt u8 gs_type;
996f09d5454SChristof Schmitt struct zfcp_adapter *adapter;
99701e0e15cSJohannes Thumshirn struct fc_bsg_request *bsg_request = job->request;
9981d69b122SJohannes Thumshirn struct fc_rport *rport = fc_bsg_to_rport(job);
999cd21c605SJohannes Thumshirn struct Scsi_Host *shost;
1000f09d5454SChristof Schmitt
100101e0e15cSJohannes Thumshirn preamble_word1 = bsg_request->rqst_data.r_ct.preamble_word1;
1002f09d5454SChristof Schmitt gs_type = (preamble_word1 & 0xff000000) >> 24;
1003f09d5454SChristof Schmitt
10041d69b122SJohannes Thumshirn shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
1005cd21c605SJohannes Thumshirn adapter = (struct zfcp_adapter *) shost->hostdata[0];
1006f09d5454SChristof Schmitt
1007f09d5454SChristof Schmitt switch (gs_type) {
1008f09d5454SChristof Schmitt case FC_FST_ALIAS:
1009f09d5454SChristof Schmitt return &adapter->gs->as;
1010f09d5454SChristof Schmitt case FC_FST_MGMT:
1011f09d5454SChristof Schmitt return &adapter->gs->ms;
1012f09d5454SChristof Schmitt case FC_FST_TIME:
1013f09d5454SChristof Schmitt return &adapter->gs->ts;
1014f09d5454SChristof Schmitt break;
1015f09d5454SChristof Schmitt case FC_FST_DIR:
1016f09d5454SChristof Schmitt return &adapter->gs->ds;
1017f09d5454SChristof Schmitt break;
1018f09d5454SChristof Schmitt default:
1019f09d5454SChristof Schmitt return NULL;
1020f09d5454SChristof Schmitt }
1021f09d5454SChristof Schmitt }
1022f09d5454SChristof Schmitt
zfcp_fc_ct_job_handler(void * data)1023f09d5454SChristof Schmitt static void zfcp_fc_ct_job_handler(void *data)
1024f09d5454SChristof Schmitt {
102575cc8cfcSJohannes Thumshirn struct bsg_job *job = data;
1026f09d5454SChristof Schmitt struct zfcp_fc_wka_port *wka_port;
1027f09d5454SChristof Schmitt
1028f09d5454SChristof Schmitt wka_port = zfcp_fc_job_wka_port(job);
1029f09d5454SChristof Schmitt zfcp_fc_wka_port_put(wka_port);
1030f09d5454SChristof Schmitt
1031f09d5454SChristof Schmitt zfcp_fc_ct_els_job_handler(data);
1032f09d5454SChristof Schmitt }
1033f09d5454SChristof Schmitt
zfcp_fc_exec_els_job(struct bsg_job * job,struct zfcp_adapter * adapter)103475cc8cfcSJohannes Thumshirn static int zfcp_fc_exec_els_job(struct bsg_job *job,
10357c7dc196SChristof Schmitt struct zfcp_adapter *adapter)
10369d544f2bSSven Schuetz {
10377c7dc196SChristof Schmitt struct zfcp_fsf_ct_els *els = job->dd_data;
10381d69b122SJohannes Thumshirn struct fc_rport *rport = fc_bsg_to_rport(job);
103901e0e15cSJohannes Thumshirn struct fc_bsg_request *bsg_request = job->request;
10409d544f2bSSven Schuetz struct zfcp_port *port;
10417c7dc196SChristof Schmitt u32 d_id;
10429d544f2bSSven Schuetz
10439d544f2bSSven Schuetz if (rport) {
1044ea945ff8SSwen Schillig port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
10457c7dc196SChristof Schmitt if (!port)
10469d544f2bSSven Schuetz return -EINVAL;
1047ecf0c772SSwen Schillig
10487c7dc196SChristof Schmitt d_id = port->d_id;
1049615f59e0SChristof Schmitt put_device(&port->dev);
10507c7dc196SChristof Schmitt } else
105101e0e15cSJohannes Thumshirn d_id = ntoh24(bsg_request->rqst_data.h_els.port_id);
10527c7dc196SChristof Schmitt
1053f09d5454SChristof Schmitt els->handler = zfcp_fc_ct_els_job_handler;
105431156ec3SChristoph Hellwig return zfcp_fsf_send_els(adapter, d_id, els, job->timeout / HZ);
10559d544f2bSSven Schuetz }
10569d544f2bSSven Schuetz
zfcp_fc_exec_ct_job(struct bsg_job * job,struct zfcp_adapter * adapter)105775cc8cfcSJohannes Thumshirn static int zfcp_fc_exec_ct_job(struct bsg_job *job,
10587c7dc196SChristof Schmitt struct zfcp_adapter *adapter)
10599d544f2bSSven Schuetz {
10609d544f2bSSven Schuetz int ret;
10617c7dc196SChristof Schmitt struct zfcp_fsf_ct_els *ct = job->dd_data;
10627c7dc196SChristof Schmitt struct zfcp_fc_wka_port *wka_port;
10639d544f2bSSven Schuetz
1064f09d5454SChristof Schmitt wka_port = zfcp_fc_job_wka_port(job);
1065f09d5454SChristof Schmitt if (!wka_port)
1066f09d5454SChristof Schmitt return -EINVAL;
10679d544f2bSSven Schuetz
10687c7dc196SChristof Schmitt ret = zfcp_fc_wka_port_get(wka_port);
10697c7dc196SChristof Schmitt if (ret)
10707c7dc196SChristof Schmitt return ret;
10717c7dc196SChristof Schmitt
1072f09d5454SChristof Schmitt ct->handler = zfcp_fc_ct_job_handler;
107331156ec3SChristoph Hellwig ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->timeout / HZ);
10747c7dc196SChristof Schmitt if (ret)
10757c7dc196SChristof Schmitt zfcp_fc_wka_port_put(wka_port);
10767c7dc196SChristof Schmitt
10779d544f2bSSven Schuetz return ret;
10789d544f2bSSven Schuetz }
10799d544f2bSSven Schuetz
zfcp_fc_exec_bsg_job(struct bsg_job * job)108075cc8cfcSJohannes Thumshirn int zfcp_fc_exec_bsg_job(struct bsg_job *job)
10817c7dc196SChristof Schmitt {
10827c7dc196SChristof Schmitt struct Scsi_Host *shost;
10837c7dc196SChristof Schmitt struct zfcp_adapter *adapter;
10847c7dc196SChristof Schmitt struct zfcp_fsf_ct_els *ct_els = job->dd_data;
108501e0e15cSJohannes Thumshirn struct fc_bsg_request *bsg_request = job->request;
10861d69b122SJohannes Thumshirn struct fc_rport *rport = fc_bsg_to_rport(job);
10879d544f2bSSven Schuetz
10881d69b122SJohannes Thumshirn shost = rport ? rport_to_shost(rport) : fc_bsg_to_shost(job);
10897c7dc196SChristof Schmitt adapter = (struct zfcp_adapter *)shost->hostdata[0];
10907c7dc196SChristof Schmitt
10917c7dc196SChristof Schmitt if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
10927c7dc196SChristof Schmitt return -EINVAL;
10937c7dc196SChristof Schmitt
10947c7dc196SChristof Schmitt ct_els->req = job->request_payload.sg_list;
10957c7dc196SChristof Schmitt ct_els->resp = job->reply_payload.sg_list;
10967c7dc196SChristof Schmitt ct_els->handler_data = job;
10977c7dc196SChristof Schmitt
109801e0e15cSJohannes Thumshirn switch (bsg_request->msgcode) {
10997c7dc196SChristof Schmitt case FC_BSG_RPT_ELS:
11007c7dc196SChristof Schmitt case FC_BSG_HST_ELS_NOLOGIN:
11017c7dc196SChristof Schmitt return zfcp_fc_exec_els_job(job, adapter);
11027c7dc196SChristof Schmitt case FC_BSG_RPT_CT:
11037c7dc196SChristof Schmitt case FC_BSG_HST_CT:
11047c7dc196SChristof Schmitt return zfcp_fc_exec_ct_job(job, adapter);
11057c7dc196SChristof Schmitt default:
11067c7dc196SChristof Schmitt return -EINVAL;
11079d544f2bSSven Schuetz }
11089d544f2bSSven Schuetz }
1109d5a282a1SSwen Schillig
zfcp_fc_timeout_bsg_job(struct bsg_job * job)111075cc8cfcSJohannes Thumshirn int zfcp_fc_timeout_bsg_job(struct bsg_job *job)
1111491ca442SSwen Schillig {
1112491ca442SSwen Schillig /* hardware tracks timeout, reset bsg timeout to not interfere */
1113491ca442SSwen Schillig return -EAGAIN;
1114491ca442SSwen Schillig }
1115491ca442SSwen Schillig
zfcp_fc_gs_setup(struct zfcp_adapter * adapter)1116d5a282a1SSwen Schillig int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
1117d5a282a1SSwen Schillig {
1118bd0072ecSChristof Schmitt struct zfcp_fc_wka_ports *wka_ports;
1119d5a282a1SSwen Schillig
1120bd0072ecSChristof Schmitt wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
1121d5a282a1SSwen Schillig if (!wka_ports)
1122d5a282a1SSwen Schillig return -ENOMEM;
1123d5a282a1SSwen Schillig
1124d5a282a1SSwen Schillig adapter->gs = wka_ports;
1125d5a282a1SSwen Schillig zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
1126d5a282a1SSwen Schillig zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
1127d5a282a1SSwen Schillig zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
1128d5a282a1SSwen Schillig zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
1129d5a282a1SSwen Schillig
1130d5a282a1SSwen Schillig return 0;
1131d5a282a1SSwen Schillig }
1132d5a282a1SSwen Schillig
zfcp_fc_gs_destroy(struct zfcp_adapter * adapter)1133d5a282a1SSwen Schillig void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
1134d5a282a1SSwen Schillig {
1135d5a282a1SSwen Schillig kfree(adapter->gs);
1136d5a282a1SSwen Schillig adapter->gs = NULL;
1137d5a282a1SSwen Schillig }
1138d5a282a1SSwen Schillig
1139