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