[SCSI] lpfc 8.2.2 : Rework the lpfc_printf_log() macro
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / lpfc / lpfc_els.c
blob9365e19696e2167e179a18e428b43b57168cdd82
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2007 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 *******************************************************************/
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
31 #include "lpfc_hw.h"
32 #include "lpfc_sli.h"
33 #include "lpfc_disc.h"
34 #include "lpfc_scsi.h"
35 #include "lpfc.h"
36 #include "lpfc_logmsg.h"
37 #include "lpfc_crtn.h"
38 #include "lpfc_vport.h"
39 #include "lpfc_debugfs.h"
41 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
42 struct lpfc_iocbq *);
43 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
44 struct lpfc_iocbq *);
46 static int lpfc_max_els_tries = 3;
48 int
49 lpfc_els_chk_latt(struct lpfc_vport *vport)
51 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
52 struct lpfc_hba *phba = vport->phba;
53 uint32_t ha_copy;
55 if (vport->port_state >= LPFC_VPORT_READY ||
56 phba->link_state == LPFC_LINK_DOWN)
57 return 0;
59 /* Read the HBA Host Attention Register */
60 ha_copy = readl(phba->HAregaddr);
62 if (!(ha_copy & HA_LATT))
63 return 0;
65 /* Pending Link Event during Discovery */
66 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
67 "0237 Pending Link Event during "
68 "Discovery: State x%x\n",
69 phba->pport->port_state);
71 /* CLEAR_LA should re-enable link attention events and
72 * we should then imediately take a LATT event. The
73 * LATT processing should call lpfc_linkdown() which
74 * will cleanup any left over in-progress discovery
75 * events.
77 spin_lock_irq(shost->host_lock);
78 vport->fc_flag |= FC_ABORT_DISCOVERY;
79 spin_unlock_irq(shost->host_lock);
81 if (phba->link_state != LPFC_CLEAR_LA)
82 lpfc_issue_clear_la(phba, vport);
84 return 1;
87 static struct lpfc_iocbq *
88 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
89 uint16_t cmdSize, uint8_t retry,
90 struct lpfc_nodelist *ndlp, uint32_t did,
91 uint32_t elscmd)
93 struct lpfc_hba *phba = vport->phba;
94 struct lpfc_iocbq *elsiocb;
95 struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
96 struct ulp_bde64 *bpl;
97 IOCB_t *icmd;
100 if (!lpfc_is_link_up(phba))
101 return NULL;
103 /* Allocate buffer for command iocb */
104 elsiocb = lpfc_sli_get_iocbq(phba);
106 if (elsiocb == NULL)
107 return NULL;
108 icmd = &elsiocb->iocb;
110 /* fill in BDEs for command */
111 /* Allocate buffer for command payload */
112 if (((pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL)) == 0) ||
113 ((pcmd->virt = lpfc_mbuf_alloc(phba,
114 MEM_PRI, &(pcmd->phys))) == 0)) {
115 kfree(pcmd);
117 lpfc_sli_release_iocbq(phba, elsiocb);
118 return NULL;
121 INIT_LIST_HEAD(&pcmd->list);
123 /* Allocate buffer for response payload */
124 if (expectRsp) {
125 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
126 if (prsp)
127 prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
128 &prsp->phys);
129 if (prsp == 0 || prsp->virt == 0) {
130 kfree(prsp);
131 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
132 kfree(pcmd);
133 lpfc_sli_release_iocbq(phba, elsiocb);
134 return NULL;
136 INIT_LIST_HEAD(&prsp->list);
137 } else {
138 prsp = NULL;
141 /* Allocate buffer for Buffer ptr list */
142 pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
143 if (pbuflist)
144 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
145 &pbuflist->phys);
146 if (pbuflist == 0 || pbuflist->virt == 0) {
147 lpfc_sli_release_iocbq(phba, elsiocb);
148 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
149 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
150 kfree(pcmd);
151 kfree(prsp);
152 kfree(pbuflist);
153 return NULL;
156 INIT_LIST_HEAD(&pbuflist->list);
158 icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
159 icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
160 icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
161 icmd->un.elsreq64.remoteID = did; /* DID */
162 if (expectRsp) {
163 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
164 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
165 icmd->ulpTimeout = phba->fc_ratov * 2;
166 } else {
167 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
168 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
170 icmd->ulpBdeCount = 1;
171 icmd->ulpLe = 1;
172 icmd->ulpClass = CLASS3;
174 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
175 icmd->un.elsreq64.myID = vport->fc_myDID;
177 /* For ELS_REQUEST64_CR, use the VPI by default */
178 icmd->ulpContext = vport->vpi;
179 icmd->ulpCt_h = 0;
180 icmd->ulpCt_l = 1;
183 bpl = (struct ulp_bde64 *) pbuflist->virt;
184 bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
185 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
186 bpl->tus.f.bdeSize = cmdSize;
187 bpl->tus.f.bdeFlags = 0;
188 bpl->tus.w = le32_to_cpu(bpl->tus.w);
190 if (expectRsp) {
191 bpl++;
192 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
193 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
194 bpl->tus.f.bdeSize = FCELSSIZE;
195 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
196 bpl->tus.w = le32_to_cpu(bpl->tus.w);
199 /* Save for completion so we can release these resources */
200 if (elscmd != ELS_CMD_LS_RJT)
201 elsiocb->context1 = lpfc_nlp_get(ndlp);
202 elsiocb->context2 = pcmd;
203 elsiocb->context3 = pbuflist;
204 elsiocb->retry = retry;
205 elsiocb->vport = vport;
206 elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
208 if (prsp) {
209 list_add(&prsp->list, &pcmd->list);
211 if (expectRsp) {
212 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
213 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
214 "0116 Xmit ELS command x%x to remote "
215 "NPORT x%x I/O tag: x%x, port state: x%x\n",
216 elscmd, did, elsiocb->iotag,
217 vport->port_state);
218 } else {
219 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
220 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
221 "0117 Xmit ELS response x%x to remote "
222 "NPORT x%x I/O tag: x%x, size: x%x\n",
223 elscmd, ndlp->nlp_DID, elsiocb->iotag,
224 cmdSize);
226 return elsiocb;
230 static int
231 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
233 struct lpfc_hba *phba = vport->phba;
234 LPFC_MBOXQ_t *mbox;
235 struct lpfc_dmabuf *mp;
236 struct lpfc_nodelist *ndlp;
237 struct serv_parm *sp;
238 int rc;
240 sp = &phba->fc_fabparam;
241 ndlp = lpfc_findnode_did(vport, Fabric_DID);
242 if (!ndlp)
243 goto fail;
245 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
246 if (!mbox)
247 goto fail;
249 vport->port_state = LPFC_FABRIC_CFG_LINK;
250 lpfc_config_link(phba, mbox);
251 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
252 mbox->vport = vport;
254 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
255 if (rc == MBX_NOT_FINISHED)
256 goto fail_free_mbox;
258 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
259 if (!mbox)
260 goto fail;
261 rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
263 if (rc)
264 goto fail_free_mbox;
266 mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
267 mbox->vport = vport;
268 mbox->context2 = lpfc_nlp_get(ndlp);
270 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
271 if (rc == MBX_NOT_FINISHED)
272 goto fail_issue_reg_login;
274 return 0;
276 fail_issue_reg_login:
277 lpfc_nlp_put(ndlp);
278 mp = (struct lpfc_dmabuf *) mbox->context1;
279 lpfc_mbuf_free(phba, mp->virt, mp->phys);
280 kfree(mp);
281 fail_free_mbox:
282 mempool_free(mbox, phba->mbox_mem_pool);
284 fail:
285 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
286 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
287 "0249 Cannot issue Register Fabric login\n");
288 return -ENXIO;
291 static int
292 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
293 struct serv_parm *sp, IOCB_t *irsp)
295 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
296 struct lpfc_hba *phba = vport->phba;
297 struct lpfc_nodelist *np;
298 struct lpfc_nodelist *next_np;
300 spin_lock_irq(shost->host_lock);
301 vport->fc_flag |= FC_FABRIC;
302 spin_unlock_irq(shost->host_lock);
304 phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
305 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
306 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
308 phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
310 if (phba->fc_topology == TOPOLOGY_LOOP) {
311 spin_lock_irq(shost->host_lock);
312 vport->fc_flag |= FC_PUBLIC_LOOP;
313 spin_unlock_irq(shost->host_lock);
314 } else {
316 * If we are a N-port connected to a Fabric, fixup sparam's so
317 * logins to devices on remote loops work.
319 vport->fc_sparam.cmn.altBbCredit = 1;
322 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
323 memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
324 memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
325 ndlp->nlp_class_sup = 0;
326 if (sp->cls1.classValid)
327 ndlp->nlp_class_sup |= FC_COS_CLASS1;
328 if (sp->cls2.classValid)
329 ndlp->nlp_class_sup |= FC_COS_CLASS2;
330 if (sp->cls3.classValid)
331 ndlp->nlp_class_sup |= FC_COS_CLASS3;
332 if (sp->cls4.classValid)
333 ndlp->nlp_class_sup |= FC_COS_CLASS4;
334 ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
335 sp->cmn.bbRcvSizeLsb;
336 memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
338 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
339 if (sp->cmn.response_multiple_NPort) {
340 lpfc_printf_vlog(vport, KERN_WARNING,
341 LOG_ELS | LOG_VPORT,
342 "1816 FLOGI NPIV supported, "
343 "response data 0x%x\n",
344 sp->cmn.response_multiple_NPort);
345 phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
346 } else {
347 /* Because we asked f/w for NPIV it still expects us
348 to call reg_vnpid atleast for the physcial host */
349 lpfc_printf_vlog(vport, KERN_WARNING,
350 LOG_ELS | LOG_VPORT,
351 "1817 Fabric does not support NPIV "
352 "- configuring single port mode.\n");
353 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
357 if ((vport->fc_prevDID != vport->fc_myDID) &&
358 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
360 /* If our NportID changed, we need to ensure all
361 * remaining NPORTs get unreg_login'ed.
363 list_for_each_entry_safe(np, next_np,
364 &vport->fc_nodes, nlp_listp) {
365 if ((np->nlp_state != NLP_STE_NPR_NODE) ||
366 !(np->nlp_flag & NLP_NPR_ADISC))
367 continue;
368 spin_lock_irq(shost->host_lock);
369 np->nlp_flag &= ~NLP_NPR_ADISC;
370 spin_unlock_irq(shost->host_lock);
371 lpfc_unreg_rpi(vport, np);
373 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
374 lpfc_mbx_unreg_vpi(vport);
375 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
379 ndlp->nlp_sid = irsp->un.ulpWord[4] & Mask_DID;
380 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
382 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
383 vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
384 lpfc_register_new_vport(phba, vport, ndlp);
385 return 0;
387 lpfc_issue_fabric_reglogin(vport);
388 return 0;
392 * We FLOGIed into an NPort, initiate pt2pt protocol
394 static int
395 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
396 struct serv_parm *sp)
398 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
399 struct lpfc_hba *phba = vport->phba;
400 LPFC_MBOXQ_t *mbox;
401 int rc;
403 spin_lock_irq(shost->host_lock);
404 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
405 spin_unlock_irq(shost->host_lock);
407 phba->fc_edtov = FF_DEF_EDTOV;
408 phba->fc_ratov = FF_DEF_RATOV;
409 rc = memcmp(&vport->fc_portname, &sp->portName,
410 sizeof(vport->fc_portname));
411 if (rc >= 0) {
412 /* This side will initiate the PLOGI */
413 spin_lock_irq(shost->host_lock);
414 vport->fc_flag |= FC_PT2PT_PLOGI;
415 spin_unlock_irq(shost->host_lock);
418 * N_Port ID cannot be 0, set our to LocalID the other
419 * side will be RemoteID.
422 /* not equal */
423 if (rc)
424 vport->fc_myDID = PT2PT_LocalID;
426 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
427 if (!mbox)
428 goto fail;
430 lpfc_config_link(phba, mbox);
432 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
433 mbox->vport = vport;
434 rc = lpfc_sli_issue_mbox(phba, mbox,
435 MBX_NOWAIT | MBX_STOP_IOCB);
436 if (rc == MBX_NOT_FINISHED) {
437 mempool_free(mbox, phba->mbox_mem_pool);
438 goto fail;
440 lpfc_nlp_put(ndlp);
442 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
443 if (!ndlp) {
445 * Cannot find existing Fabric ndlp, so allocate a
446 * new one
448 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
449 if (!ndlp)
450 goto fail;
452 lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
455 memcpy(&ndlp->nlp_portname, &sp->portName,
456 sizeof(struct lpfc_name));
457 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
458 sizeof(struct lpfc_name));
459 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
460 spin_lock_irq(shost->host_lock);
461 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
462 spin_unlock_irq(shost->host_lock);
463 } else {
464 /* This side will wait for the PLOGI */
465 lpfc_nlp_put(ndlp);
468 spin_lock_irq(shost->host_lock);
469 vport->fc_flag |= FC_PT2PT;
470 spin_unlock_irq(shost->host_lock);
472 /* Start discovery - this should just do CLEAR_LA */
473 lpfc_disc_start(vport);
474 return 0;
475 fail:
476 return -ENXIO;
479 static void
480 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
481 struct lpfc_iocbq *rspiocb)
483 struct lpfc_vport *vport = cmdiocb->vport;
484 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
485 IOCB_t *irsp = &rspiocb->iocb;
486 struct lpfc_nodelist *ndlp = cmdiocb->context1;
487 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
488 struct serv_parm *sp;
489 int rc;
491 /* Check to see if link went down during discovery */
492 if (lpfc_els_chk_latt(vport)) {
493 lpfc_nlp_put(ndlp);
494 goto out;
497 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
498 "FLOGI cmpl: status:x%x/x%x state:x%x",
499 irsp->ulpStatus, irsp->un.ulpWord[4],
500 vport->port_state);
502 if (irsp->ulpStatus) {
503 /* Check for retry */
504 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
505 goto out;
507 /* FLOGI failed, so there is no fabric */
508 spin_lock_irq(shost->host_lock);
509 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
510 spin_unlock_irq(shost->host_lock);
512 /* If private loop, then allow max outstanding els to be
513 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
514 * alpa map would take too long otherwise.
516 if (phba->alpa_map[0] == 0) {
517 vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
520 /* FLOGI failure */
521 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
522 "0100 FLOGI failure Data: x%x x%x "
523 "x%x\n",
524 irsp->ulpStatus, irsp->un.ulpWord[4],
525 irsp->ulpTimeout);
526 goto flogifail;
530 * The FLogI succeeded. Sync the data for the CPU before
531 * accessing it.
533 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
535 sp = prsp->virt + sizeof(uint32_t);
537 /* FLOGI completes successfully */
538 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
539 "0101 FLOGI completes sucessfully "
540 "Data: x%x x%x x%x x%x\n",
541 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
542 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
544 if (vport->port_state == LPFC_FLOGI) {
546 * If Common Service Parameters indicate Nport
547 * we are point to point, if Fport we are Fabric.
549 if (sp->cmn.fPort)
550 rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
551 else
552 rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
554 if (!rc)
555 goto out;
558 flogifail:
559 lpfc_nlp_put(ndlp);
561 if (!lpfc_error_lost_link(irsp)) {
562 /* FLOGI failed, so just use loop map to make discovery list */
563 lpfc_disc_list_loopmap(vport);
565 /* Start discovery */
566 lpfc_disc_start(vport);
569 out:
570 lpfc_els_free_iocb(phba, cmdiocb);
573 static int
574 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
575 uint8_t retry)
577 struct lpfc_hba *phba = vport->phba;
578 struct serv_parm *sp;
579 IOCB_t *icmd;
580 struct lpfc_iocbq *elsiocb;
581 struct lpfc_sli_ring *pring;
582 uint8_t *pcmd;
583 uint16_t cmdsize;
584 uint32_t tmo;
585 int rc;
587 pring = &phba->sli.ring[LPFC_ELS_RING];
589 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
590 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
591 ndlp->nlp_DID, ELS_CMD_FLOGI);
593 if (!elsiocb)
594 return 1;
596 icmd = &elsiocb->iocb;
597 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
599 /* For FLOGI request, remainder of payload is service parameters */
600 *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
601 pcmd += sizeof(uint32_t);
602 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
603 sp = (struct serv_parm *) pcmd;
605 /* Setup CSPs accordingly for Fabric */
606 sp->cmn.e_d_tov = 0;
607 sp->cmn.w2.r_a_tov = 0;
608 sp->cls1.classValid = 0;
609 sp->cls2.seqDelivery = 1;
610 sp->cls3.seqDelivery = 1;
611 if (sp->cmn.fcphLow < FC_PH3)
612 sp->cmn.fcphLow = FC_PH3;
613 if (sp->cmn.fcphHigh < FC_PH3)
614 sp->cmn.fcphHigh = FC_PH3;
616 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
617 sp->cmn.request_multiple_Nport = 1;
619 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
620 icmd->ulpCt_h = 1;
621 icmd->ulpCt_l = 0;
624 if (phba->fc_topology != TOPOLOGY_LOOP) {
625 icmd->un.elsreq64.myID = 0;
626 icmd->un.elsreq64.fl = 1;
629 tmo = phba->fc_ratov;
630 phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
631 lpfc_set_disctmo(vport);
632 phba->fc_ratov = tmo;
634 phba->fc_stat.elsXmitFLOGI++;
635 elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
637 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
638 "Issue FLOGI: opt:x%x",
639 phba->sli3_options, 0, 0);
641 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
642 if (rc == IOCB_ERROR) {
643 lpfc_els_free_iocb(phba, elsiocb);
644 return 1;
646 return 0;
650 lpfc_els_abort_flogi(struct lpfc_hba *phba)
652 struct lpfc_sli_ring *pring;
653 struct lpfc_iocbq *iocb, *next_iocb;
654 struct lpfc_nodelist *ndlp;
655 IOCB_t *icmd;
657 /* Abort outstanding I/O on NPort <nlp_DID> */
658 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
659 "0201 Abort outstanding I/O on NPort x%x\n",
660 Fabric_DID);
662 pring = &phba->sli.ring[LPFC_ELS_RING];
665 * Check the txcmplq for an iocb that matches the nport the driver is
666 * searching for.
668 spin_lock_irq(&phba->hbalock);
669 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
670 icmd = &iocb->iocb;
671 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
672 icmd->un.elsreq64.bdl.ulpIoTag32) {
673 ndlp = (struct lpfc_nodelist *)(iocb->context1);
674 if (ndlp && (ndlp->nlp_DID == Fabric_DID)) {
675 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
679 spin_unlock_irq(&phba->hbalock);
681 return 0;
685 lpfc_initial_flogi(struct lpfc_vport *vport)
687 struct lpfc_hba *phba = vport->phba;
688 struct lpfc_nodelist *ndlp;
690 /* First look for the Fabric ndlp */
691 ndlp = lpfc_findnode_did(vport, Fabric_DID);
692 if (!ndlp) {
693 /* Cannot find existing Fabric ndlp, so allocate a new one */
694 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
695 if (!ndlp)
696 return 0;
697 lpfc_nlp_init(vport, ndlp, Fabric_DID);
698 } else {
699 lpfc_dequeue_node(vport, ndlp);
701 if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
702 lpfc_nlp_put(ndlp);
704 return 1;
708 lpfc_initial_fdisc(struct lpfc_vport *vport)
710 struct lpfc_hba *phba = vport->phba;
711 struct lpfc_nodelist *ndlp;
713 /* First look for the Fabric ndlp */
714 ndlp = lpfc_findnode_did(vport, Fabric_DID);
715 if (!ndlp) {
716 /* Cannot find existing Fabric ndlp, so allocate a new one */
717 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
718 if (!ndlp)
719 return 0;
720 lpfc_nlp_init(vport, ndlp, Fabric_DID);
721 } else {
722 lpfc_dequeue_node(vport, ndlp);
724 if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
725 lpfc_nlp_put(ndlp);
727 return 1;
729 static void
730 lpfc_more_plogi(struct lpfc_vport *vport)
732 int sentplogi;
734 if (vport->num_disc_nodes)
735 vport->num_disc_nodes--;
737 /* Continue discovery with <num_disc_nodes> PLOGIs to go */
738 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
739 "0232 Continue discovery with %d PLOGIs to go "
740 "Data: x%x x%x x%x\n",
741 vport->num_disc_nodes, vport->fc_plogi_cnt,
742 vport->fc_flag, vport->port_state);
743 /* Check to see if there are more PLOGIs to be sent */
744 if (vport->fc_flag & FC_NLP_MORE)
745 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
746 sentplogi = lpfc_els_disc_plogi(vport);
748 return;
751 static struct lpfc_nodelist *
752 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
753 struct lpfc_nodelist *ndlp)
755 struct lpfc_vport *vport = ndlp->vport;
756 struct lpfc_nodelist *new_ndlp;
757 struct serv_parm *sp;
758 uint8_t name[sizeof(struct lpfc_name)];
759 uint32_t rc;
761 /* Fabric nodes can have the same WWPN so we don't bother searching
762 * by WWPN. Just return the ndlp that was given to us.
764 if (ndlp->nlp_type & NLP_FABRIC)
765 return ndlp;
767 sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
768 memset(name, 0, sizeof(struct lpfc_name));
770 /* Now we find out if the NPort we are logging into, matches the WWPN
771 * we have for that ndlp. If not, we have some work to do.
773 new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
775 if (new_ndlp == ndlp)
776 return ndlp;
778 if (!new_ndlp) {
779 rc = memcmp(&ndlp->nlp_portname, name,
780 sizeof(struct lpfc_name));
781 if (!rc)
782 return ndlp;
783 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
784 if (!new_ndlp)
785 return ndlp;
787 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
790 lpfc_unreg_rpi(vport, new_ndlp);
791 new_ndlp->nlp_DID = ndlp->nlp_DID;
792 new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
793 lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
795 /* Move this back to NPR state */
796 if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0)
797 lpfc_drop_node(vport, ndlp);
798 else {
799 lpfc_unreg_rpi(vport, ndlp);
800 ndlp->nlp_DID = 0; /* Two ndlps cannot have the same did */
801 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
803 return new_ndlp;
806 static void
807 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
808 struct lpfc_iocbq *rspiocb)
810 struct lpfc_vport *vport = cmdiocb->vport;
811 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
812 IOCB_t *irsp;
813 struct lpfc_nodelist *ndlp;
814 struct lpfc_dmabuf *prsp;
815 int disc, rc, did, type;
817 /* we pass cmdiocb to state machine which needs rspiocb as well */
818 cmdiocb->context_un.rsp_iocb = rspiocb;
820 irsp = &rspiocb->iocb;
821 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
822 "PLOGI cmpl: status:x%x/x%x did:x%x",
823 irsp->ulpStatus, irsp->un.ulpWord[4],
824 irsp->un.elsreq64.remoteID);
826 ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
827 if (!ndlp) {
828 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
829 "0136 PLOGI completes to NPort x%x "
830 "with no ndlp. Data: x%x x%x x%x\n",
831 irsp->un.elsreq64.remoteID,
832 irsp->ulpStatus, irsp->un.ulpWord[4],
833 irsp->ulpIoTag);
834 goto out;
837 /* Since ndlp can be freed in the disc state machine, note if this node
838 * is being used during discovery.
840 spin_lock_irq(shost->host_lock);
841 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
842 ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
843 spin_unlock_irq(shost->host_lock);
844 rc = 0;
846 /* PLOGI completes to NPort <nlp_DID> */
847 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
848 "0102 PLOGI completes to NPort x%x "
849 "Data: x%x x%x x%x x%x x%x\n",
850 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
851 irsp->ulpTimeout, disc, vport->num_disc_nodes);
852 /* Check to see if link went down during discovery */
853 if (lpfc_els_chk_latt(vport)) {
854 spin_lock_irq(shost->host_lock);
855 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
856 spin_unlock_irq(shost->host_lock);
857 goto out;
860 /* ndlp could be freed in DSM, save these values now */
861 type = ndlp->nlp_type;
862 did = ndlp->nlp_DID;
864 if (irsp->ulpStatus) {
865 /* Check for retry */
866 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
867 /* ELS command is being retried */
868 if (disc) {
869 spin_lock_irq(shost->host_lock);
870 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
871 spin_unlock_irq(shost->host_lock);
873 goto out;
875 /* PLOGI failed */
876 if (ndlp->nlp_DID == NameServer_DID) {
877 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
878 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
879 "0250 Nameserver login error: "
880 "0x%x / 0x%x\n",
881 irsp->ulpStatus, irsp->un.ulpWord[4]);
883 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
884 if (lpfc_error_lost_link(irsp)) {
885 rc = NLP_STE_FREED_NODE;
886 } else {
887 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
888 NLP_EVT_CMPL_PLOGI);
890 } else {
891 /* Good status, call state machine */
892 prsp = list_entry(((struct lpfc_dmabuf *)
893 cmdiocb->context2)->list.next,
894 struct lpfc_dmabuf, list);
895 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
896 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
897 NLP_EVT_CMPL_PLOGI);
900 if (disc && vport->num_disc_nodes) {
901 /* Check to see if there are more PLOGIs to be sent */
902 lpfc_more_plogi(vport);
904 if (vport->num_disc_nodes == 0) {
905 spin_lock_irq(shost->host_lock);
906 vport->fc_flag &= ~FC_NDISC_ACTIVE;
907 spin_unlock_irq(shost->host_lock);
909 lpfc_can_disctmo(vport);
910 if (vport->fc_flag & FC_RSCN_MODE) {
912 * Check to see if more RSCNs came in while
913 * we were processing this one.
915 if ((vport->fc_rscn_id_cnt == 0) &&
916 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
917 spin_lock_irq(shost->host_lock);
918 vport->fc_flag &= ~FC_RSCN_MODE;
919 spin_unlock_irq(shost->host_lock);
920 } else {
921 lpfc_els_handle_rscn(vport);
927 out:
928 lpfc_els_free_iocb(phba, cmdiocb);
929 return;
933 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
935 struct lpfc_hba *phba = vport->phba;
936 struct serv_parm *sp;
937 IOCB_t *icmd;
938 struct lpfc_iocbq *elsiocb;
939 struct lpfc_sli_ring *pring;
940 struct lpfc_sli *psli;
941 uint8_t *pcmd;
942 uint16_t cmdsize;
943 int ret;
945 psli = &phba->sli;
946 pring = &psli->ring[LPFC_ELS_RING]; /* ELS ring */
948 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
949 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, NULL, did,
950 ELS_CMD_PLOGI);
951 if (!elsiocb)
952 return 1;
954 icmd = &elsiocb->iocb;
955 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
957 /* For PLOGI request, remainder of payload is service parameters */
958 *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
959 pcmd += sizeof(uint32_t);
960 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
961 sp = (struct serv_parm *) pcmd;
963 if (sp->cmn.fcphLow < FC_PH_4_3)
964 sp->cmn.fcphLow = FC_PH_4_3;
966 if (sp->cmn.fcphHigh < FC_PH3)
967 sp->cmn.fcphHigh = FC_PH3;
969 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
970 "Issue PLOGI: did:x%x",
971 did, 0, 0);
973 phba->fc_stat.elsXmitPLOGI++;
974 elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
975 ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
977 if (ret == IOCB_ERROR) {
978 lpfc_els_free_iocb(phba, elsiocb);
979 return 1;
981 return 0;
984 static void
985 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
986 struct lpfc_iocbq *rspiocb)
988 struct lpfc_vport *vport = cmdiocb->vport;
989 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
990 IOCB_t *irsp;
991 struct lpfc_sli *psli;
992 struct lpfc_nodelist *ndlp;
994 psli = &phba->sli;
995 /* we pass cmdiocb to state machine which needs rspiocb as well */
996 cmdiocb->context_un.rsp_iocb = rspiocb;
998 irsp = &(rspiocb->iocb);
999 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1000 spin_lock_irq(shost->host_lock);
1001 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1002 spin_unlock_irq(shost->host_lock);
1004 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1005 "PRLI cmpl: status:x%x/x%x did:x%x",
1006 irsp->ulpStatus, irsp->un.ulpWord[4],
1007 ndlp->nlp_DID);
1008 /* PRLI completes to NPort <nlp_DID> */
1009 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1010 "0103 PRLI completes to NPort x%x "
1011 "Data: x%x x%x x%x x%x\n",
1012 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1013 irsp->ulpTimeout, vport->num_disc_nodes);
1015 vport->fc_prli_sent--;
1016 /* Check to see if link went down during discovery */
1017 if (lpfc_els_chk_latt(vport))
1018 goto out;
1020 if (irsp->ulpStatus) {
1021 /* Check for retry */
1022 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1023 /* ELS command is being retried */
1024 goto out;
1026 /* PRLI failed */
1027 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1028 if (lpfc_error_lost_link(irsp)) {
1029 goto out;
1030 } else {
1031 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1032 NLP_EVT_CMPL_PRLI);
1034 } else {
1035 /* Good status, call state machine */
1036 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1037 NLP_EVT_CMPL_PRLI);
1040 out:
1041 lpfc_els_free_iocb(phba, cmdiocb);
1042 return;
1046 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1047 uint8_t retry)
1049 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1050 struct lpfc_hba *phba = vport->phba;
1051 PRLI *npr;
1052 IOCB_t *icmd;
1053 struct lpfc_iocbq *elsiocb;
1054 struct lpfc_sli_ring *pring;
1055 struct lpfc_sli *psli;
1056 uint8_t *pcmd;
1057 uint16_t cmdsize;
1059 psli = &phba->sli;
1060 pring = &psli->ring[LPFC_ELS_RING]; /* ELS ring */
1062 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1063 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1064 ndlp->nlp_DID, ELS_CMD_PRLI);
1065 if (!elsiocb)
1066 return 1;
1068 icmd = &elsiocb->iocb;
1069 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1071 /* For PRLI request, remainder of payload is service parameters */
1072 memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1073 *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1074 pcmd += sizeof(uint32_t);
1076 /* For PRLI, remainder of payload is PRLI parameter page */
1077 npr = (PRLI *) pcmd;
1079 * If our firmware version is 3.20 or later,
1080 * set the following bits for FC-TAPE support.
1082 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1083 npr->ConfmComplAllowed = 1;
1084 npr->Retry = 1;
1085 npr->TaskRetryIdReq = 1;
1087 npr->estabImagePair = 1;
1088 npr->readXferRdyDis = 1;
1090 /* For FCP support */
1091 npr->prliType = PRLI_FCP_TYPE;
1092 npr->initiatorFunc = 1;
1094 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1095 "Issue PRLI: did:x%x",
1096 ndlp->nlp_DID, 0, 0);
1098 phba->fc_stat.elsXmitPRLI++;
1099 elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1100 spin_lock_irq(shost->host_lock);
1101 ndlp->nlp_flag |= NLP_PRLI_SND;
1102 spin_unlock_irq(shost->host_lock);
1103 if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1104 spin_lock_irq(shost->host_lock);
1105 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1106 spin_unlock_irq(shost->host_lock);
1107 lpfc_els_free_iocb(phba, elsiocb);
1108 return 1;
1110 vport->fc_prli_sent++;
1111 return 0;
1114 static void
1115 lpfc_more_adisc(struct lpfc_vport *vport)
1117 int sentadisc;
1119 if (vport->num_disc_nodes)
1120 vport->num_disc_nodes--;
1121 /* Continue discovery with <num_disc_nodes> ADISCs to go */
1122 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1123 "0210 Continue discovery with %d ADISCs to go "
1124 "Data: x%x x%x x%x\n",
1125 vport->num_disc_nodes, vport->fc_adisc_cnt,
1126 vport->fc_flag, vport->port_state);
1127 /* Check to see if there are more ADISCs to be sent */
1128 if (vport->fc_flag & FC_NLP_MORE) {
1129 lpfc_set_disctmo(vport);
1130 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1131 sentadisc = lpfc_els_disc_adisc(vport);
1133 return;
1136 static void
1137 lpfc_rscn_disc(struct lpfc_vport *vport)
1139 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1141 lpfc_can_disctmo(vport);
1143 /* RSCN discovery */
1144 /* go thru NPR nodes and issue ELS PLOGIs */
1145 if (vport->fc_npr_cnt)
1146 if (lpfc_els_disc_plogi(vport))
1147 return;
1149 if (vport->fc_flag & FC_RSCN_MODE) {
1150 /* Check to see if more RSCNs came in while we were
1151 * processing this one.
1153 if ((vport->fc_rscn_id_cnt == 0) &&
1154 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
1155 spin_lock_irq(shost->host_lock);
1156 vport->fc_flag &= ~FC_RSCN_MODE;
1157 spin_unlock_irq(shost->host_lock);
1158 } else {
1159 lpfc_els_handle_rscn(vport);
1164 static void
1165 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1166 struct lpfc_iocbq *rspiocb)
1168 struct lpfc_vport *vport = cmdiocb->vport;
1169 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1170 IOCB_t *irsp;
1171 struct lpfc_nodelist *ndlp;
1172 int disc;
1174 /* we pass cmdiocb to state machine which needs rspiocb as well */
1175 cmdiocb->context_un.rsp_iocb = rspiocb;
1177 irsp = &(rspiocb->iocb);
1178 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1180 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1181 "ADISC cmpl: status:x%x/x%x did:x%x",
1182 irsp->ulpStatus, irsp->un.ulpWord[4],
1183 ndlp->nlp_DID);
1185 /* Since ndlp can be freed in the disc state machine, note if this node
1186 * is being used during discovery.
1188 spin_lock_irq(shost->host_lock);
1189 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1190 ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1191 spin_unlock_irq(shost->host_lock);
1192 /* ADISC completes to NPort <nlp_DID> */
1193 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1194 "0104 ADISC completes to NPort x%x "
1195 "Data: x%x x%x x%x x%x x%x\n",
1196 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1197 irsp->ulpTimeout, disc, vport->num_disc_nodes);
1198 /* Check to see if link went down during discovery */
1199 if (lpfc_els_chk_latt(vport)) {
1200 spin_lock_irq(shost->host_lock);
1201 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1202 spin_unlock_irq(shost->host_lock);
1203 goto out;
1206 if (irsp->ulpStatus) {
1207 /* Check for retry */
1208 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1209 /* ELS command is being retried */
1210 if (disc) {
1211 spin_lock_irq(shost->host_lock);
1212 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1213 spin_unlock_irq(shost->host_lock);
1214 lpfc_set_disctmo(vport);
1216 goto out;
1218 /* ADISC failed */
1219 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1220 if (!lpfc_error_lost_link(irsp)) {
1221 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1222 NLP_EVT_CMPL_ADISC);
1224 } else {
1225 /* Good status, call state machine */
1226 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1227 NLP_EVT_CMPL_ADISC);
1230 if (disc && vport->num_disc_nodes) {
1231 /* Check to see if there are more ADISCs to be sent */
1232 lpfc_more_adisc(vport);
1234 /* Check to see if we are done with ADISC authentication */
1235 if (vport->num_disc_nodes == 0) {
1236 /* If we get here, there is nothing left to ADISC */
1238 * For NPIV, cmpl_reg_vpi will set port_state to READY,
1239 * and continue discovery.
1241 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1242 !(vport->fc_flag & FC_RSCN_MODE)) {
1243 lpfc_issue_reg_vpi(phba, vport);
1244 goto out;
1247 * For SLI2, we need to set port_state to READY
1248 * and continue discovery.
1250 if (vport->port_state < LPFC_VPORT_READY) {
1251 /* If we get here, there is nothing to ADISC */
1252 if (vport->port_type == LPFC_PHYSICAL_PORT)
1253 lpfc_issue_clear_la(phba, vport);
1255 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1256 vport->num_disc_nodes = 0;
1257 /* go thru NPR list, issue ELS PLOGIs */
1258 if (vport->fc_npr_cnt)
1259 lpfc_els_disc_plogi(vport);
1261 if (!vport->num_disc_nodes) {
1262 spin_lock_irq(shost->host_lock);
1263 vport->fc_flag &=
1264 ~FC_NDISC_ACTIVE;
1265 spin_unlock_irq(
1266 shost->host_lock);
1267 lpfc_can_disctmo(vport);
1270 vport->port_state = LPFC_VPORT_READY;
1271 } else {
1272 lpfc_rscn_disc(vport);
1276 out:
1277 lpfc_els_free_iocb(phba, cmdiocb);
1278 return;
1282 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1283 uint8_t retry)
1285 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1286 struct lpfc_hba *phba = vport->phba;
1287 ADISC *ap;
1288 IOCB_t *icmd;
1289 struct lpfc_iocbq *elsiocb;
1290 struct lpfc_sli *psli = &phba->sli;
1291 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1292 uint8_t *pcmd;
1293 uint16_t cmdsize;
1295 cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
1296 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1297 ndlp->nlp_DID, ELS_CMD_ADISC);
1298 if (!elsiocb)
1299 return 1;
1301 icmd = &elsiocb->iocb;
1302 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1304 /* For ADISC request, remainder of payload is service parameters */
1305 *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1306 pcmd += sizeof(uint32_t);
1308 /* Fill in ADISC payload */
1309 ap = (ADISC *) pcmd;
1310 ap->hardAL_PA = phba->fc_pref_ALPA;
1311 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
1312 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1313 ap->DID = be32_to_cpu(vport->fc_myDID);
1315 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1316 "Issue ADISC: did:x%x",
1317 ndlp->nlp_DID, 0, 0);
1319 phba->fc_stat.elsXmitADISC++;
1320 elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1321 spin_lock_irq(shost->host_lock);
1322 ndlp->nlp_flag |= NLP_ADISC_SND;
1323 spin_unlock_irq(shost->host_lock);
1324 if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1325 spin_lock_irq(shost->host_lock);
1326 ndlp->nlp_flag &= ~NLP_ADISC_SND;
1327 spin_unlock_irq(shost->host_lock);
1328 lpfc_els_free_iocb(phba, elsiocb);
1329 return 1;
1331 return 0;
1334 static void
1335 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1336 struct lpfc_iocbq *rspiocb)
1338 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1339 struct lpfc_vport *vport = ndlp->vport;
1340 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1341 IOCB_t *irsp;
1342 struct lpfc_sli *psli;
1344 psli = &phba->sli;
1345 /* we pass cmdiocb to state machine which needs rspiocb as well */
1346 cmdiocb->context_un.rsp_iocb = rspiocb;
1348 irsp = &(rspiocb->iocb);
1349 spin_lock_irq(shost->host_lock);
1350 ndlp->nlp_flag &= ~NLP_LOGO_SND;
1351 spin_unlock_irq(shost->host_lock);
1353 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1354 "LOGO cmpl: status:x%x/x%x did:x%x",
1355 irsp->ulpStatus, irsp->un.ulpWord[4],
1356 ndlp->nlp_DID);
1357 /* LOGO completes to NPort <nlp_DID> */
1358 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1359 "0105 LOGO completes to NPort x%x "
1360 "Data: x%x x%x x%x x%x\n",
1361 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1362 irsp->ulpTimeout, vport->num_disc_nodes);
1363 /* Check to see if link went down during discovery */
1364 if (lpfc_els_chk_latt(vport))
1365 goto out;
1367 if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
1368 /* NLP_EVT_DEVICE_RM should unregister the RPI
1369 * which should abort all outstanding IOs.
1371 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1372 NLP_EVT_DEVICE_RM);
1373 goto out;
1376 if (irsp->ulpStatus) {
1377 /* Check for retry */
1378 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1379 /* ELS command is being retried */
1380 goto out;
1381 /* LOGO failed */
1382 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1383 if (lpfc_error_lost_link(irsp))
1384 goto out;
1385 else
1386 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1387 NLP_EVT_CMPL_LOGO);
1388 } else {
1389 /* Good status, call state machine.
1390 * This will unregister the rpi if needed.
1392 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1393 NLP_EVT_CMPL_LOGO);
1396 out:
1397 lpfc_els_free_iocb(phba, cmdiocb);
1398 return;
1402 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1403 uint8_t retry)
1405 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1406 struct lpfc_hba *phba = vport->phba;
1407 IOCB_t *icmd;
1408 struct lpfc_iocbq *elsiocb;
1409 struct lpfc_sli_ring *pring;
1410 struct lpfc_sli *psli;
1411 uint8_t *pcmd;
1412 uint16_t cmdsize;
1413 int rc;
1415 psli = &phba->sli;
1416 pring = &psli->ring[LPFC_ELS_RING];
1418 cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
1419 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1420 ndlp->nlp_DID, ELS_CMD_LOGO);
1421 if (!elsiocb)
1422 return 1;
1424 icmd = &elsiocb->iocb;
1425 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1426 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1427 pcmd += sizeof(uint32_t);
1429 /* Fill in LOGO payload */
1430 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1431 pcmd += sizeof(uint32_t);
1432 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
1434 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1435 "Issue LOGO: did:x%x",
1436 ndlp->nlp_DID, 0, 0);
1438 phba->fc_stat.elsXmitLOGO++;
1439 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
1440 spin_lock_irq(shost->host_lock);
1441 ndlp->nlp_flag |= NLP_LOGO_SND;
1442 spin_unlock_irq(shost->host_lock);
1443 rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1445 if (rc == IOCB_ERROR) {
1446 spin_lock_irq(shost->host_lock);
1447 ndlp->nlp_flag &= ~NLP_LOGO_SND;
1448 spin_unlock_irq(shost->host_lock);
1449 lpfc_els_free_iocb(phba, elsiocb);
1450 return 1;
1452 return 0;
1455 static void
1456 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1457 struct lpfc_iocbq *rspiocb)
1459 struct lpfc_vport *vport = cmdiocb->vport;
1460 IOCB_t *irsp;
1462 irsp = &rspiocb->iocb;
1464 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1465 "ELS cmd cmpl: status:x%x/x%x did:x%x",
1466 irsp->ulpStatus, irsp->un.ulpWord[4],
1467 irsp->un.elsreq64.remoteID);
1468 /* ELS cmd tag <ulpIoTag> completes */
1469 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1470 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
1471 irsp->ulpIoTag, irsp->ulpStatus,
1472 irsp->un.ulpWord[4], irsp->ulpTimeout);
1473 /* Check to see if link went down during discovery */
1474 lpfc_els_chk_latt(vport);
1475 lpfc_els_free_iocb(phba, cmdiocb);
1476 return;
1480 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
1482 struct lpfc_hba *phba = vport->phba;
1483 IOCB_t *icmd;
1484 struct lpfc_iocbq *elsiocb;
1485 struct lpfc_sli_ring *pring;
1486 struct lpfc_sli *psli;
1487 uint8_t *pcmd;
1488 uint16_t cmdsize;
1489 struct lpfc_nodelist *ndlp;
1491 psli = &phba->sli;
1492 pring = &psli->ring[LPFC_ELS_RING]; /* ELS ring */
1493 cmdsize = (sizeof(uint32_t) + sizeof(SCR));
1494 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1495 if (!ndlp)
1496 return 1;
1498 lpfc_nlp_init(vport, ndlp, nportid);
1500 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1501 ndlp->nlp_DID, ELS_CMD_SCR);
1503 if (!elsiocb) {
1504 lpfc_nlp_put(ndlp);
1505 return 1;
1508 icmd = &elsiocb->iocb;
1509 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1511 *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
1512 pcmd += sizeof(uint32_t);
1514 /* For SCR, remainder of payload is SCR parameter page */
1515 memset(pcmd, 0, sizeof(SCR));
1516 ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
1518 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1519 "Issue SCR: did:x%x",
1520 ndlp->nlp_DID, 0, 0);
1522 phba->fc_stat.elsXmitSCR++;
1523 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
1524 if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1525 lpfc_nlp_put(ndlp);
1526 lpfc_els_free_iocb(phba, elsiocb);
1527 return 1;
1529 lpfc_nlp_put(ndlp);
1530 return 0;
1533 static int
1534 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
1536 struct lpfc_hba *phba = vport->phba;
1537 IOCB_t *icmd;
1538 struct lpfc_iocbq *elsiocb;
1539 struct lpfc_sli_ring *pring;
1540 struct lpfc_sli *psli;
1541 FARP *fp;
1542 uint8_t *pcmd;
1543 uint32_t *lp;
1544 uint16_t cmdsize;
1545 struct lpfc_nodelist *ondlp;
1546 struct lpfc_nodelist *ndlp;
1548 psli = &phba->sli;
1549 pring = &psli->ring[LPFC_ELS_RING]; /* ELS ring */
1550 cmdsize = (sizeof(uint32_t) + sizeof(FARP));
1551 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1552 if (!ndlp)
1553 return 1;
1555 lpfc_nlp_init(vport, ndlp, nportid);
1557 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1558 ndlp->nlp_DID, ELS_CMD_RNID);
1559 if (!elsiocb) {
1560 lpfc_nlp_put(ndlp);
1561 return 1;
1564 icmd = &elsiocb->iocb;
1565 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1567 *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
1568 pcmd += sizeof(uint32_t);
1570 /* Fill in FARPR payload */
1571 fp = (FARP *) (pcmd);
1572 memset(fp, 0, sizeof(FARP));
1573 lp = (uint32_t *) pcmd;
1574 *lp++ = be32_to_cpu(nportid);
1575 *lp++ = be32_to_cpu(vport->fc_myDID);
1576 fp->Rflags = 0;
1577 fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
1579 memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
1580 memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1581 ondlp = lpfc_findnode_did(vport, nportid);
1582 if (ondlp) {
1583 memcpy(&fp->OportName, &ondlp->nlp_portname,
1584 sizeof(struct lpfc_name));
1585 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
1586 sizeof(struct lpfc_name));
1589 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1590 "Issue FARPR: did:x%x",
1591 ndlp->nlp_DID, 0, 0);
1593 phba->fc_stat.elsXmitFARPR++;
1594 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
1595 if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1596 lpfc_nlp_put(ndlp);
1597 lpfc_els_free_iocb(phba, elsiocb);
1598 return 1;
1600 lpfc_nlp_put(ndlp);
1601 return 0;
1604 static void
1605 lpfc_end_rscn(struct lpfc_vport *vport)
1607 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1609 if (vport->fc_flag & FC_RSCN_MODE) {
1611 * Check to see if more RSCNs came in while we were
1612 * processing this one.
1614 if (vport->fc_rscn_id_cnt ||
1615 (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1616 lpfc_els_handle_rscn(vport);
1617 else {
1618 spin_lock_irq(shost->host_lock);
1619 vport->fc_flag &= ~FC_RSCN_MODE;
1620 spin_unlock_irq(shost->host_lock);
1625 void
1626 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
1628 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1630 spin_lock_irq(shost->host_lock);
1631 nlp->nlp_flag &= ~NLP_DELAY_TMO;
1632 spin_unlock_irq(shost->host_lock);
1633 del_timer_sync(&nlp->nlp_delayfunc);
1634 nlp->nlp_last_elscmd = 0;
1636 if (!list_empty(&nlp->els_retry_evt.evt_listp))
1637 list_del_init(&nlp->els_retry_evt.evt_listp);
1639 if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
1640 spin_lock_irq(shost->host_lock);
1641 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1642 spin_unlock_irq(shost->host_lock);
1643 if (vport->num_disc_nodes) {
1644 /* Check to see if there are more
1645 * PLOGIs to be sent
1647 lpfc_more_plogi(vport);
1649 if (vport->num_disc_nodes == 0) {
1650 spin_lock_irq(shost->host_lock);
1651 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1652 spin_unlock_irq(shost->host_lock);
1653 lpfc_can_disctmo(vport);
1654 lpfc_end_rscn(vport);
1658 return;
1661 void
1662 lpfc_els_retry_delay(unsigned long ptr)
1664 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
1665 struct lpfc_vport *vport = ndlp->vport;
1666 struct lpfc_hba *phba = vport->phba;
1667 unsigned long flags;
1668 struct lpfc_work_evt *evtp = &ndlp->els_retry_evt;
1670 ndlp = (struct lpfc_nodelist *) ptr;
1671 phba = ndlp->vport->phba;
1672 evtp = &ndlp->els_retry_evt;
1674 spin_lock_irqsave(&phba->hbalock, flags);
1675 if (!list_empty(&evtp->evt_listp)) {
1676 spin_unlock_irqrestore(&phba->hbalock, flags);
1677 return;
1680 evtp->evt_arg1 = ndlp;
1681 evtp->evt = LPFC_EVT_ELS_RETRY;
1682 list_add_tail(&evtp->evt_listp, &phba->work_list);
1683 if (phba->work_wait)
1684 lpfc_worker_wake_up(phba);
1686 spin_unlock_irqrestore(&phba->hbalock, flags);
1687 return;
1690 void
1691 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
1693 struct lpfc_vport *vport = ndlp->vport;
1694 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1695 uint32_t cmd, did, retry;
1697 spin_lock_irq(shost->host_lock);
1698 did = ndlp->nlp_DID;
1699 cmd = ndlp->nlp_last_elscmd;
1700 ndlp->nlp_last_elscmd = 0;
1702 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1703 spin_unlock_irq(shost->host_lock);
1704 return;
1707 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1708 spin_unlock_irq(shost->host_lock);
1710 * If a discovery event readded nlp_delayfunc after timer
1711 * firing and before processing the timer, cancel the
1712 * nlp_delayfunc.
1714 del_timer_sync(&ndlp->nlp_delayfunc);
1715 retry = ndlp->nlp_retry;
1717 switch (cmd) {
1718 case ELS_CMD_FLOGI:
1719 lpfc_issue_els_flogi(vport, ndlp, retry);
1720 break;
1721 case ELS_CMD_PLOGI:
1722 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
1723 ndlp->nlp_prev_state = ndlp->nlp_state;
1724 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1726 break;
1727 case ELS_CMD_ADISC:
1728 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
1729 ndlp->nlp_prev_state = ndlp->nlp_state;
1730 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
1732 break;
1733 case ELS_CMD_PRLI:
1734 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
1735 ndlp->nlp_prev_state = ndlp->nlp_state;
1736 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1738 break;
1739 case ELS_CMD_LOGO:
1740 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
1741 ndlp->nlp_prev_state = ndlp->nlp_state;
1742 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1744 break;
1745 case ELS_CMD_FDISC:
1746 lpfc_issue_els_fdisc(vport, ndlp, retry);
1747 break;
1749 return;
1752 static int
1753 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1754 struct lpfc_iocbq *rspiocb)
1756 struct lpfc_vport *vport = cmdiocb->vport;
1757 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1758 IOCB_t *irsp = &rspiocb->iocb;
1759 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1760 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
1761 uint32_t *elscmd;
1762 struct ls_rjt stat;
1763 int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
1764 uint32_t cmd = 0;
1765 uint32_t did;
1768 /* Note: context2 may be 0 for internal driver abort
1769 * of delays ELS command.
1772 if (pcmd && pcmd->virt) {
1773 elscmd = (uint32_t *) (pcmd->virt);
1774 cmd = *elscmd++;
1777 if (ndlp)
1778 did = ndlp->nlp_DID;
1779 else {
1780 /* We should only hit this case for retrying PLOGI */
1781 did = irsp->un.elsreq64.remoteID;
1782 ndlp = lpfc_findnode_did(vport, did);
1783 if (!ndlp && (cmd != ELS_CMD_PLOGI))
1784 return 1;
1787 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1788 "Retry ELS: wd7:x%x wd4:x%x did:x%x",
1789 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
1791 switch (irsp->ulpStatus) {
1792 case IOSTAT_FCP_RSP_ERROR:
1793 case IOSTAT_REMOTE_STOP:
1794 break;
1796 case IOSTAT_LOCAL_REJECT:
1797 switch ((irsp->un.ulpWord[4] & 0xff)) {
1798 case IOERR_LOOP_OPEN_FAILURE:
1799 if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
1800 delay = 1000;
1801 retry = 1;
1802 break;
1804 case IOERR_ILLEGAL_COMMAND:
1805 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) &&
1806 (cmd == ELS_CMD_FDISC)) {
1807 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1808 "0124 FDISC failed (3/6) "
1809 "retrying...\n");
1810 lpfc_mbx_unreg_vpi(vport);
1811 retry = 1;
1812 /* Always retry for this case */
1813 cmdiocb->retry = 0;
1815 break;
1817 case IOERR_NO_RESOURCES:
1818 retry = 1;
1819 if (cmdiocb->retry > 100)
1820 delay = 100;
1821 maxretry = 250;
1822 break;
1824 case IOERR_ILLEGAL_FRAME:
1825 delay = 100;
1826 retry = 1;
1827 break;
1829 case IOERR_SEQUENCE_TIMEOUT:
1830 case IOERR_INVALID_RPI:
1831 retry = 1;
1832 break;
1834 break;
1836 case IOSTAT_NPORT_RJT:
1837 case IOSTAT_FABRIC_RJT:
1838 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
1839 retry = 1;
1840 break;
1842 break;
1844 case IOSTAT_NPORT_BSY:
1845 case IOSTAT_FABRIC_BSY:
1846 retry = 1;
1847 break;
1849 case IOSTAT_LS_RJT:
1850 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
1851 /* Added for Vendor specifc support
1852 * Just keep retrying for these Rsn / Exp codes
1854 switch (stat.un.b.lsRjtRsnCode) {
1855 case LSRJT_UNABLE_TPC:
1856 if (stat.un.b.lsRjtRsnCodeExp ==
1857 LSEXP_CMD_IN_PROGRESS) {
1858 if (cmd == ELS_CMD_PLOGI) {
1859 delay = 1000;
1860 maxretry = 48;
1862 retry = 1;
1863 break;
1865 if (cmd == ELS_CMD_PLOGI) {
1866 delay = 1000;
1867 maxretry = lpfc_max_els_tries + 1;
1868 retry = 1;
1869 break;
1871 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1872 (cmd == ELS_CMD_FDISC) &&
1873 (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
1874 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1875 "0125 FDISC Failed (x%x). "
1876 "Fabric out of resources\n",
1877 stat.un.lsRjtError);
1878 lpfc_vport_set_state(vport,
1879 FC_VPORT_NO_FABRIC_RSCS);
1881 break;
1883 case LSRJT_LOGICAL_BSY:
1884 if ((cmd == ELS_CMD_PLOGI) ||
1885 (cmd == ELS_CMD_PRLI)) {
1886 delay = 1000;
1887 maxretry = 48;
1888 } else if (cmd == ELS_CMD_FDISC) {
1889 /* Always retry for this case */
1890 cmdiocb->retry = 0;
1892 retry = 1;
1893 break;
1895 case LSRJT_LOGICAL_ERR:
1896 case LSRJT_PROTOCOL_ERR:
1897 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1898 (cmd == ELS_CMD_FDISC) &&
1899 ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
1900 (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
1902 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1903 "0123 FDISC Failed (x%x). "
1904 "Fabric Detected Bad WWN\n",
1905 stat.un.lsRjtError);
1906 lpfc_vport_set_state(vport,
1907 FC_VPORT_FABRIC_REJ_WWN);
1909 break;
1911 break;
1913 case IOSTAT_INTERMED_RSP:
1914 case IOSTAT_BA_RJT:
1915 break;
1917 default:
1918 break;
1921 if (did == FDMI_DID)
1922 retry = 1;
1924 if ((++cmdiocb->retry) >= maxretry) {
1925 phba->fc_stat.elsRetryExceeded++;
1926 retry = 0;
1929 if ((vport->load_flag & FC_UNLOADING) != 0)
1930 retry = 0;
1932 if (retry) {
1934 /* Retry ELS command <elsCmd> to remote NPORT <did> */
1935 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1936 "0107 Retry ELS command x%x to remote "
1937 "NPORT x%x Data: x%x x%x\n",
1938 cmd, did, cmdiocb->retry, delay);
1940 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
1941 ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1942 ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
1943 /* Don't reset timer for no resources */
1945 /* If discovery / RSCN timer is running, reset it */
1946 if (timer_pending(&vport->fc_disctmo) ||
1947 (vport->fc_flag & FC_RSCN_MODE))
1948 lpfc_set_disctmo(vport);
1951 phba->fc_stat.elsXmitRetry++;
1952 if (ndlp && delay) {
1953 phba->fc_stat.elsDelayRetry++;
1954 ndlp->nlp_retry = cmdiocb->retry;
1956 /* delay is specified in milliseconds */
1957 mod_timer(&ndlp->nlp_delayfunc,
1958 jiffies + msecs_to_jiffies(delay));
1959 spin_lock_irq(shost->host_lock);
1960 ndlp->nlp_flag |= NLP_DELAY_TMO;
1961 spin_unlock_irq(shost->host_lock);
1963 ndlp->nlp_prev_state = ndlp->nlp_state;
1964 if (cmd == ELS_CMD_PRLI)
1965 lpfc_nlp_set_state(vport, ndlp,
1966 NLP_STE_REG_LOGIN_ISSUE);
1967 else
1968 lpfc_nlp_set_state(vport, ndlp,
1969 NLP_STE_NPR_NODE);
1970 ndlp->nlp_last_elscmd = cmd;
1972 return 1;
1974 switch (cmd) {
1975 case ELS_CMD_FLOGI:
1976 lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
1977 return 1;
1978 case ELS_CMD_FDISC:
1979 lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
1980 return 1;
1981 case ELS_CMD_PLOGI:
1982 if (ndlp) {
1983 ndlp->nlp_prev_state = ndlp->nlp_state;
1984 lpfc_nlp_set_state(vport, ndlp,
1985 NLP_STE_PLOGI_ISSUE);
1987 lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
1988 return 1;
1989 case ELS_CMD_ADISC:
1990 ndlp->nlp_prev_state = ndlp->nlp_state;
1991 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
1992 lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
1993 return 1;
1994 case ELS_CMD_PRLI:
1995 ndlp->nlp_prev_state = ndlp->nlp_state;
1996 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1997 lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
1998 return 1;
1999 case ELS_CMD_LOGO:
2000 ndlp->nlp_prev_state = ndlp->nlp_state;
2001 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2002 lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2003 return 1;
2006 /* No retry ELS command <elsCmd> to remote NPORT <did> */
2007 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2008 "0108 No retry ELS command x%x to remote "
2009 "NPORT x%x Retried:%d Error:x%x/%x\n",
2010 cmd, did, cmdiocb->retry, irsp->ulpStatus,
2011 irsp->un.ulpWord[4]);
2012 return 0;
2016 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
2018 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2020 if (elsiocb->context1) {
2021 lpfc_nlp_put(elsiocb->context1);
2022 elsiocb->context1 = NULL;
2024 /* context2 = cmd, context2->next = rsp, context3 = bpl */
2025 if (elsiocb->context2) {
2026 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2027 /* Free the response before processing the command. */
2028 if (!list_empty(&buf_ptr1->list)) {
2029 list_remove_head(&buf_ptr1->list, buf_ptr,
2030 struct lpfc_dmabuf,
2031 list);
2032 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2033 kfree(buf_ptr);
2035 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2036 kfree(buf_ptr1);
2039 if (elsiocb->context3) {
2040 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2041 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2042 kfree(buf_ptr);
2044 lpfc_sli_release_iocbq(phba, elsiocb);
2045 return 0;
2048 static void
2049 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2050 struct lpfc_iocbq *rspiocb)
2052 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2053 struct lpfc_vport *vport = cmdiocb->vport;
2054 IOCB_t *irsp;
2056 irsp = &rspiocb->iocb;
2057 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2058 "ACC LOGO cmpl: status:x%x/x%x did:x%x",
2059 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2060 /* ACC to LOGO completes to NPort <nlp_DID> */
2061 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2062 "0109 ACC to LOGO completes to NPort x%x "
2063 "Data: x%x x%x x%x\n",
2064 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2065 ndlp->nlp_rpi);
2066 switch (ndlp->nlp_state) {
2067 case NLP_STE_UNUSED_NODE: /* node is just allocated */
2068 lpfc_drop_node(vport, ndlp);
2069 break;
2070 case NLP_STE_NPR_NODE: /* NPort Recovery mode */
2071 lpfc_unreg_rpi(vport, ndlp);
2072 break;
2073 default:
2074 break;
2076 lpfc_els_free_iocb(phba, cmdiocb);
2077 return;
2080 void
2081 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2083 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2084 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2086 pmb->context1 = NULL;
2087 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2088 kfree(mp);
2089 mempool_free(pmb, phba->mbox_mem_pool);
2090 lpfc_nlp_put(ndlp);
2091 return;
2094 static void
2095 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2096 struct lpfc_iocbq *rspiocb)
2098 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2099 struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
2100 struct Scsi_Host *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
2101 IOCB_t *irsp;
2102 LPFC_MBOXQ_t *mbox = NULL;
2103 struct lpfc_dmabuf *mp = NULL;
2105 irsp = &rspiocb->iocb;
2107 if (cmdiocb->context_un.mbox)
2108 mbox = cmdiocb->context_un.mbox;
2110 /* Check to see if link went down during discovery */
2111 if (!ndlp || lpfc_els_chk_latt(vport)) {
2112 if (mbox) {
2113 mp = (struct lpfc_dmabuf *) mbox->context1;
2114 if (mp) {
2115 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2116 kfree(mp);
2118 mempool_free(mbox, phba->mbox_mem_pool);
2120 goto out;
2123 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2124 "ACC cmpl: status:x%x/x%x did:x%x",
2125 irsp->ulpStatus, irsp->un.ulpWord[4],
2126 irsp->un.rcvels.remoteID);
2127 /* ELS response tag <ulpIoTag> completes */
2128 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2129 "0110 ELS response tag x%x completes "
2130 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
2131 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
2132 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
2133 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2134 ndlp->nlp_rpi);
2135 if (mbox) {
2136 if ((rspiocb->iocb.ulpStatus == 0)
2137 && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
2138 lpfc_unreg_rpi(vport, ndlp);
2139 mbox->context2 = lpfc_nlp_get(ndlp);
2140 mbox->vport = vport;
2141 if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
2142 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2143 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2145 else {
2146 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
2147 ndlp->nlp_prev_state = ndlp->nlp_state;
2148 lpfc_nlp_set_state(vport, ndlp,
2149 NLP_STE_REG_LOGIN_ISSUE);
2151 if (lpfc_sli_issue_mbox(phba, mbox,
2152 (MBX_NOWAIT | MBX_STOP_IOCB))
2153 != MBX_NOT_FINISHED) {
2154 goto out;
2156 lpfc_nlp_put(ndlp);
2157 /* NOTE: we should have messages for unsuccessful
2158 reglogin */
2159 } else {
2160 /* Do not drop node for lpfc_els_abort'ed ELS cmds */
2161 if (!lpfc_error_lost_link(irsp) &&
2162 ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
2163 lpfc_drop_node(vport, ndlp);
2164 ndlp = NULL;
2167 mp = (struct lpfc_dmabuf *) mbox->context1;
2168 if (mp) {
2169 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2170 kfree(mp);
2172 mempool_free(mbox, phba->mbox_mem_pool);
2174 out:
2175 if (ndlp) {
2176 spin_lock_irq(shost->host_lock);
2177 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
2178 spin_unlock_irq(shost->host_lock);
2180 lpfc_els_free_iocb(phba, cmdiocb);
2181 return;
2185 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
2186 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2187 LPFC_MBOXQ_t *mbox, uint8_t newnode)
2189 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2190 struct lpfc_hba *phba = vport->phba;
2191 IOCB_t *icmd;
2192 IOCB_t *oldcmd;
2193 struct lpfc_iocbq *elsiocb;
2194 struct lpfc_sli_ring *pring;
2195 struct lpfc_sli *psli;
2196 uint8_t *pcmd;
2197 uint16_t cmdsize;
2198 int rc;
2199 ELS_PKT *els_pkt_ptr;
2201 psli = &phba->sli;
2202 pring = &psli->ring[LPFC_ELS_RING]; /* ELS ring */
2203 oldcmd = &oldiocb->iocb;
2205 switch (flag) {
2206 case ELS_CMD_ACC:
2207 cmdsize = sizeof(uint32_t);
2208 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2209 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2210 if (!elsiocb) {
2211 spin_lock_irq(shost->host_lock);
2212 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2213 spin_unlock_irq(shost->host_lock);
2214 return 1;
2217 icmd = &elsiocb->iocb;
2218 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2219 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2220 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2221 pcmd += sizeof(uint32_t);
2223 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2224 "Issue ACC: did:x%x flg:x%x",
2225 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2226 break;
2227 case ELS_CMD_PLOGI:
2228 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
2229 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2230 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2231 if (!elsiocb)
2232 return 1;
2234 icmd = &elsiocb->iocb;
2235 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2236 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2238 if (mbox)
2239 elsiocb->context_un.mbox = mbox;
2241 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2242 pcmd += sizeof(uint32_t);
2243 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2245 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2246 "Issue ACC PLOGI: did:x%x flg:x%x",
2247 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2248 break;
2249 case ELS_CMD_PRLO:
2250 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
2251 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2252 ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
2253 if (!elsiocb)
2254 return 1;
2256 icmd = &elsiocb->iocb;
2257 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2258 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2260 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
2261 sizeof(uint32_t) + sizeof(PRLO));
2262 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
2263 els_pkt_ptr = (ELS_PKT *) pcmd;
2264 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
2266 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2267 "Issue ACC PRLO: did:x%x flg:x%x",
2268 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2269 break;
2270 default:
2271 return 1;
2274 if (newnode) {
2275 lpfc_nlp_put(ndlp);
2276 elsiocb->context1 = NULL;
2278 /* Xmit ELS ACC response tag <ulpIoTag> */
2279 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2280 "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
2281 "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
2282 elsiocb->iotag, elsiocb->iocb.ulpContext,
2283 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2284 ndlp->nlp_rpi);
2285 if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2286 spin_lock_irq(shost->host_lock);
2287 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2288 spin_unlock_irq(shost->host_lock);
2289 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
2290 } else {
2291 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2294 phba->fc_stat.elsXmitACC++;
2295 rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2296 if (rc == IOCB_ERROR) {
2297 lpfc_els_free_iocb(phba, elsiocb);
2298 return 1;
2300 return 0;
2304 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
2305 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2306 LPFC_MBOXQ_t *mbox)
2308 struct lpfc_hba *phba = vport->phba;
2309 IOCB_t *icmd;
2310 IOCB_t *oldcmd;
2311 struct lpfc_iocbq *elsiocb;
2312 struct lpfc_sli_ring *pring;
2313 struct lpfc_sli *psli;
2314 uint8_t *pcmd;
2315 uint16_t cmdsize;
2316 int rc;
2318 psli = &phba->sli;
2319 pring = &psli->ring[LPFC_ELS_RING]; /* ELS ring */
2321 cmdsize = 2 * sizeof(uint32_t);
2322 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2323 ndlp->nlp_DID, ELS_CMD_LS_RJT);
2324 if (!elsiocb)
2325 return 1;
2327 icmd = &elsiocb->iocb;
2328 oldcmd = &oldiocb->iocb;
2329 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2330 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2332 *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
2333 pcmd += sizeof(uint32_t);
2334 *((uint32_t *) (pcmd)) = rejectError;
2336 if (mbox) {
2337 elsiocb->context_un.mbox = mbox;
2338 elsiocb->context1 = lpfc_nlp_get(ndlp);
2341 /* Xmit ELS RJT <err> response tag <ulpIoTag> */
2342 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2343 "0129 Xmit ELS RJT x%x response tag x%x "
2344 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
2345 "rpi x%x\n",
2346 rejectError, elsiocb->iotag,
2347 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
2348 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
2349 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2350 "Issue LS_RJT: did:x%x flg:x%x err:x%x",
2351 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
2353 phba->fc_stat.elsXmitLSRJT++;
2354 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2355 rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2356 if (rc == IOCB_ERROR) {
2357 lpfc_els_free_iocb(phba, elsiocb);
2358 return 1;
2360 return 0;
2364 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2365 struct lpfc_nodelist *ndlp)
2367 struct lpfc_hba *phba = vport->phba;
2368 struct lpfc_sli *psli = &phba->sli;
2369 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2370 ADISC *ap;
2371 IOCB_t *icmd, *oldcmd;
2372 struct lpfc_iocbq *elsiocb;
2373 uint8_t *pcmd;
2374 uint16_t cmdsize;
2375 int rc;
2377 cmdsize = sizeof(uint32_t) + sizeof(ADISC);
2378 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2379 ndlp->nlp_DID, ELS_CMD_ACC);
2380 if (!elsiocb)
2381 return 1;
2383 icmd = &elsiocb->iocb;
2384 oldcmd = &oldiocb->iocb;
2385 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2387 /* Xmit ADISC ACC response tag <ulpIoTag> */
2388 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2389 "0130 Xmit ADISC ACC response iotag x%x xri: "
2390 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
2391 elsiocb->iotag, elsiocb->iocb.ulpContext,
2392 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2393 ndlp->nlp_rpi);
2394 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2396 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2397 pcmd += sizeof(uint32_t);
2399 ap = (ADISC *) (pcmd);
2400 ap->hardAL_PA = phba->fc_pref_ALPA;
2401 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2402 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2403 ap->DID = be32_to_cpu(vport->fc_myDID);
2405 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2406 "Issue ACC ADISC: did:x%x flg:x%x",
2407 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2409 phba->fc_stat.elsXmitACC++;
2410 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2411 rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2412 if (rc == IOCB_ERROR) {
2413 lpfc_els_free_iocb(phba, elsiocb);
2414 return 1;
2416 return 0;
2420 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2421 struct lpfc_nodelist *ndlp)
2423 struct lpfc_hba *phba = vport->phba;
2424 PRLI *npr;
2425 lpfc_vpd_t *vpd;
2426 IOCB_t *icmd;
2427 IOCB_t *oldcmd;
2428 struct lpfc_iocbq *elsiocb;
2429 struct lpfc_sli_ring *pring;
2430 struct lpfc_sli *psli;
2431 uint8_t *pcmd;
2432 uint16_t cmdsize;
2433 int rc;
2435 psli = &phba->sli;
2436 pring = &psli->ring[LPFC_ELS_RING]; /* ELS ring */
2438 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
2439 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2440 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
2441 if (!elsiocb)
2442 return 1;
2444 icmd = &elsiocb->iocb;
2445 oldcmd = &oldiocb->iocb;
2446 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2447 /* Xmit PRLI ACC response tag <ulpIoTag> */
2448 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2449 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
2450 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
2451 elsiocb->iotag, elsiocb->iocb.ulpContext,
2452 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2453 ndlp->nlp_rpi);
2454 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2456 *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
2457 pcmd += sizeof(uint32_t);
2459 /* For PRLI, remainder of payload is PRLI parameter page */
2460 memset(pcmd, 0, sizeof(PRLI));
2462 npr = (PRLI *) pcmd;
2463 vpd = &phba->vpd;
2465 * If our firmware version is 3.20 or later,
2466 * set the following bits for FC-TAPE support.
2468 if (vpd->rev.feaLevelHigh >= 0x02) {
2469 npr->ConfmComplAllowed = 1;
2470 npr->Retry = 1;
2471 npr->TaskRetryIdReq = 1;
2474 npr->acceptRspCode = PRLI_REQ_EXECUTED;
2475 npr->estabImagePair = 1;
2476 npr->readXferRdyDis = 1;
2477 npr->ConfmComplAllowed = 1;
2479 npr->prliType = PRLI_FCP_TYPE;
2480 npr->initiatorFunc = 1;
2482 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2483 "Issue ACC PRLI: did:x%x flg:x%x",
2484 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2486 phba->fc_stat.elsXmitACC++;
2487 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2489 rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2490 if (rc == IOCB_ERROR) {
2491 lpfc_els_free_iocb(phba, elsiocb);
2492 return 1;
2494 return 0;
2497 static int
2498 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
2499 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
2501 struct lpfc_hba *phba = vport->phba;
2502 RNID *rn;
2503 IOCB_t *icmd, *oldcmd;
2504 struct lpfc_iocbq *elsiocb;
2505 struct lpfc_sli_ring *pring;
2506 struct lpfc_sli *psli;
2507 uint8_t *pcmd;
2508 uint16_t cmdsize;
2509 int rc;
2511 psli = &phba->sli;
2512 pring = &psli->ring[LPFC_ELS_RING];
2514 cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
2515 + (2 * sizeof(struct lpfc_name));
2516 if (format)
2517 cmdsize += sizeof(RNID_TOP_DISC);
2519 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2520 ndlp->nlp_DID, ELS_CMD_ACC);
2521 if (!elsiocb)
2522 return 1;
2524 icmd = &elsiocb->iocb;
2525 oldcmd = &oldiocb->iocb;
2526 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2527 /* Xmit RNID ACC response tag <ulpIoTag> */
2528 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2529 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
2530 elsiocb->iotag, elsiocb->iocb.ulpContext);
2531 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2532 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2533 pcmd += sizeof(uint32_t);
2535 memset(pcmd, 0, sizeof(RNID));
2536 rn = (RNID *) (pcmd);
2537 rn->Format = format;
2538 rn->CommonLen = (2 * sizeof(struct lpfc_name));
2539 memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2540 memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2541 switch (format) {
2542 case 0:
2543 rn->SpecificLen = 0;
2544 break;
2545 case RNID_TOPOLOGY_DISC:
2546 rn->SpecificLen = sizeof(RNID_TOP_DISC);
2547 memcpy(&rn->un.topologyDisc.portName,
2548 &vport->fc_portname, sizeof(struct lpfc_name));
2549 rn->un.topologyDisc.unitType = RNID_HBA;
2550 rn->un.topologyDisc.physPort = 0;
2551 rn->un.topologyDisc.attachedNodes = 0;
2552 break;
2553 default:
2554 rn->CommonLen = 0;
2555 rn->SpecificLen = 0;
2556 break;
2559 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2560 "Issue ACC RNID: did:x%x flg:x%x",
2561 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2563 phba->fc_stat.elsXmitACC++;
2564 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2565 lpfc_nlp_put(ndlp);
2566 elsiocb->context1 = NULL; /* Don't need ndlp for cmpl,
2567 * it could be freed */
2569 rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2570 if (rc == IOCB_ERROR) {
2571 lpfc_els_free_iocb(phba, elsiocb);
2572 return 1;
2574 return 0;
2578 lpfc_els_disc_adisc(struct lpfc_vport *vport)
2580 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2581 struct lpfc_nodelist *ndlp, *next_ndlp;
2582 int sentadisc = 0;
2584 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2585 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2586 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2587 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
2588 (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
2589 spin_lock_irq(shost->host_lock);
2590 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2591 spin_unlock_irq(shost->host_lock);
2592 ndlp->nlp_prev_state = ndlp->nlp_state;
2593 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2594 lpfc_issue_els_adisc(vport, ndlp, 0);
2595 sentadisc++;
2596 vport->num_disc_nodes++;
2597 if (vport->num_disc_nodes >=
2598 vport->cfg_discovery_threads) {
2599 spin_lock_irq(shost->host_lock);
2600 vport->fc_flag |= FC_NLP_MORE;
2601 spin_unlock_irq(shost->host_lock);
2602 break;
2606 if (sentadisc == 0) {
2607 spin_lock_irq(shost->host_lock);
2608 vport->fc_flag &= ~FC_NLP_MORE;
2609 spin_unlock_irq(shost->host_lock);
2611 return sentadisc;
2615 lpfc_els_disc_plogi(struct lpfc_vport *vport)
2617 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2618 struct lpfc_nodelist *ndlp, *next_ndlp;
2619 int sentplogi = 0;
2621 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
2622 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2623 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2624 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
2625 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
2626 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
2627 ndlp->nlp_prev_state = ndlp->nlp_state;
2628 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2629 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2630 sentplogi++;
2631 vport->num_disc_nodes++;
2632 if (vport->num_disc_nodes >=
2633 vport->cfg_discovery_threads) {
2634 spin_lock_irq(shost->host_lock);
2635 vport->fc_flag |= FC_NLP_MORE;
2636 spin_unlock_irq(shost->host_lock);
2637 break;
2641 if (sentplogi == 0) {
2642 spin_lock_irq(shost->host_lock);
2643 vport->fc_flag &= ~FC_NLP_MORE;
2644 spin_unlock_irq(shost->host_lock);
2646 return sentplogi;
2649 void
2650 lpfc_els_flush_rscn(struct lpfc_vport *vport)
2652 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2653 struct lpfc_hba *phba = vport->phba;
2654 int i;
2656 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2657 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
2658 vport->fc_rscn_id_list[i] = NULL;
2660 spin_lock_irq(shost->host_lock);
2661 vport->fc_rscn_id_cnt = 0;
2662 vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
2663 spin_unlock_irq(shost->host_lock);
2664 lpfc_can_disctmo(vport);
2668 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
2670 D_ID ns_did;
2671 D_ID rscn_did;
2672 uint32_t *lp;
2673 uint32_t payload_len, i;
2675 ns_did.un.word = did;
2677 /* Never match fabric nodes for RSCNs */
2678 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
2679 return 0;
2681 /* If we are doing a FULL RSCN rediscovery, match everything */
2682 if (vport->fc_flag & FC_RSCN_DISCOVERY)
2683 return did;
2685 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2686 lp = vport->fc_rscn_id_list[i]->virt;
2687 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
2688 payload_len -= sizeof(uint32_t); /* take off word 0 */
2689 while (payload_len) {
2690 rscn_did.un.word = be32_to_cpu(*lp++);
2691 payload_len -= sizeof(uint32_t);
2692 switch (rscn_did.un.b.resv) {
2693 case 0: /* Single N_Port ID effected */
2694 if (ns_did.un.word == rscn_did.un.word)
2695 return did;
2696 break;
2697 case 1: /* Whole N_Port Area effected */
2698 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
2699 && (ns_did.un.b.area == rscn_did.un.b.area))
2700 return did;
2701 break;
2702 case 2: /* Whole N_Port Domain effected */
2703 if (ns_did.un.b.domain == rscn_did.un.b.domain)
2704 return did;
2705 break;
2706 default:
2707 /* Unknown Identifier in RSCN node */
2708 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2709 "0217 Unknown Identifier in "
2710 "RSCN payload Data: x%x\n",
2711 rscn_did.un.word);
2712 case 3: /* Whole Fabric effected */
2713 return did;
2717 return 0;
2720 static int
2721 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
2723 struct lpfc_nodelist *ndlp = NULL;
2725 /* Look at all nodes effected by pending RSCNs and move
2726 * them to NPR state.
2729 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2730 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE ||
2731 lpfc_rscn_payload_check(vport, ndlp->nlp_DID) == 0)
2732 continue;
2734 lpfc_disc_state_machine(vport, ndlp, NULL,
2735 NLP_EVT_DEVICE_RECOVERY);
2738 * Make sure NLP_DELAY_TMO is NOT running after a device
2739 * recovery event.
2741 if (ndlp->nlp_flag & NLP_DELAY_TMO)
2742 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2745 return 0;
2748 static int
2749 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
2750 struct lpfc_nodelist *ndlp, uint8_t newnode)
2752 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2753 struct lpfc_hba *phba = vport->phba;
2754 struct lpfc_dmabuf *pcmd;
2755 uint32_t *lp, *datap;
2756 IOCB_t *icmd;
2757 uint32_t payload_len, length, nportid, *cmd;
2758 int rscn_cnt = vport->fc_rscn_id_cnt;
2759 int rscn_id = 0, hba_id = 0;
2760 int i;
2762 icmd = &cmdiocb->iocb;
2763 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2764 lp = (uint32_t *) pcmd->virt;
2766 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
2767 payload_len -= sizeof(uint32_t); /* take off word 0 */
2768 /* RSCN received */
2769 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2770 "0214 RSCN received Data: x%x x%x x%x x%x\n",
2771 vport->fc_flag, payload_len, *lp, rscn_cnt);
2772 for (i = 0; i < payload_len/sizeof(uint32_t); i++)
2773 fc_host_post_event(shost, fc_get_event_number(),
2774 FCH_EVT_RSCN, lp[i]);
2776 /* If we are about to begin discovery, just ACC the RSCN.
2777 * Discovery processing will satisfy it.
2779 if (vport->port_state <= LPFC_NS_QRY) {
2780 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2781 "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
2782 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2784 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
2785 newnode);
2786 return 0;
2789 /* If this RSCN just contains NPortIDs for other vports on this HBA,
2790 * just ACC and ignore it.
2792 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2793 !(vport->cfg_peer_port_login)) {
2794 i = payload_len;
2795 datap = lp;
2796 while (i > 0) {
2797 nportid = *datap++;
2798 nportid = ((be32_to_cpu(nportid)) & Mask_DID);
2799 i -= sizeof(uint32_t);
2800 rscn_id++;
2801 if (lpfc_find_vport_by_did(phba, nportid))
2802 hba_id++;
2804 if (rscn_id == hba_id) {
2805 /* ALL NPortIDs in RSCN are on HBA */
2806 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2807 "0214 Ignore RSCN "
2808 "Data: x%x x%x x%x x%x\n",
2809 vport->fc_flag, payload_len,
2810 *lp, rscn_cnt);
2811 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2812 "RCV RSCN vport: did:x%x/ste:x%x flg:x%x",
2813 ndlp->nlp_DID, vport->port_state,
2814 ndlp->nlp_flag);
2816 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
2817 ndlp, NULL, newnode);
2818 return 0;
2822 /* If we are already processing an RSCN, save the received
2823 * RSCN payload buffer, cmdiocb->context2 to process later.
2825 if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
2826 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2827 "RCV RSCN defer: did:x%x/ste:x%x flg:x%x",
2828 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2830 vport->fc_flag |= FC_RSCN_DEFERRED;
2831 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
2832 !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
2833 spin_lock_irq(shost->host_lock);
2834 vport->fc_flag |= FC_RSCN_MODE;
2835 spin_unlock_irq(shost->host_lock);
2836 if (rscn_cnt) {
2837 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
2838 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
2840 if ((rscn_cnt) &&
2841 (payload_len + length <= LPFC_BPL_SIZE)) {
2842 *cmd &= ELS_CMD_MASK;
2843 *cmd |= be32_to_cpu(payload_len + length);
2844 memcpy(((uint8_t *)cmd) + length, lp,
2845 payload_len);
2846 } else {
2847 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
2848 vport->fc_rscn_id_cnt++;
2849 /* If we zero, cmdiocb->context2, the calling
2850 * routine will not try to free it.
2852 cmdiocb->context2 = NULL;
2855 /* Deferred RSCN */
2856 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2857 "0235 Deferred RSCN "
2858 "Data: x%x x%x x%x\n",
2859 vport->fc_rscn_id_cnt, vport->fc_flag,
2860 vport->port_state);
2861 } else {
2862 spin_lock_irq(shost->host_lock);
2863 vport->fc_flag |= FC_RSCN_DISCOVERY;
2864 spin_unlock_irq(shost->host_lock);
2865 /* ReDiscovery RSCN */
2866 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2867 "0234 ReDiscovery RSCN "
2868 "Data: x%x x%x x%x\n",
2869 vport->fc_rscn_id_cnt, vport->fc_flag,
2870 vport->port_state);
2872 /* Send back ACC */
2873 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
2874 newnode);
2876 /* send RECOVERY event for ALL nodes that match RSCN payload */
2877 lpfc_rscn_recovery_check(vport);
2878 vport->fc_flag &= ~FC_RSCN_DEFERRED;
2879 return 0;
2882 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2883 "RCV RSCN: did:x%x/ste:x%x flg:x%x",
2884 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2886 spin_lock_irq(shost->host_lock);
2887 vport->fc_flag |= FC_RSCN_MODE;
2888 spin_unlock_irq(shost->host_lock);
2889 vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
2891 * If we zero, cmdiocb->context2, the calling routine will
2892 * not try to free it.
2894 cmdiocb->context2 = NULL;
2896 lpfc_set_disctmo(vport);
2898 /* Send back ACC */
2899 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, newnode);
2901 /* send RECOVERY event for ALL nodes that match RSCN payload */
2902 lpfc_rscn_recovery_check(vport);
2904 return lpfc_els_handle_rscn(vport);
2908 lpfc_els_handle_rscn(struct lpfc_vport *vport)
2910 struct lpfc_nodelist *ndlp;
2911 struct lpfc_hba *phba = vport->phba;
2913 /* Ignore RSCN if the port is being torn down. */
2914 if (vport->load_flag & FC_UNLOADING) {
2915 lpfc_els_flush_rscn(vport);
2916 return 0;
2919 /* Start timer for RSCN processing */
2920 lpfc_set_disctmo(vport);
2922 /* RSCN processed */
2923 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2924 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
2925 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
2926 vport->port_state);
2928 /* To process RSCN, first compare RSCN data with NameServer */
2929 vport->fc_ns_retry = 0;
2930 ndlp = lpfc_findnode_did(vport, NameServer_DID);
2931 if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
2932 /* Good ndlp, issue CT Request to NameServer */
2933 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
2934 /* Wait for NameServer query cmpl before we can
2935 continue */
2936 return 1;
2937 } else {
2938 /* If login to NameServer does not exist, issue one */
2939 /* Good status, issue PLOGI to NameServer */
2940 ndlp = lpfc_findnode_did(vport, NameServer_DID);
2941 if (ndlp)
2942 /* Wait for NameServer login cmpl before we can
2943 continue */
2944 return 1;
2946 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2947 if (!ndlp) {
2948 lpfc_els_flush_rscn(vport);
2949 return 0;
2950 } else {
2951 lpfc_nlp_init(vport, ndlp, NameServer_DID);
2952 ndlp->nlp_type |= NLP_FABRIC;
2953 ndlp->nlp_prev_state = ndlp->nlp_state;
2954 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2955 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
2956 /* Wait for NameServer login cmpl before we can
2957 continue */
2958 return 1;
2962 lpfc_els_flush_rscn(vport);
2963 return 0;
2966 static int
2967 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
2968 struct lpfc_nodelist *ndlp, uint8_t newnode)
2970 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2971 struct lpfc_hba *phba = vport->phba;
2972 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2973 uint32_t *lp = (uint32_t *) pcmd->virt;
2974 IOCB_t *icmd = &cmdiocb->iocb;
2975 struct serv_parm *sp;
2976 LPFC_MBOXQ_t *mbox;
2977 struct ls_rjt stat;
2978 uint32_t cmd, did;
2979 int rc;
2981 cmd = *lp++;
2982 sp = (struct serv_parm *) lp;
2984 /* FLOGI received */
2986 lpfc_set_disctmo(vport);
2988 if (phba->fc_topology == TOPOLOGY_LOOP) {
2989 /* We should never receive a FLOGI in loop mode, ignore it */
2990 did = icmd->un.elsreq64.remoteID;
2992 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
2993 Loop Mode */
2994 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2995 "0113 An FLOGI ELS command x%x was "
2996 "received from DID x%x in Loop Mode\n",
2997 cmd, did);
2998 return 1;
3001 did = Fabric_DID;
3003 if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
3004 /* For a FLOGI we accept, then if our portname is greater
3005 * then the remote portname we initiate Nport login.
3008 rc = memcmp(&vport->fc_portname, &sp->portName,
3009 sizeof(struct lpfc_name));
3011 if (!rc) {
3012 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3013 if (!mbox)
3014 return 1;
3016 lpfc_linkdown(phba);
3017 lpfc_init_link(phba, mbox,
3018 phba->cfg_topology,
3019 phba->cfg_link_speed);
3020 mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
3021 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3022 mbox->vport = vport;
3023 rc = lpfc_sli_issue_mbox
3024 (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
3025 lpfc_set_loopback_flag(phba);
3026 if (rc == MBX_NOT_FINISHED) {
3027 mempool_free(mbox, phba->mbox_mem_pool);
3029 return 1;
3030 } else if (rc > 0) { /* greater than */
3031 spin_lock_irq(shost->host_lock);
3032 vport->fc_flag |= FC_PT2PT_PLOGI;
3033 spin_unlock_irq(shost->host_lock);
3035 spin_lock_irq(shost->host_lock);
3036 vport->fc_flag |= FC_PT2PT;
3037 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3038 spin_unlock_irq(shost->host_lock);
3039 } else {
3040 /* Reject this request because invalid parameters */
3041 stat.un.b.lsRjtRsvd0 = 0;
3042 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3043 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
3044 stat.un.b.vendorUnique = 0;
3045 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3046 NULL);
3047 return 1;
3050 /* Send back ACC */
3051 lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL, newnode);
3053 return 0;
3056 static int
3057 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3058 struct lpfc_nodelist *ndlp)
3060 struct lpfc_dmabuf *pcmd;
3061 uint32_t *lp;
3062 IOCB_t *icmd;
3063 RNID *rn;
3064 struct ls_rjt stat;
3065 uint32_t cmd, did;
3067 icmd = &cmdiocb->iocb;
3068 did = icmd->un.elsreq64.remoteID;
3069 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3070 lp = (uint32_t *) pcmd->virt;
3072 cmd = *lp++;
3073 rn = (RNID *) lp;
3075 /* RNID received */
3077 switch (rn->Format) {
3078 case 0:
3079 case RNID_TOPOLOGY_DISC:
3080 /* Send back ACC */
3081 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
3082 break;
3083 default:
3084 /* Reject this request because format not supported */
3085 stat.un.b.lsRjtRsvd0 = 0;
3086 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3087 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3088 stat.un.b.vendorUnique = 0;
3089 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3090 NULL);
3092 return 0;
3095 static int
3096 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3097 struct lpfc_nodelist *ndlp)
3099 struct ls_rjt stat;
3101 /* For now, unconditionally reject this command */
3102 stat.un.b.lsRjtRsvd0 = 0;
3103 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3104 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3105 stat.un.b.vendorUnique = 0;
3106 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3107 return 0;
3110 static void
3111 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3113 struct lpfc_sli *psli = &phba->sli;
3114 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3115 MAILBOX_t *mb;
3116 IOCB_t *icmd;
3117 RPS_RSP *rps_rsp;
3118 uint8_t *pcmd;
3119 struct lpfc_iocbq *elsiocb;
3120 struct lpfc_nodelist *ndlp;
3121 uint16_t xri, status;
3122 uint32_t cmdsize;
3124 mb = &pmb->mb;
3126 ndlp = (struct lpfc_nodelist *) pmb->context2;
3127 xri = (uint16_t) ((unsigned long)(pmb->context1));
3128 pmb->context1 = NULL;
3129 pmb->context2 = NULL;
3131 if (mb->mbxStatus) {
3132 mempool_free(pmb, phba->mbox_mem_pool);
3133 return;
3136 cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
3137 mempool_free(pmb, phba->mbox_mem_pool);
3138 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
3139 lpfc_max_els_tries, ndlp,
3140 ndlp->nlp_DID, ELS_CMD_ACC);
3141 lpfc_nlp_put(ndlp);
3142 if (!elsiocb)
3143 return;
3145 icmd = &elsiocb->iocb;
3146 icmd->ulpContext = xri;
3148 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3149 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3150 pcmd += sizeof(uint32_t); /* Skip past command */
3151 rps_rsp = (RPS_RSP *)pcmd;
3153 if (phba->fc_topology != TOPOLOGY_LOOP)
3154 status = 0x10;
3155 else
3156 status = 0x8;
3157 if (phba->pport->fc_flag & FC_FABRIC)
3158 status |= 0x4;
3160 rps_rsp->rsvd1 = 0;
3161 rps_rsp->portStatus = be16_to_cpu(status);
3162 rps_rsp->linkFailureCnt = be32_to_cpu(mb->un.varRdLnk.linkFailureCnt);
3163 rps_rsp->lossSyncCnt = be32_to_cpu(mb->un.varRdLnk.lossSyncCnt);
3164 rps_rsp->lossSignalCnt = be32_to_cpu(mb->un.varRdLnk.lossSignalCnt);
3165 rps_rsp->primSeqErrCnt = be32_to_cpu(mb->un.varRdLnk.primSeqErrCnt);
3166 rps_rsp->invalidXmitWord = be32_to_cpu(mb->un.varRdLnk.invalidXmitWord);
3167 rps_rsp->crcCnt = be32_to_cpu(mb->un.varRdLnk.crcCnt);
3168 /* Xmit ELS RPS ACC response tag <ulpIoTag> */
3169 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
3170 "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
3171 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3172 elsiocb->iotag, elsiocb->iocb.ulpContext,
3173 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3174 ndlp->nlp_rpi);
3175 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3176 phba->fc_stat.elsXmitACC++;
3177 if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
3178 lpfc_els_free_iocb(phba, elsiocb);
3179 return;
3182 static int
3183 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3184 struct lpfc_nodelist *ndlp)
3186 struct lpfc_hba *phba = vport->phba;
3187 uint32_t *lp;
3188 uint8_t flag;
3189 LPFC_MBOXQ_t *mbox;
3190 struct lpfc_dmabuf *pcmd;
3191 RPS *rps;
3192 struct ls_rjt stat;
3194 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3195 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3196 stat.un.b.lsRjtRsvd0 = 0;
3197 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3198 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3199 stat.un.b.vendorUnique = 0;
3200 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3201 NULL);
3204 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3205 lp = (uint32_t *) pcmd->virt;
3206 flag = (be32_to_cpu(*lp++) & 0xf);
3207 rps = (RPS *) lp;
3209 if ((flag == 0) ||
3210 ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
3211 ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
3212 sizeof(struct lpfc_name)) == 0))) {
3214 printk("Fix me....\n");
3215 dump_stack();
3216 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
3217 if (mbox) {
3218 lpfc_read_lnk_stat(phba, mbox);
3219 mbox->context1 =
3220 (void *)((unsigned long) cmdiocb->iocb.ulpContext);
3221 mbox->context2 = lpfc_nlp_get(ndlp);
3222 mbox->vport = vport;
3223 mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
3224 if (lpfc_sli_issue_mbox (phba, mbox,
3225 (MBX_NOWAIT | MBX_STOP_IOCB)) != MBX_NOT_FINISHED)
3226 /* Mbox completion will send ELS Response */
3227 return 0;
3229 lpfc_nlp_put(ndlp);
3230 mempool_free(mbox, phba->mbox_mem_pool);
3233 stat.un.b.lsRjtRsvd0 = 0;
3234 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3235 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3236 stat.un.b.vendorUnique = 0;
3237 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3238 return 0;
3241 static int
3242 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
3243 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3245 struct lpfc_hba *phba = vport->phba;
3246 IOCB_t *icmd, *oldcmd;
3247 RPL_RSP rpl_rsp;
3248 struct lpfc_iocbq *elsiocb;
3249 struct lpfc_sli *psli = &phba->sli;
3250 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3251 uint8_t *pcmd;
3253 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3254 ndlp->nlp_DID, ELS_CMD_ACC);
3256 if (!elsiocb)
3257 return 1;
3259 icmd = &elsiocb->iocb;
3260 oldcmd = &oldiocb->iocb;
3261 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3263 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3264 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3265 pcmd += sizeof(uint16_t);
3266 *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
3267 pcmd += sizeof(uint16_t);
3269 /* Setup the RPL ACC payload */
3270 rpl_rsp.listLen = be32_to_cpu(1);
3271 rpl_rsp.index = 0;
3272 rpl_rsp.port_num_blk.portNum = 0;
3273 rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
3274 memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
3275 sizeof(struct lpfc_name));
3276 memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
3277 /* Xmit ELS RPL ACC response tag <ulpIoTag> */
3278 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3279 "0120 Xmit ELS RPL ACC response tag x%x "
3280 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3281 "rpi x%x\n",
3282 elsiocb->iotag, elsiocb->iocb.ulpContext,
3283 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3284 ndlp->nlp_rpi);
3285 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3286 phba->fc_stat.elsXmitACC++;
3287 if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
3288 lpfc_els_free_iocb(phba, elsiocb);
3289 return 1;
3291 return 0;
3294 static int
3295 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3296 struct lpfc_nodelist *ndlp)
3298 struct lpfc_dmabuf *pcmd;
3299 uint32_t *lp;
3300 uint32_t maxsize;
3301 uint16_t cmdsize;
3302 RPL *rpl;
3303 struct ls_rjt stat;
3305 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3306 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3307 stat.un.b.lsRjtRsvd0 = 0;
3308 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3309 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3310 stat.un.b.vendorUnique = 0;
3311 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3312 NULL);
3315 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3316 lp = (uint32_t *) pcmd->virt;
3317 rpl = (RPL *) (lp + 1);
3319 maxsize = be32_to_cpu(rpl->maxsize);
3321 /* We support only one port */
3322 if ((rpl->index == 0) &&
3323 ((maxsize == 0) ||
3324 ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
3325 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
3326 } else {
3327 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
3329 lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
3331 return 0;
3334 static int
3335 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3336 struct lpfc_nodelist *ndlp)
3338 struct lpfc_dmabuf *pcmd;
3339 uint32_t *lp;
3340 IOCB_t *icmd;
3341 FARP *fp;
3342 uint32_t cmd, cnt, did;
3344 icmd = &cmdiocb->iocb;
3345 did = icmd->un.elsreq64.remoteID;
3346 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3347 lp = (uint32_t *) pcmd->virt;
3349 cmd = *lp++;
3350 fp = (FARP *) lp;
3351 /* FARP-REQ received from DID <did> */
3352 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3353 "0601 FARP-REQ received from DID x%x\n", did);
3354 /* We will only support match on WWPN or WWNN */
3355 if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
3356 return 0;
3359 cnt = 0;
3360 /* If this FARP command is searching for my portname */
3361 if (fp->Mflags & FARP_MATCH_PORT) {
3362 if (memcmp(&fp->RportName, &vport->fc_portname,
3363 sizeof(struct lpfc_name)) == 0)
3364 cnt = 1;
3367 /* If this FARP command is searching for my nodename */
3368 if (fp->Mflags & FARP_MATCH_NODE) {
3369 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
3370 sizeof(struct lpfc_name)) == 0)
3371 cnt = 1;
3374 if (cnt) {
3375 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
3376 (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
3377 /* Log back into the node before sending the FARP. */
3378 if (fp->Rflags & FARP_REQUEST_PLOGI) {
3379 ndlp->nlp_prev_state = ndlp->nlp_state;
3380 lpfc_nlp_set_state(vport, ndlp,
3381 NLP_STE_PLOGI_ISSUE);
3382 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3385 /* Send a FARP response to that node */
3386 if (fp->Rflags & FARP_REQUEST_FARPR)
3387 lpfc_issue_els_farpr(vport, did, 0);
3390 return 0;
3393 static int
3394 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3395 struct lpfc_nodelist *ndlp)
3397 struct lpfc_dmabuf *pcmd;
3398 uint32_t *lp;
3399 IOCB_t *icmd;
3400 uint32_t cmd, did;
3402 icmd = &cmdiocb->iocb;
3403 did = icmd->un.elsreq64.remoteID;
3404 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3405 lp = (uint32_t *) pcmd->virt;
3407 cmd = *lp++;
3408 /* FARP-RSP received from DID <did> */
3409 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3410 "0600 FARP-RSP received from DID x%x\n", did);
3411 /* ACCEPT the Farp resp request */
3412 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
3414 return 0;
3417 static int
3418 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3419 struct lpfc_nodelist *fan_ndlp)
3421 struct lpfc_dmabuf *pcmd;
3422 uint32_t *lp;
3423 IOCB_t *icmd;
3424 uint32_t cmd, did;
3425 FAN *fp;
3426 struct lpfc_nodelist *ndlp, *next_ndlp;
3427 struct lpfc_hba *phba = vport->phba;
3429 /* FAN received */
3430 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3431 "0265 FAN received\n");
3432 icmd = &cmdiocb->iocb;
3433 did = icmd->un.elsreq64.remoteID;
3434 pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
3435 lp = (uint32_t *)pcmd->virt;
3437 cmd = *lp++;
3438 fp = (FAN *) lp;
3440 /* FAN received; Fan does not have a reply sequence */
3442 if (phba->pport->port_state == LPFC_LOCAL_CFG_LINK) {
3443 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
3444 sizeof(struct lpfc_name)) != 0) ||
3445 (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
3446 sizeof(struct lpfc_name)) != 0)) {
3448 * This node has switched fabrics. FLOGI is required
3449 * Clean up the old rpi's
3452 list_for_each_entry_safe(ndlp, next_ndlp,
3453 &vport->fc_nodes, nlp_listp) {
3454 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3455 continue;
3456 if (ndlp->nlp_type & NLP_FABRIC) {
3458 * Clean up old Fabric, Nameserver and
3459 * other NLP_FABRIC logins
3461 lpfc_drop_node(vport, ndlp);
3462 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3463 /* Fail outstanding I/O now since this
3464 * device is marked for PLOGI
3466 lpfc_unreg_rpi(vport, ndlp);
3470 vport->port_state = LPFC_FLOGI;
3471 lpfc_set_disctmo(vport);
3472 lpfc_initial_flogi(vport);
3473 return 0;
3475 /* Discovery not needed,
3476 * move the nodes to their original state.
3478 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3479 nlp_listp) {
3480 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3481 continue;
3483 switch (ndlp->nlp_prev_state) {
3484 case NLP_STE_UNMAPPED_NODE:
3485 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
3486 lpfc_nlp_set_state(vport, ndlp,
3487 NLP_STE_UNMAPPED_NODE);
3488 break;
3490 case NLP_STE_MAPPED_NODE:
3491 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
3492 lpfc_nlp_set_state(vport, ndlp,
3493 NLP_STE_MAPPED_NODE);
3494 break;
3496 default:
3497 break;
3501 /* Start discovery - this should just do CLEAR_LA */
3502 lpfc_disc_start(vport);
3504 return 0;
3507 void
3508 lpfc_els_timeout(unsigned long ptr)
3510 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3511 struct lpfc_hba *phba = vport->phba;
3512 unsigned long iflag;
3514 spin_lock_irqsave(&vport->work_port_lock, iflag);
3515 if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
3516 vport->work_port_events |= WORKER_ELS_TMO;
3517 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3519 spin_lock_irqsave(&phba->hbalock, iflag);
3520 if (phba->work_wait)
3521 lpfc_worker_wake_up(phba);
3522 spin_unlock_irqrestore(&phba->hbalock, iflag);
3524 else
3525 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3526 return;
3529 void
3530 lpfc_els_timeout_handler(struct lpfc_vport *vport)
3532 struct lpfc_hba *phba = vport->phba;
3533 struct lpfc_sli_ring *pring;
3534 struct lpfc_iocbq *tmp_iocb, *piocb;
3535 IOCB_t *cmd = NULL;
3536 struct lpfc_dmabuf *pcmd;
3537 uint32_t els_command = 0;
3538 uint32_t timeout;
3539 uint32_t remote_ID = 0xffffffff;
3541 /* If the timer is already canceled do nothing */
3542 if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
3543 return;
3545 spin_lock_irq(&phba->hbalock);
3546 timeout = (uint32_t)(phba->fc_ratov << 1);
3548 pring = &phba->sli.ring[LPFC_ELS_RING];
3550 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3551 cmd = &piocb->iocb;
3553 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
3554 piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
3555 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
3556 continue;
3558 if (piocb->vport != vport)
3559 continue;
3561 pcmd = (struct lpfc_dmabuf *) piocb->context2;
3562 if (pcmd)
3563 els_command = *(uint32_t *) (pcmd->virt);
3565 if (els_command == ELS_CMD_FARP ||
3566 els_command == ELS_CMD_FARPR ||
3567 els_command == ELS_CMD_FDISC)
3568 continue;
3570 if (vport != piocb->vport)
3571 continue;
3573 if (piocb->drvrTimeout > 0) {
3574 if (piocb->drvrTimeout >= timeout)
3575 piocb->drvrTimeout -= timeout;
3576 else
3577 piocb->drvrTimeout = 0;
3578 continue;
3581 remote_ID = 0xffffffff;
3582 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
3583 remote_ID = cmd->un.elsreq64.remoteID;
3584 else {
3585 struct lpfc_nodelist *ndlp;
3586 ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
3587 if (ndlp)
3588 remote_ID = ndlp->nlp_DID;
3590 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3591 "0127 ELS timeout Data: x%x x%x x%x "
3592 "x%x\n", els_command,
3593 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
3594 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3596 spin_unlock_irq(&phba->hbalock);
3598 if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
3599 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
3602 void
3603 lpfc_els_flush_cmd(struct lpfc_vport *vport)
3605 LIST_HEAD(completions);
3606 struct lpfc_hba *phba = vport->phba;
3607 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3608 struct lpfc_iocbq *tmp_iocb, *piocb;
3609 IOCB_t *cmd = NULL;
3611 lpfc_fabric_abort_vport(vport);
3613 spin_lock_irq(&phba->hbalock);
3614 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
3615 cmd = &piocb->iocb;
3617 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
3618 continue;
3621 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
3622 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
3623 cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
3624 cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3625 cmd->ulpCommand == CMD_ABORT_XRI_CN)
3626 continue;
3628 if (piocb->vport != vport)
3629 continue;
3631 list_move_tail(&piocb->list, &completions);
3632 pring->txq_cnt--;
3635 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3636 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
3637 continue;
3640 if (piocb->vport != vport)
3641 continue;
3643 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3645 spin_unlock_irq(&phba->hbalock);
3647 while (!list_empty(&completions)) {
3648 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
3649 cmd = &piocb->iocb;
3650 list_del_init(&piocb->list);
3652 if (!piocb->iocb_cmpl)
3653 lpfc_sli_release_iocbq(phba, piocb);
3654 else {
3655 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3656 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
3657 (piocb->iocb_cmpl) (phba, piocb, piocb);
3661 return;
3664 void
3665 lpfc_els_flush_all_cmd(struct lpfc_hba *phba)
3667 LIST_HEAD(completions);
3668 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3669 struct lpfc_iocbq *tmp_iocb, *piocb;
3670 IOCB_t *cmd = NULL;
3672 lpfc_fabric_abort_hba(phba);
3673 spin_lock_irq(&phba->hbalock);
3674 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
3675 cmd = &piocb->iocb;
3676 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
3677 continue;
3678 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
3679 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
3680 cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
3681 cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3682 cmd->ulpCommand == CMD_ABORT_XRI_CN)
3683 continue;
3684 list_move_tail(&piocb->list, &completions);
3685 pring->txq_cnt--;
3687 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3688 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
3689 continue;
3690 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3692 spin_unlock_irq(&phba->hbalock);
3693 while (!list_empty(&completions)) {
3694 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
3695 cmd = &piocb->iocb;
3696 list_del_init(&piocb->list);
3697 if (!piocb->iocb_cmpl)
3698 lpfc_sli_release_iocbq(phba, piocb);
3699 else {
3700 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3701 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
3702 (piocb->iocb_cmpl) (phba, piocb, piocb);
3705 return;
3708 static void
3709 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3710 struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
3712 struct lpfc_nodelist *ndlp;
3713 struct ls_rjt stat;
3714 uint32_t *payload;
3715 uint32_t cmd, did, newnode, rjt_err = 0;
3716 IOCB_t *icmd = &elsiocb->iocb;
3718 if (vport == NULL || elsiocb->context2 == NULL)
3719 goto dropit;
3721 newnode = 0;
3722 payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3723 cmd = *payload;
3724 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
3725 lpfc_post_buffer(phba, pring, 1, 1);
3727 did = icmd->un.rcvels.remoteID;
3728 if (icmd->ulpStatus) {
3729 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3730 "RCV Unsol ELS: status:x%x/x%x did:x%x",
3731 icmd->ulpStatus, icmd->un.ulpWord[4], did);
3732 goto dropit;
3735 /* Check to see if link went down during discovery */
3736 if (lpfc_els_chk_latt(vport))
3737 goto dropit;
3739 /* Ignore traffic recevied during vport shutdown. */
3740 if (vport->load_flag & FC_UNLOADING)
3741 goto dropit;
3743 ndlp = lpfc_findnode_did(vport, did);
3744 if (!ndlp) {
3745 /* Cannot find existing Fabric ndlp, so allocate a new one */
3746 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3747 if (!ndlp)
3748 goto dropit;
3750 lpfc_nlp_init(vport, ndlp, did);
3751 newnode = 1;
3752 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK) {
3753 ndlp->nlp_type |= NLP_FABRIC;
3755 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
3758 phba->fc_stat.elsRcvFrame++;
3759 if (elsiocb->context1)
3760 lpfc_nlp_put(elsiocb->context1);
3761 elsiocb->context1 = lpfc_nlp_get(ndlp);
3762 elsiocb->vport = vport;
3764 if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
3765 cmd &= ELS_CMD_MASK;
3767 /* ELS command <elsCmd> received from NPORT <did> */
3768 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3769 "0112 ELS command x%x received from NPORT x%x "
3770 "Data: x%x\n", cmd, did, vport->port_state);
3771 switch (cmd) {
3772 case ELS_CMD_PLOGI:
3773 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3774 "RCV PLOGI: did:x%x/ste:x%x flg:x%x",
3775 did, vport->port_state, ndlp->nlp_flag);
3777 phba->fc_stat.elsRcvPLOGI++;
3778 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
3780 if (vport->port_state < LPFC_DISC_AUTH) {
3781 rjt_err = LSRJT_UNABLE_TPC;
3782 break;
3784 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3785 NLP_EVT_RCV_PLOGI);
3787 break;
3788 case ELS_CMD_FLOGI:
3789 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3790 "RCV FLOGI: did:x%x/ste:x%x flg:x%x",
3791 did, vport->port_state, ndlp->nlp_flag);
3793 phba->fc_stat.elsRcvFLOGI++;
3794 lpfc_els_rcv_flogi(vport, elsiocb, ndlp, newnode);
3795 if (newnode)
3796 lpfc_drop_node(vport, ndlp);
3797 break;
3798 case ELS_CMD_LOGO:
3799 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3800 "RCV LOGO: did:x%x/ste:x%x flg:x%x",
3801 did, vport->port_state, ndlp->nlp_flag);
3803 phba->fc_stat.elsRcvLOGO++;
3804 if (vport->port_state < LPFC_DISC_AUTH) {
3805 rjt_err = LSRJT_UNABLE_TPC;
3806 break;
3808 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
3809 break;
3810 case ELS_CMD_PRLO:
3811 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3812 "RCV PRLO: did:x%x/ste:x%x flg:x%x",
3813 did, vport->port_state, ndlp->nlp_flag);
3815 phba->fc_stat.elsRcvPRLO++;
3816 if (vport->port_state < LPFC_DISC_AUTH) {
3817 rjt_err = LSRJT_UNABLE_TPC;
3818 break;
3820 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
3821 break;
3822 case ELS_CMD_RSCN:
3823 phba->fc_stat.elsRcvRSCN++;
3824 lpfc_els_rcv_rscn(vport, elsiocb, ndlp, newnode);
3825 if (newnode)
3826 lpfc_drop_node(vport, ndlp);
3827 break;
3828 case ELS_CMD_ADISC:
3829 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3830 "RCV ADISC: did:x%x/ste:x%x flg:x%x",
3831 did, vport->port_state, ndlp->nlp_flag);
3833 phba->fc_stat.elsRcvADISC++;
3834 if (vport->port_state < LPFC_DISC_AUTH) {
3835 rjt_err = LSRJT_UNABLE_TPC;
3836 break;
3838 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3839 NLP_EVT_RCV_ADISC);
3840 break;
3841 case ELS_CMD_PDISC:
3842 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3843 "RCV PDISC: did:x%x/ste:x%x flg:x%x",
3844 did, vport->port_state, ndlp->nlp_flag);
3846 phba->fc_stat.elsRcvPDISC++;
3847 if (vport->port_state < LPFC_DISC_AUTH) {
3848 rjt_err = LSRJT_UNABLE_TPC;
3849 break;
3851 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3852 NLP_EVT_RCV_PDISC);
3853 break;
3854 case ELS_CMD_FARPR:
3855 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3856 "RCV FARPR: did:x%x/ste:x%x flg:x%x",
3857 did, vport->port_state, ndlp->nlp_flag);
3859 phba->fc_stat.elsRcvFARPR++;
3860 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
3861 break;
3862 case ELS_CMD_FARP:
3863 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3864 "RCV FARP: did:x%x/ste:x%x flg:x%x",
3865 did, vport->port_state, ndlp->nlp_flag);
3867 phba->fc_stat.elsRcvFARP++;
3868 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
3869 break;
3870 case ELS_CMD_FAN:
3871 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3872 "RCV FAN: did:x%x/ste:x%x flg:x%x",
3873 did, vport->port_state, ndlp->nlp_flag);
3875 phba->fc_stat.elsRcvFAN++;
3876 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
3877 break;
3878 case ELS_CMD_PRLI:
3879 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3880 "RCV PRLI: did:x%x/ste:x%x flg:x%x",
3881 did, vport->port_state, ndlp->nlp_flag);
3883 phba->fc_stat.elsRcvPRLI++;
3884 if (vport->port_state < LPFC_DISC_AUTH) {
3885 rjt_err = LSRJT_UNABLE_TPC;
3886 break;
3888 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
3889 break;
3890 case ELS_CMD_LIRR:
3891 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3892 "RCV LIRR: did:x%x/ste:x%x flg:x%x",
3893 did, vport->port_state, ndlp->nlp_flag);
3895 phba->fc_stat.elsRcvLIRR++;
3896 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
3897 if (newnode)
3898 lpfc_drop_node(vport, ndlp);
3899 break;
3900 case ELS_CMD_RPS:
3901 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3902 "RCV RPS: did:x%x/ste:x%x flg:x%x",
3903 did, vport->port_state, ndlp->nlp_flag);
3905 phba->fc_stat.elsRcvRPS++;
3906 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
3907 if (newnode)
3908 lpfc_drop_node(vport, ndlp);
3909 break;
3910 case ELS_CMD_RPL:
3911 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3912 "RCV RPL: did:x%x/ste:x%x flg:x%x",
3913 did, vport->port_state, ndlp->nlp_flag);
3915 phba->fc_stat.elsRcvRPL++;
3916 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
3917 if (newnode)
3918 lpfc_drop_node(vport, ndlp);
3919 break;
3920 case ELS_CMD_RNID:
3921 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3922 "RCV RNID: did:x%x/ste:x%x flg:x%x",
3923 did, vport->port_state, ndlp->nlp_flag);
3925 phba->fc_stat.elsRcvRNID++;
3926 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
3927 if (newnode)
3928 lpfc_drop_node(vport, ndlp);
3929 break;
3930 default:
3931 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3932 "RCV ELS cmd: cmd:x%x did:x%x/ste:x%x",
3933 cmd, did, vport->port_state);
3935 /* Unsupported ELS command, reject */
3936 rjt_err = LSRJT_INVALID_CMD;
3938 /* Unknown ELS command <elsCmd> received from NPORT <did> */
3939 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3940 "0115 Unknown ELS command x%x "
3941 "received from NPORT x%x\n", cmd, did);
3942 if (newnode)
3943 lpfc_drop_node(vport, ndlp);
3944 break;
3947 /* check if need to LS_RJT received ELS cmd */
3948 if (rjt_err) {
3949 memset(&stat, 0, sizeof(stat));
3950 stat.un.b.lsRjtRsnCode = rjt_err;
3951 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
3952 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
3953 NULL);
3954 if (newnode)
3955 lpfc_drop_node(vport, ndlp);
3958 return;
3960 dropit:
3961 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
3962 "(%d):0111 Dropping received ELS cmd "
3963 "Data: x%x x%x x%x\n",
3964 vport ? vport->vpi : 0xffff, icmd->ulpStatus,
3965 icmd->un.ulpWord[4], icmd->ulpTimeout);
3966 phba->fc_stat.elsRcvDrop++;
3969 static struct lpfc_vport *
3970 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
3972 struct lpfc_vport *vport;
3973 unsigned long flags;
3975 spin_lock_irqsave(&phba->hbalock, flags);
3976 list_for_each_entry(vport, &phba->port_list, listentry) {
3977 if (vport->vpi == vpi) {
3978 spin_unlock_irqrestore(&phba->hbalock, flags);
3979 return vport;
3982 spin_unlock_irqrestore(&phba->hbalock, flags);
3983 return NULL;
3986 void
3987 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3988 struct lpfc_iocbq *elsiocb)
3990 struct lpfc_vport *vport = phba->pport;
3991 IOCB_t *icmd = &elsiocb->iocb;
3992 dma_addr_t paddr;
3993 struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
3994 struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
3996 elsiocb->context2 = NULL;
3997 elsiocb->context3 = NULL;
3999 if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
4000 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
4001 } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
4002 (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
4003 phba->fc_stat.NoRcvBuf++;
4004 /* Not enough posted buffers; Try posting more buffers */
4005 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
4006 lpfc_post_buffer(phba, pring, 0, 1);
4007 return;
4010 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4011 (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
4012 icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
4013 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
4014 vport = phba->pport;
4015 else {
4016 uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
4017 vport = lpfc_find_vport_by_vpid(phba, vpi);
4020 /* If there are no BDEs associated
4021 * with this IOCB, there is nothing to do.
4023 if (icmd->ulpBdeCount == 0)
4024 return;
4026 /* type of ELS cmd is first 32bit word
4027 * in packet
4029 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4030 elsiocb->context2 = bdeBuf1;
4031 } else {
4032 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
4033 icmd->un.cont64[0].addrLow);
4034 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
4035 paddr);
4038 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4040 * The different unsolicited event handlers would tell us
4041 * if they are done with "mp" by setting context2 to NULL.
4043 lpfc_nlp_put(elsiocb->context1);
4044 elsiocb->context1 = NULL;
4045 if (elsiocb->context2) {
4046 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
4047 elsiocb->context2 = NULL;
4050 /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
4051 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
4052 icmd->ulpBdeCount == 2) {
4053 elsiocb->context2 = bdeBuf2;
4054 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4055 /* free mp if we are done with it */
4056 if (elsiocb->context2) {
4057 lpfc_in_buf_free(phba, elsiocb->context2);
4058 elsiocb->context2 = NULL;
4063 void
4064 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4066 struct lpfc_nodelist *ndlp, *ndlp_fdmi;
4068 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4069 if (!ndlp) {
4070 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4071 if (!ndlp) {
4072 if (phba->fc_topology == TOPOLOGY_LOOP) {
4073 lpfc_disc_start(vport);
4074 return;
4076 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4077 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4078 "0251 NameServer login: no memory\n");
4079 return;
4081 lpfc_nlp_init(vport, ndlp, NameServer_DID);
4082 ndlp->nlp_type |= NLP_FABRIC;
4085 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4087 if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
4088 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4089 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4090 "0252 Cannot issue NameServer login\n");
4091 return;
4094 if (vport->cfg_fdmi_on) {
4095 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
4096 GFP_KERNEL);
4097 if (ndlp_fdmi) {
4098 lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
4099 ndlp_fdmi->nlp_type |= NLP_FABRIC;
4100 ndlp_fdmi->nlp_state =
4101 NLP_STE_PLOGI_ISSUE;
4102 lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
4106 return;
4109 static void
4110 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4112 struct lpfc_vport *vport = pmb->vport;
4113 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4114 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4115 MAILBOX_t *mb = &pmb->mb;
4117 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4118 lpfc_nlp_put(ndlp);
4120 if (mb->mbxStatus) {
4121 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4122 "0915 Register VPI failed: 0x%x\n",
4123 mb->mbxStatus);
4125 switch (mb->mbxStatus) {
4126 case 0x11: /* unsupported feature */
4127 case 0x9603: /* max_vpi exceeded */
4128 /* giving up on vport registration */
4129 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4130 spin_lock_irq(shost->host_lock);
4131 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4132 spin_unlock_irq(shost->host_lock);
4133 lpfc_can_disctmo(vport);
4134 break;
4135 default:
4136 /* Try to recover from this error */
4137 lpfc_mbx_unreg_vpi(vport);
4138 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4139 lpfc_initial_fdisc(vport);
4140 break;
4143 } else {
4144 if (vport == phba->pport)
4145 lpfc_issue_fabric_reglogin(vport);
4146 else
4147 lpfc_do_scr_ns_plogi(phba, vport);
4149 mempool_free(pmb, phba->mbox_mem_pool);
4150 return;
4153 void
4154 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
4155 struct lpfc_nodelist *ndlp)
4157 LPFC_MBOXQ_t *mbox;
4159 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4160 if (mbox) {
4161 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
4162 mbox->vport = vport;
4163 mbox->context2 = lpfc_nlp_get(ndlp);
4164 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
4165 if (lpfc_sli_issue_mbox(phba, mbox,
4166 MBX_NOWAIT | MBX_STOP_IOCB)
4167 == MBX_NOT_FINISHED) {
4168 mempool_free(mbox, phba->mbox_mem_pool);
4169 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4171 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4172 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4173 "0253 Register VPI: Can't send mbox\n");
4175 } else {
4176 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4178 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4179 "0254 Register VPI: no memory\n");
4181 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4182 lpfc_nlp_put(ndlp);
4186 static void
4187 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4188 struct lpfc_iocbq *rspiocb)
4190 struct lpfc_vport *vport = cmdiocb->vport;
4191 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4192 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4193 struct lpfc_nodelist *np;
4194 struct lpfc_nodelist *next_np;
4195 IOCB_t *irsp = &rspiocb->iocb;
4196 struct lpfc_iocbq *piocb;
4198 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4199 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
4200 irsp->ulpStatus, irsp->un.ulpWord[4],
4201 vport->fc_prevDID);
4202 /* Since all FDISCs are being single threaded, we
4203 * must reset the discovery timer for ALL vports
4204 * waiting to send FDISC when one completes.
4206 list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
4207 lpfc_set_disctmo(piocb->vport);
4210 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4211 "FDISC cmpl: status:x%x/x%x prevdid:x%x",
4212 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
4214 if (irsp->ulpStatus) {
4215 /* Check for retry */
4216 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
4217 goto out;
4218 /* FDISC failed */
4219 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4220 "0124 FDISC failed. (%d/%d)\n",
4221 irsp->ulpStatus, irsp->un.ulpWord[4]);
4222 if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
4223 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4225 lpfc_nlp_put(ndlp);
4226 /* giving up on FDISC. Cancel discovery timer */
4227 lpfc_can_disctmo(vport);
4228 } else {
4229 spin_lock_irq(shost->host_lock);
4230 vport->fc_flag |= FC_FABRIC;
4231 if (vport->phba->fc_topology == TOPOLOGY_LOOP)
4232 vport->fc_flag |= FC_PUBLIC_LOOP;
4233 spin_unlock_irq(shost->host_lock);
4235 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
4236 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
4237 if ((vport->fc_prevDID != vport->fc_myDID) &&
4238 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
4239 /* If our NportID changed, we need to ensure all
4240 * remaining NPORTs get unreg_login'ed so we can
4241 * issue unreg_vpi.
4243 list_for_each_entry_safe(np, next_np,
4244 &vport->fc_nodes, nlp_listp) {
4245 if (np->nlp_state != NLP_STE_NPR_NODE
4246 || !(np->nlp_flag & NLP_NPR_ADISC))
4247 continue;
4248 spin_lock_irq(shost->host_lock);
4249 np->nlp_flag &= ~NLP_NPR_ADISC;
4250 spin_unlock_irq(shost->host_lock);
4251 lpfc_unreg_rpi(vport, np);
4253 lpfc_mbx_unreg_vpi(vport);
4254 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4257 if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
4258 lpfc_register_new_vport(phba, vport, ndlp);
4259 else
4260 lpfc_do_scr_ns_plogi(phba, vport);
4262 lpfc_nlp_put(ndlp); /* Free Fabric ndlp for vports */
4265 out:
4266 lpfc_els_free_iocb(phba, cmdiocb);
4270 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4271 uint8_t retry)
4273 struct lpfc_hba *phba = vport->phba;
4274 IOCB_t *icmd;
4275 struct lpfc_iocbq *elsiocb;
4276 struct serv_parm *sp;
4277 uint8_t *pcmd;
4278 uint16_t cmdsize;
4279 int did = ndlp->nlp_DID;
4280 int rc;
4282 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
4283 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
4284 ELS_CMD_FDISC);
4285 if (!elsiocb) {
4286 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4287 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4288 "0255 Issue FDISC: no IOCB\n");
4289 return 1;
4292 icmd = &elsiocb->iocb;
4293 icmd->un.elsreq64.myID = 0;
4294 icmd->un.elsreq64.fl = 1;
4296 /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
4297 icmd->ulpCt_h = 1;
4298 icmd->ulpCt_l = 0;
4300 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4301 *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
4302 pcmd += sizeof(uint32_t); /* CSP Word 1 */
4303 memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
4304 sp = (struct serv_parm *) pcmd;
4305 /* Setup CSPs accordingly for Fabric */
4306 sp->cmn.e_d_tov = 0;
4307 sp->cmn.w2.r_a_tov = 0;
4308 sp->cls1.classValid = 0;
4309 sp->cls2.seqDelivery = 1;
4310 sp->cls3.seqDelivery = 1;
4312 pcmd += sizeof(uint32_t); /* CSP Word 2 */
4313 pcmd += sizeof(uint32_t); /* CSP Word 3 */
4314 pcmd += sizeof(uint32_t); /* CSP Word 4 */
4315 pcmd += sizeof(uint32_t); /* Port Name */
4316 memcpy(pcmd, &vport->fc_portname, 8);
4317 pcmd += sizeof(uint32_t); /* Node Name */
4318 pcmd += sizeof(uint32_t); /* Node Name */
4319 memcpy(pcmd, &vport->fc_nodename, 8);
4321 lpfc_set_disctmo(vport);
4323 phba->fc_stat.elsXmitFDISC++;
4324 elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
4326 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4327 "Issue FDISC: did:x%x",
4328 did, 0, 0);
4330 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
4331 if (rc == IOCB_ERROR) {
4332 lpfc_els_free_iocb(phba, elsiocb);
4333 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4334 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4335 "0256 Issue FDISC: Cannot send IOCB\n");
4336 return 1;
4338 lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
4339 vport->port_state = LPFC_FDISC;
4340 return 0;
4343 static void
4344 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4345 struct lpfc_iocbq *rspiocb)
4347 struct lpfc_vport *vport = cmdiocb->vport;
4348 IOCB_t *irsp;
4350 irsp = &rspiocb->iocb;
4351 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4352 "LOGO npiv cmpl: status:x%x/x%x did:x%x",
4353 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
4355 lpfc_els_free_iocb(phba, cmdiocb);
4356 vport->unreg_vpi_cmpl = VPORT_ERROR;
4360 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4362 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4363 struct lpfc_hba *phba = vport->phba;
4364 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4365 IOCB_t *icmd;
4366 struct lpfc_iocbq *elsiocb;
4367 uint8_t *pcmd;
4368 uint16_t cmdsize;
4370 cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
4371 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
4372 ELS_CMD_LOGO);
4373 if (!elsiocb)
4374 return 1;
4376 icmd = &elsiocb->iocb;
4377 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4378 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
4379 pcmd += sizeof(uint32_t);
4381 /* Fill in LOGO payload */
4382 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
4383 pcmd += sizeof(uint32_t);
4384 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
4386 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4387 "Issue LOGO npiv did:x%x flg:x%x",
4388 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4390 elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
4391 spin_lock_irq(shost->host_lock);
4392 ndlp->nlp_flag |= NLP_LOGO_SND;
4393 spin_unlock_irq(shost->host_lock);
4394 if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
4395 spin_lock_irq(shost->host_lock);
4396 ndlp->nlp_flag &= ~NLP_LOGO_SND;
4397 spin_unlock_irq(shost->host_lock);
4398 lpfc_els_free_iocb(phba, elsiocb);
4399 return 1;
4401 return 0;
4404 void
4405 lpfc_fabric_block_timeout(unsigned long ptr)
4407 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4408 unsigned long iflags;
4409 uint32_t tmo_posted;
4410 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
4411 tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
4412 if (!tmo_posted)
4413 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
4414 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
4416 if (!tmo_posted) {
4417 spin_lock_irqsave(&phba->hbalock, iflags);
4418 if (phba->work_wait)
4419 lpfc_worker_wake_up(phba);
4420 spin_unlock_irqrestore(&phba->hbalock, iflags);
4424 static void
4425 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
4427 struct lpfc_iocbq *iocb;
4428 unsigned long iflags;
4429 int ret;
4430 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4431 IOCB_t *cmd;
4433 repeat:
4434 iocb = NULL;
4435 spin_lock_irqsave(&phba->hbalock, iflags);
4436 /* Post any pending iocb to the SLI layer */
4437 if (atomic_read(&phba->fabric_iocb_count) == 0) {
4438 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
4439 list);
4440 if (iocb)
4441 atomic_inc(&phba->fabric_iocb_count);
4443 spin_unlock_irqrestore(&phba->hbalock, iflags);
4444 if (iocb) {
4445 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
4446 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
4447 iocb->iocb_flag |= LPFC_IO_FABRIC;
4449 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
4450 "Fabric sched1: ste:x%x",
4451 iocb->vport->port_state, 0, 0);
4453 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
4455 if (ret == IOCB_ERROR) {
4456 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
4457 iocb->fabric_iocb_cmpl = NULL;
4458 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
4459 cmd = &iocb->iocb;
4460 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4461 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4462 iocb->iocb_cmpl(phba, iocb, iocb);
4464 atomic_dec(&phba->fabric_iocb_count);
4465 goto repeat;
4469 return;
4472 void
4473 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
4475 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4477 lpfc_resume_fabric_iocbs(phba);
4478 return;
4481 static void
4482 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
4484 int blocked;
4486 blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4487 /* Start a timer to unblock fabric
4488 * iocbs after 100ms
4490 if (!blocked)
4491 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
4493 return;
4496 static void
4497 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4498 struct lpfc_iocbq *rspiocb)
4500 struct ls_rjt stat;
4502 if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
4503 BUG();
4505 switch (rspiocb->iocb.ulpStatus) {
4506 case IOSTAT_NPORT_RJT:
4507 case IOSTAT_FABRIC_RJT:
4508 if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
4509 lpfc_block_fabric_iocbs(phba);
4511 break;
4513 case IOSTAT_NPORT_BSY:
4514 case IOSTAT_FABRIC_BSY:
4515 lpfc_block_fabric_iocbs(phba);
4516 break;
4518 case IOSTAT_LS_RJT:
4519 stat.un.lsRjtError =
4520 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
4521 if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
4522 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
4523 lpfc_block_fabric_iocbs(phba);
4524 break;
4527 if (atomic_read(&phba->fabric_iocb_count) == 0)
4528 BUG();
4530 cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
4531 cmdiocb->fabric_iocb_cmpl = NULL;
4532 cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
4533 cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
4535 atomic_dec(&phba->fabric_iocb_count);
4536 if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
4537 /* Post any pending iocbs to HBA */
4538 lpfc_resume_fabric_iocbs(phba);
4543 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
4545 unsigned long iflags;
4546 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4547 int ready;
4548 int ret;
4550 if (atomic_read(&phba->fabric_iocb_count) > 1)
4551 BUG();
4553 spin_lock_irqsave(&phba->hbalock, iflags);
4554 ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
4555 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4557 spin_unlock_irqrestore(&phba->hbalock, iflags);
4558 if (ready) {
4559 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
4560 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
4561 iocb->iocb_flag |= LPFC_IO_FABRIC;
4563 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
4564 "Fabric sched2: ste:x%x",
4565 iocb->vport->port_state, 0, 0);
4567 atomic_inc(&phba->fabric_iocb_count);
4568 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
4570 if (ret == IOCB_ERROR) {
4571 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
4572 iocb->fabric_iocb_cmpl = NULL;
4573 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
4574 atomic_dec(&phba->fabric_iocb_count);
4576 } else {
4577 spin_lock_irqsave(&phba->hbalock, iflags);
4578 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
4579 spin_unlock_irqrestore(&phba->hbalock, iflags);
4580 ret = IOCB_SUCCESS;
4582 return ret;
4586 void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
4588 LIST_HEAD(completions);
4589 struct lpfc_hba *phba = vport->phba;
4590 struct lpfc_iocbq *tmp_iocb, *piocb;
4591 IOCB_t *cmd;
4593 spin_lock_irq(&phba->hbalock);
4594 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4595 list) {
4597 if (piocb->vport != vport)
4598 continue;
4600 list_move_tail(&piocb->list, &completions);
4602 spin_unlock_irq(&phba->hbalock);
4604 while (!list_empty(&completions)) {
4605 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4606 list_del_init(&piocb->list);
4608 cmd = &piocb->iocb;
4609 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4610 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4611 (piocb->iocb_cmpl) (phba, piocb, piocb);
4615 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
4617 LIST_HEAD(completions);
4618 struct lpfc_hba *phba = ndlp->vport->phba;
4619 struct lpfc_iocbq *tmp_iocb, *piocb;
4620 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4621 IOCB_t *cmd;
4623 spin_lock_irq(&phba->hbalock);
4624 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4625 list) {
4626 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
4628 list_move_tail(&piocb->list, &completions);
4631 spin_unlock_irq(&phba->hbalock);
4633 while (!list_empty(&completions)) {
4634 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4635 list_del_init(&piocb->list);
4637 cmd = &piocb->iocb;
4638 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4639 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4640 (piocb->iocb_cmpl) (phba, piocb, piocb);
4644 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
4646 LIST_HEAD(completions);
4647 struct lpfc_iocbq *piocb;
4648 IOCB_t *cmd;
4650 spin_lock_irq(&phba->hbalock);
4651 list_splice_init(&phba->fabric_iocb_list, &completions);
4652 spin_unlock_irq(&phba->hbalock);
4654 while (!list_empty(&completions)) {
4655 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4656 list_del_init(&piocb->list);
4658 cmd = &piocb->iocb;
4659 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4660 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4661 (piocb->iocb_cmpl) (phba, piocb, piocb);
4666 void lpfc_fabric_abort_flogi(struct lpfc_hba *phba)
4668 LIST_HEAD(completions);
4669 struct lpfc_iocbq *tmp_iocb, *piocb;
4670 IOCB_t *cmd;
4671 struct lpfc_nodelist *ndlp;
4673 spin_lock_irq(&phba->hbalock);
4674 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4675 list) {
4677 cmd = &piocb->iocb;
4678 ndlp = (struct lpfc_nodelist *) piocb->context1;
4679 if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
4680 ndlp != NULL &&
4681 ndlp->nlp_DID == Fabric_DID)
4682 list_move_tail(&piocb->list, &completions);
4684 spin_unlock_irq(&phba->hbalock);
4686 while (!list_empty(&completions)) {
4687 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4688 list_del_init(&piocb->list);
4690 cmd = &piocb->iocb;
4691 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4692 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4693 (piocb->iocb_cmpl) (phba, piocb, piocb);