Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[sfrench/cifs-2.6.git] / drivers / scsi / lpfc / lpfc_els.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2008 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 /* See Fibre Channel protocol T11 FC-LS for details */
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
30
31 #include "lpfc_hw.h"
32 #include "lpfc_sli.h"
33 #include "lpfc_nl.h"
34 #include "lpfc_disc.h"
35 #include "lpfc_scsi.h"
36 #include "lpfc.h"
37 #include "lpfc_logmsg.h"
38 #include "lpfc_crtn.h"
39 #include "lpfc_vport.h"
40 #include "lpfc_debugfs.h"
41
42 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
43                           struct lpfc_iocbq *);
44 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
45                         struct lpfc_iocbq *);
46 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
47 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
48                                 struct lpfc_nodelist *ndlp, uint8_t retry);
49 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
50                                   struct lpfc_iocbq *iocb);
51 static void lpfc_register_new_vport(struct lpfc_hba *phba,
52                                     struct lpfc_vport *vport,
53                                     struct lpfc_nodelist *ndlp);
54
55 static int lpfc_max_els_tries = 3;
56
57 /**
58  * lpfc_els_chk_latt - Check host link attention event for a vport
59  * @vport: pointer to a host virtual N_Port data structure.
60  *
61  * This routine checks whether there is an outstanding host link
62  * attention event during the discovery process with the @vport. It is done
63  * by reading the HBA's Host Attention (HA) register. If there is any host
64  * link attention events during this @vport's discovery process, the @vport
65  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
66  * be issued if the link state is not already in host link cleared state,
67  * and a return code shall indicate whether the host link attention event
68  * had happened.
69  *
70  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
71  * state in LPFC_VPORT_READY, the request for checking host link attention
72  * event will be ignored and a return code shall indicate no host link
73  * attention event had happened.
74  *
75  * Return codes
76  *   0 - no host link attention event happened
77  *   1 - host link attention event happened
78  **/
79 int
80 lpfc_els_chk_latt(struct lpfc_vport *vport)
81 {
82         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
83         struct lpfc_hba  *phba = vport->phba;
84         uint32_t ha_copy;
85
86         if (vport->port_state >= LPFC_VPORT_READY ||
87             phba->link_state == LPFC_LINK_DOWN)
88                 return 0;
89
90         /* Read the HBA Host Attention Register */
91         ha_copy = readl(phba->HAregaddr);
92
93         if (!(ha_copy & HA_LATT))
94                 return 0;
95
96         /* Pending Link Event during Discovery */
97         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
98                          "0237 Pending Link Event during "
99                          "Discovery: State x%x\n",
100                          phba->pport->port_state);
101
102         /* CLEAR_LA should re-enable link attention events and
103          * we should then imediately take a LATT event. The
104          * LATT processing should call lpfc_linkdown() which
105          * will cleanup any left over in-progress discovery
106          * events.
107          */
108         spin_lock_irq(shost->host_lock);
109         vport->fc_flag |= FC_ABORT_DISCOVERY;
110         spin_unlock_irq(shost->host_lock);
111
112         if (phba->link_state != LPFC_CLEAR_LA)
113                 lpfc_issue_clear_la(phba, vport);
114
115         return 1;
116 }
117
118 /**
119  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
120  * @vport: pointer to a host virtual N_Port data structure.
121  * @expectRsp: flag indicating whether response is expected.
122  * @cmdSize: size of the ELS command.
123  * @retry: number of retries to the command IOCB when it fails.
124  * @ndlp: pointer to a node-list data structure.
125  * @did: destination identifier.
126  * @elscmd: the ELS command code.
127  *
128  * This routine is used for allocating a lpfc-IOCB data structure from
129  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
130  * passed into the routine for discovery state machine to issue an Extended
131  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
132  * and preparation routine that is used by all the discovery state machine
133  * routines and the ELS command-specific fields will be later set up by
134  * the individual discovery machine routines after calling this routine
135  * allocating and preparing a generic IOCB data structure. It fills in the
136  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
137  * payload and response payload (if expected). The reference count on the
138  * ndlp is incremented by 1 and the reference to the ndlp is put into
139  * context1 of the IOCB data structure for this IOCB to hold the ndlp
140  * reference for the command's callback function to access later.
141  *
142  * Return code
143  *   Pointer to the newly allocated/prepared els iocb data structure
144  *   NULL - when els iocb data structure allocation/preparation failed
145  **/
146 static struct lpfc_iocbq *
147 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
148                    uint16_t cmdSize, uint8_t retry,
149                    struct lpfc_nodelist *ndlp, uint32_t did,
150                    uint32_t elscmd)
151 {
152         struct lpfc_hba  *phba = vport->phba;
153         struct lpfc_iocbq *elsiocb;
154         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
155         struct ulp_bde64 *bpl;
156         IOCB_t *icmd;
157
158
159         if (!lpfc_is_link_up(phba))
160                 return NULL;
161
162         /* Allocate buffer for  command iocb */
163         elsiocb = lpfc_sli_get_iocbq(phba);
164
165         if (elsiocb == NULL)
166                 return NULL;
167
168         icmd = &elsiocb->iocb;
169
170         /* fill in BDEs for command */
171         /* Allocate buffer for command payload */
172         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
173         if (pcmd)
174                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
175         if (!pcmd || !pcmd->virt)
176                 goto els_iocb_free_pcmb_exit;
177
178         INIT_LIST_HEAD(&pcmd->list);
179
180         /* Allocate buffer for response payload */
181         if (expectRsp) {
182                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
183                 if (prsp)
184                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
185                                                      &prsp->phys);
186                 if (!prsp || !prsp->virt)
187                         goto els_iocb_free_prsp_exit;
188                 INIT_LIST_HEAD(&prsp->list);
189         } else
190                 prsp = NULL;
191
192         /* Allocate buffer for Buffer ptr list */
193         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
194         if (pbuflist)
195                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
196                                                  &pbuflist->phys);
197         if (!pbuflist || !pbuflist->virt)
198                 goto els_iocb_free_pbuf_exit;
199
200         INIT_LIST_HEAD(&pbuflist->list);
201
202         icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
203         icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
204         icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
205         icmd->un.elsreq64.remoteID = did;       /* DID */
206         if (expectRsp) {
207                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
208                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
209                 icmd->ulpTimeout = phba->fc_ratov * 2;
210         } else {
211                 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
212                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
213         }
214         icmd->ulpBdeCount = 1;
215         icmd->ulpLe = 1;
216         icmd->ulpClass = CLASS3;
217
218         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
219                 icmd->un.elsreq64.myID = vport->fc_myDID;
220
221                 /* For ELS_REQUEST64_CR, use the VPI by default */
222                 icmd->ulpContext = vport->vpi;
223                 icmd->ulpCt_h = 0;
224                 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
225                 if (elscmd == ELS_CMD_ECHO)
226                         icmd->ulpCt_l = 0; /* context = invalid RPI */
227                 else
228                         icmd->ulpCt_l = 1; /* context = VPI */
229         }
230
231         bpl = (struct ulp_bde64 *) pbuflist->virt;
232         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
233         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
234         bpl->tus.f.bdeSize = cmdSize;
235         bpl->tus.f.bdeFlags = 0;
236         bpl->tus.w = le32_to_cpu(bpl->tus.w);
237
238         if (expectRsp) {
239                 bpl++;
240                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
241                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
242                 bpl->tus.f.bdeSize = FCELSSIZE;
243                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
244                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
245         }
246
247         /* prevent preparing iocb with NULL ndlp reference */
248         elsiocb->context1 = lpfc_nlp_get(ndlp);
249         if (!elsiocb->context1)
250                 goto els_iocb_free_pbuf_exit;
251         elsiocb->context2 = pcmd;
252         elsiocb->context3 = pbuflist;
253         elsiocb->retry = retry;
254         elsiocb->vport = vport;
255         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
256
257         if (prsp) {
258                 list_add(&prsp->list, &pcmd->list);
259         }
260         if (expectRsp) {
261                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
262                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
263                                  "0116 Xmit ELS command x%x to remote "
264                                  "NPORT x%x I/O tag: x%x, port state: x%x\n",
265                                  elscmd, did, elsiocb->iotag,
266                                  vport->port_state);
267         } else {
268                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
269                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
270                                  "0117 Xmit ELS response x%x to remote "
271                                  "NPORT x%x I/O tag: x%x, size: x%x\n",
272                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
273                                  cmdSize);
274         }
275         return elsiocb;
276
277 els_iocb_free_pbuf_exit:
278         if (expectRsp)
279                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
280         kfree(pbuflist);
281
282 els_iocb_free_prsp_exit:
283         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
284         kfree(prsp);
285
286 els_iocb_free_pcmb_exit:
287         kfree(pcmd);
288         lpfc_sli_release_iocbq(phba, elsiocb);
289         return NULL;
290 }
291
292 /**
293  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
294  * @vport: pointer to a host virtual N_Port data structure.
295  *
296  * This routine issues a fabric registration login for a @vport. An
297  * active ndlp node with Fabric_DID must already exist for this @vport.
298  * The routine invokes two mailbox commands to carry out fabric registration
299  * login through the HBA firmware: the first mailbox command requests the
300  * HBA to perform link configuration for the @vport; and the second mailbox
301  * command requests the HBA to perform the actual fabric registration login
302  * with the @vport.
303  *
304  * Return code
305  *   0 - successfully issued fabric registration login for @vport
306  *   -ENXIO -- failed to issue fabric registration login for @vport
307  **/
308 static int
309 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
310 {
311         struct lpfc_hba  *phba = vport->phba;
312         LPFC_MBOXQ_t *mbox;
313         struct lpfc_dmabuf *mp;
314         struct lpfc_nodelist *ndlp;
315         struct serv_parm *sp;
316         int rc;
317         int err = 0;
318
319         sp = &phba->fc_fabparam;
320         ndlp = lpfc_findnode_did(vport, Fabric_DID);
321         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
322                 err = 1;
323                 goto fail;
324         }
325
326         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
327         if (!mbox) {
328                 err = 2;
329                 goto fail;
330         }
331
332         vport->port_state = LPFC_FABRIC_CFG_LINK;
333         lpfc_config_link(phba, mbox);
334         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
335         mbox->vport = vport;
336
337         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
338         if (rc == MBX_NOT_FINISHED) {
339                 err = 3;
340                 goto fail_free_mbox;
341         }
342
343         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
344         if (!mbox) {
345                 err = 4;
346                 goto fail;
347         }
348         rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
349                             0);
350         if (rc) {
351                 err = 5;
352                 goto fail_free_mbox;
353         }
354
355         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
356         mbox->vport = vport;
357         /* increment the reference count on ndlp to hold reference
358          * for the callback routine.
359          */
360         mbox->context2 = lpfc_nlp_get(ndlp);
361
362         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
363         if (rc == MBX_NOT_FINISHED) {
364                 err = 6;
365                 goto fail_issue_reg_login;
366         }
367
368         return 0;
369
370 fail_issue_reg_login:
371         /* decrement the reference count on ndlp just incremented
372          * for the failed mbox command.
373          */
374         lpfc_nlp_put(ndlp);
375         mp = (struct lpfc_dmabuf *) mbox->context1;
376         lpfc_mbuf_free(phba, mp->virt, mp->phys);
377         kfree(mp);
378 fail_free_mbox:
379         mempool_free(mbox, phba->mbox_mem_pool);
380
381 fail:
382         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
383         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
384                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
385         return -ENXIO;
386 }
387
388 /**
389  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
390  * @vport: pointer to a host virtual N_Port data structure.
391  * @ndlp: pointer to a node-list data structure.
392  * @sp: pointer to service parameter data structure.
393  * @irsp: pointer to the IOCB within the lpfc response IOCB.
394  *
395  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
396  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
397  * port in a fabric topology. It properly sets up the parameters to the @ndlp
398  * from the IOCB response. It also check the newly assigned N_Port ID to the
399  * @vport against the previously assigned N_Port ID. If it is different from
400  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
401  * is invoked on all the remaining nodes with the @vport to unregister the
402  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
403  * is invoked to register login to the fabric.
404  *
405  * Return code
406  *   0 - Success (currently, always return 0)
407  **/
408 static int
409 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
410                            struct serv_parm *sp, IOCB_t *irsp)
411 {
412         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
413         struct lpfc_hba  *phba = vport->phba;
414         struct lpfc_nodelist *np;
415         struct lpfc_nodelist *next_np;
416
417         spin_lock_irq(shost->host_lock);
418         vport->fc_flag |= FC_FABRIC;
419         spin_unlock_irq(shost->host_lock);
420
421         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
422         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
423                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
424
425         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
426
427         if (phba->fc_topology == TOPOLOGY_LOOP) {
428                 spin_lock_irq(shost->host_lock);
429                 vport->fc_flag |= FC_PUBLIC_LOOP;
430                 spin_unlock_irq(shost->host_lock);
431         } else {
432                 /*
433                  * If we are a N-port connected to a Fabric, fixup sparam's so
434                  * logins to devices on remote loops work.
435                  */
436                 vport->fc_sparam.cmn.altBbCredit = 1;
437         }
438
439         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
440         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
441         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
442         ndlp->nlp_class_sup = 0;
443         if (sp->cls1.classValid)
444                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
445         if (sp->cls2.classValid)
446                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
447         if (sp->cls3.classValid)
448                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
449         if (sp->cls4.classValid)
450                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
451         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
452                                 sp->cmn.bbRcvSizeLsb;
453         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
454
455         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
456                 if (sp->cmn.response_multiple_NPort) {
457                         lpfc_printf_vlog(vport, KERN_WARNING,
458                                          LOG_ELS | LOG_VPORT,
459                                          "1816 FLOGI NPIV supported, "
460                                          "response data 0x%x\n",
461                                          sp->cmn.response_multiple_NPort);
462                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
463                 } else {
464                         /* Because we asked f/w for NPIV it still expects us
465                         to call reg_vnpid atleast for the physcial host */
466                         lpfc_printf_vlog(vport, KERN_WARNING,
467                                          LOG_ELS | LOG_VPORT,
468                                          "1817 Fabric does not support NPIV "
469                                          "- configuring single port mode.\n");
470                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
471                 }
472         }
473
474         if ((vport->fc_prevDID != vport->fc_myDID) &&
475                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
476
477                 /* If our NportID changed, we need to ensure all
478                  * remaining NPORTs get unreg_login'ed.
479                  */
480                 list_for_each_entry_safe(np, next_np,
481                                         &vport->fc_nodes, nlp_listp) {
482                         if (!NLP_CHK_NODE_ACT(np))
483                                 continue;
484                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
485                                    !(np->nlp_flag & NLP_NPR_ADISC))
486                                 continue;
487                         spin_lock_irq(shost->host_lock);
488                         np->nlp_flag &= ~NLP_NPR_ADISC;
489                         spin_unlock_irq(shost->host_lock);
490                         lpfc_unreg_rpi(vport, np);
491                 }
492                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
493                         lpfc_mbx_unreg_vpi(vport);
494                         spin_lock_irq(shost->host_lock);
495                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
496                         spin_unlock_irq(shost->host_lock);
497                 }
498         }
499
500         lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
501
502         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
503             vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
504                 lpfc_register_new_vport(phba, vport, ndlp);
505                 return 0;
506         }
507         lpfc_issue_fabric_reglogin(vport);
508         return 0;
509 }
510
511 /**
512  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
513  * @vport: pointer to a host virtual N_Port data structure.
514  * @ndlp: pointer to a node-list data structure.
515  * @sp: pointer to service parameter data structure.
516  *
517  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
518  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
519  * in a point-to-point topology. First, the @vport's N_Port Name is compared
520  * with the received N_Port Name: if the @vport's N_Port Name is greater than
521  * the received N_Port Name lexicographically, this node shall assign local
522  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
523  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
524  * this node shall just wait for the remote node to issue PLOGI and assign
525  * N_Port IDs.
526  *
527  * Return code
528  *   0 - Success
529  *   -ENXIO - Fail
530  **/
531 static int
532 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
533                           struct serv_parm *sp)
534 {
535         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
536         struct lpfc_hba  *phba = vport->phba;
537         LPFC_MBOXQ_t *mbox;
538         int rc;
539
540         spin_lock_irq(shost->host_lock);
541         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
542         spin_unlock_irq(shost->host_lock);
543
544         phba->fc_edtov = FF_DEF_EDTOV;
545         phba->fc_ratov = FF_DEF_RATOV;
546         rc = memcmp(&vport->fc_portname, &sp->portName,
547                     sizeof(vport->fc_portname));
548         if (rc >= 0) {
549                 /* This side will initiate the PLOGI */
550                 spin_lock_irq(shost->host_lock);
551                 vport->fc_flag |= FC_PT2PT_PLOGI;
552                 spin_unlock_irq(shost->host_lock);
553
554                 /*
555                  * N_Port ID cannot be 0, set our to LocalID the other
556                  * side will be RemoteID.
557                  */
558
559                 /* not equal */
560                 if (rc)
561                         vport->fc_myDID = PT2PT_LocalID;
562
563                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
564                 if (!mbox)
565                         goto fail;
566
567                 lpfc_config_link(phba, mbox);
568
569                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
570                 mbox->vport = vport;
571                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
572                 if (rc == MBX_NOT_FINISHED) {
573                         mempool_free(mbox, phba->mbox_mem_pool);
574                         goto fail;
575                 }
576                 /* Decrement ndlp reference count indicating that ndlp can be
577                  * safely released when other references to it are done.
578                  */
579                 lpfc_nlp_put(ndlp);
580
581                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
582                 if (!ndlp) {
583                         /*
584                          * Cannot find existing Fabric ndlp, so allocate a
585                          * new one
586                          */
587                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
588                         if (!ndlp)
589                                 goto fail;
590                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
591                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
592                         ndlp = lpfc_enable_node(vport, ndlp,
593                                                 NLP_STE_UNUSED_NODE);
594                         if(!ndlp)
595                                 goto fail;
596                 }
597
598                 memcpy(&ndlp->nlp_portname, &sp->portName,
599                        sizeof(struct lpfc_name));
600                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
601                        sizeof(struct lpfc_name));
602                 /* Set state will put ndlp onto node list if not already done */
603                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
604                 spin_lock_irq(shost->host_lock);
605                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
606                 spin_unlock_irq(shost->host_lock);
607         } else
608                 /* This side will wait for the PLOGI, decrement ndlp reference
609                  * count indicating that ndlp can be released when other
610                  * references to it are done.
611                  */
612                 lpfc_nlp_put(ndlp);
613
614         /* If we are pt2pt with another NPort, force NPIV off! */
615         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
616
617         spin_lock_irq(shost->host_lock);
618         vport->fc_flag |= FC_PT2PT;
619         spin_unlock_irq(shost->host_lock);
620
621         /* Start discovery - this should just do CLEAR_LA */
622         lpfc_disc_start(vport);
623         return 0;
624 fail:
625         return -ENXIO;
626 }
627
628 /**
629  * lpfc_cmpl_els_flogi - Completion callback function for flogi
630  * @phba: pointer to lpfc hba data structure.
631  * @cmdiocb: pointer to lpfc command iocb data structure.
632  * @rspiocb: pointer to lpfc response iocb data structure.
633  *
634  * This routine is the top-level completion callback function for issuing
635  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
636  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
637  * retry has been made (either immediately or delayed with lpfc_els_retry()
638  * returning 1), the command IOCB will be released and function returned.
639  * If the retry attempt has been given up (possibly reach the maximum
640  * number of retries), one additional decrement of ndlp reference shall be
641  * invoked before going out after releasing the command IOCB. This will
642  * actually release the remote node (Note, lpfc_els_free_iocb() will also
643  * invoke one decrement of ndlp reference count). If no error reported in
644  * the IOCB status, the command Port ID field is used to determine whether
645  * this is a point-to-point topology or a fabric topology: if the Port ID
646  * field is assigned, it is a fabric topology; otherwise, it is a
647  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
648  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
649  * specific topology completion conditions.
650  **/
651 static void
652 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
653                     struct lpfc_iocbq *rspiocb)
654 {
655         struct lpfc_vport *vport = cmdiocb->vport;
656         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
657         IOCB_t *irsp = &rspiocb->iocb;
658         struct lpfc_nodelist *ndlp = cmdiocb->context1;
659         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
660         struct serv_parm *sp;
661         int rc;
662
663         /* Check to see if link went down during discovery */
664         if (lpfc_els_chk_latt(vport)) {
665                 /* One additional decrement on node reference count to
666                  * trigger the release of the node
667                  */
668                 lpfc_nlp_put(ndlp);
669                 goto out;
670         }
671
672         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
673                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
674                 irsp->ulpStatus, irsp->un.ulpWord[4],
675                 vport->port_state);
676
677         if (irsp->ulpStatus) {
678                 /* Check for retry */
679                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
680                         goto out;
681
682                 /* FLOGI failed, so there is no fabric */
683                 spin_lock_irq(shost->host_lock);
684                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
685                 spin_unlock_irq(shost->host_lock);
686
687                 /* If private loop, then allow max outstanding els to be
688                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
689                  * alpa map would take too long otherwise.
690                  */
691                 if (phba->alpa_map[0] == 0) {
692                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
693                 }
694
695                 /* FLOGI failure */
696                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
697                                  "0100 FLOGI failure Data: x%x x%x "
698                                  "x%x\n",
699                                  irsp->ulpStatus, irsp->un.ulpWord[4],
700                                  irsp->ulpTimeout);
701                 goto flogifail;
702         }
703
704         /*
705          * The FLogI succeeded.  Sync the data for the CPU before
706          * accessing it.
707          */
708         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
709
710         sp = prsp->virt + sizeof(uint32_t);
711
712         /* FLOGI completes successfully */
713         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
714                          "0101 FLOGI completes sucessfully "
715                          "Data: x%x x%x x%x x%x\n",
716                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
717                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
718
719         if (vport->port_state == LPFC_FLOGI) {
720                 /*
721                  * If Common Service Parameters indicate Nport
722                  * we are point to point, if Fport we are Fabric.
723                  */
724                 if (sp->cmn.fPort)
725                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
726                 else
727                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
728
729                 if (!rc)
730                         goto out;
731         }
732
733 flogifail:
734         lpfc_nlp_put(ndlp);
735
736         if (!lpfc_error_lost_link(irsp)) {
737                 /* FLOGI failed, so just use loop map to make discovery list */
738                 lpfc_disc_list_loopmap(vport);
739
740                 /* Start discovery */
741                 lpfc_disc_start(vport);
742         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
743                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
744                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
745                         (phba->link_state != LPFC_CLEAR_LA)) {
746                 /* If FLOGI failed enable link interrupt. */
747                 lpfc_issue_clear_la(phba, vport);
748         }
749 out:
750         lpfc_els_free_iocb(phba, cmdiocb);
751 }
752
753 /**
754  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
755  * @vport: pointer to a host virtual N_Port data structure.
756  * @ndlp: pointer to a node-list data structure.
757  * @retry: number of retries to the command IOCB.
758  *
759  * This routine issues a Fabric Login (FLOGI) Request ELS command
760  * for a @vport. The initiator service parameters are put into the payload
761  * of the FLOGI Request IOCB and the top-level callback function pointer
762  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
763  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
764  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
765  *
766  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
767  * will be incremented by 1 for holding the ndlp and the reference to ndlp
768  * will be stored into the context1 field of the IOCB for the completion
769  * callback function to the FLOGI ELS command.
770  *
771  * Return code
772  *   0 - successfully issued flogi iocb for @vport
773  *   1 - failed to issue flogi iocb for @vport
774  **/
775 static int
776 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
777                      uint8_t retry)
778 {
779         struct lpfc_hba  *phba = vport->phba;
780         struct serv_parm *sp;
781         IOCB_t *icmd;
782         struct lpfc_iocbq *elsiocb;
783         struct lpfc_sli_ring *pring;
784         uint8_t *pcmd;
785         uint16_t cmdsize;
786         uint32_t tmo;
787         int rc;
788
789         pring = &phba->sli.ring[LPFC_ELS_RING];
790
791         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
792         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
793                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
794
795         if (!elsiocb)
796                 return 1;
797
798         icmd = &elsiocb->iocb;
799         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
800
801         /* For FLOGI request, remainder of payload is service parameters */
802         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
803         pcmd += sizeof(uint32_t);
804         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
805         sp = (struct serv_parm *) pcmd;
806
807         /* Setup CSPs accordingly for Fabric */
808         sp->cmn.e_d_tov = 0;
809         sp->cmn.w2.r_a_tov = 0;
810         sp->cls1.classValid = 0;
811         sp->cls2.seqDelivery = 1;
812         sp->cls3.seqDelivery = 1;
813         if (sp->cmn.fcphLow < FC_PH3)
814                 sp->cmn.fcphLow = FC_PH3;
815         if (sp->cmn.fcphHigh < FC_PH3)
816                 sp->cmn.fcphHigh = FC_PH3;
817
818         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
819                 sp->cmn.request_multiple_Nport = 1;
820
821                 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
822                 icmd->ulpCt_h = 1;
823                 icmd->ulpCt_l = 0;
824         }
825
826         if (phba->fc_topology != TOPOLOGY_LOOP) {
827                 icmd->un.elsreq64.myID = 0;
828                 icmd->un.elsreq64.fl = 1;
829         }
830
831         tmo = phba->fc_ratov;
832         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
833         lpfc_set_disctmo(vport);
834         phba->fc_ratov = tmo;
835
836         phba->fc_stat.elsXmitFLOGI++;
837         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
838
839         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
840                 "Issue FLOGI:     opt:x%x",
841                 phba->sli3_options, 0, 0);
842
843         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
844         if (rc == IOCB_ERROR) {
845                 lpfc_els_free_iocb(phba, elsiocb);
846                 return 1;
847         }
848         return 0;
849 }
850
851 /**
852  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
853  * @phba: pointer to lpfc hba data structure.
854  *
855  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
856  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
857  * list and issues an abort IOCB commond on each outstanding IOCB that
858  * contains a active Fabric_DID ndlp. Note that this function is to issue
859  * the abort IOCB command on all the outstanding IOCBs, thus when this
860  * function returns, it does not guarantee all the IOCBs are actually aborted.
861  *
862  * Return code
863  *   0 - Sucessfully issued abort iocb on all outstanding flogis (Always 0)
864  **/
865 int
866 lpfc_els_abort_flogi(struct lpfc_hba *phba)
867 {
868         struct lpfc_sli_ring *pring;
869         struct lpfc_iocbq *iocb, *next_iocb;
870         struct lpfc_nodelist *ndlp;
871         IOCB_t *icmd;
872
873         /* Abort outstanding I/O on NPort <nlp_DID> */
874         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
875                         "0201 Abort outstanding I/O on NPort x%x\n",
876                         Fabric_DID);
877
878         pring = &phba->sli.ring[LPFC_ELS_RING];
879
880         /*
881          * Check the txcmplq for an iocb that matches the nport the driver is
882          * searching for.
883          */
884         spin_lock_irq(&phba->hbalock);
885         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
886                 icmd = &iocb->iocb;
887                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
888                     icmd->un.elsreq64.bdl.ulpIoTag32) {
889                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
890                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
891                             (ndlp->nlp_DID == Fabric_DID))
892                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
893                 }
894         }
895         spin_unlock_irq(&phba->hbalock);
896
897         return 0;
898 }
899
900 /**
901  * lpfc_initial_flogi - Issue an initial fabric login for a vport
902  * @vport: pointer to a host virtual N_Port data structure.
903  *
904  * This routine issues an initial Fabric Login (FLOGI) for the @vport
905  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
906  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
907  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
908  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
909  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
910  * @vport.
911  *
912  * Return code
913  *   0 - failed to issue initial flogi for @vport
914  *   1 - successfully issued initial flogi for @vport
915  **/
916 int
917 lpfc_initial_flogi(struct lpfc_vport *vport)
918 {
919         struct lpfc_hba *phba = vport->phba;
920         struct lpfc_nodelist *ndlp;
921
922         vport->port_state = LPFC_FLOGI;
923         lpfc_set_disctmo(vport);
924
925         /* First look for the Fabric ndlp */
926         ndlp = lpfc_findnode_did(vport, Fabric_DID);
927         if (!ndlp) {
928                 /* Cannot find existing Fabric ndlp, so allocate a new one */
929                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
930                 if (!ndlp)
931                         return 0;
932                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
933                 /* Put ndlp onto node list */
934                 lpfc_enqueue_node(vport, ndlp);
935         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
936                 /* re-setup ndlp without removing from node list */
937                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
938                 if (!ndlp)
939                         return 0;
940         }
941
942         if (lpfc_issue_els_flogi(vport, ndlp, 0))
943                 /* This decrement of reference count to node shall kick off
944                  * the release of the node.
945                  */
946                 lpfc_nlp_put(ndlp);
947
948         return 1;
949 }
950
951 /**
952  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
953  * @vport: pointer to a host virtual N_Port data structure.
954  *
955  * This routine issues an initial Fabric Discover (FDISC) for the @vport
956  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
957  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
958  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
959  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
960  * is then invoked with the @vport and the ndlp to perform the FDISC for the
961  * @vport.
962  *
963  * Return code
964  *   0 - failed to issue initial fdisc for @vport
965  *   1 - successfully issued initial fdisc for @vport
966  **/
967 int
968 lpfc_initial_fdisc(struct lpfc_vport *vport)
969 {
970         struct lpfc_hba *phba = vport->phba;
971         struct lpfc_nodelist *ndlp;
972
973         /* First look for the Fabric ndlp */
974         ndlp = lpfc_findnode_did(vport, Fabric_DID);
975         if (!ndlp) {
976                 /* Cannot find existing Fabric ndlp, so allocate a new one */
977                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
978                 if (!ndlp)
979                         return 0;
980                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
981                 /* Put ndlp onto node list */
982                 lpfc_enqueue_node(vport, ndlp);
983         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
984                 /* re-setup ndlp without removing from node list */
985                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
986                 if (!ndlp)
987                         return 0;
988         }
989
990         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
991                 /* decrement node reference count to trigger the release of
992                  * the node.
993                  */
994                 lpfc_nlp_put(ndlp);
995                 return 0;
996         }
997         return 1;
998 }
999
1000 /**
1001  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1002  * @vport: pointer to a host virtual N_Port data structure.
1003  *
1004  * This routine checks whether there are more remaining Port Logins
1005  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1006  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1007  * to issue ELS PLOGIs up to the configured discover threads with the
1008  * @vport (@vport->cfg_discovery_threads). The function also decrement
1009  * the @vport's num_disc_node by 1 if it is not already 0.
1010  **/
1011 void
1012 lpfc_more_plogi(struct lpfc_vport *vport)
1013 {
1014         int sentplogi;
1015
1016         if (vport->num_disc_nodes)
1017                 vport->num_disc_nodes--;
1018
1019         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1020         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1021                          "0232 Continue discovery with %d PLOGIs to go "
1022                          "Data: x%x x%x x%x\n",
1023                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1024                          vport->fc_flag, vport->port_state);
1025         /* Check to see if there are more PLOGIs to be sent */
1026         if (vport->fc_flag & FC_NLP_MORE)
1027                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1028                 sentplogi = lpfc_els_disc_plogi(vport);
1029
1030         return;
1031 }
1032
1033 /**
1034  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1035  * @phba: pointer to lpfc hba data structure.
1036  * @prsp: pointer to response IOCB payload.
1037  * @ndlp: pointer to a node-list data structure.
1038  *
1039  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1040  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1041  * The following cases are considered N_Port confirmed:
1042  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1043  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1044  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1045  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1046  * 1) if there is a node on vport list other than the @ndlp with the same
1047  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1048  * on that node to release the RPI associated with the node; 2) if there is
1049  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1050  * into, a new node shall be allocated (or activated). In either case, the
1051  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1052  * be released and the new_ndlp shall be put on to the vport node list and
1053  * its pointer returned as the confirmed node.
1054  *
1055  * Note that before the @ndlp got "released", the keepDID from not-matching
1056  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1057  * of the @ndlp. This is because the release of @ndlp is actually to put it
1058  * into an inactive state on the vport node list and the vport node list
1059  * management algorithm does not allow two node with a same DID.
1060  *
1061  * Return code
1062  *   pointer to the PLOGI N_Port @ndlp
1063  **/
1064 static struct lpfc_nodelist *
1065 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1066                          struct lpfc_nodelist *ndlp)
1067 {
1068         struct lpfc_vport    *vport = ndlp->vport;
1069         struct lpfc_nodelist *new_ndlp;
1070         struct lpfc_rport_data *rdata;
1071         struct fc_rport *rport;
1072         struct serv_parm *sp;
1073         uint8_t  name[sizeof(struct lpfc_name)];
1074         uint32_t rc, keepDID = 0;
1075
1076         /* Fabric nodes can have the same WWPN so we don't bother searching
1077          * by WWPN.  Just return the ndlp that was given to us.
1078          */
1079         if (ndlp->nlp_type & NLP_FABRIC)
1080                 return ndlp;
1081
1082         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1083         memset(name, 0, sizeof(struct lpfc_name));
1084
1085         /* Now we find out if the NPort we are logging into, matches the WWPN
1086          * we have for that ndlp. If not, we have some work to do.
1087          */
1088         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1089
1090         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1091                 return ndlp;
1092
1093         if (!new_ndlp) {
1094                 rc = memcmp(&ndlp->nlp_portname, name,
1095                             sizeof(struct lpfc_name));
1096                 if (!rc)
1097                         return ndlp;
1098                 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1099                 if (!new_ndlp)
1100                         return ndlp;
1101                 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1102         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1103                 rc = memcmp(&ndlp->nlp_portname, name,
1104                             sizeof(struct lpfc_name));
1105                 if (!rc)
1106                         return ndlp;
1107                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1108                                                 NLP_STE_UNUSED_NODE);
1109                 if (!new_ndlp)
1110                         return ndlp;
1111                 keepDID = new_ndlp->nlp_DID;
1112         } else
1113                 keepDID = new_ndlp->nlp_DID;
1114
1115         lpfc_unreg_rpi(vport, new_ndlp);
1116         new_ndlp->nlp_DID = ndlp->nlp_DID;
1117         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1118
1119         if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1120                 new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1121         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1122
1123         /* Set state will put new_ndlp on to node list if not already done */
1124         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1125
1126         /* Move this back to NPR state */
1127         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1128                 /* The new_ndlp is replacing ndlp totally, so we need
1129                  * to put ndlp on UNUSED list and try to free it.
1130                  */
1131
1132                 /* Fix up the rport accordingly */
1133                 rport =  ndlp->rport;
1134                 if (rport) {
1135                         rdata = rport->dd_data;
1136                         if (rdata->pnode == ndlp) {
1137                                 lpfc_nlp_put(ndlp);
1138                                 ndlp->rport = NULL;
1139                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1140                                 new_ndlp->rport = rport;
1141                         }
1142                         new_ndlp->nlp_type = ndlp->nlp_type;
1143                 }
1144                 /* We shall actually free the ndlp with both nlp_DID and
1145                  * nlp_portname fields equals 0 to avoid any ndlp on the
1146                  * nodelist never to be used.
1147                  */
1148                 if (ndlp->nlp_DID == 0) {
1149                         spin_lock_irq(&phba->ndlp_lock);
1150                         NLP_SET_FREE_REQ(ndlp);
1151                         spin_unlock_irq(&phba->ndlp_lock);
1152                 }
1153
1154                 /* Two ndlps cannot have the same did on the nodelist */
1155                 ndlp->nlp_DID = keepDID;
1156                 lpfc_drop_node(vport, ndlp);
1157         }
1158         else {
1159                 lpfc_unreg_rpi(vport, ndlp);
1160                 /* Two ndlps cannot have the same did */
1161                 ndlp->nlp_DID = keepDID;
1162                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1163         }
1164         return new_ndlp;
1165 }
1166
1167 /**
1168  * lpfc_end_rscn - Check and handle more rscn for a vport
1169  * @vport: pointer to a host virtual N_Port data structure.
1170  *
1171  * This routine checks whether more Registration State Change
1172  * Notifications (RSCNs) came in while the discovery state machine was in
1173  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1174  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1175  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1176  * handling the RSCNs.
1177  **/
1178 void
1179 lpfc_end_rscn(struct lpfc_vport *vport)
1180 {
1181         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1182
1183         if (vport->fc_flag & FC_RSCN_MODE) {
1184                 /*
1185                  * Check to see if more RSCNs came in while we were
1186                  * processing this one.
1187                  */
1188                 if (vport->fc_rscn_id_cnt ||
1189                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1190                         lpfc_els_handle_rscn(vport);
1191                 else {
1192                         spin_lock_irq(shost->host_lock);
1193                         vport->fc_flag &= ~FC_RSCN_MODE;
1194                         spin_unlock_irq(shost->host_lock);
1195                 }
1196         }
1197 }
1198
1199 /**
1200  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1201  * @phba: pointer to lpfc hba data structure.
1202  * @cmdiocb: pointer to lpfc command iocb data structure.
1203  * @rspiocb: pointer to lpfc response iocb data structure.
1204  *
1205  * This routine is the completion callback function for issuing the Port
1206  * Login (PLOGI) command. For PLOGI completion, there must be an active
1207  * ndlp on the vport node list that matches the remote node ID from the
1208  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1209  * ignored and command IOCB released. The PLOGI response IOCB status is
1210  * checked for error conditons. If there is error status reported, PLOGI
1211  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1212  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1213  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1214  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1215  * there are additional N_Port nodes with the vport that need to perform
1216  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1217  * PLOGIs.
1218  **/
1219 static void
1220 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1221                     struct lpfc_iocbq *rspiocb)
1222 {
1223         struct lpfc_vport *vport = cmdiocb->vport;
1224         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1225         IOCB_t *irsp;
1226         struct lpfc_nodelist *ndlp;
1227         struct lpfc_dmabuf *prsp;
1228         int disc, rc, did, type;
1229
1230         /* we pass cmdiocb to state machine which needs rspiocb as well */
1231         cmdiocb->context_un.rsp_iocb = rspiocb;
1232
1233         irsp = &rspiocb->iocb;
1234         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1235                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
1236                 irsp->ulpStatus, irsp->un.ulpWord[4],
1237                 irsp->un.elsreq64.remoteID);
1238
1239         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1240         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1241                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1242                                  "0136 PLOGI completes to NPort x%x "
1243                                  "with no ndlp. Data: x%x x%x x%x\n",
1244                                  irsp->un.elsreq64.remoteID,
1245                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1246                                  irsp->ulpIoTag);
1247                 goto out;
1248         }
1249
1250         /* Since ndlp can be freed in the disc state machine, note if this node
1251          * is being used during discovery.
1252          */
1253         spin_lock_irq(shost->host_lock);
1254         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1255         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1256         spin_unlock_irq(shost->host_lock);
1257         rc   = 0;
1258
1259         /* PLOGI completes to NPort <nlp_DID> */
1260         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1261                          "0102 PLOGI completes to NPort x%x "
1262                          "Data: x%x x%x x%x x%x x%x\n",
1263                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1264                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1265         /* Check to see if link went down during discovery */
1266         if (lpfc_els_chk_latt(vport)) {
1267                 spin_lock_irq(shost->host_lock);
1268                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1269                 spin_unlock_irq(shost->host_lock);
1270                 goto out;
1271         }
1272
1273         /* ndlp could be freed in DSM, save these values now */
1274         type = ndlp->nlp_type;
1275         did = ndlp->nlp_DID;
1276
1277         if (irsp->ulpStatus) {
1278                 /* Check for retry */
1279                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1280                         /* ELS command is being retried */
1281                         if (disc) {
1282                                 spin_lock_irq(shost->host_lock);
1283                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1284                                 spin_unlock_irq(shost->host_lock);
1285                         }
1286                         goto out;
1287                 }
1288                 /* PLOGI failed */
1289                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1290                 if (lpfc_error_lost_link(irsp))
1291                         rc = NLP_STE_FREED_NODE;
1292                 else
1293                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1294                                                      NLP_EVT_CMPL_PLOGI);
1295         } else {
1296                 /* Good status, call state machine */
1297                 prsp = list_entry(((struct lpfc_dmabuf *)
1298                                    cmdiocb->context2)->list.next,
1299                                   struct lpfc_dmabuf, list);
1300                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1301                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1302                                              NLP_EVT_CMPL_PLOGI);
1303         }
1304
1305         if (disc && vport->num_disc_nodes) {
1306                 /* Check to see if there are more PLOGIs to be sent */
1307                 lpfc_more_plogi(vport);
1308
1309                 if (vport->num_disc_nodes == 0) {
1310                         spin_lock_irq(shost->host_lock);
1311                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
1312                         spin_unlock_irq(shost->host_lock);
1313
1314                         lpfc_can_disctmo(vport);
1315                         lpfc_end_rscn(vport);
1316                 }
1317         }
1318
1319 out:
1320         lpfc_els_free_iocb(phba, cmdiocb);
1321         return;
1322 }
1323
1324 /**
1325  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
1326  * @vport: pointer to a host virtual N_Port data structure.
1327  * @did: destination port identifier.
1328  * @retry: number of retries to the command IOCB.
1329  *
1330  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1331  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1332  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1333  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1334  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1335  *
1336  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1337  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1338  * will be stored into the context1 field of the IOCB for the completion
1339  * callback function to the PLOGI ELS command.
1340  *
1341  * Return code
1342  *   0 - Successfully issued a plogi for @vport
1343  *   1 - failed to issue a plogi for @vport
1344  **/
1345 int
1346 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1347 {
1348         struct lpfc_hba  *phba = vport->phba;
1349         struct serv_parm *sp;
1350         IOCB_t *icmd;
1351         struct lpfc_nodelist *ndlp;
1352         struct lpfc_iocbq *elsiocb;
1353         struct lpfc_sli_ring *pring;
1354         struct lpfc_sli *psli;
1355         uint8_t *pcmd;
1356         uint16_t cmdsize;
1357         int ret;
1358
1359         psli = &phba->sli;
1360         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1361
1362         ndlp = lpfc_findnode_did(vport, did);
1363         if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1364                 ndlp = NULL;
1365
1366         /* If ndlp is not NULL, we will bump the reference count on it */
1367         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1368         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1369                                      ELS_CMD_PLOGI);
1370         if (!elsiocb)
1371                 return 1;
1372
1373         icmd = &elsiocb->iocb;
1374         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1375
1376         /* For PLOGI request, remainder of payload is service parameters */
1377         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1378         pcmd += sizeof(uint32_t);
1379         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1380         sp = (struct serv_parm *) pcmd;
1381
1382         if (sp->cmn.fcphLow < FC_PH_4_3)
1383                 sp->cmn.fcphLow = FC_PH_4_3;
1384
1385         if (sp->cmn.fcphHigh < FC_PH3)
1386                 sp->cmn.fcphHigh = FC_PH3;
1387
1388         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1389                 "Issue PLOGI:     did:x%x",
1390                 did, 0, 0);
1391
1392         phba->fc_stat.elsXmitPLOGI++;
1393         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1394         ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1395
1396         if (ret == IOCB_ERROR) {
1397                 lpfc_els_free_iocb(phba, elsiocb);
1398                 return 1;
1399         }
1400         return 0;
1401 }
1402
1403 /**
1404  * lpfc_cmpl_els_prli - Completion callback function for prli
1405  * @phba: pointer to lpfc hba data structure.
1406  * @cmdiocb: pointer to lpfc command iocb data structure.
1407  * @rspiocb: pointer to lpfc response iocb data structure.
1408  *
1409  * This routine is the completion callback function for a Process Login
1410  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1411  * status. If there is error status reported, PRLI retry shall be attempted
1412  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1413  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1414  * ndlp to mark the PRLI completion.
1415  **/
1416 static void
1417 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1418                    struct lpfc_iocbq *rspiocb)
1419 {
1420         struct lpfc_vport *vport = cmdiocb->vport;
1421         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1422         IOCB_t *irsp;
1423         struct lpfc_sli *psli;
1424         struct lpfc_nodelist *ndlp;
1425
1426         psli = &phba->sli;
1427         /* we pass cmdiocb to state machine which needs rspiocb as well */
1428         cmdiocb->context_un.rsp_iocb = rspiocb;
1429
1430         irsp = &(rspiocb->iocb);
1431         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1432         spin_lock_irq(shost->host_lock);
1433         ndlp->nlp_flag &= ~NLP_PRLI_SND;
1434         spin_unlock_irq(shost->host_lock);
1435
1436         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1437                 "PRLI cmpl:       status:x%x/x%x did:x%x",
1438                 irsp->ulpStatus, irsp->un.ulpWord[4],
1439                 ndlp->nlp_DID);
1440         /* PRLI completes to NPort <nlp_DID> */
1441         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1442                          "0103 PRLI completes to NPort x%x "
1443                          "Data: x%x x%x x%x x%x\n",
1444                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1445                          irsp->ulpTimeout, vport->num_disc_nodes);
1446
1447         vport->fc_prli_sent--;
1448         /* Check to see if link went down during discovery */
1449         if (lpfc_els_chk_latt(vport))
1450                 goto out;
1451
1452         if (irsp->ulpStatus) {
1453                 /* Check for retry */
1454                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1455                         /* ELS command is being retried */
1456                         goto out;
1457                 }
1458                 /* PRLI failed */
1459                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1460                 if (lpfc_error_lost_link(irsp))
1461                         goto out;
1462                 else
1463                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1464                                                 NLP_EVT_CMPL_PRLI);
1465         } else
1466                 /* Good status, call state machine */
1467                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1468                                         NLP_EVT_CMPL_PRLI);
1469 out:
1470         lpfc_els_free_iocb(phba, cmdiocb);
1471         return;
1472 }
1473
1474 /**
1475  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
1476  * @vport: pointer to a host virtual N_Port data structure.
1477  * @ndlp: pointer to a node-list data structure.
1478  * @retry: number of retries to the command IOCB.
1479  *
1480  * This routine issues a Process Login (PRLI) ELS command for the
1481  * @vport. The PRLI service parameters are set up in the payload of the
1482  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1483  * is put to the IOCB completion callback func field before invoking the
1484  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1485  *
1486  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1487  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1488  * will be stored into the context1 field of the IOCB for the completion
1489  * callback function to the PRLI ELS command.
1490  *
1491  * Return code
1492  *   0 - successfully issued prli iocb command for @vport
1493  *   1 - failed to issue prli iocb command for @vport
1494  **/
1495 int
1496 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1497                     uint8_t retry)
1498 {
1499         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1500         struct lpfc_hba *phba = vport->phba;
1501         PRLI *npr;
1502         IOCB_t *icmd;
1503         struct lpfc_iocbq *elsiocb;
1504         struct lpfc_sli_ring *pring;
1505         struct lpfc_sli *psli;
1506         uint8_t *pcmd;
1507         uint16_t cmdsize;
1508
1509         psli = &phba->sli;
1510         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1511
1512         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1513         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1514                                      ndlp->nlp_DID, ELS_CMD_PRLI);
1515         if (!elsiocb)
1516                 return 1;
1517
1518         icmd = &elsiocb->iocb;
1519         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1520
1521         /* For PRLI request, remainder of payload is service parameters */
1522         memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1523         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1524         pcmd += sizeof(uint32_t);
1525
1526         /* For PRLI, remainder of payload is PRLI parameter page */
1527         npr = (PRLI *) pcmd;
1528         /*
1529          * If our firmware version is 3.20 or later,
1530          * set the following bits for FC-TAPE support.
1531          */
1532         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1533                 npr->ConfmComplAllowed = 1;
1534                 npr->Retry = 1;
1535                 npr->TaskRetryIdReq = 1;
1536         }
1537         npr->estabImagePair = 1;
1538         npr->readXferRdyDis = 1;
1539
1540         /* For FCP support */
1541         npr->prliType = PRLI_FCP_TYPE;
1542         npr->initiatorFunc = 1;
1543
1544         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1545                 "Issue PRLI:      did:x%x",
1546                 ndlp->nlp_DID, 0, 0);
1547
1548         phba->fc_stat.elsXmitPRLI++;
1549         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1550         spin_lock_irq(shost->host_lock);
1551         ndlp->nlp_flag |= NLP_PRLI_SND;
1552         spin_unlock_irq(shost->host_lock);
1553         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1554                 spin_lock_irq(shost->host_lock);
1555                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1556                 spin_unlock_irq(shost->host_lock);
1557                 lpfc_els_free_iocb(phba, elsiocb);
1558                 return 1;
1559         }
1560         vport->fc_prli_sent++;
1561         return 0;
1562 }
1563
1564 /**
1565  * lpfc_rscn_disc - Perform rscn discovery for a vport
1566  * @vport: pointer to a host virtual N_Port data structure.
1567  *
1568  * This routine performs Registration State Change Notification (RSCN)
1569  * discovery for a @vport. If the @vport's node port recovery count is not
1570  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1571  * the nodes that need recovery. If none of the PLOGI were needed through
1572  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1573  * invoked to check and handle possible more RSCN came in during the period
1574  * of processing the current ones.
1575  **/
1576 static void
1577 lpfc_rscn_disc(struct lpfc_vport *vport)
1578 {
1579         lpfc_can_disctmo(vport);
1580
1581         /* RSCN discovery */
1582         /* go thru NPR nodes and issue ELS PLOGIs */
1583         if (vport->fc_npr_cnt)
1584                 if (lpfc_els_disc_plogi(vport))
1585                         return;
1586
1587         lpfc_end_rscn(vport);
1588 }
1589
1590 /**
1591  * lpfc_adisc_done - Complete the adisc phase of discovery
1592  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
1593  *
1594  * This function is called when the final ADISC is completed during discovery.
1595  * This function handles clearing link attention or issuing reg_vpi depending
1596  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
1597  * discovery.
1598  * This function is called with no locks held.
1599  **/
1600 static void
1601 lpfc_adisc_done(struct lpfc_vport *vport)
1602 {
1603         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1604         struct lpfc_hba   *phba = vport->phba;
1605
1606         /*
1607          * For NPIV, cmpl_reg_vpi will set port_state to READY,
1608          * and continue discovery.
1609          */
1610         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1611             !(vport->fc_flag & FC_RSCN_MODE)) {
1612                 lpfc_issue_reg_vpi(phba, vport);
1613                 return;
1614         }
1615         /*
1616         * For SLI2, we need to set port_state to READY
1617         * and continue discovery.
1618         */
1619         if (vport->port_state < LPFC_VPORT_READY) {
1620                 /* If we get here, there is nothing to ADISC */
1621                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1622                         lpfc_issue_clear_la(phba, vport);
1623                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1624                         vport->num_disc_nodes = 0;
1625                         /* go thru NPR list, issue ELS PLOGIs */
1626                         if (vport->fc_npr_cnt)
1627                                 lpfc_els_disc_plogi(vport);
1628                         if (!vport->num_disc_nodes) {
1629                                 spin_lock_irq(shost->host_lock);
1630                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1631                                 spin_unlock_irq(shost->host_lock);
1632                                 lpfc_can_disctmo(vport);
1633                                 lpfc_end_rscn(vport);
1634                         }
1635                 }
1636                 vport->port_state = LPFC_VPORT_READY;
1637         } else
1638                 lpfc_rscn_disc(vport);
1639 }
1640
1641 /**
1642  * lpfc_more_adisc - Issue more adisc as needed
1643  * @vport: pointer to a host virtual N_Port data structure.
1644  *
1645  * This routine determines whether there are more ndlps on a @vport
1646  * node list need to have Address Discover (ADISC) issued. If so, it will
1647  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1648  * remaining nodes which need to have ADISC sent.
1649  **/
1650 void
1651 lpfc_more_adisc(struct lpfc_vport *vport)
1652 {
1653         int sentadisc;
1654
1655         if (vport->num_disc_nodes)
1656                 vport->num_disc_nodes--;
1657         /* Continue discovery with <num_disc_nodes> ADISCs to go */
1658         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1659                          "0210 Continue discovery with %d ADISCs to go "
1660                          "Data: x%x x%x x%x\n",
1661                          vport->num_disc_nodes, vport->fc_adisc_cnt,
1662                          vport->fc_flag, vport->port_state);
1663         /* Check to see if there are more ADISCs to be sent */
1664         if (vport->fc_flag & FC_NLP_MORE) {
1665                 lpfc_set_disctmo(vport);
1666                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1667                 sentadisc = lpfc_els_disc_adisc(vport);
1668         }
1669         if (!vport->num_disc_nodes)
1670                 lpfc_adisc_done(vport);
1671         return;
1672 }
1673
1674 /**
1675  * lpfc_cmpl_els_adisc - Completion callback function for adisc
1676  * @phba: pointer to lpfc hba data structure.
1677  * @cmdiocb: pointer to lpfc command iocb data structure.
1678  * @rspiocb: pointer to lpfc response iocb data structure.
1679  *
1680  * This routine is the completion function for issuing the Address Discover
1681  * (ADISC) command. It first checks to see whether link went down during
1682  * the discovery process. If so, the node will be marked as node port
1683  * recovery for issuing discover IOCB by the link attention handler and
1684  * exit. Otherwise, the response status is checked. If error was reported
1685  * in the response status, the ADISC command shall be retried by invoking
1686  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1687  * the response status, the state machine is invoked to set transition
1688  * with respect to NLP_EVT_CMPL_ADISC event.
1689  **/
1690 static void
1691 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1692                     struct lpfc_iocbq *rspiocb)
1693 {
1694         struct lpfc_vport *vport = cmdiocb->vport;
1695         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1696         IOCB_t *irsp;
1697         struct lpfc_nodelist *ndlp;
1698         int  disc;
1699
1700         /* we pass cmdiocb to state machine which needs rspiocb as well */
1701         cmdiocb->context_un.rsp_iocb = rspiocb;
1702
1703         irsp = &(rspiocb->iocb);
1704         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1705
1706         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1707                 "ADISC cmpl:      status:x%x/x%x did:x%x",
1708                 irsp->ulpStatus, irsp->un.ulpWord[4],
1709                 ndlp->nlp_DID);
1710
1711         /* Since ndlp can be freed in the disc state machine, note if this node
1712          * is being used during discovery.
1713          */
1714         spin_lock_irq(shost->host_lock);
1715         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1716         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1717         spin_unlock_irq(shost->host_lock);
1718         /* ADISC completes to NPort <nlp_DID> */
1719         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1720                          "0104 ADISC completes to NPort x%x "
1721                          "Data: x%x x%x x%x x%x x%x\n",
1722                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1723                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1724         /* Check to see if link went down during discovery */
1725         if (lpfc_els_chk_latt(vport)) {
1726                 spin_lock_irq(shost->host_lock);
1727                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1728                 spin_unlock_irq(shost->host_lock);
1729                 goto out;
1730         }
1731
1732         if (irsp->ulpStatus) {
1733                 /* Check for retry */
1734                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1735                         /* ELS command is being retried */
1736                         if (disc) {
1737                                 spin_lock_irq(shost->host_lock);
1738                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1739                                 spin_unlock_irq(shost->host_lock);
1740                                 lpfc_set_disctmo(vport);
1741                         }
1742                         goto out;
1743                 }
1744                 /* ADISC failed */
1745                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1746                 if (!lpfc_error_lost_link(irsp))
1747                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1748                                                 NLP_EVT_CMPL_ADISC);
1749         } else
1750                 /* Good status, call state machine */
1751                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1752                                         NLP_EVT_CMPL_ADISC);
1753
1754         /* Check to see if there are more ADISCs to be sent */
1755         if (disc && vport->num_disc_nodes)
1756                 lpfc_more_adisc(vport);
1757 out:
1758         lpfc_els_free_iocb(phba, cmdiocb);
1759         return;
1760 }
1761
1762 /**
1763  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
1764  * @vport: pointer to a virtual N_Port data structure.
1765  * @ndlp: pointer to a node-list data structure.
1766  * @retry: number of retries to the command IOCB.
1767  *
1768  * This routine issues an Address Discover (ADISC) for an @ndlp on a
1769  * @vport. It prepares the payload of the ADISC ELS command, updates the
1770  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
1771  * to issue the ADISC ELS command.
1772  *
1773  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1774  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1775  * will be stored into the context1 field of the IOCB for the completion
1776  * callback function to the ADISC ELS command.
1777  *
1778  * Return code
1779  *   0 - successfully issued adisc
1780  *   1 - failed to issue adisc
1781  **/
1782 int
1783 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1784                      uint8_t retry)
1785 {
1786         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1787         struct lpfc_hba  *phba = vport->phba;
1788         ADISC *ap;
1789         IOCB_t *icmd;
1790         struct lpfc_iocbq *elsiocb;
1791         struct lpfc_sli *psli = &phba->sli;
1792         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1793         uint8_t *pcmd;
1794         uint16_t cmdsize;
1795
1796         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
1797         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1798                                      ndlp->nlp_DID, ELS_CMD_ADISC);
1799         if (!elsiocb)
1800                 return 1;
1801
1802         icmd = &elsiocb->iocb;
1803         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1804
1805         /* For ADISC request, remainder of payload is service parameters */
1806         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1807         pcmd += sizeof(uint32_t);
1808
1809         /* Fill in ADISC payload */
1810         ap = (ADISC *) pcmd;
1811         ap->hardAL_PA = phba->fc_pref_ALPA;
1812         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
1813         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1814         ap->DID = be32_to_cpu(vport->fc_myDID);
1815
1816         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1817                 "Issue ADISC:     did:x%x",
1818                 ndlp->nlp_DID, 0, 0);
1819
1820         phba->fc_stat.elsXmitADISC++;
1821         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1822         spin_lock_irq(shost->host_lock);
1823         ndlp->nlp_flag |= NLP_ADISC_SND;
1824         spin_unlock_irq(shost->host_lock);
1825         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1826                 spin_lock_irq(shost->host_lock);
1827                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
1828                 spin_unlock_irq(shost->host_lock);
1829                 lpfc_els_free_iocb(phba, elsiocb);
1830                 return 1;
1831         }
1832         return 0;
1833 }
1834
1835 /**
1836  * lpfc_cmpl_els_logo - Completion callback function for logo
1837  * @phba: pointer to lpfc hba data structure.
1838  * @cmdiocb: pointer to lpfc command iocb data structure.
1839  * @rspiocb: pointer to lpfc response iocb data structure.
1840  *
1841  * This routine is the completion function for issuing the ELS Logout (LOGO)
1842  * command. If no error status was reported from the LOGO response, the
1843  * state machine of the associated ndlp shall be invoked for transition with
1844  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
1845  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
1846  **/
1847 static void
1848 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1849                    struct lpfc_iocbq *rspiocb)
1850 {
1851         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1852         struct lpfc_vport *vport = ndlp->vport;
1853         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1854         IOCB_t *irsp;
1855         struct lpfc_sli *psli;
1856
1857         psli = &phba->sli;
1858         /* we pass cmdiocb to state machine which needs rspiocb as well */
1859         cmdiocb->context_un.rsp_iocb = rspiocb;
1860
1861         irsp = &(rspiocb->iocb);
1862         spin_lock_irq(shost->host_lock);
1863         ndlp->nlp_flag &= ~NLP_LOGO_SND;
1864         spin_unlock_irq(shost->host_lock);
1865
1866         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1867                 "LOGO cmpl:       status:x%x/x%x did:x%x",
1868                 irsp->ulpStatus, irsp->un.ulpWord[4],
1869                 ndlp->nlp_DID);
1870         /* LOGO completes to NPort <nlp_DID> */
1871         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1872                          "0105 LOGO completes to NPort x%x "
1873                          "Data: x%x x%x x%x x%x\n",
1874                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1875                          irsp->ulpTimeout, vport->num_disc_nodes);
1876         /* Check to see if link went down during discovery */
1877         if (lpfc_els_chk_latt(vport))
1878                 goto out;
1879
1880         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
1881                 /* NLP_EVT_DEVICE_RM should unregister the RPI
1882                  * which should abort all outstanding IOs.
1883                  */
1884                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1885                                         NLP_EVT_DEVICE_RM);
1886                 goto out;
1887         }
1888
1889         if (irsp->ulpStatus) {
1890                 /* Check for retry */
1891                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1892                         /* ELS command is being retried */
1893                         goto out;
1894                 /* LOGO failed */
1895                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1896                 if (lpfc_error_lost_link(irsp))
1897                         goto out;
1898                 else
1899                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1900                                                 NLP_EVT_CMPL_LOGO);
1901         } else
1902                 /* Good status, call state machine.
1903                  * This will unregister the rpi if needed.
1904                  */
1905                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1906                                         NLP_EVT_CMPL_LOGO);
1907 out:
1908         lpfc_els_free_iocb(phba, cmdiocb);
1909         return;
1910 }
1911
1912 /**
1913  * lpfc_issue_els_logo - Issue a logo to an node on a vport
1914  * @vport: pointer to a virtual N_Port data structure.
1915  * @ndlp: pointer to a node-list data structure.
1916  * @retry: number of retries to the command IOCB.
1917  *
1918  * This routine constructs and issues an ELS Logout (LOGO) iocb command
1919  * to a remote node, referred by an @ndlp on a @vport. It constructs the
1920  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
1921  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
1922  *
1923  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1924  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1925  * will be stored into the context1 field of the IOCB for the completion
1926  * callback function to the LOGO ELS command.
1927  *
1928  * Return code
1929  *   0 - successfully issued logo
1930  *   1 - failed to issue logo
1931  **/
1932 int
1933 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1934                     uint8_t retry)
1935 {
1936         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1937         struct lpfc_hba  *phba = vport->phba;
1938         IOCB_t *icmd;
1939         struct lpfc_iocbq *elsiocb;
1940         struct lpfc_sli_ring *pring;
1941         struct lpfc_sli *psli;
1942         uint8_t *pcmd;
1943         uint16_t cmdsize;
1944         int rc;
1945
1946         psli = &phba->sli;
1947         pring = &psli->ring[LPFC_ELS_RING];
1948
1949         spin_lock_irq(shost->host_lock);
1950         if (ndlp->nlp_flag & NLP_LOGO_SND) {
1951                 spin_unlock_irq(shost->host_lock);
1952                 return 0;
1953         }
1954         spin_unlock_irq(shost->host_lock);
1955
1956         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
1957         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1958                                      ndlp->nlp_DID, ELS_CMD_LOGO);
1959         if (!elsiocb)
1960                 return 1;
1961
1962         icmd = &elsiocb->iocb;
1963         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1964         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1965         pcmd += sizeof(uint32_t);
1966
1967         /* Fill in LOGO payload */
1968         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1969         pcmd += sizeof(uint32_t);
1970         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
1971
1972         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1973                 "Issue LOGO:      did:x%x",
1974                 ndlp->nlp_DID, 0, 0);
1975
1976         phba->fc_stat.elsXmitLOGO++;
1977         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
1978         spin_lock_irq(shost->host_lock);
1979         ndlp->nlp_flag |= NLP_LOGO_SND;
1980         spin_unlock_irq(shost->host_lock);
1981         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1982
1983         if (rc == IOCB_ERROR) {
1984                 spin_lock_irq(shost->host_lock);
1985                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
1986                 spin_unlock_irq(shost->host_lock);
1987                 lpfc_els_free_iocb(phba, elsiocb);
1988                 return 1;
1989         }
1990         return 0;
1991 }
1992
1993 /**
1994  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
1995  * @phba: pointer to lpfc hba data structure.
1996  * @cmdiocb: pointer to lpfc command iocb data structure.
1997  * @rspiocb: pointer to lpfc response iocb data structure.
1998  *
1999  * This routine is a generic completion callback function for ELS commands.
2000  * Specifically, it is the callback function which does not need to perform
2001  * any command specific operations. It is currently used by the ELS command
2002  * issuing routines for the ELS State Change  Request (SCR),
2003  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2004  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2005  * certain debug loggings, this callback function simply invokes the
2006  * lpfc_els_chk_latt() routine to check whether link went down during the
2007  * discovery process.
2008  **/
2009 static void
2010 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2011                   struct lpfc_iocbq *rspiocb)
2012 {
2013         struct lpfc_vport *vport = cmdiocb->vport;
2014         IOCB_t *irsp;
2015
2016         irsp = &rspiocb->iocb;
2017
2018         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2019                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
2020                 irsp->ulpStatus, irsp->un.ulpWord[4],
2021                 irsp->un.elsreq64.remoteID);
2022         /* ELS cmd tag <ulpIoTag> completes */
2023         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2024                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2025                          irsp->ulpIoTag, irsp->ulpStatus,
2026                          irsp->un.ulpWord[4], irsp->ulpTimeout);
2027         /* Check to see if link went down during discovery */
2028         lpfc_els_chk_latt(vport);
2029         lpfc_els_free_iocb(phba, cmdiocb);
2030         return;
2031 }
2032
2033 /**
2034  * lpfc_issue_els_scr - Issue a scr to an node on a vport
2035  * @vport: pointer to a host virtual N_Port data structure.
2036  * @nportid: N_Port identifier to the remote node.
2037  * @retry: number of retries to the command IOCB.
2038  *
2039  * This routine issues a State Change Request (SCR) to a fabric node
2040  * on a @vport. The remote node @nportid is passed into the function. It
2041  * first search the @vport node list to find the matching ndlp. If no such
2042  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2043  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2044  * routine is invoked to send the SCR IOCB.
2045  *
2046  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2047  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2048  * will be stored into the context1 field of the IOCB for the completion
2049  * callback function to the SCR ELS command.
2050  *
2051  * Return code
2052  *   0 - Successfully issued scr command
2053  *   1 - Failed to issue scr command
2054  **/
2055 int
2056 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2057 {
2058         struct lpfc_hba  *phba = vport->phba;
2059         IOCB_t *icmd;
2060         struct lpfc_iocbq *elsiocb;
2061         struct lpfc_sli_ring *pring;
2062         struct lpfc_sli *psli;
2063         uint8_t *pcmd;
2064         uint16_t cmdsize;
2065         struct lpfc_nodelist *ndlp;
2066
2067         psli = &phba->sli;
2068         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2069         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2070
2071         ndlp = lpfc_findnode_did(vport, nportid);
2072         if (!ndlp) {
2073                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2074                 if (!ndlp)
2075                         return 1;
2076                 lpfc_nlp_init(vport, ndlp, nportid);
2077                 lpfc_enqueue_node(vport, ndlp);
2078         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2079                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2080                 if (!ndlp)
2081                         return 1;
2082         }
2083
2084         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2085                                      ndlp->nlp_DID, ELS_CMD_SCR);
2086
2087         if (!elsiocb) {
2088                 /* This will trigger the release of the node just
2089                  * allocated
2090                  */
2091                 lpfc_nlp_put(ndlp);
2092                 return 1;
2093         }
2094
2095         icmd = &elsiocb->iocb;
2096         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2097
2098         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2099         pcmd += sizeof(uint32_t);
2100
2101         /* For SCR, remainder of payload is SCR parameter page */
2102         memset(pcmd, 0, sizeof(SCR));
2103         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2104
2105         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2106                 "Issue SCR:       did:x%x",
2107                 ndlp->nlp_DID, 0, 0);
2108
2109         phba->fc_stat.elsXmitSCR++;
2110         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2111         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2112                 /* The additional lpfc_nlp_put will cause the following
2113                  * lpfc_els_free_iocb routine to trigger the rlease of
2114                  * the node.
2115                  */
2116                 lpfc_nlp_put(ndlp);
2117                 lpfc_els_free_iocb(phba, elsiocb);
2118                 return 1;
2119         }
2120         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2121          * trigger the release of node.
2122          */
2123         lpfc_nlp_put(ndlp);
2124         return 0;
2125 }
2126
2127 /**
2128  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
2129  * @vport: pointer to a host virtual N_Port data structure.
2130  * @nportid: N_Port identifier to the remote node.
2131  * @retry: number of retries to the command IOCB.
2132  *
2133  * This routine issues a Fibre Channel Address Resolution Response
2134  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2135  * is passed into the function. It first search the @vport node list to find
2136  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2137  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2138  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2139  *
2140  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2141  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2142  * will be stored into the context1 field of the IOCB for the completion
2143  * callback function to the PARPR ELS command.
2144  *
2145  * Return code
2146  *   0 - Successfully issued farpr command
2147  *   1 - Failed to issue farpr command
2148  **/
2149 static int
2150 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2151 {
2152         struct lpfc_hba  *phba = vport->phba;
2153         IOCB_t *icmd;
2154         struct lpfc_iocbq *elsiocb;
2155         struct lpfc_sli_ring *pring;
2156         struct lpfc_sli *psli;
2157         FARP *fp;
2158         uint8_t *pcmd;
2159         uint32_t *lp;
2160         uint16_t cmdsize;
2161         struct lpfc_nodelist *ondlp;
2162         struct lpfc_nodelist *ndlp;
2163
2164         psli = &phba->sli;
2165         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2166         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2167
2168         ndlp = lpfc_findnode_did(vport, nportid);
2169         if (!ndlp) {
2170                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2171                 if (!ndlp)
2172                         return 1;
2173                 lpfc_nlp_init(vport, ndlp, nportid);
2174                 lpfc_enqueue_node(vport, ndlp);
2175         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2176                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2177                 if (!ndlp)
2178                         return 1;
2179         }
2180
2181         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2182                                      ndlp->nlp_DID, ELS_CMD_RNID);
2183         if (!elsiocb) {
2184                 /* This will trigger the release of the node just
2185                  * allocated
2186                  */
2187                 lpfc_nlp_put(ndlp);
2188                 return 1;
2189         }
2190
2191         icmd = &elsiocb->iocb;
2192         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2193
2194         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2195         pcmd += sizeof(uint32_t);
2196
2197         /* Fill in FARPR payload */
2198         fp = (FARP *) (pcmd);
2199         memset(fp, 0, sizeof(FARP));
2200         lp = (uint32_t *) pcmd;
2201         *lp++ = be32_to_cpu(nportid);
2202         *lp++ = be32_to_cpu(vport->fc_myDID);
2203         fp->Rflags = 0;
2204         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2205
2206         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2207         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2208         ondlp = lpfc_findnode_did(vport, nportid);
2209         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2210                 memcpy(&fp->OportName, &ondlp->nlp_portname,
2211                        sizeof(struct lpfc_name));
2212                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2213                        sizeof(struct lpfc_name));
2214         }
2215
2216         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2217                 "Issue FARPR:     did:x%x",
2218                 ndlp->nlp_DID, 0, 0);
2219
2220         phba->fc_stat.elsXmitFARPR++;
2221         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2222         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2223                 /* The additional lpfc_nlp_put will cause the following
2224                  * lpfc_els_free_iocb routine to trigger the release of
2225                  * the node.
2226                  */
2227                 lpfc_nlp_put(ndlp);
2228                 lpfc_els_free_iocb(phba, elsiocb);
2229                 return 1;
2230         }
2231         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2232          * trigger the release of the node.
2233          */
2234         lpfc_nlp_put(ndlp);
2235         return 0;
2236 }
2237
2238 /**
2239  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
2240  * @vport: pointer to a host virtual N_Port data structure.
2241  * @nlp: pointer to a node-list data structure.
2242  *
2243  * This routine cancels the timer with a delayed IOCB-command retry for
2244  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2245  * removes the ELS retry event if it presents. In addition, if the
2246  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2247  * commands are sent for the @vport's nodes that require issuing discovery
2248  * ADISC.
2249  **/
2250 void
2251 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2252 {
2253         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2254         struct lpfc_work_evt *evtp;
2255
2256         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2257                 return;
2258         spin_lock_irq(shost->host_lock);
2259         nlp->nlp_flag &= ~NLP_DELAY_TMO;
2260         spin_unlock_irq(shost->host_lock);
2261         del_timer_sync(&nlp->nlp_delayfunc);
2262         nlp->nlp_last_elscmd = 0;
2263         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2264                 list_del_init(&nlp->els_retry_evt.evt_listp);
2265                 /* Decrement nlp reference count held for the delayed retry */
2266                 evtp = &nlp->els_retry_evt;
2267                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2268         }
2269         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2270                 spin_lock_irq(shost->host_lock);
2271                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2272                 spin_unlock_irq(shost->host_lock);
2273                 if (vport->num_disc_nodes) {
2274                         if (vport->port_state < LPFC_VPORT_READY) {
2275                                 /* Check if there are more ADISCs to be sent */
2276                                 lpfc_more_adisc(vport);
2277                         } else {
2278                                 /* Check if there are more PLOGIs to be sent */
2279                                 lpfc_more_plogi(vport);
2280                                 if (vport->num_disc_nodes == 0) {
2281                                         spin_lock_irq(shost->host_lock);
2282                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
2283                                         spin_unlock_irq(shost->host_lock);
2284                                         lpfc_can_disctmo(vport);
2285                                         lpfc_end_rscn(vport);
2286                                 }
2287                         }
2288                 }
2289         }
2290         return;
2291 }
2292
2293 /**
2294  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
2295  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2296  *
2297  * This routine is invoked by the ndlp delayed-function timer to check
2298  * whether there is any pending ELS retry event(s) with the node. If not, it
2299  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2300  * adds the delayed events to the HBA work list and invokes the
2301  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2302  * event. Note that lpfc_nlp_get() is called before posting the event to
2303  * the work list to hold reference count of ndlp so that it guarantees the
2304  * reference to ndlp will still be available when the worker thread gets
2305  * to the event associated with the ndlp.
2306  **/
2307 void
2308 lpfc_els_retry_delay(unsigned long ptr)
2309 {
2310         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2311         struct lpfc_vport *vport = ndlp->vport;
2312         struct lpfc_hba   *phba = vport->phba;
2313         unsigned long flags;
2314         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2315
2316         spin_lock_irqsave(&phba->hbalock, flags);
2317         if (!list_empty(&evtp->evt_listp)) {
2318                 spin_unlock_irqrestore(&phba->hbalock, flags);
2319                 return;
2320         }
2321
2322         /* We need to hold the node by incrementing the reference
2323          * count until the queued work is done
2324          */
2325         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2326         if (evtp->evt_arg1) {
2327                 evtp->evt = LPFC_EVT_ELS_RETRY;
2328                 list_add_tail(&evtp->evt_listp, &phba->work_list);
2329                 lpfc_worker_wake_up(phba);
2330         }
2331         spin_unlock_irqrestore(&phba->hbalock, flags);
2332         return;
2333 }
2334
2335 /**
2336  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
2337  * @ndlp: pointer to a node-list data structure.
2338  *
2339  * This routine is the worker-thread handler for processing the @ndlp delayed
2340  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2341  * the last ELS command from the associated ndlp and invokes the proper ELS
2342  * function according to the delayed ELS command to retry the command.
2343  **/
2344 void
2345 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2346 {
2347         struct lpfc_vport *vport = ndlp->vport;
2348         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2349         uint32_t cmd, did, retry;
2350
2351         spin_lock_irq(shost->host_lock);
2352         did = ndlp->nlp_DID;
2353         cmd = ndlp->nlp_last_elscmd;
2354         ndlp->nlp_last_elscmd = 0;
2355
2356         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2357                 spin_unlock_irq(shost->host_lock);
2358                 return;
2359         }
2360
2361         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2362         spin_unlock_irq(shost->host_lock);
2363         /*
2364          * If a discovery event readded nlp_delayfunc after timer
2365          * firing and before processing the timer, cancel the
2366          * nlp_delayfunc.
2367          */
2368         del_timer_sync(&ndlp->nlp_delayfunc);
2369         retry = ndlp->nlp_retry;
2370
2371         switch (cmd) {
2372         case ELS_CMD_FLOGI:
2373                 lpfc_issue_els_flogi(vport, ndlp, retry);
2374                 break;
2375         case ELS_CMD_PLOGI:
2376                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2377                         ndlp->nlp_prev_state = ndlp->nlp_state;
2378                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2379                 }
2380                 break;
2381         case ELS_CMD_ADISC:
2382                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2383                         ndlp->nlp_prev_state = ndlp->nlp_state;
2384                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2385                 }
2386                 break;
2387         case ELS_CMD_PRLI:
2388                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2389                         ndlp->nlp_prev_state = ndlp->nlp_state;
2390                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2391                 }
2392                 break;
2393         case ELS_CMD_LOGO:
2394                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2395                         ndlp->nlp_prev_state = ndlp->nlp_state;
2396                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2397                 }
2398                 break;
2399         case ELS_CMD_FDISC:
2400                 lpfc_issue_els_fdisc(vport, ndlp, retry);
2401                 break;
2402         }
2403         return;
2404 }
2405
2406 /**
2407  * lpfc_els_retry - Make retry decision on an els command iocb
2408  * @phba: pointer to lpfc hba data structure.
2409  * @cmdiocb: pointer to lpfc command iocb data structure.
2410  * @rspiocb: pointer to lpfc response iocb data structure.
2411  *
2412  * This routine makes a retry decision on an ELS command IOCB, which has
2413  * failed. The following ELS IOCBs use this function for retrying the command
2414  * when previously issued command responsed with error status: FLOGI, PLOGI,
2415  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2416  * returned error status, it makes the decision whether a retry shall be
2417  * issued for the command, and whether a retry shall be made immediately or
2418  * delayed. In the former case, the corresponding ELS command issuing-function
2419  * is called to retry the command. In the later case, the ELS command shall
2420  * be posted to the ndlp delayed event and delayed function timer set to the
2421  * ndlp for the delayed command issusing.
2422  *
2423  * Return code
2424  *   0 - No retry of els command is made
2425  *   1 - Immediate or delayed retry of els command is made
2426  **/
2427 static int
2428 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2429                struct lpfc_iocbq *rspiocb)
2430 {
2431         struct lpfc_vport *vport = cmdiocb->vport;
2432         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2433         IOCB_t *irsp = &rspiocb->iocb;
2434         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2435         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2436         uint32_t *elscmd;
2437         struct ls_rjt stat;
2438         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2439         int logerr = 0;
2440         uint32_t cmd = 0;
2441         uint32_t did;
2442
2443
2444         /* Note: context2 may be 0 for internal driver abort
2445          * of delays ELS command.
2446          */
2447
2448         if (pcmd && pcmd->virt) {
2449                 elscmd = (uint32_t *) (pcmd->virt);
2450                 cmd = *elscmd++;
2451         }
2452
2453         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2454                 did = ndlp->nlp_DID;
2455         else {
2456                 /* We should only hit this case for retrying PLOGI */
2457                 did = irsp->un.elsreq64.remoteID;
2458                 ndlp = lpfc_findnode_did(vport, did);
2459                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2460                     && (cmd != ELS_CMD_PLOGI))
2461                         return 1;
2462         }
2463
2464         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2465                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2466                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2467
2468         switch (irsp->ulpStatus) {
2469         case IOSTAT_FCP_RSP_ERROR:
2470         case IOSTAT_REMOTE_STOP:
2471                 break;
2472
2473         case IOSTAT_LOCAL_REJECT:
2474                 switch ((irsp->un.ulpWord[4] & 0xff)) {
2475                 case IOERR_LOOP_OPEN_FAILURE:
2476                         if (cmd == ELS_CMD_FLOGI) {
2477                                 if (PCI_DEVICE_ID_HORNET ==
2478                                         phba->pcidev->device) {
2479                                         phba->fc_topology = TOPOLOGY_LOOP;
2480                                         phba->pport->fc_myDID = 0;
2481                                         phba->alpa_map[0] = 0;
2482                                         phba->alpa_map[1] = 0;
2483                                 }
2484                         }
2485                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2486                                 delay = 1000;
2487                         retry = 1;
2488                         break;
2489
2490                 case IOERR_ILLEGAL_COMMAND:
2491                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2492                                          "0124 Retry illegal cmd x%x "
2493                                          "retry:x%x delay:x%x\n",
2494                                          cmd, cmdiocb->retry, delay);
2495                         retry = 1;
2496                         /* All command's retry policy */
2497                         maxretry = 8;
2498                         if (cmdiocb->retry > 2)
2499                                 delay = 1000;
2500                         break;
2501
2502                 case IOERR_NO_RESOURCES:
2503                         logerr = 1; /* HBA out of resources */
2504                         retry = 1;
2505                         if (cmdiocb->retry > 100)
2506                                 delay = 100;
2507                         maxretry = 250;
2508                         break;
2509
2510                 case IOERR_ILLEGAL_FRAME:
2511                         delay = 100;
2512                         retry = 1;
2513                         break;
2514
2515                 case IOERR_SEQUENCE_TIMEOUT:
2516                 case IOERR_INVALID_RPI:
2517                         retry = 1;
2518                         break;
2519                 }
2520                 break;
2521
2522         case IOSTAT_NPORT_RJT:
2523         case IOSTAT_FABRIC_RJT:
2524                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2525                         retry = 1;
2526                         break;
2527                 }
2528                 break;
2529
2530         case IOSTAT_NPORT_BSY:
2531         case IOSTAT_FABRIC_BSY:
2532                 logerr = 1; /* Fabric / Remote NPort out of resources */
2533                 retry = 1;
2534                 break;
2535
2536         case IOSTAT_LS_RJT:
2537                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2538                 /* Added for Vendor specifc support
2539                  * Just keep retrying for these Rsn / Exp codes
2540                  */
2541                 switch (stat.un.b.lsRjtRsnCode) {
2542                 case LSRJT_UNABLE_TPC:
2543                         if (stat.un.b.lsRjtRsnCodeExp ==
2544                             LSEXP_CMD_IN_PROGRESS) {
2545                                 if (cmd == ELS_CMD_PLOGI) {
2546                                         delay = 1000;
2547                                         maxretry = 48;
2548                                 }
2549                                 retry = 1;
2550                                 break;
2551                         }
2552                         if (cmd == ELS_CMD_PLOGI) {
2553                                 delay = 1000;
2554                                 maxretry = lpfc_max_els_tries + 1;
2555                                 retry = 1;
2556                                 break;
2557                         }
2558                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2559                           (cmd == ELS_CMD_FDISC) &&
2560                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2561                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2562                                                  "0125 FDISC Failed (x%x). "
2563                                                  "Fabric out of resources\n",
2564                                                  stat.un.lsRjtError);
2565                                 lpfc_vport_set_state(vport,
2566                                                      FC_VPORT_NO_FABRIC_RSCS);
2567                         }
2568                         break;
2569
2570                 case LSRJT_LOGICAL_BSY:
2571                         if ((cmd == ELS_CMD_PLOGI) ||
2572                             (cmd == ELS_CMD_PRLI)) {
2573                                 delay = 1000;
2574                                 maxretry = 48;
2575                         } else if (cmd == ELS_CMD_FDISC) {
2576                                 /* FDISC retry policy */
2577                                 maxretry = 48;
2578                                 if (cmdiocb->retry >= 32)
2579                                         delay = 1000;
2580                         }
2581                         retry = 1;
2582                         break;
2583
2584                 case LSRJT_LOGICAL_ERR:
2585                         /* There are some cases where switches return this
2586                          * error when they are not ready and should be returning
2587                          * Logical Busy. We should delay every time.
2588                          */
2589                         if (cmd == ELS_CMD_FDISC &&
2590                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2591                                 maxretry = 3;
2592                                 delay = 1000;
2593                                 retry = 1;
2594                                 break;
2595                         }
2596                 case LSRJT_PROTOCOL_ERR:
2597                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2598                           (cmd == ELS_CMD_FDISC) &&
2599                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2600                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2601                           ) {
2602                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2603                                                  "0122 FDISC Failed (x%x). "
2604                                                  "Fabric Detected Bad WWN\n",
2605                                                  stat.un.lsRjtError);
2606                                 lpfc_vport_set_state(vport,
2607                                                      FC_VPORT_FABRIC_REJ_WWN);
2608                         }
2609                         break;
2610                 }
2611                 break;
2612
2613         case IOSTAT_INTERMED_RSP:
2614         case IOSTAT_BA_RJT:
2615                 break;
2616
2617         default:
2618                 break;
2619         }
2620
2621         if (did == FDMI_DID)
2622                 retry = 1;
2623
2624         if ((cmd == ELS_CMD_FLOGI) &&
2625             (phba->fc_topology != TOPOLOGY_LOOP) &&
2626             !lpfc_error_lost_link(irsp)) {
2627                 /* FLOGI retry policy */
2628                 retry = 1;
2629                 maxretry = 48;
2630                 if (cmdiocb->retry >= 32)
2631                         delay = 1000;
2632         }
2633
2634         if ((++cmdiocb->retry) >= maxretry) {
2635                 phba->fc_stat.elsRetryExceeded++;
2636                 retry = 0;
2637         }
2638
2639         if ((vport->load_flag & FC_UNLOADING) != 0)
2640                 retry = 0;
2641
2642         if (retry) {
2643
2644                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
2645                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2646                                  "0107 Retry ELS command x%x to remote "
2647                                  "NPORT x%x Data: x%x x%x\n",
2648                                  cmd, did, cmdiocb->retry, delay);
2649
2650                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2651                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2652                         ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2653                         /* Don't reset timer for no resources */
2654
2655                         /* If discovery / RSCN timer is running, reset it */
2656                         if (timer_pending(&vport->fc_disctmo) ||
2657                             (vport->fc_flag & FC_RSCN_MODE))
2658                                 lpfc_set_disctmo(vport);
2659                 }
2660
2661                 phba->fc_stat.elsXmitRetry++;
2662                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2663                         phba->fc_stat.elsDelayRetry++;
2664                         ndlp->nlp_retry = cmdiocb->retry;
2665
2666                         /* delay is specified in milliseconds */
2667                         mod_timer(&ndlp->nlp_delayfunc,
2668                                 jiffies + msecs_to_jiffies(delay));
2669                         spin_lock_irq(shost->host_lock);
2670                         ndlp->nlp_flag |= NLP_DELAY_TMO;
2671                         spin_unlock_irq(shost->host_lock);
2672
2673                         ndlp->nlp_prev_state = ndlp->nlp_state;
2674                         if (cmd == ELS_CMD_PRLI)
2675                                 lpfc_nlp_set_state(vport, ndlp,
2676                                         NLP_STE_REG_LOGIN_ISSUE);
2677                         else
2678                                 lpfc_nlp_set_state(vport, ndlp,
2679                                         NLP_STE_NPR_NODE);
2680                         ndlp->nlp_last_elscmd = cmd;
2681
2682                         return 1;
2683                 }
2684                 switch (cmd) {
2685                 case ELS_CMD_FLOGI:
2686                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2687                         return 1;
2688                 case ELS_CMD_FDISC:
2689                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2690                         return 1;
2691                 case ELS_CMD_PLOGI:
2692                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2693                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2694                                 lpfc_nlp_set_state(vport, ndlp,
2695                                                    NLP_STE_PLOGI_ISSUE);
2696                         }
2697                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2698                         return 1;
2699                 case ELS_CMD_ADISC:
2700                         ndlp->nlp_prev_state = ndlp->nlp_state;
2701                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2702                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2703                         return 1;
2704                 case ELS_CMD_PRLI:
2705                         ndlp->nlp_prev_state = ndlp->nlp_state;
2706                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2707                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2708                         return 1;
2709                 case ELS_CMD_LOGO:
2710                         ndlp->nlp_prev_state = ndlp->nlp_state;
2711                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2712                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2713                         return 1;
2714                 }
2715         }
2716         /* No retry ELS command <elsCmd> to remote NPORT <did> */
2717         if (logerr) {
2718                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2719                          "0137 No retry ELS command x%x to remote "
2720                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2721                          cmd, did, irsp->ulpStatus,
2722                          irsp->un.ulpWord[4]);
2723         }
2724         else {
2725                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2726                          "0108 No retry ELS command x%x to remote "
2727                          "NPORT x%x Retried:%d Error:x%x/%x\n",
2728                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
2729                          irsp->un.ulpWord[4]);
2730         }
2731         return 0;
2732 }
2733
2734 /**
2735  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
2736  * @phba: pointer to lpfc hba data structure.
2737  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
2738  *
2739  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
2740  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
2741  * checks to see whether there is a lpfc DMA buffer associated with the
2742  * response of the command IOCB. If so, it will be released before releasing
2743  * the lpfc DMA buffer associated with the IOCB itself.
2744  *
2745  * Return code
2746  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2747  **/
2748 static int
2749 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
2750 {
2751         struct lpfc_dmabuf *buf_ptr;
2752
2753         /* Free the response before processing the command. */
2754         if (!list_empty(&buf_ptr1->list)) {
2755                 list_remove_head(&buf_ptr1->list, buf_ptr,
2756                                  struct lpfc_dmabuf,
2757                                  list);
2758                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2759                 kfree(buf_ptr);
2760         }
2761         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2762         kfree(buf_ptr1);
2763         return 0;
2764 }
2765
2766 /**
2767  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
2768  * @phba: pointer to lpfc hba data structure.
2769  * @buf_ptr: pointer to the lpfc dma buffer data structure.
2770  *
2771  * This routine releases the lpfc Direct Memory Access (DMA) buffer
2772  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
2773  * pool.
2774  *
2775  * Return code
2776  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2777  **/
2778 static int
2779 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
2780 {
2781         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2782         kfree(buf_ptr);
2783         return 0;
2784 }
2785
2786 /**
2787  * lpfc_els_free_iocb - Free a command iocb and its associated resources
2788  * @phba: pointer to lpfc hba data structure.
2789  * @elsiocb: pointer to lpfc els command iocb data structure.
2790  *
2791  * This routine frees a command IOCB and its associated resources. The
2792  * command IOCB data structure contains the reference to various associated
2793  * resources, these fields must be set to NULL if the associated reference
2794  * not present:
2795  *   context1 - reference to ndlp
2796  *   context2 - reference to cmd
2797  *   context2->next - reference to rsp
2798  *   context3 - reference to bpl
2799  *
2800  * It first properly decrements the reference count held on ndlp for the
2801  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
2802  * set, it invokes the lpfc_els_free_data() routine to release the Direct
2803  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
2804  * adds the DMA buffer the @phba data structure for the delayed release.
2805  * If reference to the Buffer Pointer List (BPL) is present, the
2806  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
2807  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
2808  * invoked to release the IOCB data structure back to @phba IOCBQ list.
2809  *
2810  * Return code
2811  *   0 - Success (currently, always return 0)
2812  **/
2813 int
2814 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
2815 {
2816         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2817         struct lpfc_nodelist *ndlp;
2818
2819         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
2820         if (ndlp) {
2821                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
2822                         lpfc_nlp_put(ndlp);
2823
2824                         /* If the ndlp is not being used by another discovery
2825                          * thread, free it.
2826                          */
2827                         if (!lpfc_nlp_not_used(ndlp)) {
2828                                 /* If ndlp is being used by another discovery
2829                                  * thread, just clear NLP_DEFER_RM
2830                                  */
2831                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
2832                         }
2833                 }
2834                 else
2835                         lpfc_nlp_put(ndlp);
2836                 elsiocb->context1 = NULL;
2837         }
2838         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
2839         if (elsiocb->context2) {
2840                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
2841                         /* Firmware could still be in progress of DMAing
2842                          * payload, so don't free data buffer till after
2843                          * a hbeat.
2844                          */
2845                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
2846                         buf_ptr = elsiocb->context2;
2847                         elsiocb->context2 = NULL;
2848                         if (buf_ptr) {
2849                                 buf_ptr1 = NULL;
2850                                 spin_lock_irq(&phba->hbalock);
2851                                 if (!list_empty(&buf_ptr->list)) {
2852                                         list_remove_head(&buf_ptr->list,
2853                                                 buf_ptr1, struct lpfc_dmabuf,
2854                                                 list);
2855                                         INIT_LIST_HEAD(&buf_ptr1->list);
2856                                         list_add_tail(&buf_ptr1->list,
2857                                                 &phba->elsbuf);
2858                                         phba->elsbuf_cnt++;
2859                                 }
2860                                 INIT_LIST_HEAD(&buf_ptr->list);
2861                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
2862                                 phba->elsbuf_cnt++;
2863                                 spin_unlock_irq(&phba->hbalock);
2864                         }
2865                 } else {
2866                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2867                         lpfc_els_free_data(phba, buf_ptr1);
2868                 }
2869         }
2870
2871         if (elsiocb->context3) {
2872                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2873                 lpfc_els_free_bpl(phba, buf_ptr);
2874         }
2875         lpfc_sli_release_iocbq(phba, elsiocb);
2876         return 0;
2877 }
2878
2879 /**
2880  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
2881  * @phba: pointer to lpfc hba data structure.
2882  * @cmdiocb: pointer to lpfc command iocb data structure.
2883  * @rspiocb: pointer to lpfc response iocb data structure.
2884  *
2885  * This routine is the completion callback function to the Logout (LOGO)
2886  * Accept (ACC) Response ELS command. This routine is invoked to indicate
2887  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
2888  * release the ndlp if it has the last reference remaining (reference count
2889  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
2890  * field to NULL to inform the following lpfc_els_free_iocb() routine no
2891  * ndlp reference count needs to be decremented. Otherwise, the ndlp
2892  * reference use-count shall be decremented by the lpfc_els_free_iocb()
2893  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
2894  * IOCB data structure.
2895  **/
2896 static void
2897 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2898                        struct lpfc_iocbq *rspiocb)
2899 {
2900         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2901         struct lpfc_vport *vport = cmdiocb->vport;
2902         IOCB_t *irsp;
2903
2904         irsp = &rspiocb->iocb;
2905         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2906                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
2907                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2908         /* ACC to LOGO completes to NPort <nlp_DID> */
2909         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2910                          "0109 ACC to LOGO completes to NPort x%x "
2911                          "Data: x%x x%x x%x\n",
2912                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2913                          ndlp->nlp_rpi);
2914
2915         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
2916                 /* NPort Recovery mode or node is just allocated */
2917                 if (!lpfc_nlp_not_used(ndlp)) {
2918                         /* If the ndlp is being used by another discovery
2919                          * thread, just unregister the RPI.
2920                          */
2921                         lpfc_unreg_rpi(vport, ndlp);
2922                 } else {
2923                         /* Indicate the node has already released, should
2924                          * not reference to it from within lpfc_els_free_iocb.
2925                          */
2926                         cmdiocb->context1 = NULL;
2927                 }
2928         }
2929         lpfc_els_free_iocb(phba, cmdiocb);
2930         return;
2931 }
2932
2933 /**
2934  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
2935  * @phba: pointer to lpfc hba data structure.
2936  * @pmb: pointer to the driver internal queue element for mailbox command.
2937  *
2938  * This routine is the completion callback function for unregister default
2939  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
2940  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
2941  * decrements the ndlp reference count held for this completion callback
2942  * function. After that, it invokes the lpfc_nlp_not_used() to check
2943  * whether there is only one reference left on the ndlp. If so, it will
2944  * perform one more decrement and trigger the release of the ndlp.
2945  **/
2946 void
2947 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2948 {
2949         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2950         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2951
2952         pmb->context1 = NULL;
2953         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2954         kfree(mp);
2955         mempool_free(pmb, phba->mbox_mem_pool);
2956         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2957                 lpfc_nlp_put(ndlp);
2958                 /* This is the end of the default RPI cleanup logic for this
2959                  * ndlp. If no other discovery threads are using this ndlp.
2960                  * we should free all resources associated with it.
2961                  */
2962                 lpfc_nlp_not_used(ndlp);
2963         }
2964         return;
2965 }
2966
2967 /**
2968  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
2969  * @phba: pointer to lpfc hba data structure.
2970  * @cmdiocb: pointer to lpfc command iocb data structure.
2971  * @rspiocb: pointer to lpfc response iocb data structure.
2972  *
2973  * This routine is the completion callback function for ELS Response IOCB
2974  * command. In normal case, this callback function just properly sets the
2975  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
2976  * field in the command IOCB is not NULL, the referred mailbox command will
2977  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
2978  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
2979  * link down event occurred during the discovery, the lpfc_nlp_not_used()
2980  * routine shall be invoked trying to release the ndlp if no other threads
2981  * are currently referring it.
2982  **/
2983 static void
2984 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2985                   struct lpfc_iocbq *rspiocb)
2986 {
2987         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2988         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
2989         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
2990         IOCB_t  *irsp;
2991         uint8_t *pcmd;
2992         LPFC_MBOXQ_t *mbox = NULL;
2993         struct lpfc_dmabuf *mp = NULL;
2994         uint32_t ls_rjt = 0;
2995
2996         irsp = &rspiocb->iocb;
2997
2998         if (cmdiocb->context_un.mbox)
2999                 mbox = cmdiocb->context_un.mbox;
3000
3001         /* First determine if this is a LS_RJT cmpl. Note, this callback
3002          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3003          */
3004         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
3005         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3006             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
3007                 /* A LS_RJT associated with Default RPI cleanup has its own
3008                  * seperate code path.
3009                  */
3010                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3011                         ls_rjt = 1;
3012         }
3013
3014         /* Check to see if link went down during discovery */
3015         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3016                 if (mbox) {
3017                         mp = (struct lpfc_dmabuf *) mbox->context1;
3018                         if (mp) {
3019                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3020                                 kfree(mp);
3021                         }
3022                         mempool_free(mbox, phba->mbox_mem_pool);
3023                 }
3024                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3025                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3026                         if (lpfc_nlp_not_used(ndlp)) {
3027                                 ndlp = NULL;
3028                                 /* Indicate the node has already released,
3029                                  * should not reference to it from within
3030                                  * the routine lpfc_els_free_iocb.
3031                                  */
3032                                 cmdiocb->context1 = NULL;
3033                         }
3034                 goto out;
3035         }
3036
3037         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3038                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
3039                 irsp->ulpStatus, irsp->un.ulpWord[4],
3040                 cmdiocb->iocb.un.elsreq64.remoteID);
3041         /* ELS response tag <ulpIoTag> completes */
3042         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3043                          "0110 ELS response tag x%x completes "
3044                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3045                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3046                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,