• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
5  * “Broadcom” refers to Broadcom Limited and/or its subsidiaries.  *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/idr.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/kthread.h>
31 #include <linux/pci.h>
32 #include <linux/spinlock.h>
33 #include <linux/ctype.h>
34 #include <linux/aer.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <linux/miscdevice.h>
38 #include <linux/percpu.h>
39 #include <linux/msi.h>
40 #include <linux/bitops.h>
41 
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_host.h>
45 #include <scsi/scsi_transport_fc.h>
46 #include <scsi/scsi_tcq.h>
47 #include <scsi/fc/fc_fs.h>
48 
49 #include <linux/nvme-fc-driver.h>
50 
51 #include "lpfc_hw4.h"
52 #include "lpfc_hw.h"
53 #include "lpfc_sli.h"
54 #include "lpfc_sli4.h"
55 #include "lpfc_nl.h"
56 #include "lpfc_disc.h"
57 #include "lpfc.h"
58 #include "lpfc_scsi.h"
59 #include "lpfc_nvme.h"
60 #include "lpfc_nvmet.h"
61 #include "lpfc_logmsg.h"
62 #include "lpfc_crtn.h"
63 #include "lpfc_vport.h"
64 #include "lpfc_version.h"
65 #include "lpfc_ids.h"
66 
67 char *_dump_buf_data;
68 unsigned long _dump_buf_data_order;
69 char *_dump_buf_dif;
70 unsigned long _dump_buf_dif_order;
71 spinlock_t _dump_buf_lock;
72 
73 /* Used when mapping IRQ vectors in a driver centric manner */
74 uint16_t *lpfc_used_cpu;
75 uint32_t lpfc_present_cpu;
76 
77 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
78 static int lpfc_post_rcv_buf(struct lpfc_hba *);
79 static int lpfc_sli4_queue_verify(struct lpfc_hba *);
80 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
81 static int lpfc_setup_endian_order(struct lpfc_hba *);
82 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
83 static void lpfc_free_els_sgl_list(struct lpfc_hba *);
84 static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
85 static void lpfc_init_sgl_list(struct lpfc_hba *);
86 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
87 static void lpfc_free_active_sgl(struct lpfc_hba *);
88 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
89 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
90 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
91 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
92 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
93 static void lpfc_sli4_disable_intr(struct lpfc_hba *);
94 static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
95 static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
96 
97 static struct scsi_transport_template *lpfc_transport_template = NULL;
98 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
99 static DEFINE_IDR(lpfc_hba_index);
100 #define LPFC_NVMET_BUF_POST 254
101 
102 /**
103  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
104  * @phba: pointer to lpfc hba data structure.
105  *
106  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
107  * mailbox command. It retrieves the revision information from the HBA and
108  * collects the Vital Product Data (VPD) about the HBA for preparing the
109  * configuration of the HBA.
110  *
111  * Return codes:
112  *   0 - success.
113  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
114  *   Any other value - indicates an error.
115  **/
116 int
lpfc_config_port_prep(struct lpfc_hba * phba)117 lpfc_config_port_prep(struct lpfc_hba *phba)
118 {
119 	lpfc_vpd_t *vp = &phba->vpd;
120 	int i = 0, rc;
121 	LPFC_MBOXQ_t *pmb;
122 	MAILBOX_t *mb;
123 	char *lpfc_vpd_data = NULL;
124 	uint16_t offset = 0;
125 	static char licensed[56] =
126 		    "key unlock for use with gnu public licensed code only\0";
127 	static int init_key = 1;
128 
129 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
130 	if (!pmb) {
131 		phba->link_state = LPFC_HBA_ERROR;
132 		return -ENOMEM;
133 	}
134 
135 	mb = &pmb->u.mb;
136 	phba->link_state = LPFC_INIT_MBX_CMDS;
137 
138 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
139 		if (init_key) {
140 			uint32_t *ptext = (uint32_t *) licensed;
141 
142 			for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
143 				*ptext = cpu_to_be32(*ptext);
144 			init_key = 0;
145 		}
146 
147 		lpfc_read_nv(phba, pmb);
148 		memset((char*)mb->un.varRDnvp.rsvd3, 0,
149 			sizeof (mb->un.varRDnvp.rsvd3));
150 		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
151 			 sizeof (licensed));
152 
153 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154 
155 		if (rc != MBX_SUCCESS) {
156 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
157 					"0324 Config Port initialization "
158 					"error, mbxCmd x%x READ_NVPARM, "
159 					"mbxStatus x%x\n",
160 					mb->mbxCommand, mb->mbxStatus);
161 			mempool_free(pmb, phba->mbox_mem_pool);
162 			return -ERESTART;
163 		}
164 		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
165 		       sizeof(phba->wwnn));
166 		memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
167 		       sizeof(phba->wwpn));
168 	}
169 
170 	phba->sli3_options = 0x0;
171 
172 	/* Setup and issue mailbox READ REV command */
173 	lpfc_read_rev(phba, pmb);
174 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
175 	if (rc != MBX_SUCCESS) {
176 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
177 				"0439 Adapter failed to init, mbxCmd x%x "
178 				"READ_REV, mbxStatus x%x\n",
179 				mb->mbxCommand, mb->mbxStatus);
180 		mempool_free( pmb, phba->mbox_mem_pool);
181 		return -ERESTART;
182 	}
183 
184 
185 	/*
186 	 * The value of rr must be 1 since the driver set the cv field to 1.
187 	 * This setting requires the FW to set all revision fields.
188 	 */
189 	if (mb->un.varRdRev.rr == 0) {
190 		vp->rev.rBit = 0;
191 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
192 				"0440 Adapter failed to init, READ_REV has "
193 				"missing revision information.\n");
194 		mempool_free(pmb, phba->mbox_mem_pool);
195 		return -ERESTART;
196 	}
197 
198 	if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
199 		mempool_free(pmb, phba->mbox_mem_pool);
200 		return -EINVAL;
201 	}
202 
203 	/* Save information as VPD data */
204 	vp->rev.rBit = 1;
205 	memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
206 	vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
207 	memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
208 	vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
209 	memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
210 	vp->rev.biuRev = mb->un.varRdRev.biuRev;
211 	vp->rev.smRev = mb->un.varRdRev.smRev;
212 	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
213 	vp->rev.endecRev = mb->un.varRdRev.endecRev;
214 	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
215 	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
216 	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
217 	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
218 	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
219 	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
220 
221 	/* If the sli feature level is less then 9, we must
222 	 * tear down all RPIs and VPIs on link down if NPIV
223 	 * is enabled.
224 	 */
225 	if (vp->rev.feaLevelHigh < 9)
226 		phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
227 
228 	if (lpfc_is_LC_HBA(phba->pcidev->device))
229 		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
230 						sizeof (phba->RandomData));
231 
232 	/* Get adapter VPD information */
233 	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
234 	if (!lpfc_vpd_data)
235 		goto out_free_mbox;
236 	do {
237 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
238 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
239 
240 		if (rc != MBX_SUCCESS) {
241 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
242 					"0441 VPD not present on adapter, "
243 					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
244 					mb->mbxCommand, mb->mbxStatus);
245 			mb->un.varDmp.word_cnt = 0;
246 		}
247 		/* dump mem may return a zero when finished or we got a
248 		 * mailbox error, either way we are done.
249 		 */
250 		if (mb->un.varDmp.word_cnt == 0)
251 			break;
252 		if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
253 			mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
254 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
255 				      lpfc_vpd_data + offset,
256 				      mb->un.varDmp.word_cnt);
257 		offset += mb->un.varDmp.word_cnt;
258 	} while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
259 	lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
260 
261 	kfree(lpfc_vpd_data);
262 out_free_mbox:
263 	mempool_free(pmb, phba->mbox_mem_pool);
264 	return 0;
265 }
266 
267 /**
268  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
269  * @phba: pointer to lpfc hba data structure.
270  * @pmboxq: pointer to the driver internal queue element for mailbox command.
271  *
272  * This is the completion handler for driver's configuring asynchronous event
273  * mailbox command to the device. If the mailbox command returns successfully,
274  * it will set internal async event support flag to 1; otherwise, it will
275  * set internal async event support flag to 0.
276  **/
277 static void
lpfc_config_async_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)278 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
279 {
280 	if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
281 		phba->temp_sensor_support = 1;
282 	else
283 		phba->temp_sensor_support = 0;
284 	mempool_free(pmboxq, phba->mbox_mem_pool);
285 	return;
286 }
287 
288 /**
289  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
290  * @phba: pointer to lpfc hba data structure.
291  * @pmboxq: pointer to the driver internal queue element for mailbox command.
292  *
293  * This is the completion handler for dump mailbox command for getting
294  * wake up parameters. When this command complete, the response contain
295  * Option rom version of the HBA. This function translate the version number
296  * into a human readable string and store it in OptionROMVersion.
297  **/
298 static void
lpfc_dump_wakeup_param_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)299 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
300 {
301 	struct prog_id *prg;
302 	uint32_t prog_id_word;
303 	char dist = ' ';
304 	/* character array used for decoding dist type. */
305 	char dist_char[] = "nabx";
306 
307 	if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
308 		mempool_free(pmboxq, phba->mbox_mem_pool);
309 		return;
310 	}
311 
312 	prg = (struct prog_id *) &prog_id_word;
313 
314 	/* word 7 contain option rom version */
315 	prog_id_word = pmboxq->u.mb.un.varWords[7];
316 
317 	/* Decode the Option rom version word to a readable string */
318 	if (prg->dist < 4)
319 		dist = dist_char[prg->dist];
320 
321 	if ((prg->dist == 3) && (prg->num == 0))
322 		snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
323 			prg->ver, prg->rev, prg->lev);
324 	else
325 		snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
326 			prg->ver, prg->rev, prg->lev,
327 			dist, prg->num);
328 	mempool_free(pmboxq, phba->mbox_mem_pool);
329 	return;
330 }
331 
332 /**
333  * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
334  *	cfg_soft_wwnn, cfg_soft_wwpn
335  * @vport: pointer to lpfc vport data structure.
336  *
337  *
338  * Return codes
339  *   None.
340  **/
341 void
lpfc_update_vport_wwn(struct lpfc_vport * vport)342 lpfc_update_vport_wwn(struct lpfc_vport *vport)
343 {
344 	uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
345 	u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
346 
347 	/* If the soft name exists then update it using the service params */
348 	if (vport->phba->cfg_soft_wwnn)
349 		u64_to_wwn(vport->phba->cfg_soft_wwnn,
350 			   vport->fc_sparam.nodeName.u.wwn);
351 	if (vport->phba->cfg_soft_wwpn)
352 		u64_to_wwn(vport->phba->cfg_soft_wwpn,
353 			   vport->fc_sparam.portName.u.wwn);
354 
355 	/*
356 	 * If the name is empty or there exists a soft name
357 	 * then copy the service params name, otherwise use the fc name
358 	 */
359 	if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
360 		memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
361 			sizeof(struct lpfc_name));
362 	else
363 		memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
364 			sizeof(struct lpfc_name));
365 
366 	/*
367 	 * If the port name has changed, then set the Param changes flag
368 	 * to unreg the login
369 	 */
370 	if (vport->fc_portname.u.wwn[0] != 0 &&
371 		memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
372 			sizeof(struct lpfc_name)))
373 		vport->vport_flag |= FAWWPN_PARAM_CHG;
374 
375 	if (vport->fc_portname.u.wwn[0] == 0 ||
376 	    vport->phba->cfg_soft_wwpn ||
377 	    (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
378 	    vport->vport_flag & FAWWPN_SET) {
379 		memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
380 			sizeof(struct lpfc_name));
381 		vport->vport_flag &= ~FAWWPN_SET;
382 		if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
383 			vport->vport_flag |= FAWWPN_SET;
384 	}
385 	else
386 		memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
387 			sizeof(struct lpfc_name));
388 }
389 
390 /**
391  * lpfc_config_port_post - Perform lpfc initialization after config port
392  * @phba: pointer to lpfc hba data structure.
393  *
394  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
395  * command call. It performs all internal resource and state setups on the
396  * port: post IOCB buffers, enable appropriate host interrupt attentions,
397  * ELS ring timers, etc.
398  *
399  * Return codes
400  *   0 - success.
401  *   Any other value - error.
402  **/
403 int
lpfc_config_port_post(struct lpfc_hba * phba)404 lpfc_config_port_post(struct lpfc_hba *phba)
405 {
406 	struct lpfc_vport *vport = phba->pport;
407 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
408 	LPFC_MBOXQ_t *pmb;
409 	MAILBOX_t *mb;
410 	struct lpfc_dmabuf *mp;
411 	struct lpfc_sli *psli = &phba->sli;
412 	uint32_t status, timeout;
413 	int i, j;
414 	int rc;
415 
416 	spin_lock_irq(&phba->hbalock);
417 	/*
418 	 * If the Config port completed correctly the HBA is not
419 	 * over heated any more.
420 	 */
421 	if (phba->over_temp_state == HBA_OVER_TEMP)
422 		phba->over_temp_state = HBA_NORMAL_TEMP;
423 	spin_unlock_irq(&phba->hbalock);
424 
425 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
426 	if (!pmb) {
427 		phba->link_state = LPFC_HBA_ERROR;
428 		return -ENOMEM;
429 	}
430 	mb = &pmb->u.mb;
431 
432 	/* Get login parameters for NID.  */
433 	rc = lpfc_read_sparam(phba, pmb, 0);
434 	if (rc) {
435 		mempool_free(pmb, phba->mbox_mem_pool);
436 		return -ENOMEM;
437 	}
438 
439 	pmb->vport = vport;
440 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
441 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
442 				"0448 Adapter failed init, mbxCmd x%x "
443 				"READ_SPARM mbxStatus x%x\n",
444 				mb->mbxCommand, mb->mbxStatus);
445 		phba->link_state = LPFC_HBA_ERROR;
446 		mp = (struct lpfc_dmabuf *) pmb->context1;
447 		mempool_free(pmb, phba->mbox_mem_pool);
448 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
449 		kfree(mp);
450 		return -EIO;
451 	}
452 
453 	mp = (struct lpfc_dmabuf *) pmb->context1;
454 
455 	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
456 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
457 	kfree(mp);
458 	pmb->context1 = NULL;
459 	lpfc_update_vport_wwn(vport);
460 
461 	/* Update the fc_host data structures with new wwn. */
462 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
463 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
464 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
465 
466 	/* If no serial number in VPD data, use low 6 bytes of WWNN */
467 	/* This should be consolidated into parse_vpd ? - mr */
468 	if (phba->SerialNumber[0] == 0) {
469 		uint8_t *outptr;
470 
471 		outptr = &vport->fc_nodename.u.s.IEEE[0];
472 		for (i = 0; i < 12; i++) {
473 			status = *outptr++;
474 			j = ((status & 0xf0) >> 4);
475 			if (j <= 9)
476 				phba->SerialNumber[i] =
477 				    (char)((uint8_t) 0x30 + (uint8_t) j);
478 			else
479 				phba->SerialNumber[i] =
480 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
481 			i++;
482 			j = (status & 0xf);
483 			if (j <= 9)
484 				phba->SerialNumber[i] =
485 				    (char)((uint8_t) 0x30 + (uint8_t) j);
486 			else
487 				phba->SerialNumber[i] =
488 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
489 		}
490 	}
491 
492 	lpfc_read_config(phba, pmb);
493 	pmb->vport = vport;
494 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
495 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
496 				"0453 Adapter failed to init, mbxCmd x%x "
497 				"READ_CONFIG, mbxStatus x%x\n",
498 				mb->mbxCommand, mb->mbxStatus);
499 		phba->link_state = LPFC_HBA_ERROR;
500 		mempool_free( pmb, phba->mbox_mem_pool);
501 		return -EIO;
502 	}
503 
504 	/* Check if the port is disabled */
505 	lpfc_sli_read_link_ste(phba);
506 
507 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
508 	i = (mb->un.varRdConfig.max_xri + 1);
509 	if (phba->cfg_hba_queue_depth > i) {
510 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
511 				"3359 HBA queue depth changed from %d to %d\n",
512 				phba->cfg_hba_queue_depth, i);
513 		phba->cfg_hba_queue_depth = i;
514 	}
515 
516 	/* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
517 	i = (mb->un.varRdConfig.max_xri >> 3);
518 	if (phba->pport->cfg_lun_queue_depth > i) {
519 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
520 				"3360 LUN queue depth changed from %d to %d\n",
521 				phba->pport->cfg_lun_queue_depth, i);
522 		phba->pport->cfg_lun_queue_depth = i;
523 	}
524 
525 	phba->lmt = mb->un.varRdConfig.lmt;
526 
527 	/* Get the default values for Model Name and Description */
528 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
529 
530 	phba->link_state = LPFC_LINK_DOWN;
531 
532 	/* Only process IOCBs on ELS ring till hba_state is READY */
533 	if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
534 		psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
535 	if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
536 		psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
537 
538 	/* Post receive buffers for desired rings */
539 	if (phba->sli_rev != 3)
540 		lpfc_post_rcv_buf(phba);
541 
542 	/*
543 	 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
544 	 */
545 	if (phba->intr_type == MSIX) {
546 		rc = lpfc_config_msi(phba, pmb);
547 		if (rc) {
548 			mempool_free(pmb, phba->mbox_mem_pool);
549 			return -EIO;
550 		}
551 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
552 		if (rc != MBX_SUCCESS) {
553 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
554 					"0352 Config MSI mailbox command "
555 					"failed, mbxCmd x%x, mbxStatus x%x\n",
556 					pmb->u.mb.mbxCommand,
557 					pmb->u.mb.mbxStatus);
558 			mempool_free(pmb, phba->mbox_mem_pool);
559 			return -EIO;
560 		}
561 	}
562 
563 	spin_lock_irq(&phba->hbalock);
564 	/* Initialize ERATT handling flag */
565 	phba->hba_flag &= ~HBA_ERATT_HANDLED;
566 
567 	/* Enable appropriate host interrupts */
568 	if (lpfc_readl(phba->HCregaddr, &status)) {
569 		spin_unlock_irq(&phba->hbalock);
570 		return -EIO;
571 	}
572 	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
573 	if (psli->num_rings > 0)
574 		status |= HC_R0INT_ENA;
575 	if (psli->num_rings > 1)
576 		status |= HC_R1INT_ENA;
577 	if (psli->num_rings > 2)
578 		status |= HC_R2INT_ENA;
579 	if (psli->num_rings > 3)
580 		status |= HC_R3INT_ENA;
581 
582 	if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
583 	    (phba->cfg_poll & DISABLE_FCP_RING_INT))
584 		status &= ~(HC_R0INT_ENA);
585 
586 	writel(status, phba->HCregaddr);
587 	readl(phba->HCregaddr); /* flush */
588 	spin_unlock_irq(&phba->hbalock);
589 
590 	/* Set up ring-0 (ELS) timer */
591 	timeout = phba->fc_ratov * 2;
592 	mod_timer(&vport->els_tmofunc,
593 		  jiffies + msecs_to_jiffies(1000 * timeout));
594 	/* Set up heart beat (HB) timer */
595 	mod_timer(&phba->hb_tmofunc,
596 		  jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
597 	phba->hb_outstanding = 0;
598 	phba->last_completion_time = jiffies;
599 	/* Set up error attention (ERATT) polling timer */
600 	mod_timer(&phba->eratt_poll,
601 		  jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
602 
603 	if (phba->hba_flag & LINK_DISABLED) {
604 		lpfc_printf_log(phba,
605 			KERN_ERR, LOG_INIT,
606 			"2598 Adapter Link is disabled.\n");
607 		lpfc_down_link(phba, pmb);
608 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
609 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
610 		if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
611 			lpfc_printf_log(phba,
612 			KERN_ERR, LOG_INIT,
613 			"2599 Adapter failed to issue DOWN_LINK"
614 			" mbox command rc 0x%x\n", rc);
615 
616 			mempool_free(pmb, phba->mbox_mem_pool);
617 			return -EIO;
618 		}
619 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
620 		mempool_free(pmb, phba->mbox_mem_pool);
621 		rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
622 		if (rc)
623 			return rc;
624 	}
625 	/* MBOX buffer will be freed in mbox compl */
626 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
627 	if (!pmb) {
628 		phba->link_state = LPFC_HBA_ERROR;
629 		return -ENOMEM;
630 	}
631 
632 	lpfc_config_async(phba, pmb, LPFC_ELS_RING);
633 	pmb->mbox_cmpl = lpfc_config_async_cmpl;
634 	pmb->vport = phba->pport;
635 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
636 
637 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
638 		lpfc_printf_log(phba,
639 				KERN_ERR,
640 				LOG_INIT,
641 				"0456 Adapter failed to issue "
642 				"ASYNCEVT_ENABLE mbox status x%x\n",
643 				rc);
644 		mempool_free(pmb, phba->mbox_mem_pool);
645 	}
646 
647 	/* Get Option rom version */
648 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
649 	if (!pmb) {
650 		phba->link_state = LPFC_HBA_ERROR;
651 		return -ENOMEM;
652 	}
653 
654 	lpfc_dump_wakeup_param(phba, pmb);
655 	pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
656 	pmb->vport = phba->pport;
657 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
658 
659 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
660 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
661 				"to get Option ROM version status x%x\n", rc);
662 		mempool_free(pmb, phba->mbox_mem_pool);
663 	}
664 
665 	return 0;
666 }
667 
668 /**
669  * lpfc_hba_init_link - Initialize the FC link
670  * @phba: pointer to lpfc hba data structure.
671  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
672  *
673  * This routine will issue the INIT_LINK mailbox command call.
674  * It is available to other drivers through the lpfc_hba data
675  * structure for use as a delayed link up mechanism with the
676  * module parameter lpfc_suppress_link_up.
677  *
678  * Return code
679  *		0 - success
680  *		Any other value - error
681  **/
682 static int
lpfc_hba_init_link(struct lpfc_hba * phba,uint32_t flag)683 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
684 {
685 	return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
686 }
687 
688 /**
689  * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
690  * @phba: pointer to lpfc hba data structure.
691  * @fc_topology: desired fc topology.
692  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
693  *
694  * This routine will issue the INIT_LINK mailbox command call.
695  * It is available to other drivers through the lpfc_hba data
696  * structure for use as a delayed link up mechanism with the
697  * module parameter lpfc_suppress_link_up.
698  *
699  * Return code
700  *              0 - success
701  *              Any other value - error
702  **/
703 int
lpfc_hba_init_link_fc_topology(struct lpfc_hba * phba,uint32_t fc_topology,uint32_t flag)704 lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
705 			       uint32_t flag)
706 {
707 	struct lpfc_vport *vport = phba->pport;
708 	LPFC_MBOXQ_t *pmb;
709 	MAILBOX_t *mb;
710 	int rc;
711 
712 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
713 	if (!pmb) {
714 		phba->link_state = LPFC_HBA_ERROR;
715 		return -ENOMEM;
716 	}
717 	mb = &pmb->u.mb;
718 	pmb->vport = vport;
719 
720 	if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
721 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
722 	     !(phba->lmt & LMT_1Gb)) ||
723 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
724 	     !(phba->lmt & LMT_2Gb)) ||
725 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
726 	     !(phba->lmt & LMT_4Gb)) ||
727 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
728 	     !(phba->lmt & LMT_8Gb)) ||
729 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
730 	     !(phba->lmt & LMT_10Gb)) ||
731 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
732 	     !(phba->lmt & LMT_16Gb)) ||
733 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
734 	     !(phba->lmt & LMT_32Gb))) {
735 		/* Reset link speed to auto */
736 		lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
737 			"1302 Invalid speed for this board:%d "
738 			"Reset link speed to auto.\n",
739 			phba->cfg_link_speed);
740 			phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
741 	}
742 	lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
743 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
744 	if (phba->sli_rev < LPFC_SLI_REV4)
745 		lpfc_set_loopback_flag(phba);
746 	rc = lpfc_sli_issue_mbox(phba, pmb, flag);
747 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
748 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
749 			"0498 Adapter failed to init, mbxCmd x%x "
750 			"INIT_LINK, mbxStatus x%x\n",
751 			mb->mbxCommand, mb->mbxStatus);
752 		if (phba->sli_rev <= LPFC_SLI_REV3) {
753 			/* Clear all interrupt enable conditions */
754 			writel(0, phba->HCregaddr);
755 			readl(phba->HCregaddr); /* flush */
756 			/* Clear all pending interrupts */
757 			writel(0xffffffff, phba->HAregaddr);
758 			readl(phba->HAregaddr); /* flush */
759 		}
760 		phba->link_state = LPFC_HBA_ERROR;
761 		if (rc != MBX_BUSY || flag == MBX_POLL)
762 			mempool_free(pmb, phba->mbox_mem_pool);
763 		return -EIO;
764 	}
765 	phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
766 	if (flag == MBX_POLL)
767 		mempool_free(pmb, phba->mbox_mem_pool);
768 
769 	return 0;
770 }
771 
772 /**
773  * lpfc_hba_down_link - this routine downs the FC link
774  * @phba: pointer to lpfc hba data structure.
775  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
776  *
777  * This routine will issue the DOWN_LINK mailbox command call.
778  * It is available to other drivers through the lpfc_hba data
779  * structure for use to stop the link.
780  *
781  * Return code
782  *		0 - success
783  *		Any other value - error
784  **/
785 static int
lpfc_hba_down_link(struct lpfc_hba * phba,uint32_t flag)786 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
787 {
788 	LPFC_MBOXQ_t *pmb;
789 	int rc;
790 
791 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
792 	if (!pmb) {
793 		phba->link_state = LPFC_HBA_ERROR;
794 		return -ENOMEM;
795 	}
796 
797 	lpfc_printf_log(phba,
798 		KERN_ERR, LOG_INIT,
799 		"0491 Adapter Link is disabled.\n");
800 	lpfc_down_link(phba, pmb);
801 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
802 	rc = lpfc_sli_issue_mbox(phba, pmb, flag);
803 	if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
804 		lpfc_printf_log(phba,
805 		KERN_ERR, LOG_INIT,
806 		"2522 Adapter failed to issue DOWN_LINK"
807 		" mbox command rc 0x%x\n", rc);
808 
809 		mempool_free(pmb, phba->mbox_mem_pool);
810 		return -EIO;
811 	}
812 	if (flag == MBX_POLL)
813 		mempool_free(pmb, phba->mbox_mem_pool);
814 
815 	return 0;
816 }
817 
818 /**
819  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
820  * @phba: pointer to lpfc HBA data structure.
821  *
822  * This routine will do LPFC uninitialization before the HBA is reset when
823  * bringing down the SLI Layer.
824  *
825  * Return codes
826  *   0 - success.
827  *   Any other value - error.
828  **/
829 int
lpfc_hba_down_prep(struct lpfc_hba * phba)830 lpfc_hba_down_prep(struct lpfc_hba *phba)
831 {
832 	struct lpfc_vport **vports;
833 	int i;
834 
835 	if (phba->sli_rev <= LPFC_SLI_REV3) {
836 		/* Disable interrupts */
837 		writel(0, phba->HCregaddr);
838 		readl(phba->HCregaddr); /* flush */
839 	}
840 
841 	if (phba->pport->load_flag & FC_UNLOADING)
842 		lpfc_cleanup_discovery_resources(phba->pport);
843 	else {
844 		vports = lpfc_create_vport_work_array(phba);
845 		if (vports != NULL)
846 			for (i = 0; i <= phba->max_vports &&
847 				vports[i] != NULL; i++)
848 				lpfc_cleanup_discovery_resources(vports[i]);
849 		lpfc_destroy_vport_work_array(phba, vports);
850 	}
851 	return 0;
852 }
853 
854 /**
855  * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
856  * rspiocb which got deferred
857  *
858  * @phba: pointer to lpfc HBA data structure.
859  *
860  * This routine will cleanup completed slow path events after HBA is reset
861  * when bringing down the SLI Layer.
862  *
863  *
864  * Return codes
865  *   void.
866  **/
867 static void
lpfc_sli4_free_sp_events(struct lpfc_hba * phba)868 lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
869 {
870 	struct lpfc_iocbq *rspiocbq;
871 	struct hbq_dmabuf *dmabuf;
872 	struct lpfc_cq_event *cq_event;
873 
874 	spin_lock_irq(&phba->hbalock);
875 	phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
876 	spin_unlock_irq(&phba->hbalock);
877 
878 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
879 		/* Get the response iocb from the head of work queue */
880 		spin_lock_irq(&phba->hbalock);
881 		list_remove_head(&phba->sli4_hba.sp_queue_event,
882 				 cq_event, struct lpfc_cq_event, list);
883 		spin_unlock_irq(&phba->hbalock);
884 
885 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
886 		case CQE_CODE_COMPL_WQE:
887 			rspiocbq = container_of(cq_event, struct lpfc_iocbq,
888 						 cq_event);
889 			lpfc_sli_release_iocbq(phba, rspiocbq);
890 			break;
891 		case CQE_CODE_RECEIVE:
892 		case CQE_CODE_RECEIVE_V1:
893 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
894 					      cq_event);
895 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
896 		}
897 	}
898 }
899 
900 /**
901  * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
902  * @phba: pointer to lpfc HBA data structure.
903  *
904  * This routine will cleanup posted ELS buffers after the HBA is reset
905  * when bringing down the SLI Layer.
906  *
907  *
908  * Return codes
909  *   void.
910  **/
911 static void
lpfc_hba_free_post_buf(struct lpfc_hba * phba)912 lpfc_hba_free_post_buf(struct lpfc_hba *phba)
913 {
914 	struct lpfc_sli *psli = &phba->sli;
915 	struct lpfc_sli_ring *pring;
916 	struct lpfc_dmabuf *mp, *next_mp;
917 	LIST_HEAD(buflist);
918 	int count;
919 
920 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
921 		lpfc_sli_hbqbuf_free_all(phba);
922 	else {
923 		/* Cleanup preposted buffers on the ELS ring */
924 		pring = &psli->sli3_ring[LPFC_ELS_RING];
925 		spin_lock_irq(&phba->hbalock);
926 		list_splice_init(&pring->postbufq, &buflist);
927 		spin_unlock_irq(&phba->hbalock);
928 
929 		count = 0;
930 		list_for_each_entry_safe(mp, next_mp, &buflist, list) {
931 			list_del(&mp->list);
932 			count++;
933 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
934 			kfree(mp);
935 		}
936 
937 		spin_lock_irq(&phba->hbalock);
938 		pring->postbufq_cnt -= count;
939 		spin_unlock_irq(&phba->hbalock);
940 	}
941 }
942 
943 /**
944  * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
945  * @phba: pointer to lpfc HBA data structure.
946  *
947  * This routine will cleanup the txcmplq after the HBA is reset when bringing
948  * down the SLI Layer.
949  *
950  * Return codes
951  *   void
952  **/
953 static void
lpfc_hba_clean_txcmplq(struct lpfc_hba * phba)954 lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
955 {
956 	struct lpfc_sli *psli = &phba->sli;
957 	struct lpfc_queue *qp = NULL;
958 	struct lpfc_sli_ring *pring;
959 	LIST_HEAD(completions);
960 	int i;
961 
962 	if (phba->sli_rev != LPFC_SLI_REV4) {
963 		for (i = 0; i < psli->num_rings; i++) {
964 			pring = &psli->sli3_ring[i];
965 			spin_lock_irq(&phba->hbalock);
966 			/* At this point in time the HBA is either reset or DOA
967 			 * Nothing should be on txcmplq as it will
968 			 * NEVER complete.
969 			 */
970 			list_splice_init(&pring->txcmplq, &completions);
971 			pring->txcmplq_cnt = 0;
972 			spin_unlock_irq(&phba->hbalock);
973 
974 			lpfc_sli_abort_iocb_ring(phba, pring);
975 		}
976 		/* Cancel all the IOCBs from the completions list */
977 		lpfc_sli_cancel_iocbs(phba, &completions,
978 				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
979 		return;
980 	}
981 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
982 		pring = qp->pring;
983 		if (!pring)
984 			continue;
985 		spin_lock_irq(&pring->ring_lock);
986 		list_splice_init(&pring->txcmplq, &completions);
987 		pring->txcmplq_cnt = 0;
988 		spin_unlock_irq(&pring->ring_lock);
989 		lpfc_sli_abort_iocb_ring(phba, pring);
990 	}
991 	/* Cancel all the IOCBs from the completions list */
992 	lpfc_sli_cancel_iocbs(phba, &completions,
993 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
994 }
995 
996 /**
997  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
998 	int i;
999  * @phba: pointer to lpfc HBA data structure.
1000  *
1001  * This routine will do uninitialization after the HBA is reset when bring
1002  * down the SLI Layer.
1003  *
1004  * Return codes
1005  *   0 - success.
1006  *   Any other value - error.
1007  **/
1008 static int
lpfc_hba_down_post_s3(struct lpfc_hba * phba)1009 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
1010 {
1011 	lpfc_hba_free_post_buf(phba);
1012 	lpfc_hba_clean_txcmplq(phba);
1013 	return 0;
1014 }
1015 
1016 /**
1017  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1018  * @phba: pointer to lpfc HBA data structure.
1019  *
1020  * This routine will do uninitialization after the HBA is reset when bring
1021  * down the SLI Layer.
1022  *
1023  * Return codes
1024  *   0 - success.
1025  *   Any other value - error.
1026  **/
1027 static int
lpfc_hba_down_post_s4(struct lpfc_hba * phba)1028 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1029 {
1030 	struct lpfc_scsi_buf *psb, *psb_next;
1031 	struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
1032 	LIST_HEAD(aborts);
1033 	LIST_HEAD(nvme_aborts);
1034 	LIST_HEAD(nvmet_aborts);
1035 	unsigned long iflag = 0;
1036 	struct lpfc_sglq *sglq_entry = NULL;
1037 
1038 
1039 	lpfc_sli_hbqbuf_free_all(phba);
1040 	lpfc_hba_clean_txcmplq(phba);
1041 
1042 	/* At this point in time the HBA is either reset or DOA. Either
1043 	 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
1044 	 * on the lpfc_els_sgl_list so that it can either be freed if the
1045 	 * driver is unloading or reposted if the driver is restarting
1046 	 * the port.
1047 	 */
1048 	spin_lock_irq(&phba->hbalock);  /* required for lpfc_els_sgl_list and */
1049 					/* scsl_buf_list */
1050 	/* sgl_list_lock required because worker thread uses this
1051 	 * list.
1052 	 */
1053 	spin_lock(&phba->sli4_hba.sgl_list_lock);
1054 	list_for_each_entry(sglq_entry,
1055 		&phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1056 		sglq_entry->state = SGL_FREED;
1057 
1058 	list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1059 			&phba->sli4_hba.lpfc_els_sgl_list);
1060 
1061 
1062 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
1063 	/* abts_scsi_buf_list_lock required because worker thread uses this
1064 	 * list.
1065 	 */
1066 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1067 		spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1068 		list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1069 				 &aborts);
1070 		spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1071 	}
1072 
1073 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1074 		spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1075 		list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1076 				 &nvme_aborts);
1077 		list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1078 				 &nvmet_aborts);
1079 		spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1080 	}
1081 
1082 	spin_unlock_irq(&phba->hbalock);
1083 
1084 	list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1085 		psb->pCmd = NULL;
1086 		psb->status = IOSTAT_SUCCESS;
1087 	}
1088 	spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1089 	list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1090 	spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1091 
1092 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1093 		list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1094 			psb->pCmd = NULL;
1095 			psb->status = IOSTAT_SUCCESS;
1096 		}
1097 		spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1098 		list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1099 		spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1100 
1101 		list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1102 			ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
1103 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
1104 		}
1105 	}
1106 
1107 	lpfc_sli4_free_sp_events(phba);
1108 	return 0;
1109 }
1110 
1111 /**
1112  * lpfc_hba_down_post - Wrapper func for hba down post routine
1113  * @phba: pointer to lpfc HBA data structure.
1114  *
1115  * This routine wraps the actual SLI3 or SLI4 routine for performing
1116  * uninitialization after the HBA is reset when bring down the SLI Layer.
1117  *
1118  * Return codes
1119  *   0 - success.
1120  *   Any other value - error.
1121  **/
1122 int
lpfc_hba_down_post(struct lpfc_hba * phba)1123 lpfc_hba_down_post(struct lpfc_hba *phba)
1124 {
1125 	return (*phba->lpfc_hba_down_post)(phba);
1126 }
1127 
1128 /**
1129  * lpfc_hb_timeout - The HBA-timer timeout handler
1130  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1131  *
1132  * This is the HBA-timer timeout handler registered to the lpfc driver. When
1133  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1134  * work-port-events bitmap and the worker thread is notified. This timeout
1135  * event will be used by the worker thread to invoke the actual timeout
1136  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1137  * be performed in the timeout handler and the HBA timeout event bit shall
1138  * be cleared by the worker thread after it has taken the event bitmap out.
1139  **/
1140 static void
lpfc_hb_timeout(unsigned long ptr)1141 lpfc_hb_timeout(unsigned long ptr)
1142 {
1143 	struct lpfc_hba *phba;
1144 	uint32_t tmo_posted;
1145 	unsigned long iflag;
1146 
1147 	phba = (struct lpfc_hba *)ptr;
1148 
1149 	/* Check for heart beat timeout conditions */
1150 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1151 	tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1152 	if (!tmo_posted)
1153 		phba->pport->work_port_events |= WORKER_HB_TMO;
1154 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1155 
1156 	/* Tell the worker thread there is work to do */
1157 	if (!tmo_posted)
1158 		lpfc_worker_wake_up(phba);
1159 	return;
1160 }
1161 
1162 /**
1163  * lpfc_rrq_timeout - The RRQ-timer timeout handler
1164  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1165  *
1166  * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1167  * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1168  * work-port-events bitmap and the worker thread is notified. This timeout
1169  * event will be used by the worker thread to invoke the actual timeout
1170  * handler routine, lpfc_rrq_handler. Any periodical operations will
1171  * be performed in the timeout handler and the RRQ timeout event bit shall
1172  * be cleared by the worker thread after it has taken the event bitmap out.
1173  **/
1174 static void
lpfc_rrq_timeout(unsigned long ptr)1175 lpfc_rrq_timeout(unsigned long ptr)
1176 {
1177 	struct lpfc_hba *phba;
1178 	unsigned long iflag;
1179 
1180 	phba = (struct lpfc_hba *)ptr;
1181 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1182 	if (!(phba->pport->load_flag & FC_UNLOADING))
1183 		phba->hba_flag |= HBA_RRQ_ACTIVE;
1184 	else
1185 		phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1186 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1187 
1188 	if (!(phba->pport->load_flag & FC_UNLOADING))
1189 		lpfc_worker_wake_up(phba);
1190 }
1191 
1192 /**
1193  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
1194  * @phba: pointer to lpfc hba data structure.
1195  * @pmboxq: pointer to the driver internal queue element for mailbox command.
1196  *
1197  * This is the callback function to the lpfc heart-beat mailbox command.
1198  * If configured, the lpfc driver issues the heart-beat mailbox command to
1199  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1200  * heart-beat mailbox command is issued, the driver shall set up heart-beat
1201  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1202  * heart-beat outstanding state. Once the mailbox command comes back and
1203  * no error conditions detected, the heart-beat mailbox command timer is
1204  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1205  * state is cleared for the next heart-beat. If the timer expired with the
1206  * heart-beat outstanding state set, the driver will put the HBA offline.
1207  **/
1208 static void
lpfc_hb_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)1209 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1210 {
1211 	unsigned long drvr_flag;
1212 
1213 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
1214 	phba->hb_outstanding = 0;
1215 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1216 
1217 	/* Check and reset heart-beat timer is necessary */
1218 	mempool_free(pmboxq, phba->mbox_mem_pool);
1219 	if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1220 		!(phba->link_state == LPFC_HBA_ERROR) &&
1221 		!(phba->pport->load_flag & FC_UNLOADING))
1222 		mod_timer(&phba->hb_tmofunc,
1223 			  jiffies +
1224 			  msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1225 	return;
1226 }
1227 
1228 /**
1229  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1230  * @phba: pointer to lpfc hba data structure.
1231  *
1232  * This is the actual HBA-timer timeout handler to be invoked by the worker
1233  * thread whenever the HBA timer fired and HBA-timeout event posted. This
1234  * handler performs any periodic operations needed for the device. If such
1235  * periodic event has already been attended to either in the interrupt handler
1236  * or by processing slow-ring or fast-ring events within the HBA-timer
1237  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1238  * the timer for the next timeout period. If lpfc heart-beat mailbox command
1239  * is configured and there is no heart-beat mailbox command outstanding, a
1240  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1241  * has been a heart-beat mailbox command outstanding, the HBA shall be put
1242  * to offline.
1243  **/
1244 void
lpfc_hb_timeout_handler(struct lpfc_hba * phba)1245 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1246 {
1247 	struct lpfc_vport **vports;
1248 	LPFC_MBOXQ_t *pmboxq;
1249 	struct lpfc_dmabuf *buf_ptr;
1250 	int retval, i;
1251 	struct lpfc_sli *psli = &phba->sli;
1252 	LIST_HEAD(completions);
1253 	struct lpfc_queue *qp;
1254 	unsigned long time_elapsed;
1255 	uint32_t tick_cqe, max_cqe, val;
1256 	uint64_t tot, data1, data2, data3;
1257 	struct lpfc_nvmet_tgtport *tgtp;
1258 	struct lpfc_register reg_data;
1259 	void __iomem *eqdreg = phba->sli4_hba.u.if_type2.EQDregaddr;
1260 
1261 	vports = lpfc_create_vport_work_array(phba);
1262 	if (vports != NULL)
1263 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1264 			lpfc_rcv_seq_check_edtov(vports[i]);
1265 			lpfc_fdmi_num_disc_check(vports[i]);
1266 		}
1267 	lpfc_destroy_vport_work_array(phba, vports);
1268 
1269 	if ((phba->link_state == LPFC_HBA_ERROR) ||
1270 		(phba->pport->load_flag & FC_UNLOADING) ||
1271 		(phba->pport->fc_flag & FC_OFFLINE_MODE))
1272 		return;
1273 
1274 	if (phba->cfg_auto_imax) {
1275 		if (!phba->last_eqdelay_time) {
1276 			phba->last_eqdelay_time = jiffies;
1277 			goto skip_eqdelay;
1278 		}
1279 		time_elapsed = jiffies - phba->last_eqdelay_time;
1280 		phba->last_eqdelay_time = jiffies;
1281 
1282 		tot = 0xffff;
1283 		/* Check outstanding IO count */
1284 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1285 			if (phba->nvmet_support) {
1286 				tgtp = phba->targetport->private;
1287 				/* Calculate outstanding IOs */
1288 				tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1289 				tot += atomic_read(&tgtp->xmt_fcp_release);
1290 				tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1291 			} else {
1292 				tot = atomic_read(&phba->fc4NvmeIoCmpls);
1293 				data1 = atomic_read(
1294 					&phba->fc4NvmeInputRequests);
1295 				data2 = atomic_read(
1296 					&phba->fc4NvmeOutputRequests);
1297 				data3 = atomic_read(
1298 					&phba->fc4NvmeControlRequests);
1299 				tot =  (data1 + data2 + data3) - tot;
1300 			}
1301 		}
1302 
1303 		/* Interrupts per sec per EQ */
1304 		val = phba->cfg_fcp_imax / phba->io_channel_irqs;
1305 		tick_cqe = val / CONFIG_HZ; /* Per tick per EQ */
1306 
1307 		/* Assume 1 CQE/ISR, calc max CQEs allowed for time duration */
1308 		max_cqe = time_elapsed * tick_cqe;
1309 
1310 		for (i = 0; i < phba->io_channel_irqs; i++) {
1311 			/* Fast-path EQ */
1312 			qp = phba->sli4_hba.hba_eq[i];
1313 			if (!qp)
1314 				continue;
1315 
1316 			/* Use no EQ delay if we don't have many outstanding
1317 			 * IOs, or if we are only processing 1 CQE/ISR or less.
1318 			 * Otherwise, assume we can process up to lpfc_fcp_imax
1319 			 * interrupts per HBA.
1320 			 */
1321 			if (tot < LPFC_NODELAY_MAX_IO ||
1322 			    qp->EQ_cqe_cnt <= max_cqe)
1323 				val = 0;
1324 			else
1325 				val = phba->cfg_fcp_imax;
1326 
1327 			if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
1328 				/* Use EQ Delay Register method */
1329 
1330 				/* Convert for EQ Delay register */
1331 				if (val) {
1332 					/* First, interrupts per sec per EQ */
1333 					val = phba->cfg_fcp_imax /
1334 						phba->io_channel_irqs;
1335 
1336 					/* us delay between each interrupt */
1337 					val = LPFC_SEC_TO_USEC / val;
1338 				}
1339 				if (val != qp->q_mode) {
1340 					reg_data.word0 = 0;
1341 					bf_set(lpfc_sliport_eqdelay_id,
1342 					       &reg_data, qp->queue_id);
1343 					bf_set(lpfc_sliport_eqdelay_delay,
1344 					       &reg_data, val);
1345 					writel(reg_data.word0, eqdreg);
1346 				}
1347 			} else {
1348 				/* Use mbox command method */
1349 				if (val != qp->q_mode)
1350 					lpfc_modify_hba_eq_delay(phba, i,
1351 								 1, val);
1352 			}
1353 
1354 			/*
1355 			 * val is cfg_fcp_imax or 0 for mbox delay or us delay
1356 			 * between interrupts for EQDR.
1357 			 */
1358 			qp->q_mode = val;
1359 			qp->EQ_cqe_cnt = 0;
1360 		}
1361 	}
1362 
1363 skip_eqdelay:
1364 	spin_lock_irq(&phba->pport->work_port_lock);
1365 
1366 	if (time_after(phba->last_completion_time +
1367 			msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1368 			jiffies)) {
1369 		spin_unlock_irq(&phba->pport->work_port_lock);
1370 		if (!phba->hb_outstanding)
1371 			mod_timer(&phba->hb_tmofunc,
1372 				jiffies +
1373 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1374 		else
1375 			mod_timer(&phba->hb_tmofunc,
1376 				jiffies +
1377 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1378 		return;
1379 	}
1380 	spin_unlock_irq(&phba->pport->work_port_lock);
1381 
1382 	if (phba->elsbuf_cnt &&
1383 		(phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1384 		spin_lock_irq(&phba->hbalock);
1385 		list_splice_init(&phba->elsbuf, &completions);
1386 		phba->elsbuf_cnt = 0;
1387 		phba->elsbuf_prev_cnt = 0;
1388 		spin_unlock_irq(&phba->hbalock);
1389 
1390 		while (!list_empty(&completions)) {
1391 			list_remove_head(&completions, buf_ptr,
1392 				struct lpfc_dmabuf, list);
1393 			lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1394 			kfree(buf_ptr);
1395 		}
1396 	}
1397 	phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1398 
1399 	/* If there is no heart beat outstanding, issue a heartbeat command */
1400 	if (phba->cfg_enable_hba_heartbeat) {
1401 		if (!phba->hb_outstanding) {
1402 			if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1403 				(list_empty(&psli->mboxq))) {
1404 				pmboxq = mempool_alloc(phba->mbox_mem_pool,
1405 							GFP_KERNEL);
1406 				if (!pmboxq) {
1407 					mod_timer(&phba->hb_tmofunc,
1408 						 jiffies +
1409 						 msecs_to_jiffies(1000 *
1410 						 LPFC_HB_MBOX_INTERVAL));
1411 					return;
1412 				}
1413 
1414 				lpfc_heart_beat(phba, pmboxq);
1415 				pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1416 				pmboxq->vport = phba->pport;
1417 				retval = lpfc_sli_issue_mbox(phba, pmboxq,
1418 						MBX_NOWAIT);
1419 
1420 				if (retval != MBX_BUSY &&
1421 					retval != MBX_SUCCESS) {
1422 					mempool_free(pmboxq,
1423 							phba->mbox_mem_pool);
1424 					mod_timer(&phba->hb_tmofunc,
1425 						jiffies +
1426 						msecs_to_jiffies(1000 *
1427 						LPFC_HB_MBOX_INTERVAL));
1428 					return;
1429 				}
1430 				phba->skipped_hb = 0;
1431 				phba->hb_outstanding = 1;
1432 			} else if (time_before_eq(phba->last_completion_time,
1433 					phba->skipped_hb)) {
1434 				lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1435 					"2857 Last completion time not "
1436 					" updated in %d ms\n",
1437 					jiffies_to_msecs(jiffies
1438 						 - phba->last_completion_time));
1439 			} else
1440 				phba->skipped_hb = jiffies;
1441 
1442 			mod_timer(&phba->hb_tmofunc,
1443 				 jiffies +
1444 				 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1445 			return;
1446 		} else {
1447 			/*
1448 			* If heart beat timeout called with hb_outstanding set
1449 			* we need to give the hb mailbox cmd a chance to
1450 			* complete or TMO.
1451 			*/
1452 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1453 					"0459 Adapter heartbeat still out"
1454 					"standing:last compl time was %d ms.\n",
1455 					jiffies_to_msecs(jiffies
1456 						 - phba->last_completion_time));
1457 			mod_timer(&phba->hb_tmofunc,
1458 				jiffies +
1459 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1460 		}
1461 	} else {
1462 			mod_timer(&phba->hb_tmofunc,
1463 				jiffies +
1464 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1465 	}
1466 }
1467 
1468 /**
1469  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1470  * @phba: pointer to lpfc hba data structure.
1471  *
1472  * This routine is called to bring the HBA offline when HBA hardware error
1473  * other than Port Error 6 has been detected.
1474  **/
1475 static void
lpfc_offline_eratt(struct lpfc_hba * phba)1476 lpfc_offline_eratt(struct lpfc_hba *phba)
1477 {
1478 	struct lpfc_sli   *psli = &phba->sli;
1479 
1480 	spin_lock_irq(&phba->hbalock);
1481 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1482 	spin_unlock_irq(&phba->hbalock);
1483 	lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1484 
1485 	lpfc_offline(phba);
1486 	lpfc_reset_barrier(phba);
1487 	spin_lock_irq(&phba->hbalock);
1488 	lpfc_sli_brdreset(phba);
1489 	spin_unlock_irq(&phba->hbalock);
1490 	lpfc_hba_down_post(phba);
1491 	lpfc_sli_brdready(phba, HS_MBRDY);
1492 	lpfc_unblock_mgmt_io(phba);
1493 	phba->link_state = LPFC_HBA_ERROR;
1494 	return;
1495 }
1496 
1497 /**
1498  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1499  * @phba: pointer to lpfc hba data structure.
1500  *
1501  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1502  * other than Port Error 6 has been detected.
1503  **/
1504 void
lpfc_sli4_offline_eratt(struct lpfc_hba * phba)1505 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1506 {
1507 	spin_lock_irq(&phba->hbalock);
1508 	phba->link_state = LPFC_HBA_ERROR;
1509 	spin_unlock_irq(&phba->hbalock);
1510 
1511 	lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1512 	lpfc_offline(phba);
1513 	lpfc_hba_down_post(phba);
1514 	lpfc_unblock_mgmt_io(phba);
1515 }
1516 
1517 /**
1518  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1519  * @phba: pointer to lpfc hba data structure.
1520  *
1521  * This routine is invoked to handle the deferred HBA hardware error
1522  * conditions. This type of error is indicated by HBA by setting ER1
1523  * and another ER bit in the host status register. The driver will
1524  * wait until the ER1 bit clears before handling the error condition.
1525  **/
1526 static void
lpfc_handle_deferred_eratt(struct lpfc_hba * phba)1527 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1528 {
1529 	uint32_t old_host_status = phba->work_hs;
1530 	struct lpfc_sli *psli = &phba->sli;
1531 
1532 	/* If the pci channel is offline, ignore possible errors,
1533 	 * since we cannot communicate with the pci card anyway.
1534 	 */
1535 	if (pci_channel_offline(phba->pcidev)) {
1536 		spin_lock_irq(&phba->hbalock);
1537 		phba->hba_flag &= ~DEFER_ERATT;
1538 		spin_unlock_irq(&phba->hbalock);
1539 		return;
1540 	}
1541 
1542 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1543 		"0479 Deferred Adapter Hardware Error "
1544 		"Data: x%x x%x x%x\n",
1545 		phba->work_hs,
1546 		phba->work_status[0], phba->work_status[1]);
1547 
1548 	spin_lock_irq(&phba->hbalock);
1549 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1550 	spin_unlock_irq(&phba->hbalock);
1551 
1552 
1553 	/*
1554 	 * Firmware stops when it triggred erratt. That could cause the I/Os
1555 	 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1556 	 * SCSI layer retry it after re-establishing link.
1557 	 */
1558 	lpfc_sli_abort_fcp_rings(phba);
1559 
1560 	/*
1561 	 * There was a firmware error. Take the hba offline and then
1562 	 * attempt to restart it.
1563 	 */
1564 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
1565 	lpfc_offline(phba);
1566 
1567 	/* Wait for the ER1 bit to clear.*/
1568 	while (phba->work_hs & HS_FFER1) {
1569 		msleep(100);
1570 		if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1571 			phba->work_hs = UNPLUG_ERR ;
1572 			break;
1573 		}
1574 		/* If driver is unloading let the worker thread continue */
1575 		if (phba->pport->load_flag & FC_UNLOADING) {
1576 			phba->work_hs = 0;
1577 			break;
1578 		}
1579 	}
1580 
1581 	/*
1582 	 * This is to ptrotect against a race condition in which
1583 	 * first write to the host attention register clear the
1584 	 * host status register.
1585 	 */
1586 	if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1587 		phba->work_hs = old_host_status & ~HS_FFER1;
1588 
1589 	spin_lock_irq(&phba->hbalock);
1590 	phba->hba_flag &= ~DEFER_ERATT;
1591 	spin_unlock_irq(&phba->hbalock);
1592 	phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1593 	phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1594 }
1595 
1596 static void
lpfc_board_errevt_to_mgmt(struct lpfc_hba * phba)1597 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1598 {
1599 	struct lpfc_board_event_header board_event;
1600 	struct Scsi_Host *shost;
1601 
1602 	board_event.event_type = FC_REG_BOARD_EVENT;
1603 	board_event.subcategory = LPFC_EVENT_PORTINTERR;
1604 	shost = lpfc_shost_from_vport(phba->pport);
1605 	fc_host_post_vendor_event(shost, fc_get_event_number(),
1606 				  sizeof(board_event),
1607 				  (char *) &board_event,
1608 				  LPFC_NL_VENDOR_ID);
1609 }
1610 
1611 /**
1612  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1613  * @phba: pointer to lpfc hba data structure.
1614  *
1615  * This routine is invoked to handle the following HBA hardware error
1616  * conditions:
1617  * 1 - HBA error attention interrupt
1618  * 2 - DMA ring index out of range
1619  * 3 - Mailbox command came back as unknown
1620  **/
1621 static void
lpfc_handle_eratt_s3(struct lpfc_hba * phba)1622 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1623 {
1624 	struct lpfc_vport *vport = phba->pport;
1625 	struct lpfc_sli   *psli = &phba->sli;
1626 	uint32_t event_data;
1627 	unsigned long temperature;
1628 	struct temp_event temp_event_data;
1629 	struct Scsi_Host  *shost;
1630 
1631 	/* If the pci channel is offline, ignore possible errors,
1632 	 * since we cannot communicate with the pci card anyway.
1633 	 */
1634 	if (pci_channel_offline(phba->pcidev)) {
1635 		spin_lock_irq(&phba->hbalock);
1636 		phba->hba_flag &= ~DEFER_ERATT;
1637 		spin_unlock_irq(&phba->hbalock);
1638 		return;
1639 	}
1640 
1641 	/* If resets are disabled then leave the HBA alone and return */
1642 	if (!phba->cfg_enable_hba_reset)
1643 		return;
1644 
1645 	/* Send an internal error event to mgmt application */
1646 	lpfc_board_errevt_to_mgmt(phba);
1647 
1648 	if (phba->hba_flag & DEFER_ERATT)
1649 		lpfc_handle_deferred_eratt(phba);
1650 
1651 	if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1652 		if (phba->work_hs & HS_FFER6)
1653 			/* Re-establishing Link */
1654 			lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1655 					"1301 Re-establishing Link "
1656 					"Data: x%x x%x x%x\n",
1657 					phba->work_hs, phba->work_status[0],
1658 					phba->work_status[1]);
1659 		if (phba->work_hs & HS_FFER8)
1660 			/* Device Zeroization */
1661 			lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1662 					"2861 Host Authentication device "
1663 					"zeroization Data:x%x x%x x%x\n",
1664 					phba->work_hs, phba->work_status[0],
1665 					phba->work_status[1]);
1666 
1667 		spin_lock_irq(&phba->hbalock);
1668 		psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1669 		spin_unlock_irq(&phba->hbalock);
1670 
1671 		/*
1672 		* Firmware stops when it triggled erratt with HS_FFER6.
1673 		* That could cause the I/Os dropped by the firmware.
1674 		* Error iocb (I/O) on txcmplq and let the SCSI layer
1675 		* retry it after re-establishing link.
1676 		*/
1677 		lpfc_sli_abort_fcp_rings(phba);
1678 
1679 		/*
1680 		 * There was a firmware error.  Take the hba offline and then
1681 		 * attempt to restart it.
1682 		 */
1683 		lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1684 		lpfc_offline(phba);
1685 		lpfc_sli_brdrestart(phba);
1686 		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
1687 			lpfc_unblock_mgmt_io(phba);
1688 			return;
1689 		}
1690 		lpfc_unblock_mgmt_io(phba);
1691 	} else if (phba->work_hs & HS_CRIT_TEMP) {
1692 		temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1693 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1694 		temp_event_data.event_code = LPFC_CRIT_TEMP;
1695 		temp_event_data.data = (uint32_t)temperature;
1696 
1697 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1698 				"0406 Adapter maximum temperature exceeded "
1699 				"(%ld), taking this port offline "
1700 				"Data: x%x x%x x%x\n",
1701 				temperature, phba->work_hs,
1702 				phba->work_status[0], phba->work_status[1]);
1703 
1704 		shost = lpfc_shost_from_vport(phba->pport);
1705 		fc_host_post_vendor_event(shost, fc_get_event_number(),
1706 					  sizeof(temp_event_data),
1707 					  (char *) &temp_event_data,
1708 					  SCSI_NL_VID_TYPE_PCI
1709 					  | PCI_VENDOR_ID_EMULEX);
1710 
1711 		spin_lock_irq(&phba->hbalock);
1712 		phba->over_temp_state = HBA_OVER_TEMP;
1713 		spin_unlock_irq(&phba->hbalock);
1714 		lpfc_offline_eratt(phba);
1715 
1716 	} else {
1717 		/* The if clause above forces this code path when the status
1718 		 * failure is a value other than FFER6. Do not call the offline
1719 		 * twice. This is the adapter hardware error path.
1720 		 */
1721 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1722 				"0457 Adapter Hardware Error "
1723 				"Data: x%x x%x x%x\n",
1724 				phba->work_hs,
1725 				phba->work_status[0], phba->work_status[1]);
1726 
1727 		event_data = FC_REG_DUMP_EVENT;
1728 		shost = lpfc_shost_from_vport(vport);
1729 		fc_host_post_vendor_event(shost, fc_get_event_number(),
1730 				sizeof(event_data), (char *) &event_data,
1731 				SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1732 
1733 		lpfc_offline_eratt(phba);
1734 	}
1735 	return;
1736 }
1737 
1738 /**
1739  * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1740  * @phba: pointer to lpfc hba data structure.
1741  * @mbx_action: flag for mailbox shutdown action.
1742  *
1743  * This routine is invoked to perform an SLI4 port PCI function reset in
1744  * response to port status register polling attention. It waits for port
1745  * status register (ERR, RDY, RN) bits before proceeding with function reset.
1746  * During this process, interrupt vectors are freed and later requested
1747  * for handling possible port resource change.
1748  **/
1749 static int
lpfc_sli4_port_sta_fn_reset(struct lpfc_hba * phba,int mbx_action,bool en_rn_msg)1750 lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1751 			    bool en_rn_msg)
1752 {
1753 	int rc;
1754 	uint32_t intr_mode;
1755 
1756 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1757 	    LPFC_SLI_INTF_IF_TYPE_2) {
1758 		/*
1759 		 * On error status condition, driver need to wait for port
1760 		 * ready before performing reset.
1761 		 */
1762 		rc = lpfc_sli4_pdev_status_reg_wait(phba);
1763 		if (rc)
1764 			return rc;
1765 	}
1766 
1767 	/* need reset: attempt for port recovery */
1768 	if (en_rn_msg)
1769 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1770 				"2887 Reset Needed: Attempting Port "
1771 				"Recovery...\n");
1772 	lpfc_offline_prep(phba, mbx_action);
1773 	lpfc_offline(phba);
1774 	/* release interrupt for possible resource change */
1775 	lpfc_sli4_disable_intr(phba);
1776 	rc = lpfc_sli_brdrestart(phba);
1777 	if (rc) {
1778 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1779 				"6309 Failed to restart board\n");
1780 		return rc;
1781 	}
1782 	/* request and enable interrupt */
1783 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1784 	if (intr_mode == LPFC_INTR_ERROR) {
1785 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1786 				"3175 Failed to enable interrupt\n");
1787 		return -EIO;
1788 	}
1789 	phba->intr_mode = intr_mode;
1790 	rc = lpfc_online(phba);
1791 	if (rc == 0)
1792 		lpfc_unblock_mgmt_io(phba);
1793 
1794 	return rc;
1795 }
1796 
1797 /**
1798  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1799  * @phba: pointer to lpfc hba data structure.
1800  *
1801  * This routine is invoked to handle the SLI4 HBA hardware error attention
1802  * conditions.
1803  **/
1804 static void
lpfc_handle_eratt_s4(struct lpfc_hba * phba)1805 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1806 {
1807 	struct lpfc_vport *vport = phba->pport;
1808 	uint32_t event_data;
1809 	struct Scsi_Host *shost;
1810 	uint32_t if_type;
1811 	struct lpfc_register portstat_reg = {0};
1812 	uint32_t reg_err1, reg_err2;
1813 	uint32_t uerrlo_reg, uemasklo_reg;
1814 	uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
1815 	bool en_rn_msg = true;
1816 	struct temp_event temp_event_data;
1817 	struct lpfc_register portsmphr_reg;
1818 	int rc, i;
1819 
1820 	/* If the pci channel is offline, ignore possible errors, since
1821 	 * we cannot communicate with the pci card anyway.
1822 	 */
1823 	if (pci_channel_offline(phba->pcidev))
1824 		return;
1825 
1826 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
1827 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1828 	switch (if_type) {
1829 	case LPFC_SLI_INTF_IF_TYPE_0:
1830 		pci_rd_rc1 = lpfc_readl(
1831 				phba->sli4_hba.u.if_type0.UERRLOregaddr,
1832 				&uerrlo_reg);
1833 		pci_rd_rc2 = lpfc_readl(
1834 				phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1835 				&uemasklo_reg);
1836 		/* consider PCI bus read error as pci_channel_offline */
1837 		if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1838 			return;
1839 		if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1840 			lpfc_sli4_offline_eratt(phba);
1841 			return;
1842 		}
1843 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1844 				"7623 Checking UE recoverable");
1845 
1846 		for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1847 			if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1848 				       &portsmphr_reg.word0))
1849 				continue;
1850 
1851 			smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1852 						   &portsmphr_reg);
1853 			if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1854 			    LPFC_PORT_SEM_UE_RECOVERABLE)
1855 				break;
1856 			/*Sleep for 1Sec, before checking SEMAPHORE */
1857 			msleep(1000);
1858 		}
1859 
1860 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1861 				"4827 smphr_port_status x%x : Waited %dSec",
1862 				smphr_port_status, i);
1863 
1864 		/* Recoverable UE, reset the HBA device */
1865 		if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1866 		    LPFC_PORT_SEM_UE_RECOVERABLE) {
1867 			for (i = 0; i < 20; i++) {
1868 				msleep(1000);
1869 				if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1870 				    &portsmphr_reg.word0) &&
1871 				    (LPFC_POST_STAGE_PORT_READY ==
1872 				     bf_get(lpfc_port_smphr_port_status,
1873 				     &portsmphr_reg))) {
1874 					rc = lpfc_sli4_port_sta_fn_reset(phba,
1875 						LPFC_MBX_NO_WAIT, en_rn_msg);
1876 					if (rc == 0)
1877 						return;
1878 					lpfc_printf_log(phba,
1879 						KERN_ERR, LOG_INIT,
1880 						"4215 Failed to recover UE");
1881 					break;
1882 				}
1883 			}
1884 		}
1885 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1886 				"7624 Firmware not ready: Failing UE recovery,"
1887 				" waited %dSec", i);
1888 		lpfc_sli4_offline_eratt(phba);
1889 		break;
1890 
1891 	case LPFC_SLI_INTF_IF_TYPE_2:
1892 		pci_rd_rc1 = lpfc_readl(
1893 				phba->sli4_hba.u.if_type2.STATUSregaddr,
1894 				&portstat_reg.word0);
1895 		/* consider PCI bus read error as pci_channel_offline */
1896 		if (pci_rd_rc1 == -EIO) {
1897 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1898 				"3151 PCI bus read access failure: x%x\n",
1899 				readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
1900 			return;
1901 		}
1902 		reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1903 		reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
1904 		if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1905 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1906 				"2889 Port Overtemperature event, "
1907 				"taking port offline Data: x%x x%x\n",
1908 				reg_err1, reg_err2);
1909 
1910 			phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
1911 			temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1912 			temp_event_data.event_code = LPFC_CRIT_TEMP;
1913 			temp_event_data.data = 0xFFFFFFFF;
1914 
1915 			shost = lpfc_shost_from_vport(phba->pport);
1916 			fc_host_post_vendor_event(shost, fc_get_event_number(),
1917 						  sizeof(temp_event_data),
1918 						  (char *)&temp_event_data,
1919 						  SCSI_NL_VID_TYPE_PCI
1920 						  | PCI_VENDOR_ID_EMULEX);
1921 
1922 			spin_lock_irq(&phba->hbalock);
1923 			phba->over_temp_state = HBA_OVER_TEMP;
1924 			spin_unlock_irq(&phba->hbalock);
1925 			lpfc_sli4_offline_eratt(phba);
1926 			return;
1927 		}
1928 		if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1929 		    reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
1930 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1931 					"3143 Port Down: Firmware Update "
1932 					"Detected\n");
1933 			en_rn_msg = false;
1934 		} else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1935 			 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1936 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1937 					"3144 Port Down: Debug Dump\n");
1938 		else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1939 			 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1940 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1941 					"3145 Port Down: Provisioning\n");
1942 
1943 		/* If resets are disabled then leave the HBA alone and return */
1944 		if (!phba->cfg_enable_hba_reset)
1945 			return;
1946 
1947 		/* Check port status register for function reset */
1948 		rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1949 				en_rn_msg);
1950 		if (rc == 0) {
1951 			/* don't report event on forced debug dump */
1952 			if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1953 			    reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1954 				return;
1955 			else
1956 				break;
1957 		}
1958 		/* fall through for not able to recover */
1959 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1960 				"3152 Unrecoverable error, bring the port "
1961 				"offline\n");
1962 		lpfc_sli4_offline_eratt(phba);
1963 		break;
1964 	case LPFC_SLI_INTF_IF_TYPE_1:
1965 	default:
1966 		break;
1967 	}
1968 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1969 			"3123 Report dump event to upper layer\n");
1970 	/* Send an internal error event to mgmt application */
1971 	lpfc_board_errevt_to_mgmt(phba);
1972 
1973 	event_data = FC_REG_DUMP_EVENT;
1974 	shost = lpfc_shost_from_vport(vport);
1975 	fc_host_post_vendor_event(shost, fc_get_event_number(),
1976 				  sizeof(event_data), (char *) &event_data,
1977 				  SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1978 }
1979 
1980 /**
1981  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1982  * @phba: pointer to lpfc HBA data structure.
1983  *
1984  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1985  * routine from the API jump table function pointer from the lpfc_hba struct.
1986  *
1987  * Return codes
1988  *   0 - success.
1989  *   Any other value - error.
1990  **/
1991 void
lpfc_handle_eratt(struct lpfc_hba * phba)1992 lpfc_handle_eratt(struct lpfc_hba *phba)
1993 {
1994 	(*phba->lpfc_handle_eratt)(phba);
1995 }
1996 
1997 /**
1998  * lpfc_handle_latt - The HBA link event handler
1999  * @phba: pointer to lpfc hba data structure.
2000  *
2001  * This routine is invoked from the worker thread to handle a HBA host
2002  * attention link event. SLI3 only.
2003  **/
2004 void
lpfc_handle_latt(struct lpfc_hba * phba)2005 lpfc_handle_latt(struct lpfc_hba *phba)
2006 {
2007 	struct lpfc_vport *vport = phba->pport;
2008 	struct lpfc_sli   *psli = &phba->sli;
2009 	LPFC_MBOXQ_t *pmb;
2010 	volatile uint32_t control;
2011 	struct lpfc_dmabuf *mp;
2012 	int rc = 0;
2013 
2014 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2015 	if (!pmb) {
2016 		rc = 1;
2017 		goto lpfc_handle_latt_err_exit;
2018 	}
2019 
2020 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2021 	if (!mp) {
2022 		rc = 2;
2023 		goto lpfc_handle_latt_free_pmb;
2024 	}
2025 
2026 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2027 	if (!mp->virt) {
2028 		rc = 3;
2029 		goto lpfc_handle_latt_free_mp;
2030 	}
2031 
2032 	/* Cleanup any outstanding ELS commands */
2033 	lpfc_els_flush_all_cmd(phba);
2034 
2035 	psli->slistat.link_event++;
2036 	lpfc_read_topology(phba, pmb, mp);
2037 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
2038 	pmb->vport = vport;
2039 	/* Block ELS IOCBs until we have processed this mbox command */
2040 	phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2041 	rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
2042 	if (rc == MBX_NOT_FINISHED) {
2043 		rc = 4;
2044 		goto lpfc_handle_latt_free_mbuf;
2045 	}
2046 
2047 	/* Clear Link Attention in HA REG */
2048 	spin_lock_irq(&phba->hbalock);
2049 	writel(HA_LATT, phba->HAregaddr);
2050 	readl(phba->HAregaddr); /* flush */
2051 	spin_unlock_irq(&phba->hbalock);
2052 
2053 	return;
2054 
2055 lpfc_handle_latt_free_mbuf:
2056 	phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
2057 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2058 lpfc_handle_latt_free_mp:
2059 	kfree(mp);
2060 lpfc_handle_latt_free_pmb:
2061 	mempool_free(pmb, phba->mbox_mem_pool);
2062 lpfc_handle_latt_err_exit:
2063 	/* Enable Link attention interrupts */
2064 	spin_lock_irq(&phba->hbalock);
2065 	psli->sli_flag |= LPFC_PROCESS_LA;
2066 	control = readl(phba->HCregaddr);
2067 	control |= HC_LAINT_ENA;
2068 	writel(control, phba->HCregaddr);
2069 	readl(phba->HCregaddr); /* flush */
2070 
2071 	/* Clear Link Attention in HA REG */
2072 	writel(HA_LATT, phba->HAregaddr);
2073 	readl(phba->HAregaddr); /* flush */
2074 	spin_unlock_irq(&phba->hbalock);
2075 	lpfc_linkdown(phba);
2076 	phba->link_state = LPFC_HBA_ERROR;
2077 
2078 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2079 		     "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
2080 
2081 	return;
2082 }
2083 
2084 /**
2085  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
2086  * @phba: pointer to lpfc hba data structure.
2087  * @vpd: pointer to the vital product data.
2088  * @len: length of the vital product data in bytes.
2089  *
2090  * This routine parses the Vital Product Data (VPD). The VPD is treated as
2091  * an array of characters. In this routine, the ModelName, ProgramType, and
2092  * ModelDesc, etc. fields of the phba data structure will be populated.
2093  *
2094  * Return codes
2095  *   0 - pointer to the VPD passed in is NULL
2096  *   1 - success
2097  **/
2098 int
lpfc_parse_vpd(struct lpfc_hba * phba,uint8_t * vpd,int len)2099 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
2100 {
2101 	uint8_t lenlo, lenhi;
2102 	int Length;
2103 	int i, j;
2104 	int finished = 0;
2105 	int index = 0;
2106 
2107 	if (!vpd)
2108 		return 0;
2109 
2110 	/* Vital Product */
2111 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2112 			"0455 Vital Product Data: x%x x%x x%x x%x\n",
2113 			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2114 			(uint32_t) vpd[3]);
2115 	while (!finished && (index < (len - 4))) {
2116 		switch (vpd[index]) {
2117 		case 0x82:
2118 		case 0x91:
2119 			index += 1;
2120 			lenlo = vpd[index];
2121 			index += 1;
2122 			lenhi = vpd[index];
2123 			index += 1;
2124 			i = ((((unsigned short)lenhi) << 8) + lenlo);
2125 			index += i;
2126 			break;
2127 		case 0x90:
2128 			index += 1;
2129 			lenlo = vpd[index];
2130 			index += 1;
2131 			lenhi = vpd[index];
2132 			index += 1;
2133 			Length = ((((unsigned short)lenhi) << 8) + lenlo);
2134 			if (Length > len - index)
2135 				Length = len - index;
2136 			while (Length > 0) {
2137 			/* Look for Serial Number */
2138 			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2139 				index += 2;
2140 				i = vpd[index];
2141 				index += 1;
2142 				j = 0;
2143 				Length -= (3+i);
2144 				while(i--) {
2145 					phba->SerialNumber[j++] = vpd[index++];
2146 					if (j == 31)
2147 						break;
2148 				}
2149 				phba->SerialNumber[j] = 0;
2150 				continue;
2151 			}
2152 			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2153 				phba->vpd_flag |= VPD_MODEL_DESC;
2154 				index += 2;
2155 				i = vpd[index];
2156 				index += 1;
2157 				j = 0;
2158 				Length -= (3+i);
2159 				while(i--) {
2160 					phba->ModelDesc[j++] = vpd[index++];
2161 					if (j == 255)
2162 						break;
2163 				}
2164 				phba->ModelDesc[j] = 0;
2165 				continue;
2166 			}
2167 			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2168 				phba->vpd_flag |= VPD_MODEL_NAME;
2169 				index += 2;
2170 				i = vpd[index];
2171 				index += 1;
2172 				j = 0;
2173 				Length -= (3+i);
2174 				while(i--) {
2175 					phba->ModelName[j++] = vpd[index++];
2176 					if (j == 79)
2177 						break;
2178 				}
2179 				phba->ModelName[j] = 0;
2180 				continue;
2181 			}
2182 			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2183 				phba->vpd_flag |= VPD_PROGRAM_TYPE;
2184 				index += 2;
2185 				i = vpd[index];
2186 				index += 1;
2187 				j = 0;
2188 				Length -= (3+i);
2189 				while(i--) {
2190 					phba->ProgramType[j++] = vpd[index++];
2191 					if (j == 255)
2192 						break;
2193 				}
2194 				phba->ProgramType[j] = 0;
2195 				continue;
2196 			}
2197 			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2198 				phba->vpd_flag |= VPD_PORT;
2199 				index += 2;
2200 				i = vpd[index];
2201 				index += 1;
2202 				j = 0;
2203 				Length -= (3+i);
2204 				while(i--) {
2205 					if ((phba->sli_rev == LPFC_SLI_REV4) &&
2206 					    (phba->sli4_hba.pport_name_sta ==
2207 					     LPFC_SLI4_PPNAME_GET)) {
2208 						j++;
2209 						index++;
2210 					} else
2211 						phba->Port[j++] = vpd[index++];
2212 					if (j == 19)
2213 						break;
2214 				}
2215 				if ((phba->sli_rev != LPFC_SLI_REV4) ||
2216 				    (phba->sli4_hba.pport_name_sta ==
2217 				     LPFC_SLI4_PPNAME_NON))
2218 					phba->Port[j] = 0;
2219 				continue;
2220 			}
2221 			else {
2222 				index += 2;
2223 				i = vpd[index];
2224 				index += 1;
2225 				index += i;
2226 				Length -= (3 + i);
2227 			}
2228 		}
2229 		finished = 0;
2230 		break;
2231 		case 0x78:
2232 			finished = 1;
2233 			break;
2234 		default:
2235 			index ++;
2236 			break;
2237 		}
2238 	}
2239 
2240 	return(1);
2241 }
2242 
2243 /**
2244  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
2245  * @phba: pointer to lpfc hba data structure.
2246  * @mdp: pointer to the data structure to hold the derived model name.
2247  * @descp: pointer to the data structure to hold the derived description.
2248  *
2249  * This routine retrieves HBA's description based on its registered PCI device
2250  * ID. The @descp passed into this function points to an array of 256 chars. It
2251  * shall be returned with the model name, maximum speed, and the host bus type.
2252  * The @mdp passed into this function points to an array of 80 chars. When the
2253  * function returns, the @mdp will be filled with the model name.
2254  **/
2255 static void
lpfc_get_hba_model_desc(struct lpfc_hba * phba,uint8_t * mdp,uint8_t * descp)2256 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
2257 {
2258 	lpfc_vpd_t *vp;
2259 	uint16_t dev_id = phba->pcidev->device;
2260 	int max_speed;
2261 	int GE = 0;
2262 	int oneConnect = 0; /* default is not a oneConnect */
2263 	struct {
2264 		char *name;
2265 		char *bus;
2266 		char *function;
2267 	} m = {"<Unknown>", "", ""};
2268 
2269 	if (mdp && mdp[0] != '\0'
2270 		&& descp && descp[0] != '\0')
2271 		return;
2272 
2273 	if (phba->lmt & LMT_32Gb)
2274 		max_speed = 32;
2275 	else if (phba->lmt & LMT_16Gb)
2276 		max_speed = 16;
2277 	else if (phba->lmt & LMT_10Gb)
2278 		max_speed = 10;
2279 	else if (phba->lmt & LMT_8Gb)
2280 		max_speed = 8;
2281 	else if (phba->lmt & LMT_4Gb)
2282 		max_speed = 4;
2283 	else if (phba->lmt & LMT_2Gb)
2284 		max_speed = 2;
2285 	else if (phba->lmt & LMT_1Gb)
2286 		max_speed = 1;
2287 	else
2288 		max_speed = 0;
2289 
2290 	vp = &phba->vpd;
2291 
2292 	switch (dev_id) {
2293 	case PCI_DEVICE_ID_FIREFLY:
2294 		m = (typeof(m)){"LP6000", "PCI",
2295 				"Obsolete, Unsupported Fibre Channel Adapter"};
2296 		break;
2297 	case PCI_DEVICE_ID_SUPERFLY:
2298 		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
2299 			m = (typeof(m)){"LP7000", "PCI", ""};
2300 		else
2301 			m = (typeof(m)){"LP7000E", "PCI", ""};
2302 		m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2303 		break;
2304 	case PCI_DEVICE_ID_DRAGONFLY:
2305 		m = (typeof(m)){"LP8000", "PCI",
2306 				"Obsolete, Unsupported Fibre Channel Adapter"};
2307 		break;
2308 	case PCI_DEVICE_ID_CENTAUR:
2309 		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
2310 			m = (typeof(m)){"LP9002", "PCI", ""};
2311 		else
2312 			m = (typeof(m)){"LP9000", "PCI", ""};
2313 		m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2314 		break;
2315 	case PCI_DEVICE_ID_RFLY:
2316 		m = (typeof(m)){"LP952", "PCI",
2317 				"Obsolete, Unsupported Fibre Channel Adapter"};
2318 		break;
2319 	case PCI_DEVICE_ID_PEGASUS:
2320 		m = (typeof(m)){"LP9802", "PCI-X",
2321 				"Obsolete, Unsupported Fibre Channel Adapter"};
2322 		break;
2323 	case PCI_DEVICE_ID_THOR:
2324 		m = (typeof(m)){"LP10000", "PCI-X",
2325 				"Obsolete, Unsupported Fibre Channel Adapter"};
2326 		break;
2327 	case PCI_DEVICE_ID_VIPER:
2328 		m = (typeof(m)){"LPX1000",  "PCI-X",
2329 				"Obsolete, Unsupported Fibre Channel Adapter"};
2330 		break;
2331 	case PCI_DEVICE_ID_PFLY:
2332 		m = (typeof(m)){"LP982", "PCI-X",
2333 				"Obsolete, Unsupported Fibre Channel Adapter"};
2334 		break;
2335 	case PCI_DEVICE_ID_TFLY:
2336 		m = (typeof(m)){"LP1050", "PCI-X",
2337 				"Obsolete, Unsupported Fibre Channel Adapter"};
2338 		break;
2339 	case PCI_DEVICE_ID_HELIOS:
2340 		m = (typeof(m)){"LP11000", "PCI-X2",
2341 				"Obsolete, Unsupported Fibre Channel Adapter"};
2342 		break;
2343 	case PCI_DEVICE_ID_HELIOS_SCSP:
2344 		m = (typeof(m)){"LP11000-SP", "PCI-X2",
2345 				"Obsolete, Unsupported Fibre Channel Adapter"};
2346 		break;
2347 	case PCI_DEVICE_ID_HELIOS_DCSP:
2348 		m = (typeof(m)){"LP11002-SP",  "PCI-X2",
2349 				"Obsolete, Unsupported Fibre Channel Adapter"};
2350 		break;
2351 	case PCI_DEVICE_ID_NEPTUNE:
2352 		m = (typeof(m)){"LPe1000", "PCIe",
2353 				"Obsolete, Unsupported Fibre Channel Adapter"};
2354 		break;
2355 	case PCI_DEVICE_ID_NEPTUNE_SCSP:
2356 		m = (typeof(m)){"LPe1000-SP", "PCIe",
2357 				"Obsolete, Unsupported Fibre Channel Adapter"};
2358 		break;
2359 	case PCI_DEVICE_ID_NEPTUNE_DCSP:
2360 		m = (typeof(m)){"LPe1002-SP", "PCIe",
2361 				"Obsolete, Unsupported Fibre Channel Adapter"};
2362 		break;
2363 	case PCI_DEVICE_ID_BMID:
2364 		m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
2365 		break;
2366 	case PCI_DEVICE_ID_BSMB:
2367 		m = (typeof(m)){"LP111", "PCI-X2",
2368 				"Obsolete, Unsupported Fibre Channel Adapter"};
2369 		break;
2370 	case PCI_DEVICE_ID_ZEPHYR:
2371 		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2372 		break;
2373 	case PCI_DEVICE_ID_ZEPHYR_SCSP:
2374 		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2375 		break;
2376 	case PCI_DEVICE_ID_ZEPHYR_DCSP:
2377 		m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
2378 		GE = 1;
2379 		break;
2380 	case PCI_DEVICE_ID_ZMID:
2381 		m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
2382 		break;
2383 	case PCI_DEVICE_ID_ZSMB:
2384 		m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
2385 		break;
2386 	case PCI_DEVICE_ID_LP101:
2387 		m = (typeof(m)){"LP101", "PCI-X",
2388 				"Obsolete, Unsupported Fibre Channel Adapter"};
2389 		break;
2390 	case PCI_DEVICE_ID_LP10000S:
2391 		m = (typeof(m)){"LP10000-S", "PCI",
2392 				"Obsolete, Unsupported Fibre Channel Adapter"};
2393 		break;
2394 	case PCI_DEVICE_ID_LP11000S:
2395 		m = (typeof(m)){"LP11000-S", "PCI-X2",
2396 				"Obsolete, Unsupported Fibre Channel Adapter"};
2397 		break;
2398 	case PCI_DEVICE_ID_LPE11000S:
2399 		m = (typeof(m)){"LPe11000-S", "PCIe",
2400 				"Obsolete, Unsupported Fibre Channel Adapter"};
2401 		break;
2402 	case PCI_DEVICE_ID_SAT:
2403 		m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
2404 		break;
2405 	case PCI_DEVICE_ID_SAT_MID:
2406 		m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
2407 		break;
2408 	case PCI_DEVICE_ID_SAT_SMB:
2409 		m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
2410 		break;
2411 	case PCI_DEVICE_ID_SAT_DCSP:
2412 		m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
2413 		break;
2414 	case PCI_DEVICE_ID_SAT_SCSP:
2415 		m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
2416 		break;
2417 	case PCI_DEVICE_ID_SAT_S:
2418 		m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
2419 		break;
2420 	case PCI_DEVICE_ID_HORNET:
2421 		m = (typeof(m)){"LP21000", "PCIe",
2422 				"Obsolete, Unsupported FCoE Adapter"};
2423 		GE = 1;
2424 		break;
2425 	case PCI_DEVICE_ID_PROTEUS_VF:
2426 		m = (typeof(m)){"LPev12000", "PCIe IOV",
2427 				"Obsolete, Unsupported Fibre Channel Adapter"};
2428 		break;
2429 	case PCI_DEVICE_ID_PROTEUS_PF:
2430 		m = (typeof(m)){"LPev12000", "PCIe IOV",
2431 				"Obsolete, Unsupported Fibre Channel Adapter"};
2432 		break;
2433 	case PCI_DEVICE_ID_PROTEUS_S:
2434 		m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2435 				"Obsolete, Unsupported Fibre Channel Adapter"};
2436 		break;
2437 	case PCI_DEVICE_ID_TIGERSHARK:
2438 		oneConnect = 1;
2439 		m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
2440 		break;
2441 	case PCI_DEVICE_ID_TOMCAT:
2442 		oneConnect = 1;
2443 		m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2444 		break;
2445 	case PCI_DEVICE_ID_FALCON:
2446 		m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2447 				"EmulexSecure Fibre"};
2448 		break;
2449 	case PCI_DEVICE_ID_BALIUS:
2450 		m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2451 				"Obsolete, Unsupported Fibre Channel Adapter"};
2452 		break;
2453 	case PCI_DEVICE_ID_LANCER_FC:
2454 		m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
2455 		break;
2456 	case PCI_DEVICE_ID_LANCER_FC_VF:
2457 		m = (typeof(m)){"LPe16000", "PCIe",
2458 				"Obsolete, Unsupported Fibre Channel Adapter"};
2459 		break;
2460 	case PCI_DEVICE_ID_LANCER_FCOE:
2461 		oneConnect = 1;
2462 		m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
2463 		break;
2464 	case PCI_DEVICE_ID_LANCER_FCOE_VF:
2465 		oneConnect = 1;
2466 		m = (typeof(m)){"OCe15100", "PCIe",
2467 				"Obsolete, Unsupported FCoE"};
2468 		break;
2469 	case PCI_DEVICE_ID_LANCER_G6_FC:
2470 		m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2471 		break;
2472 	case PCI_DEVICE_ID_SKYHAWK:
2473 	case PCI_DEVICE_ID_SKYHAWK_VF:
2474 		oneConnect = 1;
2475 		m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2476 		break;
2477 	default:
2478 		m = (typeof(m)){"Unknown", "", ""};
2479 		break;
2480 	}
2481 
2482 	if (mdp && mdp[0] == '\0')
2483 		snprintf(mdp, 79,"%s", m.name);
2484 	/*
2485 	 * oneConnect hba requires special processing, they are all initiators
2486 	 * and we put the port number on the end
2487 	 */
2488 	if (descp && descp[0] == '\0') {
2489 		if (oneConnect)
2490 			snprintf(descp, 255,
2491 				"Emulex OneConnect %s, %s Initiator %s",
2492 				m.name, m.function,
2493 				phba->Port);
2494 		else if (max_speed == 0)
2495 			snprintf(descp, 255,
2496 				"Emulex %s %s %s",
2497 				m.name, m.bus, m.function);
2498 		else
2499 			snprintf(descp, 255,
2500 				"Emulex %s %d%s %s %s",
2501 				m.name, max_speed, (GE) ? "GE" : "Gb",
2502 				m.bus, m.function);
2503 	}
2504 }
2505 
2506 /**
2507  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
2508  * @phba: pointer to lpfc hba data structure.
2509  * @pring: pointer to a IOCB ring.
2510  * @cnt: the number of IOCBs to be posted to the IOCB ring.
2511  *
2512  * This routine posts a given number of IOCBs with the associated DMA buffer
2513  * descriptors specified by the cnt argument to the given IOCB ring.
2514  *
2515  * Return codes
2516  *   The number of IOCBs NOT able to be posted to the IOCB ring.
2517  **/
2518 int
lpfc_post_buffer(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,int cnt)2519 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2520 {
2521 	IOCB_t *icmd;
2522 	struct lpfc_iocbq *iocb;
2523 	struct lpfc_dmabuf *mp1, *mp2;
2524 
2525 	cnt += pring->missbufcnt;
2526 
2527 	/* While there are buffers to post */
2528 	while (cnt > 0) {
2529 		/* Allocate buffer for  command iocb */
2530 		iocb = lpfc_sli_get_iocbq(phba);
2531 		if (iocb == NULL) {
2532 			pring->missbufcnt = cnt;
2533 			return cnt;
2534 		}
2535 		icmd = &iocb->iocb;
2536 
2537 		/* 2 buffers can be posted per command */
2538 		/* Allocate buffer to post */
2539 		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2540 		if (mp1)
2541 		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2542 		if (!mp1 || !mp1->virt) {
2543 			kfree(mp1);
2544 			lpfc_sli_release_iocbq(phba, iocb);
2545 			pring->missbufcnt = cnt;
2546 			return cnt;
2547 		}
2548 
2549 		INIT_LIST_HEAD(&mp1->list);
2550 		/* Allocate buffer to post */
2551 		if (cnt > 1) {
2552 			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2553 			if (mp2)
2554 				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2555 							    &mp2->phys);
2556 			if (!mp2 || !mp2->virt) {
2557 				kfree(mp2);
2558 				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2559 				kfree(mp1);
2560 				lpfc_sli_release_iocbq(phba, iocb);
2561 				pring->missbufcnt = cnt;
2562 				return cnt;
2563 			}
2564 
2565 			INIT_LIST_HEAD(&mp2->list);
2566 		} else {
2567 			mp2 = NULL;
2568 		}
2569 
2570 		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2571 		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2572 		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2573 		icmd->ulpBdeCount = 1;
2574 		cnt--;
2575 		if (mp2) {
2576 			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2577 			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2578 			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2579 			cnt--;
2580 			icmd->ulpBdeCount = 2;
2581 		}
2582 
2583 		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2584 		icmd->ulpLe = 1;
2585 
2586 		if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2587 		    IOCB_ERROR) {
2588 			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2589 			kfree(mp1);
2590 			cnt++;
2591 			if (mp2) {
2592 				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2593 				kfree(mp2);
2594 				cnt++;
2595 			}
2596 			lpfc_sli_release_iocbq(phba, iocb);
2597 			pring->missbufcnt = cnt;
2598 			return cnt;
2599 		}
2600 		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2601 		if (mp2)
2602 			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2603 	}
2604 	pring->missbufcnt = 0;
2605 	return 0;
2606 }
2607 
2608 /**
2609  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2610  * @phba: pointer to lpfc hba data structure.
2611  *
2612  * This routine posts initial receive IOCB buffers to the ELS ring. The
2613  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2614  * set to 64 IOCBs. SLI3 only.
2615  *
2616  * Return codes
2617  *   0 - success (currently always success)
2618  **/
2619 static int
lpfc_post_rcv_buf(struct lpfc_hba * phba)2620 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2621 {
2622 	struct lpfc_sli *psli = &phba->sli;
2623 
2624 	/* Ring 0, ELS / CT buffers */
2625 	lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2626 	/* Ring 2 - FCP no buffers needed */
2627 
2628 	return 0;
2629 }
2630 
2631 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2632 
2633 /**
2634  * lpfc_sha_init - Set up initial array of hash table entries
2635  * @HashResultPointer: pointer to an array as hash table.
2636  *
2637  * This routine sets up the initial values to the array of hash table entries
2638  * for the LC HBAs.
2639  **/
2640 static void
lpfc_sha_init(uint32_t * HashResultPointer)2641 lpfc_sha_init(uint32_t * HashResultPointer)
2642 {
2643 	HashResultPointer[0] = 0x67452301;
2644 	HashResultPointer[1] = 0xEFCDAB89;
2645 	HashResultPointer[2] = 0x98BADCFE;
2646 	HashResultPointer[3] = 0x10325476;
2647 	HashResultPointer[4] = 0xC3D2E1F0;
2648 }
2649 
2650 /**
2651  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2652  * @HashResultPointer: pointer to an initial/result hash table.
2653  * @HashWorkingPointer: pointer to an working hash table.
2654  *
2655  * This routine iterates an initial hash table pointed by @HashResultPointer
2656  * with the values from the working hash table pointeed by @HashWorkingPointer.
2657  * The results are putting back to the initial hash table, returned through
2658  * the @HashResultPointer as the result hash table.
2659  **/
2660 static void
lpfc_sha_iterate(uint32_t * HashResultPointer,uint32_t * HashWorkingPointer)2661 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2662 {
2663 	int t;
2664 	uint32_t TEMP;
2665 	uint32_t A, B, C, D, E;
2666 	t = 16;
2667 	do {
2668 		HashWorkingPointer[t] =
2669 		    S(1,
2670 		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2671 								     8] ^
2672 		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2673 	} while (++t <= 79);
2674 	t = 0;
2675 	A = HashResultPointer[0];
2676 	B = HashResultPointer[1];
2677 	C = HashResultPointer[2];
2678 	D = HashResultPointer[3];
2679 	E = HashResultPointer[4];
2680 
2681 	do {
2682 		if (t < 20) {
2683 			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2684 		} else if (t < 40) {
2685 			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2686 		} else if (t < 60) {
2687 			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2688 		} else {
2689 			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2690 		}
2691 		TEMP += S(5, A) + E + HashWorkingPointer[t];
2692 		E = D;
2693 		D = C;
2694 		C = S(30, B);
2695 		B = A;
2696 		A = TEMP;
2697 	} while (++t <= 79);
2698 
2699 	HashResultPointer[0] += A;
2700 	HashResultPointer[1] += B;
2701 	HashResultPointer[2] += C;
2702 	HashResultPointer[3] += D;
2703 	HashResultPointer[4] += E;
2704 
2705 }
2706 
2707 /**
2708  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2709  * @RandomChallenge: pointer to the entry of host challenge random number array.
2710  * @HashWorking: pointer to the entry of the working hash array.
2711  *
2712  * This routine calculates the working hash array referred by @HashWorking
2713  * from the challenge random numbers associated with the host, referred by
2714  * @RandomChallenge. The result is put into the entry of the working hash
2715  * array and returned by reference through @HashWorking.
2716  **/
2717 static void
lpfc_challenge_key(uint32_t * RandomChallenge,uint32_t * HashWorking)2718 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2719 {
2720 	*HashWorking = (*RandomChallenge ^ *HashWorking);
2721 }
2722 
2723 /**
2724  * lpfc_hba_init - Perform special handling for LC HBA initialization
2725  * @phba: pointer to lpfc hba data structure.
2726  * @hbainit: pointer to an array of unsigned 32-bit integers.
2727  *
2728  * This routine performs the special handling for LC HBA initialization.
2729  **/
2730 void
lpfc_hba_init(struct lpfc_hba * phba,uint32_t * hbainit)2731 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2732 {
2733 	int t;
2734 	uint32_t *HashWorking;
2735 	uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2736 
2737 	HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2738 	if (!HashWorking)
2739 		return;
2740 
2741 	HashWorking[0] = HashWorking[78] = *pwwnn++;
2742 	HashWorking[1] = HashWorking[79] = *pwwnn;
2743 
2744 	for (t = 0; t < 7; t++)
2745 		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2746 
2747 	lpfc_sha_init(hbainit);
2748 	lpfc_sha_iterate(hbainit, HashWorking);
2749 	kfree(HashWorking);
2750 }
2751 
2752 /**
2753  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2754  * @vport: pointer to a virtual N_Port data structure.
2755  *
2756  * This routine performs the necessary cleanups before deleting the @vport.
2757  * It invokes the discovery state machine to perform necessary state
2758  * transitions and to release the ndlps associated with the @vport. Note,
2759  * the physical port is treated as @vport 0.
2760  **/
2761 void
lpfc_cleanup(struct lpfc_vport * vport)2762 lpfc_cleanup(struct lpfc_vport *vport)
2763 {
2764 	struct lpfc_hba   *phba = vport->phba;
2765 	struct lpfc_nodelist *ndlp, *next_ndlp;
2766 	int i = 0;
2767 
2768 	if (phba->link_state > LPFC_LINK_DOWN)
2769 		lpfc_port_link_failure(vport);
2770 
2771 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2772 		if (!NLP_CHK_NODE_ACT(ndlp)) {
2773 			ndlp = lpfc_enable_node(vport, ndlp,
2774 						NLP_STE_UNUSED_NODE);
2775 			if (!ndlp)
2776 				continue;
2777 			spin_lock_irq(&phba->ndlp_lock);
2778 			NLP_SET_FREE_REQ(ndlp);
2779 			spin_unlock_irq(&phba->ndlp_lock);
2780 			/* Trigger the release of the ndlp memory */
2781 			lpfc_nlp_put(ndlp);
2782 			continue;
2783 		}
2784 		spin_lock_irq(&phba->ndlp_lock);
2785 		if (NLP_CHK_FREE_REQ(ndlp)) {
2786 			/* The ndlp should not be in memory free mode already */
2787 			spin_unlock_irq(&phba->ndlp_lock);
2788 			continue;
2789 		} else
2790 			/* Indicate request for freeing ndlp memory */
2791 			NLP_SET_FREE_REQ(ndlp);
2792 		spin_unlock_irq(&phba->ndlp_lock);
2793 
2794 		if (vport->port_type != LPFC_PHYSICAL_PORT &&
2795 		    ndlp->nlp_DID == Fabric_DID) {
2796 			/* Just free up ndlp with Fabric_DID for vports */
2797 			lpfc_nlp_put(ndlp);
2798 			continue;
2799 		}
2800 
2801 		/* take care of nodes in unused state before the state
2802 		 * machine taking action.
2803 		 */
2804 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2805 			lpfc_nlp_put(ndlp);
2806 			continue;
2807 		}
2808 
2809 		if (ndlp->nlp_type & NLP_FABRIC)
2810 			lpfc_disc_state_machine(vport, ndlp, NULL,
2811 					NLP_EVT_DEVICE_RECOVERY);
2812 
2813 		lpfc_disc_state_machine(vport, ndlp, NULL,
2814 					     NLP_EVT_DEVICE_RM);
2815 	}
2816 
2817 	/* At this point, ALL ndlp's should be gone
2818 	 * because of the previous NLP_EVT_DEVICE_RM.
2819 	 * Lets wait for this to happen, if needed.
2820 	 */
2821 	while (!list_empty(&vport->fc_nodes)) {
2822 		if (i++ > 3000) {
2823 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2824 				"0233 Nodelist not empty\n");
2825 			list_for_each_entry_safe(ndlp, next_ndlp,
2826 						&vport->fc_nodes, nlp_listp) {
2827 				lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2828 						LOG_NODE,
2829 						"0282 did:x%x ndlp:x%p "
2830 						"usgmap:x%x refcnt:%d\n",
2831 						ndlp->nlp_DID, (void *)ndlp,
2832 						ndlp->nlp_usg_map,
2833 						kref_read(&ndlp->kref));
2834 			}
2835 			break;
2836 		}
2837 
2838 		/* Wait for any activity on ndlps to settle */
2839 		msleep(10);
2840 	}
2841 	lpfc_cleanup_vports_rrqs(vport, NULL);
2842 }
2843 
2844 /**
2845  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2846  * @vport: pointer to a virtual N_Port data structure.
2847  *
2848  * This routine stops all the timers associated with a @vport. This function
2849  * is invoked before disabling or deleting a @vport. Note that the physical
2850  * port is treated as @vport 0.
2851  **/
2852 void
lpfc_stop_vport_timers(struct lpfc_vport * vport)2853 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2854 {
2855 	del_timer_sync(&vport->els_tmofunc);
2856 	del_timer_sync(&vport->delayed_disc_tmo);
2857 	lpfc_can_disctmo(vport);
2858 	return;
2859 }
2860 
2861 /**
2862  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2863  * @phba: pointer to lpfc hba data structure.
2864  *
2865  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2866  * caller of this routine should already hold the host lock.
2867  **/
2868 void
__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba * phba)2869 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2870 {
2871 	/* Clear pending FCF rediscovery wait flag */
2872 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2873 
2874 	/* Now, try to stop the timer */
2875 	del_timer(&phba->fcf.redisc_wait);
2876 }
2877 
2878 /**
2879  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2880  * @phba: pointer to lpfc hba data structure.
2881  *
2882  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2883  * checks whether the FCF rediscovery wait timer is pending with the host
2884  * lock held before proceeding with disabling the timer and clearing the
2885  * wait timer pendig flag.
2886  **/
2887 void
lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba * phba)2888 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2889 {
2890 	spin_lock_irq(&phba->hbalock);
2891 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2892 		/* FCF rediscovery timer already fired or stopped */
2893 		spin_unlock_irq(&phba->hbalock);
2894 		return;
2895 	}
2896 	__lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2897 	/* Clear failover in progress flags */
2898 	phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2899 	spin_unlock_irq(&phba->hbalock);
2900 }
2901 
2902 /**
2903  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2904  * @phba: pointer to lpfc hba data structure.
2905  *
2906  * This routine stops all the timers associated with a HBA. This function is
2907  * invoked before either putting a HBA offline or unloading the driver.
2908  **/
2909 void
lpfc_stop_hba_timers(struct lpfc_hba * phba)2910 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2911 {
2912 	lpfc_stop_vport_timers(phba->pport);
2913 	del_timer_sync(&phba->sli.mbox_tmo);
2914 	del_timer_sync(&phba->fabric_block_timer);
2915 	del_timer_sync(&phba->eratt_poll);
2916 	del_timer_sync(&phba->hb_tmofunc);
2917 	if (phba->sli_rev == LPFC_SLI_REV4) {
2918 		del_timer_sync(&phba->rrq_tmr);
2919 		phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2920 	}
2921 	phba->hb_outstanding = 0;
2922 
2923 	switch (phba->pci_dev_grp) {
2924 	case LPFC_PCI_DEV_LP:
2925 		/* Stop any LightPulse device specific driver timers */
2926 		del_timer_sync(&phba->fcp_poll_timer);
2927 		break;
2928 	case LPFC_PCI_DEV_OC:
2929 		/* Stop any OneConnect device sepcific driver timers */
2930 		lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2931 		break;
2932 	default:
2933 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2934 				"0297 Invalid device group (x%x)\n",
2935 				phba->pci_dev_grp);
2936 		break;
2937 	}
2938 	return;
2939 }
2940 
2941 /**
2942  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2943  * @phba: pointer to lpfc hba data structure.
2944  *
2945  * This routine marks a HBA's management interface as blocked. Once the HBA's
2946  * management interface is marked as blocked, all the user space access to
2947  * the HBA, whether they are from sysfs interface or libdfc interface will
2948  * all be blocked. The HBA is set to block the management interface when the
2949  * driver prepares the HBA interface for online or offline.
2950  **/
2951 static void
lpfc_block_mgmt_io(struct lpfc_hba * phba,int mbx_action)2952 lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
2953 {
2954 	unsigned long iflag;
2955 	uint8_t actcmd = MBX_HEARTBEAT;
2956 	unsigned long timeout;
2957 
2958 	spin_lock_irqsave(&phba->hbalock, iflag);
2959 	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2960 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2961 	if (mbx_action == LPFC_MBX_NO_WAIT)
2962 		return;
2963 	timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2964 	spin_lock_irqsave(&phba->hbalock, iflag);
2965 	if (phba->sli.mbox_active) {
2966 		actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2967 		/* Determine how long we might wait for the active mailbox
2968 		 * command to be gracefully completed by firmware.
2969 		 */
2970 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2971 				phba->sli.mbox_active) * 1000) + jiffies;
2972 	}
2973 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2974 
2975 	/* Wait for the outstnading mailbox command to complete */
2976 	while (phba->sli.mbox_active) {
2977 		/* Check active mailbox complete status every 2ms */
2978 		msleep(2);
2979 		if (time_after(jiffies, timeout)) {
2980 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2981 				"2813 Mgmt IO is Blocked %x "
2982 				"- mbox cmd %x still active\n",
2983 				phba->sli.sli_flag, actcmd);
2984 			break;
2985 		}
2986 	}
2987 }
2988 
2989 /**
2990  * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2991  * @phba: pointer to lpfc hba data structure.
2992  *
2993  * Allocate RPIs for all active remote nodes. This is needed whenever
2994  * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2995  * is to fixup the temporary rpi assignments.
2996  **/
2997 void
lpfc_sli4_node_prep(struct lpfc_hba * phba)2998 lpfc_sli4_node_prep(struct lpfc_hba *phba)
2999 {
3000 	struct lpfc_nodelist  *ndlp, *next_ndlp;
3001 	struct lpfc_vport **vports;
3002 	int i, rpi;
3003 	unsigned long flags;
3004 
3005 	if (phba->sli_rev != LPFC_SLI_REV4)
3006 		return;
3007 
3008 	vports = lpfc_create_vport_work_array(phba);
3009 	if (vports == NULL)
3010 		return;
3011 
3012 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3013 		if (vports[i]->load_flag & FC_UNLOADING)
3014 			continue;
3015 
3016 		list_for_each_entry_safe(ndlp, next_ndlp,
3017 					 &vports[i]->fc_nodes,
3018 					 nlp_listp) {
3019 			if (!NLP_CHK_NODE_ACT(ndlp))
3020 				continue;
3021 			rpi = lpfc_sli4_alloc_rpi(phba);
3022 			if (rpi == LPFC_RPI_ALLOC_ERROR) {
3023 				spin_lock_irqsave(&phba->ndlp_lock, flags);
3024 				NLP_CLR_NODE_ACT(ndlp);
3025 				spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3026 				continue;
3027 			}
3028 			ndlp->nlp_rpi = rpi;
3029 			lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3030 					 "0009 rpi:%x DID:%x "
3031 					 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3032 					 ndlp->nlp_DID, ndlp->nlp_flag,
3033 					 ndlp->nlp_usg_map, ndlp);
3034 		}
3035 	}
3036 	lpfc_destroy_vport_work_array(phba, vports);
3037 }
3038 
3039 /**
3040  * lpfc_online - Initialize and bring a HBA online
3041  * @phba: pointer to lpfc hba data structure.
3042  *
3043  * This routine initializes the HBA and brings a HBA online. During this
3044  * process, the management interface is blocked to prevent user space access
3045  * to the HBA interfering with the driver initialization.
3046  *
3047  * Return codes
3048  *   0 - successful
3049  *   1 - failed
3050  **/
3051 int
lpfc_online(struct lpfc_hba * phba)3052 lpfc_online(struct lpfc_hba *phba)
3053 {
3054 	struct lpfc_vport *vport;
3055 	struct lpfc_vport **vports;
3056 	int i, error = 0;
3057 	bool vpis_cleared = false;
3058 
3059 	if (!phba)
3060 		return 0;
3061 	vport = phba->pport;
3062 
3063 	if (!(vport->fc_flag & FC_OFFLINE_MODE))
3064 		return 0;
3065 
3066 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3067 			"0458 Bring Adapter online\n");
3068 
3069 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
3070 
3071 	if (phba->sli_rev == LPFC_SLI_REV4) {
3072 		if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3073 			lpfc_unblock_mgmt_io(phba);
3074 			return 1;
3075 		}
3076 		spin_lock_irq(&phba->hbalock);
3077 		if (!phba->sli4_hba.max_cfg_param.vpi_used)
3078 			vpis_cleared = true;
3079 		spin_unlock_irq(&phba->hbalock);
3080 
3081 		/* Reestablish the local initiator port.
3082 		 * The offline process destroyed the previous lport.
3083 		 */
3084 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3085 				!phba->nvmet_support) {
3086 			error = lpfc_nvme_create_localport(phba->pport);
3087 			if (error)
3088 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3089 					"6132 NVME restore reg failed "
3090 					"on nvmei error x%x\n", error);
3091 		}
3092 	} else {
3093 		lpfc_sli_queue_init(phba);
3094 		if (lpfc_sli_hba_setup(phba)) {	/* Initialize SLI2/SLI3 HBA */
3095 			lpfc_unblock_mgmt_io(phba);
3096 			return 1;
3097 		}
3098 	}
3099 
3100 	vports = lpfc_create_vport_work_array(phba);
3101 	if (vports != NULL) {
3102 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3103 			struct Scsi_Host *shost;
3104 			shost = lpfc_shost_from_vport(vports[i]);
3105 			spin_lock_irq(shost->host_lock);
3106 			vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3107 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3108 				vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3109 			if (phba->sli_rev == LPFC_SLI_REV4) {
3110 				vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
3111 				if ((vpis_cleared) &&
3112 				    (vports[i]->port_type !=
3113 					LPFC_PHYSICAL_PORT))
3114 					vports[i]->vpi = 0;
3115 			}
3116 			spin_unlock_irq(shost->host_lock);
3117 		}
3118 	}
3119 	lpfc_destroy_vport_work_array(phba, vports);
3120 
3121 	lpfc_unblock_mgmt_io(phba);
3122 	return 0;
3123 }
3124 
3125 /**
3126  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
3127  * @phba: pointer to lpfc hba data structure.
3128  *
3129  * This routine marks a HBA's management interface as not blocked. Once the
3130  * HBA's management interface is marked as not blocked, all the user space
3131  * access to the HBA, whether they are from sysfs interface or libdfc
3132  * interface will be allowed. The HBA is set to block the management interface
3133  * when the driver prepares the HBA interface for online or offline and then
3134  * set to unblock the management interface afterwards.
3135  **/
3136 void
lpfc_unblock_mgmt_io(struct lpfc_hba * phba)3137 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3138 {
3139 	unsigned long iflag;
3140 
3141 	spin_lock_irqsave(&phba->hbalock, iflag);
3142 	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3143 	spin_unlock_irqrestore(&phba->hbalock, iflag);
3144 }
3145 
3146 /**
3147  * lpfc_offline_prep - Prepare a HBA to be brought offline
3148  * @phba: pointer to lpfc hba data structure.
3149  *
3150  * This routine is invoked to prepare a HBA to be brought offline. It performs
3151  * unregistration login to all the nodes on all vports and flushes the mailbox
3152  * queue to make it ready to be brought offline.
3153  **/
3154 void
lpfc_offline_prep(struct lpfc_hba * phba,int mbx_action)3155 lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
3156 {
3157 	struct lpfc_vport *vport = phba->pport;
3158 	struct lpfc_nodelist  *ndlp, *next_ndlp;
3159 	struct lpfc_vport **vports;
3160 	struct Scsi_Host *shost;
3161 	int i;
3162 
3163 	if (vport->fc_flag & FC_OFFLINE_MODE)
3164 		return;
3165 
3166 	lpfc_block_mgmt_io(phba, mbx_action);
3167 
3168 	lpfc_linkdown(phba);
3169 
3170 	/* Issue an unreg_login to all nodes on all vports */
3171 	vports = lpfc_create_vport_work_array(phba);
3172 	if (vports != NULL) {
3173 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3174 			if (vports[i]->load_flag & FC_UNLOADING)
3175 				continue;
3176 			shost = lpfc_shost_from_vport(vports[i]);
3177 			spin_lock_irq(shost->host_lock);
3178 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
3179 			vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3180 			vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
3181 			spin_unlock_irq(shost->host_lock);
3182 
3183 			shost =	lpfc_shost_from_vport(vports[i]);
3184 			list_for_each_entry_safe(ndlp, next_ndlp,
3185 						 &vports[i]->fc_nodes,
3186 						 nlp_listp) {
3187 				if (!NLP_CHK_NODE_ACT(ndlp))
3188 					continue;
3189 				if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3190 					continue;
3191 				if (ndlp->nlp_type & NLP_FABRIC) {
3192 					lpfc_disc_state_machine(vports[i], ndlp,
3193 						NULL, NLP_EVT_DEVICE_RECOVERY);
3194 					lpfc_disc_state_machine(vports[i], ndlp,
3195 						NULL, NLP_EVT_DEVICE_RM);
3196 				}
3197 				spin_lock_irq(shost->host_lock);
3198 				ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3199 				spin_unlock_irq(shost->host_lock);
3200 				/*
3201 				 * Whenever an SLI4 port goes offline, free the
3202 				 * RPI. Get a new RPI when the adapter port
3203 				 * comes back online.
3204 				 */
3205 				if (phba->sli_rev == LPFC_SLI_REV4) {
3206 					lpfc_printf_vlog(ndlp->vport,
3207 							 KERN_INFO, LOG_NODE,
3208 							 "0011 lpfc_offline: "
3209 							 "ndlp:x%p did %x "
3210 							 "usgmap:x%x rpi:%x\n",
3211 							 ndlp, ndlp->nlp_DID,
3212 							 ndlp->nlp_usg_map,
3213 							 ndlp->nlp_rpi);
3214 
3215 					lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
3216 				}
3217 				lpfc_unreg_rpi(vports[i], ndlp);
3218 			}
3219 		}
3220 	}
3221 	lpfc_destroy_vport_work_array(phba, vports);
3222 
3223 	lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
3224 }
3225 
3226 /**
3227  * lpfc_offline - Bring a HBA offline
3228  * @phba: pointer to lpfc hba data structure.
3229  *
3230  * This routine actually brings a HBA offline. It stops all the timers
3231  * associated with the HBA, brings down the SLI layer, and eventually
3232  * marks the HBA as in offline state for the upper layer protocol.
3233  **/
3234 void
lpfc_offline(struct lpfc_hba * phba)3235 lpfc_offline(struct lpfc_hba *phba)
3236 {
3237 	struct Scsi_Host  *shost;
3238 	struct lpfc_vport **vports;
3239 	int i;
3240 
3241 	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
3242 		return;
3243 
3244 	/* stop port and all timers associated with this hba */
3245 	lpfc_stop_port(phba);
3246 
3247 	/* Tear down the local and target port registrations.  The
3248 	 * nvme transports need to cleanup.
3249 	 */
3250 	lpfc_nvmet_destroy_targetport(phba);
3251 	lpfc_nvme_destroy_localport(phba->pport);
3252 
3253 	vports = lpfc_create_vport_work_array(phba);
3254 	if (vports != NULL)
3255 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3256 			lpfc_stop_vport_timers(vports[i]);
3257 	lpfc_destroy_vport_work_array(phba, vports);
3258 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3259 			"0460 Bring Adapter offline\n");
3260 	/* Bring down the SLI Layer and cleanup.  The HBA is offline
3261 	   now.  */
3262 	lpfc_sli_hba_down(phba);
3263 	spin_lock_irq(&phba->hbalock);
3264 	phba->work_ha = 0;
3265 	spin_unlock_irq(&phba->hbalock);
3266 	vports = lpfc_create_vport_work_array(phba);
3267 	if (vports != NULL)
3268 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3269 			shost = lpfc_shost_from_vport(vports[i]);
3270 			spin_lock_irq(shost->host_lock);
3271 			vports[i]->work_port_events = 0;
3272 			vports[i]->fc_flag |= FC_OFFLINE_MODE;
3273 			spin_unlock_irq(shost->host_lock);
3274 		}
3275 	lpfc_destroy_vport_work_array(phba, vports);
3276 }
3277 
3278 /**
3279  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
3280  * @phba: pointer to lpfc hba data structure.
3281  *
3282  * This routine is to free all the SCSI buffers and IOCBs from the driver
3283  * list back to kernel. It is called from lpfc_pci_remove_one to free
3284  * the internal resources before the device is removed from the system.
3285  **/
3286 static void
lpfc_scsi_free(struct lpfc_hba * phba)3287 lpfc_scsi_free(struct lpfc_hba *phba)
3288 {
3289 	struct lpfc_scsi_buf *sb, *sb_next;
3290 
3291 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3292 		return;
3293 
3294 	spin_lock_irq(&phba->hbalock);
3295 
3296 	/* Release all the lpfc_scsi_bufs maintained by this host. */
3297 
3298 	spin_lock(&phba->scsi_buf_list_put_lock);
3299 	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3300 				 list) {
3301 		list_del(&sb->list);
3302 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3303 			      sb->dma_handle);
3304 		kfree(sb);
3305 		phba->total_scsi_bufs--;
3306 	}
3307 	spin_unlock(&phba->scsi_buf_list_put_lock);
3308 
3309 	spin_lock(&phba->scsi_buf_list_get_lock);
3310 	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3311 				 list) {
3312 		list_del(&sb->list);
3313 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3314 			      sb->dma_handle);
3315 		kfree(sb);
3316 		phba->total_scsi_bufs--;
3317 	}
3318 	spin_unlock(&phba->scsi_buf_list_get_lock);
3319 	spin_unlock_irq(&phba->hbalock);
3320 }
3321 /**
3322  * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3323  * @phba: pointer to lpfc hba data structure.
3324  *
3325  * This routine is to free all the NVME buffers and IOCBs from the driver
3326  * list back to kernel. It is called from lpfc_pci_remove_one to free
3327  * the internal resources before the device is removed from the system.
3328  **/
3329 static void
lpfc_nvme_free(struct lpfc_hba * phba)3330 lpfc_nvme_free(struct lpfc_hba *phba)
3331 {
3332 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
3333 
3334 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3335 		return;
3336 
3337 	spin_lock_irq(&phba->hbalock);
3338 
3339 	/* Release all the lpfc_nvme_bufs maintained by this host. */
3340 	spin_lock(&phba->nvme_buf_list_put_lock);
3341 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3342 				 &phba->lpfc_nvme_buf_list_put, list) {
3343 		list_del(&lpfc_ncmd->list);
3344 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3345 			      lpfc_ncmd->dma_handle);
3346 		kfree(lpfc_ncmd);
3347 		phba->total_nvme_bufs--;
3348 	}
3349 	spin_unlock(&phba->nvme_buf_list_put_lock);
3350 
3351 	spin_lock(&phba->nvme_buf_list_get_lock);
3352 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3353 				 &phba->lpfc_nvme_buf_list_get, list) {
3354 		list_del(&lpfc_ncmd->list);
3355 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3356 			      lpfc_ncmd->dma_handle);
3357 		kfree(lpfc_ncmd);
3358 		phba->total_nvme_bufs--;
3359 	}
3360 	spin_unlock(&phba->nvme_buf_list_get_lock);
3361 	spin_unlock_irq(&phba->hbalock);
3362 }
3363 /**
3364  * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
3365  * @phba: pointer to lpfc hba data structure.
3366  *
3367  * This routine first calculates the sizes of the current els and allocated
3368  * scsi sgl lists, and then goes through all sgls to updates the physical
3369  * XRIs assigned due to port function reset. During port initialization, the
3370  * current els and allocated scsi sgl lists are 0s.
3371  *
3372  * Return codes
3373  *   0 - successful (for now, it always returns 0)
3374  **/
3375 int
lpfc_sli4_els_sgl_update(struct lpfc_hba * phba)3376 lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
3377 {
3378 	struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3379 	uint16_t i, lxri, xri_cnt, els_xri_cnt;
3380 	LIST_HEAD(els_sgl_list);
3381 	int rc;
3382 
3383 	/*
3384 	 * update on pci function's els xri-sgl list
3385 	 */
3386 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3387 
3388 	if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3389 		/* els xri-sgl expanded */
3390 		xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3391 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3392 				"3157 ELS xri-sgl count increased from "
3393 				"%d to %d\n", phba->sli4_hba.els_xri_cnt,
3394 				els_xri_cnt);
3395 		/* allocate the additional els sgls */
3396 		for (i = 0; i < xri_cnt; i++) {
3397 			sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3398 					     GFP_KERNEL);
3399 			if (sglq_entry == NULL) {
3400 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3401 						"2562 Failure to allocate an "
3402 						"ELS sgl entry:%d\n", i);
3403 				rc = -ENOMEM;
3404 				goto out_free_mem;
3405 			}
3406 			sglq_entry->buff_type = GEN_BUFF_TYPE;
3407 			sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3408 							   &sglq_entry->phys);
3409 			if (sglq_entry->virt == NULL) {
3410 				kfree(sglq_entry);
3411 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3412 						"2563 Failure to allocate an "
3413 						"ELS mbuf:%d\n", i);
3414 				rc = -ENOMEM;
3415 				goto out_free_mem;
3416 			}
3417 			sglq_entry->sgl = sglq_entry->virt;
3418 			memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3419 			sglq_entry->state = SGL_FREED;
3420 			list_add_tail(&sglq_entry->list, &els_sgl_list);
3421 		}
3422 		spin_lock_irq(&phba->hbalock);
3423 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3424 		list_splice_init(&els_sgl_list,
3425 				 &phba->sli4_hba.lpfc_els_sgl_list);
3426 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3427 		spin_unlock_irq(&phba->hbalock);
3428 	} else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3429 		/* els xri-sgl shrinked */
3430 		xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3431 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3432 				"3158 ELS xri-sgl count decreased from "
3433 				"%d to %d\n", phba->sli4_hba.els_xri_cnt,
3434 				els_xri_cnt);
3435 		spin_lock_irq(&phba->hbalock);
3436 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3437 		list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3438 				 &els_sgl_list);
3439 		/* release extra els sgls from list */
3440 		for (i = 0; i < xri_cnt; i++) {
3441 			list_remove_head(&els_sgl_list,
3442 					 sglq_entry, struct lpfc_sglq, list);
3443 			if (sglq_entry) {
3444 				__lpfc_mbuf_free(phba, sglq_entry->virt,
3445 						 sglq_entry->phys);
3446 				kfree(sglq_entry);
3447 			}
3448 		}
3449 		list_splice_init(&els_sgl_list,
3450 				 &phba->sli4_hba.lpfc_els_sgl_list);
3451 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3452 		spin_unlock_irq(&phba->hbalock);
3453 	} else
3454 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3455 				"3163 ELS xri-sgl count unchanged: %d\n",
3456 				els_xri_cnt);
3457 	phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3458 
3459 	/* update xris to els sgls on the list */
3460 	sglq_entry = NULL;
3461 	sglq_entry_next = NULL;
3462 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3463 				 &phba->sli4_hba.lpfc_els_sgl_list, list) {
3464 		lxri = lpfc_sli4_next_xritag(phba);
3465 		if (lxri == NO_XRI) {
3466 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3467 					"2400 Failed to allocate xri for "
3468 					"ELS sgl\n");
3469 			rc = -ENOMEM;
3470 			goto out_free_mem;
3471 		}
3472 		sglq_entry->sli4_lxritag = lxri;
3473 		sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3474 	}
3475 	return 0;
3476 
3477 out_free_mem:
3478 	lpfc_free_els_sgl_list(phba);
3479 	return rc;
3480 }
3481 
3482 /**
3483  * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3484  * @phba: pointer to lpfc hba data structure.
3485  *
3486  * This routine first calculates the sizes of the current els and allocated
3487  * scsi sgl lists, and then goes through all sgls to updates the physical
3488  * XRIs assigned due to port function reset. During port initialization, the
3489  * current els and allocated scsi sgl lists are 0s.
3490  *
3491  * Return codes
3492  *   0 - successful (for now, it always returns 0)
3493  **/
3494 int
lpfc_sli4_nvmet_sgl_update(struct lpfc_hba * phba)3495 lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3496 {
3497 	struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3498 	uint16_t i, lxri, xri_cnt, els_xri_cnt;
3499 	uint16_t nvmet_xri_cnt;
3500 	LIST_HEAD(nvmet_sgl_list);
3501 	int rc;
3502 
3503 	/*
3504 	 * update on pci function's nvmet xri-sgl list
3505 	 */
3506 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3507 
3508 	/* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3509 	nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3510 	if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3511 		/* els xri-sgl expanded */
3512 		xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3513 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3514 				"6302 NVMET xri-sgl cnt grew from %d to %d\n",
3515 				phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3516 		/* allocate the additional nvmet sgls */
3517 		for (i = 0; i < xri_cnt; i++) {
3518 			sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3519 					     GFP_KERNEL);
3520 			if (sglq_entry == NULL) {
3521 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3522 						"6303 Failure to allocate an "
3523 						"NVMET sgl entry:%d\n", i);
3524 				rc = -ENOMEM;
3525 				goto out_free_mem;
3526 			}
3527 			sglq_entry->buff_type = NVMET_BUFF_TYPE;
3528 			sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3529 							   &sglq_entry->phys);
3530 			if (sglq_entry->virt == NULL) {
3531 				kfree(sglq_entry);
3532 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3533 						"6304 Failure to allocate an "
3534 						"NVMET buf:%d\n", i);
3535 				rc = -ENOMEM;
3536 				goto out_free_mem;
3537 			}
3538 			sglq_entry->sgl = sglq_entry->virt;
3539 			memset(sglq_entry->sgl, 0,
3540 			       phba->cfg_sg_dma_buf_size);
3541 			sglq_entry->state = SGL_FREED;
3542 			list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3543 		}
3544 		spin_lock_irq(&phba->hbalock);
3545 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3546 		list_splice_init(&nvmet_sgl_list,
3547 				 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3548 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3549 		spin_unlock_irq(&phba->hbalock);
3550 	} else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3551 		/* nvmet xri-sgl shrunk */
3552 		xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3553 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3554 				"6305 NVMET xri-sgl count decreased from "
3555 				"%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3556 				nvmet_xri_cnt);
3557 		spin_lock_irq(&phba->hbalock);
3558 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3559 		list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3560 				 &nvmet_sgl_list);
3561 		/* release extra nvmet sgls from list */
3562 		for (i = 0; i < xri_cnt; i++) {
3563 			list_remove_head(&nvmet_sgl_list,
3564 					 sglq_entry, struct lpfc_sglq, list);
3565 			if (sglq_entry) {
3566 				lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3567 						    sglq_entry->phys);
3568 				kfree(sglq_entry);
3569 			}
3570 		}
3571 		list_splice_init(&nvmet_sgl_list,
3572 				 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3573 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3574 		spin_unlock_irq(&phba->hbalock);
3575 	} else
3576 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3577 				"6306 NVMET xri-sgl count unchanged: %d\n",
3578 				nvmet_xri_cnt);
3579 	phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3580 
3581 	/* update xris to nvmet sgls on the list */
3582 	sglq_entry = NULL;
3583 	sglq_entry_next = NULL;
3584 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3585 				 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3586 		lxri = lpfc_sli4_next_xritag(phba);
3587 		if (lxri == NO_XRI) {
3588 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3589 					"6307 Failed to allocate xri for "
3590 					"NVMET sgl\n");
3591 			rc = -ENOMEM;
3592 			goto out_free_mem;
3593 		}
3594 		sglq_entry->sli4_lxritag = lxri;
3595 		sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3596 	}
3597 	return 0;
3598 
3599 out_free_mem:
3600 	lpfc_free_nvmet_sgl_list(phba);
3601 	return rc;
3602 }
3603 
3604 /**
3605  * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3606  * @phba: pointer to lpfc hba data structure.
3607  *
3608  * This routine first calculates the sizes of the current els and allocated
3609  * scsi sgl lists, and then goes through all sgls to updates the physical
3610  * XRIs assigned due to port function reset. During port initialization, the
3611  * current els and allocated scsi sgl lists are 0s.
3612  *
3613  * Return codes
3614  *   0 - successful (for now, it always returns 0)
3615  **/
3616 int
lpfc_sli4_scsi_sgl_update(struct lpfc_hba * phba)3617 lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3618 {
3619 	struct lpfc_scsi_buf *psb, *psb_next;
3620 	uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3621 	LIST_HEAD(scsi_sgl_list);
3622 	int rc;
3623 
3624 	/*
3625 	 * update on pci function's els xri-sgl list
3626 	 */
3627 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3628 	phba->total_scsi_bufs = 0;
3629 
3630 	/*
3631 	 * update on pci function's allocated scsi xri-sgl list
3632 	 */
3633 	/* maximum number of xris available for scsi buffers */
3634 	phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3635 				      els_xri_cnt;
3636 
3637 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3638 		return 0;
3639 
3640 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3641 		phba->sli4_hba.scsi_xri_max =  /* Split them up */
3642 			(phba->sli4_hba.scsi_xri_max *
3643 			 phba->cfg_xri_split) / 100;
3644 
3645 	spin_lock_irq(&phba->scsi_buf_list_get_lock);
3646 	spin_lock(&phba->scsi_buf_list_put_lock);
3647 	list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3648 	list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
3649 	spin_unlock(&phba->scsi_buf_list_put_lock);
3650 	spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3651 
3652 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3653 			"6060 Current allocated SCSI xri-sgl count:%d, "
3654 			"maximum  SCSI xri count:%d (split:%d)\n",
3655 			phba->sli4_hba.scsi_xri_cnt,
3656 			phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3657 
3658 	if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3659 		/* max scsi xri shrinked below the allocated scsi buffers */
3660 		scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3661 					phba->sli4_hba.scsi_xri_max;
3662 		/* release the extra allocated scsi buffers */
3663 		for (i = 0; i < scsi_xri_cnt; i++) {
3664 			list_remove_head(&scsi_sgl_list, psb,
3665 					 struct lpfc_scsi_buf, list);
3666 			if (psb) {
3667 				dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3668 					      psb->data, psb->dma_handle);
3669 				kfree(psb);
3670 			}
3671 		}
3672 		spin_lock_irq(&phba->scsi_buf_list_get_lock);
3673 		phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
3674 		spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3675 	}
3676 
3677 	/* update xris associated to remaining allocated scsi buffers */
3678 	psb = NULL;
3679 	psb_next = NULL;
3680 	list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3681 		lxri = lpfc_sli4_next_xritag(phba);
3682 		if (lxri == NO_XRI) {
3683 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3684 					"2560 Failed to allocate xri for "
3685 					"scsi buffer\n");
3686 			rc = -ENOMEM;
3687 			goto out_free_mem;
3688 		}
3689 		psb->cur_iocbq.sli4_lxritag = lxri;
3690 		psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3691 	}
3692 	spin_lock_irq(&phba->scsi_buf_list_get_lock);
3693 	spin_lock(&phba->scsi_buf_list_put_lock);
3694 	list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3695 	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
3696 	spin_unlock(&phba->scsi_buf_list_put_lock);
3697 	spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3698 	return 0;
3699 
3700 out_free_mem:
3701 	lpfc_scsi_free(phba);
3702 	return rc;
3703 }
3704 
3705 static uint64_t
lpfc_get_wwpn(struct lpfc_hba * phba)3706 lpfc_get_wwpn(struct lpfc_hba *phba)
3707 {
3708 	uint64_t wwn;
3709 	int rc;
3710 	LPFC_MBOXQ_t *mboxq;
3711 	MAILBOX_t *mb;
3712 
3713 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3714 						GFP_KERNEL);
3715 	if (!mboxq)
3716 		return (uint64_t)-1;
3717 
3718 	/* First get WWN of HBA instance */
3719 	lpfc_read_nv(phba, mboxq);
3720 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3721 	if (rc != MBX_SUCCESS) {
3722 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3723 				"6019 Mailbox failed , mbxCmd x%x "
3724 				"READ_NV, mbxStatus x%x\n",
3725 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3726 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3727 		mempool_free(mboxq, phba->mbox_mem_pool);
3728 		return (uint64_t) -1;
3729 	}
3730 	mb = &mboxq->u.mb;
3731 	memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3732 	/* wwn is WWPN of HBA instance */
3733 	mempool_free(mboxq, phba->mbox_mem_pool);
3734 	if (phba->sli_rev == LPFC_SLI_REV4)
3735 		return be64_to_cpu(wwn);
3736 	else
3737 		return rol64(wwn, 32);
3738 }
3739 
3740 /**
3741  * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3742  * @phba: pointer to lpfc hba data structure.
3743  *
3744  * This routine first calculates the sizes of the current els and allocated
3745  * scsi sgl lists, and then goes through all sgls to updates the physical
3746  * XRIs assigned due to port function reset. During port initialization, the
3747  * current els and allocated scsi sgl lists are 0s.
3748  *
3749  * Return codes
3750  *   0 - successful (for now, it always returns 0)
3751  **/
3752 int
lpfc_sli4_nvme_sgl_update(struct lpfc_hba * phba)3753 lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3754 {
3755 	struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3756 	uint16_t i, lxri, els_xri_cnt;
3757 	uint16_t nvme_xri_cnt, nvme_xri_max;
3758 	LIST_HEAD(nvme_sgl_list);
3759 	int rc;
3760 
3761 	phba->total_nvme_bufs = 0;
3762 
3763 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3764 		return 0;
3765 	/*
3766 	 * update on pci function's allocated nvme xri-sgl list
3767 	 */
3768 
3769 	/* maximum number of xris available for nvme buffers */
3770 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3771 	nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3772 	phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3773 	phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3774 
3775 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3776 			"6074 Current allocated NVME xri-sgl count:%d, "
3777 			"maximum  NVME xri count:%d\n",
3778 			phba->sli4_hba.nvme_xri_cnt,
3779 			phba->sli4_hba.nvme_xri_max);
3780 
3781 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
3782 	spin_lock(&phba->nvme_buf_list_put_lock);
3783 	list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3784 	list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3785 	spin_unlock(&phba->nvme_buf_list_put_lock);
3786 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3787 
3788 	if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3789 		/* max nvme xri shrunk below the allocated nvme buffers */
3790 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
3791 		nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3792 					phba->sli4_hba.nvme_xri_max;
3793 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3794 		/* release the extra allocated nvme buffers */
3795 		for (i = 0; i < nvme_xri_cnt; i++) {
3796 			list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3797 					 struct lpfc_nvme_buf, list);
3798 			if (lpfc_ncmd) {
3799 				dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3800 					      lpfc_ncmd->data,
3801 					      lpfc_ncmd->dma_handle);
3802 				kfree(lpfc_ncmd);
3803 			}
3804 		}
3805 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
3806 		phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3807 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3808 	}
3809 
3810 	/* update xris associated to remaining allocated nvme buffers */
3811 	lpfc_ncmd = NULL;
3812 	lpfc_ncmd_next = NULL;
3813 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3814 				 &nvme_sgl_list, list) {
3815 		lxri = lpfc_sli4_next_xritag(phba);
3816 		if (lxri == NO_XRI) {
3817 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3818 					"6075 Failed to allocate xri for "
3819 					"nvme buffer\n");
3820 			rc = -ENOMEM;
3821 			goto out_free_mem;
3822 		}
3823 		lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3824 		lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3825 	}
3826 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
3827 	spin_lock(&phba->nvme_buf_list_put_lock);
3828 	list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3829 	INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3830 	spin_unlock(&phba->nvme_buf_list_put_lock);
3831 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3832 	return 0;
3833 
3834 out_free_mem:
3835 	lpfc_nvme_free(phba);
3836 	return rc;
3837 }
3838 
3839 /**
3840  * lpfc_create_port - Create an FC port
3841  * @phba: pointer to lpfc hba data structure.
3842  * @instance: a unique integer ID to this FC port.
3843  * @dev: pointer to the device data structure.
3844  *
3845  * This routine creates a FC port for the upper layer protocol. The FC port
3846  * can be created on top of either a physical port or a virtual port provided
3847  * by the HBA. This routine also allocates a SCSI host data structure (shost)
3848  * and associates the FC port created before adding the shost into the SCSI
3849  * layer.
3850  *
3851  * Return codes
3852  *   @vport - pointer to the virtual N_Port data structure.
3853  *   NULL - port create failed.
3854  **/
3855 struct lpfc_vport *
lpfc_create_port(struct lpfc_hba * phba,int instance,struct device * dev)3856 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
3857 {
3858 	struct lpfc_vport *vport;
3859 	struct Scsi_Host  *shost = NULL;
3860 	int error = 0;
3861 	int i;
3862 	uint64_t wwn;
3863 	bool use_no_reset_hba = false;
3864 	int rc;
3865 
3866 	if (lpfc_no_hba_reset_cnt) {
3867 		if (phba->sli_rev < LPFC_SLI_REV4 &&
3868 		    dev == &phba->pcidev->dev) {
3869 			/* Reset the port first */
3870 			lpfc_sli_brdrestart(phba);
3871 			rc = lpfc_sli_chipset_init(phba);
3872 			if (rc)
3873 				return NULL;
3874 		}
3875 		wwn = lpfc_get_wwpn(phba);
3876 	}
3877 
3878 	for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3879 		if (wwn == lpfc_no_hba_reset[i]) {
3880 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3881 					"6020 Setting use_no_reset port=%llx\n",
3882 					wwn);
3883 			use_no_reset_hba = true;
3884 			break;
3885 		}
3886 	}
3887 
3888 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3889 		if (dev != &phba->pcidev->dev) {
3890 			shost = scsi_host_alloc(&lpfc_vport_template,
3891 						sizeof(struct lpfc_vport));
3892 		} else {
3893 			if (!use_no_reset_hba)
3894 				shost = scsi_host_alloc(&lpfc_template,
3895 						sizeof(struct lpfc_vport));
3896 			else
3897 				shost = scsi_host_alloc(&lpfc_template_no_hr,
3898 						sizeof(struct lpfc_vport));
3899 		}
3900 	} else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3901 		shost = scsi_host_alloc(&lpfc_template_nvme,
3902 					sizeof(struct lpfc_vport));
3903 	}
3904 	if (!shost)
3905 		goto out;
3906 
3907 	vport = (struct lpfc_vport *) shost->hostdata;
3908 	vport->phba = phba;
3909 	vport->load_flag |= FC_LOADING;
3910 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3911 	vport->fc_rscn_flush = 0;
3912 	lpfc_get_vport_cfgparam(vport);
3913 
3914 	shost->unique_id = instance;
3915 	shost->max_id = LPFC_MAX_TARGET;
3916 	shost->max_lun = vport->cfg_max_luns;
3917 	shost->this_id = -1;
3918 	shost->max_cmd_len = 16;
3919 	shost->nr_hw_queues = phba->cfg_fcp_io_channel;
3920 	if (phba->sli_rev == LPFC_SLI_REV4) {
3921 		shost->dma_boundary =
3922 			phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
3923 		shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3924 	}
3925 
3926 	/*
3927 	 * Set initial can_queue value since 0 is no longer supported and
3928 	 * scsi_add_host will fail. This will be adjusted later based on the
3929 	 * max xri value determined in hba setup.
3930 	 */
3931 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
3932 	if (dev != &phba->pcidev->dev) {
3933 		shost->transportt = lpfc_vport_transport_template;
3934 		vport->port_type = LPFC_NPIV_PORT;
3935 	} else {
3936 		shost->transportt = lpfc_transport_template;
3937 		vport->port_type = LPFC_PHYSICAL_PORT;
3938 	}
3939 
3940 	/* Initialize all internally managed lists. */
3941 	INIT_LIST_HEAD(&vport->fc_nodes);
3942 	INIT_LIST_HEAD(&vport->rcv_buffer_list);
3943 	spin_lock_init(&vport->work_port_lock);
3944 
3945 	setup_timer(&vport->fc_disctmo, lpfc_disc_timeout,
3946 			(unsigned long)vport);
3947 
3948 	setup_timer(&vport->els_tmofunc, lpfc_els_timeout,
3949 			(unsigned long)vport);
3950 
3951 	setup_timer(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo,
3952 			(unsigned long)vport);
3953 
3954 	error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
3955 	if (error)
3956 		goto out_put_shost;
3957 
3958 	spin_lock_irq(&phba->hbalock);
3959 	list_add_tail(&vport->listentry, &phba->port_list);
3960 	spin_unlock_irq(&phba->hbalock);
3961 	return vport;
3962 
3963 out_put_shost:
3964 	scsi_host_put(shost);
3965 out:
3966 	return NULL;
3967 }
3968 
3969 /**
3970  * destroy_port -  destroy an FC port
3971  * @vport: pointer to an lpfc virtual N_Port data structure.
3972  *
3973  * This routine destroys a FC port from the upper layer protocol. All the
3974  * resources associated with the port are released.
3975  **/
3976 void
destroy_port(struct lpfc_vport * vport)3977 destroy_port(struct lpfc_vport *vport)
3978 {
3979 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3980 	struct lpfc_hba  *phba = vport->phba;
3981 
3982 	lpfc_debugfs_terminate(vport);
3983 	fc_remove_host(shost);
3984 	scsi_remove_host(shost);
3985 
3986 	spin_lock_irq(&phba->hbalock);
3987 	list_del_init(&vport->listentry);
3988 	spin_unlock_irq(&phba->hbalock);
3989 
3990 	lpfc_cleanup(vport);
3991 	return;
3992 }
3993 
3994 /**
3995  * lpfc_get_instance - Get a unique integer ID
3996  *
3997  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3998  * uses the kernel idr facility to perform the task.
3999  *
4000  * Return codes:
4001  *   instance - a unique integer ID allocated as the new instance.
4002  *   -1 - lpfc get instance failed.
4003  **/
4004 int
lpfc_get_instance(void)4005 lpfc_get_instance(void)
4006 {
4007 	int ret;
4008 
4009 	ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4010 	return ret < 0 ? -1 : ret;
4011 }
4012 
4013 /**
4014  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
4015  * @shost: pointer to SCSI host data structure.
4016  * @time: elapsed time of the scan in jiffies.
4017  *
4018  * This routine is called by the SCSI layer with a SCSI host to determine
4019  * whether the scan host is finished.
4020  *
4021  * Note: there is no scan_start function as adapter initialization will have
4022  * asynchronously kicked off the link initialization.
4023  *
4024  * Return codes
4025  *   0 - SCSI host scan is not over yet.
4026  *   1 - SCSI host scan is over.
4027  **/
lpfc_scan_finished(struct Scsi_Host * shost,unsigned long time)4028 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4029 {
4030 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4031 	struct lpfc_hba   *phba = vport->phba;
4032 	int stat = 0;
4033 
4034 	spin_lock_irq(shost->host_lock);
4035 
4036 	if (vport->load_flag & FC_UNLOADING) {
4037 		stat = 1;
4038 		goto finished;
4039 	}
4040 	if (time >= msecs_to_jiffies(30 * 1000)) {
4041 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4042 				"0461 Scanning longer than 30 "
4043 				"seconds.  Continuing initialization\n");
4044 		stat = 1;
4045 		goto finished;
4046 	}
4047 	if (time >= msecs_to_jiffies(15 * 1000) &&
4048 	    phba->link_state <= LPFC_LINK_DOWN) {
4049 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4050 				"0465 Link down longer than 15 "
4051 				"seconds.  Continuing initialization\n");
4052 		stat = 1;
4053 		goto finished;
4054 	}
4055 
4056 	if (vport->port_state != LPFC_VPORT_READY)
4057 		goto finished;
4058 	if (vport->num_disc_nodes || vport->fc_prli_sent)
4059 		goto finished;
4060 	if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
4061 		goto finished;
4062 	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
4063 		goto finished;
4064 
4065 	stat = 1;
4066 
4067 finished:
4068 	spin_unlock_irq(shost->host_lock);
4069 	return stat;
4070 }
4071 
4072 /**
4073  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
4074  * @shost: pointer to SCSI host data structure.
4075  *
4076  * This routine initializes a given SCSI host attributes on a FC port. The
4077  * SCSI host can be either on top of a physical port or a virtual port.
4078  **/
lpfc_host_attrib_init(struct Scsi_Host * shost)4079 void lpfc_host_attrib_init(struct Scsi_Host *shost)
4080 {
4081 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4082 	struct lpfc_hba   *phba = vport->phba;
4083 	/*
4084 	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
4085 	 */
4086 
4087 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4088 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4089 	fc_host_supported_classes(shost) = FC_COS_CLASS3;
4090 
4091 	memset(fc_host_supported_fc4s(shost), 0,
4092 	       sizeof(fc_host_supported_fc4s(shost)));
4093 	fc_host_supported_fc4s(shost)[2] = 1;
4094 	fc_host_supported_fc4s(shost)[7] = 1;
4095 
4096 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4097 				 sizeof fc_host_symbolic_name(shost));
4098 
4099 	fc_host_supported_speeds(shost) = 0;
4100 	if (phba->lmt & LMT_32Gb)
4101 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4102 	if (phba->lmt & LMT_16Gb)
4103 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4104 	if (phba->lmt & LMT_10Gb)
4105 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4106 	if (phba->lmt & LMT_8Gb)
4107 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4108 	if (phba->lmt & LMT_4Gb)
4109 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4110 	if (phba->lmt & LMT_2Gb)
4111 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4112 	if (phba->lmt & LMT_1Gb)
4113 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4114 
4115 	fc_host_maxframe_size(shost) =
4116 		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4117 		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
4118 
4119 	fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4120 
4121 	/* This value is also unchanging */
4122 	memset(fc_host_active_fc4s(shost), 0,
4123 	       sizeof(fc_host_active_fc4s(shost)));
4124 	fc_host_active_fc4s(shost)[2] = 1;
4125 	fc_host_active_fc4s(shost)[7] = 1;
4126 
4127 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
4128 	spin_lock_irq(shost->host_lock);
4129 	vport->load_flag &= ~FC_LOADING;
4130 	spin_unlock_irq(shost->host_lock);
4131 }
4132 
4133 /**
4134  * lpfc_stop_port_s3 - Stop SLI3 device port
4135  * @phba: pointer to lpfc hba data structure.
4136  *
4137  * This routine is invoked to stop an SLI3 device port, it stops the device
4138  * from generating interrupts and stops the device driver's timers for the
4139  * device.
4140  **/
4141 static void
lpfc_stop_port_s3(struct lpfc_hba * phba)4142 lpfc_stop_port_s3(struct lpfc_hba *phba)
4143 {
4144 	/* Clear all interrupt enable conditions */
4145 	writel(0, phba->HCregaddr);
4146 	readl(phba->HCregaddr); /* flush */
4147 	/* Clear all pending interrupts */
4148 	writel(0xffffffff, phba->HAregaddr);
4149 	readl(phba->HAregaddr); /* flush */
4150 
4151 	/* Reset some HBA SLI setup states */
4152 	lpfc_stop_hba_timers(phba);
4153 	phba->pport->work_port_events = 0;
4154 }
4155 
4156 /**
4157  * lpfc_stop_port_s4 - Stop SLI4 device port
4158  * @phba: pointer to lpfc hba data structure.
4159  *
4160  * This routine is invoked to stop an SLI4 device port, it stops the device
4161  * from generating interrupts and stops the device driver's timers for the
4162  * device.
4163  **/
4164 static void
lpfc_stop_port_s4(struct lpfc_hba * phba)4165 lpfc_stop_port_s4(struct lpfc_hba *phba)
4166 {
4167 	/* Reset some HBA SLI4 setup states */
4168 	lpfc_stop_hba_timers(phba);
4169 	phba->pport->work_port_events = 0;
4170 	phba->sli4_hba.intr_enable = 0;
4171 }
4172 
4173 /**
4174  * lpfc_stop_port - Wrapper function for stopping hba port
4175  * @phba: Pointer to HBA context object.
4176  *
4177  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4178  * the API jump table function pointer from the lpfc_hba struct.
4179  **/
4180 void
lpfc_stop_port(struct lpfc_hba * phba)4181 lpfc_stop_port(struct lpfc_hba *phba)
4182 {
4183 	phba->lpfc_stop_port(phba);
4184 }
4185 
4186 /**
4187  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4188  * @phba: Pointer to hba for which this call is being executed.
4189  *
4190  * This routine starts the timer waiting for the FCF rediscovery to complete.
4191  **/
4192 void
lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba * phba)4193 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4194 {
4195 	unsigned long fcf_redisc_wait_tmo =
4196 		(jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4197 	/* Start fcf rediscovery wait period timer */
4198 	mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4199 	spin_lock_irq(&phba->hbalock);
4200 	/* Allow action to new fcf asynchronous event */
4201 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4202 	/* Mark the FCF rediscovery pending state */
4203 	phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4204 	spin_unlock_irq(&phba->hbalock);
4205 }
4206 
4207 /**
4208  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4209  * @ptr: Map to lpfc_hba data structure pointer.
4210  *
4211  * This routine is invoked when waiting for FCF table rediscover has been
4212  * timed out. If new FCF record(s) has (have) been discovered during the
4213  * wait period, a new FCF event shall be added to the FCOE async event
4214  * list, and then worker thread shall be waked up for processing from the
4215  * worker thread context.
4216  **/
4217 static void
lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)4218 lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
4219 {
4220 	struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
4221 
4222 	/* Don't send FCF rediscovery event if timer cancelled */
4223 	spin_lock_irq(&phba->hbalock);
4224 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4225 		spin_unlock_irq(&phba->hbalock);
4226 		return;
4227 	}
4228 	/* Clear FCF rediscovery timer pending flag */
4229 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4230 	/* FCF rediscovery event to worker thread */
4231 	phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4232 	spin_unlock_irq(&phba->hbalock);
4233 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
4234 			"2776 FCF rediscover quiescent timer expired\n");
4235 	/* wake up worker thread */
4236 	lpfc_worker_wake_up(phba);
4237 }
4238 
4239 /**
4240  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4241  * @phba: pointer to lpfc hba data structure.
4242  * @acqe_link: pointer to the async link completion queue entry.
4243  *
4244  * This routine is to parse the SLI4 link-attention link fault code and
4245  * translate it into the base driver's read link attention mailbox command
4246  * status.
4247  *
4248  * Return: Link-attention status in terms of base driver's coding.
4249  **/
4250 static uint16_t
lpfc_sli4_parse_latt_fault(struct lpfc_hba * phba,struct lpfc_acqe_link * acqe_link)4251 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4252 			   struct lpfc_acqe_link *acqe_link)
4253 {
4254 	uint16_t latt_fault;
4255 
4256 	switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4257 	case LPFC_ASYNC_LINK_FAULT_NONE:
4258 	case LPFC_ASYNC_LINK_FAULT_LOCAL:
4259 	case LPFC_ASYNC_LINK_FAULT_REMOTE:
4260 		latt_fault = 0;
4261 		break;
4262 	default:
4263 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4264 				"0398 Invalid link fault code: x%x\n",
4265 				bf_get(lpfc_acqe_link_fault, acqe_link));
4266 		latt_fault = MBXERR_ERROR;
4267 		break;
4268 	}
4269 	return latt_fault;
4270 }
4271 
4272 /**
4273  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4274  * @phba: pointer to lpfc hba data structure.
4275  * @acqe_link: pointer to the async link completion queue entry.
4276  *
4277  * This routine is to parse the SLI4 link attention type and translate it
4278  * into the base driver's link attention type coding.
4279  *
4280  * Return: Link attention type in terms of base driver's coding.
4281  **/
4282 static uint8_t
lpfc_sli4_parse_latt_type(struct lpfc_hba * phba,struct lpfc_acqe_link * acqe_link)4283 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4284 			  struct lpfc_acqe_link *acqe_link)
4285 {
4286 	uint8_t att_type;
4287 
4288 	switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4289 	case LPFC_ASYNC_LINK_STATUS_DOWN:
4290 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
4291 		att_type = LPFC_ATT_LINK_DOWN;
4292 		break;
4293 	case LPFC_ASYNC_LINK_STATUS_UP:
4294 		/* Ignore physical link up events - wait for logical link up */
4295 		att_type = LPFC_ATT_RESERVED;
4296 		break;
4297 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
4298 		att_type = LPFC_ATT_LINK_UP;
4299 		break;
4300 	default:
4301 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4302 				"0399 Invalid link attention type: x%x\n",
4303 				bf_get(lpfc_acqe_link_status, acqe_link));
4304 		att_type = LPFC_ATT_RESERVED;
4305 		break;
4306 	}
4307 	return att_type;
4308 }
4309 
4310 /**
4311  * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4312  * @phba: pointer to lpfc hba data structure.
4313  *
4314  * This routine is to get an SLI3 FC port's link speed in Mbps.
4315  *
4316  * Return: link speed in terms of Mbps.
4317  **/
4318 uint32_t
lpfc_sli_port_speed_get(struct lpfc_hba * phba)4319 lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4320 {
4321 	uint32_t link_speed;
4322 
4323 	if (!lpfc_is_link_up(phba))
4324 		return 0;
4325 
4326 	if (phba->sli_rev <= LPFC_SLI_REV3) {
4327 		switch (phba->fc_linkspeed) {
4328 		case LPFC_LINK_SPEED_1GHZ:
4329 			link_speed = 1000;
4330 			break;
4331 		case LPFC_LINK_SPEED_2GHZ:
4332 			link_speed = 2000;
4333 			break;
4334 		case LPFC_LINK_SPEED_4GHZ:
4335 			link_speed = 4000;
4336 			break;
4337 		case LPFC_LINK_SPEED_8GHZ:
4338 			link_speed = 8000;
4339 			break;
4340 		case LPFC_LINK_SPEED_10GHZ:
4341 			link_speed = 10000;
4342 			break;
4343 		case LPFC_LINK_SPEED_16GHZ:
4344 			link_speed = 16000;
4345 			break;
4346 		default:
4347 			link_speed = 0;
4348 		}
4349 	} else {
4350 		if (phba->sli4_hba.link_state.logical_speed)
4351 			link_speed =
4352 			      phba->sli4_hba.link_state.logical_speed;
4353 		else
4354 			link_speed = phba->sli4_hba.link_state.speed;
4355 	}
4356 	return link_speed;
4357 }
4358 
4359 /**
4360  * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4361  * @phba: pointer to lpfc hba data structure.
4362  * @evt_code: asynchronous event code.
4363  * @speed_code: asynchronous event link speed code.
4364  *
4365  * This routine is to parse the giving SLI4 async event link speed code into
4366  * value of Mbps for the link speed.
4367  *
4368  * Return: link speed in terms of Mbps.
4369  **/
4370 static uint32_t
lpfc_sli4_port_speed_parse(struct lpfc_hba * phba,uint32_t evt_code,uint8_t speed_code)4371 lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4372 			   uint8_t speed_code)
4373 {
4374 	uint32_t port_speed;
4375 
4376 	switch (evt_code) {
4377 	case LPFC_TRAILER_CODE_LINK:
4378 		switch (speed_code) {
4379 		case LPFC_ASYNC_LINK_SPEED_ZERO:
4380 			port_speed = 0;
4381 			break;
4382 		case LPFC_ASYNC_LINK_SPEED_10MBPS:
4383 			port_speed = 10;
4384 			break;
4385 		case LPFC_ASYNC_LINK_SPEED_100MBPS:
4386 			port_speed = 100;
4387 			break;
4388 		case LPFC_ASYNC_LINK_SPEED_1GBPS:
4389 			port_speed = 1000;
4390 			break;
4391 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
4392 			port_speed = 10000;
4393 			break;
4394 		case LPFC_ASYNC_LINK_SPEED_20GBPS:
4395 			port_speed = 20000;
4396 			break;
4397 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
4398 			port_speed = 25000;
4399 			break;
4400 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
4401 			port_speed = 40000;
4402 			break;
4403 		default:
4404 			port_speed = 0;
4405 		}
4406 		break;
4407 	case LPFC_TRAILER_CODE_FC:
4408 		switch (speed_code) {
4409 		case LPFC_FC_LA_SPEED_UNKNOWN:
4410 			port_speed = 0;
4411 			break;
4412 		case LPFC_FC_LA_SPEED_1G:
4413 			port_speed = 1000;
4414 			break;
4415 		case LPFC_FC_LA_SPEED_2G:
4416 			port_speed = 2000;
4417 			break;
4418 		case LPFC_FC_LA_SPEED_4G:
4419 			port_speed = 4000;
4420 			break;
4421 		case LPFC_FC_LA_SPEED_8G:
4422 			port_speed = 8000;
4423 			break;
4424 		case LPFC_FC_LA_SPEED_10G:
4425 			port_speed = 10000;
4426 			break;
4427 		case LPFC_FC_LA_SPEED_16G:
4428 			port_speed = 16000;
4429 			break;
4430 		case LPFC_FC_LA_SPEED_32G:
4431 			port_speed = 32000;
4432 			break;
4433 		default:
4434 			port_speed = 0;
4435 		}
4436 		break;
4437 	default:
4438 		port_speed = 0;
4439 	}
4440 	return port_speed;
4441 }
4442 
4443 /**
4444  * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
4445  * @phba: pointer to lpfc hba data structure.
4446  * @acqe_link: pointer to the async link completion queue entry.
4447  *
4448  * This routine is to handle the SLI4 asynchronous FCoE link event.
4449  **/
4450 static void
lpfc_sli4_async_link_evt(struct lpfc_hba * phba,struct lpfc_acqe_link * acqe_link)4451 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4452 			 struct lpfc_acqe_link *acqe_link)
4453 {
4454 	struct lpfc_dmabuf *mp;
4455 	LPFC_MBOXQ_t *pmb;
4456 	MAILBOX_t *mb;
4457 	struct lpfc_mbx_read_top *la;
4458 	uint8_t att_type;
4459 	int rc;
4460 
4461 	att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
4462 	if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
4463 		return;
4464 	phba->fcoe_eventtag = acqe_link->event_tag;
4465 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4466 	if (!pmb) {
4467 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4468 				"0395 The mboxq allocation failed\n");
4469 		return;
4470 	}
4471 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4472 	if (!mp) {
4473 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4474 				"0396 The lpfc_dmabuf allocation failed\n");
4475 		goto out_free_pmb;
4476 	}
4477 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4478 	if (!mp->virt) {
4479 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4480 				"0397 The mbuf allocation failed\n");
4481 		goto out_free_dmabuf;
4482 	}
4483 
4484 	/* Cleanup any outstanding ELS commands */
4485 	lpfc_els_flush_all_cmd(phba);
4486 
4487 	/* Block ELS IOCBs until we have done process link event */
4488 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4489 
4490 	/* Update link event statistics */
4491 	phba->sli.slistat.link_event++;
4492 
4493 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4494 	lpfc_read_topology(phba, pmb, mp);
4495 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4496 	pmb->vport = phba->pport;
4497 
4498 	/* Keep the link status for extra SLI4 state machine reference */
4499 	phba->sli4_hba.link_state.speed =
4500 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4501 				bf_get(lpfc_acqe_link_speed, acqe_link));
4502 	phba->sli4_hba.link_state.duplex =
4503 				bf_get(lpfc_acqe_link_duplex, acqe_link);
4504 	phba->sli4_hba.link_state.status =
4505 				bf_get(lpfc_acqe_link_status, acqe_link);
4506 	phba->sli4_hba.link_state.type =
4507 				bf_get(lpfc_acqe_link_type, acqe_link);
4508 	phba->sli4_hba.link_state.number =
4509 				bf_get(lpfc_acqe_link_number, acqe_link);
4510 	phba->sli4_hba.link_state.fault =
4511 				bf_get(lpfc_acqe_link_fault, acqe_link);
4512 	phba->sli4_hba.link_state.logical_speed =
4513 			bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4514 
4515 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4516 			"2900 Async FC/FCoE Link event - Speed:%dGBit "
4517 			"duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4518 			"Logical speed:%dMbps Fault:%d\n",
4519 			phba->sli4_hba.link_state.speed,
4520 			phba->sli4_hba.link_state.topology,
4521 			phba->sli4_hba.link_state.status,
4522 			phba->sli4_hba.link_state.type,
4523 			phba->sli4_hba.link_state.number,
4524 			phba->sli4_hba.link_state.logical_speed,
4525 			phba->sli4_hba.link_state.fault);
4526 	/*
4527 	 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4528 	 * topology info. Note: Optional for non FC-AL ports.
4529 	 */
4530 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4531 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4532 		if (rc == MBX_NOT_FINISHED)
4533 			goto out_free_dmabuf;
4534 		return;
4535 	}
4536 	/*
4537 	 * For FCoE Mode: fill in all the topology information we need and call
4538 	 * the READ_TOPOLOGY completion routine to continue without actually
4539 	 * sending the READ_TOPOLOGY mailbox command to the port.
4540 	 */
4541 	/* Parse and translate status field */
4542 	mb = &pmb->u.mb;
4543 	mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4544 
4545 	/* Parse and translate link attention fields */
4546 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4547 	la->eventTag = acqe_link->event_tag;
4548 	bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4549 	bf_set(lpfc_mbx_read_top_link_spd, la,
4550 	       (bf_get(lpfc_acqe_link_speed, acqe_link)));
4551 
4552 	/* Fake the the following irrelvant fields */
4553 	bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4554 	bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4555 	bf_set(lpfc_mbx_read_top_il, la, 0);
4556 	bf_set(lpfc_mbx_read_top_pb, la, 0);
4557 	bf_set(lpfc_mbx_read_top_fa, la, 0);
4558 	bf_set(lpfc_mbx_read_top_mm, la, 0);
4559 
4560 	/* Invoke the lpfc_handle_latt mailbox command callback function */
4561 	lpfc_mbx_cmpl_read_topology(phba, pmb);
4562 
4563 	return;
4564 
4565 out_free_dmabuf:
4566 	kfree(mp);
4567 out_free_pmb:
4568 	mempool_free(pmb, phba->mbox_mem_pool);
4569 }
4570 
4571 /**
4572  * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4573  * @phba: pointer to lpfc hba data structure.
4574  * @acqe_fc: pointer to the async fc completion queue entry.
4575  *
4576  * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4577  * that the event was received and then issue a read_topology mailbox command so
4578  * that the rest of the driver will treat it the same as SLI3.
4579  **/
4580 static void
lpfc_sli4_async_fc_evt(struct lpfc_hba * phba,struct lpfc_acqe_fc_la * acqe_fc)4581 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4582 {
4583 	struct lpfc_dmabuf *mp;
4584 	LPFC_MBOXQ_t *pmb;
4585 	MAILBOX_t *mb;
4586 	struct lpfc_mbx_read_top *la;
4587 	int rc;
4588 
4589 	if (bf_get(lpfc_trailer_type, acqe_fc) !=
4590 	    LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4591 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4592 				"2895 Non FC link Event detected.(%d)\n",
4593 				bf_get(lpfc_trailer_type, acqe_fc));
4594 		return;
4595 	}
4596 	/* Keep the link status for extra SLI4 state machine reference */
4597 	phba->sli4_hba.link_state.speed =
4598 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4599 				bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
4600 	phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4601 	phba->sli4_hba.link_state.topology =
4602 				bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4603 	phba->sli4_hba.link_state.status =
4604 				bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4605 	phba->sli4_hba.link_state.type =
4606 				bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4607 	phba->sli4_hba.link_state.number =
4608 				bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4609 	phba->sli4_hba.link_state.fault =
4610 				bf_get(lpfc_acqe_link_fault, acqe_fc);
4611 	phba->sli4_hba.link_state.logical_speed =
4612 				bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
4613 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4614 			"2896 Async FC event - Speed:%dGBaud Topology:x%x "
4615 			"LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4616 			"%dMbps Fault:%d\n",
4617 			phba->sli4_hba.link_state.speed,
4618 			phba->sli4_hba.link_state.topology,
4619 			phba->sli4_hba.link_state.status,
4620 			phba->sli4_hba.link_state.type,
4621 			phba->sli4_hba.link_state.number,
4622 			phba->sli4_hba.link_state.logical_speed,
4623 			phba->sli4_hba.link_state.fault);
4624 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4625 	if (!pmb) {
4626 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4627 				"2897 The mboxq allocation failed\n");
4628 		return;
4629 	}
4630 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4631 	if (!mp) {
4632 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4633 				"2898 The lpfc_dmabuf allocation failed\n");
4634 		goto out_free_pmb;
4635 	}
4636 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4637 	if (!mp->virt) {
4638 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4639 				"2899 The mbuf allocation failed\n");
4640 		goto out_free_dmabuf;
4641 	}
4642 
4643 	/* Cleanup any outstanding ELS commands */
4644 	lpfc_els_flush_all_cmd(phba);
4645 
4646 	/* Block ELS IOCBs until we have done process link event */
4647 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4648 
4649 	/* Update link event statistics */
4650 	phba->sli.slistat.link_event++;
4651 
4652 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4653 	lpfc_read_topology(phba, pmb, mp);
4654 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4655 	pmb->vport = phba->pport;
4656 
4657 	if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4658 		phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
4659 
4660 		switch (phba->sli4_hba.link_state.status) {
4661 		case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
4662 			phba->link_flag |= LS_MDS_LINK_DOWN;
4663 			break;
4664 		case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
4665 			phba->link_flag |= LS_MDS_LOOPBACK;
4666 			break;
4667 		default:
4668 			break;
4669 		}
4670 
4671 		/* Parse and translate status field */
4672 		mb = &pmb->u.mb;
4673 		mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4674 							   (void *)acqe_fc);
4675 
4676 		/* Parse and translate link attention fields */
4677 		la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4678 		la->eventTag = acqe_fc->event_tag;
4679 
4680 		if (phba->sli4_hba.link_state.status ==
4681 		    LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4682 			bf_set(lpfc_mbx_read_top_att_type, la,
4683 			       LPFC_FC_LA_TYPE_UNEXP_WWPN);
4684 		} else {
4685 			bf_set(lpfc_mbx_read_top_att_type, la,
4686 			       LPFC_FC_LA_TYPE_LINK_DOWN);
4687 		}
4688 		/* Invoke the mailbox command callback function */
4689 		lpfc_mbx_cmpl_read_topology(phba, pmb);
4690 
4691 		return;
4692 	}
4693 
4694 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4695 	if (rc == MBX_NOT_FINISHED)
4696 		goto out_free_dmabuf;
4697 	return;
4698 
4699 out_free_dmabuf:
4700 	kfree(mp);
4701 out_free_pmb:
4702 	mempool_free(pmb, phba->mbox_mem_pool);
4703 }
4704 
4705 /**
4706  * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4707  * @phba: pointer to lpfc hba data structure.
4708  * @acqe_fc: pointer to the async SLI completion queue entry.
4709  *
4710  * This routine is to handle the SLI4 asynchronous SLI events.
4711  **/
4712 static void
lpfc_sli4_async_sli_evt(struct lpfc_hba * phba,struct lpfc_acqe_sli * acqe_sli)4713 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4714 {
4715 	char port_name;
4716 	char message[128];
4717 	uint8_t status;
4718 	uint8_t evt_type;
4719 	uint8_t operational = 0;
4720 	struct temp_event temp_event_data;
4721 	struct lpfc_acqe_misconfigured_event *misconfigured;
4722 	struct Scsi_Host  *shost;
4723 
4724 	evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4725 
4726 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4727 			"2901 Async SLI event - Event Data1:x%08x Event Data2:"
4728 			"x%08x SLI Event Type:%d\n",
4729 			acqe_sli->event_data1, acqe_sli->event_data2,
4730 			evt_type);
4731 
4732 	port_name = phba->Port[0];
4733 	if (port_name == 0x00)
4734 		port_name = '?'; /* get port name is empty */
4735 
4736 	switch (evt_type) {
4737 	case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4738 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4739 		temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4740 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4741 
4742 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4743 				"3190 Over Temperature:%d Celsius- Port Name %c\n",
4744 				acqe_sli->event_data1, port_name);
4745 
4746 		phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
4747 		shost = lpfc_shost_from_vport(phba->pport);
4748 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4749 					  sizeof(temp_event_data),
4750 					  (char *)&temp_event_data,
4751 					  SCSI_NL_VID_TYPE_PCI
4752 					  | PCI_VENDOR_ID_EMULEX);
4753 		break;
4754 	case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4755 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4756 		temp_event_data.event_code = LPFC_NORMAL_TEMP;
4757 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4758 
4759 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4760 				"3191 Normal Temperature:%d Celsius - Port Name %c\n",
4761 				acqe_sli->event_data1, port_name);
4762 
4763 		shost = lpfc_shost_from_vport(phba->pport);
4764 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4765 					  sizeof(temp_event_data),
4766 					  (char *)&temp_event_data,
4767 					  SCSI_NL_VID_TYPE_PCI
4768 					  | PCI_VENDOR_ID_EMULEX);
4769 		break;
4770 	case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4771 		misconfigured = (struct lpfc_acqe_misconfigured_event *)
4772 					&acqe_sli->event_data1;
4773 
4774 		/* fetch the status for this port */
4775 		switch (phba->sli4_hba.lnk_info.lnk_no) {
4776 		case LPFC_LINK_NUMBER_0:
4777 			status = bf_get(lpfc_sli_misconfigured_port0_state,
4778 					&misconfigured->theEvent);
4779 			operational = bf_get(lpfc_sli_misconfigured_port0_op,
4780 					&misconfigured->theEvent);
4781 			break;
4782 		case LPFC_LINK_NUMBER_1:
4783 			status = bf_get(lpfc_sli_misconfigured_port1_state,
4784 					&misconfigured->theEvent);
4785 			operational = bf_get(lpfc_sli_misconfigured_port1_op,
4786 					&misconfigured->theEvent);
4787 			break;
4788 		case LPFC_LINK_NUMBER_2:
4789 			status = bf_get(lpfc_sli_misconfigured_port2_state,
4790 					&misconfigured->theEvent);
4791 			operational = bf_get(lpfc_sli_misconfigured_port2_op,
4792 					&misconfigured->theEvent);
4793 			break;
4794 		case LPFC_LINK_NUMBER_3:
4795 			status = bf_get(lpfc_sli_misconfigured_port3_state,
4796 					&misconfigured->theEvent);
4797 			operational = bf_get(lpfc_sli_misconfigured_port3_op,
4798 					&misconfigured->theEvent);
4799 			break;
4800 		default:
4801 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4802 					"3296 "
4803 					"LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4804 					"event: Invalid link %d",
4805 					phba->sli4_hba.lnk_info.lnk_no);
4806 			return;
4807 		}
4808 
4809 		/* Skip if optic state unchanged */
4810 		if (phba->sli4_hba.lnk_info.optic_state == status)
4811 			return;
4812 
4813 		switch (status) {
4814 		case LPFC_SLI_EVENT_STATUS_VALID:
4815 			sprintf(message, "Physical Link is functional");
4816 			break;
4817 		case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4818 			sprintf(message, "Optics faulted/incorrectly "
4819 				"installed/not installed - Reseat optics, "
4820 				"if issue not resolved, replace.");
4821 			break;
4822 		case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4823 			sprintf(message,
4824 				"Optics of two types installed - Remove one "
4825 				"optic or install matching pair of optics.");
4826 			break;
4827 		case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4828 			sprintf(message, "Incompatible optics - Replace with "
4829 				"compatible optics for card to function.");
4830 			break;
4831 		case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4832 			sprintf(message, "Unqualified optics - Replace with "
4833 				"Avago optics for Warranty and Technical "
4834 				"Support - Link is%s operational",
4835 				(operational) ? " not" : "");
4836 			break;
4837 		case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4838 			sprintf(message, "Uncertified optics - Replace with "
4839 				"Avago-certified optics to enable link "
4840 				"operation - Link is%s operational",
4841 				(operational) ? " not" : "");
4842 			break;
4843 		default:
4844 			/* firmware is reporting a status we don't know about */
4845 			sprintf(message, "Unknown event status x%02x", status);
4846 			break;
4847 		}
4848 		phba->sli4_hba.lnk_info.optic_state = status;
4849 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4850 				"3176 Port Name %c %s\n", port_name, message);
4851 		break;
4852 	case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4853 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4854 				"3192 Remote DPort Test Initiated - "
4855 				"Event Data1:x%08x Event Data2: x%08x\n",
4856 				acqe_sli->event_data1, acqe_sli->event_data2);
4857 		break;
4858 	default:
4859 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4860 				"3193 Async SLI event - Event Data1:x%08x Event Data2:"
4861 				"x%08x SLI Event Type:%d\n",
4862 				acqe_sli->event_data1, acqe_sli->event_data2,
4863 				evt_type);
4864 		break;
4865 	}
4866 }
4867 
4868 /**
4869  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4870  * @vport: pointer to vport data structure.
4871  *
4872  * This routine is to perform Clear Virtual Link (CVL) on a vport in
4873  * response to a CVL event.
4874  *
4875  * Return the pointer to the ndlp with the vport if successful, otherwise
4876  * return NULL.
4877  **/
4878 static struct lpfc_nodelist *
lpfc_sli4_perform_vport_cvl(struct lpfc_vport * vport)4879 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4880 {
4881 	struct lpfc_nodelist *ndlp;
4882 	struct Scsi_Host *shost;
4883 	struct lpfc_hba *phba;
4884 
4885 	if (!vport)
4886 		return NULL;
4887 	phba = vport->phba;
4888 	if (!phba)
4889 		return NULL;
4890 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
4891 	if (!ndlp) {
4892 		/* Cannot find existing Fabric ndlp, so allocate a new one */
4893 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
4894 		if (!ndlp)
4895 			return 0;
4896 		/* Set the node type */
4897 		ndlp->nlp_type |= NLP_FABRIC;
4898 		/* Put ndlp onto node list */
4899 		lpfc_enqueue_node(vport, ndlp);
4900 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
4901 		/* re-setup ndlp without removing from node list */
4902 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4903 		if (!ndlp)
4904 			return 0;
4905 	}
4906 	if ((phba->pport->port_state < LPFC_FLOGI) &&
4907 		(phba->pport->port_state != LPFC_VPORT_FAILED))
4908 		return NULL;
4909 	/* If virtual link is not yet instantiated ignore CVL */
4910 	if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4911 		&& (vport->port_state != LPFC_VPORT_FAILED))
4912 		return NULL;
4913 	shost = lpfc_shost_from_vport(vport);
4914 	if (!shost)
4915 		return NULL;
4916 	lpfc_linkdown_port(vport);
4917 	lpfc_cleanup_pending_mbox(vport);
4918 	spin_lock_irq(shost->host_lock);
4919 	vport->fc_flag |= FC_VPORT_CVL_RCVD;
4920 	spin_unlock_irq(shost->host_lock);
4921 
4922 	return ndlp;
4923 }
4924 
4925 /**
4926  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4927  * @vport: pointer to lpfc hba data structure.
4928  *
4929  * This routine is to perform Clear Virtual Link (CVL) on all vports in
4930  * response to a FCF dead event.
4931  **/
4932 static void
lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba * phba)4933 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4934 {
4935 	struct lpfc_vport **vports;
4936 	int i;
4937 
4938 	vports = lpfc_create_vport_work_array(phba);
4939 	if (vports)
4940 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4941 			lpfc_sli4_perform_vport_cvl(vports[i]);
4942 	lpfc_destroy_vport_work_array(phba, vports);
4943 }
4944 
4945 /**
4946  * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
4947  * @phba: pointer to lpfc hba data structure.
4948  * @acqe_link: pointer to the async fcoe completion queue entry.
4949  *
4950  * This routine is to handle the SLI4 asynchronous fcoe event.
4951  **/
4952 static void
lpfc_sli4_async_fip_evt(struct lpfc_hba * phba,struct lpfc_acqe_fip * acqe_fip)4953 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
4954 			struct lpfc_acqe_fip *acqe_fip)
4955 {
4956 	uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
4957 	int rc;
4958 	struct lpfc_vport *vport;
4959 	struct lpfc_nodelist *ndlp;
4960 	struct Scsi_Host  *shost;
4961 	int active_vlink_present;
4962 	struct lpfc_vport **vports;
4963 	int i;
4964 
4965 	phba->fc_eventTag = acqe_fip->event_tag;
4966 	phba->fcoe_eventtag = acqe_fip->event_tag;
4967 	switch (event_type) {
4968 	case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4969 	case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4970 		if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
4971 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4972 					LOG_DISCOVERY,
4973 					"2546 New FCF event, evt_tag:x%x, "
4974 					"index:x%x\n",
4975 					acqe_fip->event_tag,
4976 					acqe_fip->index);
4977 		else
4978 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4979 					LOG_DISCOVERY,
4980 					"2788 FCF param modified event, "
4981 					"evt_tag:x%x, index:x%x\n",
4982 					acqe_fip->event_tag,
4983 					acqe_fip->index);
4984 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4985 			/*
4986 			 * During period of FCF discovery, read the FCF
4987 			 * table record indexed by the event to update
4988 			 * FCF roundrobin failover eligible FCF bmask.
4989 			 */
4990 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4991 					LOG_DISCOVERY,
4992 					"2779 Read FCF (x%x) for updating "
4993 					"roundrobin FCF failover bmask\n",
4994 					acqe_fip->index);
4995 			rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
4996 		}
4997 
4998 		/* If the FCF discovery is in progress, do nothing. */
4999 		spin_lock_irq(&phba->hbalock);
5000 		if (phba->hba_flag & FCF_TS_INPROG) {
5001 			spin_unlock_irq(&phba->hbalock);
5002 			break;
5003 		}
5004 		/* If fast FCF failover rescan event is pending, do nothing */
5005 		if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
5006 			spin_unlock_irq(&phba->hbalock);
5007 			break;
5008 		}
5009 
5010 		/* If the FCF has been in discovered state, do nothing. */
5011 		if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
5012 			spin_unlock_irq(&phba->hbalock);
5013 			break;
5014 		}
5015 		spin_unlock_irq(&phba->hbalock);
5016 
5017 		/* Otherwise, scan the entire FCF table and re-discover SAN */
5018 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5019 				"2770 Start FCF table scan per async FCF "
5020 				"event, evt_tag:x%x, index:x%x\n",
5021 				acqe_fip->event_tag, acqe_fip->index);
5022 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5023 						     LPFC_FCOE_FCF_GET_FIRST);
5024 		if (rc)
5025 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5026 					"2547 Issue FCF scan read FCF mailbox "
5027 					"command failed (x%x)\n", rc);
5028 		break;
5029 
5030 	case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
5031 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5032 			"2548 FCF Table full count 0x%x tag 0x%x\n",
5033 			bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5034 			acqe_fip->event_tag);
5035 		break;
5036 
5037 	case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
5038 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5039 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5040 			"2549 FCF (x%x) disconnected from network, "
5041 			"tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
5042 		/*
5043 		 * If we are in the middle of FCF failover process, clear
5044 		 * the corresponding FCF bit in the roundrobin bitmap.
5045 		 */
5046 		spin_lock_irq(&phba->hbalock);
5047 		if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5048 		    (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
5049 			spin_unlock_irq(&phba->hbalock);
5050 			/* Update FLOGI FCF failover eligible FCF bmask */
5051 			lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
5052 			break;
5053 		}
5054 		spin_unlock_irq(&phba->hbalock);
5055 
5056 		/* If the event is not for currently used fcf do nothing */
5057 		if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
5058 			break;
5059 
5060 		/*
5061 		 * Otherwise, request the port to rediscover the entire FCF
5062 		 * table for a fast recovery from case that the current FCF
5063 		 * is no longer valid as we are not in the middle of FCF
5064 		 * failover process already.
5065 		 */
5066 		spin_lock_irq(&phba->hbalock);
5067 		/* Mark the fast failover process in progress */
5068 		phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5069 		spin_unlock_irq(&phba->hbalock);
5070 
5071 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5072 				"2771 Start FCF fast failover process due to "
5073 				"FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5074 				"\n", acqe_fip->event_tag, acqe_fip->index);
5075 		rc = lpfc_sli4_redisc_fcf_table(phba);
5076 		if (rc) {
5077 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5078 					LOG_DISCOVERY,
5079 					"2772 Issue FCF rediscover mabilbox "
5080 					"command failed, fail through to FCF "
5081 					"dead event\n");
5082 			spin_lock_irq(&phba->hbalock);
5083 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5084 			spin_unlock_irq(&phba->hbalock);
5085 			/*
5086 			 * Last resort will fail over by treating this
5087 			 * as a link down to FCF registration.
5088 			 */
5089 			lpfc_sli4_fcf_dead_failthrough(phba);
5090 		} else {
5091 			/* Reset FCF roundrobin bmask for new discovery */
5092 			lpfc_sli4_clear_fcf_rr_bmask(phba);
5093 			/*
5094 			 * Handling fast FCF failover to a DEAD FCF event is
5095 			 * considered equalivant to receiving CVL to all vports.
5096 			 */
5097 			lpfc_sli4_perform_all_vport_cvl(phba);
5098 		}
5099 		break;
5100 	case LPFC_FIP_EVENT_TYPE_CVL:
5101 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5102 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5103 			"2718 Clear Virtual Link Received for VPI 0x%x"
5104 			" tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
5105 
5106 		vport = lpfc_find_vport_by_vpid(phba,
5107 						acqe_fip->index);
5108 		ndlp = lpfc_sli4_perform_vport_cvl(vport);
5109 		if (!ndlp)
5110 			break;
5111 		active_vlink_present = 0;
5112 
5113 		vports = lpfc_create_vport_work_array(phba);
5114 		if (vports) {
5115 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5116 					i++) {
5117 				if ((!(vports[i]->fc_flag &
5118 					FC_VPORT_CVL_RCVD)) &&
5119 					(vports[i]->port_state > LPFC_FDISC)) {
5120 					active_vlink_present = 1;
5121 					break;
5122 				}
5123 			}
5124 			lpfc_destroy_vport_work_array(phba, vports);
5125 		}
5126 
5127 		/*
5128 		 * Don't re-instantiate if vport is marked for deletion.
5129 		 * If we are here first then vport_delete is going to wait
5130 		 * for discovery to complete.
5131 		 */
5132 		if (!(vport->load_flag & FC_UNLOADING) &&
5133 					active_vlink_present) {
5134 			/*
5135 			 * If there are other active VLinks present,
5136 			 * re-instantiate the Vlink using FDISC.
5137 			 */
5138 			mod_timer(&ndlp->nlp_delayfunc,
5139 				  jiffies + msecs_to_jiffies(1000));
5140 			shost = lpfc_shost_from_vport(vport);
5141 			spin_lock_irq(shost->host_lock);
5142 			ndlp->nlp_flag |= NLP_DELAY_TMO;
5143 			spin_unlock_irq(shost->host_lock);
5144 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5145 			vport->port_state = LPFC_FDISC;
5146 		} else {
5147 			/*
5148 			 * Otherwise, we request port to rediscover
5149 			 * the entire FCF table for a fast recovery
5150 			 * from possible case that the current FCF
5151 			 * is no longer valid if we are not already
5152 			 * in the FCF failover process.
5153 			 */
5154 			spin_lock_irq(&phba->hbalock);
5155 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5156 				spin_unlock_irq(&phba->hbalock);
5157 				break;
5158 			}
5159 			/* Mark the fast failover process in progress */
5160 			phba->fcf.fcf_flag |= FCF_ACVL_DISC;
5161 			spin_unlock_irq(&phba->hbalock);
5162 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5163 					LOG_DISCOVERY,
5164 					"2773 Start FCF failover per CVL, "
5165 					"evt_tag:x%x\n", acqe_fip->event_tag);
5166 			rc = lpfc_sli4_redisc_fcf_table(phba);
5167 			if (rc) {
5168 				lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5169 						LOG_DISCOVERY,
5170 						"2774 Issue FCF rediscover "
5171 						"mabilbox command failed, "
5172 						"through to CVL event\n");
5173 				spin_lock_irq(&phba->hbalock);
5174 				phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
5175 				spin_unlock_irq(&phba->hbalock);
5176 				/*
5177 				 * Last resort will be re-try on the
5178 				 * the current registered FCF entry.
5179 				 */
5180 				lpfc_retry_pport_discovery(phba);
5181 			} else
5182 				/*
5183 				 * Reset FCF roundrobin bmask for new
5184 				 * discovery.
5185 				 */
5186 				lpfc_sli4_clear_fcf_rr_bmask(phba);
5187 		}
5188 		break;
5189 	default:
5190 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5191 			"0288 Unknown FCoE event type 0x%x event tag "
5192 			"0x%x\n", event_type, acqe_fip->event_tag);
5193 		break;
5194 	}
5195 }
5196 
5197 /**
5198  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5199  * @phba: pointer to lpfc hba data structure.
5200  * @acqe_link: pointer to the async dcbx completion queue entry.
5201  *
5202  * This routine is to handle the SLI4 asynchronous dcbx event.
5203  **/
5204 static void
lpfc_sli4_async_dcbx_evt(struct lpfc_hba * phba,struct lpfc_acqe_dcbx * acqe_dcbx)5205 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5206 			 struct lpfc_acqe_dcbx *acqe_dcbx)
5207 {
5208 	phba->fc_eventTag = acqe_dcbx->event_tag;
5209 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5210 			"0290 The SLI4 DCBX asynchronous event is not "
5211 			"handled yet\n");
5212 }
5213 
5214 /**
5215  * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5216  * @phba: pointer to lpfc hba data structure.
5217  * @acqe_link: pointer to the async grp5 completion queue entry.
5218  *
5219  * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5220  * is an asynchronous notified of a logical link speed change.  The Port
5221  * reports the logical link speed in units of 10Mbps.
5222  **/
5223 static void
lpfc_sli4_async_grp5_evt(struct lpfc_hba * phba,struct lpfc_acqe_grp5 * acqe_grp5)5224 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5225 			 struct lpfc_acqe_grp5 *acqe_grp5)
5226 {
5227 	uint16_t prev_ll_spd;
5228 
5229 	phba->fc_eventTag = acqe_grp5->event_tag;
5230 	phba->fcoe_eventtag = acqe_grp5->event_tag;
5231 	prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5232 	phba->sli4_hba.link_state.logical_speed =
5233 		(bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
5234 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5235 			"2789 GRP5 Async Event: Updating logical link speed "
5236 			"from %dMbps to %dMbps\n", prev_ll_spd,
5237 			phba->sli4_hba.link_state.logical_speed);
5238 }
5239 
5240 /**
5241  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5242  * @phba: pointer to lpfc hba data structure.
5243  *
5244  * This routine is invoked by the worker thread to process all the pending
5245  * SLI4 asynchronous events.
5246  **/
lpfc_sli4_async_event_proc(struct lpfc_hba * phba)5247 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5248 {
5249 	struct lpfc_cq_event *cq_event;
5250 
5251 	/* First, declare the async event has been handled */
5252 	spin_lock_irq(&phba->hbalock);
5253 	phba->hba_flag &= ~ASYNC_EVENT;
5254 	spin_unlock_irq(&phba->hbalock);
5255 	/* Now, handle all the async events */
5256 	while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5257 		/* Get the first event from the head of the event queue */
5258 		spin_lock_irq(&phba->hbalock);
5259 		list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5260 				 cq_event, struct lpfc_cq_event, list);
5261 		spin_unlock_irq(&phba->hbalock);
5262 		/* Process the asynchronous event */
5263 		switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5264 		case LPFC_TRAILER_CODE_LINK:
5265 			lpfc_sli4_async_link_evt(phba,
5266 						 &cq_event->cqe.acqe_link);
5267 			break;
5268 		case LPFC_TRAILER_CODE_FCOE:
5269 			lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
5270 			break;
5271 		case LPFC_TRAILER_CODE_DCBX:
5272 			lpfc_sli4_async_dcbx_evt(phba,
5273 						 &cq_event->cqe.acqe_dcbx);
5274 			break;
5275 		case LPFC_TRAILER_CODE_GRP5:
5276 			lpfc_sli4_async_grp5_evt(phba,
5277 						 &cq_event->cqe.acqe_grp5);
5278 			break;
5279 		case LPFC_TRAILER_CODE_FC:
5280 			lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5281 			break;
5282 		case LPFC_TRAILER_CODE_SLI:
5283 			lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5284 			break;
5285 		default:
5286 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5287 					"1804 Invalid asynchrous event code: "
5288 					"x%x\n", bf_get(lpfc_trailer_code,
5289 					&cq_event->cqe.mcqe_cmpl));
5290 			break;
5291 		}
5292 		/* Free the completion event processed to the free pool */
5293 		lpfc_sli4_cq_event_release(phba, cq_event);
5294 	}
5295 }
5296 
5297 /**
5298  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5299  * @phba: pointer to lpfc hba data structure.
5300  *
5301  * This routine is invoked by the worker thread to process FCF table
5302  * rediscovery pending completion event.
5303  **/
lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba * phba)5304 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5305 {
5306 	int rc;
5307 
5308 	spin_lock_irq(&phba->hbalock);
5309 	/* Clear FCF rediscovery timeout event */
5310 	phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5311 	/* Clear driver fast failover FCF record flag */
5312 	phba->fcf.failover_rec.flag = 0;
5313 	/* Set state for FCF fast failover */
5314 	phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5315 	spin_unlock_irq(&phba->hbalock);
5316 
5317 	/* Scan FCF table from the first entry to re-discover SAN */
5318 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5319 			"2777 Start post-quiescent FCF table scan\n");
5320 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
5321 	if (rc)
5322 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5323 				"2747 Issue FCF scan read FCF mailbox "
5324 				"command failed 0x%x\n", rc);
5325 }
5326 
5327 /**
5328  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5329  * @phba: pointer to lpfc hba data structure.
5330  * @dev_grp: The HBA PCI-Device group number.
5331  *
5332  * This routine is invoked to set up the per HBA PCI-Device group function
5333  * API jump table entries.
5334  *
5335  * Return: 0 if success, otherwise -ENODEV
5336  **/
5337 int
lpfc_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)5338 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5339 {
5340 	int rc;
5341 
5342 	/* Set up lpfc PCI-device group */
5343 	phba->pci_dev_grp = dev_grp;
5344 
5345 	/* The LPFC_PCI_DEV_OC uses SLI4 */
5346 	if (dev_grp == LPFC_PCI_DEV_OC)
5347 		phba->sli_rev = LPFC_SLI_REV4;
5348 
5349 	/* Set up device INIT API function jump table */
5350 	rc = lpfc_init_api_table_setup(phba, dev_grp);
5351 	if (rc)
5352 		return -ENODEV;
5353 	/* Set up SCSI API function jump table */
5354 	rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5355 	if (rc)
5356 		return -ENODEV;
5357 	/* Set up SLI API function jump table */
5358 	rc = lpfc_sli_api_table_setup(phba, dev_grp);
5359 	if (rc)
5360 		return -ENODEV;
5361 	/* Set up MBOX API function jump table */
5362 	rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5363 	if (rc)
5364 		return -ENODEV;
5365 
5366 	return 0;
5367 }
5368 
5369 /**
5370  * lpfc_log_intr_mode - Log the active interrupt mode
5371  * @phba: pointer to lpfc hba data structure.
5372  * @intr_mode: active interrupt mode adopted.
5373  *
5374  * This routine it invoked to log the currently used active interrupt mode
5375  * to the device.
5376  **/
lpfc_log_intr_mode(struct lpfc_hba * phba,uint32_t intr_mode)5377 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5378 {
5379 	switch (intr_mode) {
5380 	case 0:
5381 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5382 				"0470 Enable INTx interrupt mode.\n");
5383 		break;
5384 	case 1:
5385 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5386 				"0481 Enabled MSI interrupt mode.\n");
5387 		break;
5388 	case 2:
5389 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5390 				"0480 Enabled MSI-X interrupt mode.\n");
5391 		break;
5392 	default:
5393 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5394 				"0482 Illegal interrupt mode.\n");
5395 		break;
5396 	}
5397 	return;
5398 }
5399 
5400 /**
5401  * lpfc_enable_pci_dev - Enable a generic PCI device.
5402  * @phba: pointer to lpfc hba data structure.
5403  *
5404  * This routine is invoked to enable the PCI device that is common to all
5405  * PCI devices.
5406  *
5407  * Return codes
5408  * 	0 - successful
5409  * 	other values - error
5410  **/
5411 static int
lpfc_enable_pci_dev(struct lpfc_hba * phba)5412 lpfc_enable_pci_dev(struct lpfc_hba *phba)
5413 {
5414 	struct pci_dev *pdev;
5415 
5416 	/* Obtain PCI device reference */
5417 	if (!phba->pcidev)
5418 		goto out_error;
5419 	else
5420 		pdev = phba->pcidev;
5421 	/* Enable PCI device */
5422 	if (pci_enable_device_mem(pdev))
5423 		goto out_error;
5424 	/* Request PCI resource for the device */
5425 	if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
5426 		goto out_disable_device;
5427 	/* Set up device as PCI master and save state for EEH */
5428 	pci_set_master(pdev);
5429 	pci_try_set_mwi(pdev);
5430 	pci_save_state(pdev);
5431 
5432 	/* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5433 	if (pci_is_pcie(pdev))
5434 		pdev->needs_freset = 1;
5435 
5436 	return 0;
5437 
5438 out_disable_device:
5439 	pci_disable_device(pdev);
5440 out_error:
5441 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5442 			"1401 Failed to enable pci device\n");
5443 	return -ENODEV;
5444 }
5445 
5446 /**
5447  * lpfc_disable_pci_dev - Disable a generic PCI device.
5448  * @phba: pointer to lpfc hba data structure.
5449  *
5450  * This routine is invoked to disable the PCI device that is common to all
5451  * PCI devices.
5452  **/
5453 static void
lpfc_disable_pci_dev(struct lpfc_hba * phba)5454 lpfc_disable_pci_dev(struct lpfc_hba *phba)
5455 {
5456 	struct pci_dev *pdev;
5457 
5458 	/* Obtain PCI device reference */
5459 	if (!phba->pcidev)
5460 		return;
5461 	else
5462 		pdev = phba->pcidev;
5463 	/* Release PCI resource and disable PCI device */
5464 	pci_release_mem_regions(pdev);
5465 	pci_disable_device(pdev);
5466 
5467 	return;
5468 }
5469 
5470 /**
5471  * lpfc_reset_hba - Reset a hba
5472  * @phba: pointer to lpfc hba data structure.
5473  *
5474  * This routine is invoked to reset a hba device. It brings the HBA
5475  * offline, performs a board restart, and then brings the board back
5476  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5477  * on outstanding mailbox commands.
5478  **/
5479 void
lpfc_reset_hba(struct lpfc_hba * phba)5480 lpfc_reset_hba(struct lpfc_hba *phba)
5481 {
5482 	/* If resets are disabled then set error state and return. */
5483 	if (!phba->cfg_enable_hba_reset) {
5484 		phba->link_state = LPFC_HBA_ERROR;
5485 		return;
5486 	}
5487 	if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5488 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5489 	else
5490 		lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
5491 	lpfc_offline(phba);
5492 	lpfc_sli_brdrestart(phba);
5493 	lpfc_online(phba);
5494 	lpfc_unblock_mgmt_io(phba);
5495 }
5496 
5497 /**
5498  * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5499  * @phba: pointer to lpfc hba data structure.
5500  *
5501  * This function enables the PCI SR-IOV virtual functions to a physical
5502  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5503  * enable the number of virtual functions to the physical function. As
5504  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5505  * API call does not considered as an error condition for most of the device.
5506  **/
5507 uint16_t
lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba * phba)5508 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5509 {
5510 	struct pci_dev *pdev = phba->pcidev;
5511 	uint16_t nr_virtfn;
5512 	int pos;
5513 
5514 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5515 	if (pos == 0)
5516 		return 0;
5517 
5518 	pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5519 	return nr_virtfn;
5520 }
5521 
5522 /**
5523  * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5524  * @phba: pointer to lpfc hba data structure.
5525  * @nr_vfn: number of virtual functions to be enabled.
5526  *
5527  * This function enables the PCI SR-IOV virtual functions to a physical
5528  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5529  * enable the number of virtual functions to the physical function. As
5530  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5531  * API call does not considered as an error condition for most of the device.
5532  **/
5533 int
lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba * phba,int nr_vfn)5534 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5535 {
5536 	struct pci_dev *pdev = phba->pcidev;
5537 	uint16_t max_nr_vfn;
5538 	int rc;
5539 
5540 	max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5541 	if (nr_vfn > max_nr_vfn) {
5542 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5543 				"3057 Requested vfs (%d) greater than "
5544 				"supported vfs (%d)", nr_vfn, max_nr_vfn);
5545 		return -EINVAL;
5546 	}
5547 
5548 	rc = pci_enable_sriov(pdev, nr_vfn);
5549 	if (rc) {
5550 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5551 				"2806 Failed to enable sriov on this device "
5552 				"with vfn number nr_vf:%d, rc:%d\n",
5553 				nr_vfn, rc);
5554 	} else
5555 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5556 				"2807 Successful enable sriov on this device "
5557 				"with vfn number nr_vf:%d\n", nr_vfn);
5558 	return rc;
5559 }
5560 
5561 /**
5562  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5563  * @phba: pointer to lpfc hba data structure.
5564  *
5565  * This routine is invoked to set up the driver internal resources before the
5566  * device specific resource setup to support the HBA device it attached to.
5567  *
5568  * Return codes
5569  *	0 - successful
5570  *	other values - error
5571  **/
5572 static int
lpfc_setup_driver_resource_phase1(struct lpfc_hba * phba)5573 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5574 {
5575 	struct lpfc_sli *psli = &phba->sli;
5576 
5577 	/*
5578 	 * Driver resources common to all SLI revisions
5579 	 */
5580 	atomic_set(&phba->fast_event_count, 0);
5581 	spin_lock_init(&phba->hbalock);
5582 
5583 	/* Initialize ndlp management spinlock */
5584 	spin_lock_init(&phba->ndlp_lock);
5585 
5586 	INIT_LIST_HEAD(&phba->port_list);
5587 	INIT_LIST_HEAD(&phba->work_list);
5588 	init_waitqueue_head(&phba->wait_4_mlo_m_q);
5589 
5590 	/* Initialize the wait queue head for the kernel thread */
5591 	init_waitqueue_head(&phba->work_waitq);
5592 
5593 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5594 			"1403 Protocols supported %s %s %s\n",
5595 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5596 				"SCSI" : " "),
5597 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
5598 				"NVME" : " "),
5599 			(phba->nvmet_support ? "NVMET" : " "));
5600 
5601 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5602 		/* Initialize the scsi buffer list used by driver for scsi IO */
5603 		spin_lock_init(&phba->scsi_buf_list_get_lock);
5604 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5605 		spin_lock_init(&phba->scsi_buf_list_put_lock);
5606 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5607 	}
5608 
5609 	if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5610 		(phba->nvmet_support == 0)) {
5611 		/* Initialize the NVME buffer list used by driver for NVME IO */
5612 		spin_lock_init(&phba->nvme_buf_list_get_lock);
5613 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5614 		spin_lock_init(&phba->nvme_buf_list_put_lock);
5615 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5616 	}
5617 
5618 	/* Initialize the fabric iocb list */
5619 	INIT_LIST_HEAD(&phba->fabric_iocb_list);
5620 
5621 	/* Initialize list to save ELS buffers */
5622 	INIT_LIST_HEAD(&phba->elsbuf);
5623 
5624 	/* Initialize FCF connection rec list */
5625 	INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5626 
5627 	/* Initialize OAS configuration list */
5628 	spin_lock_init(&phba->devicelock);
5629 	INIT_LIST_HEAD(&phba->luns);
5630 
5631 	/* MBOX heartbeat timer */
5632 	setup_timer(&psli->mbox_tmo, lpfc_mbox_timeout, (unsigned long)phba);
5633 	/* Fabric block timer */
5634 	setup_timer(&phba->fabric_block_timer, lpfc_fabric_block_timeout,
5635 			(unsigned long)phba);
5636 	/* EA polling mode timer */
5637 	setup_timer(&phba->eratt_poll, lpfc_poll_eratt,
5638 			(unsigned long)phba);
5639 	/* Heartbeat timer */
5640 	setup_timer(&phba->hb_tmofunc, lpfc_hb_timeout, (unsigned long)phba);
5641 
5642 	return 0;
5643 }
5644 
5645 /**
5646  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5647  * @phba: pointer to lpfc hba data structure.
5648  *
5649  * This routine is invoked to set up the driver internal resources specific to
5650  * support the SLI-3 HBA device it attached to.
5651  *
5652  * Return codes
5653  * 0 - successful
5654  * other values - error
5655  **/
5656 static int
lpfc_sli_driver_resource_setup(struct lpfc_hba * phba)5657 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5658 {
5659 	int rc;
5660 
5661 	/*
5662 	 * Initialize timers used by driver
5663 	 */
5664 
5665 	/* FCP polling mode timer */
5666 	setup_timer(&phba->fcp_poll_timer, lpfc_poll_timeout,
5667 			(unsigned long)phba);
5668 
5669 	/* Host attention work mask setup */
5670 	phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5671 	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5672 
5673 	/* Get all the module params for configuring this host */
5674 	lpfc_get_cfgparam(phba);
5675 	/* Set up phase-1 common device driver resources */
5676 
5677 	rc = lpfc_setup_driver_resource_phase1(phba);
5678 	if (rc)
5679 		return -ENODEV;
5680 
5681 	if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5682 		phba->menlo_flag |= HBA_MENLO_SUPPORT;
5683 		/* check for menlo minimum sg count */
5684 		if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5685 			phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5686 	}
5687 
5688 	if (!phba->sli.sli3_ring)
5689 		phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
5690 			sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5691 	if (!phba->sli.sli3_ring)
5692 		return -ENOMEM;
5693 
5694 	/*
5695 	 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
5696 	 * used to create the sg_dma_buf_pool must be dynamically calculated.
5697 	 */
5698 
5699 	/* Initialize the host templates the configured values. */
5700 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5701 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5702 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5703 
5704 	/* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5705 	if (phba->cfg_enable_bg) {
5706 		/*
5707 		 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5708 		 * the FCP rsp, and a BDE for each. Sice we have no control
5709 		 * over how many protection data segments the SCSI Layer
5710 		 * will hand us (ie: there could be one for every block
5711 		 * in the IO), we just allocate enough BDEs to accomidate
5712 		 * our max amount and we need to limit lpfc_sg_seg_cnt to
5713 		 * minimize the risk of running out.
5714 		 */
5715 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5716 			sizeof(struct fcp_rsp) +
5717 			(LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5718 
5719 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5720 			phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5721 
5722 		/* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5723 		phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5724 	} else {
5725 		/*
5726 		 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5727 		 * the FCP rsp, a BDE for each, and a BDE for up to
5728 		 * cfg_sg_seg_cnt data segments.
5729 		 */
5730 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5731 			sizeof(struct fcp_rsp) +
5732 			((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5733 
5734 		/* Total BDEs in BPL for scsi_sg_list */
5735 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5736 	}
5737 
5738 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5739 			"9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5740 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5741 			phba->cfg_total_seg_cnt);
5742 
5743 	phba->max_vpi = LPFC_MAX_VPI;
5744 	/* This will be set to correct value after config_port mbox */
5745 	phba->max_vports = 0;
5746 
5747 	/*
5748 	 * Initialize the SLI Layer to run with lpfc HBAs.
5749 	 */
5750 	lpfc_sli_setup(phba);
5751 	lpfc_sli_queue_init(phba);
5752 
5753 	/* Allocate device driver memory */
5754 	if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5755 		return -ENOMEM;
5756 
5757 	/*
5758 	 * Enable sr-iov virtual functions if supported and configured
5759 	 * through the module parameter.
5760 	 */
5761 	if (phba->cfg_sriov_nr_virtfn > 0) {
5762 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5763 						 phba->cfg_sriov_nr_virtfn);
5764 		if (rc) {
5765 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5766 					"2808 Requested number of SR-IOV "
5767 					"virtual functions (%d) is not "
5768 					"supported\n",
5769 					phba->cfg_sriov_nr_virtfn);
5770 			phba->cfg_sriov_nr_virtfn = 0;
5771 		}
5772 	}
5773 
5774 	return 0;
5775 }
5776 
5777 /**
5778  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5779  * @phba: pointer to lpfc hba data structure.
5780  *
5781  * This routine is invoked to unset the driver internal resources set up
5782  * specific for supporting the SLI-3 HBA device it attached to.
5783  **/
5784 static void
lpfc_sli_driver_resource_unset(struct lpfc_hba * phba)5785 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5786 {
5787 	/* Free device driver memory allocated */
5788 	lpfc_mem_free_all(phba);
5789 
5790 	return;
5791 }
5792 
5793 /**
5794  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5795  * @phba: pointer to lpfc hba data structure.
5796  *
5797  * This routine is invoked to set up the driver internal resources specific to
5798  * support the SLI-4 HBA device it attached to.
5799  *
5800  * Return codes
5801  * 	0 - successful
5802  * 	other values - error
5803  **/
5804 static int
lpfc_sli4_driver_resource_setup(struct lpfc_hba * phba)5805 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5806 {
5807 	LPFC_MBOXQ_t *mboxq;
5808 	MAILBOX_t *mb;
5809 	int rc, i, max_buf_size;
5810 	uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5811 	struct lpfc_mqe *mqe;
5812 	int longs;
5813 	int fof_vectors = 0;
5814 	uint64_t wwn;
5815 
5816 	phba->sli4_hba.num_online_cpu = num_online_cpus();
5817 	phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5818 	phba->sli4_hba.curr_disp_cpu = 0;
5819 
5820 	/* Get all the module params for configuring this host */
5821 	lpfc_get_cfgparam(phba);
5822 
5823 	/* Set up phase-1 common device driver resources */
5824 	rc = lpfc_setup_driver_resource_phase1(phba);
5825 	if (rc)
5826 		return -ENODEV;
5827 
5828 	/* Before proceed, wait for POST done and device ready */
5829 	rc = lpfc_sli4_post_status_check(phba);
5830 	if (rc)
5831 		return -ENODEV;
5832 
5833 	/*
5834 	 * Initialize timers used by driver
5835 	 */
5836 
5837 	setup_timer(&phba->rrq_tmr, lpfc_rrq_timeout, (unsigned long)phba);
5838 
5839 	/* FCF rediscover timer */
5840 	setup_timer(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo,
5841 			(unsigned long)phba);
5842 
5843 	/*
5844 	 * Control structure for handling external multi-buffer mailbox
5845 	 * command pass-through.
5846 	 */
5847 	memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5848 		sizeof(struct lpfc_mbox_ext_buf_ctx));
5849 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5850 
5851 	phba->max_vpi = LPFC_MAX_VPI;
5852 
5853 	/* This will be set to correct value after the read_config mbox */
5854 	phba->max_vports = 0;
5855 
5856 	/* Program the default value of vlan_id and fc_map */
5857 	phba->valid_vlan = 0;
5858 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5859 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5860 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5861 
5862 	/*
5863 	 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5864 	 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5865 	 * The WQ create will allocate the ring.
5866 	 */
5867 
5868 	/*
5869 	 * It doesn't matter what family our adapter is in, we are
5870 	 * limited to 2 Pages, 512 SGEs, for our SGL.
5871 	 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5872 	 */
5873 	max_buf_size = (2 * SLI4_PAGE_SIZE);
5874 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5875 		phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
5876 
5877 	/*
5878 	 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5879 	 * used to create the sg_dma_buf_pool must be calculated.
5880 	 */
5881 	if (phba->cfg_enable_bg) {
5882 		/*
5883 		 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5884 		 * the FCP rsp, and a SGE. Sice we have no control
5885 		 * over how many protection segments the SCSI Layer
5886 		 * will hand us (ie: there could be one for every block
5887 		 * in the IO), just allocate enough SGEs to accomidate
5888 		 * our max amount and we need to limit lpfc_sg_seg_cnt
5889 		 * to minimize the risk of running out.
5890 		 */
5891 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5892 				sizeof(struct fcp_rsp) + max_buf_size;
5893 
5894 		/* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5895 		phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5896 
5897 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5898 			phba->cfg_sg_seg_cnt =
5899 				LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5900 	} else {
5901 		/*
5902 		 * The scsi_buf for a regular I/O holds the FCP cmnd,
5903 		 * the FCP rsp, a SGE for each, and a SGE for up to
5904 		 * cfg_sg_seg_cnt data segments.
5905 		 */
5906 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5907 				sizeof(struct fcp_rsp) +
5908 				((phba->cfg_sg_seg_cnt + 2) *
5909 				sizeof(struct sli4_sge));
5910 
5911 		/* Total SGEs for scsi_sg_list */
5912 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5913 
5914 		/*
5915 		 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only
5916 		 * need to post 1 page for the SGL.
5917 		 */
5918 	}
5919 
5920 	/* Initialize the host templates with the updated values. */
5921 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5922 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5923 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5924 
5925 	if (phba->cfg_sg_dma_buf_size  <= LPFC_MIN_SG_SLI4_BUF_SZ)
5926 		phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5927 	else
5928 		phba->cfg_sg_dma_buf_size =
5929 			SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5930 
5931 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5932 			"9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5933 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5934 			phba->cfg_total_seg_cnt);
5935 
5936 	/* Initialize buffer queue management fields */
5937 	INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
5938 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5939 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5940 
5941 	/*
5942 	 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5943 	 */
5944 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5945 		/* Initialize the Abort scsi buffer list used by driver */
5946 		spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5947 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5948 	}
5949 
5950 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5951 		/* Initialize the Abort nvme buffer list used by driver */
5952 		spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5953 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
5954 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
5955 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
5956 
5957 		/* Fast-path XRI aborted CQ Event work queue list */
5958 		INIT_LIST_HEAD(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
5959 	}
5960 
5961 	/* This abort list used by worker thread */
5962 	spin_lock_init(&phba->sli4_hba.sgl_list_lock);
5963 	spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
5964 
5965 	/*
5966 	 * Initialize driver internal slow-path work queues
5967 	 */
5968 
5969 	/* Driver internel slow-path CQ Event pool */
5970 	INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5971 	/* Response IOCB work queue list */
5972 	INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
5973 	/* Asynchronous event CQ Event work queue list */
5974 	INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5975 	/* Fast-path XRI aborted CQ Event work queue list */
5976 	INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5977 	/* Slow-path XRI aborted CQ Event work queue list */
5978 	INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5979 	/* Receive queue CQ Event work queue list */
5980 	INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5981 
5982 	/* Initialize extent block lists. */
5983 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5984 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5985 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5986 	INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5987 
5988 	/* Initialize mboxq lists. If the early init routines fail
5989 	 * these lists need to be correctly initialized.
5990 	 */
5991 	INIT_LIST_HEAD(&phba->sli.mboxq);
5992 	INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
5993 
5994 	/* initialize optic_state to 0xFF */
5995 	phba->sli4_hba.lnk_info.optic_state = 0xff;
5996 
5997 	/* Allocate device driver memory */
5998 	rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5999 	if (rc)
6000 		return -ENOMEM;
6001 
6002 	/* IF Type 2 ports get initialized now. */
6003 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6004 	    LPFC_SLI_INTF_IF_TYPE_2) {
6005 		rc = lpfc_pci_function_reset(phba);
6006 		if (unlikely(rc)) {
6007 			rc = -ENODEV;
6008 			goto out_free_mem;
6009 		}
6010 		phba->temp_sensor_support = 1;
6011 	}
6012 
6013 	/* Create the bootstrap mailbox command */
6014 	rc = lpfc_create_bootstrap_mbox(phba);
6015 	if (unlikely(rc))
6016 		goto out_free_mem;
6017 
6018 	/* Set up the host's endian order with the device. */
6019 	rc = lpfc_setup_endian_order(phba);
6020 	if (unlikely(rc))
6021 		goto out_free_bsmbx;
6022 
6023 	/* Set up the hba's configuration parameters. */
6024 	rc = lpfc_sli4_read_config(phba);
6025 	if (unlikely(rc))
6026 		goto out_free_bsmbx;
6027 	rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6028 	if (unlikely(rc))
6029 		goto out_free_bsmbx;
6030 
6031 	/* IF Type 0 ports get initialized now. */
6032 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6033 	    LPFC_SLI_INTF_IF_TYPE_0) {
6034 		rc = lpfc_pci_function_reset(phba);
6035 		if (unlikely(rc))
6036 			goto out_free_bsmbx;
6037 	}
6038 
6039 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6040 						       GFP_KERNEL);
6041 	if (!mboxq) {
6042 		rc = -ENOMEM;
6043 		goto out_free_bsmbx;
6044 	}
6045 
6046 	/* Check for NVMET being configured */
6047 	phba->nvmet_support = 0;
6048 	if (lpfc_enable_nvmet_cnt) {
6049 
6050 		/* First get WWN of HBA instance */
6051 		lpfc_read_nv(phba, mboxq);
6052 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6053 		if (rc != MBX_SUCCESS) {
6054 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6055 					"6016 Mailbox failed , mbxCmd x%x "
6056 					"READ_NV, mbxStatus x%x\n",
6057 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6058 					bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6059 			mempool_free(mboxq, phba->mbox_mem_pool);
6060 			rc = -EIO;
6061 			goto out_free_bsmbx;
6062 		}
6063 		mb = &mboxq->u.mb;
6064 		memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6065 		       sizeof(uint64_t));
6066 		wwn = cpu_to_be64(wwn);
6067 		phba->sli4_hba.wwnn.u.name = wwn;
6068 		memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6069 		       sizeof(uint64_t));
6070 		/* wwn is WWPN of HBA instance */
6071 		wwn = cpu_to_be64(wwn);
6072 		phba->sli4_hba.wwpn.u.name = wwn;
6073 
6074 		/* Check to see if it matches any module parameter */
6075 		for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6076 			if (wwn == lpfc_enable_nvmet[i]) {
6077 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
6078 				if (lpfc_nvmet_mem_alloc(phba))
6079 					break;
6080 
6081 				phba->nvmet_support = 1; /* a match */
6082 
6083 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6084 						"6017 NVME Target %016llx\n",
6085 						wwn);
6086 #else
6087 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6088 						"6021 Can't enable NVME Target."
6089 						" NVME_TARGET_FC infrastructure"
6090 						" is not in kernel\n");
6091 #endif
6092 				break;
6093 			}
6094 		}
6095 	}
6096 
6097 	lpfc_nvme_mod_param_dep(phba);
6098 
6099 	/* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
6100 	lpfc_supported_pages(mboxq);
6101 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6102 	if (!rc) {
6103 		mqe = &mboxq->u.mqe;
6104 		memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6105 		       LPFC_MAX_SUPPORTED_PAGES);
6106 		for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6107 			switch (pn_page[i]) {
6108 			case LPFC_SLI4_PARAMETERS:
6109 				phba->sli4_hba.pc_sli4_params.supported = 1;
6110 				break;
6111 			default:
6112 				break;
6113 			}
6114 		}
6115 		/* Read the port's SLI4 Parameters capabilities if supported. */
6116 		if (phba->sli4_hba.pc_sli4_params.supported)
6117 			rc = lpfc_pc_sli4_params_get(phba, mboxq);
6118 		if (rc) {
6119 			mempool_free(mboxq, phba->mbox_mem_pool);
6120 			rc = -EIO;
6121 			goto out_free_bsmbx;
6122 		}
6123 	}
6124 
6125 	/*
6126 	 * Get sli4 parameters that override parameters from Port capabilities.
6127 	 * If this call fails, it isn't critical unless the SLI4 parameters come
6128 	 * back in conflict.
6129 	 */
6130 	rc = lpfc_get_sli4_parameters(phba, mboxq);
6131 	if (rc) {
6132 		if (phba->sli4_hba.extents_in_use &&
6133 		    phba->sli4_hba.rpi_hdrs_in_use) {
6134 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6135 				"2999 Unsupported SLI4 Parameters "
6136 				"Extents and RPI headers enabled.\n");
6137 		}
6138 		mempool_free(mboxq, phba->mbox_mem_pool);
6139 		rc = -EIO;
6140 		goto out_free_bsmbx;
6141 	}
6142 
6143 	mempool_free(mboxq, phba->mbox_mem_pool);
6144 
6145 	/* Verify OAS is supported */
6146 	lpfc_sli4_oas_verify(phba);
6147 	if (phba->cfg_fof)
6148 		fof_vectors = 1;
6149 
6150 	/* Verify all the SLI4 queues */
6151 	rc = lpfc_sli4_queue_verify(phba);
6152 	if (rc)
6153 		goto out_free_bsmbx;
6154 
6155 	/* Create driver internal CQE event pool */
6156 	rc = lpfc_sli4_cq_event_pool_create(phba);
6157 	if (rc)
6158 		goto out_free_bsmbx;
6159 
6160 	/* Initialize sgl lists per host */
6161 	lpfc_init_sgl_list(phba);
6162 
6163 	/* Allocate and initialize active sgl array */
6164 	rc = lpfc_init_active_sgl_array(phba);
6165 	if (rc) {
6166 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6167 				"1430 Failed to initialize sgl list.\n");
6168 		goto out_destroy_cq_event_pool;
6169 	}
6170 	rc = lpfc_sli4_init_rpi_hdrs(phba);
6171 	if (rc) {
6172 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6173 				"1432 Failed to initialize rpi headers.\n");
6174 		goto out_free_active_sgl;
6175 	}
6176 
6177 	/* Allocate eligible FCF bmask memory for FCF roundrobin failover */
6178 	longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6179 	phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
6180 					 GFP_KERNEL);
6181 	if (!phba->fcf.fcf_rr_bmask) {
6182 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6183 				"2759 Failed allocate memory for FCF round "
6184 				"robin failover bmask\n");
6185 		rc = -ENOMEM;
6186 		goto out_remove_rpi_hdrs;
6187 	}
6188 
6189 	phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6190 						sizeof(struct lpfc_hba_eq_hdl),
6191 						GFP_KERNEL);
6192 	if (!phba->sli4_hba.hba_eq_hdl) {
6193 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6194 				"2572 Failed allocate memory for "
6195 				"fast-path per-EQ handle array\n");
6196 		rc = -ENOMEM;
6197 		goto out_free_fcf_rr_bmask;
6198 	}
6199 
6200 	phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6201 					sizeof(struct lpfc_vector_map_info),
6202 					GFP_KERNEL);
6203 	if (!phba->sli4_hba.cpu_map) {
6204 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6205 				"3327 Failed allocate memory for msi-x "
6206 				"interrupt vector mapping\n");
6207 		rc = -ENOMEM;
6208 		goto out_free_hba_eq_hdl;
6209 	}
6210 	if (lpfc_used_cpu == NULL) {
6211 		lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6212 						GFP_KERNEL);
6213 		if (!lpfc_used_cpu) {
6214 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6215 					"3335 Failed allocate memory for msi-x "
6216 					"interrupt vector mapping\n");
6217 			kfree(phba->sli4_hba.cpu_map);
6218 			rc = -ENOMEM;
6219 			goto out_free_hba_eq_hdl;
6220 		}
6221 		for (i = 0; i < lpfc_present_cpu; i++)
6222 			lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6223 	}
6224 
6225 	/*
6226 	 * Enable sr-iov virtual functions if supported and configured
6227 	 * through the module parameter.
6228 	 */
6229 	if (phba->cfg_sriov_nr_virtfn > 0) {
6230 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6231 						 phba->cfg_sriov_nr_virtfn);
6232 		if (rc) {
6233 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6234 					"3020 Requested number of SR-IOV "
6235 					"virtual functions (%d) is not "
6236 					"supported\n",
6237 					phba->cfg_sriov_nr_virtfn);
6238 			phba->cfg_sriov_nr_virtfn = 0;
6239 		}
6240 	}
6241 
6242 	return 0;
6243 
6244 out_free_hba_eq_hdl:
6245 	kfree(phba->sli4_hba.hba_eq_hdl);
6246 out_free_fcf_rr_bmask:
6247 	kfree(phba->fcf.fcf_rr_bmask);
6248 out_remove_rpi_hdrs:
6249 	lpfc_sli4_remove_rpi_hdrs(phba);
6250 out_free_active_sgl:
6251 	lpfc_free_active_sgl(phba);
6252 out_destroy_cq_event_pool:
6253 	lpfc_sli4_cq_event_pool_destroy(phba);
6254 out_free_bsmbx:
6255 	lpfc_destroy_bootstrap_mbox(phba);
6256 out_free_mem:
6257 	lpfc_mem_free(phba);
6258 	return rc;
6259 }
6260 
6261 /**
6262  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6263  * @phba: pointer to lpfc hba data structure.
6264  *
6265  * This routine is invoked to unset the driver internal resources set up
6266  * specific for supporting the SLI-4 HBA device it attached to.
6267  **/
6268 static void
lpfc_sli4_driver_resource_unset(struct lpfc_hba * phba)6269 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6270 {
6271 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6272 
6273 	/* Free memory allocated for msi-x interrupt vector to CPU mapping */
6274 	kfree(phba->sli4_hba.cpu_map);
6275 	phba->sli4_hba.num_present_cpu = 0;
6276 	phba->sli4_hba.num_online_cpu = 0;
6277 	phba->sli4_hba.curr_disp_cpu = 0;
6278 
6279 	/* Free memory allocated for fast-path work queue handles */
6280 	kfree(phba->sli4_hba.hba_eq_hdl);
6281 
6282 	/* Free the allocated rpi headers. */
6283 	lpfc_sli4_remove_rpi_hdrs(phba);
6284 	lpfc_sli4_remove_rpis(phba);
6285 
6286 	/* Free eligible FCF index bmask */
6287 	kfree(phba->fcf.fcf_rr_bmask);
6288 
6289 	/* Free the ELS sgl list */
6290 	lpfc_free_active_sgl(phba);
6291 	lpfc_free_els_sgl_list(phba);
6292 	lpfc_free_nvmet_sgl_list(phba);
6293 
6294 	/* Free the completion queue EQ event pool */
6295 	lpfc_sli4_cq_event_release_all(phba);
6296 	lpfc_sli4_cq_event_pool_destroy(phba);
6297 
6298 	/* Release resource identifiers. */
6299 	lpfc_sli4_dealloc_resource_identifiers(phba);
6300 
6301 	/* Free the bsmbx region. */
6302 	lpfc_destroy_bootstrap_mbox(phba);
6303 
6304 	/* Free the SLI Layer memory with SLI4 HBAs */
6305 	lpfc_mem_free_all(phba);
6306 
6307 	/* Free the current connect table */
6308 	list_for_each_entry_safe(conn_entry, next_conn_entry,
6309 		&phba->fcf_conn_rec_list, list) {
6310 		list_del_init(&conn_entry->list);
6311 		kfree(conn_entry);
6312 	}
6313 
6314 	return;
6315 }
6316 
6317 /**
6318  * lpfc_init_api_table_setup - Set up init api function jump table
6319  * @phba: The hba struct for which this call is being executed.
6320  * @dev_grp: The HBA PCI-Device group number.
6321  *
6322  * This routine sets up the device INIT interface API function jump table
6323  * in @phba struct.
6324  *
6325  * Returns: 0 - success, -ENODEV - failure.
6326  **/
6327 int
lpfc_init_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)6328 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6329 {
6330 	phba->lpfc_hba_init_link = lpfc_hba_init_link;
6331 	phba->lpfc_hba_down_link = lpfc_hba_down_link;
6332 	phba->lpfc_selective_reset = lpfc_selective_reset;
6333 	switch (dev_grp) {
6334 	case LPFC_PCI_DEV_LP:
6335 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6336 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6337 		phba->lpfc_stop_port = lpfc_stop_port_s3;
6338 		break;
6339 	case LPFC_PCI_DEV_OC:
6340 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6341 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6342 		phba->lpfc_stop_port = lpfc_stop_port_s4;
6343 		break;
6344 	default:
6345 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6346 				"1431 Invalid HBA PCI-device group: 0x%x\n",
6347 				dev_grp);
6348 		return -ENODEV;
6349 		break;
6350 	}
6351 	return 0;
6352 }
6353 
6354 /**
6355  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6356  * @phba: pointer to lpfc hba data structure.
6357  *
6358  * This routine is invoked to set up the driver internal resources after the
6359  * device specific resource setup to support the HBA device it attached to.
6360  *
6361  * Return codes
6362  * 	0 - successful
6363  * 	other values - error
6364  **/
6365 static int
lpfc_setup_driver_resource_phase2(struct lpfc_hba * phba)6366 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6367 {
6368 	int error;
6369 
6370 	/* Startup the kernel thread for this host adapter. */
6371 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
6372 					  "lpfc_worker_%d", phba->brd_no);
6373 	if (IS_ERR(phba->worker_thread)) {
6374 		error = PTR_ERR(phba->worker_thread);
6375 		return error;
6376 	}
6377 
6378 	return 0;
6379 }
6380 
6381 /**
6382  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6383  * @phba: pointer to lpfc hba data structure.
6384  *
6385  * This routine is invoked to unset the driver internal resources set up after
6386  * the device specific resource setup for supporting the HBA device it
6387  * attached to.
6388  **/
6389 static void
lpfc_unset_driver_resource_phase2(struct lpfc_hba * phba)6390 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6391 {
6392 	/* Stop kernel worker thread */
6393 	kthread_stop(phba->worker_thread);
6394 }
6395 
6396 /**
6397  * lpfc_free_iocb_list - Free iocb list.
6398  * @phba: pointer to lpfc hba data structure.
6399  *
6400  * This routine is invoked to free the driver's IOCB list and memory.
6401  **/
6402 void
lpfc_free_iocb_list(struct lpfc_hba * phba)6403 lpfc_free_iocb_list(struct lpfc_hba *phba)
6404 {
6405 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6406 
6407 	spin_lock_irq(&phba->hbalock);
6408 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
6409 				 &phba->lpfc_iocb_list, list) {
6410 		list_del(&iocbq_entry->list);
6411 		kfree(iocbq_entry);
6412 		phba->total_iocbq_bufs--;
6413 	}
6414 	spin_unlock_irq(&phba->hbalock);
6415 
6416 	return;
6417 }
6418 
6419 /**
6420  * lpfc_init_iocb_list - Allocate and initialize iocb list.
6421  * @phba: pointer to lpfc hba data structure.
6422  *
6423  * This routine is invoked to allocate and initizlize the driver's IOCB
6424  * list and set up the IOCB tag array accordingly.
6425  *
6426  * Return codes
6427  *	0 - successful
6428  *	other values - error
6429  **/
6430 int
lpfc_init_iocb_list(struct lpfc_hba * phba,int iocb_count)6431 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6432 {
6433 	struct lpfc_iocbq *iocbq_entry = NULL;
6434 	uint16_t iotag;
6435 	int i;
6436 
6437 	/* Initialize and populate the iocb list per host.  */
6438 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
6439 	for (i = 0; i < iocb_count; i++) {
6440 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
6441 		if (iocbq_entry == NULL) {
6442 			printk(KERN_ERR "%s: only allocated %d iocbs of "
6443 				"expected %d count. Unloading driver.\n",
6444 				__func__, i, LPFC_IOCB_LIST_CNT);
6445 			goto out_free_iocbq;
6446 		}
6447 
6448 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6449 		if (iotag == 0) {
6450 			kfree(iocbq_entry);
6451 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
6452 				"Unloading driver.\n", __func__);
6453 			goto out_free_iocbq;
6454 		}
6455 		iocbq_entry->sli4_lxritag = NO_XRI;
6456 		iocbq_entry->sli4_xritag = NO_XRI;
6457 
6458 		spin_lock_irq(&phba->hbalock);
6459 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6460 		phba->total_iocbq_bufs++;
6461 		spin_unlock_irq(&phba->hbalock);
6462 	}
6463 
6464 	return 0;
6465 
6466 out_free_iocbq:
6467 	lpfc_free_iocb_list(phba);
6468 
6469 	return -ENOMEM;
6470 }
6471 
6472 /**
6473  * lpfc_free_sgl_list - Free a given sgl list.
6474  * @phba: pointer to lpfc hba data structure.
6475  * @sglq_list: pointer to the head of sgl list.
6476  *
6477  * This routine is invoked to free a give sgl list and memory.
6478  **/
6479 void
lpfc_free_sgl_list(struct lpfc_hba * phba,struct list_head * sglq_list)6480 lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
6481 {
6482 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6483 
6484 	list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6485 		list_del(&sglq_entry->list);
6486 		lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6487 		kfree(sglq_entry);
6488 	}
6489 }
6490 
6491 /**
6492  * lpfc_free_els_sgl_list - Free els sgl list.
6493  * @phba: pointer to lpfc hba data structure.
6494  *
6495  * This routine is invoked to free the driver's els sgl list and memory.
6496  **/
6497 static void
lpfc_free_els_sgl_list(struct lpfc_hba * phba)6498 lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6499 {
6500 	LIST_HEAD(sglq_list);
6501 
6502 	/* Retrieve all els sgls from driver list */
6503 	spin_lock_irq(&phba->hbalock);
6504 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6505 	list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6506 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6507 	spin_unlock_irq(&phba->hbalock);
6508 
6509 	/* Now free the sgl list */
6510 	lpfc_free_sgl_list(phba, &sglq_list);
6511 }
6512 
6513 /**
6514  * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6515  * @phba: pointer to lpfc hba data structure.
6516  *
6517  * This routine is invoked to free the driver's nvmet sgl list and memory.
6518  **/
6519 static void
lpfc_free_nvmet_sgl_list(struct lpfc_hba * phba)6520 lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6521 {
6522 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6523 	LIST_HEAD(sglq_list);
6524 
6525 	/* Retrieve all nvmet sgls from driver list */
6526 	spin_lock_irq(&phba->hbalock);
6527 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6528 	list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6529 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6530 	spin_unlock_irq(&phba->hbalock);
6531 
6532 	/* Now free the sgl list */
6533 	list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6534 		list_del(&sglq_entry->list);
6535 		lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6536 		kfree(sglq_entry);
6537 	}
6538 
6539 	/* Update the nvmet_xri_cnt to reflect no current sgls.
6540 	 * The next initialization cycle sets the count and allocates
6541 	 * the sgls over again.
6542 	 */
6543 	phba->sli4_hba.nvmet_xri_cnt = 0;
6544 }
6545 
6546 /**
6547  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6548  * @phba: pointer to lpfc hba data structure.
6549  *
6550  * This routine is invoked to allocate the driver's active sgl memory.
6551  * This array will hold the sglq_entry's for active IOs.
6552  **/
6553 static int
lpfc_init_active_sgl_array(struct lpfc_hba * phba)6554 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6555 {
6556 	int size;
6557 	size = sizeof(struct lpfc_sglq *);
6558 	size *= phba->sli4_hba.max_cfg_param.max_xri;
6559 
6560 	phba->sli4_hba.lpfc_sglq_active_list =
6561 		kzalloc(size, GFP_KERNEL);
6562 	if (!phba->sli4_hba.lpfc_sglq_active_list)
6563 		return -ENOMEM;
6564 	return 0;
6565 }
6566 
6567 /**
6568  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6569  * @phba: pointer to lpfc hba data structure.
6570  *
6571  * This routine is invoked to walk through the array of active sglq entries
6572  * and free all of the resources.
6573  * This is just a place holder for now.
6574  **/
6575 static void
lpfc_free_active_sgl(struct lpfc_hba * phba)6576 lpfc_free_active_sgl(struct lpfc_hba *phba)
6577 {
6578 	kfree(phba->sli4_hba.lpfc_sglq_active_list);
6579 }
6580 
6581 /**
6582  * lpfc_init_sgl_list - Allocate and initialize sgl list.
6583  * @phba: pointer to lpfc hba data structure.
6584  *
6585  * This routine is invoked to allocate and initizlize the driver's sgl
6586  * list and set up the sgl xritag tag array accordingly.
6587  *
6588  **/
6589 static void
lpfc_init_sgl_list(struct lpfc_hba * phba)6590 lpfc_init_sgl_list(struct lpfc_hba *phba)
6591 {
6592 	/* Initialize and populate the sglq list per host/VF. */
6593 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
6594 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
6595 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6596 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6597 
6598 	/* els xri-sgl book keeping */
6599 	phba->sli4_hba.els_xri_cnt = 0;
6600 
6601 	/* scsi xri-buffer book keeping */
6602 	phba->sli4_hba.scsi_xri_cnt = 0;
6603 
6604 	/* nvme xri-buffer book keeping */
6605 	phba->sli4_hba.nvme_xri_cnt = 0;
6606 }
6607 
6608 /**
6609  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6610  * @phba: pointer to lpfc hba data structure.
6611  *
6612  * This routine is invoked to post rpi header templates to the
6613  * port for those SLI4 ports that do not support extents.  This routine
6614  * posts a PAGE_SIZE memory region to the port to hold up to
6615  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
6616  * and should be called only when interrupts are disabled.
6617  *
6618  * Return codes
6619  * 	0 - successful
6620  *	-ERROR - otherwise.
6621  **/
6622 int
lpfc_sli4_init_rpi_hdrs(struct lpfc_hba * phba)6623 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6624 {
6625 	int rc = 0;
6626 	struct lpfc_rpi_hdr *rpi_hdr;
6627 
6628 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
6629 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6630 		return rc;
6631 	if (phba->sli4_hba.extents_in_use)
6632 		return -EIO;
6633 
6634 	rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6635 	if (!rpi_hdr) {
6636 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6637 				"0391 Error during rpi post operation\n");
6638 		lpfc_sli4_remove_rpis(phba);
6639 		rc = -ENODEV;
6640 	}
6641 
6642 	return rc;
6643 }
6644 
6645 /**
6646  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6647  * @phba: pointer to lpfc hba data structure.
6648  *
6649  * This routine is invoked to allocate a single 4KB memory region to
6650  * support rpis and stores them in the phba.  This single region
6651  * provides support for up to 64 rpis.  The region is used globally
6652  * by the device.
6653  *
6654  * Returns:
6655  *   A valid rpi hdr on success.
6656  *   A NULL pointer on any failure.
6657  **/
6658 struct lpfc_rpi_hdr *
lpfc_sli4_create_rpi_hdr(struct lpfc_hba * phba)6659 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6660 {
6661 	uint16_t rpi_limit, curr_rpi_range;
6662 	struct lpfc_dmabuf *dmabuf;
6663 	struct lpfc_rpi_hdr *rpi_hdr;
6664 
6665 	/*
6666 	 * If the SLI4 port supports extents, posting the rpi header isn't
6667 	 * required.  Set the expected maximum count and let the actual value
6668 	 * get set when extents are fully allocated.
6669 	 */
6670 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6671 		return NULL;
6672 	if (phba->sli4_hba.extents_in_use)
6673 		return NULL;
6674 
6675 	/* The limit on the logical index is just the max_rpi count. */
6676 	rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
6677 
6678 	spin_lock_irq(&phba->hbalock);
6679 	/*
6680 	 * Establish the starting RPI in this header block.  The starting
6681 	 * rpi is normalized to a zero base because the physical rpi is
6682 	 * port based.
6683 	 */
6684 	curr_rpi_range = phba->sli4_hba.next_rpi;
6685 	spin_unlock_irq(&phba->hbalock);
6686 
6687 	/* Reached full RPI range */
6688 	if (curr_rpi_range == rpi_limit)
6689 		return NULL;
6690 
6691 	/*
6692 	 * First allocate the protocol header region for the port.  The
6693 	 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6694 	 */
6695 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6696 	if (!dmabuf)
6697 		return NULL;
6698 
6699 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6700 					   LPFC_HDR_TEMPLATE_SIZE,
6701 					   &dmabuf->phys, GFP_KERNEL);
6702 	if (!dmabuf->virt) {
6703 		rpi_hdr = NULL;
6704 		goto err_free_dmabuf;
6705 	}
6706 
6707 	if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6708 		rpi_hdr = NULL;
6709 		goto err_free_coherent;
6710 	}
6711 
6712 	/* Save the rpi header data for cleanup later. */
6713 	rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6714 	if (!rpi_hdr)
6715 		goto err_free_coherent;
6716 
6717 	rpi_hdr->dmabuf = dmabuf;
6718 	rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6719 	rpi_hdr->page_count = 1;
6720 	spin_lock_irq(&phba->hbalock);
6721 
6722 	/* The rpi_hdr stores the logical index only. */
6723 	rpi_hdr->start_rpi = curr_rpi_range;
6724 	rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
6725 	list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6726 
6727 	spin_unlock_irq(&phba->hbalock);
6728 	return rpi_hdr;
6729 
6730  err_free_coherent:
6731 	dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6732 			  dmabuf->virt, dmabuf->phys);
6733  err_free_dmabuf:
6734 	kfree(dmabuf);
6735 	return NULL;
6736 }
6737 
6738 /**
6739  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6740  * @phba: pointer to lpfc hba data structure.
6741  *
6742  * This routine is invoked to remove all memory resources allocated
6743  * to support rpis for SLI4 ports not supporting extents. This routine
6744  * presumes the caller has released all rpis consumed by fabric or port
6745  * logins and is prepared to have the header pages removed.
6746  **/
6747 void
lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba * phba)6748 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6749 {
6750 	struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6751 
6752 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6753 		goto exit;
6754 
6755 	list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6756 				 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6757 		list_del(&rpi_hdr->list);
6758 		dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6759 				  rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6760 		kfree(rpi_hdr->dmabuf);
6761 		kfree(rpi_hdr);
6762 	}
6763  exit:
6764 	/* There are no rpis available to the port now. */
6765 	phba->sli4_hba.next_rpi = 0;
6766 }
6767 
6768 /**
6769  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6770  * @pdev: pointer to pci device data structure.
6771  *
6772  * This routine is invoked to allocate the driver hba data structure for an
6773  * HBA device. If the allocation is successful, the phba reference to the
6774  * PCI device data structure is set.
6775  *
6776  * Return codes
6777  *      pointer to @phba - successful
6778  *      NULL - error
6779  **/
6780 static struct lpfc_hba *
lpfc_hba_alloc(struct pci_dev * pdev)6781 lpfc_hba_alloc(struct pci_dev *pdev)
6782 {
6783 	struct lpfc_hba *phba;
6784 
6785 	/* Allocate memory for HBA structure */
6786 	phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6787 	if (!phba) {
6788 		dev_err(&pdev->dev, "failed to allocate hba struct\n");
6789 		return NULL;
6790 	}
6791 
6792 	/* Set reference to PCI device in HBA structure */
6793 	phba->pcidev = pdev;
6794 
6795 	/* Assign an unused board number */
6796 	phba->brd_no = lpfc_get_instance();
6797 	if (phba->brd_no < 0) {
6798 		kfree(phba);
6799 		return NULL;
6800 	}
6801 	phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
6802 
6803 	spin_lock_init(&phba->ct_ev_lock);
6804 	INIT_LIST_HEAD(&phba->ct_ev_waiters);
6805 
6806 	return phba;
6807 }
6808 
6809 /**
6810  * lpfc_hba_free - Free driver hba data structure with a device.
6811  * @phba: pointer to lpfc hba data structure.
6812  *
6813  * This routine is invoked to free the driver hba data structure with an
6814  * HBA device.
6815  **/
6816 static void
lpfc_hba_free(struct lpfc_hba * phba)6817 lpfc_hba_free(struct lpfc_hba *phba)
6818 {
6819 	/* Release the driver assigned board number */
6820 	idr_remove(&lpfc_hba_index, phba->brd_no);
6821 
6822 	/* Free memory allocated with sli3 rings */
6823 	kfree(phba->sli.sli3_ring);
6824 	phba->sli.sli3_ring = NULL;
6825 
6826 	kfree(phba);
6827 	return;
6828 }
6829 
6830 /**
6831  * lpfc_create_shost - Create hba physical port with associated scsi host.
6832  * @phba: pointer to lpfc hba data structure.
6833  *
6834  * This routine is invoked to create HBA physical port and associate a SCSI
6835  * host with it.
6836  *
6837  * Return codes
6838  *      0 - successful
6839  *      other values - error
6840  **/
6841 static int
lpfc_create_shost(struct lpfc_hba * phba)6842 lpfc_create_shost(struct lpfc_hba *phba)
6843 {
6844 	struct lpfc_vport *vport;
6845 	struct Scsi_Host  *shost;
6846 
6847 	/* Initialize HBA FC structure */
6848 	phba->fc_edtov = FF_DEF_EDTOV;
6849 	phba->fc_ratov = FF_DEF_RATOV;
6850 	phba->fc_altov = FF_DEF_ALTOV;
6851 	phba->fc_arbtov = FF_DEF_ARBTOV;
6852 
6853 	atomic_set(&phba->sdev_cnt, 0);
6854 	atomic_set(&phba->fc4ScsiInputRequests, 0);
6855 	atomic_set(&phba->fc4ScsiOutputRequests, 0);
6856 	atomic_set(&phba->fc4ScsiControlRequests, 0);
6857 	atomic_set(&phba->fc4ScsiIoCmpls, 0);
6858 	atomic_set(&phba->fc4NvmeInputRequests, 0);
6859 	atomic_set(&phba->fc4NvmeOutputRequests, 0);
6860 	atomic_set(&phba->fc4NvmeControlRequests, 0);
6861 	atomic_set(&phba->fc4NvmeIoCmpls, 0);
6862 	atomic_set(&phba->fc4NvmeLsRequests, 0);
6863 	atomic_set(&phba->fc4NvmeLsCmpls, 0);
6864 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
6865 	if (!vport)
6866 		return -ENODEV;
6867 
6868 	shost = lpfc_shost_from_vport(vport);
6869 	phba->pport = vport;
6870 
6871 	if (phba->nvmet_support) {
6872 		/* Only 1 vport (pport) will support NVME target */
6873 		if (phba->txrdy_payload_pool == NULL) {
6874 			phba->txrdy_payload_pool = dma_pool_create(
6875 				"txrdy_pool", &phba->pcidev->dev,
6876 				TXRDY_PAYLOAD_LEN, 16, 0);
6877 			if (phba->txrdy_payload_pool) {
6878 				phba->targetport = NULL;
6879 				phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6880 				lpfc_printf_log(phba, KERN_INFO,
6881 						LOG_INIT | LOG_NVME_DISC,
6882 						"6076 NVME Target Found\n");
6883 			}
6884 		}
6885 	}
6886 
6887 	lpfc_debugfs_initialize(vport);
6888 	/* Put reference to SCSI host to driver's device private data */
6889 	pci_set_drvdata(phba->pcidev, shost);
6890 
6891 	/*
6892 	 * At this point we are fully registered with PSA. In addition,
6893 	 * any initial discovery should be completed.
6894 	 */
6895 	vport->load_flag |= FC_ALLOW_FDMI;
6896 	if (phba->cfg_enable_SmartSAN ||
6897 	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
6898 
6899 		/* Setup appropriate attribute masks */
6900 		vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
6901 		if (phba->cfg_enable_SmartSAN)
6902 			vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6903 		else
6904 			vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6905 	}
6906 	return 0;
6907 }
6908 
6909 /**
6910  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6911  * @phba: pointer to lpfc hba data structure.
6912  *
6913  * This routine is invoked to destroy HBA physical port and the associated
6914  * SCSI host.
6915  **/
6916 static void
lpfc_destroy_shost(struct lpfc_hba * phba)6917 lpfc_destroy_shost(struct lpfc_hba *phba)
6918 {
6919 	struct lpfc_vport *vport = phba->pport;
6920 
6921 	/* Destroy physical port that associated with the SCSI host */
6922 	destroy_port(vport);
6923 
6924 	return;
6925 }
6926 
6927 /**
6928  * lpfc_setup_bg - Setup Block guard structures and debug areas.
6929  * @phba: pointer to lpfc hba data structure.
6930  * @shost: the shost to be used to detect Block guard settings.
6931  *
6932  * This routine sets up the local Block guard protocol settings for @shost.
6933  * This routine also allocates memory for debugging bg buffers.
6934  **/
6935 static void
lpfc_setup_bg(struct lpfc_hba * phba,struct Scsi_Host * shost)6936 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6937 {
6938 	uint32_t old_mask;
6939 	uint32_t old_guard;
6940 
6941 	int pagecnt = 10;
6942 	if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6943 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6944 				"1478 Registering BlockGuard with the "
6945 				"SCSI layer\n");
6946 
6947 		old_mask = phba->cfg_prot_mask;
6948 		old_guard = phba->cfg_prot_guard;
6949 
6950 		/* Only allow supported values */
6951 		phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6952 			SHOST_DIX_TYPE0_PROTECTION |
6953 			SHOST_DIX_TYPE1_PROTECTION);
6954 		phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6955 					 SHOST_DIX_GUARD_CRC);
6956 
6957 		/* DIF Type 1 protection for profiles AST1/C1 is end to end */
6958 		if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6959 			phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6960 
6961 		if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6962 			if ((old_mask != phba->cfg_prot_mask) ||
6963 				(old_guard != phba->cfg_prot_guard))
6964 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6965 					"1475 Registering BlockGuard with the "
6966 					"SCSI layer: mask %d  guard %d\n",
6967 					phba->cfg_prot_mask,
6968 					phba->cfg_prot_guard);
6969 
6970 			scsi_host_set_prot(shost, phba->cfg_prot_mask);
6971 			scsi_host_set_guard(shost, phba->cfg_prot_guard);
6972 		} else
6973 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6974 				"1479 Not Registering BlockGuard with the SCSI "
6975 				"layer, Bad protection parameters: %d %d\n",
6976 				old_mask, old_guard);
6977 	}
6978 
6979 	if (!_dump_buf_data) {
6980 		while (pagecnt) {
6981 			spin_lock_init(&_dump_buf_lock);
6982 			_dump_buf_data =
6983 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
6984 			if (_dump_buf_data) {
6985 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6986 					"9043 BLKGRD: allocated %d pages for "
6987 				       "_dump_buf_data at 0x%p\n",
6988 				       (1 << pagecnt), _dump_buf_data);
6989 				_dump_buf_data_order = pagecnt;
6990 				memset(_dump_buf_data, 0,
6991 				       ((1 << PAGE_SHIFT) << pagecnt));
6992 				break;
6993 			} else
6994 				--pagecnt;
6995 		}
6996 		if (!_dump_buf_data_order)
6997 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6998 				"9044 BLKGRD: ERROR unable to allocate "
6999 			       "memory for hexdump\n");
7000 	} else
7001 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7002 			"9045 BLKGRD: already allocated _dump_buf_data=0x%p"
7003 		       "\n", _dump_buf_data);
7004 	if (!_dump_buf_dif) {
7005 		while (pagecnt) {
7006 			_dump_buf_dif =
7007 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
7008 			if (_dump_buf_dif) {
7009 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7010 					"9046 BLKGRD: allocated %d pages for "
7011 				       "_dump_buf_dif at 0x%p\n",
7012 				       (1 << pagecnt), _dump_buf_dif);
7013 				_dump_buf_dif_order = pagecnt;
7014 				memset(_dump_buf_dif, 0,
7015 				       ((1 << PAGE_SHIFT) << pagecnt));
7016 				break;
7017 			} else
7018 				--pagecnt;
7019 		}
7020 		if (!_dump_buf_dif_order)
7021 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7022 			"9047 BLKGRD: ERROR unable to allocate "
7023 			       "memory for hexdump\n");
7024 	} else
7025 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7026 			"9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
7027 		       _dump_buf_dif);
7028 }
7029 
7030 /**
7031  * lpfc_post_init_setup - Perform necessary device post initialization setup.
7032  * @phba: pointer to lpfc hba data structure.
7033  *
7034  * This routine is invoked to perform all the necessary post initialization
7035  * setup for the device.
7036  **/
7037 static void
lpfc_post_init_setup(struct lpfc_hba * phba)7038 lpfc_post_init_setup(struct lpfc_hba *phba)
7039 {
7040 	struct Scsi_Host  *shost;
7041 	struct lpfc_adapter_event_header adapter_event;
7042 
7043 	/* Get the default values for Model Name and Description */
7044 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7045 
7046 	/*
7047 	 * hba setup may have changed the hba_queue_depth so we need to
7048 	 * adjust the value of can_queue.
7049 	 */
7050 	shost = pci_get_drvdata(phba->pcidev);
7051 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
7052 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7053 		lpfc_setup_bg(phba, shost);
7054 
7055 	lpfc_host_attrib_init(shost);
7056 
7057 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7058 		spin_lock_irq(shost->host_lock);
7059 		lpfc_poll_start_timer(phba);
7060 		spin_unlock_irq(shost->host_lock);
7061 	}
7062 
7063 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7064 			"0428 Perform SCSI scan\n");
7065 	/* Send board arrival event to upper layer */
7066 	adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7067 	adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7068 	fc_host_post_vendor_event(shost, fc_get_event_number(),
7069 				  sizeof(adapter_event),
7070 				  (char *) &adapter_event,
7071 				  LPFC_NL_VENDOR_ID);
7072 	return;
7073 }
7074 
7075 /**
7076  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7077  * @phba: pointer to lpfc hba data structure.
7078  *
7079  * This routine is invoked to set up the PCI device memory space for device
7080  * with SLI-3 interface spec.
7081  *
7082  * Return codes
7083  * 	0 - successful
7084  * 	other values - error
7085  **/
7086 static int
lpfc_sli_pci_mem_setup(struct lpfc_hba * phba)7087 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7088 {
7089 	struct pci_dev *pdev;
7090 	unsigned long bar0map_len, bar2map_len;
7091 	int i, hbq_count;
7092 	void *ptr;
7093 	int error = -ENODEV;
7094 
7095 	/* Obtain PCI device reference */
7096 	if (!phba->pcidev)
7097 		return error;
7098 	else
7099 		pdev = phba->pcidev;
7100 
7101 	/* Set the device DMA mask size */
7102 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7103 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7104 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7105 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7106 			return error;
7107 		}
7108 	}
7109 
7110 	/* Get the bus address of Bar0 and Bar2 and the number of bytes
7111 	 * required by each mapping.
7112 	 */
7113 	phba->pci_bar0_map = pci_resource_start(pdev, 0);
7114 	bar0map_len = pci_resource_len(pdev, 0);
7115 
7116 	phba->pci_bar2_map = pci_resource_start(pdev, 2);
7117 	bar2map_len = pci_resource_len(pdev, 2);
7118 
7119 	/* Map HBA SLIM to a kernel virtual address. */
7120 	phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7121 	if (!phba->slim_memmap_p) {
7122 		dev_printk(KERN_ERR, &pdev->dev,
7123 			   "ioremap failed for SLIM memory.\n");
7124 		goto out;
7125 	}
7126 
7127 	/* Map HBA Control Registers to a kernel virtual address. */
7128 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7129 	if (!phba->ctrl_regs_memmap_p) {
7130 		dev_printk(KERN_ERR, &pdev->dev,
7131 			   "ioremap failed for HBA control registers.\n");
7132 		goto out_iounmap_slim;
7133 	}
7134 
7135 	/* Allocate memory for SLI-2 structures */
7136 	phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7137 						&phba->slim2p.phys, GFP_KERNEL);
7138 	if (!phba->slim2p.virt)
7139 		goto out_iounmap;
7140 
7141 	phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7142 	phba->mbox_ext = (phba->slim2p.virt +
7143 		offsetof(struct lpfc_sli2_slim, mbx_ext_words));
7144 	phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7145 	phba->IOCBs = (phba->slim2p.virt +
7146 		       offsetof(struct lpfc_sli2_slim, IOCBs));
7147 
7148 	phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7149 						 lpfc_sli_hbq_size(),
7150 						 &phba->hbqslimp.phys,
7151 						 GFP_KERNEL);
7152 	if (!phba->hbqslimp.virt)
7153 		goto out_free_slim;
7154 
7155 	hbq_count = lpfc_sli_hbq_count();
7156 	ptr = phba->hbqslimp.virt;
7157 	for (i = 0; i < hbq_count; ++i) {
7158 		phba->hbqs[i].hbq_virt = ptr;
7159 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7160 		ptr += (lpfc_hbq_defs[i]->entry_count *
7161 			sizeof(struct lpfc_hbq_entry));
7162 	}
7163 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7164 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7165 
7166 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7167 
7168 	phba->MBslimaddr = phba->slim_memmap_p;
7169 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7170 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7171 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7172 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
7173 
7174 	return 0;
7175 
7176 out_free_slim:
7177 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7178 			  phba->slim2p.virt, phba->slim2p.phys);
7179 out_iounmap:
7180 	iounmap(phba->ctrl_regs_memmap_p);
7181 out_iounmap_slim:
7182 	iounmap(phba->slim_memmap_p);
7183 out:
7184 	return error;
7185 }
7186 
7187 /**
7188  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7189  * @phba: pointer to lpfc hba data structure.
7190  *
7191  * This routine is invoked to unset the PCI device memory space for device
7192  * with SLI-3 interface spec.
7193  **/
7194 static void
lpfc_sli_pci_mem_unset(struct lpfc_hba * phba)7195 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7196 {
7197 	struct pci_dev *pdev;
7198 
7199 	/* Obtain PCI device reference */
7200 	if (!phba->pcidev)
7201 		return;
7202 	else
7203 		pdev = phba->pcidev;
7204 
7205 	/* Free coherent DMA memory allocated */
7206 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7207 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
7208 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7209 			  phba->slim2p.virt, phba->slim2p.phys);
7210 
7211 	/* I/O memory unmap */
7212 	iounmap(phba->ctrl_regs_memmap_p);
7213 	iounmap(phba->slim_memmap_p);
7214 
7215 	return;
7216 }
7217 
7218 /**
7219  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7220  * @phba: pointer to lpfc hba data structure.
7221  *
7222  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7223  * done and check status.
7224  *
7225  * Return 0 if successful, otherwise -ENODEV.
7226  **/
7227 int
lpfc_sli4_post_status_check(struct lpfc_hba * phba)7228 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7229 {
7230 	struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7231 	struct lpfc_register reg_data;
7232 	int i, port_error = 0;
7233 	uint32_t if_type;
7234 
7235 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7236 	memset(&reg_data, 0, sizeof(reg_data));
7237 	if (!phba->sli4_hba.PSMPHRregaddr)
7238 		return -ENODEV;
7239 
7240 	/* Wait up to 30 seconds for the SLI Port POST done and ready */
7241 	for (i = 0; i < 3000; i++) {
7242 		if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7243 			&portsmphr_reg.word0) ||
7244 			(bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
7245 			/* Port has a fatal POST error, break out */
7246 			port_error = -ENODEV;
7247 			break;
7248 		}
7249 		if (LPFC_POST_STAGE_PORT_READY ==
7250 		    bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
7251 			break;
7252 		msleep(10);
7253 	}
7254 
7255 	/*
7256 	 * If there was a port error during POST, then don't proceed with
7257 	 * other register reads as the data may not be valid.  Just exit.
7258 	 */
7259 	if (port_error) {
7260 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7261 			"1408 Port Failed POST - portsmphr=0x%x, "
7262 			"perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7263 			"scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7264 			portsmphr_reg.word0,
7265 			bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7266 			bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7267 			bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7268 			bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7269 			bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7270 			bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7271 			bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7272 			bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7273 	} else {
7274 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7275 				"2534 Device Info: SLIFamily=0x%x, "
7276 				"SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7277 				"SLIHint_2=0x%x, FT=0x%x\n",
7278 				bf_get(lpfc_sli_intf_sli_family,
7279 				       &phba->sli4_hba.sli_intf),
7280 				bf_get(lpfc_sli_intf_slirev,
7281 				       &phba->sli4_hba.sli_intf),
7282 				bf_get(lpfc_sli_intf_if_type,
7283 				       &phba->sli4_hba.sli_intf),
7284 				bf_get(lpfc_sli_intf_sli_hint1,
7285 				       &phba->sli4_hba.sli_intf),
7286 				bf_get(lpfc_sli_intf_sli_hint2,
7287 				       &phba->sli4_hba.sli_intf),
7288 				bf_get(lpfc_sli_intf_func_type,
7289 				       &phba->sli4_hba.sli_intf));
7290 		/*
7291 		 * Check for other Port errors during the initialization
7292 		 * process.  Fail the load if the port did not come up
7293 		 * correctly.
7294 		 */
7295 		if_type = bf_get(lpfc_sli_intf_if_type,
7296 				 &phba->sli4_hba.sli_intf);
7297 		switch (if_type) {
7298 		case LPFC_SLI_INTF_IF_TYPE_0:
7299 			phba->sli4_hba.ue_mask_lo =
7300 			      readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7301 			phba->sli4_hba.ue_mask_hi =
7302 			      readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7303 			uerrlo_reg.word0 =
7304 			      readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7305 			uerrhi_reg.word0 =
7306 				readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7307 			if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7308 			    (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7309 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7310 						"1422 Unrecoverable Error "
7311 						"Detected during POST "
7312 						"uerr_lo_reg=0x%x, "
7313 						"uerr_hi_reg=0x%x, "
7314 						"ue_mask_lo_reg=0x%x, "
7315 						"ue_mask_hi_reg=0x%x\n",
7316 						uerrlo_reg.word0,
7317 						uerrhi_reg.word0,
7318 						phba->sli4_hba.ue_mask_lo,
7319 						phba->sli4_hba.ue_mask_hi);
7320 				port_error = -ENODEV;
7321 			}
7322 			break;
7323 		case LPFC_SLI_INTF_IF_TYPE_2:
7324 			/* Final checks.  The port status should be clean. */
7325 			if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7326 				&reg_data.word0) ||
7327 				(bf_get(lpfc_sliport_status_err, &reg_data) &&
7328 				 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
7329 				phba->work_status[0] =
7330 					readl(phba->sli4_hba.u.if_type2.
7331 					      ERR1regaddr);
7332 				phba->work_status[1] =
7333 					readl(phba->sli4_hba.u.if_type2.
7334 					      ERR2regaddr);
7335 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7336 					"2888 Unrecoverable port error "
7337 					"following POST: port status reg "
7338 					"0x%x, port_smphr reg 0x%x, "
7339 					"error 1=0x%x, error 2=0x%x\n",
7340 					reg_data.word0,
7341 					portsmphr_reg.word0,
7342 					phba->work_status[0],
7343 					phba->work_status[1]);
7344 				port_error = -ENODEV;
7345 			}
7346 			break;
7347 		case LPFC_SLI_INTF_IF_TYPE_1:
7348 		default:
7349 			break;
7350 		}
7351 	}
7352 	return port_error;
7353 }
7354 
7355 /**
7356  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7357  * @phba: pointer to lpfc hba data structure.
7358  * @if_type:  The SLI4 interface type getting configured.
7359  *
7360  * This routine is invoked to set up SLI4 BAR0 PCI config space register
7361  * memory map.
7362  **/
7363 static void
lpfc_sli4_bar0_register_memmap(struct lpfc_hba * phba,uint32_t if_type)7364 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7365 {
7366 	switch (if_type) {
7367 	case LPFC_SLI_INTF_IF_TYPE_0:
7368 		phba->sli4_hba.u.if_type0.UERRLOregaddr =
7369 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7370 		phba->sli4_hba.u.if_type0.UERRHIregaddr =
7371 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7372 		phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7373 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7374 		phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7375 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7376 		phba->sli4_hba.SLIINTFregaddr =
7377 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7378 		break;
7379 	case LPFC_SLI_INTF_IF_TYPE_2:
7380 		phba->sli4_hba.u.if_type2.EQDregaddr =
7381 			phba->sli4_hba.conf_regs_memmap_p +
7382 						LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7383 		phba->sli4_hba.u.if_type2.ERR1regaddr =
7384 			phba->sli4_hba.conf_regs_memmap_p +
7385 						LPFC_CTL_PORT_ER1_OFFSET;
7386 		phba->sli4_hba.u.if_type2.ERR2regaddr =
7387 			phba->sli4_hba.conf_regs_memmap_p +
7388 						LPFC_CTL_PORT_ER2_OFFSET;
7389 		phba->sli4_hba.u.if_type2.CTRLregaddr =
7390 			phba->sli4_hba.conf_regs_memmap_p +
7391 						LPFC_CTL_PORT_CTL_OFFSET;
7392 		phba->sli4_hba.u.if_type2.STATUSregaddr =
7393 			phba->sli4_hba.conf_regs_memmap_p +
7394 						LPFC_CTL_PORT_STA_OFFSET;
7395 		phba->sli4_hba.SLIINTFregaddr =
7396 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7397 		phba->sli4_hba.PSMPHRregaddr =
7398 			phba->sli4_hba.conf_regs_memmap_p +
7399 						LPFC_CTL_PORT_SEM_OFFSET;
7400 		phba->sli4_hba.RQDBregaddr =
7401 			phba->sli4_hba.conf_regs_memmap_p +
7402 						LPFC_ULP0_RQ_DOORBELL;
7403 		phba->sli4_hba.WQDBregaddr =
7404 			phba->sli4_hba.conf_regs_memmap_p +
7405 						LPFC_ULP0_WQ_DOORBELL;
7406 		phba->sli4_hba.EQCQDBregaddr =
7407 			phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7408 		phba->sli4_hba.MQDBregaddr =
7409 			phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7410 		phba->sli4_hba.BMBXregaddr =
7411 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7412 		break;
7413 	case LPFC_SLI_INTF_IF_TYPE_1:
7414 	default:
7415 		dev_printk(KERN_ERR, &phba->pcidev->dev,
7416 			   "FATAL - unsupported SLI4 interface type - %d\n",
7417 			   if_type);
7418 		break;
7419 	}
7420 }
7421 
7422 /**
7423  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7424  * @phba: pointer to lpfc hba data structure.
7425  *
7426  * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7427  * memory map.
7428  **/
7429 static void
lpfc_sli4_bar1_register_memmap(struct lpfc_hba * phba)7430 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7431 {
7432 	phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7433 		LPFC_SLIPORT_IF0_SMPHR;
7434 	phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7435 		LPFC_HST_ISR0;
7436 	phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7437 		LPFC_HST_IMR0;
7438 	phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7439 		LPFC_HST_ISCR0;
7440 }
7441 
7442 /**
7443  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7444  * @phba: pointer to lpfc hba data structure.
7445  * @vf: virtual function number
7446  *
7447  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7448  * based on the given viftual function number, @vf.
7449  *
7450  * Return 0 if successful, otherwise -ENODEV.
7451  **/
7452 static int
lpfc_sli4_bar2_register_memmap(struct lpfc_hba * phba,uint32_t vf)7453 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7454 {
7455 	if (vf > LPFC_VIR_FUNC_MAX)
7456 		return -ENODEV;
7457 
7458 	phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7459 				vf * LPFC_VFR_PAGE_SIZE +
7460 					LPFC_ULP0_RQ_DOORBELL);
7461 	phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7462 				vf * LPFC_VFR_PAGE_SIZE +
7463 					LPFC_ULP0_WQ_DOORBELL);
7464 	phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7465 				vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7466 	phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7467 				vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7468 	phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7469 				vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7470 	return 0;
7471 }
7472 
7473 /**
7474  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7475  * @phba: pointer to lpfc hba data structure.
7476  *
7477  * This routine is invoked to create the bootstrap mailbox
7478  * region consistent with the SLI-4 interface spec.  This
7479  * routine allocates all memory necessary to communicate
7480  * mailbox commands to the port and sets up all alignment
7481  * needs.  No locks are expected to be held when calling
7482  * this routine.
7483  *
7484  * Return codes
7485  * 	0 - successful
7486  * 	-ENOMEM - could not allocated memory.
7487  **/
7488 static int
lpfc_create_bootstrap_mbox(struct lpfc_hba * phba)7489 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7490 {
7491 	uint32_t bmbx_size;
7492 	struct lpfc_dmabuf *dmabuf;
7493 	struct dma_address *dma_address;
7494 	uint32_t pa_addr;
7495 	uint64_t phys_addr;
7496 
7497 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7498 	if (!dmabuf)
7499 		return -ENOMEM;
7500 
7501 	/*
7502 	 * The bootstrap mailbox region is comprised of 2 parts
7503 	 * plus an alignment restriction of 16 bytes.
7504 	 */
7505 	bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
7506 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7507 					   &dmabuf->phys, GFP_KERNEL);
7508 	if (!dmabuf->virt) {
7509 		kfree(dmabuf);
7510 		return -ENOMEM;
7511 	}
7512 
7513 	/*
7514 	 * Initialize the bootstrap mailbox pointers now so that the register
7515 	 * operations are simple later.  The mailbox dma address is required
7516 	 * to be 16-byte aligned.  Also align the virtual memory as each
7517 	 * maibox is copied into the bmbx mailbox region before issuing the
7518 	 * command to the port.
7519 	 */
7520 	phba->sli4_hba.bmbx.dmabuf = dmabuf;
7521 	phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7522 
7523 	phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7524 					      LPFC_ALIGN_16_BYTE);
7525 	phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7526 					      LPFC_ALIGN_16_BYTE);
7527 
7528 	/*
7529 	 * Set the high and low physical addresses now.  The SLI4 alignment
7530 	 * requirement is 16 bytes and the mailbox is posted to the port
7531 	 * as two 30-bit addresses.  The other data is a bit marking whether
7532 	 * the 30-bit address is the high or low address.
7533 	 * Upcast bmbx aphys to 64bits so shift instruction compiles
7534 	 * clean on 32 bit machines.
7535 	 */
7536 	dma_address = &phba->sli4_hba.bmbx.dma_address;
7537 	phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7538 	pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7539 	dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7540 					   LPFC_BMBX_BIT1_ADDR_HI);
7541 
7542 	pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7543 	dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7544 					   LPFC_BMBX_BIT1_ADDR_LO);
7545 	return 0;
7546 }
7547 
7548 /**
7549  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7550  * @phba: pointer to lpfc hba data structure.
7551  *
7552  * This routine is invoked to teardown the bootstrap mailbox
7553  * region and release all host resources. This routine requires
7554  * the caller to ensure all mailbox commands recovered, no
7555  * additional mailbox comands are sent, and interrupts are disabled
7556  * before calling this routine.
7557  *
7558  **/
7559 static void
lpfc_destroy_bootstrap_mbox(struct lpfc_hba * phba)7560 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7561 {
7562 	dma_free_coherent(&phba->pcidev->dev,
7563 			  phba->sli4_hba.bmbx.bmbx_size,
7564 			  phba->sli4_hba.bmbx.dmabuf->virt,
7565 			  phba->sli4_hba.bmbx.dmabuf->phys);
7566 
7567 	kfree(phba->sli4_hba.bmbx.dmabuf);
7568 	memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7569 }
7570 
7571 /**
7572  * lpfc_sli4_read_config - Get the config parameters.
7573  * @phba: pointer to lpfc hba data structure.
7574  *
7575  * This routine is invoked to read the configuration parameters from the HBA.
7576  * The configuration parameters are used to set the base and maximum values
7577  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7578  * allocation for the port.
7579  *
7580  * Return codes
7581  * 	0 - successful
7582  * 	-ENOMEM - No available memory
7583  *      -EIO - The mailbox failed to complete successfully.
7584  **/
7585 int
lpfc_sli4_read_config(struct lpfc_hba * phba)7586 lpfc_sli4_read_config(struct lpfc_hba *phba)
7587 {
7588 	LPFC_MBOXQ_t *pmb;
7589 	struct lpfc_mbx_read_config *rd_config;
7590 	union  lpfc_sli4_cfg_shdr *shdr;
7591 	uint32_t shdr_status, shdr_add_status;
7592 	struct lpfc_mbx_get_func_cfg *get_func_cfg;
7593 	struct lpfc_rsrc_desc_fcfcoe *desc;
7594 	char *pdesc_0;
7595 	uint16_t forced_link_speed;
7596 	uint32_t if_type;
7597 	int length, i, rc = 0, rc2;
7598 
7599 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7600 	if (!pmb) {
7601 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7602 				"2011 Unable to allocate memory for issuing "
7603 				"SLI_CONFIG_SPECIAL mailbox command\n");
7604 		return -ENOMEM;
7605 	}
7606 
7607 	lpfc_read_config(phba, pmb);
7608 
7609 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7610 	if (rc != MBX_SUCCESS) {
7611 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7612 			"2012 Mailbox failed , mbxCmd x%x "
7613 			"READ_CONFIG, mbxStatus x%x\n",
7614 			bf_get(lpfc_mqe_command, &pmb->u.mqe),
7615 			bf_get(lpfc_mqe_status, &pmb->u.mqe));
7616 		rc = -EIO;
7617 	} else {
7618 		rd_config = &pmb->u.mqe.un.rd_config;
7619 		if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7620 			phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7621 			phba->sli4_hba.lnk_info.lnk_tp =
7622 				bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7623 			phba->sli4_hba.lnk_info.lnk_no =
7624 				bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7625 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7626 					"3081 lnk_type:%d, lnk_numb:%d\n",
7627 					phba->sli4_hba.lnk_info.lnk_tp,
7628 					phba->sli4_hba.lnk_info.lnk_no);
7629 		} else
7630 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7631 					"3082 Mailbox (x%x) returned ldv:x0\n",
7632 					bf_get(lpfc_mqe_command, &pmb->u.mqe));
7633 		if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
7634 			phba->bbcredit_support = 1;
7635 			phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
7636 		}
7637 
7638 		phba->sli4_hba.extents_in_use =
7639 			bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
7640 		phba->sli4_hba.max_cfg_param.max_xri =
7641 			bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7642 		phba->sli4_hba.max_cfg_param.xri_base =
7643 			bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7644 		phba->sli4_hba.max_cfg_param.max_vpi =
7645 			bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7646 		/* Limit the max we support */
7647 		if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
7648 			phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
7649 		phba->sli4_hba.max_cfg_param.vpi_base =
7650 			bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7651 		phba->sli4_hba.max_cfg_param.max_rpi =
7652 			bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7653 		phba->sli4_hba.max_cfg_param.rpi_base =
7654 			bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7655 		phba->sli4_hba.max_cfg_param.max_vfi =
7656 			bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7657 		phba->sli4_hba.max_cfg_param.vfi_base =
7658 			bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7659 		phba->sli4_hba.max_cfg_param.max_fcfi =
7660 			bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
7661 		phba->sli4_hba.max_cfg_param.max_eq =
7662 			bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7663 		phba->sli4_hba.max_cfg_param.max_rq =
7664 			bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7665 		phba->sli4_hba.max_cfg_param.max_wq =
7666 			bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7667 		phba->sli4_hba.max_cfg_param.max_cq =
7668 			bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7669 		phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7670 		phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7671 		phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7672 		phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
7673 		phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7674 				(phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
7675 		phba->max_vports = phba->max_vpi;
7676 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7677 				"2003 cfg params Extents? %d "
7678 				"XRI(B:%d M:%d), "
7679 				"VPI(B:%d M:%d) "
7680 				"VFI(B:%d M:%d) "
7681 				"RPI(B:%d M:%d) "
7682 				"FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
7683 				phba->sli4_hba.extents_in_use,
7684 				phba->sli4_hba.max_cfg_param.xri_base,
7685 				phba->sli4_hba.max_cfg_param.max_xri,
7686 				phba->sli4_hba.max_cfg_param.vpi_base,
7687 				phba->sli4_hba.max_cfg_param.max_vpi,
7688 				phba->sli4_hba.max_cfg_param.vfi_base,
7689 				phba->sli4_hba.max_cfg_param.max_vfi,
7690 				phba->sli4_hba.max_cfg_param.rpi_base,
7691 				phba->sli4_hba.max_cfg_param.max_rpi,
7692 				phba->sli4_hba.max_cfg_param.max_fcfi,
7693 				phba->sli4_hba.max_cfg_param.max_eq,
7694 				phba->sli4_hba.max_cfg_param.max_cq,
7695 				phba->sli4_hba.max_cfg_param.max_wq,
7696 				phba->sli4_hba.max_cfg_param.max_rq);
7697 
7698 	}
7699 
7700 	if (rc)
7701 		goto read_cfg_out;
7702 
7703 	/* Update link speed if forced link speed is supported */
7704 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7705 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7706 		forced_link_speed =
7707 			bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7708 		if (forced_link_speed) {
7709 			phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7710 
7711 			switch (forced_link_speed) {
7712 			case LINK_SPEED_1G:
7713 				phba->cfg_link_speed =
7714 					LPFC_USER_LINK_SPEED_1G;
7715 				break;
7716 			case LINK_SPEED_2G:
7717 				phba->cfg_link_speed =
7718 					LPFC_USER_LINK_SPEED_2G;
7719 				break;
7720 			case LINK_SPEED_4G:
7721 				phba->cfg_link_speed =
7722 					LPFC_USER_LINK_SPEED_4G;
7723 				break;
7724 			case LINK_SPEED_8G:
7725 				phba->cfg_link_speed =
7726 					LPFC_USER_LINK_SPEED_8G;
7727 				break;
7728 			case LINK_SPEED_10G:
7729 				phba->cfg_link_speed =
7730 					LPFC_USER_LINK_SPEED_10G;
7731 				break;
7732 			case LINK_SPEED_16G:
7733 				phba->cfg_link_speed =
7734 					LPFC_USER_LINK_SPEED_16G;
7735 				break;
7736 			case LINK_SPEED_32G:
7737 				phba->cfg_link_speed =
7738 					LPFC_USER_LINK_SPEED_32G;
7739 				break;
7740 			case 0xffff:
7741 				phba->cfg_link_speed =
7742 					LPFC_USER_LINK_SPEED_AUTO;
7743 				break;
7744 			default:
7745 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7746 						"0047 Unrecognized link "
7747 						"speed : %d\n",
7748 						forced_link_speed);
7749 				phba->cfg_link_speed =
7750 					LPFC_USER_LINK_SPEED_AUTO;
7751 			}
7752 		}
7753 	}
7754 
7755 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
7756 	length = phba->sli4_hba.max_cfg_param.max_xri -
7757 			lpfc_sli4_get_els_iocb_cnt(phba);
7758 	if (phba->cfg_hba_queue_depth > length) {
7759 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7760 				"3361 HBA queue depth changed from %d to %d\n",
7761 				phba->cfg_hba_queue_depth, length);
7762 		phba->cfg_hba_queue_depth = length;
7763 	}
7764 
7765 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7766 	    LPFC_SLI_INTF_IF_TYPE_2)
7767 		goto read_cfg_out;
7768 
7769 	/* get the pf# and vf# for SLI4 if_type 2 port */
7770 	length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7771 		  sizeof(struct lpfc_sli4_cfg_mhdr));
7772 	lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7773 			 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7774 			 length, LPFC_SLI4_MBX_EMBED);
7775 
7776 	rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7777 	shdr = (union lpfc_sli4_cfg_shdr *)
7778 				&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7779 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7780 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7781 	if (rc2 || shdr_status || shdr_add_status) {
7782 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7783 				"3026 Mailbox failed , mbxCmd x%x "
7784 				"GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7785 				bf_get(lpfc_mqe_command, &pmb->u.mqe),
7786 				bf_get(lpfc_mqe_status, &pmb->u.mqe));
7787 		goto read_cfg_out;
7788 	}
7789 
7790 	/* search for fc_fcoe resrouce descriptor */
7791 	get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
7792 
7793 	pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7794 	desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7795 	length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7796 	if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7797 		length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7798 	else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7799 		goto read_cfg_out;
7800 
7801 	for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
7802 		desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
7803 		if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
7804 		    bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
7805 			phba->sli4_hba.iov.pf_number =
7806 				bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7807 			phba->sli4_hba.iov.vf_number =
7808 				bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7809 			break;
7810 		}
7811 	}
7812 
7813 	if (i < LPFC_RSRC_DESC_MAX_NUM)
7814 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7815 				"3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7816 				"vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7817 				phba->sli4_hba.iov.vf_number);
7818 	else
7819 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7820 				"3028 GET_FUNCTION_CONFIG: failed to find "
7821 				"Resrouce Descriptor:x%x\n",
7822 				LPFC_RSRC_DESC_TYPE_FCFCOE);
7823 
7824 read_cfg_out:
7825 	mempool_free(pmb, phba->mbox_mem_pool);
7826 	return rc;
7827 }
7828 
7829 /**
7830  * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
7831  * @phba: pointer to lpfc hba data structure.
7832  *
7833  * This routine is invoked to setup the port-side endian order when
7834  * the port if_type is 0.  This routine has no function for other
7835  * if_types.
7836  *
7837  * Return codes
7838  * 	0 - successful
7839  * 	-ENOMEM - No available memory
7840  *      -EIO - The mailbox failed to complete successfully.
7841  **/
7842 static int
lpfc_setup_endian_order(struct lpfc_hba * phba)7843 lpfc_setup_endian_order(struct lpfc_hba *phba)
7844 {
7845 	LPFC_MBOXQ_t *mboxq;
7846 	uint32_t if_type, rc = 0;
7847 	uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7848 				      HOST_ENDIAN_HIGH_WORD1};
7849 
7850 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7851 	switch (if_type) {
7852 	case LPFC_SLI_INTF_IF_TYPE_0:
7853 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7854 						       GFP_KERNEL);
7855 		if (!mboxq) {
7856 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7857 					"0492 Unable to allocate memory for "
7858 					"issuing SLI_CONFIG_SPECIAL mailbox "
7859 					"command\n");
7860 			return -ENOMEM;
7861 		}
7862 
7863 		/*
7864 		 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7865 		 * two words to contain special data values and no other data.
7866 		 */
7867 		memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7868 		memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7869 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7870 		if (rc != MBX_SUCCESS) {
7871 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7872 					"0493 SLI_CONFIG_SPECIAL mailbox "
7873 					"failed with status x%x\n",
7874 					rc);
7875 			rc = -EIO;
7876 		}
7877 		mempool_free(mboxq, phba->mbox_mem_pool);
7878 		break;
7879 	case LPFC_SLI_INTF_IF_TYPE_2:
7880 	case LPFC_SLI_INTF_IF_TYPE_1:
7881 	default:
7882 		break;
7883 	}
7884 	return rc;
7885 }
7886 
7887 /**
7888  * lpfc_sli4_queue_verify - Verify and update EQ counts
7889  * @phba: pointer to lpfc hba data structure.
7890  *
7891  * This routine is invoked to check the user settable queue counts for EQs.
7892  * After this routine is called the counts will be set to valid values that
7893  * adhere to the constraints of the system's interrupt vectors and the port's
7894  * queue resources.
7895  *
7896  * Return codes
7897  *      0 - successful
7898  *      -ENOMEM - No available memory
7899  **/
7900 static int
lpfc_sli4_queue_verify(struct lpfc_hba * phba)7901 lpfc_sli4_queue_verify(struct lpfc_hba *phba)
7902 {
7903 	int io_channel;
7904 	int fof_vectors = phba->cfg_fof ? 1 : 0;
7905 
7906 	/*
7907 	 * Sanity check for configured queue parameters against the run-time
7908 	 * device parameters
7909 	 */
7910 
7911 	/* Sanity check on HBA EQ parameters */
7912 	io_channel = phba->io_channel_irqs;
7913 
7914 	if (phba->sli4_hba.num_online_cpu < io_channel) {
7915 		lpfc_printf_log(phba,
7916 				KERN_ERR, LOG_INIT,
7917 				"3188 Reducing IO channels to match number of "
7918 				"online CPUs: from %d to %d\n",
7919 				io_channel, phba->sli4_hba.num_online_cpu);
7920 		io_channel = phba->sli4_hba.num_online_cpu;
7921 	}
7922 
7923 	if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
7924 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7925 				"2575 Reducing IO channels to match number of "
7926 				"available EQs: from %d to %d\n",
7927 				io_channel,
7928 				phba->sli4_hba.max_cfg_param.max_eq);
7929 		io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
7930 	}
7931 
7932 	/* The actual number of FCP / NVME event queues adopted */
7933 	if (io_channel != phba->io_channel_irqs)
7934 		phba->io_channel_irqs = io_channel;
7935 	if (phba->cfg_fcp_io_channel > io_channel)
7936 		phba->cfg_fcp_io_channel = io_channel;
7937 	if (phba->cfg_nvme_io_channel > io_channel)
7938 		phba->cfg_nvme_io_channel = io_channel;
7939 	if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
7940 		phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
7941 
7942 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7943 			"2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
7944 			phba->io_channel_irqs, phba->cfg_fcp_io_channel,
7945 			phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
7946 
7947 	/* Get EQ depth from module parameter, fake the default for now */
7948 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7949 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7950 
7951 	/* Get CQ depth from module parameter, fake the default for now */
7952 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7953 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7954 	return 0;
7955 }
7956 
7957 static int
lpfc_alloc_nvme_wq_cq(struct lpfc_hba * phba,int wqidx)7958 lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7959 {
7960 	struct lpfc_queue *qdesc;
7961 	int cnt;
7962 
7963 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7964 					    phba->sli4_hba.cq_ecount);
7965 	if (!qdesc) {
7966 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7967 				"0508 Failed allocate fast-path NVME CQ (%d)\n",
7968 				wqidx);
7969 		return 1;
7970 	}
7971 	phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7972 
7973 	cnt = LPFC_NVME_WQSIZE;
7974 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7975 	if (!qdesc) {
7976 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7977 				"0509 Failed allocate fast-path NVME WQ (%d)\n",
7978 				wqidx);
7979 		return 1;
7980 	}
7981 	phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7982 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7983 	return 0;
7984 }
7985 
7986 static int
lpfc_alloc_fcp_wq_cq(struct lpfc_hba * phba,int wqidx)7987 lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7988 {
7989 	struct lpfc_queue *qdesc;
7990 	uint32_t wqesize;
7991 
7992 	/* Create Fast Path FCP CQs */
7993 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7994 					phba->sli4_hba.cq_ecount);
7995 	if (!qdesc) {
7996 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7997 			"0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7998 		return 1;
7999 	}
8000 	phba->sli4_hba.fcp_cq[wqidx] = qdesc;
8001 
8002 	/* Create Fast Path FCP WQs */
8003 	wqesize = (phba->fcp_embed_io) ?
8004 		LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
8005 	qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
8006 	if (!qdesc) {
8007 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8008 				"0503 Failed allocate fast-path FCP WQ (%d)\n",
8009 				wqidx);
8010 		return 1;
8011 	}
8012 	phba->sli4_hba.fcp_wq[wqidx] = qdesc;
8013 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8014 	return 0;
8015 }
8016 
8017 /**
8018  * lpfc_sli4_queue_create - Create all the SLI4 queues
8019  * @phba: pointer to lpfc hba data structure.
8020  *
8021  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8022  * operation. For each SLI4 queue type, the parameters such as queue entry
8023  * count (queue depth) shall be taken from the module parameter. For now,
8024  * we just use some constant number as place holder.
8025  *
8026  * Return codes
8027  *      0 - successful
8028  *      -ENOMEM - No availble memory
8029  *      -EIO - The mailbox failed to complete successfully.
8030  **/
8031 int
lpfc_sli4_queue_create(struct lpfc_hba * phba)8032 lpfc_sli4_queue_create(struct lpfc_hba *phba)
8033 {
8034 	struct lpfc_queue *qdesc;
8035 	int idx, io_channel;
8036 
8037 	/*
8038 	 * Create HBA Record arrays.
8039 	 * Both NVME and FCP will share that same vectors / EQs
8040 	 */
8041 	io_channel = phba->io_channel_irqs;
8042 	if (!io_channel)
8043 		return -ERANGE;
8044 
8045 	phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8046 	phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8047 	phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8048 	phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8049 	phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8050 	phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
8051 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8052 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8053 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8054 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8055 
8056 	phba->sli4_hba.hba_eq =  kcalloc(io_channel,
8057 					sizeof(struct lpfc_queue *),
8058 					GFP_KERNEL);
8059 	if (!phba->sli4_hba.hba_eq) {
8060 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8061 			"2576 Failed allocate memory for "
8062 			"fast-path EQ record array\n");
8063 		goto out_error;
8064 	}
8065 
8066 	if (phba->cfg_fcp_io_channel) {
8067 		phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8068 						sizeof(struct lpfc_queue *),
8069 						GFP_KERNEL);
8070 		if (!phba->sli4_hba.fcp_cq) {
8071 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8072 					"2577 Failed allocate memory for "
8073 					"fast-path CQ record array\n");
8074 			goto out_error;
8075 		}
8076 		phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8077 						sizeof(struct lpfc_queue *),
8078 						GFP_KERNEL);
8079 		if (!phba->sli4_hba.fcp_wq) {
8080 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8081 					"2578 Failed allocate memory for "
8082 					"fast-path FCP WQ record array\n");
8083 			goto out_error;
8084 		}
8085 		/*
8086 		 * Since the first EQ can have multiple CQs associated with it,
8087 		 * this array is used to quickly see if we have a FCP fast-path
8088 		 * CQ match.
8089 		 */
8090 		phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8091 							sizeof(uint16_t),
8092 							GFP_KERNEL);
8093 		if (!phba->sli4_hba.fcp_cq_map) {
8094 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8095 					"2545 Failed allocate memory for "
8096 					"fast-path CQ map\n");
8097 			goto out_error;
8098 		}
8099 	}
8100 
8101 	if (phba->cfg_nvme_io_channel) {
8102 		phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8103 						sizeof(struct lpfc_queue *),
8104 						GFP_KERNEL);
8105 		if (!phba->sli4_hba.nvme_cq) {
8106 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8107 					"6077 Failed allocate memory for "
8108 					"fast-path CQ record array\n");
8109 			goto out_error;
8110 		}
8111 
8112 		phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8113 						sizeof(struct lpfc_queue *),
8114 						GFP_KERNEL);
8115 		if (!phba->sli4_hba.nvme_wq) {
8116 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8117 					"2581 Failed allocate memory for "
8118 					"fast-path NVME WQ record array\n");
8119 			goto out_error;
8120 		}
8121 
8122 		/*
8123 		 * Since the first EQ can have multiple CQs associated with it,
8124 		 * this array is used to quickly see if we have a NVME fast-path
8125 		 * CQ match.
8126 		 */
8127 		phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8128 							sizeof(uint16_t),
8129 							GFP_KERNEL);
8130 		if (!phba->sli4_hba.nvme_cq_map) {
8131 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8132 					"6078 Failed allocate memory for "
8133 					"fast-path CQ map\n");
8134 			goto out_error;
8135 		}
8136 
8137 		if (phba->nvmet_support) {
8138 			phba->sli4_hba.nvmet_cqset = kcalloc(
8139 					phba->cfg_nvmet_mrq,
8140 					sizeof(struct lpfc_queue *),
8141 					GFP_KERNEL);
8142 			if (!phba->sli4_hba.nvmet_cqset) {
8143 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8144 					"3121 Fail allocate memory for "
8145 					"fast-path CQ set array\n");
8146 				goto out_error;
8147 			}
8148 			phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8149 					phba->cfg_nvmet_mrq,
8150 					sizeof(struct lpfc_queue *),
8151 					GFP_KERNEL);
8152 			if (!phba->sli4_hba.nvmet_mrq_hdr) {
8153 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8154 					"3122 Fail allocate memory for "
8155 					"fast-path RQ set hdr array\n");
8156 				goto out_error;
8157 			}
8158 			phba->sli4_hba.nvmet_mrq_data = kcalloc(
8159 					phba->cfg_nvmet_mrq,
8160 					sizeof(struct lpfc_queue *),
8161 					GFP_KERNEL);
8162 			if (!phba->sli4_hba.nvmet_mrq_data) {
8163 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8164 					"3124 Fail allocate memory for "
8165 					"fast-path RQ set data array\n");
8166 				goto out_error;
8167 			}
8168 		}
8169 	}
8170 
8171 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8172 
8173 	/* Create HBA Event Queues (EQs) */
8174 	for (idx = 0; idx < io_channel; idx++) {
8175 		/* Create EQs */
8176 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
8177 					      phba->sli4_hba.eq_ecount);
8178 		if (!qdesc) {
8179 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8180 					"0497 Failed allocate EQ (%d)\n", idx);
8181 			goto out_error;
8182 		}
8183 		phba->sli4_hba.hba_eq[idx] = qdesc;
8184 	}
8185 
8186 	/* FCP and NVME io channels are not required to be balanced */
8187 
8188 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8189 		if (lpfc_alloc_fcp_wq_cq(phba, idx))
8190 			goto out_error;
8191 
8192 	for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8193 		if (lpfc_alloc_nvme_wq_cq(phba, idx))
8194 			goto out_error;
8195 
8196 	if (phba->nvmet_support) {
8197 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8198 			qdesc = lpfc_sli4_queue_alloc(phba,
8199 					phba->sli4_hba.cq_esize,
8200 					phba->sli4_hba.cq_ecount);
8201 			if (!qdesc) {
8202 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8203 					"3142 Failed allocate NVME "
8204 					"CQ Set (%d)\n", idx);
8205 				goto out_error;
8206 			}
8207 			phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8208 		}
8209 	}
8210 
8211 	/*
8212 	 * Create Slow Path Completion Queues (CQs)
8213 	 */
8214 
8215 	/* Create slow-path Mailbox Command Complete Queue */
8216 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8217 				      phba->sli4_hba.cq_ecount);
8218 	if (!qdesc) {
8219 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8220 				"0500 Failed allocate slow-path mailbox CQ\n");
8221 		goto out_error;
8222 	}
8223 	phba->sli4_hba.mbx_cq = qdesc;
8224 
8225 	/* Create slow-path ELS Complete Queue */
8226 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8227 				      phba->sli4_hba.cq_ecount);
8228 	if (!qdesc) {
8229 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8230 				"0501 Failed allocate slow-path ELS CQ\n");
8231 		goto out_error;
8232 	}
8233 	phba->sli4_hba.els_cq = qdesc;
8234 
8235 
8236 	/*
8237 	 * Create Slow Path Work Queues (WQs)
8238 	 */
8239 
8240 	/* Create Mailbox Command Queue */
8241 
8242 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
8243 				      phba->sli4_hba.mq_ecount);
8244 	if (!qdesc) {
8245 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8246 				"0505 Failed allocate slow-path MQ\n");
8247 		goto out_error;
8248 	}
8249 	phba->sli4_hba.mbx_wq = qdesc;
8250 
8251 	/*
8252 	 * Create ELS Work Queues
8253 	 */
8254 
8255 	/* Create slow-path ELS Work Queue */
8256 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8257 				      phba->sli4_hba.wq_ecount);
8258 	if (!qdesc) {
8259 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8260 				"0504 Failed allocate slow-path ELS WQ\n");
8261 		goto out_error;
8262 	}
8263 	phba->sli4_hba.els_wq = qdesc;
8264 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8265 
8266 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8267 		/* Create NVME LS Complete Queue */
8268 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8269 					      phba->sli4_hba.cq_ecount);
8270 		if (!qdesc) {
8271 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8272 					"6079 Failed allocate NVME LS CQ\n");
8273 			goto out_error;
8274 		}
8275 		phba->sli4_hba.nvmels_cq = qdesc;
8276 
8277 		/* Create NVME LS Work Queue */
8278 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8279 					      phba->sli4_hba.wq_ecount);
8280 		if (!qdesc) {
8281 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8282 					"6080 Failed allocate NVME LS WQ\n");
8283 			goto out_error;
8284 		}
8285 		phba->sli4_hba.nvmels_wq = qdesc;
8286 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8287 	}
8288 
8289 	/*
8290 	 * Create Receive Queue (RQ)
8291 	 */
8292 
8293 	/* Create Receive Queue for header */
8294 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8295 				      phba->sli4_hba.rq_ecount);
8296 	if (!qdesc) {
8297 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8298 				"0506 Failed allocate receive HRQ\n");
8299 		goto out_error;
8300 	}
8301 	phba->sli4_hba.hdr_rq = qdesc;
8302 
8303 	/* Create Receive Queue for data */
8304 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8305 				      phba->sli4_hba.rq_ecount);
8306 	if (!qdesc) {
8307 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8308 				"0507 Failed allocate receive DRQ\n");
8309 		goto out_error;
8310 	}
8311 	phba->sli4_hba.dat_rq = qdesc;
8312 
8313 	if (phba->nvmet_support) {
8314 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8315 			/* Create NVMET Receive Queue for header */
8316 			qdesc = lpfc_sli4_queue_alloc(phba,
8317 						      phba->sli4_hba.rq_esize,
8318 						      LPFC_NVMET_RQE_DEF_COUNT);
8319 			if (!qdesc) {
8320 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8321 						"3146 Failed allocate "
8322 						"receive HRQ\n");
8323 				goto out_error;
8324 			}
8325 			phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8326 
8327 			/* Only needed for header of RQ pair */
8328 			qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8329 					      GFP_KERNEL);
8330 			if (qdesc->rqbp == NULL) {
8331 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8332 						"6131 Failed allocate "
8333 						"Header RQBP\n");
8334 				goto out_error;
8335 			}
8336 
8337 			/* Put list in known state in case driver load fails. */
8338 			INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8339 
8340 			/* Create NVMET Receive Queue for data */
8341 			qdesc = lpfc_sli4_queue_alloc(phba,
8342 						      phba->sli4_hba.rq_esize,
8343 						      LPFC_NVMET_RQE_DEF_COUNT);
8344 			if (!qdesc) {
8345 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8346 						"3156 Failed allocate "
8347 						"receive DRQ\n");
8348 				goto out_error;
8349 			}
8350 			phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8351 		}
8352 	}
8353 
8354 	/* Create the Queues needed for Flash Optimized Fabric operations */
8355 	if (phba->cfg_fof)
8356 		lpfc_fof_queue_create(phba);
8357 	return 0;
8358 
8359 out_error:
8360 	lpfc_sli4_queue_destroy(phba);
8361 	return -ENOMEM;
8362 }
8363 
8364 static inline void
__lpfc_sli4_release_queue(struct lpfc_queue ** qp)8365 __lpfc_sli4_release_queue(struct lpfc_queue **qp)
8366 {
8367 	if (*qp != NULL) {
8368 		lpfc_sli4_queue_free(*qp);
8369 		*qp = NULL;
8370 	}
8371 }
8372 
8373 static inline void
lpfc_sli4_release_queues(struct lpfc_queue *** qs,int max)8374 lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8375 {
8376 	int idx;
8377 
8378 	if (*qs == NULL)
8379 		return;
8380 
8381 	for (idx = 0; idx < max; idx++)
8382 		__lpfc_sli4_release_queue(&(*qs)[idx]);
8383 
8384 	kfree(*qs);
8385 	*qs = NULL;
8386 }
8387 
8388 static inline void
lpfc_sli4_release_queue_map(uint16_t ** qmap)8389 lpfc_sli4_release_queue_map(uint16_t **qmap)
8390 {
8391 	if (*qmap != NULL) {
8392 		kfree(*qmap);
8393 		*qmap = NULL;
8394 	}
8395 }
8396 
8397 /**
8398  * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8399  * @phba: pointer to lpfc hba data structure.
8400  *
8401  * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8402  * operation.
8403  *
8404  * Return codes
8405  *      0 - successful
8406  *      -ENOMEM - No available memory
8407  *      -EIO - The mailbox failed to complete successfully.
8408  **/
8409 void
lpfc_sli4_queue_destroy(struct lpfc_hba * phba)8410 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8411 {
8412 	if (phba->cfg_fof)
8413 		lpfc_fof_queue_destroy(phba);
8414 
8415 	/* Release HBA eqs */
8416 	lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
8417 
8418 	/* Release FCP cqs */
8419 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8420 				 phba->cfg_fcp_io_channel);
8421 
8422 	/* Release FCP wqs */
8423 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8424 				 phba->cfg_fcp_io_channel);
8425 
8426 	/* Release FCP CQ mapping array */
8427 	lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8428 
8429 	/* Release NVME cqs */
8430 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8431 					phba->cfg_nvme_io_channel);
8432 
8433 	/* Release NVME wqs */
8434 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8435 					phba->cfg_nvme_io_channel);
8436 
8437 	/* Release NVME CQ mapping array */
8438 	lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
8439 
8440 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8441 					phba->cfg_nvmet_mrq);
8442 
8443 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8444 					phba->cfg_nvmet_mrq);
8445 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8446 					phba->cfg_nvmet_mrq);
8447 
8448 	/* Release mailbox command work queue */
8449 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
8450 
8451 	/* Release ELS work queue */
8452 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8453 
8454 	/* Release ELS work queue */
8455 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
8456 
8457 	/* Release unsolicited receive queue */
8458 	__lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8459 	__lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
8460 
8461 	/* Release ELS complete queue */
8462 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8463 
8464 	/* Release NVME LS complete queue */
8465 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
8466 
8467 	/* Release mailbox command complete queue */
8468 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8469 
8470 	/* Everything on this list has been freed */
8471 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8472 }
8473 
8474 int
lpfc_free_rq_buffer(struct lpfc_hba * phba,struct lpfc_queue * rq)8475 lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8476 {
8477 	struct lpfc_rqb *rqbp;
8478 	struct lpfc_dmabuf *h_buf;
8479 	struct rqb_dmabuf *rqb_buffer;
8480 
8481 	rqbp = rq->rqbp;
8482 	while (!list_empty(&rqbp->rqb_buffer_list)) {
8483 		list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8484 				 struct lpfc_dmabuf, list);
8485 
8486 		rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8487 		(rqbp->rqb_free_buffer)(phba, rqb_buffer);
8488 		rqbp->buffer_count--;
8489 	}
8490 	return 1;
8491 }
8492 
8493 static int
lpfc_create_wq_cq(struct lpfc_hba * phba,struct lpfc_queue * eq,struct lpfc_queue * cq,struct lpfc_queue * wq,uint16_t * cq_map,int qidx,uint32_t qtype)8494 lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8495 	struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8496 	int qidx, uint32_t qtype)
8497 {
8498 	struct lpfc_sli_ring *pring;
8499 	int rc;
8500 
8501 	if (!eq || !cq || !wq) {
8502 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8503 			"6085 Fast-path %s (%d) not allocated\n",
8504 			((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8505 		return -ENOMEM;
8506 	}
8507 
8508 	/* create the Cq first */
8509 	rc = lpfc_cq_create(phba, cq, eq,
8510 			(qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8511 	if (rc) {
8512 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8513 			"6086 Failed setup of CQ (%d), rc = 0x%x\n",
8514 			qidx, (uint32_t)rc);
8515 		return rc;
8516 	}
8517 
8518 	if (qtype != LPFC_MBOX) {
8519 		/* Setup nvme_cq_map for fast lookup */
8520 		if (cq_map)
8521 			*cq_map = cq->queue_id;
8522 
8523 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8524 			"6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8525 			qidx, cq->queue_id, qidx, eq->queue_id);
8526 
8527 		/* create the wq */
8528 		rc = lpfc_wq_create(phba, wq, cq, qtype);
8529 		if (rc) {
8530 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8531 				"6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8532 				qidx, (uint32_t)rc);
8533 			/* no need to tear down cq - caller will do so */
8534 			return rc;
8535 		}
8536 
8537 		/* Bind this CQ/WQ to the NVME ring */
8538 		pring = wq->pring;
8539 		pring->sli.sli4.wqp = (void *)wq;
8540 		cq->pring = pring;
8541 
8542 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8543 			"2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8544 			qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8545 	} else {
8546 		rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8547 		if (rc) {
8548 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8549 				"0539 Failed setup of slow-path MQ: "
8550 				"rc = 0x%x\n", rc);
8551 			/* no need to tear down cq - caller will do so */
8552 			return rc;
8553 		}
8554 
8555 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8556 			"2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8557 			phba->sli4_hba.mbx_wq->queue_id,
8558 			phba->sli4_hba.mbx_cq->queue_id);
8559 	}
8560 
8561 	return 0;
8562 }
8563 
8564 /**
8565  * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8566  * @phba: pointer to lpfc hba data structure.
8567  *
8568  * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8569  * operation.
8570  *
8571  * Return codes
8572  *      0 - successful
8573  *      -ENOMEM - No available memory
8574  *      -EIO - The mailbox failed to complete successfully.
8575  **/
8576 int
lpfc_sli4_queue_setup(struct lpfc_hba * phba)8577 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8578 {
8579 	uint32_t shdr_status, shdr_add_status;
8580 	union lpfc_sli4_cfg_shdr *shdr;
8581 	LPFC_MBOXQ_t *mboxq;
8582 	int qidx;
8583 	uint32_t length, io_channel;
8584 	int rc = -ENOMEM;
8585 
8586 	/* Check for dual-ULP support */
8587 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8588 	if (!mboxq) {
8589 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8590 				"3249 Unable to allocate memory for "
8591 				"QUERY_FW_CFG mailbox command\n");
8592 		return -ENOMEM;
8593 	}
8594 	length = (sizeof(struct lpfc_mbx_query_fw_config) -
8595 		  sizeof(struct lpfc_sli4_cfg_mhdr));
8596 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8597 			 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8598 			 length, LPFC_SLI4_MBX_EMBED);
8599 
8600 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8601 
8602 	shdr = (union lpfc_sli4_cfg_shdr *)
8603 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8604 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8605 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8606 	if (shdr_status || shdr_add_status || rc) {
8607 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8608 				"3250 QUERY_FW_CFG mailbox failed with status "
8609 				"x%x add_status x%x, mbx status x%x\n",
8610 				shdr_status, shdr_add_status, rc);
8611 		if (rc != MBX_TIMEOUT)
8612 			mempool_free(mboxq, phba->mbox_mem_pool);
8613 		rc = -ENXIO;
8614 		goto out_error;
8615 	}
8616 
8617 	phba->sli4_hba.fw_func_mode =
8618 			mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8619 	phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8620 	phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
8621 	phba->sli4_hba.physical_port =
8622 			mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
8623 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8624 			"3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8625 			"ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8626 			phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8627 
8628 	if (rc != MBX_TIMEOUT)
8629 		mempool_free(mboxq, phba->mbox_mem_pool);
8630 
8631 	/*
8632 	 * Set up HBA Event Queues (EQs)
8633 	 */
8634 	io_channel = phba->io_channel_irqs;
8635 
8636 	/* Set up HBA event queue */
8637 	if (io_channel && !phba->sli4_hba.hba_eq) {
8638 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8639 				"3147 Fast-path EQs not allocated\n");
8640 		rc = -ENOMEM;
8641 		goto out_error;
8642 	}
8643 	for (qidx = 0; qidx < io_channel; qidx++) {
8644 		if (!phba->sli4_hba.hba_eq[qidx]) {
8645 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8646 					"0522 Fast-path EQ (%d) not "
8647 					"allocated\n", qidx);
8648 			rc = -ENOMEM;
8649 			goto out_destroy;
8650 		}
8651 		rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8652 						phba->cfg_fcp_imax);
8653 		if (rc) {
8654 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8655 					"0523 Failed setup of fast-path EQ "
8656 					"(%d), rc = 0x%x\n", qidx,
8657 					(uint32_t)rc);
8658 			goto out_destroy;
8659 		}
8660 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8661 				"2584 HBA EQ setup: queue[%d]-id=%d\n",
8662 				qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
8663 	}
8664 
8665 	if (phba->cfg_nvme_io_channel) {
8666 		if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
8667 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8668 				"6084 Fast-path NVME %s array not allocated\n",
8669 				(phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
8670 			rc = -ENOMEM;
8671 			goto out_destroy;
8672 		}
8673 
8674 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8675 			rc = lpfc_create_wq_cq(phba,
8676 					phba->sli4_hba.hba_eq[
8677 						qidx % io_channel],
8678 					phba->sli4_hba.nvme_cq[qidx],
8679 					phba->sli4_hba.nvme_wq[qidx],
8680 					&phba->sli4_hba.nvme_cq_map[qidx],
8681 					qidx, LPFC_NVME);
8682 			if (rc) {
8683 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8684 					"6123 Failed to setup fastpath "
8685 					"NVME WQ/CQ (%d), rc = 0x%x\n",
8686 					qidx, (uint32_t)rc);
8687 				goto out_destroy;
8688 			}
8689 		}
8690 	}
8691 
8692 	if (phba->cfg_fcp_io_channel) {
8693 		/* Set up fast-path FCP Response Complete Queue */
8694 		if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
8695 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8696 				"3148 Fast-path FCP %s array not allocated\n",
8697 				phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
8698 			rc = -ENOMEM;
8699 			goto out_destroy;
8700 		}
8701 
8702 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8703 			rc = lpfc_create_wq_cq(phba,
8704 					phba->sli4_hba.hba_eq[
8705 						qidx % io_channel],
8706 					phba->sli4_hba.fcp_cq[qidx],
8707 					phba->sli4_hba.fcp_wq[qidx],
8708 					&phba->sli4_hba.fcp_cq_map[qidx],
8709 					qidx, LPFC_FCP);
8710 			if (rc) {
8711 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8712 					"0535 Failed to setup fastpath "
8713 					"FCP WQ/CQ (%d), rc = 0x%x\n",
8714 					qidx, (uint32_t)rc);
8715 				goto out_destroy;
8716 			}
8717 		}
8718 	}
8719 
8720 	/*
8721 	 * Set up Slow Path Complete Queues (CQs)
8722 	 */
8723 
8724 	/* Set up slow-path MBOX CQ/MQ */
8725 
8726 	if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
8727 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8728 				"0528 %s not allocated\n",
8729 				phba->sli4_hba.mbx_cq ?
8730 				"Mailbox WQ" : "Mailbox CQ");
8731 		rc = -ENOMEM;
8732 		goto out_destroy;
8733 	}
8734 
8735 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8736 			       phba->sli4_hba.mbx_cq,
8737 			       phba->sli4_hba.mbx_wq,
8738 			       NULL, 0, LPFC_MBOX);
8739 	if (rc) {
8740 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8741 			"0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8742 			(uint32_t)rc);
8743 		goto out_destroy;
8744 	}
8745 	if (phba->nvmet_support) {
8746 		if (!phba->sli4_hba.nvmet_cqset) {
8747 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8748 					"3165 Fast-path NVME CQ Set "
8749 					"array not allocated\n");
8750 			rc = -ENOMEM;
8751 			goto out_destroy;
8752 		}
8753 		if (phba->cfg_nvmet_mrq > 1) {
8754 			rc = lpfc_cq_create_set(phba,
8755 					phba->sli4_hba.nvmet_cqset,
8756 					phba->sli4_hba.hba_eq,
8757 					LPFC_WCQ, LPFC_NVMET);
8758 			if (rc) {
8759 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8760 						"3164 Failed setup of NVME CQ "
8761 						"Set, rc = 0x%x\n",
8762 						(uint32_t)rc);
8763 				goto out_destroy;
8764 			}
8765 		} else {
8766 			/* Set up NVMET Receive Complete Queue */
8767 			rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8768 					    phba->sli4_hba.hba_eq[0],
8769 					    LPFC_WCQ, LPFC_NVMET);
8770 			if (rc) {
8771 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8772 						"6089 Failed setup NVMET CQ: "
8773 						"rc = 0x%x\n", (uint32_t)rc);
8774 				goto out_destroy;
8775 			}
8776 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8777 					"6090 NVMET CQ setup: cq-id=%d, "
8778 					"parent eq-id=%d\n",
8779 					phba->sli4_hba.nvmet_cqset[0]->queue_id,
8780 					phba->sli4_hba.hba_eq[0]->queue_id);
8781 		}
8782 	}
8783 
8784 	/* Set up slow-path ELS WQ/CQ */
8785 	if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
8786 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8787 				"0530 ELS %s not allocated\n",
8788 				phba->sli4_hba.els_cq ? "WQ" : "CQ");
8789 		rc = -ENOMEM;
8790 		goto out_destroy;
8791 	}
8792 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8793 					phba->sli4_hba.els_cq,
8794 					phba->sli4_hba.els_wq,
8795 					NULL, 0, LPFC_ELS);
8796 	if (rc) {
8797 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8798 			"0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8799 			(uint32_t)rc);
8800 		goto out_destroy;
8801 	}
8802 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8803 			"2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8804 			phba->sli4_hba.els_wq->queue_id,
8805 			phba->sli4_hba.els_cq->queue_id);
8806 
8807 	if (phba->cfg_nvme_io_channel) {
8808 		/* Set up NVME LS Complete Queue */
8809 		if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8810 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8811 					"6091 LS %s not allocated\n",
8812 					phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8813 			rc = -ENOMEM;
8814 			goto out_destroy;
8815 		}
8816 		rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8817 					phba->sli4_hba.nvmels_cq,
8818 					phba->sli4_hba.nvmels_wq,
8819 					NULL, 0, LPFC_NVME_LS);
8820 		if (rc) {
8821 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8822 				"0529 Failed setup of NVVME LS WQ/CQ: "
8823 				"rc = 0x%x\n", (uint32_t)rc);
8824 			goto out_destroy;
8825 		}
8826 
8827 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8828 				"6096 ELS WQ setup: wq-id=%d, "
8829 				"parent cq-id=%d\n",
8830 				phba->sli4_hba.nvmels_wq->queue_id,
8831 				phba->sli4_hba.nvmels_cq->queue_id);
8832 	}
8833 
8834 	/*
8835 	 * Create NVMET Receive Queue (RQ)
8836 	 */
8837 	if (phba->nvmet_support) {
8838 		if ((!phba->sli4_hba.nvmet_cqset) ||
8839 		    (!phba->sli4_hba.nvmet_mrq_hdr) ||
8840 		    (!phba->sli4_hba.nvmet_mrq_data)) {
8841 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8842 					"6130 MRQ CQ Queues not "
8843 					"allocated\n");
8844 			rc = -ENOMEM;
8845 			goto out_destroy;
8846 		}
8847 		if (phba->cfg_nvmet_mrq > 1) {
8848 			rc = lpfc_mrq_create(phba,
8849 					     phba->sli4_hba.nvmet_mrq_hdr,
8850 					     phba->sli4_hba.nvmet_mrq_data,
8851 					     phba->sli4_hba.nvmet_cqset,
8852 					     LPFC_NVMET);
8853 			if (rc) {
8854 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8855 						"6098 Failed setup of NVMET "
8856 						"MRQ: rc = 0x%x\n",
8857 						(uint32_t)rc);
8858 				goto out_destroy;
8859 			}
8860 
8861 		} else {
8862 			rc = lpfc_rq_create(phba,
8863 					    phba->sli4_hba.nvmet_mrq_hdr[0],
8864 					    phba->sli4_hba.nvmet_mrq_data[0],
8865 					    phba->sli4_hba.nvmet_cqset[0],
8866 					    LPFC_NVMET);
8867 			if (rc) {
8868 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8869 						"6057 Failed setup of NVMET "
8870 						"Receive Queue: rc = 0x%x\n",
8871 						(uint32_t)rc);
8872 				goto out_destroy;
8873 			}
8874 
8875 			lpfc_printf_log(
8876 				phba, KERN_INFO, LOG_INIT,
8877 				"6099 NVMET RQ setup: hdr-rq-id=%d, "
8878 				"dat-rq-id=%d parent cq-id=%d\n",
8879 				phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
8880 				phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
8881 				phba->sli4_hba.nvmet_cqset[0]->queue_id);
8882 
8883 		}
8884 	}
8885 
8886 	if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8887 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8888 				"0540 Receive Queue not allocated\n");
8889 		rc = -ENOMEM;
8890 		goto out_destroy;
8891 	}
8892 
8893 	rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
8894 			    phba->sli4_hba.els_cq, LPFC_USOL);
8895 	if (rc) {
8896 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8897 				"0541 Failed setup of Receive Queue: "
8898 				"rc = 0x%x\n", (uint32_t)rc);
8899 		goto out_destroy;
8900 	}
8901 
8902 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8903 			"2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8904 			"parent cq-id=%d\n",
8905 			phba->sli4_hba.hdr_rq->queue_id,
8906 			phba->sli4_hba.dat_rq->queue_id,
8907 			phba->sli4_hba.els_cq->queue_id);
8908 
8909 	if (phba->cfg_fof) {
8910 		rc = lpfc_fof_queue_setup(phba);
8911 		if (rc) {
8912 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8913 					"0549 Failed setup of FOF Queues: "
8914 					"rc = 0x%x\n", rc);
8915 			goto out_destroy;
8916 		}
8917 	}
8918 
8919 	for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
8920 		lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
8921 					 phba->cfg_fcp_imax);
8922 
8923 	return 0;
8924 
8925 out_destroy:
8926 	lpfc_sli4_queue_unset(phba);
8927 out_error:
8928 	return rc;
8929 }
8930 
8931 /**
8932  * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8933  * @phba: pointer to lpfc hba data structure.
8934  *
8935  * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8936  * operation.
8937  *
8938  * Return codes
8939  *      0 - successful
8940  *      -ENOMEM - No available memory
8941  *      -EIO - The mailbox failed to complete successfully.
8942  **/
8943 void
lpfc_sli4_queue_unset(struct lpfc_hba * phba)8944 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8945 {
8946 	int qidx;
8947 
8948 	/* Unset the queues created for Flash Optimized Fabric operations */
8949 	if (phba->cfg_fof)
8950 		lpfc_fof_queue_destroy(phba);
8951 
8952 	/* Unset mailbox command work queue */
8953 	if (phba->sli4_hba.mbx_wq)
8954 		lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8955 
8956 	/* Unset NVME LS work queue */
8957 	if (phba->sli4_hba.nvmels_wq)
8958 		lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8959 
8960 	/* Unset ELS work queue */
8961 	if (phba->sli4_hba.els_wq)
8962 		lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8963 
8964 	/* Unset unsolicited receive queue */
8965 	if (phba->sli4_hba.hdr_rq)
8966 		lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8967 				phba->sli4_hba.dat_rq);
8968 
8969 	/* Unset FCP work queue */
8970 	if (phba->sli4_hba.fcp_wq)
8971 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8972 			lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8973 
8974 	/* Unset NVME work queue */
8975 	if (phba->sli4_hba.nvme_wq) {
8976 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8977 			lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
8978 	}
8979 
8980 	/* Unset mailbox command complete queue */
8981 	if (phba->sli4_hba.mbx_cq)
8982 		lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8983 
8984 	/* Unset ELS complete queue */
8985 	if (phba->sli4_hba.els_cq)
8986 		lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8987 
8988 	/* Unset NVME LS complete queue */
8989 	if (phba->sli4_hba.nvmels_cq)
8990 		lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8991 
8992 	/* Unset NVME response complete queue */
8993 	if (phba->sli4_hba.nvme_cq)
8994 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8995 			lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8996 
8997 	/* Unset NVMET MRQ queue */
8998 	if (phba->sli4_hba.nvmet_mrq_hdr) {
8999 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9000 			lpfc_rq_destroy(phba,
9001 					phba->sli4_hba.nvmet_mrq_hdr[qidx],
9002 					phba->sli4_hba.nvmet_mrq_data[qidx]);
9003 	}
9004 
9005 	/* Unset NVMET CQ Set complete queue */
9006 	if (phba->sli4_hba.nvmet_cqset) {
9007 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9008 			lpfc_cq_destroy(phba,
9009 					phba->sli4_hba.nvmet_cqset[qidx]);
9010 	}
9011 
9012 	/* Unset FCP response complete queue */
9013 	if (phba->sli4_hba.fcp_cq)
9014 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9015 			lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
9016 
9017 	/* Unset fast-path event queue */
9018 	if (phba->sli4_hba.hba_eq)
9019 		for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
9020 			lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
9021 }
9022 
9023 /**
9024  * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9025  * @phba: pointer to lpfc hba data structure.
9026  *
9027  * This routine is invoked to allocate and set up a pool of completion queue
9028  * events. The body of the completion queue event is a completion queue entry
9029  * CQE. For now, this pool is used for the interrupt service routine to queue
9030  * the following HBA completion queue events for the worker thread to process:
9031  *   - Mailbox asynchronous events
9032  *   - Receive queue completion unsolicited events
9033  * Later, this can be used for all the slow-path events.
9034  *
9035  * Return codes
9036  *      0 - successful
9037  *      -ENOMEM - No available memory
9038  **/
9039 static int
lpfc_sli4_cq_event_pool_create(struct lpfc_hba * phba)9040 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9041 {
9042 	struct lpfc_cq_event *cq_event;
9043 	int i;
9044 
9045 	for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9046 		cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9047 		if (!cq_event)
9048 			goto out_pool_create_fail;
9049 		list_add_tail(&cq_event->list,
9050 			      &phba->sli4_hba.sp_cqe_event_pool);
9051 	}
9052 	return 0;
9053 
9054 out_pool_create_fail:
9055 	lpfc_sli4_cq_event_pool_destroy(phba);
9056 	return -ENOMEM;
9057 }
9058 
9059 /**
9060  * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9061  * @phba: pointer to lpfc hba data structure.
9062  *
9063  * This routine is invoked to free the pool of completion queue events at
9064  * driver unload time. Note that, it is the responsibility of the driver
9065  * cleanup routine to free all the outstanding completion-queue events
9066  * allocated from this pool back into the pool before invoking this routine
9067  * to destroy the pool.
9068  **/
9069 static void
lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba * phba)9070 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9071 {
9072 	struct lpfc_cq_event *cq_event, *next_cq_event;
9073 
9074 	list_for_each_entry_safe(cq_event, next_cq_event,
9075 				 &phba->sli4_hba.sp_cqe_event_pool, list) {
9076 		list_del(&cq_event->list);
9077 		kfree(cq_event);
9078 	}
9079 }
9080 
9081 /**
9082  * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9083  * @phba: pointer to lpfc hba data structure.
9084  *
9085  * This routine is the lock free version of the API invoked to allocate a
9086  * completion-queue event from the free pool.
9087  *
9088  * Return: Pointer to the newly allocated completion-queue event if successful
9089  *         NULL otherwise.
9090  **/
9091 struct lpfc_cq_event *
__lpfc_sli4_cq_event_alloc(struct lpfc_hba * phba)9092 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9093 {
9094 	struct lpfc_cq_event *cq_event = NULL;
9095 
9096 	list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9097 			 struct lpfc_cq_event, list);
9098 	return cq_event;
9099 }
9100 
9101 /**
9102  * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9103  * @phba: pointer to lpfc hba data structure.
9104  *
9105  * This routine is the lock version of the API invoked to allocate a
9106  * completion-queue event from the free pool.
9107  *
9108  * Return: Pointer to the newly allocated completion-queue event if successful
9109  *         NULL otherwise.
9110  **/
9111 struct lpfc_cq_event *
lpfc_sli4_cq_event_alloc(struct lpfc_hba * phba)9112 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9113 {
9114 	struct lpfc_cq_event *cq_event;
9115 	unsigned long iflags;
9116 
9117 	spin_lock_irqsave(&phba->hbalock, iflags);
9118 	cq_event = __lpfc_sli4_cq_event_alloc(phba);
9119 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9120 	return cq_event;
9121 }
9122 
9123 /**
9124  * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9125  * @phba: pointer to lpfc hba data structure.
9126  * @cq_event: pointer to the completion queue event to be freed.
9127  *
9128  * This routine is the lock free version of the API invoked to release a
9129  * completion-queue event back into the free pool.
9130  **/
9131 void
__lpfc_sli4_cq_event_release(struct lpfc_hba * phba,struct lpfc_cq_event * cq_event)9132 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9133 			     struct lpfc_cq_event *cq_event)
9134 {
9135 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9136 }
9137 
9138 /**
9139  * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9140  * @phba: pointer to lpfc hba data structure.
9141  * @cq_event: pointer to the completion queue event to be freed.
9142  *
9143  * This routine is the lock version of the API invoked to release a
9144  * completion-queue event back into the free pool.
9145  **/
9146 void
lpfc_sli4_cq_event_release(struct lpfc_hba * phba,struct lpfc_cq_event * cq_event)9147 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9148 			   struct lpfc_cq_event *cq_event)
9149 {
9150 	unsigned long iflags;
9151 	spin_lock_irqsave(&phba->hbalock, iflags);
9152 	__lpfc_sli4_cq_event_release(phba, cq_event);
9153 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9154 }
9155 
9156 /**
9157  * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9158  * @phba: pointer to lpfc hba data structure.
9159  *
9160  * This routine is to free all the pending completion-queue events to the
9161  * back into the free pool for device reset.
9162  **/
9163 static void
lpfc_sli4_cq_event_release_all(struct lpfc_hba * phba)9164 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9165 {
9166 	LIST_HEAD(cqelist);
9167 	struct lpfc_cq_event *cqe;
9168 	unsigned long iflags;
9169 
9170 	/* Retrieve all the pending WCQEs from pending WCQE lists */
9171 	spin_lock_irqsave(&phba->hbalock, iflags);
9172 	/* Pending FCP XRI abort events */
9173 	list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9174 			 &cqelist);
9175 	/* Pending ELS XRI abort events */
9176 	list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9177 			 &cqelist);
9178 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9179 		/* Pending NVME XRI abort events */
9180 		list_splice_init(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
9181 				 &cqelist);
9182 	}
9183 	/* Pending asynnc events */
9184 	list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9185 			 &cqelist);
9186 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9187 
9188 	while (!list_empty(&cqelist)) {
9189 		list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9190 		lpfc_sli4_cq_event_release(phba, cqe);
9191 	}
9192 }
9193 
9194 /**
9195  * lpfc_pci_function_reset - Reset pci function.
9196  * @phba: pointer to lpfc hba data structure.
9197  *
9198  * This routine is invoked to request a PCI function reset. It will destroys
9199  * all resources assigned to the PCI function which originates this request.
9200  *
9201  * Return codes
9202  *      0 - successful
9203  *      -ENOMEM - No available memory
9204  *      -EIO - The mailbox failed to complete successfully.
9205  **/
9206 int
lpfc_pci_function_reset(struct lpfc_hba * phba)9207 lpfc_pci_function_reset(struct lpfc_hba *phba)
9208 {
9209 	LPFC_MBOXQ_t *mboxq;
9210 	uint32_t rc = 0, if_type;
9211 	uint32_t shdr_status, shdr_add_status;
9212 	uint32_t rdy_chk;
9213 	uint32_t port_reset = 0;
9214 	union lpfc_sli4_cfg_shdr *shdr;
9215 	struct lpfc_register reg_data;
9216 	uint16_t devid;
9217 
9218 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9219 	switch (if_type) {
9220 	case LPFC_SLI_INTF_IF_TYPE_0:
9221 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9222 						       GFP_KERNEL);
9223 		if (!mboxq) {
9224 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9225 					"0494 Unable to allocate memory for "
9226 					"issuing SLI_FUNCTION_RESET mailbox "
9227 					"command\n");
9228 			return -ENOMEM;
9229 		}
9230 
9231 		/* Setup PCI function reset mailbox-ioctl command */
9232 		lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9233 				 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9234 				 LPFC_SLI4_MBX_EMBED);
9235 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9236 		shdr = (union lpfc_sli4_cfg_shdr *)
9237 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9238 		shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9239 		shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9240 					 &shdr->response);
9241 		if (rc != MBX_TIMEOUT)
9242 			mempool_free(mboxq, phba->mbox_mem_pool);
9243 		if (shdr_status || shdr_add_status || rc) {
9244 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9245 					"0495 SLI_FUNCTION_RESET mailbox "
9246 					"failed with status x%x add_status x%x,"
9247 					" mbx status x%x\n",
9248 					shdr_status, shdr_add_status, rc);
9249 			rc = -ENXIO;
9250 		}
9251 		break;
9252 	case LPFC_SLI_INTF_IF_TYPE_2:
9253 wait:
9254 		/*
9255 		 * Poll the Port Status Register and wait for RDY for
9256 		 * up to 30 seconds. If the port doesn't respond, treat
9257 		 * it as an error.
9258 		 */
9259 		for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
9260 			if (lpfc_readl(phba->sli4_hba.u.if_type2.
9261 				STATUSregaddr, &reg_data.word0)) {
9262 				rc = -ENODEV;
9263 				goto out;
9264 			}
9265 			if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9266 				break;
9267 			msleep(20);
9268 		}
9269 
9270 		if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9271 			phba->work_status[0] = readl(
9272 				phba->sli4_hba.u.if_type2.ERR1regaddr);
9273 			phba->work_status[1] = readl(
9274 				phba->sli4_hba.u.if_type2.ERR2regaddr);
9275 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9276 					"2890 Port not ready, port status reg "
9277 					"0x%x error 1=0x%x, error 2=0x%x\n",
9278 					reg_data.word0,
9279 					phba->work_status[0],
9280 					phba->work_status[1]);
9281 			rc = -ENODEV;
9282 			goto out;
9283 		}
9284 
9285 		if (!port_reset) {
9286 			/*
9287 			 * Reset the port now
9288 			 */
9289 			reg_data.word0 = 0;
9290 			bf_set(lpfc_sliport_ctrl_end, &reg_data,
9291 			       LPFC_SLIPORT_LITTLE_ENDIAN);
9292 			bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9293 			       LPFC_SLIPORT_INIT_PORT);
9294 			writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9295 			       CTRLregaddr);
9296 			/* flush */
9297 			pci_read_config_word(phba->pcidev,
9298 					     PCI_DEVICE_ID, &devid);
9299 
9300 			port_reset = 1;
9301 			msleep(20);
9302 			goto wait;
9303 		} else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9304 			rc = -ENODEV;
9305 			goto out;
9306 		}
9307 		break;
9308 
9309 	case LPFC_SLI_INTF_IF_TYPE_1:
9310 	default:
9311 		break;
9312 	}
9313 
9314 out:
9315 	/* Catch the not-ready port failure after a port reset. */
9316 	if (rc) {
9317 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9318 				"3317 HBA not functional: IP Reset Failed "
9319 				"try: echo fw_reset > board_mode\n");
9320 		rc = -ENODEV;
9321 	}
9322 
9323 	return rc;
9324 }
9325 
9326 /**
9327  * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9328  * @phba: pointer to lpfc hba data structure.
9329  *
9330  * This routine is invoked to set up the PCI device memory space for device
9331  * with SLI-4 interface spec.
9332  *
9333  * Return codes
9334  * 	0 - successful
9335  * 	other values - error
9336  **/
9337 static int
lpfc_sli4_pci_mem_setup(struct lpfc_hba * phba)9338 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9339 {
9340 	struct pci_dev *pdev;
9341 	unsigned long bar0map_len, bar1map_len, bar2map_len;
9342 	int error = -ENODEV;
9343 	uint32_t if_type;
9344 
9345 	/* Obtain PCI device reference */
9346 	if (!phba->pcidev)
9347 		return error;
9348 	else
9349 		pdev = phba->pcidev;
9350 
9351 	/* Set the device DMA mask size */
9352 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9353 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9354 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9355 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
9356 			return error;
9357 		}
9358 	}
9359 
9360 	/*
9361 	 * The BARs and register set definitions and offset locations are
9362 	 * dependent on the if_type.
9363 	 */
9364 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9365 				  &phba->sli4_hba.sli_intf.word0)) {
9366 		return error;
9367 	}
9368 
9369 	/* There is no SLI3 failback for SLI4 devices. */
9370 	if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9371 	    LPFC_SLI_INTF_VALID) {
9372 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9373 				"2894 SLI_INTF reg contents invalid "
9374 				"sli_intf reg 0x%x\n",
9375 				phba->sli4_hba.sli_intf.word0);
9376 		return error;
9377 	}
9378 
9379 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9380 	/*
9381 	 * Get the bus address of SLI4 device Bar regions and the
9382 	 * number of bytes required by each mapping. The mapping of the
9383 	 * particular PCI BARs regions is dependent on the type of
9384 	 * SLI4 device.
9385 	 */
9386 	if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9387 		phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9388 		bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
9389 
9390 		/*
9391 		 * Map SLI4 PCI Config Space Register base to a kernel virtual
9392 		 * addr
9393 		 */
9394 		phba->sli4_hba.conf_regs_memmap_p =
9395 			ioremap(phba->pci_bar0_map, bar0map_len);
9396 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9397 			dev_printk(KERN_ERR, &pdev->dev,
9398 				   "ioremap failed for SLI4 PCI config "
9399 				   "registers.\n");
9400 			goto out;
9401 		}
9402 		phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
9403 		/* Set up BAR0 PCI config space register memory map */
9404 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9405 	} else {
9406 		phba->pci_bar0_map = pci_resource_start(pdev, 1);
9407 		bar0map_len = pci_resource_len(pdev, 1);
9408 		if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9409 			dev_printk(KERN_ERR, &pdev->dev,
9410 			   "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9411 			goto out;
9412 		}
9413 		phba->sli4_hba.conf_regs_memmap_p =
9414 				ioremap(phba->pci_bar0_map, bar0map_len);
9415 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9416 			dev_printk(KERN_ERR, &pdev->dev,
9417 				"ioremap failed for SLI4 PCI config "
9418 				"registers.\n");
9419 				goto out;
9420 		}
9421 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9422 	}
9423 
9424 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9425 		if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
9426 			/*
9427 			 * Map SLI4 if type 0 HBA Control Register base to a
9428 			 * kernel virtual address and setup the registers.
9429 			 */
9430 			phba->pci_bar1_map = pci_resource_start(pdev,
9431 								PCI_64BIT_BAR2);
9432 			bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9433 			phba->sli4_hba.ctrl_regs_memmap_p =
9434 					ioremap(phba->pci_bar1_map,
9435 						bar1map_len);
9436 			if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9437 				dev_err(&pdev->dev,
9438 					   "ioremap failed for SLI4 HBA "
9439 					    "control registers.\n");
9440 				error = -ENOMEM;
9441 				goto out_iounmap_conf;
9442 			}
9443 			phba->pci_bar2_memmap_p =
9444 					 phba->sli4_hba.ctrl_regs_memmap_p;
9445 			lpfc_sli4_bar1_register_memmap(phba);
9446 		} else {
9447 			error = -ENOMEM;
9448 			goto out_iounmap_conf;
9449 		}
9450 	}
9451 
9452 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9453 		if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9454 			/*
9455 			 * Map SLI4 if type 0 HBA Doorbell Register base to
9456 			 * a kernel virtual address and setup the registers.
9457 			 */
9458 			phba->pci_bar2_map = pci_resource_start(pdev,
9459 								PCI_64BIT_BAR4);
9460 			bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9461 			phba->sli4_hba.drbl_regs_memmap_p =
9462 					ioremap(phba->pci_bar2_map,
9463 						bar2map_len);
9464 			if (!phba->sli4_hba.drbl_regs_memmap_p) {
9465 				dev_err(&pdev->dev,
9466 					   "ioremap failed for SLI4 HBA"
9467 					   " doorbell registers.\n");
9468 				error = -ENOMEM;
9469 				goto out_iounmap_ctrl;
9470 			}
9471 			phba->pci_bar4_memmap_p =
9472 					phba->sli4_hba.drbl_regs_memmap_p;
9473 			error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9474 			if (error)
9475 				goto out_iounmap_all;
9476 		} else {
9477 			error = -ENOMEM;
9478 			goto out_iounmap_all;
9479 		}
9480 	}
9481 
9482 	return 0;
9483 
9484 out_iounmap_all:
9485 	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9486 out_iounmap_ctrl:
9487 	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9488 out_iounmap_conf:
9489 	iounmap(phba->sli4_hba.conf_regs_memmap_p);
9490 out:
9491 	return error;
9492 }
9493 
9494 /**
9495  * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9496  * @phba: pointer to lpfc hba data structure.
9497  *
9498  * This routine is invoked to unset the PCI device memory space for device
9499  * with SLI-4 interface spec.
9500  **/
9501 static void
lpfc_sli4_pci_mem_unset(struct lpfc_hba * phba)9502 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9503 {
9504 	uint32_t if_type;
9505 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9506 
9507 	switch (if_type) {
9508 	case LPFC_SLI_INTF_IF_TYPE_0:
9509 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9510 		iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9511 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9512 		break;
9513 	case LPFC_SLI_INTF_IF_TYPE_2:
9514 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9515 		break;
9516 	case LPFC_SLI_INTF_IF_TYPE_1:
9517 	default:
9518 		dev_printk(KERN_ERR, &phba->pcidev->dev,
9519 			   "FATAL - unsupported SLI4 interface type - %d\n",
9520 			   if_type);
9521 		break;
9522 	}
9523 }
9524 
9525 /**
9526  * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9527  * @phba: pointer to lpfc hba data structure.
9528  *
9529  * This routine is invoked to enable the MSI-X interrupt vectors to device
9530  * with SLI-3 interface specs.
9531  *
9532  * Return codes
9533  *   0 - successful
9534  *   other values - error
9535  **/
9536 static int
lpfc_sli_enable_msix(struct lpfc_hba * phba)9537 lpfc_sli_enable_msix(struct lpfc_hba *phba)
9538 {
9539 	int rc;
9540 	LPFC_MBOXQ_t *pmb;
9541 
9542 	/* Set up MSI-X multi-message vectors */
9543 	rc = pci_alloc_irq_vectors(phba->pcidev,
9544 			LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9545 	if (rc < 0) {
9546 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9547 				"0420 PCI enable MSI-X failed (%d)\n", rc);
9548 		goto vec_fail_out;
9549 	}
9550 
9551 	/*
9552 	 * Assign MSI-X vectors to interrupt handlers
9553 	 */
9554 
9555 	/* vector-0 is associated to slow-path handler */
9556 	rc = request_irq(pci_irq_vector(phba->pcidev, 0),
9557 			 &lpfc_sli_sp_intr_handler, 0,
9558 			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9559 	if (rc) {
9560 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9561 				"0421 MSI-X slow-path request_irq failed "
9562 				"(%d)\n", rc);
9563 		goto msi_fail_out;
9564 	}
9565 
9566 	/* vector-1 is associated to fast-path handler */
9567 	rc = request_irq(pci_irq_vector(phba->pcidev, 1),
9568 			 &lpfc_sli_fp_intr_handler, 0,
9569 			 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9570 
9571 	if (rc) {
9572 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9573 				"0429 MSI-X fast-path request_irq failed "
9574 				"(%d)\n", rc);
9575 		goto irq_fail_out;
9576 	}
9577 
9578 	/*
9579 	 * Configure HBA MSI-X attention conditions to messages
9580 	 */
9581 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9582 
9583 	if (!pmb) {
9584 		rc = -ENOMEM;
9585 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9586 				"0474 Unable to allocate memory for issuing "
9587 				"MBOX_CONFIG_MSI command\n");
9588 		goto mem_fail_out;
9589 	}
9590 	rc = lpfc_config_msi(phba, pmb);
9591 	if (rc)
9592 		goto mbx_fail_out;
9593 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9594 	if (rc != MBX_SUCCESS) {
9595 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9596 				"0351 Config MSI mailbox command failed, "
9597 				"mbxCmd x%x, mbxStatus x%x\n",
9598 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9599 		goto mbx_fail_out;
9600 	}
9601 
9602 	/* Free memory allocated for mailbox command */
9603 	mempool_free(pmb, phba->mbox_mem_pool);
9604 	return rc;
9605 
9606 mbx_fail_out:
9607 	/* Free memory allocated for mailbox command */
9608 	mempool_free(pmb, phba->mbox_mem_pool);
9609 
9610 mem_fail_out:
9611 	/* free the irq already requested */
9612 	free_irq(pci_irq_vector(phba->pcidev, 1), phba);
9613 
9614 irq_fail_out:
9615 	/* free the irq already requested */
9616 	free_irq(pci_irq_vector(phba->pcidev, 0), phba);
9617 
9618 msi_fail_out:
9619 	/* Unconfigure MSI-X capability structure */
9620 	pci_free_irq_vectors(phba->pcidev);
9621 
9622 vec_fail_out:
9623 	return rc;
9624 }
9625 
9626 /**
9627  * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9628  * @phba: pointer to lpfc hba data structure.
9629  *
9630  * This routine is invoked to enable the MSI interrupt mode to device with
9631  * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9632  * enable the MSI vector. The device driver is responsible for calling the
9633  * request_irq() to register MSI vector with a interrupt the handler, which
9634  * is done in this function.
9635  *
9636  * Return codes
9637  * 	0 - successful
9638  * 	other values - error
9639  */
9640 static int
lpfc_sli_enable_msi(struct lpfc_hba * phba)9641 lpfc_sli_enable_msi(struct lpfc_hba *phba)
9642 {
9643 	int rc;
9644 
9645 	rc = pci_enable_msi(phba->pcidev);
9646 	if (!rc)
9647 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9648 				"0462 PCI enable MSI mode success.\n");
9649 	else {
9650 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9651 				"0471 PCI enable MSI mode failed (%d)\n", rc);
9652 		return rc;
9653 	}
9654 
9655 	rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9656 			 0, LPFC_DRIVER_NAME, phba);
9657 	if (rc) {
9658 		pci_disable_msi(phba->pcidev);
9659 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9660 				"0478 MSI request_irq failed (%d)\n", rc);
9661 	}
9662 	return rc;
9663 }
9664 
9665 /**
9666  * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9667  * @phba: pointer to lpfc hba data structure.
9668  *
9669  * This routine is invoked to enable device interrupt and associate driver's
9670  * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9671  * spec. Depends on the interrupt mode configured to the driver, the driver
9672  * will try to fallback from the configured interrupt mode to an interrupt
9673  * mode which is supported by the platform, kernel, and device in the order
9674  * of:
9675  * MSI-X -> MSI -> IRQ.
9676  *
9677  * Return codes
9678  *   0 - successful
9679  *   other values - error
9680  **/
9681 static uint32_t
lpfc_sli_enable_intr(struct lpfc_hba * phba,uint32_t cfg_mode)9682 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9683 {
9684 	uint32_t intr_mode = LPFC_INTR_ERROR;
9685 	int retval;
9686 
9687 	if (cfg_mode == 2) {
9688 		/* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9689 		retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9690 		if (!retval) {
9691 			/* Now, try to enable MSI-X interrupt mode */
9692 			retval = lpfc_sli_enable_msix(phba);
9693 			if (!retval) {
9694 				/* Indicate initialization to MSI-X mode */
9695 				phba->intr_type = MSIX;
9696 				intr_mode = 2;
9697 			}
9698 		}
9699 	}
9700 
9701 	/* Fallback to MSI if MSI-X initialization failed */
9702 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9703 		retval = lpfc_sli_enable_msi(phba);
9704 		if (!retval) {
9705 			/* Indicate initialization to MSI mode */
9706 			phba->intr_type = MSI;
9707 			intr_mode = 1;
9708 		}
9709 	}
9710 
9711 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9712 	if (phba->intr_type == NONE) {
9713 		retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9714 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9715 		if (!retval) {
9716 			/* Indicate initialization to INTx mode */
9717 			phba->intr_type = INTx;
9718 			intr_mode = 0;
9719 		}
9720 	}
9721 	return intr_mode;
9722 }
9723 
9724 /**
9725  * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9726  * @phba: pointer to lpfc hba data structure.
9727  *
9728  * This routine is invoked to disable device interrupt and disassociate the
9729  * driver's interrupt handler(s) from interrupt vector(s) to device with
9730  * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9731  * release the interrupt vector(s) for the message signaled interrupt.
9732  **/
9733 static void
lpfc_sli_disable_intr(struct lpfc_hba * phba)9734 lpfc_sli_disable_intr(struct lpfc_hba *phba)
9735 {
9736 	int nr_irqs, i;
9737 
9738 	if (phba->intr_type == MSIX)
9739 		nr_irqs = LPFC_MSIX_VECTORS;
9740 	else
9741 		nr_irqs = 1;
9742 
9743 	for (i = 0; i < nr_irqs; i++)
9744 		free_irq(pci_irq_vector(phba->pcidev, i), phba);
9745 	pci_free_irq_vectors(phba->pcidev);
9746 
9747 	/* Reset interrupt management states */
9748 	phba->intr_type = NONE;
9749 	phba->sli.slistat.sli_intr = 0;
9750 }
9751 
9752 /**
9753  * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
9754  * @phba: pointer to lpfc hba data structure.
9755  * @vectors: number of msix vectors allocated.
9756  *
9757  * The routine will figure out the CPU affinity assignment for every
9758  * MSI-X vector allocated for the HBA.  The hba_eq_hdl will be updated
9759  * with a pointer to the CPU mask that defines ALL the CPUs this vector
9760  * can be associated with. If the vector can be unquely associated with
9761  * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9762  * In addition, the CPU to IO channel mapping will be calculated
9763  * and the phba->sli4_hba.cpu_map array will reflect this.
9764  */
9765 static void
lpfc_cpu_affinity_check(struct lpfc_hba * phba,int vectors)9766 lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
9767 {
9768 	struct lpfc_vector_map_info *cpup;
9769 	int index = 0;
9770 	int vec = 0;
9771 	int cpu;
9772 #ifdef CONFIG_X86
9773 	struct cpuinfo_x86 *cpuinfo;
9774 #endif
9775 
9776 	/* Init cpu_map array */
9777 	memset(phba->sli4_hba.cpu_map, 0xff,
9778 	       (sizeof(struct lpfc_vector_map_info) *
9779 	       phba->sli4_hba.num_present_cpu));
9780 
9781 	/* Update CPU map with physical id and core id of each CPU */
9782 	cpup = phba->sli4_hba.cpu_map;
9783 	for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9784 #ifdef CONFIG_X86
9785 		cpuinfo = &cpu_data(cpu);
9786 		cpup->phys_id = cpuinfo->phys_proc_id;
9787 		cpup->core_id = cpuinfo->cpu_core_id;
9788 #else
9789 		/* No distinction between CPUs for other platforms */
9790 		cpup->phys_id = 0;
9791 		cpup->core_id = 0;
9792 #endif
9793 		cpup->channel_id = index;  /* For now round robin */
9794 		cpup->irq = pci_irq_vector(phba->pcidev, vec);
9795 		vec++;
9796 		if (vec >= vectors)
9797 			vec = 0;
9798 		index++;
9799 		if (index >= phba->cfg_fcp_io_channel)
9800 			index = 0;
9801 		cpup++;
9802 	}
9803 }
9804 
9805 
9806 /**
9807  * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9808  * @phba: pointer to lpfc hba data structure.
9809  *
9810  * This routine is invoked to enable the MSI-X interrupt vectors to device
9811  * with SLI-4 interface spec.
9812  *
9813  * Return codes
9814  * 0 - successful
9815  * other values - error
9816  **/
9817 static int
lpfc_sli4_enable_msix(struct lpfc_hba * phba)9818 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9819 {
9820 	int vectors, rc, index;
9821 	char *name;
9822 
9823 	/* Set up MSI-X multi-message vectors */
9824 	vectors = phba->io_channel_irqs;
9825 	if (phba->cfg_fof)
9826 		vectors++;
9827 
9828 	rc = pci_alloc_irq_vectors(phba->pcidev,
9829 				(phba->nvmet_support) ? 1 : 2,
9830 				vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
9831 	if (rc < 0) {
9832 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9833 				"0484 PCI enable MSI-X failed (%d)\n", rc);
9834 		goto vec_fail_out;
9835 	}
9836 	vectors = rc;
9837 
9838 	/* Assign MSI-X vectors to interrupt handlers */
9839 	for (index = 0; index < vectors; index++) {
9840 		name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
9841 		memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
9842 		snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
9843 			 LPFC_DRIVER_HANDLER_NAME"%d", index);
9844 
9845 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9846 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9847 		atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
9848 		if (phba->cfg_fof && (index == (vectors - 1)))
9849 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
9850 				 &lpfc_sli4_fof_intr_handler, 0,
9851 				 name,
9852 				 &phba->sli4_hba.hba_eq_hdl[index]);
9853 		else
9854 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
9855 				 &lpfc_sli4_hba_intr_handler, 0,
9856 				 name,
9857 				 &phba->sli4_hba.hba_eq_hdl[index]);
9858 		if (rc) {
9859 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9860 					"0486 MSI-X fast-path (%d) "
9861 					"request_irq failed (%d)\n", index, rc);
9862 			goto cfg_fail_out;
9863 		}
9864 	}
9865 
9866 	if (phba->cfg_fof)
9867 		vectors--;
9868 
9869 	if (vectors != phba->io_channel_irqs) {
9870 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9871 				"3238 Reducing IO channels to match number of "
9872 				"MSI-X vectors, requested %d got %d\n",
9873 				phba->io_channel_irqs, vectors);
9874 		if (phba->cfg_fcp_io_channel > vectors)
9875 			phba->cfg_fcp_io_channel = vectors;
9876 		if (phba->cfg_nvme_io_channel > vectors)
9877 			phba->cfg_nvme_io_channel = vectors;
9878 		if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9879 			phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9880 		else
9881 			phba->io_channel_irqs = phba->cfg_nvme_io_channel;
9882 	}
9883 	lpfc_cpu_affinity_check(phba, vectors);
9884 
9885 	return rc;
9886 
9887 cfg_fail_out:
9888 	/* free the irq already requested */
9889 	for (--index; index >= 0; index--)
9890 		free_irq(pci_irq_vector(phba->pcidev, index),
9891 				&phba->sli4_hba.hba_eq_hdl[index]);
9892 
9893 	/* Unconfigure MSI-X capability structure */
9894 	pci_free_irq_vectors(phba->pcidev);
9895 
9896 vec_fail_out:
9897 	return rc;
9898 }
9899 
9900 /**
9901  * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9902  * @phba: pointer to lpfc hba data structure.
9903  *
9904  * This routine is invoked to enable the MSI interrupt mode to device with
9905  * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9906  * to enable the MSI vector. The device driver is responsible for calling
9907  * the request_irq() to register MSI vector with a interrupt the handler,
9908  * which is done in this function.
9909  *
9910  * Return codes
9911  * 	0 - successful
9912  * 	other values - error
9913  **/
9914 static int
lpfc_sli4_enable_msi(struct lpfc_hba * phba)9915 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9916 {
9917 	int rc, index;
9918 
9919 	rc = pci_enable_msi(phba->pcidev);
9920 	if (!rc)
9921 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9922 				"0487 PCI enable MSI mode success.\n");
9923 	else {
9924 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9925 				"0488 PCI enable MSI mode failed (%d)\n", rc);
9926 		return rc;
9927 	}
9928 
9929 	rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9930 			 0, LPFC_DRIVER_NAME, phba);
9931 	if (rc) {
9932 		pci_disable_msi(phba->pcidev);
9933 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9934 				"0490 MSI request_irq failed (%d)\n", rc);
9935 		return rc;
9936 	}
9937 
9938 	for (index = 0; index < phba->io_channel_irqs; index++) {
9939 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9940 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9941 	}
9942 
9943 	if (phba->cfg_fof) {
9944 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9945 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9946 	}
9947 	return 0;
9948 }
9949 
9950 /**
9951  * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9952  * @phba: pointer to lpfc hba data structure.
9953  *
9954  * This routine is invoked to enable device interrupt and associate driver's
9955  * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9956  * interface spec. Depends on the interrupt mode configured to the driver,
9957  * the driver will try to fallback from the configured interrupt mode to an
9958  * interrupt mode which is supported by the platform, kernel, and device in
9959  * the order of:
9960  * MSI-X -> MSI -> IRQ.
9961  *
9962  * Return codes
9963  * 	0 - successful
9964  * 	other values - error
9965  **/
9966 static uint32_t
lpfc_sli4_enable_intr(struct lpfc_hba * phba,uint32_t cfg_mode)9967 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9968 {
9969 	uint32_t intr_mode = LPFC_INTR_ERROR;
9970 	int retval, idx;
9971 
9972 	if (cfg_mode == 2) {
9973 		/* Preparation before conf_msi mbox cmd */
9974 		retval = 0;
9975 		if (!retval) {
9976 			/* Now, try to enable MSI-X interrupt mode */
9977 			retval = lpfc_sli4_enable_msix(phba);
9978 			if (!retval) {
9979 				/* Indicate initialization to MSI-X mode */
9980 				phba->intr_type = MSIX;
9981 				intr_mode = 2;
9982 			}
9983 		}
9984 	}
9985 
9986 	/* Fallback to MSI if MSI-X initialization failed */
9987 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9988 		retval = lpfc_sli4_enable_msi(phba);
9989 		if (!retval) {
9990 			/* Indicate initialization to MSI mode */
9991 			phba->intr_type = MSI;
9992 			intr_mode = 1;
9993 		}
9994 	}
9995 
9996 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9997 	if (phba->intr_type == NONE) {
9998 		retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9999 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10000 		if (!retval) {
10001 			struct lpfc_hba_eq_hdl *eqhdl;
10002 
10003 			/* Indicate initialization to INTx mode */
10004 			phba->intr_type = INTx;
10005 			intr_mode = 0;
10006 
10007 			for (idx = 0; idx < phba->io_channel_irqs; idx++) {
10008 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10009 				eqhdl->idx = idx;
10010 				eqhdl->phba = phba;
10011 				atomic_set(&eqhdl->hba_eq_in_use, 1);
10012 			}
10013 			if (phba->cfg_fof) {
10014 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10015 				eqhdl->idx = idx;
10016 				eqhdl->phba = phba;
10017 				atomic_set(&eqhdl->hba_eq_in_use, 1);
10018 			}
10019 		}
10020 	}
10021 	return intr_mode;
10022 }
10023 
10024 /**
10025  * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
10026  * @phba: pointer to lpfc hba data structure.
10027  *
10028  * This routine is invoked to disable device interrupt and disassociate
10029  * the driver's interrupt handler(s) from interrupt vector(s) to device
10030  * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10031  * will release the interrupt vector(s) for the message signaled interrupt.
10032  **/
10033 static void
lpfc_sli4_disable_intr(struct lpfc_hba * phba)10034 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10035 {
10036 	/* Disable the currently initialized interrupt mode */
10037 	if (phba->intr_type == MSIX) {
10038 		int index;
10039 
10040 		/* Free up MSI-X multi-message vectors */
10041 		for (index = 0; index < phba->io_channel_irqs; index++)
10042 			free_irq(pci_irq_vector(phba->pcidev, index),
10043 					&phba->sli4_hba.hba_eq_hdl[index]);
10044 
10045 		if (phba->cfg_fof)
10046 			free_irq(pci_irq_vector(phba->pcidev, index),
10047 					&phba->sli4_hba.hba_eq_hdl[index]);
10048 	} else {
10049 		free_irq(phba->pcidev->irq, phba);
10050 	}
10051 
10052 	pci_free_irq_vectors(phba->pcidev);
10053 
10054 	/* Reset interrupt management states */
10055 	phba->intr_type = NONE;
10056 	phba->sli.slistat.sli_intr = 0;
10057 }
10058 
10059 /**
10060  * lpfc_unset_hba - Unset SLI3 hba device initialization
10061  * @phba: pointer to lpfc hba data structure.
10062  *
10063  * This routine is invoked to unset the HBA device initialization steps to
10064  * a device with SLI-3 interface spec.
10065  **/
10066 static void
lpfc_unset_hba(struct lpfc_hba * phba)10067 lpfc_unset_hba(struct lpfc_hba *phba)
10068 {
10069 	struct lpfc_vport *vport = phba->pport;
10070 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10071 
10072 	spin_lock_irq(shost->host_lock);
10073 	vport->load_flag |= FC_UNLOADING;
10074 	spin_unlock_irq(shost->host_lock);
10075 
10076 	kfree(phba->vpi_bmask);
10077 	kfree(phba->vpi_ids);
10078 
10079 	lpfc_stop_hba_timers(phba);
10080 
10081 	phba->pport->work_port_events = 0;
10082 
10083 	lpfc_sli_hba_down(phba);
10084 
10085 	lpfc_sli_brdrestart(phba);
10086 
10087 	lpfc_sli_disable_intr(phba);
10088 
10089 	return;
10090 }
10091 
10092 /**
10093  * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10094  * @phba: Pointer to HBA context object.
10095  *
10096  * This function is called in the SLI4 code path to wait for completion
10097  * of device's XRIs exchange busy. It will check the XRI exchange busy
10098  * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10099  * that, it will check the XRI exchange busy on outstanding FCP and ELS
10100  * I/Os every 30 seconds, log error message, and wait forever. Only when
10101  * all XRI exchange busy complete, the driver unload shall proceed with
10102  * invoking the function reset ioctl mailbox command to the CNA and the
10103  * the rest of the driver unload resource release.
10104  **/
10105 static void
lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba * phba)10106 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10107 {
10108 	int wait_time = 0;
10109 	int nvme_xri_cmpl = 1;
10110 	int nvmet_xri_cmpl = 1;
10111 	int fcp_xri_cmpl = 1;
10112 	int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10113 
10114 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10115 		fcp_xri_cmpl =
10116 			list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10117 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10118 		nvme_xri_cmpl =
10119 			list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10120 		nvmet_xri_cmpl =
10121 			list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10122 	}
10123 
10124 	while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10125 	       !nvmet_xri_cmpl) {
10126 		if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
10127 			if (!nvme_xri_cmpl)
10128 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10129 						"6100 NVME XRI exchange busy "
10130 						"wait time: %d seconds.\n",
10131 						wait_time/1000);
10132 			if (!fcp_xri_cmpl)
10133 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10134 						"2877 FCP XRI exchange busy "
10135 						"wait time: %d seconds.\n",
10136 						wait_time/1000);
10137 			if (!els_xri_cmpl)
10138 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10139 						"2878 ELS XRI exchange busy "
10140 						"wait time: %d seconds.\n",
10141 						wait_time/1000);
10142 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10143 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10144 		} else {
10145 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10146 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10147 		}
10148 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10149 			nvme_xri_cmpl = list_empty(
10150 				&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10151 			nvmet_xri_cmpl = list_empty(
10152 				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10153 		}
10154 
10155 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10156 			fcp_xri_cmpl = list_empty(
10157 				&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10158 
10159 		els_xri_cmpl =
10160 			list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10161 
10162 	}
10163 }
10164 
10165 /**
10166  * lpfc_sli4_hba_unset - Unset the fcoe hba
10167  * @phba: Pointer to HBA context object.
10168  *
10169  * This function is called in the SLI4 code path to reset the HBA's FCoE
10170  * function. The caller is not required to hold any lock. This routine
10171  * issues PCI function reset mailbox command to reset the FCoE function.
10172  * At the end of the function, it calls lpfc_hba_down_post function to
10173  * free any pending commands.
10174  **/
10175 static void
lpfc_sli4_hba_unset(struct lpfc_hba * phba)10176 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10177 {
10178 	int wait_cnt = 0;
10179 	LPFC_MBOXQ_t *mboxq;
10180 	struct pci_dev *pdev = phba->pcidev;
10181 
10182 	lpfc_stop_hba_timers(phba);
10183 	phba->sli4_hba.intr_enable = 0;
10184 
10185 	/*
10186 	 * Gracefully wait out the potential current outstanding asynchronous
10187 	 * mailbox command.
10188 	 */
10189 
10190 	/* First, block any pending async mailbox command from posted */
10191 	spin_lock_irq(&phba->hbalock);
10192 	phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10193 	spin_unlock_irq(&phba->hbalock);
10194 	/* Now, trying to wait it out if we can */
10195 	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10196 		msleep(10);
10197 		if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10198 			break;
10199 	}
10200 	/* Forcefully release the outstanding mailbox command if timed out */
10201 	if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10202 		spin_lock_irq(&phba->hbalock);
10203 		mboxq = phba->sli.mbox_active;
10204 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10205 		__lpfc_mbox_cmpl_put(phba, mboxq);
10206 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10207 		phba->sli.mbox_active = NULL;
10208 		spin_unlock_irq(&phba->hbalock);
10209 	}
10210 
10211 	/* Abort all iocbs associated with the hba */
10212 	lpfc_sli_hba_iocb_abort(phba);
10213 
10214 	/* Wait for completion of device XRI exchange busy */
10215 	lpfc_sli4_xri_exchange_busy_wait(phba);
10216 
10217 	/* Disable PCI subsystem interrupt */
10218 	lpfc_sli4_disable_intr(phba);
10219 
10220 	/* Disable SR-IOV if enabled */
10221 	if (phba->cfg_sriov_nr_virtfn)
10222 		pci_disable_sriov(pdev);
10223 
10224 	/* Stop kthread signal shall trigger work_done one more time */
10225 	kthread_stop(phba->worker_thread);
10226 
10227 	/* Unset the queues shared with the hardware then release all
10228 	 * allocated resources.
10229 	 */
10230 	lpfc_sli4_queue_unset(phba);
10231 	lpfc_sli4_queue_destroy(phba);
10232 
10233 	/* Reset SLI4 HBA FCoE function */
10234 	lpfc_pci_function_reset(phba);
10235 
10236 	/* Stop the SLI4 device port */
10237 	phba->pport->work_port_events = 0;
10238 }
10239 
10240  /**
10241  * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10242  * @phba: Pointer to HBA context object.
10243  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10244  *
10245  * This function is called in the SLI4 code path to read the port's
10246  * sli4 capabilities.
10247  *
10248  * This function may be be called from any context that can block-wait
10249  * for the completion.  The expectation is that this routine is called
10250  * typically from probe_one or from the online routine.
10251  **/
10252 int
lpfc_pc_sli4_params_get(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)10253 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10254 {
10255 	int rc;
10256 	struct lpfc_mqe *mqe;
10257 	struct lpfc_pc_sli4_params *sli4_params;
10258 	uint32_t mbox_tmo;
10259 
10260 	rc = 0;
10261 	mqe = &mboxq->u.mqe;
10262 
10263 	/* Read the port's SLI4 Parameters port capabilities */
10264 	lpfc_pc_sli4_params(mboxq);
10265 	if (!phba->sli4_hba.intr_enable)
10266 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10267 	else {
10268 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10269 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10270 	}
10271 
10272 	if (unlikely(rc))
10273 		return 1;
10274 
10275 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10276 	sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10277 	sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10278 	sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10279 	sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10280 					     &mqe->un.sli4_params);
10281 	sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10282 					     &mqe->un.sli4_params);
10283 	sli4_params->proto_types = mqe->un.sli4_params.word3;
10284 	sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10285 	sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10286 	sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10287 	sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10288 	sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10289 	sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10290 	sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10291 	sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10292 	sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10293 	sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10294 	sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10295 	sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10296 	sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10297 	sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10298 	sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10299 	sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10300 	sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10301 	sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10302 	sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10303 	sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
10304 
10305 	/* Make sure that sge_supp_len can be handled by the driver */
10306 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10307 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10308 
10309 	return rc;
10310 }
10311 
10312 /**
10313  * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10314  * @phba: Pointer to HBA context object.
10315  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10316  *
10317  * This function is called in the SLI4 code path to read the port's
10318  * sli4 capabilities.
10319  *
10320  * This function may be be called from any context that can block-wait
10321  * for the completion.  The expectation is that this routine is called
10322  * typically from probe_one or from the online routine.
10323  **/
10324 int
lpfc_get_sli4_parameters(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)10325 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10326 {
10327 	int rc;
10328 	struct lpfc_mqe *mqe = &mboxq->u.mqe;
10329 	struct lpfc_pc_sli4_params *sli4_params;
10330 	uint32_t mbox_tmo;
10331 	int length;
10332 	struct lpfc_sli4_parameters *mbx_sli4_parameters;
10333 
10334 	/*
10335 	 * By default, the driver assumes the SLI4 port requires RPI
10336 	 * header postings.  The SLI4_PARAM response will correct this
10337 	 * assumption.
10338 	 */
10339 	phba->sli4_hba.rpi_hdrs_in_use = 1;
10340 
10341 	/* Read the port's SLI4 Config Parameters */
10342 	length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10343 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10344 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10345 			 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10346 			 length, LPFC_SLI4_MBX_EMBED);
10347 	if (!phba->sli4_hba.intr_enable)
10348 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10349 	else {
10350 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10351 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10352 	}
10353 	if (unlikely(rc))
10354 		return rc;
10355 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10356 	mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10357 	sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10358 	sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10359 	sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10360 	sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10361 					     mbx_sli4_parameters);
10362 	sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10363 					     mbx_sli4_parameters);
10364 	if (bf_get(cfg_phwq, mbx_sli4_parameters))
10365 		phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10366 	else
10367 		phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10368 	sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10369 	sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
10370 	sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
10371 	sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10372 	sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10373 	sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10374 	sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
10375 	sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
10376 	sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10377 					    mbx_sli4_parameters);
10378 	sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
10379 	sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10380 					   mbx_sli4_parameters);
10381 	phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10382 	phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
10383 	phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10384 			      bf_get(cfg_xib, mbx_sli4_parameters));
10385 
10386 	if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10387 	    !phba->nvme_support) {
10388 		phba->nvme_support = 0;
10389 		phba->nvmet_support = 0;
10390 		phba->cfg_nvmet_mrq = 0;
10391 		phba->cfg_nvme_io_channel = 0;
10392 		phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10393 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10394 				"6101 Disabling NVME support: "
10395 				"Not supported by firmware: %d %d\n",
10396 				bf_get(cfg_nvme, mbx_sli4_parameters),
10397 				bf_get(cfg_xib, mbx_sli4_parameters));
10398 
10399 		/* If firmware doesn't support NVME, just use SCSI support */
10400 		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10401 			return -ENODEV;
10402 		phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10403 	}
10404 
10405 	if (bf_get(cfg_xib, mbx_sli4_parameters) && phba->cfg_suppress_rsp)
10406 		phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10407 
10408 	if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10409 		phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10410 
10411 	/* Make sure that sge_supp_len can be handled by the driver */
10412 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10413 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10414 
10415 	/*
10416 	 * Issue IOs with CDB embedded in WQE to minimized the number
10417 	 * of DMAs the firmware has to do. Setting this to 1 also forces
10418 	 * the driver to use 128 bytes WQEs for FCP IOs.
10419 	 */
10420 	if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10421 		phba->fcp_embed_io = 1;
10422 	else
10423 		phba->fcp_embed_io = 0;
10424 
10425 	/*
10426 	 * Check if the SLI port supports MDS Diagnostics
10427 	 */
10428 	if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10429 		phba->mds_diags_support = 1;
10430 	else
10431 		phba->mds_diags_support = 0;
10432 	return 0;
10433 }
10434 
10435 /**
10436  * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10437  * @pdev: pointer to PCI device
10438  * @pid: pointer to PCI device identifier
10439  *
10440  * This routine is to be called to attach a device with SLI-3 interface spec
10441  * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10442  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10443  * information of the device and driver to see if the driver state that it can
10444  * support this kind of device. If the match is successful, the driver core
10445  * invokes this routine. If this routine determines it can claim the HBA, it
10446  * does all the initialization that it needs to do to handle the HBA properly.
10447  *
10448  * Return code
10449  * 	0 - driver can claim the device
10450  * 	negative value - driver can not claim the device
10451  **/
10452 static int
lpfc_pci_probe_one_s3(struct pci_dev * pdev,const struct pci_device_id * pid)10453 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10454 {
10455 	struct lpfc_hba   *phba;
10456 	struct lpfc_vport *vport = NULL;
10457 	struct Scsi_Host  *shost = NULL;
10458 	int error;
10459 	uint32_t cfg_mode, intr_mode;
10460 
10461 	/* Allocate memory for HBA structure */
10462 	phba = lpfc_hba_alloc(pdev);
10463 	if (!phba)
10464 		return -ENOMEM;
10465 
10466 	/* Perform generic PCI device enabling operation */
10467 	error = lpfc_enable_pci_dev(phba);
10468 	if (error)
10469 		goto out_free_phba;
10470 
10471 	/* Set up SLI API function jump table for PCI-device group-0 HBAs */
10472 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10473 	if (error)
10474 		goto out_disable_pci_dev;
10475 
10476 	/* Set up SLI-3 specific device PCI memory space */
10477 	error = lpfc_sli_pci_mem_setup(phba);
10478 	if (error) {
10479 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10480 				"1402 Failed to set up pci memory space.\n");
10481 		goto out_disable_pci_dev;
10482 	}
10483 
10484 	/* Set up SLI-3 specific device driver resources */
10485 	error = lpfc_sli_driver_resource_setup(phba);
10486 	if (error) {
10487 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10488 				"1404 Failed to set up driver resource.\n");
10489 		goto out_unset_pci_mem_s3;
10490 	}
10491 
10492 	/* Initialize and populate the iocb list per host */
10493 
10494 	error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10495 	if (error) {
10496 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10497 				"1405 Failed to initialize iocb list.\n");
10498 		goto out_unset_driver_resource_s3;
10499 	}
10500 
10501 	/* Set up common device driver resources */
10502 	error = lpfc_setup_driver_resource_phase2(phba);
10503 	if (error) {
10504 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10505 				"1406 Failed to set up driver resource.\n");
10506 		goto out_free_iocb_list;
10507 	}
10508 
10509 	/* Get the default values for Model Name and Description */
10510 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10511 
10512 	/* Create SCSI host to the physical port */
10513 	error = lpfc_create_shost(phba);
10514 	if (error) {
10515 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10516 				"1407 Failed to create scsi host.\n");
10517 		goto out_unset_driver_resource;
10518 	}
10519 
10520 	/* Configure sysfs attributes */
10521 	vport = phba->pport;
10522 	error = lpfc_alloc_sysfs_attr(vport);
10523 	if (error) {
10524 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10525 				"1476 Failed to allocate sysfs attr\n");
10526 		goto out_destroy_shost;
10527 	}
10528 
10529 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
10530 	/* Now, trying to enable interrupt and bring up the device */
10531 	cfg_mode = phba->cfg_use_msi;
10532 	while (true) {
10533 		/* Put device to a known state before enabling interrupt */
10534 		lpfc_stop_port(phba);
10535 		/* Configure and enable interrupt */
10536 		intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10537 		if (intr_mode == LPFC_INTR_ERROR) {
10538 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10539 					"0431 Failed to enable interrupt.\n");
10540 			error = -ENODEV;
10541 			goto out_free_sysfs_attr;
10542 		}
10543 		/* SLI-3 HBA setup */
10544 		if (lpfc_sli_hba_setup(phba)) {
10545 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10546 					"1477 Failed to set up hba\n");
10547 			error = -ENODEV;
10548 			goto out_remove_device;
10549 		}
10550 
10551 		/* Wait 50ms for the interrupts of previous mailbox commands */
10552 		msleep(50);
10553 		/* Check active interrupts on message signaled interrupts */
10554 		if (intr_mode == 0 ||
10555 		    phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10556 			/* Log the current active interrupt mode */
10557 			phba->intr_mode = intr_mode;
10558 			lpfc_log_intr_mode(phba, intr_mode);
10559 			break;
10560 		} else {
10561 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10562 					"0447 Configure interrupt mode (%d) "
10563 					"failed active interrupt test.\n",
10564 					intr_mode);
10565 			/* Disable the current interrupt mode */
10566 			lpfc_sli_disable_intr(phba);
10567 			/* Try next level of interrupt mode */
10568 			cfg_mode = --intr_mode;
10569 		}
10570 	}
10571 
10572 	/* Perform post initialization setup */
10573 	lpfc_post_init_setup(phba);
10574 
10575 	/* Check if there are static vports to be created. */
10576 	lpfc_create_static_vport(phba);
10577 
10578 	return 0;
10579 
10580 out_remove_device:
10581 	lpfc_unset_hba(phba);
10582 out_free_sysfs_attr:
10583 	lpfc_free_sysfs_attr(vport);
10584 out_destroy_shost:
10585 	lpfc_destroy_shost(phba);
10586 out_unset_driver_resource:
10587 	lpfc_unset_driver_resource_phase2(phba);
10588 out_free_iocb_list:
10589 	lpfc_free_iocb_list(phba);
10590 out_unset_driver_resource_s3:
10591 	lpfc_sli_driver_resource_unset(phba);
10592 out_unset_pci_mem_s3:
10593 	lpfc_sli_pci_mem_unset(phba);
10594 out_disable_pci_dev:
10595 	lpfc_disable_pci_dev(phba);
10596 	if (shost)
10597 		scsi_host_put(shost);
10598 out_free_phba:
10599 	lpfc_hba_free(phba);
10600 	return error;
10601 }
10602 
10603 /**
10604  * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
10605  * @pdev: pointer to PCI device
10606  *
10607  * This routine is to be called to disattach a device with SLI-3 interface
10608  * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10609  * removed from PCI bus, it performs all the necessary cleanup for the HBA
10610  * device to be removed from the PCI subsystem properly.
10611  **/
10612 static void
lpfc_pci_remove_one_s3(struct pci_dev * pdev)10613 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
10614 {
10615 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
10616 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10617 	struct lpfc_vport **vports;
10618 	struct lpfc_hba   *phba = vport->phba;
10619 	int i;
10620 
10621 	spin_lock_irq(&phba->hbalock);
10622 	vport->load_flag |= FC_UNLOADING;
10623 	spin_unlock_irq(&phba->hbalock);
10624 
10625 	lpfc_free_sysfs_attr(vport);
10626 
10627 	/* Release all the vports against this physical port */
10628 	vports = lpfc_create_vport_work_array(phba);
10629 	if (vports != NULL)
10630 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10631 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10632 				continue;
10633 			fc_vport_terminate(vports[i]->fc_vport);
10634 		}
10635 	lpfc_destroy_vport_work_array(phba, vports);
10636 
10637 	/* Remove FC host and then SCSI host with the physical port */
10638 	fc_remove_host(shost);
10639 	scsi_remove_host(shost);
10640 
10641 	lpfc_cleanup(vport);
10642 
10643 	/*
10644 	 * Bring down the SLI Layer. This step disable all interrupts,
10645 	 * clears the rings, discards all mailbox commands, and resets
10646 	 * the HBA.
10647 	 */
10648 
10649 	/* HBA interrupt will be disabled after this call */
10650 	lpfc_sli_hba_down(phba);
10651 	/* Stop kthread signal shall trigger work_done one more time */
10652 	kthread_stop(phba->worker_thread);
10653 	/* Final cleanup of txcmplq and reset the HBA */
10654 	lpfc_sli_brdrestart(phba);
10655 
10656 	kfree(phba->vpi_bmask);
10657 	kfree(phba->vpi_ids);
10658 
10659 	lpfc_stop_hba_timers(phba);
10660 	spin_lock_irq(&phba->hbalock);
10661 	list_del_init(&vport->listentry);
10662 	spin_unlock_irq(&phba->hbalock);
10663 
10664 	lpfc_debugfs_terminate(vport);
10665 
10666 	/* Disable SR-IOV if enabled */
10667 	if (phba->cfg_sriov_nr_virtfn)
10668 		pci_disable_sriov(pdev);
10669 
10670 	/* Disable interrupt */
10671 	lpfc_sli_disable_intr(phba);
10672 
10673 	scsi_host_put(shost);
10674 
10675 	/*
10676 	 * Call scsi_free before mem_free since scsi bufs are released to their
10677 	 * corresponding pools here.
10678 	 */
10679 	lpfc_scsi_free(phba);
10680 	lpfc_mem_free_all(phba);
10681 
10682 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10683 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
10684 
10685 	/* Free resources associated with SLI2 interface */
10686 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
10687 			  phba->slim2p.virt, phba->slim2p.phys);
10688 
10689 	/* unmap adapter SLIM and Control Registers */
10690 	iounmap(phba->ctrl_regs_memmap_p);
10691 	iounmap(phba->slim_memmap_p);
10692 
10693 	lpfc_hba_free(phba);
10694 
10695 	pci_release_mem_regions(pdev);
10696 	pci_disable_device(pdev);
10697 }
10698 
10699 /**
10700  * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
10701  * @pdev: pointer to PCI device
10702  * @msg: power management message
10703  *
10704  * This routine is to be called from the kernel's PCI subsystem to support
10705  * system Power Management (PM) to device with SLI-3 interface spec. When
10706  * PM invokes this method, it quiesces the device by stopping the driver's
10707  * worker thread for the device, turning off device's interrupt and DMA,
10708  * and bring the device offline. Note that as the driver implements the
10709  * minimum PM requirements to a power-aware driver's PM support for the
10710  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10711  * to the suspend() method call will be treated as SUSPEND and the driver will
10712  * fully reinitialize its device during resume() method call, the driver will
10713  * set device to PCI_D3hot state in PCI config space instead of setting it
10714  * according to the @msg provided by the PM.
10715  *
10716  * Return code
10717  * 	0 - driver suspended the device
10718  * 	Error otherwise
10719  **/
10720 static int
lpfc_pci_suspend_one_s3(struct pci_dev * pdev,pm_message_t msg)10721 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
10722 {
10723 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10724 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10725 
10726 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10727 			"0473 PCI device Power Management suspend.\n");
10728 
10729 	/* Bring down the device */
10730 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10731 	lpfc_offline(phba);
10732 	kthread_stop(phba->worker_thread);
10733 
10734 	/* Disable interrupt from device */
10735 	lpfc_sli_disable_intr(phba);
10736 
10737 	/* Save device state to PCI config space */
10738 	pci_save_state(pdev);
10739 	pci_set_power_state(pdev, PCI_D3hot);
10740 
10741 	return 0;
10742 }
10743 
10744 /**
10745  * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
10746  * @pdev: pointer to PCI device
10747  *
10748  * This routine is to be called from the kernel's PCI subsystem to support
10749  * system Power Management (PM) to device with SLI-3 interface spec. When PM
10750  * invokes this method, it restores the device's PCI config space state and
10751  * fully reinitializes the device and brings it online. Note that as the
10752  * driver implements the minimum PM requirements to a power-aware driver's
10753  * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10754  * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10755  * driver will fully reinitialize its device during resume() method call,
10756  * the device will be set to PCI_D0 directly in PCI config space before
10757  * restoring the state.
10758  *
10759  * Return code
10760  * 	0 - driver suspended the device
10761  * 	Error otherwise
10762  **/
10763 static int
lpfc_pci_resume_one_s3(struct pci_dev * pdev)10764 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
10765 {
10766 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10767 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10768 	uint32_t intr_mode;
10769 	int error;
10770 
10771 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10772 			"0452 PCI device Power Management resume.\n");
10773 
10774 	/* Restore device state from PCI config space */
10775 	pci_set_power_state(pdev, PCI_D0);
10776 	pci_restore_state(pdev);
10777 
10778 	/*
10779 	 * As the new kernel behavior of pci_restore_state() API call clears
10780 	 * device saved_state flag, need to save the restored state again.
10781 	 */
10782 	pci_save_state(pdev);
10783 
10784 	if (pdev->is_busmaster)
10785 		pci_set_master(pdev);
10786 
10787 	/* Startup the kernel thread for this host adapter. */
10788 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
10789 					"lpfc_worker_%d", phba->brd_no);
10790 	if (IS_ERR(phba->worker_thread)) {
10791 		error = PTR_ERR(phba->worker_thread);
10792 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10793 				"0434 PM resume failed to start worker "
10794 				"thread: error=x%x.\n", error);
10795 		return error;
10796 	}
10797 
10798 	/* Configure and enable interrupt */
10799 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
10800 	if (intr_mode == LPFC_INTR_ERROR) {
10801 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10802 				"0430 PM resume Failed to enable interrupt\n");
10803 		return -EIO;
10804 	} else
10805 		phba->intr_mode = intr_mode;
10806 
10807 	/* Restart HBA and bring it online */
10808 	lpfc_sli_brdrestart(phba);
10809 	lpfc_online(phba);
10810 
10811 	/* Log the current active interrupt mode */
10812 	lpfc_log_intr_mode(phba, phba->intr_mode);
10813 
10814 	return 0;
10815 }
10816 
10817 /**
10818  * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10819  * @phba: pointer to lpfc hba data structure.
10820  *
10821  * This routine is called to prepare the SLI3 device for PCI slot recover. It
10822  * aborts all the outstanding SCSI I/Os to the pci device.
10823  **/
10824 static void
lpfc_sli_prep_dev_for_recover(struct lpfc_hba * phba)10825 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10826 {
10827 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10828 			"2723 PCI channel I/O abort preparing for recovery\n");
10829 
10830 	/*
10831 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10832 	 * and let the SCSI mid-layer to retry them to recover.
10833 	 */
10834 	lpfc_sli_abort_fcp_rings(phba);
10835 }
10836 
10837 /**
10838  * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10839  * @phba: pointer to lpfc hba data structure.
10840  *
10841  * This routine is called to prepare the SLI3 device for PCI slot reset. It
10842  * disables the device interrupt and pci device, and aborts the internal FCP
10843  * pending I/Os.
10844  **/
10845 static void
lpfc_sli_prep_dev_for_reset(struct lpfc_hba * phba)10846 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10847 {
10848 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10849 			"2710 PCI channel disable preparing for reset\n");
10850 
10851 	/* Block any management I/Os to the device */
10852 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
10853 
10854 	/* Block all SCSI devices' I/Os on the host */
10855 	lpfc_scsi_dev_block(phba);
10856 
10857 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
10858 	lpfc_sli_flush_fcp_rings(phba);
10859 
10860 	/* stop all timers */
10861 	lpfc_stop_hba_timers(phba);
10862 
10863 	/* Disable interrupt and pci device */
10864 	lpfc_sli_disable_intr(phba);
10865 	pci_disable_device(phba->pcidev);
10866 }
10867 
10868 /**
10869  * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10870  * @phba: pointer to lpfc hba data structure.
10871  *
10872  * This routine is called to prepare the SLI3 device for PCI slot permanently
10873  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10874  * pending I/Os.
10875  **/
10876 static void
lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba * phba)10877 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10878 {
10879 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10880 			"2711 PCI channel permanent disable for failure\n");
10881 	/* Block all SCSI devices' I/Os on the host */
10882 	lpfc_scsi_dev_block(phba);
10883 
10884 	/* stop all timers */
10885 	lpfc_stop_hba_timers(phba);
10886 
10887 	/* Clean up all driver's outstanding SCSI I/Os */
10888 	lpfc_sli_flush_fcp_rings(phba);
10889 }
10890 
10891 /**
10892  * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
10893  * @pdev: pointer to PCI device.
10894  * @state: the current PCI connection state.
10895  *
10896  * This routine is called from the PCI subsystem for I/O error handling to
10897  * device with SLI-3 interface spec. This function is called by the PCI
10898  * subsystem after a PCI bus error affecting this device has been detected.
10899  * When this function is invoked, it will need to stop all the I/Os and
10900  * interrupt(s) to the device. Once that is done, it will return
10901  * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10902  * as desired.
10903  *
10904  * Return codes
10905  * 	PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
10906  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10907  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10908  **/
10909 static pci_ers_result_t
lpfc_io_error_detected_s3(struct pci_dev * pdev,pci_channel_state_t state)10910 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
10911 {
10912 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10913 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10914 
10915 	switch (state) {
10916 	case pci_channel_io_normal:
10917 		/* Non-fatal error, prepare for recovery */
10918 		lpfc_sli_prep_dev_for_recover(phba);
10919 		return PCI_ERS_RESULT_CAN_RECOVER;
10920 	case pci_channel_io_frozen:
10921 		/* Fatal error, prepare for slot reset */
10922 		lpfc_sli_prep_dev_for_reset(phba);
10923 		return PCI_ERS_RESULT_NEED_RESET;
10924 	case pci_channel_io_perm_failure:
10925 		/* Permanent failure, prepare for device down */
10926 		lpfc_sli_prep_dev_for_perm_failure(phba);
10927 		return PCI_ERS_RESULT_DISCONNECT;
10928 	default:
10929 		/* Unknown state, prepare and request slot reset */
10930 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10931 				"0472 Unknown PCI error state: x%x\n", state);
10932 		lpfc_sli_prep_dev_for_reset(phba);
10933 		return PCI_ERS_RESULT_NEED_RESET;
10934 	}
10935 }
10936 
10937 /**
10938  * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
10939  * @pdev: pointer to PCI device.
10940  *
10941  * This routine is called from the PCI subsystem for error handling to
10942  * device with SLI-3 interface spec. This is called after PCI bus has been
10943  * reset to restart the PCI card from scratch, as if from a cold-boot.
10944  * During the PCI subsystem error recovery, after driver returns
10945  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10946  * recovery and then call this routine before calling the .resume method
10947  * to recover the device. This function will initialize the HBA device,
10948  * enable the interrupt, but it will just put the HBA to offline state
10949  * without passing any I/O traffic.
10950  *
10951  * Return codes
10952  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
10953  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10954  */
10955 static pci_ers_result_t
lpfc_io_slot_reset_s3(struct pci_dev * pdev)10956 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
10957 {
10958 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10959 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10960 	struct lpfc_sli *psli = &phba->sli;
10961 	uint32_t intr_mode;
10962 
10963 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10964 	if (pci_enable_device_mem(pdev)) {
10965 		printk(KERN_ERR "lpfc: Cannot re-enable "
10966 			"PCI device after reset.\n");
10967 		return PCI_ERS_RESULT_DISCONNECT;
10968 	}
10969 
10970 	pci_restore_state(pdev);
10971 
10972 	/*
10973 	 * As the new kernel behavior of pci_restore_state() API call clears
10974 	 * device saved_state flag, need to save the restored state again.
10975 	 */
10976 	pci_save_state(pdev);
10977 
10978 	if (pdev->is_busmaster)
10979 		pci_set_master(pdev);
10980 
10981 	spin_lock_irq(&phba->hbalock);
10982 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10983 	spin_unlock_irq(&phba->hbalock);
10984 
10985 	/* Configure and enable interrupt */
10986 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
10987 	if (intr_mode == LPFC_INTR_ERROR) {
10988 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10989 				"0427 Cannot re-enable interrupt after "
10990 				"slot reset.\n");
10991 		return PCI_ERS_RESULT_DISCONNECT;
10992 	} else
10993 		phba->intr_mode = intr_mode;
10994 
10995 	/* Take device offline, it will perform cleanup */
10996 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10997 	lpfc_offline(phba);
10998 	lpfc_sli_brdrestart(phba);
10999 
11000 	/* Log the current active interrupt mode */
11001 	lpfc_log_intr_mode(phba, phba->intr_mode);
11002 
11003 	return PCI_ERS_RESULT_RECOVERED;
11004 }
11005 
11006 /**
11007  * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
11008  * @pdev: pointer to PCI device
11009  *
11010  * This routine is called from the PCI subsystem for error handling to device
11011  * with SLI-3 interface spec. It is called when kernel error recovery tells
11012  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11013  * error recovery. After this call, traffic can start to flow from this device
11014  * again.
11015  */
11016 static void
lpfc_io_resume_s3(struct pci_dev * pdev)11017 lpfc_io_resume_s3(struct pci_dev *pdev)
11018 {
11019 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11020 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11021 
11022 	/* Bring device online, it will be no-op for non-fatal error resume */
11023 	lpfc_online(phba);
11024 
11025 	/* Clean up Advanced Error Reporting (AER) if needed */
11026 	if (phba->hba_flag & HBA_AER_ENABLED)
11027 		pci_cleanup_aer_uncorrect_error_status(pdev);
11028 }
11029 
11030 /**
11031  * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
11032  * @phba: pointer to lpfc hba data structure.
11033  *
11034  * returns the number of ELS/CT IOCBs to reserve
11035  **/
11036 int
lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba * phba)11037 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11038 {
11039 	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11040 
11041 	if (phba->sli_rev == LPFC_SLI_REV4) {
11042 		if (max_xri <= 100)
11043 			return 10;
11044 		else if (max_xri <= 256)
11045 			return 25;
11046 		else if (max_xri <= 512)
11047 			return 50;
11048 		else if (max_xri <= 1024)
11049 			return 100;
11050 		else if (max_xri <= 1536)
11051 			return 150;
11052 		else if (max_xri <= 2048)
11053 			return 200;
11054 		else
11055 			return 250;
11056 	} else
11057 		return 0;
11058 }
11059 
11060 /**
11061  * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11062  * @phba: pointer to lpfc hba data structure.
11063  *
11064  * returns the number of ELS/CT + NVMET IOCBs to reserve
11065  **/
11066 int
lpfc_sli4_get_iocb_cnt(struct lpfc_hba * phba)11067 lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11068 {
11069 	int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11070 
11071 	if (phba->nvmet_support)
11072 		max_xri += LPFC_NVMET_BUF_POST;
11073 	return max_xri;
11074 }
11075 
11076 
11077 /**
11078  * lpfc_write_firmware - attempt to write a firmware image to the port
11079  * @fw: pointer to firmware image returned from request_firmware.
11080  * @phba: pointer to lpfc hba data structure.
11081  *
11082  **/
11083 static void
lpfc_write_firmware(const struct firmware * fw,void * context)11084 lpfc_write_firmware(const struct firmware *fw, void *context)
11085 {
11086 	struct lpfc_hba *phba = (struct lpfc_hba *)context;
11087 	char fwrev[FW_REV_STR_SIZE];
11088 	struct lpfc_grp_hdr *image;
11089 	struct list_head dma_buffer_list;
11090 	int i, rc = 0;
11091 	struct lpfc_dmabuf *dmabuf, *next;
11092 	uint32_t offset = 0, temp_offset = 0;
11093 	uint32_t magic_number, ftype, fid, fsize;
11094 
11095 	/* It can be null in no-wait mode, sanity check */
11096 	if (!fw) {
11097 		rc = -ENXIO;
11098 		goto out;
11099 	}
11100 	image = (struct lpfc_grp_hdr *)fw->data;
11101 
11102 	magic_number = be32_to_cpu(image->magic_number);
11103 	ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11104 	fid = bf_get_be32(lpfc_grp_hdr_id, image),
11105 	fsize = be32_to_cpu(image->size);
11106 
11107 	INIT_LIST_HEAD(&dma_buffer_list);
11108 	if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
11109 	     magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
11110 	    ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
11111 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11112 				"3022 Invalid FW image found. "
11113 				"Magic:%x Type:%x ID:%x Size %d %zd\n",
11114 				magic_number, ftype, fid, fsize, fw->size);
11115 		rc = -EINVAL;
11116 		goto release_out;
11117 	}
11118 	lpfc_decode_firmware_rev(phba, fwrev, 1);
11119 	if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
11120 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11121 				"3023 Updating Firmware, Current Version:%s "
11122 				"New Version:%s\n",
11123 				fwrev, image->revision);
11124 		for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11125 			dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11126 					 GFP_KERNEL);
11127 			if (!dmabuf) {
11128 				rc = -ENOMEM;
11129 				goto release_out;
11130 			}
11131 			dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11132 							  SLI4_PAGE_SIZE,
11133 							  &dmabuf->phys,
11134 							  GFP_KERNEL);
11135 			if (!dmabuf->virt) {
11136 				kfree(dmabuf);
11137 				rc = -ENOMEM;
11138 				goto release_out;
11139 			}
11140 			list_add_tail(&dmabuf->list, &dma_buffer_list);
11141 		}
11142 		while (offset < fw->size) {
11143 			temp_offset = offset;
11144 			list_for_each_entry(dmabuf, &dma_buffer_list, list) {
11145 				if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
11146 					memcpy(dmabuf->virt,
11147 					       fw->data + temp_offset,
11148 					       fw->size - temp_offset);
11149 					temp_offset = fw->size;
11150 					break;
11151 				}
11152 				memcpy(dmabuf->virt, fw->data + temp_offset,
11153 				       SLI4_PAGE_SIZE);
11154 				temp_offset += SLI4_PAGE_SIZE;
11155 			}
11156 			rc = lpfc_wr_object(phba, &dma_buffer_list,
11157 				    (fw->size - offset), &offset);
11158 			if (rc)
11159 				goto release_out;
11160 		}
11161 		rc = offset;
11162 	}
11163 
11164 release_out:
11165 	list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11166 		list_del(&dmabuf->list);
11167 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11168 				  dmabuf->virt, dmabuf->phys);
11169 		kfree(dmabuf);
11170 	}
11171 	release_firmware(fw);
11172 out:
11173 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11174 			"3024 Firmware update done: %d.\n", rc);
11175 	return;
11176 }
11177 
11178 /**
11179  * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11180  * @phba: pointer to lpfc hba data structure.
11181  *
11182  * This routine is called to perform Linux generic firmware upgrade on device
11183  * that supports such feature.
11184  **/
11185 int
lpfc_sli4_request_firmware_update(struct lpfc_hba * phba,uint8_t fw_upgrade)11186 lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11187 {
11188 	uint8_t file_name[ELX_MODEL_NAME_SIZE];
11189 	int ret;
11190 	const struct firmware *fw;
11191 
11192 	/* Only supported on SLI4 interface type 2 for now */
11193 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11194 	    LPFC_SLI_INTF_IF_TYPE_2)
11195 		return -EPERM;
11196 
11197 	snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11198 
11199 	if (fw_upgrade == INT_FW_UPGRADE) {
11200 		ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11201 					file_name, &phba->pcidev->dev,
11202 					GFP_KERNEL, (void *)phba,
11203 					lpfc_write_firmware);
11204 	} else if (fw_upgrade == RUN_FW_UPGRADE) {
11205 		ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11206 		if (!ret)
11207 			lpfc_write_firmware(fw, (void *)phba);
11208 	} else {
11209 		ret = -EINVAL;
11210 	}
11211 
11212 	return ret;
11213 }
11214 
11215 /**
11216  * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11217  * @pdev: pointer to PCI device
11218  * @pid: pointer to PCI device identifier
11219  *
11220  * This routine is called from the kernel's PCI subsystem to device with
11221  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11222  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11223  * information of the device and driver to see if the driver state that it
11224  * can support this kind of device. If the match is successful, the driver
11225  * core invokes this routine. If this routine determines it can claim the HBA,
11226  * it does all the initialization that it needs to do to handle the HBA
11227  * properly.
11228  *
11229  * Return code
11230  * 	0 - driver can claim the device
11231  * 	negative value - driver can not claim the device
11232  **/
11233 static int
lpfc_pci_probe_one_s4(struct pci_dev * pdev,const struct pci_device_id * pid)11234 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11235 {
11236 	struct lpfc_hba   *phba;
11237 	struct lpfc_vport *vport = NULL;
11238 	struct Scsi_Host  *shost = NULL;
11239 	int error;
11240 	uint32_t cfg_mode, intr_mode;
11241 
11242 	/* Allocate memory for HBA structure */
11243 	phba = lpfc_hba_alloc(pdev);
11244 	if (!phba)
11245 		return -ENOMEM;
11246 
11247 	/* Perform generic PCI device enabling operation */
11248 	error = lpfc_enable_pci_dev(phba);
11249 	if (error)
11250 		goto out_free_phba;
11251 
11252 	/* Set up SLI API function jump table for PCI-device group-1 HBAs */
11253 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11254 	if (error)
11255 		goto out_disable_pci_dev;
11256 
11257 	/* Set up SLI-4 specific device PCI memory space */
11258 	error = lpfc_sli4_pci_mem_setup(phba);
11259 	if (error) {
11260 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11261 				"1410 Failed to set up pci memory space.\n");
11262 		goto out_disable_pci_dev;
11263 	}
11264 
11265 	/* Set up SLI-4 Specific device driver resources */
11266 	error = lpfc_sli4_driver_resource_setup(phba);
11267 	if (error) {
11268 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11269 				"1412 Failed to set up driver resource.\n");
11270 		goto out_unset_pci_mem_s4;
11271 	}
11272 
11273 	INIT_LIST_HEAD(&phba->active_rrq_list);
11274 	INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
11275 
11276 	/* Set up common device driver resources */
11277 	error = lpfc_setup_driver_resource_phase2(phba);
11278 	if (error) {
11279 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11280 				"1414 Failed to set up driver resource.\n");
11281 		goto out_unset_driver_resource_s4;
11282 	}
11283 
11284 	/* Get the default values for Model Name and Description */
11285 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11286 
11287 	/* Create SCSI host to the physical port */
11288 	error = lpfc_create_shost(phba);
11289 	if (error) {
11290 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11291 				"1415 Failed to create scsi host.\n");
11292 		goto out_unset_driver_resource;
11293 	}
11294 
11295 	/* Configure sysfs attributes */
11296 	vport = phba->pport;
11297 	error = lpfc_alloc_sysfs_attr(vport);
11298 	if (error) {
11299 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11300 				"1416 Failed to allocate sysfs attr\n");
11301 		goto out_destroy_shost;
11302 	}
11303 
11304 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
11305 	/* Now, trying to enable interrupt and bring up the device */
11306 	cfg_mode = phba->cfg_use_msi;
11307 
11308 	/* Put device to a known state before enabling interrupt */
11309 	lpfc_stop_port(phba);
11310 
11311 	/* Configure and enable interrupt */
11312 	intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11313 	if (intr_mode == LPFC_INTR_ERROR) {
11314 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11315 				"0426 Failed to enable interrupt.\n");
11316 		error = -ENODEV;
11317 		goto out_free_sysfs_attr;
11318 	}
11319 	/* Default to single EQ for non-MSI-X */
11320 	if (phba->intr_type != MSIX) {
11321 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11322 			phba->cfg_fcp_io_channel = 1;
11323 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11324 			phba->cfg_nvme_io_channel = 1;
11325 			if (phba->nvmet_support)
11326 				phba->cfg_nvmet_mrq = 1;
11327 		}
11328 		phba->io_channel_irqs = 1;
11329 	}
11330 
11331 	/* Set up SLI-4 HBA */
11332 	if (lpfc_sli4_hba_setup(phba)) {
11333 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11334 				"1421 Failed to set up hba\n");
11335 		error = -ENODEV;
11336 		goto out_disable_intr;
11337 	}
11338 
11339 	/* Log the current active interrupt mode */
11340 	phba->intr_mode = intr_mode;
11341 	lpfc_log_intr_mode(phba, intr_mode);
11342 
11343 	/* Perform post initialization setup */
11344 	lpfc_post_init_setup(phba);
11345 
11346 	/* NVME support in FW earlier in the driver load corrects the
11347 	 * FC4 type making a check for nvme_support unnecessary.
11348 	 */
11349 	if ((phba->nvmet_support == 0) &&
11350 	    (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11351 		/* Create NVME binding with nvme_fc_transport. This
11352 		 * ensures the vport is initialized.  If the localport
11353 		 * create fails, it should not unload the driver to
11354 		 * support field issues.
11355 		 */
11356 		error = lpfc_nvme_create_localport(vport);
11357 		if (error) {
11358 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11359 					"6004 NVME registration failed, "
11360 					"error x%x\n",
11361 					error);
11362 		}
11363 	}
11364 
11365 	/* check for firmware upgrade or downgrade */
11366 	if (phba->cfg_request_firmware_upgrade)
11367 		lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
11368 
11369 	/* Check if there are static vports to be created. */
11370 	lpfc_create_static_vport(phba);
11371 	return 0;
11372 
11373 out_disable_intr:
11374 	lpfc_sli4_disable_intr(phba);
11375 out_free_sysfs_attr:
11376 	lpfc_free_sysfs_attr(vport);
11377 out_destroy_shost:
11378 	lpfc_destroy_shost(phba);
11379 out_unset_driver_resource:
11380 	lpfc_unset_driver_resource_phase2(phba);
11381 out_unset_driver_resource_s4:
11382 	lpfc_sli4_driver_resource_unset(phba);
11383 out_unset_pci_mem_s4:
11384 	lpfc_sli4_pci_mem_unset(phba);
11385 out_disable_pci_dev:
11386 	lpfc_disable_pci_dev(phba);
11387 	if (shost)
11388 		scsi_host_put(shost);
11389 out_free_phba:
11390 	lpfc_hba_free(phba);
11391 	return error;
11392 }
11393 
11394 /**
11395  * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11396  * @pdev: pointer to PCI device
11397  *
11398  * This routine is called from the kernel's PCI subsystem to device with
11399  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11400  * removed from PCI bus, it performs all the necessary cleanup for the HBA
11401  * device to be removed from the PCI subsystem properly.
11402  **/
11403 static void
lpfc_pci_remove_one_s4(struct pci_dev * pdev)11404 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11405 {
11406 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11407 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11408 	struct lpfc_vport **vports;
11409 	struct lpfc_hba *phba = vport->phba;
11410 	int i;
11411 
11412 	/* Mark the device unloading flag */
11413 	spin_lock_irq(&phba->hbalock);
11414 	vport->load_flag |= FC_UNLOADING;
11415 	spin_unlock_irq(&phba->hbalock);
11416 
11417 	/* Free the HBA sysfs attributes */
11418 	lpfc_free_sysfs_attr(vport);
11419 
11420 	/* Release all the vports against this physical port */
11421 	vports = lpfc_create_vport_work_array(phba);
11422 	if (vports != NULL)
11423 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11424 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11425 				continue;
11426 			fc_vport_terminate(vports[i]->fc_vport);
11427 		}
11428 	lpfc_destroy_vport_work_array(phba, vports);
11429 
11430 	/* Remove FC host and then SCSI host with the physical port */
11431 	fc_remove_host(shost);
11432 	scsi_remove_host(shost);
11433 	/*
11434 	 * Bring down the SLI Layer. This step disables all interrupts,
11435 	 * clears the rings, discards all mailbox commands, and resets
11436 	 * the HBA FCoE function.
11437 	 */
11438 	lpfc_debugfs_terminate(vport);
11439 	lpfc_sli4_hba_unset(phba);
11440 
11441 	/* Perform ndlp cleanup on the physical port.  The nvme and nvmet
11442 	 * localports are destroyed after to cleanup all transport memory.
11443 	 */
11444 	lpfc_cleanup(vport);
11445 	lpfc_nvmet_destroy_targetport(phba);
11446 	lpfc_nvme_destroy_localport(vport);
11447 
11448 
11449 	lpfc_stop_hba_timers(phba);
11450 	spin_lock_irq(&phba->hbalock);
11451 	list_del_init(&vport->listentry);
11452 	spin_unlock_irq(&phba->hbalock);
11453 
11454 	/* Perform scsi free before driver resource_unset since scsi
11455 	 * buffers are released to their corresponding pools here.
11456 	 */
11457 	lpfc_scsi_free(phba);
11458 	lpfc_nvme_free(phba);
11459 	lpfc_free_iocb_list(phba);
11460 
11461 	lpfc_sli4_driver_resource_unset(phba);
11462 
11463 	/* Unmap adapter Control and Doorbell registers */
11464 	lpfc_sli4_pci_mem_unset(phba);
11465 
11466 	/* Release PCI resources and disable device's PCI function */
11467 	scsi_host_put(shost);
11468 	lpfc_disable_pci_dev(phba);
11469 
11470 	/* Finally, free the driver's device data structure */
11471 	lpfc_hba_free(phba);
11472 
11473 	return;
11474 }
11475 
11476 /**
11477  * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11478  * @pdev: pointer to PCI device
11479  * @msg: power management message
11480  *
11481  * This routine is called from the kernel's PCI subsystem to support system
11482  * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11483  * this method, it quiesces the device by stopping the driver's worker
11484  * thread for the device, turning off device's interrupt and DMA, and bring
11485  * the device offline. Note that as the driver implements the minimum PM
11486  * requirements to a power-aware driver's PM support for suspend/resume -- all
11487  * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11488  * method call will be treated as SUSPEND and the driver will fully
11489  * reinitialize its device during resume() method call, the driver will set
11490  * device to PCI_D3hot state in PCI config space instead of setting it
11491  * according to the @msg provided by the PM.
11492  *
11493  * Return code
11494  * 	0 - driver suspended the device
11495  * 	Error otherwise
11496  **/
11497 static int
lpfc_pci_suspend_one_s4(struct pci_dev * pdev,pm_message_t msg)11498 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11499 {
11500 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11501 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11502 
11503 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11504 			"2843 PCI device Power Management suspend.\n");
11505 
11506 	/* Bring down the device */
11507 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11508 	lpfc_offline(phba);
11509 	kthread_stop(phba->worker_thread);
11510 
11511 	/* Disable interrupt from device */
11512 	lpfc_sli4_disable_intr(phba);
11513 	lpfc_sli4_queue_destroy(phba);
11514 
11515 	/* Save device state to PCI config space */
11516 	pci_save_state(pdev);
11517 	pci_set_power_state(pdev, PCI_D3hot);
11518 
11519 	return 0;
11520 }
11521 
11522 /**
11523  * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11524  * @pdev: pointer to PCI device
11525  *
11526  * This routine is called from the kernel's PCI subsystem to support system
11527  * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11528  * this method, it restores the device's PCI config space state and fully
11529  * reinitializes the device and brings it online. Note that as the driver
11530  * implements the minimum PM requirements to a power-aware driver's PM for
11531  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11532  * to the suspend() method call will be treated as SUSPEND and the driver
11533  * will fully reinitialize its device during resume() method call, the device
11534  * will be set to PCI_D0 directly in PCI config space before restoring the
11535  * state.
11536  *
11537  * Return code
11538  * 	0 - driver suspended the device
11539  * 	Error otherwise
11540  **/
11541 static int
lpfc_pci_resume_one_s4(struct pci_dev * pdev)11542 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11543 {
11544 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11545 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11546 	uint32_t intr_mode;
11547 	int error;
11548 
11549 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11550 			"0292 PCI device Power Management resume.\n");
11551 
11552 	/* Restore device state from PCI config space */
11553 	pci_set_power_state(pdev, PCI_D0);
11554 	pci_restore_state(pdev);
11555 
11556 	/*
11557 	 * As the new kernel behavior of pci_restore_state() API call clears
11558 	 * device saved_state flag, need to save the restored state again.
11559 	 */
11560 	pci_save_state(pdev);
11561 
11562 	if (pdev->is_busmaster)
11563 		pci_set_master(pdev);
11564 
11565 	 /* Startup the kernel thread for this host adapter. */
11566 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
11567 					"lpfc_worker_%d", phba->brd_no);
11568 	if (IS_ERR(phba->worker_thread)) {
11569 		error = PTR_ERR(phba->worker_thread);
11570 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11571 				"0293 PM resume failed to start worker "
11572 				"thread: error=x%x.\n", error);
11573 		return error;
11574 	}
11575 
11576 	/* Configure and enable interrupt */
11577 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11578 	if (intr_mode == LPFC_INTR_ERROR) {
11579 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11580 				"0294 PM resume Failed to enable interrupt\n");
11581 		return -EIO;
11582 	} else
11583 		phba->intr_mode = intr_mode;
11584 
11585 	/* Restart HBA and bring it online */
11586 	lpfc_sli_brdrestart(phba);
11587 	lpfc_online(phba);
11588 
11589 	/* Log the current active interrupt mode */
11590 	lpfc_log_intr_mode(phba, phba->intr_mode);
11591 
11592 	return 0;
11593 }
11594 
11595 /**
11596  * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11597  * @phba: pointer to lpfc hba data structure.
11598  *
11599  * This routine is called to prepare the SLI4 device for PCI slot recover. It
11600  * aborts all the outstanding SCSI I/Os to the pci device.
11601  **/
11602 static void
lpfc_sli4_prep_dev_for_recover(struct lpfc_hba * phba)11603 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11604 {
11605 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11606 			"2828 PCI channel I/O abort preparing for recovery\n");
11607 	/*
11608 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11609 	 * and let the SCSI mid-layer to retry them to recover.
11610 	 */
11611 	lpfc_sli_abort_fcp_rings(phba);
11612 }
11613 
11614 /**
11615  * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11616  * @phba: pointer to lpfc hba data structure.
11617  *
11618  * This routine is called to prepare the SLI4 device for PCI slot reset. It
11619  * disables the device interrupt and pci device, and aborts the internal FCP
11620  * pending I/Os.
11621  **/
11622 static void
lpfc_sli4_prep_dev_for_reset(struct lpfc_hba * phba)11623 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11624 {
11625 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11626 			"2826 PCI channel disable preparing for reset\n");
11627 
11628 	/* Block any management I/Os to the device */
11629 	lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
11630 
11631 	/* Block all SCSI devices' I/Os on the host */
11632 	lpfc_scsi_dev_block(phba);
11633 
11634 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
11635 	lpfc_sli_flush_fcp_rings(phba);
11636 
11637 	/* stop all timers */
11638 	lpfc_stop_hba_timers(phba);
11639 
11640 	/* Disable interrupt and pci device */
11641 	lpfc_sli4_disable_intr(phba);
11642 	lpfc_sli4_queue_destroy(phba);
11643 	pci_disable_device(phba->pcidev);
11644 }
11645 
11646 /**
11647  * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11648  * @phba: pointer to lpfc hba data structure.
11649  *
11650  * This routine is called to prepare the SLI4 device for PCI slot permanently
11651  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11652  * pending I/Os.
11653  **/
11654 static void
lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba * phba)11655 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11656 {
11657 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11658 			"2827 PCI channel permanent disable for failure\n");
11659 
11660 	/* Block all SCSI devices' I/Os on the host */
11661 	lpfc_scsi_dev_block(phba);
11662 
11663 	/* stop all timers */
11664 	lpfc_stop_hba_timers(phba);
11665 
11666 	/* Clean up all driver's outstanding SCSI I/Os */
11667 	lpfc_sli_flush_fcp_rings(phba);
11668 }
11669 
11670 /**
11671  * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11672  * @pdev: pointer to PCI device.
11673  * @state: the current PCI connection state.
11674  *
11675  * This routine is called from the PCI subsystem for error handling to device
11676  * with SLI-4 interface spec. This function is called by the PCI subsystem
11677  * after a PCI bus error affecting this device has been detected. When this
11678  * function is invoked, it will need to stop all the I/Os and interrupt(s)
11679  * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11680  * for the PCI subsystem to perform proper recovery as desired.
11681  *
11682  * Return codes
11683  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11684  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11685  **/
11686 static pci_ers_result_t
lpfc_io_error_detected_s4(struct pci_dev * pdev,pci_channel_state_t state)11687 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11688 {
11689 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11690 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11691 
11692 	switch (state) {
11693 	case pci_channel_io_normal:
11694 		/* Non-fatal error, prepare for recovery */
11695 		lpfc_sli4_prep_dev_for_recover(phba);
11696 		return PCI_ERS_RESULT_CAN_RECOVER;
11697 	case pci_channel_io_frozen:
11698 		/* Fatal error, prepare for slot reset */
11699 		lpfc_sli4_prep_dev_for_reset(phba);
11700 		return PCI_ERS_RESULT_NEED_RESET;
11701 	case pci_channel_io_perm_failure:
11702 		/* Permanent failure, prepare for device down */
11703 		lpfc_sli4_prep_dev_for_perm_failure(phba);
11704 		return PCI_ERS_RESULT_DISCONNECT;
11705 	default:
11706 		/* Unknown state, prepare and request slot reset */
11707 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11708 				"2825 Unknown PCI error state: x%x\n", state);
11709 		lpfc_sli4_prep_dev_for_reset(phba);
11710 		return PCI_ERS_RESULT_NEED_RESET;
11711 	}
11712 }
11713 
11714 /**
11715  * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11716  * @pdev: pointer to PCI device.
11717  *
11718  * This routine is called from the PCI subsystem for error handling to device
11719  * with SLI-4 interface spec. It is called after PCI bus has been reset to
11720  * restart the PCI card from scratch, as if from a cold-boot. During the
11721  * PCI subsystem error recovery, after the driver returns
11722  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11723  * recovery and then call this routine before calling the .resume method to
11724  * recover the device. This function will initialize the HBA device, enable
11725  * the interrupt, but it will just put the HBA to offline state without
11726  * passing any I/O traffic.
11727  *
11728  * Return codes
11729  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11730  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11731  */
11732 static pci_ers_result_t
lpfc_io_slot_reset_s4(struct pci_dev * pdev)11733 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11734 {
11735 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11736 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11737 	struct lpfc_sli *psli = &phba->sli;
11738 	uint32_t intr_mode;
11739 
11740 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11741 	if (pci_enable_device_mem(pdev)) {
11742 		printk(KERN_ERR "lpfc: Cannot re-enable "
11743 			"PCI device after reset.\n");
11744 		return PCI_ERS_RESULT_DISCONNECT;
11745 	}
11746 
11747 	pci_restore_state(pdev);
11748 
11749 	/*
11750 	 * As the new kernel behavior of pci_restore_state() API call clears
11751 	 * device saved_state flag, need to save the restored state again.
11752 	 */
11753 	pci_save_state(pdev);
11754 
11755 	if (pdev->is_busmaster)
11756 		pci_set_master(pdev);
11757 
11758 	spin_lock_irq(&phba->hbalock);
11759 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11760 	spin_unlock_irq(&phba->hbalock);
11761 
11762 	/* Configure and enable interrupt */
11763 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11764 	if (intr_mode == LPFC_INTR_ERROR) {
11765 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11766 				"2824 Cannot re-enable interrupt after "
11767 				"slot reset.\n");
11768 		return PCI_ERS_RESULT_DISCONNECT;
11769 	} else
11770 		phba->intr_mode = intr_mode;
11771 
11772 	/* Log the current active interrupt mode */
11773 	lpfc_log_intr_mode(phba, phba->intr_mode);
11774 
11775 	return PCI_ERS_RESULT_RECOVERED;
11776 }
11777 
11778 /**
11779  * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11780  * @pdev: pointer to PCI device
11781  *
11782  * This routine is called from the PCI subsystem for error handling to device
11783  * with SLI-4 interface spec. It is called when kernel error recovery tells
11784  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11785  * error recovery. After this call, traffic can start to flow from this device
11786  * again.
11787  **/
11788 static void
lpfc_io_resume_s4(struct pci_dev * pdev)11789 lpfc_io_resume_s4(struct pci_dev *pdev)
11790 {
11791 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11792 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11793 
11794 	/*
11795 	 * In case of slot reset, as function reset is performed through
11796 	 * mailbox command which needs DMA to be enabled, this operation
11797 	 * has to be moved to the io resume phase. Taking device offline
11798 	 * will perform the necessary cleanup.
11799 	 */
11800 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11801 		/* Perform device reset */
11802 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11803 		lpfc_offline(phba);
11804 		lpfc_sli_brdrestart(phba);
11805 		/* Bring the device back online */
11806 		lpfc_online(phba);
11807 	}
11808 
11809 	/* Clean up Advanced Error Reporting (AER) if needed */
11810 	if (phba->hba_flag & HBA_AER_ENABLED)
11811 		pci_cleanup_aer_uncorrect_error_status(pdev);
11812 }
11813 
11814 /**
11815  * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11816  * @pdev: pointer to PCI device
11817  * @pid: pointer to PCI device identifier
11818  *
11819  * This routine is to be registered to the kernel's PCI subsystem. When an
11820  * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11821  * at PCI device-specific information of the device and driver to see if the
11822  * driver state that it can support this kind of device. If the match is
11823  * successful, the driver core invokes this routine. This routine dispatches
11824  * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11825  * do all the initialization that it needs to do to handle the HBA device
11826  * properly.
11827  *
11828  * Return code
11829  * 	0 - driver can claim the device
11830  * 	negative value - driver can not claim the device
11831  **/
11832 static int
lpfc_pci_probe_one(struct pci_dev * pdev,const struct pci_device_id * pid)11833 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11834 {
11835 	int rc;
11836 	struct lpfc_sli_intf intf;
11837 
11838 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
11839 		return -ENODEV;
11840 
11841 	if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
11842 	    (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
11843 		rc = lpfc_pci_probe_one_s4(pdev, pid);
11844 	else
11845 		rc = lpfc_pci_probe_one_s3(pdev, pid);
11846 
11847 	return rc;
11848 }
11849 
11850 /**
11851  * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11852  * @pdev: pointer to PCI device
11853  *
11854  * This routine is to be registered to the kernel's PCI subsystem. When an
11855  * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11856  * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11857  * remove routine, which will perform all the necessary cleanup for the
11858  * device to be removed from the PCI subsystem properly.
11859  **/
11860 static void
lpfc_pci_remove_one(struct pci_dev * pdev)11861 lpfc_pci_remove_one(struct pci_dev *pdev)
11862 {
11863 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11864 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11865 
11866 	switch (phba->pci_dev_grp) {
11867 	case LPFC_PCI_DEV_LP:
11868 		lpfc_pci_remove_one_s3(pdev);
11869 		break;
11870 	case LPFC_PCI_DEV_OC:
11871 		lpfc_pci_remove_one_s4(pdev);
11872 		break;
11873 	default:
11874 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11875 				"1424 Invalid PCI device group: 0x%x\n",
11876 				phba->pci_dev_grp);
11877 		break;
11878 	}
11879 	return;
11880 }
11881 
11882 /**
11883  * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11884  * @pdev: pointer to PCI device
11885  * @msg: power management message
11886  *
11887  * This routine is to be registered to the kernel's PCI subsystem to support
11888  * system Power Management (PM). When PM invokes this method, it dispatches
11889  * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11890  * suspend the device.
11891  *
11892  * Return code
11893  * 	0 - driver suspended the device
11894  * 	Error otherwise
11895  **/
11896 static int
lpfc_pci_suspend_one(struct pci_dev * pdev,pm_message_t msg)11897 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11898 {
11899 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11900 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11901 	int rc = -ENODEV;
11902 
11903 	switch (phba->pci_dev_grp) {
11904 	case LPFC_PCI_DEV_LP:
11905 		rc = lpfc_pci_suspend_one_s3(pdev, msg);
11906 		break;
11907 	case LPFC_PCI_DEV_OC:
11908 		rc = lpfc_pci_suspend_one_s4(pdev, msg);
11909 		break;
11910 	default:
11911 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11912 				"1425 Invalid PCI device group: 0x%x\n",
11913 				phba->pci_dev_grp);
11914 		break;
11915 	}
11916 	return rc;
11917 }
11918 
11919 /**
11920  * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11921  * @pdev: pointer to PCI device
11922  *
11923  * This routine is to be registered to the kernel's PCI subsystem to support
11924  * system Power Management (PM). When PM invokes this method, it dispatches
11925  * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11926  * resume the device.
11927  *
11928  * Return code
11929  * 	0 - driver suspended the device
11930  * 	Error otherwise
11931  **/
11932 static int
lpfc_pci_resume_one(struct pci_dev * pdev)11933 lpfc_pci_resume_one(struct pci_dev *pdev)
11934 {
11935 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11936 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11937 	int rc = -ENODEV;
11938 
11939 	switch (phba->pci_dev_grp) {
11940 	case LPFC_PCI_DEV_LP:
11941 		rc = lpfc_pci_resume_one_s3(pdev);
11942 		break;
11943 	case LPFC_PCI_DEV_OC:
11944 		rc = lpfc_pci_resume_one_s4(pdev);
11945 		break;
11946 	default:
11947 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11948 				"1426 Invalid PCI device group: 0x%x\n",
11949 				phba->pci_dev_grp);
11950 		break;
11951 	}
11952 	return rc;
11953 }
11954 
11955 /**
11956  * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11957  * @pdev: pointer to PCI device.
11958  * @state: the current PCI connection state.
11959  *
11960  * This routine is registered to the PCI subsystem for error handling. This
11961  * function is called by the PCI subsystem after a PCI bus error affecting
11962  * this device has been detected. When this routine is invoked, it dispatches
11963  * the action to the proper SLI-3 or SLI-4 device error detected handling
11964  * routine, which will perform the proper error detected operation.
11965  *
11966  * Return codes
11967  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11968  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11969  **/
11970 static pci_ers_result_t
lpfc_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)11971 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11972 {
11973 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11974 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11975 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11976 
11977 	switch (phba->pci_dev_grp) {
11978 	case LPFC_PCI_DEV_LP:
11979 		rc = lpfc_io_error_detected_s3(pdev, state);
11980 		break;
11981 	case LPFC_PCI_DEV_OC:
11982 		rc = lpfc_io_error_detected_s4(pdev, state);
11983 		break;
11984 	default:
11985 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11986 				"1427 Invalid PCI device group: 0x%x\n",
11987 				phba->pci_dev_grp);
11988 		break;
11989 	}
11990 	return rc;
11991 }
11992 
11993 /**
11994  * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11995  * @pdev: pointer to PCI device.
11996  *
11997  * This routine is registered to the PCI subsystem for error handling. This
11998  * function is called after PCI bus has been reset to restart the PCI card
11999  * from scratch, as if from a cold-boot. When this routine is invoked, it
12000  * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
12001  * routine, which will perform the proper device reset.
12002  *
12003  * Return codes
12004  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
12005  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12006  **/
12007 static pci_ers_result_t
lpfc_io_slot_reset(struct pci_dev * pdev)12008 lpfc_io_slot_reset(struct pci_dev *pdev)
12009 {
12010 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12011 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12012 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12013 
12014 	switch (phba->pci_dev_grp) {
12015 	case LPFC_PCI_DEV_LP:
12016 		rc = lpfc_io_slot_reset_s3(pdev);
12017 		break;
12018 	case LPFC_PCI_DEV_OC:
12019 		rc = lpfc_io_slot_reset_s4(pdev);
12020 		break;
12021 	default:
12022 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12023 				"1428 Invalid PCI device group: 0x%x\n",
12024 				phba->pci_dev_grp);
12025 		break;
12026 	}
12027 	return rc;
12028 }
12029 
12030 /**
12031  * lpfc_io_resume - lpfc method for resuming PCI I/O operation
12032  * @pdev: pointer to PCI device
12033  *
12034  * This routine is registered to the PCI subsystem for error handling. It
12035  * is called when kernel error recovery tells the lpfc driver that it is
12036  * OK to resume normal PCI operation after PCI bus error recovery. When
12037  * this routine is invoked, it dispatches the action to the proper SLI-3
12038  * or SLI-4 device io_resume routine, which will resume the device operation.
12039  **/
12040 static void
lpfc_io_resume(struct pci_dev * pdev)12041 lpfc_io_resume(struct pci_dev *pdev)
12042 {
12043 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12044 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12045 
12046 	switch (phba->pci_dev_grp) {
12047 	case LPFC_PCI_DEV_LP:
12048 		lpfc_io_resume_s3(pdev);
12049 		break;
12050 	case LPFC_PCI_DEV_OC:
12051 		lpfc_io_resume_s4(pdev);
12052 		break;
12053 	default:
12054 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12055 				"1429 Invalid PCI device group: 0x%x\n",
12056 				phba->pci_dev_grp);
12057 		break;
12058 	}
12059 	return;
12060 }
12061 
12062 /**
12063  * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12064  * @phba: pointer to lpfc hba data structure.
12065  *
12066  * This routine checks to see if OAS is supported for this adapter. If
12067  * supported, the configure Flash Optimized Fabric flag is set.  Otherwise,
12068  * the enable oas flag is cleared and the pool created for OAS device data
12069  * is destroyed.
12070  *
12071  **/
12072 void
lpfc_sli4_oas_verify(struct lpfc_hba * phba)12073 lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12074 {
12075 
12076 	if (!phba->cfg_EnableXLane)
12077 		return;
12078 
12079 	if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12080 		phba->cfg_fof = 1;
12081 	} else {
12082 		phba->cfg_fof = 0;
12083 		if (phba->device_data_mem_pool)
12084 			mempool_destroy(phba->device_data_mem_pool);
12085 		phba->device_data_mem_pool = NULL;
12086 	}
12087 
12088 	return;
12089 }
12090 
12091 /**
12092  * lpfc_fof_queue_setup - Set up all the fof queues
12093  * @phba: pointer to lpfc hba data structure.
12094  *
12095  * This routine is invoked to set up all the fof queues for the FC HBA
12096  * operation.
12097  *
12098  * Return codes
12099  *      0 - successful
12100  *      -ENOMEM - No available memory
12101  **/
12102 int
lpfc_fof_queue_setup(struct lpfc_hba * phba)12103 lpfc_fof_queue_setup(struct lpfc_hba *phba)
12104 {
12105 	struct lpfc_sli_ring *pring;
12106 	int rc;
12107 
12108 	rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12109 	if (rc)
12110 		return -ENOMEM;
12111 
12112 	if (phba->cfg_fof) {
12113 
12114 		rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12115 				    phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12116 		if (rc)
12117 			goto out_oas_cq;
12118 
12119 		rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12120 				    phba->sli4_hba.oas_cq, LPFC_FCP);
12121 		if (rc)
12122 			goto out_oas_wq;
12123 
12124 		/* Bind this CQ/WQ to the NVME ring */
12125 		pring = phba->sli4_hba.oas_wq->pring;
12126 		pring->sli.sli4.wqp =
12127 			(void *)phba->sli4_hba.oas_wq;
12128 		phba->sli4_hba.oas_cq->pring = pring;
12129 	}
12130 
12131 	return 0;
12132 
12133 out_oas_wq:
12134 	lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
12135 out_oas_cq:
12136 	lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12137 	return rc;
12138 
12139 }
12140 
12141 /**
12142  * lpfc_fof_queue_create - Create all the fof queues
12143  * @phba: pointer to lpfc hba data structure.
12144  *
12145  * This routine is invoked to allocate all the fof queues for the FC HBA
12146  * operation. For each SLI4 queue type, the parameters such as queue entry
12147  * count (queue depth) shall be taken from the module parameter. For now,
12148  * we just use some constant number as place holder.
12149  *
12150  * Return codes
12151  *      0 - successful
12152  *      -ENOMEM - No availble memory
12153  *      -EIO - The mailbox failed to complete successfully.
12154  **/
12155 int
lpfc_fof_queue_create(struct lpfc_hba * phba)12156 lpfc_fof_queue_create(struct lpfc_hba *phba)
12157 {
12158 	struct lpfc_queue *qdesc;
12159 	uint32_t wqesize;
12160 
12161 	/* Create FOF EQ */
12162 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
12163 				      phba->sli4_hba.eq_ecount);
12164 	if (!qdesc)
12165 		goto out_error;
12166 
12167 	phba->sli4_hba.fof_eq = qdesc;
12168 
12169 	if (phba->cfg_fof) {
12170 
12171 		/* Create OAS CQ */
12172 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
12173 						      phba->sli4_hba.cq_ecount);
12174 		if (!qdesc)
12175 			goto out_error;
12176 
12177 		phba->sli4_hba.oas_cq = qdesc;
12178 
12179 		/* Create OAS WQ */
12180 		wqesize = (phba->fcp_embed_io) ?
12181 				LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12182 		qdesc = lpfc_sli4_queue_alloc(phba, wqesize,
12183 					      phba->sli4_hba.wq_ecount);
12184 
12185 		if (!qdesc)
12186 			goto out_error;
12187 
12188 		phba->sli4_hba.oas_wq = qdesc;
12189 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
12190 
12191 	}
12192 	return 0;
12193 
12194 out_error:
12195 	lpfc_fof_queue_destroy(phba);
12196 	return -ENOMEM;
12197 }
12198 
12199 /**
12200  * lpfc_fof_queue_destroy - Destroy all the fof queues
12201  * @phba: pointer to lpfc hba data structure.
12202  *
12203  * This routine is invoked to release all the SLI4 queues with the FC HBA
12204  * operation.
12205  *
12206  * Return codes
12207  *      0 - successful
12208  **/
12209 int
lpfc_fof_queue_destroy(struct lpfc_hba * phba)12210 lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12211 {
12212 	/* Release FOF Event queue */
12213 	if (phba->sli4_hba.fof_eq != NULL) {
12214 		lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12215 		phba->sli4_hba.fof_eq = NULL;
12216 	}
12217 
12218 	/* Release OAS Completion queue */
12219 	if (phba->sli4_hba.oas_cq != NULL) {
12220 		lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12221 		phba->sli4_hba.oas_cq = NULL;
12222 	}
12223 
12224 	/* Release OAS Work queue */
12225 	if (phba->sli4_hba.oas_wq != NULL) {
12226 		lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12227 		phba->sli4_hba.oas_wq = NULL;
12228 	}
12229 	return 0;
12230 }
12231 
12232 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12233 
12234 static const struct pci_error_handlers lpfc_err_handler = {
12235 	.error_detected = lpfc_io_error_detected,
12236 	.slot_reset = lpfc_io_slot_reset,
12237 	.resume = lpfc_io_resume,
12238 };
12239 
12240 static struct pci_driver lpfc_driver = {
12241 	.name		= LPFC_DRIVER_NAME,
12242 	.id_table	= lpfc_id_table,
12243 	.probe		= lpfc_pci_probe_one,
12244 	.remove		= lpfc_pci_remove_one,
12245 	.shutdown	= lpfc_pci_remove_one,
12246 	.suspend        = lpfc_pci_suspend_one,
12247 	.resume		= lpfc_pci_resume_one,
12248 	.err_handler    = &lpfc_err_handler,
12249 };
12250 
12251 static const struct file_operations lpfc_mgmt_fop = {
12252 	.owner = THIS_MODULE,
12253 };
12254 
12255 static struct miscdevice lpfc_mgmt_dev = {
12256 	.minor = MISC_DYNAMIC_MINOR,
12257 	.name = "lpfcmgmt",
12258 	.fops = &lpfc_mgmt_fop,
12259 };
12260 
12261 /**
12262  * lpfc_init - lpfc module initialization routine
12263  *
12264  * This routine is to be invoked when the lpfc module is loaded into the
12265  * kernel. The special kernel macro module_init() is used to indicate the
12266  * role of this routine to the kernel as lpfc module entry point.
12267  *
12268  * Return codes
12269  *   0 - successful
12270  *   -ENOMEM - FC attach transport failed
12271  *   all others - failed
12272  */
12273 static int __init
lpfc_init(void)12274 lpfc_init(void)
12275 {
12276 	int error = 0;
12277 
12278 	printk(LPFC_MODULE_DESC "\n");
12279 	printk(LPFC_COPYRIGHT "\n");
12280 
12281 	error = misc_register(&lpfc_mgmt_dev);
12282 	if (error)
12283 		printk(KERN_ERR "Could not register lpfcmgmt device, "
12284 			"misc_register returned with status %d", error);
12285 
12286 	lpfc_transport_functions.vport_create = lpfc_vport_create;
12287 	lpfc_transport_functions.vport_delete = lpfc_vport_delete;
12288 	lpfc_transport_template =
12289 				fc_attach_transport(&lpfc_transport_functions);
12290 	if (lpfc_transport_template == NULL)
12291 		return -ENOMEM;
12292 	lpfc_vport_transport_template =
12293 		fc_attach_transport(&lpfc_vport_transport_functions);
12294 	if (lpfc_vport_transport_template == NULL) {
12295 		fc_release_transport(lpfc_transport_template);
12296 		return -ENOMEM;
12297 	}
12298 
12299 	/* Initialize in case vector mapping is needed */
12300 	lpfc_used_cpu = NULL;
12301 	lpfc_present_cpu = num_present_cpus();
12302 
12303 	error = pci_register_driver(&lpfc_driver);
12304 	if (error) {
12305 		fc_release_transport(lpfc_transport_template);
12306 		fc_release_transport(lpfc_vport_transport_template);
12307 	}
12308 
12309 	return error;
12310 }
12311 
12312 /**
12313  * lpfc_exit - lpfc module removal routine
12314  *
12315  * This routine is invoked when the lpfc module is removed from the kernel.
12316  * The special kernel macro module_exit() is used to indicate the role of
12317  * this routine to the kernel as lpfc module exit point.
12318  */
12319 static void __exit
lpfc_exit(void)12320 lpfc_exit(void)
12321 {
12322 	misc_deregister(&lpfc_mgmt_dev);
12323 	pci_unregister_driver(&lpfc_driver);
12324 	fc_release_transport(lpfc_transport_template);
12325 	fc_release_transport(lpfc_vport_transport_template);
12326 	if (_dump_buf_data) {
12327 		printk(KERN_ERR	"9062 BLKGRD: freeing %lu pages for "
12328 				"_dump_buf_data at 0x%p\n",
12329 				(1L << _dump_buf_data_order), _dump_buf_data);
12330 		free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12331 	}
12332 
12333 	if (_dump_buf_dif) {
12334 		printk(KERN_ERR	"9049 BLKGRD: freeing %lu pages for "
12335 				"_dump_buf_dif at 0x%p\n",
12336 				(1L << _dump_buf_dif_order), _dump_buf_dif);
12337 		free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12338 	}
12339 	kfree(lpfc_used_cpu);
12340 	idr_destroy(&lpfc_hba_index);
12341 }
12342 
12343 module_init(lpfc_init);
12344 module_exit(lpfc_exit);
12345 MODULE_LICENSE("GPL");
12346 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
12347 MODULE_AUTHOR("Broadcom");
12348 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
12349