xref: /linux/drivers/scsi/lpfc/lpfc_mbox.c (revision dbcedec3a31119d7594baacc743300d127c99c56)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2024 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi.h>
32 #include <scsi/fc/fc_fs.h>
33 
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_compat.h"
45 
46 /**
47  * lpfc_mbox_rsrc_prep - Prepare a mailbox with DMA buffer memory.
48  * @phba: pointer to lpfc hba data structure.
49  * @mbox: pointer to the driver internal queue element for mailbox command.
50  *
51  * A mailbox command consists of the pool memory for the command, @mbox, and
52  * one or more DMA buffers for the data transfer.  This routine provides
53  * a standard framework for allocating the dma buffer and assigning to the
54  * @mbox.  Callers should cleanup the mbox with a call to
55  * lpfc_mbox_rsrc_cleanup.
56  *
57  * The lpfc_mbuf_alloc routine acquires the hbalock so the caller is
58  * responsible to ensure the hbalock is released.  Also note that the
59  * driver design is a single dmabuf/mbuf per mbox in the ctx_buf.
60  *
61  **/
62 int
63 lpfc_mbox_rsrc_prep(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
64 {
65 	struct lpfc_dmabuf *mp;
66 
67 	mp = kmalloc(sizeof(*mp), GFP_KERNEL);
68 	if (!mp)
69 		return -ENOMEM;
70 
71 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
72 	if (!mp->virt) {
73 		kfree(mp);
74 		return -ENOMEM;
75 	}
76 
77 	memset(mp->virt, 0, LPFC_BPL_SIZE);
78 
79 	/* Initialization only.  Driver does not use a list of dmabufs. */
80 	INIT_LIST_HEAD(&mp->list);
81 	mbox->ctx_buf = mp;
82 	return 0;
83 }
84 
85 /**
86  * lpfc_mbox_rsrc_cleanup - Free the mailbox DMA buffer and virtual memory.
87  * @phba: pointer to lpfc hba data structure.
88  * @mbox: pointer to the driver internal queue element for mailbox command.
89  * @locked: value that indicates if the hbalock is held (1) or not (0).
90  *
91  * A mailbox command consists of the pool memory for the command, @mbox, and
92  * possibly a DMA buffer for the data transfer.  This routine provides
93  * a standard framework for releasing any dma buffers and freeing all
94  * memory resources in it as well as releasing the @mbox back to the @phba pool.
95  * Callers should use this routine for cleanup for all mailboxes prepped with
96  * lpfc_mbox_rsrc_prep.
97  *
98  **/
99 void
100 lpfc_mbox_rsrc_cleanup(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
101 		       enum lpfc_mbox_ctx locked)
102 {
103 	struct lpfc_dmabuf *mp;
104 
105 	mp = mbox->ctx_buf;
106 	mbox->ctx_buf = NULL;
107 
108 	/* Release the generic BPL buffer memory.  */
109 	if (mp) {
110 		if (locked == MBOX_THD_LOCKED)
111 			__lpfc_mbuf_free(phba, mp->virt, mp->phys);
112 		else
113 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
114 		kfree(mp);
115 	}
116 
117 	mempool_free(mbox, phba->mbox_mem_pool);
118 }
119 
120 /**
121  * lpfc_dump_static_vport - Dump HBA's static vport information.
122  * @phba: pointer to lpfc hba data structure.
123  * @pmb: pointer to the driver internal queue element for mailbox command.
124  * @offset: offset for dumping vport info.
125  *
126  * The dump mailbox command provides a method for the device driver to obtain
127  * various types of information from the HBA device.
128  *
129  * This routine prepares the mailbox command for dumping list of static
130  * vports to be created.
131  **/
132 int
133 lpfc_dump_static_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
134 		uint16_t offset)
135 {
136 	MAILBOX_t *mb;
137 	struct lpfc_dmabuf *mp;
138 	int rc;
139 
140 	mb = &pmb->u.mb;
141 
142 	/* Setup to dump vport info region */
143 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
144 	mb->mbxCommand = MBX_DUMP_MEMORY;
145 	mb->un.varDmp.type = DMP_NV_PARAMS;
146 	mb->un.varDmp.entry_index = offset;
147 	mb->un.varDmp.region_id = DMP_REGION_VPORT;
148 	mb->mbxOwner = OWN_HOST;
149 
150 	/* For SLI3 HBAs data is embedded in mailbox */
151 	if (phba->sli_rev != LPFC_SLI_REV4) {
152 		mb->un.varDmp.cv = 1;
153 		mb->un.varDmp.word_cnt = DMP_RSP_SIZE/sizeof(uint32_t);
154 		return 0;
155 	}
156 
157 	rc = lpfc_mbox_rsrc_prep(phba, pmb);
158 	if (rc) {
159 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
160 				"2605 %s: memory allocation failed\n",
161 				__func__);
162 		return 1;
163 	}
164 
165 	mp = pmb->ctx_buf;
166 	mb->un.varWords[3] = putPaddrLow(mp->phys);
167 	mb->un.varWords[4] = putPaddrHigh(mp->phys);
168 	mb->un.varDmp.sli4_length = sizeof(struct static_vport_info);
169 
170 	return 0;
171 }
172 
173 /**
174  * lpfc_down_link - Bring down HBAs link.
175  * @phba: pointer to lpfc hba data structure.
176  * @pmb: pointer to the driver internal queue element for mailbox command.
177  *
178  * This routine prepares a mailbox command to bring down HBA link.
179  **/
180 void
181 lpfc_down_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
182 {
183 	MAILBOX_t *mb;
184 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
185 	mb = &pmb->u.mb;
186 	mb->mbxCommand = MBX_DOWN_LINK;
187 	mb->mbxOwner = OWN_HOST;
188 }
189 
190 /**
191  * lpfc_dump_mem - Prepare a mailbox command for reading a region.
192  * @phba: pointer to lpfc hba data structure.
193  * @pmb: pointer to the driver internal queue element for mailbox command.
194  * @offset: offset into the region.
195  * @region_id: config region id.
196  *
197  * The dump mailbox command provides a method for the device driver to obtain
198  * various types of information from the HBA device.
199  *
200  * This routine prepares the mailbox command for dumping HBA's config region.
201  **/
202 void
203 lpfc_dump_mem(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, uint16_t offset,
204 		uint16_t region_id)
205 {
206 	MAILBOX_t *mb;
207 
208 	mb = &pmb->u.mb;
209 
210 	/* Setup to dump VPD region */
211 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
212 	mb->mbxCommand = MBX_DUMP_MEMORY;
213 	mb->un.varDmp.cv = 1;
214 	mb->un.varDmp.type = DMP_NV_PARAMS;
215 	mb->un.varDmp.entry_index = offset;
216 	mb->un.varDmp.region_id = region_id;
217 	mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
218 	mb->un.varDmp.co = 0;
219 	mb->un.varDmp.resp_offset = 0;
220 	mb->mbxOwner = OWN_HOST;
221 	return;
222 }
223 
224 /**
225  * lpfc_dump_wakeup_param - Prepare mailbox command for retrieving wakeup params
226  * @phba: pointer to lpfc hba data structure.
227  * @pmb: pointer to the driver internal queue element for mailbox command.
228  *
229  * This function create a dump memory mailbox command to dump wake up
230  * parameters.
231  */
232 void
233 lpfc_dump_wakeup_param(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
234 {
235 	MAILBOX_t *mb;
236 
237 	mb = &pmb->u.mb;
238 
239 	/* Setup to dump VPD region */
240 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
241 	mb->mbxCommand = MBX_DUMP_MEMORY;
242 	mb->mbxOwner = OWN_HOST;
243 	mb->un.varDmp.cv = 1;
244 	mb->un.varDmp.type = DMP_NV_PARAMS;
245 	if (phba->sli_rev < LPFC_SLI_REV4)
246 		mb->un.varDmp.entry_index = 0;
247 	mb->un.varDmp.region_id = WAKE_UP_PARMS_REGION_ID;
248 	mb->un.varDmp.word_cnt = WAKE_UP_PARMS_WORD_SIZE;
249 	mb->un.varDmp.co = 0;
250 	mb->un.varDmp.resp_offset = 0;
251 	return;
252 }
253 
254 /**
255  * lpfc_read_nv - Prepare a mailbox command for reading HBA's NVRAM param
256  * @phba: pointer to lpfc hba data structure.
257  * @pmb: pointer to the driver internal queue element for mailbox command.
258  *
259  * The read NVRAM mailbox command returns the HBA's non-volatile parameters
260  * that are used as defaults when the Fibre Channel link is brought on-line.
261  *
262  * This routine prepares the mailbox command for reading information stored
263  * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
264  **/
265 void
266 lpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
267 {
268 	MAILBOX_t *mb;
269 
270 	mb = &pmb->u.mb;
271 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
272 	mb->mbxCommand = MBX_READ_NV;
273 	mb->mbxOwner = OWN_HOST;
274 	return;
275 }
276 
277 /**
278  * lpfc_config_async - Prepare a mailbox command for enabling HBA async event
279  * @phba: pointer to lpfc hba data structure.
280  * @pmb: pointer to the driver internal queue element for mailbox command.
281  * @ring: ring number for the asynchronous event to be configured.
282  *
283  * The asynchronous event enable mailbox command is used to enable the
284  * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
285  * specifies the default ring to which events are posted.
286  *
287  * This routine prepares the mailbox command for enabling HBA asynchronous
288  * event support on a IOCB ring.
289  **/
290 void
291 lpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
292 		uint32_t ring)
293 {
294 	MAILBOX_t *mb;
295 
296 	mb = &pmb->u.mb;
297 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
298 	mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
299 	mb->un.varCfgAsyncEvent.ring = ring;
300 	mb->mbxOwner = OWN_HOST;
301 	return;
302 }
303 
304 /**
305  * lpfc_heart_beat - Prepare a mailbox command for heart beat
306  * @phba: pointer to lpfc hba data structure.
307  * @pmb: pointer to the driver internal queue element for mailbox command.
308  *
309  * The heart beat mailbox command is used to detect an unresponsive HBA, which
310  * is defined as any device where no error attention is sent and both mailbox
311  * and rings are not processed.
312  *
313  * This routine prepares the mailbox command for issuing a heart beat in the
314  * form of mailbox command to the HBA. The timely completion of the heart
315  * beat mailbox command indicates the health of the HBA.
316  **/
317 void
318 lpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
319 {
320 	MAILBOX_t *mb;
321 
322 	mb = &pmb->u.mb;
323 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
324 	mb->mbxCommand = MBX_HEARTBEAT;
325 	mb->mbxOwner = OWN_HOST;
326 	return;
327 }
328 
329 /**
330  * lpfc_read_topology - Prepare a mailbox command for reading HBA topology
331  * @phba: pointer to lpfc hba data structure.
332  * @pmb: pointer to the driver internal queue element for mailbox command.
333  * @mp: DMA buffer memory for reading the link attention information into.
334  *
335  * The read topology mailbox command is issued to read the link topology
336  * information indicated by the HBA port when the Link Event bit of the Host
337  * Attention (HSTATT) register is set to 1 (For SLI-3) or when an FC Link
338  * Attention ACQE is received from the port (For SLI-4). A Link Event
339  * Attention occurs based on an exception detected at the Fibre Channel link
340  * interface.
341  *
342  * This routine prepares the mailbox command for reading HBA link topology
343  * information. A DMA memory has been set aside and address passed to the
344  * HBA through @mp for the HBA to DMA link attention information into the
345  * memory as part of the execution of the mailbox command.
346  *
347  * Return codes
348  *    0 - Success (currently always return 0)
349  **/
350 int
351 lpfc_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
352 		   struct lpfc_dmabuf *mp)
353 {
354 	MAILBOX_t *mb;
355 
356 	mb = &pmb->u.mb;
357 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
358 
359 	INIT_LIST_HEAD(&mp->list);
360 	mb->mbxCommand = MBX_READ_TOPOLOGY;
361 	mb->un.varReadTop.lilpBde64.tus.f.bdeSize = LPFC_ALPA_MAP_SIZE;
362 	mb->un.varReadTop.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
363 	mb->un.varReadTop.lilpBde64.addrLow = putPaddrLow(mp->phys);
364 
365 	/* Save address for later completion and set the owner to host so that
366 	 * the FW knows this mailbox is available for processing.
367 	 */
368 	pmb->ctx_buf = mp;
369 	mb->mbxOwner = OWN_HOST;
370 	return (0);
371 }
372 
373 /**
374  * lpfc_clear_la - Prepare a mailbox command for clearing HBA link attention
375  * @phba: pointer to lpfc hba data structure.
376  * @pmb: pointer to the driver internal queue element for mailbox command.
377  *
378  * The clear link attention mailbox command is issued to clear the link event
379  * attention condition indicated by the Link Event bit of the Host Attention
380  * (HSTATT) register. The link event attention condition is cleared only if
381  * the event tag specified matches that of the current link event counter.
382  * The current event tag is read using the read link attention event mailbox
383  * command.
384  *
385  * This routine prepares the mailbox command for clearing HBA link attention
386  * information.
387  **/
388 void
389 lpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
390 {
391 	MAILBOX_t *mb;
392 
393 	mb = &pmb->u.mb;
394 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
395 
396 	mb->un.varClearLA.eventTag = phba->fc_eventTag;
397 	mb->mbxCommand = MBX_CLEAR_LA;
398 	mb->mbxOwner = OWN_HOST;
399 	return;
400 }
401 
402 /**
403  * lpfc_config_link - Prepare a mailbox command for configuring link on a HBA
404  * @phba: pointer to lpfc hba data structure.
405  * @pmb: pointer to the driver internal queue element for mailbox command.
406  *
407  * The configure link mailbox command is used before the initialize link
408  * mailbox command to override default value and to configure link-oriented
409  * parameters such as DID address and various timers. Typically, this
410  * command would be used after an F_Port login to set the returned DID address
411  * and the fabric timeout values. This command is not valid before a configure
412  * port command has configured the HBA port.
413  *
414  * This routine prepares the mailbox command for configuring link on a HBA.
415  **/
416 void
417 lpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
418 {
419 	struct lpfc_vport  *vport = phba->pport;
420 	MAILBOX_t *mb = &pmb->u.mb;
421 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
422 
423 	/* NEW_FEATURE
424 	 * SLI-2, Coalescing Response Feature.
425 	 */
426 	if (phba->cfg_cr_delay && (phba->sli_rev < LPFC_SLI_REV4)) {
427 		mb->un.varCfgLnk.cr = 1;
428 		mb->un.varCfgLnk.ci = 1;
429 		mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
430 		mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
431 	}
432 
433 	mb->un.varCfgLnk.myId = vport->fc_myDID;
434 	mb->un.varCfgLnk.edtov = phba->fc_edtov;
435 	mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
436 	mb->un.varCfgLnk.ratov = phba->fc_ratov;
437 	mb->un.varCfgLnk.rttov = phba->fc_rttov;
438 	mb->un.varCfgLnk.altov = phba->fc_altov;
439 	mb->un.varCfgLnk.crtov = phba->fc_crtov;
440 	mb->un.varCfgLnk.cscn = 0;
441 	if (phba->bbcredit_support && phba->cfg_enable_bbcr) {
442 		mb->un.varCfgLnk.cscn = 1;
443 		mb->un.varCfgLnk.bbscn = bf_get(lpfc_bbscn_def,
444 						 &phba->sli4_hba.bbscn_params);
445 	}
446 
447 	if (phba->cfg_ack0 && (phba->sli_rev < LPFC_SLI_REV4))
448 		mb->un.varCfgLnk.ack0_enable = 1;
449 
450 	mb->mbxCommand = MBX_CONFIG_LINK;
451 	mb->mbxOwner = OWN_HOST;
452 	return;
453 }
454 
455 /**
456  * lpfc_config_msi - Prepare a mailbox command for configuring msi-x
457  * @phba: pointer to lpfc hba data structure.
458  * @pmb: pointer to the driver internal queue element for mailbox command.
459  *
460  * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
461  * MSI-X multi-message interrupt vector association to interrupt attention
462  * conditions.
463  *
464  * Return codes
465  *    0 - Success
466  *    -EINVAL - Failure
467  **/
468 int
469 lpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
470 {
471 	MAILBOX_t *mb = &pmb->u.mb;
472 	uint32_t attentionConditions[2];
473 
474 	/* Sanity check */
475 	if (phba->cfg_use_msi != 2) {
476 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
477 				"0475 Not configured for supporting MSI-X "
478 				"cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
479 		return -EINVAL;
480 	}
481 
482 	if (phba->sli_rev < 3) {
483 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
484 				"0476 HBA not supporting SLI-3 or later "
485 				"SLI Revision: 0x%x\n", phba->sli_rev);
486 		return -EINVAL;
487 	}
488 
489 	/* Clear mailbox command fields */
490 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
491 
492 	/*
493 	 * SLI-3, Message Signaled Interrupt Feature.
494 	 */
495 
496 	/* Multi-message attention configuration */
497 	attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
498 				  HA_LATT | HA_MBATT);
499 	attentionConditions[1] = 0;
500 
501 	mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
502 	mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
503 
504 	/*
505 	 * Set up message number to HA bit association
506 	 */
507 #ifdef __BIG_ENDIAN_BITFIELD
508 	/* RA0 (FCP Ring) */
509 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
510 	/* RA1 (Other Protocol Extra Ring) */
511 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
512 #else   /*  __LITTLE_ENDIAN_BITFIELD */
513 	/* RA0 (FCP Ring) */
514 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
515 	/* RA1 (Other Protocol Extra Ring) */
516 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
517 #endif
518 	/* Multi-message interrupt autoclear configuration*/
519 	mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
520 	mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
521 
522 	/* For now, HBA autoclear does not work reliably, disable it */
523 	mb->un.varCfgMSI.autoClearHA[0] = 0;
524 	mb->un.varCfgMSI.autoClearHA[1] = 0;
525 
526 	/* Set command and owner bit */
527 	mb->mbxCommand = MBX_CONFIG_MSI;
528 	mb->mbxOwner = OWN_HOST;
529 
530 	return 0;
531 }
532 
533 /**
534  * lpfc_init_link - Prepare a mailbox command for initialize link on a HBA
535  * @phba: pointer to lpfc hba data structure.
536  * @pmb: pointer to the driver internal queue element for mailbox command.
537  * @topology: the link topology for the link to be initialized to.
538  * @linkspeed: the link speed for the link to be initialized to.
539  *
540  * The initialize link mailbox command is used to initialize the Fibre
541  * Channel link. This command must follow a configure port command that
542  * establishes the mode of operation.
543  *
544  * This routine prepares the mailbox command for initializing link on a HBA
545  * with the specified link topology and speed.
546  **/
547 void
548 lpfc_init_link(struct lpfc_hba * phba,
549 	       LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
550 {
551 	lpfc_vpd_t *vpd;
552 	MAILBOX_t *mb;
553 
554 	mb = &pmb->u.mb;
555 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
556 
557 	switch (topology) {
558 	case FLAGS_TOPOLOGY_MODE_LOOP_PT:
559 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
560 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
561 		break;
562 	case FLAGS_TOPOLOGY_MODE_PT_PT:
563 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
564 		break;
565 	case FLAGS_TOPOLOGY_MODE_LOOP:
566 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
567 		break;
568 	case FLAGS_TOPOLOGY_MODE_PT_LOOP:
569 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
570 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
571 		break;
572 	case FLAGS_LOCAL_LB:
573 		mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
574 		break;
575 	}
576 
577 	/* Topology handling for ASIC_GEN_NUM 0xC and later */
578 	if ((phba->sli4_hba.pc_sli4_params.sli_family == LPFC_SLI_INTF_FAMILY_G6 ||
579 	     phba->sli4_hba.pc_sli4_params.if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
580 	    !(phba->sli4_hba.pc_sli4_params.pls) &&
581 	    mb->un.varInitLnk.link_flags & FLAGS_TOPOLOGY_MODE_LOOP) {
582 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
583 		phba->cfg_topology = FLAGS_TOPOLOGY_MODE_PT_PT;
584 	}
585 
586 	/* Enable asynchronous ABTS responses from firmware */
587 	if (phba->sli_rev == LPFC_SLI_REV3 && !phba->cfg_fcp_wait_abts_rsp)
588 		mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
589 
590 	/* NEW_FEATURE
591 	 * Setting up the link speed
592 	 */
593 	vpd = &phba->vpd;
594 	if (vpd->rev.feaLevelHigh >= 0x02){
595 		switch(linkspeed){
596 		case LPFC_USER_LINK_SPEED_1G:
597 			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
598 			mb->un.varInitLnk.link_speed = LINK_SPEED_1G;
599 			break;
600 		case LPFC_USER_LINK_SPEED_2G:
601 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
602 			mb->un.varInitLnk.link_speed = LINK_SPEED_2G;
603 			break;
604 		case LPFC_USER_LINK_SPEED_4G:
605 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
606 			mb->un.varInitLnk.link_speed = LINK_SPEED_4G;
607 			break;
608 		case LPFC_USER_LINK_SPEED_8G:
609 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
610 			mb->un.varInitLnk.link_speed = LINK_SPEED_8G;
611 			break;
612 		case LPFC_USER_LINK_SPEED_10G:
613 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
614 			mb->un.varInitLnk.link_speed = LINK_SPEED_10G;
615 			break;
616 		case LPFC_USER_LINK_SPEED_16G:
617 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
618 			mb->un.varInitLnk.link_speed = LINK_SPEED_16G;
619 			break;
620 		case LPFC_USER_LINK_SPEED_32G:
621 			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
622 			mb->un.varInitLnk.link_speed = LINK_SPEED_32G;
623 			break;
624 		case LPFC_USER_LINK_SPEED_64G:
625 			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
626 			mb->un.varInitLnk.link_speed = LINK_SPEED_64G;
627 			break;
628 		case LPFC_USER_LINK_SPEED_AUTO:
629 		default:
630 			mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
631 			break;
632 		}
633 
634 	}
635 	else
636 		mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
637 
638 	mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
639 	mb->mbxOwner = OWN_HOST;
640 	mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
641 	return;
642 }
643 
644 /**
645  * lpfc_read_sparam - Prepare a mailbox command for reading HBA parameters
646  * @phba: pointer to lpfc hba data structure.
647  * @pmb: pointer to the driver internal queue element for mailbox command.
648  * @vpi: virtual N_Port identifier.
649  *
650  * The read service parameter mailbox command is used to read the HBA port
651  * service parameters. The service parameters are read into the buffer
652  * specified directly by a BDE in the mailbox command. These service
653  * parameters may then be used to build the payload of an N_Port/F_POrt
654  * login request and reply (LOGI/ACC).
655  *
656  * This routine prepares the mailbox command for reading HBA port service
657  * parameters. The DMA memory is allocated in this function and the addresses
658  * are populated into the mailbox command for the HBA to DMA the service
659  * parameters into.
660  *
661  * Return codes
662  *    0 - Success
663  *    1 - DMA memory allocation failed
664  **/
665 int
666 lpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
667 {
668 	struct lpfc_dmabuf *mp;
669 	MAILBOX_t *mb;
670 	int rc;
671 
672 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
673 
674 	/* Get a buffer to hold the HBAs Service Parameters */
675 	rc = lpfc_mbox_rsrc_prep(phba, pmb);
676 	if (rc) {
677 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
678 			        "0301 READ_SPARAM: no buffers\n");
679 		return 1;
680 	}
681 
682 	mp = pmb->ctx_buf;
683 	mb = &pmb->u.mb;
684 	mb->mbxOwner = OWN_HOST;
685 	mb->mbxCommand = MBX_READ_SPARM64;
686 	mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
687 	mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
688 	mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
689 	if (phba->sli_rev >= LPFC_SLI_REV3)
690 		mb->un.varRdSparm.vpi = phba->vpi_ids[vpi];
691 
692 	return (0);
693 }
694 
695 /**
696  * lpfc_unreg_did - Prepare a mailbox command for unregistering DID
697  * @phba: pointer to lpfc hba data structure.
698  * @vpi: virtual N_Port identifier.
699  * @did: remote port identifier.
700  * @pmb: pointer to the driver internal queue element for mailbox command.
701  *
702  * The unregister DID mailbox command is used to unregister an N_Port/F_Port
703  * login for an unknown RPI by specifying the DID of a remote port. This
704  * command frees an RPI context in the HBA port. This has the effect of
705  * performing an implicit N_Port/F_Port logout.
706  *
707  * This routine prepares the mailbox command for unregistering a remote
708  * N_Port/F_Port (DID) login.
709  **/
710 void
711 lpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
712 	       LPFC_MBOXQ_t * pmb)
713 {
714 	MAILBOX_t *mb;
715 
716 	mb = &pmb->u.mb;
717 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
718 
719 	mb->un.varUnregDID.did = did;
720 	mb->un.varUnregDID.vpi = vpi;
721 	if ((vpi != 0xffff) &&
722 	    (phba->sli_rev == LPFC_SLI_REV4))
723 		mb->un.varUnregDID.vpi = phba->vpi_ids[vpi];
724 
725 	mb->mbxCommand = MBX_UNREG_D_ID;
726 	mb->mbxOwner = OWN_HOST;
727 	return;
728 }
729 
730 /**
731  * lpfc_read_config - Prepare a mailbox command for reading HBA configuration
732  * @phba: pointer to lpfc hba data structure.
733  * @pmb: pointer to the driver internal queue element for mailbox command.
734  *
735  * The read configuration mailbox command is used to read the HBA port
736  * configuration parameters. This mailbox command provides a method for
737  * seeing any parameters that may have changed via various configuration
738  * mailbox commands.
739  *
740  * This routine prepares the mailbox command for reading out HBA configuration
741  * parameters.
742  **/
743 void
744 lpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
745 {
746 	MAILBOX_t *mb;
747 
748 	mb = &pmb->u.mb;
749 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
750 
751 	mb->mbxCommand = MBX_READ_CONFIG;
752 	mb->mbxOwner = OWN_HOST;
753 	return;
754 }
755 
756 /**
757  * lpfc_read_lnk_stat - Prepare a mailbox command for reading HBA link stats
758  * @phba: pointer to lpfc hba data structure.
759  * @pmb: pointer to the driver internal queue element for mailbox command.
760  *
761  * The read link status mailbox command is used to read the link status from
762  * the HBA. Link status includes all link-related error counters. These
763  * counters are maintained by the HBA and originated in the link hardware
764  * unit. Note that all of these counters wrap.
765  *
766  * This routine prepares the mailbox command for reading out HBA link status.
767  **/
768 void
769 lpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
770 {
771 	MAILBOX_t *mb;
772 
773 	mb = &pmb->u.mb;
774 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
775 
776 	mb->mbxCommand = MBX_READ_LNK_STAT;
777 	mb->mbxOwner = OWN_HOST;
778 	return;
779 }
780 
781 /**
782  * lpfc_reg_rpi - Prepare a mailbox command for registering remote login
783  * @phba: pointer to lpfc hba data structure.
784  * @vpi: virtual N_Port identifier.
785  * @did: remote port identifier.
786  * @param: pointer to memory holding the server parameters.
787  * @pmb: pointer to the driver internal queue element for mailbox command.
788  * @rpi: the rpi to use in the registration (usually only used for SLI4.
789  *
790  * The registration login mailbox command is used to register an N_Port or
791  * F_Port login. This registration allows the HBA to cache the remote N_Port
792  * service parameters internally and thereby make the appropriate FC-2
793  * decisions. The remote port service parameters are handed off by the driver
794  * to the HBA using a descriptor entry that directly identifies a buffer in
795  * host memory. In exchange, the HBA returns an RPI identifier.
796  *
797  * This routine prepares the mailbox command for registering remote port login.
798  * The function allocates DMA buffer for passing the service parameters to the
799  * HBA with the mailbox command.
800  *
801  * Return codes
802  *    0 - Success
803  *    1 - DMA memory allocation failed
804  **/
805 int
806 lpfc_reg_rpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
807 	     uint8_t *param, LPFC_MBOXQ_t *pmb, uint16_t rpi)
808 {
809 	MAILBOX_t *mb = &pmb->u.mb;
810 	uint8_t *sparam;
811 	struct lpfc_dmabuf *mp;
812 	int rc;
813 
814 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
815 
816 	mb->un.varRegLogin.rpi = 0;
817 	if (phba->sli_rev == LPFC_SLI_REV4)
818 		mb->un.varRegLogin.rpi = phba->sli4_hba.rpi_ids[rpi];
819 	if (phba->sli_rev >= LPFC_SLI_REV3)
820 		mb->un.varRegLogin.vpi = phba->vpi_ids[vpi];
821 	mb->un.varRegLogin.did = did;
822 	mb->mbxOwner = OWN_HOST;
823 
824 	/* Get a buffer to hold NPorts Service Parameters */
825 	rc = lpfc_mbox_rsrc_prep(phba, pmb);
826 	if (rc) {
827 		mb->mbxCommand = MBX_REG_LOGIN64;
828 		/* REG_LOGIN: no buffers */
829 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
830 				"0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
831 				"rpi x%x\n", vpi, did, rpi);
832 		return 1;
833 	}
834 
835 	/* Copy param's into a new buffer */
836 	mp = pmb->ctx_buf;
837 	sparam = mp->virt;
838 	memcpy(sparam, param, sizeof (struct serv_parm));
839 
840 	/* Finish initializing the mailbox. */
841 	mb->mbxCommand = MBX_REG_LOGIN64;
842 	mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
843 	mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
844 	mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
845 
846 	return 0;
847 }
848 
849 /**
850  * lpfc_unreg_login - Prepare a mailbox command for unregistering remote login
851  * @phba: pointer to lpfc hba data structure.
852  * @vpi: virtual N_Port identifier.
853  * @rpi: remote port identifier
854  * @pmb: pointer to the driver internal queue element for mailbox command.
855  *
856  * The unregistration login mailbox command is used to unregister an N_Port
857  * or F_Port login. This command frees an RPI context in the HBA. It has the
858  * effect of performing an implicit N_Port/F_Port logout.
859  *
860  * This routine prepares the mailbox command for unregistering remote port
861  * login.
862  *
863  * For SLI4 ports, the rpi passed to this function must be the physical
864  * rpi value, not the logical index.
865  **/
866 void
867 lpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
868 		 LPFC_MBOXQ_t * pmb)
869 {
870 	MAILBOX_t *mb;
871 
872 	mb = &pmb->u.mb;
873 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
874 
875 	mb->un.varUnregLogin.rpi = rpi;
876 	mb->un.varUnregLogin.rsvd1 = 0;
877 	if (phba->sli_rev >= LPFC_SLI_REV3)
878 		mb->un.varUnregLogin.vpi = phba->vpi_ids[vpi];
879 
880 	mb->mbxCommand = MBX_UNREG_LOGIN;
881 	mb->mbxOwner = OWN_HOST;
882 
883 	return;
884 }
885 
886 /**
887  * lpfc_sli4_unreg_all_rpis - unregister all RPIs for a vport on SLI4 HBA.
888  * @vport: pointer to a vport object.
889  *
890  * This routine sends mailbox command to unregister all active RPIs for
891  * a vport.
892  **/
893 void
894 lpfc_sli4_unreg_all_rpis(struct lpfc_vport *vport)
895 {
896 	struct lpfc_hba  *phba  = vport->phba;
897 	LPFC_MBOXQ_t     *mbox;
898 	int rc;
899 
900 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
901 	if (mbox) {
902 		/*
903 		 * For SLI4 functions, the rpi field is overloaded for
904 		 * the vport context unreg all.  This routine passes
905 		 * 0 for the rpi field in lpfc_unreg_login for compatibility
906 		 * with SLI3 and then overrides the rpi field with the
907 		 * expected value for SLI4.
908 		 */
909 		lpfc_unreg_login(phba, vport->vpi, phba->vpi_ids[vport->vpi],
910 				 mbox);
911 		mbox->u.mb.un.varUnregLogin.rsvd1 = 0x4000;
912 		mbox->vport = vport;
913 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
914 		mbox->ctx_ndlp = NULL;
915 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
916 		if (rc == MBX_NOT_FINISHED)
917 			mempool_free(mbox, phba->mbox_mem_pool);
918 	}
919 }
920 
921 /**
922  * lpfc_reg_vpi - Prepare a mailbox command for registering vport identifier
923  * @vport: pointer to a vport object.
924  * @pmb: pointer to the driver internal queue element for mailbox command.
925  *
926  * The registration vport identifier mailbox command is used to activate a
927  * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
928  * N_Port_ID against the information in the selected virtual N_Port context
929  * block and marks it active to allow normal processing of IOCB commands and
930  * received unsolicited exchanges.
931  *
932  * This routine prepares the mailbox command for registering a virtual N_Port.
933  **/
934 void
935 lpfc_reg_vpi(struct lpfc_vport *vport, LPFC_MBOXQ_t *pmb)
936 {
937 	MAILBOX_t *mb = &pmb->u.mb;
938 	struct lpfc_hba *phba = vport->phba;
939 
940 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
941 	/*
942 	 * Set the re-reg VPI bit for f/w to update the MAC address.
943 	 */
944 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
945 		!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag))
946 		mb->un.varRegVpi.upd = 1;
947 
948 	mb->un.varRegVpi.vpi = phba->vpi_ids[vport->vpi];
949 	mb->un.varRegVpi.sid = vport->fc_myDID;
950 	if (phba->sli_rev == LPFC_SLI_REV4)
951 		mb->un.varRegVpi.vfi = phba->sli4_hba.vfi_ids[vport->vfi];
952 	else
953 		mb->un.varRegVpi.vfi = vport->vfi + vport->phba->vfi_base;
954 	memcpy(mb->un.varRegVpi.wwn, &vport->fc_portname,
955 	       sizeof(struct lpfc_name));
956 	mb->un.varRegVpi.wwn[0] = cpu_to_le32(mb->un.varRegVpi.wwn[0]);
957 	mb->un.varRegVpi.wwn[1] = cpu_to_le32(mb->un.varRegVpi.wwn[1]);
958 
959 	mb->mbxCommand = MBX_REG_VPI;
960 	mb->mbxOwner = OWN_HOST;
961 	return;
962 
963 }
964 
965 /**
966  * lpfc_unreg_vpi - Prepare a mailbox command for unregistering vport id
967  * @phba: pointer to lpfc hba data structure.
968  * @vpi: virtual N_Port identifier.
969  * @pmb: pointer to the driver internal queue element for mailbox command.
970  *
971  * The unregistration vport identifier mailbox command is used to inactivate
972  * a virtual N_Port. The driver must have logged out and unregistered all
973  * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
974  * unregisters any default RPIs associated with the specified vpi, aborting
975  * any active exchanges. The HBA will post the mailbox response after making
976  * the virtual N_Port inactive.
977  *
978  * This routine prepares the mailbox command for unregistering a virtual
979  * N_Port.
980  **/
981 void
982 lpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
983 {
984 	MAILBOX_t *mb = &pmb->u.mb;
985 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
986 
987 	if (phba->sli_rev == LPFC_SLI_REV3)
988 		mb->un.varUnregVpi.vpi = phba->vpi_ids[vpi];
989 	else if (phba->sli_rev >= LPFC_SLI_REV4)
990 		mb->un.varUnregVpi.sli4_vpi = phba->vpi_ids[vpi];
991 
992 	mb->mbxCommand = MBX_UNREG_VPI;
993 	mb->mbxOwner = OWN_HOST;
994 	return;
995 
996 }
997 
998 /**
999  * lpfc_config_pcb_setup - Set up IOCB rings in the Port Control Block (PCB)
1000  * @phba: pointer to lpfc hba data structure.
1001  *
1002  * This routine sets up and initializes the IOCB rings in the Port Control
1003  * Block (PCB).
1004  **/
1005 static void
1006 lpfc_config_pcb_setup(struct lpfc_hba * phba)
1007 {
1008 	struct lpfc_sli *psli = &phba->sli;
1009 	struct lpfc_sli_ring *pring;
1010 	PCB_t *pcbp = phba->pcb;
1011 	dma_addr_t pdma_addr;
1012 	uint32_t offset;
1013 	uint32_t iocbCnt = 0;
1014 	int i;
1015 
1016 	pcbp->maxRing = (psli->num_rings - 1);
1017 
1018 	for (i = 0; i < psli->num_rings; i++) {
1019 		pring = &psli->sli3_ring[i];
1020 
1021 		pring->sli.sli3.sizeCiocb =
1022 			phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE :
1023 							SLI2_IOCB_CMD_SIZE;
1024 		pring->sli.sli3.sizeRiocb =
1025 			phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE :
1026 							SLI2_IOCB_RSP_SIZE;
1027 		/* A ring MUST have both cmd and rsp entries defined to be
1028 		   valid */
1029 		if ((pring->sli.sli3.numCiocb == 0) ||
1030 			(pring->sli.sli3.numRiocb == 0)) {
1031 			pcbp->rdsc[i].cmdEntries = 0;
1032 			pcbp->rdsc[i].rspEntries = 0;
1033 			pcbp->rdsc[i].cmdAddrHigh = 0;
1034 			pcbp->rdsc[i].rspAddrHigh = 0;
1035 			pcbp->rdsc[i].cmdAddrLow = 0;
1036 			pcbp->rdsc[i].rspAddrLow = 0;
1037 			pring->sli.sli3.cmdringaddr = NULL;
1038 			pring->sli.sli3.rspringaddr = NULL;
1039 			continue;
1040 		}
1041 		/* Command ring setup for ring */
1042 		pring->sli.sli3.cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
1043 		pcbp->rdsc[i].cmdEntries = pring->sli.sli3.numCiocb;
1044 
1045 		offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
1046 			 (uint8_t *) phba->slim2p.virt;
1047 		pdma_addr = phba->slim2p.phys + offset;
1048 		pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
1049 		pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
1050 		iocbCnt += pring->sli.sli3.numCiocb;
1051 
1052 		/* Response ring setup for ring */
1053 		pring->sli.sli3.rspringaddr = (void *) &phba->IOCBs[iocbCnt];
1054 
1055 		pcbp->rdsc[i].rspEntries = pring->sli.sli3.numRiocb;
1056 		offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
1057 			 (uint8_t *)phba->slim2p.virt;
1058 		pdma_addr = phba->slim2p.phys + offset;
1059 		pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
1060 		pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
1061 		iocbCnt += pring->sli.sli3.numRiocb;
1062 	}
1063 }
1064 
1065 /**
1066  * lpfc_read_rev - Prepare a mailbox command for reading HBA revision
1067  * @phba: pointer to lpfc hba data structure.
1068  * @pmb: pointer to the driver internal queue element for mailbox command.
1069  *
1070  * The read revision mailbox command is used to read the revision levels of
1071  * the HBA components. These components include hardware units, resident
1072  * firmware, and available firmware. HBAs that supports SLI-3 mode of
1073  * operation provide different response information depending on the version
1074  * requested by the driver.
1075  *
1076  * This routine prepares the mailbox command for reading HBA revision
1077  * information.
1078  **/
1079 void
1080 lpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1081 {
1082 	MAILBOX_t *mb = &pmb->u.mb;
1083 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1084 	mb->un.varRdRev.cv = 1;
1085 	mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
1086 	mb->mbxCommand = MBX_READ_REV;
1087 	mb->mbxOwner = OWN_HOST;
1088 	return;
1089 }
1090 
1091 void
1092 lpfc_sli4_swap_str(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1093 {
1094 	MAILBOX_t *mb = &pmb->u.mb;
1095 	struct lpfc_mqe *mqe;
1096 
1097 	switch (mb->mbxCommand) {
1098 	case  MBX_READ_REV:
1099 		 mqe = &pmb->u.mqe;
1100 		lpfc_sli_pcimem_bcopy(mqe->un.read_rev.fw_name,
1101 				 mqe->un.read_rev.fw_name, 16);
1102 		lpfc_sli_pcimem_bcopy(mqe->un.read_rev.ulp_fw_name,
1103 				 mqe->un.read_rev.ulp_fw_name, 16);
1104 		break;
1105 	default:
1106 		break;
1107 	}
1108 	return;
1109 }
1110 
1111 /**
1112  * lpfc_build_hbq_profile2 - Set up the HBQ Selection Profile 2
1113  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1114  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1115  *
1116  * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
1117  * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
1118  * the Sequence Length Test using the fields in the Selection Profile 2
1119  * extension in words 20:31.
1120  **/
1121 static void
1122 lpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
1123 			struct lpfc_hbq_init  *hbq_desc)
1124 {
1125 	hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
1126 	hbqmb->profiles.profile2.maxlen     = hbq_desc->maxlen;
1127 	hbqmb->profiles.profile2.seqlenoff  = hbq_desc->seqlenoff;
1128 }
1129 
1130 /**
1131  * lpfc_build_hbq_profile3 - Set up the HBQ Selection Profile 3
1132  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1133  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1134  *
1135  * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
1136  * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
1137  * the Sequence Length Test and Byte Field Test using the fields in the
1138  * Selection Profile 3 extension in words 20:31.
1139  **/
1140 static void
1141 lpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
1142 			struct lpfc_hbq_init  *hbq_desc)
1143 {
1144 	hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
1145 	hbqmb->profiles.profile3.maxlen     = hbq_desc->maxlen;
1146 	hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
1147 	hbqmb->profiles.profile3.seqlenoff  = hbq_desc->seqlenoff;
1148 	memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
1149 	       sizeof(hbqmb->profiles.profile3.cmdmatch));
1150 }
1151 
1152 /**
1153  * lpfc_build_hbq_profile5 - Set up the HBQ Selection Profile 5
1154  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1155  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1156  *
1157  * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
1158  * HBA tests the initial frame of an incoming sequence using the frame's
1159  * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
1160  * and Byte Field Test using the fields in the Selection Profile 5 extension
1161  * words 20:31.
1162  **/
1163 static void
1164 lpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
1165 			struct lpfc_hbq_init  *hbq_desc)
1166 {
1167 	hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
1168 	hbqmb->profiles.profile5.maxlen     = hbq_desc->maxlen;
1169 	hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
1170 	hbqmb->profiles.profile5.seqlenoff  = hbq_desc->seqlenoff;
1171 	memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
1172 	       sizeof(hbqmb->profiles.profile5.cmdmatch));
1173 }
1174 
1175 /**
1176  * lpfc_config_hbq - Prepare a mailbox command for configuring an HBQ
1177  * @phba: pointer to lpfc hba data structure.
1178  * @id: HBQ identifier.
1179  * @hbq_desc: pointer to the HBA descriptor data structure.
1180  * @hbq_entry_index: index of the HBQ entry data structures.
1181  * @pmb: pointer to the driver internal queue element for mailbox command.
1182  *
1183  * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
1184  * an HBQ. The configuration binds events that require buffers to a particular
1185  * ring and HBQ based on a selection profile.
1186  *
1187  * This routine prepares the mailbox command for configuring an HBQ.
1188  **/
1189 void
1190 lpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
1191 		 struct lpfc_hbq_init *hbq_desc,
1192 		uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
1193 {
1194 	int i;
1195 	MAILBOX_t *mb = &pmb->u.mb;
1196 	struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
1197 
1198 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1199 	hbqmb->hbqId = id;
1200 	hbqmb->entry_count = hbq_desc->entry_count;   /* # entries in HBQ */
1201 	hbqmb->recvNotify = hbq_desc->rn;             /* Receive
1202 						       * Notification */
1203 	hbqmb->numMask    = hbq_desc->mask_count;     /* # R_CTL/TYPE masks
1204 						       * # in words 0-19 */
1205 	hbqmb->profile    = hbq_desc->profile;	      /* Selection profile:
1206 						       * 0 = all,
1207 						       * 7 = logentry */
1208 	hbqmb->ringMask   = hbq_desc->ring_mask;      /* Binds HBQ to a ring
1209 						       * e.g. Ring0=b0001,
1210 						       * ring2=b0100 */
1211 	hbqmb->headerLen  = hbq_desc->headerLen;      /* 0 if not profile 4
1212 						       * or 5 */
1213 	hbqmb->logEntry   = hbq_desc->logEntry;       /* Set to 1 if this
1214 						       * HBQ will be used
1215 						       * for LogEntry
1216 						       * buffers */
1217 	hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
1218 		hbq_entry_index * sizeof(struct lpfc_hbq_entry);
1219 	hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
1220 
1221 	mb->mbxCommand = MBX_CONFIG_HBQ;
1222 	mb->mbxOwner = OWN_HOST;
1223 
1224 				/* Copy info for profiles 2,3,5. Other
1225 				 * profiles this area is reserved
1226 				 */
1227 	if (hbq_desc->profile == 2)
1228 		lpfc_build_hbq_profile2(hbqmb, hbq_desc);
1229 	else if (hbq_desc->profile == 3)
1230 		lpfc_build_hbq_profile3(hbqmb, hbq_desc);
1231 	else if (hbq_desc->profile == 5)
1232 		lpfc_build_hbq_profile5(hbqmb, hbq_desc);
1233 
1234 	/* Return if no rctl / type masks for this HBQ */
1235 	if (!hbq_desc->mask_count)
1236 		return;
1237 
1238 	/* Otherwise we setup specific rctl / type masks for this HBQ */
1239 	for (i = 0; i < hbq_desc->mask_count; i++) {
1240 		hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
1241 		hbqmb->hbqMasks[i].tmask  = hbq_desc->hbqMasks[i].tmask;
1242 		hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
1243 		hbqmb->hbqMasks[i].rctlmask  = hbq_desc->hbqMasks[i].rctlmask;
1244 	}
1245 
1246 	return;
1247 }
1248 
1249 /**
1250  * lpfc_config_ring - Prepare a mailbox command for configuring an IOCB ring
1251  * @phba: pointer to lpfc hba data structure.
1252  * @ring: ring number/index
1253  * @pmb: pointer to the driver internal queue element for mailbox command.
1254  *
1255  * The configure ring mailbox command is used to configure an IOCB ring. This
1256  * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
1257  * ring. This is used to map incoming sequences to a particular ring whose
1258  * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
1259  * attempt to configure a ring whose number is greater than the number
1260  * specified in the Port Control Block (PCB). It is an error to issue the
1261  * configure ring command more than once with the same ring number. The HBA
1262  * returns an error if the driver attempts this.
1263  *
1264  * This routine prepares the mailbox command for configuring IOCB ring.
1265  **/
1266 void
1267 lpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
1268 {
1269 	int i;
1270 	MAILBOX_t *mb = &pmb->u.mb;
1271 	struct lpfc_sli *psli;
1272 	struct lpfc_sli_ring *pring;
1273 
1274 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1275 
1276 	mb->un.varCfgRing.ring = ring;
1277 	mb->un.varCfgRing.maxOrigXchg = 0;
1278 	mb->un.varCfgRing.maxRespXchg = 0;
1279 	mb->un.varCfgRing.recvNotify = 1;
1280 
1281 	psli = &phba->sli;
1282 	pring = &psli->sli3_ring[ring];
1283 	mb->un.varCfgRing.numMask = pring->num_mask;
1284 	mb->mbxCommand = MBX_CONFIG_RING;
1285 	mb->mbxOwner = OWN_HOST;
1286 
1287 	/* Is this ring configured for a specific profile */
1288 	if (pring->prt[0].profile) {
1289 		mb->un.varCfgRing.profile = pring->prt[0].profile;
1290 		return;
1291 	}
1292 
1293 	/* Otherwise we setup specific rctl / type masks for this ring */
1294 	for (i = 0; i < pring->num_mask; i++) {
1295 		mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
1296 		if (mb->un.varCfgRing.rrRegs[i].rval != FC_RCTL_ELS_REQ)
1297 			mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
1298 		else
1299 			mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
1300 		mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
1301 		mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
1302 	}
1303 
1304 	return;
1305 }
1306 
1307 /**
1308  * lpfc_config_port - Prepare a mailbox command for configuring port
1309  * @phba: pointer to lpfc hba data structure.
1310  * @pmb: pointer to the driver internal queue element for mailbox command.
1311  *
1312  * The configure port mailbox command is used to identify the Port Control
1313  * Block (PCB) in the driver memory. After this command is issued, the
1314  * driver must not access the mailbox in the HBA without first resetting
1315  * the HBA. The HBA may copy the PCB information to internal storage for
1316  * subsequent use; the driver can not change the PCB information unless it
1317  * resets the HBA.
1318  *
1319  * This routine prepares the mailbox command for configuring port.
1320  **/
1321 void
1322 lpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1323 {
1324 	MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
1325 	MAILBOX_t *mb = &pmb->u.mb;
1326 	dma_addr_t pdma_addr;
1327 	uint32_t bar_low, bar_high;
1328 	size_t offset;
1329 	struct lpfc_hgp hgp;
1330 	int i;
1331 	uint32_t pgp_offset;
1332 
1333 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1334 	mb->mbxCommand = MBX_CONFIG_PORT;
1335 	mb->mbxOwner = OWN_HOST;
1336 
1337 	mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
1338 
1339 	offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
1340 	pdma_addr = phba->slim2p.phys + offset;
1341 	mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
1342 	mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
1343 
1344 	/* Always Host Group Pointer is in SLIM */
1345 	mb->un.varCfgPort.hps = 1;
1346 
1347 	/* If HBA supports SLI=3 ask for it */
1348 
1349 	if (phba->sli_rev == LPFC_SLI_REV3 && phba->vpd.sli3Feat.cerbm) {
1350 		if (phba->cfg_enable_bg)
1351 			mb->un.varCfgPort.cbg = 1; /* configure BlockGuard */
1352 		mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
1353 		mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
1354 		mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
1355 		if (phba->max_vpi && phba->cfg_enable_npiv &&
1356 		    phba->vpd.sli3Feat.cmv) {
1357 			mb->un.varCfgPort.max_vpi = LPFC_MAX_VPI;
1358 			mb->un.varCfgPort.cmv = 1;
1359 		} else
1360 			mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
1361 	} else
1362 		phba->sli_rev = LPFC_SLI_REV2;
1363 	mb->un.varCfgPort.sli_mode = phba->sli_rev;
1364 
1365 	/* If this is an SLI3 port, configure async status notification. */
1366 	if (phba->sli_rev == LPFC_SLI_REV3)
1367 		mb->un.varCfgPort.casabt = 1;
1368 
1369 	/* Now setup pcb */
1370 	phba->pcb->type = TYPE_NATIVE_SLI2;
1371 	phba->pcb->feature = FEATURE_INITIAL_SLI2;
1372 
1373 	/* Setup Mailbox pointers */
1374 	phba->pcb->mailBoxSize = sizeof(MAILBOX_t) + MAILBOX_EXT_SIZE;
1375 	offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
1376 	pdma_addr = phba->slim2p.phys + offset;
1377 	phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
1378 	phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
1379 
1380 	/*
1381 	 * Setup Host Group ring pointer.
1382 	 *
1383 	 * For efficiency reasons, the ring get/put pointers can be
1384 	 * placed in adapter memory (SLIM) rather than in host memory.
1385 	 * This allows firmware to avoid PCI reads/writes when updating
1386 	 * and checking pointers.
1387 	 *
1388 	 * The firmware recognizes the use of SLIM memory by comparing
1389 	 * the address of the get/put pointers structure with that of
1390 	 * the SLIM BAR (BAR0).
1391 	 *
1392 	 * Caution: be sure to use the PCI config space value of BAR0/BAR1
1393 	 * (the hardware's view of the base address), not the OS's
1394 	 * value of pci_resource_start() as the OS value may be a cookie
1395 	 * for ioremap/iomap.
1396 	 */
1397 
1398 
1399 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
1400 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
1401 
1402 	/*
1403 	 * Set up HGP - Port Memory
1404 	 *
1405 	 * The port expects the host get/put pointers to reside in memory
1406 	 * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
1407 	 * area of SLIM.  In SLI-2 mode, there's an additional 16 reserved
1408 	 * words (0x40 bytes).  This area is not reserved if HBQs are
1409 	 * configured in SLI-3.
1410 	 *
1411 	 * CR0Put    - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
1412 	 * RR0Get                      0xc4              0x84
1413 	 * CR1Put                      0xc8              0x88
1414 	 * RR1Get                      0xcc              0x8c
1415 	 * CR2Put                      0xd0              0x90
1416 	 * RR2Get                      0xd4              0x94
1417 	 * CR3Put                      0xd8              0x98
1418 	 * RR3Get                      0xdc              0x9c
1419 	 *
1420 	 * Reserved                    0xa0-0xbf
1421 	 *    If HBQs configured:
1422 	 *                         HBQ 0 Put ptr  0xc0
1423 	 *                         HBQ 1 Put ptr  0xc4
1424 	 *                         HBQ 2 Put ptr  0xc8
1425 	 *                         ......
1426 	 *                         HBQ(M-1)Put Pointer 0xc0+(M-1)*4
1427 	 *
1428 	 */
1429 
1430 	if (phba->cfg_hostmem_hgp && phba->sli_rev != 3) {
1431 		phba->host_gp = (struct lpfc_hgp __iomem *)
1432 				 &phba->mbox->us.s2.host[0];
1433 		phba->hbq_put = NULL;
1434 		offset = (uint8_t *)&phba->mbox->us.s2.host -
1435 			(uint8_t *)phba->slim2p.virt;
1436 		pdma_addr = phba->slim2p.phys + offset;
1437 		phba->pcb->hgpAddrHigh = putPaddrHigh(pdma_addr);
1438 		phba->pcb->hgpAddrLow = putPaddrLow(pdma_addr);
1439 	} else {
1440 		/* Always Host Group Pointer is in SLIM */
1441 		mb->un.varCfgPort.hps = 1;
1442 
1443 		if (phba->sli_rev == 3) {
1444 			phba->host_gp = &mb_slim->us.s3.host[0];
1445 			phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
1446 		} else {
1447 			phba->host_gp = &mb_slim->us.s2.host[0];
1448 			phba->hbq_put = NULL;
1449 		}
1450 
1451 		/* mask off BAR0's flag bits 0 - 3 */
1452 		phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
1453 			(void __iomem *)phba->host_gp -
1454 			(void __iomem *)phba->MBslimaddr;
1455 		if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
1456 			phba->pcb->hgpAddrHigh = bar_high;
1457 		else
1458 			phba->pcb->hgpAddrHigh = 0;
1459 		/* write HGP data to SLIM at the required longword offset */
1460 		memset(&hgp, 0, sizeof(struct lpfc_hgp));
1461 
1462 		for (i = 0; i < phba->sli.num_rings; i++) {
1463 			lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
1464 				    sizeof(*phba->host_gp));
1465 		}
1466 	}
1467 
1468 	/* Setup Port Group offset */
1469 	if (phba->sli_rev == 3)
1470 		pgp_offset = offsetof(struct lpfc_sli2_slim,
1471 				      mbx.us.s3_pgp.port);
1472 	else
1473 		pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
1474 	pdma_addr = phba->slim2p.phys + pgp_offset;
1475 	phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
1476 	phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
1477 
1478 	/* Use callback routine to setp rings in the pcb */
1479 	lpfc_config_pcb_setup(phba);
1480 
1481 	/* special handling for LC HBAs */
1482 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
1483 		uint32_t hbainit[5];
1484 
1485 		lpfc_hba_init(phba, hbainit);
1486 
1487 		memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
1488 	}
1489 
1490 	/* Swap PCB if needed */
1491 	lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
1492 }
1493 
1494 /**
1495  * lpfc_kill_board - Prepare a mailbox command for killing board
1496  * @phba: pointer to lpfc hba data structure.
1497  * @pmb: pointer to the driver internal queue element for mailbox command.
1498  *
1499  * The kill board mailbox command is used to tell firmware to perform a
1500  * graceful shutdown of a channel on a specified board to prepare for reset.
1501  * When the kill board mailbox command is received, the ER3 bit is set to 1
1502  * in the Host Status register and the ER Attention bit is set to 1 in the
1503  * Host Attention register of the HBA function that received the kill board
1504  * command.
1505  *
1506  * This routine prepares the mailbox command for killing the board in
1507  * preparation for a graceful shutdown.
1508  **/
1509 void
1510 lpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1511 {
1512 	MAILBOX_t *mb = &pmb->u.mb;
1513 
1514 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1515 	mb->mbxCommand = MBX_KILL_BOARD;
1516 	mb->mbxOwner = OWN_HOST;
1517 	return;
1518 }
1519 
1520 /**
1521  * lpfc_mbox_put - Put a mailbox cmd into the tail of driver's mailbox queue
1522  * @phba: pointer to lpfc hba data structure.
1523  * @mbq: pointer to the driver internal queue element for mailbox command.
1524  *
1525  * Driver maintains a internal mailbox command queue implemented as a linked
1526  * list. When a mailbox command is issued, it shall be put into the mailbox
1527  * command queue such that they shall be processed orderly as HBA can process
1528  * one mailbox command at a time.
1529  **/
1530 void
1531 lpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1532 {
1533 	struct lpfc_sli *psli;
1534 
1535 	psli = &phba->sli;
1536 
1537 	list_add_tail(&mbq->list, &psli->mboxq);
1538 
1539 	psli->mboxq_cnt++;
1540 
1541 	return;
1542 }
1543 
1544 /**
1545  * lpfc_mbox_get - Remove a mailbox cmd from the head of driver's mailbox queue
1546  * @phba: pointer to lpfc hba data structure.
1547  *
1548  * Driver maintains a internal mailbox command queue implemented as a linked
1549  * list. When a mailbox command is issued, it shall be put into the mailbox
1550  * command queue such that they shall be processed orderly as HBA can process
1551  * one mailbox command at a time. After HBA finished processing a mailbox
1552  * command, the driver will remove a pending mailbox command from the head of
1553  * the mailbox command queue and send to the HBA for processing.
1554  *
1555  * Return codes
1556  *    pointer to the driver internal queue element for mailbox command.
1557  **/
1558 LPFC_MBOXQ_t *
1559 lpfc_mbox_get(struct lpfc_hba * phba)
1560 {
1561 	LPFC_MBOXQ_t *mbq = NULL;
1562 	struct lpfc_sli *psli = &phba->sli;
1563 
1564 	list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
1565 	if (mbq)
1566 		psli->mboxq_cnt--;
1567 
1568 	return mbq;
1569 }
1570 
1571 /**
1572  * __lpfc_mbox_cmpl_put - Put mailbox cmd into mailbox cmd complete list
1573  * @phba: pointer to lpfc hba data structure.
1574  * @mbq: pointer to the driver internal queue element for mailbox command.
1575  *
1576  * This routine put the completed mailbox command into the mailbox command
1577  * complete list. This is the unlocked version of the routine. The mailbox
1578  * complete list is used by the driver worker thread to process mailbox
1579  * complete callback functions outside the driver interrupt handler.
1580  **/
1581 void
1582 __lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1583 {
1584 	list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
1585 }
1586 
1587 /**
1588  * lpfc_mbox_cmpl_put - Put mailbox command into mailbox command complete list
1589  * @phba: pointer to lpfc hba data structure.
1590  * @mbq: pointer to the driver internal queue element for mailbox command.
1591  *
1592  * This routine put the completed mailbox command into the mailbox command
1593  * complete list. This is the locked version of the routine. The mailbox
1594  * complete list is used by the driver worker thread to process mailbox
1595  * complete callback functions outside the driver interrupt handler.
1596  **/
1597 void
1598 lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1599 {
1600 	unsigned long iflag;
1601 
1602 	/* This function expects to be called from interrupt context */
1603 	spin_lock_irqsave(&phba->hbalock, iflag);
1604 	__lpfc_mbox_cmpl_put(phba, mbq);
1605 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1606 	return;
1607 }
1608 
1609 /**
1610  * lpfc_mbox_cmd_check - Check the validality of a mailbox command
1611  * @phba: pointer to lpfc hba data structure.
1612  * @mboxq: pointer to the driver internal queue element for mailbox command.
1613  *
1614  * This routine is to check whether a mailbox command is valid to be issued.
1615  * This check will be performed by both the mailbox issue API when a client
1616  * is to issue a mailbox command to the mailbox transport.
1617  *
1618  * Return 0 - pass the check, -ENODEV - fail the check
1619  **/
1620 int
1621 lpfc_mbox_cmd_check(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1622 {
1623 	/* Mailbox command that have a completion handler must also have a
1624 	 * vport specified.
1625 	 */
1626 	if (mboxq->mbox_cmpl && mboxq->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
1627 	    mboxq->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
1628 		if (!mboxq->vport) {
1629 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1630 					"1814 Mbox x%x failed, no vport\n",
1631 					mboxq->u.mb.mbxCommand);
1632 			dump_stack();
1633 			return -ENODEV;
1634 		}
1635 	}
1636 	return 0;
1637 }
1638 
1639 /**
1640  * lpfc_mbox_dev_check - Check the device state for issuing a mailbox command
1641  * @phba: pointer to lpfc hba data structure.
1642  *
1643  * This routine is to check whether the HBA device is ready for posting a
1644  * mailbox command. It is used by the mailbox transport API at the time the
1645  * to post a mailbox command to the device.
1646  *
1647  * Return 0 - pass the check, -ENODEV - fail the check
1648  **/
1649 int
1650 lpfc_mbox_dev_check(struct lpfc_hba *phba)
1651 {
1652 	/* If the PCI channel is in offline state, do not issue mbox */
1653 	if (unlikely(pci_channel_offline(phba->pcidev)))
1654 		return -ENODEV;
1655 
1656 	/* If the HBA is in error state, do not issue mbox */
1657 	if (phba->link_state == LPFC_HBA_ERROR)
1658 		return -ENODEV;
1659 
1660 	return 0;
1661 }
1662 
1663 /**
1664  * lpfc_mbox_tmo_val - Retrieve mailbox command timeout value
1665  * @phba: pointer to lpfc hba data structure.
1666  * @mboxq: pointer to the driver internal queue element for mailbox command.
1667  *
1668  * This routine retrieves the proper timeout value according to the mailbox
1669  * command code.
1670  *
1671  * Return codes
1672  *    Timeout value to be used for the given mailbox command
1673  **/
1674 int
1675 lpfc_mbox_tmo_val(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1676 {
1677 	MAILBOX_t *mbox = &mboxq->u.mb;
1678 	uint8_t subsys, opcode;
1679 
1680 	switch (mbox->mbxCommand) {
1681 	case MBX_WRITE_NV:	/* 0x03 */
1682 	case MBX_DUMP_MEMORY:	/* 0x17 */
1683 	case MBX_UPDATE_CFG:	/* 0x1B */
1684 	case MBX_DOWN_LOAD:	/* 0x1C */
1685 	case MBX_DEL_LD_ENTRY:	/* 0x1D */
1686 	case MBX_WRITE_VPARMS:	/* 0x32 */
1687 	case MBX_LOAD_AREA:	/* 0x81 */
1688 	case MBX_WRITE_WWN:     /* 0x98 */
1689 	case MBX_LOAD_EXP_ROM:	/* 0x9C */
1690 	case MBX_ACCESS_VDATA:	/* 0xA5 */
1691 		return LPFC_MBOX_TMO_FLASH_CMD;
1692 	case MBX_SLI4_CONFIG:	/* 0x9b */
1693 		subsys = lpfc_sli_config_mbox_subsys_get(phba, mboxq);
1694 		opcode = lpfc_sli_config_mbox_opcode_get(phba, mboxq);
1695 		if (subsys == LPFC_MBOX_SUBSYSTEM_COMMON) {
1696 			switch (opcode) {
1697 			case LPFC_MBOX_OPCODE_READ_OBJECT:
1698 			case LPFC_MBOX_OPCODE_WRITE_OBJECT:
1699 			case LPFC_MBOX_OPCODE_READ_OBJECT_LIST:
1700 			case LPFC_MBOX_OPCODE_DELETE_OBJECT:
1701 			case LPFC_MBOX_OPCODE_GET_PROFILE_LIST:
1702 			case LPFC_MBOX_OPCODE_SET_ACT_PROFILE:
1703 			case LPFC_MBOX_OPCODE_GET_PROFILE_CONFIG:
1704 			case LPFC_MBOX_OPCODE_SET_PROFILE_CONFIG:
1705 			case LPFC_MBOX_OPCODE_GET_FACTORY_PROFILE_CONFIG:
1706 			case LPFC_MBOX_OPCODE_GET_PROFILE_CAPACITIES:
1707 			case LPFC_MBOX_OPCODE_SEND_ACTIVATION:
1708 			case LPFC_MBOX_OPCODE_RESET_LICENSES:
1709 			case LPFC_MBOX_OPCODE_SET_BOOT_CONFIG:
1710 			case LPFC_MBOX_OPCODE_GET_VPD_DATA:
1711 			case LPFC_MBOX_OPCODE_SET_PHYSICAL_LINK_CONFIG:
1712 				return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
1713 			}
1714 		}
1715 		if (subsys == LPFC_MBOX_SUBSYSTEM_FCOE) {
1716 			switch (opcode) {
1717 			case LPFC_MBOX_OPCODE_FCOE_SET_FCLINK_SETTINGS:
1718 				return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
1719 			}
1720 		}
1721 		return LPFC_MBOX_SLI4_CONFIG_TMO;
1722 	}
1723 	return LPFC_MBOX_TMO;
1724 }
1725 
1726 /**
1727  * lpfc_sli4_mbx_sge_set - Set a sge entry in non-embedded mailbox command
1728  * @mbox: pointer to lpfc mbox command.
1729  * @sgentry: sge entry index.
1730  * @phyaddr: physical address for the sge
1731  * @length: Length of the sge.
1732  *
1733  * This routine sets up an entry in the non-embedded mailbox command at the sge
1734  * index location.
1735  **/
1736 void
1737 lpfc_sli4_mbx_sge_set(struct lpfcMboxq *mbox, uint32_t sgentry,
1738 		      dma_addr_t phyaddr, uint32_t length)
1739 {
1740 	struct lpfc_mbx_nembed_cmd *nembed_sge;
1741 
1742 	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1743 				&mbox->u.mqe.un.nembed_cmd;
1744 	nembed_sge->sge[sgentry].pa_lo = putPaddrLow(phyaddr);
1745 	nembed_sge->sge[sgentry].pa_hi = putPaddrHigh(phyaddr);
1746 	nembed_sge->sge[sgentry].length = length;
1747 }
1748 
1749 /**
1750  * lpfc_sli4_mbx_sge_get - Get a sge entry from non-embedded mailbox command
1751  * @mbox: pointer to lpfc mbox command.
1752  * @sgentry: sge entry index.
1753  * @sge: pointer to lpfc mailbox sge to load into.
1754  *
1755  * This routine gets an entry from the non-embedded mailbox command at the sge
1756  * index location.
1757  **/
1758 void
1759 lpfc_sli4_mbx_sge_get(struct lpfcMboxq *mbox, uint32_t sgentry,
1760 		      struct lpfc_mbx_sge *sge)
1761 {
1762 	struct lpfc_mbx_nembed_cmd *nembed_sge;
1763 
1764 	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1765 				&mbox->u.mqe.un.nembed_cmd;
1766 	sge->pa_lo = nembed_sge->sge[sgentry].pa_lo;
1767 	sge->pa_hi = nembed_sge->sge[sgentry].pa_hi;
1768 	sge->length = nembed_sge->sge[sgentry].length;
1769 }
1770 
1771 /**
1772  * lpfc_sli4_mbox_cmd_free - Free a sli4 mailbox command
1773  * @phba: pointer to lpfc hba data structure.
1774  * @mbox: pointer to lpfc mbox command.
1775  *
1776  * This routine cleans up and releases an SLI4 mailbox command that was
1777  * configured using lpfc_sli4_config.  It accounts for the embedded and
1778  * non-embedded config types.
1779  **/
1780 void
1781 lpfc_sli4_mbox_cmd_free(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1782 {
1783 	struct lpfc_mbx_sli4_config *sli4_cfg;
1784 	struct lpfc_mbx_sge sge;
1785 	dma_addr_t phyaddr;
1786 	uint32_t sgecount, sgentry;
1787 
1788 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
1789 
1790 	/* For embedded mbox command, just free the mbox command */
1791 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1792 		mempool_free(mbox, phba->mbox_mem_pool);
1793 		return;
1794 	}
1795 
1796 	/* For non-embedded mbox command, we need to free the pages first */
1797 	sgecount = bf_get(lpfc_mbox_hdr_sge_cnt, &sli4_cfg->header.cfg_mhdr);
1798 	/* There is nothing we can do if there is no sge address array */
1799 	if (unlikely(!mbox->sge_array)) {
1800 		mempool_free(mbox, phba->mbox_mem_pool);
1801 		return;
1802 	}
1803 	/* Each non-embedded DMA memory was allocated in the length of a page */
1804 	for (sgentry = 0; sgentry < sgecount; sgentry++) {
1805 		lpfc_sli4_mbx_sge_get(mbox, sgentry, &sge);
1806 		phyaddr = getPaddr(sge.pa_hi, sge.pa_lo);
1807 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
1808 				  mbox->sge_array->addr[sgentry], phyaddr);
1809 	}
1810 	/* Reinitialize the context pointers to avoid stale usage. */
1811 	mbox->ctx_buf = NULL;
1812 	memset(&mbox->ctx_u, 0, sizeof(mbox->ctx_u));
1813 	kfree(mbox->sge_array);
1814 	/* Finally, free the mailbox command itself */
1815 	mempool_free(mbox, phba->mbox_mem_pool);
1816 }
1817 
1818 /**
1819  * lpfc_sli4_config - Initialize the  SLI4 Config Mailbox command
1820  * @phba: pointer to lpfc hba data structure.
1821  * @mbox: pointer to lpfc mbox command.
1822  * @subsystem: The sli4 config sub mailbox subsystem.
1823  * @opcode: The sli4 config sub mailbox command opcode.
1824  * @length: Length of the sli4 config mailbox command (including sub-header).
1825  * @emb: True if embedded mbox command should be setup.
1826  *
1827  * This routine sets up the header fields of SLI4 specific mailbox command
1828  * for sending IOCTL command.
1829  *
1830  * Return: the actual length of the mbox command allocated (mostly useful
1831  *         for none embedded mailbox command).
1832  **/
1833 int
1834 lpfc_sli4_config(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1835 		 uint8_t subsystem, uint8_t opcode, uint32_t length, bool emb)
1836 {
1837 	struct lpfc_mbx_sli4_config *sli4_config;
1838 	union lpfc_sli4_cfg_shdr *cfg_shdr = NULL;
1839 	uint32_t alloc_len;
1840 	uint32_t resid_len;
1841 	uint32_t pagen, pcount;
1842 	void *viraddr;
1843 	dma_addr_t phyaddr;
1844 
1845 	/* Set up SLI4 mailbox command header fields */
1846 	memset(mbox, 0, sizeof(*mbox));
1847 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_SLI4_CONFIG);
1848 
1849 	/* Set up SLI4 ioctl command header fields */
1850 	sli4_config = &mbox->u.mqe.un.sli4_config;
1851 
1852 	/* Setup for the embedded mbox command */
1853 	if (emb) {
1854 		/* Set up main header fields */
1855 		bf_set(lpfc_mbox_hdr_emb, &sli4_config->header.cfg_mhdr, 1);
1856 		sli4_config->header.cfg_mhdr.payload_length = length;
1857 		/* Set up sub-header fields following main header */
1858 		bf_set(lpfc_mbox_hdr_opcode,
1859 			&sli4_config->header.cfg_shdr.request, opcode);
1860 		bf_set(lpfc_mbox_hdr_subsystem,
1861 			&sli4_config->header.cfg_shdr.request, subsystem);
1862 		sli4_config->header.cfg_shdr.request.request_length =
1863 			length - LPFC_MBX_CMD_HDR_LENGTH;
1864 		return length;
1865 	}
1866 
1867 	/* Setup for the non-embedded mbox command */
1868 	pcount = (SLI4_PAGE_ALIGN(length))/SLI4_PAGE_SIZE;
1869 	pcount = (pcount > LPFC_SLI4_MBX_SGE_MAX_PAGES) ?
1870 				LPFC_SLI4_MBX_SGE_MAX_PAGES : pcount;
1871 	/* Allocate record for keeping SGE virtual addresses */
1872 	mbox->sge_array = kzalloc(sizeof(struct lpfc_mbx_nembed_sge_virt),
1873 				  GFP_KERNEL);
1874 	if (!mbox->sge_array) {
1875 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1876 				"2527 Failed to allocate non-embedded SGE "
1877 				"array.\n");
1878 		return 0;
1879 	}
1880 	for (pagen = 0, alloc_len = 0; pagen < pcount; pagen++) {
1881 		/* The DMA memory is always allocated in the length of a
1882 		 * page even though the last SGE might not fill up to a
1883 		 * page, this is used as a priori size of SLI4_PAGE_SIZE for
1884 		 * the later DMA memory free.
1885 		 */
1886 		viraddr = dma_alloc_coherent(&phba->pcidev->dev,
1887 					     SLI4_PAGE_SIZE, &phyaddr,
1888 					     GFP_KERNEL);
1889 		/* In case of malloc fails, proceed with whatever we have */
1890 		if (!viraddr)
1891 			break;
1892 		mbox->sge_array->addr[pagen] = viraddr;
1893 		/* Keep the first page for later sub-header construction */
1894 		if (pagen == 0)
1895 			cfg_shdr = (union lpfc_sli4_cfg_shdr *)viraddr;
1896 		resid_len = length - alloc_len;
1897 		if (resid_len > SLI4_PAGE_SIZE) {
1898 			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1899 					      SLI4_PAGE_SIZE);
1900 			alloc_len += SLI4_PAGE_SIZE;
1901 		} else {
1902 			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1903 					      resid_len);
1904 			alloc_len = length;
1905 		}
1906 	}
1907 
1908 	/* Set up main header fields in mailbox command */
1909 	sli4_config->header.cfg_mhdr.payload_length = alloc_len;
1910 	bf_set(lpfc_mbox_hdr_sge_cnt, &sli4_config->header.cfg_mhdr, pagen);
1911 
1912 	/* Set up sub-header fields into the first page */
1913 	if (pagen > 0) {
1914 		bf_set(lpfc_mbox_hdr_opcode, &cfg_shdr->request, opcode);
1915 		bf_set(lpfc_mbox_hdr_subsystem, &cfg_shdr->request, subsystem);
1916 		cfg_shdr->request.request_length =
1917 				alloc_len - sizeof(union  lpfc_sli4_cfg_shdr);
1918 	}
1919 	/* The sub-header is in DMA memory, which needs endian converstion */
1920 	if (cfg_shdr)
1921 		lpfc_sli_pcimem_bcopy(cfg_shdr, cfg_shdr,
1922 				      sizeof(union  lpfc_sli4_cfg_shdr));
1923 	return alloc_len;
1924 }
1925 
1926 /**
1927  * lpfc_sli4_mbox_rsrc_extent - Initialize the opcode resource extent.
1928  * @phba: pointer to lpfc hba data structure.
1929  * @mbox: pointer to an allocated lpfc mbox resource.
1930  * @exts_count: the number of extents, if required, to allocate.
1931  * @rsrc_type: the resource extent type.
1932  * @emb: true if LPFC_SLI4_MBX_EMBED. false if LPFC_SLI4_MBX_NEMBED.
1933  *
1934  * This routine completes the subcommand header for SLI4 resource extent
1935  * mailbox commands.  It is called after lpfc_sli4_config.  The caller must
1936  * pass an allocated mailbox and the attributes required to initialize the
1937  * mailbox correctly.
1938  *
1939  * Return: the actual length of the mbox command allocated.
1940  **/
1941 int
1942 lpfc_sli4_mbox_rsrc_extent(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1943 			   uint16_t exts_count, uint16_t rsrc_type, bool emb)
1944 {
1945 	uint8_t opcode = 0;
1946 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc_extnt = NULL;
1947 	void *virtaddr = NULL;
1948 
1949 	/* Set up SLI4 ioctl command header fields */
1950 	if (emb == LPFC_SLI4_MBX_NEMBED) {
1951 		/* Get the first SGE entry from the non-embedded DMA memory */
1952 		virtaddr = mbox->sge_array->addr[0];
1953 		if (virtaddr == NULL)
1954 			return 1;
1955 		n_rsrc_extnt = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
1956 	}
1957 
1958 	/*
1959 	 * The resource type is common to all extent Opcodes and resides in the
1960 	 * same position.
1961 	 */
1962 	if (emb == LPFC_SLI4_MBX_EMBED)
1963 		bf_set(lpfc_mbx_alloc_rsrc_extents_type,
1964 		       &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
1965 		       rsrc_type);
1966 	else {
1967 		/* This is DMA data.  Byteswap is required. */
1968 		bf_set(lpfc_mbx_alloc_rsrc_extents_type,
1969 		       n_rsrc_extnt, rsrc_type);
1970 		lpfc_sli_pcimem_bcopy(&n_rsrc_extnt->word4,
1971 				      &n_rsrc_extnt->word4,
1972 				      sizeof(uint32_t));
1973 	}
1974 
1975 	/* Complete the initialization for the particular Opcode. */
1976 	opcode = lpfc_sli_config_mbox_opcode_get(phba, mbox);
1977 	switch (opcode) {
1978 	case LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT:
1979 		if (emb == LPFC_SLI4_MBX_EMBED)
1980 			bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
1981 			       &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
1982 			       exts_count);
1983 		else
1984 			bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
1985 			       n_rsrc_extnt, exts_count);
1986 		break;
1987 	case LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT:
1988 	case LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO:
1989 	case LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT:
1990 		/* Initialization is complete.*/
1991 		break;
1992 	default:
1993 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1994 				"2929 Resource Extent Opcode x%x is "
1995 				"unsupported\n", opcode);
1996 		return 1;
1997 	}
1998 
1999 	return 0;
2000 }
2001 
2002 /**
2003  * lpfc_sli_config_mbox_subsys_get - Get subsystem from a sli_config mbox cmd
2004  * @phba: pointer to lpfc hba data structure.
2005  * @mbox: pointer to lpfc mbox command queue entry.
2006  *
2007  * This routine gets the subsystem from a SLI4 specific SLI_CONFIG mailbox
2008  * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if the
2009  * sub-header is not present, subsystem LPFC_MBOX_SUBSYSTEM_NA (0x0) shall
2010  * be returned.
2011  **/
2012 uint8_t
2013 lpfc_sli_config_mbox_subsys_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2014 {
2015 	struct lpfc_mbx_sli4_config *sli4_cfg;
2016 	union lpfc_sli4_cfg_shdr *cfg_shdr;
2017 
2018 	if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
2019 		return LPFC_MBOX_SUBSYSTEM_NA;
2020 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
2021 
2022 	/* For embedded mbox command, get opcode from embedded sub-header*/
2023 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
2024 		cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
2025 		return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
2026 	}
2027 
2028 	/* For non-embedded mbox command, get opcode from first dma page */
2029 	if (unlikely(!mbox->sge_array))
2030 		return LPFC_MBOX_SUBSYSTEM_NA;
2031 	cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
2032 	return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
2033 }
2034 
2035 /**
2036  * lpfc_sli_config_mbox_opcode_get - Get opcode from a sli_config mbox cmd
2037  * @phba: pointer to lpfc hba data structure.
2038  * @mbox: pointer to lpfc mbox command queue entry.
2039  *
2040  * This routine gets the opcode from a SLI4 specific SLI_CONFIG mailbox
2041  * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if
2042  * the sub-header is not present, opcode LPFC_MBOX_OPCODE_NA (0x0) be
2043  * returned.
2044  **/
2045 uint8_t
2046 lpfc_sli_config_mbox_opcode_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2047 {
2048 	struct lpfc_mbx_sli4_config *sli4_cfg;
2049 	union lpfc_sli4_cfg_shdr *cfg_shdr;
2050 
2051 	if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
2052 		return LPFC_MBOX_OPCODE_NA;
2053 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
2054 
2055 	/* For embedded mbox command, get opcode from embedded sub-header*/
2056 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
2057 		cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
2058 		return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
2059 	}
2060 
2061 	/* For non-embedded mbox command, get opcode from first dma page */
2062 	if (unlikely(!mbox->sge_array))
2063 		return LPFC_MBOX_OPCODE_NA;
2064 	cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
2065 	return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
2066 }
2067 
2068 /**
2069  * lpfc_sli4_mbx_read_fcf_rec - Allocate and construct read fcf mbox cmd
2070  * @phba: pointer to lpfc hba data structure.
2071  * @mboxq: pointer to lpfc mbox command.
2072  * @fcf_index: index to fcf table.
2073  *
2074  * This routine routine allocates and constructs non-embedded mailbox command
2075  * for reading a FCF table entry referred by @fcf_index.
2076  *
2077  * Return: pointer to the mailbox command constructed if successful, otherwise
2078  * NULL.
2079  **/
2080 int
2081 lpfc_sli4_mbx_read_fcf_rec(struct lpfc_hba *phba,
2082 			   struct lpfcMboxq *mboxq,
2083 			   uint16_t fcf_index)
2084 {
2085 	void *virt_addr;
2086 	uint8_t *bytep;
2087 	struct lpfc_mbx_sge sge;
2088 	uint32_t alloc_len, req_len;
2089 	struct lpfc_mbx_read_fcf_tbl *read_fcf;
2090 
2091 	if (!mboxq)
2092 		return -ENOMEM;
2093 
2094 	req_len = sizeof(struct fcf_record) +
2095 		  sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
2096 
2097 	/* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
2098 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2099 			LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
2100 			LPFC_SLI4_MBX_NEMBED);
2101 
2102 	if (alloc_len < req_len) {
2103 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2104 				"0291 Allocated DMA memory size (x%x) is "
2105 				"less than the requested DMA memory "
2106 				"size (x%x)\n", alloc_len, req_len);
2107 		return -ENOMEM;
2108 	}
2109 
2110 	/* Get the first SGE entry from the non-embedded DMA memory. This
2111 	 * routine only uses a single SGE.
2112 	 */
2113 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
2114 	virt_addr = mboxq->sge_array->addr[0];
2115 	read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
2116 
2117 	/* Set up command fields */
2118 	bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
2119 	/* Perform necessary endian conversion */
2120 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
2121 	lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
2122 
2123 	return 0;
2124 }
2125 
2126 /**
2127  * lpfc_request_features: Configure SLI4 REQUEST_FEATURES mailbox
2128  * @phba: pointer to lpfc hba data structure.
2129  * @mboxq: pointer to lpfc mbox command.
2130  *
2131  * This routine sets up the mailbox for an SLI4 REQUEST_FEATURES
2132  * mailbox command.
2133  **/
2134 void
2135 lpfc_request_features(struct lpfc_hba *phba, struct lpfcMboxq *mboxq)
2136 {
2137 	/* Set up SLI4 mailbox command header fields */
2138 	memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
2139 	bf_set(lpfc_mqe_command, &mboxq->u.mqe, MBX_SLI4_REQ_FTRS);
2140 
2141 	/* Set up host requested features. */
2142 	bf_set(lpfc_mbx_rq_ftr_rq_fcpi, &mboxq->u.mqe.un.req_ftrs, 1);
2143 	bf_set(lpfc_mbx_rq_ftr_rq_perfh, &mboxq->u.mqe.un.req_ftrs, 1);
2144 
2145 	/* Enable DIF (block guard) only if configured to do so. */
2146 	if (phba->cfg_enable_bg)
2147 		bf_set(lpfc_mbx_rq_ftr_rq_dif, &mboxq->u.mqe.un.req_ftrs, 1);
2148 
2149 	/* Enable NPIV only if configured to do so. */
2150 	if (phba->max_vpi && phba->cfg_enable_npiv)
2151 		bf_set(lpfc_mbx_rq_ftr_rq_npiv, &mboxq->u.mqe.un.req_ftrs, 1);
2152 
2153 	if (phba->nvmet_support) {
2154 		bf_set(lpfc_mbx_rq_ftr_rq_mrqp, &mboxq->u.mqe.un.req_ftrs, 1);
2155 		/* iaab/iaar NOT set for now */
2156 		bf_set(lpfc_mbx_rq_ftr_rq_iaab, &mboxq->u.mqe.un.req_ftrs, 0);
2157 		bf_set(lpfc_mbx_rq_ftr_rq_iaar, &mboxq->u.mqe.un.req_ftrs, 0);
2158 	}
2159 
2160 	/* Enable Application Services Header for appheader VMID */
2161 	if (phba->cfg_vmid_app_header) {
2162 		bf_set(lpfc_mbx_rq_ftr_rq_ashdr, &mboxq->u.mqe.un.req_ftrs, 1);
2163 		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 1);
2164 	}
2165 	return;
2166 }
2167 
2168 /**
2169  * lpfc_init_vfi - Initialize the INIT_VFI mailbox command
2170  * @mbox: pointer to lpfc mbox command to initialize.
2171  * @vport: Vport associated with the VF.
2172  *
2173  * This routine initializes @mbox to all zeros and then fills in the mailbox
2174  * fields from @vport. INIT_VFI configures virtual fabrics identified by VFI
2175  * in the context of an FCF. The driver issues this command to setup a VFI
2176  * before issuing a FLOGI to login to the VSAN. The driver should also issue a
2177  * REG_VFI after a successful VSAN login.
2178  **/
2179 void
2180 lpfc_init_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
2181 {
2182 	struct lpfc_mbx_init_vfi *init_vfi;
2183 
2184 	memset(mbox, 0, sizeof(*mbox));
2185 	mbox->vport = vport;
2186 	init_vfi = &mbox->u.mqe.un.init_vfi;
2187 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VFI);
2188 	bf_set(lpfc_init_vfi_vr, init_vfi, 1);
2189 	bf_set(lpfc_init_vfi_vt, init_vfi, 1);
2190 	bf_set(lpfc_init_vfi_vp, init_vfi, 1);
2191 	bf_set(lpfc_init_vfi_vfi, init_vfi,
2192 	       vport->phba->sli4_hba.vfi_ids[vport->vfi]);
2193 	bf_set(lpfc_init_vfi_vpi, init_vfi,
2194 	       vport->phba->vpi_ids[vport->vpi]);
2195 	bf_set(lpfc_init_vfi_fcfi, init_vfi,
2196 	       vport->phba->fcf.fcfi);
2197 }
2198 
2199 /**
2200  * lpfc_reg_vfi - Initialize the REG_VFI mailbox command
2201  * @mbox: pointer to lpfc mbox command to initialize.
2202  * @vport: vport associated with the VF.
2203  * @phys: BDE DMA bus address used to send the service parameters to the HBA.
2204  *
2205  * This routine initializes @mbox to all zeros and then fills in the mailbox
2206  * fields from @vport, and uses @buf as a DMAable buffer to send the vport's
2207  * fc service parameters to the HBA for this VFI. REG_VFI configures virtual
2208  * fabrics identified by VFI in the context of an FCF.
2209  **/
2210 void
2211 lpfc_reg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport, dma_addr_t phys)
2212 {
2213 	struct lpfc_mbx_reg_vfi *reg_vfi;
2214 	struct lpfc_hba *phba = vport->phba;
2215 	uint8_t bbscn_fabric = 0, bbscn_max = 0, bbscn_def = 0;
2216 
2217 	memset(mbox, 0, sizeof(*mbox));
2218 	reg_vfi = &mbox->u.mqe.un.reg_vfi;
2219 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_VFI);
2220 	bf_set(lpfc_reg_vfi_vp, reg_vfi, 1);
2221 	bf_set(lpfc_reg_vfi_vfi, reg_vfi,
2222 	       phba->sli4_hba.vfi_ids[vport->vfi]);
2223 	bf_set(lpfc_reg_vfi_fcfi, reg_vfi, phba->fcf.fcfi);
2224 	bf_set(lpfc_reg_vfi_vpi, reg_vfi, phba->vpi_ids[vport->vpi]);
2225 	memcpy(reg_vfi->wwn, &vport->fc_portname, sizeof(struct lpfc_name));
2226 	reg_vfi->wwn[0] = cpu_to_le32(reg_vfi->wwn[0]);
2227 	reg_vfi->wwn[1] = cpu_to_le32(reg_vfi->wwn[1]);
2228 	reg_vfi->e_d_tov = phba->fc_edtov;
2229 	reg_vfi->r_a_tov = phba->fc_ratov;
2230 	if (phys) {
2231 		reg_vfi->bde.addrHigh = putPaddrHigh(phys);
2232 		reg_vfi->bde.addrLow = putPaddrLow(phys);
2233 		reg_vfi->bde.tus.f.bdeSize = sizeof(vport->fc_sparam);
2234 		reg_vfi->bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2235 	}
2236 	bf_set(lpfc_reg_vfi_nport_id, reg_vfi, vport->fc_myDID);
2237 
2238 	/* Only FC supports upd bit */
2239 	if ((phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC) &&
2240 	    test_bit(FC_VFI_REGISTERED, &vport->fc_flag) &&
2241 	    (!phba->fc_topology_changed))
2242 		bf_set(lpfc_reg_vfi_upd, reg_vfi, 1);
2243 
2244 	bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 0);
2245 	bf_set(lpfc_reg_vfi_bbscn, reg_vfi, 0);
2246 	bbscn_fabric = (phba->fc_fabparam.cmn.bbRcvSizeMsb >> 4) & 0xF;
2247 
2248 	if (phba->bbcredit_support && phba->cfg_enable_bbcr  &&
2249 	    bbscn_fabric != 0) {
2250 		bbscn_max = bf_get(lpfc_bbscn_max,
2251 				   &phba->sli4_hba.bbscn_params);
2252 		if (bbscn_fabric <= bbscn_max) {
2253 			bbscn_def = bf_get(lpfc_bbscn_def,
2254 					   &phba->sli4_hba.bbscn_params);
2255 
2256 			if (bbscn_fabric > bbscn_def)
2257 				bf_set(lpfc_reg_vfi_bbscn, reg_vfi,
2258 				       bbscn_fabric);
2259 			else
2260 				bf_set(lpfc_reg_vfi_bbscn, reg_vfi, bbscn_def);
2261 
2262 			bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 1);
2263 		}
2264 	}
2265 	lpfc_printf_vlog(vport, KERN_INFO, LOG_MBOX,
2266 			"3134 Register VFI, mydid:x%x, fcfi:%d, "
2267 			"vfi:%d, vpi:%d, fc_pname:%x%x fc_flag:x%lx "
2268 			"port_state:x%x topology chg:%d bbscn_fabric :%d\n",
2269 			vport->fc_myDID,
2270 			phba->fcf.fcfi,
2271 			phba->sli4_hba.vfi_ids[vport->vfi],
2272 			phba->vpi_ids[vport->vpi],
2273 			reg_vfi->wwn[0], reg_vfi->wwn[1], vport->fc_flag,
2274 			vport->port_state, phba->fc_topology_changed,
2275 			bbscn_fabric);
2276 }
2277 
2278 /**
2279  * lpfc_init_vpi - Initialize the INIT_VPI mailbox command
2280  * @phba: pointer to the hba structure to init the VPI for.
2281  * @mbox: pointer to lpfc mbox command to initialize.
2282  * @vpi: VPI to be initialized.
2283  *
2284  * The INIT_VPI mailbox command supports virtual N_Ports. The driver uses the
2285  * command to activate a virtual N_Port. The HBA assigns a MAC address to use
2286  * with the virtual N Port.  The SLI Host issues this command before issuing a
2287  * FDISC to connect to the Fabric. The SLI Host should issue a REG_VPI after a
2288  * successful virtual NPort login.
2289  **/
2290 void
2291 lpfc_init_vpi(struct lpfc_hba *phba, struct lpfcMboxq *mbox, uint16_t vpi)
2292 {
2293 	memset(mbox, 0, sizeof(*mbox));
2294 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VPI);
2295 	bf_set(lpfc_init_vpi_vpi, &mbox->u.mqe.un.init_vpi,
2296 	       phba->vpi_ids[vpi]);
2297 	bf_set(lpfc_init_vpi_vfi, &mbox->u.mqe.un.init_vpi,
2298 	       phba->sli4_hba.vfi_ids[phba->pport->vfi]);
2299 }
2300 
2301 /**
2302  * lpfc_unreg_vfi - Initialize the UNREG_VFI mailbox command
2303  * @mbox: pointer to lpfc mbox command to initialize.
2304  * @vport: vport associated with the VF.
2305  *
2306  * The UNREG_VFI mailbox command causes the SLI Host to put a virtual fabric
2307  * (logical NPort) into the inactive state. The SLI Host must have logged out
2308  * and unregistered all remote N_Ports to abort any activity on the virtual
2309  * fabric. The SLI Port posts the mailbox response after marking the virtual
2310  * fabric inactive.
2311  **/
2312 void
2313 lpfc_unreg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
2314 {
2315 	memset(mbox, 0, sizeof(*mbox));
2316 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_VFI);
2317 	bf_set(lpfc_unreg_vfi_vfi, &mbox->u.mqe.un.unreg_vfi,
2318 	       vport->phba->sli4_hba.vfi_ids[vport->vfi]);
2319 }
2320 
2321 /**
2322  * lpfc_sli4_dump_cfg_rg23 - Dump sli4 port config region 23
2323  * @phba: pointer to the hba structure containing.
2324  * @mbox: pointer to lpfc mbox command to initialize.
2325  *
2326  * This function create a SLI4 dump mailbox command to dump configure
2327  * region 23.
2328  **/
2329 int
2330 lpfc_sli4_dump_cfg_rg23(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2331 {
2332 	struct lpfc_dmabuf *mp = NULL;
2333 	MAILBOX_t *mb;
2334 	int rc;
2335 
2336 	memset(mbox, 0, sizeof(*mbox));
2337 	mb = &mbox->u.mb;
2338 
2339 	rc = lpfc_mbox_rsrc_prep(phba, mbox);
2340 	if (rc) {
2341 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2342 				"2569 %s: memory allocation failed\n",
2343 				__func__);
2344 		return 1;
2345 	}
2346 
2347 	mb->mbxCommand = MBX_DUMP_MEMORY;
2348 	mb->un.varDmp.type = DMP_NV_PARAMS;
2349 	mb->un.varDmp.region_id = DMP_REGION_23;
2350 	mb->un.varDmp.sli4_length = DMP_RGN23_SIZE;
2351 	mp = mbox->ctx_buf;
2352 	mb->un.varWords[3] = putPaddrLow(mp->phys);
2353 	mb->un.varWords[4] = putPaddrHigh(mp->phys);
2354 	return 0;
2355 }
2356 
2357 static void
2358 lpfc_mbx_cmpl_rdp_link_stat(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2359 {
2360 	MAILBOX_t *mb;
2361 	int rc = FAILURE;
2362 	struct lpfc_rdp_context *rdp_context = mboxq->ctx_u.rdp;
2363 
2364 	mb = &mboxq->u.mb;
2365 	if (mb->mbxStatus)
2366 		goto mbx_failed;
2367 
2368 	memcpy(&rdp_context->link_stat, &mb->un.varRdLnk, sizeof(READ_LNK_VAR));
2369 
2370 	rc = SUCCESS;
2371 
2372 mbx_failed:
2373 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
2374 	rdp_context->cmpl(phba, rdp_context, rc);
2375 }
2376 
2377 static void
2378 lpfc_mbx_cmpl_rdp_page_a2(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2379 {
2380 	struct lpfc_dmabuf *mp = mbox->ctx_buf;
2381 	struct lpfc_rdp_context *rdp_context = mbox->ctx_u.rdp;
2382 
2383 	if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
2384 		goto error_mbox_free;
2385 
2386 	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a2,
2387 				DMP_SFF_PAGE_A2_SIZE);
2388 
2389 	lpfc_read_lnk_stat(phba, mbox);
2390 	mbox->vport = rdp_context->ndlp->vport;
2391 
2392 	/* Save the dma buffer for cleanup in the final completion. */
2393 	mbox->ctx_buf = mp;
2394 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_link_stat;
2395 	mbox->ctx_u.rdp = rdp_context;
2396 	if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) == MBX_NOT_FINISHED)
2397 		goto error_mbox_free;
2398 
2399 	return;
2400 
2401 error_mbox_free:
2402 	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
2403 	rdp_context->cmpl(phba, rdp_context, FAILURE);
2404 }
2405 
2406 void
2407 lpfc_mbx_cmpl_rdp_page_a0(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2408 {
2409 	int rc;
2410 	struct lpfc_dmabuf *mp = mbox->ctx_buf;
2411 	struct lpfc_rdp_context *rdp_context = mbox->ctx_u.rdp;
2412 
2413 	if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
2414 		goto error;
2415 
2416 	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a0,
2417 				DMP_SFF_PAGE_A0_SIZE);
2418 
2419 	memset(mbox, 0, sizeof(*mbox));
2420 
2421 	memset(mp->virt, 0, DMP_SFF_PAGE_A2_SIZE);
2422 	INIT_LIST_HEAD(&mp->list);
2423 
2424 	/* save address for completion */
2425 	mbox->ctx_buf = mp;
2426 	mbox->vport = rdp_context->ndlp->vport;
2427 
2428 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
2429 	bf_set(lpfc_mbx_memory_dump_type3_type,
2430 		&mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
2431 	bf_set(lpfc_mbx_memory_dump_type3_link,
2432 		&mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
2433 	bf_set(lpfc_mbx_memory_dump_type3_page_no,
2434 		&mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A2);
2435 	bf_set(lpfc_mbx_memory_dump_type3_length,
2436 		&mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A2_SIZE);
2437 	mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
2438 	mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
2439 
2440 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a2;
2441 	mbox->ctx_u.rdp = rdp_context;
2442 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2443 	if (rc == MBX_NOT_FINISHED)
2444 		goto error;
2445 
2446 	return;
2447 
2448 error:
2449 	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
2450 	rdp_context->cmpl(phba, rdp_context, FAILURE);
2451 }
2452 
2453 
2454 /*
2455  * lpfc_sli4_dump_page_a0 - Dump sli4 read SFP Diagnostic.
2456  * @phba: pointer to the hba structure containing.
2457  * @mbox: pointer to lpfc mbox command to initialize.
2458  *
2459  * This function create a SLI4 dump mailbox command to dump configure
2460  * type 3 page 0xA0.
2461  */
2462 int
2463 lpfc_sli4_dump_page_a0(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2464 {
2465 	int rc;
2466 	struct lpfc_dmabuf *mp = NULL;
2467 
2468 	memset(mbox, 0, sizeof(*mbox));
2469 
2470 	rc = lpfc_mbox_rsrc_prep(phba, mbox);
2471 	if (rc) {
2472 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2473 			"3569 dump type 3 page 0xA0 allocation failed\n");
2474 		return 1;
2475 	}
2476 
2477 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
2478 	bf_set(lpfc_mbx_memory_dump_type3_type,
2479 		&mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
2480 	bf_set(lpfc_mbx_memory_dump_type3_link,
2481 		&mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
2482 	bf_set(lpfc_mbx_memory_dump_type3_page_no,
2483 		&mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A0);
2484 	bf_set(lpfc_mbx_memory_dump_type3_length,
2485 		&mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A0_SIZE);
2486 
2487 	mp = mbox->ctx_buf;
2488 	mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
2489 	mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
2490 
2491 	return 0;
2492 }
2493 
2494 /**
2495  * lpfc_reg_fcfi - Initialize the REG_FCFI mailbox command
2496  * @phba: pointer to the hba structure containing the FCF index and RQ ID.
2497  * @mbox: pointer to lpfc mbox command to initialize.
2498  *
2499  * The REG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs). The
2500  * SLI Host uses the command to activate an FCF after it has acquired FCF
2501  * information via a READ_FCF mailbox command. This mailbox command also is used
2502  * to indicate where received unsolicited frames from this FCF will be sent. By
2503  * default this routine will set up the FCF to forward all unsolicited frames
2504  * to the RQ ID passed in the @phba. This can be overridden by the caller for
2505  * more complicated setups.
2506  **/
2507 void
2508 lpfc_reg_fcfi(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2509 {
2510 	struct lpfc_mbx_reg_fcfi *reg_fcfi;
2511 
2512 	memset(mbox, 0, sizeof(*mbox));
2513 	reg_fcfi = &mbox->u.mqe.un.reg_fcfi;
2514 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI);
2515 	if (phba->nvmet_support == 0) {
2516 		bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
2517 		       phba->sli4_hba.hdr_rq->queue_id);
2518 		/* Match everything - rq_id0 */
2519 		bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, 0);
2520 		bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0);
2521 		bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi, 0);
2522 		bf_set(lpfc_reg_fcfi_rctl_mask0, reg_fcfi, 0);
2523 
2524 		bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi, REG_FCF_INVALID_QID);
2525 
2526 		/* addr mode is bit wise inverted value of fcf addr_mode */
2527 		bf_set(lpfc_reg_fcfi_mam, reg_fcfi,
2528 		       (~phba->fcf.addr_mode) & 0x3);
2529 	} else {
2530 		/* This is ONLY for NVMET MRQ == 1 */
2531 		if (phba->cfg_nvmet_mrq != 1)
2532 			return;
2533 
2534 		bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
2535 		       phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
2536 		/* Match type FCP - rq_id0 */
2537 		bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, FC_TYPE_FCP);
2538 		bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0xff);
2539 		bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi,
2540 		       FC_RCTL_DD_UNSOL_CMD);
2541 
2542 		bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi,
2543 		       phba->sli4_hba.hdr_rq->queue_id);
2544 		/* Match everything else - rq_id1 */
2545 		bf_set(lpfc_reg_fcfi_type_match1, reg_fcfi, 0);
2546 		bf_set(lpfc_reg_fcfi_type_mask1, reg_fcfi, 0);
2547 		bf_set(lpfc_reg_fcfi_rctl_match1, reg_fcfi, 0);
2548 		bf_set(lpfc_reg_fcfi_rctl_mask1, reg_fcfi, 0);
2549 	}
2550 	bf_set(lpfc_reg_fcfi_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
2551 	bf_set(lpfc_reg_fcfi_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
2552 	bf_set(lpfc_reg_fcfi_info_index, reg_fcfi,
2553 	       phba->fcf.current_rec.fcf_indx);
2554 	if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
2555 		bf_set(lpfc_reg_fcfi_vv, reg_fcfi, 1);
2556 		bf_set(lpfc_reg_fcfi_vlan_tag, reg_fcfi,
2557 		       phba->fcf.current_rec.vlan_id);
2558 	}
2559 }
2560 
2561 /**
2562  * lpfc_reg_fcfi_mrq - Initialize the REG_FCFI_MRQ mailbox command
2563  * @phba: pointer to the hba structure containing the FCF index and RQ ID.
2564  * @mbox: pointer to lpfc mbox command to initialize.
2565  * @mode: 0 to register FCFI, 1 to register MRQs
2566  *
2567  * The REG_FCFI_MRQ mailbox command supports Fibre Channel Forwarders (FCFs).
2568  * The SLI Host uses the command to activate an FCF after it has acquired FCF
2569  * information via a READ_FCF mailbox command. This mailbox command also is used
2570  * to indicate where received unsolicited frames from this FCF will be sent. By
2571  * default this routine will set up the FCF to forward all unsolicited frames
2572  * to the RQ ID passed in the @phba. This can be overridden by the caller for
2573  * more complicated setups.
2574  **/
2575 void
2576 lpfc_reg_fcfi_mrq(struct lpfc_hba *phba, struct lpfcMboxq *mbox, int mode)
2577 {
2578 	struct lpfc_mbx_reg_fcfi_mrq *reg_fcfi;
2579 
2580 	/* This is ONLY for MRQ */
2581 	if (phba->cfg_nvmet_mrq <= 1)
2582 		return;
2583 
2584 	memset(mbox, 0, sizeof(*mbox));
2585 	reg_fcfi = &mbox->u.mqe.un.reg_fcfi_mrq;
2586 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI_MRQ);
2587 	if (mode == 0) {
2588 		bf_set(lpfc_reg_fcfi_mrq_info_index, reg_fcfi,
2589 		       phba->fcf.current_rec.fcf_indx);
2590 		if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
2591 			bf_set(lpfc_reg_fcfi_mrq_vv, reg_fcfi, 1);
2592 			bf_set(lpfc_reg_fcfi_mrq_vlan_tag, reg_fcfi,
2593 			       phba->fcf.current_rec.vlan_id);
2594 		}
2595 		return;
2596 	}
2597 
2598 	bf_set(lpfc_reg_fcfi_mrq_rq_id0, reg_fcfi,
2599 	       phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
2600 	/* Match NVME frames of type FCP (protocol NVME) - rq_id0 */
2601 	bf_set(lpfc_reg_fcfi_mrq_type_match0, reg_fcfi, FC_TYPE_FCP);
2602 	bf_set(lpfc_reg_fcfi_mrq_type_mask0, reg_fcfi, 0xff);
2603 	bf_set(lpfc_reg_fcfi_mrq_rctl_match0, reg_fcfi, FC_RCTL_DD_UNSOL_CMD);
2604 	bf_set(lpfc_reg_fcfi_mrq_rctl_mask0, reg_fcfi, 0xff);
2605 	bf_set(lpfc_reg_fcfi_mrq_ptc0, reg_fcfi, 1);
2606 	bf_set(lpfc_reg_fcfi_mrq_pt0, reg_fcfi, 1);
2607 
2608 	bf_set(lpfc_reg_fcfi_mrq_policy, reg_fcfi, 3); /* NVME connection id */
2609 	bf_set(lpfc_reg_fcfi_mrq_mode, reg_fcfi, 1);
2610 	bf_set(lpfc_reg_fcfi_mrq_filter, reg_fcfi, 1); /* rq_id0 */
2611 	bf_set(lpfc_reg_fcfi_mrq_npairs, reg_fcfi, phba->cfg_nvmet_mrq);
2612 
2613 	bf_set(lpfc_reg_fcfi_mrq_rq_id1, reg_fcfi,
2614 	       phba->sli4_hba.hdr_rq->queue_id);
2615 	/* Match everything - rq_id1 */
2616 	bf_set(lpfc_reg_fcfi_mrq_type_match1, reg_fcfi, 0);
2617 	bf_set(lpfc_reg_fcfi_mrq_type_mask1, reg_fcfi, 0);
2618 	bf_set(lpfc_reg_fcfi_mrq_rctl_match1, reg_fcfi, 0);
2619 	bf_set(lpfc_reg_fcfi_mrq_rctl_mask1, reg_fcfi, 0);
2620 
2621 	bf_set(lpfc_reg_fcfi_mrq_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
2622 	bf_set(lpfc_reg_fcfi_mrq_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
2623 }
2624 
2625 /**
2626  * lpfc_unreg_fcfi - Initialize the UNREG_FCFI mailbox command
2627  * @mbox: pointer to lpfc mbox command to initialize.
2628  * @fcfi: FCFI to be unregistered.
2629  *
2630  * The UNREG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs).
2631  * The SLI Host uses the command to inactivate an FCFI.
2632  **/
2633 void
2634 lpfc_unreg_fcfi(struct lpfcMboxq *mbox, uint16_t fcfi)
2635 {
2636 	memset(mbox, 0, sizeof(*mbox));
2637 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_FCFI);
2638 	bf_set(lpfc_unreg_fcfi, &mbox->u.mqe.un.unreg_fcfi, fcfi);
2639 }
2640 
2641 /**
2642  * lpfc_resume_rpi - Initialize the RESUME_RPI mailbox command
2643  * @mbox: pointer to lpfc mbox command to initialize.
2644  * @ndlp: The nodelist structure that describes the RPI to resume.
2645  *
2646  * The RESUME_RPI mailbox command is used to restart I/O to an RPI after a
2647  * link event.
2648  **/
2649 void
2650 lpfc_resume_rpi(struct lpfcMboxq *mbox, struct lpfc_nodelist *ndlp)
2651 {
2652 	struct lpfc_hba *phba = ndlp->phba;
2653 	struct lpfc_mbx_resume_rpi *resume_rpi;
2654 
2655 	memset(mbox, 0, sizeof(*mbox));
2656 	resume_rpi = &mbox->u.mqe.un.resume_rpi;
2657 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_RESUME_RPI);
2658 	bf_set(lpfc_resume_rpi_index, resume_rpi,
2659 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2660 	bf_set(lpfc_resume_rpi_ii, resume_rpi, RESUME_INDEX_RPI);
2661 	resume_rpi->event_tag = ndlp->phba->fc_eventTag;
2662 }
2663 
2664