Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[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.data;
75         fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
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.data;
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 =
136                 (struct fcp_logo *) status_buffer->payload.data;
137
138         zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
139 }
140
141 /**
142  * zfcp_fc_incoming_els - handle incoming ELS
143  * @fsf_req - request which contains incoming ELS
144  */
145 void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
146 {
147         struct fsf_status_read_buffer *status_buffer =
148                 (struct fsf_status_read_buffer *) fsf_req->data;
149         unsigned int els_type = status_buffer->payload.data[0];
150
151         zfcp_san_dbf_event_incoming_els(fsf_req);
152         if (els_type == LS_PLOGI)
153                 zfcp_fc_incoming_plogi(fsf_req);
154         else if (els_type == LS_LOGO)
155                 zfcp_fc_incoming_logo(fsf_req);
156         else if (els_type == LS_RSCN)
157                 zfcp_fc_incoming_rscn(fsf_req);
158 }
159
160 static void zfcp_ns_gid_pn_handler(unsigned long data)
161 {
162         struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
163         struct zfcp_send_ct *ct = &gid_pn->ct;
164         struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
165         struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
166         struct zfcp_port *port = gid_pn->port;
167
168         if (ct->status)
169                 goto out;
170         if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT) {
171                 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
172                 goto out;
173         }
174         /* paranoia */
175         if (ct_iu_req->wwpn != port->wwpn)
176                 goto out;
177         /* looks like a valid d_id */
178         port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
179         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
180 out:
181         mempool_free(gid_pn, port->adapter->pool.data_gid_pn);
182 }
183
184 /**
185  * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
186  * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
187  * return: -ENOMEM on error, 0 otherwise
188  */
189 int zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
190 {
191         int ret;
192         struct zfcp_gid_pn_data *gid_pn;
193         struct zfcp_adapter *adapter = erp_action->adapter;
194
195         gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
196         if (!gid_pn)
197                 return -ENOMEM;
198
199         memset(gid_pn, 0, sizeof(*gid_pn));
200
201         /* setup parameters for send generic command */
202         gid_pn->port = erp_action->port;
203         gid_pn->ct.port = adapter->nameserver_port;
204         gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
205         gid_pn->ct.handler_data = (unsigned long) gid_pn;
206         gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
207         gid_pn->ct.req = &gid_pn->req;
208         gid_pn->ct.resp = &gid_pn->resp;
209         gid_pn->ct.req_count = 1;
210         gid_pn->ct.resp_count = 1;
211         sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
212                     sizeof(struct ct_iu_gid_pn_req));
213         sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
214                     sizeof(struct ct_iu_gid_pn_resp));
215
216         /* setup nameserver request */
217         gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
218         gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
219         gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
220         gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
221         gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
222         gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_MAX_SIZE;
223         gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
224
225         ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
226                                erp_action);
227         if (ret)
228                 mempool_free(gid_pn, adapter->pool.data_gid_pn);
229         return ret;
230 }
231
232 /**
233  * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
234  * @port: zfcp_port structure
235  * @plogi: plogi payload
236  *
237  * Evaluate PLOGI playload and copy important fields into zfcp_port structure
238  */
239 void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
240 {
241         port->maxframe_size = plogi->serv_param.common_serv_param[7] |
242                 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
243         if (plogi->serv_param.class1_serv_param[0] & 0x80)
244                 port->supported_classes |= FC_COS_CLASS1;
245         if (plogi->serv_param.class2_serv_param[0] & 0x80)
246                 port->supported_classes |= FC_COS_CLASS2;
247         if (plogi->serv_param.class3_serv_param[0] & 0x80)
248                 port->supported_classes |= FC_COS_CLASS3;
249         if (plogi->serv_param.class4_serv_param[0] & 0x80)
250                 port->supported_classes |= FC_COS_CLASS4;
251 }
252
253 struct zfcp_els_adisc {
254         struct zfcp_send_els els;
255         struct scatterlist req;
256         struct scatterlist resp;
257         struct zfcp_ls_adisc ls_adisc;
258         struct zfcp_ls_adisc_acc ls_adisc_acc;
259 };
260
261 static void zfcp_fc_adisc_handler(unsigned long data)
262 {
263         struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
264         struct zfcp_port *port = adisc->els.port;
265         struct zfcp_ls_adisc_acc *ls_adisc = &adisc->ls_adisc_acc;
266
267         if (adisc->els.status) {
268                 /* request rejected or timed out */
269                 zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
270                 goto out;
271         }
272
273         if (!port->wwnn)
274                 port->wwnn = ls_adisc->wwnn;
275
276         if (port->wwpn != ls_adisc->wwpn)
277                 zfcp_erp_port_reopen(port, 0, 64, NULL);
278
279  out:
280         zfcp_port_put(port);
281         kfree(adisc);
282 }
283
284 static int zfcp_fc_adisc(struct zfcp_port *port)
285 {
286         struct zfcp_els_adisc *adisc;
287         struct zfcp_adapter *adapter = port->adapter;
288
289         adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
290         if (!adisc)
291                 return -ENOMEM;
292
293         adisc->els.req = &adisc->req;
294         adisc->els.resp = &adisc->resp;
295         sg_init_one(adisc->els.req, &adisc->ls_adisc,
296                     sizeof(struct zfcp_ls_adisc));
297         sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
298                     sizeof(struct zfcp_ls_adisc_acc));
299
300         adisc->els.req_count = 1;
301         adisc->els.resp_count = 1;
302         adisc->els.adapter = adapter;
303         adisc->els.port = port;
304         adisc->els.d_id = port->d_id;
305         adisc->els.handler = zfcp_fc_adisc_handler;
306         adisc->els.handler_data = (unsigned long) adisc;
307         adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
308
309         /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
310            without FC-AL-2 capability, so we don't set it */
311         adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
312         adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
313         adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
314
315         return zfcp_fsf_send_els(&adisc->els);
316 }
317
318 /**
319  * zfcp_test_link - lightweight link test procedure
320  * @port: port to be tested
321  *
322  * Test status of a link to a remote port using the ELS command ADISC.
323  * If there is a problem with the remote port, error recovery steps
324  * will be triggered.
325  */
326 void zfcp_test_link(struct zfcp_port *port)
327 {
328         int retval;
329
330         zfcp_port_get(port);
331         retval = zfcp_fc_adisc(port);
332         if (retval == 0)
333                 return;
334
335         /* send of ADISC was not possible */
336         zfcp_port_put(port);
337         if (retval != -EBUSY)
338                 zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
339 }
340
341 static int zfcp_scan_get_nameserver(struct zfcp_adapter *adapter)
342 {
343         int ret;
344
345         if (!adapter->nameserver_port)
346                 return -EINTR;
347
348         if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
349                                &adapter->nameserver_port->status)) {
350                 ret = zfcp_erp_port_reopen(adapter->nameserver_port, 0, 148,
351                                            NULL);
352                 if (ret)
353                         return ret;
354                 zfcp_erp_wait(adapter);
355         }
356         return !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
357                                   &adapter->nameserver_port->status);
358 }
359
360 static void zfcp_gpn_ft_handler(unsigned long _done)
361 {
362         complete((struct completion *)_done);
363 }
364
365 static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
366 {
367         struct scatterlist *sg = &gpn_ft->sg_req;
368
369         kfree(sg_virt(sg)); /* free request buffer */
370         zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
371
372         kfree(gpn_ft);
373 }
374
375 static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
376 {
377         struct zfcp_gpn_ft *gpn_ft;
378         struct ct_iu_gpn_ft_req *req;
379
380         gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
381         if (!gpn_ft)
382                 return NULL;
383
384         req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
385         if (!req) {
386                 kfree(gpn_ft);
387                 gpn_ft = NULL;
388                 goto out;
389         }
390         sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
391
392         if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
393                 zfcp_free_sg_env(gpn_ft);
394                 gpn_ft = NULL;
395         }
396 out:
397         return gpn_ft;
398 }
399
400
401 static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
402                                   struct zfcp_adapter *adapter)
403 {
404         struct zfcp_send_ct *ct = &gpn_ft->ct;
405         struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
406         struct completion done;
407         int ret;
408
409         /* prepare CT IU for GPN_FT */
410         req->header.revision = ZFCP_CT_REVISION;
411         req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
412         req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
413         req->header.options = ZFCP_CT_SYNCHRONOUS;
414         req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
415         req->header.max_res_size = (sizeof(struct gpn_ft_resp_acc) *
416                                         (ZFCP_GPN_FT_MAX_ENTRIES - 1)) >> 2;
417         req->flags = 0;
418         req->domain_id_scope = 0;
419         req->area_id_scope = 0;
420         req->fc4_type = ZFCP_CT_SCSI_FCP;
421
422         /* prepare zfcp_send_ct */
423         ct->port = adapter->nameserver_port;
424         ct->handler = zfcp_gpn_ft_handler;
425         ct->handler_data = (unsigned long)&done;
426         ct->timeout = 10;
427         ct->req = &gpn_ft->sg_req;
428         ct->resp = gpn_ft->sg_resp;
429         ct->req_count = 1;
430         ct->resp_count = ZFCP_GPN_FT_BUFFERS;
431
432         init_completion(&done);
433         ret = zfcp_fsf_send_ct(ct, NULL, NULL);
434         if (!ret)
435                 wait_for_completion(&done);
436         return ret;
437 }
438
439 static void zfcp_validate_port(struct zfcp_port *port)
440 {
441         struct zfcp_adapter *adapter = port->adapter;
442
443         atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
444
445         if (port == adapter->nameserver_port)
446                 return;
447         if ((port->supported_classes != 0) || (port->units != 0)) {
448                 zfcp_port_put(port);
449                 return;
450         }
451         zfcp_erp_port_shutdown(port, 0, 151, NULL);
452         zfcp_erp_wait(adapter);
453         zfcp_port_put(port);
454         zfcp_port_dequeue(port);
455 }
456
457 static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
458 {
459         struct zfcp_send_ct *ct = &gpn_ft->ct;
460         struct scatterlist *sg = gpn_ft->sg_resp;
461         struct ct_hdr *hdr = sg_virt(sg);
462         struct gpn_ft_resp_acc *acc = sg_virt(sg);
463         struct zfcp_adapter *adapter = ct->port->adapter;
464         struct zfcp_port *port, *tmp;
465         u32 d_id;
466         int ret = 0, x, last = 0;
467
468         if (ct->status)
469                 return -EIO;
470
471         if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
472                 if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
473                         return -EAGAIN; /* might be a temporary condition */
474                 return -EIO;
475         }
476
477         if (hdr->max_res_size)
478                 return -E2BIG;
479
480         down(&zfcp_data.config_sema);
481
482         /* first entry is the header */
483         for (x = 1; x < ZFCP_GPN_FT_MAX_ENTRIES && !last; x++) {
484                 if (x % (ZFCP_GPN_FT_ENTRIES + 1))
485                         acc++;
486                 else
487                         acc = sg_virt(++sg);
488
489                 last = acc->control & 0x80;
490                 d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
491                        acc->port_id[2];
492
493                 /* skip the adapter's port and known remote ports */
494                 if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
495                         continue;
496                 port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
497                 if (port) {
498                         zfcp_port_get(port);
499                         continue;
500                 }
501
502                 port = zfcp_port_enqueue(adapter, acc->wwpn,
503                                          ZFCP_STATUS_PORT_DID_DID |
504                                          ZFCP_STATUS_COMMON_NOESC, d_id);
505                 if (IS_ERR(port))
506                         ret = PTR_ERR(port);
507                 else
508                         zfcp_erp_port_reopen(port, 0, 149, NULL);
509         }
510
511         zfcp_erp_wait(adapter);
512         list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
513                 zfcp_validate_port(port);
514         up(&zfcp_data.config_sema);
515         return ret;
516 }
517
518 /**
519  * zfcp_scan_ports - scan remote ports and attach new ports
520  * @adapter: pointer to struct zfcp_adapter
521  */
522 int zfcp_scan_ports(struct zfcp_adapter *adapter)
523 {
524         int ret, i;
525         struct zfcp_gpn_ft *gpn_ft;
526
527         zfcp_erp_wait(adapter); /* wait until adapter is finished with ERP */
528         if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
529                 return 0;
530
531         ret = zfcp_scan_get_nameserver(adapter);
532         if (ret)
533                 return ret;
534
535         gpn_ft = zfcp_alloc_sg_env();
536         if (!gpn_ft)
537                 return -ENOMEM;
538
539         for (i = 0; i < 3; i++) {
540                 ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
541                 if (!ret) {
542                         ret = zfcp_scan_eval_gpn_ft(gpn_ft);
543                         if (ret == -EAGAIN)
544                                 ssleep(1);
545                         else
546                                 break;
547                 }
548         }
549         zfcp_free_sg_env(gpn_ft);
550
551         return ret;
552 }
553
554
555 void _zfcp_scan_ports_later(struct work_struct *work)
556 {
557         zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
558 }