blob: bb2752b4130fde32f21982248171b895e5f5c3cc [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;
119 /* wait 10 miliseconds, other reqs might pop in */
120 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 Schilligea460a82009-05-15 13:18:20 +0200153 list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
Swen Schillig24095492009-03-02 13:09:07 +0100154 if ((port->d_id & range) == (elem->nport_did & range))
Christof Schmitt24073b42008-06-10 18:20:54 +0200155 zfcp_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200156 if (!port->d_id)
157 zfcp_erp_port_reopen(port,
158 ZFCP_STATUS_COMMON_ERP_FAILED,
159 "fcrscn1", NULL);
160 }
Swen Schillig24095492009-03-02 13:09:07 +0100161
Christof Schmitt24073b42008-06-10 18:20:54 +0200162 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
163}
164
165static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
166{
167 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
168 struct fcp_rscn_head *fcp_rscn_head;
169 struct fcp_rscn_element *fcp_rscn_element;
170 u16 i;
171 u16 no_entries;
172 u32 range_mask;
173
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200174 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
175 fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200176
177 /* see FC-FS */
178 no_entries = fcp_rscn_head->payload_len /
179 sizeof(struct fcp_rscn_element);
180
181 for (i = 1; i < no_entries; i++) {
182 /* skip head and start with 1st element */
183 fcp_rscn_element++;
Christof Schmitte0d7fcb2008-12-19 16:56:58 +0100184 range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
Christof Schmitt24073b42008-06-10 18:20:54 +0200185 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
186 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200187 schedule_work(&fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200188}
189
Swen Schillig7ba58c92008-10-01 12:42:18 +0200190static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200191{
192 struct zfcp_adapter *adapter = req->adapter;
193 struct zfcp_port *port;
194 unsigned long flags;
195
196 read_lock_irqsave(&zfcp_data.config_lock, flags);
197 list_for_each_entry(port, &adapter->port_list_head, list)
198 if (port->wwpn == wwpn)
199 break;
200 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
201
202 if (port && (port->wwpn == wwpn))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100203 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200204}
205
206static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
207{
208 struct fsf_status_read_buffer *status_buffer =
209 (struct fsf_status_read_buffer *)req->data;
210 struct fsf_plogi *els_plogi =
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200211 (struct fsf_plogi *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200212
213 zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
214}
215
216static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
217{
218 struct fsf_status_read_buffer *status_buffer =
219 (struct fsf_status_read_buffer *)req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200220 struct fcp_logo *els_logo =
221 (struct fcp_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200222
223 zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
224}
225
226/**
227 * zfcp_fc_incoming_els - handle incoming ELS
228 * @fsf_req - request which contains incoming ELS
229 */
230void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
231{
232 struct fsf_status_read_buffer *status_buffer =
233 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200234 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200235
236 zfcp_san_dbf_event_incoming_els(fsf_req);
237 if (els_type == LS_PLOGI)
238 zfcp_fc_incoming_plogi(fsf_req);
239 else if (els_type == LS_LOGO)
240 zfcp_fc_incoming_logo(fsf_req);
241 else if (els_type == LS_RSCN)
242 zfcp_fc_incoming_rscn(fsf_req);
243}
244
Swen Schillig5ab944f2008-10-01 12:42:17 +0200245static void zfcp_fc_ns_handler(unsigned long data)
246{
247 struct zfcp_fc_ns_handler_data *compl_rec =
248 (struct zfcp_fc_ns_handler_data *) data;
249
250 if (compl_rec->handler)
251 compl_rec->handler(compl_rec->handler_data);
252
253 complete(&compl_rec->done);
254}
255
256static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200257{
258 struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
259 struct zfcp_send_ct *ct = &gid_pn->ct;
260 struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
261 struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
262 struct zfcp_port *port = gid_pn->port;
263
264 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200265 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100266 if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200267 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100268
Christof Schmitt24073b42008-06-10 18:20:54 +0200269 /* paranoia */
270 if (ct_iu_req->wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200271 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200272 /* looks like a valid d_id */
273 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
Christof Schmitt24073b42008-06-10 18:20:54 +0200274}
275
Swen Schillig5ab944f2008-10-01 12:42:17 +0200276int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
277 struct zfcp_gid_pn_data *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200278{
Christof Schmitt24073b42008-06-10 18:20:54 +0200279 struct zfcp_adapter *adapter = erp_action->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200280 struct zfcp_fc_ns_handler_data compl_rec;
281 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200282
283 /* setup parameters for send generic command */
284 gid_pn->port = erp_action->port;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200285 gid_pn->ct.wka_port = &adapter->nsp;
286 gid_pn->ct.handler = zfcp_fc_ns_handler;
287 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
Christof Schmitt24073b42008-06-10 18:20:54 +0200288 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
289 gid_pn->ct.req = &gid_pn->req;
290 gid_pn->ct.resp = &gid_pn->resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200291 sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
292 sizeof(struct ct_iu_gid_pn_req));
293 sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
294 sizeof(struct ct_iu_gid_pn_resp));
295
296 /* setup nameserver request */
297 gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
298 gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
299 gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
300 gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
301 gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100302 gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
Christof Schmitt24073b42008-06-10 18:20:54 +0200303 gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
304
Swen Schillig5ab944f2008-10-01 12:42:17 +0200305 init_completion(&compl_rec.done);
306 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
307 compl_rec.handler_data = (unsigned long) gid_pn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200308 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
309 erp_action);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200310 if (!ret)
311 wait_for_completion(&compl_rec.done);
312 return ret;
313}
314
315/**
316 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
317 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
318 * return: -ENOMEM on error, 0 otherwise
319 */
320int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
321{
322 int ret;
323 struct zfcp_gid_pn_data *gid_pn;
324 struct zfcp_adapter *adapter = erp_action->adapter;
325
326 gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
327 if (!gid_pn)
328 return -ENOMEM;
329
330 memset(gid_pn, 0, sizeof(*gid_pn));
331
332 ret = zfcp_wka_port_get(&adapter->nsp);
Christof Schmitt24073b42008-06-10 18:20:54 +0200333 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200334 goto out;
335
336 ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
337
338 zfcp_wka_port_put(&adapter->nsp);
339out:
340 mempool_free(gid_pn, adapter->pool.data_gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200341 return ret;
342}
343
344/**
345 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
346 * @port: zfcp_port structure
347 * @plogi: plogi payload
348 *
349 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
350 */
351void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
352{
353 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
354 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
355 if (plogi->serv_param.class1_serv_param[0] & 0x80)
356 port->supported_classes |= FC_COS_CLASS1;
357 if (plogi->serv_param.class2_serv_param[0] & 0x80)
358 port->supported_classes |= FC_COS_CLASS2;
359 if (plogi->serv_param.class3_serv_param[0] & 0x80)
360 port->supported_classes |= FC_COS_CLASS3;
361 if (plogi->serv_param.class4_serv_param[0] & 0x80)
362 port->supported_classes |= FC_COS_CLASS4;
363}
364
365struct zfcp_els_adisc {
366 struct zfcp_send_els els;
367 struct scatterlist req;
368 struct scatterlist resp;
369 struct zfcp_ls_adisc ls_adisc;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200370 struct zfcp_ls_adisc ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200371};
372
373static void zfcp_fc_adisc_handler(unsigned long data)
374{
375 struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
376 struct zfcp_port *port = adisc->els.port;
Swen Schillig44cc76f2008-10-01 12:42:16 +0200377 struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200378
Christof Schmitt3968ce82008-07-02 10:56:32 +0200379 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200380 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200381 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
382 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200383 goto out;
384 }
385
386 if (!port->wwnn)
387 port->wwnn = ls_adisc->wwnn;
388
Swen Schillig24095492009-03-02 13:09:07 +0100389 if ((port->wwpn != ls_adisc->wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100390 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100391 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
392 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100393 goto out;
394 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200395
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100396 /* port is good, unblock rport without going through erp */
397 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200398 out:
399 zfcp_port_put(port);
400 kfree(adisc);
401}
402
403static int zfcp_fc_adisc(struct zfcp_port *port)
404{
405 struct zfcp_els_adisc *adisc;
406 struct zfcp_adapter *adapter = port->adapter;
407
408 adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
409 if (!adisc)
410 return -ENOMEM;
411
412 adisc->els.req = &adisc->req;
413 adisc->els.resp = &adisc->resp;
414 sg_init_one(adisc->els.req, &adisc->ls_adisc,
415 sizeof(struct zfcp_ls_adisc));
416 sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
Swen Schillig44cc76f2008-10-01 12:42:16 +0200417 sizeof(struct zfcp_ls_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200418
Christof Schmitt24073b42008-06-10 18:20:54 +0200419 adisc->els.adapter = adapter;
420 adisc->els.port = port;
421 adisc->els.d_id = port->d_id;
422 adisc->els.handler = zfcp_fc_adisc_handler;
423 adisc->els.handler_data = (unsigned long) adisc;
424 adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
425
426 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
427 without FC-AL-2 capability, so we don't set it */
428 adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
429 adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
430 adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
431
432 return zfcp_fsf_send_els(&adisc->els);
433}
434
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100435void zfcp_fc_link_test_work(struct work_struct *work)
436{
437 struct zfcp_port *port =
438 container_of(work, struct zfcp_port, test_link_work);
439 int retval;
440
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100441 zfcp_port_get(port);
442 port->rport_task = RPORT_DEL;
443 zfcp_scsi_rport_work(&port->rport_work);
444
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100445 retval = zfcp_fc_adisc(port);
446 if (retval == 0)
447 return;
448
449 /* send of ADISC was not possible */
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100450 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
451
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100452 zfcp_port_put(port);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100453}
454
Christof Schmitt24073b42008-06-10 18:20:54 +0200455/**
456 * zfcp_test_link - lightweight link test procedure
457 * @port: port to be tested
458 *
459 * Test status of a link to a remote port using the ELS command ADISC.
460 * If there is a problem with the remote port, error recovery steps
461 * will be triggered.
462 */
463void zfcp_test_link(struct zfcp_port *port)
464{
Christof Schmitt24073b42008-06-10 18:20:54 +0200465 zfcp_port_get(port);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100466 if (!queue_work(zfcp_data.work_queue, &port->test_link_work))
467 zfcp_port_put(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200468}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200469
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100470static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200471{
472 struct scatterlist *sg = &gpn_ft->sg_req;
473
474 kfree(sg_virt(sg)); /* free request buffer */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100475 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200476
477 kfree(gpn_ft);
478}
479
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100480static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200481{
482 struct zfcp_gpn_ft *gpn_ft;
483 struct ct_iu_gpn_ft_req *req;
484
485 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
486 if (!gpn_ft)
487 return NULL;
488
489 req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
490 if (!req) {
491 kfree(gpn_ft);
492 gpn_ft = NULL;
493 goto out;
494 }
495 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
496
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100497 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
498 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200499 gpn_ft = NULL;
500 }
501out:
502 return gpn_ft;
503}
504
505
506static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100507 struct zfcp_adapter *adapter,
508 int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200509{
510 struct zfcp_send_ct *ct = &gpn_ft->ct;
511 struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200512 struct zfcp_fc_ns_handler_data compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200513 int ret;
514
515 /* prepare CT IU for GPN_FT */
516 req->header.revision = ZFCP_CT_REVISION;
517 req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
518 req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
519 req->header.options = ZFCP_CT_SYNCHRONOUS;
520 req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100521 req->header.max_res_size = max_bytes / 4;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200522 req->flags = 0;
523 req->domain_id_scope = 0;
524 req->area_id_scope = 0;
525 req->fc4_type = ZFCP_CT_SCSI_FCP;
526
527 /* prepare zfcp_send_ct */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200528 ct->wka_port = &adapter->nsp;
529 ct->handler = zfcp_fc_ns_handler;
530 ct->handler_data = (unsigned long)&compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200531 ct->timeout = 10;
532 ct->req = &gpn_ft->sg_req;
533 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200534
Swen Schillig5ab944f2008-10-01 12:42:17 +0200535 init_completion(&compl_rec.done);
536 compl_rec.handler = NULL;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200537 ret = zfcp_fsf_send_ct(ct, NULL, NULL);
538 if (!ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200539 wait_for_completion(&compl_rec.done);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200540 return ret;
541}
542
543static void zfcp_validate_port(struct zfcp_port *port)
544{
545 struct zfcp_adapter *adapter = port->adapter;
546
Martin Petermann6ab35c02009-04-17 15:08:13 +0200547 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
548 return;
549
Swen Schilligcc8c2822008-06-10 18:21:00 +0200550 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
551
Christof Schmitt04062892008-10-01 12:42:20 +0200552 if ((port->supported_classes != 0) ||
553 !list_empty(&port->unit_list_head)) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200554 zfcp_port_put(port);
555 return;
556 }
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100557 zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200558 zfcp_erp_wait(adapter);
559 zfcp_port_put(port);
560 zfcp_port_dequeue(port);
561}
562
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100563static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200564{
565 struct zfcp_send_ct *ct = &gpn_ft->ct;
566 struct scatterlist *sg = gpn_ft->sg_resp;
567 struct ct_hdr *hdr = sg_virt(sg);
568 struct gpn_ft_resp_acc *acc = sg_virt(sg);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200569 struct zfcp_adapter *adapter = ct->wka_port->adapter;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200570 struct zfcp_port *port, *tmp;
571 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200572 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200573
574 if (ct->status)
575 return -EIO;
576
577 if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
578 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
579 return -EAGAIN; /* might be a temporary condition */
580 return -EIO;
581 }
582
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100583 if (hdr->max_res_size) {
584 dev_warn(&adapter->ccw_device->dev,
585 "The name server reported %d words residual data\n",
586 hdr->max_res_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200587 return -E2BIG;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100588 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200589
590 down(&zfcp_data.config_sema);
591
592 /* first entry is the header */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100593 for (x = 1; x < max_entries && !last; x++) {
Swen Schilligcc8c2822008-06-10 18:21:00 +0200594 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
595 acc++;
596 else
597 acc = sg_virt(++sg);
598
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200599 last = acc->control & 0x80;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200600 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
601 acc->port_id[2];
602
Swen Schillig5ab944f2008-10-01 12:42:17 +0200603 /* don't attach ports with a well known address */
604 if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
605 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200606 /* skip the adapter's port and known remote ports */
Swen Schillig95285392008-08-21 13:43:35 +0200607 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200608 continue;
Swen Schillig95285392008-08-21 13:43:35 +0200609 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
Martin Petermann6ab35c02009-04-17 15:08:13 +0200610 if (port)
Swen Schillig95285392008-08-21 13:43:35 +0200611 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200612
613 port = zfcp_port_enqueue(adapter, acc->wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200614 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schillig317e6b62008-07-02 10:56:37 +0200615 if (IS_ERR(port))
616 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200617 else
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100618 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200619 }
620
621 zfcp_erp_wait(adapter);
622 list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
623 zfcp_validate_port(port);
624 up(&zfcp_data.config_sema);
625 return ret;
626}
627
628/**
629 * zfcp_scan_ports - scan remote ports and attach new ports
630 * @adapter: pointer to struct zfcp_adapter
631 */
632int zfcp_scan_ports(struct zfcp_adapter *adapter)
633{
634 int ret, i;
635 struct zfcp_gpn_ft *gpn_ft;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100636 int chain, max_entries, buf_num, max_bytes;
637
638 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
639 buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
640 max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
641 max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200642
Swen Schillig306b6ed2009-04-17 15:08:02 +0200643 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
644 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200645 return 0;
646
Swen Schillig5ab944f2008-10-01 12:42:17 +0200647 ret = zfcp_wka_port_get(&adapter->nsp);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200648 if (ret)
649 return ret;
650
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100651 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200652 if (!gpn_ft) {
653 ret = -ENOMEM;
654 goto out;
655 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200656
657 for (i = 0; i < 3; i++) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100658 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200659 if (!ret) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100660 ret = zfcp_scan_eval_gpn_ft(gpn_ft, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200661 if (ret == -EAGAIN)
662 ssleep(1);
663 else
664 break;
665 }
666 }
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100667 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200668out:
669 zfcp_wka_port_put(&adapter->nsp);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200670 return ret;
671}
672
673
674void _zfcp_scan_ports_later(struct work_struct *work)
675{
676 zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
677}