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