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