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