1 /*
2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20 /*
21 * RPORT GENERAL INFO
22 *
23 * This file contains all processing regarding fc_rports. It contains the
24 * rport state machine and does all rport interaction with the transport class.
25 * There should be no other places in libfc that interact directly with the
26 * transport class in regards to adding and deleting rports.
27 *
28 * fc_rport's represent N_Port's within the fabric.
29 */
30
31 /*
32 * RPORT LOCKING
33 *
34 * The rport should never hold the rport mutex and then attempt to acquire
35 * either the lport or disc mutexes. The rport's mutex is considered lesser
36 * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
37 * more comments on the hierarchy.
38 *
39 * The locking strategy is similar to the lport's strategy. The lock protects
40 * the rport's states and is held and released by the entry points to the rport
41 * block. All _enter_* functions correspond to rport states and expect the rport
42 * mutex to be locked before calling them. This means that rports only handle
43 * one request or response at a time, since they're not critical for the I/O
44 * path this potential over-use of the mutex is acceptable.
45 */
46
47 /*
48 * RPORT REFERENCE COUNTING
49 *
50 * A rport reference should be taken when:
51 * - an rport is allocated
52 * - a workqueue item is scheduled
53 * - an ELS request is send
54 * The reference should be dropped when:
55 * - the workqueue function has finished
56 * - the ELS response is handled
57 * - an rport is removed
58 */
59
60 #include <linux/kernel.h>
61 #include <linux/spinlock.h>
62 #include <linux/interrupt.h>
63 #include <linux/slab.h>
64 #include <linux/rcupdate.h>
65 #include <linux/timer.h>
66 #include <linux/workqueue.h>
67 #include <linux/export.h>
68 #include <linux/rculist.h>
69
70 #include <asm/unaligned.h>
71
72 #include <scsi/libfc.h>
73 #include <scsi/fc_encode.h>
74
75 #include "fc_libfc.h"
76
77 static struct workqueue_struct *rport_event_queue;
78
79 static void fc_rport_enter_flogi(struct fc_rport_priv *);
80 static void fc_rport_enter_plogi(struct fc_rport_priv *);
81 static void fc_rport_enter_prli(struct fc_rport_priv *);
82 static void fc_rport_enter_rtv(struct fc_rport_priv *);
83 static void fc_rport_enter_ready(struct fc_rport_priv *);
84 static void fc_rport_enter_logo(struct fc_rport_priv *);
85 static void fc_rport_enter_adisc(struct fc_rport_priv *);
86
87 static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
88 static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
89 static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
90 static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
91 static void fc_rport_timeout(struct work_struct *);
92 static void fc_rport_error(struct fc_rport_priv *, int);
93 static void fc_rport_error_retry(struct fc_rport_priv *, int);
94 static void fc_rport_work(struct work_struct *);
95
96 static const char *fc_rport_state_names[] = {
97 [RPORT_ST_INIT] = "Init",
98 [RPORT_ST_FLOGI] = "FLOGI",
99 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
100 [RPORT_ST_PLOGI] = "PLOGI",
101 [RPORT_ST_PRLI] = "PRLI",
102 [RPORT_ST_RTV] = "RTV",
103 [RPORT_ST_READY] = "Ready",
104 [RPORT_ST_ADISC] = "ADISC",
105 [RPORT_ST_DELETE] = "Delete",
106 };
107
108 /**
109 * fc_rport_lookup() - Lookup a remote port by port_id
110 * @lport: The local port to lookup the remote port on
111 * @port_id: The remote port ID to look up
112 *
113 * The reference count of the fc_rport_priv structure is
114 * increased by one.
115 */
fc_rport_lookup(const struct fc_lport * lport,u32 port_id)116 struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
117 u32 port_id)
118 {
119 struct fc_rport_priv *rdata = NULL, *tmp_rdata;
120
121 rcu_read_lock();
122 list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers)
123 if (tmp_rdata->ids.port_id == port_id &&
124 kref_get_unless_zero(&tmp_rdata->kref)) {
125 rdata = tmp_rdata;
126 break;
127 }
128 rcu_read_unlock();
129 return rdata;
130 }
131 EXPORT_SYMBOL(fc_rport_lookup);
132
133 /**
134 * fc_rport_create() - Create a new remote port
135 * @lport: The local port this remote port will be associated with
136 * @ids: The identifiers for the new remote port
137 *
138 * The remote port will start in the INIT state.
139 */
fc_rport_create(struct fc_lport * lport,u32 port_id)140 struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id)
141 {
142 struct fc_rport_priv *rdata;
143 size_t rport_priv_size = sizeof(*rdata);
144
145 lockdep_assert_held(&lport->disc.disc_mutex);
146
147 rdata = fc_rport_lookup(lport, port_id);
148 if (rdata) {
149 kref_put(&rdata->kref, fc_rport_destroy);
150 return rdata;
151 }
152
153 if (lport->rport_priv_size > 0)
154 rport_priv_size = lport->rport_priv_size;
155 rdata = kzalloc(rport_priv_size, GFP_KERNEL);
156 if (!rdata)
157 return NULL;
158
159 rdata->ids.node_name = -1;
160 rdata->ids.port_name = -1;
161 rdata->ids.port_id = port_id;
162 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
163
164 kref_init(&rdata->kref);
165 mutex_init(&rdata->rp_mutex);
166 rdata->local_port = lport;
167 rdata->rp_state = RPORT_ST_INIT;
168 rdata->event = RPORT_EV_NONE;
169 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
170 rdata->e_d_tov = lport->e_d_tov;
171 rdata->r_a_tov = lport->r_a_tov;
172 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
173 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
174 INIT_WORK(&rdata->event_work, fc_rport_work);
175 if (port_id != FC_FID_DIR_SERV) {
176 rdata->lld_event_callback = lport->tt.rport_event_callback;
177 list_add_rcu(&rdata->peers, &lport->disc.rports);
178 }
179 return rdata;
180 }
181 EXPORT_SYMBOL(fc_rport_create);
182
183 /**
184 * fc_rport_destroy() - Free a remote port after last reference is released
185 * @kref: The remote port's kref
186 */
fc_rport_destroy(struct kref * kref)187 void fc_rport_destroy(struct kref *kref)
188 {
189 struct fc_rport_priv *rdata;
190
191 rdata = container_of(kref, struct fc_rport_priv, kref);
192 kfree_rcu(rdata, rcu);
193 }
194 EXPORT_SYMBOL(fc_rport_destroy);
195
196 /**
197 * fc_rport_state() - Return a string identifying the remote port's state
198 * @rdata: The remote port
199 */
fc_rport_state(struct fc_rport_priv * rdata)200 static const char *fc_rport_state(struct fc_rport_priv *rdata)
201 {
202 const char *cp;
203
204 cp = fc_rport_state_names[rdata->rp_state];
205 if (!cp)
206 cp = "Unknown";
207 return cp;
208 }
209
210 /**
211 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
212 * @rport: The remote port that gets a new timeout value
213 * @timeout: The new timeout value (in seconds)
214 */
fc_set_rport_loss_tmo(struct fc_rport * rport,u32 timeout)215 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
216 {
217 if (timeout)
218 rport->dev_loss_tmo = timeout;
219 else
220 rport->dev_loss_tmo = 1;
221 }
222 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
223
224 /**
225 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
226 * parameters in a FLOGI frame
227 * @flp: The FLOGI or PLOGI payload
228 * @maxval: The maximum frame size upper limit; this may be less than what
229 * is in the service parameters
230 */
fc_plogi_get_maxframe(struct fc_els_flogi * flp,unsigned int maxval)231 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
232 unsigned int maxval)
233 {
234 unsigned int mfs;
235
236 /*
237 * Get max payload from the common service parameters and the
238 * class 3 receive data field size.
239 */
240 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
241 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
242 maxval = mfs;
243 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
244 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
245 maxval = mfs;
246 return maxval;
247 }
248
249 /**
250 * fc_rport_state_enter() - Change the state of a remote port
251 * @rdata: The remote port whose state should change
252 * @new: The new state
253 */
fc_rport_state_enter(struct fc_rport_priv * rdata,enum fc_rport_state new)254 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
255 enum fc_rport_state new)
256 {
257 lockdep_assert_held(&rdata->rp_mutex);
258
259 if (rdata->rp_state != new)
260 rdata->retries = 0;
261 rdata->rp_state = new;
262 }
263
264 /**
265 * fc_rport_work() - Handler for remote port events in the rport_event_queue
266 * @work: Handle to the remote port being dequeued
267 *
268 * Reference counting: drops kref on return
269 */
fc_rport_work(struct work_struct * work)270 static void fc_rport_work(struct work_struct *work)
271 {
272 u32 port_id;
273 struct fc_rport_priv *rdata =
274 container_of(work, struct fc_rport_priv, event_work);
275 struct fc_rport_libfc_priv *rpriv;
276 enum fc_rport_event event;
277 struct fc_lport *lport = rdata->local_port;
278 struct fc_rport_operations *rport_ops;
279 struct fc_rport_identifiers ids;
280 struct fc_rport *rport;
281 struct fc4_prov *prov;
282 u8 type;
283
284 mutex_lock(&rdata->rp_mutex);
285 event = rdata->event;
286 rport_ops = rdata->ops;
287 rport = rdata->rport;
288
289 FC_RPORT_DBG(rdata, "work event %u\n", event);
290
291 switch (event) {
292 case RPORT_EV_READY:
293 ids = rdata->ids;
294 rdata->event = RPORT_EV_NONE;
295 rdata->major_retries = 0;
296 kref_get(&rdata->kref);
297 mutex_unlock(&rdata->rp_mutex);
298
299 if (!rport) {
300 FC_RPORT_DBG(rdata, "No rport!\n");
301 rport = fc_remote_port_add(lport->host, 0, &ids);
302 }
303 if (!rport) {
304 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
305 fc_rport_logoff(rdata);
306 kref_put(&rdata->kref, fc_rport_destroy);
307 return;
308 }
309 mutex_lock(&rdata->rp_mutex);
310 if (rdata->rport)
311 FC_RPORT_DBG(rdata, "rport already allocated\n");
312 rdata->rport = rport;
313 rport->maxframe_size = rdata->maxframe_size;
314 rport->supported_classes = rdata->supported_classes;
315
316 rpriv = rport->dd_data;
317 rpriv->local_port = lport;
318 rpriv->rp_state = rdata->rp_state;
319 rpriv->flags = rdata->flags;
320 rpriv->e_d_tov = rdata->e_d_tov;
321 rpriv->r_a_tov = rdata->r_a_tov;
322 mutex_unlock(&rdata->rp_mutex);
323
324 if (rport_ops && rport_ops->event_callback) {
325 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
326 rport_ops->event_callback(lport, rdata, event);
327 }
328 if (rdata->lld_event_callback) {
329 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
330 rdata->lld_event_callback(lport, rdata, event);
331 }
332 kref_put(&rdata->kref, fc_rport_destroy);
333 break;
334
335 case RPORT_EV_FAILED:
336 case RPORT_EV_LOGO:
337 case RPORT_EV_STOP:
338 if (rdata->prli_count) {
339 mutex_lock(&fc_prov_mutex);
340 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
341 prov = fc_passive_prov[type];
342 if (prov && prov->prlo)
343 prov->prlo(rdata);
344 }
345 mutex_unlock(&fc_prov_mutex);
346 }
347 port_id = rdata->ids.port_id;
348 mutex_unlock(&rdata->rp_mutex);
349
350 if (rport_ops && rport_ops->event_callback) {
351 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
352 rport_ops->event_callback(lport, rdata, event);
353 }
354 if (rdata->lld_event_callback) {
355 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
356 rdata->lld_event_callback(lport, rdata, event);
357 }
358 if (cancel_delayed_work_sync(&rdata->retry_work))
359 kref_put(&rdata->kref, fc_rport_destroy);
360
361 /*
362 * Reset any outstanding exchanges before freeing rport.
363 */
364 lport->tt.exch_mgr_reset(lport, 0, port_id);
365 lport->tt.exch_mgr_reset(lport, port_id, 0);
366
367 if (rport) {
368 rpriv = rport->dd_data;
369 rpriv->rp_state = RPORT_ST_DELETE;
370 mutex_lock(&rdata->rp_mutex);
371 rdata->rport = NULL;
372 mutex_unlock(&rdata->rp_mutex);
373 fc_remote_port_delete(rport);
374 }
375
376 mutex_lock(&rdata->rp_mutex);
377 if (rdata->rp_state == RPORT_ST_DELETE) {
378 if (port_id == FC_FID_DIR_SERV) {
379 rdata->event = RPORT_EV_NONE;
380 mutex_unlock(&rdata->rp_mutex);
381 kref_put(&rdata->kref, fc_rport_destroy);
382 } else if ((rdata->flags & FC_RP_STARTED) &&
383 rdata->major_retries <
384 lport->max_rport_retry_count) {
385 rdata->major_retries++;
386 rdata->event = RPORT_EV_NONE;
387 FC_RPORT_DBG(rdata, "work restart\n");
388 fc_rport_enter_flogi(rdata);
389 mutex_unlock(&rdata->rp_mutex);
390 } else {
391 mutex_unlock(&rdata->rp_mutex);
392 FC_RPORT_DBG(rdata, "work delete\n");
393 mutex_lock(&lport->disc.disc_mutex);
394 list_del_rcu(&rdata->peers);
395 mutex_unlock(&lport->disc.disc_mutex);
396 kref_put(&rdata->kref, fc_rport_destroy);
397 }
398 } else {
399 /*
400 * Re-open for events. Reissue READY event if ready.
401 */
402 rdata->event = RPORT_EV_NONE;
403 if (rdata->rp_state == RPORT_ST_READY) {
404 FC_RPORT_DBG(rdata, "work reopen\n");
405 fc_rport_enter_ready(rdata);
406 }
407 mutex_unlock(&rdata->rp_mutex);
408 }
409 break;
410
411 default:
412 mutex_unlock(&rdata->rp_mutex);
413 break;
414 }
415 kref_put(&rdata->kref, fc_rport_destroy);
416 }
417
418 /**
419 * fc_rport_login() - Start the remote port login state machine
420 * @rdata: The remote port to be logged in to
421 *
422 * Initiates the RP state machine. It is called from the LP module.
423 * This function will issue the following commands to the N_Port
424 * identified by the FC ID provided.
425 *
426 * - PLOGI
427 * - PRLI
428 * - RTV
429 *
430 * Locking Note: Called without the rport lock held. This
431 * function will hold the rport lock, call an _enter_*
432 * function and then unlock the rport.
433 *
434 * This indicates the intent to be logged into the remote port.
435 * If it appears we are already logged in, ADISC is used to verify
436 * the setup.
437 */
fc_rport_login(struct fc_rport_priv * rdata)438 int fc_rport_login(struct fc_rport_priv *rdata)
439 {
440 mutex_lock(&rdata->rp_mutex);
441
442 if (rdata->flags & FC_RP_STARTED) {
443 FC_RPORT_DBG(rdata, "port already started\n");
444 mutex_unlock(&rdata->rp_mutex);
445 return 0;
446 }
447
448 rdata->flags |= FC_RP_STARTED;
449 switch (rdata->rp_state) {
450 case RPORT_ST_READY:
451 FC_RPORT_DBG(rdata, "ADISC port\n");
452 fc_rport_enter_adisc(rdata);
453 break;
454 case RPORT_ST_DELETE:
455 FC_RPORT_DBG(rdata, "Restart deleted port\n");
456 break;
457 case RPORT_ST_INIT:
458 FC_RPORT_DBG(rdata, "Login to port\n");
459 fc_rport_enter_flogi(rdata);
460 break;
461 default:
462 FC_RPORT_DBG(rdata, "Login in progress, state %s\n",
463 fc_rport_state(rdata));
464 break;
465 }
466 mutex_unlock(&rdata->rp_mutex);
467
468 return 0;
469 }
470 EXPORT_SYMBOL(fc_rport_login);
471
472 /**
473 * fc_rport_enter_delete() - Schedule a remote port to be deleted
474 * @rdata: The remote port to be deleted
475 * @event: The event to report as the reason for deletion
476 *
477 * Allow state change into DELETE only once.
478 *
479 * Call queue_work only if there's no event already pending.
480 * Set the new event so that the old pending event will not occur.
481 * Since we have the mutex, even if fc_rport_work() is already started,
482 * it'll see the new event.
483 *
484 * Reference counting: does not modify kref
485 */
fc_rport_enter_delete(struct fc_rport_priv * rdata,enum fc_rport_event event)486 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
487 enum fc_rport_event event)
488 {
489 lockdep_assert_held(&rdata->rp_mutex);
490
491 if (rdata->rp_state == RPORT_ST_DELETE)
492 return;
493
494 FC_RPORT_DBG(rdata, "Delete port\n");
495
496 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
497
498 if (rdata->event == RPORT_EV_NONE) {
499 kref_get(&rdata->kref);
500 if (!queue_work(rport_event_queue, &rdata->event_work))
501 kref_put(&rdata->kref, fc_rport_destroy);
502 }
503
504 rdata->event = event;
505 }
506
507 /**
508 * fc_rport_logoff() - Logoff and remove a remote port
509 * @rdata: The remote port to be logged off of
510 *
511 * Locking Note: Called without the rport lock held. This
512 * function will hold the rport lock, call an _enter_*
513 * function and then unlock the rport.
514 */
fc_rport_logoff(struct fc_rport_priv * rdata)515 int fc_rport_logoff(struct fc_rport_priv *rdata)
516 {
517 struct fc_lport *lport = rdata->local_port;
518 u32 port_id = rdata->ids.port_id;
519
520 mutex_lock(&rdata->rp_mutex);
521
522 FC_RPORT_DBG(rdata, "Remove port\n");
523
524 rdata->flags &= ~FC_RP_STARTED;
525 if (rdata->rp_state == RPORT_ST_DELETE) {
526 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
527 goto out;
528 }
529 /*
530 * FC-LS states:
531 * To explicitly Logout, the initiating Nx_Port shall terminate
532 * other open Sequences that it initiated with the destination
533 * Nx_Port prior to performing Logout.
534 */
535 lport->tt.exch_mgr_reset(lport, 0, port_id);
536 lport->tt.exch_mgr_reset(lport, port_id, 0);
537
538 fc_rport_enter_logo(rdata);
539
540 /*
541 * Change the state to Delete so that we discard
542 * the response.
543 */
544 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
545 out:
546 mutex_unlock(&rdata->rp_mutex);
547 return 0;
548 }
549 EXPORT_SYMBOL(fc_rport_logoff);
550
551 /**
552 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
553 * @rdata: The remote port that is ready
554 *
555 * Reference counting: schedules workqueue, does not modify kref
556 */
fc_rport_enter_ready(struct fc_rport_priv * rdata)557 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
558 {
559 lockdep_assert_held(&rdata->rp_mutex);
560
561 fc_rport_state_enter(rdata, RPORT_ST_READY);
562
563 FC_RPORT_DBG(rdata, "Port is Ready\n");
564
565 kref_get(&rdata->kref);
566 if (rdata->event == RPORT_EV_NONE &&
567 !queue_work(rport_event_queue, &rdata->event_work))
568 kref_put(&rdata->kref, fc_rport_destroy);
569
570 rdata->event = RPORT_EV_READY;
571 }
572
573 /**
574 * fc_rport_timeout() - Handler for the retry_work timer
575 * @work: Handle to the remote port that has timed out
576 *
577 * Locking Note: Called without the rport lock held. This
578 * function will hold the rport lock, call an _enter_*
579 * function and then unlock the rport.
580 *
581 * Reference counting: Drops kref on return.
582 */
fc_rport_timeout(struct work_struct * work)583 static void fc_rport_timeout(struct work_struct *work)
584 {
585 struct fc_rport_priv *rdata =
586 container_of(work, struct fc_rport_priv, retry_work.work);
587
588 mutex_lock(&rdata->rp_mutex);
589 FC_RPORT_DBG(rdata, "Port timeout, state %s\n", fc_rport_state(rdata));
590
591 switch (rdata->rp_state) {
592 case RPORT_ST_FLOGI:
593 fc_rport_enter_flogi(rdata);
594 break;
595 case RPORT_ST_PLOGI:
596 fc_rport_enter_plogi(rdata);
597 break;
598 case RPORT_ST_PRLI:
599 fc_rport_enter_prli(rdata);
600 break;
601 case RPORT_ST_RTV:
602 fc_rport_enter_rtv(rdata);
603 break;
604 case RPORT_ST_ADISC:
605 fc_rport_enter_adisc(rdata);
606 break;
607 case RPORT_ST_PLOGI_WAIT:
608 case RPORT_ST_READY:
609 case RPORT_ST_INIT:
610 case RPORT_ST_DELETE:
611 break;
612 }
613
614 mutex_unlock(&rdata->rp_mutex);
615 kref_put(&rdata->kref, fc_rport_destroy);
616 }
617
618 /**
619 * fc_rport_error() - Error handler, called once retries have been exhausted
620 * @rdata: The remote port the error is happened on
621 * @err: The error code
622 *
623 * Reference counting: does not modify kref
624 */
fc_rport_error(struct fc_rport_priv * rdata,int err)625 static void fc_rport_error(struct fc_rport_priv *rdata, int err)
626 {
627 struct fc_lport *lport = rdata->local_port;
628
629 lockdep_assert_held(&rdata->rp_mutex);
630
631 FC_RPORT_DBG(rdata, "Error %d in state %s, retries %d\n",
632 -err, fc_rport_state(rdata), rdata->retries);
633
634 switch (rdata->rp_state) {
635 case RPORT_ST_FLOGI:
636 rdata->flags &= ~FC_RP_STARTED;
637 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
638 break;
639 case RPORT_ST_PLOGI:
640 if (lport->point_to_multipoint) {
641 rdata->flags &= ~FC_RP_STARTED;
642 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
643 } else
644 fc_rport_enter_logo(rdata);
645 break;
646 case RPORT_ST_RTV:
647 fc_rport_enter_ready(rdata);
648 break;
649 case RPORT_ST_PRLI:
650 case RPORT_ST_ADISC:
651 fc_rport_enter_logo(rdata);
652 break;
653 case RPORT_ST_PLOGI_WAIT:
654 case RPORT_ST_DELETE:
655 case RPORT_ST_READY:
656 case RPORT_ST_INIT:
657 break;
658 }
659 }
660
661 /**
662 * fc_rport_error_retry() - Handler for remote port state retries
663 * @rdata: The remote port whose state is to be retried
664 * @err: The error code
665 *
666 * If the error was an exchange timeout retry immediately,
667 * otherwise wait for E_D_TOV.
668 *
669 * Reference counting: increments kref when scheduling retry_work
670 */
fc_rport_error_retry(struct fc_rport_priv * rdata,int err)671 static void fc_rport_error_retry(struct fc_rport_priv *rdata, int err)
672 {
673 unsigned long delay = msecs_to_jiffies(rdata->e_d_tov);
674
675 lockdep_assert_held(&rdata->rp_mutex);
676
677 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
678 if (err == -FC_EX_CLOSED)
679 goto out;
680
681 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
682 FC_RPORT_DBG(rdata, "Error %d in state %s, retrying\n",
683 err, fc_rport_state(rdata));
684 rdata->retries++;
685 /* no additional delay on exchange timeouts */
686 if (err == -FC_EX_TIMEOUT)
687 delay = 0;
688 kref_get(&rdata->kref);
689 if (!schedule_delayed_work(&rdata->retry_work, delay))
690 kref_put(&rdata->kref, fc_rport_destroy);
691 return;
692 }
693
694 out:
695 fc_rport_error(rdata, err);
696 }
697
698 /**
699 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
700 * @rdata: The remote port which we logged into or which logged into us.
701 * @fp: The FLOGI or PLOGI request or response frame
702 *
703 * Returns non-zero error if a problem is detected with the frame.
704 * Does not free the frame.
705 *
706 * This is only used in point-to-multipoint mode for FIP currently.
707 */
fc_rport_login_complete(struct fc_rport_priv * rdata,struct fc_frame * fp)708 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
709 struct fc_frame *fp)
710 {
711 struct fc_lport *lport = rdata->local_port;
712 struct fc_els_flogi *flogi;
713 unsigned int e_d_tov;
714 u16 csp_flags;
715
716 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
717 if (!flogi)
718 return -EINVAL;
719
720 csp_flags = ntohs(flogi->fl_csp.sp_features);
721
722 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
723 if (csp_flags & FC_SP_FT_FPORT) {
724 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
725 return -EINVAL;
726 }
727 } else {
728
729 /*
730 * E_D_TOV is not valid on an incoming FLOGI request.
731 */
732 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
733 if (csp_flags & FC_SP_FT_EDTR)
734 e_d_tov /= 1000000;
735 if (e_d_tov > rdata->e_d_tov)
736 rdata->e_d_tov = e_d_tov;
737 }
738 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
739 return 0;
740 }
741
742 /**
743 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
744 * @sp: The sequence that the FLOGI was on
745 * @fp: The FLOGI response frame
746 * @rp_arg: The remote port that received the FLOGI response
747 */
fc_rport_flogi_resp(struct fc_seq * sp,struct fc_frame * fp,void * rp_arg)748 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
749 void *rp_arg)
750 {
751 struct fc_rport_priv *rdata = rp_arg;
752 struct fc_lport *lport = rdata->local_port;
753 struct fc_els_flogi *flogi;
754 unsigned int r_a_tov;
755 u8 opcode;
756 int err = 0;
757
758 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n",
759 IS_ERR(fp) ? "error" : fc_els_resp_type(fp));
760
761 if (fp == ERR_PTR(-FC_EX_CLOSED))
762 goto put;
763
764 mutex_lock(&rdata->rp_mutex);
765
766 if (rdata->rp_state != RPORT_ST_FLOGI) {
767 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
768 "%s\n", fc_rport_state(rdata));
769 if (IS_ERR(fp))
770 goto err;
771 goto out;
772 }
773
774 if (IS_ERR(fp)) {
775 fc_rport_error(rdata, PTR_ERR(fp));
776 goto err;
777 }
778 opcode = fc_frame_payload_op(fp);
779 if (opcode == ELS_LS_RJT) {
780 struct fc_els_ls_rjt *rjt;
781
782 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
783 FC_RPORT_DBG(rdata, "FLOGI ELS rejected, reason %x expl %x\n",
784 rjt->er_reason, rjt->er_explan);
785 err = -FC_EX_ELS_RJT;
786 goto bad;
787 } else if (opcode != ELS_LS_ACC) {
788 FC_RPORT_DBG(rdata, "FLOGI ELS invalid opcode %x\n", opcode);
789 err = -FC_EX_ELS_RJT;
790 goto bad;
791 }
792 if (fc_rport_login_complete(rdata, fp)) {
793 FC_RPORT_DBG(rdata, "FLOGI failed, no login\n");
794 err = -FC_EX_INV_LOGIN;
795 goto bad;
796 }
797
798 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
799 if (!flogi) {
800 err = -FC_EX_ALLOC_ERR;
801 goto bad;
802 }
803 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
804 if (r_a_tov > rdata->r_a_tov)
805 rdata->r_a_tov = r_a_tov;
806
807 if (rdata->ids.port_name < lport->wwpn)
808 fc_rport_enter_plogi(rdata);
809 else
810 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
811 out:
812 fc_frame_free(fp);
813 err:
814 mutex_unlock(&rdata->rp_mutex);
815 put:
816 kref_put(&rdata->kref, fc_rport_destroy);
817 return;
818 bad:
819 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
820 fc_rport_error_retry(rdata, err);
821 goto out;
822 }
823
824 /**
825 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
826 * @rdata: The remote port to send a FLOGI to
827 *
828 * Reference counting: increments kref when sending ELS
829 */
fc_rport_enter_flogi(struct fc_rport_priv * rdata)830 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
831 {
832 struct fc_lport *lport = rdata->local_port;
833 struct fc_frame *fp;
834
835 lockdep_assert_held(&rdata->rp_mutex);
836
837 if (!lport->point_to_multipoint)
838 return fc_rport_enter_plogi(rdata);
839
840 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
841 fc_rport_state(rdata));
842
843 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
844
845 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
846 if (!fp)
847 return fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
848
849 kref_get(&rdata->kref);
850 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
851 fc_rport_flogi_resp, rdata,
852 2 * lport->r_a_tov)) {
853 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
854 kref_put(&rdata->kref, fc_rport_destroy);
855 }
856 }
857
858 /**
859 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
860 * @lport: The local port that received the PLOGI request
861 * @rx_fp: The PLOGI request frame
862 *
863 * Reference counting: drops kref on return
864 */
fc_rport_recv_flogi_req(struct fc_lport * lport,struct fc_frame * rx_fp)865 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
866 struct fc_frame *rx_fp)
867 {
868 struct fc_disc *disc;
869 struct fc_els_flogi *flp;
870 struct fc_rport_priv *rdata;
871 struct fc_frame *fp = rx_fp;
872 struct fc_seq_els_data rjt_data;
873 u32 sid;
874
875 sid = fc_frame_sid(fp);
876
877 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
878
879 disc = &lport->disc;
880 if (!lport->point_to_multipoint) {
881 rjt_data.reason = ELS_RJT_UNSUP;
882 rjt_data.explan = ELS_EXPL_NONE;
883 goto reject;
884 }
885
886 flp = fc_frame_payload_get(fp, sizeof(*flp));
887 if (!flp) {
888 rjt_data.reason = ELS_RJT_LOGIC;
889 rjt_data.explan = ELS_EXPL_INV_LEN;
890 goto reject;
891 }
892
893 rdata = fc_rport_lookup(lport, sid);
894 if (!rdata) {
895 rjt_data.reason = ELS_RJT_FIP;
896 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
897 goto reject;
898 }
899 mutex_lock(&rdata->rp_mutex);
900
901 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
902 fc_rport_state(rdata));
903
904 switch (rdata->rp_state) {
905 case RPORT_ST_INIT:
906 /*
907 * If received the FLOGI request on RPORT which is INIT state
908 * (means not transition to FLOGI either fc_rport timeout
909 * function didn;t trigger or this end hasn;t received
910 * beacon yet from other end. In that case only, allow RPORT
911 * state machine to continue, otherwise fall through which
912 * causes the code to send reject response.
913 * NOTE; Not checking for FIP->state such as VNMP_UP or
914 * VNMP_CLAIM because if FIP state is not one of those,
915 * RPORT wouldn;t have created and 'rport_lookup' would have
916 * failed anyway in that case.
917 */
918 break;
919 case RPORT_ST_DELETE:
920 mutex_unlock(&rdata->rp_mutex);
921 rjt_data.reason = ELS_RJT_FIP;
922 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
923 goto reject_put;
924 case RPORT_ST_FLOGI:
925 case RPORT_ST_PLOGI_WAIT:
926 case RPORT_ST_PLOGI:
927 break;
928 case RPORT_ST_PRLI:
929 case RPORT_ST_RTV:
930 case RPORT_ST_READY:
931 case RPORT_ST_ADISC:
932 /*
933 * Set the remote port to be deleted and to then restart.
934 * This queues work to be sure exchanges are reset.
935 */
936 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
937 mutex_unlock(&rdata->rp_mutex);
938 rjt_data.reason = ELS_RJT_BUSY;
939 rjt_data.explan = ELS_EXPL_NONE;
940 goto reject_put;
941 }
942 if (fc_rport_login_complete(rdata, fp)) {
943 mutex_unlock(&rdata->rp_mutex);
944 rjt_data.reason = ELS_RJT_LOGIC;
945 rjt_data.explan = ELS_EXPL_NONE;
946 goto reject_put;
947 }
948
949 fp = fc_frame_alloc(lport, sizeof(*flp));
950 if (!fp)
951 goto out;
952
953 fc_flogi_fill(lport, fp);
954 flp = fc_frame_payload_get(fp, sizeof(*flp));
955 flp->fl_cmd = ELS_LS_ACC;
956
957 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
958 lport->tt.frame_send(lport, fp);
959
960 /*
961 * Do not proceed with the state machine if our
962 * FLOGI has crossed with an FLOGI from the
963 * remote port; wait for the FLOGI response instead.
964 */
965 if (rdata->rp_state != RPORT_ST_FLOGI) {
966 if (rdata->ids.port_name < lport->wwpn)
967 fc_rport_enter_plogi(rdata);
968 else
969 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
970 }
971 out:
972 mutex_unlock(&rdata->rp_mutex);
973 kref_put(&rdata->kref, fc_rport_destroy);
974 fc_frame_free(rx_fp);
975 return;
976
977 reject_put:
978 kref_put(&rdata->kref, fc_rport_destroy);
979 reject:
980 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
981 fc_frame_free(rx_fp);
982 }
983
984 /**
985 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
986 * @sp: The sequence the PLOGI is on
987 * @fp: The PLOGI response frame
988 * @rdata_arg: The remote port that sent the PLOGI response
989 *
990 * Locking Note: This function will be called without the rport lock
991 * held, but it will lock, call an _enter_* function or fc_rport_error
992 * and then unlock the rport.
993 */
fc_rport_plogi_resp(struct fc_seq * sp,struct fc_frame * fp,void * rdata_arg)994 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
995 void *rdata_arg)
996 {
997 struct fc_rport_priv *rdata = rdata_arg;
998 struct fc_lport *lport = rdata->local_port;
999 struct fc_els_flogi *plp = NULL;
1000 u16 csp_seq;
1001 u16 cssp_seq;
1002 u8 op;
1003
1004 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
1005
1006 if (fp == ERR_PTR(-FC_EX_CLOSED))
1007 goto put;
1008
1009 mutex_lock(&rdata->rp_mutex);
1010
1011 if (rdata->rp_state != RPORT_ST_PLOGI) {
1012 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
1013 "%s\n", fc_rport_state(rdata));
1014 if (IS_ERR(fp))
1015 goto err;
1016 goto out;
1017 }
1018
1019 if (IS_ERR(fp)) {
1020 fc_rport_error_retry(rdata, PTR_ERR(fp));
1021 goto err;
1022 }
1023
1024 op = fc_frame_payload_op(fp);
1025 if (op == ELS_LS_ACC &&
1026 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
1027 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
1028 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
1029
1030 /* save plogi response sp_features for further reference */
1031 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
1032
1033 if (lport->point_to_multipoint)
1034 fc_rport_login_complete(rdata, fp);
1035 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
1036 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
1037 if (cssp_seq < csp_seq)
1038 csp_seq = cssp_seq;
1039 rdata->max_seq = csp_seq;
1040 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
1041 fc_rport_enter_prli(rdata);
1042 } else {
1043 struct fc_els_ls_rjt *rjt;
1044
1045 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1046 FC_RPORT_DBG(rdata, "PLOGI ELS rejected, reason %x expl %x\n",
1047 rjt->er_reason, rjt->er_explan);
1048 fc_rport_error_retry(rdata, -FC_EX_ELS_RJT);
1049 }
1050 out:
1051 fc_frame_free(fp);
1052 err:
1053 mutex_unlock(&rdata->rp_mutex);
1054 put:
1055 kref_put(&rdata->kref, fc_rport_destroy);
1056 }
1057
1058 static bool
fc_rport_compatible_roles(struct fc_lport * lport,struct fc_rport_priv * rdata)1059 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
1060 {
1061 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
1062 return true;
1063 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
1064 (lport->service_params & FCP_SPPF_INIT_FCN))
1065 return true;
1066 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
1067 (lport->service_params & FCP_SPPF_TARG_FCN))
1068 return true;
1069 return false;
1070 }
1071
1072 /**
1073 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
1074 * @rdata: The remote port to send a PLOGI to
1075 *
1076 * Reference counting: increments kref when sending ELS
1077 */
fc_rport_enter_plogi(struct fc_rport_priv * rdata)1078 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
1079 {
1080 struct fc_lport *lport = rdata->local_port;
1081 struct fc_frame *fp;
1082
1083 lockdep_assert_held(&rdata->rp_mutex);
1084
1085 if (!fc_rport_compatible_roles(lport, rdata)) {
1086 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
1087 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
1088 return;
1089 }
1090
1091 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
1092 fc_rport_state(rdata));
1093
1094 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
1095
1096 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
1097 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1098 if (!fp) {
1099 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
1100 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1101 return;
1102 }
1103 rdata->e_d_tov = lport->e_d_tov;
1104
1105 kref_get(&rdata->kref);
1106 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
1107 fc_rport_plogi_resp, rdata,
1108 2 * lport->r_a_tov)) {
1109 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1110 kref_put(&rdata->kref, fc_rport_destroy);
1111 }
1112 }
1113
1114 /**
1115 * fc_rport_prli_resp() - Process Login (PRLI) response handler
1116 * @sp: The sequence the PRLI response was on
1117 * @fp: The PRLI response frame
1118 * @rdata_arg: The remote port that sent the PRLI response
1119 *
1120 * Locking Note: This function will be called without the rport lock
1121 * held, but it will lock, call an _enter_* function or fc_rport_error
1122 * and then unlock the rport.
1123 */
fc_rport_prli_resp(struct fc_seq * sp,struct fc_frame * fp,void * rdata_arg)1124 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
1125 void *rdata_arg)
1126 {
1127 struct fc_rport_priv *rdata = rdata_arg;
1128 struct {
1129 struct fc_els_prli prli;
1130 struct fc_els_spp spp;
1131 } *pp;
1132 struct fc_els_spp temp_spp;
1133 struct fc_els_ls_rjt *rjt;
1134 struct fc4_prov *prov;
1135 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1136 u32 fcp_parm = 0;
1137 u8 op;
1138 enum fc_els_spp_resp resp_code;
1139
1140 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1141
1142 if (fp == ERR_PTR(-FC_EX_CLOSED))
1143 goto put;
1144
1145 mutex_lock(&rdata->rp_mutex);
1146
1147 if (rdata->rp_state != RPORT_ST_PRLI) {
1148 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1149 "%s\n", fc_rport_state(rdata));
1150 if (IS_ERR(fp))
1151 goto err;
1152 goto out;
1153 }
1154
1155 if (IS_ERR(fp)) {
1156 fc_rport_error_retry(rdata, PTR_ERR(fp));
1157 goto err;
1158 }
1159
1160 /* reinitialize remote port roles */
1161 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1162
1163 op = fc_frame_payload_op(fp);
1164 if (op == ELS_LS_ACC) {
1165 pp = fc_frame_payload_get(fp, sizeof(*pp));
1166 if (!pp)
1167 goto out;
1168
1169 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1170 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x spp_type 0x%x\n",
1171 pp->spp.spp_flags, pp->spp.spp_type);
1172 rdata->spp_type = pp->spp.spp_type;
1173 if (resp_code != FC_SPP_RESP_ACK) {
1174 if (resp_code == FC_SPP_RESP_CONF)
1175 fc_rport_error(rdata, -FC_EX_SEQ_ERR);
1176 else
1177 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1178 goto out;
1179 }
1180 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1181 goto out;
1182
1183 fcp_parm = ntohl(pp->spp.spp_params);
1184 if (fcp_parm & FCP_SPPF_RETRY)
1185 rdata->flags |= FC_RP_FLAGS_RETRY;
1186 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1187 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1188
1189 /*
1190 * Call prli provider if we should act as a target
1191 */
1192 prov = fc_passive_prov[rdata->spp_type];
1193 if (prov) {
1194 memset(&temp_spp, 0, sizeof(temp_spp));
1195 prov->prli(rdata, pp->prli.prli_spp_len,
1196 &pp->spp, &temp_spp);
1197 }
1198 /*
1199 * Check if the image pair could be established
1200 */
1201 if (rdata->spp_type != FC_TYPE_FCP ||
1202 !(pp->spp.spp_flags & FC_SPP_EST_IMG_PAIR)) {
1203 /*
1204 * Nope; we can't use this port as a target.
1205 */
1206 fcp_parm &= ~FCP_SPPF_TARG_FCN;
1207 }
1208 rdata->supported_classes = FC_COS_CLASS3;
1209 if (fcp_parm & FCP_SPPF_INIT_FCN)
1210 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1211 if (fcp_parm & FCP_SPPF_TARG_FCN)
1212 roles |= FC_RPORT_ROLE_FCP_TARGET;
1213
1214 rdata->ids.roles = roles;
1215 fc_rport_enter_rtv(rdata);
1216
1217 } else {
1218 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1219 FC_RPORT_DBG(rdata, "PRLI ELS rejected, reason %x expl %x\n",
1220 rjt->er_reason, rjt->er_explan);
1221 fc_rport_error_retry(rdata, FC_EX_ELS_RJT);
1222 }
1223
1224 out:
1225 fc_frame_free(fp);
1226 err:
1227 mutex_unlock(&rdata->rp_mutex);
1228 put:
1229 kref_put(&rdata->kref, fc_rport_destroy);
1230 }
1231
1232 /**
1233 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1234 * @rdata: The remote port to send the PRLI request to
1235 *
1236 * Reference counting: increments kref when sending ELS
1237 */
fc_rport_enter_prli(struct fc_rport_priv * rdata)1238 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1239 {
1240 struct fc_lport *lport = rdata->local_port;
1241 struct {
1242 struct fc_els_prli prli;
1243 struct fc_els_spp spp;
1244 } *pp;
1245 struct fc_frame *fp;
1246 struct fc4_prov *prov;
1247
1248 lockdep_assert_held(&rdata->rp_mutex);
1249
1250 /*
1251 * If the rport is one of the well known addresses
1252 * we skip PRLI and RTV and go straight to READY.
1253 */
1254 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1255 fc_rport_enter_ready(rdata);
1256 return;
1257 }
1258
1259 /*
1260 * And if the local port does not support the initiator function
1261 * there's no need to send a PRLI, either.
1262 */
1263 if (!(lport->service_params & FCP_SPPF_INIT_FCN)) {
1264 fc_rport_enter_ready(rdata);
1265 return;
1266 }
1267
1268 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1269 fc_rport_state(rdata));
1270
1271 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1272
1273 fp = fc_frame_alloc(lport, sizeof(*pp));
1274 if (!fp) {
1275 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1276 return;
1277 }
1278
1279 fc_prli_fill(lport, fp);
1280
1281 prov = fc_passive_prov[FC_TYPE_FCP];
1282 if (prov) {
1283 pp = fc_frame_payload_get(fp, sizeof(*pp));
1284 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1285 }
1286
1287 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1288 fc_host_port_id(lport->host), FC_TYPE_ELS,
1289 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1290
1291 kref_get(&rdata->kref);
1292 if (!fc_exch_seq_send(lport, fp, fc_rport_prli_resp,
1293 NULL, rdata, 2 * lport->r_a_tov)) {
1294 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1295 kref_put(&rdata->kref, fc_rport_destroy);
1296 }
1297 }
1298
1299 /**
1300 * fc_rport_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1301 * @sp: The sequence the RTV was on
1302 * @fp: The RTV response frame
1303 * @rdata_arg: The remote port that sent the RTV response
1304 *
1305 * Many targets don't seem to support this.
1306 *
1307 * Locking Note: This function will be called without the rport lock
1308 * held, but it will lock, call an _enter_* function or fc_rport_error
1309 * and then unlock the rport.
1310 */
fc_rport_rtv_resp(struct fc_seq * sp,struct fc_frame * fp,void * rdata_arg)1311 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1312 void *rdata_arg)
1313 {
1314 struct fc_rport_priv *rdata = rdata_arg;
1315 u8 op;
1316
1317 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1318
1319 if (fp == ERR_PTR(-FC_EX_CLOSED))
1320 goto put;
1321
1322 mutex_lock(&rdata->rp_mutex);
1323
1324 if (rdata->rp_state != RPORT_ST_RTV) {
1325 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1326 "%s\n", fc_rport_state(rdata));
1327 if (IS_ERR(fp))
1328 goto err;
1329 goto out;
1330 }
1331
1332 if (IS_ERR(fp)) {
1333 fc_rport_error(rdata, PTR_ERR(fp));
1334 goto err;
1335 }
1336
1337 op = fc_frame_payload_op(fp);
1338 if (op == ELS_LS_ACC) {
1339 struct fc_els_rtv_acc *rtv;
1340 u32 toq;
1341 u32 tov;
1342
1343 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1344 if (rtv) {
1345 toq = ntohl(rtv->rtv_toq);
1346 tov = ntohl(rtv->rtv_r_a_tov);
1347 if (tov == 0)
1348 tov = 1;
1349 if (tov > rdata->r_a_tov)
1350 rdata->r_a_tov = tov;
1351 tov = ntohl(rtv->rtv_e_d_tov);
1352 if (toq & FC_ELS_RTV_EDRES)
1353 tov /= 1000000;
1354 if (tov == 0)
1355 tov = 1;
1356 if (tov > rdata->e_d_tov)
1357 rdata->e_d_tov = tov;
1358 }
1359 }
1360
1361 fc_rport_enter_ready(rdata);
1362
1363 out:
1364 fc_frame_free(fp);
1365 err:
1366 mutex_unlock(&rdata->rp_mutex);
1367 put:
1368 kref_put(&rdata->kref, fc_rport_destroy);
1369 }
1370
1371 /**
1372 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1373 * @rdata: The remote port to send the RTV request to
1374 *
1375 * Reference counting: increments kref when sending ELS
1376 */
fc_rport_enter_rtv(struct fc_rport_priv * rdata)1377 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1378 {
1379 struct fc_frame *fp;
1380 struct fc_lport *lport = rdata->local_port;
1381
1382 lockdep_assert_held(&rdata->rp_mutex);
1383
1384 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1385 fc_rport_state(rdata));
1386
1387 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1388
1389 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1390 if (!fp) {
1391 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1392 return;
1393 }
1394
1395 kref_get(&rdata->kref);
1396 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1397 fc_rport_rtv_resp, rdata,
1398 2 * lport->r_a_tov)) {
1399 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1400 kref_put(&rdata->kref, fc_rport_destroy);
1401 }
1402 }
1403
1404 /**
1405 * fc_rport_recv_rtv_req() - Handler for Read Timeout Value (RTV) requests
1406 * @rdata: The remote port that sent the RTV request
1407 * @in_fp: The RTV request frame
1408 */
fc_rport_recv_rtv_req(struct fc_rport_priv * rdata,struct fc_frame * in_fp)1409 static void fc_rport_recv_rtv_req(struct fc_rport_priv *rdata,
1410 struct fc_frame *in_fp)
1411 {
1412 struct fc_lport *lport = rdata->local_port;
1413 struct fc_frame *fp;
1414 struct fc_els_rtv_acc *rtv;
1415 struct fc_seq_els_data rjt_data;
1416
1417 lockdep_assert_held(&rdata->rp_mutex);
1418 lockdep_assert_held(&lport->lp_mutex);
1419
1420 FC_RPORT_DBG(rdata, "Received RTV request\n");
1421
1422 fp = fc_frame_alloc(lport, sizeof(*rtv));
1423 if (!fp) {
1424 rjt_data.reason = ELS_RJT_UNAB;
1425 rjt_data.explan = ELS_EXPL_INSUF_RES;
1426 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1427 goto drop;
1428 }
1429 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1430 rtv->rtv_cmd = ELS_LS_ACC;
1431 rtv->rtv_r_a_tov = htonl(lport->r_a_tov);
1432 rtv->rtv_e_d_tov = htonl(lport->e_d_tov);
1433 rtv->rtv_toq = 0;
1434 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1435 lport->tt.frame_send(lport, fp);
1436 drop:
1437 fc_frame_free(in_fp);
1438 }
1439
1440 /**
1441 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1442 * @sp: The sequence the LOGO was on
1443 * @fp: The LOGO response frame
1444 * @lport_arg: The local port
1445 */
fc_rport_logo_resp(struct fc_seq * sp,struct fc_frame * fp,void * rdata_arg)1446 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1447 void *rdata_arg)
1448 {
1449 struct fc_rport_priv *rdata = rdata_arg;
1450 struct fc_lport *lport = rdata->local_port;
1451
1452 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1453 "Received a LOGO %s\n", fc_els_resp_type(fp));
1454 if (!IS_ERR(fp))
1455 fc_frame_free(fp);
1456 kref_put(&rdata->kref, fc_rport_destroy);
1457 }
1458
1459 /**
1460 * fc_rport_enter_logo() - Send a logout (LOGO) request
1461 * @rdata: The remote port to send the LOGO request to
1462 *
1463 * Reference counting: increments kref when sending ELS
1464 */
fc_rport_enter_logo(struct fc_rport_priv * rdata)1465 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1466 {
1467 struct fc_lport *lport = rdata->local_port;
1468 struct fc_frame *fp;
1469
1470 lockdep_assert_held(&rdata->rp_mutex);
1471
1472 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1473 fc_rport_state(rdata));
1474
1475 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1476 if (!fp)
1477 return;
1478 kref_get(&rdata->kref);
1479 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1480 fc_rport_logo_resp, rdata, 0))
1481 kref_put(&rdata->kref, fc_rport_destroy);
1482 }
1483
1484 /**
1485 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1486 * @sp: The sequence the ADISC response was on
1487 * @fp: The ADISC response frame
1488 * @rdata_arg: The remote port that sent the ADISC response
1489 *
1490 * Locking Note: This function will be called without the rport lock
1491 * held, but it will lock, call an _enter_* function or fc_rport_error
1492 * and then unlock the rport.
1493 */
fc_rport_adisc_resp(struct fc_seq * sp,struct fc_frame * fp,void * rdata_arg)1494 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1495 void *rdata_arg)
1496 {
1497 struct fc_rport_priv *rdata = rdata_arg;
1498 struct fc_els_adisc *adisc;
1499 u8 op;
1500
1501 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1502
1503 if (fp == ERR_PTR(-FC_EX_CLOSED))
1504 goto put;
1505
1506 mutex_lock(&rdata->rp_mutex);
1507
1508 if (rdata->rp_state != RPORT_ST_ADISC) {
1509 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1510 fc_rport_state(rdata));
1511 if (IS_ERR(fp))
1512 goto err;
1513 goto out;
1514 }
1515
1516 if (IS_ERR(fp)) {
1517 fc_rport_error(rdata, PTR_ERR(fp));
1518 goto err;
1519 }
1520
1521 /*
1522 * If address verification failed. Consider us logged out of the rport.
1523 * Since the rport is still in discovery, we want to be
1524 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1525 */
1526 op = fc_frame_payload_op(fp);
1527 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1528 if (op != ELS_LS_ACC || !adisc ||
1529 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1530 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1531 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1532 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1533 fc_rport_enter_flogi(rdata);
1534 } else {
1535 FC_RPORT_DBG(rdata, "ADISC OK\n");
1536 fc_rport_enter_ready(rdata);
1537 }
1538 out:
1539 fc_frame_free(fp);
1540 err:
1541 mutex_unlock(&rdata->rp_mutex);
1542 put:
1543 kref_put(&rdata->kref, fc_rport_destroy);
1544 }
1545
1546 /**
1547 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1548 * @rdata: The remote port to send the ADISC request to
1549 *
1550 * Reference counting: increments kref when sending ELS
1551 */
fc_rport_enter_adisc(struct fc_rport_priv * rdata)1552 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1553 {
1554 struct fc_lport *lport = rdata->local_port;
1555 struct fc_frame *fp;
1556
1557 lockdep_assert_held(&rdata->rp_mutex);
1558
1559 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1560 fc_rport_state(rdata));
1561
1562 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1563
1564 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1565 if (!fp) {
1566 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1567 return;
1568 }
1569 kref_get(&rdata->kref);
1570 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1571 fc_rport_adisc_resp, rdata,
1572 2 * lport->r_a_tov)) {
1573 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1574 kref_put(&rdata->kref, fc_rport_destroy);
1575 }
1576 }
1577
1578 /**
1579 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1580 * @rdata: The remote port that sent the ADISC request
1581 * @in_fp: The ADISC request frame
1582 */
fc_rport_recv_adisc_req(struct fc_rport_priv * rdata,struct fc_frame * in_fp)1583 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1584 struct fc_frame *in_fp)
1585 {
1586 struct fc_lport *lport = rdata->local_port;
1587 struct fc_frame *fp;
1588 struct fc_els_adisc *adisc;
1589 struct fc_seq_els_data rjt_data;
1590
1591 lockdep_assert_held(&rdata->rp_mutex);
1592 lockdep_assert_held(&lport->lp_mutex);
1593
1594 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1595
1596 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1597 if (!adisc) {
1598 rjt_data.reason = ELS_RJT_PROT;
1599 rjt_data.explan = ELS_EXPL_INV_LEN;
1600 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1601 goto drop;
1602 }
1603
1604 fp = fc_frame_alloc(lport, sizeof(*adisc));
1605 if (!fp)
1606 goto drop;
1607 fc_adisc_fill(lport, fp);
1608 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1609 adisc->adisc_cmd = ELS_LS_ACC;
1610 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1611 lport->tt.frame_send(lport, fp);
1612 drop:
1613 fc_frame_free(in_fp);
1614 }
1615
1616 /**
1617 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1618 * @rdata: The remote port that sent the RLS request
1619 * @rx_fp: The PRLI request frame
1620 */
fc_rport_recv_rls_req(struct fc_rport_priv * rdata,struct fc_frame * rx_fp)1621 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1622 struct fc_frame *rx_fp)
1623
1624 {
1625 struct fc_lport *lport = rdata->local_port;
1626 struct fc_frame *fp;
1627 struct fc_els_rls *rls;
1628 struct fc_els_rls_resp *rsp;
1629 struct fc_els_lesb *lesb;
1630 struct fc_seq_els_data rjt_data;
1631 struct fc_host_statistics *hst;
1632
1633 lockdep_assert_held(&rdata->rp_mutex);
1634
1635 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1636 fc_rport_state(rdata));
1637
1638 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1639 if (!rls) {
1640 rjt_data.reason = ELS_RJT_PROT;
1641 rjt_data.explan = ELS_EXPL_INV_LEN;
1642 goto out_rjt;
1643 }
1644
1645 fp = fc_frame_alloc(lport, sizeof(*rsp));
1646 if (!fp) {
1647 rjt_data.reason = ELS_RJT_UNAB;
1648 rjt_data.explan = ELS_EXPL_INSUF_RES;
1649 goto out_rjt;
1650 }
1651
1652 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1653 memset(rsp, 0, sizeof(*rsp));
1654 rsp->rls_cmd = ELS_LS_ACC;
1655 lesb = &rsp->rls_lesb;
1656 if (lport->tt.get_lesb) {
1657 /* get LESB from LLD if it supports it */
1658 lport->tt.get_lesb(lport, lesb);
1659 } else {
1660 fc_get_host_stats(lport->host);
1661 hst = &lport->host_stats;
1662 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1663 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1664 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1665 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1666 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1667 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1668 }
1669
1670 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1671 lport->tt.frame_send(lport, fp);
1672 goto out;
1673
1674 out_rjt:
1675 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1676 out:
1677 fc_frame_free(rx_fp);
1678 }
1679
1680 /**
1681 * fc_rport_recv_els_req() - Handler for validated ELS requests
1682 * @lport: The local port that received the ELS request
1683 * @fp: The ELS request frame
1684 *
1685 * Handle incoming ELS requests that require port login.
1686 * The ELS opcode has already been validated by the caller.
1687 *
1688 * Reference counting: does not modify kref
1689 */
fc_rport_recv_els_req(struct fc_lport * lport,struct fc_frame * fp)1690 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1691 {
1692 struct fc_rport_priv *rdata;
1693 struct fc_seq_els_data els_data;
1694
1695 lockdep_assert_held(&lport->lp_mutex);
1696
1697 rdata = fc_rport_lookup(lport, fc_frame_sid(fp));
1698 if (!rdata) {
1699 FC_RPORT_ID_DBG(lport, fc_frame_sid(fp),
1700 "Received ELS 0x%02x from non-logged-in port\n",
1701 fc_frame_payload_op(fp));
1702 goto reject;
1703 }
1704
1705 mutex_lock(&rdata->rp_mutex);
1706
1707 switch (rdata->rp_state) {
1708 case RPORT_ST_PRLI:
1709 case RPORT_ST_RTV:
1710 case RPORT_ST_READY:
1711 case RPORT_ST_ADISC:
1712 break;
1713 case RPORT_ST_PLOGI:
1714 if (fc_frame_payload_op(fp) == ELS_PRLI) {
1715 FC_RPORT_DBG(rdata, "Reject ELS PRLI "
1716 "while in state %s\n",
1717 fc_rport_state(rdata));
1718 mutex_unlock(&rdata->rp_mutex);
1719 kref_put(&rdata->kref, fc_rport_destroy);
1720 goto busy;
1721 }
1722 default:
1723 FC_RPORT_DBG(rdata,
1724 "Reject ELS 0x%02x while in state %s\n",
1725 fc_frame_payload_op(fp), fc_rport_state(rdata));
1726 mutex_unlock(&rdata->rp_mutex);
1727 kref_put(&rdata->kref, fc_rport_destroy);
1728 goto reject;
1729 }
1730
1731 switch (fc_frame_payload_op(fp)) {
1732 case ELS_PRLI:
1733 fc_rport_recv_prli_req(rdata, fp);
1734 break;
1735 case ELS_PRLO:
1736 fc_rport_recv_prlo_req(rdata, fp);
1737 break;
1738 case ELS_ADISC:
1739 fc_rport_recv_adisc_req(rdata, fp);
1740 break;
1741 case ELS_RRQ:
1742 fc_seq_els_rsp_send(fp, ELS_RRQ, NULL);
1743 fc_frame_free(fp);
1744 break;
1745 case ELS_REC:
1746 fc_seq_els_rsp_send(fp, ELS_REC, NULL);
1747 fc_frame_free(fp);
1748 break;
1749 case ELS_RLS:
1750 fc_rport_recv_rls_req(rdata, fp);
1751 break;
1752 case ELS_RTV:
1753 fc_rport_recv_rtv_req(rdata, fp);
1754 break;
1755 default:
1756 fc_frame_free(fp); /* can't happen */
1757 break;
1758 }
1759
1760 mutex_unlock(&rdata->rp_mutex);
1761 kref_put(&rdata->kref, fc_rport_destroy);
1762 return;
1763
1764 reject:
1765 els_data.reason = ELS_RJT_UNAB;
1766 els_data.explan = ELS_EXPL_PLOGI_REQD;
1767 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1768 fc_frame_free(fp);
1769 return;
1770
1771 busy:
1772 els_data.reason = ELS_RJT_BUSY;
1773 els_data.explan = ELS_EXPL_NONE;
1774 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1775 fc_frame_free(fp);
1776 return;
1777 }
1778
1779 /**
1780 * fc_rport_recv_req() - Handler for requests
1781 * @lport: The local port that received the request
1782 * @fp: The request frame
1783 *
1784 * Reference counting: does not modify kref
1785 */
fc_rport_recv_req(struct fc_lport * lport,struct fc_frame * fp)1786 void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1787 {
1788 struct fc_seq_els_data els_data;
1789
1790 lockdep_assert_held(&lport->lp_mutex);
1791
1792 /*
1793 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1794 * don't require prior login.
1795 * Check for unsupported opcodes first and reject them.
1796 * For some ops, it would be incorrect to reject with "PLOGI required".
1797 */
1798 switch (fc_frame_payload_op(fp)) {
1799 case ELS_FLOGI:
1800 fc_rport_recv_flogi_req(lport, fp);
1801 break;
1802 case ELS_PLOGI:
1803 fc_rport_recv_plogi_req(lport, fp);
1804 break;
1805 case ELS_LOGO:
1806 fc_rport_recv_logo_req(lport, fp);
1807 break;
1808 case ELS_PRLI:
1809 case ELS_PRLO:
1810 case ELS_ADISC:
1811 case ELS_RRQ:
1812 case ELS_REC:
1813 case ELS_RLS:
1814 case ELS_RTV:
1815 fc_rport_recv_els_req(lport, fp);
1816 break;
1817 default:
1818 els_data.reason = ELS_RJT_UNSUP;
1819 els_data.explan = ELS_EXPL_NONE;
1820 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1821 fc_frame_free(fp);
1822 break;
1823 }
1824 }
1825 EXPORT_SYMBOL(fc_rport_recv_req);
1826
1827 /**
1828 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1829 * @lport: The local port that received the PLOGI request
1830 * @rx_fp: The PLOGI request frame
1831 *
1832 * Reference counting: increments kref on return
1833 */
fc_rport_recv_plogi_req(struct fc_lport * lport,struct fc_frame * rx_fp)1834 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1835 struct fc_frame *rx_fp)
1836 {
1837 struct fc_disc *disc;
1838 struct fc_rport_priv *rdata;
1839 struct fc_frame *fp = rx_fp;
1840 struct fc_els_flogi *pl;
1841 struct fc_seq_els_data rjt_data;
1842 u32 sid;
1843
1844 lockdep_assert_held(&lport->lp_mutex);
1845
1846 sid = fc_frame_sid(fp);
1847
1848 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1849
1850 pl = fc_frame_payload_get(fp, sizeof(*pl));
1851 if (!pl) {
1852 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1853 rjt_data.reason = ELS_RJT_PROT;
1854 rjt_data.explan = ELS_EXPL_INV_LEN;
1855 goto reject;
1856 }
1857
1858 disc = &lport->disc;
1859 mutex_lock(&disc->disc_mutex);
1860 rdata = fc_rport_create(lport, sid);
1861 if (!rdata) {
1862 mutex_unlock(&disc->disc_mutex);
1863 rjt_data.reason = ELS_RJT_UNAB;
1864 rjt_data.explan = ELS_EXPL_INSUF_RES;
1865 goto reject;
1866 }
1867
1868 mutex_lock(&rdata->rp_mutex);
1869 mutex_unlock(&disc->disc_mutex);
1870
1871 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1872 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1873
1874 /*
1875 * If the rport was just created, possibly due to the incoming PLOGI,
1876 * set the state appropriately and accept the PLOGI.
1877 *
1878 * If we had also sent a PLOGI, and if the received PLOGI is from a
1879 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1880 * "command already in progress".
1881 *
1882 * XXX TBD: If the session was ready before, the PLOGI should result in
1883 * all outstanding exchanges being reset.
1884 */
1885 switch (rdata->rp_state) {
1886 case RPORT_ST_INIT:
1887 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1888 break;
1889 case RPORT_ST_PLOGI_WAIT:
1890 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1891 break;
1892 case RPORT_ST_PLOGI:
1893 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1894 if (rdata->ids.port_name < lport->wwpn) {
1895 mutex_unlock(&rdata->rp_mutex);
1896 rjt_data.reason = ELS_RJT_INPROG;
1897 rjt_data.explan = ELS_EXPL_NONE;
1898 goto reject;
1899 }
1900 break;
1901 case RPORT_ST_PRLI:
1902 case RPORT_ST_RTV:
1903 case RPORT_ST_READY:
1904 case RPORT_ST_ADISC:
1905 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1906 "- ignored for now\n", rdata->rp_state);
1907 /* XXX TBD - should reset */
1908 break;
1909 case RPORT_ST_FLOGI:
1910 case RPORT_ST_DELETE:
1911 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1912 fc_rport_state(rdata));
1913 mutex_unlock(&rdata->rp_mutex);
1914 rjt_data.reason = ELS_RJT_BUSY;
1915 rjt_data.explan = ELS_EXPL_NONE;
1916 goto reject;
1917 }
1918 if (!fc_rport_compatible_roles(lport, rdata)) {
1919 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1920 mutex_unlock(&rdata->rp_mutex);
1921 rjt_data.reason = ELS_RJT_LOGIC;
1922 rjt_data.explan = ELS_EXPL_NONE;
1923 goto reject;
1924 }
1925
1926 /*
1927 * Get session payload size from incoming PLOGI.
1928 */
1929 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1930
1931 /*
1932 * Send LS_ACC. If this fails, the originator should retry.
1933 */
1934 fp = fc_frame_alloc(lport, sizeof(*pl));
1935 if (!fp)
1936 goto out;
1937
1938 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1939 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1940 lport->tt.frame_send(lport, fp);
1941 fc_rport_enter_prli(rdata);
1942 out:
1943 mutex_unlock(&rdata->rp_mutex);
1944 fc_frame_free(rx_fp);
1945 return;
1946
1947 reject:
1948 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1949 fc_frame_free(fp);
1950 }
1951
1952 /**
1953 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1954 * @rdata: The remote port that sent the PRLI request
1955 * @rx_fp: The PRLI request frame
1956 */
fc_rport_recv_prli_req(struct fc_rport_priv * rdata,struct fc_frame * rx_fp)1957 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1958 struct fc_frame *rx_fp)
1959 {
1960 struct fc_lport *lport = rdata->local_port;
1961 struct fc_frame *fp;
1962 struct {
1963 struct fc_els_prli prli;
1964 struct fc_els_spp spp;
1965 } *pp;
1966 struct fc_els_spp *rspp; /* request service param page */
1967 struct fc_els_spp *spp; /* response spp */
1968 unsigned int len;
1969 unsigned int plen;
1970 enum fc_els_spp_resp resp;
1971 struct fc_seq_els_data rjt_data;
1972 struct fc4_prov *prov;
1973
1974 lockdep_assert_held(&rdata->rp_mutex);
1975
1976 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1977 fc_rport_state(rdata));
1978
1979 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1980 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1981 if (!pp)
1982 goto reject_len;
1983 plen = ntohs(pp->prli.prli_len);
1984 if ((plen % 4) != 0 || plen > len || plen < 16)
1985 goto reject_len;
1986 if (plen < len)
1987 len = plen;
1988 plen = pp->prli.prli_spp_len;
1989 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1990 plen > len || len < sizeof(*pp) || plen < 12)
1991 goto reject_len;
1992 rspp = &pp->spp;
1993
1994 fp = fc_frame_alloc(lport, len);
1995 if (!fp) {
1996 rjt_data.reason = ELS_RJT_UNAB;
1997 rjt_data.explan = ELS_EXPL_INSUF_RES;
1998 goto reject;
1999 }
2000 pp = fc_frame_payload_get(fp, len);
2001 WARN_ON(!pp);
2002 memset(pp, 0, len);
2003 pp->prli.prli_cmd = ELS_LS_ACC;
2004 pp->prli.prli_spp_len = plen;
2005 pp->prli.prli_len = htons(len);
2006 len -= sizeof(struct fc_els_prli);
2007
2008 /*
2009 * Go through all the service parameter pages and build
2010 * response. If plen indicates longer SPP than standard,
2011 * use that. The entire response has been pre-cleared above.
2012 */
2013 spp = &pp->spp;
2014 mutex_lock(&fc_prov_mutex);
2015 while (len >= plen) {
2016 rdata->spp_type = rspp->spp_type;
2017 spp->spp_type = rspp->spp_type;
2018 spp->spp_type_ext = rspp->spp_type_ext;
2019 resp = 0;
2020
2021 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
2022 enum fc_els_spp_resp active = 0, passive = 0;
2023
2024 prov = fc_active_prov[rspp->spp_type];
2025 if (prov)
2026 active = prov->prli(rdata, plen, rspp, spp);
2027 prov = fc_passive_prov[rspp->spp_type];
2028 if (prov)
2029 passive = prov->prli(rdata, plen, rspp, spp);
2030 if (!active || passive == FC_SPP_RESP_ACK)
2031 resp = passive;
2032 else
2033 resp = active;
2034 FC_RPORT_DBG(rdata, "PRLI rspp type %x "
2035 "active %x passive %x\n",
2036 rspp->spp_type, active, passive);
2037 }
2038 if (!resp) {
2039 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
2040 resp |= FC_SPP_RESP_CONF;
2041 else
2042 resp |= FC_SPP_RESP_INVL;
2043 }
2044 spp->spp_flags |= resp;
2045 len -= plen;
2046 rspp = (struct fc_els_spp *)((char *)rspp + plen);
2047 spp = (struct fc_els_spp *)((char *)spp + plen);
2048 }
2049 mutex_unlock(&fc_prov_mutex);
2050
2051 /*
2052 * Send LS_ACC. If this fails, the originator should retry.
2053 */
2054 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2055 lport->tt.frame_send(lport, fp);
2056
2057 goto drop;
2058
2059 reject_len:
2060 rjt_data.reason = ELS_RJT_PROT;
2061 rjt_data.explan = ELS_EXPL_INV_LEN;
2062 reject:
2063 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2064 drop:
2065 fc_frame_free(rx_fp);
2066 }
2067
2068 /**
2069 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
2070 * @rdata: The remote port that sent the PRLO request
2071 * @rx_fp: The PRLO request frame
2072 */
fc_rport_recv_prlo_req(struct fc_rport_priv * rdata,struct fc_frame * rx_fp)2073 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
2074 struct fc_frame *rx_fp)
2075 {
2076 struct fc_lport *lport = rdata->local_port;
2077 struct fc_frame *fp;
2078 struct {
2079 struct fc_els_prlo prlo;
2080 struct fc_els_spp spp;
2081 } *pp;
2082 struct fc_els_spp *rspp; /* request service param page */
2083 struct fc_els_spp *spp; /* response spp */
2084 unsigned int len;
2085 unsigned int plen;
2086 struct fc_seq_els_data rjt_data;
2087
2088 lockdep_assert_held(&rdata->rp_mutex);
2089
2090 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
2091 fc_rport_state(rdata));
2092
2093 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
2094 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
2095 if (!pp)
2096 goto reject_len;
2097 plen = ntohs(pp->prlo.prlo_len);
2098 if (plen != 20)
2099 goto reject_len;
2100 if (plen < len)
2101 len = plen;
2102
2103 rspp = &pp->spp;
2104
2105 fp = fc_frame_alloc(lport, len);
2106 if (!fp) {
2107 rjt_data.reason = ELS_RJT_UNAB;
2108 rjt_data.explan = ELS_EXPL_INSUF_RES;
2109 goto reject;
2110 }
2111
2112 pp = fc_frame_payload_get(fp, len);
2113 WARN_ON(!pp);
2114 memset(pp, 0, len);
2115 pp->prlo.prlo_cmd = ELS_LS_ACC;
2116 pp->prlo.prlo_obs = 0x10;
2117 pp->prlo.prlo_len = htons(len);
2118 spp = &pp->spp;
2119 spp->spp_type = rspp->spp_type;
2120 spp->spp_type_ext = rspp->spp_type_ext;
2121 spp->spp_flags = FC_SPP_RESP_ACK;
2122
2123 fc_rport_enter_prli(rdata);
2124
2125 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2126 lport->tt.frame_send(lport, fp);
2127 goto drop;
2128
2129 reject_len:
2130 rjt_data.reason = ELS_RJT_PROT;
2131 rjt_data.explan = ELS_EXPL_INV_LEN;
2132 reject:
2133 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2134 drop:
2135 fc_frame_free(rx_fp);
2136 }
2137
2138 /**
2139 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
2140 * @lport: The local port that received the LOGO request
2141 * @fp: The LOGO request frame
2142 *
2143 * Reference counting: drops kref on return
2144 */
fc_rport_recv_logo_req(struct fc_lport * lport,struct fc_frame * fp)2145 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
2146 {
2147 struct fc_rport_priv *rdata;
2148 u32 sid;
2149
2150 lockdep_assert_held(&lport->lp_mutex);
2151
2152 fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
2153
2154 sid = fc_frame_sid(fp);
2155
2156 rdata = fc_rport_lookup(lport, sid);
2157 if (rdata) {
2158 mutex_lock(&rdata->rp_mutex);
2159 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
2160 fc_rport_state(rdata));
2161
2162 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
2163 mutex_unlock(&rdata->rp_mutex);
2164 kref_put(&rdata->kref, fc_rport_destroy);
2165 } else
2166 FC_RPORT_ID_DBG(lport, sid,
2167 "Received LOGO from non-logged-in port\n");
2168 fc_frame_free(fp);
2169 }
2170
2171 /**
2172 * fc_rport_flush_queue() - Flush the rport_event_queue
2173 */
fc_rport_flush_queue(void)2174 void fc_rport_flush_queue(void)
2175 {
2176 flush_workqueue(rport_event_queue);
2177 }
2178 EXPORT_SYMBOL(fc_rport_flush_queue);
2179
2180 /**
2181 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
2182 * @rdata: remote port private
2183 * @spp_len: service parameter page length
2184 * @rspp: received service parameter page
2185 * @spp: response service parameter page
2186 *
2187 * Returns the value for the response code to be placed in spp_flags;
2188 * Returns 0 if not an initiator.
2189 */
fc_rport_fcp_prli(struct fc_rport_priv * rdata,u32 spp_len,const struct fc_els_spp * rspp,struct fc_els_spp * spp)2190 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2191 const struct fc_els_spp *rspp,
2192 struct fc_els_spp *spp)
2193 {
2194 struct fc_lport *lport = rdata->local_port;
2195 u32 fcp_parm;
2196
2197 fcp_parm = ntohl(rspp->spp_params);
2198 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2199 if (fcp_parm & FCP_SPPF_INIT_FCN)
2200 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2201 if (fcp_parm & FCP_SPPF_TARG_FCN)
2202 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2203 if (fcp_parm & FCP_SPPF_RETRY)
2204 rdata->flags |= FC_RP_FLAGS_RETRY;
2205 rdata->supported_classes = FC_COS_CLASS3;
2206
2207 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
2208 return 0;
2209
2210 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2211
2212 /*
2213 * OR in our service parameters with other providers (target), if any.
2214 */
2215 fcp_parm = ntohl(spp->spp_params);
2216 spp->spp_params = htonl(fcp_parm | lport->service_params);
2217 return FC_SPP_RESP_ACK;
2218 }
2219
2220 /*
2221 * FC-4 provider ops for FCP initiator.
2222 */
2223 struct fc4_prov fc_rport_fcp_init = {
2224 .prli = fc_rport_fcp_prli,
2225 };
2226
2227 /**
2228 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2229 * @rdata: remote port private
2230 * @spp_len: service parameter page length
2231 * @rspp: received service parameter page
2232 * @spp: response service parameter page
2233 */
fc_rport_t0_prli(struct fc_rport_priv * rdata,u32 spp_len,const struct fc_els_spp * rspp,struct fc_els_spp * spp)2234 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2235 const struct fc_els_spp *rspp,
2236 struct fc_els_spp *spp)
2237 {
2238 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2239 return FC_SPP_RESP_INVL;
2240 return FC_SPP_RESP_ACK;
2241 }
2242
2243 /*
2244 * FC-4 provider ops for type 0 service parameters.
2245 *
2246 * This handles the special case of type 0 which is always successful
2247 * but doesn't do anything otherwise.
2248 */
2249 struct fc4_prov fc_rport_t0_prov = {
2250 .prli = fc_rport_t0_prli,
2251 };
2252
2253 /**
2254 * fc_setup_rport() - Initialize the rport_event_queue
2255 */
fc_setup_rport(void)2256 int fc_setup_rport(void)
2257 {
2258 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2259 if (!rport_event_queue)
2260 return -ENOMEM;
2261 return 0;
2262 }
2263
2264 /**
2265 * fc_destroy_rport() - Destroy the rport_event_queue
2266 */
fc_destroy_rport(void)2267 void fc_destroy_rport(void)
2268 {
2269 destroy_workqueue(rport_event_queue);
2270 }
2271
2272 /**
2273 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2274 * @rport: The remote port whose I/O should be terminated
2275 */
fc_rport_terminate_io(struct fc_rport * rport)2276 void fc_rport_terminate_io(struct fc_rport *rport)
2277 {
2278 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2279 struct fc_lport *lport = rpriv->local_port;
2280
2281 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2282 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2283 }
2284 EXPORT_SYMBOL(fc_rport_terminate_io);
2285