blob: df23bcead23dbc45e8dfa5b4b0833e8ee3fac7e3 [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 Schmitta2fa0ae2009-03-02 13:09:08 +01006 * Copyright IBM Corporation 2008, 2009
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 Schmitt24073b42008-06-10 18:20:54 +020012#include "zfcp_ext.h"
13
Christof Schmitte0d7fcb2008-12-19 16:56:58 +010014enum rscn_address_format {
15 RSCN_PORT_ADDRESS = 0x0,
16 RSCN_AREA_ADDRESS = 0x1,
17 RSCN_DOMAIN_ADDRESS = 0x2,
18 RSCN_FABRIC_ADDRESS = 0x3,
19};
20
21static u32 rscn_range_mask[] = {
22 [RSCN_PORT_ADDRESS] = 0xFFFFFF,
23 [RSCN_AREA_ADDRESS] = 0xFFFF00,
24 [RSCN_DOMAIN_ADDRESS] = 0xFF0000,
25 [RSCN_FABRIC_ADDRESS] = 0x000000,
26};
27
Swen Schilligcc8c2822008-06-10 18:21:00 +020028struct gpn_ft_resp_acc {
29 u8 control;
30 u8 port_id[3];
31 u8 reserved[4];
32 u64 wwpn;
33} __attribute__ ((packed));
34
Christof Schmitt39eb7e92008-12-19 16:57:01 +010035#define ZFCP_CT_SIZE_ONE_PAGE (PAGE_SIZE - sizeof(struct ct_hdr))
36#define ZFCP_GPN_FT_ENTRIES (ZFCP_CT_SIZE_ONE_PAGE \
37 / sizeof(struct gpn_ft_resp_acc))
Swen Schilligcc8c2822008-06-10 18:21:00 +020038#define ZFCP_GPN_FT_BUFFERS 4
Christof Schmitt39eb7e92008-12-19 16:57:01 +010039#define ZFCP_GPN_FT_MAX_SIZE (ZFCP_GPN_FT_BUFFERS * PAGE_SIZE \
40 - sizeof(struct ct_hdr))
Swen Schilligcc8c2822008-06-10 18:21:00 +020041#define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
42
43struct ct_iu_gpn_ft_resp {
44 struct ct_hdr header;
45 struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
46} __attribute__ ((packed));
47
48struct zfcp_gpn_ft {
49 struct zfcp_send_ct ct;
50 struct scatterlist sg_req;
51 struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
52};
53
Swen Schillig5ab944f2008-10-01 12:42:17 +020054struct zfcp_fc_ns_handler_data {
55 struct completion done;
56 void (*handler)(unsigned long);
57 unsigned long handler_data;
58};
59
Swen Schillig6f53a2d2009-08-18 15:43:23 +020060static int zfcp_fc_wka_port_get(struct zfcp_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020061{
62 if (mutex_lock_interruptible(&wka_port->mutex))
63 return -ERESTARTSYS;
64
Christof Schmitt1c1cba12008-11-26 18:07:36 +010065 if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
66 wka_port->status == ZFCP_WKA_PORT_CLOSING) {
Swen Schillig5ab944f2008-10-01 12:42:17 +020067 wka_port->status = ZFCP_WKA_PORT_OPENING;
68 if (zfcp_fsf_open_wka_port(wka_port))
69 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
70 }
71
72 mutex_unlock(&wka_port->mutex);
73
Swen Schillig27f492c2009-07-13 15:06:13 +020074 wait_event(wka_port->completion_wq,
75 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
76 wka_port->status == ZFCP_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +020077
78 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
79 atomic_inc(&wka_port->refcount);
80 return 0;
81 }
82 return -EIO;
83}
84
Swen Schillig6f53a2d2009-08-18 15:43:23 +020085static void zfcp_fc_wka_port_offline(struct work_struct *work)
Swen Schillig5ab944f2008-10-01 12:42:17 +020086{
Jean Delvarebf6aede2009-04-02 16:56:54 -070087 struct delayed_work *dw = to_delayed_work(work);
Swen Schillig5ab944f2008-10-01 12:42:17 +020088 struct zfcp_wka_port *wka_port =
89 container_of(dw, struct zfcp_wka_port, work);
90
Swen Schillig5ab944f2008-10-01 12:42:17 +020091 mutex_lock(&wka_port->mutex);
92 if ((atomic_read(&wka_port->refcount) != 0) ||
93 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
94 goto out;
95
96 wka_port->status = ZFCP_WKA_PORT_CLOSING;
97 if (zfcp_fsf_close_wka_port(wka_port)) {
98 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
99 wake_up(&wka_port->completion_wq);
100 }
101out:
102 mutex_unlock(&wka_port->mutex);
103}
104
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200105static void zfcp_fc_wka_port_put(struct zfcp_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200106{
107 if (atomic_dec_return(&wka_port->refcount) != 0)
108 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200109 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200110 schedule_delayed_work(&wka_port->work, HZ / 100);
111}
112
Sven Schuetz9d544f22009-04-06 18:31:47 +0200113static void zfcp_fc_wka_port_init(struct zfcp_wka_port *wka_port, u32 d_id,
114 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200115{
Swen Schillig5ab944f2008-10-01 12:42:17 +0200116 init_waitqueue_head(&wka_port->completion_wq);
117
118 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200119 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200120
121 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
122 atomic_set(&wka_port->refcount, 0);
123 mutex_init(&wka_port->mutex);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200124 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200125}
126
Christof Schmitt55c770f2009-08-18 15:43:12 +0200127static void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
Swen Schillig828bc122009-04-17 15:08:05 +0200128{
129 cancel_delayed_work_sync(&wka->work);
130 mutex_lock(&wka->mutex);
131 wka->status = ZFCP_WKA_PORT_OFFLINE;
132 mutex_unlock(&wka->mutex);
133}
134
Christof Schmitt55c770f2009-08-18 15:43:12 +0200135void zfcp_fc_wka_ports_force_offline(struct zfcp_wka_ports *gs)
136{
137 zfcp_fc_wka_port_force_offline(&gs->ms);
138 zfcp_fc_wka_port_force_offline(&gs->ts);
139 zfcp_fc_wka_port_force_offline(&gs->ds);
140 zfcp_fc_wka_port_force_offline(&gs->as);
141 zfcp_fc_wka_port_force_offline(&gs->ks);
142}
143
Christof Schmitt24073b42008-06-10 18:20:54 +0200144static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
145 struct fcp_rscn_element *elem)
146{
147 unsigned long flags;
148 struct zfcp_port *port;
149
150 read_lock_irqsave(&zfcp_data.config_lock, flags);
Swen Schilligea460a82009-05-15 13:18:20 +0200151 list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
Swen Schillig24095492009-03-02 13:09:07 +0100152 if ((port->d_id & range) == (elem->nport_did & range))
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200153 zfcp_fc_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200154 if (!port->d_id)
155 zfcp_erp_port_reopen(port,
156 ZFCP_STATUS_COMMON_ERP_FAILED,
157 "fcrscn1", NULL);
158 }
Swen Schillig24095492009-03-02 13:09:07 +0100159
Christof Schmitt24073b42008-06-10 18:20:54 +0200160 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
161}
162
163static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
164{
165 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
166 struct fcp_rscn_head *fcp_rscn_head;
167 struct fcp_rscn_element *fcp_rscn_element;
168 u16 i;
169 u16 no_entries;
170 u32 range_mask;
171
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200172 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
173 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200174
175 /* see FC-FS */
176 no_entries = fcp_rscn_head->payload_len /
177 sizeof(struct fcp_rscn_element);
178
179 for (i = 1; i < no_entries; i++) {
180 /* skip head and start with 1st element */
181 fcp_rscn_element++;
Christof Schmitte0d7fcb2008-12-19 16:56:58 +0100182 range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
Christof Schmitt24073b42008-06-10 18:20:54 +0200183 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
184 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200185 schedule_work(&fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200186}
187
Swen Schillig7ba58c92008-10-01 12:42:18 +0200188static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200189{
190 struct zfcp_adapter *adapter = req->adapter;
191 struct zfcp_port *port;
192 unsigned long flags;
193
194 read_lock_irqsave(&zfcp_data.config_lock, flags);
195 list_for_each_entry(port, &adapter->port_list_head, list)
196 if (port->wwpn == wwpn)
197 break;
198 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
199
200 if (port && (port->wwpn == wwpn))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100201 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200202}
203
204static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
205{
206 struct fsf_status_read_buffer *status_buffer =
207 (struct fsf_status_read_buffer *)req->data;
208 struct fsf_plogi *els_plogi =
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200209 (struct fsf_plogi *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200210
211 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
212}
213
214static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
215{
216 struct fsf_status_read_buffer *status_buffer =
217 (struct fsf_status_read_buffer *)req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200218 struct fcp_logo *els_logo =
219 (struct fcp_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200220
221 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
222}
223
224/**
225 * zfcp_fc_incoming_els - handle incoming ELS
226 * @fsf_req - request which contains incoming ELS
227 */
228void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
229{
230 struct fsf_status_read_buffer *status_buffer =
231 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200232 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200233
Swen Schillig57717102009-08-18 15:43:21 +0200234 zfcp_dbf_san_incoming_els(fsf_req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200235 if (els_type == LS_PLOGI)
236 zfcp_fc_incoming_plogi(fsf_req);
237 else if (els_type == LS_LOGO)
238 zfcp_fc_incoming_logo(fsf_req);
239 else if (els_type == LS_RSCN)
240 zfcp_fc_incoming_rscn(fsf_req);
241}
242
Swen Schillig5ab944f2008-10-01 12:42:17 +0200243static void zfcp_fc_ns_handler(unsigned long data)
244{
245 struct zfcp_fc_ns_handler_data *compl_rec =
246 (struct zfcp_fc_ns_handler_data *) data;
247
248 if (compl_rec->handler)
249 compl_rec->handler(compl_rec->handler_data);
250
251 complete(&compl_rec->done);
252}
253
254static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200255{
256 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
257 struct zfcp_send_ct *ct = &gid_pn->ct;
258 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
259 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
260 struct zfcp_port *port = gid_pn->port;
261
262 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200263 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100264 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200265 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100266
Christof Schmitt24073b42008-06-10 18:20:54 +0200267 /* paranoia */
268 if (ct_iu_req->wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200269 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200270 /* looks like a valid d_id */
271 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
Christof Schmitt24073b42008-06-10 18:20:54 +0200272}
273
Christof Schmitt799b76d2009-08-18 15:43:20 +0200274static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
Swen Schillig5ab944f2008-10-01 12:42:17 +0200275 struct zfcp_gid_pn_data *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200276{
Christof Schmitt799b76d2009-08-18 15:43:20 +0200277 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200278 struct zfcp_fc_ns_handler_data compl_rec;
279 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200280
281 /* setup parameters for send generic command */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200282 gid_pn->port = port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200283 gid_pn->ct.wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200284 gid_pn->ct.handler = zfcp_fc_ns_handler;
285 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
Christof Schmitt24073b42008-06-10 18:20:54 +0200286 gid_pn->ct.req = &gid_pn->req;
287 gid_pn->ct.resp = &gid_pn->resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200288 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
289 sizeof(struct ct_iu_gid_pn_req));
290 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
291 sizeof(struct ct_iu_gid_pn_resp));
292
293 /* setup nameserver request */
294 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
295 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
296 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
297 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
298 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100299 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200300 gid_pn->ct_iu_req.wwpn = port->wwpn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200301
Swen Schillig5ab944f2008-10-01 12:42:17 +0200302 init_completion(&compl_rec.done);
303 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
304 compl_rec.handler_data = (unsigned long) gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200305 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.gid_pn_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200306 if (!ret)
307 wait_for_completion(&compl_rec.done);
308 return ret;
309}
310
311/**
312 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
Christof Schmitt799b76d2009-08-18 15:43:20 +0200313 * @port: port where GID_PN request is needed
Swen Schillig5ab944f2008-10-01 12:42:17 +0200314 * return: -ENOMEM on error, 0 otherwise
315 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200316static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200317{
318 int ret;
319 struct zfcp_gid_pn_data *gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200320 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200321
Swen Schilliga4623c42009-08-18 15:43:15 +0200322 gid_pn = mempool_alloc(adapter->pool.gid_pn_data, GFP_ATOMIC);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200323 if (!gid_pn)
324 return -ENOMEM;
325
326 memset(gid_pn, 0, sizeof(*gid_pn));
327
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200328 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200329 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200330 goto out;
331
Christof Schmitt799b76d2009-08-18 15:43:20 +0200332 ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200333
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200334 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200335out:
Swen Schilliga4623c42009-08-18 15:43:15 +0200336 mempool_free(gid_pn, adapter->pool.gid_pn_data);
Christof Schmitt24073b42008-06-10 18:20:54 +0200337 return ret;
338}
339
Christof Schmitt799b76d2009-08-18 15:43:20 +0200340void zfcp_fc_port_did_lookup(struct work_struct *work)
341{
342 int ret;
343 struct zfcp_port *port = container_of(work, struct zfcp_port,
344 gid_pn_work);
345
346 ret = zfcp_fc_ns_gid_pn(port);
347 if (ret) {
348 /* could not issue gid_pn for some reason */
349 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
350 goto out;
351 }
352
353 if (!port->d_id) {
354 zfcp_erp_port_failed(port, "fcgpn_2", NULL);
355 goto out;
356 }
357
358 zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
359out:
360 zfcp_port_put(port);
361}
362
Christof Schmitt24073b42008-06-10 18:20:54 +0200363/**
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200364 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
365 * @port: The zfcp_port to lookup the d_id for.
366 */
367void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
368{
369 zfcp_port_get(port);
370 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
371 zfcp_port_put(port);
372}
373
374/**
Christof Schmitt24073b42008-06-10 18:20:54 +0200375 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
376 * @port: zfcp_port structure
377 * @plogi: plogi payload
378 *
379 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
380 */
381void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
382{
383 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
384 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
385 if (plogi->serv_param.class1_serv_param[0] & 0x80)
386 port->supported_classes |= FC_COS_CLASS1;
387 if (plogi->serv_param.class2_serv_param[0] & 0x80)
388 port->supported_classes |= FC_COS_CLASS2;
389 if (plogi->serv_param.class3_serv_param[0] & 0x80)
390 port->supported_classes |= FC_COS_CLASS3;
391 if (plogi->serv_param.class4_serv_param[0] & 0x80)
392 port->supported_classes |= FC_COS_CLASS4;
393}
394
395struct zfcp_els_adisc {
396 struct zfcp_send_els els;
397 struct scatterlist req;
398 struct scatterlist resp;
399 struct zfcp_ls_adisc ls_adisc;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200400 struct zfcp_ls_adisc ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200401};
402
403static void zfcp_fc_adisc_handler(unsigned long data)
404{
405 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
406 struct zfcp_port *port = adisc->els.port;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200407 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200408
Christof Schmitt3968ce82008-07-02 10:56:32 +0200409 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200410 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200411 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
412 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200413 goto out;
414 }
415
416 if (!port->wwnn)
417 port->wwnn = ls_adisc->wwnn;
418
Swen Schillig24095492009-03-02 13:09:07 +0100419 if ((port->wwpn != ls_adisc->wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100420 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100421 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
422 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100423 goto out;
424 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200425
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100426 /* port is good, unblock rport without going through erp */
427 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200428 out:
Christof Schmitt14e242e2009-08-18 15:43:11 +0200429 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitt24073b42008-06-10 18:20:54 +0200430 zfcp_port_put(port);
431 kfree(adisc);
432}
433
434static int zfcp_fc_adisc(struct zfcp_port *port)
435{
436 struct zfcp_els_adisc *adisc;
437 struct zfcp_adapter *adapter = port->adapter;
438
439 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
440 if (!adisc)
441 return -ENOMEM;
442
443 adisc->els.req = &adisc->req;
444 adisc->els.resp = &adisc->resp;
445 sg_init_one(adisc->els.req, &adisc->ls_adisc,
446 sizeof(struct zfcp_ls_adisc));
447 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
Swen Schillig44cc76f2008-10-01 12:42:16 +0200448 sizeof(struct zfcp_ls_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200449
Christof Schmitt24073b42008-06-10 18:20:54 +0200450 adisc->els.adapter = adapter;
451 adisc->els.port = port;
452 adisc->els.d_id = port->d_id;
453 adisc->els.handler = zfcp_fc_adisc_handler;
454 adisc->els.handler_data = (unsigned long) adisc;
455 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
456
457 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
458 without FC-AL-2 capability, so we don't set it */
459 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
460 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
461 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
462
463 return zfcp_fsf_send_els(&adisc->els);
464}
465
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100466void zfcp_fc_link_test_work(struct work_struct *work)
467{
468 struct zfcp_port *port =
469 container_of(work, struct zfcp_port, test_link_work);
470 int retval;
471
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100472 zfcp_port_get(port);
473 port->rport_task = RPORT_DEL;
474 zfcp_scsi_rport_work(&port->rport_work);
475
Christof Schmitt14e242e2009-08-18 15:43:11 +0200476 /* only issue one test command at one time per port */
477 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
478 goto out;
479
480 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
481
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100482 retval = zfcp_fc_adisc(port);
483 if (retval == 0)
484 return;
485
486 /* send of ADISC was not possible */
Christof Schmitt14e242e2009-08-18 15:43:11 +0200487 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100488 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
489
Christof Schmitt14e242e2009-08-18 15:43:11 +0200490out:
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100491 zfcp_port_put(port);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100492}
493
Christof Schmitt24073b42008-06-10 18:20:54 +0200494/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200495 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200496 * @port: port to be tested
497 *
498 * Test status of a link to a remote port using the ELS command ADISC.
499 * If there is a problem with the remote port, error recovery steps
500 * will be triggered.
501 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200502void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200503{
Christof Schmitt24073b42008-06-10 18:20:54 +0200504 zfcp_port_get(port);
Swen Schillig45446832009-08-18 15:43:17 +0200505 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100506 zfcp_port_put(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200507}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200508
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100509static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200510{
511 struct scatterlist *sg = &gpn_ft->sg_req;
512
Swen Schilliga4623c42009-08-18 15:43:15 +0200513 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100514 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200515
516 kfree(gpn_ft);
517}
518
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100519static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200520{
521 struct zfcp_gpn_ft *gpn_ft;
522 struct ct_iu_gpn_ft_req *req;
523
524 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
525 if (!gpn_ft)
526 return NULL;
527
Swen Schilliga4623c42009-08-18 15:43:15 +0200528 req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200529 if (!req) {
530 kfree(gpn_ft);
531 gpn_ft = NULL;
532 goto out;
533 }
534 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
535
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100536 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
537 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200538 gpn_ft = NULL;
539 }
540out:
541 return gpn_ft;
542}
543
544
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200545static int zfcp_fc_send_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
546 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200547{
548 struct zfcp_send_ct *ct = &gpn_ft->ct;
549 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200550 struct zfcp_fc_ns_handler_data compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200551 int ret;
552
553 /* prepare CT IU for GPN_FT */
554 req->header.revision = ZFCP_CT_REVISION;
555 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
556 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
557 req->header.options = ZFCP_CT_SYNCHRONOUS;
558 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100559 req->header.max_res_size = max_bytes / 4;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200560 req->flags = 0;
561 req->domain_id_scope = 0;
562 req->area_id_scope = 0;
563 req->fc4_type = ZFCP_CT_SCSI_FCP;
564
565 /* prepare zfcp_send_ct */
Sven Schuetz9d544f22009-04-06 18:31:47 +0200566 ct->wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200567 ct->handler = zfcp_fc_ns_handler;
568 ct->handler_data = (unsigned long)&compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200569 ct->req = &gpn_ft->sg_req;
570 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200571
Swen Schillig5ab944f2008-10-01 12:42:17 +0200572 init_completion(&compl_rec.done);
573 compl_rec.handler = NULL;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200574 ret = zfcp_fsf_send_ct(ct, NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200575 if (!ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200576 wait_for_completion(&compl_rec.done);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200577 return ret;
578}
579
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200580static void zfcp_fc_validate_port(struct zfcp_port *port)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200581{
582 struct zfcp_adapter *adapter = port->adapter;
583
Martin Petermann6ab35c02009-04-17 15:08:13 +0200584 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
585 return;
586
Swen Schilligcc8c2822008-06-10 18:21:00 +0200587 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
588
Christof Schmitt04062892008-10-01 12:42:20 +0200589 if ((port->supported_classes != 0) ||
590 !list_empty(&port->unit_list_head)) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200591 zfcp_port_put(port);
592 return;
593 }
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100594 zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200595 zfcp_erp_wait(adapter);
596 zfcp_port_put(port);
597 zfcp_port_dequeue(port);
598}
599
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200600static int zfcp_fc_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200601{
602 struct zfcp_send_ct *ct = &gpn_ft->ct;
603 struct scatterlist *sg = gpn_ft->sg_resp;
604 struct ct_hdr *hdr = sg_virt(sg);
605 struct gpn_ft_resp_acc *acc = sg_virt(sg);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200606 struct zfcp_adapter *adapter = ct->wka_port->adapter;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200607 struct zfcp_port *port, *tmp;
608 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200609 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200610
611 if (ct->status)
612 return -EIO;
613
614 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
615 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
616 return -EAGAIN; /* might be a temporary condition */
617 return -EIO;
618 }
619
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100620 if (hdr->max_res_size) {
621 dev_warn(&adapter->ccw_device->dev,
622 "The name server reported %d words residual data\n",
623 hdr->max_res_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200624 return -E2BIG;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100625 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200626
Christof Schmitt24680de2009-08-18 15:43:28 +0200627 mutex_lock(&zfcp_data.config_mutex);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200628
629 /* first entry is the header */
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100630 for (x = 1; x < max_entries && !last; x++) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200631 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
632 acc++;
633 else
634 acc = sg_virt(++sg);
635
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200636 last = acc->control & 0x80;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200637 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
638 acc->port_id[2];
639
Swen Schillig5ab944f2008-10-01 12:42:17 +0200640 /* don't attach ports with a well known address */
641 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
642 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200643 /* skip the adapter's port and known remote ports */
Swen Schillig95285392008-08-21 13:43:35 +0200644 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200645 continue;
Swen Schillig95285392008-08-21 13:43:35 +0200646 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
Martin Petermann6ab35c02009-04-17 15:08:13 +0200647 if (port)
Swen Schillig95285392008-08-21 13:43:35 +0200648 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200649
650 port = zfcp_port_enqueue(adapter, acc->wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200651 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schillig317e6b62008-07-02 10:56:37 +0200652 if (IS_ERR(port))
653 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200654 else
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100655 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200656 }
657
658 zfcp_erp_wait(adapter);
659 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200660 zfcp_fc_validate_port(port);
Christof Schmitt24680de2009-08-18 15:43:28 +0200661 mutex_unlock(&zfcp_data.config_mutex);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200662 return ret;
663}
664
665/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200666 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schilligcc8c2822008-06-10 18:21:00 +0200667 * @adapter: pointer to struct zfcp_adapter
668 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200669int zfcp_fc_scan_ports(struct zfcp_adapter *adapter)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200670{
671 int ret, i;
672 struct zfcp_gpn_ft *gpn_ft;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100673 int chain, max_entries, buf_num, max_bytes;
674
675 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
676 buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
677 max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
678 max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200679
Swen Schillig306b6ed2009-04-17 15:08:02 +0200680 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
681 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200682 return 0;
683
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200684 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200685 if (ret)
686 return ret;
687
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100688 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200689 if (!gpn_ft) {
690 ret = -ENOMEM;
691 goto out;
692 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200693
694 for (i = 0; i < 3; i++) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200695 ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200696 if (!ret) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200697 ret = zfcp_fc_eval_gpn_ft(gpn_ft, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200698 if (ret == -EAGAIN)
699 ssleep(1);
700 else
701 break;
702 }
703 }
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100704 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200705out:
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200706 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200707 return ret;
708}
709
710
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200711void _zfcp_fc_scan_ports_later(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200712{
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200713 zfcp_fc_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
Swen Schilligcc8c2822008-06-10 18:21:00 +0200714}
Sven Schuetz9d544f22009-04-06 18:31:47 +0200715
716struct zfcp_els_fc_job {
717 struct zfcp_send_els els;
718 struct fc_bsg_job *job;
719};
720
721static void zfcp_fc_generic_els_handler(unsigned long data)
722{
723 struct zfcp_els_fc_job *els_fc_job = (struct zfcp_els_fc_job *) data;
724 struct fc_bsg_job *job = els_fc_job->job;
725 struct fc_bsg_reply *reply = job->reply;
726
727 if (els_fc_job->els.status) {
728 /* request rejected or timed out */
729 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_REJECT;
730 goto out;
731 }
732
733 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200734 reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200735
736out:
737 job->state_flags = FC_RQST_STATE_DONE;
738 job->job_done(job);
739 kfree(els_fc_job);
740}
741
742int zfcp_fc_execute_els_fc_job(struct fc_bsg_job *job)
743{
744 struct zfcp_els_fc_job *els_fc_job;
745 struct fc_rport *rport = job->rport;
746 struct Scsi_Host *shost;
747 struct zfcp_adapter *adapter;
748 struct zfcp_port *port;
749 u8 *port_did;
750
751 shost = rport ? rport_to_shost(rport) : job->shost;
752 adapter = (struct zfcp_adapter *)shost->hostdata[0];
753
754 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
755 return -EINVAL;
756
757 els_fc_job = kzalloc(sizeof(struct zfcp_els_fc_job), GFP_KERNEL);
758 if (!els_fc_job)
759 return -ENOMEM;
760
761 els_fc_job->els.adapter = adapter;
762 if (rport) {
763 read_lock_irq(&zfcp_data.config_lock);
Swen Schilligea945ff2009-08-18 15:43:24 +0200764 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200765 if (port)
Christof Schmittdc577d52009-05-15 13:18:22 +0200766 els_fc_job->els.d_id = port->d_id;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200767 read_unlock_irq(&zfcp_data.config_lock);
768 if (!port) {
769 kfree(els_fc_job);
770 return -EINVAL;
771 }
Sven Schuetz9d544f22009-04-06 18:31:47 +0200772 } else {
773 port_did = job->request->rqst_data.h_els.port_id;
774 els_fc_job->els.d_id = (port_did[0] << 16) +
775 (port_did[1] << 8) + port_did[2];
776 }
777
778 els_fc_job->els.req = job->request_payload.sg_list;
779 els_fc_job->els.resp = job->reply_payload.sg_list;
780 els_fc_job->els.handler = zfcp_fc_generic_els_handler;
781 els_fc_job->els.handler_data = (unsigned long) els_fc_job;
782 els_fc_job->job = job;
783
784 return zfcp_fsf_send_els(&els_fc_job->els);
785}
786
787struct zfcp_ct_fc_job {
788 struct zfcp_send_ct ct;
789 struct fc_bsg_job *job;
790};
791
792static void zfcp_fc_generic_ct_handler(unsigned long data)
793{
794 struct zfcp_ct_fc_job *ct_fc_job = (struct zfcp_ct_fc_job *) data;
795 struct fc_bsg_job *job = ct_fc_job->job;
796
797 job->reply->reply_data.ctels_reply.status = ct_fc_job->ct.status ?
798 FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200799 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200800 job->state_flags = FC_RQST_STATE_DONE;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200801 job->job_done(job);
802
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200803 zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200804
805 kfree(ct_fc_job);
806}
807
808int zfcp_fc_execute_ct_fc_job(struct fc_bsg_job *job)
809{
810 int ret;
811 u8 gs_type;
812 struct fc_rport *rport = job->rport;
813 struct Scsi_Host *shost;
814 struct zfcp_adapter *adapter;
815 struct zfcp_ct_fc_job *ct_fc_job;
816 u32 preamble_word1;
817
818 shost = rport ? rport_to_shost(rport) : job->shost;
819
820 adapter = (struct zfcp_adapter *)shost->hostdata[0];
821 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
822 return -EINVAL;
823
824 ct_fc_job = kzalloc(sizeof(struct zfcp_ct_fc_job), GFP_KERNEL);
825 if (!ct_fc_job)
826 return -ENOMEM;
827
828 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
829 gs_type = (preamble_word1 & 0xff000000) >> 24;
830
831 switch (gs_type) {
832 case FC_FST_ALIAS:
833 ct_fc_job->ct.wka_port = &adapter->gs->as;
834 break;
835 case FC_FST_MGMT:
836 ct_fc_job->ct.wka_port = &adapter->gs->ms;
837 break;
838 case FC_FST_TIME:
839 ct_fc_job->ct.wka_port = &adapter->gs->ts;
840 break;
841 case FC_FST_DIR:
842 ct_fc_job->ct.wka_port = &adapter->gs->ds;
843 break;
844 default:
845 kfree(ct_fc_job);
846 return -EINVAL; /* no such service */
847 }
848
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200849 ret = zfcp_fc_wka_port_get(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200850 if (ret) {
851 kfree(ct_fc_job);
852 return ret;
853 }
854
855 ct_fc_job->ct.req = job->request_payload.sg_list;
856 ct_fc_job->ct.resp = job->reply_payload.sg_list;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200857 ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
858 ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
859 ct_fc_job->ct.completion = NULL;
860 ct_fc_job->job = job;
861
Christof Schmitt799b76d2009-08-18 15:43:20 +0200862 ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200863 if (ret) {
864 kfree(ct_fc_job);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200865 zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200866 }
867 return ret;
868}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200869
870int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
871{
872 struct zfcp_wka_ports *wka_ports;
873
874 wka_ports = kzalloc(sizeof(struct zfcp_wka_ports), GFP_KERNEL);
875 if (!wka_ports)
876 return -ENOMEM;
877
878 adapter->gs = wka_ports;
879 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
880 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
881 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
882 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
883 zfcp_fc_wka_port_init(&wka_ports->ks, FC_FID_SEC_KEY, adapter);
884
885 return 0;
886}
887
888void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
889{
890 kfree(adapter->gs);
891 adapter->gs = NULL;
892}
893