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