1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
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/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/kthread.h>
28 #include <linux/pci.h>
29 #include <linux/spinlock.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_transport_fc.h>
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_version.h"
43 #include "lpfc_vport.h"
45 inline void lpfc_vport_set_state(struct lpfc_vport *vport,
46 enum fc_vport_state new_state)
48 struct fc_vport *fc_vport = vport->fc_vport;
52 * When the transport defines fc_vport_set state we will replace
53 * this code with the following line
55 /* fc_vport_set_state(fc_vport, new_state); */
56 if (new_state != FC_VPORT_INITIALIZING)
57 fc_vport->vport_last_state = fc_vport->vport_state;
58 fc_vport->vport_state = new_state;
61 /* for all the error states we will set the invternal state to FAILED */
63 case FC_VPORT_NO_FABRIC_SUPP:
64 case FC_VPORT_NO_FABRIC_RSCS:
65 case FC_VPORT_FABRIC_LOGOUT:
66 case FC_VPORT_FABRIC_REJ_WWN:
68 vport->port_state = LPFC_VPORT_FAILED;
70 case FC_VPORT_LINKDOWN:
71 vport->port_state = LPFC_VPORT_UNKNOWN;
80 lpfc_alloc_vpi(struct lpfc_hba *phba)
84 spin_lock_irq(&phba->hbalock);
85 /* Start at bit 1 because vpi zero is reserved for the physical port */
86 vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
87 if (vpi > phba->max_vpi)
90 set_bit(vpi, phba->vpi_bmask);
91 spin_unlock_irq(&phba->hbalock);
96 lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
98 spin_lock_irq(&phba->hbalock);
99 clear_bit(vpi, phba->vpi_bmask);
100 spin_unlock_irq(&phba->hbalock);
104 lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
108 struct lpfc_dmabuf *mp;
111 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
117 lpfc_read_sparam(phba, pmb, vport->vpi);
119 * Grab buffer pointer and clear context1 so we can use
120 * lpfc_sli_issue_box_wait
122 mp = (struct lpfc_dmabuf *) pmb->context1;
123 pmb->context1 = NULL;
126 rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
127 if (rc != MBX_SUCCESS) {
128 if (signal_pending(current)) {
129 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
130 "1830 Signal aborted mbxCmd x%x\n",
132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
134 if (rc != MBX_TIMEOUT)
135 mempool_free(pmb, phba->mbox_mem_pool);
138 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
139 "1818 VPort failed init, mbxCmd x%x "
140 "READ_SPARM mbxStatus x%x, rc = x%x\n",
141 mb->mbxCommand, mb->mbxStatus, rc);
142 lpfc_mbuf_free(phba, mp->virt, mp->phys);
144 if (rc != MBX_TIMEOUT)
145 mempool_free(pmb, phba->mbox_mem_pool);
150 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
151 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
152 sizeof (struct lpfc_name));
153 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
154 sizeof (struct lpfc_name));
156 lpfc_mbuf_free(phba, mp->virt, mp->phys);
158 mempool_free(pmb, phba->mbox_mem_pool);
164 lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
165 const char *name_type)
167 /* ensure that IEEE format 1 addresses
168 * contain zeros in bits 59-48
170 if (!((wwn->u.wwn[0] >> 4) == 1 &&
171 ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
174 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
175 "1822 Invalid %s: %02x:%02x:%02x:%02x:"
176 "%02x:%02x:%02x:%02x\n",
178 wwn->u.wwn[0], wwn->u.wwn[1],
179 wwn->u.wwn[2], wwn->u.wwn[3],
180 wwn->u.wwn[4], wwn->u.wwn[5],
181 wwn->u.wwn[6], wwn->u.wwn[7]);
186 lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
188 struct lpfc_vport *vport;
191 spin_lock_irqsave(&phba->hbalock, flags);
192 list_for_each_entry(vport, &phba->port_list, listentry) {
193 if (vport == new_vport)
195 /* If they match, return not unique */
196 if (memcmp(&vport->fc_sparam.portName,
197 &new_vport->fc_sparam.portName,
198 sizeof(struct lpfc_name)) == 0) {
199 spin_unlock_irqrestore(&phba->hbalock, flags);
203 spin_unlock_irqrestore(&phba->hbalock, flags);
208 lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
210 struct lpfc_nodelist *ndlp;
211 struct Scsi_Host *shost = fc_vport->shost;
212 struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
213 struct lpfc_hba *phba = pport->phba;
214 struct lpfc_vport *vport = NULL;
217 int rc = VPORT_ERROR;
221 if ((phba->sli_rev < 3) ||
222 !(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
223 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
224 "1808 Create VPORT failed: "
225 "NPIV is not enabled: SLImode:%d\n",
231 vpi = lpfc_alloc_vpi(phba);
233 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
234 "1809 Create VPORT failed: "
235 "Max VPORTs (%d) exceeded\n",
237 rc = VPORT_NORESOURCES;
242 /* Assign an unused board number */
243 if ((instance = lpfc_get_instance()) < 0) {
244 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
245 "1810 Create VPORT failed: Cannot get "
246 "instance number\n");
247 lpfc_free_vpi(phba, vpi);
248 rc = VPORT_NORESOURCES;
252 vport = lpfc_create_port(phba, instance, &fc_vport->dev);
254 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
255 "1811 Create VPORT failed: vpi x%x\n", vpi);
256 lpfc_free_vpi(phba, vpi);
257 rc = VPORT_NORESOURCES;
262 lpfc_debugfs_initialize(vport);
264 if ((status = lpfc_vport_sparm(phba, vport))) {
265 if (status == -EINTR) {
266 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
267 "1831 Create VPORT Interrupted.\n");
270 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
271 "1813 Create VPORT failed. "
272 "Cannot get sparam\n");
273 rc = VPORT_NORESOURCES;
275 lpfc_free_vpi(phba, vpi);
280 memcpy(vport->fc_portname.u.wwn, vport->fc_sparam.portName.u.wwn, 8);
281 memcpy(vport->fc_nodename.u.wwn, vport->fc_sparam.nodeName.u.wwn, 8);
282 size = strnlen(fc_vport->symbolic_name, LPFC_VNAME_LEN);
284 vport->vname = kzalloc(size+1, GFP_KERNEL);
286 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
287 "1814 Create VPORT failed. "
288 "vname allocation failed.\n");
290 lpfc_free_vpi(phba, vpi);
294 memcpy(vport->vname, fc_vport->symbolic_name, size+1);
296 if (fc_vport->node_name != 0)
297 u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
298 if (fc_vport->port_name != 0)
299 u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
301 memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
302 memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
304 if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
305 !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
306 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
307 "1821 Create VPORT failed. "
308 "Invalid WWN format\n");
309 lpfc_free_vpi(phba, vpi);
315 if (!lpfc_unique_wwpn(phba, vport)) {
316 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
317 "1823 Create VPORT failed. "
318 "Duplicate WWN on HBA\n");
319 lpfc_free_vpi(phba, vpi);
325 *(struct lpfc_vport **)fc_vport->dd_data = vport;
326 vport->fc_vport = fc_vport;
328 if ((phba->link_state < LPFC_LINK_UP) ||
329 (phba->fc_topology == TOPOLOGY_LOOP)) {
330 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
340 /* Use the Physical nodes Fabric NDLP to determine if the link is
341 * up and ready to FDISC.
343 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
344 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
345 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
346 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
347 lpfc_set_disctmo(vport);
348 lpfc_initial_fdisc(vport);
350 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
351 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
352 "0262 No NPIV Fabric support\n");
355 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
360 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
361 "1825 Vport Created.\n");
362 lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
368 disable_vport(struct fc_vport *fc_vport)
370 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
371 struct lpfc_hba *phba = vport->phba;
372 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
375 ndlp = lpfc_findnode_did(vport, Fabric_DID);
376 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
377 && phba->link_state >= LPFC_LINK_UP) {
378 vport->unreg_vpi_cmpl = VPORT_INVAL;
379 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
380 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
381 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
382 timeout = schedule_timeout(timeout);
385 lpfc_sli_host_down(vport);
387 /* Mark all nodes for discovery so we can remove them by
388 * calling lpfc_cleanup_rpis(vport, 1)
390 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
391 if (!NLP_CHK_NODE_ACT(ndlp))
393 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
395 lpfc_disc_state_machine(vport, ndlp, NULL,
396 NLP_EVT_DEVICE_RECOVERY);
398 lpfc_cleanup_rpis(vport, 1);
400 lpfc_stop_vport_timers(vport);
401 lpfc_unreg_all_rpis(vport);
402 lpfc_unreg_default_rpis(vport);
404 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
405 * scsi_host_put() to release the vport.
407 lpfc_mbx_unreg_vpi(vport);
409 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
410 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
411 "1826 Vport Disabled.\n");
416 enable_vport(struct fc_vport *fc_vport)
418 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
419 struct lpfc_hba *phba = vport->phba;
420 struct lpfc_nodelist *ndlp = NULL;
422 if ((phba->link_state < LPFC_LINK_UP) ||
423 (phba->fc_topology == TOPOLOGY_LOOP)) {
424 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
428 vport->load_flag |= FC_LOADING;
429 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
431 /* Use the Physical nodes Fabric NDLP to determine if the link is
432 * up and ready to FDISC.
434 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
435 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
436 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
437 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
438 lpfc_set_disctmo(vport);
439 lpfc_initial_fdisc(vport);
441 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
442 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
443 "0264 No NPIV Fabric support\n");
446 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
448 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
449 "1827 Vport Enabled.\n");
454 lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
457 return disable_vport(fc_vport);
459 return enable_vport(fc_vport);
464 lpfc_vport_delete(struct fc_vport *fc_vport)
466 struct lpfc_nodelist *ndlp = NULL;
467 struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost;
468 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
469 struct lpfc_hba *phba = vport->phba;
472 if (vport->port_type == LPFC_PHYSICAL_PORT) {
473 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
474 "1812 vport_delete failed: Cannot delete "
479 * If we are not unloading the driver then prevent the vport_delete
480 * from happening until after this vport's discovery is finished.
482 if (!(phba->pport->load_flag & FC_UNLOADING)) {
484 while (check_count < ((phba->fc_ratov * 3) + 3) &&
485 vport->port_state > LPFC_VPORT_FAILED &&
486 vport->port_state < LPFC_VPORT_READY) {
490 if (vport->port_state > LPFC_VPORT_FAILED &&
491 vport->port_state < LPFC_VPORT_READY)
495 * This is a bit of a mess. We want to ensure the shost doesn't get
496 * torn down until we're done with the embedded lpfc_vport structure.
498 * Beyond holding a reference for this function, we also need a
499 * reference for outstanding I/O requests we schedule during delete
500 * processing. But once we scsi_remove_host() we can no longer obtain
501 * a reference through scsi_host_get().
503 * So we take two references here. We release one reference at the
504 * bottom of the function -- after delinking the vport. And we
505 * release the other at the completion of the unreg_vpi that get's
506 * initiated after we've disposed of all other resources associated
509 if (!scsi_host_get(shost) || !scsi_host_get(shost))
511 spin_lock_irq(&phba->hbalock);
512 vport->load_flag |= FC_UNLOADING;
513 spin_unlock_irq(&phba->hbalock);
515 lpfc_debugfs_terminate(vport);
516 fc_remove_host(lpfc_shost_from_vport(vport));
517 scsi_remove_host(lpfc_shost_from_vport(vport));
519 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
521 /* In case of driver unload, we shall not perform fabric logo as the
522 * worker thread already stopped at this stage and, in this case, we
523 * can safely skip the fabric logo.
525 if (phba->pport->load_flag & FC_UNLOADING) {
526 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
527 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
528 phba->link_state >= LPFC_LINK_UP) {
529 /* First look for the Fabric ndlp */
530 ndlp = lpfc_findnode_did(vport, Fabric_DID);
533 else if (!NLP_CHK_NODE_ACT(ndlp)) {
534 ndlp = lpfc_enable_node(vport, ndlp,
535 NLP_STE_UNUSED_NODE);
539 /* Remove ndlp from vport npld list */
540 lpfc_dequeue_node(vport, ndlp);
542 /* Indicate free memory when release */
543 spin_lock_irq(&phba->ndlp_lock);
544 NLP_SET_FREE_REQ(ndlp);
545 spin_unlock_irq(&phba->ndlp_lock);
546 /* Kick off release ndlp when it can be safely done */
552 /* Otherwise, we will perform fabric logo as needed */
553 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
554 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
555 phba->link_state >= LPFC_LINK_UP &&
556 phba->fc_topology != TOPOLOGY_LOOP) {
557 if (vport->cfg_enable_da_id) {
558 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
559 if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
560 while (vport->ct_flags && timeout)
561 timeout = schedule_timeout(timeout);
563 lpfc_printf_log(vport->phba, KERN_WARNING,
565 "1829 CT command failed to "
566 "delete objects on fabric. \n");
568 /* First look for the Fabric ndlp */
569 ndlp = lpfc_findnode_did(vport, Fabric_DID);
571 /* Cannot find existing Fabric ndlp, allocate one */
572 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
575 lpfc_nlp_init(vport, ndlp, Fabric_DID);
576 /* Indicate free memory when release */
577 NLP_SET_FREE_REQ(ndlp);
579 if (!NLP_CHK_NODE_ACT(ndlp))
580 ndlp = lpfc_enable_node(vport, ndlp,
581 NLP_STE_UNUSED_NODE);
585 /* Remove ndlp from vport npld list */
586 lpfc_dequeue_node(vport, ndlp);
587 spin_lock_irq(&phba->ndlp_lock);
588 if (!NLP_CHK_FREE_REQ(ndlp))
589 /* Indicate free memory when release */
590 NLP_SET_FREE_REQ(ndlp);
592 /* Skip this if ndlp is already in free mode */
593 spin_unlock_irq(&phba->ndlp_lock);
596 spin_unlock_irq(&phba->ndlp_lock);
598 vport->unreg_vpi_cmpl = VPORT_INVAL;
599 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
600 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
601 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
602 timeout = schedule_timeout(timeout);
607 lpfc_sli_host_down(vport);
609 lpfc_stop_vport_timers(vport);
611 if (!(phba->pport->load_flag & FC_UNLOADING)) {
612 lpfc_unreg_all_rpis(vport);
613 lpfc_unreg_default_rpis(vport);
615 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
616 * does the scsi_host_put() to release the vport.
618 lpfc_mbx_unreg_vpi(vport);
621 lpfc_free_vpi(phba, vport->vpi);
622 vport->work_port_events = 0;
623 spin_lock_irq(&phba->hbalock);
624 list_del_init(&vport->listentry);
625 spin_unlock_irq(&phba->hbalock);
626 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
627 "1828 Vport Deleted.\n");
628 scsi_host_put(shost);
633 lpfc_create_vport_work_array(struct lpfc_hba *phba)
635 struct lpfc_vport *port_iterator;
636 struct lpfc_vport **vports;
638 vports = kzalloc((phba->max_vpi + 1) * sizeof(struct lpfc_vport *),
642 spin_lock_irq(&phba->hbalock);
643 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
644 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
645 lpfc_printf_vlog(port_iterator, KERN_WARNING, LOG_VPORT,
646 "1801 Create vport work array FAILED: "
647 "cannot do scsi_host_get\n");
650 vports[index++] = port_iterator;
652 spin_unlock_irq(&phba->hbalock);
657 lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
662 for (i=0; vports[i] != NULL && i <= phba->max_vpi; i++)
663 scsi_host_put(lpfc_shost_from_vport(vports[i]));