blob: e03410043cd720ad825fdf75b8589e7323695c4e [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 Schmitt9d05ce22009-11-24 16:54:09 +010012#include <linux/types.h>
13#include <scsi/fc/fc_els.h>
14#include <scsi/libfc.h>
Christof Schmitt24073b42008-06-10 18:20:54 +020015#include "zfcp_ext.h"
Christof Schmitt9d05ce22009-11-24 16:54:09 +010016#include "zfcp_fc.h"
Christof Schmitt24073b42008-06-10 18:20:54 +020017
Christof Schmitt9d05ce22009-11-24 16:54:09 +010018static u32 zfcp_fc_rscn_range_mask[] = {
19 [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
20 [ELS_ADDR_FMT_AREA] = 0xFFFF00,
21 [ELS_ADDR_FMT_DOM] = 0xFF0000,
22 [ELS_ADDR_FMT_FAB] = 0x000000,
Christof Schmitte0d7fcb2008-12-19 16:56:58 +010023};
24
Swen Schilligcc8c2822008-06-10 18:21:00 +020025struct gpn_ft_resp_acc {
26 u8 control;
27 u8 port_id[3];
28 u8 reserved[4];
29 u64 wwpn;
30} __attribute__ ((packed));
31
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +010032#define ZFCP_CT_SIZE_ONE_PAGE (PAGE_SIZE - sizeof(struct ct_hdr))
33#define ZFCP_GPN_FT_ENTRIES (ZFCP_CT_SIZE_ONE_PAGE \
34 / sizeof(struct gpn_ft_resp_acc))
Swen Schilligcc8c2822008-06-10 18:21:00 +020035#define ZFCP_GPN_FT_BUFFERS 4
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +010036#define ZFCP_GPN_FT_MAX_SIZE (ZFCP_GPN_FT_BUFFERS * PAGE_SIZE \
37 - sizeof(struct ct_hdr))
Swen Schilligcc8c2822008-06-10 18:21:00 +020038#define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
39
40struct ct_iu_gpn_ft_resp {
41 struct ct_hdr header;
42 struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
43} __attribute__ ((packed));
44
45struct zfcp_gpn_ft {
46 struct zfcp_send_ct ct;
47 struct scatterlist sg_req;
48 struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
49};
50
Swen Schillig5ab944f2008-10-01 12:42:17 +020051struct zfcp_fc_ns_handler_data {
52 struct completion done;
53 void (*handler)(unsigned long);
54 unsigned long handler_data;
55};
56
Swen Schillig6f53a2d2009-08-18 15:43:23 +020057static int zfcp_fc_wka_port_get(struct zfcp_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020058{
59 if (mutex_lock_interruptible(&wka_port->mutex))
60 return -ERESTARTSYS;
61
Christof Schmitt1c1cba12008-11-26 18:07:36 +010062 if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
63 wka_port->status == ZFCP_WKA_PORT_CLOSING) {
Swen Schillig5ab944f2008-10-01 12:42:17 +020064 wka_port->status = ZFCP_WKA_PORT_OPENING;
65 if (zfcp_fsf_open_wka_port(wka_port))
66 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
67 }
68
69 mutex_unlock(&wka_port->mutex);
70
Swen Schillig27f492c2009-07-13 15:06:13 +020071 wait_event(wka_port->completion_wq,
72 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
73 wka_port->status == ZFCP_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +020074
75 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
76 atomic_inc(&wka_port->refcount);
77 return 0;
78 }
79 return -EIO;
80}
81
Swen Schillig6f53a2d2009-08-18 15:43:23 +020082static void zfcp_fc_wka_port_offline(struct work_struct *work)
Swen Schillig5ab944f2008-10-01 12:42:17 +020083{
Jean Delvarebf6aede2009-04-02 16:56:54 -070084 struct delayed_work *dw = to_delayed_work(work);
Swen Schillig5ab944f2008-10-01 12:42:17 +020085 struct zfcp_wka_port *wka_port =
86 container_of(dw, struct zfcp_wka_port, work);
87
Swen Schillig5ab944f2008-10-01 12:42:17 +020088 mutex_lock(&wka_port->mutex);
89 if ((atomic_read(&wka_port->refcount) != 0) ||
90 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
91 goto out;
92
93 wka_port->status = ZFCP_WKA_PORT_CLOSING;
94 if (zfcp_fsf_close_wka_port(wka_port)) {
95 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
96 wake_up(&wka_port->completion_wq);
97 }
98out:
99 mutex_unlock(&wka_port->mutex);
100}
101
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200102static void zfcp_fc_wka_port_put(struct zfcp_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200103{
104 if (atomic_dec_return(&wka_port->refcount) != 0)
105 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200106 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200107 schedule_delayed_work(&wka_port->work, HZ / 100);
108}
109
Sven Schuetz9d544f22009-04-06 18:31:47 +0200110static void zfcp_fc_wka_port_init(struct zfcp_wka_port *wka_port, u32 d_id,
111 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200112{
Swen Schillig5ab944f2008-10-01 12:42:17 +0200113 init_waitqueue_head(&wka_port->completion_wq);
114
115 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200116 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200117
118 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
119 atomic_set(&wka_port->refcount, 0);
120 mutex_init(&wka_port->mutex);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200121 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200122}
123
Christof Schmitt55c770f2009-08-18 15:43:12 +0200124static void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
Swen Schillig828bc122009-04-17 15:08:05 +0200125{
126 cancel_delayed_work_sync(&wka->work);
127 mutex_lock(&wka->mutex);
128 wka->status = ZFCP_WKA_PORT_OFFLINE;
129 mutex_unlock(&wka->mutex);
130}
131
Christof Schmitt55c770f2009-08-18 15:43:12 +0200132void zfcp_fc_wka_ports_force_offline(struct zfcp_wka_ports *gs)
133{
Swen Schilligf3450c72009-11-24 16:53:59 +0100134 if (!gs)
135 return;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200136 zfcp_fc_wka_port_force_offline(&gs->ms);
137 zfcp_fc_wka_port_force_offline(&gs->ts);
138 zfcp_fc_wka_port_force_offline(&gs->ds);
139 zfcp_fc_wka_port_force_offline(&gs->as);
140 zfcp_fc_wka_port_force_offline(&gs->ks);
141}
142
Christof Schmitt24073b42008-06-10 18:20:54 +0200143static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100144 struct fc_els_rscn_page *page)
Christof Schmitt24073b42008-06-10 18:20:54 +0200145{
146 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +0100147 struct zfcp_adapter *adapter = fsf_req->adapter;
Christof Schmitt24073b42008-06-10 18:20:54 +0200148 struct zfcp_port *port;
149
Swen Schilligecf0c772009-11-24 16:53:58 +0100150 read_lock_irqsave(&adapter->port_list_lock, flags);
151 list_for_each_entry(port, &adapter->port_list, list) {
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100152 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & 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 Schilligecf0c772009-11-24 16:53:58 +0100159 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200160}
161
162static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
163{
164 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100165 struct fc_els_rscn *head;
166 struct fc_els_rscn_page *page;
Christof Schmitt24073b42008-06-10 18:20:54 +0200167 u16 i;
168 u16 no_entries;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100169 unsigned int afmt;
Christof Schmitt24073b42008-06-10 18:20:54 +0200170
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100171 head = (struct fc_els_rscn *) status_buffer->payload.data;
172 page = (struct fc_els_rscn_page *) head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200173
174 /* see FC-FS */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100175 no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200176
177 for (i = 1; i < no_entries; i++) {
178 /* skip head and start with 1st element */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100179 page++;
180 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
181 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
182 page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200183 }
Swen Schillig9eae07e2009-11-24 16:54:06 +0100184 queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200185}
186
Swen Schillig7ba58c92008-10-01 12:42:18 +0200187static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200188{
Swen Schilligecf0c772009-11-24 16:53:58 +0100189 unsigned long flags;
Christof Schmitt24073b42008-06-10 18:20:54 +0200190 struct zfcp_adapter *adapter = req->adapter;
191 struct zfcp_port *port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200192
Swen Schilligecf0c772009-11-24 16:53:58 +0100193 read_lock_irqsave(&adapter->port_list_lock, flags);
194 list_for_each_entry(port, &adapter->port_list, list)
195 if (port->wwpn == wwpn) {
196 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200197 break;
Swen Schilligecf0c772009-11-24 16:53:58 +0100198 }
199 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200200}
201
202static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
203{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100204 struct fsf_status_read_buffer *status_buffer;
205 struct fc_els_flogi *plogi;
Christof Schmitt24073b42008-06-10 18:20:54 +0200206
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100207 status_buffer = (struct fsf_status_read_buffer *) req->data;
208 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
209 zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200210}
211
212static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
213{
214 struct fsf_status_read_buffer *status_buffer =
215 (struct fsf_status_read_buffer *)req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100216 struct fc_els_logo *logo =
217 (struct fc_els_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200218
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100219 zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200220}
221
222/**
223 * zfcp_fc_incoming_els - handle incoming ELS
224 * @fsf_req - request which contains incoming ELS
225 */
226void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
227{
228 struct fsf_status_read_buffer *status_buffer =
229 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200230 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200231
Swen Schillig57717102009-08-18 15:43:21 +0200232 zfcp_dbf_san_incoming_els(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100233 if (els_type == ELS_PLOGI)
Christof Schmitt24073b42008-06-10 18:20:54 +0200234 zfcp_fc_incoming_plogi(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100235 else if (els_type == ELS_LOGO)
Christof Schmitt24073b42008-06-10 18:20:54 +0200236 zfcp_fc_incoming_logo(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100237 else if (els_type == ELS_RSCN)
Christof Schmitt24073b42008-06-10 18:20:54 +0200238 zfcp_fc_incoming_rscn(fsf_req);
239}
240
Swen Schillig5ab944f2008-10-01 12:42:17 +0200241static void zfcp_fc_ns_handler(unsigned long data)
242{
243 struct zfcp_fc_ns_handler_data *compl_rec =
244 (struct zfcp_fc_ns_handler_data *) data;
245
246 if (compl_rec->handler)
247 compl_rec->handler(compl_rec->handler_data);
248
249 complete(&compl_rec->done);
250}
251
252static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200253{
254 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
255 struct zfcp_send_ct *ct = &gid_pn->ct;
256 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
257 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
258 struct zfcp_port *port = gid_pn->port;
259
260 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200261 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100262 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200263 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100264
Christof Schmitt24073b42008-06-10 18:20:54 +0200265 /* paranoia */
266 if (ct_iu_req->wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200267 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200268 /* looks like a valid d_id */
269 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
Christof Schmitt24073b42008-06-10 18:20:54 +0200270}
271
Christof Schmitt799b76d2009-08-18 15:43:20 +0200272static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
Swen Schillig5ab944f2008-10-01 12:42:17 +0200273 struct zfcp_gid_pn_data *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200274{
Christof Schmitt799b76d2009-08-18 15:43:20 +0200275 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200276 struct zfcp_fc_ns_handler_data compl_rec;
277 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200278
279 /* setup parameters for send generic command */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200280 gid_pn->port = port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200281 gid_pn->ct.wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200282 gid_pn->ct.handler = zfcp_fc_ns_handler;
283 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
Christof Schmitt24073b42008-06-10 18:20:54 +0200284 gid_pn->ct.req = &gid_pn->req;
285 gid_pn->ct.resp = &gid_pn->resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200286 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
287 sizeof(struct ct_iu_gid_pn_req));
288 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
289 sizeof(struct ct_iu_gid_pn_resp));
290
291 /* setup nameserver request */
292 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
293 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
294 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
295 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
296 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100297 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200298 gid_pn->ct_iu_req.wwpn = port->wwpn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200299
Swen Schillig5ab944f2008-10-01 12:42:17 +0200300 init_completion(&compl_rec.done);
301 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
302 compl_rec.handler_data = (unsigned long) gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200303 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.gid_pn_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200304 if (!ret)
305 wait_for_completion(&compl_rec.done);
306 return ret;
307}
308
309/**
310 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
Christof Schmitt799b76d2009-08-18 15:43:20 +0200311 * @port: port where GID_PN request is needed
Swen Schillig5ab944f2008-10-01 12:42:17 +0200312 * return: -ENOMEM on error, 0 otherwise
313 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200314static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200315{
316 int ret;
317 struct zfcp_gid_pn_data *gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200318 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200319
Swen Schilliga4623c42009-08-18 15:43:15 +0200320 gid_pn = mempool_alloc(adapter->pool.gid_pn_data, GFP_ATOMIC);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200321 if (!gid_pn)
322 return -ENOMEM;
323
324 memset(gid_pn, 0, sizeof(*gid_pn));
325
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200326 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200327 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200328 goto out;
329
Christof Schmitt799b76d2009-08-18 15:43:20 +0200330 ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200331
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200332 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200333out:
Swen Schilliga4623c42009-08-18 15:43:15 +0200334 mempool_free(gid_pn, adapter->pool.gid_pn_data);
Christof Schmitt24073b42008-06-10 18:20:54 +0200335 return ret;
336}
337
Christof Schmitt799b76d2009-08-18 15:43:20 +0200338void zfcp_fc_port_did_lookup(struct work_struct *work)
339{
340 int ret;
341 struct zfcp_port *port = container_of(work, struct zfcp_port,
342 gid_pn_work);
343
344 ret = zfcp_fc_ns_gid_pn(port);
345 if (ret) {
346 /* could not issue gid_pn for some reason */
347 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
348 goto out;
349 }
350
351 if (!port->d_id) {
352 zfcp_erp_port_failed(port, "fcgpn_2", NULL);
353 goto out;
354 }
355
356 zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
357out:
Swen Schilligf3450c72009-11-24 16:53:59 +0100358 put_device(&port->sysfs_device);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200359}
360
Christof Schmitt24073b42008-06-10 18:20:54 +0200361/**
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200362 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
363 * @port: The zfcp_port to lookup the d_id for.
364 */
365void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
366{
Swen Schilligf3450c72009-11-24 16:53:59 +0100367 get_device(&port->sysfs_device);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200368 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
Swen Schilligf3450c72009-11-24 16:53:59 +0100369 put_device(&port->sysfs_device);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200370}
371
372/**
Christof Schmitt24073b42008-06-10 18:20:54 +0200373 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
374 * @port: zfcp_port structure
375 * @plogi: plogi payload
376 *
377 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
378 */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100379void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
Christof Schmitt24073b42008-06-10 18:20:54 +0200380{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100381 if (plogi->fl_wwpn != port->wwpn) {
382 port->d_id = 0;
383 dev_warn(&port->adapter->ccw_device->dev,
384 "A port opened with WWPN 0x%016Lx returned data that "
385 "identifies it as WWPN 0x%016Lx\n",
386 (unsigned long long) port->wwpn,
387 (unsigned long long) plogi->fl_wwpn);
388 return;
389 }
390
391 port->wwnn = plogi->fl_wwnn;
392 port->maxframe_size = plogi->fl_csp.sp_bb_data;
393
394 if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200395 port->supported_classes |= FC_COS_CLASS1;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100396 if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200397 port->supported_classes |= FC_COS_CLASS2;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100398 if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200399 port->supported_classes |= FC_COS_CLASS3;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100400 if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200401 port->supported_classes |= FC_COS_CLASS4;
402}
403
Christof Schmitt24073b42008-06-10 18:20:54 +0200404static void zfcp_fc_adisc_handler(unsigned long data)
405{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100406 struct zfcp_fc_els_adisc *adisc = (struct zfcp_fc_els_adisc *) data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200407 struct zfcp_port *port = adisc->els.port;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100408 struct fc_els_adisc *adisc_resp = &adisc->adisc_resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200409
Christof Schmitt3968ce82008-07-02 10:56:32 +0200410 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200411 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200412 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
413 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200414 goto out;
415 }
416
417 if (!port->wwnn)
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100418 port->wwnn = adisc_resp->adisc_wwnn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200419
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100420 if ((port->wwpn != adisc_resp->adisc_wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100421 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100422 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
423 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100424 goto out;
425 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200426
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100427 /* port is good, unblock rport without going through erp */
428 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200429 out:
Christof Schmitt14e242e2009-08-18 15:43:11 +0200430 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Swen Schilligf3450c72009-11-24 16:53:59 +0100431 put_device(&port->sysfs_device);
Christof Schmitt24073b42008-06-10 18:20:54 +0200432 kfree(adisc);
433}
434
435static int zfcp_fc_adisc(struct zfcp_port *port)
436{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100437 struct zfcp_fc_els_adisc *adisc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200438 struct zfcp_adapter *adapter = port->adapter;
439
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100440 adisc = kzalloc(sizeof(struct zfcp_fc_els_adisc), GFP_ATOMIC);
Christof Schmitt24073b42008-06-10 18:20:54 +0200441 if (!adisc)
442 return -ENOMEM;
443
444 adisc->els.req = &adisc->req;
445 adisc->els.resp = &adisc->resp;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100446 sg_init_one(adisc->els.req, &adisc->adisc_req,
447 sizeof(struct fc_els_adisc));
448 sg_init_one(adisc->els.resp, &adisc->adisc_resp,
449 sizeof(struct fc_els_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200450
Christof Schmitt24073b42008-06-10 18:20:54 +0200451 adisc->els.adapter = adapter;
452 adisc->els.port = port;
453 adisc->els.d_id = port->d_id;
454 adisc->els.handler = zfcp_fc_adisc_handler;
455 adisc->els.handler_data = (unsigned long) adisc;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100456 adisc->els.ls_code = adisc->adisc_req.adisc_cmd = ELS_ADISC;
Christof Schmitt24073b42008-06-10 18:20:54 +0200457
458 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
459 without FC-AL-2 capability, so we don't set it */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100460 adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
461 adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
462 hton24(adisc->adisc_req.adisc_port_id,
463 fc_host_port_id(adapter->scsi_host));
Christof Schmitt24073b42008-06-10 18:20:54 +0200464
465 return zfcp_fsf_send_els(&adisc->els);
466}
467
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100468void zfcp_fc_link_test_work(struct work_struct *work)
469{
470 struct zfcp_port *port =
471 container_of(work, struct zfcp_port, test_link_work);
472 int retval;
473
Swen Schilligf3450c72009-11-24 16:53:59 +0100474 get_device(&port->sysfs_device);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100475 port->rport_task = RPORT_DEL;
476 zfcp_scsi_rport_work(&port->rport_work);
477
Christof Schmitt14e242e2009-08-18 15:43:11 +0200478 /* only issue one test command at one time per port */
479 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
480 goto out;
481
482 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
483
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100484 retval = zfcp_fc_adisc(port);
485 if (retval == 0)
486 return;
487
488 /* send of ADISC was not possible */
Christof Schmitt14e242e2009-08-18 15:43:11 +0200489 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100490 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
491
Christof Schmitt14e242e2009-08-18 15:43:11 +0200492out:
Swen Schilligf3450c72009-11-24 16:53:59 +0100493 put_device(&port->sysfs_device);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100494}
495
Christof Schmitt24073b42008-06-10 18:20:54 +0200496/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200497 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200498 * @port: port to be tested
499 *
500 * Test status of a link to a remote port using the ELS command ADISC.
501 * If there is a problem with the remote port, error recovery steps
502 * will be triggered.
503 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200504void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200505{
Swen Schilligf3450c72009-11-24 16:53:59 +0100506 get_device(&port->sysfs_device);
Swen Schillig45446832009-08-18 15:43:17 +0200507 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Swen Schilligf3450c72009-11-24 16:53:59 +0100508 put_device(&port->sysfs_device);
Christof Schmitt24073b42008-06-10 18:20:54 +0200509}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200510
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100511static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200512{
513 struct scatterlist *sg = &gpn_ft->sg_req;
514
Swen Schilliga4623c42009-08-18 15:43:15 +0200515 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100516 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200517
518 kfree(gpn_ft);
519}
520
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100521static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200522{
523 struct zfcp_gpn_ft *gpn_ft;
524 struct ct_iu_gpn_ft_req *req;
525
526 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
527 if (!gpn_ft)
528 return NULL;
529
Swen Schilliga4623c42009-08-18 15:43:15 +0200530 req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200531 if (!req) {
532 kfree(gpn_ft);
533 gpn_ft = NULL;
534 goto out;
535 }
536 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
537
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100538 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
539 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200540 gpn_ft = NULL;
541 }
542out:
543 return gpn_ft;
544}
545
546
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200547static int zfcp_fc_send_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
548 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200549{
550 struct zfcp_send_ct *ct = &gpn_ft->ct;
551 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200552 struct zfcp_fc_ns_handler_data compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200553 int ret;
554
555 /* prepare CT IU for GPN_FT */
556 req->header.revision = ZFCP_CT_REVISION;
557 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
558 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
559 req->header.options = ZFCP_CT_SYNCHRONOUS;
560 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100561 req->header.max_res_size = max_bytes / 4;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200562 req->flags = 0;
563 req->domain_id_scope = 0;
564 req->area_id_scope = 0;
565 req->fc4_type = ZFCP_CT_SCSI_FCP;
566
567 /* prepare zfcp_send_ct */
Sven Schuetz9d544f22009-04-06 18:31:47 +0200568 ct->wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200569 ct->handler = zfcp_fc_ns_handler;
570 ct->handler_data = (unsigned long)&compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200571 ct->req = &gpn_ft->sg_req;
572 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200573
Swen Schillig5ab944f2008-10-01 12:42:17 +0200574 init_completion(&compl_rec.done);
575 compl_rec.handler = NULL;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200576 ret = zfcp_fsf_send_ct(ct, NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200577 if (!ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200578 wait_for_completion(&compl_rec.done);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200579 return ret;
580}
581
Swen Schilligf3450c72009-11-24 16:53:59 +0100582static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200583{
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) ||
Swen Schilligf3450c72009-11-24 16:53:59 +0100590 !list_empty(&port->unit_list))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200591 return;
Swen Schilligf3450c72009-11-24 16:53:59 +0100592
Swen Schilligf3450c72009-11-24 16:53:59 +0100593 list_move_tail(&port->list, lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200594}
595
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200596static int zfcp_fc_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200597{
598 struct zfcp_send_ct *ct = &gpn_ft->ct;
599 struct scatterlist *sg = gpn_ft->sg_resp;
600 struct ct_hdr *hdr = sg_virt(sg);
601 struct gpn_ft_resp_acc *acc = sg_virt(sg);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200602 struct zfcp_adapter *adapter = ct->wka_port->adapter;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200603 struct zfcp_port *port, *tmp;
Swen Schilligecf0c772009-11-24 16:53:58 +0100604 unsigned long flags;
Swen Schilligf3450c72009-11-24 16:53:59 +0100605 LIST_HEAD(remove_lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200606 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200607 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200608
609 if (ct->status)
610 return -EIO;
611
612 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
613 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
614 return -EAGAIN; /* might be a temporary condition */
615 return -EIO;
616 }
617
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100618 if (hdr->max_res_size) {
619 dev_warn(&adapter->ccw_device->dev,
620 "The name server reported %d words residual data\n",
621 hdr->max_res_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200622 return -E2BIG;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100623 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200624
Swen Schilligcc8c2822008-06-10 18:21:00 +0200625 /* first entry is the header */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100626 for (x = 1; x < max_entries && !last; x++) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200627 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
628 acc++;
629 else
630 acc = sg_virt(++sg);
631
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200632 last = acc->control & 0x80;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200633 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
634 acc->port_id[2];
635
Swen Schillig5ab944f2008-10-01 12:42:17 +0200636 /* don't attach ports with a well known address */
637 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
638 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200639 /* skip the adapter's port and known remote ports */
Swen Schillig95285392008-08-21 13:43:35 +0200640 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200641 continue;
642
643 port = zfcp_port_enqueue(adapter, acc->wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200644 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100645 if (!IS_ERR(port))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100646 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligecf0c772009-11-24 16:53:58 +0100647 else if (PTR_ERR(port) != -EEXIST)
648 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200649 }
650
651 zfcp_erp_wait(adapter);
Swen Schilligecf0c772009-11-24 16:53:58 +0100652 write_lock_irqsave(&adapter->port_list_lock, flags);
653 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100654 zfcp_fc_validate_port(port, &remove_lh);
Swen Schilligecf0c772009-11-24 16:53:58 +0100655 write_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schilligf3450c72009-11-24 16:53:59 +0100656
657 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
658 zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
659 zfcp_device_unregister(&port->sysfs_device,
660 &zfcp_sysfs_port_attrs);
661 }
662
Swen Schilligcc8c2822008-06-10 18:21:00 +0200663 return ret;
664}
665
666/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200667 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schillig9eae07e2009-11-24 16:54:06 +0100668 * @work: reference to scheduled work
Swen Schilligcc8c2822008-06-10 18:21:00 +0200669 */
Swen Schillig9eae07e2009-11-24 16:54:06 +0100670void zfcp_fc_scan_ports(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200671{
Swen Schillig9eae07e2009-11-24 16:54:06 +0100672 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
673 scan_work);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200674 int ret, i;
675 struct zfcp_gpn_ft *gpn_ft;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100676 int chain, max_entries, buf_num, max_bytes;
677
678 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
679 buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
680 max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
681 max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200682
Swen Schillig306b6ed2009-04-17 15:08:02 +0200683 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
684 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schillig9eae07e2009-11-24 16:54:06 +0100685 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200686
Swen Schillig9eae07e2009-11-24 16:54:06 +0100687 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
688 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200689
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100690 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig9eae07e2009-11-24 16:54:06 +0100691 if (!gpn_ft)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200692 goto out;
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 Schmitt39eb7e9a2008-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}
708
709
Sven Schuetz9d544f22009-04-06 18:31:47 +0200710struct zfcp_els_fc_job {
711 struct zfcp_send_els els;
712 struct fc_bsg_job *job;
713};
714
715static void zfcp_fc_generic_els_handler(unsigned long data)
716{
717 struct zfcp_els_fc_job *els_fc_job = (struct zfcp_els_fc_job *) data;
718 struct fc_bsg_job *job = els_fc_job->job;
719 struct fc_bsg_reply *reply = job->reply;
720
721 if (els_fc_job->els.status) {
722 /* request rejected or timed out */
723 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_REJECT;
724 goto out;
725 }
726
727 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200728 reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200729
730out:
731 job->state_flags = FC_RQST_STATE_DONE;
732 job->job_done(job);
733 kfree(els_fc_job);
734}
735
736int zfcp_fc_execute_els_fc_job(struct fc_bsg_job *job)
737{
738 struct zfcp_els_fc_job *els_fc_job;
739 struct fc_rport *rport = job->rport;
740 struct Scsi_Host *shost;
741 struct zfcp_adapter *adapter;
742 struct zfcp_port *port;
743 u8 *port_did;
744
745 shost = rport ? rport_to_shost(rport) : job->shost;
746 adapter = (struct zfcp_adapter *)shost->hostdata[0];
747
748 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
749 return -EINVAL;
750
751 els_fc_job = kzalloc(sizeof(struct zfcp_els_fc_job), GFP_KERNEL);
752 if (!els_fc_job)
753 return -ENOMEM;
754
755 els_fc_job->els.adapter = adapter;
756 if (rport) {
Swen Schilligea945ff2009-08-18 15:43:24 +0200757 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200758 if (!port) {
759 kfree(els_fc_job);
760 return -EINVAL;
761 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100762
763 els_fc_job->els.d_id = port->d_id;
Swen Schilligf3450c72009-11-24 16:53:59 +0100764 put_device(&port->sysfs_device);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200765 } else {
766 port_did = job->request->rqst_data.h_els.port_id;
767 els_fc_job->els.d_id = (port_did[0] << 16) +
768 (port_did[1] << 8) + port_did[2];
769 }
770
771 els_fc_job->els.req = job->request_payload.sg_list;
772 els_fc_job->els.resp = job->reply_payload.sg_list;
773 els_fc_job->els.handler = zfcp_fc_generic_els_handler;
774 els_fc_job->els.handler_data = (unsigned long) els_fc_job;
775 els_fc_job->job = job;
776
777 return zfcp_fsf_send_els(&els_fc_job->els);
778}
779
780struct zfcp_ct_fc_job {
781 struct zfcp_send_ct ct;
782 struct fc_bsg_job *job;
783};
784
785static void zfcp_fc_generic_ct_handler(unsigned long data)
786{
787 struct zfcp_ct_fc_job *ct_fc_job = (struct zfcp_ct_fc_job *) data;
788 struct fc_bsg_job *job = ct_fc_job->job;
789
790 job->reply->reply_data.ctels_reply.status = ct_fc_job->ct.status ?
791 FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200792 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200793 job->state_flags = FC_RQST_STATE_DONE;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200794 job->job_done(job);
795
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200796 zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200797
798 kfree(ct_fc_job);
799}
800
801int zfcp_fc_execute_ct_fc_job(struct fc_bsg_job *job)
802{
803 int ret;
804 u8 gs_type;
805 struct fc_rport *rport = job->rport;
806 struct Scsi_Host *shost;
807 struct zfcp_adapter *adapter;
808 struct zfcp_ct_fc_job *ct_fc_job;
809 u32 preamble_word1;
810
811 shost = rport ? rport_to_shost(rport) : job->shost;
812
813 adapter = (struct zfcp_adapter *)shost->hostdata[0];
814 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
815 return -EINVAL;
816
817 ct_fc_job = kzalloc(sizeof(struct zfcp_ct_fc_job), GFP_KERNEL);
818 if (!ct_fc_job)
819 return -ENOMEM;
820
821 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
822 gs_type = (preamble_word1 & 0xff000000) >> 24;
823
824 switch (gs_type) {
825 case FC_FST_ALIAS:
826 ct_fc_job->ct.wka_port = &adapter->gs->as;
827 break;
828 case FC_FST_MGMT:
829 ct_fc_job->ct.wka_port = &adapter->gs->ms;
830 break;
831 case FC_FST_TIME:
832 ct_fc_job->ct.wka_port = &adapter->gs->ts;
833 break;
834 case FC_FST_DIR:
835 ct_fc_job->ct.wka_port = &adapter->gs->ds;
836 break;
837 default:
838 kfree(ct_fc_job);
839 return -EINVAL; /* no such service */
840 }
841
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200842 ret = zfcp_fc_wka_port_get(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200843 if (ret) {
844 kfree(ct_fc_job);
845 return ret;
846 }
847
848 ct_fc_job->ct.req = job->request_payload.sg_list;
849 ct_fc_job->ct.resp = job->reply_payload.sg_list;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200850 ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
851 ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
852 ct_fc_job->ct.completion = NULL;
853 ct_fc_job->job = job;
854
Christof Schmitt799b76d2009-08-18 15:43:20 +0200855 ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200856 if (ret) {
857 kfree(ct_fc_job);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200858 zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200859 }
860 return ret;
861}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200862
863int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
864{
865 struct zfcp_wka_ports *wka_ports;
866
867 wka_ports = kzalloc(sizeof(struct zfcp_wka_ports), GFP_KERNEL);
868 if (!wka_ports)
869 return -ENOMEM;
870
871 adapter->gs = wka_ports;
872 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
873 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
874 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
875 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
876 zfcp_fc_wka_port_init(&wka_ports->ks, FC_FID_SEC_KEY, adapter);
877
878 return 0;
879}
880
881void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
882{
883 kfree(adapter->gs);
884 adapter->gs = NULL;
885}
886