blob: eabdfe24456e4056585769bf09fbe4d4af7e849f [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 *
6 * Copyright IBM Corporation 2008
7 */
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{
97 struct delayed_work *dw = container_of(work, struct delayed_work, work);
98 struct zfcp_wka_port *wka_port =
99 container_of(dw, struct zfcp_wka_port, work);
100
101 wait_event(wka_port->completion_wq,
102 atomic_read(&wka_port->refcount) == 0);
103
104 mutex_lock(&wka_port->mutex);
105 if ((atomic_read(&wka_port->refcount) != 0) ||
106 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
107 goto out;
108
109 wka_port->status = ZFCP_WKA_PORT_CLOSING;
110 if (zfcp_fsf_close_wka_port(wka_port)) {
111 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
112 wake_up(&wka_port->completion_wq);
113 }
114out:
115 mutex_unlock(&wka_port->mutex);
116}
117
118static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
119{
120 if (atomic_dec_return(&wka_port->refcount) != 0)
121 return;
122 /* wait 10 miliseconds, other reqs might pop in */
123 schedule_delayed_work(&wka_port->work, HZ / 100);
124}
125
126void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
127{
128 struct zfcp_wka_port *wka_port = &adapter->nsp;
129
130 init_waitqueue_head(&wka_port->completion_wq);
131
132 wka_port->adapter = adapter;
133 wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
134
135 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
136 atomic_set(&wka_port->refcount, 0);
137 mutex_init(&wka_port->mutex);
138 INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
139}
140
Christof Schmitt24073b42008-06-10 18:20:54 +0200141static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
142 struct fcp_rscn_element *elem)
143{
144 unsigned long flags;
145 struct zfcp_port *port;
146
147 read_lock_irqsave(&zfcp_data.config_lock, flags);
148 list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
Martin Petermannbce02612008-11-26 18:07:35 +0100149 if (!(atomic_read(&port->status) & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt24073b42008-06-10 18:20:54 +0200150 /* Try to connect to unused ports anyway. */
151 zfcp_erp_port_reopen(port,
152 ZFCP_STATUS_COMMON_ERP_FAILED,
153 82, fsf_req);
154 else if ((port->d_id & range) == (elem->nport_did & range))
155 /* Check connection status for connected ports */
156 zfcp_test_link(port);
157 }
158 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
159}
160
161static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
162{
163 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
164 struct fcp_rscn_head *fcp_rscn_head;
165 struct fcp_rscn_element *fcp_rscn_element;
166 u16 i;
167 u16 no_entries;
168 u32 range_mask;
169
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200170 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
171 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200172
173 /* see FC-FS */
174 no_entries = fcp_rscn_head->payload_len /
175 sizeof(struct fcp_rscn_element);
176
177 for (i = 1; i < no_entries; i++) {
178 /* skip head and start with 1st element */
179 fcp_rscn_element++;
Christof Schmitte0d7fcb2008-12-19 16:56:58 +0100180 range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
Christof Schmitt24073b42008-06-10 18:20:54 +0200181 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
182 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200183 schedule_work(&fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200184}
185
Swen Schillig7ba58c92008-10-01 12:42:18 +0200186static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200187{
188 struct zfcp_adapter *adapter = req->adapter;
189 struct zfcp_port *port;
190 unsigned long flags;
191
192 read_lock_irqsave(&zfcp_data.config_lock, flags);
193 list_for_each_entry(port, &adapter->port_list_head, list)
194 if (port->wwpn == wwpn)
195 break;
196 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
197
198 if (port && (port->wwpn == wwpn))
199 zfcp_erp_port_forced_reopen(port, 0, 83, req);
200}
201
202static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
203{
204 struct fsf_status_read_buffer *status_buffer =
205 (struct fsf_status_read_buffer *)req->data;
206 struct fsf_plogi *els_plogi =
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200207 (struct fsf_plogi *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200208
209 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
210}
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;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200216 struct fcp_logo *els_logo =
217 (struct fcp_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200218
219 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
220}
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
232 zfcp_san_dbf_event_incoming_els(fsf_req);
233 if (els_type == LS_PLOGI)
234 zfcp_fc_incoming_plogi(fsf_req);
235 else if (els_type == LS_LOGO)
236 zfcp_fc_incoming_logo(fsf_req);
237 else if (els_type == LS_RSCN)
238 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 Schmitt24073b42008-06-10 18:20:54 +0200262 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
263 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200264 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200265 }
266 /* paranoia */
267 if (ct_iu_req->wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200268 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200269 /* looks like a valid d_id */
270 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
Christof Schmitt24073b42008-06-10 18:20:54 +0200271}
272
Swen Schillig5ab944f2008-10-01 12:42:17 +0200273int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
274 struct zfcp_gid_pn_data *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200275{
Christof Schmitt24073b42008-06-10 18:20:54 +0200276 struct zfcp_adapter *adapter = erp_action->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200277 struct zfcp_fc_ns_handler_data compl_rec;
278 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200279
280 /* setup parameters for send generic command */
281 gid_pn->port = erp_action->port;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200282 gid_pn->ct.wka_port = &adapter->nsp;
283 gid_pn->ct.handler = zfcp_fc_ns_handler;
284 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
Christof Schmitt24073b42008-06-10 18:20:54 +0200285 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
286 gid_pn->ct.req = &gid_pn->req;
287 gid_pn->ct.resp = &gid_pn->resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200288 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
289 sizeof(struct ct_iu_gid_pn_req));
290 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
291 sizeof(struct ct_iu_gid_pn_resp));
292
293 /* setup nameserver request */
294 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
295 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
296 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
297 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
298 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100299 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
Christof Schmitt24073b42008-06-10 18:20:54 +0200300 gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
301
Swen Schillig5ab944f2008-10-01 12:42:17 +0200302 init_completion(&compl_rec.done);
303 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
304 compl_rec.handler_data = (unsigned long) gid_pn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200305 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
306 erp_action);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200307 if (!ret)
308 wait_for_completion(&compl_rec.done);
309 return ret;
310}
311
312/**
313 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
314 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
315 * return: -ENOMEM on error, 0 otherwise
316 */
317int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
318{
319 int ret;
320 struct zfcp_gid_pn_data *gid_pn;
321 struct zfcp_adapter *adapter = erp_action->adapter;
322
323 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
324 if (!gid_pn)
325 return -ENOMEM;
326
327 memset(gid_pn, 0, sizeof(*gid_pn));
328
329 ret = zfcp_wka_port_get(&adapter->nsp);
Christof Schmitt24073b42008-06-10 18:20:54 +0200330 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200331 goto out;
332
333 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
334
335 zfcp_wka_port_put(&adapter->nsp);
336out:
337 mempool_free(gid_pn, adapter->pool.data_gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200338 return ret;
339}
340
341/**
342 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
343 * @port: zfcp_port structure
344 * @plogi: plogi payload
345 *
346 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
347 */
348void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
349{
350 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
351 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
352 if (plogi->serv_param.class1_serv_param[0] & 0x80)
353 port->supported_classes |= FC_COS_CLASS1;
354 if (plogi->serv_param.class2_serv_param[0] & 0x80)
355 port->supported_classes |= FC_COS_CLASS2;
356 if (plogi->serv_param.class3_serv_param[0] & 0x80)
357 port->supported_classes |= FC_COS_CLASS3;
358 if (plogi->serv_param.class4_serv_param[0] & 0x80)
359 port->supported_classes |= FC_COS_CLASS4;
360}
361
362struct zfcp_els_adisc {
363 struct zfcp_send_els els;
364 struct scatterlist req;
365 struct scatterlist resp;
366 struct zfcp_ls_adisc ls_adisc;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200367 struct zfcp_ls_adisc ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200368};
369
370static void zfcp_fc_adisc_handler(unsigned long data)
371{
372 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
373 struct zfcp_port *port = adisc->els.port;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200374 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200375
Christof Schmitt3968ce82008-07-02 10:56:32 +0200376 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200377 /* request rejected or timed out */
378 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
379 goto out;
380 }
381
382 if (!port->wwnn)
383 port->wwnn = ls_adisc->wwnn;
384
385 if (port->wwpn != ls_adisc->wwpn)
386 zfcp_erp_port_reopen(port, 0, 64, NULL);
387
388 out:
389 zfcp_port_put(port);
390 kfree(adisc);
391}
392
393static int zfcp_fc_adisc(struct zfcp_port *port)
394{
395 struct zfcp_els_adisc *adisc;
396 struct zfcp_adapter *adapter = port->adapter;
397
398 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
399 if (!adisc)
400 return -ENOMEM;
401
402 adisc->els.req = &adisc->req;
403 adisc->els.resp = &adisc->resp;
404 sg_init_one(adisc->els.req, &adisc->ls_adisc,
405 sizeof(struct zfcp_ls_adisc));
406 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
Swen Schillig44cc76f2008-10-01 12:42:16 +0200407 sizeof(struct zfcp_ls_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200408
Christof Schmitt24073b42008-06-10 18:20:54 +0200409 adisc->els.adapter = adapter;
410 adisc->els.port = port;
411 adisc->els.d_id = port->d_id;
412 adisc->els.handler = zfcp_fc_adisc_handler;
413 adisc->els.handler_data = (unsigned long) adisc;
414 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
415
416 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
417 without FC-AL-2 capability, so we don't set it */
418 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
419 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
420 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
421
422 return zfcp_fsf_send_els(&adisc->els);
423}
424
425/**
426 * zfcp_test_link - lightweight link test procedure
427 * @port: port to be tested
428 *
429 * Test status of a link to a remote port using the ELS command ADISC.
430 * If there is a problem with the remote port, error recovery steps
431 * will be triggered.
432 */
433void zfcp_test_link(struct zfcp_port *port)
434{
435 int retval;
436
437 zfcp_port_get(port);
438 retval = zfcp_fc_adisc(port);
Swen Schillig95285392008-08-21 13:43:35 +0200439 if (retval == 0)
Christof Schmitt24073b42008-06-10 18:20:54 +0200440 return;
441
442 /* send of ADISC was not possible */
443 zfcp_port_put(port);
Swen Schillig95285392008-08-21 13:43:35 +0200444 if (retval != -EBUSY)
445 zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200446}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200447
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100448static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200449{
450 struct scatterlist *sg = &gpn_ft->sg_req;
451
452 kfree(sg_virt(sg)); /* free request buffer */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100453 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200454
455 kfree(gpn_ft);
456}
457
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100458static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200459{
460 struct zfcp_gpn_ft *gpn_ft;
461 struct ct_iu_gpn_ft_req *req;
462
463 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
464 if (!gpn_ft)
465 return NULL;
466
467 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
468 if (!req) {
469 kfree(gpn_ft);
470 gpn_ft = NULL;
471 goto out;
472 }
473 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
474
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100475 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
476 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200477 gpn_ft = NULL;
478 }
479out:
480 return gpn_ft;
481}
482
483
484static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100485 struct zfcp_adapter *adapter,
486 int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200487{
488 struct zfcp_send_ct *ct = &gpn_ft->ct;
489 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200490 struct zfcp_fc_ns_handler_data compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200491 int ret;
492
493 /* prepare CT IU for GPN_FT */
494 req->header.revision = ZFCP_CT_REVISION;
495 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
496 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
497 req->header.options = ZFCP_CT_SYNCHRONOUS;
498 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100499 req->header.max_res_size = max_bytes / 4;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200500 req->flags = 0;
501 req->domain_id_scope = 0;
502 req->area_id_scope = 0;
503 req->fc4_type = ZFCP_CT_SCSI_FCP;
504
505 /* prepare zfcp_send_ct */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200506 ct->wka_port = &adapter->nsp;
507 ct->handler = zfcp_fc_ns_handler;
508 ct->handler_data = (unsigned long)&compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200509 ct->timeout = 10;
510 ct->req = &gpn_ft->sg_req;
511 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200512
Swen Schillig5ab944f2008-10-01 12:42:17 +0200513 init_completion(&compl_rec.done);
514 compl_rec.handler = NULL;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200515 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
516 if (!ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200517 wait_for_completion(&compl_rec.done);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200518 return ret;
519}
520
521static void zfcp_validate_port(struct zfcp_port *port)
522{
523 struct zfcp_adapter *adapter = port->adapter;
524
525 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
526
Christof Schmitt04062892008-10-01 12:42:20 +0200527 if ((port->supported_classes != 0) ||
528 !list_empty(&port->unit_list_head)) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200529 zfcp_port_put(port);
530 return;
531 }
532 zfcp_erp_port_shutdown(port, 0, 151, NULL);
533 zfcp_erp_wait(adapter);
534 zfcp_port_put(port);
535 zfcp_port_dequeue(port);
536}
537
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100538static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200539{
540 struct zfcp_send_ct *ct = &gpn_ft->ct;
541 struct scatterlist *sg = gpn_ft->sg_resp;
542 struct ct_hdr *hdr = sg_virt(sg);
543 struct gpn_ft_resp_acc *acc = sg_virt(sg);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200544 struct zfcp_adapter *adapter = ct->wka_port->adapter;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200545 struct zfcp_port *port, *tmp;
546 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200547 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200548
549 if (ct->status)
550 return -EIO;
551
552 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
553 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
554 return -EAGAIN; /* might be a temporary condition */
555 return -EIO;
556 }
557
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100558 if (hdr->max_res_size) {
559 dev_warn(&adapter->ccw_device->dev,
560 "The name server reported %d words residual data\n",
561 hdr->max_res_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200562 return -E2BIG;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100563 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200564
565 down(&zfcp_data.config_sema);
566
567 /* first entry is the header */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100568 for (x = 1; x < max_entries && !last; x++) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200569 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
570 acc++;
571 else
572 acc = sg_virt(++sg);
573
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200574 last = acc->control & 0x80;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200575 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
576 acc->port_id[2];
577
Swen Schillig5ab944f2008-10-01 12:42:17 +0200578 /* don't attach ports with a well known address */
579 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
580 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200581 /* skip the adapter's port and known remote ports */
Swen Schillig95285392008-08-21 13:43:35 +0200582 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200583 continue;
Swen Schillig95285392008-08-21 13:43:35 +0200584 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
585 if (port) {
586 zfcp_port_get(port);
587 continue;
588 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200589
590 port = zfcp_port_enqueue(adapter, acc->wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200591 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schillig317e6b62008-07-02 10:56:37 +0200592 if (IS_ERR(port))
593 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200594 else
Swen Schillig317e6b62008-07-02 10:56:37 +0200595 zfcp_erp_port_reopen(port, 0, 149, NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200596 }
597
598 zfcp_erp_wait(adapter);
599 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
600 zfcp_validate_port(port);
601 up(&zfcp_data.config_sema);
602 return ret;
603}
604
605/**
606 * zfcp_scan_ports - scan remote ports and attach new ports
607 * @adapter: pointer to struct zfcp_adapter
608 */
609int zfcp_scan_ports(struct zfcp_adapter *adapter)
610{
611 int ret, i;
612 struct zfcp_gpn_ft *gpn_ft;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100613 int chain, max_entries, buf_num, max_bytes;
614
615 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
616 buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
617 max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
618 max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200619
620 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
621 return 0;
622
Swen Schillig5ab944f2008-10-01 12:42:17 +0200623 ret = zfcp_wka_port_get(&adapter->nsp);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200624 if (ret)
625 return ret;
626
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100627 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200628 if (!gpn_ft) {
629 ret = -ENOMEM;
630 goto out;
631 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200632
633 for (i = 0; i < 3; i++) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100634 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200635 if (!ret) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100636 ret = zfcp_scan_eval_gpn_ft(gpn_ft, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200637 if (ret == -EAGAIN)
638 ssleep(1);
639 else
640 break;
641 }
642 }
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100643 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200644out:
645 zfcp_wka_port_put(&adapter->nsp);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200646 return ret;
647}
648
649
650void _zfcp_scan_ports_later(struct work_struct *work)
651{
652 zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
653}