]>
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 | |
37 | * more comments on the heirarchy. | |
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/rcupdate.h> | |
51 | #include <linux/timer.h> | |
52 | #include <linux/workqueue.h> | |
53 | #include <asm/unaligned.h> | |
54 | ||
55 | #include <scsi/libfc.h> | |
56 | #include <scsi/fc_encode.h> | |
57 | ||
8866a5d9 RL |
58 | #include "fc_libfc.h" |
59 | ||
42e9a92f RL |
60 | struct workqueue_struct *rport_event_queue; |
61 | ||
9fb9d328 JE |
62 | static void fc_rport_enter_plogi(struct fc_rport_priv *); |
63 | static void fc_rport_enter_prli(struct fc_rport_priv *); | |
64 | static void fc_rport_enter_rtv(struct fc_rport_priv *); | |
65 | static void fc_rport_enter_ready(struct fc_rport_priv *); | |
66 | static void fc_rport_enter_logo(struct fc_rport_priv *); | |
370c3bd0 | 67 | static void fc_rport_enter_adisc(struct fc_rport_priv *); |
42e9a92f | 68 | |
3ac6f98f | 69 | static void fc_rport_recv_plogi_req(struct fc_lport *, |
42e9a92f | 70 | struct fc_seq *, struct fc_frame *); |
9fb9d328 | 71 | static void fc_rport_recv_prli_req(struct fc_rport_priv *, |
42e9a92f | 72 | struct fc_seq *, struct fc_frame *); |
9fb9d328 | 73 | static void fc_rport_recv_prlo_req(struct fc_rport_priv *, |
42e9a92f | 74 | struct fc_seq *, struct fc_frame *); |
83fe6a93 | 75 | static void fc_rport_recv_logo_req(struct fc_lport *, |
42e9a92f RL |
76 | struct fc_seq *, struct fc_frame *); |
77 | static void fc_rport_timeout(struct work_struct *); | |
9fb9d328 JE |
78 | static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *); |
79 | static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *); | |
42e9a92f RL |
80 | static void fc_rport_work(struct work_struct *); |
81 | ||
82 | static const char *fc_rport_state_names[] = { | |
42e9a92f RL |
83 | [RPORT_ST_INIT] = "Init", |
84 | [RPORT_ST_PLOGI] = "PLOGI", | |
85 | [RPORT_ST_PRLI] = "PRLI", | |
86 | [RPORT_ST_RTV] = "RTV", | |
87 | [RPORT_ST_READY] = "Ready", | |
88 | [RPORT_ST_LOGO] = "LOGO", | |
370c3bd0 | 89 | [RPORT_ST_ADISC] = "ADISC", |
14194054 | 90 | [RPORT_ST_DELETE] = "Delete", |
b4a9c7ed | 91 | [RPORT_ST_RESTART] = "Restart", |
42e9a92f RL |
92 | }; |
93 | ||
8025b5db | 94 | /** |
3a3b42bf RL |
95 | * fc_rport_lookup() - Lookup a remote port by port_id |
96 | * @lport: The local port to lookup the remote port on | |
97 | * @port_id: The remote port ID to look up | |
8025b5db JE |
98 | */ |
99 | static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport, | |
100 | u32 port_id) | |
101 | { | |
102 | struct fc_rport_priv *rdata; | |
103 | ||
104 | list_for_each_entry(rdata, &lport->disc.rports, peers) | |
b4a9c7ed | 105 | if (rdata->ids.port_id == port_id) |
8025b5db JE |
106 | return rdata; |
107 | return NULL; | |
108 | } | |
109 | ||
9e9d0452 | 110 | /** |
9737e6a7 | 111 | * fc_rport_create() - Create a new remote port |
3a3b42bf RL |
112 | * @lport: The local port this remote port will be associated with |
113 | * @ids: The identifiers for the new remote port | |
114 | * | |
115 | * The remote port will start in the INIT state. | |
9e9d0452 | 116 | * |
48f00902 | 117 | * Locking note: must be called with the disc_mutex held. |
9e9d0452 JE |
118 | */ |
119 | static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, | |
9737e6a7 | 120 | u32 port_id) |
42e9a92f | 121 | { |
ab28f1fd | 122 | struct fc_rport_priv *rdata; |
42e9a92f | 123 | |
9737e6a7 | 124 | rdata = lport->tt.rport_lookup(lport, port_id); |
19f97e3c JE |
125 | if (rdata) |
126 | return rdata; | |
127 | ||
9e9d0452 JE |
128 | rdata = kzalloc(sizeof(*rdata), GFP_KERNEL); |
129 | if (!rdata) | |
42e9a92f RL |
130 | return NULL; |
131 | ||
9737e6a7 RL |
132 | rdata->ids.node_name = -1; |
133 | rdata->ids.port_name = -1; | |
134 | rdata->ids.port_id = port_id; | |
135 | rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN; | |
136 | ||
f211fa51 | 137 | kref_init(&rdata->kref); |
42e9a92f | 138 | mutex_init(&rdata->rp_mutex); |
795d86f5 | 139 | rdata->local_port = lport; |
42e9a92f RL |
140 | rdata->rp_state = RPORT_ST_INIT; |
141 | rdata->event = RPORT_EV_NONE; | |
142 | rdata->flags = FC_RP_FLAGS_REC_SUPPORTED; | |
795d86f5 JE |
143 | rdata->e_d_tov = lport->e_d_tov; |
144 | rdata->r_a_tov = lport->r_a_tov; | |
f211fa51 | 145 | rdata->maxframe_size = FC_MIN_MAX_PAYLOAD; |
42e9a92f RL |
146 | INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout); |
147 | INIT_WORK(&rdata->event_work, fc_rport_work); | |
9737e6a7 | 148 | if (port_id != FC_FID_DIR_SERV) |
48f00902 | 149 | list_add(&rdata->peers, &lport->disc.rports); |
9fb9d328 | 150 | return rdata; |
42e9a92f RL |
151 | } |
152 | ||
f211fa51 | 153 | /** |
3a3b42bf RL |
154 | * fc_rport_destroy() - Free a remote port after last reference is released |
155 | * @kref: The remote port's kref | |
f211fa51 JE |
156 | */ |
157 | static void fc_rport_destroy(struct kref *kref) | |
158 | { | |
159 | struct fc_rport_priv *rdata; | |
f211fa51 JE |
160 | |
161 | rdata = container_of(kref, struct fc_rport_priv, kref); | |
9e9d0452 | 162 | kfree(rdata); |
f211fa51 JE |
163 | } |
164 | ||
42e9a92f | 165 | /** |
3a3b42bf RL |
166 | * fc_rport_state() - Return a string identifying the remote port's state |
167 | * @rdata: The remote port | |
42e9a92f | 168 | */ |
9fb9d328 | 169 | static const char *fc_rport_state(struct fc_rport_priv *rdata) |
42e9a92f RL |
170 | { |
171 | const char *cp; | |
42e9a92f RL |
172 | |
173 | cp = fc_rport_state_names[rdata->rp_state]; | |
174 | if (!cp) | |
175 | cp = "Unknown"; | |
176 | return cp; | |
177 | } | |
178 | ||
179 | /** | |
3a3b42bf RL |
180 | * fc_set_rport_loss_tmo() - Set the remote port loss timeout |
181 | * @rport: The remote port that gets a new timeout value | |
182 | * @timeout: The new timeout value (in seconds) | |
42e9a92f RL |
183 | */ |
184 | void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout) | |
185 | { | |
186 | if (timeout) | |
187 | rport->dev_loss_tmo = timeout + 5; | |
188 | else | |
189 | rport->dev_loss_tmo = 30; | |
190 | } | |
191 | EXPORT_SYMBOL(fc_set_rport_loss_tmo); | |
192 | ||
193 | /** | |
3a3b42bf RL |
194 | * fc_plogi_get_maxframe() - Get the maximum payload from the common service |
195 | * parameters in a FLOGI frame | |
196 | * @flp: The FLOGI payload | |
197 | * @maxval: The maximum frame size upper limit; this may be less than what | |
198 | * is in the service parameters | |
42e9a92f | 199 | */ |
b2ab99c9 RL |
200 | static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp, |
201 | unsigned int maxval) | |
42e9a92f RL |
202 | { |
203 | unsigned int mfs; | |
204 | ||
205 | /* | |
206 | * Get max payload from the common service parameters and the | |
207 | * class 3 receive data field size. | |
208 | */ | |
209 | mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK; | |
210 | if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval) | |
211 | maxval = mfs; | |
212 | mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs); | |
213 | if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval) | |
214 | maxval = mfs; | |
215 | return maxval; | |
216 | } | |
217 | ||
218 | /** | |
3a3b42bf RL |
219 | * fc_rport_state_enter() - Change the state of a remote port |
220 | * @rdata: The remote port whose state should change | |
221 | * @new: The new state | |
42e9a92f RL |
222 | * |
223 | * Locking Note: Called with the rport lock held | |
224 | */ | |
9fb9d328 | 225 | static void fc_rport_state_enter(struct fc_rport_priv *rdata, |
42e9a92f RL |
226 | enum fc_rport_state new) |
227 | { | |
42e9a92f RL |
228 | if (rdata->rp_state != new) |
229 | rdata->retries = 0; | |
230 | rdata->rp_state = new; | |
231 | } | |
232 | ||
3a3b42bf RL |
233 | /** |
234 | * fc_rport_work() - Handler for remote port events in the rport_event_queue | |
235 | * @work: Handle to the remote port being dequeued | |
236 | */ | |
42e9a92f RL |
237 | static void fc_rport_work(struct work_struct *work) |
238 | { | |
571f824c | 239 | u32 port_id; |
ab28f1fd JE |
240 | struct fc_rport_priv *rdata = |
241 | container_of(work, struct fc_rport_priv, event_work); | |
3a3b42bf | 242 | struct fc_rport_libfc_priv *rpriv; |
42e9a92f | 243 | enum fc_rport_event event; |
42e9a92f RL |
244 | struct fc_lport *lport = rdata->local_port; |
245 | struct fc_rport_operations *rport_ops; | |
629f4427 | 246 | struct fc_rport_identifiers ids; |
f211fa51 | 247 | struct fc_rport *rport; |
b4a9c7ed | 248 | int restart = 0; |
42e9a92f RL |
249 | |
250 | mutex_lock(&rdata->rp_mutex); | |
251 | event = rdata->event; | |
252 | rport_ops = rdata->ops; | |
f211fa51 | 253 | rport = rdata->rport; |
42e9a92f | 254 | |
9e9d0452 JE |
255 | FC_RPORT_DBG(rdata, "work event %u\n", event); |
256 | ||
629f4427 | 257 | switch (event) { |
4c0f62b5 | 258 | case RPORT_EV_READY: |
f211fa51 | 259 | ids = rdata->ids; |
5f7ea3b7 | 260 | rdata->event = RPORT_EV_NONE; |
9e9d0452 | 261 | kref_get(&rdata->kref); |
42e9a92f RL |
262 | mutex_unlock(&rdata->rp_mutex); |
263 | ||
9e9d0452 JE |
264 | if (!rport) |
265 | rport = fc_remote_port_add(lport->host, 0, &ids); | |
266 | if (!rport) { | |
267 | FC_RPORT_DBG(rdata, "Failed to add the rport\n"); | |
268 | lport->tt.rport_logoff(rdata); | |
269 | kref_put(&rdata->kref, lport->tt.rport_destroy); | |
270 | return; | |
42e9a92f | 271 | } |
9e9d0452 JE |
272 | mutex_lock(&rdata->rp_mutex); |
273 | if (rdata->rport) | |
274 | FC_RPORT_DBG(rdata, "rport already allocated\n"); | |
275 | rdata->rport = rport; | |
276 | rport->maxframe_size = rdata->maxframe_size; | |
277 | rport->supported_classes = rdata->supported_classes; | |
278 | ||
3a3b42bf RL |
279 | rpriv = rport->dd_data; |
280 | rpriv->local_port = lport; | |
281 | rpriv->rp_state = rdata->rp_state; | |
282 | rpriv->flags = rdata->flags; | |
283 | rpriv->e_d_tov = rdata->e_d_tov; | |
284 | rpriv->r_a_tov = rdata->r_a_tov; | |
9e9d0452 JE |
285 | mutex_unlock(&rdata->rp_mutex); |
286 | ||
8345592b | 287 | if (rport_ops && rport_ops->event_callback) { |
9e9d0452 | 288 | FC_RPORT_DBG(rdata, "callback ev %d\n", event); |
9fb9d328 | 289 | rport_ops->event_callback(lport, rdata, event); |
9e9d0452 JE |
290 | } |
291 | kref_put(&rdata->kref, lport->tt.rport_destroy); | |
629f4427 JE |
292 | break; |
293 | ||
294 | case RPORT_EV_FAILED: | |
295 | case RPORT_EV_LOGO: | |
296 | case RPORT_EV_STOP: | |
9e9d0452 | 297 | port_id = rdata->ids.port_id; |
42e9a92f | 298 | mutex_unlock(&rdata->rp_mutex); |
9e9d0452 | 299 | |
48f00902 | 300 | if (port_id != FC_FID_DIR_SERV) { |
b4a9c7ed JE |
301 | /* |
302 | * We must drop rp_mutex before taking disc_mutex. | |
303 | * Re-evaluate state to allow for restart. | |
304 | * A transition to RESTART state must only happen | |
305 | * while disc_mutex is held and rdata is on the list. | |
306 | */ | |
48f00902 | 307 | mutex_lock(&lport->disc.disc_mutex); |
b4a9c7ed JE |
308 | mutex_lock(&rdata->rp_mutex); |
309 | if (rdata->rp_state == RPORT_ST_RESTART) | |
310 | restart = 1; | |
311 | else | |
312 | list_del(&rdata->peers); | |
313 | mutex_unlock(&rdata->rp_mutex); | |
48f00902 JE |
314 | mutex_unlock(&lport->disc.disc_mutex); |
315 | } | |
316 | ||
8345592b | 317 | if (rport_ops && rport_ops->event_callback) { |
9e9d0452 | 318 | FC_RPORT_DBG(rdata, "callback ev %d\n", event); |
9fb9d328 | 319 | rport_ops->event_callback(lport, rdata, event); |
9e9d0452 | 320 | } |
201e5795 | 321 | cancel_delayed_work_sync(&rdata->retry_work); |
9e9d0452 JE |
322 | |
323 | /* | |
324 | * Reset any outstanding exchanges before freeing rport. | |
325 | */ | |
326 | lport->tt.exch_mgr_reset(lport, 0, port_id); | |
327 | lport->tt.exch_mgr_reset(lport, port_id, 0); | |
328 | ||
329 | if (rport) { | |
3a3b42bf RL |
330 | rpriv = rport->dd_data; |
331 | rpriv->rp_state = RPORT_ST_DELETE; | |
9e9d0452 JE |
332 | mutex_lock(&rdata->rp_mutex); |
333 | rdata->rport = NULL; | |
334 | mutex_unlock(&rdata->rp_mutex); | |
42e9a92f | 335 | fc_remote_port_delete(rport); |
571f824c | 336 | } |
b4a9c7ed JE |
337 | if (restart) { |
338 | mutex_lock(&rdata->rp_mutex); | |
339 | FC_RPORT_DBG(rdata, "work restart\n"); | |
340 | fc_rport_enter_plogi(rdata); | |
341 | mutex_unlock(&rdata->rp_mutex); | |
342 | } else | |
343 | kref_put(&rdata->kref, lport->tt.rport_destroy); | |
629f4427 JE |
344 | break; |
345 | ||
346 | default: | |
42e9a92f | 347 | mutex_unlock(&rdata->rp_mutex); |
629f4427 JE |
348 | break; |
349 | } | |
42e9a92f RL |
350 | } |
351 | ||
352 | /** | |
34f42a07 | 353 | * fc_rport_login() - Start the remote port login state machine |
3a3b42bf | 354 | * @rdata: The remote port to be logged in to |
42e9a92f RL |
355 | * |
356 | * Locking Note: Called without the rport lock held. This | |
357 | * function will hold the rport lock, call an _enter_* | |
358 | * function and then unlock the rport. | |
370c3bd0 JE |
359 | * |
360 | * This indicates the intent to be logged into the remote port. | |
361 | * If it appears we are already logged in, ADISC is used to verify | |
362 | * the setup. | |
42e9a92f | 363 | */ |
9fb9d328 | 364 | int fc_rport_login(struct fc_rport_priv *rdata) |
42e9a92f | 365 | { |
42e9a92f RL |
366 | mutex_lock(&rdata->rp_mutex); |
367 | ||
370c3bd0 JE |
368 | switch (rdata->rp_state) { |
369 | case RPORT_ST_READY: | |
370 | FC_RPORT_DBG(rdata, "ADISC port\n"); | |
371 | fc_rport_enter_adisc(rdata); | |
372 | break; | |
b4a9c7ed JE |
373 | case RPORT_ST_RESTART: |
374 | break; | |
375 | case RPORT_ST_DELETE: | |
376 | FC_RPORT_DBG(rdata, "Restart deleted port\n"); | |
377 | fc_rport_state_enter(rdata, RPORT_ST_RESTART); | |
378 | break; | |
370c3bd0 JE |
379 | default: |
380 | FC_RPORT_DBG(rdata, "Login to port\n"); | |
381 | fc_rport_enter_plogi(rdata); | |
382 | break; | |
383 | } | |
42e9a92f RL |
384 | mutex_unlock(&rdata->rp_mutex); |
385 | ||
386 | return 0; | |
387 | } | |
388 | ||
5f7ea3b7 | 389 | /** |
3a3b42bf RL |
390 | * fc_rport_enter_delete() - Schedule a remote port to be deleted |
391 | * @rdata: The remote port to be deleted | |
392 | * @event: The event to report as the reason for deletion | |
5f7ea3b7 JE |
393 | * |
394 | * Locking Note: Called with the rport lock held. | |
395 | * | |
396 | * Allow state change into DELETE only once. | |
397 | * | |
398 | * Call queue_work only if there's no event already pending. | |
399 | * Set the new event so that the old pending event will not occur. | |
400 | * Since we have the mutex, even if fc_rport_work() is already started, | |
401 | * it'll see the new event. | |
402 | */ | |
9fb9d328 | 403 | static void fc_rport_enter_delete(struct fc_rport_priv *rdata, |
5f7ea3b7 JE |
404 | enum fc_rport_event event) |
405 | { | |
5f7ea3b7 JE |
406 | if (rdata->rp_state == RPORT_ST_DELETE) |
407 | return; | |
408 | ||
9fb9d328 | 409 | FC_RPORT_DBG(rdata, "Delete port\n"); |
5f7ea3b7 | 410 | |
9fb9d328 | 411 | fc_rport_state_enter(rdata, RPORT_ST_DELETE); |
5f7ea3b7 JE |
412 | |
413 | if (rdata->event == RPORT_EV_NONE) | |
414 | queue_work(rport_event_queue, &rdata->event_work); | |
415 | rdata->event = event; | |
416 | } | |
417 | ||
42e9a92f | 418 | /** |
3a3b42bf RL |
419 | * fc_rport_logoff() - Logoff and remove a remote port |
420 | * @rdata: The remote port to be logged off of | |
42e9a92f RL |
421 | * |
422 | * Locking Note: Called without the rport lock held. This | |
423 | * function will hold the rport lock, call an _enter_* | |
424 | * function and then unlock the rport. | |
425 | */ | |
9fb9d328 | 426 | int fc_rport_logoff(struct fc_rport_priv *rdata) |
42e9a92f | 427 | { |
42e9a92f RL |
428 | mutex_lock(&rdata->rp_mutex); |
429 | ||
9fb9d328 | 430 | FC_RPORT_DBG(rdata, "Remove port\n"); |
42e9a92f | 431 | |
14194054 | 432 | if (rdata->rp_state == RPORT_ST_DELETE) { |
9fb9d328 | 433 | FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n"); |
b4c6f546 AJ |
434 | goto out; |
435 | } | |
436 | ||
b4a9c7ed JE |
437 | if (rdata->rp_state == RPORT_ST_RESTART) |
438 | FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n"); | |
439 | else | |
440 | fc_rport_enter_logo(rdata); | |
42e9a92f RL |
441 | |
442 | /* | |
14194054 | 443 | * Change the state to Delete so that we discard |
42e9a92f RL |
444 | * the response. |
445 | */ | |
9fb9d328 | 446 | fc_rport_enter_delete(rdata, RPORT_EV_STOP); |
b4c6f546 | 447 | out: |
b4a9c7ed | 448 | mutex_unlock(&rdata->rp_mutex); |
42e9a92f RL |
449 | return 0; |
450 | } | |
451 | ||
452 | /** | |
3a3b42bf RL |
453 | * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state |
454 | * @rdata: The remote port that is ready | |
42e9a92f RL |
455 | * |
456 | * Locking Note: The rport lock is expected to be held before calling | |
457 | * this routine. | |
458 | */ | |
9fb9d328 | 459 | static void fc_rport_enter_ready(struct fc_rport_priv *rdata) |
42e9a92f | 460 | { |
9fb9d328 | 461 | fc_rport_state_enter(rdata, RPORT_ST_READY); |
42e9a92f | 462 | |
9fb9d328 | 463 | FC_RPORT_DBG(rdata, "Port is Ready\n"); |
42e9a92f | 464 | |
5f7ea3b7 JE |
465 | if (rdata->event == RPORT_EV_NONE) |
466 | queue_work(rport_event_queue, &rdata->event_work); | |
4c0f62b5 | 467 | rdata->event = RPORT_EV_READY; |
42e9a92f RL |
468 | } |
469 | ||
470 | /** | |
3a3b42bf RL |
471 | * fc_rport_timeout() - Handler for the retry_work timer |
472 | * @work: Handle to the remote port that has timed out | |
42e9a92f RL |
473 | * |
474 | * Locking Note: Called without the rport lock held. This | |
475 | * function will hold the rport lock, call an _enter_* | |
476 | * function and then unlock the rport. | |
477 | */ | |
478 | static void fc_rport_timeout(struct work_struct *work) | |
479 | { | |
ab28f1fd JE |
480 | struct fc_rport_priv *rdata = |
481 | container_of(work, struct fc_rport_priv, retry_work.work); | |
42e9a92f RL |
482 | |
483 | mutex_lock(&rdata->rp_mutex); | |
484 | ||
485 | switch (rdata->rp_state) { | |
486 | case RPORT_ST_PLOGI: | |
9fb9d328 | 487 | fc_rport_enter_plogi(rdata); |
42e9a92f RL |
488 | break; |
489 | case RPORT_ST_PRLI: | |
9fb9d328 | 490 | fc_rport_enter_prli(rdata); |
42e9a92f RL |
491 | break; |
492 | case RPORT_ST_RTV: | |
9fb9d328 | 493 | fc_rport_enter_rtv(rdata); |
42e9a92f RL |
494 | break; |
495 | case RPORT_ST_LOGO: | |
9fb9d328 | 496 | fc_rport_enter_logo(rdata); |
42e9a92f | 497 | break; |
370c3bd0 JE |
498 | case RPORT_ST_ADISC: |
499 | fc_rport_enter_adisc(rdata); | |
500 | break; | |
42e9a92f RL |
501 | case RPORT_ST_READY: |
502 | case RPORT_ST_INIT: | |
14194054 | 503 | case RPORT_ST_DELETE: |
b4a9c7ed | 504 | case RPORT_ST_RESTART: |
42e9a92f RL |
505 | break; |
506 | } | |
507 | ||
508 | mutex_unlock(&rdata->rp_mutex); | |
42e9a92f RL |
509 | } |
510 | ||
511 | /** | |
34f42a07 | 512 | * fc_rport_error() - Error handler, called once retries have been exhausted |
3a3b42bf RL |
513 | * @rdata: The remote port the error is happened on |
514 | * @fp: The error code encapsulated in a frame pointer | |
42e9a92f | 515 | * |
42e9a92f RL |
516 | * Locking Note: The rport lock is expected to be held before |
517 | * calling this routine | |
518 | */ | |
9fb9d328 | 519 | static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp) |
42e9a92f | 520 | { |
9fb9d328 | 521 | FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n", |
cdbe6dfe JE |
522 | IS_ERR(fp) ? -PTR_ERR(fp) : 0, |
523 | fc_rport_state(rdata), rdata->retries); | |
42e9a92f | 524 | |
6755db1c CL |
525 | switch (rdata->rp_state) { |
526 | case RPORT_ST_PLOGI: | |
6755db1c | 527 | case RPORT_ST_LOGO: |
9fb9d328 | 528 | fc_rport_enter_delete(rdata, RPORT_EV_FAILED); |
6755db1c CL |
529 | break; |
530 | case RPORT_ST_RTV: | |
9fb9d328 | 531 | fc_rport_enter_ready(rdata); |
6755db1c | 532 | break; |
370c3bd0 JE |
533 | case RPORT_ST_PRLI: |
534 | case RPORT_ST_ADISC: | |
535 | fc_rport_enter_logo(rdata); | |
536 | break; | |
14194054 | 537 | case RPORT_ST_DELETE: |
b4a9c7ed | 538 | case RPORT_ST_RESTART: |
6755db1c CL |
539 | case RPORT_ST_READY: |
540 | case RPORT_ST_INIT: | |
541 | break; | |
42e9a92f RL |
542 | } |
543 | } | |
544 | ||
6755db1c | 545 | /** |
3a3b42bf RL |
546 | * fc_rport_error_retry() - Handler for remote port state retries |
547 | * @rdata: The remote port whose state is to be retried | |
548 | * @fp: The error code encapsulated in a frame pointer | |
6755db1c CL |
549 | * |
550 | * If the error was an exchange timeout retry immediately, | |
551 | * otherwise wait for E_D_TOV. | |
552 | * | |
553 | * Locking Note: The rport lock is expected to be held before | |
554 | * calling this routine | |
555 | */ | |
9fb9d328 JE |
556 | static void fc_rport_error_retry(struct fc_rport_priv *rdata, |
557 | struct fc_frame *fp) | |
6755db1c | 558 | { |
6755db1c CL |
559 | unsigned long delay = FC_DEF_E_D_TOV; |
560 | ||
561 | /* make sure this isn't an FC_EX_CLOSED error, never retry those */ | |
562 | if (PTR_ERR(fp) == -FC_EX_CLOSED) | |
9fb9d328 | 563 | return fc_rport_error(rdata, fp); |
6755db1c | 564 | |
a3666955 | 565 | if (rdata->retries < rdata->local_port->max_rport_retry_count) { |
9fb9d328 JE |
566 | FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n", |
567 | PTR_ERR(fp), fc_rport_state(rdata)); | |
6755db1c CL |
568 | rdata->retries++; |
569 | /* no additional delay on exchange timeouts */ | |
570 | if (PTR_ERR(fp) == -FC_EX_TIMEOUT) | |
571 | delay = 0; | |
6755db1c CL |
572 | schedule_delayed_work(&rdata->retry_work, delay); |
573 | return; | |
574 | } | |
575 | ||
9fb9d328 | 576 | return fc_rport_error(rdata, fp); |
6755db1c CL |
577 | } |
578 | ||
42e9a92f | 579 | /** |
3a3b42bf RL |
580 | * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses |
581 | * @sp: The sequence the PLOGI is on | |
582 | * @fp: The PLOGI response frame | |
583 | * @rdata_arg: The remote port that sent the PLOGI response | |
42e9a92f RL |
584 | * |
585 | * Locking Note: This function will be called without the rport lock | |
586 | * held, but it will lock, call an _enter_* function or fc_rport_error | |
587 | * and then unlock the rport. | |
588 | */ | |
589 | static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp, | |
9fb9d328 | 590 | void *rdata_arg) |
42e9a92f | 591 | { |
9fb9d328 | 592 | struct fc_rport_priv *rdata = rdata_arg; |
42e9a92f | 593 | struct fc_lport *lport = rdata->local_port; |
a29e7646 | 594 | struct fc_els_flogi *plp = NULL; |
42e9a92f RL |
595 | unsigned int tov; |
596 | u16 csp_seq; | |
597 | u16 cssp_seq; | |
598 | u8 op; | |
599 | ||
600 | mutex_lock(&rdata->rp_mutex); | |
601 | ||
f657d299 | 602 | FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp)); |
42e9a92f RL |
603 | |
604 | if (rdata->rp_state != RPORT_ST_PLOGI) { | |
9fb9d328 JE |
605 | FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state " |
606 | "%s\n", fc_rport_state(rdata)); | |
76f6804e AJ |
607 | if (IS_ERR(fp)) |
608 | goto err; | |
42e9a92f RL |
609 | goto out; |
610 | } | |
611 | ||
76f6804e | 612 | if (IS_ERR(fp)) { |
9fb9d328 | 613 | fc_rport_error_retry(rdata, fp); |
76f6804e AJ |
614 | goto err; |
615 | } | |
616 | ||
42e9a92f RL |
617 | op = fc_frame_payload_op(fp); |
618 | if (op == ELS_LS_ACC && | |
619 | (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) { | |
f211fa51 JE |
620 | rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn); |
621 | rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn); | |
42e9a92f RL |
622 | |
623 | tov = ntohl(plp->fl_csp.sp_e_d_tov); | |
624 | if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR) | |
625 | tov /= 1000; | |
626 | if (tov > rdata->e_d_tov) | |
627 | rdata->e_d_tov = tov; | |
628 | csp_seq = ntohs(plp->fl_csp.sp_tot_seq); | |
629 | cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq); | |
630 | if (cssp_seq < csp_seq) | |
631 | csp_seq = cssp_seq; | |
632 | rdata->max_seq = csp_seq; | |
f211fa51 | 633 | rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs); |
3ac6f98f | 634 | fc_rport_enter_prli(rdata); |
42e9a92f | 635 | } else |
9fb9d328 | 636 | fc_rport_error_retry(rdata, fp); |
42e9a92f RL |
637 | |
638 | out: | |
639 | fc_frame_free(fp); | |
640 | err: | |
641 | mutex_unlock(&rdata->rp_mutex); | |
f211fa51 | 642 | kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy); |
42e9a92f RL |
643 | } |
644 | ||
645 | /** | |
3a3b42bf RL |
646 | * fc_rport_enter_plogi() - Send Port Login (PLOGI) request |
647 | * @rdata: The remote port to send a PLOGI to | |
42e9a92f RL |
648 | * |
649 | * Locking Note: The rport lock is expected to be held before calling | |
650 | * this routine. | |
651 | */ | |
9fb9d328 | 652 | static void fc_rport_enter_plogi(struct fc_rport_priv *rdata) |
42e9a92f | 653 | { |
42e9a92f RL |
654 | struct fc_lport *lport = rdata->local_port; |
655 | struct fc_frame *fp; | |
656 | ||
9fb9d328 JE |
657 | FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n", |
658 | fc_rport_state(rdata)); | |
42e9a92f | 659 | |
9fb9d328 | 660 | fc_rport_state_enter(rdata, RPORT_ST_PLOGI); |
42e9a92f | 661 | |
f211fa51 | 662 | rdata->maxframe_size = FC_MIN_MAX_PAYLOAD; |
42e9a92f RL |
663 | fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi)); |
664 | if (!fp) { | |
9fb9d328 | 665 | fc_rport_error_retry(rdata, fp); |
42e9a92f RL |
666 | return; |
667 | } | |
668 | rdata->e_d_tov = lport->e_d_tov; | |
669 | ||
f211fa51 | 670 | if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI, |
9fb9d328 | 671 | fc_rport_plogi_resp, rdata, lport->e_d_tov)) |
8f550f93 | 672 | fc_rport_error_retry(rdata, NULL); |
42e9a92f | 673 | else |
f211fa51 | 674 | kref_get(&rdata->kref); |
42e9a92f RL |
675 | } |
676 | ||
677 | /** | |
34f42a07 | 678 | * fc_rport_prli_resp() - Process Login (PRLI) response handler |
3a3b42bf RL |
679 | * @sp: The sequence the PRLI response was on |
680 | * @fp: The PRLI response frame | |
681 | * @rdata_arg: The remote port that sent the PRLI response | |
42e9a92f RL |
682 | * |
683 | * Locking Note: This function will be called without the rport lock | |
684 | * held, but it will lock, call an _enter_* function or fc_rport_error | |
685 | * and then unlock the rport. | |
686 | */ | |
687 | static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp, | |
9fb9d328 | 688 | void *rdata_arg) |
42e9a92f | 689 | { |
9fb9d328 | 690 | struct fc_rport_priv *rdata = rdata_arg; |
42e9a92f RL |
691 | struct { |
692 | struct fc_els_prli prli; | |
693 | struct fc_els_spp spp; | |
694 | } *pp; | |
695 | u32 roles = FC_RPORT_ROLE_UNKNOWN; | |
696 | u32 fcp_parm = 0; | |
697 | u8 op; | |
698 | ||
699 | mutex_lock(&rdata->rp_mutex); | |
700 | ||
f657d299 | 701 | FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp)); |
42e9a92f RL |
702 | |
703 | if (rdata->rp_state != RPORT_ST_PRLI) { | |
9fb9d328 JE |
704 | FC_RPORT_DBG(rdata, "Received a PRLI response, but in state " |
705 | "%s\n", fc_rport_state(rdata)); | |
76f6804e AJ |
706 | if (IS_ERR(fp)) |
707 | goto err; | |
42e9a92f RL |
708 | goto out; |
709 | } | |
710 | ||
76f6804e | 711 | if (IS_ERR(fp)) { |
9fb9d328 | 712 | fc_rport_error_retry(rdata, fp); |
76f6804e AJ |
713 | goto err; |
714 | } | |
715 | ||
6bd054cb RL |
716 | /* reinitialize remote port roles */ |
717 | rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN; | |
718 | ||
42e9a92f RL |
719 | op = fc_frame_payload_op(fp); |
720 | if (op == ELS_LS_ACC) { | |
721 | pp = fc_frame_payload_get(fp, sizeof(*pp)); | |
722 | if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) { | |
723 | fcp_parm = ntohl(pp->spp.spp_params); | |
724 | if (fcp_parm & FCP_SPPF_RETRY) | |
725 | rdata->flags |= FC_RP_FLAGS_RETRY; | |
726 | } | |
727 | ||
f211fa51 | 728 | rdata->supported_classes = FC_COS_CLASS3; |
42e9a92f RL |
729 | if (fcp_parm & FCP_SPPF_INIT_FCN) |
730 | roles |= FC_RPORT_ROLE_FCP_INITIATOR; | |
731 | if (fcp_parm & FCP_SPPF_TARG_FCN) | |
732 | roles |= FC_RPORT_ROLE_FCP_TARGET; | |
733 | ||
f211fa51 | 734 | rdata->ids.roles = roles; |
9fb9d328 | 735 | fc_rport_enter_rtv(rdata); |
42e9a92f RL |
736 | |
737 | } else { | |
9fb9d328 JE |
738 | FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n"); |
739 | fc_rport_enter_delete(rdata, RPORT_EV_FAILED); | |
42e9a92f RL |
740 | } |
741 | ||
742 | out: | |
743 | fc_frame_free(fp); | |
744 | err: | |
745 | mutex_unlock(&rdata->rp_mutex); | |
f211fa51 | 746 | kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy); |
42e9a92f RL |
747 | } |
748 | ||
749 | /** | |
3a3b42bf RL |
750 | * fc_rport_logo_resp() - Handler for logout (LOGO) responses |
751 | * @sp: The sequence the LOGO was on | |
752 | * @fp: The LOGO response frame | |
753 | * @rdata_arg: The remote port that sent the LOGO response | |
42e9a92f RL |
754 | * |
755 | * Locking Note: This function will be called without the rport lock | |
756 | * held, but it will lock, call an _enter_* function or fc_rport_error | |
757 | * and then unlock the rport. | |
758 | */ | |
759 | static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp, | |
9fb9d328 | 760 | void *rdata_arg) |
42e9a92f | 761 | { |
9fb9d328 | 762 | struct fc_rport_priv *rdata = rdata_arg; |
42e9a92f RL |
763 | u8 op; |
764 | ||
765 | mutex_lock(&rdata->rp_mutex); | |
766 | ||
f657d299 | 767 | FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp)); |
42e9a92f | 768 | |
42e9a92f | 769 | if (rdata->rp_state != RPORT_ST_LOGO) { |
9fb9d328 JE |
770 | FC_RPORT_DBG(rdata, "Received a LOGO response, but in state " |
771 | "%s\n", fc_rport_state(rdata)); | |
76f6804e AJ |
772 | if (IS_ERR(fp)) |
773 | goto err; | |
42e9a92f RL |
774 | goto out; |
775 | } | |
776 | ||
76f6804e | 777 | if (IS_ERR(fp)) { |
9fb9d328 | 778 | fc_rport_error_retry(rdata, fp); |
76f6804e AJ |
779 | goto err; |
780 | } | |
781 | ||
42e9a92f | 782 | op = fc_frame_payload_op(fp); |
68a1750b JE |
783 | if (op != ELS_LS_ACC) |
784 | FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n", | |
785 | op); | |
786 | fc_rport_enter_delete(rdata, RPORT_EV_LOGO); | |
42e9a92f RL |
787 | |
788 | out: | |
789 | fc_frame_free(fp); | |
790 | err: | |
791 | mutex_unlock(&rdata->rp_mutex); | |
f211fa51 | 792 | kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy); |
42e9a92f RL |
793 | } |
794 | ||
795 | /** | |
3a3b42bf RL |
796 | * fc_rport_enter_prli() - Send Process Login (PRLI) request |
797 | * @rdata: The remote port to send the PRLI request to | |
42e9a92f RL |
798 | * |
799 | * Locking Note: The rport lock is expected to be held before calling | |
800 | * this routine. | |
801 | */ | |
9fb9d328 | 802 | static void fc_rport_enter_prli(struct fc_rport_priv *rdata) |
42e9a92f | 803 | { |
42e9a92f RL |
804 | struct fc_lport *lport = rdata->local_port; |
805 | struct { | |
806 | struct fc_els_prli prli; | |
807 | struct fc_els_spp spp; | |
808 | } *pp; | |
809 | struct fc_frame *fp; | |
810 | ||
3ac6f98f JE |
811 | /* |
812 | * If the rport is one of the well known addresses | |
813 | * we skip PRLI and RTV and go straight to READY. | |
814 | */ | |
815 | if (rdata->ids.port_id >= FC_FID_DOM_MGR) { | |
816 | fc_rport_enter_ready(rdata); | |
817 | return; | |
818 | } | |
819 | ||
9fb9d328 JE |
820 | FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n", |
821 | fc_rport_state(rdata)); | |
42e9a92f | 822 | |
9fb9d328 | 823 | fc_rport_state_enter(rdata, RPORT_ST_PRLI); |
42e9a92f RL |
824 | |
825 | fp = fc_frame_alloc(lport, sizeof(*pp)); | |
826 | if (!fp) { | |
9fb9d328 | 827 | fc_rport_error_retry(rdata, fp); |
42e9a92f RL |
828 | return; |
829 | } | |
830 | ||
f211fa51 | 831 | if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI, |
9fb9d328 | 832 | fc_rport_prli_resp, rdata, lport->e_d_tov)) |
8f550f93 | 833 | fc_rport_error_retry(rdata, NULL); |
42e9a92f | 834 | else |
f211fa51 | 835 | kref_get(&rdata->kref); |
42e9a92f RL |
836 | } |
837 | ||
838 | /** | |
3a3b42bf RL |
839 | * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses |
840 | * @sp: The sequence the RTV was on | |
841 | * @fp: The RTV response frame | |
842 | * @rdata_arg: The remote port that sent the RTV response | |
42e9a92f RL |
843 | * |
844 | * Many targets don't seem to support this. | |
845 | * | |
846 | * Locking Note: This function will be called without the rport lock | |
847 | * held, but it will lock, call an _enter_* function or fc_rport_error | |
848 | * and then unlock the rport. | |
849 | */ | |
850 | static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp, | |
9fb9d328 | 851 | void *rdata_arg) |
42e9a92f | 852 | { |
9fb9d328 | 853 | struct fc_rport_priv *rdata = rdata_arg; |
42e9a92f RL |
854 | u8 op; |
855 | ||
856 | mutex_lock(&rdata->rp_mutex); | |
857 | ||
f657d299 | 858 | FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp)); |
42e9a92f RL |
859 | |
860 | if (rdata->rp_state != RPORT_ST_RTV) { | |
9fb9d328 JE |
861 | FC_RPORT_DBG(rdata, "Received a RTV response, but in state " |
862 | "%s\n", fc_rport_state(rdata)); | |
76f6804e AJ |
863 | if (IS_ERR(fp)) |
864 | goto err; | |
42e9a92f RL |
865 | goto out; |
866 | } | |
867 | ||
76f6804e | 868 | if (IS_ERR(fp)) { |
9fb9d328 | 869 | fc_rport_error(rdata, fp); |
76f6804e AJ |
870 | goto err; |
871 | } | |
872 | ||
42e9a92f RL |
873 | op = fc_frame_payload_op(fp); |
874 | if (op == ELS_LS_ACC) { | |
875 | struct fc_els_rtv_acc *rtv; | |
876 | u32 toq; | |
877 | u32 tov; | |
878 | ||
879 | rtv = fc_frame_payload_get(fp, sizeof(*rtv)); | |
880 | if (rtv) { | |
881 | toq = ntohl(rtv->rtv_toq); | |
882 | tov = ntohl(rtv->rtv_r_a_tov); | |
883 | if (tov == 0) | |
884 | tov = 1; | |
885 | rdata->r_a_tov = tov; | |
886 | tov = ntohl(rtv->rtv_e_d_tov); | |
887 | if (toq & FC_ELS_RTV_EDRES) | |
888 | tov /= 1000000; | |
889 | if (tov == 0) | |
890 | tov = 1; | |
891 | rdata->e_d_tov = tov; | |
892 | } | |
893 | } | |
894 | ||
9fb9d328 | 895 | fc_rport_enter_ready(rdata); |
42e9a92f RL |
896 | |
897 | out: | |
898 | fc_frame_free(fp); | |
899 | err: | |
900 | mutex_unlock(&rdata->rp_mutex); | |
f211fa51 | 901 | kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy); |
42e9a92f RL |
902 | } |
903 | ||
904 | /** | |
3a3b42bf RL |
905 | * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request |
906 | * @rdata: The remote port to send the RTV request to | |
42e9a92f RL |
907 | * |
908 | * Locking Note: The rport lock is expected to be held before calling | |
909 | * this routine. | |
910 | */ | |
9fb9d328 | 911 | static void fc_rport_enter_rtv(struct fc_rport_priv *rdata) |
42e9a92f RL |
912 | { |
913 | struct fc_frame *fp; | |
42e9a92f RL |
914 | struct fc_lport *lport = rdata->local_port; |
915 | ||
9fb9d328 JE |
916 | FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n", |
917 | fc_rport_state(rdata)); | |
42e9a92f | 918 | |
9fb9d328 | 919 | fc_rport_state_enter(rdata, RPORT_ST_RTV); |
42e9a92f RL |
920 | |
921 | fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv)); | |
922 | if (!fp) { | |
9fb9d328 | 923 | fc_rport_error_retry(rdata, fp); |
42e9a92f RL |
924 | return; |
925 | } | |
926 | ||
f211fa51 | 927 | if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV, |
3a3b42bf | 928 | fc_rport_rtv_resp, rdata, lport->e_d_tov)) |
8f550f93 | 929 | fc_rport_error_retry(rdata, NULL); |
42e9a92f | 930 | else |
f211fa51 | 931 | kref_get(&rdata->kref); |
42e9a92f RL |
932 | } |
933 | ||
934 | /** | |
3a3b42bf RL |
935 | * fc_rport_enter_logo() - Send a logout (LOGO) request |
936 | * @rdata: The remote port to send the LOGO request to | |
42e9a92f RL |
937 | * |
938 | * Locking Note: The rport lock is expected to be held before calling | |
939 | * this routine. | |
940 | */ | |
9fb9d328 | 941 | static void fc_rport_enter_logo(struct fc_rport_priv *rdata) |
42e9a92f | 942 | { |
42e9a92f RL |
943 | struct fc_lport *lport = rdata->local_port; |
944 | struct fc_frame *fp; | |
945 | ||
9fb9d328 JE |
946 | FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n", |
947 | fc_rport_state(rdata)); | |
42e9a92f | 948 | |
9fb9d328 | 949 | fc_rport_state_enter(rdata, RPORT_ST_LOGO); |
42e9a92f RL |
950 | |
951 | fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo)); | |
952 | if (!fp) { | |
9fb9d328 | 953 | fc_rport_error_retry(rdata, fp); |
42e9a92f RL |
954 | return; |
955 | } | |
956 | ||
f211fa51 | 957 | if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO, |
9fb9d328 | 958 | fc_rport_logo_resp, rdata, lport->e_d_tov)) |
8f550f93 | 959 | fc_rport_error_retry(rdata, NULL); |
42e9a92f | 960 | else |
f211fa51 | 961 | kref_get(&rdata->kref); |
42e9a92f RL |
962 | } |
963 | ||
370c3bd0 | 964 | /** |
3a3b42bf RL |
965 | * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses |
966 | * @sp: The sequence the ADISC response was on | |
967 | * @fp: The ADISC response frame | |
968 | * @rdata_arg: The remote port that sent the ADISC response | |
370c3bd0 JE |
969 | * |
970 | * Locking Note: This function will be called without the rport lock | |
971 | * held, but it will lock, call an _enter_* function or fc_rport_error | |
972 | * and then unlock the rport. | |
973 | */ | |
974 | static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp, | |
3a3b42bf | 975 | void *rdata_arg) |
370c3bd0 JE |
976 | { |
977 | struct fc_rport_priv *rdata = rdata_arg; | |
978 | struct fc_els_adisc *adisc; | |
979 | u8 op; | |
980 | ||
981 | mutex_lock(&rdata->rp_mutex); | |
982 | ||
983 | FC_RPORT_DBG(rdata, "Received a ADISC response\n"); | |
984 | ||
985 | if (rdata->rp_state != RPORT_ST_ADISC) { | |
986 | FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n", | |
987 | fc_rport_state(rdata)); | |
988 | if (IS_ERR(fp)) | |
989 | goto err; | |
990 | goto out; | |
991 | } | |
992 | ||
993 | if (IS_ERR(fp)) { | |
994 | fc_rport_error(rdata, fp); | |
995 | goto err; | |
996 | } | |
997 | ||
998 | /* | |
999 | * If address verification failed. Consider us logged out of the rport. | |
1000 | * Since the rport is still in discovery, we want to be | |
1001 | * logged in, so go to PLOGI state. Otherwise, go back to READY. | |
1002 | */ | |
1003 | op = fc_frame_payload_op(fp); | |
1004 | adisc = fc_frame_payload_get(fp, sizeof(*adisc)); | |
1005 | if (op != ELS_LS_ACC || !adisc || | |
1006 | ntoh24(adisc->adisc_port_id) != rdata->ids.port_id || | |
1007 | get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name || | |
1008 | get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) { | |
1009 | FC_RPORT_DBG(rdata, "ADISC error or mismatch\n"); | |
1010 | fc_rport_enter_plogi(rdata); | |
1011 | } else { | |
1012 | FC_RPORT_DBG(rdata, "ADISC OK\n"); | |
1013 | fc_rport_enter_ready(rdata); | |
1014 | } | |
1015 | out: | |
1016 | fc_frame_free(fp); | |
1017 | err: | |
1018 | mutex_unlock(&rdata->rp_mutex); | |
1019 | kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy); | |
1020 | } | |
1021 | ||
1022 | /** | |
3a3b42bf RL |
1023 | * fc_rport_enter_adisc() - Send Address Discover (ADISC) request |
1024 | * @rdata: The remote port to send the ADISC request to | |
370c3bd0 JE |
1025 | * |
1026 | * Locking Note: The rport lock is expected to be held before calling | |
1027 | * this routine. | |
1028 | */ | |
1029 | static void fc_rport_enter_adisc(struct fc_rport_priv *rdata) | |
1030 | { | |
1031 | struct fc_lport *lport = rdata->local_port; | |
1032 | struct fc_frame *fp; | |
1033 | ||
1034 | FC_RPORT_DBG(rdata, "sending ADISC from %s state\n", | |
1035 | fc_rport_state(rdata)); | |
1036 | ||
1037 | fc_rport_state_enter(rdata, RPORT_ST_ADISC); | |
1038 | ||
1039 | fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc)); | |
1040 | if (!fp) { | |
1041 | fc_rport_error_retry(rdata, fp); | |
1042 | return; | |
1043 | } | |
1044 | if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC, | |
1045 | fc_rport_adisc_resp, rdata, lport->e_d_tov)) | |
8f550f93 | 1046 | fc_rport_error_retry(rdata, NULL); |
370c3bd0 JE |
1047 | else |
1048 | kref_get(&rdata->kref); | |
1049 | } | |
1050 | ||
8abbe3a4 | 1051 | /** |
3a3b42bf RL |
1052 | * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests |
1053 | * @rdata: The remote port that sent the ADISC request | |
1054 | * @sp: The sequence the ADISC request was on | |
1055 | * @in_fp: The ADISC request frame | |
8abbe3a4 JE |
1056 | * |
1057 | * Locking Note: Called with the lport and rport locks held. | |
1058 | */ | |
1059 | static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata, | |
1060 | struct fc_seq *sp, struct fc_frame *in_fp) | |
1061 | { | |
1062 | struct fc_lport *lport = rdata->local_port; | |
1063 | struct fc_frame *fp; | |
1064 | struct fc_exch *ep = fc_seq_exch(sp); | |
1065 | struct fc_els_adisc *adisc; | |
1066 | struct fc_seq_els_data rjt_data; | |
1067 | u32 f_ctl; | |
1068 | ||
1069 | FC_RPORT_DBG(rdata, "Received ADISC request\n"); | |
1070 | ||
1071 | adisc = fc_frame_payload_get(in_fp, sizeof(*adisc)); | |
1072 | if (!adisc) { | |
1073 | rjt_data.fp = NULL; | |
1074 | rjt_data.reason = ELS_RJT_PROT; | |
1075 | rjt_data.explan = ELS_EXPL_INV_LEN; | |
1076 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); | |
1077 | goto drop; | |
1078 | } | |
1079 | ||
1080 | fp = fc_frame_alloc(lport, sizeof(*adisc)); | |
1081 | if (!fp) | |
1082 | goto drop; | |
1083 | fc_adisc_fill(lport, fp); | |
1084 | adisc = fc_frame_payload_get(fp, sizeof(*adisc)); | |
1085 | adisc->adisc_cmd = ELS_LS_ACC; | |
1086 | sp = lport->tt.seq_start_next(sp); | |
1087 | f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT; | |
1088 | fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, | |
1089 | FC_TYPE_ELS, f_ctl, 0); | |
1090 | lport->tt.seq_send(lport, sp, fp); | |
1091 | drop: | |
1092 | fc_frame_free(in_fp); | |
1093 | } | |
1094 | ||
42e9a92f | 1095 | /** |
3a3b42bf RL |
1096 | * fc_rport_recv_els_req() - Handler for validated ELS requests |
1097 | * @lport: The local port that received the ELS request | |
1098 | * @sp: The sequence that the ELS request was on | |
1099 | * @fp: The ELS request frame | |
83fe6a93 JE |
1100 | * |
1101 | * Handle incoming ELS requests that require port login. | |
1102 | * The ELS opcode has already been validated by the caller. | |
42e9a92f | 1103 | * |
131203a1 | 1104 | * Locking Note: Called with the lport lock held. |
42e9a92f | 1105 | */ |
83fe6a93 JE |
1106 | static void fc_rport_recv_els_req(struct fc_lport *lport, |
1107 | struct fc_seq *sp, struct fc_frame *fp) | |
42e9a92f | 1108 | { |
131203a1 | 1109 | struct fc_rport_priv *rdata; |
42e9a92f RL |
1110 | struct fc_frame_header *fh; |
1111 | struct fc_seq_els_data els_data; | |
42e9a92f | 1112 | |
42e9a92f | 1113 | els_data.fp = NULL; |
83fe6a93 JE |
1114 | els_data.reason = ELS_RJT_UNAB; |
1115 | els_data.explan = ELS_EXPL_PLOGI_REQD; | |
3ac6f98f | 1116 | |
42e9a92f RL |
1117 | fh = fc_frame_header_get(fp); |
1118 | ||
25b37b98 | 1119 | mutex_lock(&lport->disc.disc_mutex); |
83fe6a93 | 1120 | rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id)); |
131203a1 | 1121 | if (!rdata) { |
25b37b98 | 1122 | mutex_unlock(&lport->disc.disc_mutex); |
83fe6a93 | 1123 | goto reject; |
131203a1 JE |
1124 | } |
1125 | mutex_lock(&rdata->rp_mutex); | |
25b37b98 | 1126 | mutex_unlock(&lport->disc.disc_mutex); |
131203a1 | 1127 | |
83fe6a93 JE |
1128 | switch (rdata->rp_state) { |
1129 | case RPORT_ST_PRLI: | |
1130 | case RPORT_ST_RTV: | |
1131 | case RPORT_ST_READY: | |
370c3bd0 | 1132 | case RPORT_ST_ADISC: |
83fe6a93 JE |
1133 | break; |
1134 | default: | |
1135 | mutex_unlock(&rdata->rp_mutex); | |
1136 | goto reject; | |
1137 | } | |
1138 | ||
1139 | switch (fc_frame_payload_op(fp)) { | |
131203a1 JE |
1140 | case ELS_PRLI: |
1141 | fc_rport_recv_prli_req(rdata, sp, fp); | |
1142 | break; | |
1143 | case ELS_PRLO: | |
1144 | fc_rport_recv_prlo_req(rdata, sp, fp); | |
1145 | break; | |
8abbe3a4 JE |
1146 | case ELS_ADISC: |
1147 | fc_rport_recv_adisc_req(rdata, sp, fp); | |
1148 | break; | |
131203a1 JE |
1149 | case ELS_RRQ: |
1150 | els_data.fp = fp; | |
1151 | lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data); | |
1152 | break; | |
1153 | case ELS_REC: | |
1154 | els_data.fp = fp; | |
1155 | lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data); | |
1156 | break; | |
1157 | default: | |
83fe6a93 | 1158 | fc_frame_free(fp); /* can't happen */ |
131203a1 | 1159 | break; |
42e9a92f RL |
1160 | } |
1161 | ||
1162 | mutex_unlock(&rdata->rp_mutex); | |
83fe6a93 JE |
1163 | return; |
1164 | ||
1165 | reject: | |
1166 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data); | |
1167 | fc_frame_free(fp); | |
1168 | } | |
1169 | ||
1170 | /** | |
3a3b42bf RL |
1171 | * fc_rport_recv_req() - Handler for requests |
1172 | * @sp: The sequence the request was on | |
1173 | * @fp: The request frame | |
1174 | * @lport: The local port that received the request | |
83fe6a93 JE |
1175 | * |
1176 | * Locking Note: Called with the lport lock held. | |
1177 | */ | |
1178 | void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp, | |
1179 | struct fc_lport *lport) | |
1180 | { | |
1181 | struct fc_seq_els_data els_data; | |
1182 | ||
1183 | /* | |
1184 | * Handle PLOGI and LOGO requests separately, since they | |
1185 | * don't require prior login. | |
1186 | * Check for unsupported opcodes first and reject them. | |
1187 | * For some ops, it would be incorrect to reject with "PLOGI required". | |
1188 | */ | |
1189 | switch (fc_frame_payload_op(fp)) { | |
1190 | case ELS_PLOGI: | |
1191 | fc_rport_recv_plogi_req(lport, sp, fp); | |
1192 | break; | |
1193 | case ELS_LOGO: | |
1194 | fc_rport_recv_logo_req(lport, sp, fp); | |
1195 | break; | |
1196 | case ELS_PRLI: | |
1197 | case ELS_PRLO: | |
8abbe3a4 | 1198 | case ELS_ADISC: |
83fe6a93 JE |
1199 | case ELS_RRQ: |
1200 | case ELS_REC: | |
1201 | fc_rport_recv_els_req(lport, sp, fp); | |
1202 | break; | |
1203 | default: | |
1204 | fc_frame_free(fp); | |
1205 | els_data.fp = NULL; | |
1206 | els_data.reason = ELS_RJT_UNSUP; | |
1207 | els_data.explan = ELS_EXPL_NONE; | |
1208 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data); | |
1209 | break; | |
1210 | } | |
42e9a92f RL |
1211 | } |
1212 | ||
1213 | /** | |
3a3b42bf RL |
1214 | * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests |
1215 | * @lport: The local port that received the PLOGI request | |
1216 | * @sp: The sequence that the PLOGI request was on | |
1217 | * @rx_fp: The PLOGI request frame | |
42e9a92f | 1218 | * |
3ac6f98f | 1219 | * Locking Note: The rport lock is held before calling this function. |
42e9a92f | 1220 | */ |
3ac6f98f | 1221 | static void fc_rport_recv_plogi_req(struct fc_lport *lport, |
42e9a92f RL |
1222 | struct fc_seq *sp, struct fc_frame *rx_fp) |
1223 | { | |
3ac6f98f JE |
1224 | struct fc_disc *disc; |
1225 | struct fc_rport_priv *rdata; | |
42e9a92f RL |
1226 | struct fc_frame *fp = rx_fp; |
1227 | struct fc_exch *ep; | |
1228 | struct fc_frame_header *fh; | |
1229 | struct fc_els_flogi *pl; | |
1230 | struct fc_seq_els_data rjt_data; | |
3ac6f98f | 1231 | u32 sid, f_ctl; |
42e9a92f | 1232 | |
3ac6f98f | 1233 | rjt_data.fp = NULL; |
42e9a92f | 1234 | fh = fc_frame_header_get(fp); |
3ac6f98f | 1235 | sid = ntoh24(fh->fh_s_id); |
42e9a92f | 1236 | |
3ac6f98f | 1237 | FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n"); |
42e9a92f | 1238 | |
42e9a92f RL |
1239 | pl = fc_frame_payload_get(fp, sizeof(*pl)); |
1240 | if (!pl) { | |
3ac6f98f JE |
1241 | FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n"); |
1242 | rjt_data.reason = ELS_RJT_PROT; | |
1243 | rjt_data.explan = ELS_EXPL_INV_LEN; | |
1244 | goto reject; | |
42e9a92f | 1245 | } |
3ac6f98f JE |
1246 | |
1247 | disc = &lport->disc; | |
1248 | mutex_lock(&disc->disc_mutex); | |
1249 | rdata = lport->tt.rport_create(lport, sid); | |
1250 | if (!rdata) { | |
1251 | mutex_unlock(&disc->disc_mutex); | |
1252 | rjt_data.reason = ELS_RJT_UNAB; | |
1253 | rjt_data.explan = ELS_EXPL_INSUF_RES; | |
1254 | goto reject; | |
1255 | } | |
1256 | ||
1257 | mutex_lock(&rdata->rp_mutex); | |
1258 | mutex_unlock(&disc->disc_mutex); | |
1259 | ||
1260 | rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn); | |
1261 | rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn); | |
42e9a92f RL |
1262 | |
1263 | /* | |
3ac6f98f | 1264 | * If the rport was just created, possibly due to the incoming PLOGI, |
42e9a92f RL |
1265 | * set the state appropriately and accept the PLOGI. |
1266 | * | |
1267 | * If we had also sent a PLOGI, and if the received PLOGI is from a | |
1268 | * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason | |
1269 | * "command already in progress". | |
1270 | * | |
1271 | * XXX TBD: If the session was ready before, the PLOGI should result in | |
1272 | * all outstanding exchanges being reset. | |
1273 | */ | |
1274 | switch (rdata->rp_state) { | |
1275 | case RPORT_ST_INIT: | |
3ac6f98f | 1276 | FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n"); |
42e9a92f RL |
1277 | break; |
1278 | case RPORT_ST_PLOGI: | |
3ac6f98f JE |
1279 | FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n"); |
1280 | if (rdata->ids.port_name < lport->wwpn) { | |
1281 | mutex_unlock(&rdata->rp_mutex); | |
1282 | rjt_data.reason = ELS_RJT_INPROG; | |
1283 | rjt_data.explan = ELS_EXPL_NONE; | |
1284 | goto reject; | |
1285 | } | |
42e9a92f RL |
1286 | break; |
1287 | case RPORT_ST_PRLI: | |
b4a9c7ed | 1288 | case RPORT_ST_RTV: |
42e9a92f | 1289 | case RPORT_ST_READY: |
370c3bd0 JE |
1290 | case RPORT_ST_ADISC: |
1291 | FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d " | |
1292 | "- ignored for now\n", rdata->rp_state); | |
1293 | /* XXX TBD - should reset */ | |
42e9a92f | 1294 | break; |
14194054 | 1295 | case RPORT_ST_DELETE: |
b4a9c7ed JE |
1296 | case RPORT_ST_LOGO: |
1297 | case RPORT_ST_RESTART: | |
1298 | FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n", | |
1299 | fc_rport_state(rdata)); | |
1300 | mutex_unlock(&rdata->rp_mutex); | |
1301 | rjt_data.reason = ELS_RJT_BUSY; | |
1302 | rjt_data.explan = ELS_EXPL_NONE; | |
1303 | goto reject; | |
42e9a92f RL |
1304 | } |
1305 | ||
3ac6f98f JE |
1306 | /* |
1307 | * Get session payload size from incoming PLOGI. | |
1308 | */ | |
1309 | rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs); | |
1310 | fc_frame_free(rx_fp); | |
1311 | ||
1312 | /* | |
1313 | * Send LS_ACC. If this fails, the originator should retry. | |
1314 | */ | |
1315 | sp = lport->tt.seq_start_next(sp); | |
1316 | if (!sp) | |
1317 | goto out; | |
1318 | fp = fc_frame_alloc(lport, sizeof(*pl)); | |
1319 | if (!fp) | |
1320 | goto out; | |
1321 | ||
1322 | fc_plogi_fill(lport, fp, ELS_LS_ACC); | |
1323 | f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT; | |
1324 | ep = fc_seq_exch(sp); | |
1325 | fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, | |
1326 | FC_TYPE_ELS, f_ctl, 0); | |
1327 | lport->tt.seq_send(lport, sp, fp); | |
1328 | fc_rport_enter_prli(rdata); | |
1329 | out: | |
1330 | mutex_unlock(&rdata->rp_mutex); | |
1331 | return; | |
1332 | ||
1333 | reject: | |
1334 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); | |
1335 | fc_frame_free(fp); | |
42e9a92f RL |
1336 | } |
1337 | ||
1338 | /** | |
3a3b42bf RL |
1339 | * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests |
1340 | * @rdata: The remote port that sent the PRLI request | |
1341 | * @sp: The sequence that the PRLI was on | |
1342 | * @rx_fp: The PRLI request frame | |
42e9a92f RL |
1343 | * |
1344 | * Locking Note: The rport lock is exected to be held before calling | |
1345 | * this function. | |
1346 | */ | |
9fb9d328 | 1347 | static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata, |
42e9a92f RL |
1348 | struct fc_seq *sp, struct fc_frame *rx_fp) |
1349 | { | |
42e9a92f RL |
1350 | struct fc_lport *lport = rdata->local_port; |
1351 | struct fc_exch *ep; | |
1352 | struct fc_frame *fp; | |
1353 | struct fc_frame_header *fh; | |
1354 | struct { | |
1355 | struct fc_els_prli prli; | |
1356 | struct fc_els_spp spp; | |
1357 | } *pp; | |
1358 | struct fc_els_spp *rspp; /* request service param page */ | |
1359 | struct fc_els_spp *spp; /* response spp */ | |
1360 | unsigned int len; | |
1361 | unsigned int plen; | |
1362 | enum fc_els_rjt_reason reason = ELS_RJT_UNAB; | |
1363 | enum fc_els_rjt_explan explan = ELS_EXPL_NONE; | |
1364 | enum fc_els_spp_resp resp; | |
1365 | struct fc_seq_els_data rjt_data; | |
1366 | u32 f_ctl; | |
1367 | u32 fcp_parm; | |
1368 | u32 roles = FC_RPORT_ROLE_UNKNOWN; | |
1369 | rjt_data.fp = NULL; | |
1370 | ||
1371 | fh = fc_frame_header_get(rx_fp); | |
1372 | ||
9fb9d328 JE |
1373 | FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n", |
1374 | fc_rport_state(rdata)); | |
42e9a92f RL |
1375 | |
1376 | switch (rdata->rp_state) { | |
1377 | case RPORT_ST_PRLI: | |
3ac6f98f | 1378 | case RPORT_ST_RTV: |
42e9a92f | 1379 | case RPORT_ST_READY: |
370c3bd0 | 1380 | case RPORT_ST_ADISC: |
42e9a92f RL |
1381 | reason = ELS_RJT_NONE; |
1382 | break; | |
1383 | default: | |
b4c6f546 AJ |
1384 | fc_frame_free(rx_fp); |
1385 | return; | |
42e9a92f RL |
1386 | break; |
1387 | } | |
1388 | len = fr_len(rx_fp) - sizeof(*fh); | |
1389 | pp = fc_frame_payload_get(rx_fp, sizeof(*pp)); | |
1390 | if (pp == NULL) { | |
1391 | reason = ELS_RJT_PROT; | |
1392 | explan = ELS_EXPL_INV_LEN; | |
1393 | } else { | |
1394 | plen = ntohs(pp->prli.prli_len); | |
1395 | if ((plen % 4) != 0 || plen > len) { | |
1396 | reason = ELS_RJT_PROT; | |
1397 | explan = ELS_EXPL_INV_LEN; | |
1398 | } else if (plen < len) { | |
1399 | len = plen; | |
1400 | } | |
1401 | plen = pp->prli.prli_spp_len; | |
1402 | if ((plen % 4) != 0 || plen < sizeof(*spp) || | |
1403 | plen > len || len < sizeof(*pp)) { | |
1404 | reason = ELS_RJT_PROT; | |
1405 | explan = ELS_EXPL_INV_LEN; | |
1406 | } | |
1407 | rspp = &pp->spp; | |
1408 | } | |
1409 | if (reason != ELS_RJT_NONE || | |
1410 | (fp = fc_frame_alloc(lport, len)) == NULL) { | |
1411 | rjt_data.reason = reason; | |
1412 | rjt_data.explan = explan; | |
1413 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); | |
1414 | } else { | |
1415 | sp = lport->tt.seq_start_next(sp); | |
1416 | WARN_ON(!sp); | |
1417 | pp = fc_frame_payload_get(fp, len); | |
1418 | WARN_ON(!pp); | |
1419 | memset(pp, 0, len); | |
1420 | pp->prli.prli_cmd = ELS_LS_ACC; | |
1421 | pp->prli.prli_spp_len = plen; | |
1422 | pp->prli.prli_len = htons(len); | |
1423 | len -= sizeof(struct fc_els_prli); | |
1424 | ||
6bd054cb RL |
1425 | /* reinitialize remote port roles */ |
1426 | rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN; | |
1427 | ||
42e9a92f RL |
1428 | /* |
1429 | * Go through all the service parameter pages and build | |
1430 | * response. If plen indicates longer SPP than standard, | |
1431 | * use that. The entire response has been pre-cleared above. | |
1432 | */ | |
1433 | spp = &pp->spp; | |
1434 | while (len >= plen) { | |
1435 | spp->spp_type = rspp->spp_type; | |
1436 | spp->spp_type_ext = rspp->spp_type_ext; | |
1437 | spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR; | |
1438 | resp = FC_SPP_RESP_ACK; | |
1439 | if (rspp->spp_flags & FC_SPP_RPA_VAL) | |
1440 | resp = FC_SPP_RESP_NO_PA; | |
1441 | switch (rspp->spp_type) { | |
1442 | case 0: /* common to all FC-4 types */ | |
1443 | break; | |
1444 | case FC_TYPE_FCP: | |
1445 | fcp_parm = ntohl(rspp->spp_params); | |
85b5893c | 1446 | if (fcp_parm & FCP_SPPF_RETRY) |
42e9a92f | 1447 | rdata->flags |= FC_RP_FLAGS_RETRY; |
f211fa51 | 1448 | rdata->supported_classes = FC_COS_CLASS3; |
42e9a92f RL |
1449 | if (fcp_parm & FCP_SPPF_INIT_FCN) |
1450 | roles |= FC_RPORT_ROLE_FCP_INITIATOR; | |
1451 | if (fcp_parm & FCP_SPPF_TARG_FCN) | |
1452 | roles |= FC_RPORT_ROLE_FCP_TARGET; | |
f211fa51 | 1453 | rdata->ids.roles = roles; |
42e9a92f RL |
1454 | |
1455 | spp->spp_params = | |
1456 | htonl(lport->service_params); | |
1457 | break; | |
1458 | default: | |
1459 | resp = FC_SPP_RESP_INVL; | |
1460 | break; | |
1461 | } | |
1462 | spp->spp_flags |= resp; | |
1463 | len -= plen; | |
1464 | rspp = (struct fc_els_spp *)((char *)rspp + plen); | |
1465 | spp = (struct fc_els_spp *)((char *)spp + plen); | |
1466 | } | |
1467 | ||
1468 | /* | |
1469 | * Send LS_ACC. If this fails, the originator should retry. | |
1470 | */ | |
1471 | f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ; | |
1472 | f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT; | |
1473 | ep = fc_seq_exch(sp); | |
1474 | fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, | |
1475 | FC_TYPE_ELS, f_ctl, 0); | |
1476 | lport->tt.seq_send(lport, sp, fp); | |
1477 | ||
1478 | /* | |
1479 | * Get lock and re-check state. | |
1480 | */ | |
1481 | switch (rdata->rp_state) { | |
1482 | case RPORT_ST_PRLI: | |
9fb9d328 | 1483 | fc_rport_enter_ready(rdata); |
42e9a92f RL |
1484 | break; |
1485 | case RPORT_ST_READY: | |
370c3bd0 | 1486 | case RPORT_ST_ADISC: |
42e9a92f RL |
1487 | break; |
1488 | default: | |
1489 | break; | |
1490 | } | |
1491 | } | |
1492 | fc_frame_free(rx_fp); | |
1493 | } | |
1494 | ||
1495 | /** | |
3a3b42bf RL |
1496 | * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests |
1497 | * @rdata: The remote port that sent the PRLO request | |
1498 | * @sp: The sequence that the PRLO was on | |
1499 | * @fp: The PRLO request frame | |
42e9a92f RL |
1500 | * |
1501 | * Locking Note: The rport lock is exected to be held before calling | |
1502 | * this function. | |
1503 | */ | |
9fb9d328 JE |
1504 | static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata, |
1505 | struct fc_seq *sp, | |
42e9a92f RL |
1506 | struct fc_frame *fp) |
1507 | { | |
42e9a92f RL |
1508 | struct fc_lport *lport = rdata->local_port; |
1509 | ||
1510 | struct fc_frame_header *fh; | |
1511 | struct fc_seq_els_data rjt_data; | |
1512 | ||
1513 | fh = fc_frame_header_get(fp); | |
1514 | ||
9fb9d328 JE |
1515 | FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n", |
1516 | fc_rport_state(rdata)); | |
42e9a92f RL |
1517 | |
1518 | rjt_data.fp = NULL; | |
1519 | rjt_data.reason = ELS_RJT_UNAB; | |
1520 | rjt_data.explan = ELS_EXPL_NONE; | |
1521 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); | |
1522 | fc_frame_free(fp); | |
1523 | } | |
1524 | ||
1525 | /** | |
3a3b42bf RL |
1526 | * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests |
1527 | * @lport: The local port that received the LOGO request | |
1528 | * @sp: The sequence that the LOGO request was on | |
1529 | * @fp: The LOGO request frame | |
42e9a92f RL |
1530 | * |
1531 | * Locking Note: The rport lock is exected to be held before calling | |
1532 | * this function. | |
1533 | */ | |
83fe6a93 | 1534 | static void fc_rport_recv_logo_req(struct fc_lport *lport, |
9fb9d328 | 1535 | struct fc_seq *sp, |
42e9a92f RL |
1536 | struct fc_frame *fp) |
1537 | { | |
1538 | struct fc_frame_header *fh; | |
83fe6a93 JE |
1539 | struct fc_rport_priv *rdata; |
1540 | u32 sid; | |
42e9a92f | 1541 | |
feab4ae7 JE |
1542 | lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL); |
1543 | ||
42e9a92f | 1544 | fh = fc_frame_header_get(fp); |
83fe6a93 | 1545 | sid = ntoh24(fh->fh_s_id); |
42e9a92f | 1546 | |
83fe6a93 JE |
1547 | mutex_lock(&lport->disc.disc_mutex); |
1548 | rdata = lport->tt.rport_lookup(lport, sid); | |
1549 | if (rdata) { | |
1550 | mutex_lock(&rdata->rp_mutex); | |
1551 | FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n", | |
1552 | fc_rport_state(rdata)); | |
feab4ae7 | 1553 | |
b4a9c7ed JE |
1554 | fc_rport_enter_delete(rdata, RPORT_EV_LOGO); |
1555 | ||
feab4ae7 | 1556 | /* |
b4a9c7ed JE |
1557 | * If the remote port was created due to discovery, set state |
1558 | * to log back in. It may have seen a stale RSCN about us. | |
feab4ae7 | 1559 | */ |
b4a9c7ed JE |
1560 | if (rdata->disc_id) |
1561 | fc_rport_state_enter(rdata, RPORT_ST_RESTART); | |
83fe6a93 JE |
1562 | mutex_unlock(&rdata->rp_mutex); |
1563 | } else | |
1564 | FC_RPORT_ID_DBG(lport, sid, | |
1565 | "Received LOGO from non-logged-in port\n"); | |
1566 | mutex_unlock(&lport->disc.disc_mutex); | |
42e9a92f RL |
1567 | fc_frame_free(fp); |
1568 | } | |
1569 | ||
3a3b42bf RL |
1570 | /** |
1571 | * fc_rport_flush_queue() - Flush the rport_event_queue | |
1572 | */ | |
42e9a92f RL |
1573 | static void fc_rport_flush_queue(void) |
1574 | { | |
1575 | flush_workqueue(rport_event_queue); | |
1576 | } | |
1577 | ||
3a3b42bf RL |
1578 | /** |
1579 | * fc_rport_init() - Initialize the remote port layer for a local port | |
1580 | * @lport: The local port to initialize the remote port layer for | |
1581 | */ | |
42e9a92f RL |
1582 | int fc_rport_init(struct fc_lport *lport) |
1583 | { | |
8025b5db JE |
1584 | if (!lport->tt.rport_lookup) |
1585 | lport->tt.rport_lookup = fc_rport_lookup; | |
1586 | ||
5101ff99 | 1587 | if (!lport->tt.rport_create) |
9e9d0452 | 1588 | lport->tt.rport_create = fc_rport_create; |
5101ff99 | 1589 | |
42e9a92f RL |
1590 | if (!lport->tt.rport_login) |
1591 | lport->tt.rport_login = fc_rport_login; | |
1592 | ||
1593 | if (!lport->tt.rport_logoff) | |
1594 | lport->tt.rport_logoff = fc_rport_logoff; | |
1595 | ||
1596 | if (!lport->tt.rport_recv_req) | |
1597 | lport->tt.rport_recv_req = fc_rport_recv_req; | |
1598 | ||
1599 | if (!lport->tt.rport_flush_queue) | |
1600 | lport->tt.rport_flush_queue = fc_rport_flush_queue; | |
1601 | ||
f211fa51 JE |
1602 | if (!lport->tt.rport_destroy) |
1603 | lport->tt.rport_destroy = fc_rport_destroy; | |
1604 | ||
42e9a92f RL |
1605 | return 0; |
1606 | } | |
1607 | EXPORT_SYMBOL(fc_rport_init); | |
1608 | ||
3a3b42bf RL |
1609 | /** |
1610 | * fc_setup_rport() - Initialize the rport_event_queue | |
1611 | */ | |
1612 | int fc_setup_rport() | |
42e9a92f RL |
1613 | { |
1614 | rport_event_queue = create_singlethread_workqueue("fc_rport_eq"); | |
1615 | if (!rport_event_queue) | |
1616 | return -ENOMEM; | |
1617 | return 0; | |
1618 | } | |
42e9a92f | 1619 | |
3a3b42bf RL |
1620 | /** |
1621 | * fc_destroy_rport() - Destroy the rport_event_queue | |
1622 | */ | |
1623 | void fc_destroy_rport() | |
42e9a92f RL |
1624 | { |
1625 | destroy_workqueue(rport_event_queue); | |
1626 | } | |
42e9a92f | 1627 | |
3a3b42bf RL |
1628 | /** |
1629 | * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port | |
1630 | * @rport: The remote port whose I/O should be terminated | |
1631 | */ | |
42e9a92f RL |
1632 | void fc_rport_terminate_io(struct fc_rport *rport) |
1633 | { | |
3a3b42bf RL |
1634 | struct fc_rport_libfc_priv *rpriv = rport->dd_data; |
1635 | struct fc_lport *lport = rpriv->local_port; | |
42e9a92f | 1636 | |
1f6ff364 AJ |
1637 | lport->tt.exch_mgr_reset(lport, 0, rport->port_id); |
1638 | lport->tt.exch_mgr_reset(lport, rport->port_id, 0); | |
42e9a92f RL |
1639 | } |
1640 | EXPORT_SYMBOL(fc_rport_terminate_io); |