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#include <linux/kernel.h>
48#include <linux/spinlock.h>
49#include <linux/interrupt.h>
50#include <linux/slab.h>
51#include <linux/rcupdate.h>
52#include <linux/timer.h>
53#include <linux/workqueue.h>
54#include <linux/export.h>
55#include <asm/unaligned.h>
56
57#include <scsi/libfc.h>
58#include <scsi/fc_encode.h>
59
60#include "fc_libfc.h"
61
62static struct workqueue_struct *rport_event_queue;
63
64static void fc_rport_enter_flogi(struct fc_rport_priv *);
65static void fc_rport_enter_plogi(struct fc_rport_priv *);
66static void fc_rport_enter_prli(struct fc_rport_priv *);
67static void fc_rport_enter_rtv(struct fc_rport_priv *);
68static void fc_rport_enter_ready(struct fc_rport_priv *);
69static void fc_rport_enter_logo(struct fc_rport_priv *);
70static void fc_rport_enter_adisc(struct fc_rport_priv *);
71
72static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
73static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
74static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
75static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
76static void fc_rport_timeout(struct work_struct *);
77static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
78static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
79static void fc_rport_work(struct work_struct *);
80
81static const char *fc_rport_state_names[] = {
82 [RPORT_ST_INIT] = "Init",
83 [RPORT_ST_FLOGI] = "FLOGI",
84 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
85 [RPORT_ST_PLOGI] = "PLOGI",
86 [RPORT_ST_PRLI] = "PRLI",
87 [RPORT_ST_RTV] = "RTV",
88 [RPORT_ST_READY] = "Ready",
89 [RPORT_ST_ADISC] = "ADISC",
90 [RPORT_ST_DELETE] = "Delete",
91};
92
93
94
95
96
97
98
99
100static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
101 u32 port_id)
102{
103 struct fc_rport_priv *rdata;
104
105 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
106 if (rdata->ids.port_id == port_id)
107 return rdata;
108 return NULL;
109}
110
111
112
113
114
115
116
117
118
119
120static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
121 u32 port_id)
122{
123 struct fc_rport_priv *rdata;
124
125 rdata = lport->tt.rport_lookup(lport, port_id);
126 if (rdata)
127 return rdata;
128
129 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
130 if (!rdata)
131 return NULL;
132
133 rdata->ids.node_name = -1;
134 rdata->ids.port_name = -1;
135 rdata->ids.port_id = port_id;
136 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
137
138 kref_init(&rdata->kref);
139 mutex_init(&rdata->rp_mutex);
140 rdata->local_port = lport;
141 rdata->rp_state = RPORT_ST_INIT;
142 rdata->event = RPORT_EV_NONE;
143 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
144 rdata->e_d_tov = lport->e_d_tov;
145 rdata->r_a_tov = lport->r_a_tov;
146 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
147 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
148 INIT_WORK(&rdata->event_work, fc_rport_work);
149 if (port_id != FC_FID_DIR_SERV) {
150 rdata->lld_event_callback = lport->tt.rport_event_callback;
151 list_add_rcu(&rdata->peers, &lport->disc.rports);
152 }
153 return rdata;
154}
155
156
157
158
159
160static void fc_rport_destroy(struct kref *kref)
161{
162 struct fc_rport_priv *rdata;
163
164 rdata = container_of(kref, struct fc_rport_priv, kref);
165 kfree_rcu(rdata, rcu);
166}
167
168
169
170
171
172static const char *fc_rport_state(struct fc_rport_priv *rdata)
173{
174 const char *cp;
175
176 cp = fc_rport_state_names[rdata->rp_state];
177 if (!cp)
178 cp = "Unknown";
179 return cp;
180}
181
182
183
184
185
186
187void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
188{
189 if (timeout)
190 rport->dev_loss_tmo = timeout;
191 else
192 rport->dev_loss_tmo = 1;
193}
194EXPORT_SYMBOL(fc_set_rport_loss_tmo);
195
196
197
198
199
200
201
202
203static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
204 unsigned int maxval)
205{
206 unsigned int mfs;
207
208
209
210
211
212 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
213 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
214 maxval = mfs;
215 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
216 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
217 maxval = mfs;
218 return maxval;
219}
220
221
222
223
224
225
226
227
228static void fc_rport_state_enter(struct fc_rport_priv *rdata,
229 enum fc_rport_state new)
230{
231 if (rdata->rp_state != new)
232 rdata->retries = 0;
233 rdata->rp_state = new;
234}
235
236
237
238
239
240static void fc_rport_work(struct work_struct *work)
241{
242 u32 port_id;
243 struct fc_rport_priv *rdata =
244 container_of(work, struct fc_rport_priv, event_work);
245 struct fc_rport_libfc_priv *rpriv;
246 enum fc_rport_event event;
247 struct fc_lport *lport = rdata->local_port;
248 struct fc_rport_operations *rport_ops;
249 struct fc_rport_identifiers ids;
250 struct fc_rport *rport;
251 struct fc4_prov *prov;
252 u8 type;
253
254 mutex_lock(&rdata->rp_mutex);
255 event = rdata->event;
256 rport_ops = rdata->ops;
257 rport = rdata->rport;
258
259 FC_RPORT_DBG(rdata, "work event %u\n", event);
260
261 switch (event) {
262 case RPORT_EV_READY:
263 ids = rdata->ids;
264 rdata->event = RPORT_EV_NONE;
265 rdata->major_retries = 0;
266 kref_get(&rdata->kref);
267 mutex_unlock(&rdata->rp_mutex);
268
269 if (!rport)
270 rport = fc_remote_port_add(lport->host, 0, &ids);
271 if (!rport) {
272 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
273 lport->tt.rport_logoff(rdata);
274 kref_put(&rdata->kref, lport->tt.rport_destroy);
275 return;
276 }
277 mutex_lock(&rdata->rp_mutex);
278 if (rdata->rport)
279 FC_RPORT_DBG(rdata, "rport already allocated\n");
280 rdata->rport = rport;
281 rport->maxframe_size = rdata->maxframe_size;
282 rport->supported_classes = rdata->supported_classes;
283
284 rpriv = rport->dd_data;
285 rpriv->local_port = lport;
286 rpriv->rp_state = rdata->rp_state;
287 rpriv->flags = rdata->flags;
288 rpriv->e_d_tov = rdata->e_d_tov;
289 rpriv->r_a_tov = rdata->r_a_tov;
290 mutex_unlock(&rdata->rp_mutex);
291
292 if (rport_ops && rport_ops->event_callback) {
293 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
294 rport_ops->event_callback(lport, rdata, event);
295 }
296 if (rdata->lld_event_callback) {
297 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
298 rdata->lld_event_callback(lport, rdata, event);
299 }
300 kref_put(&rdata->kref, lport->tt.rport_destroy);
301 break;
302
303 case RPORT_EV_FAILED:
304 case RPORT_EV_LOGO:
305 case RPORT_EV_STOP:
306 if (rdata->prli_count) {
307 mutex_lock(&fc_prov_mutex);
308 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
309 prov = fc_passive_prov[type];
310 if (prov && prov->prlo)
311 prov->prlo(rdata);
312 }
313 mutex_unlock(&fc_prov_mutex);
314 }
315 port_id = rdata->ids.port_id;
316 mutex_unlock(&rdata->rp_mutex);
317
318 if (rport_ops && rport_ops->event_callback) {
319 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
320 rport_ops->event_callback(lport, rdata, event);
321 }
322 if (rdata->lld_event_callback) {
323 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
324 rdata->lld_event_callback(lport, rdata, event);
325 }
326 cancel_delayed_work_sync(&rdata->retry_work);
327
328
329
330
331 lport->tt.exch_mgr_reset(lport, 0, port_id);
332 lport->tt.exch_mgr_reset(lport, port_id, 0);
333
334 if (rport) {
335 rpriv = rport->dd_data;
336 rpriv->rp_state = RPORT_ST_DELETE;
337 mutex_lock(&rdata->rp_mutex);
338 rdata->rport = NULL;
339 mutex_unlock(&rdata->rp_mutex);
340 fc_remote_port_delete(rport);
341 }
342
343 mutex_lock(&lport->disc.disc_mutex);
344 mutex_lock(&rdata->rp_mutex);
345 if (rdata->rp_state == RPORT_ST_DELETE) {
346 if (port_id == FC_FID_DIR_SERV) {
347 rdata->event = RPORT_EV_NONE;
348 mutex_unlock(&rdata->rp_mutex);
349 kref_put(&rdata->kref, lport->tt.rport_destroy);
350 } else if ((rdata->flags & FC_RP_STARTED) &&
351 rdata->major_retries <
352 lport->max_rport_retry_count) {
353 rdata->major_retries++;
354 rdata->event = RPORT_EV_NONE;
355 FC_RPORT_DBG(rdata, "work restart\n");
356 fc_rport_enter_flogi(rdata);
357 mutex_unlock(&rdata->rp_mutex);
358 } else {
359 FC_RPORT_DBG(rdata, "work delete\n");
360 list_del_rcu(&rdata->peers);
361 mutex_unlock(&rdata->rp_mutex);
362 kref_put(&rdata->kref, lport->tt.rport_destroy);
363 }
364 } else {
365
366
367
368 rdata->event = RPORT_EV_NONE;
369 if (rdata->rp_state == RPORT_ST_READY)
370 fc_rport_enter_ready(rdata);
371 mutex_unlock(&rdata->rp_mutex);
372 }
373 mutex_unlock(&lport->disc.disc_mutex);
374 break;
375
376 default:
377 mutex_unlock(&rdata->rp_mutex);
378 break;
379 }
380}
381
382
383
384
385
386
387
388
389
390
391
392
393
394static int fc_rport_login(struct fc_rport_priv *rdata)
395{
396 mutex_lock(&rdata->rp_mutex);
397
398 rdata->flags |= FC_RP_STARTED;
399 switch (rdata->rp_state) {
400 case RPORT_ST_READY:
401 FC_RPORT_DBG(rdata, "ADISC port\n");
402 fc_rport_enter_adisc(rdata);
403 break;
404 case RPORT_ST_DELETE:
405 FC_RPORT_DBG(rdata, "Restart deleted port\n");
406 break;
407 default:
408 FC_RPORT_DBG(rdata, "Login to port\n");
409 fc_rport_enter_flogi(rdata);
410 break;
411 }
412 mutex_unlock(&rdata->rp_mutex);
413
414 return 0;
415}
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
432 enum fc_rport_event event)
433{
434 if (rdata->rp_state == RPORT_ST_DELETE)
435 return;
436
437 FC_RPORT_DBG(rdata, "Delete port\n");
438
439 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
440
441 if (rdata->event == RPORT_EV_NONE)
442 queue_work(rport_event_queue, &rdata->event_work);
443 rdata->event = event;
444}
445
446
447
448
449
450
451
452
453
454static int fc_rport_logoff(struct fc_rport_priv *rdata)
455{
456 mutex_lock(&rdata->rp_mutex);
457
458 FC_RPORT_DBG(rdata, "Remove port\n");
459
460 rdata->flags &= ~FC_RP_STARTED;
461 if (rdata->rp_state == RPORT_ST_DELETE) {
462 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
463 goto out;
464 }
465 fc_rport_enter_logo(rdata);
466
467
468
469
470
471 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
472out:
473 mutex_unlock(&rdata->rp_mutex);
474 return 0;
475}
476
477
478
479
480
481
482
483
484static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
485{
486 fc_rport_state_enter(rdata, RPORT_ST_READY);
487
488 FC_RPORT_DBG(rdata, "Port is Ready\n");
489
490 if (rdata->event == RPORT_EV_NONE)
491 queue_work(rport_event_queue, &rdata->event_work);
492 rdata->event = RPORT_EV_READY;
493}
494
495
496
497
498
499
500
501
502
503static void fc_rport_timeout(struct work_struct *work)
504{
505 struct fc_rport_priv *rdata =
506 container_of(work, struct fc_rport_priv, retry_work.work);
507
508 mutex_lock(&rdata->rp_mutex);
509
510 switch (rdata->rp_state) {
511 case RPORT_ST_FLOGI:
512 fc_rport_enter_flogi(rdata);
513 break;
514 case RPORT_ST_PLOGI:
515 fc_rport_enter_plogi(rdata);
516 break;
517 case RPORT_ST_PRLI:
518 fc_rport_enter_prli(rdata);
519 break;
520 case RPORT_ST_RTV:
521 fc_rport_enter_rtv(rdata);
522 break;
523 case RPORT_ST_ADISC:
524 fc_rport_enter_adisc(rdata);
525 break;
526 case RPORT_ST_PLOGI_WAIT:
527 case RPORT_ST_READY:
528 case RPORT_ST_INIT:
529 case RPORT_ST_DELETE:
530 break;
531 }
532
533 mutex_unlock(&rdata->rp_mutex);
534}
535
536
537
538
539
540
541
542
543
544static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
545{
546 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
547 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
548 fc_rport_state(rdata), rdata->retries);
549
550 switch (rdata->rp_state) {
551 case RPORT_ST_FLOGI:
552 case RPORT_ST_PLOGI:
553 rdata->flags &= ~FC_RP_STARTED;
554 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
555 break;
556 case RPORT_ST_RTV:
557 fc_rport_enter_ready(rdata);
558 break;
559 case RPORT_ST_PRLI:
560 case RPORT_ST_ADISC:
561 fc_rport_enter_logo(rdata);
562 break;
563 case RPORT_ST_PLOGI_WAIT:
564 case RPORT_ST_DELETE:
565 case RPORT_ST_READY:
566 case RPORT_ST_INIT:
567 break;
568 }
569}
570
571
572
573
574
575
576
577
578
579
580
581
582static void fc_rport_error_retry(struct fc_rport_priv *rdata,
583 struct fc_frame *fp)
584{
585 unsigned long delay = msecs_to_jiffies(FC_DEF_E_D_TOV);
586
587
588 if (PTR_ERR(fp) == -FC_EX_CLOSED)
589 goto out;
590
591 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
592 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
593 PTR_ERR(fp), fc_rport_state(rdata));
594 rdata->retries++;
595
596 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
597 delay = 0;
598 schedule_delayed_work(&rdata->retry_work, delay);
599 return;
600 }
601
602out:
603 fc_rport_error(rdata, fp);
604}
605
606
607
608
609
610
611
612
613
614
615
616static int fc_rport_login_complete(struct fc_rport_priv *rdata,
617 struct fc_frame *fp)
618{
619 struct fc_lport *lport = rdata->local_port;
620 struct fc_els_flogi *flogi;
621 unsigned int e_d_tov;
622 u16 csp_flags;
623
624 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
625 if (!flogi)
626 return -EINVAL;
627
628 csp_flags = ntohs(flogi->fl_csp.sp_features);
629
630 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
631 if (csp_flags & FC_SP_FT_FPORT) {
632 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
633 return -EINVAL;
634 }
635 } else {
636
637
638
639
640 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
641 if (csp_flags & FC_SP_FT_EDTR)
642 e_d_tov /= 1000000;
643 if (e_d_tov > rdata->e_d_tov)
644 rdata->e_d_tov = e_d_tov;
645 }
646 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
647 return 0;
648}
649
650
651
652
653
654
655
656static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
657 void *rp_arg)
658{
659 struct fc_rport_priv *rdata = rp_arg;
660 struct fc_lport *lport = rdata->local_port;
661 struct fc_els_flogi *flogi;
662 unsigned int r_a_tov;
663
664 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
665
666 if (fp == ERR_PTR(-FC_EX_CLOSED))
667 goto put;
668
669 mutex_lock(&rdata->rp_mutex);
670
671 if (rdata->rp_state != RPORT_ST_FLOGI) {
672 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
673 "%s\n", fc_rport_state(rdata));
674 if (IS_ERR(fp))
675 goto err;
676 goto out;
677 }
678
679 if (IS_ERR(fp)) {
680 fc_rport_error(rdata, fp);
681 goto err;
682 }
683
684 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
685 goto bad;
686 if (fc_rport_login_complete(rdata, fp))
687 goto bad;
688
689 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
690 if (!flogi)
691 goto bad;
692 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
693 if (r_a_tov > rdata->r_a_tov)
694 rdata->r_a_tov = r_a_tov;
695
696 if (rdata->ids.port_name < lport->wwpn)
697 fc_rport_enter_plogi(rdata);
698 else
699 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
700out:
701 fc_frame_free(fp);
702err:
703 mutex_unlock(&rdata->rp_mutex);
704put:
705 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
706 return;
707bad:
708 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
709 fc_rport_error_retry(rdata, fp);
710 goto out;
711}
712
713
714
715
716
717
718
719
720static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
721{
722 struct fc_lport *lport = rdata->local_port;
723 struct fc_frame *fp;
724
725 if (!lport->point_to_multipoint)
726 return fc_rport_enter_plogi(rdata);
727
728 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
729 fc_rport_state(rdata));
730
731 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
732
733 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
734 if (!fp)
735 return fc_rport_error_retry(rdata, fp);
736
737 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
738 fc_rport_flogi_resp, rdata,
739 2 * lport->r_a_tov))
740 fc_rport_error_retry(rdata, NULL);
741 else
742 kref_get(&rdata->kref);
743}
744
745
746
747
748
749
750static void fc_rport_recv_flogi_req(struct fc_lport *lport,
751 struct fc_frame *rx_fp)
752{
753 struct fc_disc *disc;
754 struct fc_els_flogi *flp;
755 struct fc_rport_priv *rdata;
756 struct fc_frame *fp = rx_fp;
757 struct fc_seq_els_data rjt_data;
758 u32 sid;
759
760 sid = fc_frame_sid(fp);
761
762 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
763
764 disc = &lport->disc;
765 mutex_lock(&disc->disc_mutex);
766
767 if (!lport->point_to_multipoint) {
768 rjt_data.reason = ELS_RJT_UNSUP;
769 rjt_data.explan = ELS_EXPL_NONE;
770 goto reject;
771 }
772
773 flp = fc_frame_payload_get(fp, sizeof(*flp));
774 if (!flp) {
775 rjt_data.reason = ELS_RJT_LOGIC;
776 rjt_data.explan = ELS_EXPL_INV_LEN;
777 goto reject;
778 }
779
780 rdata = lport->tt.rport_lookup(lport, sid);
781 if (!rdata) {
782 rjt_data.reason = ELS_RJT_FIP;
783 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
784 goto reject;
785 }
786 mutex_lock(&rdata->rp_mutex);
787
788 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
789 fc_rport_state(rdata));
790
791 switch (rdata->rp_state) {
792 case RPORT_ST_INIT:
793
794
795
796
797
798
799
800
801
802
803
804
805 if (lport->point_to_multipoint)
806 break;
807 case RPORT_ST_DELETE:
808 mutex_unlock(&rdata->rp_mutex);
809 rjt_data.reason = ELS_RJT_FIP;
810 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
811 goto reject;
812 case RPORT_ST_FLOGI:
813 case RPORT_ST_PLOGI_WAIT:
814 case RPORT_ST_PLOGI:
815 break;
816 case RPORT_ST_PRLI:
817 case RPORT_ST_RTV:
818 case RPORT_ST_READY:
819 case RPORT_ST_ADISC:
820
821
822
823
824 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
825 mutex_unlock(&rdata->rp_mutex);
826 rjt_data.reason = ELS_RJT_BUSY;
827 rjt_data.explan = ELS_EXPL_NONE;
828 goto reject;
829 }
830 if (fc_rport_login_complete(rdata, fp)) {
831 mutex_unlock(&rdata->rp_mutex);
832 rjt_data.reason = ELS_RJT_LOGIC;
833 rjt_data.explan = ELS_EXPL_NONE;
834 goto reject;
835 }
836
837 fp = fc_frame_alloc(lport, sizeof(*flp));
838 if (!fp)
839 goto out;
840
841 fc_flogi_fill(lport, fp);
842 flp = fc_frame_payload_get(fp, sizeof(*flp));
843 flp->fl_cmd = ELS_LS_ACC;
844
845 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
846 lport->tt.frame_send(lport, fp);
847
848 if (rdata->ids.port_name < lport->wwpn)
849 fc_rport_enter_plogi(rdata);
850 else
851 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
852out:
853 mutex_unlock(&rdata->rp_mutex);
854 mutex_unlock(&disc->disc_mutex);
855 fc_frame_free(rx_fp);
856 return;
857
858reject:
859 mutex_unlock(&disc->disc_mutex);
860 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
861 fc_frame_free(rx_fp);
862}
863
864
865
866
867
868
869
870
871
872
873
874static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
875 void *rdata_arg)
876{
877 struct fc_rport_priv *rdata = rdata_arg;
878 struct fc_lport *lport = rdata->local_port;
879 struct fc_els_flogi *plp = NULL;
880 u16 csp_seq;
881 u16 cssp_seq;
882 u8 op;
883
884 mutex_lock(&rdata->rp_mutex);
885
886 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
887
888 if (rdata->rp_state != RPORT_ST_PLOGI) {
889 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
890 "%s\n", fc_rport_state(rdata));
891 if (IS_ERR(fp))
892 goto err;
893 goto out;
894 }
895
896 if (IS_ERR(fp)) {
897 fc_rport_error_retry(rdata, fp);
898 goto err;
899 }
900
901 op = fc_frame_payload_op(fp);
902 if (op == ELS_LS_ACC &&
903 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
904 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
905 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
906
907
908 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
909
910 if (lport->point_to_multipoint)
911 fc_rport_login_complete(rdata, fp);
912 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
913 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
914 if (cssp_seq < csp_seq)
915 csp_seq = cssp_seq;
916 rdata->max_seq = csp_seq;
917 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
918 fc_rport_enter_prli(rdata);
919 } else
920 fc_rport_error_retry(rdata, fp);
921
922out:
923 fc_frame_free(fp);
924err:
925 mutex_unlock(&rdata->rp_mutex);
926 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
927}
928
929static bool
930fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
931{
932 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
933 return true;
934 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
935 (lport->service_params & FCP_SPPF_INIT_FCN))
936 return true;
937 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
938 (lport->service_params & FCP_SPPF_TARG_FCN))
939 return true;
940 return false;
941}
942
943
944
945
946
947
948
949
950static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
951{
952 struct fc_lport *lport = rdata->local_port;
953 struct fc_frame *fp;
954
955 if (!fc_rport_compatible_roles(lport, rdata)) {
956 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
957 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
958 return;
959 }
960
961 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
962 fc_rport_state(rdata));
963
964 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
965
966 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
967 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
968 if (!fp) {
969 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
970 fc_rport_error_retry(rdata, fp);
971 return;
972 }
973 rdata->e_d_tov = lport->e_d_tov;
974
975 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
976 fc_rport_plogi_resp, rdata,
977 2 * lport->r_a_tov))
978 fc_rport_error_retry(rdata, NULL);
979 else
980 kref_get(&rdata->kref);
981}
982
983
984
985
986
987
988
989
990
991
992
993static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
994 void *rdata_arg)
995{
996 struct fc_rport_priv *rdata = rdata_arg;
997 struct {
998 struct fc_els_prli prli;
999 struct fc_els_spp spp;
1000 } *pp;
1001 struct fc_els_spp temp_spp;
1002 struct fc4_prov *prov;
1003 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1004 u32 fcp_parm = 0;
1005 u8 op;
1006 u8 resp_code = 0;
1007
1008 mutex_lock(&rdata->rp_mutex);
1009
1010 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1011
1012 if (rdata->rp_state != RPORT_ST_PRLI) {
1013 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1014 "%s\n", fc_rport_state(rdata));
1015 if (IS_ERR(fp))
1016 goto err;
1017 goto out;
1018 }
1019
1020 if (IS_ERR(fp)) {
1021 fc_rport_error_retry(rdata, fp);
1022 goto err;
1023 }
1024
1025
1026 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1027
1028 op = fc_frame_payload_op(fp);
1029 if (op == ELS_LS_ACC) {
1030 pp = fc_frame_payload_get(fp, sizeof(*pp));
1031 if (!pp)
1032 goto out;
1033
1034 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1035 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1036 pp->spp.spp_flags);
1037 rdata->spp_type = pp->spp.spp_type;
1038 if (resp_code != FC_SPP_RESP_ACK) {
1039 if (resp_code == FC_SPP_RESP_CONF)
1040 fc_rport_error(rdata, fp);
1041 else
1042 fc_rport_error_retry(rdata, fp);
1043 goto out;
1044 }
1045 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1046 goto out;
1047
1048 fcp_parm = ntohl(pp->spp.spp_params);
1049 if (fcp_parm & FCP_SPPF_RETRY)
1050 rdata->flags |= FC_RP_FLAGS_RETRY;
1051 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1052 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1053
1054 prov = fc_passive_prov[FC_TYPE_FCP];
1055 if (prov) {
1056 memset(&temp_spp, 0, sizeof(temp_spp));
1057 prov->prli(rdata, pp->prli.prli_spp_len,
1058 &pp->spp, &temp_spp);
1059 }
1060
1061 rdata->supported_classes = FC_COS_CLASS3;
1062 if (fcp_parm & FCP_SPPF_INIT_FCN)
1063 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1064 if (fcp_parm & FCP_SPPF_TARG_FCN)
1065 roles |= FC_RPORT_ROLE_FCP_TARGET;
1066
1067 rdata->ids.roles = roles;
1068 fc_rport_enter_rtv(rdata);
1069
1070 } else {
1071 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
1072 fc_rport_error_retry(rdata, fp);
1073 }
1074
1075out:
1076 fc_frame_free(fp);
1077err:
1078 mutex_unlock(&rdata->rp_mutex);
1079 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1080}
1081
1082
1083
1084
1085
1086
1087
1088
1089static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1090{
1091 struct fc_lport *lport = rdata->local_port;
1092 struct {
1093 struct fc_els_prli prli;
1094 struct fc_els_spp spp;
1095 } *pp;
1096 struct fc_frame *fp;
1097 struct fc4_prov *prov;
1098
1099
1100
1101
1102
1103 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1104 fc_rport_enter_ready(rdata);
1105 return;
1106 }
1107
1108 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1109 fc_rport_state(rdata));
1110
1111 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1112
1113 fp = fc_frame_alloc(lport, sizeof(*pp));
1114 if (!fp) {
1115 fc_rport_error_retry(rdata, fp);
1116 return;
1117 }
1118
1119 fc_prli_fill(lport, fp);
1120
1121 prov = fc_passive_prov[FC_TYPE_FCP];
1122 if (prov) {
1123 pp = fc_frame_payload_get(fp, sizeof(*pp));
1124 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1125 }
1126
1127 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1128 fc_host_port_id(lport->host), FC_TYPE_ELS,
1129 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1130
1131 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1132 NULL, rdata, 2 * lport->r_a_tov))
1133 fc_rport_error_retry(rdata, NULL);
1134 else
1135 kref_get(&rdata->kref);
1136}
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1151 void *rdata_arg)
1152{
1153 struct fc_rport_priv *rdata = rdata_arg;
1154 u8 op;
1155
1156 mutex_lock(&rdata->rp_mutex);
1157
1158 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1159
1160 if (rdata->rp_state != RPORT_ST_RTV) {
1161 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1162 "%s\n", fc_rport_state(rdata));
1163 if (IS_ERR(fp))
1164 goto err;
1165 goto out;
1166 }
1167
1168 if (IS_ERR(fp)) {
1169 fc_rport_error(rdata, fp);
1170 goto err;
1171 }
1172
1173 op = fc_frame_payload_op(fp);
1174 if (op == ELS_LS_ACC) {
1175 struct fc_els_rtv_acc *rtv;
1176 u32 toq;
1177 u32 tov;
1178
1179 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1180 if (rtv) {
1181 toq = ntohl(rtv->rtv_toq);
1182 tov = ntohl(rtv->rtv_r_a_tov);
1183 if (tov == 0)
1184 tov = 1;
1185 rdata->r_a_tov = tov;
1186 tov = ntohl(rtv->rtv_e_d_tov);
1187 if (toq & FC_ELS_RTV_EDRES)
1188 tov /= 1000000;
1189 if (tov == 0)
1190 tov = 1;
1191 rdata->e_d_tov = tov;
1192 }
1193 }
1194
1195 fc_rport_enter_ready(rdata);
1196
1197out:
1198 fc_frame_free(fp);
1199err:
1200 mutex_unlock(&rdata->rp_mutex);
1201 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1202}
1203
1204
1205
1206
1207
1208
1209
1210
1211static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1212{
1213 struct fc_frame *fp;
1214 struct fc_lport *lport = rdata->local_port;
1215
1216 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1217 fc_rport_state(rdata));
1218
1219 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1220
1221 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1222 if (!fp) {
1223 fc_rport_error_retry(rdata, fp);
1224 return;
1225 }
1226
1227 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1228 fc_rport_rtv_resp, rdata,
1229 2 * lport->r_a_tov))
1230 fc_rport_error_retry(rdata, NULL);
1231 else
1232 kref_get(&rdata->kref);
1233}
1234
1235
1236
1237
1238
1239
1240
1241static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1242 void *lport_arg)
1243{
1244 struct fc_lport *lport = lport_arg;
1245
1246 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1247 "Received a LOGO %s\n", fc_els_resp_type(fp));
1248 if (IS_ERR(fp))
1249 return;
1250 fc_frame_free(fp);
1251}
1252
1253
1254
1255
1256
1257
1258
1259
1260static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1261{
1262 struct fc_lport *lport = rdata->local_port;
1263 struct fc_frame *fp;
1264
1265 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1266 fc_rport_state(rdata));
1267
1268 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1269 if (!fp)
1270 return;
1271 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1272 fc_rport_logo_resp, lport, 0);
1273}
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1286 void *rdata_arg)
1287{
1288 struct fc_rport_priv *rdata = rdata_arg;
1289 struct fc_els_adisc *adisc;
1290 u8 op;
1291
1292 mutex_lock(&rdata->rp_mutex);
1293
1294 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1295
1296 if (rdata->rp_state != RPORT_ST_ADISC) {
1297 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1298 fc_rport_state(rdata));
1299 if (IS_ERR(fp))
1300 goto err;
1301 goto out;
1302 }
1303
1304 if (IS_ERR(fp)) {
1305 fc_rport_error(rdata, fp);
1306 goto err;
1307 }
1308
1309
1310
1311
1312
1313
1314 op = fc_frame_payload_op(fp);
1315 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1316 if (op != ELS_LS_ACC || !adisc ||
1317 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1318 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1319 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1320 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1321 fc_rport_enter_flogi(rdata);
1322 } else {
1323 FC_RPORT_DBG(rdata, "ADISC OK\n");
1324 fc_rport_enter_ready(rdata);
1325 }
1326out:
1327 fc_frame_free(fp);
1328err:
1329 mutex_unlock(&rdata->rp_mutex);
1330 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1331}
1332
1333
1334
1335
1336
1337
1338
1339
1340static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1341{
1342 struct fc_lport *lport = rdata->local_port;
1343 struct fc_frame *fp;
1344
1345 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1346 fc_rport_state(rdata));
1347
1348 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1349
1350 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1351 if (!fp) {
1352 fc_rport_error_retry(rdata, fp);
1353 return;
1354 }
1355 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1356 fc_rport_adisc_resp, rdata,
1357 2 * lport->r_a_tov))
1358 fc_rport_error_retry(rdata, NULL);
1359 else
1360 kref_get(&rdata->kref);
1361}
1362
1363
1364
1365
1366
1367
1368
1369
1370static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1371 struct fc_frame *in_fp)
1372{
1373 struct fc_lport *lport = rdata->local_port;
1374 struct fc_frame *fp;
1375 struct fc_els_adisc *adisc;
1376 struct fc_seq_els_data rjt_data;
1377
1378 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1379
1380 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1381 if (!adisc) {
1382 rjt_data.reason = ELS_RJT_PROT;
1383 rjt_data.explan = ELS_EXPL_INV_LEN;
1384 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1385 goto drop;
1386 }
1387
1388 fp = fc_frame_alloc(lport, sizeof(*adisc));
1389 if (!fp)
1390 goto drop;
1391 fc_adisc_fill(lport, fp);
1392 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1393 adisc->adisc_cmd = ELS_LS_ACC;
1394 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1395 lport->tt.frame_send(lport, fp);
1396drop:
1397 fc_frame_free(in_fp);
1398}
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1409 struct fc_frame *rx_fp)
1410
1411{
1412 struct fc_lport *lport = rdata->local_port;
1413 struct fc_frame *fp;
1414 struct fc_els_rls *rls;
1415 struct fc_els_rls_resp *rsp;
1416 struct fc_els_lesb *lesb;
1417 struct fc_seq_els_data rjt_data;
1418 struct fc_host_statistics *hst;
1419
1420 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1421 fc_rport_state(rdata));
1422
1423 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1424 if (!rls) {
1425 rjt_data.reason = ELS_RJT_PROT;
1426 rjt_data.explan = ELS_EXPL_INV_LEN;
1427 goto out_rjt;
1428 }
1429
1430 fp = fc_frame_alloc(lport, sizeof(*rsp));
1431 if (!fp) {
1432 rjt_data.reason = ELS_RJT_UNAB;
1433 rjt_data.explan = ELS_EXPL_INSUF_RES;
1434 goto out_rjt;
1435 }
1436
1437 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1438 memset(rsp, 0, sizeof(*rsp));
1439 rsp->rls_cmd = ELS_LS_ACC;
1440 lesb = &rsp->rls_lesb;
1441 if (lport->tt.get_lesb) {
1442
1443 lport->tt.get_lesb(lport, lesb);
1444 } else {
1445 fc_get_host_stats(lport->host);
1446 hst = &lport->host_stats;
1447 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1448 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1449 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1450 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1451 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1452 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1453 }
1454
1455 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1456 lport->tt.frame_send(lport, fp);
1457 goto out;
1458
1459out_rjt:
1460 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1461out:
1462 fc_frame_free(rx_fp);
1463}
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1476{
1477 struct fc_rport_priv *rdata;
1478 struct fc_seq_els_data els_data;
1479
1480 mutex_lock(&lport->disc.disc_mutex);
1481 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
1482 if (!rdata) {
1483 mutex_unlock(&lport->disc.disc_mutex);
1484 goto reject;
1485 }
1486 mutex_lock(&rdata->rp_mutex);
1487 mutex_unlock(&lport->disc.disc_mutex);
1488
1489 switch (rdata->rp_state) {
1490 case RPORT_ST_PRLI:
1491 case RPORT_ST_RTV:
1492 case RPORT_ST_READY:
1493 case RPORT_ST_ADISC:
1494 break;
1495 default:
1496 mutex_unlock(&rdata->rp_mutex);
1497 goto reject;
1498 }
1499
1500 switch (fc_frame_payload_op(fp)) {
1501 case ELS_PRLI:
1502 fc_rport_recv_prli_req(rdata, fp);
1503 break;
1504 case ELS_PRLO:
1505 fc_rport_recv_prlo_req(rdata, fp);
1506 break;
1507 case ELS_ADISC:
1508 fc_rport_recv_adisc_req(rdata, fp);
1509 break;
1510 case ELS_RRQ:
1511 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1512 fc_frame_free(fp);
1513 break;
1514 case ELS_REC:
1515 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1516 fc_frame_free(fp);
1517 break;
1518 case ELS_RLS:
1519 fc_rport_recv_rls_req(rdata, fp);
1520 break;
1521 default:
1522 fc_frame_free(fp);
1523 break;
1524 }
1525
1526 mutex_unlock(&rdata->rp_mutex);
1527 return;
1528
1529reject:
1530 els_data.reason = ELS_RJT_UNAB;
1531 els_data.explan = ELS_EXPL_PLOGI_REQD;
1532 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1533 fc_frame_free(fp);
1534}
1535
1536
1537
1538
1539
1540
1541
1542
1543static void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1544{
1545 struct fc_seq_els_data els_data;
1546
1547
1548
1549
1550
1551
1552
1553 switch (fc_frame_payload_op(fp)) {
1554 case ELS_FLOGI:
1555 fc_rport_recv_flogi_req(lport, fp);
1556 break;
1557 case ELS_PLOGI:
1558 fc_rport_recv_plogi_req(lport, fp);
1559 break;
1560 case ELS_LOGO:
1561 fc_rport_recv_logo_req(lport, fp);
1562 break;
1563 case ELS_PRLI:
1564 case ELS_PRLO:
1565 case ELS_ADISC:
1566 case ELS_RRQ:
1567 case ELS_REC:
1568 case ELS_RLS:
1569 fc_rport_recv_els_req(lport, fp);
1570 break;
1571 default:
1572 els_data.reason = ELS_RJT_UNSUP;
1573 els_data.explan = ELS_EXPL_NONE;
1574 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1575 fc_frame_free(fp);
1576 break;
1577 }
1578}
1579
1580
1581
1582
1583
1584
1585
1586
1587static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1588 struct fc_frame *rx_fp)
1589{
1590 struct fc_disc *disc;
1591 struct fc_rport_priv *rdata;
1592 struct fc_frame *fp = rx_fp;
1593 struct fc_els_flogi *pl;
1594 struct fc_seq_els_data rjt_data;
1595 u32 sid;
1596
1597 sid = fc_frame_sid(fp);
1598
1599 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1600
1601 pl = fc_frame_payload_get(fp, sizeof(*pl));
1602 if (!pl) {
1603 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1604 rjt_data.reason = ELS_RJT_PROT;
1605 rjt_data.explan = ELS_EXPL_INV_LEN;
1606 goto reject;
1607 }
1608
1609 disc = &lport->disc;
1610 mutex_lock(&disc->disc_mutex);
1611 rdata = lport->tt.rport_create(lport, sid);
1612 if (!rdata) {
1613 mutex_unlock(&disc->disc_mutex);
1614 rjt_data.reason = ELS_RJT_UNAB;
1615 rjt_data.explan = ELS_EXPL_INSUF_RES;
1616 goto reject;
1617 }
1618
1619 mutex_lock(&rdata->rp_mutex);
1620 mutex_unlock(&disc->disc_mutex);
1621
1622 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1623 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636 switch (rdata->rp_state) {
1637 case RPORT_ST_INIT:
1638 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1639 break;
1640 case RPORT_ST_PLOGI_WAIT:
1641 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1642 break;
1643 case RPORT_ST_PLOGI:
1644 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1645 if (rdata->ids.port_name < lport->wwpn) {
1646 mutex_unlock(&rdata->rp_mutex);
1647 rjt_data.reason = ELS_RJT_INPROG;
1648 rjt_data.explan = ELS_EXPL_NONE;
1649 goto reject;
1650 }
1651 break;
1652 case RPORT_ST_PRLI:
1653 case RPORT_ST_RTV:
1654 case RPORT_ST_READY:
1655 case RPORT_ST_ADISC:
1656 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1657 "- ignored for now\n", rdata->rp_state);
1658
1659 break;
1660 case RPORT_ST_FLOGI:
1661 case RPORT_ST_DELETE:
1662 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1663 fc_rport_state(rdata));
1664 mutex_unlock(&rdata->rp_mutex);
1665 rjt_data.reason = ELS_RJT_BUSY;
1666 rjt_data.explan = ELS_EXPL_NONE;
1667 goto reject;
1668 }
1669 if (!fc_rport_compatible_roles(lport, rdata)) {
1670 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1671 mutex_unlock(&rdata->rp_mutex);
1672 rjt_data.reason = ELS_RJT_LOGIC;
1673 rjt_data.explan = ELS_EXPL_NONE;
1674 goto reject;
1675 }
1676
1677
1678
1679
1680 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1681
1682
1683
1684
1685 fp = fc_frame_alloc(lport, sizeof(*pl));
1686 if (!fp)
1687 goto out;
1688
1689 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1690 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1691 lport->tt.frame_send(lport, fp);
1692 fc_rport_enter_prli(rdata);
1693out:
1694 mutex_unlock(&rdata->rp_mutex);
1695 fc_frame_free(rx_fp);
1696 return;
1697
1698reject:
1699 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1700 fc_frame_free(fp);
1701}
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1712 struct fc_frame *rx_fp)
1713{
1714 struct fc_lport *lport = rdata->local_port;
1715 struct fc_frame *fp;
1716 struct {
1717 struct fc_els_prli prli;
1718 struct fc_els_spp spp;
1719 } *pp;
1720 struct fc_els_spp *rspp;
1721 struct fc_els_spp *spp;
1722 unsigned int len;
1723 unsigned int plen;
1724 enum fc_els_spp_resp resp;
1725 enum fc_els_spp_resp passive;
1726 struct fc_seq_els_data rjt_data;
1727 struct fc4_prov *prov;
1728
1729 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1730 fc_rport_state(rdata));
1731
1732 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1733 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1734 if (!pp)
1735 goto reject_len;
1736 plen = ntohs(pp->prli.prli_len);
1737 if ((plen % 4) != 0 || plen > len || plen < 16)
1738 goto reject_len;
1739 if (plen < len)
1740 len = plen;
1741 plen = pp->prli.prli_spp_len;
1742 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1743 plen > len || len < sizeof(*pp) || plen < 12)
1744 goto reject_len;
1745 rspp = &pp->spp;
1746
1747 fp = fc_frame_alloc(lport, len);
1748 if (!fp) {
1749 rjt_data.reason = ELS_RJT_UNAB;
1750 rjt_data.explan = ELS_EXPL_INSUF_RES;
1751 goto reject;
1752 }
1753 pp = fc_frame_payload_get(fp, len);
1754 WARN_ON(!pp);
1755 memset(pp, 0, len);
1756 pp->prli.prli_cmd = ELS_LS_ACC;
1757 pp->prli.prli_spp_len = plen;
1758 pp->prli.prli_len = htons(len);
1759 len -= sizeof(struct fc_els_prli);
1760
1761
1762
1763
1764
1765
1766 spp = &pp->spp;
1767 mutex_lock(&fc_prov_mutex);
1768 while (len >= plen) {
1769 rdata->spp_type = rspp->spp_type;
1770 spp->spp_type = rspp->spp_type;
1771 spp->spp_type_ext = rspp->spp_type_ext;
1772 resp = 0;
1773
1774 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1775 prov = fc_active_prov[rspp->spp_type];
1776 if (prov)
1777 resp = prov->prli(rdata, plen, rspp, spp);
1778 prov = fc_passive_prov[rspp->spp_type];
1779 if (prov) {
1780 passive = prov->prli(rdata, plen, rspp, spp);
1781 if (!resp || passive == FC_SPP_RESP_ACK)
1782 resp = passive;
1783 }
1784 }
1785 if (!resp) {
1786 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1787 resp |= FC_SPP_RESP_CONF;
1788 else
1789 resp |= FC_SPP_RESP_INVL;
1790 }
1791 spp->spp_flags |= resp;
1792 len -= plen;
1793 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1794 spp = (struct fc_els_spp *)((char *)spp + plen);
1795 }
1796 mutex_unlock(&fc_prov_mutex);
1797
1798
1799
1800
1801 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1802 lport->tt.frame_send(lport, fp);
1803
1804 switch (rdata->rp_state) {
1805 case RPORT_ST_PRLI:
1806 fc_rport_enter_ready(rdata);
1807 break;
1808 default:
1809 break;
1810 }
1811 goto drop;
1812
1813reject_len:
1814 rjt_data.reason = ELS_RJT_PROT;
1815 rjt_data.explan = ELS_EXPL_INV_LEN;
1816reject:
1817 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1818drop:
1819 fc_frame_free(rx_fp);
1820}
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1831 struct fc_frame *rx_fp)
1832{
1833 struct fc_lport *lport = rdata->local_port;
1834 struct fc_frame *fp;
1835 struct {
1836 struct fc_els_prlo prlo;
1837 struct fc_els_spp spp;
1838 } *pp;
1839 struct fc_els_spp *rspp;
1840 struct fc_els_spp *spp;
1841 unsigned int len;
1842 unsigned int plen;
1843 struct fc_seq_els_data rjt_data;
1844
1845 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1846 fc_rport_state(rdata));
1847
1848 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1849 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1850 if (!pp)
1851 goto reject_len;
1852 plen = ntohs(pp->prlo.prlo_len);
1853 if (plen != 20)
1854 goto reject_len;
1855 if (plen < len)
1856 len = plen;
1857
1858 rspp = &pp->spp;
1859
1860 fp = fc_frame_alloc(lport, len);
1861 if (!fp) {
1862 rjt_data.reason = ELS_RJT_UNAB;
1863 rjt_data.explan = ELS_EXPL_INSUF_RES;
1864 goto reject;
1865 }
1866
1867 pp = fc_frame_payload_get(fp, len);
1868 WARN_ON(!pp);
1869 memset(pp, 0, len);
1870 pp->prlo.prlo_cmd = ELS_LS_ACC;
1871 pp->prlo.prlo_obs = 0x10;
1872 pp->prlo.prlo_len = htons(len);
1873 spp = &pp->spp;
1874 spp->spp_type = rspp->spp_type;
1875 spp->spp_type_ext = rspp->spp_type_ext;
1876 spp->spp_flags = FC_SPP_RESP_ACK;
1877
1878 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1879
1880 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1881 lport->tt.frame_send(lport, fp);
1882 goto drop;
1883
1884reject_len:
1885 rjt_data.reason = ELS_RJT_PROT;
1886 rjt_data.explan = ELS_EXPL_INV_LEN;
1887reject:
1888 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1889drop:
1890 fc_frame_free(rx_fp);
1891}
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
1902{
1903 struct fc_rport_priv *rdata;
1904 u32 sid;
1905
1906 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
1907
1908 sid = fc_frame_sid(fp);
1909
1910 mutex_lock(&lport->disc.disc_mutex);
1911 rdata = lport->tt.rport_lookup(lport, sid);
1912 if (rdata) {
1913 mutex_lock(&rdata->rp_mutex);
1914 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1915 fc_rport_state(rdata));
1916
1917 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1918 mutex_unlock(&rdata->rp_mutex);
1919 } else
1920 FC_RPORT_ID_DBG(lport, sid,
1921 "Received LOGO from non-logged-in port\n");
1922 mutex_unlock(&lport->disc.disc_mutex);
1923 fc_frame_free(fp);
1924}
1925
1926
1927
1928
1929static void fc_rport_flush_queue(void)
1930{
1931 flush_workqueue(rport_event_queue);
1932}
1933
1934
1935
1936
1937
1938int fc_rport_init(struct fc_lport *lport)
1939{
1940 if (!lport->tt.rport_lookup)
1941 lport->tt.rport_lookup = fc_rport_lookup;
1942
1943 if (!lport->tt.rport_create)
1944 lport->tt.rport_create = fc_rport_create;
1945
1946 if (!lport->tt.rport_login)
1947 lport->tt.rport_login = fc_rport_login;
1948
1949 if (!lport->tt.rport_logoff)
1950 lport->tt.rport_logoff = fc_rport_logoff;
1951
1952 if (!lport->tt.rport_recv_req)
1953 lport->tt.rport_recv_req = fc_rport_recv_req;
1954
1955 if (!lport->tt.rport_flush_queue)
1956 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1957
1958 if (!lport->tt.rport_destroy)
1959 lport->tt.rport_destroy = fc_rport_destroy;
1960
1961 return 0;
1962}
1963EXPORT_SYMBOL(fc_rport_init);
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
1976 const struct fc_els_spp *rspp,
1977 struct fc_els_spp *spp)
1978{
1979 struct fc_lport *lport = rdata->local_port;
1980 u32 fcp_parm;
1981
1982 fcp_parm = ntohl(rspp->spp_params);
1983 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1984 if (fcp_parm & FCP_SPPF_INIT_FCN)
1985 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1986 if (fcp_parm & FCP_SPPF_TARG_FCN)
1987 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1988 if (fcp_parm & FCP_SPPF_RETRY)
1989 rdata->flags |= FC_RP_FLAGS_RETRY;
1990 rdata->supported_classes = FC_COS_CLASS3;
1991
1992 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
1993 return 0;
1994
1995 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1996
1997
1998
1999
2000 fcp_parm = ntohl(spp->spp_params);
2001 spp->spp_params = htonl(fcp_parm | lport->service_params);
2002 return FC_SPP_RESP_ACK;
2003}
2004
2005
2006
2007
2008struct fc4_prov fc_rport_fcp_init = {
2009 .prli = fc_rport_fcp_prli,
2010};
2011
2012
2013
2014
2015
2016
2017
2018
2019static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2020 const struct fc_els_spp *rspp,
2021 struct fc_els_spp *spp)
2022{
2023 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2024 return FC_SPP_RESP_INVL;
2025 return FC_SPP_RESP_ACK;
2026}
2027
2028
2029
2030
2031
2032
2033
2034struct fc4_prov fc_rport_t0_prov = {
2035 .prli = fc_rport_t0_prli,
2036};
2037
2038
2039
2040
2041int fc_setup_rport(void)
2042{
2043 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2044 if (!rport_event_queue)
2045 return -ENOMEM;
2046 return 0;
2047}
2048
2049
2050
2051
2052void fc_destroy_rport(void)
2053{
2054 destroy_workqueue(rport_event_queue);
2055}
2056
2057
2058
2059
2060
2061void fc_rport_terminate_io(struct fc_rport *rport)
2062{
2063 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2064 struct fc_lport *lport = rpriv->local_port;
2065
2066 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2067 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2068}
2069EXPORT_SYMBOL(fc_rport_terminate_io);
2070