aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libfc/fc_rport.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libfc/fc_rport.c')
-rw-r--r--drivers/scsi/libfc/fc_rport.c1144
1 files changed, 696 insertions, 448 deletions
diff --git a/drivers/scsi/libfc/fc_rport.c b/drivers/scsi/libfc/fc_rport.c
index 7162385f52eb..03ea6748e7ee 100644
--- a/drivers/scsi/libfc/fc_rport.c
+++ b/drivers/scsi/libfc/fc_rport.c
@@ -57,94 +57,114 @@
57 57
58struct workqueue_struct *rport_event_queue; 58struct workqueue_struct *rport_event_queue;
59 59
60static void fc_rport_enter_plogi(struct fc_rport *); 60static void fc_rport_enter_plogi(struct fc_rport_priv *);
61static void fc_rport_enter_prli(struct fc_rport *); 61static void fc_rport_enter_prli(struct fc_rport_priv *);
62static void fc_rport_enter_rtv(struct fc_rport *); 62static void fc_rport_enter_rtv(struct fc_rport_priv *);
63static void fc_rport_enter_ready(struct fc_rport *); 63static void fc_rport_enter_ready(struct fc_rport_priv *);
64static void fc_rport_enter_logo(struct fc_rport *); 64static void fc_rport_enter_logo(struct fc_rport_priv *);
65 65static void fc_rport_enter_adisc(struct fc_rport_priv *);
66static void fc_rport_recv_plogi_req(struct fc_rport *, 66
67static void fc_rport_recv_plogi_req(struct fc_lport *,
67 struct fc_seq *, struct fc_frame *); 68 struct fc_seq *, struct fc_frame *);
68static void fc_rport_recv_prli_req(struct fc_rport *, 69static void fc_rport_recv_prli_req(struct fc_rport_priv *,
69 struct fc_seq *, struct fc_frame *); 70 struct fc_seq *, struct fc_frame *);
70static void fc_rport_recv_prlo_req(struct fc_rport *, 71static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
71 struct fc_seq *, struct fc_frame *); 72 struct fc_seq *, struct fc_frame *);
72static void fc_rport_recv_logo_req(struct fc_rport *, 73static void fc_rport_recv_logo_req(struct fc_lport *,
73 struct fc_seq *, struct fc_frame *); 74 struct fc_seq *, struct fc_frame *);
74static void fc_rport_timeout(struct work_struct *); 75static void fc_rport_timeout(struct work_struct *);
75static void fc_rport_error(struct fc_rport *, struct fc_frame *); 76static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
76static void fc_rport_error_retry(struct fc_rport *, struct fc_frame *); 77static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
77static void fc_rport_work(struct work_struct *); 78static void fc_rport_work(struct work_struct *);
78 79
79static const char *fc_rport_state_names[] = { 80static const char *fc_rport_state_names[] = {
80 [RPORT_ST_NONE] = "None",
81 [RPORT_ST_INIT] = "Init", 81 [RPORT_ST_INIT] = "Init",
82 [RPORT_ST_PLOGI] = "PLOGI", 82 [RPORT_ST_PLOGI] = "PLOGI",
83 [RPORT_ST_PRLI] = "PRLI", 83 [RPORT_ST_PRLI] = "PRLI",
84 [RPORT_ST_RTV] = "RTV", 84 [RPORT_ST_RTV] = "RTV",
85 [RPORT_ST_READY] = "Ready", 85 [RPORT_ST_READY] = "Ready",
86 [RPORT_ST_LOGO] = "LOGO", 86 [RPORT_ST_LOGO] = "LOGO",
87 [RPORT_ST_ADISC] = "ADISC",
88 [RPORT_ST_DELETE] = "Delete",
87}; 89};
88 90
89static void fc_rport_rogue_destroy(struct device *dev) 91/**
92 * fc_rport_lookup() - lookup a remote port by port_id
93 * @lport: Fibre Channel host port instance
94 * @port_id: remote port port_id to match
95 */
96static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
97 u32 port_id)
90{ 98{
91 struct fc_rport *rport = dev_to_rport(dev); 99 struct fc_rport_priv *rdata;
92 FC_RPORT_DBG(rport, "Destroying rogue rport\n"); 100
93 kfree(rport); 101 list_for_each_entry(rdata, &lport->disc.rports, peers)
102 if (rdata->ids.port_id == port_id &&
103 rdata->rp_state != RPORT_ST_DELETE)
104 return rdata;
105 return NULL;
94} 106}
95 107
96struct fc_rport *fc_rport_rogue_create(struct fc_disc_port *dp) 108/**
109 * fc_rport_create() - Create a new remote port
110 * @lport: The local port that the new remote port is for
111 * @port_id: The port ID for the new remote port
112 *
113 * Locking note: must be called with the disc_mutex held.
114 */
115static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
116 u32 port_id)
97{ 117{
98 struct fc_rport *rport; 118 struct fc_rport_priv *rdata;
99 struct fc_rport_libfc_priv *rdata;
100 rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
101 119
102 if (!rport) 120 rdata = lport->tt.rport_lookup(lport, port_id);
103 return NULL; 121 if (rdata)
122 return rdata;
104 123
105 rdata = RPORT_TO_PRIV(rport); 124 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
125 if (!rdata)
126 return NULL;
106 127
107 rport->dd_data = rdata; 128 rdata->ids.node_name = -1;
108 rport->port_id = dp->ids.port_id; 129 rdata->ids.port_name = -1;
109 rport->port_name = dp->ids.port_name; 130 rdata->ids.port_id = port_id;
110 rport->node_name = dp->ids.node_name; 131 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
111 rport->roles = dp->ids.roles;
112 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
113 /*
114 * Note: all this libfc rogue rport code will be removed for
115 * upstream so it fine that this is really ugly and hacky right now.
116 */
117 device_initialize(&rport->dev);
118 rport->dev.release = fc_rport_rogue_destroy;
119 132
133 kref_init(&rdata->kref);
120 mutex_init(&rdata->rp_mutex); 134 mutex_init(&rdata->rp_mutex);
121 rdata->local_port = dp->lp; 135 rdata->local_port = lport;
122 rdata->trans_state = FC_PORTSTATE_ROGUE;
123 rdata->rp_state = RPORT_ST_INIT; 136 rdata->rp_state = RPORT_ST_INIT;
124 rdata->event = RPORT_EV_NONE; 137 rdata->event = RPORT_EV_NONE;
125 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED; 138 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
126 rdata->ops = NULL; 139 rdata->e_d_tov = lport->e_d_tov;
127 rdata->e_d_tov = dp->lp->e_d_tov; 140 rdata->r_a_tov = lport->r_a_tov;
128 rdata->r_a_tov = dp->lp->r_a_tov; 141 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
129 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout); 142 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
130 INIT_WORK(&rdata->event_work, fc_rport_work); 143 INIT_WORK(&rdata->event_work, fc_rport_work);
131 /* 144 if (port_id != FC_FID_DIR_SERV)
132 * For good measure, but not necessary as we should only 145 list_add(&rdata->peers, &lport->disc.rports);
133 * add REAL rport to the lport list. 146 return rdata;
134 */ 147}
135 INIT_LIST_HEAD(&rdata->peers); 148
149/**
150 * fc_rport_destroy() - free a remote port after last reference is released.
151 * @kref: pointer to kref inside struct fc_rport_priv
152 */
153static void fc_rport_destroy(struct kref *kref)
154{
155 struct fc_rport_priv *rdata;
136 156
137 return rport; 157 rdata = container_of(kref, struct fc_rport_priv, kref);
158 kfree(rdata);
138} 159}
139 160
140/** 161/**
141 * fc_rport_state() - return a string for the state the rport is in 162 * fc_rport_state() - return a string for the state the rport is in
142 * @rport: The rport whose state we want to get a string for 163 * @rdata: remote port private data
143 */ 164 */
144static const char *fc_rport_state(struct fc_rport *rport) 165static const char *fc_rport_state(struct fc_rport_priv *rdata)
145{ 166{
146 const char *cp; 167 const char *cp;
147 struct fc_rport_libfc_priv *rdata = rport->dd_data;
148 168
149 cp = fc_rport_state_names[rdata->rp_state]; 169 cp = fc_rport_state_names[rdata->rp_state];
150 if (!cp) 170 if (!cp)
@@ -191,15 +211,14 @@ static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
191 211
192/** 212/**
193 * fc_rport_state_enter() - Change the rport's state 213 * fc_rport_state_enter() - Change the rport's state
194 * @rport: The rport whose state should change 214 * @rdata: The rport whose state should change
195 * @new: The new state of the rport 215 * @new: The new state of the rport
196 * 216 *
197 * Locking Note: Called with the rport lock held 217 * Locking Note: Called with the rport lock held
198 */ 218 */
199static void fc_rport_state_enter(struct fc_rport *rport, 219static void fc_rport_state_enter(struct fc_rport_priv *rdata,
200 enum fc_rport_state new) 220 enum fc_rport_state new)
201{ 221{
202 struct fc_rport_libfc_priv *rdata = rport->dd_data;
203 if (rdata->rp_state != new) 222 if (rdata->rp_state != new)
204 rdata->retries = 0; 223 rdata->retries = 0;
205 rdata->rp_state = new; 224 rdata->rp_state = new;
@@ -208,147 +227,187 @@ static void fc_rport_state_enter(struct fc_rport *rport,
208static void fc_rport_work(struct work_struct *work) 227static void fc_rport_work(struct work_struct *work)
209{ 228{
210 u32 port_id; 229 u32 port_id;
211 struct fc_rport_libfc_priv *rdata = 230 struct fc_rport_priv *rdata =
212 container_of(work, struct fc_rport_libfc_priv, event_work); 231 container_of(work, struct fc_rport_priv, event_work);
232 struct fc_rport_libfc_priv *rp;
213 enum fc_rport_event event; 233 enum fc_rport_event event;
214 enum fc_rport_trans_state trans_state;
215 struct fc_lport *lport = rdata->local_port; 234 struct fc_lport *lport = rdata->local_port;
216 struct fc_rport_operations *rport_ops; 235 struct fc_rport_operations *rport_ops;
217 struct fc_rport *rport = PRIV_TO_RPORT(rdata); 236 struct fc_rport_identifiers ids;
237 struct fc_rport *rport;
218 238
219 mutex_lock(&rdata->rp_mutex); 239 mutex_lock(&rdata->rp_mutex);
220 event = rdata->event; 240 event = rdata->event;
221 rport_ops = rdata->ops; 241 rport_ops = rdata->ops;
242 rport = rdata->rport;
222 243
223 if (event == RPORT_EV_CREATED) { 244 FC_RPORT_DBG(rdata, "work event %u\n", event);
224 struct fc_rport *new_rport;
225 struct fc_rport_libfc_priv *new_rdata;
226 struct fc_rport_identifiers ids;
227 245
228 ids.port_id = rport->port_id; 246 switch (event) {
229 ids.roles = rport->roles; 247 case RPORT_EV_READY:
230 ids.port_name = rport->port_name; 248 ids = rdata->ids;
231 ids.node_name = rport->node_name; 249 rdata->event = RPORT_EV_NONE;
250 kref_get(&rdata->kref);
251 mutex_unlock(&rdata->rp_mutex);
232 252
253 if (!rport)
254 rport = fc_remote_port_add(lport->host, 0, &ids);
255 if (!rport) {
256 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
257 lport->tt.rport_logoff(rdata);
258 kref_put(&rdata->kref, lport->tt.rport_destroy);
259 return;
260 }
261 mutex_lock(&rdata->rp_mutex);
262 if (rdata->rport)
263 FC_RPORT_DBG(rdata, "rport already allocated\n");
264 rdata->rport = rport;
265 rport->maxframe_size = rdata->maxframe_size;
266 rport->supported_classes = rdata->supported_classes;
267
268 rp = rport->dd_data;
269 rp->local_port = lport;
270 rp->rp_state = rdata->rp_state;
271 rp->flags = rdata->flags;
272 rp->e_d_tov = rdata->e_d_tov;
273 rp->r_a_tov = rdata->r_a_tov;
233 mutex_unlock(&rdata->rp_mutex); 274 mutex_unlock(&rdata->rp_mutex);
234 275
235 new_rport = fc_remote_port_add(lport->host, 0, &ids); 276 if (rport_ops && rport_ops->event_callback) {
236 if (new_rport) { 277 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
237 /* 278 rport_ops->event_callback(lport, rdata, event);
238 * Switch from the rogue rport to the rport
239 * returned by the FC class.
240 */
241 new_rport->maxframe_size = rport->maxframe_size;
242
243 new_rdata = new_rport->dd_data;
244 new_rdata->e_d_tov = rdata->e_d_tov;
245 new_rdata->r_a_tov = rdata->r_a_tov;
246 new_rdata->ops = rdata->ops;
247 new_rdata->local_port = rdata->local_port;
248 new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
249 new_rdata->trans_state = FC_PORTSTATE_REAL;
250 mutex_init(&new_rdata->rp_mutex);
251 INIT_DELAYED_WORK(&new_rdata->retry_work,
252 fc_rport_timeout);
253 INIT_LIST_HEAD(&new_rdata->peers);
254 INIT_WORK(&new_rdata->event_work, fc_rport_work);
255
256 fc_rport_state_enter(new_rport, RPORT_ST_READY);
257 } else {
258 printk(KERN_WARNING "libfc: Failed to allocate "
259 " memory for rport (%6x)\n", ids.port_id);
260 event = RPORT_EV_FAILED;
261 } 279 }
262 if (rport->port_id != FC_FID_DIR_SERV) 280 kref_put(&rdata->kref, lport->tt.rport_destroy);
263 if (rport_ops->event_callback) 281 break;
264 rport_ops->event_callback(lport, rport, 282
265 RPORT_EV_FAILED); 283 case RPORT_EV_FAILED:
266 put_device(&rport->dev); 284 case RPORT_EV_LOGO:
267 rport = new_rport; 285 case RPORT_EV_STOP:
268 rdata = new_rport->dd_data; 286 port_id = rdata->ids.port_id;
269 if (rport_ops->event_callback)
270 rport_ops->event_callback(lport, rport, event);
271 } else if ((event == RPORT_EV_FAILED) ||
272 (event == RPORT_EV_LOGO) ||
273 (event == RPORT_EV_STOP)) {
274 trans_state = rdata->trans_state;
275 mutex_unlock(&rdata->rp_mutex); 287 mutex_unlock(&rdata->rp_mutex);
276 if (rport_ops->event_callback) 288
277 rport_ops->event_callback(lport, rport, event); 289 if (port_id != FC_FID_DIR_SERV) {
278 if (trans_state == FC_PORTSTATE_ROGUE) 290 mutex_lock(&lport->disc.disc_mutex);
279 put_device(&rport->dev); 291 list_del(&rdata->peers);
280 else { 292 mutex_unlock(&lport->disc.disc_mutex);
281 port_id = rport->port_id; 293 }
294
295 if (rport_ops && rport_ops->event_callback) {
296 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
297 rport_ops->event_callback(lport, rdata, event);
298 }
299 cancel_delayed_work_sync(&rdata->retry_work);
300
301 /*
302 * Reset any outstanding exchanges before freeing rport.
303 */
304 lport->tt.exch_mgr_reset(lport, 0, port_id);
305 lport->tt.exch_mgr_reset(lport, port_id, 0);
306
307 if (rport) {
308 rp = rport->dd_data;
309 rp->rp_state = RPORT_ST_DELETE;
310 mutex_lock(&rdata->rp_mutex);
311 rdata->rport = NULL;
312 mutex_unlock(&rdata->rp_mutex);
282 fc_remote_port_delete(rport); 313 fc_remote_port_delete(rport);
283 lport->tt.exch_mgr_reset(lport, 0, port_id);
284 lport->tt.exch_mgr_reset(lport, port_id, 0);
285 } 314 }
286 } else 315 kref_put(&rdata->kref, lport->tt.rport_destroy);
316 break;
317
318 default:
287 mutex_unlock(&rdata->rp_mutex); 319 mutex_unlock(&rdata->rp_mutex);
320 break;
321 }
288} 322}
289 323
290/** 324/**
291 * fc_rport_login() - Start the remote port login state machine 325 * fc_rport_login() - Start the remote port login state machine
292 * @rport: Fibre Channel remote port 326 * @rdata: private remote port
293 * 327 *
294 * Locking Note: Called without the rport lock held. This 328 * Locking Note: Called without the rport lock held. This
295 * function will hold the rport lock, call an _enter_* 329 * function will hold the rport lock, call an _enter_*
296 * function and then unlock the rport. 330 * function and then unlock the rport.
331 *
332 * This indicates the intent to be logged into the remote port.
333 * If it appears we are already logged in, ADISC is used to verify
334 * the setup.
297 */ 335 */
298int fc_rport_login(struct fc_rport *rport) 336int fc_rport_login(struct fc_rport_priv *rdata)
299{ 337{
300 struct fc_rport_libfc_priv *rdata = rport->dd_data;
301
302 mutex_lock(&rdata->rp_mutex); 338 mutex_lock(&rdata->rp_mutex);
303 339
304 FC_RPORT_DBG(rport, "Login to port\n"); 340 switch (rdata->rp_state) {
305 341 case RPORT_ST_READY:
306 fc_rport_enter_plogi(rport); 342 FC_RPORT_DBG(rdata, "ADISC port\n");
307 343 fc_rport_enter_adisc(rdata);
344 break;
345 default:
346 FC_RPORT_DBG(rdata, "Login to port\n");
347 fc_rport_enter_plogi(rdata);
348 break;
349 }
308 mutex_unlock(&rdata->rp_mutex); 350 mutex_unlock(&rdata->rp_mutex);
309 351
310 return 0; 352 return 0;
311} 353}
312 354
313/** 355/**
356 * fc_rport_enter_delete() - schedule a remote port to be deleted.
357 * @rdata: private remote port
358 * @event: event to report as the reason for deletion
359 *
360 * Locking Note: Called with the rport lock held.
361 *
362 * Allow state change into DELETE only once.
363 *
364 * Call queue_work only if there's no event already pending.
365 * Set the new event so that the old pending event will not occur.
366 * Since we have the mutex, even if fc_rport_work() is already started,
367 * it'll see the new event.
368 */
369static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
370 enum fc_rport_event event)
371{
372 if (rdata->rp_state == RPORT_ST_DELETE)
373 return;
374
375 FC_RPORT_DBG(rdata, "Delete port\n");
376
377 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
378
379 if (rdata->event == RPORT_EV_NONE)
380 queue_work(rport_event_queue, &rdata->event_work);
381 rdata->event = event;
382}
383
384/**
314 * fc_rport_logoff() - Logoff and remove an rport 385 * fc_rport_logoff() - Logoff and remove an rport
315 * @rport: Fibre Channel remote port to be removed 386 * @rdata: private remote port
316 * 387 *
317 * Locking Note: Called without the rport lock held. This 388 * Locking Note: Called without the rport lock held. This
318 * function will hold the rport lock, call an _enter_* 389 * function will hold the rport lock, call an _enter_*
319 * function and then unlock the rport. 390 * function and then unlock the rport.
320 */ 391 */
321int fc_rport_logoff(struct fc_rport *rport) 392int fc_rport_logoff(struct fc_rport_priv *rdata)
322{ 393{
323 struct fc_rport_libfc_priv *rdata = rport->dd_data;
324
325 mutex_lock(&rdata->rp_mutex); 394 mutex_lock(&rdata->rp_mutex);
326 395
327 FC_RPORT_DBG(rport, "Remove port\n"); 396 FC_RPORT_DBG(rdata, "Remove port\n");
328 397
329 if (rdata->rp_state == RPORT_ST_NONE) { 398 if (rdata->rp_state == RPORT_ST_DELETE) {
330 FC_RPORT_DBG(rport, "Port in NONE state, not removing\n"); 399 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
331 mutex_unlock(&rdata->rp_mutex); 400 mutex_unlock(&rdata->rp_mutex);
332 goto out; 401 goto out;
333 } 402 }
334 403
335 fc_rport_enter_logo(rport); 404 fc_rport_enter_logo(rdata);
336 405
337 /* 406 /*
338 * Change the state to NONE so that we discard 407 * Change the state to Delete so that we discard
339 * the response. 408 * the response.
340 */ 409 */
341 fc_rport_state_enter(rport, RPORT_ST_NONE); 410 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
342
343 mutex_unlock(&rdata->rp_mutex);
344
345 cancel_delayed_work_sync(&rdata->retry_work);
346
347 mutex_lock(&rdata->rp_mutex);
348
349 rdata->event = RPORT_EV_STOP;
350 queue_work(rport_event_queue, &rdata->event_work);
351
352 mutex_unlock(&rdata->rp_mutex); 411 mutex_unlock(&rdata->rp_mutex);
353 412
354out: 413out:
@@ -357,26 +416,25 @@ out:
357 416
358/** 417/**
359 * fc_rport_enter_ready() - The rport is ready 418 * fc_rport_enter_ready() - The rport is ready
360 * @rport: Fibre Channel remote port that is ready 419 * @rdata: private remote port
361 * 420 *
362 * Locking Note: The rport lock is expected to be held before calling 421 * Locking Note: The rport lock is expected to be held before calling
363 * this routine. 422 * this routine.
364 */ 423 */
365static void fc_rport_enter_ready(struct fc_rport *rport) 424static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
366{ 425{
367 struct fc_rport_libfc_priv *rdata = rport->dd_data; 426 fc_rport_state_enter(rdata, RPORT_ST_READY);
368
369 fc_rport_state_enter(rport, RPORT_ST_READY);
370 427
371 FC_RPORT_DBG(rport, "Port is Ready\n"); 428 FC_RPORT_DBG(rdata, "Port is Ready\n");
372 429
373 rdata->event = RPORT_EV_CREATED; 430 if (rdata->event == RPORT_EV_NONE)
374 queue_work(rport_event_queue, &rdata->event_work); 431 queue_work(rport_event_queue, &rdata->event_work);
432 rdata->event = RPORT_EV_READY;
375} 433}
376 434
377/** 435/**
378 * fc_rport_timeout() - Handler for the retry_work timer. 436 * fc_rport_timeout() - Handler for the retry_work timer.
379 * @work: The work struct of the fc_rport_libfc_priv 437 * @work: The work struct of the fc_rport_priv
380 * 438 *
381 * Locking Note: Called without the rport lock held. This 439 * Locking Note: Called without the rport lock held. This
382 * function will hold the rport lock, call an _enter_* 440 * function will hold the rport lock, call an _enter_*
@@ -384,63 +442,63 @@ static void fc_rport_enter_ready(struct fc_rport *rport)
384 */ 442 */
385static void fc_rport_timeout(struct work_struct *work) 443static void fc_rport_timeout(struct work_struct *work)
386{ 444{
387 struct fc_rport_libfc_priv *rdata = 445 struct fc_rport_priv *rdata =
388 container_of(work, struct fc_rport_libfc_priv, retry_work.work); 446 container_of(work, struct fc_rport_priv, retry_work.work);
389 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
390 447
391 mutex_lock(&rdata->rp_mutex); 448 mutex_lock(&rdata->rp_mutex);
392 449
393 switch (rdata->rp_state) { 450 switch (rdata->rp_state) {
394 case RPORT_ST_PLOGI: 451 case RPORT_ST_PLOGI:
395 fc_rport_enter_plogi(rport); 452 fc_rport_enter_plogi(rdata);
396 break; 453 break;
397 case RPORT_ST_PRLI: 454 case RPORT_ST_PRLI:
398 fc_rport_enter_prli(rport); 455 fc_rport_enter_prli(rdata);
399 break; 456 break;
400 case RPORT_ST_RTV: 457 case RPORT_ST_RTV:
401 fc_rport_enter_rtv(rport); 458 fc_rport_enter_rtv(rdata);
402 break; 459 break;
403 case RPORT_ST_LOGO: 460 case RPORT_ST_LOGO:
404 fc_rport_enter_logo(rport); 461 fc_rport_enter_logo(rdata);
462 break;
463 case RPORT_ST_ADISC:
464 fc_rport_enter_adisc(rdata);
405 break; 465 break;
406 case RPORT_ST_READY: 466 case RPORT_ST_READY:
407 case RPORT_ST_INIT: 467 case RPORT_ST_INIT:
408 case RPORT_ST_NONE: 468 case RPORT_ST_DELETE:
409 break; 469 break;
410 } 470 }
411 471
412 mutex_unlock(&rdata->rp_mutex); 472 mutex_unlock(&rdata->rp_mutex);
413 put_device(&rport->dev);
414} 473}
415 474
416/** 475/**
417 * fc_rport_error() - Error handler, called once retries have been exhausted 476 * fc_rport_error() - Error handler, called once retries have been exhausted
418 * @rport: The fc_rport object 477 * @rdata: private remote port
419 * @fp: The frame pointer 478 * @fp: The frame pointer
420 * 479 *
421 * Locking Note: The rport lock is expected to be held before 480 * Locking Note: The rport lock is expected to be held before
422 * calling this routine 481 * calling this routine
423 */ 482 */
424static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp) 483static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
425{ 484{
426 struct fc_rport_libfc_priv *rdata = rport->dd_data; 485 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
427 486 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
428 FC_RPORT_DBG(rport, "Error %ld in state %s, retries %d\n", 487 fc_rport_state(rdata), rdata->retries);
429 PTR_ERR(fp), fc_rport_state(rport), rdata->retries);
430 488
431 switch (rdata->rp_state) { 489 switch (rdata->rp_state) {
432 case RPORT_ST_PLOGI: 490 case RPORT_ST_PLOGI:
433 case RPORT_ST_PRLI:
434 case RPORT_ST_LOGO: 491 case RPORT_ST_LOGO:
435 rdata->event = RPORT_EV_FAILED; 492 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
436 fc_rport_state_enter(rport, RPORT_ST_NONE);
437 queue_work(rport_event_queue,
438 &rdata->event_work);
439 break; 493 break;
440 case RPORT_ST_RTV: 494 case RPORT_ST_RTV:
441 fc_rport_enter_ready(rport); 495 fc_rport_enter_ready(rdata);
442 break; 496 break;
443 case RPORT_ST_NONE: 497 case RPORT_ST_PRLI:
498 case RPORT_ST_ADISC:
499 fc_rport_enter_logo(rdata);
500 break;
501 case RPORT_ST_DELETE:
444 case RPORT_ST_READY: 502 case RPORT_ST_READY:
445 case RPORT_ST_INIT: 503 case RPORT_ST_INIT:
446 break; 504 break;
@@ -449,7 +507,7 @@ static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
449 507
450/** 508/**
451 * fc_rport_error_retry() - Error handler when retries are desired 509 * fc_rport_error_retry() - Error handler when retries are desired
452 * @rport: The fc_rport object 510 * @rdata: private remote port data
453 * @fp: The frame pointer 511 * @fp: The frame pointer
454 * 512 *
455 * If the error was an exchange timeout retry immediately, 513 * If the error was an exchange timeout retry immediately,
@@ -458,45 +516,43 @@ static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
458 * Locking Note: The rport lock is expected to be held before 516 * Locking Note: The rport lock is expected to be held before
459 * calling this routine 517 * calling this routine
460 */ 518 */
461static void fc_rport_error_retry(struct fc_rport *rport, struct fc_frame *fp) 519static void fc_rport_error_retry(struct fc_rport_priv *rdata,
520 struct fc_frame *fp)
462{ 521{
463 struct fc_rport_libfc_priv *rdata = rport->dd_data;
464 unsigned long delay = FC_DEF_E_D_TOV; 522 unsigned long delay = FC_DEF_E_D_TOV;
465 523
466 /* make sure this isn't an FC_EX_CLOSED error, never retry those */ 524 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
467 if (PTR_ERR(fp) == -FC_EX_CLOSED) 525 if (PTR_ERR(fp) == -FC_EX_CLOSED)
468 return fc_rport_error(rport, fp); 526 return fc_rport_error(rdata, fp);
469 527
470 if (rdata->retries < rdata->local_port->max_rport_retry_count) { 528 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
471 FC_RPORT_DBG(rport, "Error %ld in state %s, retrying\n", 529 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
472 PTR_ERR(fp), fc_rport_state(rport)); 530 PTR_ERR(fp), fc_rport_state(rdata));
473 rdata->retries++; 531 rdata->retries++;
474 /* no additional delay on exchange timeouts */ 532 /* no additional delay on exchange timeouts */
475 if (PTR_ERR(fp) == -FC_EX_TIMEOUT) 533 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
476 delay = 0; 534 delay = 0;
477 get_device(&rport->dev);
478 schedule_delayed_work(&rdata->retry_work, delay); 535 schedule_delayed_work(&rdata->retry_work, delay);
479 return; 536 return;
480 } 537 }
481 538
482 return fc_rport_error(rport, fp); 539 return fc_rport_error(rdata, fp);
483} 540}
484 541
485/** 542/**
486 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response 543 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
487 * @sp: current sequence in the PLOGI exchange 544 * @sp: current sequence in the PLOGI exchange
488 * @fp: response frame 545 * @fp: response frame
489 * @rp_arg: Fibre Channel remote port 546 * @rdata_arg: private remote port data
490 * 547 *
491 * Locking Note: This function will be called without the rport lock 548 * Locking Note: This function will be called without the rport lock
492 * held, but it will lock, call an _enter_* function or fc_rport_error 549 * held, but it will lock, call an _enter_* function or fc_rport_error
493 * and then unlock the rport. 550 * and then unlock the rport.
494 */ 551 */
495static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp, 552static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
496 void *rp_arg) 553 void *rdata_arg)
497{ 554{
498 struct fc_rport *rport = rp_arg; 555 struct fc_rport_priv *rdata = rdata_arg;
499 struct fc_rport_libfc_priv *rdata = rport->dd_data;
500 struct fc_lport *lport = rdata->local_port; 556 struct fc_lport *lport = rdata->local_port;
501 struct fc_els_flogi *plp = NULL; 557 struct fc_els_flogi *plp = NULL;
502 unsigned int tov; 558 unsigned int tov;
@@ -506,26 +562,26 @@ static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
506 562
507 mutex_lock(&rdata->rp_mutex); 563 mutex_lock(&rdata->rp_mutex);
508 564
509 FC_RPORT_DBG(rport, "Received a PLOGI response\n"); 565 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
510 566
511 if (rdata->rp_state != RPORT_ST_PLOGI) { 567 if (rdata->rp_state != RPORT_ST_PLOGI) {
512 FC_RPORT_DBG(rport, "Received a PLOGI response, but in state " 568 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
513 "%s\n", fc_rport_state(rport)); 569 "%s\n", fc_rport_state(rdata));
514 if (IS_ERR(fp)) 570 if (IS_ERR(fp))
515 goto err; 571 goto err;
516 goto out; 572 goto out;
517 } 573 }
518 574
519 if (IS_ERR(fp)) { 575 if (IS_ERR(fp)) {
520 fc_rport_error_retry(rport, fp); 576 fc_rport_error_retry(rdata, fp);
521 goto err; 577 goto err;
522 } 578 }
523 579
524 op = fc_frame_payload_op(fp); 580 op = fc_frame_payload_op(fp);
525 if (op == ELS_LS_ACC && 581 if (op == ELS_LS_ACC &&
526 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) { 582 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
527 rport->port_name = get_unaligned_be64(&plp->fl_wwpn); 583 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
528 rport->node_name = get_unaligned_be64(&plp->fl_wwnn); 584 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
529 585
530 tov = ntohl(plp->fl_csp.sp_e_d_tov); 586 tov = ntohl(plp->fl_csp.sp_e_d_tov);
531 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR) 587 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
@@ -537,75 +593,64 @@ static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
537 if (cssp_seq < csp_seq) 593 if (cssp_seq < csp_seq)
538 csp_seq = cssp_seq; 594 csp_seq = cssp_seq;
539 rdata->max_seq = csp_seq; 595 rdata->max_seq = csp_seq;
540 rport->maxframe_size = 596 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
541 fc_plogi_get_maxframe(plp, lport->mfs); 597 fc_rport_enter_prli(rdata);
542
543 /*
544 * If the rport is one of the well known addresses
545 * we skip PRLI and RTV and go straight to READY.
546 */
547 if (rport->port_id >= FC_FID_DOM_MGR)
548 fc_rport_enter_ready(rport);
549 else
550 fc_rport_enter_prli(rport);
551 } else 598 } else
552 fc_rport_error_retry(rport, fp); 599 fc_rport_error_retry(rdata, fp);
553 600
554out: 601out:
555 fc_frame_free(fp); 602 fc_frame_free(fp);
556err: 603err:
557 mutex_unlock(&rdata->rp_mutex); 604 mutex_unlock(&rdata->rp_mutex);
558 put_device(&rport->dev); 605 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
559} 606}
560 607
561/** 608/**
562 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer 609 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
563 * @rport: Fibre Channel remote port to send PLOGI to 610 * @rdata: private remote port data
564 * 611 *
565 * Locking Note: The rport lock is expected to be held before calling 612 * Locking Note: The rport lock is expected to be held before calling
566 * this routine. 613 * this routine.
567 */ 614 */
568static void fc_rport_enter_plogi(struct fc_rport *rport) 615static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
569{ 616{
570 struct fc_rport_libfc_priv *rdata = rport->dd_data;
571 struct fc_lport *lport = rdata->local_port; 617 struct fc_lport *lport = rdata->local_port;
572 struct fc_frame *fp; 618 struct fc_frame *fp;
573 619
574 FC_RPORT_DBG(rport, "Port entered PLOGI state from %s state\n", 620 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
575 fc_rport_state(rport)); 621 fc_rport_state(rdata));
576 622
577 fc_rport_state_enter(rport, RPORT_ST_PLOGI); 623 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
578 624
579 rport->maxframe_size = FC_MIN_MAX_PAYLOAD; 625 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
580 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi)); 626 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
581 if (!fp) { 627 if (!fp) {
582 fc_rport_error_retry(rport, fp); 628 fc_rport_error_retry(rdata, fp);
583 return; 629 return;
584 } 630 }
585 rdata->e_d_tov = lport->e_d_tov; 631 rdata->e_d_tov = lport->e_d_tov;
586 632
587 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI, 633 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
588 fc_rport_plogi_resp, rport, lport->e_d_tov)) 634 fc_rport_plogi_resp, rdata, lport->e_d_tov))
589 fc_rport_error_retry(rport, fp); 635 fc_rport_error_retry(rdata, fp);
590 else 636 else
591 get_device(&rport->dev); 637 kref_get(&rdata->kref);
592} 638}
593 639
594/** 640/**
595 * fc_rport_prli_resp() - Process Login (PRLI) response handler 641 * fc_rport_prli_resp() - Process Login (PRLI) response handler
596 * @sp: current sequence in the PRLI exchange 642 * @sp: current sequence in the PRLI exchange
597 * @fp: response frame 643 * @fp: response frame
598 * @rp_arg: Fibre Channel remote port 644 * @rdata_arg: private remote port data
599 * 645 *
600 * Locking Note: This function will be called without the rport lock 646 * Locking Note: This function will be called without the rport lock
601 * held, but it will lock, call an _enter_* function or fc_rport_error 647 * held, but it will lock, call an _enter_* function or fc_rport_error
602 * and then unlock the rport. 648 * and then unlock the rport.
603 */ 649 */
604static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp, 650static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
605 void *rp_arg) 651 void *rdata_arg)
606{ 652{
607 struct fc_rport *rport = rp_arg; 653 struct fc_rport_priv *rdata = rdata_arg;
608 struct fc_rport_libfc_priv *rdata = rport->dd_data;
609 struct { 654 struct {
610 struct fc_els_prli prli; 655 struct fc_els_prli prli;
611 struct fc_els_spp spp; 656 struct fc_els_spp spp;
@@ -616,21 +661,24 @@ static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
616 661
617 mutex_lock(&rdata->rp_mutex); 662 mutex_lock(&rdata->rp_mutex);
618 663
619 FC_RPORT_DBG(rport, "Received a PRLI response\n"); 664 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
620 665
621 if (rdata->rp_state != RPORT_ST_PRLI) { 666 if (rdata->rp_state != RPORT_ST_PRLI) {
622 FC_RPORT_DBG(rport, "Received a PRLI response, but in state " 667 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
623 "%s\n", fc_rport_state(rport)); 668 "%s\n", fc_rport_state(rdata));
624 if (IS_ERR(fp)) 669 if (IS_ERR(fp))
625 goto err; 670 goto err;
626 goto out; 671 goto out;
627 } 672 }
628 673
629 if (IS_ERR(fp)) { 674 if (IS_ERR(fp)) {
630 fc_rport_error_retry(rport, fp); 675 fc_rport_error_retry(rdata, fp);
631 goto err; 676 goto err;
632 } 677 }
633 678
679 /* reinitialize remote port roles */
680 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
681
634 op = fc_frame_payload_op(fp); 682 op = fc_frame_payload_op(fp);
635 if (op == ELS_LS_ACC) { 683 if (op == ELS_LS_ACC) {
636 pp = fc_frame_payload_get(fp, sizeof(*pp)); 684 pp = fc_frame_payload_get(fp, sizeof(*pp));
@@ -640,90 +688,82 @@ static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
640 rdata->flags |= FC_RP_FLAGS_RETRY; 688 rdata->flags |= FC_RP_FLAGS_RETRY;
641 } 689 }
642 690
643 rport->supported_classes = FC_COS_CLASS3; 691 rdata->supported_classes = FC_COS_CLASS3;
644 if (fcp_parm & FCP_SPPF_INIT_FCN) 692 if (fcp_parm & FCP_SPPF_INIT_FCN)
645 roles |= FC_RPORT_ROLE_FCP_INITIATOR; 693 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
646 if (fcp_parm & FCP_SPPF_TARG_FCN) 694 if (fcp_parm & FCP_SPPF_TARG_FCN)
647 roles |= FC_RPORT_ROLE_FCP_TARGET; 695 roles |= FC_RPORT_ROLE_FCP_TARGET;
648 696
649 rport->roles = roles; 697 rdata->ids.roles = roles;
650 fc_rport_enter_rtv(rport); 698 fc_rport_enter_rtv(rdata);
651 699
652 } else { 700 } else {
653 FC_RPORT_DBG(rport, "Bad ELS response for PRLI command\n"); 701 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
654 rdata->event = RPORT_EV_FAILED; 702 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
655 fc_rport_state_enter(rport, RPORT_ST_NONE);
656 queue_work(rport_event_queue, &rdata->event_work);
657 } 703 }
658 704
659out: 705out:
660 fc_frame_free(fp); 706 fc_frame_free(fp);
661err: 707err:
662 mutex_unlock(&rdata->rp_mutex); 708 mutex_unlock(&rdata->rp_mutex);
663 put_device(&rport->dev); 709 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
664} 710}
665 711
666/** 712/**
667 * fc_rport_logo_resp() - Logout (LOGO) response handler 713 * fc_rport_logo_resp() - Logout (LOGO) response handler
668 * @sp: current sequence in the LOGO exchange 714 * @sp: current sequence in the LOGO exchange
669 * @fp: response frame 715 * @fp: response frame
670 * @rp_arg: Fibre Channel remote port 716 * @rdata_arg: private remote port data
671 * 717 *
672 * Locking Note: This function will be called without the rport lock 718 * Locking Note: This function will be called without the rport lock
673 * held, but it will lock, call an _enter_* function or fc_rport_error 719 * held, but it will lock, call an _enter_* function or fc_rport_error
674 * and then unlock the rport. 720 * and then unlock the rport.
675 */ 721 */
676static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp, 722static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
677 void *rp_arg) 723 void *rdata_arg)
678{ 724{
679 struct fc_rport *rport = rp_arg; 725 struct fc_rport_priv *rdata = rdata_arg;
680 struct fc_rport_libfc_priv *rdata = rport->dd_data;
681 u8 op; 726 u8 op;
682 727
683 mutex_lock(&rdata->rp_mutex); 728 mutex_lock(&rdata->rp_mutex);
684 729
685 FC_RPORT_DBG(rport, "Received a LOGO response\n"); 730 FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
686 731
687 if (rdata->rp_state != RPORT_ST_LOGO) { 732 if (rdata->rp_state != RPORT_ST_LOGO) {
688 FC_RPORT_DBG(rport, "Received a LOGO response, but in state " 733 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
689 "%s\n", fc_rport_state(rport)); 734 "%s\n", fc_rport_state(rdata));
690 if (IS_ERR(fp)) 735 if (IS_ERR(fp))
691 goto err; 736 goto err;
692 goto out; 737 goto out;
693 } 738 }
694 739
695 if (IS_ERR(fp)) { 740 if (IS_ERR(fp)) {
696 fc_rport_error_retry(rport, fp); 741 fc_rport_error_retry(rdata, fp);
697 goto err; 742 goto err;
698 } 743 }
699 744
700 op = fc_frame_payload_op(fp); 745 op = fc_frame_payload_op(fp);
701 if (op == ELS_LS_ACC) { 746 if (op != ELS_LS_ACC)
702 fc_rport_enter_rtv(rport); 747 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
703 } else { 748 op);
704 FC_RPORT_DBG(rport, "Bad ELS response for LOGO command\n"); 749 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
705 rdata->event = RPORT_EV_LOGO;
706 fc_rport_state_enter(rport, RPORT_ST_NONE);
707 queue_work(rport_event_queue, &rdata->event_work);
708 }
709 750
710out: 751out:
711 fc_frame_free(fp); 752 fc_frame_free(fp);
712err: 753err:
713 mutex_unlock(&rdata->rp_mutex); 754 mutex_unlock(&rdata->rp_mutex);
714 put_device(&rport->dev); 755 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
715} 756}
716 757
717/** 758/**
718 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer 759 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
719 * @rport: Fibre Channel remote port to send PRLI to 760 * @rdata: private remote port data
720 * 761 *
721 * Locking Note: The rport lock is expected to be held before calling 762 * Locking Note: The rport lock is expected to be held before calling
722 * this routine. 763 * this routine.
723 */ 764 */
724static void fc_rport_enter_prli(struct fc_rport *rport) 765static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
725{ 766{
726 struct fc_rport_libfc_priv *rdata = rport->dd_data;
727 struct fc_lport *lport = rdata->local_port; 767 struct fc_lport *lport = rdata->local_port;
728 struct { 768 struct {
729 struct fc_els_prli prli; 769 struct fc_els_prli prli;
@@ -731,29 +771,38 @@ static void fc_rport_enter_prli(struct fc_rport *rport)
731 } *pp; 771 } *pp;
732 struct fc_frame *fp; 772 struct fc_frame *fp;
733 773
734 FC_RPORT_DBG(rport, "Port entered PRLI state from %s state\n", 774 /*
735 fc_rport_state(rport)); 775 * If the rport is one of the well known addresses
776 * we skip PRLI and RTV and go straight to READY.
777 */
778 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
779 fc_rport_enter_ready(rdata);
780 return;
781 }
782
783 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
784 fc_rport_state(rdata));
736 785
737 fc_rport_state_enter(rport, RPORT_ST_PRLI); 786 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
738 787
739 fp = fc_frame_alloc(lport, sizeof(*pp)); 788 fp = fc_frame_alloc(lport, sizeof(*pp));
740 if (!fp) { 789 if (!fp) {
741 fc_rport_error_retry(rport, fp); 790 fc_rport_error_retry(rdata, fp);
742 return; 791 return;
743 } 792 }
744 793
745 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI, 794 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
746 fc_rport_prli_resp, rport, lport->e_d_tov)) 795 fc_rport_prli_resp, rdata, lport->e_d_tov))
747 fc_rport_error_retry(rport, fp); 796 fc_rport_error_retry(rdata, fp);
748 else 797 else
749 get_device(&rport->dev); 798 kref_get(&rdata->kref);
750} 799}
751 800
752/** 801/**
753 * fc_rport_els_rtv_resp() - Request Timeout Value response handler 802 * fc_rport_els_rtv_resp() - Request Timeout Value response handler
754 * @sp: current sequence in the RTV exchange 803 * @sp: current sequence in the RTV exchange
755 * @fp: response frame 804 * @fp: response frame
756 * @rp_arg: Fibre Channel remote port 805 * @rdata_arg: private remote port data
757 * 806 *
758 * Many targets don't seem to support this. 807 * Many targets don't seem to support this.
759 * 808 *
@@ -762,26 +811,25 @@ static void fc_rport_enter_prli(struct fc_rport *rport)
762 * and then unlock the rport. 811 * and then unlock the rport.
763 */ 812 */
764static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp, 813static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
765 void *rp_arg) 814 void *rdata_arg)
766{ 815{
767 struct fc_rport *rport = rp_arg; 816 struct fc_rport_priv *rdata = rdata_arg;
768 struct fc_rport_libfc_priv *rdata = rport->dd_data;
769 u8 op; 817 u8 op;
770 818
771 mutex_lock(&rdata->rp_mutex); 819 mutex_lock(&rdata->rp_mutex);
772 820
773 FC_RPORT_DBG(rport, "Received a RTV response\n"); 821 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
774 822
775 if (rdata->rp_state != RPORT_ST_RTV) { 823 if (rdata->rp_state != RPORT_ST_RTV) {
776 FC_RPORT_DBG(rport, "Received a RTV response, but in state " 824 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
777 "%s\n", fc_rport_state(rport)); 825 "%s\n", fc_rport_state(rdata));
778 if (IS_ERR(fp)) 826 if (IS_ERR(fp))
779 goto err; 827 goto err;
780 goto out; 828 goto out;
781 } 829 }
782 830
783 if (IS_ERR(fp)) { 831 if (IS_ERR(fp)) {
784 fc_rport_error(rport, fp); 832 fc_rport_error(rdata, fp);
785 goto err; 833 goto err;
786 } 834 }
787 835
@@ -807,184 +855,376 @@ static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
807 } 855 }
808 } 856 }
809 857
810 fc_rport_enter_ready(rport); 858 fc_rport_enter_ready(rdata);
811 859
812out: 860out:
813 fc_frame_free(fp); 861 fc_frame_free(fp);
814err: 862err:
815 mutex_unlock(&rdata->rp_mutex); 863 mutex_unlock(&rdata->rp_mutex);
816 put_device(&rport->dev); 864 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
817} 865}
818 866
819/** 867/**
820 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer 868 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
821 * @rport: Fibre Channel remote port to send RTV to 869 * @rdata: private remote port data
822 * 870 *
823 * Locking Note: The rport lock is expected to be held before calling 871 * Locking Note: The rport lock is expected to be held before calling
824 * this routine. 872 * this routine.
825 */ 873 */
826static void fc_rport_enter_rtv(struct fc_rport *rport) 874static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
827{ 875{
828 struct fc_frame *fp; 876 struct fc_frame *fp;
829 struct fc_rport_libfc_priv *rdata = rport->dd_data;
830 struct fc_lport *lport = rdata->local_port; 877 struct fc_lport *lport = rdata->local_port;
831 878
832 FC_RPORT_DBG(rport, "Port entered RTV state from %s state\n", 879 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
833 fc_rport_state(rport)); 880 fc_rport_state(rdata));
834 881
835 fc_rport_state_enter(rport, RPORT_ST_RTV); 882 fc_rport_state_enter(rdata, RPORT_ST_RTV);
836 883
837 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv)); 884 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
838 if (!fp) { 885 if (!fp) {
839 fc_rport_error_retry(rport, fp); 886 fc_rport_error_retry(rdata, fp);
840 return; 887 return;
841 } 888 }
842 889
843 if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV, 890 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
844 fc_rport_rtv_resp, rport, lport->e_d_tov)) 891 fc_rport_rtv_resp, rdata, lport->e_d_tov))
845 fc_rport_error_retry(rport, fp); 892 fc_rport_error_retry(rdata, fp);
846 else 893 else
847 get_device(&rport->dev); 894 kref_get(&rdata->kref);
848} 895}
849 896
850/** 897/**
851 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer 898 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
852 * @rport: Fibre Channel remote port to send LOGO to 899 * @rdata: private remote port data
853 * 900 *
854 * Locking Note: The rport lock is expected to be held before calling 901 * Locking Note: The rport lock is expected to be held before calling
855 * this routine. 902 * this routine.
856 */ 903 */
857static void fc_rport_enter_logo(struct fc_rport *rport) 904static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
858{ 905{
859 struct fc_rport_libfc_priv *rdata = rport->dd_data;
860 struct fc_lport *lport = rdata->local_port; 906 struct fc_lport *lport = rdata->local_port;
861 struct fc_frame *fp; 907 struct fc_frame *fp;
862 908
863 FC_RPORT_DBG(rport, "Port entered LOGO state from %s state\n", 909 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
864 fc_rport_state(rport)); 910 fc_rport_state(rdata));
865 911
866 fc_rport_state_enter(rport, RPORT_ST_LOGO); 912 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
867 913
868 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo)); 914 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
869 if (!fp) { 915 if (!fp) {
870 fc_rport_error_retry(rport, fp); 916 fc_rport_error_retry(rdata, fp);
871 return; 917 return;
872 } 918 }
873 919
874 if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO, 920 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
875 fc_rport_logo_resp, rport, lport->e_d_tov)) 921 fc_rport_logo_resp, rdata, lport->e_d_tov))
876 fc_rport_error_retry(rport, fp); 922 fc_rport_error_retry(rdata, fp);
877 else 923 else
878 get_device(&rport->dev); 924 kref_get(&rdata->kref);
879} 925}
880 926
881
882/** 927/**
883 * fc_rport_recv_req() - Receive a request from a rport 928 * fc_rport_els_adisc_resp() - Address Discovery response handler
884 * @sp: current sequence in the PLOGI exchange 929 * @sp: current sequence in the ADISC exchange
885 * @fp: response frame 930 * @fp: response frame
886 * @rp_arg: Fibre Channel remote port 931 * @rdata_arg: remote port private.
887 * 932 *
888 * Locking Note: Called without the rport lock held. This 933 * Locking Note: This function will be called without the rport lock
889 * function will hold the rport lock, call an _enter_* 934 * held, but it will lock, call an _enter_* function or fc_rport_error
890 * function and then unlock the rport. 935 * and then unlock the rport.
891 */ 936 */
892void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp, 937static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
893 struct fc_rport *rport) 938 void *rdata_arg)
939{
940 struct fc_rport_priv *rdata = rdata_arg;
941 struct fc_els_adisc *adisc;
942 u8 op;
943
944 mutex_lock(&rdata->rp_mutex);
945
946 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
947
948 if (rdata->rp_state != RPORT_ST_ADISC) {
949 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
950 fc_rport_state(rdata));
951 if (IS_ERR(fp))
952 goto err;
953 goto out;
954 }
955
956 if (IS_ERR(fp)) {
957 fc_rport_error(rdata, fp);
958 goto err;
959 }
960
961 /*
962 * If address verification failed. Consider us logged out of the rport.
963 * Since the rport is still in discovery, we want to be
964 * logged in, so go to PLOGI state. Otherwise, go back to READY.
965 */
966 op = fc_frame_payload_op(fp);
967 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
968 if (op != ELS_LS_ACC || !adisc ||
969 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
970 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
971 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
972 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
973 fc_rport_enter_plogi(rdata);
974 } else {
975 FC_RPORT_DBG(rdata, "ADISC OK\n");
976 fc_rport_enter_ready(rdata);
977 }
978out:
979 fc_frame_free(fp);
980err:
981 mutex_unlock(&rdata->rp_mutex);
982 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
983}
984
985/**
986 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request to peer
987 * @rdata: remote port private data
988 *
989 * Locking Note: The rport lock is expected to be held before calling
990 * this routine.
991 */
992static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
894{ 993{
895 struct fc_rport_libfc_priv *rdata = rport->dd_data;
896 struct fc_lport *lport = rdata->local_port; 994 struct fc_lport *lport = rdata->local_port;
995 struct fc_frame *fp;
996
997 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
998 fc_rport_state(rdata));
897 999
1000 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1001
1002 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1003 if (!fp) {
1004 fc_rport_error_retry(rdata, fp);
1005 return;
1006 }
1007 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1008 fc_rport_adisc_resp, rdata, lport->e_d_tov))
1009 fc_rport_error_retry(rdata, fp);
1010 else
1011 kref_get(&rdata->kref);
1012}
1013
1014/**
1015 * fc_rport_recv_adisc_req() - Handle incoming Address Discovery (ADISC) Request
1016 * @rdata: remote port private
1017 * @sp: current sequence in the ADISC exchange
1018 * @in_fp: ADISC request frame
1019 *
1020 * Locking Note: Called with the lport and rport locks held.
1021 */
1022static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1023 struct fc_seq *sp, struct fc_frame *in_fp)
1024{
1025 struct fc_lport *lport = rdata->local_port;
1026 struct fc_frame *fp;
1027 struct fc_exch *ep = fc_seq_exch(sp);
1028 struct fc_els_adisc *adisc;
1029 struct fc_seq_els_data rjt_data;
1030 u32 f_ctl;
1031
1032 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1033
1034 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1035 if (!adisc) {
1036 rjt_data.fp = NULL;
1037 rjt_data.reason = ELS_RJT_PROT;
1038 rjt_data.explan = ELS_EXPL_INV_LEN;
1039 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1040 goto drop;
1041 }
1042
1043 fp = fc_frame_alloc(lport, sizeof(*adisc));
1044 if (!fp)
1045 goto drop;
1046 fc_adisc_fill(lport, fp);
1047 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1048 adisc->adisc_cmd = ELS_LS_ACC;
1049 sp = lport->tt.seq_start_next(sp);
1050 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1051 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1052 FC_TYPE_ELS, f_ctl, 0);
1053 lport->tt.seq_send(lport, sp, fp);
1054drop:
1055 fc_frame_free(in_fp);
1056}
1057
1058/**
1059 * fc_rport_recv_els_req() - handle a validated ELS request.
1060 * @lport: Fibre Channel local port
1061 * @sp: current sequence in the PLOGI exchange
1062 * @fp: response frame
1063 *
1064 * Handle incoming ELS requests that require port login.
1065 * The ELS opcode has already been validated by the caller.
1066 *
1067 * Locking Note: Called with the lport lock held.
1068 */
1069static void fc_rport_recv_els_req(struct fc_lport *lport,
1070 struct fc_seq *sp, struct fc_frame *fp)
1071{
1072 struct fc_rport_priv *rdata;
898 struct fc_frame_header *fh; 1073 struct fc_frame_header *fh;
899 struct fc_seq_els_data els_data; 1074 struct fc_seq_els_data els_data;
900 u8 op;
901
902 mutex_lock(&rdata->rp_mutex);
903 1075
904 els_data.fp = NULL; 1076 els_data.fp = NULL;
905 els_data.explan = ELS_EXPL_NONE; 1077 els_data.reason = ELS_RJT_UNAB;
906 els_data.reason = ELS_RJT_NONE; 1078 els_data.explan = ELS_EXPL_PLOGI_REQD;
907 1079
908 fh = fc_frame_header_get(fp); 1080 fh = fc_frame_header_get(fp);
909 1081
910 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) { 1082 mutex_lock(&lport->disc.disc_mutex);
911 op = fc_frame_payload_op(fp); 1083 rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
912 switch (op) { 1084 if (!rdata) {
913 case ELS_PLOGI: 1085 mutex_unlock(&lport->disc.disc_mutex);
914 fc_rport_recv_plogi_req(rport, sp, fp); 1086 goto reject;
915 break; 1087 }
916 case ELS_PRLI: 1088 mutex_lock(&rdata->rp_mutex);
917 fc_rport_recv_prli_req(rport, sp, fp); 1089 mutex_unlock(&lport->disc.disc_mutex);
918 break; 1090
919 case ELS_PRLO: 1091 switch (rdata->rp_state) {
920 fc_rport_recv_prlo_req(rport, sp, fp); 1092 case RPORT_ST_PRLI:
921 break; 1093 case RPORT_ST_RTV:
922 case ELS_LOGO: 1094 case RPORT_ST_READY:
923 fc_rport_recv_logo_req(rport, sp, fp); 1095 case RPORT_ST_ADISC:
924 break; 1096 break;
925 case ELS_RRQ: 1097 default:
926 els_data.fp = fp; 1098 mutex_unlock(&rdata->rp_mutex);
927 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data); 1099 goto reject;
928 break; 1100 }
929 case ELS_REC: 1101
930 els_data.fp = fp; 1102 switch (fc_frame_payload_op(fp)) {
931 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data); 1103 case ELS_PRLI:
932 break; 1104 fc_rport_recv_prli_req(rdata, sp, fp);
933 default: 1105 break;
934 els_data.reason = ELS_RJT_UNSUP; 1106 case ELS_PRLO:
935 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data); 1107 fc_rport_recv_prlo_req(rdata, sp, fp);
936 break; 1108 break;
937 } 1109 case ELS_ADISC:
1110 fc_rport_recv_adisc_req(rdata, sp, fp);
1111 break;
1112 case ELS_RRQ:
1113 els_data.fp = fp;
1114 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1115 break;
1116 case ELS_REC:
1117 els_data.fp = fp;
1118 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1119 break;
1120 default:
1121 fc_frame_free(fp); /* can't happen */
1122 break;
938 } 1123 }
939 1124
940 mutex_unlock(&rdata->rp_mutex); 1125 mutex_unlock(&rdata->rp_mutex);
1126 return;
1127
1128reject:
1129 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1130 fc_frame_free(fp);
1131}
1132
1133/**
1134 * fc_rport_recv_req() - Handle a received ELS request from a rport
1135 * @sp: current sequence in the PLOGI exchange
1136 * @fp: response frame
1137 * @lport: Fibre Channel local port
1138 *
1139 * Locking Note: Called with the lport lock held.
1140 */
1141void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1142 struct fc_lport *lport)
1143{
1144 struct fc_seq_els_data els_data;
1145
1146 /*
1147 * Handle PLOGI and LOGO requests separately, since they
1148 * don't require prior login.
1149 * Check for unsupported opcodes first and reject them.
1150 * For some ops, it would be incorrect to reject with "PLOGI required".
1151 */
1152 switch (fc_frame_payload_op(fp)) {
1153 case ELS_PLOGI:
1154 fc_rport_recv_plogi_req(lport, sp, fp);
1155 break;
1156 case ELS_LOGO:
1157 fc_rport_recv_logo_req(lport, sp, fp);
1158 break;
1159 case ELS_PRLI:
1160 case ELS_PRLO:
1161 case ELS_ADISC:
1162 case ELS_RRQ:
1163 case ELS_REC:
1164 fc_rport_recv_els_req(lport, sp, fp);
1165 break;
1166 default:
1167 fc_frame_free(fp);
1168 els_data.fp = NULL;
1169 els_data.reason = ELS_RJT_UNSUP;
1170 els_data.explan = ELS_EXPL_NONE;
1171 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1172 break;
1173 }
941} 1174}
942 1175
943/** 1176/**
944 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request 1177 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
945 * @rport: Fibre Channel remote port that initiated PLOGI 1178 * @lport: local port
946 * @sp: current sequence in the PLOGI exchange 1179 * @sp: current sequence in the PLOGI exchange
947 * @fp: PLOGI request frame 1180 * @fp: PLOGI request frame
948 * 1181 *
949 * Locking Note: The rport lock is exected to be held before calling 1182 * Locking Note: The rport lock is held before calling this function.
950 * this function.
951 */ 1183 */
952static void fc_rport_recv_plogi_req(struct fc_rport *rport, 1184static void fc_rport_recv_plogi_req(struct fc_lport *lport,
953 struct fc_seq *sp, struct fc_frame *rx_fp) 1185 struct fc_seq *sp, struct fc_frame *rx_fp)
954{ 1186{
955 struct fc_rport_libfc_priv *rdata = rport->dd_data; 1187 struct fc_disc *disc;
956 struct fc_lport *lport = rdata->local_port; 1188 struct fc_rport_priv *rdata;
957 struct fc_frame *fp = rx_fp; 1189 struct fc_frame *fp = rx_fp;
958 struct fc_exch *ep; 1190 struct fc_exch *ep;
959 struct fc_frame_header *fh; 1191 struct fc_frame_header *fh;
960 struct fc_els_flogi *pl; 1192 struct fc_els_flogi *pl;
961 struct fc_seq_els_data rjt_data; 1193 struct fc_seq_els_data rjt_data;
962 u32 sid; 1194 u32 sid, f_ctl;
963 u64 wwpn;
964 u64 wwnn;
965 enum fc_els_rjt_reason reject = 0;
966 u32 f_ctl;
967 rjt_data.fp = NULL;
968 1195
1196 rjt_data.fp = NULL;
969 fh = fc_frame_header_get(fp); 1197 fh = fc_frame_header_get(fp);
1198 sid = ntoh24(fh->fh_s_id);
970 1199
971 FC_RPORT_DBG(rport, "Received PLOGI request while in state %s\n", 1200 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
972 fc_rport_state(rport));
973 1201
974 sid = ntoh24(fh->fh_s_id);
975 pl = fc_frame_payload_get(fp, sizeof(*pl)); 1202 pl = fc_frame_payload_get(fp, sizeof(*pl));
976 if (!pl) { 1203 if (!pl) {
977 FC_RPORT_DBG(rport, "Received PLOGI too short\n"); 1204 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
978 WARN_ON(1); 1205 rjt_data.reason = ELS_RJT_PROT;
979 /* XXX TBD: send reject? */ 1206 rjt_data.explan = ELS_EXPL_INV_LEN;
980 fc_frame_free(fp); 1207 goto reject;
981 return; 1208 }
1209
1210 disc = &lport->disc;
1211 mutex_lock(&disc->disc_mutex);
1212 rdata = lport->tt.rport_create(lport, sid);
1213 if (!rdata) {
1214 mutex_unlock(&disc->disc_mutex);
1215 rjt_data.reason = ELS_RJT_UNAB;
1216 rjt_data.explan = ELS_EXPL_INSUF_RES;
1217 goto reject;
982 } 1218 }
983 wwpn = get_unaligned_be64(&pl->fl_wwpn); 1219
984 wwnn = get_unaligned_be64(&pl->fl_wwnn); 1220 mutex_lock(&rdata->rp_mutex);
1221 mutex_unlock(&disc->disc_mutex);
1222
1223 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1224 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
985 1225
986 /* 1226 /*
987 * If the session was just created, possibly due to the incoming PLOGI, 1227 * If the rport was just created, possibly due to the incoming PLOGI,
988 * set the state appropriately and accept the PLOGI. 1228 * set the state appropriately and accept the PLOGI.
989 * 1229 *
990 * If we had also sent a PLOGI, and if the received PLOGI is from a 1230 * If we had also sent a PLOGI, and if the received PLOGI is from a
@@ -996,86 +1236,76 @@ static void fc_rport_recv_plogi_req(struct fc_rport *rport,
996 */ 1236 */
997 switch (rdata->rp_state) { 1237 switch (rdata->rp_state) {
998 case RPORT_ST_INIT: 1238 case RPORT_ST_INIT:
999 FC_RPORT_DBG(rport, "Received PLOGI, wwpn %llx state INIT " 1239 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1000 "- reject\n", (unsigned long long)wwpn);
1001 reject = ELS_RJT_UNSUP;
1002 break; 1240 break;
1003 case RPORT_ST_PLOGI: 1241 case RPORT_ST_PLOGI:
1004 FC_RPORT_DBG(rport, "Received PLOGI in PLOGI state %d\n", 1242 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1005 rdata->rp_state); 1243 if (rdata->ids.port_name < lport->wwpn) {
1006 if (wwpn < lport->wwpn) 1244 mutex_unlock(&rdata->rp_mutex);
1007 reject = ELS_RJT_INPROG; 1245 rjt_data.reason = ELS_RJT_INPROG;
1246 rjt_data.explan = ELS_EXPL_NONE;
1247 goto reject;
1248 }
1008 break; 1249 break;
1009 case RPORT_ST_PRLI: 1250 case RPORT_ST_PRLI:
1010 case RPORT_ST_READY: 1251 case RPORT_ST_READY:
1011 FC_RPORT_DBG(rport, "Received PLOGI in logged-in state %d " 1252 case RPORT_ST_ADISC:
1253 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1012 "- ignored for now\n", rdata->rp_state); 1254 "- ignored for now\n", rdata->rp_state);
1013 /* XXX TBD - should reset */ 1255 /* XXX TBD - should reset */
1014 break; 1256 break;
1015 case RPORT_ST_NONE: 1257 case RPORT_ST_DELETE:
1016 default: 1258 default:
1017 FC_RPORT_DBG(rport, "Received PLOGI in unexpected " 1259 FC_RPORT_DBG(rdata, "Received PLOGI in unexpected state %d\n",
1018 "state %d\n", rdata->rp_state); 1260 rdata->rp_state);
1019 fc_frame_free(fp); 1261 fc_frame_free(rx_fp);
1020 return; 1262 goto out;
1021 break;
1022 } 1263 }
1023 1264
1024 if (reject) { 1265 /*
1025 rjt_data.reason = reject; 1266 * Get session payload size from incoming PLOGI.
1026 rjt_data.explan = ELS_EXPL_NONE; 1267 */
1027 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); 1268 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1028 fc_frame_free(fp); 1269 fc_frame_free(rx_fp);
1029 } else { 1270
1030 fp = fc_frame_alloc(lport, sizeof(*pl)); 1271 /*
1031 if (fp == NULL) { 1272 * Send LS_ACC. If this fails, the originator should retry.
1032 fp = rx_fp; 1273 */
1033 rjt_data.reason = ELS_RJT_UNAB; 1274 sp = lport->tt.seq_start_next(sp);
1034 rjt_data.explan = ELS_EXPL_NONE; 1275 if (!sp)
1035 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); 1276 goto out;
1036 fc_frame_free(fp); 1277 fp = fc_frame_alloc(lport, sizeof(*pl));
1037 } else { 1278 if (!fp)
1038 sp = lport->tt.seq_start_next(sp); 1279 goto out;
1039 WARN_ON(!sp); 1280
1040 fc_rport_set_name(rport, wwpn, wwnn); 1281 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1041 1282 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1042 /* 1283 ep = fc_seq_exch(sp);
1043 * Get session payload size from incoming PLOGI. 1284 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1044 */ 1285 FC_TYPE_ELS, f_ctl, 0);
1045 rport->maxframe_size = 1286 lport->tt.seq_send(lport, sp, fp);
1046 fc_plogi_get_maxframe(pl, lport->mfs); 1287 fc_rport_enter_prli(rdata);
1047 fc_frame_free(rx_fp); 1288out:
1048 fc_plogi_fill(lport, fp, ELS_LS_ACC); 1289 mutex_unlock(&rdata->rp_mutex);
1049 1290 return;
1050 /* 1291
1051 * Send LS_ACC. If this fails, 1292reject:
1052 * the originator should retry. 1293 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1053 */ 1294 fc_frame_free(fp);
1054 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1055 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1056 ep = fc_seq_exch(sp);
1057 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1058 FC_TYPE_ELS, f_ctl, 0);
1059 lport->tt.seq_send(lport, sp, fp);
1060 if (rdata->rp_state == RPORT_ST_PLOGI)
1061 fc_rport_enter_prli(rport);
1062 }
1063 }
1064} 1295}
1065 1296
1066/** 1297/**
1067 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request 1298 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
1068 * @rport: Fibre Channel remote port that initiated PRLI 1299 * @rdata: private remote port data
1069 * @sp: current sequence in the PRLI exchange 1300 * @sp: current sequence in the PRLI exchange
1070 * @fp: PRLI request frame 1301 * @fp: PRLI request frame
1071 * 1302 *
1072 * Locking Note: The rport lock is exected to be held before calling 1303 * Locking Note: The rport lock is exected to be held before calling
1073 * this function. 1304 * this function.
1074 */ 1305 */
1075static void fc_rport_recv_prli_req(struct fc_rport *rport, 1306static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1076 struct fc_seq *sp, struct fc_frame *rx_fp) 1307 struct fc_seq *sp, struct fc_frame *rx_fp)
1077{ 1308{
1078 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1079 struct fc_lport *lport = rdata->local_port; 1309 struct fc_lport *lport = rdata->local_port;
1080 struct fc_exch *ep; 1310 struct fc_exch *ep;
1081 struct fc_frame *fp; 1311 struct fc_frame *fp;
@@ -1099,12 +1329,14 @@ static void fc_rport_recv_prli_req(struct fc_rport *rport,
1099 1329
1100 fh = fc_frame_header_get(rx_fp); 1330 fh = fc_frame_header_get(rx_fp);
1101 1331
1102 FC_RPORT_DBG(rport, "Received PRLI request while in state %s\n", 1332 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1103 fc_rport_state(rport)); 1333 fc_rport_state(rdata));
1104 1334
1105 switch (rdata->rp_state) { 1335 switch (rdata->rp_state) {
1106 case RPORT_ST_PRLI: 1336 case RPORT_ST_PRLI:
1337 case RPORT_ST_RTV:
1107 case RPORT_ST_READY: 1338 case RPORT_ST_READY:
1339 case RPORT_ST_ADISC:
1108 reason = ELS_RJT_NONE; 1340 reason = ELS_RJT_NONE;
1109 break; 1341 break;
1110 default: 1342 default:
@@ -1149,6 +1381,9 @@ static void fc_rport_recv_prli_req(struct fc_rport *rport,
1149 pp->prli.prli_len = htons(len); 1381 pp->prli.prli_len = htons(len);
1150 len -= sizeof(struct fc_els_prli); 1382 len -= sizeof(struct fc_els_prli);
1151 1383
1384 /* reinitialize remote port roles */
1385 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1386
1152 /* 1387 /*
1153 * Go through all the service parameter pages and build 1388 * Go through all the service parameter pages and build
1154 * response. If plen indicates longer SPP than standard, 1389 * response. If plen indicates longer SPP than standard,
@@ -1169,12 +1404,12 @@ static void fc_rport_recv_prli_req(struct fc_rport *rport,
1169 fcp_parm = ntohl(rspp->spp_params); 1404 fcp_parm = ntohl(rspp->spp_params);
1170 if (fcp_parm * FCP_SPPF_RETRY) 1405 if (fcp_parm * FCP_SPPF_RETRY)
1171 rdata->flags |= FC_RP_FLAGS_RETRY; 1406 rdata->flags |= FC_RP_FLAGS_RETRY;
1172 rport->supported_classes = FC_COS_CLASS3; 1407 rdata->supported_classes = FC_COS_CLASS3;
1173 if (fcp_parm & FCP_SPPF_INIT_FCN) 1408 if (fcp_parm & FCP_SPPF_INIT_FCN)
1174 roles |= FC_RPORT_ROLE_FCP_INITIATOR; 1409 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1175 if (fcp_parm & FCP_SPPF_TARG_FCN) 1410 if (fcp_parm & FCP_SPPF_TARG_FCN)
1176 roles |= FC_RPORT_ROLE_FCP_TARGET; 1411 roles |= FC_RPORT_ROLE_FCP_TARGET;
1177 rport->roles = roles; 1412 rdata->ids.roles = roles;
1178 1413
1179 spp->spp_params = 1414 spp->spp_params =
1180 htonl(lport->service_params); 1415 htonl(lport->service_params);
@@ -1204,9 +1439,10 @@ static void fc_rport_recv_prli_req(struct fc_rport *rport,
1204 */ 1439 */
1205 switch (rdata->rp_state) { 1440 switch (rdata->rp_state) {
1206 case RPORT_ST_PRLI: 1441 case RPORT_ST_PRLI:
1207 fc_rport_enter_ready(rport); 1442 fc_rport_enter_ready(rdata);
1208 break; 1443 break;
1209 case RPORT_ST_READY: 1444 case RPORT_ST_READY:
1445 case RPORT_ST_ADISC:
1210 break; 1446 break;
1211 default: 1447 default:
1212 break; 1448 break;
@@ -1217,17 +1453,17 @@ static void fc_rport_recv_prli_req(struct fc_rport *rport,
1217 1453
1218/** 1454/**
1219 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request 1455 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
1220 * @rport: Fibre Channel remote port that initiated PRLO 1456 * @rdata: private remote port data
1221 * @sp: current sequence in the PRLO exchange 1457 * @sp: current sequence in the PRLO exchange
1222 * @fp: PRLO request frame 1458 * @fp: PRLO request frame
1223 * 1459 *
1224 * Locking Note: The rport lock is exected to be held before calling 1460 * Locking Note: The rport lock is exected to be held before calling
1225 * this function. 1461 * this function.
1226 */ 1462 */
1227static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp, 1463static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1464 struct fc_seq *sp,
1228 struct fc_frame *fp) 1465 struct fc_frame *fp)
1229{ 1466{
1230 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1231 struct fc_lport *lport = rdata->local_port; 1467 struct fc_lport *lport = rdata->local_port;
1232 1468
1233 struct fc_frame_header *fh; 1469 struct fc_frame_header *fh;
@@ -1235,13 +1471,8 @@ static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1235 1471
1236 fh = fc_frame_header_get(fp); 1472 fh = fc_frame_header_get(fp);
1237 1473
1238 FC_RPORT_DBG(rport, "Received PRLO request while in state %s\n", 1474 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1239 fc_rport_state(rport)); 1475 fc_rport_state(rdata));
1240
1241 if (rdata->rp_state == RPORT_ST_NONE) {
1242 fc_frame_free(fp);
1243 return;
1244 }
1245 1476
1246 rjt_data.fp = NULL; 1477 rjt_data.fp = NULL;
1247 rjt_data.reason = ELS_RJT_UNAB; 1478 rjt_data.reason = ELS_RJT_UNAB;
@@ -1252,35 +1483,46 @@ static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1252 1483
1253/** 1484/**
1254 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request 1485 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
1255 * @rport: Fibre Channel remote port that initiated LOGO 1486 * @lport: local port.
1256 * @sp: current sequence in the LOGO exchange 1487 * @sp: current sequence in the LOGO exchange
1257 * @fp: LOGO request frame 1488 * @fp: LOGO request frame
1258 * 1489 *
1259 * Locking Note: The rport lock is exected to be held before calling 1490 * Locking Note: The rport lock is exected to be held before calling
1260 * this function. 1491 * this function.
1261 */ 1492 */
1262static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp, 1493static void fc_rport_recv_logo_req(struct fc_lport *lport,
1494 struct fc_seq *sp,
1263 struct fc_frame *fp) 1495 struct fc_frame *fp)
1264{ 1496{
1265 struct fc_frame_header *fh; 1497 struct fc_frame_header *fh;
1266 struct fc_rport_libfc_priv *rdata = rport->dd_data; 1498 struct fc_rport_priv *rdata;
1267 struct fc_lport *lport = rdata->local_port; 1499 u32 sid;
1268
1269 fh = fc_frame_header_get(fp);
1270 1500
1271 FC_RPORT_DBG(rport, "Received LOGO request while in state %s\n", 1501 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1272 fc_rport_state(rport));
1273 1502
1274 if (rdata->rp_state == RPORT_ST_NONE) { 1503 fh = fc_frame_header_get(fp);
1275 fc_frame_free(fp); 1504 sid = ntoh24(fh->fh_s_id);
1276 return;
1277 }
1278 1505
1279 rdata->event = RPORT_EV_LOGO; 1506 mutex_lock(&lport->disc.disc_mutex);
1280 fc_rport_state_enter(rport, RPORT_ST_NONE); 1507 rdata = lport->tt.rport_lookup(lport, sid);
1281 queue_work(rport_event_queue, &rdata->event_work); 1508 if (rdata) {
1509 mutex_lock(&rdata->rp_mutex);
1510 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1511 fc_rport_state(rdata));
1282 1512
1283 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL); 1513 /*
1514 * If the remote port was created due to discovery,
1515 * log back in. It may have seen a stale RSCN about us.
1516 */
1517 if (rdata->rp_state != RPORT_ST_DELETE && rdata->disc_id)
1518 fc_rport_enter_plogi(rdata);
1519 else
1520 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1521 mutex_unlock(&rdata->rp_mutex);
1522 } else
1523 FC_RPORT_ID_DBG(lport, sid,
1524 "Received LOGO from non-logged-in port\n");
1525 mutex_unlock(&lport->disc.disc_mutex);
1284 fc_frame_free(fp); 1526 fc_frame_free(fp);
1285} 1527}
1286 1528
@@ -1291,8 +1533,11 @@ static void fc_rport_flush_queue(void)
1291 1533
1292int fc_rport_init(struct fc_lport *lport) 1534int fc_rport_init(struct fc_lport *lport)
1293{ 1535{
1536 if (!lport->tt.rport_lookup)
1537 lport->tt.rport_lookup = fc_rport_lookup;
1538
1294 if (!lport->tt.rport_create) 1539 if (!lport->tt.rport_create)
1295 lport->tt.rport_create = fc_rport_rogue_create; 1540 lport->tt.rport_create = fc_rport_create;
1296 1541
1297 if (!lport->tt.rport_login) 1542 if (!lport->tt.rport_login)
1298 lport->tt.rport_login = fc_rport_login; 1543 lport->tt.rport_login = fc_rport_login;
@@ -1306,6 +1551,9 @@ int fc_rport_init(struct fc_lport *lport)
1306 if (!lport->tt.rport_flush_queue) 1551 if (!lport->tt.rport_flush_queue)
1307 lport->tt.rport_flush_queue = fc_rport_flush_queue; 1552 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1308 1553
1554 if (!lport->tt.rport_destroy)
1555 lport->tt.rport_destroy = fc_rport_destroy;
1556
1309 return 0; 1557 return 0;
1310} 1558}
1311EXPORT_SYMBOL(fc_rport_init); 1559EXPORT_SYMBOL(fc_rport_init);
@@ -1327,8 +1575,8 @@ EXPORT_SYMBOL(fc_destroy_rport);
1327 1575
1328void fc_rport_terminate_io(struct fc_rport *rport) 1576void fc_rport_terminate_io(struct fc_rport *rport)
1329{ 1577{
1330 struct fc_rport_libfc_priv *rdata = rport->dd_data; 1578 struct fc_rport_libfc_priv *rp = rport->dd_data;
1331 struct fc_lport *lport = rdata->local_port; 1579 struct fc_lport *lport = rp->local_port;
1332 1580
1333 lport->tt.exch_mgr_reset(lport, 0, rport->port_id); 1581 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1334 lport->tt.exch_mgr_reset(lport, rport->port_id, 0); 1582 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);