1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/timer.h>
36#include <linux/slab.h>
37#include <linux/err.h>
38#include <linux/export.h>
39#include <asm/unaligned.h>
40
41#include <scsi/fc/fc_gs.h>
42
43#include <scsi/libfc.h>
44
45#include "fc_libfc.h"
46
47#define FC_DISC_RETRY_LIMIT 3
48#define FC_DISC_RETRY_DELAY 500UL
49
50static void fc_disc_gpn_ft_req(struct fc_disc *);
51static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
52static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
53static void fc_disc_timeout(struct work_struct *);
54static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
55static void fc_disc_restart(struct fc_disc *);
56
57
58
59
60
61static void fc_disc_stop_rports(struct fc_disc *disc)
62{
63 struct fc_lport *lport;
64 struct fc_rport_priv *rdata;
65
66 lport = fc_disc_lport(disc);
67 lockdep_assert_held(&disc->disc_mutex);
68
69 list_for_each_entry(rdata, &disc->rports, peers) {
70 if (kref_get_unless_zero(&rdata->kref)) {
71 lport->tt.rport_logoff(rdata);
72 kref_put(&rdata->kref, lport->tt.rport_destroy);
73 }
74 }
75}
76
77
78
79
80
81
82static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp)
83{
84 struct fc_lport *lport;
85 struct fc_els_rscn *rp;
86 struct fc_els_rscn_page *pp;
87 struct fc_seq_els_data rjt_data;
88 unsigned int len;
89 int redisc = 0;
90 enum fc_els_rscn_ev_qual ev_qual;
91 enum fc_els_rscn_addr_fmt fmt;
92 LIST_HEAD(disc_ports);
93 struct fc_disc_port *dp, *next;
94
95 lockdep_assert_held(&disc->disc_mutex);
96
97 lport = fc_disc_lport(disc);
98
99 FC_DISC_DBG(disc, "Received an RSCN event\n");
100
101
102 rp = fc_frame_payload_get(fp, sizeof(*rp));
103 if (!rp)
104 goto reject;
105
106 if (rp->rscn_page_len != sizeof(*pp))
107 goto reject;
108
109 len = ntohs(rp->rscn_plen);
110 if (len < sizeof(*rp))
111 goto reject;
112
113 rp = fc_frame_payload_get(fp, len);
114 if (!rp)
115 goto reject;
116
117 len -= sizeof(*rp);
118 if (len % sizeof(*pp))
119 goto reject;
120
121 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
122 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
123 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
124 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
125 fmt &= ELS_RSCN_ADDR_FMT_MASK;
126
127
128
129
130 switch (fmt) {
131 case ELS_ADDR_FMT_PORT:
132 FC_DISC_DBG(disc, "Port address format for port "
133 "(%6.6x)\n", ntoh24(pp->rscn_fid));
134 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
135 if (!dp) {
136 redisc = 1;
137 break;
138 }
139 dp->lp = lport;
140 dp->port_id = ntoh24(pp->rscn_fid);
141 list_add_tail(&dp->peers, &disc_ports);
142 break;
143 case ELS_ADDR_FMT_AREA:
144 case ELS_ADDR_FMT_DOM:
145 case ELS_ADDR_FMT_FAB:
146 default:
147 FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
148 redisc = 1;
149 break;
150 }
151 }
152 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
153
154
155
156
157
158
159
160 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
161 list_del(&dp->peers);
162 if (!redisc)
163 redisc = fc_disc_single(lport, dp);
164 kfree(dp);
165 }
166 if (redisc) {
167 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
168 fc_disc_restart(disc);
169 } else {
170 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
171 "redisc %d state %d in_prog %d\n",
172 redisc, lport->state, disc->pending);
173 }
174 fc_frame_free(fp);
175 return;
176reject:
177 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
178 rjt_data.reason = ELS_RJT_LOGIC;
179 rjt_data.explan = ELS_EXPL_NONE;
180 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
181 fc_frame_free(fp);
182}
183
184
185
186
187
188
189
190
191
192
193static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp)
194{
195 u8 op;
196 struct fc_disc *disc = &lport->disc;
197
198 op = fc_frame_payload_op(fp);
199 switch (op) {
200 case ELS_RSCN:
201 mutex_lock(&disc->disc_mutex);
202 fc_disc_recv_rscn_req(disc, fp);
203 mutex_unlock(&disc->disc_mutex);
204 break;
205 default:
206 FC_DISC_DBG(disc, "Received an unsupported request, "
207 "the opcode is (%x)\n", op);
208 fc_frame_free(fp);
209 break;
210 }
211}
212
213
214
215
216
217static void fc_disc_restart(struct fc_disc *disc)
218{
219 lockdep_assert_held(&disc->disc_mutex);
220
221 if (!disc->disc_callback)
222 return;
223
224 FC_DISC_DBG(disc, "Restarting discovery\n");
225
226 disc->requested = 1;
227 if (disc->pending)
228 return;
229
230
231
232
233
234
235 disc->disc_id = (disc->disc_id + 2) | 1;
236 disc->retry_count = 0;
237 fc_disc_gpn_ft_req(disc);
238}
239
240
241
242
243
244
245static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
246 enum fc_disc_event),
247 struct fc_lport *lport)
248{
249 struct fc_disc *disc = &lport->disc;
250
251
252
253
254
255
256 mutex_lock(&disc->disc_mutex);
257 disc->disc_callback = disc_callback;
258 fc_disc_restart(disc);
259 mutex_unlock(&disc->disc_mutex);
260}
261
262
263
264
265
266
267static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
268{
269 struct fc_lport *lport = fc_disc_lport(disc);
270 struct fc_rport_priv *rdata;
271
272 lockdep_assert_held(&disc->disc_mutex);
273 FC_DISC_DBG(disc, "Discovery complete\n");
274
275 disc->pending = 0;
276 if (disc->requested) {
277 fc_disc_restart(disc);
278 return;
279 }
280
281
282
283
284
285
286
287
288
289
290 list_for_each_entry(rdata, &disc->rports, peers) {
291 if (!kref_get_unless_zero(&rdata->kref))
292 continue;
293 if (rdata->disc_id) {
294 if (rdata->disc_id == disc->disc_id)
295 lport->tt.rport_login(rdata);
296 else
297 lport->tt.rport_logoff(rdata);
298 }
299 kref_put(&rdata->kref, lport->tt.rport_destroy);
300 }
301 mutex_unlock(&disc->disc_mutex);
302 disc->disc_callback(lport, event);
303 mutex_lock(&disc->disc_mutex);
304}
305
306
307
308
309
310
311static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
312{
313 struct fc_lport *lport = fc_disc_lport(disc);
314 unsigned long delay = 0;
315
316 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
317 PTR_ERR(fp), disc->retry_count,
318 FC_DISC_RETRY_LIMIT);
319
320 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
321
322
323
324
325 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
326
327 if (!fp)
328 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
329 else {
330 delay = msecs_to_jiffies(lport->e_d_tov);
331
332
333 if (!disc->retry_count)
334 delay /= 4;
335 }
336 disc->retry_count++;
337 schedule_delayed_work(&disc->disc_work, delay);
338 } else
339 fc_disc_done(disc, DISC_EV_FAILED);
340 } else if (PTR_ERR(fp) == -FC_EX_CLOSED) {
341
342
343
344
345
346 disc->pending = 0;
347 }
348}
349
350
351
352
353
354static void fc_disc_gpn_ft_req(struct fc_disc *disc)
355{
356 struct fc_frame *fp;
357 struct fc_lport *lport = fc_disc_lport(disc);
358
359 lockdep_assert_held(&disc->disc_mutex);
360
361 WARN_ON(!fc_lport_test_ready(lport));
362
363 disc->pending = 1;
364 disc->requested = 0;
365
366 disc->buf_len = 0;
367 disc->seq_count = 0;
368 fp = fc_frame_alloc(lport,
369 sizeof(struct fc_ct_hdr) +
370 sizeof(struct fc_ns_gid_ft));
371 if (!fp)
372 goto err;
373
374 if (lport->tt.elsct_send(lport, 0, fp,
375 FC_NS_GPN_FT,
376 fc_disc_gpn_ft_resp,
377 disc, 3 * lport->r_a_tov))
378 return;
379err:
380 fc_disc_error(disc, NULL);
381}
382
383
384
385
386
387
388
389
390
391static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
392{
393 struct fc_lport *lport;
394 struct fc_gpn_ft_resp *np;
395 char *bp;
396 size_t plen;
397 size_t tlen;
398 int error = 0;
399 struct fc_rport_identifiers ids;
400 struct fc_rport_priv *rdata;
401
402 lport = fc_disc_lport(disc);
403 disc->seq_count++;
404
405
406
407
408 bp = buf;
409 plen = len;
410 np = (struct fc_gpn_ft_resp *)bp;
411 tlen = disc->buf_len;
412 disc->buf_len = 0;
413 if (tlen) {
414 WARN_ON(tlen >= sizeof(*np));
415 plen = sizeof(*np) - tlen;
416 WARN_ON(plen <= 0);
417 WARN_ON(plen >= sizeof(*np));
418 if (plen > len)
419 plen = len;
420 np = &disc->partial_buf;
421 memcpy((char *)np + tlen, bp, plen);
422
423
424
425
426
427 bp -= tlen;
428 len += tlen;
429 plen += tlen;
430 disc->buf_len = (unsigned char) plen;
431 if (plen == sizeof(*np))
432 disc->buf_len = 0;
433 }
434
435
436
437
438
439
440
441
442 while (plen >= sizeof(*np)) {
443 ids.port_id = ntoh24(np->fp_fid);
444 ids.port_name = ntohll(np->fp_wwpn);
445
446 if (ids.port_id != lport->port_id &&
447 ids.port_name != lport->wwpn) {
448 rdata = lport->tt.rport_create(lport, ids.port_id);
449 if (rdata) {
450 rdata->ids.port_name = ids.port_name;
451 rdata->disc_id = disc->disc_id;
452 } else {
453 printk(KERN_WARNING "libfc: Failed to allocate "
454 "memory for the newly discovered port "
455 "(%6.6x)\n", ids.port_id);
456 error = -ENOMEM;
457 }
458 }
459
460 if (np->fp_flags & FC_NS_FID_LAST) {
461 fc_disc_done(disc, DISC_EV_SUCCESS);
462 len = 0;
463 break;
464 }
465 len -= sizeof(*np);
466 bp += sizeof(*np);
467 np = (struct fc_gpn_ft_resp *)bp;
468 plen = len;
469 }
470
471
472
473
474 if (error == 0 && len > 0 && len < sizeof(*np)) {
475 if (np != &disc->partial_buf) {
476 FC_DISC_DBG(disc, "Partial buffer remains "
477 "for discovery\n");
478 memcpy(&disc->partial_buf, np, len);
479 }
480 disc->buf_len = (unsigned char) len;
481 }
482 return error;
483}
484
485
486
487
488
489static void fc_disc_timeout(struct work_struct *work)
490{
491 struct fc_disc *disc = container_of(work,
492 struct fc_disc,
493 disc_work.work);
494 mutex_lock(&disc->disc_mutex);
495 fc_disc_gpn_ft_req(disc);
496 mutex_unlock(&disc->disc_mutex);
497}
498
499
500
501
502
503
504
505
506
507
508static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
509 void *disc_arg)
510{
511 struct fc_disc *disc = disc_arg;
512 struct fc_ct_hdr *cp;
513 struct fc_frame_header *fh;
514 enum fc_disc_event event = DISC_EV_NONE;
515 unsigned int seq_cnt;
516 unsigned int len;
517 int error = 0;
518
519 mutex_lock(&disc->disc_mutex);
520 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
521
522 if (IS_ERR(fp)) {
523 fc_disc_error(disc, fp);
524 mutex_unlock(&disc->disc_mutex);
525 return;
526 }
527
528 WARN_ON(!fc_frame_is_linear(fp));
529 fh = fc_frame_header_get(fp);
530 len = fr_len(fp) - sizeof(*fh);
531 seq_cnt = ntohs(fh->fh_seq_cnt);
532 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
533 cp = fc_frame_payload_get(fp, sizeof(*cp));
534 if (!cp) {
535 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
536 fr_len(fp));
537 event = DISC_EV_FAILED;
538 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
539
540
541 len -= sizeof(*cp);
542 error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
543 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
544 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
545 "(check zoning)\n", cp->ct_reason,
546 cp->ct_explan);
547 event = DISC_EV_FAILED;
548 if (cp->ct_reason == FC_FS_RJT_UNABL &&
549 cp->ct_explan == FC_FS_EXP_FTNR)
550 event = DISC_EV_SUCCESS;
551 } else {
552 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
553 "%x\n", ntohs(cp->ct_cmd));
554 event = DISC_EV_FAILED;
555 }
556 } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
557 error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
558 } else {
559 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
560 "seq_cnt %x expected %x sof %x eof %x\n",
561 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
562 event = DISC_EV_FAILED;
563 }
564 if (error)
565 fc_disc_error(disc, fp);
566 else if (event != DISC_EV_NONE)
567 fc_disc_done(disc, event);
568 fc_frame_free(fp);
569 mutex_unlock(&disc->disc_mutex);
570}
571
572
573
574
575
576
577
578
579
580static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
581 void *rdata_arg)
582{
583 struct fc_rport_priv *rdata = rdata_arg;
584 struct fc_rport_priv *new_rdata;
585 struct fc_lport *lport;
586 struct fc_disc *disc;
587 struct fc_ct_hdr *cp;
588 struct fc_ns_gid_pn *pn;
589 u64 port_name;
590
591 lport = rdata->local_port;
592 disc = &lport->disc;
593
594 if (PTR_ERR(fp) == -FC_EX_CLOSED)
595 goto out;
596 if (IS_ERR(fp))
597 goto redisc;
598
599 cp = fc_frame_payload_get(fp, sizeof(*cp));
600 if (!cp)
601 goto redisc;
602 if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
603 if (fr_len(fp) < sizeof(struct fc_frame_header) +
604 sizeof(*cp) + sizeof(*pn))
605 goto redisc;
606 pn = (struct fc_ns_gid_pn *)(cp + 1);
607 port_name = get_unaligned_be64(&pn->fn_wwpn);
608 mutex_lock(&rdata->rp_mutex);
609 if (rdata->ids.port_name == -1)
610 rdata->ids.port_name = port_name;
611 else if (rdata->ids.port_name != port_name) {
612 FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. "
613 "Port-id %6.6x wwpn %16.16llx\n",
614 rdata->ids.port_id, port_name);
615 mutex_unlock(&rdata->rp_mutex);
616 lport->tt.rport_logoff(rdata);
617 mutex_lock(&lport->disc.disc_mutex);
618 new_rdata = lport->tt.rport_create(lport,
619 rdata->ids.port_id);
620 mutex_unlock(&lport->disc.disc_mutex);
621 if (new_rdata) {
622 new_rdata->disc_id = disc->disc_id;
623 lport->tt.rport_login(new_rdata);
624 }
625 goto out;
626 }
627 rdata->disc_id = disc->disc_id;
628 mutex_unlock(&rdata->rp_mutex);
629 lport->tt.rport_login(rdata);
630 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
631 FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
632 cp->ct_reason, cp->ct_explan);
633 lport->tt.rport_logoff(rdata);
634 } else {
635 FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
636 ntohs(cp->ct_cmd));
637redisc:
638 mutex_lock(&disc->disc_mutex);
639 fc_disc_restart(disc);
640 mutex_unlock(&disc->disc_mutex);
641 }
642out:
643 kref_put(&rdata->kref, lport->tt.rport_destroy);
644}
645
646
647
648
649
650
651
652
653static int fc_disc_gpn_id_req(struct fc_lport *lport,
654 struct fc_rport_priv *rdata)
655{
656 struct fc_frame *fp;
657
658 lockdep_assert_held(&lport->disc.disc_mutex);
659 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
660 sizeof(struct fc_ns_fid));
661 if (!fp)
662 return -ENOMEM;
663 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
664 fc_disc_gpn_id_resp, rdata,
665 3 * lport->r_a_tov))
666 return -ENOMEM;
667 kref_get(&rdata->kref);
668 return 0;
669}
670
671
672
673
674
675
676static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
677{
678 struct fc_rport_priv *rdata;
679
680 lockdep_assert_held(&lport->disc.disc_mutex);
681
682 rdata = lport->tt.rport_create(lport, dp->port_id);
683 if (!rdata)
684 return -ENOMEM;
685 rdata->disc_id = 0;
686 return fc_disc_gpn_id_req(lport, rdata);
687}
688
689
690
691
692
693static void fc_disc_stop(struct fc_lport *lport)
694{
695 struct fc_disc *disc = &lport->disc;
696
697 if (disc->pending)
698 cancel_delayed_work_sync(&disc->disc_work);
699 mutex_lock(&disc->disc_mutex);
700 fc_disc_stop_rports(disc);
701 mutex_unlock(&disc->disc_mutex);
702}
703
704
705
706
707
708
709
710
711static void fc_disc_stop_final(struct fc_lport *lport)
712{
713 fc_disc_stop(lport);
714 lport->tt.rport_flush_queue();
715}
716
717
718
719
720
721
722void fc_disc_config(struct fc_lport *lport, void *priv)
723{
724 struct fc_disc *disc = &lport->disc;
725
726 if (!lport->tt.disc_start)
727 lport->tt.disc_start = fc_disc_start;
728
729 if (!lport->tt.disc_stop)
730 lport->tt.disc_stop = fc_disc_stop;
731
732 if (!lport->tt.disc_stop_final)
733 lport->tt.disc_stop_final = fc_disc_stop_final;
734
735 if (!lport->tt.disc_recv_req)
736 lport->tt.disc_recv_req = fc_disc_recv_req;
737
738 disc = &lport->disc;
739
740 disc->priv = priv;
741}
742EXPORT_SYMBOL(fc_disc_config);
743
744
745
746
747
748void fc_disc_init(struct fc_lport *lport)
749{
750 struct fc_disc *disc = &lport->disc;
751
752 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
753 mutex_init(&disc->disc_mutex);
754 INIT_LIST_HEAD(&disc->rports);
755}
756EXPORT_SYMBOL(fc_disc_init);
757