0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #include <linux/kernel.h>
0049 #include <linux/spinlock.h>
0050 #include <linux/interrupt.h>
0051 #include <linux/slab.h>
0052 #include <linux/rcupdate.h>
0053 #include <linux/timer.h>
0054 #include <linux/workqueue.h>
0055 #include <linux/export.h>
0056 #include <linux/rculist.h>
0057
0058 #include <asm/unaligned.h>
0059
0060 #include <scsi/libfc.h>
0061
0062 #include "fc_encode.h"
0063 #include "fc_libfc.h"
0064
0065 static struct workqueue_struct *rport_event_queue;
0066
0067 static void fc_rport_enter_flogi(struct fc_rport_priv *);
0068 static void fc_rport_enter_plogi(struct fc_rport_priv *);
0069 static void fc_rport_enter_prli(struct fc_rport_priv *);
0070 static void fc_rport_enter_rtv(struct fc_rport_priv *);
0071 static void fc_rport_enter_ready(struct fc_rport_priv *);
0072 static void fc_rport_enter_logo(struct fc_rport_priv *);
0073 static void fc_rport_enter_adisc(struct fc_rport_priv *);
0074
0075 static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
0076 static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
0077 static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
0078 static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
0079 static void fc_rport_timeout(struct work_struct *);
0080 static void fc_rport_error(struct fc_rport_priv *, int);
0081 static void fc_rport_error_retry(struct fc_rport_priv *, int);
0082 static void fc_rport_work(struct work_struct *);
0083
0084 static const char *fc_rport_state_names[] = {
0085 [RPORT_ST_INIT] = "Init",
0086 [RPORT_ST_FLOGI] = "FLOGI",
0087 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
0088 [RPORT_ST_PLOGI] = "PLOGI",
0089 [RPORT_ST_PRLI] = "PRLI",
0090 [RPORT_ST_RTV] = "RTV",
0091 [RPORT_ST_READY] = "Ready",
0092 [RPORT_ST_ADISC] = "ADISC",
0093 [RPORT_ST_DELETE] = "Delete",
0094 };
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
0105 u32 port_id)
0106 {
0107 struct fc_rport_priv *rdata = NULL, *tmp_rdata;
0108
0109 rcu_read_lock();
0110 list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers)
0111 if (tmp_rdata->ids.port_id == port_id &&
0112 kref_get_unless_zero(&tmp_rdata->kref)) {
0113 rdata = tmp_rdata;
0114 break;
0115 }
0116 rcu_read_unlock();
0117 return rdata;
0118 }
0119 EXPORT_SYMBOL(fc_rport_lookup);
0120
0121
0122
0123
0124
0125
0126
0127
0128 struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id)
0129 {
0130 struct fc_rport_priv *rdata;
0131 size_t rport_priv_size = sizeof(*rdata);
0132
0133 lockdep_assert_held(&lport->disc.disc_mutex);
0134
0135 rdata = fc_rport_lookup(lport, port_id);
0136 if (rdata) {
0137 kref_put(&rdata->kref, fc_rport_destroy);
0138 return rdata;
0139 }
0140
0141 if (lport->rport_priv_size > 0)
0142 rport_priv_size = lport->rport_priv_size;
0143 rdata = kzalloc(rport_priv_size, GFP_KERNEL);
0144 if (!rdata)
0145 return NULL;
0146
0147 rdata->ids.node_name = -1;
0148 rdata->ids.port_name = -1;
0149 rdata->ids.port_id = port_id;
0150 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
0151
0152 kref_init(&rdata->kref);
0153 mutex_init(&rdata->rp_mutex);
0154 rdata->local_port = lport;
0155 rdata->rp_state = RPORT_ST_INIT;
0156 rdata->event = RPORT_EV_NONE;
0157 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
0158 rdata->e_d_tov = lport->e_d_tov;
0159 rdata->r_a_tov = lport->r_a_tov;
0160 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
0161 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
0162 INIT_WORK(&rdata->event_work, fc_rport_work);
0163 if (port_id != FC_FID_DIR_SERV) {
0164 rdata->lld_event_callback = lport->tt.rport_event_callback;
0165 list_add_rcu(&rdata->peers, &lport->disc.rports);
0166 }
0167 return rdata;
0168 }
0169 EXPORT_SYMBOL(fc_rport_create);
0170
0171
0172
0173
0174
0175 void fc_rport_destroy(struct kref *kref)
0176 {
0177 struct fc_rport_priv *rdata;
0178
0179 rdata = container_of(kref, struct fc_rport_priv, kref);
0180 kfree_rcu(rdata, rcu);
0181 }
0182 EXPORT_SYMBOL(fc_rport_destroy);
0183
0184
0185
0186
0187
0188 static const char *fc_rport_state(struct fc_rport_priv *rdata)
0189 {
0190 const char *cp;
0191
0192 cp = fc_rport_state_names[rdata->rp_state];
0193 if (!cp)
0194 cp = "Unknown";
0195 return cp;
0196 }
0197
0198
0199
0200
0201
0202
0203 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
0204 {
0205 if (timeout)
0206 rport->dev_loss_tmo = timeout;
0207 else
0208 rport->dev_loss_tmo = 1;
0209 }
0210 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
0211
0212
0213
0214
0215
0216
0217
0218
0219 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
0220 unsigned int maxval)
0221 {
0222 unsigned int mfs;
0223
0224
0225
0226
0227
0228 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
0229 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
0230 maxval = mfs;
0231 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
0232 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
0233 maxval = mfs;
0234 return maxval;
0235 }
0236
0237
0238
0239
0240
0241
0242 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
0243 enum fc_rport_state new)
0244 {
0245 lockdep_assert_held(&rdata->rp_mutex);
0246
0247 if (rdata->rp_state != new)
0248 rdata->retries = 0;
0249 rdata->rp_state = new;
0250 }
0251
0252
0253
0254
0255
0256
0257
0258 static void fc_rport_work(struct work_struct *work)
0259 {
0260 u32 port_id;
0261 struct fc_rport_priv *rdata =
0262 container_of(work, struct fc_rport_priv, event_work);
0263 struct fc_rport_libfc_priv *rpriv;
0264 enum fc_rport_event event;
0265 struct fc_lport *lport = rdata->local_port;
0266 struct fc_rport_operations *rport_ops;
0267 struct fc_rport_identifiers ids;
0268 struct fc_rport *rport;
0269 struct fc4_prov *prov;
0270 u8 type;
0271
0272 mutex_lock(&rdata->rp_mutex);
0273 event = rdata->event;
0274 rport_ops = rdata->ops;
0275 rport = rdata->rport;
0276
0277 FC_RPORT_DBG(rdata, "work event %u\n", event);
0278
0279 switch (event) {
0280 case RPORT_EV_READY:
0281 ids = rdata->ids;
0282 rdata->event = RPORT_EV_NONE;
0283 rdata->major_retries = 0;
0284 kref_get(&rdata->kref);
0285 mutex_unlock(&rdata->rp_mutex);
0286
0287 if (!rport) {
0288 FC_RPORT_DBG(rdata, "No rport!\n");
0289 rport = fc_remote_port_add(lport->host, 0, &ids);
0290 }
0291 if (!rport) {
0292 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
0293 fc_rport_logoff(rdata);
0294 kref_put(&rdata->kref, fc_rport_destroy);
0295 return;
0296 }
0297 mutex_lock(&rdata->rp_mutex);
0298 if (rdata->rport)
0299 FC_RPORT_DBG(rdata, "rport already allocated\n");
0300 rdata->rport = rport;
0301 rport->maxframe_size = rdata->maxframe_size;
0302 rport->supported_classes = rdata->supported_classes;
0303
0304 rpriv = rport->dd_data;
0305 rpriv->local_port = lport;
0306 rpriv->rp_state = rdata->rp_state;
0307 rpriv->flags = rdata->flags;
0308 rpriv->e_d_tov = rdata->e_d_tov;
0309 rpriv->r_a_tov = rdata->r_a_tov;
0310 mutex_unlock(&rdata->rp_mutex);
0311
0312 if (rport_ops && rport_ops->event_callback) {
0313 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
0314 rport_ops->event_callback(lport, rdata, event);
0315 }
0316 if (rdata->lld_event_callback) {
0317 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
0318 rdata->lld_event_callback(lport, rdata, event);
0319 }
0320 kref_put(&rdata->kref, fc_rport_destroy);
0321 break;
0322
0323 case RPORT_EV_FAILED:
0324 case RPORT_EV_LOGO:
0325 case RPORT_EV_STOP:
0326 if (rdata->prli_count) {
0327 mutex_lock(&fc_prov_mutex);
0328 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
0329 prov = fc_passive_prov[type];
0330 if (prov && prov->prlo)
0331 prov->prlo(rdata);
0332 }
0333 mutex_unlock(&fc_prov_mutex);
0334 }
0335 port_id = rdata->ids.port_id;
0336 mutex_unlock(&rdata->rp_mutex);
0337
0338 if (rport_ops && rport_ops->event_callback) {
0339 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
0340 rport_ops->event_callback(lport, rdata, event);
0341 }
0342 if (rdata->lld_event_callback) {
0343 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
0344 rdata->lld_event_callback(lport, rdata, event);
0345 }
0346 if (cancel_delayed_work_sync(&rdata->retry_work))
0347 kref_put(&rdata->kref, fc_rport_destroy);
0348
0349
0350
0351
0352 lport->tt.exch_mgr_reset(lport, 0, port_id);
0353 lport->tt.exch_mgr_reset(lport, port_id, 0);
0354
0355 if (rport) {
0356 rpriv = rport->dd_data;
0357 rpriv->rp_state = RPORT_ST_DELETE;
0358 mutex_lock(&rdata->rp_mutex);
0359 rdata->rport = NULL;
0360 mutex_unlock(&rdata->rp_mutex);
0361 fc_remote_port_delete(rport);
0362 }
0363
0364 mutex_lock(&rdata->rp_mutex);
0365 if (rdata->rp_state == RPORT_ST_DELETE) {
0366 if (port_id == FC_FID_DIR_SERV) {
0367 rdata->event = RPORT_EV_NONE;
0368 mutex_unlock(&rdata->rp_mutex);
0369 kref_put(&rdata->kref, fc_rport_destroy);
0370 } else if ((rdata->flags & FC_RP_STARTED) &&
0371 rdata->major_retries <
0372 lport->max_rport_retry_count) {
0373 rdata->major_retries++;
0374 rdata->event = RPORT_EV_NONE;
0375 FC_RPORT_DBG(rdata, "work restart\n");
0376 fc_rport_enter_flogi(rdata);
0377 mutex_unlock(&rdata->rp_mutex);
0378 } else {
0379 mutex_unlock(&rdata->rp_mutex);
0380 FC_RPORT_DBG(rdata, "work delete\n");
0381 mutex_lock(&lport->disc.disc_mutex);
0382 list_del_rcu(&rdata->peers);
0383 mutex_unlock(&lport->disc.disc_mutex);
0384 kref_put(&rdata->kref, fc_rport_destroy);
0385 }
0386 } else {
0387
0388
0389
0390 rdata->event = RPORT_EV_NONE;
0391 if (rdata->rp_state == RPORT_ST_READY) {
0392 FC_RPORT_DBG(rdata, "work reopen\n");
0393 fc_rport_enter_ready(rdata);
0394 }
0395 mutex_unlock(&rdata->rp_mutex);
0396 }
0397 break;
0398
0399 default:
0400 mutex_unlock(&rdata->rp_mutex);
0401 break;
0402 }
0403 kref_put(&rdata->kref, fc_rport_destroy);
0404 }
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426 int fc_rport_login(struct fc_rport_priv *rdata)
0427 {
0428 mutex_lock(&rdata->rp_mutex);
0429
0430 if (rdata->flags & FC_RP_STARTED) {
0431 FC_RPORT_DBG(rdata, "port already started\n");
0432 mutex_unlock(&rdata->rp_mutex);
0433 return 0;
0434 }
0435
0436 rdata->flags |= FC_RP_STARTED;
0437 switch (rdata->rp_state) {
0438 case RPORT_ST_READY:
0439 FC_RPORT_DBG(rdata, "ADISC port\n");
0440 fc_rport_enter_adisc(rdata);
0441 break;
0442 case RPORT_ST_DELETE:
0443 FC_RPORT_DBG(rdata, "Restart deleted port\n");
0444 break;
0445 case RPORT_ST_INIT:
0446 FC_RPORT_DBG(rdata, "Login to port\n");
0447 fc_rport_enter_flogi(rdata);
0448 break;
0449 default:
0450 FC_RPORT_DBG(rdata, "Login in progress, state %s\n",
0451 fc_rport_state(rdata));
0452 break;
0453 }
0454 mutex_unlock(&rdata->rp_mutex);
0455
0456 return 0;
0457 }
0458 EXPORT_SYMBOL(fc_rport_login);
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
0475 enum fc_rport_event event)
0476 {
0477 lockdep_assert_held(&rdata->rp_mutex);
0478
0479 if (rdata->rp_state == RPORT_ST_DELETE)
0480 return;
0481
0482 FC_RPORT_DBG(rdata, "Delete port\n");
0483
0484 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
0485
0486 if (rdata->event == RPORT_EV_NONE) {
0487 kref_get(&rdata->kref);
0488 if (!queue_work(rport_event_queue, &rdata->event_work))
0489 kref_put(&rdata->kref, fc_rport_destroy);
0490 }
0491
0492 rdata->event = event;
0493 }
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 int fc_rport_logoff(struct fc_rport_priv *rdata)
0504 {
0505 struct fc_lport *lport = rdata->local_port;
0506 u32 port_id = rdata->ids.port_id;
0507
0508 mutex_lock(&rdata->rp_mutex);
0509
0510 FC_RPORT_DBG(rdata, "Remove port\n");
0511
0512 rdata->flags &= ~FC_RP_STARTED;
0513 if (rdata->rp_state == RPORT_ST_DELETE) {
0514 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
0515 goto out;
0516 }
0517
0518
0519
0520
0521
0522
0523 lport->tt.exch_mgr_reset(lport, 0, port_id);
0524 lport->tt.exch_mgr_reset(lport, port_id, 0);
0525
0526 fc_rport_enter_logo(rdata);
0527
0528
0529
0530
0531
0532 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
0533 out:
0534 mutex_unlock(&rdata->rp_mutex);
0535 return 0;
0536 }
0537 EXPORT_SYMBOL(fc_rport_logoff);
0538
0539
0540
0541
0542
0543
0544
0545 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
0546 {
0547 lockdep_assert_held(&rdata->rp_mutex);
0548
0549 fc_rport_state_enter(rdata, RPORT_ST_READY);
0550
0551 FC_RPORT_DBG(rdata, "Port is Ready\n");
0552
0553 kref_get(&rdata->kref);
0554 if (rdata->event == RPORT_EV_NONE &&
0555 !queue_work(rport_event_queue, &rdata->event_work))
0556 kref_put(&rdata->kref, fc_rport_destroy);
0557
0558 rdata->event = RPORT_EV_READY;
0559 }
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571 static void fc_rport_timeout(struct work_struct *work)
0572 {
0573 struct fc_rport_priv *rdata =
0574 container_of(work, struct fc_rport_priv, retry_work.work);
0575
0576 mutex_lock(&rdata->rp_mutex);
0577 FC_RPORT_DBG(rdata, "Port timeout, state %s\n", fc_rport_state(rdata));
0578
0579 switch (rdata->rp_state) {
0580 case RPORT_ST_FLOGI:
0581 fc_rport_enter_flogi(rdata);
0582 break;
0583 case RPORT_ST_PLOGI:
0584 fc_rport_enter_plogi(rdata);
0585 break;
0586 case RPORT_ST_PRLI:
0587 fc_rport_enter_prli(rdata);
0588 break;
0589 case RPORT_ST_RTV:
0590 fc_rport_enter_rtv(rdata);
0591 break;
0592 case RPORT_ST_ADISC:
0593 fc_rport_enter_adisc(rdata);
0594 break;
0595 case RPORT_ST_PLOGI_WAIT:
0596 case RPORT_ST_READY:
0597 case RPORT_ST_INIT:
0598 case RPORT_ST_DELETE:
0599 break;
0600 }
0601
0602 mutex_unlock(&rdata->rp_mutex);
0603 kref_put(&rdata->kref, fc_rport_destroy);
0604 }
0605
0606
0607
0608
0609
0610
0611
0612
0613 static void fc_rport_error(struct fc_rport_priv *rdata, int err)
0614 {
0615 struct fc_lport *lport = rdata->local_port;
0616
0617 lockdep_assert_held(&rdata->rp_mutex);
0618
0619 FC_RPORT_DBG(rdata, "Error %d in state %s, retries %d\n",
0620 -err, fc_rport_state(rdata), rdata->retries);
0621
0622 switch (rdata->rp_state) {
0623 case RPORT_ST_FLOGI:
0624 rdata->flags &= ~FC_RP_STARTED;
0625 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
0626 break;
0627 case RPORT_ST_PLOGI:
0628 if (lport->point_to_multipoint) {
0629 rdata->flags &= ~FC_RP_STARTED;
0630 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
0631 } else
0632 fc_rport_enter_logo(rdata);
0633 break;
0634 case RPORT_ST_RTV:
0635 fc_rport_enter_ready(rdata);
0636 break;
0637 case RPORT_ST_PRLI:
0638 fc_rport_enter_plogi(rdata);
0639 break;
0640 case RPORT_ST_ADISC:
0641 fc_rport_enter_logo(rdata);
0642 break;
0643 case RPORT_ST_PLOGI_WAIT:
0644 case RPORT_ST_DELETE:
0645 case RPORT_ST_READY:
0646 case RPORT_ST_INIT:
0647 break;
0648 }
0649 }
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661 static void fc_rport_error_retry(struct fc_rport_priv *rdata, int err)
0662 {
0663 unsigned long delay = msecs_to_jiffies(rdata->e_d_tov);
0664
0665 lockdep_assert_held(&rdata->rp_mutex);
0666
0667
0668 if (err == -FC_EX_CLOSED)
0669 goto out;
0670
0671 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
0672 FC_RPORT_DBG(rdata, "Error %d in state %s, retrying\n",
0673 err, fc_rport_state(rdata));
0674 rdata->retries++;
0675
0676 if (err == -FC_EX_TIMEOUT)
0677 delay = 0;
0678 kref_get(&rdata->kref);
0679 if (!schedule_delayed_work(&rdata->retry_work, delay))
0680 kref_put(&rdata->kref, fc_rport_destroy);
0681 return;
0682 }
0683
0684 out:
0685 fc_rport_error(rdata, err);
0686 }
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
0699 struct fc_frame *fp)
0700 {
0701 struct fc_lport *lport = rdata->local_port;
0702 struct fc_els_flogi *flogi;
0703 unsigned int e_d_tov;
0704 u16 csp_flags;
0705
0706 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
0707 if (!flogi)
0708 return -EINVAL;
0709
0710 csp_flags = ntohs(flogi->fl_csp.sp_features);
0711
0712 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
0713 if (csp_flags & FC_SP_FT_FPORT) {
0714 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
0715 return -EINVAL;
0716 }
0717 } else {
0718
0719
0720
0721
0722 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
0723 if (csp_flags & FC_SP_FT_EDTR)
0724 e_d_tov /= 1000000;
0725 if (e_d_tov > rdata->e_d_tov)
0726 rdata->e_d_tov = e_d_tov;
0727 }
0728 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
0729 return 0;
0730 }
0731
0732
0733
0734
0735
0736
0737
0738 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
0739 void *rp_arg)
0740 {
0741 struct fc_rport_priv *rdata = rp_arg;
0742 struct fc_lport *lport = rdata->local_port;
0743 struct fc_els_flogi *flogi;
0744 unsigned int r_a_tov;
0745 u8 opcode;
0746 int err = 0;
0747
0748 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n",
0749 IS_ERR(fp) ? "error" : fc_els_resp_type(fp));
0750
0751 if (fp == ERR_PTR(-FC_EX_CLOSED))
0752 goto put;
0753
0754 mutex_lock(&rdata->rp_mutex);
0755
0756 if (rdata->rp_state != RPORT_ST_FLOGI) {
0757 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
0758 "%s\n", fc_rport_state(rdata));
0759 if (IS_ERR(fp))
0760 goto err;
0761 goto out;
0762 }
0763
0764 if (IS_ERR(fp)) {
0765 fc_rport_error(rdata, PTR_ERR(fp));
0766 goto err;
0767 }
0768 opcode = fc_frame_payload_op(fp);
0769 if (opcode == ELS_LS_RJT) {
0770 struct fc_els_ls_rjt *rjt;
0771
0772 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
0773 FC_RPORT_DBG(rdata, "FLOGI ELS rejected, reason %x expl %x\n",
0774 rjt->er_reason, rjt->er_explan);
0775 err = -FC_EX_ELS_RJT;
0776 goto bad;
0777 } else if (opcode != ELS_LS_ACC) {
0778 FC_RPORT_DBG(rdata, "FLOGI ELS invalid opcode %x\n", opcode);
0779 err = -FC_EX_ELS_RJT;
0780 goto bad;
0781 }
0782 if (fc_rport_login_complete(rdata, fp)) {
0783 FC_RPORT_DBG(rdata, "FLOGI failed, no login\n");
0784 err = -FC_EX_INV_LOGIN;
0785 goto bad;
0786 }
0787
0788 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
0789 if (!flogi) {
0790 err = -FC_EX_ALLOC_ERR;
0791 goto bad;
0792 }
0793 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
0794 if (r_a_tov > rdata->r_a_tov)
0795 rdata->r_a_tov = r_a_tov;
0796
0797 if (rdata->ids.port_name < lport->wwpn)
0798 fc_rport_enter_plogi(rdata);
0799 else
0800 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
0801 out:
0802 fc_frame_free(fp);
0803 err:
0804 mutex_unlock(&rdata->rp_mutex);
0805 put:
0806 kref_put(&rdata->kref, fc_rport_destroy);
0807 return;
0808 bad:
0809 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
0810 fc_rport_error_retry(rdata, err);
0811 goto out;
0812 }
0813
0814
0815
0816
0817
0818
0819
0820 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
0821 {
0822 struct fc_lport *lport = rdata->local_port;
0823 struct fc_frame *fp;
0824
0825 lockdep_assert_held(&rdata->rp_mutex);
0826
0827 if (!lport->point_to_multipoint)
0828 return fc_rport_enter_plogi(rdata);
0829
0830 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
0831 fc_rport_state(rdata));
0832
0833 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
0834
0835 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
0836 if (!fp)
0837 return fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
0838
0839 kref_get(&rdata->kref);
0840 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
0841 fc_rport_flogi_resp, rdata,
0842 2 * lport->r_a_tov)) {
0843 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
0844 kref_put(&rdata->kref, fc_rport_destroy);
0845 }
0846 }
0847
0848
0849
0850
0851
0852
0853
0854
0855 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
0856 struct fc_frame *rx_fp)
0857 {
0858 struct fc_els_flogi *flp;
0859 struct fc_rport_priv *rdata;
0860 struct fc_frame *fp = rx_fp;
0861 struct fc_seq_els_data rjt_data;
0862 u32 sid;
0863
0864 sid = fc_frame_sid(fp);
0865
0866 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
0867
0868 if (!lport->point_to_multipoint) {
0869 rjt_data.reason = ELS_RJT_UNSUP;
0870 rjt_data.explan = ELS_EXPL_NONE;
0871 goto reject;
0872 }
0873
0874 flp = fc_frame_payload_get(fp, sizeof(*flp));
0875 if (!flp) {
0876 rjt_data.reason = ELS_RJT_LOGIC;
0877 rjt_data.explan = ELS_EXPL_INV_LEN;
0878 goto reject;
0879 }
0880
0881 rdata = fc_rport_lookup(lport, sid);
0882 if (!rdata) {
0883 rjt_data.reason = ELS_RJT_FIP;
0884 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
0885 goto reject;
0886 }
0887 mutex_lock(&rdata->rp_mutex);
0888
0889 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
0890 fc_rport_state(rdata));
0891
0892 switch (rdata->rp_state) {
0893 case RPORT_ST_INIT:
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906 break;
0907 case RPORT_ST_DELETE:
0908 mutex_unlock(&rdata->rp_mutex);
0909 rjt_data.reason = ELS_RJT_FIP;
0910 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
0911 goto reject_put;
0912 case RPORT_ST_FLOGI:
0913 case RPORT_ST_PLOGI_WAIT:
0914 case RPORT_ST_PLOGI:
0915 break;
0916 case RPORT_ST_PRLI:
0917 case RPORT_ST_RTV:
0918 case RPORT_ST_READY:
0919 case RPORT_ST_ADISC:
0920
0921
0922
0923
0924 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
0925 mutex_unlock(&rdata->rp_mutex);
0926 rjt_data.reason = ELS_RJT_BUSY;
0927 rjt_data.explan = ELS_EXPL_NONE;
0928 goto reject_put;
0929 }
0930 if (fc_rport_login_complete(rdata, fp)) {
0931 mutex_unlock(&rdata->rp_mutex);
0932 rjt_data.reason = ELS_RJT_LOGIC;
0933 rjt_data.explan = ELS_EXPL_NONE;
0934 goto reject_put;
0935 }
0936
0937 fp = fc_frame_alloc(lport, sizeof(*flp));
0938 if (!fp)
0939 goto out;
0940
0941 fc_flogi_fill(lport, fp);
0942 flp = fc_frame_payload_get(fp, sizeof(*flp));
0943 flp->fl_cmd = ELS_LS_ACC;
0944
0945 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
0946 lport->tt.frame_send(lport, fp);
0947
0948
0949
0950
0951
0952
0953 if (rdata->rp_state != RPORT_ST_FLOGI) {
0954 if (rdata->ids.port_name < lport->wwpn)
0955 fc_rport_enter_plogi(rdata);
0956 else
0957 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
0958 }
0959 out:
0960 mutex_unlock(&rdata->rp_mutex);
0961 kref_put(&rdata->kref, fc_rport_destroy);
0962 fc_frame_free(rx_fp);
0963 return;
0964
0965 reject_put:
0966 kref_put(&rdata->kref, fc_rport_destroy);
0967 reject:
0968 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
0969 fc_frame_free(rx_fp);
0970 }
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
0983 void *rdata_arg)
0984 {
0985 struct fc_rport_priv *rdata = rdata_arg;
0986 struct fc_lport *lport = rdata->local_port;
0987 struct fc_els_flogi *plp = NULL;
0988 u16 csp_seq;
0989 u16 cssp_seq;
0990 u8 op;
0991
0992 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
0993
0994 if (fp == ERR_PTR(-FC_EX_CLOSED))
0995 goto put;
0996
0997 mutex_lock(&rdata->rp_mutex);
0998
0999 if (rdata->rp_state != RPORT_ST_PLOGI) {
1000 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
1001 "%s\n", fc_rport_state(rdata));
1002 if (IS_ERR(fp))
1003 goto err;
1004 goto out;
1005 }
1006
1007 if (IS_ERR(fp)) {
1008 fc_rport_error_retry(rdata, PTR_ERR(fp));
1009 goto err;
1010 }
1011
1012 op = fc_frame_payload_op(fp);
1013 if (op == ELS_LS_ACC &&
1014 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
1015 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
1016 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
1017
1018
1019 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
1020
1021 if (lport->point_to_multipoint)
1022 fc_rport_login_complete(rdata, fp);
1023 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
1024 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
1025 if (cssp_seq < csp_seq)
1026 csp_seq = cssp_seq;
1027 rdata->max_seq = csp_seq;
1028 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
1029 fc_rport_enter_prli(rdata);
1030 } else {
1031 struct fc_els_ls_rjt *rjt;
1032
1033 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1034 if (!rjt)
1035 FC_RPORT_DBG(rdata, "PLOGI bad response\n");
1036 else
1037 FC_RPORT_DBG(rdata, "PLOGI ELS rejected, reason %x expl %x\n",
1038 rjt->er_reason, rjt->er_explan);
1039 fc_rport_error_retry(rdata, -FC_EX_ELS_RJT);
1040 }
1041 out:
1042 fc_frame_free(fp);
1043 err:
1044 mutex_unlock(&rdata->rp_mutex);
1045 put:
1046 kref_put(&rdata->kref, fc_rport_destroy);
1047 }
1048
1049 static bool
1050 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
1051 {
1052 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
1053 return true;
1054 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
1055 (lport->service_params & FCP_SPPF_INIT_FCN))
1056 return true;
1057 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
1058 (lport->service_params & FCP_SPPF_TARG_FCN))
1059 return true;
1060 return false;
1061 }
1062
1063
1064
1065
1066
1067
1068
1069 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
1070 {
1071 struct fc_lport *lport = rdata->local_port;
1072 struct fc_frame *fp;
1073
1074 lockdep_assert_held(&rdata->rp_mutex);
1075
1076 if (!fc_rport_compatible_roles(lport, rdata)) {
1077 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
1078 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
1079 return;
1080 }
1081
1082 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
1083 fc_rport_state(rdata));
1084
1085 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
1086
1087 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
1088 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1089 if (!fp) {
1090 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
1091 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1092 return;
1093 }
1094 rdata->e_d_tov = lport->e_d_tov;
1095
1096 kref_get(&rdata->kref);
1097 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
1098 fc_rport_plogi_resp, rdata,
1099 2 * lport->r_a_tov)) {
1100 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1101 kref_put(&rdata->kref, fc_rport_destroy);
1102 }
1103 }
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
1116 void *rdata_arg)
1117 {
1118 struct fc_rport_priv *rdata = rdata_arg;
1119 struct {
1120 struct fc_els_prli prli;
1121 struct fc_els_spp spp;
1122 } *pp;
1123 struct fc_els_spp temp_spp;
1124 struct fc_els_ls_rjt *rjt;
1125 struct fc4_prov *prov;
1126 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1127 u32 fcp_parm = 0;
1128 u8 op;
1129 enum fc_els_spp_resp resp_code;
1130
1131 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1132
1133 if (fp == ERR_PTR(-FC_EX_CLOSED))
1134 goto put;
1135
1136 mutex_lock(&rdata->rp_mutex);
1137
1138 if (rdata->rp_state != RPORT_ST_PRLI) {
1139 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1140 "%s\n", fc_rport_state(rdata));
1141 if (IS_ERR(fp))
1142 goto err;
1143 goto out;
1144 }
1145
1146 if (IS_ERR(fp)) {
1147 fc_rport_error_retry(rdata, PTR_ERR(fp));
1148 goto err;
1149 }
1150
1151
1152 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1153
1154 op = fc_frame_payload_op(fp);
1155 if (op == ELS_LS_ACC) {
1156 pp = fc_frame_payload_get(fp, sizeof(*pp));
1157 if (!pp) {
1158 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1159 goto out;
1160 }
1161
1162 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1163 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x spp_type 0x%x\n",
1164 pp->spp.spp_flags, pp->spp.spp_type);
1165
1166 rdata->spp_type = pp->spp.spp_type;
1167 if (resp_code != FC_SPP_RESP_ACK) {
1168 if (resp_code == FC_SPP_RESP_CONF)
1169 fc_rport_error(rdata, -FC_EX_SEQ_ERR);
1170 else
1171 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1172 goto out;
1173 }
1174 if (pp->prli.prli_spp_len < sizeof(pp->spp)) {
1175 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1176 goto out;
1177 }
1178
1179 fcp_parm = ntohl(pp->spp.spp_params);
1180 if (fcp_parm & FCP_SPPF_RETRY)
1181 rdata->flags |= FC_RP_FLAGS_RETRY;
1182 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1183 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1184
1185
1186
1187
1188 if (rdata->spp_type < FC_FC4_PROV_SIZE) {
1189 prov = fc_passive_prov[rdata->spp_type];
1190 if (prov) {
1191 memset(&temp_spp, 0, sizeof(temp_spp));
1192 prov->prli(rdata, pp->prli.prli_spp_len,
1193 &pp->spp, &temp_spp);
1194 }
1195 }
1196
1197
1198
1199 if (rdata->spp_type != FC_TYPE_FCP ||
1200 !(pp->spp.spp_flags & FC_SPP_EST_IMG_PAIR)) {
1201
1202
1203
1204 fcp_parm &= ~FCP_SPPF_TARG_FCN;
1205 }
1206 rdata->supported_classes = FC_COS_CLASS3;
1207 if (fcp_parm & FCP_SPPF_INIT_FCN)
1208 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1209 if (fcp_parm & FCP_SPPF_TARG_FCN)
1210 roles |= FC_RPORT_ROLE_FCP_TARGET;
1211
1212 rdata->ids.roles = roles;
1213 fc_rport_enter_rtv(rdata);
1214
1215 } else {
1216 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1217 if (!rjt)
1218 FC_RPORT_DBG(rdata, "PRLI bad response\n");
1219 else {
1220 FC_RPORT_DBG(rdata, "PRLI ELS rejected, reason %x expl %x\n",
1221 rjt->er_reason, rjt->er_explan);
1222 if (rjt->er_reason == ELS_RJT_UNAB &&
1223 rjt->er_explan == ELS_EXPL_PLOGI_REQD) {
1224 fc_rport_enter_plogi(rdata);
1225 goto out;
1226 }
1227 }
1228 fc_rport_error_retry(rdata, FC_EX_ELS_RJT);
1229 }
1230
1231 out:
1232 fc_frame_free(fp);
1233 err:
1234 mutex_unlock(&rdata->rp_mutex);
1235 put:
1236 kref_put(&rdata->kref, fc_rport_destroy);
1237 }
1238
1239
1240
1241
1242
1243
1244
1245 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1246 {
1247 struct fc_lport *lport = rdata->local_port;
1248 struct {
1249 struct fc_els_prli prli;
1250 struct fc_els_spp spp;
1251 } *pp;
1252 struct fc_frame *fp;
1253 struct fc4_prov *prov;
1254
1255 lockdep_assert_held(&rdata->rp_mutex);
1256
1257
1258
1259
1260
1261 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1262 fc_rport_enter_ready(rdata);
1263 return;
1264 }
1265
1266
1267
1268
1269
1270 if (!(lport->service_params & FCP_SPPF_INIT_FCN)) {
1271 fc_rport_enter_ready(rdata);
1272 return;
1273 }
1274
1275 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1276 fc_rport_state(rdata));
1277
1278 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1279
1280 fp = fc_frame_alloc(lport, sizeof(*pp));
1281 if (!fp) {
1282 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1283 return;
1284 }
1285
1286 fc_prli_fill(lport, fp);
1287
1288 prov = fc_passive_prov[FC_TYPE_FCP];
1289 if (prov) {
1290 pp = fc_frame_payload_get(fp, sizeof(*pp));
1291 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1292 }
1293
1294 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1295 fc_host_port_id(lport->host), FC_TYPE_ELS,
1296 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1297
1298 kref_get(&rdata->kref);
1299 if (!fc_exch_seq_send(lport, fp, fc_rport_prli_resp,
1300 NULL, rdata, 2 * lport->r_a_tov)) {
1301 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1302 kref_put(&rdata->kref, fc_rport_destroy);
1303 }
1304 }
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1319 void *rdata_arg)
1320 {
1321 struct fc_rport_priv *rdata = rdata_arg;
1322 u8 op;
1323
1324 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1325
1326 if (fp == ERR_PTR(-FC_EX_CLOSED))
1327 goto put;
1328
1329 mutex_lock(&rdata->rp_mutex);
1330
1331 if (rdata->rp_state != RPORT_ST_RTV) {
1332 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1333 "%s\n", fc_rport_state(rdata));
1334 if (IS_ERR(fp))
1335 goto err;
1336 goto out;
1337 }
1338
1339 if (IS_ERR(fp)) {
1340 fc_rport_error(rdata, PTR_ERR(fp));
1341 goto err;
1342 }
1343
1344 op = fc_frame_payload_op(fp);
1345 if (op == ELS_LS_ACC) {
1346 struct fc_els_rtv_acc *rtv;
1347 u32 toq;
1348 u32 tov;
1349
1350 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1351 if (rtv) {
1352 toq = ntohl(rtv->rtv_toq);
1353 tov = ntohl(rtv->rtv_r_a_tov);
1354 if (tov == 0)
1355 tov = 1;
1356 if (tov > rdata->r_a_tov)
1357 rdata->r_a_tov = tov;
1358 tov = ntohl(rtv->rtv_e_d_tov);
1359 if (toq & FC_ELS_RTV_EDRES)
1360 tov /= 1000000;
1361 if (tov == 0)
1362 tov = 1;
1363 if (tov > rdata->e_d_tov)
1364 rdata->e_d_tov = tov;
1365 }
1366 }
1367
1368 fc_rport_enter_ready(rdata);
1369
1370 out:
1371 fc_frame_free(fp);
1372 err:
1373 mutex_unlock(&rdata->rp_mutex);
1374 put:
1375 kref_put(&rdata->kref, fc_rport_destroy);
1376 }
1377
1378
1379
1380
1381
1382
1383
1384 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1385 {
1386 struct fc_frame *fp;
1387 struct fc_lport *lport = rdata->local_port;
1388
1389 lockdep_assert_held(&rdata->rp_mutex);
1390
1391 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1392 fc_rport_state(rdata));
1393
1394 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1395
1396 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1397 if (!fp) {
1398 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1399 return;
1400 }
1401
1402 kref_get(&rdata->kref);
1403 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1404 fc_rport_rtv_resp, rdata,
1405 2 * lport->r_a_tov)) {
1406 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1407 kref_put(&rdata->kref, fc_rport_destroy);
1408 }
1409 }
1410
1411
1412
1413
1414
1415
1416 static void fc_rport_recv_rtv_req(struct fc_rport_priv *rdata,
1417 struct fc_frame *in_fp)
1418 {
1419 struct fc_lport *lport = rdata->local_port;
1420 struct fc_frame *fp;
1421 struct fc_els_rtv_acc *rtv;
1422 struct fc_seq_els_data rjt_data;
1423
1424 lockdep_assert_held(&rdata->rp_mutex);
1425 lockdep_assert_held(&lport->lp_mutex);
1426
1427 FC_RPORT_DBG(rdata, "Received RTV request\n");
1428
1429 fp = fc_frame_alloc(lport, sizeof(*rtv));
1430 if (!fp) {
1431 rjt_data.reason = ELS_RJT_UNAB;
1432 rjt_data.explan = ELS_EXPL_INSUF_RES;
1433 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1434 goto drop;
1435 }
1436 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1437 rtv->rtv_cmd = ELS_LS_ACC;
1438 rtv->rtv_r_a_tov = htonl(lport->r_a_tov);
1439 rtv->rtv_e_d_tov = htonl(lport->e_d_tov);
1440 rtv->rtv_toq = 0;
1441 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1442 lport->tt.frame_send(lport, fp);
1443 drop:
1444 fc_frame_free(in_fp);
1445 }
1446
1447
1448
1449
1450
1451
1452
1453 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1454 void *rdata_arg)
1455 {
1456 struct fc_rport_priv *rdata = rdata_arg;
1457 struct fc_lport *lport = rdata->local_port;
1458
1459 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1460 "Received a LOGO %s\n", fc_els_resp_type(fp));
1461 if (!IS_ERR(fp))
1462 fc_frame_free(fp);
1463 kref_put(&rdata->kref, fc_rport_destroy);
1464 }
1465
1466
1467
1468
1469
1470
1471
1472 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1473 {
1474 struct fc_lport *lport = rdata->local_port;
1475 struct fc_frame *fp;
1476
1477 lockdep_assert_held(&rdata->rp_mutex);
1478
1479 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1480 fc_rport_state(rdata));
1481
1482 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1483 if (!fp)
1484 return;
1485 kref_get(&rdata->kref);
1486 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1487 fc_rport_logo_resp, rdata, 0))
1488 kref_put(&rdata->kref, fc_rport_destroy);
1489 }
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1502 void *rdata_arg)
1503 {
1504 struct fc_rport_priv *rdata = rdata_arg;
1505 struct fc_els_adisc *adisc;
1506 u8 op;
1507
1508 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1509
1510 if (fp == ERR_PTR(-FC_EX_CLOSED))
1511 goto put;
1512
1513 mutex_lock(&rdata->rp_mutex);
1514
1515 if (rdata->rp_state != RPORT_ST_ADISC) {
1516 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1517 fc_rport_state(rdata));
1518 if (IS_ERR(fp))
1519 goto err;
1520 goto out;
1521 }
1522
1523 if (IS_ERR(fp)) {
1524 fc_rport_error(rdata, PTR_ERR(fp));
1525 goto err;
1526 }
1527
1528
1529
1530
1531
1532
1533 op = fc_frame_payload_op(fp);
1534 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1535 if (op != ELS_LS_ACC || !adisc ||
1536 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1537 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1538 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1539 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1540 fc_rport_enter_flogi(rdata);
1541 } else {
1542 FC_RPORT_DBG(rdata, "ADISC OK\n");
1543 fc_rport_enter_ready(rdata);
1544 }
1545 out:
1546 fc_frame_free(fp);
1547 err:
1548 mutex_unlock(&rdata->rp_mutex);
1549 put:
1550 kref_put(&rdata->kref, fc_rport_destroy);
1551 }
1552
1553
1554
1555
1556
1557
1558
1559 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1560 {
1561 struct fc_lport *lport = rdata->local_port;
1562 struct fc_frame *fp;
1563
1564 lockdep_assert_held(&rdata->rp_mutex);
1565
1566 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1567 fc_rport_state(rdata));
1568
1569 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1570
1571 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1572 if (!fp) {
1573 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1574 return;
1575 }
1576 kref_get(&rdata->kref);
1577 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1578 fc_rport_adisc_resp, rdata,
1579 2 * lport->r_a_tov)) {
1580 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1581 kref_put(&rdata->kref, fc_rport_destroy);
1582 }
1583 }
1584
1585
1586
1587
1588
1589
1590 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1591 struct fc_frame *in_fp)
1592 {
1593 struct fc_lport *lport = rdata->local_port;
1594 struct fc_frame *fp;
1595 struct fc_els_adisc *adisc;
1596 struct fc_seq_els_data rjt_data;
1597
1598 lockdep_assert_held(&rdata->rp_mutex);
1599 lockdep_assert_held(&lport->lp_mutex);
1600
1601 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1602
1603 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1604 if (!adisc) {
1605 rjt_data.reason = ELS_RJT_PROT;
1606 rjt_data.explan = ELS_EXPL_INV_LEN;
1607 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1608 goto drop;
1609 }
1610
1611 fp = fc_frame_alloc(lport, sizeof(*adisc));
1612 if (!fp)
1613 goto drop;
1614 fc_adisc_fill(lport, fp);
1615 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1616 adisc->adisc_cmd = ELS_LS_ACC;
1617 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1618 lport->tt.frame_send(lport, fp);
1619 drop:
1620 fc_frame_free(in_fp);
1621 }
1622
1623
1624
1625
1626
1627
1628 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1629 struct fc_frame *rx_fp)
1630
1631 {
1632 struct fc_lport *lport = rdata->local_port;
1633 struct fc_frame *fp;
1634 struct fc_els_rls *rls;
1635 struct fc_els_rls_resp *rsp;
1636 struct fc_els_lesb *lesb;
1637 struct fc_seq_els_data rjt_data;
1638 struct fc_host_statistics *hst;
1639
1640 lockdep_assert_held(&rdata->rp_mutex);
1641
1642 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1643 fc_rport_state(rdata));
1644
1645 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1646 if (!rls) {
1647 rjt_data.reason = ELS_RJT_PROT;
1648 rjt_data.explan = ELS_EXPL_INV_LEN;
1649 goto out_rjt;
1650 }
1651
1652 fp = fc_frame_alloc(lport, sizeof(*rsp));
1653 if (!fp) {
1654 rjt_data.reason = ELS_RJT_UNAB;
1655 rjt_data.explan = ELS_EXPL_INSUF_RES;
1656 goto out_rjt;
1657 }
1658
1659 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1660 memset(rsp, 0, sizeof(*rsp));
1661 rsp->rls_cmd = ELS_LS_ACC;
1662 lesb = &rsp->rls_lesb;
1663 if (lport->tt.get_lesb) {
1664
1665 lport->tt.get_lesb(lport, lesb);
1666 } else {
1667 fc_get_host_stats(lport->host);
1668 hst = &lport->host_stats;
1669 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1670 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1671 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1672 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1673 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1674 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1675 }
1676
1677 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1678 lport->tt.frame_send(lport, fp);
1679 goto out;
1680
1681 out_rjt:
1682 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1683 out:
1684 fc_frame_free(rx_fp);
1685 }
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1698 {
1699 struct fc_rport_priv *rdata;
1700 struct fc_seq_els_data els_data;
1701
1702 lockdep_assert_held(&lport->lp_mutex);
1703
1704 rdata = fc_rport_lookup(lport, fc_frame_sid(fp));
1705 if (!rdata) {
1706 FC_RPORT_ID_DBG(lport, fc_frame_sid(fp),
1707 "Received ELS 0x%02x from non-logged-in port\n",
1708 fc_frame_payload_op(fp));
1709 goto reject;
1710 }
1711
1712 mutex_lock(&rdata->rp_mutex);
1713
1714 switch (rdata->rp_state) {
1715 case RPORT_ST_PRLI:
1716 case RPORT_ST_RTV:
1717 case RPORT_ST_READY:
1718 case RPORT_ST_ADISC:
1719 break;
1720 case RPORT_ST_PLOGI:
1721 if (fc_frame_payload_op(fp) == ELS_PRLI) {
1722 FC_RPORT_DBG(rdata, "Reject ELS PRLI "
1723 "while in state %s\n",
1724 fc_rport_state(rdata));
1725 mutex_unlock(&rdata->rp_mutex);
1726 kref_put(&rdata->kref, fc_rport_destroy);
1727 goto busy;
1728 }
1729 fallthrough;
1730 default:
1731 FC_RPORT_DBG(rdata,
1732 "Reject ELS 0x%02x while in state %s\n",
1733 fc_frame_payload_op(fp), fc_rport_state(rdata));
1734 mutex_unlock(&rdata->rp_mutex);
1735 kref_put(&rdata->kref, fc_rport_destroy);
1736 goto reject;
1737 }
1738
1739 switch (fc_frame_payload_op(fp)) {
1740 case ELS_PRLI:
1741 fc_rport_recv_prli_req(rdata, fp);
1742 break;
1743 case ELS_PRLO:
1744 fc_rport_recv_prlo_req(rdata, fp);
1745 break;
1746 case ELS_ADISC:
1747 fc_rport_recv_adisc_req(rdata, fp);
1748 break;
1749 case ELS_RRQ:
1750 fc_seq_els_rsp_send(fp, ELS_RRQ, NULL);
1751 fc_frame_free(fp);
1752 break;
1753 case ELS_REC:
1754 fc_seq_els_rsp_send(fp, ELS_REC, NULL);
1755 fc_frame_free(fp);
1756 break;
1757 case ELS_RLS:
1758 fc_rport_recv_rls_req(rdata, fp);
1759 break;
1760 case ELS_RTV:
1761 fc_rport_recv_rtv_req(rdata, fp);
1762 break;
1763 default:
1764 fc_frame_free(fp);
1765 break;
1766 }
1767
1768 mutex_unlock(&rdata->rp_mutex);
1769 kref_put(&rdata->kref, fc_rport_destroy);
1770 return;
1771
1772 reject:
1773 els_data.reason = ELS_RJT_UNAB;
1774 els_data.explan = ELS_EXPL_PLOGI_REQD;
1775 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1776 fc_frame_free(fp);
1777 return;
1778
1779 busy:
1780 els_data.reason = ELS_RJT_BUSY;
1781 els_data.explan = ELS_EXPL_NONE;
1782 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1783 fc_frame_free(fp);
1784 return;
1785 }
1786
1787
1788
1789
1790
1791
1792
1793
1794 void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1795 {
1796 struct fc_seq_els_data els_data;
1797
1798 lockdep_assert_held(&lport->lp_mutex);
1799
1800
1801
1802
1803
1804
1805
1806 switch (fc_frame_payload_op(fp)) {
1807 case ELS_FLOGI:
1808 fc_rport_recv_flogi_req(lport, fp);
1809 break;
1810 case ELS_PLOGI:
1811 fc_rport_recv_plogi_req(lport, fp);
1812 break;
1813 case ELS_LOGO:
1814 fc_rport_recv_logo_req(lport, fp);
1815 break;
1816 case ELS_PRLI:
1817 case ELS_PRLO:
1818 case ELS_ADISC:
1819 case ELS_RRQ:
1820 case ELS_REC:
1821 case ELS_RLS:
1822 case ELS_RTV:
1823 fc_rport_recv_els_req(lport, fp);
1824 break;
1825 default:
1826 els_data.reason = ELS_RJT_UNSUP;
1827 els_data.explan = ELS_EXPL_NONE;
1828 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1829 fc_frame_free(fp);
1830 break;
1831 }
1832 }
1833 EXPORT_SYMBOL(fc_rport_recv_req);
1834
1835
1836
1837
1838
1839
1840
1841
1842 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1843 struct fc_frame *rx_fp)
1844 {
1845 struct fc_disc *disc;
1846 struct fc_rport_priv *rdata;
1847 struct fc_frame *fp = rx_fp;
1848 struct fc_els_flogi *pl;
1849 struct fc_seq_els_data rjt_data;
1850 u32 sid;
1851
1852 lockdep_assert_held(&lport->lp_mutex);
1853
1854 sid = fc_frame_sid(fp);
1855
1856 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1857
1858 pl = fc_frame_payload_get(fp, sizeof(*pl));
1859 if (!pl) {
1860 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1861 rjt_data.reason = ELS_RJT_PROT;
1862 rjt_data.explan = ELS_EXPL_INV_LEN;
1863 goto reject;
1864 }
1865
1866 disc = &lport->disc;
1867 mutex_lock(&disc->disc_mutex);
1868 rdata = fc_rport_create(lport, sid);
1869 if (!rdata) {
1870 mutex_unlock(&disc->disc_mutex);
1871 rjt_data.reason = ELS_RJT_UNAB;
1872 rjt_data.explan = ELS_EXPL_INSUF_RES;
1873 goto reject;
1874 }
1875
1876 mutex_lock(&rdata->rp_mutex);
1877 mutex_unlock(&disc->disc_mutex);
1878
1879 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1880 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893 switch (rdata->rp_state) {
1894 case RPORT_ST_INIT:
1895 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1896 break;
1897 case RPORT_ST_PLOGI_WAIT:
1898 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1899 break;
1900 case RPORT_ST_PLOGI:
1901 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1902 if (rdata->ids.port_name < lport->wwpn) {
1903 mutex_unlock(&rdata->rp_mutex);
1904 rjt_data.reason = ELS_RJT_INPROG;
1905 rjt_data.explan = ELS_EXPL_NONE;
1906 goto reject;
1907 }
1908 break;
1909 case RPORT_ST_PRLI:
1910 case RPORT_ST_RTV:
1911 case RPORT_ST_READY:
1912 case RPORT_ST_ADISC:
1913 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1914 "- ignored for now\n", rdata->rp_state);
1915
1916 break;
1917 case RPORT_ST_FLOGI:
1918 case RPORT_ST_DELETE:
1919 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1920 fc_rport_state(rdata));
1921 mutex_unlock(&rdata->rp_mutex);
1922 rjt_data.reason = ELS_RJT_BUSY;
1923 rjt_data.explan = ELS_EXPL_NONE;
1924 goto reject;
1925 }
1926 if (!fc_rport_compatible_roles(lport, rdata)) {
1927 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1928 mutex_unlock(&rdata->rp_mutex);
1929 rjt_data.reason = ELS_RJT_LOGIC;
1930 rjt_data.explan = ELS_EXPL_NONE;
1931 goto reject;
1932 }
1933
1934
1935
1936
1937 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1938
1939
1940
1941
1942 fp = fc_frame_alloc(lport, sizeof(*pl));
1943 if (!fp)
1944 goto out;
1945
1946 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1947 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1948 lport->tt.frame_send(lport, fp);
1949 fc_rport_enter_prli(rdata);
1950 out:
1951 mutex_unlock(&rdata->rp_mutex);
1952 fc_frame_free(rx_fp);
1953 return;
1954
1955 reject:
1956 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1957 fc_frame_free(fp);
1958 }
1959
1960
1961
1962
1963
1964
1965 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1966 struct fc_frame *rx_fp)
1967 {
1968 struct fc_lport *lport = rdata->local_port;
1969 struct fc_frame *fp;
1970 struct {
1971 struct fc_els_prli prli;
1972 struct fc_els_spp spp;
1973 } *pp;
1974 struct fc_els_spp *rspp;
1975 struct fc_els_spp *spp;
1976 unsigned int len;
1977 unsigned int plen;
1978 enum fc_els_spp_resp resp;
1979 struct fc_seq_els_data rjt_data;
1980 struct fc4_prov *prov;
1981
1982 lockdep_assert_held(&rdata->rp_mutex);
1983
1984 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1985 fc_rport_state(rdata));
1986
1987 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1988 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1989 if (!pp)
1990 goto reject_len;
1991 plen = ntohs(pp->prli.prli_len);
1992 if ((plen % 4) != 0 || plen > len || plen < 16)
1993 goto reject_len;
1994 if (plen < len)
1995 len = plen;
1996 plen = pp->prli.prli_spp_len;
1997 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1998 plen > len || len < sizeof(*pp) || plen < 12)
1999 goto reject_len;
2000 rspp = &pp->spp;
2001
2002 fp = fc_frame_alloc(lport, len);
2003 if (!fp) {
2004 rjt_data.reason = ELS_RJT_UNAB;
2005 rjt_data.explan = ELS_EXPL_INSUF_RES;
2006 goto reject;
2007 }
2008 pp = fc_frame_payload_get(fp, len);
2009 WARN_ON(!pp);
2010 memset(pp, 0, len);
2011 pp->prli.prli_cmd = ELS_LS_ACC;
2012 pp->prli.prli_spp_len = plen;
2013 pp->prli.prli_len = htons(len);
2014 len -= sizeof(struct fc_els_prli);
2015
2016
2017
2018
2019
2020
2021 spp = &pp->spp;
2022 mutex_lock(&fc_prov_mutex);
2023 while (len >= plen) {
2024 rdata->spp_type = rspp->spp_type;
2025 spp->spp_type = rspp->spp_type;
2026 spp->spp_type_ext = rspp->spp_type_ext;
2027 resp = 0;
2028
2029 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
2030 enum fc_els_spp_resp active = 0, passive = 0;
2031
2032 prov = fc_active_prov[rspp->spp_type];
2033 if (prov)
2034 active = prov->prli(rdata, plen, rspp, spp);
2035 prov = fc_passive_prov[rspp->spp_type];
2036 if (prov)
2037 passive = prov->prli(rdata, plen, rspp, spp);
2038 if (!active || passive == FC_SPP_RESP_ACK)
2039 resp = passive;
2040 else
2041 resp = active;
2042 FC_RPORT_DBG(rdata, "PRLI rspp type %x "
2043 "active %x passive %x\n",
2044 rspp->spp_type, active, passive);
2045 }
2046 if (!resp) {
2047 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
2048 resp |= FC_SPP_RESP_CONF;
2049 else
2050 resp |= FC_SPP_RESP_INVL;
2051 }
2052 spp->spp_flags |= resp;
2053 len -= plen;
2054 rspp = (struct fc_els_spp *)((char *)rspp + plen);
2055 spp = (struct fc_els_spp *)((char *)spp + plen);
2056 }
2057 mutex_unlock(&fc_prov_mutex);
2058
2059
2060
2061
2062 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2063 lport->tt.frame_send(lport, fp);
2064
2065 goto drop;
2066
2067 reject_len:
2068 rjt_data.reason = ELS_RJT_PROT;
2069 rjt_data.explan = ELS_EXPL_INV_LEN;
2070 reject:
2071 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2072 drop:
2073 fc_frame_free(rx_fp);
2074 }
2075
2076
2077
2078
2079
2080
2081 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
2082 struct fc_frame *rx_fp)
2083 {
2084 struct fc_lport *lport = rdata->local_port;
2085 struct fc_frame *fp;
2086 struct {
2087 struct fc_els_prlo prlo;
2088 struct fc_els_spp spp;
2089 } *pp;
2090 struct fc_els_spp *rspp;
2091 struct fc_els_spp *spp;
2092 unsigned int len;
2093 unsigned int plen;
2094 struct fc_seq_els_data rjt_data;
2095
2096 lockdep_assert_held(&rdata->rp_mutex);
2097
2098 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
2099 fc_rport_state(rdata));
2100
2101 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
2102 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
2103 if (!pp)
2104 goto reject_len;
2105 plen = ntohs(pp->prlo.prlo_len);
2106 if (plen != 20)
2107 goto reject_len;
2108 if (plen < len)
2109 len = plen;
2110
2111 rspp = &pp->spp;
2112
2113 fp = fc_frame_alloc(lport, len);
2114 if (!fp) {
2115 rjt_data.reason = ELS_RJT_UNAB;
2116 rjt_data.explan = ELS_EXPL_INSUF_RES;
2117 goto reject;
2118 }
2119
2120 pp = fc_frame_payload_get(fp, len);
2121 WARN_ON(!pp);
2122 memset(pp, 0, len);
2123 pp->prlo.prlo_cmd = ELS_LS_ACC;
2124 pp->prlo.prlo_obs = 0x10;
2125 pp->prlo.prlo_len = htons(len);
2126 spp = &pp->spp;
2127 spp->spp_type = rspp->spp_type;
2128 spp->spp_type_ext = rspp->spp_type_ext;
2129 spp->spp_flags = FC_SPP_RESP_ACK;
2130
2131 fc_rport_enter_prli(rdata);
2132
2133 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2134 lport->tt.frame_send(lport, fp);
2135 goto drop;
2136
2137 reject_len:
2138 rjt_data.reason = ELS_RJT_PROT;
2139 rjt_data.explan = ELS_EXPL_INV_LEN;
2140 reject:
2141 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2142 drop:
2143 fc_frame_free(rx_fp);
2144 }
2145
2146
2147
2148
2149
2150
2151
2152
2153 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
2154 {
2155 struct fc_rport_priv *rdata;
2156 u32 sid;
2157
2158 lockdep_assert_held(&lport->lp_mutex);
2159
2160 fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
2161
2162 sid = fc_frame_sid(fp);
2163
2164 rdata = fc_rport_lookup(lport, sid);
2165 if (rdata) {
2166 mutex_lock(&rdata->rp_mutex);
2167 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
2168 fc_rport_state(rdata));
2169
2170 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
2171 mutex_unlock(&rdata->rp_mutex);
2172 kref_put(&rdata->kref, fc_rport_destroy);
2173 } else
2174 FC_RPORT_ID_DBG(lport, sid,
2175 "Received LOGO from non-logged-in port\n");
2176 fc_frame_free(fp);
2177 }
2178
2179
2180
2181
2182 void fc_rport_flush_queue(void)
2183 {
2184 flush_workqueue(rport_event_queue);
2185 }
2186 EXPORT_SYMBOL(fc_rport_flush_queue);
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2199 const struct fc_els_spp *rspp,
2200 struct fc_els_spp *spp)
2201 {
2202 struct fc_lport *lport = rdata->local_port;
2203 u32 fcp_parm;
2204
2205 fcp_parm = ntohl(rspp->spp_params);
2206 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2207 if (fcp_parm & FCP_SPPF_INIT_FCN)
2208 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2209 if (fcp_parm & FCP_SPPF_TARG_FCN)
2210 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2211 if (fcp_parm & FCP_SPPF_RETRY)
2212 rdata->flags |= FC_RP_FLAGS_RETRY;
2213 rdata->supported_classes = FC_COS_CLASS3;
2214
2215 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
2216 return 0;
2217
2218 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2219
2220
2221
2222
2223 fcp_parm = ntohl(spp->spp_params);
2224 spp->spp_params = htonl(fcp_parm | lport->service_params);
2225 return FC_SPP_RESP_ACK;
2226 }
2227
2228
2229
2230
2231 struct fc4_prov fc_rport_fcp_init = {
2232 .prli = fc_rport_fcp_prli,
2233 };
2234
2235
2236
2237
2238
2239
2240
2241
2242 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2243 const struct fc_els_spp *rspp,
2244 struct fc_els_spp *spp)
2245 {
2246 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2247 return FC_SPP_RESP_INVL;
2248 return FC_SPP_RESP_ACK;
2249 }
2250
2251
2252
2253
2254
2255
2256
2257 struct fc4_prov fc_rport_t0_prov = {
2258 .prli = fc_rport_t0_prli,
2259 };
2260
2261
2262
2263
2264 int fc_setup_rport(void)
2265 {
2266 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2267 if (!rport_event_queue)
2268 return -ENOMEM;
2269 return 0;
2270 }
2271
2272
2273
2274
2275 void fc_destroy_rport(void)
2276 {
2277 destroy_workqueue(rport_event_queue);
2278 }
2279
2280
2281
2282
2283
2284 void fc_rport_terminate_io(struct fc_rport *rport)
2285 {
2286 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2287 struct fc_lport *lport = rpriv->local_port;
2288
2289 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2290 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2291 }
2292 EXPORT_SYMBOL(fc_rport_terminate_io);