Commit | Line | Data |
---|---|---|
92d7f7b0 JS |
1 | /******************************************************************* |
2 | * This file is part of the Emulex Linux Device Driver for * | |
3 | * Fibre Channel Host Bus Adapters. * | |
e47c9093 | 4 | * Copyright (C) 2004-2008 Emulex. All rights reserved. * |
92d7f7b0 JS |
5 | * EMULEX and SLI are trademarks of Emulex. * |
6 | * www.emulex.com * | |
7 | * Portions Copyright (C) 2004-2005 Christoph Hellwig * | |
8 | * * | |
9 | * This program is free software; you can redistribute it and/or * | |
10 | * modify it under the terms of version 2 of the GNU General * | |
11 | * Public License as published by the Free Software Foundation. * | |
12 | * This program is distributed in the hope that it will be useful. * | |
13 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * | |
14 | * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * | |
15 | * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE * | |
16 | * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD * | |
17 | * TO BE LEGALLY INVALID. See the GNU General Public License for * | |
18 | * more details, a copy of which can be found in the file COPYING * | |
19 | * included with this package. * | |
20 | *******************************************************************/ | |
21 | ||
22 | #include <linux/blkdev.h> | |
23 | #include <linux/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> | |
30 | ||
31 | #include <scsi/scsi.h> | |
32 | #include <scsi/scsi_device.h> | |
33 | #include <scsi/scsi_host.h> | |
34 | #include <scsi/scsi_transport_fc.h> | |
da0436e9 | 35 | #include "lpfc_hw4.h" |
92d7f7b0 JS |
36 | #include "lpfc_hw.h" |
37 | #include "lpfc_sli.h" | |
da0436e9 | 38 | #include "lpfc_sli4.h" |
ea2151b4 | 39 | #include "lpfc_nl.h" |
92d7f7b0 JS |
40 | #include "lpfc_disc.h" |
41 | #include "lpfc_scsi.h" | |
42 | #include "lpfc.h" | |
43 | #include "lpfc_logmsg.h" | |
44 | #include "lpfc_crtn.h" | |
45 | #include "lpfc_version.h" | |
46 | #include "lpfc_vport.h" | |
47 | ||
48 | inline void lpfc_vport_set_state(struct lpfc_vport *vport, | |
49 | enum fc_vport_state new_state) | |
50 | { | |
51 | struct fc_vport *fc_vport = vport->fc_vport; | |
52 | ||
53 | if (fc_vport) { | |
54 | /* | |
55 | * When the transport defines fc_vport_set state we will replace | |
56 | * this code with the following line | |
57 | */ | |
58 | /* fc_vport_set_state(fc_vport, new_state); */ | |
59 | if (new_state != FC_VPORT_INITIALIZING) | |
60 | fc_vport->vport_last_state = fc_vport->vport_state; | |
61 | fc_vport->vport_state = new_state; | |
62 | } | |
63 | ||
64 | /* for all the error states we will set the invternal state to FAILED */ | |
65 | switch (new_state) { | |
66 | case FC_VPORT_NO_FABRIC_SUPP: | |
67 | case FC_VPORT_NO_FABRIC_RSCS: | |
68 | case FC_VPORT_FABRIC_LOGOUT: | |
69 | case FC_VPORT_FABRIC_REJ_WWN: | |
70 | case FC_VPORT_FAILED: | |
71 | vport->port_state = LPFC_VPORT_FAILED; | |
72 | break; | |
73 | case FC_VPORT_LINKDOWN: | |
74 | vport->port_state = LPFC_VPORT_UNKNOWN; | |
75 | break; | |
76 | default: | |
77 | /* do nothing */ | |
78 | break; | |
79 | } | |
80 | } | |
81 | ||
82 | static int | |
83 | lpfc_alloc_vpi(struct lpfc_hba *phba) | |
84 | { | |
85 | int vpi; | |
86 | ||
87 | spin_lock_irq(&phba->hbalock); | |
858c9f6c JS |
88 | /* Start at bit 1 because vpi zero is reserved for the physical port */ |
89 | vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1); | |
92d7f7b0 JS |
90 | if (vpi > phba->max_vpi) |
91 | vpi = 0; | |
92 | else | |
93 | set_bit(vpi, phba->vpi_bmask); | |
da0436e9 JS |
94 | if (phba->sli_rev == LPFC_SLI_REV4) |
95 | phba->sli4_hba.max_cfg_param.vpi_used++; | |
92d7f7b0 JS |
96 | spin_unlock_irq(&phba->hbalock); |
97 | return vpi; | |
98 | } | |
99 | ||
100 | static void | |
101 | lpfc_free_vpi(struct lpfc_hba *phba, int vpi) | |
102 | { | |
da0436e9 JS |
103 | if (vpi == 0) |
104 | return; | |
92d7f7b0 JS |
105 | spin_lock_irq(&phba->hbalock); |
106 | clear_bit(vpi, phba->vpi_bmask); | |
da0436e9 JS |
107 | if (phba->sli_rev == LPFC_SLI_REV4) |
108 | phba->sli4_hba.max_cfg_param.vpi_used--; | |
92d7f7b0 JS |
109 | spin_unlock_irq(&phba->hbalock); |
110 | } | |
111 | ||
112 | static int | |
113 | lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport) | |
114 | { | |
115 | LPFC_MBOXQ_t *pmb; | |
116 | MAILBOX_t *mb; | |
117 | struct lpfc_dmabuf *mp; | |
118 | int rc; | |
119 | ||
120 | pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL); | |
121 | if (!pmb) { | |
122 | return -ENOMEM; | |
123 | } | |
f4b4c68f | 124 | mb = &pmb->u.mb; |
92d7f7b0 JS |
125 | |
126 | lpfc_read_sparam(phba, pmb, vport->vpi); | |
127 | /* | |
128 | * Grab buffer pointer and clear context1 so we can use | |
129 | * lpfc_sli_issue_box_wait | |
130 | */ | |
131 | mp = (struct lpfc_dmabuf *) pmb->context1; | |
132 | pmb->context1 = NULL; | |
133 | ||
134 | pmb->vport = vport; | |
135 | rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2); | |
136 | if (rc != MBX_SUCCESS) { | |
98c9ea5c JS |
137 | if (signal_pending(current)) { |
138 | lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT, | |
139 | "1830 Signal aborted mbxCmd x%x\n", | |
140 | mb->mbxCommand); | |
141 | lpfc_mbuf_free(phba, mp->virt, mp->phys); | |
142 | kfree(mp); | |
143 | if (rc != MBX_TIMEOUT) | |
144 | mempool_free(pmb, phba->mbox_mem_pool); | |
145 | return -EINTR; | |
146 | } else { | |
147 | lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT, | |
148 | "1818 VPort failed init, mbxCmd x%x " | |
149 | "READ_SPARM mbxStatus x%x, rc = x%x\n", | |
150 | mb->mbxCommand, mb->mbxStatus, rc); | |
151 | lpfc_mbuf_free(phba, mp->virt, mp->phys); | |
152 | kfree(mp); | |
153 | if (rc != MBX_TIMEOUT) | |
154 | mempool_free(pmb, phba->mbox_mem_pool); | |
155 | return -EIO; | |
156 | } | |
92d7f7b0 JS |
157 | } |
158 | ||
159 | memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm)); | |
160 | memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName, | |
161 | sizeof (struct lpfc_name)); | |
162 | memcpy(&vport->fc_portname, &vport->fc_sparam.portName, | |
163 | sizeof (struct lpfc_name)); | |
164 | ||
165 | lpfc_mbuf_free(phba, mp->virt, mp->phys); | |
166 | kfree(mp); | |
167 | mempool_free(pmb, phba->mbox_mem_pool); | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | static int | |
173 | lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn, | |
174 | const char *name_type) | |
175 | { | |
176 | /* ensure that IEEE format 1 addresses | |
177 | * contain zeros in bits 59-48 | |
178 | */ | |
179 | if (!((wwn->u.wwn[0] >> 4) == 1 && | |
180 | ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0))) | |
181 | return 1; | |
182 | ||
183 | lpfc_printf_log(phba, KERN_ERR, LOG_VPORT, | |
e8b62011 | 184 | "1822 Invalid %s: %02x:%02x:%02x:%02x:" |
92d7f7b0 | 185 | "%02x:%02x:%02x:%02x\n", |
e8b62011 | 186 | name_type, |
92d7f7b0 JS |
187 | wwn->u.wwn[0], wwn->u.wwn[1], |
188 | wwn->u.wwn[2], wwn->u.wwn[3], | |
189 | wwn->u.wwn[4], wwn->u.wwn[5], | |
190 | wwn->u.wwn[6], wwn->u.wwn[7]); | |
191 | return 0; | |
192 | } | |
193 | ||
194 | static int | |
195 | lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport) | |
196 | { | |
197 | struct lpfc_vport *vport; | |
549e55cd | 198 | unsigned long flags; |
92d7f7b0 | 199 | |
549e55cd | 200 | spin_lock_irqsave(&phba->hbalock, flags); |
92d7f7b0 JS |
201 | list_for_each_entry(vport, &phba->port_list, listentry) { |
202 | if (vport == new_vport) | |
203 | continue; | |
204 | /* If they match, return not unique */ | |
205 | if (memcmp(&vport->fc_sparam.portName, | |
549e55cd JS |
206 | &new_vport->fc_sparam.portName, |
207 | sizeof(struct lpfc_name)) == 0) { | |
208 | spin_unlock_irqrestore(&phba->hbalock, flags); | |
92d7f7b0 | 209 | return 0; |
549e55cd | 210 | } |
92d7f7b0 | 211 | } |
549e55cd | 212 | spin_unlock_irqrestore(&phba->hbalock, flags); |
92d7f7b0 JS |
213 | return 1; |
214 | } | |
215 | ||
90160e01 | 216 | /** |
3621a710 | 217 | * lpfc_discovery_wait - Wait for driver discovery to quiesce |
90160e01 JS |
218 | * @vport: The virtual port for which this call is being executed. |
219 | * | |
220 | * This driver calls this routine specifically from lpfc_vport_delete | |
221 | * to enforce a synchronous execution of vport | |
222 | * delete relative to discovery activities. The | |
223 | * lpfc_vport_delete routine should not return until it | |
224 | * can reasonably guarantee that discovery has quiesced. | |
225 | * Post FDISC LOGO, the driver must wait until its SAN teardown is | |
226 | * complete and all resources recovered before allowing | |
227 | * cleanup. | |
228 | * | |
229 | * This routine does not require any locks held. | |
230 | **/ | |
231 | static void lpfc_discovery_wait(struct lpfc_vport *vport) | |
232 | { | |
233 | struct lpfc_hba *phba = vport->phba; | |
234 | uint32_t wait_flags = 0; | |
235 | unsigned long wait_time_max; | |
236 | unsigned long start_time; | |
237 | ||
238 | wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE | | |
239 | FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO; | |
240 | ||
241 | /* | |
242 | * The time constraint on this loop is a balance between the | |
243 | * fabric RA_TOV value and dev_loss tmo. The driver's | |
244 | * devloss_tmo is 10 giving this loop a 3x multiplier minimally. | |
245 | */ | |
246 | wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000); | |
247 | wait_time_max += jiffies; | |
248 | start_time = jiffies; | |
249 | while (time_before(jiffies, wait_time_max)) { | |
250 | if ((vport->num_disc_nodes > 0) || | |
251 | (vport->fc_flag & wait_flags) || | |
252 | ((vport->port_state > LPFC_VPORT_FAILED) && | |
253 | (vport->port_state < LPFC_VPORT_READY))) { | |
21e9a0a5 | 254 | lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT, |
90160e01 | 255 | "1833 Vport discovery quiesce Wait:" |
21e9a0a5 | 256 | " state x%x fc_flags x%x" |
90160e01 JS |
257 | " num_nodes x%x, waiting 1000 msecs" |
258 | " total wait msecs x%x\n", | |
21e9a0a5 JS |
259 | vport->port_state, vport->fc_flag, |
260 | vport->num_disc_nodes, | |
90160e01 JS |
261 | jiffies_to_msecs(jiffies - start_time)); |
262 | msleep(1000); | |
263 | } else { | |
264 | /* Base case. Wait variants satisfied. Break out */ | |
21e9a0a5 | 265 | lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT, |
90160e01 | 266 | "1834 Vport discovery quiesced:" |
21e9a0a5 | 267 | " state x%x fc_flags x%x" |
90160e01 | 268 | " wait msecs x%x\n", |
21e9a0a5 | 269 | vport->port_state, vport->fc_flag, |
90160e01 JS |
270 | jiffies_to_msecs(jiffies |
271 | - start_time)); | |
272 | break; | |
273 | } | |
274 | } | |
275 | ||
276 | if (time_after(jiffies, wait_time_max)) | |
21e9a0a5 | 277 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, |
90160e01 | 278 | "1835 Vport discovery quiesce failed:" |
21e9a0a5 JS |
279 | " state x%x fc_flags x%x wait msecs x%x\n", |
280 | vport->port_state, vport->fc_flag, | |
90160e01 JS |
281 | jiffies_to_msecs(jiffies - start_time)); |
282 | } | |
283 | ||
92d7f7b0 JS |
284 | int |
285 | lpfc_vport_create(struct fc_vport *fc_vport, bool disable) | |
286 | { | |
287 | struct lpfc_nodelist *ndlp; | |
3de2a653 JS |
288 | struct Scsi_Host *shost = fc_vport->shost; |
289 | struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata; | |
92d7f7b0 JS |
290 | struct lpfc_hba *phba = pport->phba; |
291 | struct lpfc_vport *vport = NULL; | |
292 | int instance; | |
293 | int vpi; | |
294 | int rc = VPORT_ERROR; | |
98c9ea5c | 295 | int status; |
92d7f7b0 | 296 | |
eada272d | 297 | if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) { |
92d7f7b0 | 298 | lpfc_printf_log(phba, KERN_ERR, LOG_VPORT, |
e8b62011 | 299 | "1808 Create VPORT failed: " |
92d7f7b0 | 300 | "NPIV is not enabled: SLImode:%d\n", |
e8b62011 | 301 | phba->sli_rev); |
92d7f7b0 JS |
302 | rc = VPORT_INVAL; |
303 | goto error_out; | |
304 | } | |
305 | ||
306 | vpi = lpfc_alloc_vpi(phba); | |
307 | if (vpi == 0) { | |
308 | lpfc_printf_log(phba, KERN_ERR, LOG_VPORT, | |
e8b62011 | 309 | "1809 Create VPORT failed: " |
92d7f7b0 | 310 | "Max VPORTs (%d) exceeded\n", |
e8b62011 | 311 | phba->max_vpi); |
92d7f7b0 JS |
312 | rc = VPORT_NORESOURCES; |
313 | goto error_out; | |
314 | } | |
315 | ||
da0436e9 JS |
316 | /* |
317 | * In SLI4, the vpi must be activated before it can be used | |
318 | * by the port. | |
319 | */ | |
320 | if (phba->sli_rev == LPFC_SLI_REV4) { | |
321 | rc = lpfc_sli4_init_vpi(phba, vpi); | |
322 | if (rc) { | |
323 | lpfc_printf_log(phba, KERN_ERR, LOG_VPORT, | |
324 | "1838 Failed to INIT_VPI on vpi %d " | |
325 | "status %d\n", vpi, rc); | |
326 | rc = VPORT_NORESOURCES; | |
327 | lpfc_free_vpi(phba, vpi); | |
328 | goto error_out; | |
329 | } | |
330 | } | |
92d7f7b0 JS |
331 | |
332 | /* Assign an unused board number */ | |
333 | if ((instance = lpfc_get_instance()) < 0) { | |
334 | lpfc_printf_log(phba, KERN_ERR, LOG_VPORT, | |
e8b62011 JS |
335 | "1810 Create VPORT failed: Cannot get " |
336 | "instance number\n"); | |
92d7f7b0 JS |
337 | lpfc_free_vpi(phba, vpi); |
338 | rc = VPORT_NORESOURCES; | |
339 | goto error_out; | |
340 | } | |
341 | ||
3de2a653 | 342 | vport = lpfc_create_port(phba, instance, &fc_vport->dev); |
92d7f7b0 JS |
343 | if (!vport) { |
344 | lpfc_printf_log(phba, KERN_ERR, LOG_VPORT, | |
e8b62011 | 345 | "1811 Create VPORT failed: vpi x%x\n", vpi); |
92d7f7b0 JS |
346 | lpfc_free_vpi(phba, vpi); |
347 | rc = VPORT_NORESOURCES; | |
348 | goto error_out; | |
349 | } | |
350 | ||
351 | vport->vpi = vpi; | |
858c9f6c JS |
352 | lpfc_debugfs_initialize(vport); |
353 | ||
98c9ea5c JS |
354 | if ((status = lpfc_vport_sparm(phba, vport))) { |
355 | if (status == -EINTR) { | |
356 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, | |
357 | "1831 Create VPORT Interrupted.\n"); | |
358 | rc = VPORT_ERROR; | |
359 | } else { | |
360 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, | |
361 | "1813 Create VPORT failed. " | |
362 | "Cannot get sparam\n"); | |
363 | rc = VPORT_NORESOURCES; | |
364 | } | |
92d7f7b0 JS |
365 | lpfc_free_vpi(phba, vpi); |
366 | destroy_port(vport); | |
92d7f7b0 JS |
367 | goto error_out; |
368 | } | |
369 | ||
370 | memcpy(vport->fc_portname.u.wwn, vport->fc_sparam.portName.u.wwn, 8); | |
371 | memcpy(vport->fc_nodename.u.wwn, vport->fc_sparam.nodeName.u.wwn, 8); | |
92d7f7b0 JS |
372 | if (fc_vport->node_name != 0) |
373 | u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn); | |
374 | if (fc_vport->port_name != 0) | |
375 | u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn); | |
376 | ||
377 | memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8); | |
378 | memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8); | |
379 | ||
380 | if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") || | |
381 | !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) { | |
e8b62011 JS |
382 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, |
383 | "1821 Create VPORT failed. " | |
384 | "Invalid WWN format\n"); | |
92d7f7b0 JS |
385 | lpfc_free_vpi(phba, vpi); |
386 | destroy_port(vport); | |
387 | rc = VPORT_INVAL; | |
388 | goto error_out; | |
389 | } | |
390 | ||
391 | if (!lpfc_unique_wwpn(phba, vport)) { | |
e8b62011 JS |
392 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, |
393 | "1823 Create VPORT failed. " | |
394 | "Duplicate WWN on HBA\n"); | |
92d7f7b0 JS |
395 | lpfc_free_vpi(phba, vpi); |
396 | destroy_port(vport); | |
397 | rc = VPORT_INVAL; | |
398 | goto error_out; | |
399 | } | |
400 | ||
eada272d JS |
401 | /* Create binary sysfs attribute for vport */ |
402 | lpfc_alloc_sysfs_attr(vport); | |
403 | ||
92d7f7b0 JS |
404 | *(struct lpfc_vport **)fc_vport->dd_data = vport; |
405 | vport->fc_vport = fc_vport; | |
406 | ||
407 | if ((phba->link_state < LPFC_LINK_UP) || | |
408 | (phba->fc_topology == TOPOLOGY_LOOP)) { | |
409 | lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN); | |
410 | rc = VPORT_OK; | |
411 | goto out; | |
412 | } | |
413 | ||
414 | if (disable) { | |
eada272d | 415 | lpfc_vport_set_state(vport, FC_VPORT_DISABLED); |
92d7f7b0 JS |
416 | rc = VPORT_OK; |
417 | goto out; | |
418 | } | |
419 | ||
420 | /* Use the Physical nodes Fabric NDLP to determine if the link is | |
421 | * up and ready to FDISC. | |
422 | */ | |
423 | ndlp = lpfc_findnode_did(phba->pport, Fabric_DID); | |
e47c9093 JS |
424 | if (ndlp && NLP_CHK_NODE_ACT(ndlp) && |
425 | ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) { | |
858c9f6c JS |
426 | if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) { |
427 | lpfc_set_disctmo(vport); | |
428 | lpfc_initial_fdisc(vport); | |
429 | } else { | |
430 | lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP); | |
e8b62011 JS |
431 | lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS, |
432 | "0262 No NPIV Fabric support\n"); | |
858c9f6c | 433 | } |
92d7f7b0 JS |
434 | } else { |
435 | lpfc_vport_set_state(vport, FC_VPORT_FAILED); | |
436 | } | |
437 | rc = VPORT_OK; | |
438 | ||
439 | out: | |
a58cbd52 JS |
440 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, |
441 | "1825 Vport Created.\n"); | |
92d7f7b0 JS |
442 | lpfc_host_attrib_init(lpfc_shost_from_vport(vport)); |
443 | error_out: | |
444 | return rc; | |
445 | } | |
446 | ||
311464ec | 447 | static int |
92d7f7b0 JS |
448 | disable_vport(struct fc_vport *fc_vport) |
449 | { | |
450 | struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data; | |
451 | struct lpfc_hba *phba = vport->phba; | |
452 | struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL; | |
453 | long timeout; | |
454 | ||
455 | ndlp = lpfc_findnode_did(vport, Fabric_DID); | |
e47c9093 JS |
456 | if (ndlp && NLP_CHK_NODE_ACT(ndlp) |
457 | && phba->link_state >= LPFC_LINK_UP) { | |
92d7f7b0 JS |
458 | vport->unreg_vpi_cmpl = VPORT_INVAL; |
459 | timeout = msecs_to_jiffies(phba->fc_ratov * 2000); | |
460 | if (!lpfc_issue_els_npiv_logo(vport, ndlp)) | |
461 | while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout) | |
462 | timeout = schedule_timeout(timeout); | |
463 | } | |
464 | ||
465 | lpfc_sli_host_down(vport); | |
466 | ||
467 | /* Mark all nodes for discovery so we can remove them by | |
468 | * calling lpfc_cleanup_rpis(vport, 1) | |
469 | */ | |
470 | list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) { | |
e47c9093 JS |
471 | if (!NLP_CHK_NODE_ACT(ndlp)) |
472 | continue; | |
92d7f7b0 JS |
473 | if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) |
474 | continue; | |
475 | lpfc_disc_state_machine(vport, ndlp, NULL, | |
476 | NLP_EVT_DEVICE_RECOVERY); | |
477 | } | |
478 | lpfc_cleanup_rpis(vport, 1); | |
479 | ||
480 | lpfc_stop_vport_timers(vport); | |
481 | lpfc_unreg_all_rpis(vport); | |
482 | lpfc_unreg_default_rpis(vport); | |
483 | /* | |
484 | * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the | |
485 | * scsi_host_put() to release the vport. | |
486 | */ | |
487 | lpfc_mbx_unreg_vpi(vport); | |
488 | ||
489 | lpfc_vport_set_state(vport, FC_VPORT_DISABLED); | |
a58cbd52 JS |
490 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, |
491 | "1826 Vport Disabled.\n"); | |
92d7f7b0 JS |
492 | return VPORT_OK; |
493 | } | |
494 | ||
311464ec | 495 | static int |
92d7f7b0 JS |
496 | enable_vport(struct fc_vport *fc_vport) |
497 | { | |
498 | struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data; | |
499 | struct lpfc_hba *phba = vport->phba; | |
500 | struct lpfc_nodelist *ndlp = NULL; | |
501 | ||
502 | if ((phba->link_state < LPFC_LINK_UP) || | |
503 | (phba->fc_topology == TOPOLOGY_LOOP)) { | |
504 | lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN); | |
505 | return VPORT_OK; | |
506 | } | |
507 | ||
508 | vport->load_flag |= FC_LOADING; | |
509 | vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI; | |
510 | ||
511 | /* Use the Physical nodes Fabric NDLP to determine if the link is | |
512 | * up and ready to FDISC. | |
513 | */ | |
514 | ndlp = lpfc_findnode_did(phba->pport, Fabric_DID); | |
e47c9093 JS |
515 | if (ndlp && NLP_CHK_NODE_ACT(ndlp) |
516 | && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) { | |
858c9f6c JS |
517 | if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) { |
518 | lpfc_set_disctmo(vport); | |
519 | lpfc_initial_fdisc(vport); | |
520 | } else { | |
521 | lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP); | |
e8b62011 JS |
522 | lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS, |
523 | "0264 No NPIV Fabric support\n"); | |
858c9f6c | 524 | } |
92d7f7b0 JS |
525 | } else { |
526 | lpfc_vport_set_state(vport, FC_VPORT_FAILED); | |
527 | } | |
a58cbd52 JS |
528 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, |
529 | "1827 Vport Enabled.\n"); | |
92d7f7b0 JS |
530 | return VPORT_OK; |
531 | } | |
532 | ||
533 | int | |
534 | lpfc_vport_disable(struct fc_vport *fc_vport, bool disable) | |
535 | { | |
536 | if (disable) | |
537 | return disable_vport(fc_vport); | |
538 | else | |
539 | return enable_vport(fc_vport); | |
540 | } | |
541 | ||
542 | ||
543 | int | |
544 | lpfc_vport_delete(struct fc_vport *fc_vport) | |
545 | { | |
546 | struct lpfc_nodelist *ndlp = NULL; | |
92d7f7b0 JS |
547 | struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost; |
548 | struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data; | |
549 | struct lpfc_hba *phba = vport->phba; | |
550 | long timeout; | |
92d7f7b0 | 551 | |
51ef4c26 JS |
552 | if (vport->port_type == LPFC_PHYSICAL_PORT) { |
553 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, | |
554 | "1812 vport_delete failed: Cannot delete " | |
555 | "physical host\n"); | |
556 | return VPORT_ERROR; | |
557 | } | |
21e9a0a5 JS |
558 | |
559 | /* If the vport is a static vport fail the deletion. */ | |
560 | if ((vport->vport_flag & STATIC_VPORT) && | |
561 | !(phba->pport->load_flag & FC_UNLOADING)) { | |
562 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, | |
563 | "1837 vport_delete failed: Cannot delete " | |
564 | "static vport.\n"); | |
565 | return VPORT_ERROR; | |
566 | } | |
567 | ||
51ef4c26 JS |
568 | /* |
569 | * If we are not unloading the driver then prevent the vport_delete | |
570 | * from happening until after this vport's discovery is finished. | |
571 | */ | |
572 | if (!(phba->pport->load_flag & FC_UNLOADING)) { | |
573 | int check_count = 0; | |
574 | while (check_count < ((phba->fc_ratov * 3) + 3) && | |
575 | vport->port_state > LPFC_VPORT_FAILED && | |
576 | vport->port_state < LPFC_VPORT_READY) { | |
577 | check_count++; | |
578 | msleep(1000); | |
579 | } | |
580 | if (vport->port_state > LPFC_VPORT_FAILED && | |
581 | vport->port_state < LPFC_VPORT_READY) | |
582 | return -EAGAIN; | |
583 | } | |
92d7f7b0 JS |
584 | /* |
585 | * This is a bit of a mess. We want to ensure the shost doesn't get | |
586 | * torn down until we're done with the embedded lpfc_vport structure. | |
587 | * | |
588 | * Beyond holding a reference for this function, we also need a | |
589 | * reference for outstanding I/O requests we schedule during delete | |
590 | * processing. But once we scsi_remove_host() we can no longer obtain | |
591 | * a reference through scsi_host_get(). | |
592 | * | |
593 | * So we take two references here. We release one reference at the | |
594 | * bottom of the function -- after delinking the vport. And we | |
595 | * release the other at the completion of the unreg_vpi that get's | |
596 | * initiated after we've disposed of all other resources associated | |
597 | * with the port. | |
598 | */ | |
d7c255b2 | 599 | if (!scsi_host_get(shost)) |
92d7f7b0 | 600 | return VPORT_INVAL; |
d7c255b2 JS |
601 | if (!scsi_host_get(shost)) { |
602 | scsi_host_put(shost); | |
603 | return VPORT_INVAL; | |
604 | } | |
51ef4c26 | 605 | spin_lock_irq(&phba->hbalock); |
92d7f7b0 | 606 | vport->load_flag |= FC_UNLOADING; |
51ef4c26 | 607 | spin_unlock_irq(&phba->hbalock); |
eada272d JS |
608 | |
609 | lpfc_free_sysfs_attr(vport); | |
610 | ||
858c9f6c | 611 | lpfc_debugfs_terminate(vport); |
eada272d JS |
612 | |
613 | /* Remove FC host and then SCSI host with the vport */ | |
92d7f7b0 JS |
614 | fc_remove_host(lpfc_shost_from_vport(vport)); |
615 | scsi_remove_host(lpfc_shost_from_vport(vport)); | |
616 | ||
617 | ndlp = lpfc_findnode_did(phba->pport, Fabric_DID); | |
e47c9093 JS |
618 | |
619 | /* In case of driver unload, we shall not perform fabric logo as the | |
620 | * worker thread already stopped at this stage and, in this case, we | |
621 | * can safely skip the fabric logo. | |
622 | */ | |
623 | if (phba->pport->load_flag & FC_UNLOADING) { | |
624 | if (ndlp && NLP_CHK_NODE_ACT(ndlp) && | |
625 | ndlp->nlp_state == NLP_STE_UNMAPPED_NODE && | |
626 | phba->link_state >= LPFC_LINK_UP) { | |
627 | /* First look for the Fabric ndlp */ | |
628 | ndlp = lpfc_findnode_did(vport, Fabric_DID); | |
629 | if (!ndlp) | |
630 | goto skip_logo; | |
631 | else if (!NLP_CHK_NODE_ACT(ndlp)) { | |
632 | ndlp = lpfc_enable_node(vport, ndlp, | |
633 | NLP_STE_UNUSED_NODE); | |
634 | if (!ndlp) | |
635 | goto skip_logo; | |
636 | } | |
637 | /* Remove ndlp from vport npld list */ | |
638 | lpfc_dequeue_node(vport, ndlp); | |
639 | ||
640 | /* Indicate free memory when release */ | |
641 | spin_lock_irq(&phba->ndlp_lock); | |
642 | NLP_SET_FREE_REQ(ndlp); | |
643 | spin_unlock_irq(&phba->ndlp_lock); | |
644 | /* Kick off release ndlp when it can be safely done */ | |
645 | lpfc_nlp_put(ndlp); | |
646 | } | |
647 | goto skip_logo; | |
648 | } | |
649 | ||
650 | /* Otherwise, we will perform fabric logo as needed */ | |
651 | if (ndlp && NLP_CHK_NODE_ACT(ndlp) && | |
652 | ndlp->nlp_state == NLP_STE_UNMAPPED_NODE && | |
58da1ffb JS |
653 | phba->link_state >= LPFC_LINK_UP && |
654 | phba->fc_topology != TOPOLOGY_LOOP) { | |
7ee5d43e JS |
655 | if (vport->cfg_enable_da_id) { |
656 | timeout = msecs_to_jiffies(phba->fc_ratov * 2000); | |
657 | if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0)) | |
658 | while (vport->ct_flags && timeout) | |
659 | timeout = schedule_timeout(timeout); | |
660 | else | |
661 | lpfc_printf_log(vport->phba, KERN_WARNING, | |
662 | LOG_VPORT, | |
663 | "1829 CT command failed to " | |
664 | "delete objects on fabric. \n"); | |
665 | } | |
92d7f7b0 JS |
666 | /* First look for the Fabric ndlp */ |
667 | ndlp = lpfc_findnode_did(vport, Fabric_DID); | |
668 | if (!ndlp) { | |
669 | /* Cannot find existing Fabric ndlp, allocate one */ | |
670 | ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL); | |
671 | if (!ndlp) | |
672 | goto skip_logo; | |
673 | lpfc_nlp_init(vport, ndlp, Fabric_DID); | |
e47c9093 JS |
674 | /* Indicate free memory when release */ |
675 | NLP_SET_FREE_REQ(ndlp); | |
92d7f7b0 | 676 | } else { |
e47c9093 JS |
677 | if (!NLP_CHK_NODE_ACT(ndlp)) |
678 | ndlp = lpfc_enable_node(vport, ndlp, | |
679 | NLP_STE_UNUSED_NODE); | |
680 | if (!ndlp) | |
681 | goto skip_logo; | |
682 | ||
683 | /* Remove ndlp from vport npld list */ | |
92d7f7b0 | 684 | lpfc_dequeue_node(vport, ndlp); |
e47c9093 JS |
685 | spin_lock_irq(&phba->ndlp_lock); |
686 | if (!NLP_CHK_FREE_REQ(ndlp)) | |
687 | /* Indicate free memory when release */ | |
688 | NLP_SET_FREE_REQ(ndlp); | |
689 | else { | |
690 | /* Skip this if ndlp is already in free mode */ | |
691 | spin_unlock_irq(&phba->ndlp_lock); | |
692 | goto skip_logo; | |
693 | } | |
694 | spin_unlock_irq(&phba->ndlp_lock); | |
92d7f7b0 JS |
695 | } |
696 | vport->unreg_vpi_cmpl = VPORT_INVAL; | |
697 | timeout = msecs_to_jiffies(phba->fc_ratov * 2000); | |
698 | if (!lpfc_issue_els_npiv_logo(vport, ndlp)) | |
699 | while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout) | |
700 | timeout = schedule_timeout(timeout); | |
701 | } | |
702 | ||
90160e01 JS |
703 | if (!(phba->pport->load_flag & FC_UNLOADING)) |
704 | lpfc_discovery_wait(vport); | |
705 | ||
92d7f7b0 | 706 | skip_logo: |
87af33fe | 707 | lpfc_cleanup(vport); |
92d7f7b0 JS |
708 | lpfc_sli_host_down(vport); |
709 | ||
92d7f7b0 | 710 | lpfc_stop_vport_timers(vport); |
87af33fe JS |
711 | |
712 | if (!(phba->pport->load_flag & FC_UNLOADING)) { | |
e47c9093 | 713 | lpfc_unreg_all_rpis(vport); |
87af33fe JS |
714 | lpfc_unreg_default_rpis(vport); |
715 | /* | |
716 | * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) | |
717 | * does the scsi_host_put() to release the vport. | |
718 | */ | |
d7c255b2 JS |
719 | if (lpfc_mbx_unreg_vpi(vport)) |
720 | scsi_host_put(shost); | |
721 | } else | |
722 | scsi_host_put(shost); | |
92d7f7b0 JS |
723 | |
724 | lpfc_free_vpi(phba, vport->vpi); | |
725 | vport->work_port_events = 0; | |
726 | spin_lock_irq(&phba->hbalock); | |
727 | list_del_init(&vport->listentry); | |
728 | spin_unlock_irq(&phba->hbalock); | |
a58cbd52 JS |
729 | lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT, |
730 | "1828 Vport Deleted.\n"); | |
92d7f7b0 | 731 | scsi_host_put(shost); |
51ef4c26 | 732 | return VPORT_OK; |
92d7f7b0 JS |
733 | } |
734 | ||
549e55cd JS |
735 | struct lpfc_vport ** |
736 | lpfc_create_vport_work_array(struct lpfc_hba *phba) | |
737 | { | |
738 | struct lpfc_vport *port_iterator; | |
739 | struct lpfc_vport **vports; | |
740 | int index = 0; | |
21e9a0a5 | 741 | vports = kzalloc((phba->max_vports + 1) * sizeof(struct lpfc_vport *), |
549e55cd JS |
742 | GFP_KERNEL); |
743 | if (vports == NULL) | |
744 | return NULL; | |
745 | spin_lock_irq(&phba->hbalock); | |
746 | list_for_each_entry(port_iterator, &phba->port_list, listentry) { | |
e8b62011 | 747 | if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) { |
51ef4c26 | 748 | lpfc_printf_vlog(port_iterator, KERN_WARNING, LOG_VPORT, |
e8b62011 JS |
749 | "1801 Create vport work array FAILED: " |
750 | "cannot do scsi_host_get\n"); | |
549e55cd | 751 | continue; |
e8b62011 | 752 | } |
549e55cd JS |
753 | vports[index++] = port_iterator; |
754 | } | |
755 | spin_unlock_irq(&phba->hbalock); | |
756 | return vports; | |
757 | } | |
758 | ||
759 | void | |
09372820 | 760 | lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports) |
549e55cd JS |
761 | { |
762 | int i; | |
763 | if (vports == NULL) | |
764 | return; | |
21e9a0a5 | 765 | for (i = 0; vports[i] != NULL && i <= phba->max_vports; i++) |
549e55cd JS |
766 | scsi_host_put(lpfc_shost_from_vport(vports[i])); |
767 | kfree(vports); | |
768 | } | |
ea2151b4 JS |
769 | |
770 | ||
771 | /** | |
3621a710 | 772 | * lpfc_vport_reset_stat_data - Reset the statistical data for the vport |
ea2151b4 JS |
773 | * @vport: Pointer to vport object. |
774 | * | |
775 | * This function resets the statistical data for the vport. This function | |
776 | * is called with the host_lock held | |
777 | **/ | |
778 | void | |
779 | lpfc_vport_reset_stat_data(struct lpfc_vport *vport) | |
780 | { | |
781 | struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL; | |
782 | ||
783 | list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) { | |
784 | if (!NLP_CHK_NODE_ACT(ndlp)) | |
785 | continue; | |
786 | if (ndlp->lat_data) | |
787 | memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT * | |
788 | sizeof(struct lpfc_scsicmd_bkt)); | |
789 | } | |
790 | } | |
791 | ||
792 | ||
793 | /** | |
3621a710 | 794 | * lpfc_alloc_bucket - Allocate data buffer required for statistical data |
ea2151b4 JS |
795 | * @vport: Pointer to vport object. |
796 | * | |
797 | * This function allocates data buffer required for all the FC | |
798 | * nodes of the vport to collect statistical data. | |
799 | **/ | |
800 | void | |
801 | lpfc_alloc_bucket(struct lpfc_vport *vport) | |
802 | { | |
803 | struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL; | |
804 | ||
805 | list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) { | |
806 | if (!NLP_CHK_NODE_ACT(ndlp)) | |
807 | continue; | |
808 | ||
809 | kfree(ndlp->lat_data); | |
810 | ndlp->lat_data = NULL; | |
811 | ||
812 | if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) { | |
813 | ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT, | |
814 | sizeof(struct lpfc_scsicmd_bkt), | |
815 | GFP_ATOMIC); | |
816 | ||
817 | if (!ndlp->lat_data) | |
818 | lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE, | |
819 | "0287 lpfc_alloc_bucket failed to " | |
820 | "allocate statistical data buffer DID " | |
821 | "0x%x\n", ndlp->nlp_DID); | |
822 | } | |
823 | } | |
824 | } | |
825 | ||
826 | /** | |
3621a710 | 827 | * lpfc_free_bucket - Free data buffer required for statistical data |
ea2151b4 JS |
828 | * @vport: Pointer to vport object. |
829 | * | |
830 | * Th function frees statistical data buffer of all the FC | |
831 | * nodes of the vport. | |
832 | **/ | |
833 | void | |
834 | lpfc_free_bucket(struct lpfc_vport *vport) | |
835 | { | |
836 | struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL; | |
837 | ||
838 | list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) { | |
839 | if (!NLP_CHK_NODE_ACT(ndlp)) | |
840 | continue; | |
841 | ||
842 | kfree(ndlp->lat_data); | |
843 | ndlp->lat_data = NULL; | |
844 | } | |
845 | } |