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