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