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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90#include <linux/timer.h>
91#include <asm/unaligned.h>
92
93#include <scsi/fc/fc_gs.h>
94
95#include <scsi/libfc.h>
96#include <scsi/fc_encode.h>
97
98
99#define FC_LOCAL_PTP_FID_LO 0x010101
100#define FC_LOCAL_PTP_FID_HI 0x010102
101
102#define DNS_DELAY 3
103
104static void fc_lport_error(struct fc_lport *, struct fc_frame *);
105
106static void fc_lport_enter_reset(struct fc_lport *);
107static void fc_lport_enter_flogi(struct fc_lport *);
108static void fc_lport_enter_dns(struct fc_lport *);
109static void fc_lport_enter_rpn_id(struct fc_lport *);
110static void fc_lport_enter_rft_id(struct fc_lport *);
111static void fc_lport_enter_scr(struct fc_lport *);
112static void fc_lport_enter_ready(struct fc_lport *);
113static void fc_lport_enter_logo(struct fc_lport *);
114
115static const char *fc_lport_state_names[] = {
116 [LPORT_ST_DISABLED] = "disabled",
117 [LPORT_ST_FLOGI] = "FLOGI",
118 [LPORT_ST_DNS] = "dNS",
119 [LPORT_ST_RPN_ID] = "RPN_ID",
120 [LPORT_ST_RFT_ID] = "RFT_ID",
121 [LPORT_ST_SCR] = "SCR",
122 [LPORT_ST_READY] = "Ready",
123 [LPORT_ST_LOGO] = "LOGO",
124 [LPORT_ST_RESET] = "reset",
125};
126
127static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
128{
129 fc_frame_free(fp);
130 return 0;
131}
132
133
134
135
136
137
138
139
140
141
142static void fc_lport_rport_callback(struct fc_lport *lport,
143 struct fc_rport_priv *rdata,
144 enum fc_rport_event event)
145{
146 FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
147 rdata->ids.port_id);
148
149 mutex_lock(&lport->lp_mutex);
150 switch (event) {
151 case RPORT_EV_READY:
152 if (lport->state == LPORT_ST_DNS) {
153 lport->dns_rp = rdata;
154 fc_lport_enter_rpn_id(lport);
155 } else {
156 FC_LPORT_DBG(lport, "Received an READY event "
157 "on port (%6x) for the directory "
158 "server, but the lport is not "
159 "in the DNS state, it's in the "
160 "%d state", rdata->ids.port_id,
161 lport->state);
162 lport->tt.rport_logoff(rdata);
163 }
164 break;
165 case RPORT_EV_LOGO:
166 case RPORT_EV_FAILED:
167 case RPORT_EV_STOP:
168 lport->dns_rp = NULL;
169 break;
170 case RPORT_EV_NONE:
171 break;
172 }
173 mutex_unlock(&lport->lp_mutex);
174}
175
176
177
178
179
180static const char *fc_lport_state(struct fc_lport *lport)
181{
182 const char *cp;
183
184 cp = fc_lport_state_names[lport->state];
185 if (!cp)
186 cp = "unknown";
187 return cp;
188}
189
190
191
192
193
194
195
196
197static void fc_lport_ptp_setup(struct fc_lport *lport,
198 u32 remote_fid, u64 remote_wwpn,
199 u64 remote_wwnn)
200{
201 mutex_lock(&lport->disc.disc_mutex);
202 if (lport->ptp_rp)
203 lport->tt.rport_logoff(lport->ptp_rp);
204 lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
205 lport->ptp_rp->ids.port_name = remote_wwpn;
206 lport->ptp_rp->ids.node_name = remote_wwnn;
207 mutex_unlock(&lport->disc.disc_mutex);
208
209 lport->tt.rport_login(lport->ptp_rp);
210
211 fc_lport_enter_ready(lport);
212}
213
214void fc_get_host_port_type(struct Scsi_Host *shost)
215{
216
217 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
218}
219EXPORT_SYMBOL(fc_get_host_port_type);
220
221void fc_get_host_port_state(struct Scsi_Host *shost)
222{
223 struct fc_lport *lp = shost_priv(shost);
224
225 if (lp->link_up)
226 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
227 else
228 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
229}
230EXPORT_SYMBOL(fc_get_host_port_state);
231
232void fc_get_host_speed(struct Scsi_Host *shost)
233{
234 struct fc_lport *lport = shost_priv(shost);
235
236 fc_host_speed(shost) = lport->link_speed;
237}
238EXPORT_SYMBOL(fc_get_host_speed);
239
240struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
241{
242 struct fc_host_statistics *fcoe_stats;
243 struct fc_lport *lp = shost_priv(shost);
244 struct timespec v0, v1;
245 unsigned int cpu;
246
247 fcoe_stats = &lp->host_stats;
248 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
249
250 jiffies_to_timespec(jiffies, &v0);
251 jiffies_to_timespec(lp->boot_time, &v1);
252 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
253
254 for_each_possible_cpu(cpu) {
255 struct fcoe_dev_stats *stats;
256
257 stats = per_cpu_ptr(lp->dev_stats, cpu);
258
259 fcoe_stats->tx_frames += stats->TxFrames;
260 fcoe_stats->tx_words += stats->TxWords;
261 fcoe_stats->rx_frames += stats->RxFrames;
262 fcoe_stats->rx_words += stats->RxWords;
263 fcoe_stats->error_frames += stats->ErrorFrames;
264 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
265 fcoe_stats->fcp_input_requests += stats->InputRequests;
266 fcoe_stats->fcp_output_requests += stats->OutputRequests;
267 fcoe_stats->fcp_control_requests += stats->ControlRequests;
268 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
269 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
270 fcoe_stats->link_failure_count += stats->LinkFailureCount;
271 }
272 fcoe_stats->lip_count = -1;
273 fcoe_stats->nos_count = -1;
274 fcoe_stats->loss_of_sync_count = -1;
275 fcoe_stats->loss_of_signal_count = -1;
276 fcoe_stats->prim_seq_protocol_err_count = -1;
277 fcoe_stats->dumped_frames = -1;
278 return fcoe_stats;
279}
280EXPORT_SYMBOL(fc_get_host_stats);
281
282
283
284
285static void
286fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
287 unsigned int op)
288{
289 struct fc_els_csp *sp;
290 struct fc_els_cssp *cp;
291
292 memset(flogi, 0, sizeof(*flogi));
293 flogi->fl_cmd = (u8) op;
294 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
295 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
296 sp = &flogi->fl_csp;
297 sp->sp_hi_ver = 0x20;
298 sp->sp_lo_ver = 0x20;
299 sp->sp_bb_cred = htons(10);
300 sp->sp_bb_data = htons((u16) lport->mfs);
301 cp = &flogi->fl_cssp[3 - 1];
302 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
303 if (op != ELS_FLOGI) {
304 sp->sp_features = htons(FC_SP_FT_CIRO);
305 sp->sp_tot_seq = htons(255);
306 sp->sp_rel_off = htons(0x1f);
307 sp->sp_e_d_tov = htonl(lport->e_d_tov);
308
309 cp->cp_rdfs = htons((u16) lport->mfs);
310 cp->cp_con_seq = htons(255);
311 cp->cp_open_seq = 1;
312 }
313}
314
315
316
317
318static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
319{
320 __be32 *mp;
321
322 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
323 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
324}
325
326
327
328
329
330
331
332
333
334
335static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
336 struct fc_lport *lport)
337{
338 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
339 fc_lport_state(lport));
340
341 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
342 fc_frame_free(fp);
343}
344
345
346
347
348
349
350
351
352
353
354static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
355 struct fc_lport *lport)
356{
357 struct fc_frame *fp;
358 struct fc_exch *ep = fc_seq_exch(sp);
359 unsigned int len;
360 void *pp;
361 void *dp;
362 u32 f_ctl;
363
364 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
365 fc_lport_state(lport));
366
367 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
368 pp = fc_frame_payload_get(in_fp, len);
369
370 if (len < sizeof(__be32))
371 len = sizeof(__be32);
372
373 fp = fc_frame_alloc(lport, len);
374 if (fp) {
375 dp = fc_frame_payload_get(fp, len);
376 memcpy(dp, pp, len);
377 *((u32 *)dp) = htonl(ELS_LS_ACC << 24);
378 sp = lport->tt.seq_start_next(sp);
379 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
380 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
381 FC_TYPE_ELS, f_ctl, 0);
382 lport->tt.seq_send(lport, sp, fp);
383 }
384 fc_frame_free(in_fp);
385}
386
387
388
389
390
391
392
393
394
395
396static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
397 struct fc_lport *lport)
398{
399 struct fc_frame *fp;
400 struct fc_exch *ep = fc_seq_exch(sp);
401 struct fc_els_rnid *req;
402 struct {
403 struct fc_els_rnid_resp rnid;
404 struct fc_els_rnid_cid cid;
405 struct fc_els_rnid_gen gen;
406 } *rp;
407 struct fc_seq_els_data rjt_data;
408 u8 fmt;
409 size_t len;
410 u32 f_ctl;
411
412 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
413 fc_lport_state(lport));
414
415 req = fc_frame_payload_get(in_fp, sizeof(*req));
416 if (!req) {
417 rjt_data.fp = NULL;
418 rjt_data.reason = ELS_RJT_LOGIC;
419 rjt_data.explan = ELS_EXPL_NONE;
420 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
421 } else {
422 fmt = req->rnid_fmt;
423 len = sizeof(*rp);
424 if (fmt != ELS_RNIDF_GEN ||
425 ntohl(lport->rnid_gen.rnid_atype) == 0) {
426 fmt = ELS_RNIDF_NONE;
427 len -= sizeof(rp->gen);
428 }
429 fp = fc_frame_alloc(lport, len);
430 if (fp) {
431 rp = fc_frame_payload_get(fp, len);
432 memset(rp, 0, len);
433 rp->rnid.rnid_cmd = ELS_LS_ACC;
434 rp->rnid.rnid_fmt = fmt;
435 rp->rnid.rnid_cid_len = sizeof(rp->cid);
436 rp->cid.rnid_wwpn = htonll(lport->wwpn);
437 rp->cid.rnid_wwnn = htonll(lport->wwnn);
438 if (fmt == ELS_RNIDF_GEN) {
439 rp->rnid.rnid_sid_len = sizeof(rp->gen);
440 memcpy(&rp->gen, &lport->rnid_gen,
441 sizeof(rp->gen));
442 }
443 sp = lport->tt.seq_start_next(sp);
444 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
445 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
446 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
447 FC_TYPE_ELS, f_ctl, 0);
448 lport->tt.seq_send(lport, sp, fp);
449 }
450 }
451 fc_frame_free(in_fp);
452}
453
454
455
456
457
458
459
460
461
462
463static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
464 struct fc_lport *lport)
465{
466 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
467 fc_lport_enter_reset(lport);
468 fc_frame_free(fp);
469}
470
471
472
473
474
475
476
477
478int fc_fabric_login(struct fc_lport *lport)
479{
480 int rc = -1;
481
482 mutex_lock(&lport->lp_mutex);
483 if (lport->state == LPORT_ST_DISABLED) {
484 fc_lport_enter_reset(lport);
485 rc = 0;
486 }
487 mutex_unlock(&lport->lp_mutex);
488
489 return rc;
490}
491EXPORT_SYMBOL(fc_fabric_login);
492
493
494
495
496
497void fc_linkup(struct fc_lport *lport)
498{
499 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
500 fc_host_port_id(lport->host));
501
502 mutex_lock(&lport->lp_mutex);
503 if (!lport->link_up) {
504 lport->link_up = 1;
505
506 if (lport->state == LPORT_ST_RESET)
507 fc_lport_enter_flogi(lport);
508 }
509 mutex_unlock(&lport->lp_mutex);
510}
511EXPORT_SYMBOL(fc_linkup);
512
513
514
515
516
517void fc_linkdown(struct fc_lport *lport)
518{
519 mutex_lock(&lport->lp_mutex);
520 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
521 fc_host_port_id(lport->host));
522
523 if (lport->link_up) {
524 lport->link_up = 0;
525 fc_lport_enter_reset(lport);
526 lport->tt.fcp_cleanup(lport);
527 }
528 mutex_unlock(&lport->lp_mutex);
529}
530EXPORT_SYMBOL(fc_linkdown);
531
532
533
534
535
536
537
538
539int fc_fabric_logoff(struct fc_lport *lport)
540{
541 lport->tt.disc_stop_final(lport);
542 mutex_lock(&lport->lp_mutex);
543 if (lport->dns_rp)
544 lport->tt.rport_logoff(lport->dns_rp);
545 mutex_unlock(&lport->lp_mutex);
546 lport->tt.rport_flush_queue();
547 mutex_lock(&lport->lp_mutex);
548 fc_lport_enter_logo(lport);
549 mutex_unlock(&lport->lp_mutex);
550 cancel_delayed_work_sync(&lport->retry_work);
551 return 0;
552}
553EXPORT_SYMBOL(fc_fabric_logoff);
554
555
556
557
558
559
560
561
562
563
564
565
566
567int fc_lport_destroy(struct fc_lport *lport)
568{
569 mutex_lock(&lport->lp_mutex);
570 lport->state = LPORT_ST_DISABLED;
571 lport->link_up = 0;
572 lport->tt.frame_send = fc_frame_drop;
573 mutex_unlock(&lport->lp_mutex);
574
575 lport->tt.fcp_abort_io(lport);
576 lport->tt.disc_stop_final(lport);
577 lport->tt.exch_mgr_reset(lport, 0, 0);
578 return 0;
579}
580EXPORT_SYMBOL(fc_lport_destroy);
581
582
583
584
585
586
587
588
589
590
591int fc_set_mfs(struct fc_lport *lport, u32 mfs)
592{
593 unsigned int old_mfs;
594 int rc = -EINVAL;
595
596 mutex_lock(&lport->lp_mutex);
597
598 old_mfs = lport->mfs;
599
600 if (mfs >= FC_MIN_MAX_FRAME) {
601 mfs &= ~3;
602 if (mfs > FC_MAX_FRAME)
603 mfs = FC_MAX_FRAME;
604 mfs -= sizeof(struct fc_frame_header);
605 lport->mfs = mfs;
606 rc = 0;
607 }
608
609 if (!rc && mfs < old_mfs)
610 fc_lport_enter_reset(lport);
611
612 mutex_unlock(&lport->lp_mutex);
613
614 return rc;
615}
616EXPORT_SYMBOL(fc_set_mfs);
617
618
619
620
621
622
623void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
624{
625 switch (event) {
626 case DISC_EV_SUCCESS:
627 FC_LPORT_DBG(lport, "Discovery succeeded\n");
628 break;
629 case DISC_EV_FAILED:
630 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
631 fc_host_port_id(lport->host));
632 mutex_lock(&lport->lp_mutex);
633 fc_lport_enter_reset(lport);
634 mutex_unlock(&lport->lp_mutex);
635 break;
636 case DISC_EV_NONE:
637 WARN_ON(1);
638 break;
639 }
640}
641
642
643
644
645
646
647
648
649static void fc_lport_enter_ready(struct fc_lport *lport)
650{
651 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
652 fc_lport_state(lport));
653
654 fc_lport_state_enter(lport, LPORT_ST_READY);
655
656 if (!lport->ptp_rp)
657 lport->tt.disc_start(fc_lport_disc_callback, lport);
658}
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
674 struct fc_frame *rx_fp,
675 struct fc_lport *lport)
676{
677 struct fc_frame *fp;
678 struct fc_frame_header *fh;
679 struct fc_seq *sp;
680 struct fc_exch *ep;
681 struct fc_els_flogi *flp;
682 struct fc_els_flogi *new_flp;
683 u64 remote_wwpn;
684 u32 remote_fid;
685 u32 local_fid;
686 u32 f_ctl;
687
688 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
689 fc_lport_state(lport));
690
691 fh = fc_frame_header_get(rx_fp);
692 remote_fid = ntoh24(fh->fh_s_id);
693 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
694 if (!flp)
695 goto out;
696 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
697 if (remote_wwpn == lport->wwpn) {
698 printk(KERN_WARNING "libfc: Received FLOGI from port "
699 "with same WWPN %llx\n", remote_wwpn);
700 goto out;
701 }
702 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
703
704
705
706
707
708
709 local_fid = FC_LOCAL_PTP_FID_LO;
710 if (remote_wwpn < lport->wwpn) {
711 local_fid = FC_LOCAL_PTP_FID_HI;
712 if (!remote_fid || remote_fid == local_fid)
713 remote_fid = FC_LOCAL_PTP_FID_LO;
714 } else if (!remote_fid) {
715 remote_fid = FC_LOCAL_PTP_FID_HI;
716 }
717
718 fc_host_port_id(lport->host) = local_fid;
719
720 fp = fc_frame_alloc(lport, sizeof(*flp));
721 if (fp) {
722 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
723 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
724 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
725 new_flp->fl_cmd = (u8) ELS_LS_ACC;
726
727
728
729
730
731 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
732 ep = fc_seq_exch(sp);
733 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
734 FC_TYPE_ELS, f_ctl, 0);
735 lport->tt.seq_send(lport, sp, fp);
736
737 } else {
738 fc_lport_error(lport, fp);
739 }
740 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
741 get_unaligned_be64(&flp->fl_wwnn));
742
743out:
744 sp = fr_seq(rx_fp);
745 fc_frame_free(rx_fp);
746}
747
748
749
750
751
752
753
754
755
756
757
758
759
760static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
761 struct fc_frame *fp)
762{
763 struct fc_frame_header *fh = fc_frame_header_get(fp);
764 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
765
766 mutex_lock(&lport->lp_mutex);
767
768
769
770
771
772
773 if (!lport->link_up)
774 fc_frame_free(fp);
775 else if (fh->fh_type == FC_TYPE_ELS &&
776 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
777
778
779
780 recv = lport->tt.rport_recv_req;
781 switch (fc_frame_payload_op(fp)) {
782 case ELS_FLOGI:
783 recv = fc_lport_recv_flogi_req;
784 break;
785 case ELS_LOGO:
786 fh = fc_frame_header_get(fp);
787 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
788 recv = fc_lport_recv_logo_req;
789 break;
790 case ELS_RSCN:
791 recv = lport->tt.disc_recv_req;
792 break;
793 case ELS_ECHO:
794 recv = fc_lport_recv_echo_req;
795 break;
796 case ELS_RLIR:
797 recv = fc_lport_recv_rlir_req;
798 break;
799 case ELS_RNID:
800 recv = fc_lport_recv_rnid_req;
801 break;
802 }
803
804 recv(sp, fp, lport);
805 } else {
806 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
807 fr_eof(fp));
808 fc_frame_free(fp);
809 }
810 mutex_unlock(&lport->lp_mutex);
811
812
813
814
815
816 lport->tt.exch_done(sp);
817}
818
819
820
821
822
823
824
825
826int fc_lport_reset(struct fc_lport *lport)
827{
828 cancel_delayed_work_sync(&lport->retry_work);
829 mutex_lock(&lport->lp_mutex);
830 fc_lport_enter_reset(lport);
831 mutex_unlock(&lport->lp_mutex);
832 return 0;
833}
834EXPORT_SYMBOL(fc_lport_reset);
835
836
837
838
839
840
841
842
843static void fc_lport_reset_locked(struct fc_lport *lport)
844{
845 if (lport->dns_rp)
846 lport->tt.rport_logoff(lport->dns_rp);
847
848 lport->ptp_rp = NULL;
849
850 lport->tt.disc_stop(lport);
851
852 lport->tt.exch_mgr_reset(lport, 0, 0);
853 fc_host_fabric_name(lport->host) = 0;
854 fc_host_port_id(lport->host) = 0;
855}
856
857
858
859
860
861
862
863
864static void fc_lport_enter_reset(struct fc_lport *lport)
865{
866 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
867 fc_lport_state(lport));
868
869 fc_lport_state_enter(lport, LPORT_ST_RESET);
870 fc_lport_reset_locked(lport);
871 if (lport->link_up)
872 fc_lport_enter_flogi(lport);
873}
874
875
876
877
878
879
880
881
882static void fc_lport_enter_disabled(struct fc_lport *lport)
883{
884 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
885 fc_lport_state(lport));
886
887 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
888 fc_lport_reset_locked(lport);
889}
890
891
892
893
894
895
896
897
898
899
900static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
901{
902 unsigned long delay = 0;
903 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
904 PTR_ERR(fp), fc_lport_state(lport),
905 lport->retry_count);
906
907 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
908
909
910
911
912 if (lport->retry_count < lport->max_retry_count) {
913 lport->retry_count++;
914 if (!fp)
915 delay = msecs_to_jiffies(500);
916 else
917 delay = msecs_to_jiffies(lport->e_d_tov);
918
919 schedule_delayed_work(&lport->retry_work, delay);
920 } else {
921 switch (lport->state) {
922 case LPORT_ST_DISABLED:
923 case LPORT_ST_READY:
924 case LPORT_ST_RESET:
925 case LPORT_ST_RPN_ID:
926 case LPORT_ST_RFT_ID:
927 case LPORT_ST_SCR:
928 case LPORT_ST_DNS:
929 case LPORT_ST_FLOGI:
930 case LPORT_ST_LOGO:
931 fc_lport_enter_reset(lport);
932 break;
933 }
934 }
935 }
936}
937
938
939
940
941
942
943
944
945
946
947
948
949static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
950 void *lp_arg)
951{
952 struct fc_lport *lport = lp_arg;
953 struct fc_frame_header *fh;
954 struct fc_ct_hdr *ct;
955
956 FC_LPORT_DBG(lport, "Received a RFT_ID %s\n", fc_els_resp_type(fp));
957
958 if (fp == ERR_PTR(-FC_EX_CLOSED))
959 return;
960
961 mutex_lock(&lport->lp_mutex);
962
963 if (lport->state != LPORT_ST_RFT_ID) {
964 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state "
965 "%s\n", fc_lport_state(lport));
966 if (IS_ERR(fp))
967 goto err;
968 goto out;
969 }
970
971 if (IS_ERR(fp)) {
972 fc_lport_error(lport, fp);
973 goto err;
974 }
975
976 fh = fc_frame_header_get(fp);
977 ct = fc_frame_payload_get(fp, sizeof(*ct));
978
979 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
980 ct->ct_fs_type == FC_FST_DIR &&
981 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
982 ntohs(ct->ct_cmd) == FC_FS_ACC)
983 fc_lport_enter_scr(lport);
984 else
985 fc_lport_error(lport, fp);
986out:
987 fc_frame_free(fp);
988err:
989 mutex_unlock(&lport->lp_mutex);
990}
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003static void fc_lport_rpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1004 void *lp_arg)
1005{
1006 struct fc_lport *lport = lp_arg;
1007 struct fc_frame_header *fh;
1008 struct fc_ct_hdr *ct;
1009
1010 FC_LPORT_DBG(lport, "Received a RPN_ID %s\n", fc_els_resp_type(fp));
1011
1012 if (fp == ERR_PTR(-FC_EX_CLOSED))
1013 return;
1014
1015 mutex_lock(&lport->lp_mutex);
1016
1017 if (lport->state != LPORT_ST_RPN_ID) {
1018 FC_LPORT_DBG(lport, "Received a RPN_ID response, but in state "
1019 "%s\n", fc_lport_state(lport));
1020 if (IS_ERR(fp))
1021 goto err;
1022 goto out;
1023 }
1024
1025 if (IS_ERR(fp)) {
1026 fc_lport_error(lport, fp);
1027 goto err;
1028 }
1029
1030 fh = fc_frame_header_get(fp);
1031 ct = fc_frame_payload_get(fp, sizeof(*ct));
1032 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1033 ct->ct_fs_type == FC_FST_DIR &&
1034 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1035 ntohs(ct->ct_cmd) == FC_FS_ACC)
1036 fc_lport_enter_rft_id(lport);
1037 else
1038 fc_lport_error(lport, fp);
1039
1040out:
1041 fc_frame_free(fp);
1042err:
1043 mutex_unlock(&lport->lp_mutex);
1044}
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1057 void *lp_arg)
1058{
1059 struct fc_lport *lport = lp_arg;
1060 u8 op;
1061
1062 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1063
1064 if (fp == ERR_PTR(-FC_EX_CLOSED))
1065 return;
1066
1067 mutex_lock(&lport->lp_mutex);
1068
1069 if (lport->state != LPORT_ST_SCR) {
1070 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1071 "%s\n", fc_lport_state(lport));
1072 if (IS_ERR(fp))
1073 goto err;
1074 goto out;
1075 }
1076
1077 if (IS_ERR(fp)) {
1078 fc_lport_error(lport, fp);
1079 goto err;
1080 }
1081
1082 op = fc_frame_payload_op(fp);
1083 if (op == ELS_LS_ACC)
1084 fc_lport_enter_ready(lport);
1085 else
1086 fc_lport_error(lport, fp);
1087
1088out:
1089 fc_frame_free(fp);
1090err:
1091 mutex_unlock(&lport->lp_mutex);
1092}
1093
1094
1095
1096
1097
1098
1099
1100
1101static void fc_lport_enter_scr(struct fc_lport *lport)
1102{
1103 struct fc_frame *fp;
1104
1105 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1106 fc_lport_state(lport));
1107
1108 fc_lport_state_enter(lport, LPORT_ST_SCR);
1109
1110 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1111 if (!fp) {
1112 fc_lport_error(lport, fp);
1113 return;
1114 }
1115
1116 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
1117 fc_lport_scr_resp, lport, lport->e_d_tov))
1118 fc_lport_error(lport, fp);
1119}
1120
1121
1122
1123
1124
1125
1126
1127
1128static void fc_lport_enter_rft_id(struct fc_lport *lport)
1129{
1130 struct fc_frame *fp;
1131 struct fc_ns_fts *lps;
1132 int i;
1133
1134 FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n",
1135 fc_lport_state(lport));
1136
1137 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1138
1139 lps = &lport->fcts;
1140 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1141 while (--i >= 0)
1142 if (ntohl(lps->ff_type_map[i]) != 0)
1143 break;
1144 if (i < 0) {
1145
1146 fc_lport_enter_scr(lport);
1147 return;
1148 }
1149
1150 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1151 sizeof(struct fc_ns_rft));
1152 if (!fp) {
1153 fc_lport_error(lport, fp);
1154 return;
1155 }
1156
1157 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RFT_ID,
1158 fc_lport_rft_id_resp,
1159 lport, lport->e_d_tov))
1160 fc_lport_error(lport, fp);
1161}
1162
1163
1164
1165
1166
1167
1168
1169
1170static void fc_lport_enter_rpn_id(struct fc_lport *lport)
1171{
1172 struct fc_frame *fp;
1173
1174 FC_LPORT_DBG(lport, "Entered RPN_ID state from %s state\n",
1175 fc_lport_state(lport));
1176
1177 fc_lport_state_enter(lport, LPORT_ST_RPN_ID);
1178
1179 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1180 sizeof(struct fc_ns_rn_id));
1181 if (!fp) {
1182 fc_lport_error(lport, fp);
1183 return;
1184 }
1185
1186 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RPN_ID,
1187 fc_lport_rpn_id_resp,
1188 lport, lport->e_d_tov))
1189 fc_lport_error(lport, fp);
1190}
1191
1192static struct fc_rport_operations fc_lport_rport_ops = {
1193 .event_callback = fc_lport_rport_callback,
1194};
1195
1196
1197
1198
1199
1200
1201
1202
1203static void fc_lport_enter_dns(struct fc_lport *lport)
1204{
1205 struct fc_rport_priv *rdata;
1206
1207 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1208 fc_lport_state(lport));
1209
1210 fc_lport_state_enter(lport, LPORT_ST_DNS);
1211
1212 mutex_lock(&lport->disc.disc_mutex);
1213 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
1214 mutex_unlock(&lport->disc.disc_mutex);
1215 if (!rdata)
1216 goto err;
1217
1218 rdata->ops = &fc_lport_rport_ops;
1219 lport->tt.rport_login(rdata);
1220 return;
1221
1222err:
1223 fc_lport_error(lport, NULL);
1224}
1225
1226
1227
1228
1229
1230static void fc_lport_timeout(struct work_struct *work)
1231{
1232 struct fc_lport *lport =
1233 container_of(work, struct fc_lport,
1234 retry_work.work);
1235
1236 mutex_lock(&lport->lp_mutex);
1237
1238 switch (lport->state) {
1239 case LPORT_ST_DISABLED:
1240 case LPORT_ST_READY:
1241 case LPORT_ST_RESET:
1242 WARN_ON(1);
1243 break;
1244 case LPORT_ST_FLOGI:
1245 fc_lport_enter_flogi(lport);
1246 break;
1247 case LPORT_ST_DNS:
1248 fc_lport_enter_dns(lport);
1249 break;
1250 case LPORT_ST_RPN_ID:
1251 fc_lport_enter_rpn_id(lport);
1252 break;
1253 case LPORT_ST_RFT_ID:
1254 fc_lport_enter_rft_id(lport);
1255 break;
1256 case LPORT_ST_SCR:
1257 fc_lport_enter_scr(lport);
1258 break;
1259 case LPORT_ST_LOGO:
1260 fc_lport_enter_logo(lport);
1261 break;
1262 }
1263
1264 mutex_unlock(&lport->lp_mutex);
1265}
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277static void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1278 void *lp_arg)
1279{
1280 struct fc_lport *lport = lp_arg;
1281 u8 op;
1282
1283 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1284
1285 if (fp == ERR_PTR(-FC_EX_CLOSED))
1286 return;
1287
1288 mutex_lock(&lport->lp_mutex);
1289
1290 if (lport->state != LPORT_ST_LOGO) {
1291 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1292 "%s\n", fc_lport_state(lport));
1293 if (IS_ERR(fp))
1294 goto err;
1295 goto out;
1296 }
1297
1298 if (IS_ERR(fp)) {
1299 fc_lport_error(lport, fp);
1300 goto err;
1301 }
1302
1303 op = fc_frame_payload_op(fp);
1304 if (op == ELS_LS_ACC)
1305 fc_lport_enter_disabled(lport);
1306 else
1307 fc_lport_error(lport, fp);
1308
1309out:
1310 fc_frame_free(fp);
1311err:
1312 mutex_unlock(&lport->lp_mutex);
1313}
1314
1315
1316
1317
1318
1319
1320
1321
1322static void fc_lport_enter_logo(struct fc_lport *lport)
1323{
1324 struct fc_frame *fp;
1325 struct fc_els_logo *logo;
1326
1327 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1328 fc_lport_state(lport));
1329
1330 fc_lport_state_enter(lport, LPORT_ST_LOGO);
1331
1332 fp = fc_frame_alloc(lport, sizeof(*logo));
1333 if (!fp) {
1334 fc_lport_error(lport, fp);
1335 return;
1336 }
1337
1338 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1339 fc_lport_logo_resp, lport, lport->e_d_tov))
1340 fc_lport_error(lport, fp);
1341}
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1354 void *lp_arg)
1355{
1356 struct fc_lport *lport = lp_arg;
1357 struct fc_frame_header *fh;
1358 struct fc_els_flogi *flp;
1359 u32 did;
1360 u16 csp_flags;
1361 unsigned int r_a_tov;
1362 unsigned int e_d_tov;
1363 u16 mfs;
1364
1365 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1366
1367 if (fp == ERR_PTR(-FC_EX_CLOSED))
1368 return;
1369
1370 mutex_lock(&lport->lp_mutex);
1371
1372 if (lport->state != LPORT_ST_FLOGI) {
1373 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1374 "%s\n", fc_lport_state(lport));
1375 if (IS_ERR(fp))
1376 goto err;
1377 goto out;
1378 }
1379
1380 if (IS_ERR(fp)) {
1381 fc_lport_error(lport, fp);
1382 goto err;
1383 }
1384
1385 fh = fc_frame_header_get(fp);
1386 did = ntoh24(fh->fh_d_id);
1387 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1388
1389 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1390 did);
1391 fc_host_port_id(lport->host) = did;
1392
1393 flp = fc_frame_payload_get(fp, sizeof(*flp));
1394 if (flp) {
1395 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1396 FC_SP_BB_DATA_MASK;
1397 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1398 mfs < lport->mfs)
1399 lport->mfs = mfs;
1400 csp_flags = ntohs(flp->fl_csp.sp_features);
1401 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1402 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1403 if (csp_flags & FC_SP_FT_EDTR)
1404 e_d_tov /= 1000000;
1405 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1406 if (e_d_tov > lport->e_d_tov)
1407 lport->e_d_tov = e_d_tov;
1408 lport->r_a_tov = 2 * e_d_tov;
1409 printk(KERN_INFO "libfc: Port (%6x) entered "
1410 "point to point mode\n", did);
1411 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1412 get_unaligned_be64(
1413 &flp->fl_wwpn),
1414 get_unaligned_be64(
1415 &flp->fl_wwnn));
1416 } else {
1417 lport->e_d_tov = e_d_tov;
1418 lport->r_a_tov = r_a_tov;
1419 fc_host_fabric_name(lport->host) =
1420 get_unaligned_be64(&flp->fl_wwnn);
1421 fc_lport_enter_dns(lport);
1422 }
1423 }
1424 } else {
1425 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
1426 }
1427
1428out:
1429 fc_frame_free(fp);
1430err:
1431 mutex_unlock(&lport->lp_mutex);
1432}
1433
1434
1435
1436
1437
1438
1439
1440
1441void fc_lport_enter_flogi(struct fc_lport *lport)
1442{
1443 struct fc_frame *fp;
1444
1445 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1446 fc_lport_state(lport));
1447
1448 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1449
1450 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1451 if (!fp)
1452 return fc_lport_error(lport, fp);
1453
1454 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_FLOGI,
1455 fc_lport_flogi_resp, lport, lport->e_d_tov))
1456 fc_lport_error(lport, fp);
1457}
1458
1459
1460int fc_lport_config(struct fc_lport *lport)
1461{
1462 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1463 mutex_init(&lport->lp_mutex);
1464
1465 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
1466
1467 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1468 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1469
1470 return 0;
1471}
1472EXPORT_SYMBOL(fc_lport_config);
1473
1474int fc_lport_init(struct fc_lport *lport)
1475{
1476 if (!lport->tt.lport_recv)
1477 lport->tt.lport_recv = fc_lport_recv_req;
1478
1479 if (!lport->tt.lport_reset)
1480 lport->tt.lport_reset = fc_lport_reset;
1481
1482 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1483 fc_host_node_name(lport->host) = lport->wwnn;
1484 fc_host_port_name(lport->host) = lport->wwpn;
1485 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1486 memset(fc_host_supported_fc4s(lport->host), 0,
1487 sizeof(fc_host_supported_fc4s(lport->host)));
1488 fc_host_supported_fc4s(lport->host)[2] = 1;
1489 fc_host_supported_fc4s(lport->host)[7] = 1;
1490
1491
1492 memset(fc_host_active_fc4s(lport->host), 0,
1493 sizeof(fc_host_active_fc4s(lport->host)));
1494 fc_host_active_fc4s(lport->host)[2] = 1;
1495 fc_host_active_fc4s(lport->host)[7] = 1;
1496 fc_host_maxframe_size(lport->host) = lport->mfs;
1497 fc_host_supported_speeds(lport->host) = 0;
1498 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1499 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1500 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1501 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1502
1503 INIT_LIST_HEAD(&lport->ema_list);
1504 return 0;
1505}
1506EXPORT_SYMBOL(fc_lport_init);
1507