blob: 18fd975412d3da1d2aa5867eae23c10e176bdc25 [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 Schmitt39eb7e9a2008-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 Schmitt39eb7e9a2008-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
82 wait_event_timeout(
83 wka_port->completion_wq,
84 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
85 wka_port->status == ZFCP_WKA_PORT_OFFLINE,
86 HZ >> 1);
87
88 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
89 atomic_inc(&wka_port->refcount);
90 return 0;
91 }
92 return -EIO;
93}
94
95static void zfcp_wka_port_offline(struct work_struct *work)
96{
Jean Delvarebf6aede2009-04-02 16:56:54 -070097 struct delayed_work *dw = to_delayed_work(work);
Swen Schillig5ab944f2008-10-01 12:42:17 +020098 struct zfcp_wka_port *wka_port =
99 container_of(dw, struct zfcp_wka_port, work);
100
Swen Schillig5ab944f2008-10-01 12:42:17 +0200101 mutex_lock(&wka_port->mutex);
102 if ((atomic_read(&wka_port->refcount) != 0) ||
103 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
104 goto out;
105
106 wka_port->status = ZFCP_WKA_PORT_CLOSING;
107 if (zfcp_fsf_close_wka_port(wka_port)) {
108 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
109 wake_up(&wka_port->completion_wq);
110 }
111out:
112 mutex_unlock(&wka_port->mutex);
113}
114
115static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
116{
117 if (atomic_dec_return(&wka_port->refcount) != 0)
118 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200119 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200120 schedule_delayed_work(&wka_port->work, HZ / 100);
121}
122
123void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
124{
125 struct zfcp_wka_port *wka_port = &adapter->nsp;
126
127 init_waitqueue_head(&wka_port->completion_wq);
128
129 wka_port->adapter = adapter;
130 wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
131
132 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
133 atomic_set(&wka_port->refcount, 0);
134 mutex_init(&wka_port->mutex);
135 INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
136}
137
Swen Schillig828bc122009-04-17 15:08:05 +0200138void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
139{
140 cancel_delayed_work_sync(&wka->work);
141 mutex_lock(&wka->mutex);
142 wka->status = ZFCP_WKA_PORT_OFFLINE;
143 mutex_unlock(&wka->mutex);
144}
145
Christof Schmitt24073b42008-06-10 18:20:54 +0200146static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
147 struct fcp_rscn_element *elem)
148{
149 unsigned long flags;
150 struct zfcp_port *port;
151
152 read_lock_irqsave(&zfcp_data.config_lock, flags);
Swen Schillig24095492009-03-02 13:09:07 +0100153 list_for_each_entry(port, &fsf_req->adapter->port_list_head, list)
154 if ((port->d_id & range) == (elem->nport_did & range))
Christof Schmitt24073b42008-06-10 18:20:54 +0200155 zfcp_test_link(port);
Swen Schillig24095492009-03-02 13:09:07 +0100156
Christof Schmitt24073b42008-06-10 18:20:54 +0200157 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
158}
159
160static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
161{
162 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
163 struct fcp_rscn_head *fcp_rscn_head;
164 struct fcp_rscn_element *fcp_rscn_element;
165 u16 i;
166 u16 no_entries;
167 u32 range_mask;
168
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200169 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
170 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200171
172 /* see FC-FS */
173 no_entries = fcp_rscn_head->payload_len /
174 sizeof(struct fcp_rscn_element);
175
176 for (i = 1; i < no_entries; i++) {
177 /* skip head and start with 1st element */
178 fcp_rscn_element++;
Christof Schmitte0d7fcb2008-12-19 16:56:58 +0100179 range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
Christof Schmitt24073b42008-06-10 18:20:54 +0200180 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
181 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200182 schedule_work(&fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200183}
184
Swen Schillig7ba58c92008-10-01 12:42:18 +0200185static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200186{
187 struct zfcp_adapter *adapter = req->adapter;
188 struct zfcp_port *port;
189 unsigned long flags;
190
191 read_lock_irqsave(&zfcp_data.config_lock, flags);
192 list_for_each_entry(port, &adapter->port_list_head, list)
193 if (port->wwpn == wwpn)
194 break;
195 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
196
197 if (port && (port->wwpn == wwpn))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100198 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200199}
200
201static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
202{
203 struct fsf_status_read_buffer *status_buffer =
204 (struct fsf_status_read_buffer *)req->data;
205 struct fsf_plogi *els_plogi =
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200206 (struct fsf_plogi *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200207
208 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
209}
210
211static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
212{
213 struct fsf_status_read_buffer *status_buffer =
214 (struct fsf_status_read_buffer *)req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200215 struct fcp_logo *els_logo =
216 (struct fcp_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200217
218 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
219}
220
221/**
222 * zfcp_fc_incoming_els - handle incoming ELS
223 * @fsf_req - request which contains incoming ELS
224 */
225void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
226{
227 struct fsf_status_read_buffer *status_buffer =
228 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200229 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200230
231 zfcp_san_dbf_event_incoming_els(fsf_req);
232 if (els_type == LS_PLOGI)
233 zfcp_fc_incoming_plogi(fsf_req);
234 else if (els_type == LS_LOGO)
235 zfcp_fc_incoming_logo(fsf_req);
236 else if (els_type == LS_RSCN)
237 zfcp_fc_incoming_rscn(fsf_req);
238}
239
Swen Schillig5ab944f2008-10-01 12:42:17 +0200240static void zfcp_fc_ns_handler(unsigned long data)
241{
242 struct zfcp_fc_ns_handler_data *compl_rec =
243 (struct zfcp_fc_ns_handler_data *) data;
244
245 if (compl_rec->handler)
246 compl_rec->handler(compl_rec->handler_data);
247
248 complete(&compl_rec->done);
249}
250
251static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200252{
253 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
254 struct zfcp_send_ct *ct = &gid_pn->ct;
255 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
256 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
257 struct zfcp_port *port = gid_pn->port;
258
259 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200260 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100261 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200262 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100263
Christof Schmitt24073b42008-06-10 18:20:54 +0200264 /* paranoia */
265 if (ct_iu_req->wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200266 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200267 /* looks like a valid d_id */
268 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
Christof Schmitt24073b42008-06-10 18:20:54 +0200269}
270
Swen Schillig5ab944f2008-10-01 12:42:17 +0200271int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
272 struct zfcp_gid_pn_data *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200273{
Christof Schmitt24073b42008-06-10 18:20:54 +0200274 struct zfcp_adapter *adapter = erp_action->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200275 struct zfcp_fc_ns_handler_data compl_rec;
276 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200277
278 /* setup parameters for send generic command */
279 gid_pn->port = erp_action->port;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200280 gid_pn->ct.wka_port = &adapter->nsp;
281 gid_pn->ct.handler = zfcp_fc_ns_handler;
282 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
Christof Schmitt24073b42008-06-10 18:20:54 +0200283 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
284 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 Schmitt24073b42008-06-10 18:20:54 +0200298 gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
299
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 Schmitt24073b42008-06-10 18:20:54 +0200303 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
304 erp_action);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200305 if (!ret)
306 wait_for_completion(&compl_rec.done);
307 return ret;
308}
309
310/**
311 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
312 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
313 * return: -ENOMEM on error, 0 otherwise
314 */
315int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
316{
317 int ret;
318 struct zfcp_gid_pn_data *gid_pn;
319 struct zfcp_adapter *adapter = erp_action->adapter;
320
321 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
322 if (!gid_pn)
323 return -ENOMEM;
324
325 memset(gid_pn, 0, sizeof(*gid_pn));
326
327 ret = zfcp_wka_port_get(&adapter->nsp);
Christof Schmitt24073b42008-06-10 18:20:54 +0200328 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200329 goto out;
330
331 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
332
333 zfcp_wka_port_put(&adapter->nsp);
334out:
335 mempool_free(gid_pn, adapter->pool.data_gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200336 return ret;
337}
338
339/**
340 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
341 * @port: zfcp_port structure
342 * @plogi: plogi payload
343 *
344 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
345 */
346void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
347{
348 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
349 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
350 if (plogi->serv_param.class1_serv_param[0] & 0x80)
351 port->supported_classes |= FC_COS_CLASS1;
352 if (plogi->serv_param.class2_serv_param[0] & 0x80)
353 port->supported_classes |= FC_COS_CLASS2;
354 if (plogi->serv_param.class3_serv_param[0] & 0x80)
355 port->supported_classes |= FC_COS_CLASS3;
356 if (plogi->serv_param.class4_serv_param[0] & 0x80)
357 port->supported_classes |= FC_COS_CLASS4;
358}
359
360struct zfcp_els_adisc {
361 struct zfcp_send_els els;
362 struct scatterlist req;
363 struct scatterlist resp;
364 struct zfcp_ls_adisc ls_adisc;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200365 struct zfcp_ls_adisc ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200366};
367
368static void zfcp_fc_adisc_handler(unsigned long data)
369{
370 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
371 struct zfcp_port *port = adisc->els.port;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200372 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200373
Christof Schmitt3968ce82008-07-02 10:56:32 +0200374 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200375 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200376 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
377 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200378 goto out;
379 }
380
381 if (!port->wwnn)
382 port->wwnn = ls_adisc->wwnn;
383
Swen Schillig24095492009-03-02 13:09:07 +0100384 if ((port->wwpn != ls_adisc->wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100385 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100386 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
387 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100388 goto out;
389 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200390
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100391 /* port is good, unblock rport without going through erp */
392 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200393 out:
394 zfcp_port_put(port);
395 kfree(adisc);
396}
397
398static int zfcp_fc_adisc(struct zfcp_port *port)
399{
400 struct zfcp_els_adisc *adisc;
401 struct zfcp_adapter *adapter = port->adapter;
402
403 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
404 if (!adisc)
405 return -ENOMEM;
406
407 adisc->els.req = &adisc->req;
408 adisc->els.resp = &adisc->resp;
409 sg_init_one(adisc->els.req, &adisc->ls_adisc,
410 sizeof(struct zfcp_ls_adisc));
411 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
Swen Schillig44cc76f2008-10-01 12:42:16 +0200412 sizeof(struct zfcp_ls_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200413
Christof Schmitt24073b42008-06-10 18:20:54 +0200414 adisc->els.adapter = adapter;
415 adisc->els.port = port;
416 adisc->els.d_id = port->d_id;
417 adisc->els.handler = zfcp_fc_adisc_handler;
418 adisc->els.handler_data = (unsigned long) adisc;
419 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
420
421 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
422 without FC-AL-2 capability, so we don't set it */
423 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
424 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
425 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
426
427 return zfcp_fsf_send_els(&adisc->els);
428}
429
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100430void zfcp_fc_link_test_work(struct work_struct *work)
431{
432 struct zfcp_port *port =
433 container_of(work, struct zfcp_port, test_link_work);
434 int retval;
435
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100436 zfcp_port_get(port);
437 port->rport_task = RPORT_DEL;
438 zfcp_scsi_rport_work(&port->rport_work);
439
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100440 retval = zfcp_fc_adisc(port);
441 if (retval == 0)
442 return;
443
444 /* send of ADISC was not possible */
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100445 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
446
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100447 zfcp_port_put(port);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100448}
449
Christof Schmitt24073b42008-06-10 18:20:54 +0200450/**
451 * zfcp_test_link - lightweight link test procedure
452 * @port: port to be tested
453 *
454 * Test status of a link to a remote port using the ELS command ADISC.
455 * If there is a problem with the remote port, error recovery steps
456 * will be triggered.
457 */
458void zfcp_test_link(struct zfcp_port *port)
459{
Christof Schmitt24073b42008-06-10 18:20:54 +0200460 zfcp_port_get(port);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100461 if (!queue_work(zfcp_data.work_queue, &port->test_link_work))
462 zfcp_port_put(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200463}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200464
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100465static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200466{
467 struct scatterlist *sg = &gpn_ft->sg_req;
468
469 kfree(sg_virt(sg)); /* free request buffer */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100470 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200471
472 kfree(gpn_ft);
473}
474
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100475static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200476{
477 struct zfcp_gpn_ft *gpn_ft;
478 struct ct_iu_gpn_ft_req *req;
479
480 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
481 if (!gpn_ft)
482 return NULL;
483
484 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
485 if (!req) {
486 kfree(gpn_ft);
487 gpn_ft = NULL;
488 goto out;
489 }
490 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
491
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100492 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
493 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200494 gpn_ft = NULL;
495 }
496out:
497 return gpn_ft;
498}
499
500
501static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100502 struct zfcp_adapter *adapter,
503 int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200504{
505 struct zfcp_send_ct *ct = &gpn_ft->ct;
506 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200507 struct zfcp_fc_ns_handler_data compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200508 int ret;
509
510 /* prepare CT IU for GPN_FT */
511 req->header.revision = ZFCP_CT_REVISION;
512 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
513 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
514 req->header.options = ZFCP_CT_SYNCHRONOUS;
515 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100516 req->header.max_res_size = max_bytes / 4;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200517 req->flags = 0;
518 req->domain_id_scope = 0;
519 req->area_id_scope = 0;
520 req->fc4_type = ZFCP_CT_SCSI_FCP;
521
522 /* prepare zfcp_send_ct */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200523 ct->wka_port = &adapter->nsp;
524 ct->handler = zfcp_fc_ns_handler;
525 ct->handler_data = (unsigned long)&compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200526 ct->timeout = 10;
527 ct->req = &gpn_ft->sg_req;
528 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200529
Swen Schillig5ab944f2008-10-01 12:42:17 +0200530 init_completion(&compl_rec.done);
531 compl_rec.handler = NULL;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200532 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
533 if (!ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200534 wait_for_completion(&compl_rec.done);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200535 return ret;
536}
537
538static void zfcp_validate_port(struct zfcp_port *port)
539{
540 struct zfcp_adapter *adapter = port->adapter;
541
Martin Petermann6ab35c02009-04-17 15:08:13 +0200542 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
543 return;
544
Swen Schilligcc8c2822008-06-10 18:21:00 +0200545 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
546
Christof Schmitt04062892008-10-01 12:42:20 +0200547 if ((port->supported_classes != 0) ||
548 !list_empty(&port->unit_list_head)) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200549 zfcp_port_put(port);
550 return;
551 }
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100552 zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200553 zfcp_erp_wait(adapter);
554 zfcp_port_put(port);
555 zfcp_port_dequeue(port);
556}
557
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100558static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200559{
560 struct zfcp_send_ct *ct = &gpn_ft->ct;
561 struct scatterlist *sg = gpn_ft->sg_resp;
562 struct ct_hdr *hdr = sg_virt(sg);
563 struct gpn_ft_resp_acc *acc = sg_virt(sg);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200564 struct zfcp_adapter *adapter = ct->wka_port->adapter;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200565 struct zfcp_port *port, *tmp;
566 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200567 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200568
569 if (ct->status)
570 return -EIO;
571
572 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
573 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
574 return -EAGAIN; /* might be a temporary condition */
575 return -EIO;
576 }
577
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100578 if (hdr->max_res_size) {
579 dev_warn(&adapter->ccw_device->dev,
580 "The name server reported %d words residual data\n",
581 hdr->max_res_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200582 return -E2BIG;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100583 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200584
585 down(&zfcp_data.config_sema);
586
587 /* first entry is the header */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100588 for (x = 1; x < max_entries && !last; x++) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200589 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
590 acc++;
591 else
592 acc = sg_virt(++sg);
593
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200594 last = acc->control & 0x80;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200595 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
596 acc->port_id[2];
597
Swen Schillig5ab944f2008-10-01 12:42:17 +0200598 /* don't attach ports with a well known address */
599 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
600 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200601 /* skip the adapter's port and known remote ports */
Swen Schillig95285392008-08-21 13:43:35 +0200602 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200603 continue;
Swen Schillig95285392008-08-21 13:43:35 +0200604 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
Martin Petermann6ab35c02009-04-17 15:08:13 +0200605 if (port)
Swen Schillig95285392008-08-21 13:43:35 +0200606 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200607
608 port = zfcp_port_enqueue(adapter, acc->wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200609 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schillig317e6b62008-07-02 10:56:37 +0200610 if (IS_ERR(port))
611 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200612 else
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100613 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200614 }
615
616 zfcp_erp_wait(adapter);
617 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
618 zfcp_validate_port(port);
619 up(&zfcp_data.config_sema);
620 return ret;
621}
622
623/**
624 * zfcp_scan_ports - scan remote ports and attach new ports
625 * @adapter: pointer to struct zfcp_adapter
626 */
627int zfcp_scan_ports(struct zfcp_adapter *adapter)
628{
629 int ret, i;
630 struct zfcp_gpn_ft *gpn_ft;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100631 int chain, max_entries, buf_num, max_bytes;
632
633 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
634 buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
635 max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
636 max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200637
Swen Schillig306b6ed2009-04-17 15:08:02 +0200638 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
639 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200640 return 0;
641
Swen Schillig5ab944f2008-10-01 12:42:17 +0200642 ret = zfcp_wka_port_get(&adapter->nsp);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200643 if (ret)
644 return ret;
645
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100646 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200647 if (!gpn_ft) {
648 ret = -ENOMEM;
649 goto out;
650 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200651
652 for (i = 0; i < 3; i++) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100653 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200654 if (!ret) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100655 ret = zfcp_scan_eval_gpn_ft(gpn_ft, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200656 if (ret == -EAGAIN)
657 ssleep(1);
658 else
659 break;
660 }
661 }
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100662 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200663out:
664 zfcp_wka_port_put(&adapter->nsp);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200665 return ret;
666}
667
668
669void _zfcp_scan_ports_later(struct work_struct *work)
670{
671 zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
672}