blob: 47daebfa7e59af2f7b17e91184182199ed7bb26f [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 ct_iu_gpn_ft_req {
29 struct ct_hdr header;
30 u8 flags;
31 u8 domain_id_scope;
32 u8 area_id_scope;
33 u8 fc4_type;
34} __attribute__ ((packed));
35
36struct gpn_ft_resp_acc {
37 u8 control;
38 u8 port_id[3];
39 u8 reserved[4];
40 u64 wwpn;
41} __attribute__ ((packed));
42
Christof Schmitt39eb7e92008-12-19 16:57:01 +010043#define ZFCP_CT_SIZE_ONE_PAGE (PAGE_SIZE - sizeof(struct ct_hdr))
44#define ZFCP_GPN_FT_ENTRIES (ZFCP_CT_SIZE_ONE_PAGE \
45 / sizeof(struct gpn_ft_resp_acc))
Swen Schilligcc8c2822008-06-10 18:21:00 +020046#define ZFCP_GPN_FT_BUFFERS 4
Christof Schmitt39eb7e92008-12-19 16:57:01 +010047#define ZFCP_GPN_FT_MAX_SIZE (ZFCP_GPN_FT_BUFFERS * PAGE_SIZE \
48 - sizeof(struct ct_hdr))
Swen Schilligcc8c2822008-06-10 18:21:00 +020049#define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
50
51struct ct_iu_gpn_ft_resp {
52 struct ct_hdr header;
53 struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
54} __attribute__ ((packed));
55
56struct zfcp_gpn_ft {
57 struct zfcp_send_ct ct;
58 struct scatterlist sg_req;
59 struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
60};
61
Swen Schillig5ab944f2008-10-01 12:42:17 +020062struct zfcp_fc_ns_handler_data {
63 struct completion done;
64 void (*handler)(unsigned long);
65 unsigned long handler_data;
66};
67
68static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
69{
70 if (mutex_lock_interruptible(&wka_port->mutex))
71 return -ERESTARTSYS;
72
Christof Schmitt1c1cba12008-11-26 18:07:36 +010073 if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
74 wka_port->status == ZFCP_WKA_PORT_CLOSING) {
Swen Schillig5ab944f2008-10-01 12:42:17 +020075 wka_port->status = ZFCP_WKA_PORT_OPENING;
76 if (zfcp_fsf_open_wka_port(wka_port))
77 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
78 }
79
80 mutex_unlock(&wka_port->mutex);
81
Swen Schillig27f492c2009-07-13 15:06:13 +020082 wait_event(wka_port->completion_wq,
83 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
84 wka_port->status == ZFCP_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +020085
86 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
87 atomic_inc(&wka_port->refcount);
88 return 0;
89 }
90 return -EIO;
91}
92
93static void zfcp_wka_port_offline(struct work_struct *work)
94{
Jean Delvarebf6aede2009-04-02 16:56:54 -070095 struct delayed_work *dw = to_delayed_work(work);
Swen Schillig5ab944f2008-10-01 12:42:17 +020096 struct zfcp_wka_port *wka_port =
97 container_of(dw, struct zfcp_wka_port, work);
98
Swen Schillig5ab944f2008-10-01 12:42:17 +020099 mutex_lock(&wka_port->mutex);
100 if ((atomic_read(&wka_port->refcount) != 0) ||
101 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
102 goto out;
103
104 wka_port->status = ZFCP_WKA_PORT_CLOSING;
105 if (zfcp_fsf_close_wka_port(wka_port)) {
106 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
107 wake_up(&wka_port->completion_wq);
108 }
109out:
110 mutex_unlock(&wka_port->mutex);
111}
112
113static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
114{
115 if (atomic_dec_return(&wka_port->refcount) != 0)
116 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200117 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200118 schedule_delayed_work(&wka_port->work, HZ / 100);
119}
120
Sven Schuetz9d544f22009-04-06 18:31:47 +0200121static void zfcp_fc_wka_port_init(struct zfcp_wka_port *wka_port, u32 d_id,
122 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200123{
Swen Schillig5ab944f2008-10-01 12:42:17 +0200124 init_waitqueue_head(&wka_port->completion_wq);
125
126 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200127 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200128
129 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
130 atomic_set(&wka_port->refcount, 0);
131 mutex_init(&wka_port->mutex);
132 INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
133}
134
Swen Schillig828bc122009-04-17 15:08:05 +0200135void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
136{
137 cancel_delayed_work_sync(&wka->work);
138 mutex_lock(&wka->mutex);
139 wka->status = ZFCP_WKA_PORT_OFFLINE;
140 mutex_unlock(&wka->mutex);
141}
142
Sven Schuetz9d544f22009-04-06 18:31:47 +0200143void zfcp_fc_wka_ports_init(struct zfcp_adapter *adapter)
144{
145 struct zfcp_wka_ports *gs = adapter->gs;
146
147 zfcp_fc_wka_port_init(&gs->ms, FC_FID_MGMT_SERV, adapter);
148 zfcp_fc_wka_port_init(&gs->ts, FC_FID_TIME_SERV, adapter);
149 zfcp_fc_wka_port_init(&gs->ds, FC_FID_DIR_SERV, adapter);
150 zfcp_fc_wka_port_init(&gs->as, FC_FID_ALIASES, adapter);
151 zfcp_fc_wka_port_init(&gs->ks, FC_FID_SEC_KEY, adapter);
152}
153
Christof Schmitt24073b42008-06-10 18:20:54 +0200154static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
155 struct fcp_rscn_element *elem)
156{
157 unsigned long flags;
158 struct zfcp_port *port;
159
160 read_lock_irqsave(&zfcp_data.config_lock, flags);
Swen Schilligea460a82009-05-15 13:18:20 +0200161 list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
Swen Schillig24095492009-03-02 13:09:07 +0100162 if ((port->d_id & range) == (elem->nport_did & range))
Christof Schmitt24073b42008-06-10 18:20:54 +0200163 zfcp_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200164 if (!port->d_id)
165 zfcp_erp_port_reopen(port,
166 ZFCP_STATUS_COMMON_ERP_FAILED,
167 "fcrscn1", NULL);
168 }
Swen Schillig24095492009-03-02 13:09:07 +0100169
Christof Schmitt24073b42008-06-10 18:20:54 +0200170 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
171}
172
173static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
174{
175 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
176 struct fcp_rscn_head *fcp_rscn_head;
177 struct fcp_rscn_element *fcp_rscn_element;
178 u16 i;
179 u16 no_entries;
180 u32 range_mask;
181
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200182 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
183 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200184
185 /* see FC-FS */
186 no_entries = fcp_rscn_head->payload_len /
187 sizeof(struct fcp_rscn_element);
188
189 for (i = 1; i < no_entries; i++) {
190 /* skip head and start with 1st element */
191 fcp_rscn_element++;
Christof Schmitte0d7fcb2008-12-19 16:56:58 +0100192 range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
Christof Schmitt24073b42008-06-10 18:20:54 +0200193 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
194 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200195 schedule_work(&fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200196}
197
Swen Schillig7ba58c92008-10-01 12:42:18 +0200198static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200199{
200 struct zfcp_adapter *adapter = req->adapter;
201 struct zfcp_port *port;
202 unsigned long flags;
203
204 read_lock_irqsave(&zfcp_data.config_lock, flags);
205 list_for_each_entry(port, &adapter->port_list_head, list)
206 if (port->wwpn == wwpn)
207 break;
208 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
209
210 if (port && (port->wwpn == wwpn))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100211 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200212}
213
214static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
215{
216 struct fsf_status_read_buffer *status_buffer =
217 (struct fsf_status_read_buffer *)req->data;
218 struct fsf_plogi *els_plogi =
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200219 (struct fsf_plogi *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200220
221 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
222}
223
224static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
225{
226 struct fsf_status_read_buffer *status_buffer =
227 (struct fsf_status_read_buffer *)req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200228 struct fcp_logo *els_logo =
229 (struct fcp_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200230
231 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
232}
233
234/**
235 * zfcp_fc_incoming_els - handle incoming ELS
236 * @fsf_req - request which contains incoming ELS
237 */
238void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
239{
240 struct fsf_status_read_buffer *status_buffer =
241 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200242 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200243
244 zfcp_san_dbf_event_incoming_els(fsf_req);
245 if (els_type == LS_PLOGI)
246 zfcp_fc_incoming_plogi(fsf_req);
247 else if (els_type == LS_LOGO)
248 zfcp_fc_incoming_logo(fsf_req);
249 else if (els_type == LS_RSCN)
250 zfcp_fc_incoming_rscn(fsf_req);
251}
252
Swen Schillig5ab944f2008-10-01 12:42:17 +0200253static void zfcp_fc_ns_handler(unsigned long data)
254{
255 struct zfcp_fc_ns_handler_data *compl_rec =
256 (struct zfcp_fc_ns_handler_data *) data;
257
258 if (compl_rec->handler)
259 compl_rec->handler(compl_rec->handler_data);
260
261 complete(&compl_rec->done);
262}
263
264static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200265{
266 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
267 struct zfcp_send_ct *ct = &gid_pn->ct;
268 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
269 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
270 struct zfcp_port *port = gid_pn->port;
271
272 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200273 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100274 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200275 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100276
Christof Schmitt24073b42008-06-10 18:20:54 +0200277 /* paranoia */
278 if (ct_iu_req->wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200279 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200280 /* looks like a valid d_id */
281 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
Christof Schmitt24073b42008-06-10 18:20:54 +0200282}
283
Swen Schillig5ab944f2008-10-01 12:42:17 +0200284int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
285 struct zfcp_gid_pn_data *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200286{
Christof Schmitt24073b42008-06-10 18:20:54 +0200287 struct zfcp_adapter *adapter = erp_action->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200288 struct zfcp_fc_ns_handler_data compl_rec;
289 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200290
291 /* setup parameters for send generic command */
292 gid_pn->port = erp_action->port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200293 gid_pn->ct.wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200294 gid_pn->ct.handler = zfcp_fc_ns_handler;
295 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
Christof Schmitt24073b42008-06-10 18:20:54 +0200296 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
297 gid_pn->ct.req = &gid_pn->req;
298 gid_pn->ct.resp = &gid_pn->resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200299 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
300 sizeof(struct ct_iu_gid_pn_req));
301 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
302 sizeof(struct ct_iu_gid_pn_resp));
303
304 /* setup nameserver request */
305 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
306 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
307 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
308 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
309 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100310 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
Christof Schmitt24073b42008-06-10 18:20:54 +0200311 gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
312
Swen Schillig5ab944f2008-10-01 12:42:17 +0200313 init_completion(&compl_rec.done);
314 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
315 compl_rec.handler_data = (unsigned long) gid_pn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200316 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
317 erp_action);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200318 if (!ret)
319 wait_for_completion(&compl_rec.done);
320 return ret;
321}
322
323/**
324 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
325 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
326 * return: -ENOMEM on error, 0 otherwise
327 */
328int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
329{
330 int ret;
331 struct zfcp_gid_pn_data *gid_pn;
332 struct zfcp_adapter *adapter = erp_action->adapter;
333
334 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
335 if (!gid_pn)
336 return -ENOMEM;
337
338 memset(gid_pn, 0, sizeof(*gid_pn));
339
Sven Schuetz9d544f22009-04-06 18:31:47 +0200340 ret = zfcp_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200341 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200342 goto out;
343
344 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
345
Sven Schuetz9d544f22009-04-06 18:31:47 +0200346 zfcp_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200347out:
348 mempool_free(gid_pn, adapter->pool.data_gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200349 return ret;
350}
351
352/**
353 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
354 * @port: zfcp_port structure
355 * @plogi: plogi payload
356 *
357 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
358 */
359void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
360{
361 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
362 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
363 if (plogi->serv_param.class1_serv_param[0] & 0x80)
364 port->supported_classes |= FC_COS_CLASS1;
365 if (plogi->serv_param.class2_serv_param[0] & 0x80)
366 port->supported_classes |= FC_COS_CLASS2;
367 if (plogi->serv_param.class3_serv_param[0] & 0x80)
368 port->supported_classes |= FC_COS_CLASS3;
369 if (plogi->serv_param.class4_serv_param[0] & 0x80)
370 port->supported_classes |= FC_COS_CLASS4;
371}
372
373struct zfcp_els_adisc {
374 struct zfcp_send_els els;
375 struct scatterlist req;
376 struct scatterlist resp;
377 struct zfcp_ls_adisc ls_adisc;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200378 struct zfcp_ls_adisc ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200379};
380
381static void zfcp_fc_adisc_handler(unsigned long data)
382{
383 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
384 struct zfcp_port *port = adisc->els.port;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200385 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200386
Christof Schmitt3968ce82008-07-02 10:56:32 +0200387 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200388 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200389 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
390 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200391 goto out;
392 }
393
394 if (!port->wwnn)
395 port->wwnn = ls_adisc->wwnn;
396
Swen Schillig24095492009-03-02 13:09:07 +0100397 if ((port->wwpn != ls_adisc->wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100398 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100399 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
400 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100401 goto out;
402 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200403
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100404 /* port is good, unblock rport without going through erp */
405 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200406 out:
407 zfcp_port_put(port);
408 kfree(adisc);
409}
410
411static int zfcp_fc_adisc(struct zfcp_port *port)
412{
413 struct zfcp_els_adisc *adisc;
414 struct zfcp_adapter *adapter = port->adapter;
415
416 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
417 if (!adisc)
418 return -ENOMEM;
419
420 adisc->els.req = &adisc->req;
421 adisc->els.resp = &adisc->resp;
422 sg_init_one(adisc->els.req, &adisc->ls_adisc,
423 sizeof(struct zfcp_ls_adisc));
424 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
Swen Schillig44cc76f2008-10-01 12:42:16 +0200425 sizeof(struct zfcp_ls_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200426
Christof Schmitt24073b42008-06-10 18:20:54 +0200427 adisc->els.adapter = adapter;
428 adisc->els.port = port;
429 adisc->els.d_id = port->d_id;
430 adisc->els.handler = zfcp_fc_adisc_handler;
431 adisc->els.handler_data = (unsigned long) adisc;
432 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
433
434 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
435 without FC-AL-2 capability, so we don't set it */
436 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
437 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
438 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
439
440 return zfcp_fsf_send_els(&adisc->els);
441}
442
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100443void zfcp_fc_link_test_work(struct work_struct *work)
444{
445 struct zfcp_port *port =
446 container_of(work, struct zfcp_port, test_link_work);
447 int retval;
448
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100449 zfcp_port_get(port);
450 port->rport_task = RPORT_DEL;
451 zfcp_scsi_rport_work(&port->rport_work);
452
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100453 retval = zfcp_fc_adisc(port);
454 if (retval == 0)
455 return;
456
457 /* send of ADISC was not possible */
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100458 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
459
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100460 zfcp_port_put(port);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100461}
462
Christof Schmitt24073b42008-06-10 18:20:54 +0200463/**
464 * zfcp_test_link - lightweight link test procedure
465 * @port: port to be tested
466 *
467 * Test status of a link to a remote port using the ELS command ADISC.
468 * If there is a problem with the remote port, error recovery steps
469 * will be triggered.
470 */
471void zfcp_test_link(struct zfcp_port *port)
472{
Christof Schmitt24073b42008-06-10 18:20:54 +0200473 zfcp_port_get(port);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100474 if (!queue_work(zfcp_data.work_queue, &port->test_link_work))
475 zfcp_port_put(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200476}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200477
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100478static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200479{
480 struct scatterlist *sg = &gpn_ft->sg_req;
481
482 kfree(sg_virt(sg)); /* free request buffer */
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100483 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200484
485 kfree(gpn_ft);
486}
487
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100488static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200489{
490 struct zfcp_gpn_ft *gpn_ft;
491 struct ct_iu_gpn_ft_req *req;
492
493 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
494 if (!gpn_ft)
495 return NULL;
496
497 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
498 if (!req) {
499 kfree(gpn_ft);
500 gpn_ft = NULL;
501 goto out;
502 }
503 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
504
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100505 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
506 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200507 gpn_ft = NULL;
508 }
509out:
510 return gpn_ft;
511}
512
513
514static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100515 struct zfcp_adapter *adapter,
516 int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200517{
518 struct zfcp_send_ct *ct = &gpn_ft->ct;
519 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200520 struct zfcp_fc_ns_handler_data compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200521 int ret;
522
523 /* prepare CT IU for GPN_FT */
524 req->header.revision = ZFCP_CT_REVISION;
525 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
526 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
527 req->header.options = ZFCP_CT_SYNCHRONOUS;
528 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100529 req->header.max_res_size = max_bytes / 4;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200530 req->flags = 0;
531 req->domain_id_scope = 0;
532 req->area_id_scope = 0;
533 req->fc4_type = ZFCP_CT_SCSI_FCP;
534
535 /* prepare zfcp_send_ct */
Sven Schuetz9d544f22009-04-06 18:31:47 +0200536 ct->wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200537 ct->handler = zfcp_fc_ns_handler;
538 ct->handler_data = (unsigned long)&compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200539 ct->timeout = 10;
540 ct->req = &gpn_ft->sg_req;
541 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200542
Swen Schillig5ab944f2008-10-01 12:42:17 +0200543 init_completion(&compl_rec.done);
544 compl_rec.handler = NULL;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200545 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
546 if (!ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200547 wait_for_completion(&compl_rec.done);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200548 return ret;
549}
550
551static void zfcp_validate_port(struct zfcp_port *port)
552{
553 struct zfcp_adapter *adapter = port->adapter;
554
Martin Petermann6ab35c02009-04-17 15:08:13 +0200555 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
556 return;
557
Swen Schilligcc8c2822008-06-10 18:21:00 +0200558 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
559
Christof Schmitt04062892008-10-01 12:42:20 +0200560 if ((port->supported_classes != 0) ||
561 !list_empty(&port->unit_list_head)) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200562 zfcp_port_put(port);
563 return;
564 }
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100565 zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200566 zfcp_erp_wait(adapter);
567 zfcp_port_put(port);
568 zfcp_port_dequeue(port);
569}
570
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100571static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200572{
573 struct zfcp_send_ct *ct = &gpn_ft->ct;
574 struct scatterlist *sg = gpn_ft->sg_resp;
575 struct ct_hdr *hdr = sg_virt(sg);
576 struct gpn_ft_resp_acc *acc = sg_virt(sg);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200577 struct zfcp_adapter *adapter = ct->wka_port->adapter;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200578 struct zfcp_port *port, *tmp;
579 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200580 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200581
582 if (ct->status)
583 return -EIO;
584
585 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
586 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
587 return -EAGAIN; /* might be a temporary condition */
588 return -EIO;
589 }
590
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100591 if (hdr->max_res_size) {
592 dev_warn(&adapter->ccw_device->dev,
593 "The name server reported %d words residual data\n",
594 hdr->max_res_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200595 return -E2BIG;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100596 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200597
598 down(&zfcp_data.config_sema);
599
600 /* first entry is the header */
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100601 for (x = 1; x < max_entries && !last; x++) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200602 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
603 acc++;
604 else
605 acc = sg_virt(++sg);
606
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200607 last = acc->control & 0x80;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200608 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
609 acc->port_id[2];
610
Swen Schillig5ab944f2008-10-01 12:42:17 +0200611 /* don't attach ports with a well known address */
612 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
613 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200614 /* skip the adapter's port and known remote ports */
Swen Schillig95285392008-08-21 13:43:35 +0200615 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200616 continue;
Swen Schillig95285392008-08-21 13:43:35 +0200617 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
Martin Petermann6ab35c02009-04-17 15:08:13 +0200618 if (port)
Swen Schillig95285392008-08-21 13:43:35 +0200619 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200620
621 port = zfcp_port_enqueue(adapter, acc->wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200622 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schillig317e6b62008-07-02 10:56:37 +0200623 if (IS_ERR(port))
624 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200625 else
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100626 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200627 }
628
629 zfcp_erp_wait(adapter);
630 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
631 zfcp_validate_port(port);
632 up(&zfcp_data.config_sema);
633 return ret;
634}
635
636/**
637 * zfcp_scan_ports - scan remote ports and attach new ports
638 * @adapter: pointer to struct zfcp_adapter
639 */
640int zfcp_scan_ports(struct zfcp_adapter *adapter)
641{
642 int ret, i;
643 struct zfcp_gpn_ft *gpn_ft;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100644 int chain, max_entries, buf_num, max_bytes;
645
646 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
647 buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
648 max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
649 max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200650
Swen Schillig306b6ed2009-04-17 15:08:02 +0200651 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
652 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200653 return 0;
654
Sven Schuetz9d544f22009-04-06 18:31:47 +0200655 ret = zfcp_wka_port_get(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200656 if (ret)
657 return ret;
658
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100659 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200660 if (!gpn_ft) {
661 ret = -ENOMEM;
662 goto out;
663 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200664
665 for (i = 0; i < 3; i++) {
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100666 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200667 if (!ret) {
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100668 ret = zfcp_scan_eval_gpn_ft(gpn_ft, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200669 if (ret == -EAGAIN)
670 ssleep(1);
671 else
672 break;
673 }
674 }
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100675 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200676out:
Sven Schuetz9d544f22009-04-06 18:31:47 +0200677 zfcp_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200678 return ret;
679}
680
681
682void _zfcp_scan_ports_later(struct work_struct *work)
683{
684 zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
685}
Sven Schuetz9d544f22009-04-06 18:31:47 +0200686
687struct zfcp_els_fc_job {
688 struct zfcp_send_els els;
689 struct fc_bsg_job *job;
690};
691
692static void zfcp_fc_generic_els_handler(unsigned long data)
693{
694 struct zfcp_els_fc_job *els_fc_job = (struct zfcp_els_fc_job *) data;
695 struct fc_bsg_job *job = els_fc_job->job;
696 struct fc_bsg_reply *reply = job->reply;
697
698 if (els_fc_job->els.status) {
699 /* request rejected or timed out */
700 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_REJECT;
701 goto out;
702 }
703
704 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200705 reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200706
707out:
708 job->state_flags = FC_RQST_STATE_DONE;
709 job->job_done(job);
710 kfree(els_fc_job);
711}
712
713int zfcp_fc_execute_els_fc_job(struct fc_bsg_job *job)
714{
715 struct zfcp_els_fc_job *els_fc_job;
716 struct fc_rport *rport = job->rport;
717 struct Scsi_Host *shost;
718 struct zfcp_adapter *adapter;
719 struct zfcp_port *port;
720 u8 *port_did;
721
722 shost = rport ? rport_to_shost(rport) : job->shost;
723 adapter = (struct zfcp_adapter *)shost->hostdata[0];
724
725 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
726 return -EINVAL;
727
728 els_fc_job = kzalloc(sizeof(struct zfcp_els_fc_job), GFP_KERNEL);
729 if (!els_fc_job)
730 return -ENOMEM;
731
732 els_fc_job->els.adapter = adapter;
733 if (rport) {
734 read_lock_irq(&zfcp_data.config_lock);
735 port = rport->dd_data;
736 if (port)
Christof Schmittdc577d52009-05-15 13:18:22 +0200737 els_fc_job->els.d_id = port->d_id;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200738 read_unlock_irq(&zfcp_data.config_lock);
739 if (!port) {
740 kfree(els_fc_job);
741 return -EINVAL;
742 }
Sven Schuetz9d544f22009-04-06 18:31:47 +0200743 } else {
744 port_did = job->request->rqst_data.h_els.port_id;
745 els_fc_job->els.d_id = (port_did[0] << 16) +
746 (port_did[1] << 8) + port_did[2];
747 }
748
749 els_fc_job->els.req = job->request_payload.sg_list;
750 els_fc_job->els.resp = job->reply_payload.sg_list;
751 els_fc_job->els.handler = zfcp_fc_generic_els_handler;
752 els_fc_job->els.handler_data = (unsigned long) els_fc_job;
753 els_fc_job->job = job;
754
755 return zfcp_fsf_send_els(&els_fc_job->els);
756}
757
758struct zfcp_ct_fc_job {
759 struct zfcp_send_ct ct;
760 struct fc_bsg_job *job;
761};
762
763static void zfcp_fc_generic_ct_handler(unsigned long data)
764{
765 struct zfcp_ct_fc_job *ct_fc_job = (struct zfcp_ct_fc_job *) data;
766 struct fc_bsg_job *job = ct_fc_job->job;
767
768 job->reply->reply_data.ctels_reply.status = ct_fc_job->ct.status ?
769 FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200770 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200771 job->state_flags = FC_RQST_STATE_DONE;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200772 job->job_done(job);
773
774 zfcp_wka_port_put(ct_fc_job->ct.wka_port);
775
776 kfree(ct_fc_job);
777}
778
779int zfcp_fc_execute_ct_fc_job(struct fc_bsg_job *job)
780{
781 int ret;
782 u8 gs_type;
783 struct fc_rport *rport = job->rport;
784 struct Scsi_Host *shost;
785 struct zfcp_adapter *adapter;
786 struct zfcp_ct_fc_job *ct_fc_job;
787 u32 preamble_word1;
788
789 shost = rport ? rport_to_shost(rport) : job->shost;
790
791 adapter = (struct zfcp_adapter *)shost->hostdata[0];
792 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
793 return -EINVAL;
794
795 ct_fc_job = kzalloc(sizeof(struct zfcp_ct_fc_job), GFP_KERNEL);
796 if (!ct_fc_job)
797 return -ENOMEM;
798
799 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
800 gs_type = (preamble_word1 & 0xff000000) >> 24;
801
802 switch (gs_type) {
803 case FC_FST_ALIAS:
804 ct_fc_job->ct.wka_port = &adapter->gs->as;
805 break;
806 case FC_FST_MGMT:
807 ct_fc_job->ct.wka_port = &adapter->gs->ms;
808 break;
809 case FC_FST_TIME:
810 ct_fc_job->ct.wka_port = &adapter->gs->ts;
811 break;
812 case FC_FST_DIR:
813 ct_fc_job->ct.wka_port = &adapter->gs->ds;
814 break;
815 default:
816 kfree(ct_fc_job);
817 return -EINVAL; /* no such service */
818 }
819
820 ret = zfcp_wka_port_get(ct_fc_job->ct.wka_port);
821 if (ret) {
822 kfree(ct_fc_job);
823 return ret;
824 }
825
826 ct_fc_job->ct.req = job->request_payload.sg_list;
827 ct_fc_job->ct.resp = job->reply_payload.sg_list;
828 ct_fc_job->ct.timeout = ZFCP_FSF_REQUEST_TIMEOUT;
829 ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
830 ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
831 ct_fc_job->ct.completion = NULL;
832 ct_fc_job->job = job;
833
834 ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL, NULL);
835 if (ret) {
836 kfree(ct_fc_job);
837 zfcp_wka_port_put(ct_fc_job->ct.wka_port);
838 }
839 return ret;
840}