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