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