[SCSI] bfa: remove all OS wrappers
[pandora-kernel.git] / drivers / scsi / bfa / bfa_fcs_rport.c
1 /*
2  * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17
18 /*
19  *  rport.c Remote port implementation.
20  */
21
22 #include "bfad_drv.h"
23 #include "bfa_fcs.h"
24 #include "bfa_fcbuild.h"
25
26 BFA_TRC_FILE(FCS, RPORT);
27
28 static u32
29 bfa_fcs_rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT * 1000;
30          /* In millisecs */
31 /*
32  * forward declarations
33  */
34 static struct bfa_fcs_rport_s *bfa_fcs_rport_alloc(
35                 struct bfa_fcs_lport_s *port, wwn_t pwwn, u32 rpid);
36 static void     bfa_fcs_rport_free(struct bfa_fcs_rport_s *rport);
37 static void     bfa_fcs_rport_hal_online(struct bfa_fcs_rport_s *rport);
38 static void     bfa_fcs_rport_online_action(struct bfa_fcs_rport_s *rport);
39 static void     bfa_fcs_rport_offline_action(struct bfa_fcs_rport_s *rport);
40 static void     bfa_fcs_rport_update(struct bfa_fcs_rport_s *rport,
41                                         struct fc_logi_s *plogi);
42 static void     bfa_fcs_rport_timeout(void *arg);
43 static void     bfa_fcs_rport_send_plogi(void *rport_cbarg,
44                                          struct bfa_fcxp_s *fcxp_alloced);
45 static void     bfa_fcs_rport_send_plogiacc(void *rport_cbarg,
46                                         struct bfa_fcxp_s *fcxp_alloced);
47 static void     bfa_fcs_rport_plogi_response(void *fcsarg,
48                                 struct bfa_fcxp_s *fcxp, void *cbarg,
49                                 bfa_status_t req_status, u32 rsp_len,
50                                 u32 resid_len, struct fchs_s *rsp_fchs);
51 static void     bfa_fcs_rport_send_adisc(void *rport_cbarg,
52                                          struct bfa_fcxp_s *fcxp_alloced);
53 static void     bfa_fcs_rport_adisc_response(void *fcsarg,
54                                 struct bfa_fcxp_s *fcxp, void *cbarg,
55                                 bfa_status_t req_status, u32 rsp_len,
56                                 u32 resid_len, struct fchs_s *rsp_fchs);
57 static void     bfa_fcs_rport_send_nsdisc(void *rport_cbarg,
58                                          struct bfa_fcxp_s *fcxp_alloced);
59 static void     bfa_fcs_rport_gidpn_response(void *fcsarg,
60                                 struct bfa_fcxp_s *fcxp, void *cbarg,
61                                 bfa_status_t req_status, u32 rsp_len,
62                                 u32 resid_len, struct fchs_s *rsp_fchs);
63 static void     bfa_fcs_rport_gpnid_response(void *fcsarg,
64                                 struct bfa_fcxp_s *fcxp, void *cbarg,
65                                 bfa_status_t req_status, u32 rsp_len,
66                                 u32 resid_len, struct fchs_s *rsp_fchs);
67 static void     bfa_fcs_rport_send_logo(void *rport_cbarg,
68                                         struct bfa_fcxp_s *fcxp_alloced);
69 static void     bfa_fcs_rport_send_logo_acc(void *rport_cbarg);
70 static void     bfa_fcs_rport_process_prli(struct bfa_fcs_rport_s *rport,
71                                         struct fchs_s *rx_fchs, u16 len);
72 static void     bfa_fcs_rport_send_ls_rjt(struct bfa_fcs_rport_s *rport,
73                                 struct fchs_s *rx_fchs, u8 reason_code,
74                                           u8 reason_code_expl);
75 static void     bfa_fcs_rport_process_adisc(struct bfa_fcs_rport_s *rport,
76                                 struct fchs_s *rx_fchs, u16 len);
77 static void bfa_fcs_rport_send_prlo_acc(struct bfa_fcs_rport_s *rport);
78
79 static void     bfa_fcs_rport_sm_uninit(struct bfa_fcs_rport_s *rport,
80                                         enum rport_event event);
81 static void     bfa_fcs_rport_sm_plogi_sending(struct bfa_fcs_rport_s *rport,
82                                                 enum rport_event event);
83 static void     bfa_fcs_rport_sm_plogiacc_sending(struct bfa_fcs_rport_s *rport,
84                                                   enum rport_event event);
85 static void     bfa_fcs_rport_sm_plogi_retry(struct bfa_fcs_rport_s *rport,
86                                                 enum rport_event event);
87 static void     bfa_fcs_rport_sm_plogi(struct bfa_fcs_rport_s *rport,
88                                         enum rport_event event);
89 static void     bfa_fcs_rport_sm_hal_online(struct bfa_fcs_rport_s *rport,
90                                                 enum rport_event event);
91 static void     bfa_fcs_rport_sm_online(struct bfa_fcs_rport_s *rport,
92                                         enum rport_event event);
93 static void     bfa_fcs_rport_sm_nsquery_sending(struct bfa_fcs_rport_s *rport,
94                                                  enum rport_event event);
95 static void     bfa_fcs_rport_sm_nsquery(struct bfa_fcs_rport_s *rport,
96                                          enum rport_event event);
97 static void     bfa_fcs_rport_sm_adisc_sending(struct bfa_fcs_rport_s *rport,
98                                                 enum rport_event event);
99 static void     bfa_fcs_rport_sm_adisc(struct bfa_fcs_rport_s *rport,
100                                         enum rport_event event);
101 static void     bfa_fcs_rport_sm_fc4_logorcv(struct bfa_fcs_rport_s *rport,
102                                                 enum rport_event event);
103 static void     bfa_fcs_rport_sm_fc4_logosend(struct bfa_fcs_rport_s *rport,
104                                                 enum rport_event event);
105 static void     bfa_fcs_rport_sm_fc4_offline(struct bfa_fcs_rport_s *rport,
106                                                 enum rport_event event);
107 static void     bfa_fcs_rport_sm_hcb_offline(struct bfa_fcs_rport_s *rport,
108                                                 enum rport_event event);
109 static void     bfa_fcs_rport_sm_hcb_logorcv(struct bfa_fcs_rport_s *rport,
110                                                 enum rport_event event);
111 static void     bfa_fcs_rport_sm_hcb_logosend(struct bfa_fcs_rport_s *rport,
112                                                 enum rport_event event);
113 static void     bfa_fcs_rport_sm_logo_sending(struct bfa_fcs_rport_s *rport,
114                                                 enum rport_event event);
115 static void     bfa_fcs_rport_sm_offline(struct bfa_fcs_rport_s *rport,
116                                          enum rport_event event);
117 static void     bfa_fcs_rport_sm_nsdisc_sending(struct bfa_fcs_rport_s *rport,
118                                                 enum rport_event event);
119 static void     bfa_fcs_rport_sm_nsdisc_retry(struct bfa_fcs_rport_s *rport,
120                                                 enum rport_event event);
121 static void     bfa_fcs_rport_sm_nsdisc_sent(struct bfa_fcs_rport_s *rport,
122                                                 enum rport_event event);
123 static void     bfa_fcs_rport_sm_nsdisc_sent(struct bfa_fcs_rport_s *rport,
124                                                 enum rport_event event);
125
126 static struct bfa_sm_table_s rport_sm_table[] = {
127         {BFA_SM(bfa_fcs_rport_sm_uninit), BFA_RPORT_UNINIT},
128         {BFA_SM(bfa_fcs_rport_sm_plogi_sending), BFA_RPORT_PLOGI},
129         {BFA_SM(bfa_fcs_rport_sm_plogiacc_sending), BFA_RPORT_ONLINE},
130         {BFA_SM(bfa_fcs_rport_sm_plogi_retry), BFA_RPORT_PLOGI_RETRY},
131         {BFA_SM(bfa_fcs_rport_sm_plogi), BFA_RPORT_PLOGI},
132         {BFA_SM(bfa_fcs_rport_sm_hal_online), BFA_RPORT_ONLINE},
133         {BFA_SM(bfa_fcs_rport_sm_online), BFA_RPORT_ONLINE},
134         {BFA_SM(bfa_fcs_rport_sm_nsquery_sending), BFA_RPORT_NSQUERY},
135         {BFA_SM(bfa_fcs_rport_sm_nsquery), BFA_RPORT_NSQUERY},
136         {BFA_SM(bfa_fcs_rport_sm_adisc_sending), BFA_RPORT_ADISC},
137         {BFA_SM(bfa_fcs_rport_sm_adisc), BFA_RPORT_ADISC},
138         {BFA_SM(bfa_fcs_rport_sm_fc4_logorcv), BFA_RPORT_LOGORCV},
139         {BFA_SM(bfa_fcs_rport_sm_fc4_logosend), BFA_RPORT_LOGO},
140         {BFA_SM(bfa_fcs_rport_sm_fc4_offline), BFA_RPORT_OFFLINE},
141         {BFA_SM(bfa_fcs_rport_sm_hcb_offline), BFA_RPORT_OFFLINE},
142         {BFA_SM(bfa_fcs_rport_sm_hcb_logorcv), BFA_RPORT_LOGORCV},
143         {BFA_SM(bfa_fcs_rport_sm_hcb_logosend), BFA_RPORT_LOGO},
144         {BFA_SM(bfa_fcs_rport_sm_logo_sending), BFA_RPORT_LOGO},
145         {BFA_SM(bfa_fcs_rport_sm_offline), BFA_RPORT_OFFLINE},
146         {BFA_SM(bfa_fcs_rport_sm_nsdisc_sending), BFA_RPORT_NSDISC},
147         {BFA_SM(bfa_fcs_rport_sm_nsdisc_retry), BFA_RPORT_NSDISC},
148         {BFA_SM(bfa_fcs_rport_sm_nsdisc_sent), BFA_RPORT_NSDISC},
149 };
150
151 /*
152  *              Beginning state.
153  */
154 static void
155 bfa_fcs_rport_sm_uninit(struct bfa_fcs_rport_s *rport, enum rport_event event)
156 {
157         bfa_trc(rport->fcs, rport->pwwn);
158         bfa_trc(rport->fcs, rport->pid);
159         bfa_trc(rport->fcs, event);
160
161         switch (event) {
162         case RPSM_EVENT_PLOGI_SEND:
163                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogi_sending);
164                 rport->plogi_retries = 0;
165                 bfa_fcs_rport_send_plogi(rport, NULL);
166                 break;
167
168         case RPSM_EVENT_PLOGI_RCVD:
169                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
170                 bfa_fcs_rport_send_plogiacc(rport, NULL);
171                 break;
172
173         case RPSM_EVENT_PLOGI_COMP:
174                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
175                 bfa_fcs_rport_hal_online(rport);
176                 break;
177
178         case RPSM_EVENT_ADDRESS_CHANGE:
179         case RPSM_EVENT_ADDRESS_DISC:
180                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
181                 rport->ns_retries = 0;
182                 bfa_fcs_rport_send_nsdisc(rport, NULL);
183                 break;
184         default:
185                 bfa_sm_fault(rport->fcs, event);
186         }
187 }
188
189 /*
190  *              PLOGI is being sent.
191  */
192 static void
193 bfa_fcs_rport_sm_plogi_sending(struct bfa_fcs_rport_s *rport,
194          enum rport_event event)
195 {
196         bfa_trc(rport->fcs, rport->pwwn);
197         bfa_trc(rport->fcs, rport->pid);
198         bfa_trc(rport->fcs, event);
199
200         switch (event) {
201         case RPSM_EVENT_FCXP_SENT:
202                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogi);
203                 break;
204
205         case RPSM_EVENT_DELETE:
206                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
207                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
208                 bfa_fcs_rport_free(rport);
209                 break;
210
211         case RPSM_EVENT_PLOGI_RCVD:
212                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
213                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
214                 bfa_fcs_rport_send_plogiacc(rport, NULL);
215                 break;
216
217         case RPSM_EVENT_ADDRESS_CHANGE:
218         case RPSM_EVENT_SCN:
219                 /* query the NS */
220                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
221                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
222                 rport->ns_retries = 0;
223                 bfa_fcs_rport_send_nsdisc(rport, NULL);
224                 break;
225
226         case RPSM_EVENT_LOGO_IMP:
227                 rport->pid = 0;
228                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
229                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
230                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
231                                 bfa_fcs_rport_timeout, rport,
232                                 bfa_fcs_rport_del_timeout);
233                 break;
234
235
236         default:
237                 bfa_sm_fault(rport->fcs, event);
238         }
239 }
240
241 /*
242  *              PLOGI is being sent.
243  */
244 static void
245 bfa_fcs_rport_sm_plogiacc_sending(struct bfa_fcs_rport_s *rport,
246          enum rport_event event)
247 {
248         bfa_trc(rport->fcs, rport->pwwn);
249         bfa_trc(rport->fcs, rport->pid);
250         bfa_trc(rport->fcs, event);
251
252         switch (event) {
253         case RPSM_EVENT_FCXP_SENT:
254                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
255                 bfa_fcs_rport_hal_online(rport);
256                 break;
257
258         case RPSM_EVENT_DELETE:
259                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
260                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
261                 bfa_fcs_rport_free(rport);
262                 break;
263
264         case RPSM_EVENT_PLOGI_RCVD:
265         case RPSM_EVENT_SCN:
266                 /*
267                  * Ignore, SCN is possibly online notification.
268                  */
269                 break;
270
271         case RPSM_EVENT_ADDRESS_CHANGE:
272                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
273                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
274                 rport->ns_retries = 0;
275                 bfa_fcs_rport_send_nsdisc(rport, NULL);
276                 break;
277
278         case RPSM_EVENT_LOGO_IMP:
279                 rport->pid = 0;
280                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
281                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
282                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
283                                 bfa_fcs_rport_timeout, rport,
284                                 bfa_fcs_rport_del_timeout);
285                 break;
286
287         case RPSM_EVENT_HCB_OFFLINE:
288                 /*
289                  * Ignore BFA callback, on a PLOGI receive we call bfa offline.
290                  */
291                 break;
292
293         default:
294                 bfa_sm_fault(rport->fcs, event);
295         }
296 }
297
298 /*
299  *              PLOGI is sent.
300  */
301 static void
302 bfa_fcs_rport_sm_plogi_retry(struct bfa_fcs_rport_s *rport,
303                         enum rport_event event)
304 {
305         bfa_trc(rport->fcs, rport->pwwn);
306         bfa_trc(rport->fcs, rport->pid);
307         bfa_trc(rport->fcs, event);
308
309         switch (event) {
310         case RPSM_EVENT_TIMEOUT:
311                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogi_sending);
312                 bfa_fcs_rport_send_plogi(rport, NULL);
313                 break;
314
315         case RPSM_EVENT_DELETE:
316                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
317                 bfa_timer_stop(&rport->timer);
318                 bfa_fcs_rport_free(rport);
319                 break;
320
321         case RPSM_EVENT_PRLO_RCVD:
322         case RPSM_EVENT_LOGO_RCVD:
323                 break;
324
325         case RPSM_EVENT_PLOGI_RCVD:
326                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
327                 bfa_timer_stop(&rport->timer);
328                 bfa_fcs_rport_send_plogiacc(rport, NULL);
329                 break;
330
331         case RPSM_EVENT_ADDRESS_CHANGE:
332         case RPSM_EVENT_SCN:
333                 bfa_timer_stop(&rport->timer);
334                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
335                 rport->ns_retries = 0;
336                 bfa_fcs_rport_send_nsdisc(rport, NULL);
337                 break;
338
339         case RPSM_EVENT_LOGO_IMP:
340                 rport->pid = 0;
341                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
342                 bfa_timer_stop(&rport->timer);
343                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
344                                 bfa_fcs_rport_timeout, rport,
345                                 bfa_fcs_rport_del_timeout);
346                 break;
347
348         case RPSM_EVENT_PLOGI_COMP:
349                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
350                 bfa_timer_stop(&rport->timer);
351                 bfa_fcs_rport_hal_online(rport);
352                 break;
353
354         default:
355                 bfa_sm_fault(rport->fcs, event);
356         }
357 }
358
359 /*
360  *              PLOGI is sent.
361  */
362 static void
363 bfa_fcs_rport_sm_plogi(struct bfa_fcs_rport_s *rport, enum rport_event event)
364 {
365         bfa_trc(rport->fcs, rport->pwwn);
366         bfa_trc(rport->fcs, rport->pid);
367         bfa_trc(rport->fcs, event);
368
369         switch (event) {
370         case RPSM_EVENT_ACCEPTED:
371                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
372                 rport->plogi_retries = 0;
373                 bfa_fcs_rport_hal_online(rport);
374                 break;
375
376         case RPSM_EVENT_LOGO_RCVD:
377                 bfa_fcs_rport_send_logo_acc(rport);
378                 /*
379                  * !! fall through !!
380                  */
381         case RPSM_EVENT_PRLO_RCVD:
382                 if (rport->prlo == BFA_TRUE)
383                         bfa_fcs_rport_send_prlo_acc(rport);
384
385                 bfa_fcxp_discard(rport->fcxp);
386                 /*
387                  * !! fall through !!
388                  */
389         case RPSM_EVENT_FAILED:
390                 if (rport->plogi_retries < BFA_FCS_RPORT_MAX_RETRIES) {
391                         rport->plogi_retries++;
392                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogi_retry);
393                         bfa_timer_start(rport->fcs->bfa, &rport->timer,
394                                         bfa_fcs_rport_timeout, rport,
395                                         BFA_FCS_RETRY_TIMEOUT);
396                 } else {
397                         bfa_stats(rport->port, rport_del_max_plogi_retry);
398                         rport->pid = 0;
399                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
400                         bfa_timer_start(rport->fcs->bfa, &rport->timer,
401                                         bfa_fcs_rport_timeout, rport,
402                                         bfa_fcs_rport_del_timeout);
403                 }
404                 break;
405
406         case    RPSM_EVENT_PLOGI_RETRY:
407                 rport->plogi_retries = 0;
408                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogi_retry);
409                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
410                                 bfa_fcs_rport_timeout, rport,
411                                 (FC_RA_TOV * 1000));
412                 break;
413
414         case RPSM_EVENT_LOGO_IMP:
415                 rport->pid = 0;
416                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
417                 bfa_fcxp_discard(rport->fcxp);
418                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
419                                 bfa_fcs_rport_timeout, rport,
420                                 bfa_fcs_rport_del_timeout);
421                 break;
422
423         case RPSM_EVENT_ADDRESS_CHANGE:
424         case RPSM_EVENT_SCN:
425                 bfa_fcxp_discard(rport->fcxp);
426                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
427                 rport->ns_retries = 0;
428                 bfa_fcs_rport_send_nsdisc(rport, NULL);
429                 break;
430
431         case RPSM_EVENT_PLOGI_RCVD:
432                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
433                 bfa_fcxp_discard(rport->fcxp);
434                 bfa_fcs_rport_send_plogiacc(rport, NULL);
435                 break;
436
437         case RPSM_EVENT_DELETE:
438                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
439                 bfa_fcxp_discard(rport->fcxp);
440                 bfa_fcs_rport_free(rport);
441                 break;
442
443         case RPSM_EVENT_PLOGI_COMP:
444                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
445                 bfa_fcxp_discard(rport->fcxp);
446                 bfa_fcs_rport_hal_online(rport);
447                 break;
448
449         default:
450                 bfa_sm_fault(rport->fcs, event);
451         }
452 }
453
454 /*
455  *              PLOGI is complete. Awaiting BFA rport online callback. FC-4s
456  *              are offline.
457  */
458 static void
459 bfa_fcs_rport_sm_hal_online(struct bfa_fcs_rport_s *rport,
460                         enum rport_event event)
461 {
462         bfa_trc(rport->fcs, rport->pwwn);
463         bfa_trc(rport->fcs, rport->pid);
464         bfa_trc(rport->fcs, event);
465
466         switch (event) {
467         case RPSM_EVENT_HCB_ONLINE:
468                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_online);
469                 bfa_fcs_rport_online_action(rport);
470                 break;
471
472         case RPSM_EVENT_PRLO_RCVD:
473                 break;
474
475         case RPSM_EVENT_LOGO_RCVD:
476                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_logorcv);
477                 bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_OFFLINE);
478                 break;
479
480         case RPSM_EVENT_LOGO_IMP:
481         case RPSM_EVENT_ADDRESS_CHANGE:
482                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_offline);
483                 bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_OFFLINE);
484                 break;
485
486         case RPSM_EVENT_PLOGI_RCVD:
487                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
488                 bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_OFFLINE);
489                 bfa_fcs_rport_send_plogiacc(rport, NULL);
490                 break;
491
492         case RPSM_EVENT_DELETE:
493                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_logosend);
494                 bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_OFFLINE);
495                 break;
496
497         case RPSM_EVENT_SCN:
498                 /*
499                  * @todo
500                  * Ignore SCN - PLOGI just completed, FC-4 login should detect
501                  * device failures.
502                  */
503                 break;
504
505         default:
506                 bfa_sm_fault(rport->fcs, event);
507         }
508 }
509
510 /*
511  *              Rport is ONLINE. FC-4s active.
512  */
513 static void
514 bfa_fcs_rport_sm_online(struct bfa_fcs_rport_s *rport, enum rport_event event)
515 {
516         bfa_trc(rport->fcs, rport->pwwn);
517         bfa_trc(rport->fcs, rport->pid);
518         bfa_trc(rport->fcs, event);
519
520         switch (event) {
521         case RPSM_EVENT_SCN:
522                 if (bfa_fcs_fabric_is_switched(rport->port->fabric)) {
523                         bfa_sm_set_state(rport,
524                                          bfa_fcs_rport_sm_nsquery_sending);
525                         rport->ns_retries = 0;
526                         bfa_fcs_rport_send_nsdisc(rport, NULL);
527                 } else {
528                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_adisc_sending);
529                         bfa_fcs_rport_send_adisc(rport, NULL);
530                 }
531                 break;
532
533         case RPSM_EVENT_PLOGI_RCVD:
534         case RPSM_EVENT_LOGO_IMP:
535         case RPSM_EVENT_ADDRESS_CHANGE:
536                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
537                 bfa_fcs_rport_offline_action(rport);
538                 break;
539
540         case RPSM_EVENT_DELETE:
541                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logosend);
542                 bfa_fcs_rport_offline_action(rport);
543                 break;
544
545         case RPSM_EVENT_LOGO_RCVD:
546         case RPSM_EVENT_PRLO_RCVD:
547                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logorcv);
548                 bfa_fcs_rport_offline_action(rport);
549                 break;
550
551         case RPSM_EVENT_PLOGI_COMP:
552                 break;
553
554         default:
555                 bfa_sm_fault(rport->fcs, event);
556         }
557 }
558
559 /*
560  *              An SCN event is received in ONLINE state. NS query is being sent
561  *              prior to ADISC authentication with rport. FC-4s are paused.
562  */
563 static void
564 bfa_fcs_rport_sm_nsquery_sending(struct bfa_fcs_rport_s *rport,
565          enum rport_event event)
566 {
567         bfa_trc(rport->fcs, rport->pwwn);
568         bfa_trc(rport->fcs, rport->pid);
569         bfa_trc(rport->fcs, event);
570
571         switch (event) {
572         case RPSM_EVENT_FCXP_SENT:
573                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsquery);
574                 break;
575
576         case RPSM_EVENT_DELETE:
577                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logosend);
578                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
579                 bfa_fcs_rport_offline_action(rport);
580                 break;
581
582         case RPSM_EVENT_SCN:
583                 /*
584                  * ignore SCN, wait for response to query itself
585                  */
586                 break;
587
588         case RPSM_EVENT_LOGO_RCVD:
589         case RPSM_EVENT_PRLO_RCVD:
590                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logorcv);
591                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
592                 bfa_fcs_rport_offline_action(rport);
593                 break;
594
595         case RPSM_EVENT_LOGO_IMP:
596                 rport->pid = 0;
597                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
598                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
599                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
600                                 bfa_fcs_rport_timeout, rport,
601                                 bfa_fcs_rport_del_timeout);
602                 break;
603
604         case RPSM_EVENT_PLOGI_RCVD:
605         case RPSM_EVENT_ADDRESS_CHANGE:
606         case RPSM_EVENT_PLOGI_COMP:
607                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
608                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
609                 bfa_fcs_rport_offline_action(rport);
610                 break;
611
612         default:
613                 bfa_sm_fault(rport->fcs, event);
614         }
615 }
616
617 /*
618  *      An SCN event is received in ONLINE state. NS query is sent to rport.
619  *      FC-4s are paused.
620  */
621 static void
622 bfa_fcs_rport_sm_nsquery(struct bfa_fcs_rport_s *rport, enum rport_event event)
623 {
624         bfa_trc(rport->fcs, rport->pwwn);
625         bfa_trc(rport->fcs, rport->pid);
626         bfa_trc(rport->fcs, event);
627
628         switch (event) {
629         case RPSM_EVENT_ACCEPTED:
630                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_adisc_sending);
631                 bfa_fcs_rport_send_adisc(rport, NULL);
632                 break;
633
634         case RPSM_EVENT_FAILED:
635                 rport->ns_retries++;
636                 if (rport->ns_retries < BFA_FCS_RPORT_MAX_RETRIES) {
637                         bfa_sm_set_state(rport,
638                                          bfa_fcs_rport_sm_nsquery_sending);
639                         bfa_fcs_rport_send_nsdisc(rport, NULL);
640                 } else {
641                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
642                         bfa_fcs_rport_offline_action(rport);
643                 }
644                 break;
645
646         case RPSM_EVENT_DELETE:
647                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logosend);
648                 bfa_fcxp_discard(rport->fcxp);
649                 bfa_fcs_rport_offline_action(rport);
650                 break;
651
652         case RPSM_EVENT_SCN:
653                 break;
654
655         case RPSM_EVENT_LOGO_RCVD:
656         case RPSM_EVENT_PRLO_RCVD:
657                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logorcv);
658                 bfa_fcxp_discard(rport->fcxp);
659                 bfa_fcs_rport_offline_action(rport);
660                 break;
661
662         case RPSM_EVENT_PLOGI_COMP:
663         case RPSM_EVENT_ADDRESS_CHANGE:
664         case RPSM_EVENT_PLOGI_RCVD:
665         case RPSM_EVENT_LOGO_IMP:
666                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
667                 bfa_fcxp_discard(rport->fcxp);
668                 bfa_fcs_rport_offline_action(rport);
669                 break;
670
671         default:
672                 bfa_sm_fault(rport->fcs, event);
673         }
674 }
675
676 /*
677  *      An SCN event is received in ONLINE state. ADISC is being sent for
678  *      authenticating with rport. FC-4s are paused.
679  */
680 static void
681 bfa_fcs_rport_sm_adisc_sending(struct bfa_fcs_rport_s *rport,
682          enum rport_event event)
683 {
684         bfa_trc(rport->fcs, rport->pwwn);
685         bfa_trc(rport->fcs, rport->pid);
686         bfa_trc(rport->fcs, event);
687
688         switch (event) {
689         case RPSM_EVENT_FCXP_SENT:
690                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_adisc);
691                 break;
692
693         case RPSM_EVENT_DELETE:
694                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logosend);
695                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
696                 bfa_fcs_rport_offline_action(rport);
697                 break;
698
699         case RPSM_EVENT_LOGO_IMP:
700         case RPSM_EVENT_ADDRESS_CHANGE:
701                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
702                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
703                 bfa_fcs_rport_offline_action(rport);
704                 break;
705
706         case RPSM_EVENT_LOGO_RCVD:
707         case RPSM_EVENT_PRLO_RCVD:
708                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logorcv);
709                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
710                 bfa_fcs_rport_offline_action(rport);
711                 break;
712
713         case RPSM_EVENT_SCN:
714                 break;
715
716         case RPSM_EVENT_PLOGI_RCVD:
717                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
718                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
719                 bfa_fcs_rport_offline_action(rport);
720                 break;
721
722         default:
723                 bfa_sm_fault(rport->fcs, event);
724         }
725 }
726
727 /*
728  *              An SCN event is received in ONLINE state. ADISC is to rport.
729  *              FC-4s are paused.
730  */
731 static void
732 bfa_fcs_rport_sm_adisc(struct bfa_fcs_rport_s *rport, enum rport_event event)
733 {
734         bfa_trc(rport->fcs, rport->pwwn);
735         bfa_trc(rport->fcs, rport->pid);
736         bfa_trc(rport->fcs, event);
737
738         switch (event) {
739         case RPSM_EVENT_ACCEPTED:
740                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_online);
741                 break;
742
743         case RPSM_EVENT_PLOGI_RCVD:
744                 /*
745                  * Too complex to cleanup FC-4 & rport and then acc to PLOGI.
746                  * At least go offline when a PLOGI is received.
747                  */
748                 bfa_fcxp_discard(rport->fcxp);
749                 /*
750                  * !!! fall through !!!
751                  */
752
753         case RPSM_EVENT_FAILED:
754         case RPSM_EVENT_ADDRESS_CHANGE:
755                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
756                 bfa_fcs_rport_offline_action(rport);
757                 break;
758
759         case RPSM_EVENT_DELETE:
760                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logosend);
761                 bfa_fcxp_discard(rport->fcxp);
762                 bfa_fcs_rport_offline_action(rport);
763                 break;
764
765         case RPSM_EVENT_SCN:
766                 /*
767                  * already processing RSCN
768                  */
769                 break;
770
771         case RPSM_EVENT_LOGO_IMP:
772                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_offline);
773                 bfa_fcxp_discard(rport->fcxp);
774                 bfa_fcs_rport_offline_action(rport);
775                 break;
776
777         case RPSM_EVENT_LOGO_RCVD:
778         case RPSM_EVENT_PRLO_RCVD:
779                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logorcv);
780                 bfa_fcxp_discard(rport->fcxp);
781                 bfa_fcs_rport_offline_action(rport);
782                 break;
783
784         default:
785                 bfa_sm_fault(rport->fcs, event);
786         }
787 }
788
789 /*
790  *              Rport has sent LOGO. Awaiting FC-4 offline completion callback.
791  */
792 static void
793 bfa_fcs_rport_sm_fc4_logorcv(struct bfa_fcs_rport_s *rport,
794                         enum rport_event event)
795 {
796         bfa_trc(rport->fcs, rport->pwwn);
797         bfa_trc(rport->fcs, rport->pid);
798         bfa_trc(rport->fcs, event);
799
800         switch (event) {
801         case RPSM_EVENT_FC4_OFFLINE:
802                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_logorcv);
803                 bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_OFFLINE);
804                 break;
805
806         case RPSM_EVENT_DELETE:
807                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logosend);
808                 break;
809
810         case RPSM_EVENT_LOGO_RCVD:
811         case RPSM_EVENT_PRLO_RCVD:
812         case RPSM_EVENT_ADDRESS_CHANGE:
813                 break;
814
815         default:
816                 bfa_sm_fault(rport->fcs, event);
817         }
818 }
819
820 /*
821  *              LOGO needs to be sent to rport. Awaiting FC-4 offline completion
822  *              callback.
823  */
824 static void
825 bfa_fcs_rport_sm_fc4_logosend(struct bfa_fcs_rport_s *rport,
826          enum rport_event event)
827 {
828         bfa_trc(rport->fcs, rport->pwwn);
829         bfa_trc(rport->fcs, rport->pid);
830         bfa_trc(rport->fcs, event);
831
832         switch (event) {
833         case RPSM_EVENT_FC4_OFFLINE:
834                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_logosend);
835                 bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_OFFLINE);
836                 break;
837
838         default:
839                 bfa_sm_fault(rport->fcs, event);
840         }
841 }
842
843 /*
844  *      Rport is going offline. Awaiting FC-4 offline completion callback.
845  */
846 static void
847 bfa_fcs_rport_sm_fc4_offline(struct bfa_fcs_rport_s *rport,
848                         enum rport_event event)
849 {
850         bfa_trc(rport->fcs, rport->pwwn);
851         bfa_trc(rport->fcs, rport->pid);
852         bfa_trc(rport->fcs, event);
853
854         switch (event) {
855         case RPSM_EVENT_FC4_OFFLINE:
856                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_offline);
857                 bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_OFFLINE);
858                 break;
859
860         case RPSM_EVENT_SCN:
861         case RPSM_EVENT_LOGO_IMP:
862         case RPSM_EVENT_LOGO_RCVD:
863         case RPSM_EVENT_PRLO_RCVD:
864         case RPSM_EVENT_ADDRESS_CHANGE:
865                 /*
866                  * rport is already going offline.
867                  * SCN - ignore and wait till transitioning to offline state
868                  */
869                 break;
870
871         case RPSM_EVENT_DELETE:
872                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_fc4_logosend);
873                 break;
874
875         default:
876                 bfa_sm_fault(rport->fcs, event);
877         }
878 }
879
880 /*
881  *              Rport is offline. FC-4s are offline. Awaiting BFA rport offline
882  *              callback.
883  */
884 static void
885 bfa_fcs_rport_sm_hcb_offline(struct bfa_fcs_rport_s *rport,
886                                 enum rport_event event)
887 {
888         bfa_trc(rport->fcs, rport->pwwn);
889         bfa_trc(rport->fcs, rport->pid);
890         bfa_trc(rport->fcs, event);
891
892         switch (event) {
893         case RPSM_EVENT_HCB_OFFLINE:
894         case RPSM_EVENT_ADDRESS_CHANGE:
895                 if (bfa_fcs_lport_is_online(rport->port)) {
896                         if (bfa_fcs_fabric_is_switched(rport->port->fabric)) {
897                                 bfa_sm_set_state(rport,
898                                         bfa_fcs_rport_sm_nsdisc_sending);
899                                 rport->ns_retries = 0;
900                                 bfa_fcs_rport_send_nsdisc(rport, NULL);
901                         } else {
902                                 bfa_sm_set_state(rport,
903                                         bfa_fcs_rport_sm_plogi_sending);
904                                 rport->plogi_retries = 0;
905                                 bfa_fcs_rport_send_plogi(rport, NULL);
906                         }
907                 } else {
908                         rport->pid = 0;
909                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
910                         bfa_timer_start(rport->fcs->bfa, &rport->timer,
911                                         bfa_fcs_rport_timeout, rport,
912                                         bfa_fcs_rport_del_timeout);
913                 }
914                 break;
915
916         case RPSM_EVENT_DELETE:
917                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
918                 bfa_fcs_rport_free(rport);
919                 break;
920
921         case RPSM_EVENT_SCN:
922         case RPSM_EVENT_LOGO_RCVD:
923         case RPSM_EVENT_PRLO_RCVD:
924                 /*
925                  * Ignore, already offline.
926                  */
927                 break;
928
929         default:
930                 bfa_sm_fault(rport->fcs, event);
931         }
932 }
933
934 /*
935  *              Rport is offline. FC-4s are offline. Awaiting BFA rport offline
936  *              callback to send LOGO accept.
937  */
938 static void
939 bfa_fcs_rport_sm_hcb_logorcv(struct bfa_fcs_rport_s *rport,
940                         enum rport_event event)
941 {
942         bfa_trc(rport->fcs, rport->pwwn);
943         bfa_trc(rport->fcs, rport->pid);
944         bfa_trc(rport->fcs, event);
945
946         switch (event) {
947         case RPSM_EVENT_HCB_OFFLINE:
948         case RPSM_EVENT_ADDRESS_CHANGE:
949                 if (rport->pid && (rport->prlo == BFA_TRUE))
950                         bfa_fcs_rport_send_prlo_acc(rport);
951                 if (rport->pid && (rport->prlo == BFA_FALSE))
952                         bfa_fcs_rport_send_logo_acc(rport);
953                 /*
954                  * If the lport is online and if the rport is not a well
955                  * known address port,
956                  * we try to re-discover the r-port.
957                  */
958                 if (bfa_fcs_lport_is_online(rport->port) &&
959                         (!BFA_FCS_PID_IS_WKA(rport->pid))) {
960                         bfa_sm_set_state(rport,
961                                 bfa_fcs_rport_sm_nsdisc_sending);
962                         rport->ns_retries = 0;
963                         bfa_fcs_rport_send_nsdisc(rport, NULL);
964                 } else {
965                         /*
966                          * if it is not a well known address, reset the
967                          * pid to 0.
968                          */
969                         if (!BFA_FCS_PID_IS_WKA(rport->pid))
970                                 rport->pid = 0;
971                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
972                         bfa_timer_start(rport->fcs->bfa, &rport->timer,
973                                         bfa_fcs_rport_timeout, rport,
974                                         bfa_fcs_rport_del_timeout);
975                 }
976                 break;
977
978         case RPSM_EVENT_DELETE:
979                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_logosend);
980                 break;
981
982         case RPSM_EVENT_LOGO_IMP:
983                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hcb_offline);
984                 break;
985
986         case RPSM_EVENT_LOGO_RCVD:
987         case RPSM_EVENT_PRLO_RCVD:
988                 /*
989                  * Ignore - already processing a LOGO.
990                  */
991                 break;
992
993         default:
994                 bfa_sm_fault(rport->fcs, event);
995         }
996 }
997
998 /*
999  *              Rport is being deleted. FC-4s are offline.
1000  *  Awaiting BFA rport offline
1001  *              callback to send LOGO.
1002  */
1003 static void
1004 bfa_fcs_rport_sm_hcb_logosend(struct bfa_fcs_rport_s *rport,
1005                  enum rport_event event)
1006 {
1007         bfa_trc(rport->fcs, rport->pwwn);
1008         bfa_trc(rport->fcs, rport->pid);
1009         bfa_trc(rport->fcs, event);
1010
1011         switch (event) {
1012         case RPSM_EVENT_HCB_OFFLINE:
1013                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_logo_sending);
1014                 bfa_fcs_rport_send_logo(rport, NULL);
1015                 break;
1016
1017         case RPSM_EVENT_LOGO_RCVD:
1018         case RPSM_EVENT_PRLO_RCVD:
1019         case RPSM_EVENT_ADDRESS_CHANGE:
1020                 break;
1021
1022         default:
1023                 bfa_sm_fault(rport->fcs, event);
1024         }
1025 }
1026
1027 /*
1028  *              Rport is being deleted. FC-4s are offline. LOGO is being sent.
1029  */
1030 static void
1031 bfa_fcs_rport_sm_logo_sending(struct bfa_fcs_rport_s *rport,
1032          enum rport_event event)
1033 {
1034         bfa_trc(rport->fcs, rport->pwwn);
1035         bfa_trc(rport->fcs, rport->pid);
1036         bfa_trc(rport->fcs, event);
1037
1038         switch (event) {
1039         case RPSM_EVENT_FCXP_SENT:
1040                 /* Once LOGO is sent, we donot wait for the response */
1041                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1042                 bfa_fcs_rport_free(rport);
1043                 break;
1044
1045         case RPSM_EVENT_SCN:
1046         case RPSM_EVENT_ADDRESS_CHANGE:
1047                 break;
1048
1049         case RPSM_EVENT_LOGO_RCVD:
1050         case RPSM_EVENT_PRLO_RCVD:
1051                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1052                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
1053                 bfa_fcs_rport_free(rport);
1054                 break;
1055
1056         default:
1057                 bfa_sm_fault(rport->fcs, event);
1058         }
1059 }
1060
1061 /*
1062  *              Rport is offline. FC-4s are offline. BFA rport is offline.
1063  *              Timer active to delete stale rport.
1064  */
1065 static void
1066 bfa_fcs_rport_sm_offline(struct bfa_fcs_rport_s *rport, enum rport_event event)
1067 {
1068         bfa_trc(rport->fcs, rport->pwwn);
1069         bfa_trc(rport->fcs, rport->pid);
1070         bfa_trc(rport->fcs, event);
1071
1072         switch (event) {
1073         case RPSM_EVENT_TIMEOUT:
1074                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1075                 bfa_fcs_rport_free(rport);
1076                 break;
1077
1078         case RPSM_EVENT_SCN:
1079         case RPSM_EVENT_ADDRESS_CHANGE:
1080                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
1081                 bfa_timer_stop(&rport->timer);
1082                 rport->ns_retries = 0;
1083                 bfa_fcs_rport_send_nsdisc(rport, NULL);
1084                 break;
1085
1086         case RPSM_EVENT_DELETE:
1087                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1088                 bfa_timer_stop(&rport->timer);
1089                 bfa_fcs_rport_free(rport);
1090                 break;
1091
1092         case RPSM_EVENT_PLOGI_RCVD:
1093                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
1094                 bfa_timer_stop(&rport->timer);
1095                 bfa_fcs_rport_send_plogiacc(rport, NULL);
1096                 break;
1097
1098         case RPSM_EVENT_LOGO_RCVD:
1099         case RPSM_EVENT_PRLO_RCVD:
1100         case RPSM_EVENT_LOGO_IMP:
1101                 break;
1102
1103         case RPSM_EVENT_PLOGI_COMP:
1104                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
1105                 bfa_timer_stop(&rport->timer);
1106                 bfa_fcs_rport_hal_online(rport);
1107                 break;
1108
1109         case RPSM_EVENT_PLOGI_SEND:
1110                 bfa_timer_stop(&rport->timer);
1111                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogi_sending);
1112                 rport->plogi_retries = 0;
1113                 bfa_fcs_rport_send_plogi(rport, NULL);
1114                 break;
1115
1116         default:
1117                 bfa_sm_fault(rport->fcs, event);
1118         }
1119 }
1120
1121 /*
1122  *      Rport address has changed. Nameserver discovery request is being sent.
1123  */
1124 static void
1125 bfa_fcs_rport_sm_nsdisc_sending(struct bfa_fcs_rport_s *rport,
1126          enum rport_event event)
1127 {
1128         bfa_trc(rport->fcs, rport->pwwn);
1129         bfa_trc(rport->fcs, rport->pid);
1130         bfa_trc(rport->fcs, event);
1131
1132         switch (event) {
1133         case RPSM_EVENT_FCXP_SENT:
1134                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sent);
1135                 break;
1136
1137         case RPSM_EVENT_DELETE:
1138                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1139                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
1140                 bfa_fcs_rport_free(rport);
1141                 break;
1142
1143         case RPSM_EVENT_PLOGI_RCVD:
1144                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
1145                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
1146                 bfa_fcs_rport_send_plogiacc(rport, NULL);
1147                 break;
1148
1149         case RPSM_EVENT_SCN:
1150         case RPSM_EVENT_LOGO_RCVD:
1151         case RPSM_EVENT_PRLO_RCVD:
1152         case RPSM_EVENT_PLOGI_SEND:
1153                 break;
1154
1155         case RPSM_EVENT_ADDRESS_CHANGE:
1156                 rport->ns_retries = 0; /* reset the retry count */
1157                 break;
1158
1159         case RPSM_EVENT_LOGO_IMP:
1160                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
1161                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
1162                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
1163                                 bfa_fcs_rport_timeout, rport,
1164                                 bfa_fcs_rport_del_timeout);
1165                 break;
1166
1167         case RPSM_EVENT_PLOGI_COMP:
1168                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
1169                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rport->fcxp_wqe);
1170                 bfa_fcs_rport_hal_online(rport);
1171                 break;
1172
1173         default:
1174                 bfa_sm_fault(rport->fcs, event);
1175         }
1176 }
1177
1178 /*
1179  *              Nameserver discovery failed. Waiting for timeout to retry.
1180  */
1181 static void
1182 bfa_fcs_rport_sm_nsdisc_retry(struct bfa_fcs_rport_s *rport,
1183          enum rport_event event)
1184 {
1185         bfa_trc(rport->fcs, rport->pwwn);
1186         bfa_trc(rport->fcs, rport->pid);
1187         bfa_trc(rport->fcs, event);
1188
1189         switch (event) {
1190         case RPSM_EVENT_TIMEOUT:
1191                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
1192                 bfa_fcs_rport_send_nsdisc(rport, NULL);
1193                 break;
1194
1195         case RPSM_EVENT_SCN:
1196         case RPSM_EVENT_ADDRESS_CHANGE:
1197                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_nsdisc_sending);
1198                 bfa_timer_stop(&rport->timer);
1199                 rport->ns_retries = 0;
1200                 bfa_fcs_rport_send_nsdisc(rport, NULL);
1201                 break;
1202
1203         case RPSM_EVENT_DELETE:
1204                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1205                 bfa_timer_stop(&rport->timer);
1206                 bfa_fcs_rport_free(rport);
1207                 break;
1208
1209         case RPSM_EVENT_PLOGI_RCVD:
1210                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
1211                 bfa_timer_stop(&rport->timer);
1212                 bfa_fcs_rport_send_plogiacc(rport, NULL);
1213                 break;
1214
1215         case RPSM_EVENT_LOGO_IMP:
1216                 rport->pid = 0;
1217                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
1218                 bfa_timer_stop(&rport->timer);
1219                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
1220                                 bfa_fcs_rport_timeout, rport,
1221                                 bfa_fcs_rport_del_timeout);
1222                 break;
1223
1224         case RPSM_EVENT_LOGO_RCVD:
1225                 bfa_fcs_rport_send_logo_acc(rport);
1226                 break;
1227         case RPSM_EVENT_PRLO_RCVD:
1228                 bfa_fcs_rport_send_prlo_acc(rport);
1229                 break;
1230
1231         case RPSM_EVENT_PLOGI_COMP:
1232                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
1233                 bfa_timer_stop(&rport->timer);
1234                 bfa_fcs_rport_hal_online(rport);
1235                 break;
1236
1237         default:
1238                 bfa_sm_fault(rport->fcs, event);
1239         }
1240 }
1241
1242 /*
1243  *              Rport address has changed. Nameserver discovery request is sent.
1244  */
1245 static void
1246 bfa_fcs_rport_sm_nsdisc_sent(struct bfa_fcs_rport_s *rport,
1247                         enum rport_event event)
1248 {
1249         bfa_trc(rport->fcs, rport->pwwn);
1250         bfa_trc(rport->fcs, rport->pid);
1251         bfa_trc(rport->fcs, event);
1252
1253         switch (event) {
1254         case RPSM_EVENT_ACCEPTED:
1255         case RPSM_EVENT_ADDRESS_CHANGE:
1256                 if (rport->pid) {
1257                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogi_sending);
1258                         bfa_fcs_rport_send_plogi(rport, NULL);
1259                 } else {
1260                         bfa_sm_set_state(rport,
1261                                  bfa_fcs_rport_sm_nsdisc_sending);
1262                         rport->ns_retries = 0;
1263                         bfa_fcs_rport_send_nsdisc(rport, NULL);
1264                 }
1265                 break;
1266
1267         case RPSM_EVENT_FAILED:
1268                 rport->ns_retries++;
1269                 if (rport->ns_retries < BFA_FCS_RPORT_MAX_RETRIES) {
1270                         bfa_sm_set_state(rport,
1271                                  bfa_fcs_rport_sm_nsdisc_sending);
1272                         bfa_fcs_rport_send_nsdisc(rport, NULL);
1273                 } else {
1274                         rport->pid = 0;
1275                         bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
1276                         bfa_timer_start(rport->fcs->bfa, &rport->timer,
1277                                         bfa_fcs_rport_timeout, rport,
1278                                         bfa_fcs_rport_del_timeout);
1279                 };
1280                 break;
1281
1282         case RPSM_EVENT_DELETE:
1283                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1284                 bfa_fcxp_discard(rport->fcxp);
1285                 bfa_fcs_rport_free(rport);
1286                 break;
1287
1288         case RPSM_EVENT_PLOGI_RCVD:
1289                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_plogiacc_sending);
1290                 bfa_fcxp_discard(rport->fcxp);
1291                 bfa_fcs_rport_send_plogiacc(rport, NULL);
1292                 break;
1293
1294         case RPSM_EVENT_LOGO_IMP:
1295                 rport->pid = 0;
1296                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_offline);
1297                 bfa_fcxp_discard(rport->fcxp);
1298                 bfa_timer_start(rport->fcs->bfa, &rport->timer,
1299                                 bfa_fcs_rport_timeout, rport,
1300                                 bfa_fcs_rport_del_timeout);
1301                 break;
1302
1303
1304         case RPSM_EVENT_PRLO_RCVD:
1305                 bfa_fcs_rport_send_prlo_acc(rport);
1306                 break;
1307         case RPSM_EVENT_SCN:
1308                 /*
1309                  * ignore, wait for NS query response
1310                  */
1311                 break;
1312
1313         case RPSM_EVENT_LOGO_RCVD:
1314                 /*
1315                  * Not logged-in yet. Accept LOGO.
1316                  */
1317                 bfa_fcs_rport_send_logo_acc(rport);
1318                 break;
1319
1320         case RPSM_EVENT_PLOGI_COMP:
1321                 bfa_sm_set_state(rport, bfa_fcs_rport_sm_hal_online);
1322                 bfa_fcxp_discard(rport->fcxp);
1323                 bfa_fcs_rport_hal_online(rport);
1324                 break;
1325
1326         default:
1327                 bfa_sm_fault(rport->fcs, event);
1328         }
1329 }
1330
1331
1332
1333 /*
1334  *  fcs_rport_private FCS RPORT provate functions
1335  */
1336
1337 static void
1338 bfa_fcs_rport_send_plogi(void *rport_cbarg, struct bfa_fcxp_s *fcxp_alloced)
1339 {
1340         struct bfa_fcs_rport_s *rport = rport_cbarg;
1341         struct bfa_fcs_lport_s *port = rport->port;
1342         struct fchs_s   fchs;
1343         int             len;
1344         struct bfa_fcxp_s *fcxp;
1345
1346         bfa_trc(rport->fcs, rport->pwwn);
1347
1348         fcxp = fcxp_alloced ? fcxp_alloced : bfa_fcs_fcxp_alloc(port->fcs);
1349         if (!fcxp) {
1350                 bfa_fcs_fcxp_alloc_wait(port->fcs->bfa, &rport->fcxp_wqe,
1351                                         bfa_fcs_rport_send_plogi, rport);
1352                 return;
1353         }
1354         rport->fcxp = fcxp;
1355
1356         len = fc_plogi_build(&fchs, bfa_fcxp_get_reqbuf(fcxp), rport->pid,
1357                                 bfa_fcs_lport_get_fcid(port), 0,
1358                                 port->port_cfg.pwwn, port->port_cfg.nwwn,
1359                                 bfa_fcport_get_maxfrsize(port->fcs->bfa));
1360
1361         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1362                         FC_CLASS_3, len, &fchs, bfa_fcs_rport_plogi_response,
1363                         (void *)rport, FC_MAX_PDUSZ, FC_ELS_TOV);
1364
1365         rport->stats.plogis++;
1366         bfa_sm_send_event(rport, RPSM_EVENT_FCXP_SENT);
1367 }
1368
1369 static void
1370 bfa_fcs_rport_plogi_response(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
1371                                 bfa_status_t req_status, u32 rsp_len,
1372                                 u32 resid_len, struct fchs_s *rsp_fchs)
1373 {
1374         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
1375         struct fc_logi_s        *plogi_rsp;
1376         struct fc_ls_rjt_s      *ls_rjt;
1377         struct bfa_fcs_rport_s *twin;
1378         struct list_head        *qe;
1379
1380         bfa_trc(rport->fcs, rport->pwwn);
1381
1382         /*
1383          * Sanity Checks
1384          */
1385         if (req_status != BFA_STATUS_OK) {
1386                 bfa_trc(rport->fcs, req_status);
1387                 rport->stats.plogi_failed++;
1388                 bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1389                 return;
1390         }
1391
1392         plogi_rsp = (struct fc_logi_s *) BFA_FCXP_RSP_PLD(fcxp);
1393
1394         /*
1395          * Check for failure first.
1396          */
1397         if (plogi_rsp->els_cmd.els_code != FC_ELS_ACC) {
1398                 ls_rjt = (struct fc_ls_rjt_s *) BFA_FCXP_RSP_PLD(fcxp);
1399
1400                 bfa_trc(rport->fcs, ls_rjt->reason_code);
1401                 bfa_trc(rport->fcs, ls_rjt->reason_code_expl);
1402
1403                 if ((ls_rjt->reason_code == FC_LS_RJT_RSN_UNABLE_TO_PERF_CMD) &&
1404                  (ls_rjt->reason_code_expl == FC_LS_RJT_EXP_INSUFF_RES)) {
1405                         rport->stats.rjt_insuff_res++;
1406                         bfa_sm_send_event(rport, RPSM_EVENT_PLOGI_RETRY);
1407                         return;
1408                 }
1409
1410                 rport->stats.plogi_rejects++;
1411                 bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1412                 return;
1413         }
1414
1415         /*
1416          * PLOGI is complete. Make sure this device is not one of the known
1417          * device with a new FC port address.
1418          */
1419         list_for_each(qe, &rport->port->rport_q) {
1420                 twin = (struct bfa_fcs_rport_s *) qe;
1421                 if (twin == rport)
1422                         continue;
1423                 if (!rport->pwwn && (plogi_rsp->port_name == twin->pwwn)) {
1424                         bfa_trc(rport->fcs, twin->pid);
1425                         bfa_trc(rport->fcs, rport->pid);
1426
1427                         /* Update plogi stats in twin */
1428                         twin->stats.plogis  += rport->stats.plogis;
1429                         twin->stats.plogi_rejects  +=
1430                                  rport->stats.plogi_rejects;
1431                         twin->stats.plogi_timeouts  +=
1432                                  rport->stats.plogi_timeouts;
1433                         twin->stats.plogi_failed +=
1434                                  rport->stats.plogi_failed;
1435                         twin->stats.plogi_rcvd    += rport->stats.plogi_rcvd;
1436                         twin->stats.plogi_accs++;
1437
1438                         bfa_sm_send_event(rport, RPSM_EVENT_DELETE);
1439
1440                         bfa_fcs_rport_update(twin, plogi_rsp);
1441                         twin->pid = rsp_fchs->s_id;
1442                         bfa_sm_send_event(twin, RPSM_EVENT_PLOGI_COMP);
1443                         return;
1444                 }
1445         }
1446
1447         /*
1448          * Normal login path -- no evil twins.
1449          */
1450         rport->stats.plogi_accs++;
1451         bfa_fcs_rport_update(rport, plogi_rsp);
1452         bfa_sm_send_event(rport, RPSM_EVENT_ACCEPTED);
1453 }
1454
1455 static void
1456 bfa_fcs_rport_send_plogiacc(void *rport_cbarg, struct bfa_fcxp_s *fcxp_alloced)
1457 {
1458         struct bfa_fcs_rport_s *rport = rport_cbarg;
1459         struct bfa_fcs_lport_s *port = rport->port;
1460         struct fchs_s           fchs;
1461         int             len;
1462         struct bfa_fcxp_s *fcxp;
1463
1464         bfa_trc(rport->fcs, rport->pwwn);
1465         bfa_trc(rport->fcs, rport->reply_oxid);
1466
1467         fcxp = fcxp_alloced ? fcxp_alloced : bfa_fcs_fcxp_alloc(port->fcs);
1468         if (!fcxp) {
1469                 bfa_fcs_fcxp_alloc_wait(port->fcs->bfa, &rport->fcxp_wqe,
1470                                         bfa_fcs_rport_send_plogiacc, rport);
1471                 return;
1472         }
1473         rport->fcxp = fcxp;
1474
1475         len = fc_plogi_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1476                                  rport->pid, bfa_fcs_lport_get_fcid(port),
1477                                  rport->reply_oxid, port->port_cfg.pwwn,
1478                                  port->port_cfg.nwwn,
1479                                  bfa_fcport_get_maxfrsize(port->fcs->bfa));
1480
1481         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1482                         FC_CLASS_3, len, &fchs, NULL, NULL, FC_MAX_PDUSZ, 0);
1483
1484         bfa_sm_send_event(rport, RPSM_EVENT_FCXP_SENT);
1485 }
1486
1487 static void
1488 bfa_fcs_rport_send_adisc(void *rport_cbarg, struct bfa_fcxp_s *fcxp_alloced)
1489 {
1490         struct bfa_fcs_rport_s *rport = rport_cbarg;
1491         struct bfa_fcs_lport_s *port = rport->port;
1492         struct fchs_s           fchs;
1493         int             len;
1494         struct bfa_fcxp_s *fcxp;
1495
1496         bfa_trc(rport->fcs, rport->pwwn);
1497
1498         fcxp = fcxp_alloced ? fcxp_alloced : bfa_fcs_fcxp_alloc(port->fcs);
1499         if (!fcxp) {
1500                 bfa_fcs_fcxp_alloc_wait(port->fcs->bfa, &rport->fcxp_wqe,
1501                                         bfa_fcs_rport_send_adisc, rport);
1502                 return;
1503         }
1504         rport->fcxp = fcxp;
1505
1506         len = fc_adisc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp), rport->pid,
1507                                 bfa_fcs_lport_get_fcid(port), 0,
1508                                 port->port_cfg.pwwn, port->port_cfg.nwwn);
1509
1510         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1511                         FC_CLASS_3, len, &fchs, bfa_fcs_rport_adisc_response,
1512                         rport, FC_MAX_PDUSZ, FC_ELS_TOV);
1513
1514         rport->stats.adisc_sent++;
1515         bfa_sm_send_event(rport, RPSM_EVENT_FCXP_SENT);
1516 }
1517
1518 static void
1519 bfa_fcs_rport_adisc_response(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
1520                                 bfa_status_t req_status, u32 rsp_len,
1521                                 u32 resid_len, struct fchs_s *rsp_fchs)
1522 {
1523         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
1524         void            *pld = bfa_fcxp_get_rspbuf(fcxp);
1525         struct fc_ls_rjt_s      *ls_rjt;
1526
1527         if (req_status != BFA_STATUS_OK) {
1528                 bfa_trc(rport->fcs, req_status);
1529                 rport->stats.adisc_failed++;
1530                 bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1531                 return;
1532         }
1533
1534         if (fc_adisc_rsp_parse((struct fc_adisc_s *)pld, rsp_len, rport->pwwn,
1535                                 rport->nwwn)  == FC_PARSE_OK) {
1536                 rport->stats.adisc_accs++;
1537                 bfa_sm_send_event(rport, RPSM_EVENT_ACCEPTED);
1538                 return;
1539         }
1540
1541         rport->stats.adisc_rejects++;
1542         ls_rjt = pld;
1543         bfa_trc(rport->fcs, ls_rjt->els_cmd.els_code);
1544         bfa_trc(rport->fcs, ls_rjt->reason_code);
1545         bfa_trc(rport->fcs, ls_rjt->reason_code_expl);
1546         bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1547 }
1548
1549 static void
1550 bfa_fcs_rport_send_nsdisc(void *rport_cbarg, struct bfa_fcxp_s *fcxp_alloced)
1551 {
1552         struct bfa_fcs_rport_s *rport = rport_cbarg;
1553         struct bfa_fcs_lport_s *port = rport->port;
1554         struct fchs_s   fchs;
1555         struct bfa_fcxp_s *fcxp;
1556         int             len;
1557         bfa_cb_fcxp_send_t cbfn;
1558
1559         bfa_trc(rport->fcs, rport->pid);
1560
1561         fcxp = fcxp_alloced ? fcxp_alloced : bfa_fcs_fcxp_alloc(port->fcs);
1562         if (!fcxp) {
1563                 bfa_fcs_fcxp_alloc_wait(port->fcs->bfa, &rport->fcxp_wqe,
1564                                         bfa_fcs_rport_send_nsdisc, rport);
1565                 return;
1566         }
1567         rport->fcxp = fcxp;
1568
1569         if (rport->pwwn) {
1570                 len = fc_gidpn_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1571                                 bfa_fcs_lport_get_fcid(port), 0, rport->pwwn);
1572                 cbfn = bfa_fcs_rport_gidpn_response;
1573         } else {
1574                 len = fc_gpnid_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1575                                 bfa_fcs_lport_get_fcid(port), 0, rport->pid);
1576                 cbfn = bfa_fcs_rport_gpnid_response;
1577         }
1578
1579         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1580                         FC_CLASS_3, len, &fchs, cbfn,
1581                         (void *)rport, FC_MAX_PDUSZ, FC_FCCT_TOV);
1582
1583         bfa_sm_send_event(rport, RPSM_EVENT_FCXP_SENT);
1584 }
1585
1586 static void
1587 bfa_fcs_rport_gidpn_response(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
1588                                 bfa_status_t req_status, u32 rsp_len,
1589                                 u32 resid_len, struct fchs_s *rsp_fchs)
1590 {
1591         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
1592         struct ct_hdr_s *cthdr;
1593         struct fcgs_gidpn_resp_s        *gidpn_rsp;
1594         struct bfa_fcs_rport_s  *twin;
1595         struct list_head        *qe;
1596
1597         bfa_trc(rport->fcs, rport->pwwn);
1598
1599         cthdr = (struct ct_hdr_s *) BFA_FCXP_RSP_PLD(fcxp);
1600         cthdr->cmd_rsp_code = be16_to_cpu(cthdr->cmd_rsp_code);
1601
1602         if (cthdr->cmd_rsp_code == CT_RSP_ACCEPT) {
1603                 /* Check if the pid is the same as before. */
1604                 gidpn_rsp = (struct fcgs_gidpn_resp_s *) (cthdr + 1);
1605
1606                 if (gidpn_rsp->dap == rport->pid) {
1607                         /* Device is online  */
1608                         bfa_sm_send_event(rport, RPSM_EVENT_ACCEPTED);
1609                 } else {
1610                         /*
1611                          * Device's PID has changed. We need to cleanup
1612                          * and re-login. If there is another device with
1613                          * the the newly discovered pid, send an scn notice
1614                          * so that its new pid can be discovered.
1615                          */
1616                         list_for_each(qe, &rport->port->rport_q) {
1617                                 twin = (struct bfa_fcs_rport_s *) qe;
1618                                 if (twin == rport)
1619                                         continue;
1620                                 if (gidpn_rsp->dap == twin->pid) {
1621                                         bfa_trc(rport->fcs, twin->pid);
1622                                         bfa_trc(rport->fcs, rport->pid);
1623
1624                                         twin->pid = 0;
1625                                         bfa_sm_send_event(twin,
1626                                          RPSM_EVENT_ADDRESS_CHANGE);
1627                                 }
1628                         }
1629                         rport->pid = gidpn_rsp->dap;
1630                         bfa_sm_send_event(rport, RPSM_EVENT_ADDRESS_CHANGE);
1631                 }
1632                 return;
1633         }
1634
1635         /*
1636          * Reject Response
1637          */
1638         switch (cthdr->reason_code) {
1639         case CT_RSN_LOGICAL_BUSY:
1640                 /*
1641                  * Need to retry
1642                  */
1643                 bfa_sm_send_event(rport, RPSM_EVENT_TIMEOUT);
1644                 break;
1645
1646         case CT_RSN_UNABLE_TO_PERF:
1647                 /*
1648                  * device doesn't exist : Start timer to cleanup this later.
1649                  */
1650                 bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1651                 break;
1652
1653         default:
1654                 bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1655                 break;
1656         }
1657 }
1658
1659 static void
1660 bfa_fcs_rport_gpnid_response(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
1661                                 bfa_status_t req_status, u32 rsp_len,
1662                                 u32 resid_len, struct fchs_s *rsp_fchs)
1663 {
1664         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
1665         struct ct_hdr_s *cthdr;
1666
1667         bfa_trc(rport->fcs, rport->pwwn);
1668
1669         cthdr = (struct ct_hdr_s *) BFA_FCXP_RSP_PLD(fcxp);
1670         cthdr->cmd_rsp_code = be16_to_cpu(cthdr->cmd_rsp_code);
1671
1672         if (cthdr->cmd_rsp_code == CT_RSP_ACCEPT) {
1673                 bfa_sm_send_event(rport, RPSM_EVENT_ACCEPTED);
1674                 return;
1675         }
1676
1677         /*
1678          * Reject Response
1679          */
1680         switch (cthdr->reason_code) {
1681         case CT_RSN_LOGICAL_BUSY:
1682                 /*
1683                  * Need to retry
1684                  */
1685                 bfa_sm_send_event(rport, RPSM_EVENT_TIMEOUT);
1686                 break;
1687
1688         case CT_RSN_UNABLE_TO_PERF:
1689                 /*
1690                  * device doesn't exist : Start timer to cleanup this later.
1691                  */
1692                 bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1693                 break;
1694
1695         default:
1696                 bfa_sm_send_event(rport, RPSM_EVENT_FAILED);
1697                 break;
1698         }
1699 }
1700
1701 /*
1702  *      Called to send a logout to the rport.
1703  */
1704 static void
1705 bfa_fcs_rport_send_logo(void *rport_cbarg, struct bfa_fcxp_s *fcxp_alloced)
1706 {
1707         struct bfa_fcs_rport_s *rport = rport_cbarg;
1708         struct bfa_fcs_lport_s *port;
1709         struct fchs_s   fchs;
1710         struct bfa_fcxp_s *fcxp;
1711         u16     len;
1712
1713         bfa_trc(rport->fcs, rport->pid);
1714
1715         port = rport->port;
1716
1717         fcxp = fcxp_alloced ? fcxp_alloced : bfa_fcs_fcxp_alloc(port->fcs);
1718         if (!fcxp) {
1719                 bfa_fcs_fcxp_alloc_wait(port->fcs->bfa, &rport->fcxp_wqe,
1720                                         bfa_fcs_rport_send_logo, rport);
1721                 return;
1722         }
1723         rport->fcxp = fcxp;
1724
1725         len = fc_logo_build(&fchs, bfa_fcxp_get_reqbuf(fcxp), rport->pid,
1726                                 bfa_fcs_lport_get_fcid(port), 0,
1727                                 bfa_fcs_lport_get_pwwn(port));
1728
1729         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1730                         FC_CLASS_3, len, &fchs, NULL,
1731                         rport, FC_MAX_PDUSZ, FC_ELS_TOV);
1732
1733         rport->stats.logos++;
1734         bfa_fcxp_discard(rport->fcxp);
1735         bfa_sm_send_event(rport, RPSM_EVENT_FCXP_SENT);
1736 }
1737
1738 /*
1739  *      Send ACC for a LOGO received.
1740  */
1741 static void
1742 bfa_fcs_rport_send_logo_acc(void *rport_cbarg)
1743 {
1744         struct bfa_fcs_rport_s *rport = rport_cbarg;
1745         struct bfa_fcs_lport_s *port;
1746         struct fchs_s   fchs;
1747         struct bfa_fcxp_s *fcxp;
1748         u16     len;
1749
1750         bfa_trc(rport->fcs, rport->pid);
1751
1752         port = rport->port;
1753
1754         fcxp = bfa_fcs_fcxp_alloc(port->fcs);
1755         if (!fcxp)
1756                 return;
1757
1758         rport->stats.logo_rcvd++;
1759         len = fc_logo_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1760                                 rport->pid, bfa_fcs_lport_get_fcid(port),
1761                                 rport->reply_oxid);
1762
1763         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1764                         FC_CLASS_3, len, &fchs, NULL, NULL, FC_MAX_PDUSZ, 0);
1765 }
1766
1767 /*
1768  *      brief
1769  *      This routine will be called by bfa_timer on timer timeouts.
1770  *
1771  *      param[in]       rport                   - pointer to bfa_fcs_lport_ns_t.
1772  *      param[out]      rport_status    - pointer to return vport status in
1773  *
1774  *      return
1775  *              void
1776  *
1777  *      Special Considerations:
1778  *
1779  *      note
1780  */
1781 static void
1782 bfa_fcs_rport_timeout(void *arg)
1783 {
1784         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) arg;
1785
1786         rport->stats.plogi_timeouts++;
1787         bfa_stats(rport->port, rport_plogi_timeouts);
1788         bfa_sm_send_event(rport, RPSM_EVENT_TIMEOUT);
1789 }
1790
1791 static void
1792 bfa_fcs_rport_process_prli(struct bfa_fcs_rport_s *rport,
1793                         struct fchs_s *rx_fchs, u16 len)
1794 {
1795         struct bfa_fcxp_s *fcxp;
1796         struct fchs_s   fchs;
1797         struct bfa_fcs_lport_s *port = rport->port;
1798         struct fc_prli_s        *prli;
1799
1800         bfa_trc(port->fcs, rx_fchs->s_id);
1801         bfa_trc(port->fcs, rx_fchs->d_id);
1802
1803         rport->stats.prli_rcvd++;
1804
1805         /*
1806          * We are in Initiator Mode
1807          */
1808         prli = (struct fc_prli_s *) (rx_fchs + 1);
1809
1810         if (prli->parampage.servparams.target) {
1811                 /*
1812                  * PRLI from a target ?
1813                  * Send the Acc.
1814                  * PRLI sent by us will be used to transition the IT nexus,
1815                  * once the response is received from the target.
1816                  */
1817                 bfa_trc(port->fcs, rx_fchs->s_id);
1818                 rport->scsi_function = BFA_RPORT_TARGET;
1819         } else {
1820                 bfa_trc(rport->fcs, prli->parampage.type);
1821                 rport->scsi_function = BFA_RPORT_INITIATOR;
1822                 bfa_fcs_itnim_is_initiator(rport->itnim);
1823         }
1824
1825         fcxp = bfa_fcs_fcxp_alloc(port->fcs);
1826         if (!fcxp)
1827                 return;
1828
1829         len = fc_prli_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1830                                 rx_fchs->s_id, bfa_fcs_lport_get_fcid(port),
1831                                 rx_fchs->ox_id, port->port_cfg.roles);
1832
1833         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1834                         FC_CLASS_3, len, &fchs, NULL, NULL, FC_MAX_PDUSZ, 0);
1835 }
1836
1837 static void
1838 bfa_fcs_rport_process_rpsc(struct bfa_fcs_rport_s *rport,
1839                         struct fchs_s *rx_fchs, u16 len)
1840 {
1841         struct bfa_fcxp_s *fcxp;
1842         struct fchs_s   fchs;
1843         struct bfa_fcs_lport_s *port = rport->port;
1844         struct fc_rpsc_speed_info_s speeds;
1845         struct bfa_port_attr_s pport_attr;
1846
1847         bfa_trc(port->fcs, rx_fchs->s_id);
1848         bfa_trc(port->fcs, rx_fchs->d_id);
1849
1850         rport->stats.rpsc_rcvd++;
1851         speeds.port_speed_cap =
1852                 RPSC_SPEED_CAP_1G | RPSC_SPEED_CAP_2G | RPSC_SPEED_CAP_4G |
1853                 RPSC_SPEED_CAP_8G;
1854
1855         /*
1856          * get curent speed from pport attributes from BFA
1857          */
1858         bfa_fcport_get_attr(port->fcs->bfa, &pport_attr);
1859
1860         speeds.port_op_speed = fc_bfa_speed_to_rpsc_operspeed(pport_attr.speed);
1861
1862         fcxp = bfa_fcs_fcxp_alloc(port->fcs);
1863         if (!fcxp)
1864                 return;
1865
1866         len = fc_rpsc_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1867                                 rx_fchs->s_id, bfa_fcs_lport_get_fcid(port),
1868                                 rx_fchs->ox_id, &speeds);
1869
1870         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
1871                         FC_CLASS_3, len, &fchs, NULL, NULL, FC_MAX_PDUSZ, 0);
1872 }
1873
1874 static void
1875 bfa_fcs_rport_process_adisc(struct bfa_fcs_rport_s *rport,
1876                         struct fchs_s *rx_fchs, u16 len)
1877 {
1878         struct bfa_fcxp_s *fcxp;
1879         struct fchs_s   fchs;
1880         struct bfa_fcs_lport_s *port = rport->port;
1881         struct fc_adisc_s       *adisc;
1882
1883         bfa_trc(port->fcs, rx_fchs->s_id);
1884         bfa_trc(port->fcs, rx_fchs->d_id);
1885
1886         rport->stats.adisc_rcvd++;
1887
1888         adisc = (struct fc_adisc_s *) (rx_fchs + 1);
1889
1890         /*
1891          * Accept if the itnim for this rport is online.
1892          * Else reject the ADISC.
1893          */
1894         if (bfa_fcs_itnim_get_online_state(rport->itnim) == BFA_STATUS_OK) {
1895
1896                 fcxp = bfa_fcs_fcxp_alloc(port->fcs);
1897                 if (!fcxp)
1898                         return;
1899
1900                 len = fc_adisc_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1901                          rx_fchs->s_id, bfa_fcs_lport_get_fcid(port),
1902                          rx_fchs->ox_id, port->port_cfg.pwwn,
1903                          port->port_cfg.nwwn);
1904
1905                 bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag,
1906                                 BFA_FALSE, FC_CLASS_3, len, &fchs, NULL, NULL,
1907                                 FC_MAX_PDUSZ, 0);
1908         } else {
1909                 rport->stats.adisc_rejected++;
1910                 bfa_fcs_rport_send_ls_rjt(rport, rx_fchs,
1911                                           FC_LS_RJT_RSN_UNABLE_TO_PERF_CMD,
1912                                           FC_LS_RJT_EXP_LOGIN_REQUIRED);
1913         }
1914 }
1915
1916 static void
1917 bfa_fcs_rport_hal_online(struct bfa_fcs_rport_s *rport)
1918 {
1919         struct bfa_fcs_lport_s *port = rport->port;
1920         struct bfa_rport_info_s rport_info;
1921
1922         rport_info.pid = rport->pid;
1923         rport_info.local_pid = port->pid;
1924         rport_info.lp_tag = port->lp_tag;
1925         rport_info.vf_id = port->fabric->vf_id;
1926         rport_info.vf_en = port->fabric->is_vf;
1927         rport_info.fc_class = rport->fc_cos;
1928         rport_info.cisc = rport->cisc;
1929         rport_info.max_frmsz = rport->maxfrsize;
1930         bfa_rport_online(rport->bfa_rport, &rport_info);
1931 }
1932
1933 static struct bfa_fcs_rport_s *
1934 bfa_fcs_rport_alloc(struct bfa_fcs_lport_s *port, wwn_t pwwn, u32 rpid)
1935 {
1936         struct bfa_fcs_s        *fcs = port->fcs;
1937         struct bfa_fcs_rport_s *rport;
1938         struct bfad_rport_s     *rport_drv;
1939
1940         /*
1941          * allocate rport
1942          */
1943         if (bfa_fcb_rport_alloc(fcs->bfad, &rport, &rport_drv)
1944                 != BFA_STATUS_OK) {
1945                 bfa_trc(fcs, rpid);
1946                 return NULL;
1947         }
1948
1949         /*
1950          * Initialize r-port
1951          */
1952         rport->port = port;
1953         rport->fcs = fcs;
1954         rport->rp_drv = rport_drv;
1955         rport->pid = rpid;
1956         rport->pwwn = pwwn;
1957
1958         /*
1959          * allocate BFA rport
1960          */
1961         rport->bfa_rport = bfa_rport_create(port->fcs->bfa, rport);
1962         if (!rport->bfa_rport) {
1963                 bfa_trc(fcs, rpid);
1964                 kfree(rport_drv);
1965                 return NULL;
1966         }
1967
1968         /*
1969          * allocate FC-4s
1970          */
1971         bfa_assert(bfa_fcs_lport_is_initiator(port));
1972
1973         if (bfa_fcs_lport_is_initiator(port)) {
1974                 rport->itnim = bfa_fcs_itnim_create(rport);
1975                 if (!rport->itnim) {
1976                         bfa_trc(fcs, rpid);
1977                         bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_DELETE);
1978                         kfree(rport_drv);
1979                         return NULL;
1980                 }
1981         }
1982
1983         bfa_fcs_lport_add_rport(port, rport);
1984
1985         bfa_sm_set_state(rport, bfa_fcs_rport_sm_uninit);
1986
1987         /* Initialize the Rport Features(RPF) Sub Module  */
1988         if (!BFA_FCS_PID_IS_WKA(rport->pid))
1989                 bfa_fcs_rpf_init(rport);
1990
1991         return rport;
1992 }
1993
1994
1995 static void
1996 bfa_fcs_rport_free(struct bfa_fcs_rport_s *rport)
1997 {
1998         struct bfa_fcs_lport_s *port = rport->port;
1999
2000         /*
2001          * - delete FC-4s
2002          * - delete BFA rport
2003          * - remove from queue of rports
2004          */
2005         if (bfa_fcs_lport_is_initiator(port)) {
2006                 bfa_fcs_itnim_delete(rport->itnim);
2007                 if (rport->pid != 0 && !BFA_FCS_PID_IS_WKA(rport->pid))
2008                         bfa_fcs_rpf_rport_offline(rport);
2009         }
2010
2011         bfa_sm_send_event(rport->bfa_rport, BFA_RPORT_SM_DELETE);
2012         bfa_fcs_lport_del_rport(port, rport);
2013         kfree(rport->rp_drv);
2014 }
2015
2016 static void
2017 bfa_fcs_rport_online_action(struct bfa_fcs_rport_s *rport)
2018 {
2019         struct bfa_fcs_lport_s *port = rport->port;
2020         struct bfad_s *bfad = (struct bfad_s *)port->fcs->bfad;
2021         char    lpwwn_buf[BFA_STRING_32];
2022         char    rpwwn_buf[BFA_STRING_32];
2023
2024         rport->stats.onlines++;
2025
2026         if (bfa_fcs_lport_is_initiator(port)) {
2027                 bfa_fcs_itnim_rport_online(rport->itnim);
2028                 if (!BFA_FCS_PID_IS_WKA(rport->pid))
2029                         bfa_fcs_rpf_rport_online(rport);
2030         };
2031
2032         wwn2str(lpwwn_buf, bfa_fcs_lport_get_pwwn(port));
2033         wwn2str(rpwwn_buf, rport->pwwn);
2034         if (!BFA_FCS_PID_IS_WKA(rport->pid))
2035                 BFA_LOG(KERN_INFO, bfad, bfa_log_level,
2036                 "Remote port (WWN = %s) online for logical port (WWN = %s)\n",
2037                 rpwwn_buf, lpwwn_buf);
2038 }
2039
2040 static void
2041 bfa_fcs_rport_offline_action(struct bfa_fcs_rport_s *rport)
2042 {
2043         struct bfa_fcs_lport_s *port = rport->port;
2044         struct bfad_s *bfad = (struct bfad_s *)port->fcs->bfad;
2045         char    lpwwn_buf[BFA_STRING_32];
2046         char    rpwwn_buf[BFA_STRING_32];
2047
2048         rport->stats.offlines++;
2049
2050         wwn2str(lpwwn_buf, bfa_fcs_lport_get_pwwn(port));
2051         wwn2str(rpwwn_buf, rport->pwwn);
2052         if (!BFA_FCS_PID_IS_WKA(rport->pid)) {
2053                 if (bfa_fcs_lport_is_online(rport->port) == BFA_TRUE)
2054                         BFA_LOG(KERN_ERR, bfad, bfa_log_level,
2055                                 "Remote port (WWN = %s) connectivity lost for "
2056                                 "logical port (WWN = %s)\n",
2057                                 rpwwn_buf, lpwwn_buf);
2058                 else
2059                         BFA_LOG(KERN_INFO, bfad, bfa_log_level,
2060                                 "Remote port (WWN = %s) offlined by "
2061                                 "logical port (WWN = %s)\n",
2062                                 rpwwn_buf, lpwwn_buf);
2063         }
2064
2065         if (bfa_fcs_lport_is_initiator(port)) {
2066                 bfa_fcs_itnim_rport_offline(rport->itnim);
2067                 if (!BFA_FCS_PID_IS_WKA(rport->pid))
2068                         bfa_fcs_rpf_rport_offline(rport);
2069         }
2070 }
2071
2072 /*
2073  * Update rport parameters from PLOGI or PLOGI accept.
2074  */
2075 static void
2076 bfa_fcs_rport_update(struct bfa_fcs_rport_s *rport, struct fc_logi_s *plogi)
2077 {
2078         bfa_fcs_lport_t *port = rport->port;
2079
2080         /*
2081          * - port name
2082          * - node name
2083          */
2084         rport->pwwn = plogi->port_name;
2085         rport->nwwn = plogi->node_name;
2086
2087         /*
2088          * - class of service
2089          */
2090         rport->fc_cos = 0;
2091         if (plogi->class3.class_valid)
2092                 rport->fc_cos = FC_CLASS_3;
2093
2094         if (plogi->class2.class_valid)
2095                 rport->fc_cos |= FC_CLASS_2;
2096
2097         /*
2098          * - CISC
2099          * - MAX receive frame size
2100          */
2101         rport->cisc = plogi->csp.cisc;
2102         rport->maxfrsize = be16_to_cpu(plogi->class3.rxsz);
2103
2104         bfa_trc(port->fcs, be16_to_cpu(plogi->csp.bbcred));
2105         bfa_trc(port->fcs, port->fabric->bb_credit);
2106         /*
2107          * Direct Attach P2P mode :
2108          * This is to handle a bug (233476) in IBM targets in Direct Attach
2109          *  Mode. Basically, in FLOGI Accept the target would have
2110          * erroneously set the BB Credit to the value used in the FLOGI
2111          * sent by the HBA. It uses the correct value (its own BB credit)
2112          * in PLOGI.
2113          */
2114         if ((!bfa_fcs_fabric_is_switched(port->fabric))  &&
2115                 (be16_to_cpu(plogi->csp.bbcred) < port->fabric->bb_credit)) {
2116
2117                 bfa_trc(port->fcs, be16_to_cpu(plogi->csp.bbcred));
2118                 bfa_trc(port->fcs, port->fabric->bb_credit);
2119
2120                 port->fabric->bb_credit = be16_to_cpu(plogi->csp.bbcred);
2121                 bfa_fcport_set_tx_bbcredit(port->fcs->bfa,
2122                                           port->fabric->bb_credit);
2123         }
2124
2125 }
2126
2127 /*
2128  *      Called to handle LOGO received from an existing remote port.
2129  */
2130 static void
2131 bfa_fcs_rport_process_logo(struct bfa_fcs_rport_s *rport, struct fchs_s *fchs)
2132 {
2133         rport->reply_oxid = fchs->ox_id;
2134         bfa_trc(rport->fcs, rport->reply_oxid);
2135
2136         rport->prlo = BFA_FALSE;
2137         rport->stats.logo_rcvd++;
2138         bfa_sm_send_event(rport, RPSM_EVENT_LOGO_RCVD);
2139 }
2140
2141
2142
2143 /*
2144  *  fcs_rport_public FCS rport public interfaces
2145  */
2146
2147 /*
2148  *      Called by bport/vport to create a remote port instance for a discovered
2149  *      remote device.
2150  *
2151  * @param[in] port      - base port or vport
2152  * @param[in] rpid      - remote port ID
2153  *
2154  * @return None
2155  */
2156 struct bfa_fcs_rport_s *
2157 bfa_fcs_rport_create(struct bfa_fcs_lport_s *port, u32 rpid)
2158 {
2159         struct bfa_fcs_rport_s *rport;
2160
2161         bfa_trc(port->fcs, rpid);
2162         rport = bfa_fcs_rport_alloc(port, WWN_NULL, rpid);
2163         if (!rport)
2164                 return NULL;
2165
2166         bfa_sm_send_event(rport, RPSM_EVENT_PLOGI_SEND);
2167         return rport;
2168 }
2169
2170 /*
2171  * Called to create a rport for which only the wwn is known.
2172  *
2173  * @param[in] port      - base port
2174  * @param[in] rpwwn     - remote port wwn
2175  *
2176  * @return None
2177  */
2178 struct bfa_fcs_rport_s *
2179 bfa_fcs_rport_create_by_wwn(struct bfa_fcs_lport_s *port, wwn_t rpwwn)
2180 {
2181         struct bfa_fcs_rport_s *rport;
2182         bfa_trc(port->fcs, rpwwn);
2183         rport = bfa_fcs_rport_alloc(port, rpwwn, 0);
2184         if (!rport)
2185                 return NULL;
2186
2187         bfa_sm_send_event(rport, RPSM_EVENT_ADDRESS_DISC);
2188         return rport;
2189 }
2190 /*
2191  * Called by bport in private loop topology to indicate that a
2192  * rport has been discovered and plogi has been completed.
2193  *
2194  * @param[in] port      - base port or vport
2195  * @param[in] rpid      - remote port ID
2196  */
2197 void
2198 bfa_fcs_rport_start(struct bfa_fcs_lport_s *port, struct fchs_s *fchs,
2199          struct fc_logi_s *plogi)
2200 {
2201         struct bfa_fcs_rport_s *rport;
2202
2203         rport = bfa_fcs_rport_alloc(port, WWN_NULL, fchs->s_id);
2204         if (!rport)
2205                 return;
2206
2207         bfa_fcs_rport_update(rport, plogi);
2208
2209         bfa_sm_send_event(rport, RPSM_EVENT_PLOGI_COMP);
2210 }
2211
2212 /*
2213  *      Called by bport/vport to handle PLOGI received from a new remote port.
2214  *      If an existing rport does a plogi, it will be handled separately.
2215  */
2216 void
2217 bfa_fcs_rport_plogi_create(struct bfa_fcs_lport_s *port, struct fchs_s *fchs,
2218                                 struct fc_logi_s *plogi)
2219 {
2220         struct bfa_fcs_rport_s *rport;
2221
2222         rport = bfa_fcs_rport_alloc(port, plogi->port_name, fchs->s_id);
2223         if (!rport)
2224                 return;
2225
2226         bfa_fcs_rport_update(rport, plogi);
2227
2228         rport->reply_oxid = fchs->ox_id;
2229         bfa_trc(rport->fcs, rport->reply_oxid);
2230
2231         rport->stats.plogi_rcvd++;
2232         bfa_sm_send_event(rport, RPSM_EVENT_PLOGI_RCVD);
2233 }
2234
2235 static int
2236 wwn_compare(wwn_t wwn1, wwn_t wwn2)
2237 {
2238         u8              *b1 = (u8 *) &wwn1;
2239         u8              *b2 = (u8 *) &wwn2;
2240         int             i;
2241
2242         for (i = 0; i < sizeof(wwn_t); i++) {
2243                 if (b1[i] < b2[i])
2244                         return -1;
2245                 if (b1[i] > b2[i])
2246                         return 1;
2247         }
2248         return 0;
2249 }
2250
2251 /*
2252  *      Called by bport/vport to handle PLOGI received from an existing
2253  *       remote port.
2254  */
2255 void
2256 bfa_fcs_rport_plogi(struct bfa_fcs_rport_s *rport, struct fchs_s *rx_fchs,
2257                         struct fc_logi_s *plogi)
2258 {
2259         /*
2260          * @todo Handle P2P and initiator-initiator.
2261          */
2262
2263         bfa_fcs_rport_update(rport, plogi);
2264
2265         rport->reply_oxid = rx_fchs->ox_id;
2266         bfa_trc(rport->fcs, rport->reply_oxid);
2267
2268         /*
2269          * In Switched fabric topology,
2270          * PLOGI to each other. If our pwwn is smaller, ignore it,
2271          * if it is not a well known address.
2272          * If the link topology is N2N,
2273          * this Plogi should be accepted.
2274          */
2275         if ((wwn_compare(rport->port->port_cfg.pwwn, rport->pwwn) == -1) &&
2276                 (bfa_fcs_fabric_is_switched(rport->port->fabric)) &&
2277                 (!BFA_FCS_PID_IS_WKA(rport->pid))) {
2278                 bfa_trc(rport->fcs, rport->pid);
2279                 return;
2280         }
2281
2282         rport->stats.plogi_rcvd++;
2283         bfa_sm_send_event(rport, RPSM_EVENT_PLOGI_RCVD);
2284 }
2285
2286
2287 /*
2288  *      Called by bport/vport to notify SCN for the remote port
2289  */
2290 void
2291 bfa_fcs_rport_scn(struct bfa_fcs_rport_s *rport)
2292 {
2293         rport->stats.rscns++;
2294         bfa_sm_send_event(rport, RPSM_EVENT_SCN);
2295 }
2296
2297
2298 /*
2299  *      brief
2300  *      This routine BFA callback for bfa_rport_online() call.
2301  *
2302  *      param[in]       cb_arg  -  rport struct.
2303  *
2304  *      return
2305  *              void
2306  *
2307  *      Special Considerations:
2308  *
2309  *      note
2310  */
2311 void
2312 bfa_cb_rport_online(void *cbarg)
2313 {
2314
2315         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
2316
2317         bfa_trc(rport->fcs, rport->pwwn);
2318         bfa_sm_send_event(rport, RPSM_EVENT_HCB_ONLINE);
2319 }
2320
2321 /*
2322  *      brief
2323  *      This routine BFA callback for bfa_rport_offline() call.
2324  *
2325  *      param[in]       rport   -
2326  *
2327  *      return
2328  *              void
2329  *
2330  *      Special Considerations:
2331  *
2332  *      note
2333  */
2334 void
2335 bfa_cb_rport_offline(void *cbarg)
2336 {
2337         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
2338
2339         bfa_trc(rport->fcs, rport->pwwn);
2340         bfa_sm_send_event(rport, RPSM_EVENT_HCB_OFFLINE);
2341 }
2342
2343 /*
2344  *      brief
2345  *      This routine is a static BFA callback when there is a QoS flow_id
2346  *      change notification
2347  *
2348  *      param[in]       rport   -
2349  *
2350  *      return
2351  *              void
2352  *
2353  *      Special Considerations:
2354  *
2355  *      note
2356  */
2357 void
2358 bfa_cb_rport_qos_scn_flowid(void *cbarg,
2359                 struct bfa_rport_qos_attr_s old_qos_attr,
2360                 struct bfa_rport_qos_attr_s new_qos_attr)
2361 {
2362         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
2363
2364         bfa_trc(rport->fcs, rport->pwwn);
2365 }
2366
2367 /*
2368  *      brief
2369  *      This routine is a static BFA callback when there is a QoS priority
2370  *      change notification
2371  *
2372  *      param[in]       rport   -
2373  *
2374  *      return
2375  *              void
2376  *
2377  *      Special Considerations:
2378  *
2379  *      note
2380  */
2381 void
2382 bfa_cb_rport_qos_scn_prio(void *cbarg,
2383                 struct bfa_rport_qos_attr_s old_qos_attr,
2384                 struct bfa_rport_qos_attr_s new_qos_attr)
2385 {
2386         struct bfa_fcs_rport_s *rport = (struct bfa_fcs_rport_s *) cbarg;
2387
2388         bfa_trc(rport->fcs, rport->pwwn);
2389 }
2390
2391 /*
2392  *              Called to process any unsolicted frames from this remote port
2393  */
2394 void
2395 bfa_fcs_rport_uf_recv(struct bfa_fcs_rport_s *rport,
2396                         struct fchs_s *fchs, u16 len)
2397 {
2398         struct bfa_fcs_lport_s *port = rport->port;
2399         struct fc_els_cmd_s     *els_cmd;
2400
2401         bfa_trc(rport->fcs, fchs->s_id);
2402         bfa_trc(rport->fcs, fchs->d_id);
2403         bfa_trc(rport->fcs, fchs->type);
2404
2405         if (fchs->type != FC_TYPE_ELS)
2406                 return;
2407
2408         els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
2409
2410         bfa_trc(rport->fcs, els_cmd->els_code);
2411
2412         switch (els_cmd->els_code) {
2413         case FC_ELS_LOGO:
2414                 bfa_stats(port, plogi_rcvd);
2415                 bfa_fcs_rport_process_logo(rport, fchs);
2416                 break;
2417
2418         case FC_ELS_ADISC:
2419                 bfa_stats(port, adisc_rcvd);
2420                 bfa_fcs_rport_process_adisc(rport, fchs, len);
2421                 break;
2422
2423         case FC_ELS_PRLO:
2424                 bfa_stats(port, prlo_rcvd);
2425                 if (bfa_fcs_lport_is_initiator(port))
2426                         bfa_fcs_fcpim_uf_recv(rport->itnim, fchs, len);
2427                 break;
2428
2429         case FC_ELS_PRLI:
2430                 bfa_stats(port, prli_rcvd);
2431                 bfa_fcs_rport_process_prli(rport, fchs, len);
2432                 break;
2433
2434         case FC_ELS_RPSC:
2435                 bfa_stats(port, rpsc_rcvd);
2436                 bfa_fcs_rport_process_rpsc(rport, fchs, len);
2437                 break;
2438
2439         default:
2440                 bfa_stats(port, un_handled_els_rcvd);
2441                 bfa_fcs_rport_send_ls_rjt(rport, fchs,
2442                                           FC_LS_RJT_RSN_CMD_NOT_SUPP,
2443                                           FC_LS_RJT_EXP_NO_ADDL_INFO);
2444                 break;
2445         }
2446 }
2447
2448 /* send best case  acc to prlo */
2449 static void
2450 bfa_fcs_rport_send_prlo_acc(struct bfa_fcs_rport_s *rport)
2451 {
2452         struct bfa_fcs_lport_s *port = rport->port;
2453         struct fchs_s   fchs;
2454         struct bfa_fcxp_s *fcxp;
2455         int             len;
2456
2457         bfa_trc(rport->fcs, rport->pid);
2458
2459         fcxp = bfa_fcs_fcxp_alloc(port->fcs);
2460         if (!fcxp)
2461                 return;
2462         len = fc_prlo_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
2463                         rport->pid, bfa_fcs_lport_get_fcid(port),
2464                         rport->reply_oxid, 0);
2465
2466         bfa_fcxp_send(fcxp, rport->bfa_rport, port->fabric->vf_id,
2467                 port->lp_tag, BFA_FALSE, FC_CLASS_3, len, &fchs,
2468                 NULL, NULL, FC_MAX_PDUSZ, 0);
2469 }
2470
2471 /*
2472  * Send a LS reject
2473  */
2474 static void
2475 bfa_fcs_rport_send_ls_rjt(struct bfa_fcs_rport_s *rport, struct fchs_s *rx_fchs,
2476                           u8 reason_code, u8 reason_code_expl)
2477 {
2478         struct bfa_fcs_lport_s *port = rport->port;
2479         struct fchs_s   fchs;
2480         struct bfa_fcxp_s *fcxp;
2481         int             len;
2482
2483         bfa_trc(rport->fcs, rx_fchs->s_id);
2484
2485         fcxp = bfa_fcs_fcxp_alloc(rport->fcs);
2486         if (!fcxp)
2487                 return;
2488
2489         len = fc_ls_rjt_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
2490                                 rx_fchs->s_id, bfa_fcs_lport_get_fcid(port),
2491                                 rx_fchs->ox_id, reason_code, reason_code_expl);
2492
2493         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag,
2494                         BFA_FALSE, FC_CLASS_3, len, &fchs, NULL, NULL,
2495                         FC_MAX_PDUSZ, 0);
2496 }
2497
2498 /*
2499  * Return state of rport.
2500  */
2501 int
2502 bfa_fcs_rport_get_state(struct bfa_fcs_rport_s *rport)
2503 {
2504         return bfa_sm_to_state(rport_sm_table, rport->sm);
2505 }
2506
2507
2508 /*
2509  *      brief
2510  *               Called by the Driver to set rport delete/ageout timeout
2511  *
2512  *      param[in]               rport timeout value in seconds.
2513  *
2514  *      return None
2515  */
2516 void
2517 bfa_fcs_rport_set_del_timeout(u8 rport_tmo)
2518 {
2519         /* convert to Millisecs */
2520         if (rport_tmo > 0)
2521                 bfa_fcs_rport_del_timeout = rport_tmo * 1000;
2522 }
2523 void
2524 bfa_fcs_rport_prlo(struct bfa_fcs_rport_s *rport, __be16 ox_id)
2525 {
2526         bfa_trc(rport->fcs, rport->pid);
2527
2528         rport->prlo = BFA_TRUE;
2529         rport->reply_oxid = ox_id;
2530         bfa_sm_send_event(rport, RPSM_EVENT_PRLO_RCVD);
2531 }
2532
2533
2534
2535 /*
2536  * Remote port implementation.
2537  */
2538
2539 /*
2540  *  fcs_rport_api FCS rport API.
2541  */
2542
2543 struct bfa_fcs_rport_s *
2544 bfa_fcs_rport_lookup(struct bfa_fcs_lport_s *port, wwn_t rpwwn)
2545 {
2546         struct bfa_fcs_rport_s *rport;
2547
2548         rport = bfa_fcs_lport_get_rport_by_pwwn(port, rpwwn);
2549         if (rport == NULL) {
2550                 /*
2551                  * TBD Error handling
2552                  */
2553         }
2554
2555         return rport;
2556 }
2557
2558 struct bfa_fcs_rport_s *
2559 bfa_fcs_rport_lookup_by_nwwn(struct bfa_fcs_lport_s *port, wwn_t rnwwn)
2560 {
2561         struct bfa_fcs_rport_s *rport;
2562
2563         rport = bfa_fcs_lport_get_rport_by_nwwn(port, rnwwn);
2564         if (rport == NULL) {
2565                 /*
2566                  * TBD Error handling
2567                  */
2568         }
2569
2570         return rport;
2571 }
2572
2573 /*
2574  * Remote port features (RPF) implementation.
2575  */
2576
2577 #define BFA_FCS_RPF_RETRIES     (3)
2578 #define BFA_FCS_RPF_RETRY_TIMEOUT  (1000) /* 1 sec (In millisecs) */
2579
2580 static void     bfa_fcs_rpf_send_rpsc2(void *rport_cbarg,
2581                                 struct bfa_fcxp_s *fcxp_alloced);
2582 static void     bfa_fcs_rpf_rpsc2_response(void *fcsarg,
2583                         struct bfa_fcxp_s *fcxp,
2584                         void *cbarg,
2585                         bfa_status_t req_status,
2586                         u32 rsp_len,
2587                         u32 resid_len,
2588                         struct fchs_s *rsp_fchs);
2589
2590 static void     bfa_fcs_rpf_timeout(void *arg);
2591
2592 /*
2593  *  fcs_rport_ftrs_sm FCS rport state machine events
2594  */
2595
2596 enum rpf_event {
2597         RPFSM_EVENT_RPORT_OFFLINE  = 1, /* Rport offline                */
2598         RPFSM_EVENT_RPORT_ONLINE   = 2, /* Rport online                 */
2599         RPFSM_EVENT_FCXP_SENT      = 3, /* Frame from has been sent     */
2600         RPFSM_EVENT_TIMEOUT        = 4, /* Rport SM timeout event       */
2601         RPFSM_EVENT_RPSC_COMP      = 5,
2602         RPFSM_EVENT_RPSC_FAIL      = 6,
2603         RPFSM_EVENT_RPSC_ERROR     = 7,
2604 };
2605
2606 static void     bfa_fcs_rpf_sm_uninit(struct bfa_fcs_rpf_s *rpf,
2607                                         enum rpf_event event);
2608 static void     bfa_fcs_rpf_sm_rpsc_sending(struct bfa_fcs_rpf_s *rpf,
2609                                        enum rpf_event event);
2610 static void     bfa_fcs_rpf_sm_rpsc(struct bfa_fcs_rpf_s *rpf,
2611                                        enum rpf_event event);
2612 static void     bfa_fcs_rpf_sm_rpsc_retry(struct bfa_fcs_rpf_s *rpf,
2613                                         enum rpf_event event);
2614 static void     bfa_fcs_rpf_sm_offline(struct bfa_fcs_rpf_s *rpf,
2615                                         enum rpf_event event);
2616 static void     bfa_fcs_rpf_sm_online(struct bfa_fcs_rpf_s *rpf,
2617                                         enum rpf_event event);
2618
2619 static void
2620 bfa_fcs_rpf_sm_uninit(struct bfa_fcs_rpf_s *rpf, enum rpf_event event)
2621 {
2622         struct bfa_fcs_rport_s *rport = rpf->rport;
2623         struct bfa_fcs_fabric_s *fabric = &rport->fcs->fabric;
2624
2625         bfa_trc(rport->fcs, rport->pwwn);
2626         bfa_trc(rport->fcs, rport->pid);
2627         bfa_trc(rport->fcs, event);
2628
2629         switch (event) {
2630         case RPFSM_EVENT_RPORT_ONLINE:
2631                 /* Send RPSC2 to a Brocade fabric only. */
2632                 if ((!BFA_FCS_PID_IS_WKA(rport->pid)) &&
2633                         ((rport->port->fabric->lps->brcd_switch) ||
2634                         (bfa_fcs_fabric_get_switch_oui(fabric) ==
2635                                                 BFA_FCS_BRCD_SWITCH_OUI))) {
2636                         bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_rpsc_sending);
2637                         rpf->rpsc_retries = 0;
2638                         bfa_fcs_rpf_send_rpsc2(rpf, NULL);
2639                 }
2640                 break;
2641
2642         case RPFSM_EVENT_RPORT_OFFLINE:
2643                 break;
2644
2645         default:
2646                 bfa_sm_fault(rport->fcs, event);
2647         }
2648 }
2649
2650 static void
2651 bfa_fcs_rpf_sm_rpsc_sending(struct bfa_fcs_rpf_s *rpf, enum rpf_event event)
2652 {
2653         struct bfa_fcs_rport_s *rport = rpf->rport;
2654
2655         bfa_trc(rport->fcs, event);
2656
2657         switch (event) {
2658         case RPFSM_EVENT_FCXP_SENT:
2659                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_rpsc);
2660                 break;
2661
2662         case RPFSM_EVENT_RPORT_OFFLINE:
2663                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_offline);
2664                 bfa_fcxp_walloc_cancel(rport->fcs->bfa, &rpf->fcxp_wqe);
2665                 rpf->rpsc_retries = 0;
2666                 break;
2667
2668         default:
2669                 bfa_sm_fault(rport->fcs, event);
2670         }
2671 }
2672
2673 static void
2674 bfa_fcs_rpf_sm_rpsc(struct bfa_fcs_rpf_s *rpf, enum rpf_event event)
2675 {
2676         struct bfa_fcs_rport_s *rport = rpf->rport;
2677
2678         bfa_trc(rport->fcs, rport->pid);
2679         bfa_trc(rport->fcs, event);
2680
2681         switch (event) {
2682         case RPFSM_EVENT_RPSC_COMP:
2683                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_online);
2684                 /* Update speed info in f/w via BFA */
2685                 if (rpf->rpsc_speed != BFA_PORT_SPEED_UNKNOWN)
2686                         bfa_rport_speed(rport->bfa_rport, rpf->rpsc_speed);
2687                 else if (rpf->assigned_speed != BFA_PORT_SPEED_UNKNOWN)
2688                         bfa_rport_speed(rport->bfa_rport, rpf->assigned_speed);
2689                 break;
2690
2691         case RPFSM_EVENT_RPSC_FAIL:
2692                 /* RPSC not supported by rport */
2693                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_online);
2694                 break;
2695
2696         case RPFSM_EVENT_RPSC_ERROR:
2697                 /* need to retry...delayed a bit. */
2698                 if (rpf->rpsc_retries++ < BFA_FCS_RPF_RETRIES) {
2699                         bfa_timer_start(rport->fcs->bfa, &rpf->timer,
2700                                     bfa_fcs_rpf_timeout, rpf,
2701                                     BFA_FCS_RPF_RETRY_TIMEOUT);
2702                         bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_rpsc_retry);
2703                 } else {
2704                         bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_online);
2705                 }
2706                 break;
2707
2708         case RPFSM_EVENT_RPORT_OFFLINE:
2709                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_offline);
2710                 bfa_fcxp_discard(rpf->fcxp);
2711                 rpf->rpsc_retries = 0;
2712                 break;
2713
2714         default:
2715                 bfa_sm_fault(rport->fcs, event);
2716         }
2717 }
2718
2719 static void
2720 bfa_fcs_rpf_sm_rpsc_retry(struct bfa_fcs_rpf_s *rpf, enum rpf_event event)
2721 {
2722         struct bfa_fcs_rport_s *rport = rpf->rport;
2723
2724         bfa_trc(rport->fcs, rport->pid);
2725         bfa_trc(rport->fcs, event);
2726
2727         switch (event) {
2728         case RPFSM_EVENT_TIMEOUT:
2729                 /* re-send the RPSC */
2730                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_rpsc_sending);
2731                 bfa_fcs_rpf_send_rpsc2(rpf, NULL);
2732                 break;
2733
2734         case RPFSM_EVENT_RPORT_OFFLINE:
2735                 bfa_timer_stop(&rpf->timer);
2736                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_offline);
2737                 rpf->rpsc_retries = 0;
2738                 break;
2739
2740         default:
2741                 bfa_sm_fault(rport->fcs, event);
2742         }
2743 }
2744
2745 static void
2746 bfa_fcs_rpf_sm_online(struct bfa_fcs_rpf_s *rpf, enum rpf_event event)
2747 {
2748         struct bfa_fcs_rport_s *rport = rpf->rport;
2749
2750         bfa_trc(rport->fcs, rport->pwwn);
2751         bfa_trc(rport->fcs, rport->pid);
2752         bfa_trc(rport->fcs, event);
2753
2754         switch (event) {
2755         case RPFSM_EVENT_RPORT_OFFLINE:
2756                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_offline);
2757                 rpf->rpsc_retries = 0;
2758                 break;
2759
2760         default:
2761                 bfa_sm_fault(rport->fcs, event);
2762         }
2763 }
2764
2765 static void
2766 bfa_fcs_rpf_sm_offline(struct bfa_fcs_rpf_s *rpf, enum rpf_event event)
2767 {
2768         struct bfa_fcs_rport_s *rport = rpf->rport;
2769
2770         bfa_trc(rport->fcs, rport->pwwn);
2771         bfa_trc(rport->fcs, rport->pid);
2772         bfa_trc(rport->fcs, event);
2773
2774         switch (event) {
2775         case RPFSM_EVENT_RPORT_ONLINE:
2776                 bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_rpsc_sending);
2777                 bfa_fcs_rpf_send_rpsc2(rpf, NULL);
2778                 break;
2779
2780         case RPFSM_EVENT_RPORT_OFFLINE:
2781                 break;
2782
2783         default:
2784                 bfa_sm_fault(rport->fcs, event);
2785         }
2786 }
2787 /*
2788  * Called when Rport is created.
2789  */
2790 void
2791 bfa_fcs_rpf_init(struct bfa_fcs_rport_s *rport)
2792 {
2793         struct bfa_fcs_rpf_s *rpf = &rport->rpf;
2794
2795         bfa_trc(rport->fcs, rport->pid);
2796         rpf->rport = rport;
2797
2798         bfa_sm_set_state(rpf, bfa_fcs_rpf_sm_uninit);
2799 }
2800
2801 /*
2802  * Called when Rport becomes online
2803  */
2804 void
2805 bfa_fcs_rpf_rport_online(struct bfa_fcs_rport_s *rport)
2806 {
2807         bfa_trc(rport->fcs, rport->pid);
2808
2809         if (__fcs_min_cfg(rport->port->fcs))
2810                 return;
2811
2812         if (bfa_fcs_fabric_is_switched(rport->port->fabric))
2813                 bfa_sm_send_event(&rport->rpf, RPFSM_EVENT_RPORT_ONLINE);
2814 }
2815
2816 /*
2817  * Called when Rport becomes offline
2818  */
2819 void
2820 bfa_fcs_rpf_rport_offline(struct bfa_fcs_rport_s *rport)
2821 {
2822         bfa_trc(rport->fcs, rport->pid);
2823
2824         if (__fcs_min_cfg(rport->port->fcs))
2825                 return;
2826
2827         rport->rpf.rpsc_speed = 0;
2828         bfa_sm_send_event(&rport->rpf, RPFSM_EVENT_RPORT_OFFLINE);
2829 }
2830
2831 static void
2832 bfa_fcs_rpf_timeout(void *arg)
2833 {
2834         struct bfa_fcs_rpf_s *rpf = (struct bfa_fcs_rpf_s *) arg;
2835         struct bfa_fcs_rport_s *rport = rpf->rport;
2836
2837         bfa_trc(rport->fcs, rport->pid);
2838         bfa_sm_send_event(rpf, RPFSM_EVENT_TIMEOUT);
2839 }
2840
2841 static void
2842 bfa_fcs_rpf_send_rpsc2(void *rpf_cbarg, struct bfa_fcxp_s *fcxp_alloced)
2843 {
2844         struct bfa_fcs_rpf_s *rpf = (struct bfa_fcs_rpf_s *)rpf_cbarg;
2845         struct bfa_fcs_rport_s *rport = rpf->rport;
2846         struct bfa_fcs_lport_s *port = rport->port;
2847         struct fchs_s   fchs;
2848         int             len;
2849         struct bfa_fcxp_s *fcxp;
2850
2851         bfa_trc(rport->fcs, rport->pwwn);
2852
2853         fcxp = fcxp_alloced ? fcxp_alloced : bfa_fcs_fcxp_alloc(port->fcs);
2854         if (!fcxp) {
2855                 bfa_fcs_fcxp_alloc_wait(port->fcs->bfa, &rpf->fcxp_wqe,
2856                                         bfa_fcs_rpf_send_rpsc2, rpf);
2857                 return;
2858         }
2859         rpf->fcxp = fcxp;
2860
2861         len = fc_rpsc2_build(&fchs, bfa_fcxp_get_reqbuf(fcxp), rport->pid,
2862                             bfa_fcs_lport_get_fcid(port), &rport->pid, 1);
2863
2864         bfa_fcxp_send(fcxp, NULL, port->fabric->vf_id, port->lp_tag, BFA_FALSE,
2865                           FC_CLASS_3, len, &fchs, bfa_fcs_rpf_rpsc2_response,
2866                           rpf, FC_MAX_PDUSZ, FC_ELS_TOV);
2867         rport->stats.rpsc_sent++;
2868         bfa_sm_send_event(rpf, RPFSM_EVENT_FCXP_SENT);
2869
2870 }
2871
2872 static void
2873 bfa_fcs_rpf_rpsc2_response(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
2874                             bfa_status_t req_status, u32 rsp_len,
2875                             u32 resid_len, struct fchs_s *rsp_fchs)
2876 {
2877         struct bfa_fcs_rpf_s *rpf = (struct bfa_fcs_rpf_s *) cbarg;
2878         struct bfa_fcs_rport_s *rport = rpf->rport;
2879         struct fc_ls_rjt_s *ls_rjt;
2880         struct fc_rpsc2_acc_s *rpsc2_acc;
2881         u16     num_ents;
2882
2883         bfa_trc(rport->fcs, req_status);
2884
2885         if (req_status != BFA_STATUS_OK) {
2886                 bfa_trc(rport->fcs, req_status);
2887                 if (req_status == BFA_STATUS_ETIMER)
2888                         rport->stats.rpsc_failed++;
2889                 bfa_sm_send_event(rpf, RPFSM_EVENT_RPSC_ERROR);
2890                 return;
2891         }
2892
2893         rpsc2_acc = (struct fc_rpsc2_acc_s *) BFA_FCXP_RSP_PLD(fcxp);
2894         if (rpsc2_acc->els_cmd == FC_ELS_ACC) {
2895                 rport->stats.rpsc_accs++;
2896                 num_ents = be16_to_cpu(rpsc2_acc->num_pids);
2897                 bfa_trc(rport->fcs, num_ents);
2898                 if (num_ents > 0) {
2899                         bfa_assert(rpsc2_acc->port_info[0].pid != rport->pid);
2900                         bfa_trc(rport->fcs,
2901                                 be16_to_cpu(rpsc2_acc->port_info[0].pid));
2902                         bfa_trc(rport->fcs,
2903                                 be16_to_cpu(rpsc2_acc->port_info[0].speed));
2904                         bfa_trc(rport->fcs,
2905                                 be16_to_cpu(rpsc2_acc->port_info[0].index));
2906                         bfa_trc(rport->fcs,
2907                                 rpsc2_acc->port_info[0].type);
2908
2909                         if (rpsc2_acc->port_info[0].speed == 0) {
2910                                 bfa_sm_send_event(rpf, RPFSM_EVENT_RPSC_ERROR);
2911                                 return;
2912                         }
2913
2914                         rpf->rpsc_speed = fc_rpsc_operspeed_to_bfa_speed(
2915                                 be16_to_cpu(rpsc2_acc->port_info[0].speed));
2916
2917                         bfa_sm_send_event(rpf, RPFSM_EVENT_RPSC_COMP);
2918                 }
2919         } else {
2920                 ls_rjt = (struct fc_ls_rjt_s *) BFA_FCXP_RSP_PLD(fcxp);
2921                 bfa_trc(rport->fcs, ls_rjt->reason_code);
2922                 bfa_trc(rport->fcs, ls_rjt->reason_code_expl);
2923                 rport->stats.rpsc_rejects++;
2924                 if (ls_rjt->reason_code == FC_LS_RJT_RSN_CMD_NOT_SUPP)
2925                         bfa_sm_send_event(rpf, RPFSM_EVENT_RPSC_FAIL);
2926                 else
2927                         bfa_sm_send_event(rpf, RPFSM_EVENT_RPSC_ERROR);
2928         }
2929 }