blob: 2a1cbb74b99b624cb7bec855b16caba61dd7cf0f [file] [log] [blame]
Christof Schmitt24073b42008-06-10 18:20:54 +02001/*
2 * zfcp device driver
3 *
4 * Fibre Channel related functions for the zfcp device driver.
5 *
Christof Schmitt615f59e2010-02-17 11:18:56 +01006 * Copyright IBM Corporation 2008, 2010
Christof Schmitt24073b42008-06-10 18:20:54 +02007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Christof Schmitt9d05ce22009-11-24 16:54:09 +010012#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Christof Schmitt9d05ce22009-11-24 16:54:09 +010014#include <scsi/fc/fc_els.h>
15#include <scsi/libfc.h>
Christof Schmitt24073b42008-06-10 18:20:54 +020016#include "zfcp_ext.h"
Christof Schmitt9d05ce22009-11-24 16:54:09 +010017#include "zfcp_fc.h"
Christof Schmitt24073b42008-06-10 18:20:54 +020018
Christof Schmitt9d05ce22009-11-24 16:54:09 +010019static u32 zfcp_fc_rscn_range_mask[] = {
20 [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
21 [ELS_ADDR_FMT_AREA] = 0xFFFF00,
22 [ELS_ADDR_FMT_DOM] = 0xFF0000,
23 [ELS_ADDR_FMT_FAB] = 0x000000,
Christof Schmitte0d7fcb2008-12-19 16:56:58 +010024};
25
Christof Schmittbd0072e2009-11-24 16:54:11 +010026static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020027{
28 if (mutex_lock_interruptible(&wka_port->mutex))
29 return -ERESTARTSYS;
30
Christof Schmittbd0072e2009-11-24 16:54:11 +010031 if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
32 wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
33 wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
Swen Schillig5ab944f2008-10-01 12:42:17 +020034 if (zfcp_fsf_open_wka_port(wka_port))
Christof Schmittbd0072e2009-11-24 16:54:11 +010035 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +020036 }
37
38 mutex_unlock(&wka_port->mutex);
39
Swen Schillig27f492c2009-07-13 15:06:13 +020040 wait_event(wka_port->completion_wq,
Christof Schmittbd0072e2009-11-24 16:54:11 +010041 wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
42 wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +020043
Christof Schmittbd0072e2009-11-24 16:54:11 +010044 if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
Swen Schillig5ab944f2008-10-01 12:42:17 +020045 atomic_inc(&wka_port->refcount);
46 return 0;
47 }
48 return -EIO;
49}
50
Swen Schillig6f53a2d2009-08-18 15:43:23 +020051static void zfcp_fc_wka_port_offline(struct work_struct *work)
Swen Schillig5ab944f2008-10-01 12:42:17 +020052{
Jean Delvarebf6aede2009-04-02 16:56:54 -070053 struct delayed_work *dw = to_delayed_work(work);
Christof Schmittbd0072e2009-11-24 16:54:11 +010054 struct zfcp_fc_wka_port *wka_port =
55 container_of(dw, struct zfcp_fc_wka_port, work);
Swen Schillig5ab944f2008-10-01 12:42:17 +020056
Swen Schillig5ab944f2008-10-01 12:42:17 +020057 mutex_lock(&wka_port->mutex);
58 if ((atomic_read(&wka_port->refcount) != 0) ||
Christof Schmittbd0072e2009-11-24 16:54:11 +010059 (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
Swen Schillig5ab944f2008-10-01 12:42:17 +020060 goto out;
61
Christof Schmittbd0072e2009-11-24 16:54:11 +010062 wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
Swen Schillig5ab944f2008-10-01 12:42:17 +020063 if (zfcp_fsf_close_wka_port(wka_port)) {
Christof Schmittbd0072e2009-11-24 16:54:11 +010064 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +020065 wake_up(&wka_port->completion_wq);
66 }
67out:
68 mutex_unlock(&wka_port->mutex);
69}
70
Christof Schmittbd0072e2009-11-24 16:54:11 +010071static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020072{
73 if (atomic_dec_return(&wka_port->refcount) != 0)
74 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +020075 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +020076 schedule_delayed_work(&wka_port->work, HZ / 100);
77}
78
Christof Schmittbd0072e2009-11-24 16:54:11 +010079static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
Sven Schuetz9d544f22009-04-06 18:31:47 +020080 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +020081{
Swen Schillig5ab944f2008-10-01 12:42:17 +020082 init_waitqueue_head(&wka_port->completion_wq);
83
84 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +020085 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +020086
Christof Schmittbd0072e2009-11-24 16:54:11 +010087 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +020088 atomic_set(&wka_port->refcount, 0);
89 mutex_init(&wka_port->mutex);
Swen Schillig6f53a2d2009-08-18 15:43:23 +020090 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
Swen Schillig5ab944f2008-10-01 12:42:17 +020091}
92
Christof Schmittbd0072e2009-11-24 16:54:11 +010093static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
Swen Schillig828bc122009-04-17 15:08:05 +020094{
95 cancel_delayed_work_sync(&wka->work);
96 mutex_lock(&wka->mutex);
Christof Schmittbd0072e2009-11-24 16:54:11 +010097 wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig828bc122009-04-17 15:08:05 +020098 mutex_unlock(&wka->mutex);
99}
100
Christof Schmittbd0072e2009-11-24 16:54:11 +0100101void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
Christof Schmitt55c770f2009-08-18 15:43:12 +0200102{
Swen Schilligf3450c72009-11-24 16:53:59 +0100103 if (!gs)
104 return;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200105 zfcp_fc_wka_port_force_offline(&gs->ms);
106 zfcp_fc_wka_port_force_offline(&gs->ts);
107 zfcp_fc_wka_port_force_offline(&gs->ds);
108 zfcp_fc_wka_port_force_offline(&gs->as);
Christof Schmitt55c770f2009-08-18 15:43:12 +0200109}
110
Christof Schmitt24073b42008-06-10 18:20:54 +0200111static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100112 struct fc_els_rscn_page *page)
Christof Schmitt24073b42008-06-10 18:20:54 +0200113{
114 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +0100115 struct zfcp_adapter *adapter = fsf_req->adapter;
Christof Schmitt24073b42008-06-10 18:20:54 +0200116 struct zfcp_port *port;
117
Swen Schilligecf0c772009-11-24 16:53:58 +0100118 read_lock_irqsave(&adapter->port_list_lock, flags);
119 list_for_each_entry(port, &adapter->port_list, list) {
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100120 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200121 zfcp_fc_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200122 if (!port->d_id)
123 zfcp_erp_port_reopen(port,
124 ZFCP_STATUS_COMMON_ERP_FAILED,
125 "fcrscn1", NULL);
126 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100127 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200128}
129
130static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
131{
132 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100133 struct fc_els_rscn *head;
134 struct fc_els_rscn_page *page;
Christof Schmitt24073b42008-06-10 18:20:54 +0200135 u16 i;
136 u16 no_entries;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100137 unsigned int afmt;
Christof Schmitt24073b42008-06-10 18:20:54 +0200138
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100139 head = (struct fc_els_rscn *) status_buffer->payload.data;
140 page = (struct fc_els_rscn_page *) head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200141
142 /* see FC-FS */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100143 no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200144
145 for (i = 1; i < no_entries; i++) {
146 /* skip head and start with 1st element */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100147 page++;
148 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
149 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
150 page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200151 }
Swen Schillig9eae07e2009-11-24 16:54:06 +0100152 queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200153}
154
Swen Schillig7ba58c92008-10-01 12:42:18 +0200155static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200156{
Swen Schilligecf0c772009-11-24 16:53:58 +0100157 unsigned long flags;
Christof Schmitt24073b42008-06-10 18:20:54 +0200158 struct zfcp_adapter *adapter = req->adapter;
159 struct zfcp_port *port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200160
Swen Schilligecf0c772009-11-24 16:53:58 +0100161 read_lock_irqsave(&adapter->port_list_lock, flags);
162 list_for_each_entry(port, &adapter->port_list, list)
163 if (port->wwpn == wwpn) {
164 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200165 break;
Swen Schilligecf0c772009-11-24 16:53:58 +0100166 }
167 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200168}
169
170static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
171{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100172 struct fsf_status_read_buffer *status_buffer;
173 struct fc_els_flogi *plogi;
Christof Schmitt24073b42008-06-10 18:20:54 +0200174
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100175 status_buffer = (struct fsf_status_read_buffer *) req->data;
176 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
177 zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200178}
179
180static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
181{
182 struct fsf_status_read_buffer *status_buffer =
183 (struct fsf_status_read_buffer *)req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100184 struct fc_els_logo *logo =
185 (struct fc_els_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200186
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100187 zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200188}
189
190/**
191 * zfcp_fc_incoming_els - handle incoming ELS
192 * @fsf_req - request which contains incoming ELS
193 */
194void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
195{
196 struct fsf_status_read_buffer *status_buffer =
197 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200198 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200199
Swen Schillig57717102009-08-18 15:43:21 +0200200 zfcp_dbf_san_incoming_els(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100201 if (els_type == ELS_PLOGI)
Christof Schmitt24073b42008-06-10 18:20:54 +0200202 zfcp_fc_incoming_plogi(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100203 else if (els_type == ELS_LOGO)
Christof Schmitt24073b42008-06-10 18:20:54 +0200204 zfcp_fc_incoming_logo(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100205 else if (els_type == ELS_RSCN)
Christof Schmitt24073b42008-06-10 18:20:54 +0200206 zfcp_fc_incoming_rscn(fsf_req);
207}
208
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100209static void zfcp_fc_ns_gid_pn_eval(void *data)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200210{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100211 struct zfcp_fc_gid_pn *gid_pn = data;
212 struct zfcp_fsf_ct_els *ct = &gid_pn->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100213 struct zfcp_fc_gid_pn_req *gid_pn_req = sg_virt(ct->req);
214 struct zfcp_fc_gid_pn_resp *gid_pn_resp = sg_virt(ct->resp);
Christof Schmitt24073b42008-06-10 18:20:54 +0200215 struct zfcp_port *port = gid_pn->port;
216
217 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200218 return;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100219 if (gid_pn_resp->ct_hdr.ct_cmd != FC_FS_ACC)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200220 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100221
Christof Schmitt24073b42008-06-10 18:20:54 +0200222 /* paranoia */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100223 if (gid_pn_req->gid_pn.fn_wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200224 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200225 /* looks like a valid d_id */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100226 port->d_id = ntoh24(gid_pn_resp->gid_pn.fp_fid);
Christof Schmitt24073b42008-06-10 18:20:54 +0200227}
228
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100229static void zfcp_fc_complete(void *data)
230{
231 complete(data);
232}
233
Christof Schmitt799b76d2009-08-18 15:43:20 +0200234static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100235 struct zfcp_fc_gid_pn *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200236{
Christof Schmitt799b76d2009-08-18 15:43:20 +0200237 struct zfcp_adapter *adapter = port->adapter;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100238 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200239 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200240
241 /* setup parameters for send generic command */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200242 gid_pn->port = port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100243 gid_pn->ct.handler = zfcp_fc_complete;
244 gid_pn->ct.handler_data = &completion;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100245 gid_pn->ct.req = &gid_pn->sg_req;
246 gid_pn->ct.resp = &gid_pn->sg_resp;
247 sg_init_one(&gid_pn->sg_req, &gid_pn->gid_pn_req,
248 sizeof(struct zfcp_fc_gid_pn_req));
249 sg_init_one(&gid_pn->sg_resp, &gid_pn->gid_pn_resp,
250 sizeof(struct zfcp_fc_gid_pn_resp));
Christof Schmitt24073b42008-06-10 18:20:54 +0200251
252 /* setup nameserver request */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100253 gid_pn->gid_pn_req.ct_hdr.ct_rev = FC_CT_REV;
254 gid_pn->gid_pn_req.ct_hdr.ct_fs_type = FC_FST_DIR;
255 gid_pn->gid_pn_req.ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
256 gid_pn->gid_pn_req.ct_hdr.ct_options = 0;
257 gid_pn->gid_pn_req.ct_hdr.ct_cmd = FC_NS_GID_PN;
258 gid_pn->gid_pn_req.ct_hdr.ct_mr_size = ZFCP_FC_CT_SIZE_PAGE / 4;
259 gid_pn->gid_pn_req.gid_pn.fn_wwpn = port->wwpn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200260
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100261 ret = zfcp_fsf_send_ct(&adapter->gs->ds, &gid_pn->ct,
Swen Schillig51375ee2010-01-14 17:19:02 +0100262 adapter->pool.gid_pn_req,
263 ZFCP_FC_CTELS_TMO);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100264 if (!ret) {
265 wait_for_completion(&completion);
266 zfcp_fc_ns_gid_pn_eval(gid_pn);
267 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200268 return ret;
269}
270
271/**
272 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
Christof Schmitt799b76d2009-08-18 15:43:20 +0200273 * @port: port where GID_PN request is needed
Swen Schillig5ab944f2008-10-01 12:42:17 +0200274 * return: -ENOMEM on error, 0 otherwise
275 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200276static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200277{
278 int ret;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100279 struct zfcp_fc_gid_pn *gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200280 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200281
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100282 gid_pn = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200283 if (!gid_pn)
284 return -ENOMEM;
285
286 memset(gid_pn, 0, sizeof(*gid_pn));
287
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200288 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200289 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200290 goto out;
291
Christof Schmitt799b76d2009-08-18 15:43:20 +0200292 ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200293
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200294 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200295out:
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100296 mempool_free(gid_pn, adapter->pool.gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200297 return ret;
298}
299
Christof Schmitt799b76d2009-08-18 15:43:20 +0200300void zfcp_fc_port_did_lookup(struct work_struct *work)
301{
302 int ret;
303 struct zfcp_port *port = container_of(work, struct zfcp_port,
304 gid_pn_work);
305
306 ret = zfcp_fc_ns_gid_pn(port);
307 if (ret) {
308 /* could not issue gid_pn for some reason */
309 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
310 goto out;
311 }
312
313 if (!port->d_id) {
314 zfcp_erp_port_failed(port, "fcgpn_2", NULL);
315 goto out;
316 }
317
318 zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
319out:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100320 put_device(&port->dev);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200321}
322
Christof Schmitt24073b42008-06-10 18:20:54 +0200323/**
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200324 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
325 * @port: The zfcp_port to lookup the d_id for.
326 */
327void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
328{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100329 get_device(&port->dev);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200330 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
Christof Schmitt615f59e2010-02-17 11:18:56 +0100331 put_device(&port->dev);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200332}
333
334/**
Christof Schmitt24073b42008-06-10 18:20:54 +0200335 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
336 * @port: zfcp_port structure
337 * @plogi: plogi payload
338 *
339 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
340 */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100341void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
Christof Schmitt24073b42008-06-10 18:20:54 +0200342{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100343 if (plogi->fl_wwpn != port->wwpn) {
344 port->d_id = 0;
345 dev_warn(&port->adapter->ccw_device->dev,
346 "A port opened with WWPN 0x%016Lx returned data that "
347 "identifies it as WWPN 0x%016Lx\n",
348 (unsigned long long) port->wwpn,
349 (unsigned long long) plogi->fl_wwpn);
350 return;
351 }
352
353 port->wwnn = plogi->fl_wwnn;
354 port->maxframe_size = plogi->fl_csp.sp_bb_data;
355
356 if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200357 port->supported_classes |= FC_COS_CLASS1;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100358 if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200359 port->supported_classes |= FC_COS_CLASS2;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100360 if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200361 port->supported_classes |= FC_COS_CLASS3;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100362 if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200363 port->supported_classes |= FC_COS_CLASS4;
364}
365
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100366static void zfcp_fc_adisc_handler(void *data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200367{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100368 struct zfcp_fc_els_adisc *adisc = data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200369 struct zfcp_port *port = adisc->els.port;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100370 struct fc_els_adisc *adisc_resp = &adisc->adisc_resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200371
Christof Schmitt3968ce82008-07-02 10:56:32 +0200372 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200373 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200374 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
375 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200376 goto out;
377 }
378
379 if (!port->wwnn)
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100380 port->wwnn = adisc_resp->adisc_wwnn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200381
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100382 if ((port->wwpn != adisc_resp->adisc_wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100383 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100384 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
385 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100386 goto out;
387 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200388
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100389 /* port is good, unblock rport without going through erp */
390 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200391 out:
Christof Schmitt14e242e2009-08-18 15:43:11 +0200392 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitt615f59e2010-02-17 11:18:56 +0100393 put_device(&port->dev);
Christof Schmittee744622009-11-24 16:54:14 +0100394 kmem_cache_free(zfcp_data.adisc_cache, adisc);
Christof Schmitt24073b42008-06-10 18:20:54 +0200395}
396
397static int zfcp_fc_adisc(struct zfcp_port *port)
398{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100399 struct zfcp_fc_els_adisc *adisc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200400 struct zfcp_adapter *adapter = port->adapter;
Christof Schmittee744622009-11-24 16:54:14 +0100401 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200402
Christof Schmittee744622009-11-24 16:54:14 +0100403 adisc = kmem_cache_alloc(zfcp_data.adisc_cache, GFP_ATOMIC);
Christof Schmitt24073b42008-06-10 18:20:54 +0200404 if (!adisc)
405 return -ENOMEM;
406
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100407 adisc->els.port = port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200408 adisc->els.req = &adisc->req;
409 adisc->els.resp = &adisc->resp;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100410 sg_init_one(adisc->els.req, &adisc->adisc_req,
411 sizeof(struct fc_els_adisc));
412 sg_init_one(adisc->els.resp, &adisc->adisc_resp,
413 sizeof(struct fc_els_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200414
Christof Schmitt24073b42008-06-10 18:20:54 +0200415 adisc->els.handler = zfcp_fc_adisc_handler;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100416 adisc->els.handler_data = adisc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200417
418 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
419 without FC-AL-2 capability, so we don't set it */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100420 adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
421 adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100422 adisc->adisc_req.adisc_cmd = ELS_ADISC;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100423 hton24(adisc->adisc_req.adisc_port_id,
424 fc_host_port_id(adapter->scsi_host));
Christof Schmitt24073b42008-06-10 18:20:54 +0200425
Swen Schillig51375ee2010-01-14 17:19:02 +0100426 ret = zfcp_fsf_send_els(adapter, port->d_id, &adisc->els,
427 ZFCP_FC_CTELS_TMO);
Christof Schmittee744622009-11-24 16:54:14 +0100428 if (ret)
429 kmem_cache_free(zfcp_data.adisc_cache, adisc);
430
431 return ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200432}
433
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100434void zfcp_fc_link_test_work(struct work_struct *work)
435{
436 struct zfcp_port *port =
437 container_of(work, struct zfcp_port, test_link_work);
438 int retval;
439
Christof Schmitt615f59e2010-02-17 11:18:56 +0100440 get_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100441 port->rport_task = RPORT_DEL;
442 zfcp_scsi_rport_work(&port->rport_work);
443
Christof Schmitt14e242e2009-08-18 15:43:11 +0200444 /* only issue one test command at one time per port */
445 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
446 goto out;
447
448 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
449
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100450 retval = zfcp_fc_adisc(port);
451 if (retval == 0)
452 return;
453
454 /* send of ADISC was not possible */
Christof Schmitt14e242e2009-08-18 15:43:11 +0200455 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100456 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
457
Christof Schmitt14e242e2009-08-18 15:43:11 +0200458out:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100459 put_device(&port->dev);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100460}
461
Christof Schmitt24073b42008-06-10 18:20:54 +0200462/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200463 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200464 * @port: port to be tested
465 *
466 * Test status of a link to a remote port using the ELS command ADISC.
467 * If there is a problem with the remote port, error recovery steps
468 * will be triggered.
469 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200470void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200471{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100472 get_device(&port->dev);
Swen Schillig45446832009-08-18 15:43:17 +0200473 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Christof Schmitt615f59e2010-02-17 11:18:56 +0100474 put_device(&port->dev);
Christof Schmitt24073b42008-06-10 18:20:54 +0200475}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200476
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100477static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200478{
479 struct scatterlist *sg = &gpn_ft->sg_req;
480
Swen Schilliga4623c42009-08-18 15:43:15 +0200481 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100482 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200483
484 kfree(gpn_ft);
485}
486
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100487static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200488{
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100489 struct zfcp_fc_gpn_ft *gpn_ft;
490 struct zfcp_fc_gpn_ft_req *req;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200491
492 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
493 if (!gpn_ft)
494 return NULL;
495
Swen Schilliga4623c42009-08-18 15:43:15 +0200496 req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200497 if (!req) {
498 kfree(gpn_ft);
499 gpn_ft = NULL;
500 goto out;
501 }
502 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
503
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100504 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
505 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200506 gpn_ft = NULL;
507 }
508out:
509 return gpn_ft;
510}
511
512
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100513static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200514 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200515{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100516 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100517 struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100518 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200519 int ret;
520
521 /* prepare CT IU for GPN_FT */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100522 req->ct_hdr.ct_rev = FC_CT_REV;
523 req->ct_hdr.ct_fs_type = FC_FST_DIR;
524 req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
525 req->ct_hdr.ct_options = 0;
526 req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
527 req->ct_hdr.ct_mr_size = max_bytes / 4;
528 req->gpn_ft.fn_domain_id_scope = 0;
529 req->gpn_ft.fn_area_id_scope = 0;
530 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200531
532 /* prepare zfcp_send_ct */
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100533 ct->handler = zfcp_fc_complete;
534 ct->handler_data = &completion;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200535 ct->req = &gpn_ft->sg_req;
536 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200537
Swen Schillig51375ee2010-01-14 17:19:02 +0100538 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct, NULL,
539 ZFCP_FC_CTELS_TMO);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200540 if (!ret)
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100541 wait_for_completion(&completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200542 return ret;
543}
544
Swen Schilligf3450c72009-11-24 16:53:59 +0100545static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200546{
Martin Petermann6ab35c02009-04-17 15:08:13 +0200547 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
548 return;
549
Swen Schilligcc8c2822008-06-10 18:21:00 +0200550 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
551
Christof Schmitt04062892008-10-01 12:42:20 +0200552 if ((port->supported_classes != 0) ||
Swen Schilligf3450c72009-11-24 16:53:59 +0100553 !list_empty(&port->unit_list))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200554 return;
Swen Schilligf3450c72009-11-24 16:53:59 +0100555
Swen Schilligf3450c72009-11-24 16:53:59 +0100556 list_move_tail(&port->list, lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200557}
558
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100559static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
560 struct zfcp_adapter *adapter, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200561{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100562 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200563 struct scatterlist *sg = gpn_ft->sg_resp;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100564 struct fc_ct_hdr *hdr = sg_virt(sg);
565 struct fc_gpn_ft_resp *acc = sg_virt(sg);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200566 struct zfcp_port *port, *tmp;
Swen Schilligecf0c772009-11-24 16:53:58 +0100567 unsigned long flags;
Swen Schilligf3450c72009-11-24 16:53:59 +0100568 LIST_HEAD(remove_lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200569 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200570 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200571
572 if (ct->status)
573 return -EIO;
574
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100575 if (hdr->ct_cmd != FC_FS_ACC) {
576 if (hdr->ct_reason == FC_BA_RJT_UNABLE)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200577 return -EAGAIN; /* might be a temporary condition */
578 return -EIO;
579 }
580
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100581 if (hdr->ct_mr_size) {
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100582 dev_warn(&adapter->ccw_device->dev,
583 "The name server reported %d words residual data\n",
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100584 hdr->ct_mr_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200585 return -E2BIG;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100586 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200587
Swen Schilligcc8c2822008-06-10 18:21:00 +0200588 /* first entry is the header */
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100589 for (x = 1; x < max_entries && !last; x++) {
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100590 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200591 acc++;
592 else
593 acc = sg_virt(++sg);
594
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100595 last = acc->fp_flags & FC_NS_FID_LAST;
596 d_id = ntoh24(acc->fp_fid);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200597
Swen Schillig5ab944f2008-10-01 12:42:17 +0200598 /* don't attach ports with a well known address */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100599 if (d_id >= FC_FID_WELL_KNOWN_BASE)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200600 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200601 /* skip the adapter's port and known remote ports */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100602 if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200603 continue;
604
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100605 port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200606 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100607 if (!IS_ERR(port))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100608 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligecf0c772009-11-24 16:53:58 +0100609 else if (PTR_ERR(port) != -EEXIST)
610 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200611 }
612
613 zfcp_erp_wait(adapter);
Swen Schilligecf0c772009-11-24 16:53:58 +0100614 write_lock_irqsave(&adapter->port_list_lock, flags);
615 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100616 zfcp_fc_validate_port(port, &remove_lh);
Swen Schilligecf0c772009-11-24 16:53:58 +0100617 write_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schilligf3450c72009-11-24 16:53:59 +0100618
619 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
620 zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
Christof Schmitt615f59e2010-02-17 11:18:56 +0100621 zfcp_device_unregister(&port->dev, &zfcp_sysfs_port_attrs);
Swen Schilligf3450c72009-11-24 16:53:59 +0100622 }
623
Swen Schilligcc8c2822008-06-10 18:21:00 +0200624 return ret;
625}
626
627/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200628 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schillig9eae07e2009-11-24 16:54:06 +0100629 * @work: reference to scheduled work
Swen Schilligcc8c2822008-06-10 18:21:00 +0200630 */
Swen Schillig9eae07e2009-11-24 16:54:06 +0100631void zfcp_fc_scan_ports(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200632{
Swen Schillig9eae07e2009-11-24 16:54:06 +0100633 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
634 scan_work);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200635 int ret, i;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100636 struct zfcp_fc_gpn_ft *gpn_ft;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100637 int chain, max_entries, buf_num, max_bytes;
638
639 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100640 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
641 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
642 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200643
Swen Schillig306b6ed2009-04-17 15:08:02 +0200644 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
645 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schillig9eae07e2009-11-24 16:54:06 +0100646 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200647
Swen Schillig9eae07e2009-11-24 16:54:06 +0100648 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
649 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200650
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100651 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig9eae07e2009-11-24 16:54:06 +0100652 if (!gpn_ft)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200653 goto out;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200654
655 for (i = 0; i < 3; i++) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200656 ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200657 if (!ret) {
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100658 ret = zfcp_fc_eval_gpn_ft(gpn_ft, adapter, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200659 if (ret == -EAGAIN)
660 ssleep(1);
661 else
662 break;
663 }
664 }
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100665 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200666out:
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200667 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200668}
669
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100670static void zfcp_fc_ct_els_job_handler(void *data)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200671{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100672 struct fc_bsg_job *job = data;
673 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
Swen Schillig7dec9cf2010-01-26 17:49:19 +0100674 struct fc_bsg_reply *jr = job->reply;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200675
Swen Schillig7dec9cf2010-01-26 17:49:19 +0100676 jr->reply_payload_rcv_len = job->reply_payload.payload_len;
677 jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
678 jr->result = zfcp_ct_els->status ? -EIO : 0;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200679 job->job_done(job);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200680}
681
Christof Schmittf09d5452010-01-13 17:52:36 +0100682static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct fc_bsg_job *job)
683{
684 u32 preamble_word1;
685 u8 gs_type;
686 struct zfcp_adapter *adapter;
687
688 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
689 gs_type = (preamble_word1 & 0xff000000) >> 24;
690
691 adapter = (struct zfcp_adapter *) job->shost->hostdata[0];
692
693 switch (gs_type) {
694 case FC_FST_ALIAS:
695 return &adapter->gs->as;
696 case FC_FST_MGMT:
697 return &adapter->gs->ms;
698 case FC_FST_TIME:
699 return &adapter->gs->ts;
700 break;
701 case FC_FST_DIR:
702 return &adapter->gs->ds;
703 break;
704 default:
705 return NULL;
706 }
707}
708
709static void zfcp_fc_ct_job_handler(void *data)
710{
711 struct fc_bsg_job *job = data;
712 struct zfcp_fc_wka_port *wka_port;
713
714 wka_port = zfcp_fc_job_wka_port(job);
715 zfcp_fc_wka_port_put(wka_port);
716
717 zfcp_fc_ct_els_job_handler(data);
718}
719
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100720static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
721 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200722{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100723 struct zfcp_fsf_ct_els *els = job->dd_data;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200724 struct fc_rport *rport = job->rport;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200725 struct zfcp_port *port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100726 u32 d_id;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200727
Sven Schuetz9d544f22009-04-06 18:31:47 +0200728 if (rport) {
Swen Schilligea945ff2009-08-18 15:43:24 +0200729 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100730 if (!port)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200731 return -EINVAL;
Swen Schilligecf0c772009-11-24 16:53:58 +0100732
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100733 d_id = port->d_id;
Christof Schmitt615f59e2010-02-17 11:18:56 +0100734 put_device(&port->dev);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100735 } else
736 d_id = ntoh24(job->request->rqst_data.h_els.port_id);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200737
Christof Schmittf09d5452010-01-13 17:52:36 +0100738 els->handler = zfcp_fc_ct_els_job_handler;
Swen Schillig51375ee2010-01-14 17:19:02 +0100739 return zfcp_fsf_send_els(adapter, d_id, els, job->req->timeout / HZ);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200740}
741
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100742static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
743 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200744{
745 int ret;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100746 struct zfcp_fsf_ct_els *ct = job->dd_data;
747 struct zfcp_fc_wka_port *wka_port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200748
Christof Schmittf09d5452010-01-13 17:52:36 +0100749 wka_port = zfcp_fc_job_wka_port(job);
750 if (!wka_port)
751 return -EINVAL;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200752
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100753 ret = zfcp_fc_wka_port_get(wka_port);
754 if (ret)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200755 return ret;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200756
Christof Schmittf09d5452010-01-13 17:52:36 +0100757 ct->handler = zfcp_fc_ct_job_handler;
Swen Schillig51375ee2010-01-14 17:19:02 +0100758 ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->req->timeout / HZ);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100759 if (ret)
760 zfcp_fc_wka_port_put(wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200761
Sven Schuetz9d544f22009-04-06 18:31:47 +0200762 return ret;
763}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200764
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100765int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
766{
767 struct Scsi_Host *shost;
768 struct zfcp_adapter *adapter;
769 struct zfcp_fsf_ct_els *ct_els = job->dd_data;
770
771 shost = job->rport ? rport_to_shost(job->rport) : job->shost;
772 adapter = (struct zfcp_adapter *)shost->hostdata[0];
773
774 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
775 return -EINVAL;
776
777 ct_els->req = job->request_payload.sg_list;
778 ct_els->resp = job->reply_payload.sg_list;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100779 ct_els->handler_data = job;
780
781 switch (job->request->msgcode) {
782 case FC_BSG_RPT_ELS:
783 case FC_BSG_HST_ELS_NOLOGIN:
784 return zfcp_fc_exec_els_job(job, adapter);
785 case FC_BSG_RPT_CT:
786 case FC_BSG_HST_CT:
787 return zfcp_fc_exec_ct_job(job, adapter);
788 default:
789 return -EINVAL;
790 }
791}
792
Swen Schillig491ca442010-01-14 17:19:01 +0100793int zfcp_fc_timeout_bsg_job(struct fc_bsg_job *job)
794{
795 /* hardware tracks timeout, reset bsg timeout to not interfere */
796 return -EAGAIN;
797}
798
Swen Schilligd5a282a2009-08-18 15:43:22 +0200799int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
800{
Christof Schmittbd0072e2009-11-24 16:54:11 +0100801 struct zfcp_fc_wka_ports *wka_ports;
Swen Schilligd5a282a2009-08-18 15:43:22 +0200802
Christof Schmittbd0072e2009-11-24 16:54:11 +0100803 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200804 if (!wka_ports)
805 return -ENOMEM;
806
807 adapter->gs = wka_ports;
808 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
809 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
810 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
811 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200812
813 return 0;
814}
815
816void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
817{
818 kfree(adapter->gs);
819 adapter->gs = NULL;
820}
821