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