1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2006 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
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 vpi = find_next_zero_bit(phba->vpi_bmask, phba->max_vpi, 1);
86 if (vpi > phba->max_vpi)
89 set_bit(vpi, phba->vpi_bmask);
90 spin_unlock_irq(&phba->hbalock);
95 lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
97 spin_lock_irq(&phba->hbalock);
98 clear_bit(vpi, phba->vpi_bmask);
99 spin_unlock_irq(&phba->hbalock);
103 lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
107 struct lpfc_dmabuf *mp;
110 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
116 lpfc_read_sparam(phba, pmb, vport->vpi);
118 * Grab buffer pointer and clear context1 so we can use
119 * lpfc_sli_issue_box_wait
121 mp = (struct lpfc_dmabuf *) pmb->context1;
122 pmb->context1 = NULL;
125 rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
126 if (rc != MBX_SUCCESS) {
127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
128 "%d (%d):1818 VPort failed init, mbxCmd x%x "
129 "READ_SPARM mbxStatus x%x, rc = x%x\n",
130 phba->brd_no, vport->vpi,
131 mb->mbxCommand, mb->mbxStatus, rc);
132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
134 mempool_free(pmb, phba->mbox_mem_pool);
138 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
139 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
140 sizeof (struct lpfc_name));
141 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
142 sizeof (struct lpfc_name));
144 lpfc_mbuf_free(phba, mp->virt, mp->phys);
146 mempool_free(pmb, phba->mbox_mem_pool);
152 lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
153 const char *name_type)
155 /* ensure that IEEE format 1 addresses
156 * contain zeros in bits 59-48
158 if (!((wwn->u.wwn[0] >> 4) == 1 &&
159 ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
162 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
163 "%d:1822 Invalid %s: %02x:%02x:%02x:%02x:"
164 "%02x:%02x:%02x:%02x\n",
165 phba->brd_no, name_type,
166 wwn->u.wwn[0], wwn->u.wwn[1],
167 wwn->u.wwn[2], wwn->u.wwn[3],
168 wwn->u.wwn[4], wwn->u.wwn[5],
169 wwn->u.wwn[6], wwn->u.wwn[7]);
174 lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
176 struct lpfc_vport *vport;
178 list_for_each_entry(vport, &phba->port_list, listentry) {
179 if (vport == new_vport)
181 /* If they match, return not unique */
182 if (memcmp(&vport->fc_sparam.portName,
183 &new_vport->fc_sparam.portName,
184 sizeof(struct lpfc_name)) == 0)
191 lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
193 struct lpfc_nodelist *ndlp;
194 struct lpfc_vport *pport =
195 (struct lpfc_vport *) fc_vport->shost->hostdata;
196 struct lpfc_hba *phba = pport->phba;
197 struct lpfc_vport *vport = NULL;
200 int rc = VPORT_ERROR;
202 if ((phba->sli_rev < 3) ||
203 !(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
204 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
205 "%d:1808 Create VPORT failed: "
206 "NPIV is not enabled: SLImode:%d\n",
207 phba->brd_no, phba->sli_rev);
212 vpi = lpfc_alloc_vpi(phba);
214 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
215 "%d:1809 Create VPORT failed: "
216 "Max VPORTs (%d) exceeded\n",
217 phba->brd_no, phba->max_vpi);
218 rc = VPORT_NORESOURCES;
223 /* Assign an unused board number */
224 if ((instance = lpfc_get_instance()) < 0) {
225 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
226 "%d:1810 Create VPORT failed: Cannot get "
227 "instance number\n", phba->brd_no);
228 lpfc_free_vpi(phba, vpi);
229 rc = VPORT_NORESOURCES;
233 vport = lpfc_create_port(phba, instance, fc_vport);
235 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
236 "%d:1811 Create VPORT failed: vpi x%x\n",
238 lpfc_free_vpi(phba, vpi);
239 rc = VPORT_NORESOURCES;
244 if (lpfc_vport_sparm(phba, vport)) {
245 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
246 "%d:1813 Create VPORT failed: vpi:%d "
247 "Cannot get sparam\n",
249 lpfc_free_vpi(phba, vpi);
251 rc = VPORT_NORESOURCES;
255 memcpy(vport->fc_portname.u.wwn, vport->fc_sparam.portName.u.wwn, 8);
256 memcpy(vport->fc_nodename.u.wwn, vport->fc_sparam.nodeName.u.wwn, 8);
258 if (fc_vport->node_name != 0)
259 u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
260 if (fc_vport->port_name != 0)
261 u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
263 memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
264 memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
266 if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
267 !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
268 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
269 "%d:1821 Create VPORT failed: vpi:%d "
270 "Invalid WWN format\n",
272 lpfc_free_vpi(phba, vpi);
278 if (!lpfc_unique_wwpn(phba, vport)) {
279 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
280 "%d:1823 Create VPORT failed: vpi:%d "
281 "Duplicate WWN on HBA\n",
283 lpfc_free_vpi(phba, vpi);
289 *(struct lpfc_vport **)fc_vport->dd_data = vport;
290 vport->fc_vport = fc_vport;
292 if ((phba->link_state < LPFC_LINK_UP) ||
293 (phba->fc_topology == TOPOLOGY_LOOP)) {
294 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
304 /* Use the Physical nodes Fabric NDLP to determine if the link is
305 * up and ready to FDISC.
307 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
308 if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
309 lpfc_set_disctmo(vport);
310 lpfc_initial_fdisc(vport);
312 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
317 lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
323 disable_vport(struct fc_vport *fc_vport)
325 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
326 struct lpfc_hba *phba = vport->phba;
327 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
330 ndlp = lpfc_findnode_did(vport, Fabric_DID);
331 if (ndlp && phba->link_state >= LPFC_LINK_UP) {
332 vport->unreg_vpi_cmpl = VPORT_INVAL;
333 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
334 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
335 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
336 timeout = schedule_timeout(timeout);
339 lpfc_sli_host_down(vport);
341 /* Mark all nodes for discovery so we can remove them by
342 * calling lpfc_cleanup_rpis(vport, 1)
344 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
345 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
347 lpfc_disc_state_machine(vport, ndlp, NULL,
348 NLP_EVT_DEVICE_RECOVERY);
350 lpfc_cleanup_rpis(vport, 1);
352 lpfc_stop_vport_timers(vport);
353 lpfc_unreg_all_rpis(vport);
354 lpfc_unreg_default_rpis(vport);
356 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
357 * scsi_host_put() to release the vport.
359 lpfc_mbx_unreg_vpi(vport);
361 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
366 enable_vport(struct fc_vport *fc_vport)
368 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
369 struct lpfc_hba *phba = vport->phba;
370 struct lpfc_nodelist *ndlp = NULL;
372 if ((phba->link_state < LPFC_LINK_UP) ||
373 (phba->fc_topology == TOPOLOGY_LOOP)) {
374 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
378 vport->load_flag |= FC_LOADING;
379 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
381 /* Use the Physical nodes Fabric NDLP to determine if the link is
382 * up and ready to FDISC.
384 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
385 if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
386 lpfc_set_disctmo(vport);
387 lpfc_initial_fdisc(vport);
389 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
396 lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
399 return disable_vport(fc_vport);
401 return enable_vport(fc_vport);
406 lpfc_vport_delete(struct fc_vport *fc_vport)
408 struct lpfc_nodelist *ndlp = NULL;
409 struct lpfc_nodelist *next_ndlp;
410 struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost;
411 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
412 struct lpfc_hba *phba = vport->phba;
414 int rc = VPORT_ERROR;
417 * This is a bit of a mess. We want to ensure the shost doesn't get
418 * torn down until we're done with the embedded lpfc_vport structure.
420 * Beyond holding a reference for this function, we also need a
421 * reference for outstanding I/O requests we schedule during delete
422 * processing. But once we scsi_remove_host() we can no longer obtain
423 * a reference through scsi_host_get().
425 * So we take two references here. We release one reference at the
426 * bottom of the function -- after delinking the vport. And we
427 * release the other at the completion of the unreg_vpi that get's
428 * initiated after we've disposed of all other resources associated
431 if (!scsi_host_get(shost) || !scsi_host_get(shost))
434 if (vport->port_type == LPFC_PHYSICAL_PORT) {
435 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
436 "%d:1812 vport_delete failed: Cannot delete "
437 "physical host\n", phba->brd_no);
441 vport->load_flag |= FC_UNLOADING;
444 fc_remove_host(lpfc_shost_from_vport(vport));
445 scsi_remove_host(lpfc_shost_from_vport(vport));
447 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
448 if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
449 phba->link_state >= LPFC_LINK_UP) {
451 /* First look for the Fabric ndlp */
452 ndlp = lpfc_findnode_did(vport, Fabric_DID);
454 /* Cannot find existing Fabric ndlp, allocate one */
455 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
458 lpfc_nlp_init(vport, ndlp, Fabric_DID);
460 lpfc_dequeue_node(vport, ndlp);
462 vport->unreg_vpi_cmpl = VPORT_INVAL;
463 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
464 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
465 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
466 timeout = schedule_timeout(timeout);
470 lpfc_sli_host_down(vport);
472 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
473 lpfc_disc_state_machine(vport, ndlp, NULL,
474 NLP_EVT_DEVICE_RECOVERY);
475 lpfc_disc_state_machine(vport, ndlp, NULL,
479 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
480 /* free any ndlp's in unused state */
481 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
482 lpfc_drop_node(vport, ndlp);
485 lpfc_stop_vport_timers(vport);
486 lpfc_unreg_all_rpis(vport);
487 lpfc_unreg_default_rpis(vport);
489 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
490 * scsi_host_put() to release the vport.
492 lpfc_mbx_unreg_vpi(vport);
494 lpfc_free_vpi(phba, vport->vpi);
495 vport->work_port_events = 0;
496 spin_lock_irq(&phba->hbalock);
497 list_del_init(&vport->listentry);
498 spin_unlock_irq(&phba->hbalock);
502 scsi_host_put(shost);
507 EXPORT_SYMBOL(lpfc_vport_create);
508 EXPORT_SYMBOL(lpfc_vport_delete);