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