[SCSI] zfcp: Fix error checking for ELS ADISC requests
[pandora-kernel.git] / drivers / s390 / scsi / zfcp_fc.c
1 /*
2  * zfcp device driver
3  *
4  * Fibre Channel related functions for the zfcp device driver.
5  *
6  * Copyright IBM Corporation 2008
7  */
8
9 #include "zfcp_ext.h"
10
11 struct ct_iu_gpn_ft_req {
12         struct ct_hdr header;
13         u8 flags;
14         u8 domain_id_scope;
15         u8 area_id_scope;
16         u8 fc4_type;
17 } __attribute__ ((packed));
18
19 struct gpn_ft_resp_acc {
20         u8 control;
21         u8 port_id[3];
22         u8 reserved[4];
23         u64 wwpn;
24 } __attribute__ ((packed));
25
26 #define ZFCP_GPN_FT_ENTRIES ((PAGE_SIZE - sizeof(struct ct_hdr)) \
27                                 / sizeof(struct gpn_ft_resp_acc))
28 #define ZFCP_GPN_FT_BUFFERS 4
29 #define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
30
31 struct ct_iu_gpn_ft_resp {
32         struct ct_hdr header;
33         struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
34 } __attribute__ ((packed));
35
36 struct zfcp_gpn_ft {
37         struct zfcp_send_ct ct;
38         struct scatterlist sg_req;
39         struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
40 };
41
42 static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
43                                    struct fcp_rscn_element *elem)
44 {
45         unsigned long flags;
46         struct zfcp_port *port;
47
48         read_lock_irqsave(&zfcp_data.config_lock, flags);
49         list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
50                 if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
51                         continue;
52                 /* FIXME: ZFCP_STATUS_PORT_DID_DID check is racy */
53                 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status))
54                         /* Try to connect to unused ports anyway. */
55                         zfcp_erp_port_reopen(port,
56                                              ZFCP_STATUS_COMMON_ERP_FAILED,
57                                              82, fsf_req);
58                 else if ((port->d_id & range) == (elem->nport_did & range))
59                         /* Check connection status for connected ports */
60                         zfcp_test_link(port);
61         }
62         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
63 }
64
65 static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
66 {
67         struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
68         struct fcp_rscn_head *fcp_rscn_head;
69         struct fcp_rscn_element *fcp_rscn_element;
70         u16 i;
71         u16 no_entries;
72         u32 range_mask;
73
74         fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
75         fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
76
77         /* see FC-FS */
78         no_entries = fcp_rscn_head->payload_len /
79                         sizeof(struct fcp_rscn_element);
80
81         for (i = 1; i < no_entries; i++) {
82                 /* skip head and start with 1st element */
83                 fcp_rscn_element++;
84                 switch (fcp_rscn_element->addr_format) {
85                 case ZFCP_PORT_ADDRESS:
86                         range_mask = ZFCP_PORTS_RANGE_PORT;
87                         break;
88                 case ZFCP_AREA_ADDRESS:
89                         range_mask = ZFCP_PORTS_RANGE_AREA;
90                         break;
91                 case ZFCP_DOMAIN_ADDRESS:
92                         range_mask = ZFCP_PORTS_RANGE_DOMAIN;
93                         break;
94                 case ZFCP_FABRIC_ADDRESS:
95                         range_mask = ZFCP_PORTS_RANGE_FABRIC;
96                         break;
97                 default:
98                         continue;
99                 }
100                 _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
101         }
102         schedule_work(&fsf_req->adapter->scan_work);
103 }
104
105 static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, wwn_t wwpn)
106 {
107         struct zfcp_adapter *adapter = req->adapter;
108         struct zfcp_port *port;
109         unsigned long flags;
110
111         read_lock_irqsave(&zfcp_data.config_lock, flags);
112         list_for_each_entry(port, &adapter->port_list_head, list)
113                 if (port->wwpn == wwpn)
114                         break;
115         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
116
117         if (port && (port->wwpn == wwpn))
118                 zfcp_erp_port_forced_reopen(port, 0, 83, req);
119 }
120
121 static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
122 {
123         struct fsf_status_read_buffer *status_buffer =
124                 (struct fsf_status_read_buffer *)req->data;
125         struct fsf_plogi *els_plogi =
126                 (struct fsf_plogi *) status_buffer->payload;
127
128         zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
129 }
130
131 static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
132 {
133         struct fsf_status_read_buffer *status_buffer =
134                 (struct fsf_status_read_buffer *)req->data;
135         struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
136
137         zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
138 }
139
140 /**
141  * zfcp_fc_incoming_els - handle incoming ELS
142  * @fsf_req - request which contains incoming ELS
143  */
144 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
145 {
146         struct fsf_status_read_buffer *status_buffer =
147                 (struct fsf_status_read_buffer *) fsf_req->data;
148         unsigned int els_type = status_buffer->payload[0];
149
150         zfcp_san_dbf_event_incoming_els(fsf_req);
151         if (els_type == LS_PLOGI)
152                 zfcp_fc_incoming_plogi(fsf_req);
153         else if (els_type == LS_LOGO)
154                 zfcp_fc_incoming_logo(fsf_req);
155         else if (els_type == LS_RSCN)
156                 zfcp_fc_incoming_rscn(fsf_req);
157 }
158
159 static void zfcp_ns_gid_pn_handler(unsigned long data)
160 {
161         struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
162         struct zfcp_send_ct *ct = &gid_pn->ct;
163         struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
164         struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
165         struct zfcp_port *port = gid_pn->port;
166
167         if (ct->status)
168                 goto out;
169         if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
170                 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
171                 goto out;
172         }
173         /* paranoia */
174         if (ct_iu_req->wwpn != port->wwpn)
175                 goto out;
176         /* looks like a valid d_id */
177         port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
178         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
179 out:
180         mempool_free(gid_pn, port->adapter->pool.data_gid_pn);
181 }
182
183 /**
184  * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
185  * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
186  * return: -ENOMEM on error, 0 otherwise
187  */
188 int zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
189 {
190         int ret;
191         struct zfcp_gid_pn_data *gid_pn;
192         struct zfcp_adapter *adapter = erp_action->adapter;
193
194         gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
195         if (!gid_pn)
196                 return -ENOMEM;
197
198         memset(gid_pn, 0, sizeof(*gid_pn));
199
200         /* setup parameters for send generic command */
201         gid_pn->port = erp_action->port;
202         gid_pn->ct.port = adapter->nameserver_port;
203         gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
204         gid_pn->ct.handler_data = (unsigned long) gid_pn;
205         gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
206         gid_pn->ct.req = &gid_pn->req;
207         gid_pn->ct.resp = &gid_pn->resp;
208         gid_pn->ct.req_count = 1;
209         gid_pn->ct.resp_count = 1;
210         sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
211                     sizeof(struct ct_iu_gid_pn_req));
212         sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
213                     sizeof(struct ct_iu_gid_pn_resp));
214
215         /* setup nameserver request */
216         gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
217         gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
218         gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
219         gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
220         gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
221         gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_MAX_SIZE;
222         gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
223
224         ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
225                                erp_action);
226         if (ret)
227                 mempool_free(gid_pn, adapter->pool.data_gid_pn);
228         return ret;
229 }
230
231 /**
232  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
233  * @port: zfcp_port structure
234  * @plogi: plogi payload
235  *
236  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
237  */
238 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
239 {
240         port->maxframe_size = plogi->serv_param.common_serv_param[7] |
241                 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
242         if (plogi->serv_param.class1_serv_param[0] & 0x80)
243                 port->supported_classes |= FC_COS_CLASS1;
244         if (plogi->serv_param.class2_serv_param[0] & 0x80)
245                 port->supported_classes |= FC_COS_CLASS2;
246         if (plogi->serv_param.class3_serv_param[0] & 0x80)
247                 port->supported_classes |= FC_COS_CLASS3;
248         if (plogi->serv_param.class4_serv_param[0] & 0x80)
249                 port->supported_classes |= FC_COS_CLASS4;
250 }
251
252 struct zfcp_els_adisc {
253         struct zfcp_send_els els;
254         struct scatterlist req;
255         struct scatterlist resp;
256         struct zfcp_ls_adisc ls_adisc;
257         struct zfcp_ls_adisc_acc ls_adisc_acc;
258 };
259
260 static void zfcp_fc_adisc_handler(unsigned long data)
261 {
262         struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
263         struct zfcp_port *port = adisc->els.port;
264         struct zfcp_ls_adisc_acc *ls_adisc = &adisc->ls_adisc_acc;
265
266         if (adisc->els.status) {
267                 /* request rejected or timed out */
268                 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
269                 goto out;
270         }
271
272         if (!port->wwnn)
273                 port->wwnn = ls_adisc->wwnn;
274
275         if (port->wwpn != ls_adisc->wwpn)
276                 zfcp_erp_port_reopen(port, 0, 64, NULL);
277
278  out:
279         zfcp_port_put(port);
280         kfree(adisc);
281 }
282
283 static int zfcp_fc_adisc(struct zfcp_port *port)
284 {
285         struct zfcp_els_adisc *adisc;
286         struct zfcp_adapter *adapter = port->adapter;
287
288         adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
289         if (!adisc)
290                 return -ENOMEM;
291
292         adisc->els.req = &adisc->req;
293         adisc->els.resp = &adisc->resp;
294         sg_init_one(adisc->els.req, &adisc->ls_adisc,
295                     sizeof(struct zfcp_ls_adisc));
296         sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
297                     sizeof(struct zfcp_ls_adisc_acc));
298
299         adisc->els.req_count = 1;
300         adisc->els.resp_count = 1;
301         adisc->els.adapter = adapter;
302         adisc->els.port = port;
303         adisc->els.d_id = port->d_id;
304         adisc->els.handler = zfcp_fc_adisc_handler;
305         adisc->els.handler_data = (unsigned long) adisc;
306         adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
307
308         /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
309            without FC-AL-2 capability, so we don't set it */
310         adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
311         adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
312         adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
313
314         return zfcp_fsf_send_els(&adisc->els);
315 }
316
317 /**
318  * zfcp_test_link - lightweight link test procedure
319  * @port: port to be tested
320  *
321  * Test status of a link to a remote port using the ELS command ADISC.
322  * If there is a problem with the remote port, error recovery steps
323  * will be triggered.
324  */
325 void zfcp_test_link(struct zfcp_port *port)
326 {
327         int retval;
328
329         zfcp_port_get(port);
330         retval = zfcp_fc_adisc(port);
331         if (retval == 0 || retval == -EBUSY)
332                 return;
333
334         /* send of ADISC was not possible */
335         zfcp_port_put(port);
336         zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
337 }
338
339 static int zfcp_scan_get_nameserver(struct zfcp_adapter *adapter)
340 {
341         int ret;
342
343         if (!adapter->nameserver_port)
344                 return -EINTR;
345
346         if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
347                                &adapter->nameserver_port->status)) {
348                 ret = zfcp_erp_port_reopen(adapter->nameserver_port, 0, 148,
349                                            NULL);
350                 if (ret)
351                         return ret;
352                 zfcp_erp_wait(adapter);
353                 zfcp_port_put(adapter->nameserver_port);
354         }
355         return !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
356                                   &adapter->nameserver_port->status);
357 }
358
359 static void zfcp_gpn_ft_handler(unsigned long _done)
360 {
361         complete((struct completion *)_done);
362 }
363
364 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
365 {
366         struct scatterlist *sg = &gpn_ft->sg_req;
367
368         kfree(sg_virt(sg)); /* free request buffer */
369         zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
370
371         kfree(gpn_ft);
372 }
373
374 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
375 {
376         struct zfcp_gpn_ft *gpn_ft;
377         struct ct_iu_gpn_ft_req *req;
378
379         gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
380         if (!gpn_ft)
381                 return NULL;
382
383         req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
384         if (!req) {
385                 kfree(gpn_ft);
386                 gpn_ft = NULL;
387                 goto out;
388         }
389         sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
390
391         if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
392                 zfcp_free_sg_env(gpn_ft);
393                 gpn_ft = NULL;
394         }
395 out:
396         return gpn_ft;
397 }
398
399
400 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
401                                   struct zfcp_adapter *adapter)
402 {
403         struct zfcp_send_ct *ct = &gpn_ft->ct;
404         struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
405         struct completion done;
406         int ret;
407
408         /* prepare CT IU for GPN_FT */
409         req->header.revision = ZFCP_CT_REVISION;
410         req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
411         req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
412         req->header.options = ZFCP_CT_SYNCHRONOUS;
413         req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
414         req->header.max_res_size = (sizeof(struct gpn_ft_resp_acc) *
415                                         (ZFCP_GPN_FT_MAX_ENTRIES - 1)) >> 2;
416         req->flags = 0;
417         req->domain_id_scope = 0;
418         req->area_id_scope = 0;
419         req->fc4_type = ZFCP_CT_SCSI_FCP;
420
421         /* prepare zfcp_send_ct */
422         ct->port = adapter->nameserver_port;
423         ct->handler = zfcp_gpn_ft_handler;
424         ct->handler_data = (unsigned long)&done;
425         ct->timeout = 10;
426         ct->req = &gpn_ft->sg_req;
427         ct->resp = gpn_ft->sg_resp;
428         ct->req_count = 1;
429         ct->resp_count = ZFCP_GPN_FT_BUFFERS;
430
431         init_completion(&done);
432         ret = zfcp_fsf_send_ct(ct, NULL, NULL);
433         if (!ret)
434                 wait_for_completion(&done);
435         return ret;
436 }
437
438 static void zfcp_validate_port(struct zfcp_port *port)
439 {
440         struct zfcp_adapter *adapter = port->adapter;
441
442         atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
443
444         if (port == adapter->nameserver_port)
445                 return;
446         if ((port->supported_classes != 0) || (port->units != 0)) {
447                 zfcp_port_put(port);
448                 return;
449         }
450         zfcp_erp_port_shutdown(port, 0, 151, NULL);
451         zfcp_erp_wait(adapter);
452         zfcp_port_put(port);
453         zfcp_port_dequeue(port);
454 }
455
456 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
457 {
458         struct zfcp_send_ct *ct = &gpn_ft->ct;
459         struct scatterlist *sg = gpn_ft->sg_resp;
460         struct ct_hdr *hdr = sg_virt(sg);
461         struct gpn_ft_resp_acc *acc = sg_virt(sg);
462         struct zfcp_adapter *adapter = ct->port->adapter;
463         struct zfcp_port *port, *tmp;
464         u32 d_id;
465         int ret = 0, x;
466
467         if (ct->status)
468                 return -EIO;
469
470         if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
471                 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
472                         return -EAGAIN; /* might be a temporary condition */
473                 return -EIO;
474         }
475
476         if (hdr->max_res_size)
477                 return -E2BIG;
478
479         down(&zfcp_data.config_sema);
480
481         /* first entry is the header */
482         for (x = 1; x < ZFCP_GPN_FT_MAX_ENTRIES; x++) {
483                 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
484                         acc++;
485                 else
486                         acc = sg_virt(++sg);
487
488                 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
489                        acc->port_id[2];
490
491                 /* skip the adapter's port and known remote ports */
492                 if (acc->wwpn == fc_host_port_name(adapter->scsi_host) ||
493                      zfcp_get_port_by_did(adapter, d_id))
494                         continue;
495
496                 port = zfcp_port_enqueue(adapter, acc->wwpn,
497                                          ZFCP_STATUS_PORT_DID_DID |
498                                          ZFCP_STATUS_COMMON_NOESC, d_id);
499                 if (port)
500                         zfcp_erp_port_reopen(port, 0, 149, NULL);
501                 else
502                         ret = -ENOMEM;
503                 if (acc->control & 0x80) /* last entry */
504                         break;
505         }
506
507         zfcp_erp_wait(adapter);
508         list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
509                 zfcp_validate_port(port);
510         up(&zfcp_data.config_sema);
511         return ret;
512 }
513
514 /**
515  * zfcp_scan_ports - scan remote ports and attach new ports
516  * @adapter: pointer to struct zfcp_adapter
517  */
518 int zfcp_scan_ports(struct zfcp_adapter *adapter)
519 {
520         int ret, i;
521         struct zfcp_gpn_ft *gpn_ft;
522
523         zfcp_erp_wait(adapter); /* wait until adapter is finished with ERP */
524         if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
525                 return 0;
526
527         ret = zfcp_scan_get_nameserver(adapter);
528         if (ret)
529                 return ret;
530
531         gpn_ft = zfcp_alloc_sg_env();
532         if (!gpn_ft)
533                 return -ENOMEM;
534
535         for (i = 0; i < 3; i++) {
536                 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
537                 if (!ret) {
538                         ret = zfcp_scan_eval_gpn_ft(gpn_ft);
539                         if (ret == -EAGAIN)
540                                 ssleep(1);
541                         else
542                                 break;
543                 }
544         }
545         zfcp_free_sg_env(gpn_ft);
546
547         return ret;
548 }
549
550
551 void _zfcp_scan_ports_later(struct work_struct *work)
552 {
553         zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
554 }