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