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