These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / scsi / lpfc / lpfc_nportdisc.c
1  /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2015 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_sli4.h"
36 #include "lpfc_nl.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44
45
46 /* Called to verify a rcv'ed ADISC was intended for us. */
47 static int
48 lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
49                  struct lpfc_name *nn, struct lpfc_name *pn)
50 {
51         /* First, we MUST have a RPI registered */
52         if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED))
53                 return 0;
54
55         /* Compare the ADISC rsp WWNN / WWPN matches our internal node
56          * table entry for that node.
57          */
58         if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)))
59                 return 0;
60
61         if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)))
62                 return 0;
63
64         /* we match, return success */
65         return 1;
66 }
67
68 int
69 lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
70                  struct serv_parm *sp, uint32_t class, int flogi)
71 {
72         volatile struct serv_parm *hsp = &vport->fc_sparam;
73         uint16_t hsp_value, ssp_value = 0;
74
75         /*
76          * The receive data field size and buffer-to-buffer receive data field
77          * size entries are 16 bits but are represented as two 8-bit fields in
78          * the driver data structure to account for rsvd bits and other control
79          * bits.  Reconstruct and compare the fields as a 16-bit values before
80          * correcting the byte values.
81          */
82         if (sp->cls1.classValid) {
83                 if (!flogi) {
84                         hsp_value = ((hsp->cls1.rcvDataSizeMsb << 8) |
85                                      hsp->cls1.rcvDataSizeLsb);
86                         ssp_value = ((sp->cls1.rcvDataSizeMsb << 8) |
87                                      sp->cls1.rcvDataSizeLsb);
88                         if (!ssp_value)
89                                 goto bad_service_param;
90                         if (ssp_value > hsp_value) {
91                                 sp->cls1.rcvDataSizeLsb =
92                                         hsp->cls1.rcvDataSizeLsb;
93                                 sp->cls1.rcvDataSizeMsb =
94                                         hsp->cls1.rcvDataSizeMsb;
95                         }
96                 }
97         } else if (class == CLASS1)
98                 goto bad_service_param;
99         if (sp->cls2.classValid) {
100                 if (!flogi) {
101                         hsp_value = ((hsp->cls2.rcvDataSizeMsb << 8) |
102                                      hsp->cls2.rcvDataSizeLsb);
103                         ssp_value = ((sp->cls2.rcvDataSizeMsb << 8) |
104                                      sp->cls2.rcvDataSizeLsb);
105                         if (!ssp_value)
106                                 goto bad_service_param;
107                         if (ssp_value > hsp_value) {
108                                 sp->cls2.rcvDataSizeLsb =
109                                         hsp->cls2.rcvDataSizeLsb;
110                                 sp->cls2.rcvDataSizeMsb =
111                                         hsp->cls2.rcvDataSizeMsb;
112                         }
113                 }
114         } else if (class == CLASS2)
115                 goto bad_service_param;
116         if (sp->cls3.classValid) {
117                 if (!flogi) {
118                         hsp_value = ((hsp->cls3.rcvDataSizeMsb << 8) |
119                                      hsp->cls3.rcvDataSizeLsb);
120                         ssp_value = ((sp->cls3.rcvDataSizeMsb << 8) |
121                                      sp->cls3.rcvDataSizeLsb);
122                         if (!ssp_value)
123                                 goto bad_service_param;
124                         if (ssp_value > hsp_value) {
125                                 sp->cls3.rcvDataSizeLsb =
126                                         hsp->cls3.rcvDataSizeLsb;
127                                 sp->cls3.rcvDataSizeMsb =
128                                         hsp->cls3.rcvDataSizeMsb;
129                         }
130                 }
131         } else if (class == CLASS3)
132                 goto bad_service_param;
133
134         /*
135          * Preserve the upper four bits of the MSB from the PLOGI response.
136          * These bits contain the Buffer-to-Buffer State Change Number
137          * from the target and need to be passed to the FW.
138          */
139         hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
140         ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
141         if (ssp_value > hsp_value) {
142                 sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
143                 sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
144                                        (hsp->cmn.bbRcvSizeMsb & 0x0F);
145         }
146
147         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
148         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
149         return 1;
150 bad_service_param:
151         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
152                          "0207 Device %x "
153                          "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
154                          "invalid service parameters.  Ignoring device.\n",
155                          ndlp->nlp_DID,
156                          sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
157                          sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
158                          sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
159                          sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
160         return 0;
161 }
162
163 static void *
164 lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
165                         struct lpfc_iocbq *rspiocb)
166 {
167         struct lpfc_dmabuf *pcmd, *prsp;
168         uint32_t *lp;
169         void     *ptr = NULL;
170         IOCB_t   *irsp;
171
172         irsp = &rspiocb->iocb;
173         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
174
175         /* For lpfc_els_abort, context2 could be zero'ed to delay
176          * freeing associated memory till after ABTS completes.
177          */
178         if (pcmd) {
179                 prsp =  list_get_first(&pcmd->list, struct lpfc_dmabuf,
180                                        list);
181                 if (prsp) {
182                         lp = (uint32_t *) prsp->virt;
183                         ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
184                 }
185         } else {
186                 /* Force ulpStatus error since we are returning NULL ptr */
187                 if (!(irsp->ulpStatus)) {
188                         irsp->ulpStatus = IOSTAT_LOCAL_REJECT;
189                         irsp->un.ulpWord[4] = IOERR_SLI_ABORTED;
190                 }
191                 ptr = NULL;
192         }
193         return ptr;
194 }
195
196
197
198 /*
199  * Free resources / clean up outstanding I/Os
200  * associated with a LPFC_NODELIST entry. This
201  * routine effectively results in a "software abort".
202  */
203 int
204 lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
205 {
206         LIST_HEAD(abort_list);
207         struct lpfc_sli  *psli = &phba->sli;
208         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
209         struct lpfc_iocbq *iocb, *next_iocb;
210
211         /* Abort outstanding I/O on NPort <nlp_DID> */
212         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
213                          "2819 Abort outstanding I/O on NPort x%x "
214                          "Data: x%x x%x x%x\n",
215                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
216                          ndlp->nlp_rpi);
217         /* Clean up all fabric IOs first.*/
218         lpfc_fabric_abort_nport(ndlp);
219
220         /*
221          * Lock the ELS ring txcmplq for SLI3/SLI4 and build a local list
222          * of all ELS IOs that need an ABTS.  The IOs need to stay on the
223          * txcmplq so that the abort operation completes them successfully.
224          */
225         spin_lock_irq(&phba->hbalock);
226         if (phba->sli_rev == LPFC_SLI_REV4)
227                 spin_lock(&pring->ring_lock);
228         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
229         /* Add to abort_list on on NDLP match. */
230                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
231                         list_add_tail(&iocb->dlist, &abort_list);
232         }
233         if (phba->sli_rev == LPFC_SLI_REV4)
234                 spin_unlock(&pring->ring_lock);
235         spin_unlock_irq(&phba->hbalock);
236
237         /* Abort the targeted IOs and remove them from the abort list. */
238         list_for_each_entry_safe(iocb, next_iocb, &abort_list, dlist) {
239                         spin_lock_irq(&phba->hbalock);
240                         list_del_init(&iocb->dlist);
241                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
242                         spin_unlock_irq(&phba->hbalock);
243         }
244
245         INIT_LIST_HEAD(&abort_list);
246
247         /* Now process the txq */
248         spin_lock_irq(&phba->hbalock);
249         if (phba->sli_rev == LPFC_SLI_REV4)
250                 spin_lock(&pring->ring_lock);
251
252         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
253                 /* Check to see if iocb matches the nport we are looking for */
254                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
255                         list_del_init(&iocb->list);
256                         list_add_tail(&iocb->list, &abort_list);
257                 }
258         }
259
260         if (phba->sli_rev == LPFC_SLI_REV4)
261                 spin_unlock(&pring->ring_lock);
262         spin_unlock_irq(&phba->hbalock);
263
264         /* Cancel all the IOCBs from the completions list */
265         lpfc_sli_cancel_iocbs(phba, &abort_list,
266                               IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
267
268         lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
269         return 0;
270 }
271
272 static int
273 lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
274                struct lpfc_iocbq *cmdiocb)
275 {
276         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
277         struct lpfc_hba    *phba = vport->phba;
278         struct lpfc_dmabuf *pcmd;
279         uint64_t nlp_portwwn = 0;
280         uint32_t *lp;
281         IOCB_t *icmd;
282         struct serv_parm *sp;
283         LPFC_MBOXQ_t *mbox;
284         struct ls_rjt stat;
285         int rc;
286
287         memset(&stat, 0, sizeof (struct ls_rjt));
288         if (vport->port_state <= LPFC_FDISC) {
289                 /* Before responding to PLOGI, check for pt2pt mode.
290                  * If we are pt2pt, with an outstanding FLOGI, abort
291                  * the FLOGI and resend it first.
292                  */
293                 if (vport->fc_flag & FC_PT2PT) {
294                          lpfc_els_abort_flogi(phba);
295                         if (!(vport->fc_flag & FC_PT2PT_PLOGI)) {
296                                 /* If the other side is supposed to initiate
297                                  * the PLOGI anyway, just ACC it now and
298                                  * move on with discovery.
299                                  */
300                                 phba->fc_edtov = FF_DEF_EDTOV;
301                                 phba->fc_ratov = FF_DEF_RATOV;
302                                 /* Start discovery - this should just do
303                                    CLEAR_LA */
304                                 lpfc_disc_start(vport);
305                         } else
306                                 lpfc_initial_flogi(vport);
307                 } else {
308                         stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
309                         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
310                         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
311                                             ndlp, NULL);
312                         return 0;
313                 }
314         }
315         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
316         lp = (uint32_t *) pcmd->virt;
317         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
318         if (wwn_to_u64(sp->portName.u.wwn) == 0) {
319                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
320                                  "0140 PLOGI Reject: invalid nname\n");
321                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
322                 stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
323                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
324                         NULL);
325                 return 0;
326         }
327         if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
328                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
329                                  "0141 PLOGI Reject: invalid pname\n");
330                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
331                 stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
332                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
333                         NULL);
334                 return 0;
335         }
336
337         nlp_portwwn = wwn_to_u64(ndlp->nlp_portname.u.wwn);
338         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
339                 /* Reject this request because invalid parameters */
340                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
341                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
342                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
343                         NULL);
344                 return 0;
345         }
346         icmd = &cmdiocb->iocb;
347
348         /* PLOGI chkparm OK */
349         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
350                          "0114 PLOGI chkparm OK Data: x%x x%x x%x "
351                          "x%x x%x x%x\n",
352                          ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
353                          ndlp->nlp_rpi, vport->port_state,
354                          vport->fc_flag);
355
356         if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
357                 ndlp->nlp_fcp_info |= CLASS2;
358         else
359                 ndlp->nlp_fcp_info |= CLASS3;
360
361         ndlp->nlp_class_sup = 0;
362         if (sp->cls1.classValid)
363                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
364         if (sp->cls2.classValid)
365                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
366         if (sp->cls3.classValid)
367                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
368         if (sp->cls4.classValid)
369                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
370         ndlp->nlp_maxframe =
371                 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
372
373         /* if already logged in, do implicit logout */
374         switch (ndlp->nlp_state) {
375         case  NLP_STE_NPR_NODE:
376                 if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
377                         break;
378         case  NLP_STE_REG_LOGIN_ISSUE:
379         case  NLP_STE_PRLI_ISSUE:
380         case  NLP_STE_UNMAPPED_NODE:
381         case  NLP_STE_MAPPED_NODE:
382                 /* lpfc_plogi_confirm_nport skips fabric did, handle it here */
383                 if (!(ndlp->nlp_type & NLP_FABRIC)) {
384                         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
385                                          ndlp, NULL);
386                         return 1;
387                 }
388                 if (nlp_portwwn != 0 &&
389                     nlp_portwwn != wwn_to_u64(sp->portName.u.wwn))
390                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
391                                          "0143 PLOGI recv'd from DID: x%x "
392                                          "WWPN changed: old %llx new %llx\n",
393                                          ndlp->nlp_DID,
394                                          (unsigned long long)nlp_portwwn,
395                                          (unsigned long long)
396                                          wwn_to_u64(sp->portName.u.wwn));
397
398                 ndlp->nlp_prev_state = ndlp->nlp_state;
399                 /* rport needs to be unregistered first */
400                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
401                 break;
402         }
403
404         /* Check for Nport to NPort pt2pt protocol */
405         if ((vport->fc_flag & FC_PT2PT) &&
406             !(vport->fc_flag & FC_PT2PT_PLOGI)) {
407
408                 /* rcv'ed PLOGI decides what our NPortId will be */
409                 vport->fc_myDID = icmd->un.rcvels.parmRo;
410                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
411                 if (mbox == NULL)
412                         goto out;
413                 lpfc_config_link(phba, mbox);
414                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
415                 mbox->vport = vport;
416                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
417                 if (rc == MBX_NOT_FINISHED) {
418                         mempool_free(mbox, phba->mbox_mem_pool);
419                         goto out;
420                 }
421                 /*
422                  * For SLI4, the VFI/VPI are registered AFTER the
423                  * Nport with the higher WWPN sends us a PLOGI with
424                  * our assigned NPortId.
425                  */
426                 if (phba->sli_rev == LPFC_SLI_REV4)
427                         lpfc_issue_reg_vfi(vport);
428
429                 lpfc_can_disctmo(vport);
430         }
431         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
432         if (!mbox)
433                 goto out;
434
435         /* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
436         if (phba->sli_rev == LPFC_SLI_REV4)
437                 lpfc_unreg_rpi(vport, ndlp);
438
439         rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
440                             (uint8_t *) sp, mbox, ndlp->nlp_rpi);
441         if (rc) {
442                 mempool_free(mbox, phba->mbox_mem_pool);
443                 goto out;
444         }
445
446         /* ACC PLOGI rsp command needs to execute first,
447          * queue this mbox command to be processed later.
448          */
449         mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
450         /*
451          * mbox->context2 = lpfc_nlp_get(ndlp) deferred until mailbox
452          * command issued in lpfc_cmpl_els_acc().
453          */
454         mbox->vport = vport;
455         spin_lock_irq(shost->host_lock);
456         ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
457         spin_unlock_irq(shost->host_lock);
458
459         /*
460          * If there is an outstanding PLOGI issued, abort it before
461          * sending ACC rsp for received PLOGI. If pending plogi
462          * is not canceled here, the plogi will be rejected by
463          * remote port and will be retried. On a configuration with
464          * single discovery thread, this will cause a huge delay in
465          * discovery. Also this will cause multiple state machines
466          * running in parallel for this node.
467          */
468         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) {
469                 /* software abort outstanding PLOGI */
470                 lpfc_els_abort(phba, ndlp);
471         }
472
473         if ((vport->port_type == LPFC_NPIV_PORT &&
474              vport->cfg_restrict_login)) {
475
476                 /* In order to preserve RPIs, we want to cleanup
477                  * the default RPI the firmware created to rcv
478                  * this ELS request. The only way to do this is
479                  * to register, then unregister the RPI.
480                  */
481                 spin_lock_irq(shost->host_lock);
482                 ndlp->nlp_flag |= NLP_RM_DFLT_RPI;
483                 spin_unlock_irq(shost->host_lock);
484                 stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
485                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
486                 rc = lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
487                         ndlp, mbox);
488                 if (rc)
489                         mempool_free(mbox, phba->mbox_mem_pool);
490                 return 1;
491         }
492         rc = lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox);
493         if (rc)
494                 mempool_free(mbox, phba->mbox_mem_pool);
495         return 1;
496 out:
497         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
498         stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
499         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
500         return 0;
501 }
502
503 /**
504  * lpfc_mbx_cmpl_resume_rpi - Resume RPI completion routine
505  * @phba: pointer to lpfc hba data structure.
506  * @mboxq: pointer to mailbox object
507  *
508  * This routine is invoked to issue a completion to a rcv'ed
509  * ADISC or PDISC after the paused RPI has been resumed.
510  **/
511 static void
512 lpfc_mbx_cmpl_resume_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
513 {
514         struct lpfc_vport *vport;
515         struct lpfc_iocbq *elsiocb;
516         struct lpfc_nodelist *ndlp;
517         uint32_t cmd;
518
519         elsiocb = (struct lpfc_iocbq *)mboxq->context1;
520         ndlp = (struct lpfc_nodelist *) mboxq->context2;
521         vport = mboxq->vport;
522         cmd = elsiocb->drvrTimeout;
523
524         if (cmd == ELS_CMD_ADISC) {
525                 lpfc_els_rsp_adisc_acc(vport, elsiocb, ndlp);
526         } else {
527                 lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, elsiocb,
528                         ndlp, NULL);
529         }
530         kfree(elsiocb);
531         mempool_free(mboxq, phba->mbox_mem_pool);
532 }
533
534 static int
535 lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
536                 struct lpfc_iocbq *cmdiocb)
537 {
538         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
539         struct lpfc_iocbq  *elsiocb;
540         struct lpfc_dmabuf *pcmd;
541         struct serv_parm   *sp;
542         struct lpfc_name   *pnn, *ppn;
543         struct ls_rjt stat;
544         ADISC *ap;
545         IOCB_t *icmd;
546         uint32_t *lp;
547         uint32_t cmd;
548
549         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
550         lp = (uint32_t *) pcmd->virt;
551
552         cmd = *lp++;
553         if (cmd == ELS_CMD_ADISC) {
554                 ap = (ADISC *) lp;
555                 pnn = (struct lpfc_name *) & ap->nodeName;
556                 ppn = (struct lpfc_name *) & ap->portName;
557         } else {
558                 sp = (struct serv_parm *) lp;
559                 pnn = (struct lpfc_name *) & sp->nodeName;
560                 ppn = (struct lpfc_name *) & sp->portName;
561         }
562
563         icmd = &cmdiocb->iocb;
564         if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
565
566                 /*
567                  * As soon as  we send ACC, the remote NPort can
568                  * start sending us data. Thus, for SLI4 we must
569                  * resume the RPI before the ACC goes out.
570                  */
571                 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
572                         elsiocb = kmalloc(sizeof(struct lpfc_iocbq),
573                                 GFP_KERNEL);
574                         if (elsiocb) {
575
576                                 /* Save info from cmd IOCB used in rsp */
577                                 memcpy((uint8_t *)elsiocb, (uint8_t *)cmdiocb,
578                                         sizeof(struct lpfc_iocbq));
579
580                                 /* Save the ELS cmd */
581                                 elsiocb->drvrTimeout = cmd;
582
583                                 lpfc_sli4_resume_rpi(ndlp,
584                                         lpfc_mbx_cmpl_resume_rpi, elsiocb);
585                                 goto out;
586                         }
587                 }
588
589                 if (cmd == ELS_CMD_ADISC) {
590                         lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
591                 } else {
592                         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
593                                 ndlp, NULL);
594                 }
595 out:
596                 /* If we are authenticated, move to the proper state */
597                 if (ndlp->nlp_type & NLP_FCP_TARGET)
598                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
599                 else
600                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
601
602                 return 1;
603         }
604         /* Reject this request because invalid parameters */
605         stat.un.b.lsRjtRsvd0 = 0;
606         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
607         stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
608         stat.un.b.vendorUnique = 0;
609         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
610
611         /* 1 sec timeout */
612         mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
613
614         spin_lock_irq(shost->host_lock);
615         ndlp->nlp_flag |= NLP_DELAY_TMO;
616         spin_unlock_irq(shost->host_lock);
617         ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
618         ndlp->nlp_prev_state = ndlp->nlp_state;
619         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
620         return 0;
621 }
622
623 static int
624 lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
625               struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
626 {
627         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
628         struct lpfc_hba    *phba = vport->phba;
629         struct lpfc_vport **vports;
630         int i, active_vlink_present = 0 ;
631
632         /* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
633         /* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
634          * PLOGIs during LOGO storms from a device.
635          */
636         spin_lock_irq(shost->host_lock);
637         ndlp->nlp_flag |= NLP_LOGO_ACC;
638         spin_unlock_irq(shost->host_lock);
639         if (els_cmd == ELS_CMD_PRLO)
640                 lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
641         else
642                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
643         if (ndlp->nlp_DID == Fabric_DID) {
644                 if (vport->port_state <= LPFC_FDISC)
645                         goto out;
646                 lpfc_linkdown_port(vport);
647                 spin_lock_irq(shost->host_lock);
648                 vport->fc_flag |= FC_VPORT_LOGO_RCVD;
649                 spin_unlock_irq(shost->host_lock);
650                 vports = lpfc_create_vport_work_array(phba);
651                 if (vports) {
652                         for (i = 0; i <= phba->max_vports && vports[i] != NULL;
653                                         i++) {
654                                 if ((!(vports[i]->fc_flag &
655                                         FC_VPORT_LOGO_RCVD)) &&
656                                         (vports[i]->port_state > LPFC_FDISC)) {
657                                         active_vlink_present = 1;
658                                         break;
659                                 }
660                         }
661                         lpfc_destroy_vport_work_array(phba, vports);
662                 }
663
664                 /*
665                  * Don't re-instantiate if vport is marked for deletion.
666                  * If we are here first then vport_delete is going to wait
667                  * for discovery to complete.
668                  */
669                 if (!(vport->load_flag & FC_UNLOADING) &&
670                                         active_vlink_present) {
671                         /*
672                          * If there are other active VLinks present,
673                          * re-instantiate the Vlink using FDISC.
674                          */
675                         mod_timer(&ndlp->nlp_delayfunc,
676                                   jiffies + msecs_to_jiffies(1000));
677                         spin_lock_irq(shost->host_lock);
678                         ndlp->nlp_flag |= NLP_DELAY_TMO;
679                         spin_unlock_irq(shost->host_lock);
680                         ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
681                         vport->port_state = LPFC_FDISC;
682                 } else {
683                         spin_lock_irq(shost->host_lock);
684                         phba->pport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG;
685                         spin_unlock_irq(shost->host_lock);
686                         lpfc_retry_pport_discovery(phba);
687                 }
688         } else if ((!(ndlp->nlp_type & NLP_FABRIC) &&
689                 ((ndlp->nlp_type & NLP_FCP_TARGET) ||
690                 !(ndlp->nlp_type & NLP_FCP_INITIATOR))) ||
691                 (ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
692                 /* Only try to re-login if this is NOT a Fabric Node */
693                 mod_timer(&ndlp->nlp_delayfunc,
694                           jiffies + msecs_to_jiffies(1000 * 1));
695                 spin_lock_irq(shost->host_lock);
696                 ndlp->nlp_flag |= NLP_DELAY_TMO;
697                 spin_unlock_irq(shost->host_lock);
698
699                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
700         }
701 out:
702         ndlp->nlp_prev_state = ndlp->nlp_state;
703         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
704
705         spin_lock_irq(shost->host_lock);
706         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
707         spin_unlock_irq(shost->host_lock);
708         /* The driver has to wait until the ACC completes before it continues
709          * processing the LOGO.  The action will resume in
710          * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
711          * unreg_login, the driver waits so the ACC does not get aborted.
712          */
713         return 0;
714 }
715
716 static void
717 lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
718               struct lpfc_iocbq *cmdiocb)
719 {
720         struct lpfc_dmabuf *pcmd;
721         uint32_t *lp;
722         PRLI *npr;
723         struct fc_rport *rport = ndlp->rport;
724         u32 roles;
725
726         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
727         lp = (uint32_t *) pcmd->virt;
728         npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));
729
730         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
731         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
732         ndlp->nlp_flag &= ~NLP_FIRSTBURST;
733         if (npr->prliType == PRLI_FCP_TYPE) {
734                 if (npr->initiatorFunc)
735                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
736                 if (npr->targetFunc) {
737                         ndlp->nlp_type |= NLP_FCP_TARGET;
738                         if (npr->writeXferRdyDis)
739                                 ndlp->nlp_flag |= NLP_FIRSTBURST;
740                 }
741                 if (npr->Retry)
742                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
743         }
744         if (rport) {
745                 /* We need to update the rport role values */
746                 roles = FC_RPORT_ROLE_UNKNOWN;
747                 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
748                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
749                 if (ndlp->nlp_type & NLP_FCP_TARGET)
750                         roles |= FC_RPORT_ROLE_FCP_TARGET;
751
752                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
753                         "rport rolechg:   role:x%x did:x%x flg:x%x",
754                         roles, ndlp->nlp_DID, ndlp->nlp_flag);
755
756                 fc_remote_port_rolechg(rport, roles);
757         }
758 }
759
760 static uint32_t
761 lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
762 {
763         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
764
765         if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
766                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
767                 return 0;
768         }
769
770         if (!(vport->fc_flag & FC_PT2PT)) {
771                 /* Check config parameter use-adisc or FCP-2 */
772                 if ((vport->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
773                     ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
774                      (ndlp->nlp_type & NLP_FCP_TARGET))) {
775                         spin_lock_irq(shost->host_lock);
776                         ndlp->nlp_flag |= NLP_NPR_ADISC;
777                         spin_unlock_irq(shost->host_lock);
778                         return 1;
779                 }
780         }
781         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
782         lpfc_unreg_rpi(vport, ndlp);
783         return 0;
784 }
785
786 /**
787  * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
788  * @phba : Pointer to lpfc_hba structure.
789  * @vport: Pointer to lpfc_vport structure.
790  * @rpi  : rpi to be release.
791  *
792  * This function will send a unreg_login mailbox command to the firmware
793  * to release a rpi.
794  **/
795 void
796 lpfc_release_rpi(struct lpfc_hba *phba,
797                 struct lpfc_vport *vport,
798                 uint16_t rpi)
799 {
800         LPFC_MBOXQ_t *pmb;
801         int rc;
802
803         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
804                         GFP_KERNEL);
805         if (!pmb)
806                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
807                         "2796 mailbox memory allocation failed \n");
808         else {
809                 lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
810                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
811                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
812                 if (rc == MBX_NOT_FINISHED)
813                         mempool_free(pmb, phba->mbox_mem_pool);
814         }
815 }
816
817 static uint32_t
818 lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
819                   void *arg, uint32_t evt)
820 {
821         struct lpfc_hba *phba;
822         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
823         uint16_t rpi;
824
825         phba = vport->phba;
826         /* Release the RPI if reglogin completing */
827         if (!(phba->pport->load_flag & FC_UNLOADING) &&
828                 (evt == NLP_EVT_CMPL_REG_LOGIN) &&
829                 (!pmb->u.mb.mbxStatus)) {
830                 rpi = pmb->u.mb.un.varWords[0];
831                 lpfc_release_rpi(phba, vport, rpi);
832         }
833         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
834                          "0271 Illegal State Transition: node x%x "
835                          "event x%x, state x%x Data: x%x x%x\n",
836                          ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
837                          ndlp->nlp_flag);
838         return ndlp->nlp_state;
839 }
840
841 static uint32_t
842 lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
843                   void *arg, uint32_t evt)
844 {
845         /* This transition is only legal if we previously
846          * rcv'ed a PLOGI. Since we don't want 2 discovery threads
847          * working on the same NPortID, do nothing for this thread
848          * to stop it.
849          */
850         if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
851                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
852                          "0272 Illegal State Transition: node x%x "
853                          "event x%x, state x%x Data: x%x x%x\n",
854                          ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
855                          ndlp->nlp_flag);
856         }
857         return ndlp->nlp_state;
858 }
859
860 /* Start of Discovery State Machine routines */
861
862 static uint32_t
863 lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
864                            void *arg, uint32_t evt)
865 {
866         struct lpfc_iocbq *cmdiocb;
867
868         cmdiocb = (struct lpfc_iocbq *) arg;
869
870         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
871                 return ndlp->nlp_state;
872         }
873         return NLP_STE_FREED_NODE;
874 }
875
876 static uint32_t
877 lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
878                          void *arg, uint32_t evt)
879 {
880         lpfc_issue_els_logo(vport, ndlp, 0);
881         return ndlp->nlp_state;
882 }
883
884 static uint32_t
885 lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
886                           void *arg, uint32_t evt)
887 {
888         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
889         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
890
891         spin_lock_irq(shost->host_lock);
892         ndlp->nlp_flag |= NLP_LOGO_ACC;
893         spin_unlock_irq(shost->host_lock);
894         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
895
896         return ndlp->nlp_state;
897 }
898
899 static uint32_t
900 lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
901                            void *arg, uint32_t evt)
902 {
903         return NLP_STE_FREED_NODE;
904 }
905
906 static uint32_t
907 lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
908                            void *arg, uint32_t evt)
909 {
910         return NLP_STE_FREED_NODE;
911 }
912
913 static uint32_t
914 lpfc_device_recov_unused_node(struct lpfc_vport *vport,
915                         struct lpfc_nodelist *ndlp,
916                            void *arg, uint32_t evt)
917 {
918         return ndlp->nlp_state;
919 }
920
921 static uint32_t
922 lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
923                            void *arg, uint32_t evt)
924 {
925         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
926         struct lpfc_hba   *phba = vport->phba;
927         struct lpfc_iocbq *cmdiocb = arg;
928         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
929         uint32_t *lp = (uint32_t *) pcmd->virt;
930         struct serv_parm *sp = (struct serv_parm *) (lp + 1);
931         struct ls_rjt stat;
932         int port_cmp;
933
934         memset(&stat, 0, sizeof (struct ls_rjt));
935
936         /* For a PLOGI, we only accept if our portname is less
937          * than the remote portname.
938          */
939         phba->fc_stat.elsLogiCol++;
940         port_cmp = memcmp(&vport->fc_portname, &sp->portName,
941                           sizeof(struct lpfc_name));
942
943         if (port_cmp >= 0) {
944                 /* Reject this request because the remote node will accept
945                    ours */
946                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
947                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
948                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
949                         NULL);
950         } else {
951                 if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
952                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
953                     (vport->num_disc_nodes)) {
954                         spin_lock_irq(shost->host_lock);
955                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
956                         spin_unlock_irq(shost->host_lock);
957                         /* Check if there are more PLOGIs to be sent */
958                         lpfc_more_plogi(vport);
959                         if (vport->num_disc_nodes == 0) {
960                                 spin_lock_irq(shost->host_lock);
961                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
962                                 spin_unlock_irq(shost->host_lock);
963                                 lpfc_can_disctmo(vport);
964                                 lpfc_end_rscn(vport);
965                         }
966                 }
967         } /* If our portname was less */
968
969         return ndlp->nlp_state;
970 }
971
972 static uint32_t
973 lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
974                           void *arg, uint32_t evt)
975 {
976         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
977         struct ls_rjt     stat;
978
979         memset(&stat, 0, sizeof (struct ls_rjt));
980         stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
981         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
982         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
983         return ndlp->nlp_state;
984 }
985
986 static uint32_t
987 lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
988                           void *arg, uint32_t evt)
989 {
990         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
991
992                                 /* software abort outstanding PLOGI */
993         lpfc_els_abort(vport->phba, ndlp);
994
995         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
996         return ndlp->nlp_state;
997 }
998
999 static uint32_t
1000 lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1001                          void *arg, uint32_t evt)
1002 {
1003         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1004         struct lpfc_hba   *phba = vport->phba;
1005         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1006
1007         /* software abort outstanding PLOGI */
1008         lpfc_els_abort(phba, ndlp);
1009
1010         if (evt == NLP_EVT_RCV_LOGO) {
1011                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1012         } else {
1013                 lpfc_issue_els_logo(vport, ndlp, 0);
1014         }
1015
1016         /* Put ndlp in npr state set plogi timer for 1 sec */
1017         mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000 * 1));
1018         spin_lock_irq(shost->host_lock);
1019         ndlp->nlp_flag |= NLP_DELAY_TMO;
1020         spin_unlock_irq(shost->host_lock);
1021         ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1022         ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1023         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1024
1025         return ndlp->nlp_state;
1026 }
1027
1028 static uint32_t
1029 lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
1030                             struct lpfc_nodelist *ndlp,
1031                             void *arg,
1032                             uint32_t evt)
1033 {
1034         struct lpfc_hba    *phba = vport->phba;
1035         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1036         struct lpfc_iocbq  *cmdiocb, *rspiocb;
1037         struct lpfc_dmabuf *pcmd, *prsp, *mp;
1038         uint32_t *lp;
1039         IOCB_t *irsp;
1040         struct serv_parm *sp;
1041         LPFC_MBOXQ_t *mbox;
1042
1043         cmdiocb = (struct lpfc_iocbq *) arg;
1044         rspiocb = cmdiocb->context_un.rsp_iocb;
1045
1046         if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1047                 /* Recovery from PLOGI collision logic */
1048                 return ndlp->nlp_state;
1049         }
1050
1051         irsp = &rspiocb->iocb;
1052
1053         if (irsp->ulpStatus)
1054                 goto out;
1055
1056         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
1057
1058         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1059         if (!prsp)
1060                 goto out;
1061
1062         lp = (uint32_t *) prsp->virt;
1063         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1064
1065         /* Some switches have FDMI servers returning 0 for WWN */
1066         if ((ndlp->nlp_DID != FDMI_DID) &&
1067                 (wwn_to_u64(sp->portName.u.wwn) == 0 ||
1068                 wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
1069                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1070                                  "0142 PLOGI RSP: Invalid WWN.\n");
1071                 goto out;
1072         }
1073         if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
1074                 goto out;
1075         /* PLOGI chkparm OK */
1076         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1077                          "0121 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
1078                          ndlp->nlp_DID, ndlp->nlp_state,
1079                          ndlp->nlp_flag, ndlp->nlp_rpi);
1080         if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
1081                 ndlp->nlp_fcp_info |= CLASS2;
1082         else
1083                 ndlp->nlp_fcp_info |= CLASS3;
1084
1085         ndlp->nlp_class_sup = 0;
1086         if (sp->cls1.classValid)
1087                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
1088         if (sp->cls2.classValid)
1089                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
1090         if (sp->cls3.classValid)
1091                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
1092         if (sp->cls4.classValid)
1093                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
1094         ndlp->nlp_maxframe =
1095                 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
1096
1097         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1098         if (!mbox) {
1099                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1100                         "0133 PLOGI: no memory for reg_login "
1101                         "Data: x%x x%x x%x x%x\n",
1102                         ndlp->nlp_DID, ndlp->nlp_state,
1103                         ndlp->nlp_flag, ndlp->nlp_rpi);
1104                 goto out;
1105         }
1106
1107         lpfc_unreg_rpi(vport, ndlp);
1108
1109         if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
1110                          (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1111                 switch (ndlp->nlp_DID) {
1112                 case NameServer_DID:
1113                         mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
1114                         break;
1115                 case FDMI_DID:
1116                         mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
1117                         break;
1118                 default:
1119                         ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1120                         mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
1121                 }
1122                 mbox->context2 = lpfc_nlp_get(ndlp);
1123                 mbox->vport = vport;
1124                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
1125                     != MBX_NOT_FINISHED) {
1126                         lpfc_nlp_set_state(vport, ndlp,
1127                                            NLP_STE_REG_LOGIN_ISSUE);
1128                         return ndlp->nlp_state;
1129                 }
1130                 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
1131                         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1132                 /* decrement node reference count to the failed mbox
1133                  * command
1134                  */
1135                 lpfc_nlp_put(ndlp);
1136                 mp = (struct lpfc_dmabuf *) mbox->context1;
1137                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1138                 kfree(mp);
1139                 mempool_free(mbox, phba->mbox_mem_pool);
1140
1141                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1142                                  "0134 PLOGI: cannot issue reg_login "
1143                                  "Data: x%x x%x x%x x%x\n",
1144                                  ndlp->nlp_DID, ndlp->nlp_state,
1145                                  ndlp->nlp_flag, ndlp->nlp_rpi);
1146         } else {
1147                 mempool_free(mbox, phba->mbox_mem_pool);
1148
1149                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1150                                  "0135 PLOGI: cannot format reg_login "
1151                                  "Data: x%x x%x x%x x%x\n",
1152                                  ndlp->nlp_DID, ndlp->nlp_state,
1153                                  ndlp->nlp_flag, ndlp->nlp_rpi);
1154         }
1155
1156
1157 out:
1158         if (ndlp->nlp_DID == NameServer_DID) {
1159                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1160                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1161                                  "0261 Cannot Register NameServer login\n");
1162         }
1163
1164         /*
1165         ** In case the node reference counter does not go to zero, ensure that
1166         ** the stale state for the node is not processed.
1167         */
1168
1169         ndlp->nlp_prev_state = ndlp->nlp_state;
1170         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1171         spin_lock_irq(shost->host_lock);
1172         ndlp->nlp_flag |= NLP_DEFER_RM;
1173         spin_unlock_irq(shost->host_lock);
1174         return NLP_STE_FREED_NODE;
1175 }
1176
1177 static uint32_t
1178 lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1179                            void *arg, uint32_t evt)
1180 {
1181         return ndlp->nlp_state;
1182 }
1183
1184 static uint32_t
1185 lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
1186         struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
1187 {
1188         struct lpfc_hba *phba;
1189         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1190         MAILBOX_t *mb = &pmb->u.mb;
1191         uint16_t rpi;
1192
1193         phba = vport->phba;
1194         /* Release the RPI */
1195         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1196                 !mb->mbxStatus) {
1197                 rpi = pmb->u.mb.un.varWords[0];
1198                 lpfc_release_rpi(phba, vport, rpi);
1199         }
1200         return ndlp->nlp_state;
1201 }
1202
1203 static uint32_t
1204 lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1205                            void *arg, uint32_t evt)
1206 {
1207         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1208
1209         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1210                 spin_lock_irq(shost->host_lock);
1211                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1212                 spin_unlock_irq(shost->host_lock);
1213                 return ndlp->nlp_state;
1214         } else {
1215                 /* software abort outstanding PLOGI */
1216                 lpfc_els_abort(vport->phba, ndlp);
1217
1218                 lpfc_drop_node(vport, ndlp);
1219                 return NLP_STE_FREED_NODE;
1220         }
1221 }
1222
1223 static uint32_t
1224 lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
1225                               struct lpfc_nodelist *ndlp,
1226                               void *arg,
1227                               uint32_t evt)
1228 {
1229         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1230         struct lpfc_hba  *phba = vport->phba;
1231
1232         /* Don't do anything that will mess up processing of the
1233          * previous RSCN.
1234          */
1235         if (vport->fc_flag & FC_RSCN_DEFERRED)
1236                 return ndlp->nlp_state;
1237
1238         /* software abort outstanding PLOGI */
1239         lpfc_els_abort(phba, ndlp);
1240
1241         ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1242         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1243         spin_lock_irq(shost->host_lock);
1244         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1245         spin_unlock_irq(shost->host_lock);
1246
1247         return ndlp->nlp_state;
1248 }
1249
1250 static uint32_t
1251 lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1252                            void *arg, uint32_t evt)
1253 {
1254         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1255         struct lpfc_hba   *phba = vport->phba;
1256         struct lpfc_iocbq *cmdiocb;
1257
1258         /* software abort outstanding ADISC */
1259         lpfc_els_abort(phba, ndlp);
1260
1261         cmdiocb = (struct lpfc_iocbq *) arg;
1262
1263         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1264                 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1265                         spin_lock_irq(shost->host_lock);
1266                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1267                         spin_unlock_irq(shost->host_lock);
1268                         if (vport->num_disc_nodes)
1269                                 lpfc_more_adisc(vport);
1270                 }
1271                 return ndlp->nlp_state;
1272         }
1273         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1274         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1275         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1276
1277         return ndlp->nlp_state;
1278 }
1279
1280 static uint32_t
1281 lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1282                           void *arg, uint32_t evt)
1283 {
1284         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1285
1286         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1287         return ndlp->nlp_state;
1288 }
1289
1290 static uint32_t
1291 lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1292                           void *arg, uint32_t evt)
1293 {
1294         struct lpfc_hba *phba = vport->phba;
1295         struct lpfc_iocbq *cmdiocb;
1296
1297         cmdiocb = (struct lpfc_iocbq *) arg;
1298
1299         /* software abort outstanding ADISC */
1300         lpfc_els_abort(phba, ndlp);
1301
1302         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1303         return ndlp->nlp_state;
1304 }
1305
1306 static uint32_t
1307 lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
1308                             struct lpfc_nodelist *ndlp,
1309                             void *arg, uint32_t evt)
1310 {
1311         struct lpfc_iocbq *cmdiocb;
1312
1313         cmdiocb = (struct lpfc_iocbq *) arg;
1314
1315         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1316         return ndlp->nlp_state;
1317 }
1318
1319 static uint32_t
1320 lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1321                           void *arg, uint32_t evt)
1322 {
1323         struct lpfc_iocbq *cmdiocb;
1324
1325         cmdiocb = (struct lpfc_iocbq *) arg;
1326
1327         /* Treat like rcv logo */
1328         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1329         return ndlp->nlp_state;
1330 }
1331
1332 static uint32_t
1333 lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
1334                             struct lpfc_nodelist *ndlp,
1335                             void *arg, uint32_t evt)
1336 {
1337         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1338         struct lpfc_hba   *phba = vport->phba;
1339         struct lpfc_iocbq *cmdiocb, *rspiocb;
1340         IOCB_t *irsp;
1341         ADISC *ap;
1342         int rc;
1343
1344         cmdiocb = (struct lpfc_iocbq *) arg;
1345         rspiocb = cmdiocb->context_un.rsp_iocb;
1346
1347         ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1348         irsp = &rspiocb->iocb;
1349
1350         if ((irsp->ulpStatus) ||
1351             (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
1352                 /* 1 sec timeout */
1353                 mod_timer(&ndlp->nlp_delayfunc,
1354                           jiffies + msecs_to_jiffies(1000));
1355                 spin_lock_irq(shost->host_lock);
1356                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1357                 spin_unlock_irq(shost->host_lock);
1358                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1359
1360                 memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
1361                 memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
1362
1363                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1364                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1365                 lpfc_unreg_rpi(vport, ndlp);
1366                 return ndlp->nlp_state;
1367         }
1368
1369         if (phba->sli_rev == LPFC_SLI_REV4) {
1370                 rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1371                 if (rc) {
1372                         /* Stay in state and retry. */
1373                         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1374                         return ndlp->nlp_state;
1375                 }
1376         }
1377
1378         if (ndlp->nlp_type & NLP_FCP_TARGET) {
1379                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1380                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1381         } else {
1382                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1383                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1384         }
1385
1386         return ndlp->nlp_state;
1387 }
1388
1389 static uint32_t
1390 lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1391                            void *arg, uint32_t evt)
1392 {
1393         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1394
1395         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1396                 spin_lock_irq(shost->host_lock);
1397                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1398                 spin_unlock_irq(shost->host_lock);
1399                 return ndlp->nlp_state;
1400         } else {
1401                 /* software abort outstanding ADISC */
1402                 lpfc_els_abort(vport->phba, ndlp);
1403
1404                 lpfc_drop_node(vport, ndlp);
1405                 return NLP_STE_FREED_NODE;
1406         }
1407 }
1408
1409 static uint32_t
1410 lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
1411                               struct lpfc_nodelist *ndlp,
1412                               void *arg,
1413                               uint32_t evt)
1414 {
1415         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1416         struct lpfc_hba  *phba = vport->phba;
1417
1418         /* Don't do anything that will mess up processing of the
1419          * previous RSCN.
1420          */
1421         if (vport->fc_flag & FC_RSCN_DEFERRED)
1422                 return ndlp->nlp_state;
1423
1424         /* software abort outstanding ADISC */
1425         lpfc_els_abort(phba, ndlp);
1426
1427         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1428         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1429         spin_lock_irq(shost->host_lock);
1430         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1431         spin_unlock_irq(shost->host_lock);
1432         lpfc_disc_set_adisc(vport, ndlp);
1433         return ndlp->nlp_state;
1434 }
1435
1436 static uint32_t
1437 lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
1438                               struct lpfc_nodelist *ndlp,
1439                               void *arg,
1440                               uint32_t evt)
1441 {
1442         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1443
1444         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1445         return ndlp->nlp_state;
1446 }
1447
1448 static uint32_t
1449 lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
1450                              struct lpfc_nodelist *ndlp,
1451                              void *arg,
1452                              uint32_t evt)
1453 {
1454         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1455
1456         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1457         return ndlp->nlp_state;
1458 }
1459
1460 static uint32_t
1461 lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
1462                              struct lpfc_nodelist *ndlp,
1463                              void *arg,
1464                              uint32_t evt)
1465 {
1466         struct lpfc_hba   *phba = vport->phba;
1467         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1468         LPFC_MBOXQ_t      *mb;
1469         LPFC_MBOXQ_t      *nextmb;
1470         struct lpfc_dmabuf *mp;
1471
1472         cmdiocb = (struct lpfc_iocbq *) arg;
1473
1474         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1475         if ((mb = phba->sli.mbox_active)) {
1476                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1477                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1478                         lpfc_nlp_put(ndlp);
1479                         mb->context2 = NULL;
1480                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1481                 }
1482         }
1483
1484         spin_lock_irq(&phba->hbalock);
1485         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1486                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1487                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1488                         mp = (struct lpfc_dmabuf *) (mb->context1);
1489                         if (mp) {
1490                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
1491                                 kfree(mp);
1492                         }
1493                         lpfc_nlp_put(ndlp);
1494                         list_del(&mb->list);
1495                         phba->sli.mboxq_cnt--;
1496                         mempool_free(mb, phba->mbox_mem_pool);
1497                 }
1498         }
1499         spin_unlock_irq(&phba->hbalock);
1500
1501         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1502         return ndlp->nlp_state;
1503 }
1504
1505 static uint32_t
1506 lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
1507                                struct lpfc_nodelist *ndlp,
1508                                void *arg,
1509                                uint32_t evt)
1510 {
1511         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1512
1513         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1514         return ndlp->nlp_state;
1515 }
1516
1517 static uint32_t
1518 lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
1519                              struct lpfc_nodelist *ndlp,
1520                              void *arg,
1521                              uint32_t evt)
1522 {
1523         struct lpfc_iocbq *cmdiocb;
1524
1525         cmdiocb = (struct lpfc_iocbq *) arg;
1526         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1527         return ndlp->nlp_state;
1528 }
1529
1530 static uint32_t
1531 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
1532                                   struct lpfc_nodelist *ndlp,
1533                                   void *arg,
1534                                   uint32_t evt)
1535 {
1536         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1537         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1538         MAILBOX_t *mb = &pmb->u.mb;
1539         uint32_t did  = mb->un.varWords[1];
1540
1541         if (mb->mbxStatus) {
1542                 /* RegLogin failed */
1543                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1544                                 "0246 RegLogin failed Data: x%x x%x x%x x%x "
1545                                  "x%x\n",
1546                                  did, mb->mbxStatus, vport->port_state,
1547                                  mb->un.varRegLogin.vpi,
1548                                  mb->un.varRegLogin.rpi);
1549                 /*
1550                  * If RegLogin failed due to lack of HBA resources do not
1551                  * retry discovery.
1552                  */
1553                 if (mb->mbxStatus == MBXERR_RPI_FULL) {
1554                         ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1555                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1556                         return ndlp->nlp_state;
1557                 }
1558
1559                 /* Put ndlp in npr state set plogi timer for 1 sec */
1560                 mod_timer(&ndlp->nlp_delayfunc,
1561                           jiffies + msecs_to_jiffies(1000 * 1));
1562                 spin_lock_irq(shost->host_lock);
1563                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1564                 spin_unlock_irq(shost->host_lock);
1565                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1566
1567                 lpfc_issue_els_logo(vport, ndlp, 0);
1568                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1569                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1570                 return ndlp->nlp_state;
1571         }
1572
1573         /* SLI4 ports have preallocated logical rpis. */
1574         if (vport->phba->sli_rev < LPFC_SLI_REV4)
1575                 ndlp->nlp_rpi = mb->un.varWords[0];
1576
1577         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1578
1579         /* Only if we are not a fabric nport do we issue PRLI */
1580         if (!(ndlp->nlp_type & NLP_FABRIC)) {
1581                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1582                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1583                 lpfc_issue_els_prli(vport, ndlp, 0);
1584         } else {
1585                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1586                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1587         }
1588         return ndlp->nlp_state;
1589 }
1590
1591 static uint32_t
1592 lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
1593                               struct lpfc_nodelist *ndlp,
1594                               void *arg,
1595                               uint32_t evt)
1596 {
1597         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1598
1599         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1600                 spin_lock_irq(shost->host_lock);
1601                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1602                 spin_unlock_irq(shost->host_lock);
1603                 return ndlp->nlp_state;
1604         } else {
1605                 lpfc_drop_node(vport, ndlp);
1606                 return NLP_STE_FREED_NODE;
1607         }
1608 }
1609
1610 static uint32_t
1611 lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
1612                                  struct lpfc_nodelist *ndlp,
1613                                  void *arg,
1614                                  uint32_t evt)
1615 {
1616         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1617
1618         /* Don't do anything that will mess up processing of the
1619          * previous RSCN.
1620          */
1621         if (vport->fc_flag & FC_RSCN_DEFERRED)
1622                 return ndlp->nlp_state;
1623
1624         ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1625         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1626         spin_lock_irq(shost->host_lock);
1627         ndlp->nlp_flag |= NLP_IGNR_REG_CMPL;
1628         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1629         spin_unlock_irq(shost->host_lock);
1630         lpfc_disc_set_adisc(vport, ndlp);
1631         return ndlp->nlp_state;
1632 }
1633
1634 static uint32_t
1635 lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1636                           void *arg, uint32_t evt)
1637 {
1638         struct lpfc_iocbq *cmdiocb;
1639
1640         cmdiocb = (struct lpfc_iocbq *) arg;
1641
1642         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1643         return ndlp->nlp_state;
1644 }
1645
1646 static uint32_t
1647 lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1648                          void *arg, uint32_t evt)
1649 {
1650         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1651
1652         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1653         return ndlp->nlp_state;
1654 }
1655
1656 static uint32_t
1657 lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1658                          void *arg, uint32_t evt)
1659 {
1660         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1661
1662         /* Software abort outstanding PRLI before sending acc */
1663         lpfc_els_abort(vport->phba, ndlp);
1664
1665         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1666         return ndlp->nlp_state;
1667 }
1668
1669 static uint32_t
1670 lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1671                            void *arg, uint32_t evt)
1672 {
1673         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1674
1675         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1676         return ndlp->nlp_state;
1677 }
1678
1679 /* This routine is envoked when we rcv a PRLO request from a nport
1680  * we are logged into.  We should send back a PRLO rsp setting the
1681  * appropriate bits.
1682  * NEXT STATE = PRLI_ISSUE
1683  */
1684 static uint32_t
1685 lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1686                          void *arg, uint32_t evt)
1687 {
1688         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1689
1690         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1691         return ndlp->nlp_state;
1692 }
1693
1694 static uint32_t
1695 lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1696                           void *arg, uint32_t evt)
1697 {
1698         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1699         struct lpfc_iocbq *cmdiocb, *rspiocb;
1700         struct lpfc_hba   *phba = vport->phba;
1701         IOCB_t *irsp;
1702         PRLI *npr;
1703
1704         cmdiocb = (struct lpfc_iocbq *) arg;
1705         rspiocb = cmdiocb->context_un.rsp_iocb;
1706         npr = (PRLI *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1707
1708         irsp = &rspiocb->iocb;
1709         if (irsp->ulpStatus) {
1710                 if ((vport->port_type == LPFC_NPIV_PORT) &&
1711                     vport->cfg_restrict_login) {
1712                         goto out;
1713                 }
1714                 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1715                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1716                 return ndlp->nlp_state;
1717         }
1718
1719         /* Check out PRLI rsp */
1720         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1721         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
1722         ndlp->nlp_flag &= ~NLP_FIRSTBURST;
1723         if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
1724             (npr->prliType == PRLI_FCP_TYPE)) {
1725                 if (npr->initiatorFunc)
1726                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
1727                 if (npr->targetFunc) {
1728                         ndlp->nlp_type |= NLP_FCP_TARGET;
1729                         if (npr->writeXferRdyDis)
1730                                 ndlp->nlp_flag |= NLP_FIRSTBURST;
1731                 }
1732                 if (npr->Retry)
1733                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1734         }
1735         if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
1736             (vport->port_type == LPFC_NPIV_PORT) &&
1737              vport->cfg_restrict_login) {
1738 out:
1739                 spin_lock_irq(shost->host_lock);
1740                 ndlp->nlp_flag |= NLP_TARGET_REMOVE;
1741                 spin_unlock_irq(shost->host_lock);
1742                 lpfc_issue_els_logo(vport, ndlp, 0);
1743
1744                 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1745                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1746                 return ndlp->nlp_state;
1747         }
1748
1749         ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1750         if (ndlp->nlp_type & NLP_FCP_TARGET)
1751                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1752         else
1753                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1754         return ndlp->nlp_state;
1755 }
1756
1757 /*! lpfc_device_rm_prli_issue
1758  *
1759  * \pre
1760  * \post
1761  * \param   phba
1762  * \param   ndlp
1763  * \param   arg
1764  * \param   evt
1765  * \return  uint32_t
1766  *
1767  * \b Description:
1768  *    This routine is envoked when we a request to remove a nport we are in the
1769  *    process of PRLIing. We should software abort outstanding prli, unreg
1770  *    login, send a logout. We will change node state to UNUSED_NODE, put it
1771  *    on plogi list so it can be freed when LOGO completes.
1772  *
1773  */
1774
1775 static uint32_t
1776 lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1777                           void *arg, uint32_t evt)
1778 {
1779         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1780
1781         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1782                 spin_lock_irq(shost->host_lock);
1783                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1784                 spin_unlock_irq(shost->host_lock);
1785                 return ndlp->nlp_state;
1786         } else {
1787                 /* software abort outstanding PLOGI */
1788                 lpfc_els_abort(vport->phba, ndlp);
1789
1790                 lpfc_drop_node(vport, ndlp);
1791                 return NLP_STE_FREED_NODE;
1792         }
1793 }
1794
1795
1796 /*! lpfc_device_recov_prli_issue
1797  *
1798  * \pre
1799  * \post
1800  * \param   phba
1801  * \param   ndlp
1802  * \param   arg
1803  * \param   evt
1804  * \return  uint32_t
1805  *
1806  * \b Description:
1807  *    The routine is envoked when the state of a device is unknown, like
1808  *    during a link down. We should remove the nodelist entry from the
1809  *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
1810  *    outstanding PRLI command, then free the node entry.
1811  */
1812 static uint32_t
1813 lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
1814                              struct lpfc_nodelist *ndlp,
1815                              void *arg,
1816                              uint32_t evt)
1817 {
1818         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1819         struct lpfc_hba  *phba = vport->phba;
1820
1821         /* Don't do anything that will mess up processing of the
1822          * previous RSCN.
1823          */
1824         if (vport->fc_flag & FC_RSCN_DEFERRED)
1825                 return ndlp->nlp_state;
1826
1827         /* software abort outstanding PRLI */
1828         lpfc_els_abort(phba, ndlp);
1829
1830         ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1831         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1832         spin_lock_irq(shost->host_lock);
1833         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1834         spin_unlock_irq(shost->host_lock);
1835         lpfc_disc_set_adisc(vport, ndlp);
1836         return ndlp->nlp_state;
1837 }
1838
1839 static uint32_t
1840 lpfc_rcv_plogi_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1841                           void *arg, uint32_t evt)
1842 {
1843         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1844         struct ls_rjt     stat;
1845
1846         memset(&stat, 0, sizeof(struct ls_rjt));
1847         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1848         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1849         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1850         return ndlp->nlp_state;
1851 }
1852
1853 static uint32_t
1854 lpfc_rcv_prli_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1855                          void *arg, uint32_t evt)
1856 {
1857         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1858         struct ls_rjt     stat;
1859
1860         memset(&stat, 0, sizeof(struct ls_rjt));
1861         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1862         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1863         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1864         return ndlp->nlp_state;
1865 }
1866
1867 static uint32_t
1868 lpfc_rcv_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1869                          void *arg, uint32_t evt)
1870 {
1871         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1872         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1873
1874         spin_lock_irq(shost->host_lock);
1875         ndlp->nlp_flag |= NLP_LOGO_ACC;
1876         spin_unlock_irq(shost->host_lock);
1877         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1878         return ndlp->nlp_state;
1879 }
1880
1881 static uint32_t
1882 lpfc_rcv_padisc_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1883                            void *arg, uint32_t evt)
1884 {
1885         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1886         struct ls_rjt     stat;
1887
1888         memset(&stat, 0, sizeof(struct ls_rjt));
1889         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1890         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1891         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1892         return ndlp->nlp_state;
1893 }
1894
1895 static uint32_t
1896 lpfc_rcv_prlo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1897                          void *arg, uint32_t evt)
1898 {
1899         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1900         struct ls_rjt     stat;
1901
1902         memset(&stat, 0, sizeof(struct ls_rjt));
1903         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1904         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1905         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1906         return ndlp->nlp_state;
1907 }
1908
1909 static uint32_t
1910 lpfc_cmpl_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1911                           void *arg, uint32_t evt)
1912 {
1913         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1914
1915         ndlp->nlp_prev_state = NLP_STE_LOGO_ISSUE;
1916         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1917         spin_lock_irq(shost->host_lock);
1918         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1919         spin_unlock_irq(shost->host_lock);
1920         lpfc_disc_set_adisc(vport, ndlp);
1921         return ndlp->nlp_state;
1922 }
1923
1924 static uint32_t
1925 lpfc_device_rm_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1926                           void *arg, uint32_t evt)
1927 {
1928         /*
1929          * Take no action.  If a LOGO is outstanding, then possibly DevLoss has
1930          * timed out and is calling for Device Remove.  In this case, the LOGO
1931          * must be allowed to complete in state LOGO_ISSUE so that the rpi
1932          * and other NLP flags are correctly cleaned up.
1933          */
1934         return ndlp->nlp_state;
1935 }
1936
1937 static uint32_t
1938 lpfc_device_recov_logo_issue(struct lpfc_vport *vport,
1939                              struct lpfc_nodelist *ndlp,
1940                              void *arg, uint32_t evt)
1941 {
1942         /*
1943          * Device Recovery events have no meaning for a node with a LOGO
1944          * outstanding.  The LOGO has to complete first and handle the
1945          * node from that point.
1946          */
1947         return ndlp->nlp_state;
1948 }
1949
1950 static uint32_t
1951 lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1952                           void *arg, uint32_t evt)
1953 {
1954         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1955
1956         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1957         return ndlp->nlp_state;
1958 }
1959
1960 static uint32_t
1961 lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1962                          void *arg, uint32_t evt)
1963 {
1964         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1965
1966         lpfc_rcv_prli(vport, ndlp, cmdiocb);
1967         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1968         return ndlp->nlp_state;
1969 }
1970
1971 static uint32_t
1972 lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1973                          void *arg, uint32_t evt)
1974 {
1975         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1976
1977         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1978         return ndlp->nlp_state;
1979 }
1980
1981 static uint32_t
1982 lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1983                            void *arg, uint32_t evt)
1984 {
1985         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1986
1987         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1988         return ndlp->nlp_state;
1989 }
1990
1991 static uint32_t
1992 lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1993                          void *arg, uint32_t evt)
1994 {
1995         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1996
1997         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1998         return ndlp->nlp_state;
1999 }
2000
2001 static uint32_t
2002 lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
2003                              struct lpfc_nodelist *ndlp,
2004                              void *arg,
2005                              uint32_t evt)
2006 {
2007         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2008
2009         ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
2010         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2011         spin_lock_irq(shost->host_lock);
2012         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2013         spin_unlock_irq(shost->host_lock);
2014         lpfc_disc_set_adisc(vport, ndlp);
2015
2016         return ndlp->nlp_state;
2017 }
2018
2019 static uint32_t
2020 lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2021                            void *arg, uint32_t evt)
2022 {
2023         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2024
2025         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2026         return ndlp->nlp_state;
2027 }
2028
2029 static uint32_t
2030 lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2031                           void *arg, uint32_t evt)
2032 {
2033         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2034
2035         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2036         return ndlp->nlp_state;
2037 }
2038
2039 static uint32_t
2040 lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2041                           void *arg, uint32_t evt)
2042 {
2043         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2044
2045         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2046         return ndlp->nlp_state;
2047 }
2048
2049 static uint32_t
2050 lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
2051                             struct lpfc_nodelist *ndlp,
2052                             void *arg, uint32_t evt)
2053 {
2054         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2055
2056         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2057         return ndlp->nlp_state;
2058 }
2059
2060 static uint32_t
2061 lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2062                           void *arg, uint32_t evt)
2063 {
2064         struct lpfc_hba  *phba = vport->phba;
2065         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2066
2067         /* flush the target */
2068         lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
2069                             ndlp->nlp_sid, 0, LPFC_CTX_TGT);
2070
2071         /* Treat like rcv logo */
2072         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2073         return ndlp->nlp_state;
2074 }
2075
2076 static uint32_t
2077 lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
2078                               struct lpfc_nodelist *ndlp,
2079                               void *arg,
2080                               uint32_t evt)
2081 {
2082         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2083
2084         ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
2085         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2086         spin_lock_irq(shost->host_lock);
2087         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2088         spin_unlock_irq(shost->host_lock);
2089         lpfc_disc_set_adisc(vport, ndlp);
2090         return ndlp->nlp_state;
2091 }
2092
2093 static uint32_t
2094 lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2095                         void *arg, uint32_t evt)
2096 {
2097         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2098         struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
2099
2100         /* Ignore PLOGI if we have an outstanding LOGO */
2101         if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2102                 return ndlp->nlp_state;
2103         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2104                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2105                 spin_lock_irq(shost->host_lock);
2106                 ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
2107                 spin_unlock_irq(shost->host_lock);
2108         } else if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2109                 /* send PLOGI immediately, move to PLOGI issue state */
2110                 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2111                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2112                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2113                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2114                 }
2115         }
2116         return ndlp->nlp_state;
2117 }
2118
2119 static uint32_t
2120 lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2121                        void *arg, uint32_t evt)
2122 {
2123         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2124         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2125         struct ls_rjt     stat;
2126
2127         memset(&stat, 0, sizeof (struct ls_rjt));
2128         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2129         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2130         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2131
2132         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2133                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2134                         spin_lock_irq(shost->host_lock);
2135                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2136                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2137                         spin_unlock_irq(shost->host_lock);
2138                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2139                         lpfc_issue_els_adisc(vport, ndlp, 0);
2140                 } else {
2141                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2142                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2143                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2144                 }
2145         }
2146         return ndlp->nlp_state;
2147 }
2148
2149 static uint32_t
2150 lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
2151                        void *arg, uint32_t evt)
2152 {
2153         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2154
2155         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2156         return ndlp->nlp_state;
2157 }
2158
2159 static uint32_t
2160 lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2161                          void *arg, uint32_t evt)
2162 {
2163         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2164
2165         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2166         /*
2167          * Do not start discovery if discovery is about to start
2168          * or discovery in progress for this node. Starting discovery
2169          * here will affect the counting of discovery threads.
2170          */
2171         if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2172             !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2173                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2174                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2175                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2176                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2177                         lpfc_issue_els_adisc(vport, ndlp, 0);
2178                 } else {
2179                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2180                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2181                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2182                 }
2183         }
2184         return ndlp->nlp_state;
2185 }
2186
2187 static uint32_t
2188 lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2189                        void *arg, uint32_t evt)
2190 {
2191         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2192         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2193
2194         spin_lock_irq(shost->host_lock);
2195         ndlp->nlp_flag |= NLP_LOGO_ACC;
2196         spin_unlock_irq(shost->host_lock);
2197
2198         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2199
2200         if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2201                 mod_timer(&ndlp->nlp_delayfunc,
2202                           jiffies + msecs_to_jiffies(1000 * 1));
2203                 spin_lock_irq(shost->host_lock);
2204                 ndlp->nlp_flag |= NLP_DELAY_TMO;
2205                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2206                 spin_unlock_irq(shost->host_lock);
2207                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2208         } else {
2209                 spin_lock_irq(shost->host_lock);
2210                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2211                 spin_unlock_irq(shost->host_lock);
2212         }
2213         return ndlp->nlp_state;
2214 }
2215
2216 static uint32_t
2217 lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2218                          void *arg, uint32_t evt)
2219 {
2220         struct lpfc_iocbq *cmdiocb, *rspiocb;
2221         IOCB_t *irsp;
2222         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2223
2224         cmdiocb = (struct lpfc_iocbq *) arg;
2225         rspiocb = cmdiocb->context_un.rsp_iocb;
2226
2227         irsp = &rspiocb->iocb;
2228         if (irsp->ulpStatus) {
2229                 spin_lock_irq(shost->host_lock);
2230                 ndlp->nlp_flag |= NLP_DEFER_RM;
2231                 spin_unlock_irq(shost->host_lock);
2232                 return NLP_STE_FREED_NODE;
2233         }
2234         return ndlp->nlp_state;
2235 }
2236
2237 static uint32_t
2238 lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2239                         void *arg, uint32_t evt)
2240 {
2241         struct lpfc_iocbq *cmdiocb, *rspiocb;
2242         IOCB_t *irsp;
2243
2244         cmdiocb = (struct lpfc_iocbq *) arg;
2245         rspiocb = cmdiocb->context_un.rsp_iocb;
2246
2247         irsp = &rspiocb->iocb;
2248         if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2249                 lpfc_drop_node(vport, ndlp);
2250                 return NLP_STE_FREED_NODE;
2251         }
2252         return ndlp->nlp_state;
2253 }
2254
2255 static uint32_t
2256 lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2257                         void *arg, uint32_t evt)
2258 {
2259         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2260
2261         /* For the fabric port just clear the fc flags. */
2262         if (ndlp->nlp_DID == Fabric_DID) {
2263                 spin_lock_irq(shost->host_lock);
2264                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2265                 spin_unlock_irq(shost->host_lock);
2266         }
2267         lpfc_unreg_rpi(vport, ndlp);
2268         return ndlp->nlp_state;
2269 }
2270
2271 static uint32_t
2272 lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2273                          void *arg, uint32_t evt)
2274 {
2275         struct lpfc_iocbq *cmdiocb, *rspiocb;
2276         IOCB_t *irsp;
2277
2278         cmdiocb = (struct lpfc_iocbq *) arg;
2279         rspiocb = cmdiocb->context_un.rsp_iocb;
2280
2281         irsp = &rspiocb->iocb;
2282         if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2283                 lpfc_drop_node(vport, ndlp);
2284                 return NLP_STE_FREED_NODE;
2285         }
2286         return ndlp->nlp_state;
2287 }
2288
2289 static uint32_t
2290 lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
2291                             struct lpfc_nodelist *ndlp,
2292                             void *arg, uint32_t evt)
2293 {
2294         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2295         MAILBOX_t    *mb = &pmb->u.mb;
2296
2297         if (!mb->mbxStatus) {
2298                 /* SLI4 ports have preallocated logical rpis. */
2299                 if (vport->phba->sli_rev < LPFC_SLI_REV4)
2300                         ndlp->nlp_rpi = mb->un.varWords[0];
2301                 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2302         } else {
2303                 if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
2304                         lpfc_drop_node(vport, ndlp);
2305                         return NLP_STE_FREED_NODE;
2306                 }
2307         }
2308         return ndlp->nlp_state;
2309 }
2310
2311 static uint32_t
2312 lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2313                         void *arg, uint32_t evt)
2314 {
2315         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2316
2317         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2318                 spin_lock_irq(shost->host_lock);
2319                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2320                 spin_unlock_irq(shost->host_lock);
2321                 return ndlp->nlp_state;
2322         }
2323         lpfc_drop_node(vport, ndlp);
2324         return NLP_STE_FREED_NODE;
2325 }
2326
2327 static uint32_t
2328 lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2329                            void *arg, uint32_t evt)
2330 {
2331         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2332
2333         /* Don't do anything that will mess up processing of the
2334          * previous RSCN.
2335          */
2336         if (vport->fc_flag & FC_RSCN_DEFERRED)
2337                 return ndlp->nlp_state;
2338
2339         lpfc_cancel_retry_delay_tmo(vport, ndlp);
2340         spin_lock_irq(shost->host_lock);
2341         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2342         spin_unlock_irq(shost->host_lock);
2343         return ndlp->nlp_state;
2344 }
2345
2346
2347 /* This next section defines the NPort Discovery State Machine */
2348
2349 /* There are 4 different double linked lists nodelist entries can reside on.
2350  * The plogi list and adisc list are used when Link Up discovery or RSCN
2351  * processing is needed. Each list holds the nodes that we will send PLOGI
2352  * or ADISC on. These lists will keep track of what nodes will be effected
2353  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
2354  * The unmapped_list will contain all nodes that we have successfully logged
2355  * into at the Fibre Channel level. The mapped_list will contain all nodes
2356  * that are mapped FCP targets.
2357  */
2358 /*
2359  * The bind list is a list of undiscovered (potentially non-existent) nodes
2360  * that we have saved binding information on. This information is used when
2361  * nodes transition from the unmapped to the mapped list.
2362  */
2363 /* For UNUSED_NODE state, the node has just been allocated .
2364  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
2365  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
2366  * and put on the unmapped list. For ADISC processing, the node is taken off
2367  * the ADISC list and placed on either the mapped or unmapped list (depending
2368  * on its previous state). Once on the unmapped list, a PRLI is issued and the
2369  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
2370  * changed to UNMAPPED_NODE. If the completion indicates a mapped
2371  * node, the node is taken off the unmapped list. The binding list is checked
2372  * for a valid binding, or a binding is automatically assigned. If binding
2373  * assignment is unsuccessful, the node is left on the unmapped list. If
2374  * binding assignment is successful, the associated binding list entry (if
2375  * any) is removed, and the node is placed on the mapped list.
2376  */
2377 /*
2378  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
2379  * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
2380  * expire, all effected nodes will receive a DEVICE_RM event.
2381  */
2382 /*
2383  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
2384  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
2385  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
2386  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
2387  * we will first process the ADISC list.  32 entries are processed initially and
2388  * ADISC is initited for each one.  Completions / Events for each node are
2389  * funnelled thru the state machine.  As each node finishes ADISC processing, it
2390  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
2391  * waiting, and the ADISC list count is identically 0, then we are done. For
2392  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
2393  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
2394  * list.  32 entries are processed initially and PLOGI is initited for each one.
2395  * Completions / Events for each node are funnelled thru the state machine.  As
2396  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
2397  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
2398  * indentically 0, then we are done. We have now completed discovery / RSCN
2399  * handling. Upon completion, ALL nodes should be on either the mapped or
2400  * unmapped lists.
2401  */
2402
2403 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
2404      (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
2405         /* Action routine                  Event       Current State  */
2406         lpfc_rcv_plogi_unused_node,     /* RCV_PLOGI   UNUSED_NODE    */
2407         lpfc_rcv_els_unused_node,       /* RCV_PRLI        */
2408         lpfc_rcv_logo_unused_node,      /* RCV_LOGO        */
2409         lpfc_rcv_els_unused_node,       /* RCV_ADISC       */
2410         lpfc_rcv_els_unused_node,       /* RCV_PDISC       */
2411         lpfc_rcv_els_unused_node,       /* RCV_PRLO        */
2412         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2413         lpfc_disc_illegal,              /* CMPL_PRLI       */
2414         lpfc_cmpl_logo_unused_node,     /* CMPL_LOGO       */
2415         lpfc_disc_illegal,              /* CMPL_ADISC      */
2416         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2417         lpfc_device_rm_unused_node,     /* DEVICE_RM       */
2418         lpfc_device_recov_unused_node,  /* DEVICE_RECOVERY */
2419
2420         lpfc_rcv_plogi_plogi_issue,     /* RCV_PLOGI   PLOGI_ISSUE    */
2421         lpfc_rcv_prli_plogi_issue,      /* RCV_PRLI        */
2422         lpfc_rcv_logo_plogi_issue,      /* RCV_LOGO        */
2423         lpfc_rcv_els_plogi_issue,       /* RCV_ADISC       */
2424         lpfc_rcv_els_plogi_issue,       /* RCV_PDISC       */
2425         lpfc_rcv_els_plogi_issue,       /* RCV_PRLO        */
2426         lpfc_cmpl_plogi_plogi_issue,    /* CMPL_PLOGI      */
2427         lpfc_disc_illegal,              /* CMPL_PRLI       */
2428         lpfc_cmpl_logo_plogi_issue,     /* CMPL_LOGO       */
2429         lpfc_disc_illegal,              /* CMPL_ADISC      */
2430         lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
2431         lpfc_device_rm_plogi_issue,     /* DEVICE_RM       */
2432         lpfc_device_recov_plogi_issue,  /* DEVICE_RECOVERY */
2433
2434         lpfc_rcv_plogi_adisc_issue,     /* RCV_PLOGI   ADISC_ISSUE    */
2435         lpfc_rcv_prli_adisc_issue,      /* RCV_PRLI        */
2436         lpfc_rcv_logo_adisc_issue,      /* RCV_LOGO        */
2437         lpfc_rcv_padisc_adisc_issue,    /* RCV_ADISC       */
2438         lpfc_rcv_padisc_adisc_issue,    /* RCV_PDISC       */
2439         lpfc_rcv_prlo_adisc_issue,      /* RCV_PRLO        */
2440         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2441         lpfc_disc_illegal,              /* CMPL_PRLI       */
2442         lpfc_disc_illegal,              /* CMPL_LOGO       */
2443         lpfc_cmpl_adisc_adisc_issue,    /* CMPL_ADISC      */
2444         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2445         lpfc_device_rm_adisc_issue,     /* DEVICE_RM       */
2446         lpfc_device_recov_adisc_issue,  /* DEVICE_RECOVERY */
2447
2448         lpfc_rcv_plogi_reglogin_issue,  /* RCV_PLOGI  REG_LOGIN_ISSUE */
2449         lpfc_rcv_prli_reglogin_issue,   /* RCV_PLOGI       */
2450         lpfc_rcv_logo_reglogin_issue,   /* RCV_LOGO        */
2451         lpfc_rcv_padisc_reglogin_issue, /* RCV_ADISC       */
2452         lpfc_rcv_padisc_reglogin_issue, /* RCV_PDISC       */
2453         lpfc_rcv_prlo_reglogin_issue,   /* RCV_PRLO        */
2454         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2455         lpfc_disc_illegal,              /* CMPL_PRLI       */
2456         lpfc_disc_illegal,              /* CMPL_LOGO       */
2457         lpfc_disc_illegal,              /* CMPL_ADISC      */
2458         lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
2459         lpfc_device_rm_reglogin_issue,  /* DEVICE_RM       */
2460         lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
2461
2462         lpfc_rcv_plogi_prli_issue,      /* RCV_PLOGI   PRLI_ISSUE     */
2463         lpfc_rcv_prli_prli_issue,       /* RCV_PRLI        */
2464         lpfc_rcv_logo_prli_issue,       /* RCV_LOGO        */
2465         lpfc_rcv_padisc_prli_issue,     /* RCV_ADISC       */
2466         lpfc_rcv_padisc_prli_issue,     /* RCV_PDISC       */
2467         lpfc_rcv_prlo_prli_issue,       /* RCV_PRLO        */
2468         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2469         lpfc_cmpl_prli_prli_issue,      /* CMPL_PRLI       */
2470         lpfc_disc_illegal,              /* CMPL_LOGO       */
2471         lpfc_disc_illegal,              /* CMPL_ADISC      */
2472         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2473         lpfc_device_rm_prli_issue,      /* DEVICE_RM       */
2474         lpfc_device_recov_prli_issue,   /* DEVICE_RECOVERY */
2475
2476         lpfc_rcv_plogi_logo_issue,      /* RCV_PLOGI   LOGO_ISSUE     */
2477         lpfc_rcv_prli_logo_issue,       /* RCV_PRLI        */
2478         lpfc_rcv_logo_logo_issue,       /* RCV_LOGO        */
2479         lpfc_rcv_padisc_logo_issue,     /* RCV_ADISC       */
2480         lpfc_rcv_padisc_logo_issue,     /* RCV_PDISC       */
2481         lpfc_rcv_prlo_logo_issue,       /* RCV_PRLO        */
2482         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2483         lpfc_disc_illegal,              /* CMPL_PRLI       */
2484         lpfc_cmpl_logo_logo_issue,      /* CMPL_LOGO       */
2485         lpfc_disc_illegal,              /* CMPL_ADISC      */
2486         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2487         lpfc_device_rm_logo_issue,      /* DEVICE_RM       */
2488         lpfc_device_recov_logo_issue,   /* DEVICE_RECOVERY */
2489
2490         lpfc_rcv_plogi_unmap_node,      /* RCV_PLOGI   UNMAPPED_NODE  */
2491         lpfc_rcv_prli_unmap_node,       /* RCV_PRLI        */
2492         lpfc_rcv_logo_unmap_node,       /* RCV_LOGO        */
2493         lpfc_rcv_padisc_unmap_node,     /* RCV_ADISC       */
2494         lpfc_rcv_padisc_unmap_node,     /* RCV_PDISC       */
2495         lpfc_rcv_prlo_unmap_node,       /* RCV_PRLO        */
2496         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2497         lpfc_disc_illegal,              /* CMPL_PRLI       */
2498         lpfc_disc_illegal,              /* CMPL_LOGO       */
2499         lpfc_disc_illegal,              /* CMPL_ADISC      */
2500         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2501         lpfc_disc_illegal,              /* DEVICE_RM       */
2502         lpfc_device_recov_unmap_node,   /* DEVICE_RECOVERY */
2503
2504         lpfc_rcv_plogi_mapped_node,     /* RCV_PLOGI   MAPPED_NODE    */
2505         lpfc_rcv_prli_mapped_node,      /* RCV_PRLI        */
2506         lpfc_rcv_logo_mapped_node,      /* RCV_LOGO        */
2507         lpfc_rcv_padisc_mapped_node,    /* RCV_ADISC       */
2508         lpfc_rcv_padisc_mapped_node,    /* RCV_PDISC       */
2509         lpfc_rcv_prlo_mapped_node,      /* RCV_PRLO        */
2510         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2511         lpfc_disc_illegal,              /* CMPL_PRLI       */
2512         lpfc_disc_illegal,              /* CMPL_LOGO       */
2513         lpfc_disc_illegal,              /* CMPL_ADISC      */
2514         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2515         lpfc_disc_illegal,              /* DEVICE_RM       */
2516         lpfc_device_recov_mapped_node,  /* DEVICE_RECOVERY */
2517
2518         lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
2519         lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
2520         lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
2521         lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
2522         lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
2523         lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
2524         lpfc_cmpl_plogi_npr_node,       /* CMPL_PLOGI      */
2525         lpfc_cmpl_prli_npr_node,        /* CMPL_PRLI       */
2526         lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
2527         lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
2528         lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
2529         lpfc_device_rm_npr_node,        /* DEVICE_RM       */
2530         lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
2531 };
2532
2533 int
2534 lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2535                         void *arg, uint32_t evt)
2536 {
2537         uint32_t cur_state, rc;
2538         uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
2539                          uint32_t);
2540         uint32_t got_ndlp = 0;
2541
2542         if (lpfc_nlp_get(ndlp))
2543                 got_ndlp = 1;
2544
2545         cur_state = ndlp->nlp_state;
2546
2547         /* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
2548         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2549                          "0211 DSM in event x%x on NPort x%x in "
2550                          "state %d Data: x%x\n",
2551                          evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
2552
2553         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2554                  "DSM in:          evt:%d ste:%d did:x%x",
2555                 evt, cur_state, ndlp->nlp_DID);
2556
2557         func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
2558         rc = (func) (vport, ndlp, arg, evt);
2559
2560         /* DSM out state <rc> on NPort <nlp_DID> */
2561         if (got_ndlp) {
2562                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2563                          "0212 DSM out state %d on NPort x%x Data: x%x\n",
2564                          rc, ndlp->nlp_DID, ndlp->nlp_flag);
2565
2566                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2567                         "DSM out:         ste:%d did:x%x flg:x%x",
2568                         rc, ndlp->nlp_DID, ndlp->nlp_flag);
2569                 /* Decrement the ndlp reference count held for this function */
2570                 lpfc_nlp_put(ndlp);
2571         } else {
2572                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2573                         "0213 DSM out state %d on NPort free\n", rc);
2574
2575                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2576                         "DSM out:         ste:%d did:x%x flg:x%x",
2577                         rc, 0, 0);
2578         }
2579
2580         return rc;
2581 }