xref: /linux/drivers/scsi/lpfc/lpfc_mbox.c (revision 2277ab4a1df50e05bc732fe9488d4e902bb8399a)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21 
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_transport_fc.h>
28 
29 #include <scsi/scsi.h>
30 
31 #include "lpfc_hw4.h"
32 #include "lpfc_hw.h"
33 #include "lpfc_sli.h"
34 #include "lpfc_sli4.h"
35 #include "lpfc_nl.h"
36 #include "lpfc_disc.h"
37 #include "lpfc_scsi.h"
38 #include "lpfc.h"
39 #include "lpfc_logmsg.h"
40 #include "lpfc_crtn.h"
41 #include "lpfc_compat.h"
42 
43 /**
44  * lpfc_dump_static_vport - Dump HBA's static vport information.
45  * @phba: pointer to lpfc hba data structure.
46  * @pmb: pointer to the driver internal queue element for mailbox command.
47  * @offset: offset for dumping vport info.
48  *
49  * The dump mailbox command provides a method for the device driver to obtain
50  * various types of information from the HBA device.
51  *
52  * This routine prepares the mailbox command for dumping list of static
53  * vports to be created.
54  **/
55 void
56 lpfc_dump_static_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
57 		uint16_t offset)
58 {
59 	MAILBOX_t *mb;
60 	void *ctx;
61 
62 	mb = &pmb->u.mb;
63 	ctx = pmb->context2;
64 
65 	/* Setup to dump vport info region */
66 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
67 	mb->mbxCommand = MBX_DUMP_MEMORY;
68 	mb->un.varDmp.cv = 1;
69 	mb->un.varDmp.type = DMP_NV_PARAMS;
70 	mb->un.varDmp.entry_index = offset;
71 	mb->un.varDmp.region_id = DMP_REGION_VPORT;
72 	mb->un.varDmp.word_cnt = DMP_RSP_SIZE/sizeof(uint32_t);
73 	mb->un.varDmp.co = 0;
74 	mb->un.varDmp.resp_offset = 0;
75 	pmb->context2 = ctx;
76 	mb->mbxOwner = OWN_HOST;
77 
78 	return;
79 }
80 
81 /**
82  * lpfc_dump_mem - Prepare a mailbox command for retrieving HBA's VPD memory
83  * @phba: pointer to lpfc hba data structure.
84  * @pmb: pointer to the driver internal queue element for mailbox command.
85  * @offset: offset for dumping VPD memory mailbox command.
86  *
87  * The dump mailbox command provides a method for the device driver to obtain
88  * various types of information from the HBA device.
89  *
90  * This routine prepares the mailbox command for dumping HBA Vital Product
91  * Data (VPD) memory. This mailbox command is to be used for retrieving a
92  * portion (DMP_RSP_SIZE bytes) of a HBA's VPD from the HBA at an address
93  * offset specified by the offset parameter.
94  **/
95 void
96 lpfc_dump_mem(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb, uint16_t offset)
97 {
98 	MAILBOX_t *mb;
99 	void *ctx;
100 
101 	mb = &pmb->u.mb;
102 	ctx = pmb->context2;
103 
104 	/* Setup to dump VPD region */
105 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
106 	mb->mbxCommand = MBX_DUMP_MEMORY;
107 	mb->un.varDmp.cv = 1;
108 	mb->un.varDmp.type = DMP_NV_PARAMS;
109 	mb->un.varDmp.entry_index = offset;
110 	mb->un.varDmp.region_id = DMP_REGION_VPD;
111 	mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
112 	mb->un.varDmp.co = 0;
113 	mb->un.varDmp.resp_offset = 0;
114 	pmb->context2 = ctx;
115 	mb->mbxOwner = OWN_HOST;
116 	return;
117 }
118 
119 /**
120  * lpfc_dump_wakeup_param - Prepare mailbox command for retrieving wakeup params
121  * @phba: pointer to lpfc hba data structure.
122  * @pmb: pointer to the driver internal queue element for mailbox command.
123  *
124  * This function create a dump memory mailbox command to dump wake up
125  * parameters.
126  */
127 void
128 lpfc_dump_wakeup_param(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
129 {
130 	MAILBOX_t *mb;
131 	void *ctx;
132 
133 	mb = &pmb->u.mb;
134 	/* Save context so that we can restore after memset */
135 	ctx = pmb->context2;
136 
137 	/* Setup to dump VPD region */
138 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
139 	mb->mbxCommand = MBX_DUMP_MEMORY;
140 	mb->mbxOwner = OWN_HOST;
141 	mb->un.varDmp.cv = 1;
142 	mb->un.varDmp.type = DMP_NV_PARAMS;
143 	mb->un.varDmp.entry_index = 0;
144 	mb->un.varDmp.region_id = WAKE_UP_PARMS_REGION_ID;
145 	mb->un.varDmp.word_cnt = WAKE_UP_PARMS_WORD_SIZE;
146 	mb->un.varDmp.co = 0;
147 	mb->un.varDmp.resp_offset = 0;
148 	pmb->context2 = ctx;
149 	return;
150 }
151 
152 /**
153  * lpfc_read_nv - Prepare a mailbox command for reading HBA's NVRAM param
154  * @phba: pointer to lpfc hba data structure.
155  * @pmb: pointer to the driver internal queue element for mailbox command.
156  *
157  * The read NVRAM mailbox command returns the HBA's non-volatile parameters
158  * that are used as defaults when the Fibre Channel link is brought on-line.
159  *
160  * This routine prepares the mailbox command for reading information stored
161  * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
162  **/
163 void
164 lpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
165 {
166 	MAILBOX_t *mb;
167 
168 	mb = &pmb->u.mb;
169 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
170 	mb->mbxCommand = MBX_READ_NV;
171 	mb->mbxOwner = OWN_HOST;
172 	return;
173 }
174 
175 /**
176  * lpfc_config_async - Prepare a mailbox command for enabling HBA async event
177  * @phba: pointer to lpfc hba data structure.
178  * @pmb: pointer to the driver internal queue element for mailbox command.
179  * @ring: ring number for the asynchronous event to be configured.
180  *
181  * The asynchronous event enable mailbox command is used to enable the
182  * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
183  * specifies the default ring to which events are posted.
184  *
185  * This routine prepares the mailbox command for enabling HBA asynchronous
186  * event support on a IOCB ring.
187  **/
188 void
189 lpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
190 		uint32_t ring)
191 {
192 	MAILBOX_t *mb;
193 
194 	mb = &pmb->u.mb;
195 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
196 	mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
197 	mb->un.varCfgAsyncEvent.ring = ring;
198 	mb->mbxOwner = OWN_HOST;
199 	return;
200 }
201 
202 /**
203  * lpfc_heart_beat - Prepare a mailbox command for heart beat
204  * @phba: pointer to lpfc hba data structure.
205  * @pmb: pointer to the driver internal queue element for mailbox command.
206  *
207  * The heart beat mailbox command is used to detect an unresponsive HBA, which
208  * is defined as any device where no error attention is sent and both mailbox
209  * and rings are not processed.
210  *
211  * This routine prepares the mailbox command for issuing a heart beat in the
212  * form of mailbox command to the HBA. The timely completion of the heart
213  * beat mailbox command indicates the health of the HBA.
214  **/
215 void
216 lpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
217 {
218 	MAILBOX_t *mb;
219 
220 	mb = &pmb->u.mb;
221 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
222 	mb->mbxCommand = MBX_HEARTBEAT;
223 	mb->mbxOwner = OWN_HOST;
224 	return;
225 }
226 
227 /**
228  * lpfc_read_la - Prepare a mailbox command for reading HBA link attention
229  * @phba: pointer to lpfc hba data structure.
230  * @pmb: pointer to the driver internal queue element for mailbox command.
231  * @mp: DMA buffer memory for reading the link attention information into.
232  *
233  * The read link attention mailbox command is issued to read the Link Event
234  * Attention information indicated by the HBA port when the Link Event bit
235  * of the Host Attention (HSTATT) register is set to 1. A Link Event
236  * Attention occurs based on an exception detected at the Fibre Channel link
237  * interface.
238  *
239  * This routine prepares the mailbox command for reading HBA link attention
240  * information. A DMA memory has been set aside and address passed to the
241  * HBA through @mp for the HBA to DMA link attention information into the
242  * memory as part of the execution of the mailbox command.
243  *
244  * Return codes
245  *    0 - Success (currently always return 0)
246  **/
247 int
248 lpfc_read_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb, struct lpfc_dmabuf *mp)
249 {
250 	MAILBOX_t *mb;
251 	struct lpfc_sli *psli;
252 
253 	psli = &phba->sli;
254 	mb = &pmb->u.mb;
255 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
256 
257 	INIT_LIST_HEAD(&mp->list);
258 	mb->mbxCommand = MBX_READ_LA64;
259 	mb->un.varReadLA.un.lilpBde64.tus.f.bdeSize = 128;
260 	mb->un.varReadLA.un.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
261 	mb->un.varReadLA.un.lilpBde64.addrLow = putPaddrLow(mp->phys);
262 
263 	/* Save address for later completion and set the owner to host so that
264 	 * the FW knows this mailbox is available for processing.
265 	 */
266 	pmb->context1 = (uint8_t *) mp;
267 	mb->mbxOwner = OWN_HOST;
268 	return (0);
269 }
270 
271 /**
272  * lpfc_clear_la - Prepare a mailbox command for clearing HBA link attention
273  * @phba: pointer to lpfc hba data structure.
274  * @pmb: pointer to the driver internal queue element for mailbox command.
275  *
276  * The clear link attention mailbox command is issued to clear the link event
277  * attention condition indicated by the Link Event bit of the Host Attention
278  * (HSTATT) register. The link event attention condition is cleared only if
279  * the event tag specified matches that of the current link event counter.
280  * The current event tag is read using the read link attention event mailbox
281  * command.
282  *
283  * This routine prepares the mailbox command for clearing HBA link attention
284  * information.
285  **/
286 void
287 lpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
288 {
289 	MAILBOX_t *mb;
290 
291 	mb = &pmb->u.mb;
292 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
293 
294 	mb->un.varClearLA.eventTag = phba->fc_eventTag;
295 	mb->mbxCommand = MBX_CLEAR_LA;
296 	mb->mbxOwner = OWN_HOST;
297 	return;
298 }
299 
300 /**
301  * lpfc_config_link - Prepare a mailbox command for configuring link on a HBA
302  * @phba: pointer to lpfc hba data structure.
303  * @pmb: pointer to the driver internal queue element for mailbox command.
304  *
305  * The configure link mailbox command is used before the initialize link
306  * mailbox command to override default value and to configure link-oriented
307  * parameters such as DID address and various timers. Typically, this
308  * command would be used after an F_Port login to set the returned DID address
309  * and the fabric timeout values. This command is not valid before a configure
310  * port command has configured the HBA port.
311  *
312  * This routine prepares the mailbox command for configuring link on a HBA.
313  **/
314 void
315 lpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
316 {
317 	struct lpfc_vport  *vport = phba->pport;
318 	MAILBOX_t *mb = &pmb->u.mb;
319 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
320 
321 	/* NEW_FEATURE
322 	 * SLI-2, Coalescing Response Feature.
323 	 */
324 	if (phba->cfg_cr_delay) {
325 		mb->un.varCfgLnk.cr = 1;
326 		mb->un.varCfgLnk.ci = 1;
327 		mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
328 		mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
329 	}
330 
331 	mb->un.varCfgLnk.myId = vport->fc_myDID;
332 	mb->un.varCfgLnk.edtov = phba->fc_edtov;
333 	mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
334 	mb->un.varCfgLnk.ratov = phba->fc_ratov;
335 	mb->un.varCfgLnk.rttov = phba->fc_rttov;
336 	mb->un.varCfgLnk.altov = phba->fc_altov;
337 	mb->un.varCfgLnk.crtov = phba->fc_crtov;
338 	mb->un.varCfgLnk.citov = phba->fc_citov;
339 
340 	if (phba->cfg_ack0)
341 		mb->un.varCfgLnk.ack0_enable = 1;
342 
343 	mb->mbxCommand = MBX_CONFIG_LINK;
344 	mb->mbxOwner = OWN_HOST;
345 	return;
346 }
347 
348 /**
349  * lpfc_config_msi - Prepare a mailbox command for configuring msi-x
350  * @phba: pointer to lpfc hba data structure.
351  * @pmb: pointer to the driver internal queue element for mailbox command.
352  *
353  * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
354  * MSI-X multi-message interrupt vector association to interrupt attention
355  * conditions.
356  *
357  * Return codes
358  *    0 - Success
359  *    -EINVAL - Failure
360  **/
361 int
362 lpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
363 {
364 	MAILBOX_t *mb = &pmb->u.mb;
365 	uint32_t attentionConditions[2];
366 
367 	/* Sanity check */
368 	if (phba->cfg_use_msi != 2) {
369 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
370 				"0475 Not configured for supporting MSI-X "
371 				"cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
372 		return -EINVAL;
373 	}
374 
375 	if (phba->sli_rev < 3) {
376 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
377 				"0476 HBA not supporting SLI-3 or later "
378 				"SLI Revision: 0x%x\n", phba->sli_rev);
379 		return -EINVAL;
380 	}
381 
382 	/* Clear mailbox command fields */
383 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
384 
385 	/*
386 	 * SLI-3, Message Signaled Interrupt Fearure.
387 	 */
388 
389 	/* Multi-message attention configuration */
390 	attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
391 				  HA_LATT | HA_MBATT);
392 	attentionConditions[1] = 0;
393 
394 	mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
395 	mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
396 
397 	/*
398 	 * Set up message number to HA bit association
399 	 */
400 #ifdef __BIG_ENDIAN_BITFIELD
401 	/* RA0 (FCP Ring) */
402 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
403 	/* RA1 (Other Protocol Extra Ring) */
404 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
405 #else   /*  __LITTLE_ENDIAN_BITFIELD */
406 	/* RA0 (FCP Ring) */
407 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
408 	/* RA1 (Other Protocol Extra Ring) */
409 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
410 #endif
411 	/* Multi-message interrupt autoclear configuration*/
412 	mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
413 	mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
414 
415 	/* For now, HBA autoclear does not work reliably, disable it */
416 	mb->un.varCfgMSI.autoClearHA[0] = 0;
417 	mb->un.varCfgMSI.autoClearHA[1] = 0;
418 
419 	/* Set command and owner bit */
420 	mb->mbxCommand = MBX_CONFIG_MSI;
421 	mb->mbxOwner = OWN_HOST;
422 
423 	return 0;
424 }
425 
426 /**
427  * lpfc_init_link - Prepare a mailbox command for initialize link on a HBA
428  * @phba: pointer to lpfc hba data structure.
429  * @pmb: pointer to the driver internal queue element for mailbox command.
430  * @topology: the link topology for the link to be initialized to.
431  * @linkspeed: the link speed for the link to be initialized to.
432  *
433  * The initialize link mailbox command is used to initialize the Fibre
434  * Channel link. This command must follow a configure port command that
435  * establishes the mode of operation.
436  *
437  * This routine prepares the mailbox command for initializing link on a HBA
438  * with the specified link topology and speed.
439  **/
440 void
441 lpfc_init_link(struct lpfc_hba * phba,
442 	       LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
443 {
444 	lpfc_vpd_t *vpd;
445 	struct lpfc_sli *psli;
446 	MAILBOX_t *mb;
447 
448 	mb = &pmb->u.mb;
449 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
450 
451 	psli = &phba->sli;
452 	switch (topology) {
453 	case FLAGS_TOPOLOGY_MODE_LOOP_PT:
454 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
455 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
456 		break;
457 	case FLAGS_TOPOLOGY_MODE_PT_PT:
458 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
459 		break;
460 	case FLAGS_TOPOLOGY_MODE_LOOP:
461 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
462 		break;
463 	case FLAGS_TOPOLOGY_MODE_PT_LOOP:
464 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
465 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
466 		break;
467 	case FLAGS_LOCAL_LB:
468 		mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
469 		break;
470 	}
471 
472 	/* Enable asynchronous ABTS responses from firmware */
473 	mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
474 
475 	/* NEW_FEATURE
476 	 * Setting up the link speed
477 	 */
478 	vpd = &phba->vpd;
479 	if (vpd->rev.feaLevelHigh >= 0x02){
480 		switch(linkspeed){
481 			case LINK_SPEED_1G:
482 			case LINK_SPEED_2G:
483 			case LINK_SPEED_4G:
484 			case LINK_SPEED_8G:
485 				mb->un.varInitLnk.link_flags |=
486 							FLAGS_LINK_SPEED;
487 				mb->un.varInitLnk.link_speed = linkspeed;
488 			break;
489 			case LINK_SPEED_AUTO:
490 			default:
491 				mb->un.varInitLnk.link_speed =
492 							LINK_SPEED_AUTO;
493 			break;
494 		}
495 
496 	}
497 	else
498 		mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
499 
500 	mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
501 	mb->mbxOwner = OWN_HOST;
502 	mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
503 	return;
504 }
505 
506 /**
507  * lpfc_read_sparam - Prepare a mailbox command for reading HBA parameters
508  * @phba: pointer to lpfc hba data structure.
509  * @pmb: pointer to the driver internal queue element for mailbox command.
510  * @vpi: virtual N_Port identifier.
511  *
512  * The read service parameter mailbox command is used to read the HBA port
513  * service parameters. The service parameters are read into the buffer
514  * specified directly by a BDE in the mailbox command. These service
515  * parameters may then be used to build the payload of an N_Port/F_POrt
516  * login request and reply (LOGI/ACC).
517  *
518  * This routine prepares the mailbox command for reading HBA port service
519  * parameters. The DMA memory is allocated in this function and the addresses
520  * are populated into the mailbox command for the HBA to DMA the service
521  * parameters into.
522  *
523  * Return codes
524  *    0 - Success
525  *    1 - DMA memory allocation failed
526  **/
527 int
528 lpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
529 {
530 	struct lpfc_dmabuf *mp;
531 	MAILBOX_t *mb;
532 	struct lpfc_sli *psli;
533 
534 	psli = &phba->sli;
535 	mb = &pmb->u.mb;
536 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
537 
538 	mb->mbxOwner = OWN_HOST;
539 
540 	/* Get a buffer to hold the HBAs Service Parameters */
541 
542 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
543 	if (mp)
544 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
545 	if (!mp || !mp->virt) {
546 		kfree(mp);
547 		mb->mbxCommand = MBX_READ_SPARM64;
548 		/* READ_SPARAM: no buffers */
549 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
550 			        "0301 READ_SPARAM: no buffers\n");
551 		return (1);
552 	}
553 	INIT_LIST_HEAD(&mp->list);
554 	mb->mbxCommand = MBX_READ_SPARM64;
555 	mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
556 	mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
557 	mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
558 	mb->un.varRdSparm.vpi = vpi + phba->vpi_base;
559 
560 	/* save address for completion */
561 	pmb->context1 = mp;
562 
563 	return (0);
564 }
565 
566 /**
567  * lpfc_unreg_did - Prepare a mailbox command for unregistering DID
568  * @phba: pointer to lpfc hba data structure.
569  * @vpi: virtual N_Port identifier.
570  * @did: remote port identifier.
571  * @pmb: pointer to the driver internal queue element for mailbox command.
572  *
573  * The unregister DID mailbox command is used to unregister an N_Port/F_Port
574  * login for an unknown RPI by specifying the DID of a remote port. This
575  * command frees an RPI context in the HBA port. This has the effect of
576  * performing an implicit N_Port/F_Port logout.
577  *
578  * This routine prepares the mailbox command for unregistering a remote
579  * N_Port/F_Port (DID) login.
580  **/
581 void
582 lpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
583 	       LPFC_MBOXQ_t * pmb)
584 {
585 	MAILBOX_t *mb;
586 
587 	mb = &pmb->u.mb;
588 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
589 
590 	mb->un.varUnregDID.did = did;
591 	if (vpi != 0xffff)
592 		vpi += phba->vpi_base;
593 	mb->un.varUnregDID.vpi = vpi;
594 
595 	mb->mbxCommand = MBX_UNREG_D_ID;
596 	mb->mbxOwner = OWN_HOST;
597 	return;
598 }
599 
600 /**
601  * lpfc_read_config - Prepare a mailbox command for reading HBA configuration
602  * @phba: pointer to lpfc hba data structure.
603  * @pmb: pointer to the driver internal queue element for mailbox command.
604  *
605  * The read configuration mailbox command is used to read the HBA port
606  * configuration parameters. This mailbox command provides a method for
607  * seeing any parameters that may have changed via various configuration
608  * mailbox commands.
609  *
610  * This routine prepares the mailbox command for reading out HBA configuration
611  * parameters.
612  **/
613 void
614 lpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
615 {
616 	MAILBOX_t *mb;
617 
618 	mb = &pmb->u.mb;
619 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
620 
621 	mb->mbxCommand = MBX_READ_CONFIG;
622 	mb->mbxOwner = OWN_HOST;
623 	return;
624 }
625 
626 /**
627  * lpfc_read_lnk_stat - Prepare a mailbox command for reading HBA link stats
628  * @phba: pointer to lpfc hba data structure.
629  * @pmb: pointer to the driver internal queue element for mailbox command.
630  *
631  * The read link status mailbox command is used to read the link status from
632  * the HBA. Link status includes all link-related error counters. These
633  * counters are maintained by the HBA and originated in the link hardware
634  * unit. Note that all of these counters wrap.
635  *
636  * This routine prepares the mailbox command for reading out HBA link status.
637  **/
638 void
639 lpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
640 {
641 	MAILBOX_t *mb;
642 
643 	mb = &pmb->u.mb;
644 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
645 
646 	mb->mbxCommand = MBX_READ_LNK_STAT;
647 	mb->mbxOwner = OWN_HOST;
648 	return;
649 }
650 
651 /**
652  * lpfc_reg_rpi - Prepare a mailbox command for registering remote login
653  * @phba: pointer to lpfc hba data structure.
654  * @vpi: virtual N_Port identifier.
655  * @did: remote port identifier.
656  * @param: pointer to memory holding the server parameters.
657  * @pmb: pointer to the driver internal queue element for mailbox command.
658  * @flag: action flag to be passed back for the complete function.
659  *
660  * The registration login mailbox command is used to register an N_Port or
661  * F_Port login. This registration allows the HBA to cache the remote N_Port
662  * service parameters internally and thereby make the appropriate FC-2
663  * decisions. The remote port service parameters are handed off by the driver
664  * to the HBA using a descriptor entry that directly identifies a buffer in
665  * host memory. In exchange, the HBA returns an RPI identifier.
666  *
667  * This routine prepares the mailbox command for registering remote port login.
668  * The function allocates DMA buffer for passing the service parameters to the
669  * HBA with the mailbox command.
670  *
671  * Return codes
672  *    0 - Success
673  *    1 - DMA memory allocation failed
674  **/
675 int
676 lpfc_reg_rpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
677 	       uint8_t *param, LPFC_MBOXQ_t *pmb, uint32_t flag)
678 {
679 	MAILBOX_t *mb = &pmb->u.mb;
680 	uint8_t *sparam;
681 	struct lpfc_dmabuf *mp;
682 
683 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
684 
685 	mb->un.varRegLogin.rpi = 0;
686 	if (phba->sli_rev == LPFC_SLI_REV4) {
687 		mb->un.varRegLogin.rpi = lpfc_sli4_alloc_rpi(phba);
688 		if (mb->un.varRegLogin.rpi == LPFC_RPI_ALLOC_ERROR)
689 			return 1;
690 	}
691 
692 	mb->un.varRegLogin.vpi = vpi + phba->vpi_base;
693 	mb->un.varRegLogin.did = did;
694 	mb->un.varWords[30] = flag;	/* Set flag to issue action on cmpl */
695 
696 	mb->mbxOwner = OWN_HOST;
697 
698 	/* Get a buffer to hold NPorts Service Parameters */
699 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
700 	if (mp)
701 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
702 	if (!mp || !mp->virt) {
703 		kfree(mp);
704 		mb->mbxCommand = MBX_REG_LOGIN64;
705 		/* REG_LOGIN: no buffers */
706 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
707 				"0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
708 				"flag x%x\n", vpi, did, flag);
709 		return (1);
710 	}
711 	INIT_LIST_HEAD(&mp->list);
712 	sparam = mp->virt;
713 
714 	/* Copy param's into a new buffer */
715 	memcpy(sparam, param, sizeof (struct serv_parm));
716 
717 	/* save address for completion */
718 	pmb->context1 = (uint8_t *) mp;
719 
720 	mb->mbxCommand = MBX_REG_LOGIN64;
721 	mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
722 	mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
723 	mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
724 
725 	return (0);
726 }
727 
728 /**
729  * lpfc_unreg_login - Prepare a mailbox command for unregistering remote login
730  * @phba: pointer to lpfc hba data structure.
731  * @vpi: virtual N_Port identifier.
732  * @rpi: remote port identifier
733  * @pmb: pointer to the driver internal queue element for mailbox command.
734  *
735  * The unregistration login mailbox command is used to unregister an N_Port
736  * or F_Port login. This command frees an RPI context in the HBA. It has the
737  * effect of performing an implicit N_Port/F_Port logout.
738  *
739  * This routine prepares the mailbox command for unregistering remote port
740  * login.
741  **/
742 void
743 lpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
744 		 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->un.varUnregLogin.rpi = (uint16_t) rpi;
752 	mb->un.varUnregLogin.rsvd1 = 0;
753 	mb->un.varUnregLogin.vpi = vpi + phba->vpi_base;
754 
755 	mb->mbxCommand = MBX_UNREG_LOGIN;
756 	mb->mbxOwner = OWN_HOST;
757 
758 	return;
759 }
760 
761 /**
762  * lpfc_reg_vpi - Prepare a mailbox command for registering vport identifier
763  * @phba: pointer to lpfc hba data structure.
764  * @vpi: virtual N_Port identifier.
765  * @sid: Fibre Channel S_ID (N_Port_ID assigned to a virtual N_Port).
766  * @pmb: pointer to the driver internal queue element for mailbox command.
767  *
768  * The registration vport identifier mailbox command is used to activate a
769  * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
770  * N_Port_ID against the information in the selected virtual N_Port context
771  * block and marks it active to allow normal processing of IOCB commands and
772  * received unsolicited exchanges.
773  *
774  * This routine prepares the mailbox command for registering a virtual N_Port.
775  **/
776 void
777 lpfc_reg_vpi(struct lpfc_vport *vport, LPFC_MBOXQ_t *pmb)
778 {
779 	MAILBOX_t *mb = &pmb->u.mb;
780 
781 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
782 
783 	mb->un.varRegVpi.vpi = vport->vpi + vport->phba->vpi_base;
784 	mb->un.varRegVpi.sid = vport->fc_myDID;
785 	mb->un.varRegVpi.vfi = vport->vfi + vport->phba->vfi_base;
786 
787 	mb->mbxCommand = MBX_REG_VPI;
788 	mb->mbxOwner = OWN_HOST;
789 	return;
790 
791 }
792 
793 /**
794  * lpfc_unreg_vpi - Prepare a mailbox command for unregistering vport id
795  * @phba: pointer to lpfc hba data structure.
796  * @vpi: virtual N_Port identifier.
797  * @pmb: pointer to the driver internal queue element for mailbox command.
798  *
799  * The unregistration vport identifier mailbox command is used to inactivate
800  * a virtual N_Port. The driver must have logged out and unregistered all
801  * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
802  * unregisters any default RPIs associated with the specified vpi, aborting
803  * any active exchanges. The HBA will post the mailbox response after making
804  * the virtual N_Port inactive.
805  *
806  * This routine prepares the mailbox command for unregistering a virtual
807  * N_Port.
808  **/
809 void
810 lpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
811 {
812 	MAILBOX_t *mb = &pmb->u.mb;
813 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
814 
815 	mb->un.varUnregVpi.vpi = vpi + phba->vpi_base;
816 
817 	mb->mbxCommand = MBX_UNREG_VPI;
818 	mb->mbxOwner = OWN_HOST;
819 	return;
820 
821 }
822 
823 /**
824  * lpfc_config_pcb_setup - Set up IOCB rings in the Port Control Block (PCB)
825  * @phba: pointer to lpfc hba data structure.
826  *
827  * This routine sets up and initializes the IOCB rings in the Port Control
828  * Block (PCB).
829  **/
830 static void
831 lpfc_config_pcb_setup(struct lpfc_hba * phba)
832 {
833 	struct lpfc_sli *psli = &phba->sli;
834 	struct lpfc_sli_ring *pring;
835 	PCB_t *pcbp = phba->pcb;
836 	dma_addr_t pdma_addr;
837 	uint32_t offset;
838 	uint32_t iocbCnt = 0;
839 	int i;
840 
841 	pcbp->maxRing = (psli->num_rings - 1);
842 
843 	for (i = 0; i < psli->num_rings; i++) {
844 		pring = &psli->ring[i];
845 
846 		pring->sizeCiocb = phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE:
847 							SLI2_IOCB_CMD_SIZE;
848 		pring->sizeRiocb = phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE:
849 							SLI2_IOCB_RSP_SIZE;
850 		/* A ring MUST have both cmd and rsp entries defined to be
851 		   valid */
852 		if ((pring->numCiocb == 0) || (pring->numRiocb == 0)) {
853 			pcbp->rdsc[i].cmdEntries = 0;
854 			pcbp->rdsc[i].rspEntries = 0;
855 			pcbp->rdsc[i].cmdAddrHigh = 0;
856 			pcbp->rdsc[i].rspAddrHigh = 0;
857 			pcbp->rdsc[i].cmdAddrLow = 0;
858 			pcbp->rdsc[i].rspAddrLow = 0;
859 			pring->cmdringaddr = NULL;
860 			pring->rspringaddr = NULL;
861 			continue;
862 		}
863 		/* Command ring setup for ring */
864 		pring->cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
865 		pcbp->rdsc[i].cmdEntries = pring->numCiocb;
866 
867 		offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
868 			 (uint8_t *) phba->slim2p.virt;
869 		pdma_addr = phba->slim2p.phys + offset;
870 		pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
871 		pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
872 		iocbCnt += pring->numCiocb;
873 
874 		/* Response ring setup for ring */
875 		pring->rspringaddr = (void *) &phba->IOCBs[iocbCnt];
876 
877 		pcbp->rdsc[i].rspEntries = pring->numRiocb;
878 		offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
879 			 (uint8_t *)phba->slim2p.virt;
880 		pdma_addr = phba->slim2p.phys + offset;
881 		pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
882 		pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
883 		iocbCnt += pring->numRiocb;
884 	}
885 }
886 
887 /**
888  * lpfc_read_rev - Prepare a mailbox command for reading HBA revision
889  * @phba: pointer to lpfc hba data structure.
890  * @pmb: pointer to the driver internal queue element for mailbox command.
891  *
892  * The read revision mailbox command is used to read the revision levels of
893  * the HBA components. These components include hardware units, resident
894  * firmware, and available firmware. HBAs that supports SLI-3 mode of
895  * operation provide different response information depending on the version
896  * requested by the driver.
897  *
898  * This routine prepares the mailbox command for reading HBA revision
899  * information.
900  **/
901 void
902 lpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
903 {
904 	MAILBOX_t *mb = &pmb->u.mb;
905 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
906 	mb->un.varRdRev.cv = 1;
907 	mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
908 	mb->mbxCommand = MBX_READ_REV;
909 	mb->mbxOwner = OWN_HOST;
910 	return;
911 }
912 
913 /**
914  * lpfc_build_hbq_profile2 - Set up the HBQ Selection Profile 2
915  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
916  * @hbq_desc: pointer to the HBQ selection profile descriptor.
917  *
918  * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
919  * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
920  * the Sequence Length Test using the fields in the Selection Profile 2
921  * extension in words 20:31.
922  **/
923 static void
924 lpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
925 			struct lpfc_hbq_init  *hbq_desc)
926 {
927 	hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
928 	hbqmb->profiles.profile2.maxlen     = hbq_desc->maxlen;
929 	hbqmb->profiles.profile2.seqlenoff  = hbq_desc->seqlenoff;
930 }
931 
932 /**
933  * lpfc_build_hbq_profile3 - Set up the HBQ Selection Profile 3
934  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
935  * @hbq_desc: pointer to the HBQ selection profile descriptor.
936  *
937  * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
938  * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
939  * the Sequence Length Test and Byte Field Test using the fields in the
940  * Selection Profile 3 extension in words 20:31.
941  **/
942 static void
943 lpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
944 			struct lpfc_hbq_init  *hbq_desc)
945 {
946 	hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
947 	hbqmb->profiles.profile3.maxlen     = hbq_desc->maxlen;
948 	hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
949 	hbqmb->profiles.profile3.seqlenoff  = hbq_desc->seqlenoff;
950 	memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
951 	       sizeof(hbqmb->profiles.profile3.cmdmatch));
952 }
953 
954 /**
955  * lpfc_build_hbq_profile5 - Set up the HBQ Selection Profile 5
956  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
957  * @hbq_desc: pointer to the HBQ selection profile descriptor.
958  *
959  * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
960  * HBA tests the initial frame of an incoming sequence using the frame's
961  * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
962  * and Byte Field Test using the fields in the Selection Profile 5 extension
963  * words 20:31.
964  **/
965 static void
966 lpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
967 			struct lpfc_hbq_init  *hbq_desc)
968 {
969 	hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
970 	hbqmb->profiles.profile5.maxlen     = hbq_desc->maxlen;
971 	hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
972 	hbqmb->profiles.profile5.seqlenoff  = hbq_desc->seqlenoff;
973 	memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
974 	       sizeof(hbqmb->profiles.profile5.cmdmatch));
975 }
976 
977 /**
978  * lpfc_config_hbq - Prepare a mailbox command for configuring an HBQ
979  * @phba: pointer to lpfc hba data structure.
980  * @id: HBQ identifier.
981  * @hbq_desc: pointer to the HBA descriptor data structure.
982  * @hbq_entry_index: index of the HBQ entry data structures.
983  * @pmb: pointer to the driver internal queue element for mailbox command.
984  *
985  * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
986  * an HBQ. The configuration binds events that require buffers to a particular
987  * ring and HBQ based on a selection profile.
988  *
989  * This routine prepares the mailbox command for configuring an HBQ.
990  **/
991 void
992 lpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
993 		 struct lpfc_hbq_init *hbq_desc,
994 		uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
995 {
996 	int i;
997 	MAILBOX_t *mb = &pmb->u.mb;
998 	struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
999 
1000 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1001 	hbqmb->hbqId = id;
1002 	hbqmb->entry_count = hbq_desc->entry_count;   /* # entries in HBQ */
1003 	hbqmb->recvNotify = hbq_desc->rn;             /* Receive
1004 						       * Notification */
1005 	hbqmb->numMask    = hbq_desc->mask_count;     /* # R_CTL/TYPE masks
1006 						       * # in words 0-19 */
1007 	hbqmb->profile    = hbq_desc->profile;	      /* Selection profile:
1008 						       * 0 = all,
1009 						       * 7 = logentry */
1010 	hbqmb->ringMask   = hbq_desc->ring_mask;      /* Binds HBQ to a ring
1011 						       * e.g. Ring0=b0001,
1012 						       * ring2=b0100 */
1013 	hbqmb->headerLen  = hbq_desc->headerLen;      /* 0 if not profile 4
1014 						       * or 5 */
1015 	hbqmb->logEntry   = hbq_desc->logEntry;       /* Set to 1 if this
1016 						       * HBQ will be used
1017 						       * for LogEntry
1018 						       * buffers */
1019 	hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
1020 		hbq_entry_index * sizeof(struct lpfc_hbq_entry);
1021 	hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
1022 
1023 	mb->mbxCommand = MBX_CONFIG_HBQ;
1024 	mb->mbxOwner = OWN_HOST;
1025 
1026 				/* Copy info for profiles 2,3,5. Other
1027 				 * profiles this area is reserved
1028 				 */
1029 	if (hbq_desc->profile == 2)
1030 		lpfc_build_hbq_profile2(hbqmb, hbq_desc);
1031 	else if (hbq_desc->profile == 3)
1032 		lpfc_build_hbq_profile3(hbqmb, hbq_desc);
1033 	else if (hbq_desc->profile == 5)
1034 		lpfc_build_hbq_profile5(hbqmb, hbq_desc);
1035 
1036 	/* Return if no rctl / type masks for this HBQ */
1037 	if (!hbq_desc->mask_count)
1038 		return;
1039 
1040 	/* Otherwise we setup specific rctl / type masks for this HBQ */
1041 	for (i = 0; i < hbq_desc->mask_count; i++) {
1042 		hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
1043 		hbqmb->hbqMasks[i].tmask  = hbq_desc->hbqMasks[i].tmask;
1044 		hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
1045 		hbqmb->hbqMasks[i].rctlmask  = hbq_desc->hbqMasks[i].rctlmask;
1046 	}
1047 
1048 	return;
1049 }
1050 
1051 /**
1052  * lpfc_config_ring - Prepare a mailbox command for configuring an IOCB ring
1053  * @phba: pointer to lpfc hba data structure.
1054  * @ring:
1055  * @pmb: pointer to the driver internal queue element for mailbox command.
1056  *
1057  * The configure ring mailbox command is used to configure an IOCB ring. This
1058  * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
1059  * ring. This is used to map incoming sequences to a particular ring whose
1060  * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
1061  * attempt to configure a ring whose number is greater than the number
1062  * specified in the Port Control Block (PCB). It is an error to issue the
1063  * configure ring command more than once with the same ring number. The HBA
1064  * returns an error if the driver attempts this.
1065  *
1066  * This routine prepares the mailbox command for configuring IOCB ring.
1067  **/
1068 void
1069 lpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
1070 {
1071 	int i;
1072 	MAILBOX_t *mb = &pmb->u.mb;
1073 	struct lpfc_sli *psli;
1074 	struct lpfc_sli_ring *pring;
1075 
1076 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1077 
1078 	mb->un.varCfgRing.ring = ring;
1079 	mb->un.varCfgRing.maxOrigXchg = 0;
1080 	mb->un.varCfgRing.maxRespXchg = 0;
1081 	mb->un.varCfgRing.recvNotify = 1;
1082 
1083 	psli = &phba->sli;
1084 	pring = &psli->ring[ring];
1085 	mb->un.varCfgRing.numMask = pring->num_mask;
1086 	mb->mbxCommand = MBX_CONFIG_RING;
1087 	mb->mbxOwner = OWN_HOST;
1088 
1089 	/* Is this ring configured for a specific profile */
1090 	if (pring->prt[0].profile) {
1091 		mb->un.varCfgRing.profile = pring->prt[0].profile;
1092 		return;
1093 	}
1094 
1095 	/* Otherwise we setup specific rctl / type masks for this ring */
1096 	for (i = 0; i < pring->num_mask; i++) {
1097 		mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
1098 		if (mb->un.varCfgRing.rrRegs[i].rval != FC_ELS_REQ)
1099 			mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
1100 		else
1101 			mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
1102 		mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
1103 		mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
1104 	}
1105 
1106 	return;
1107 }
1108 
1109 /**
1110  * lpfc_config_port - Prepare a mailbox command for configuring port
1111  * @phba: pointer to lpfc hba data structure.
1112  * @pmb: pointer to the driver internal queue element for mailbox command.
1113  *
1114  * The configure port mailbox command is used to identify the Port Control
1115  * Block (PCB) in the driver memory. After this command is issued, the
1116  * driver must not access the mailbox in the HBA without first resetting
1117  * the HBA. The HBA may copy the PCB information to internal storage for
1118  * subsequent use; the driver can not change the PCB information unless it
1119  * resets the HBA.
1120  *
1121  * This routine prepares the mailbox command for configuring port.
1122  **/
1123 void
1124 lpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1125 {
1126 	MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
1127 	MAILBOX_t *mb = &pmb->u.mb;
1128 	dma_addr_t pdma_addr;
1129 	uint32_t bar_low, bar_high;
1130 	size_t offset;
1131 	struct lpfc_hgp hgp;
1132 	int i;
1133 	uint32_t pgp_offset;
1134 
1135 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1136 	mb->mbxCommand = MBX_CONFIG_PORT;
1137 	mb->mbxOwner = OWN_HOST;
1138 
1139 	mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
1140 
1141 	offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
1142 	pdma_addr = phba->slim2p.phys + offset;
1143 	mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
1144 	mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
1145 
1146 	/* Always Host Group Pointer is in SLIM */
1147 	mb->un.varCfgPort.hps = 1;
1148 
1149 	/* If HBA supports SLI=3 ask for it */
1150 
1151 	if (phba->sli_rev == LPFC_SLI_REV3 && phba->vpd.sli3Feat.cerbm) {
1152 		if (phba->cfg_enable_bg)
1153 			mb->un.varCfgPort.cbg = 1; /* configure BlockGuard */
1154 		mb->un.varCfgPort.cdss = 1; /* Configure Security */
1155 		mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
1156 		mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
1157 		mb->un.varCfgPort.cinb = 1; /* Interrupt Notification Block */
1158 		mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
1159 		if (phba->max_vpi && phba->cfg_enable_npiv &&
1160 		    phba->vpd.sli3Feat.cmv) {
1161 			mb->un.varCfgPort.max_vpi = LPFC_MAX_VPI;
1162 			mb->un.varCfgPort.cmv = 1;
1163 		} else
1164 			mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
1165 	} else
1166 		phba->sli_rev = LPFC_SLI_REV2;
1167 	mb->un.varCfgPort.sli_mode = phba->sli_rev;
1168 
1169 	/* Now setup pcb */
1170 	phba->pcb->type = TYPE_NATIVE_SLI2;
1171 	phba->pcb->feature = FEATURE_INITIAL_SLI2;
1172 
1173 	/* Setup Mailbox pointers */
1174 	phba->pcb->mailBoxSize = sizeof(MAILBOX_t);
1175 	offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
1176 	pdma_addr = phba->slim2p.phys + offset;
1177 	phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
1178 	phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
1179 
1180 	/*
1181 	 * Setup Host Group ring pointer.
1182 	 *
1183 	 * For efficiency reasons, the ring get/put pointers can be
1184 	 * placed in adapter memory (SLIM) rather than in host memory.
1185 	 * This allows firmware to avoid PCI reads/writes when updating
1186 	 * and checking pointers.
1187 	 *
1188 	 * The firmware recognizes the use of SLIM memory by comparing
1189 	 * the address of the get/put pointers structure with that of
1190 	 * the SLIM BAR (BAR0).
1191 	 *
1192 	 * Caution: be sure to use the PCI config space value of BAR0/BAR1
1193 	 * (the hardware's view of the base address), not the OS's
1194 	 * value of pci_resource_start() as the OS value may be a cookie
1195 	 * for ioremap/iomap.
1196 	 */
1197 
1198 
1199 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
1200 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
1201 
1202 	/*
1203 	 * Set up HGP - Port Memory
1204 	 *
1205 	 * The port expects the host get/put pointers to reside in memory
1206 	 * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
1207 	 * area of SLIM.  In SLI-2 mode, there's an additional 16 reserved
1208 	 * words (0x40 bytes).  This area is not reserved if HBQs are
1209 	 * configured in SLI-3.
1210 	 *
1211 	 * CR0Put    - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
1212 	 * RR0Get                      0xc4              0x84
1213 	 * CR1Put                      0xc8              0x88
1214 	 * RR1Get                      0xcc              0x8c
1215 	 * CR2Put                      0xd0              0x90
1216 	 * RR2Get                      0xd4              0x94
1217 	 * CR3Put                      0xd8              0x98
1218 	 * RR3Get                      0xdc              0x9c
1219 	 *
1220 	 * Reserved                    0xa0-0xbf
1221 	 *    If HBQs configured:
1222 	 *                         HBQ 0 Put ptr  0xc0
1223 	 *                         HBQ 1 Put ptr  0xc4
1224 	 *                         HBQ 2 Put ptr  0xc8
1225 	 *                         ......
1226 	 *                         HBQ(M-1)Put Pointer 0xc0+(M-1)*4
1227 	 *
1228 	 */
1229 
1230 	if (phba->sli_rev == 3) {
1231 		phba->host_gp = &mb_slim->us.s3.host[0];
1232 		phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
1233 	} else {
1234 		phba->host_gp = &mb_slim->us.s2.host[0];
1235 		phba->hbq_put = NULL;
1236 	}
1237 
1238 	/* mask off BAR0's flag bits 0 - 3 */
1239 	phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
1240 		(void __iomem *)phba->host_gp -
1241 		(void __iomem *)phba->MBslimaddr;
1242 	if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
1243 		phba->pcb->hgpAddrHigh = bar_high;
1244 	else
1245 		phba->pcb->hgpAddrHigh = 0;
1246 	/* write HGP data to SLIM at the required longword offset */
1247 	memset(&hgp, 0, sizeof(struct lpfc_hgp));
1248 
1249 	for (i=0; i < phba->sli.num_rings; i++) {
1250 		lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
1251 				    sizeof(*phba->host_gp));
1252 	}
1253 
1254 	/* Setup Port Group offset */
1255 	if (phba->sli_rev == 3)
1256 		pgp_offset = offsetof(struct lpfc_sli2_slim,
1257 				      mbx.us.s3_pgp.port);
1258 	else
1259 		pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
1260 	pdma_addr = phba->slim2p.phys + pgp_offset;
1261 	phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
1262 	phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
1263 
1264 	/* Use callback routine to setp rings in the pcb */
1265 	lpfc_config_pcb_setup(phba);
1266 
1267 	/* special handling for LC HBAs */
1268 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
1269 		uint32_t hbainit[5];
1270 
1271 		lpfc_hba_init(phba, hbainit);
1272 
1273 		memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
1274 	}
1275 
1276 	/* Swap PCB if needed */
1277 	lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
1278 }
1279 
1280 /**
1281  * lpfc_kill_board - Prepare a mailbox command for killing board
1282  * @phba: pointer to lpfc hba data structure.
1283  * @pmb: pointer to the driver internal queue element for mailbox command.
1284  *
1285  * The kill board mailbox command is used to tell firmware to perform a
1286  * graceful shutdown of a channel on a specified board to prepare for reset.
1287  * When the kill board mailbox command is received, the ER3 bit is set to 1
1288  * in the Host Status register and the ER Attention bit is set to 1 in the
1289  * Host Attention register of the HBA function that received the kill board
1290  * command.
1291  *
1292  * This routine prepares the mailbox command for killing the board in
1293  * preparation for a graceful shutdown.
1294  **/
1295 void
1296 lpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1297 {
1298 	MAILBOX_t *mb = &pmb->u.mb;
1299 
1300 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1301 	mb->mbxCommand = MBX_KILL_BOARD;
1302 	mb->mbxOwner = OWN_HOST;
1303 	return;
1304 }
1305 
1306 /**
1307  * lpfc_mbox_put - Put a mailbox cmd into the tail of driver's mailbox queue
1308  * @phba: pointer to lpfc hba data structure.
1309  * @mbq: pointer to the driver internal queue element for mailbox command.
1310  *
1311  * Driver maintains a internal mailbox command queue implemented as a linked
1312  * list. When a mailbox command is issued, it shall be put into the mailbox
1313  * command queue such that they shall be processed orderly as HBA can process
1314  * one mailbox command at a time.
1315  **/
1316 void
1317 lpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1318 {
1319 	struct lpfc_sli *psli;
1320 
1321 	psli = &phba->sli;
1322 
1323 	list_add_tail(&mbq->list, &psli->mboxq);
1324 
1325 	psli->mboxq_cnt++;
1326 
1327 	return;
1328 }
1329 
1330 /**
1331  * lpfc_mbox_get - Remove a mailbox cmd from the head of driver's mailbox queue
1332  * @phba: pointer to lpfc hba data structure.
1333  *
1334  * Driver maintains a internal mailbox command queue implemented as a linked
1335  * list. When a mailbox command is issued, it shall be put into the mailbox
1336  * command queue such that they shall be processed orderly as HBA can process
1337  * one mailbox command at a time. After HBA finished processing a mailbox
1338  * command, the driver will remove a pending mailbox command from the head of
1339  * the mailbox command queue and send to the HBA for processing.
1340  *
1341  * Return codes
1342  *    pointer to the driver internal queue element for mailbox command.
1343  **/
1344 LPFC_MBOXQ_t *
1345 lpfc_mbox_get(struct lpfc_hba * phba)
1346 {
1347 	LPFC_MBOXQ_t *mbq = NULL;
1348 	struct lpfc_sli *psli = &phba->sli;
1349 
1350 	list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
1351 	if (mbq)
1352 		psli->mboxq_cnt--;
1353 
1354 	return mbq;
1355 }
1356 
1357 /**
1358  * __lpfc_mbox_cmpl_put - Put mailbox cmd into mailbox cmd complete list
1359  * @phba: pointer to lpfc hba data structure.
1360  * @mbq: pointer to the driver internal queue element for mailbox command.
1361  *
1362  * This routine put the completed mailbox command into the mailbox command
1363  * complete list. This is the unlocked version of the routine. The mailbox
1364  * complete list is used by the driver worker thread to process mailbox
1365  * complete callback functions outside the driver interrupt handler.
1366  **/
1367 void
1368 __lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1369 {
1370 	list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
1371 }
1372 
1373 /**
1374  * lpfc_mbox_cmpl_put - Put mailbox command into mailbox command complete list
1375  * @phba: pointer to lpfc hba data structure.
1376  * @mbq: pointer to the driver internal queue element for mailbox command.
1377  *
1378  * This routine put the completed mailbox command into the mailbox command
1379  * complete list. This is the locked version of the routine. The mailbox
1380  * complete list is used by the driver worker thread to process mailbox
1381  * complete callback functions outside the driver interrupt handler.
1382  **/
1383 void
1384 lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1385 {
1386 	unsigned long iflag;
1387 
1388 	/* This function expects to be called from interrupt context */
1389 	spin_lock_irqsave(&phba->hbalock, iflag);
1390 	__lpfc_mbox_cmpl_put(phba, mbq);
1391 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1392 	return;
1393 }
1394 
1395 /**
1396  * lpfc_mbox_cmd_check - Check the validality of a mailbox command
1397  * @phba: pointer to lpfc hba data structure.
1398  * @mboxq: pointer to the driver internal queue element for mailbox command.
1399  *
1400  * This routine is to check whether a mailbox command is valid to be issued.
1401  * This check will be performed by both the mailbox issue API when a client
1402  * is to issue a mailbox command to the mailbox transport.
1403  *
1404  * Return 0 - pass the check, -ENODEV - fail the check
1405  **/
1406 int
1407 lpfc_mbox_cmd_check(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1408 {
1409 	/* Mailbox command that have a completion handler must also have a
1410 	 * vport specified.
1411 	 */
1412 	if (mboxq->mbox_cmpl && mboxq->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
1413 	    mboxq->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
1414 		if (!mboxq->vport) {
1415 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1416 					"1814 Mbox x%x failed, no vport\n",
1417 					mboxq->u.mb.mbxCommand);
1418 			dump_stack();
1419 			return -ENODEV;
1420 		}
1421 	}
1422 	return 0;
1423 }
1424 
1425 /**
1426  * lpfc_mbox_dev_check - Check the device state for issuing a mailbox command
1427  * @phba: pointer to lpfc hba data structure.
1428  *
1429  * This routine is to check whether the HBA device is ready for posting a
1430  * mailbox command. It is used by the mailbox transport API at the time the
1431  * to post a mailbox command to the device.
1432  *
1433  * Return 0 - pass the check, -ENODEV - fail the check
1434  **/
1435 int
1436 lpfc_mbox_dev_check(struct lpfc_hba *phba)
1437 {
1438 	/* If the PCI channel is in offline state, do not issue mbox */
1439 	if (unlikely(pci_channel_offline(phba->pcidev)))
1440 		return -ENODEV;
1441 
1442 	/* If the HBA is in error state, do not issue mbox */
1443 	if (phba->link_state == LPFC_HBA_ERROR)
1444 		return -ENODEV;
1445 
1446 	return 0;
1447 }
1448 
1449 /**
1450  * lpfc_mbox_tmo_val - Retrieve mailbox command timeout value
1451  * @phba: pointer to lpfc hba data structure.
1452  * @cmd: mailbox command code.
1453  *
1454  * This routine retrieves the proper timeout value according to the mailbox
1455  * command code.
1456  *
1457  * Return codes
1458  *    Timeout value to be used for the given mailbox command
1459  **/
1460 int
1461 lpfc_mbox_tmo_val(struct lpfc_hba *phba, int cmd)
1462 {
1463 	switch (cmd) {
1464 	case MBX_WRITE_NV:	/* 0x03 */
1465 	case MBX_UPDATE_CFG:	/* 0x1B */
1466 	case MBX_DOWN_LOAD:	/* 0x1C */
1467 	case MBX_DEL_LD_ENTRY:	/* 0x1D */
1468 	case MBX_LOAD_AREA:	/* 0x81 */
1469 	case MBX_WRITE_WWN:     /* 0x98 */
1470 	case MBX_LOAD_EXP_ROM:	/* 0x9C */
1471 		return LPFC_MBOX_TMO_FLASH_CMD;
1472 	case MBX_SLI4_CONFIG:	/* 0x9b */
1473 		return LPFC_MBOX_SLI4_CONFIG_TMO;
1474 	}
1475 	return LPFC_MBOX_TMO;
1476 }
1477 
1478 /**
1479  * lpfc_sli4_mbx_sge_set - Set a sge entry in non-embedded mailbox command
1480  * @mbox: pointer to lpfc mbox command.
1481  * @sgentry: sge entry index.
1482  * @phyaddr: physical address for the sge
1483  * @length: Length of the sge.
1484  *
1485  * This routine sets up an entry in the non-embedded mailbox command at the sge
1486  * index location.
1487  **/
1488 void
1489 lpfc_sli4_mbx_sge_set(struct lpfcMboxq *mbox, uint32_t sgentry,
1490 		      dma_addr_t phyaddr, uint32_t length)
1491 {
1492 	struct lpfc_mbx_nembed_cmd *nembed_sge;
1493 
1494 	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1495 				&mbox->u.mqe.un.nembed_cmd;
1496 	nembed_sge->sge[sgentry].pa_lo = putPaddrLow(phyaddr);
1497 	nembed_sge->sge[sgentry].pa_hi = putPaddrHigh(phyaddr);
1498 	nembed_sge->sge[sgentry].length = length;
1499 }
1500 
1501 /**
1502  * lpfc_sli4_mbx_sge_get - Get a sge entry from non-embedded mailbox command
1503  * @mbox: pointer to lpfc mbox command.
1504  * @sgentry: sge entry index.
1505  *
1506  * This routine gets an entry from the non-embedded mailbox command at the sge
1507  * index location.
1508  **/
1509 void
1510 lpfc_sli4_mbx_sge_get(struct lpfcMboxq *mbox, uint32_t sgentry,
1511 		      struct lpfc_mbx_sge *sge)
1512 {
1513 	struct lpfc_mbx_nembed_cmd *nembed_sge;
1514 
1515 	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1516 				&mbox->u.mqe.un.nembed_cmd;
1517 	sge->pa_lo = nembed_sge->sge[sgentry].pa_lo;
1518 	sge->pa_hi = nembed_sge->sge[sgentry].pa_hi;
1519 	sge->length = nembed_sge->sge[sgentry].length;
1520 }
1521 
1522 /**
1523  * lpfc_sli4_mbox_cmd_free - Free a sli4 mailbox command
1524  * @phba: pointer to lpfc hba data structure.
1525  * @mbox: pointer to lpfc mbox command.
1526  *
1527  * This routine frees SLI4 specific mailbox command for sending IOCTL command.
1528  **/
1529 void
1530 lpfc_sli4_mbox_cmd_free(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1531 {
1532 	struct lpfc_mbx_sli4_config *sli4_cfg;
1533 	struct lpfc_mbx_sge sge;
1534 	dma_addr_t phyaddr;
1535 	uint32_t sgecount, sgentry;
1536 
1537 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
1538 
1539 	/* For embedded mbox command, just free the mbox command */
1540 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1541 		mempool_free(mbox, phba->mbox_mem_pool);
1542 		return;
1543 	}
1544 
1545 	/* For non-embedded mbox command, we need to free the pages first */
1546 	sgecount = bf_get(lpfc_mbox_hdr_sge_cnt, &sli4_cfg->header.cfg_mhdr);
1547 	/* There is nothing we can do if there is no sge address array */
1548 	if (unlikely(!mbox->sge_array)) {
1549 		mempool_free(mbox, phba->mbox_mem_pool);
1550 		return;
1551 	}
1552 	/* Each non-embedded DMA memory was allocated in the length of a page */
1553 	for (sgentry = 0; sgentry < sgecount; sgentry++) {
1554 		lpfc_sli4_mbx_sge_get(mbox, sgentry, &sge);
1555 		phyaddr = getPaddr(sge.pa_hi, sge.pa_lo);
1556 		dma_free_coherent(&phba->pcidev->dev, PAGE_SIZE,
1557 				  mbox->sge_array->addr[sgentry], phyaddr);
1558 	}
1559 	/* Free the sge address array memory */
1560 	kfree(mbox->sge_array);
1561 	/* Finally, free the mailbox command itself */
1562 	mempool_free(mbox, phba->mbox_mem_pool);
1563 }
1564 
1565 /**
1566  * lpfc_sli4_config - Initialize the  SLI4 Config Mailbox command
1567  * @phba: pointer to lpfc hba data structure.
1568  * @mbox: pointer to lpfc mbox command.
1569  * @subsystem: The sli4 config sub mailbox subsystem.
1570  * @opcode: The sli4 config sub mailbox command opcode.
1571  * @length: Length of the sli4 config mailbox command.
1572  *
1573  * This routine sets up the header fields of SLI4 specific mailbox command
1574  * for sending IOCTL command.
1575  *
1576  * Return: the actual length of the mbox command allocated (mostly useful
1577  *         for none embedded mailbox command).
1578  **/
1579 int
1580 lpfc_sli4_config(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1581 		 uint8_t subsystem, uint8_t opcode, uint32_t length, bool emb)
1582 {
1583 	struct lpfc_mbx_sli4_config *sli4_config;
1584 	union lpfc_sli4_cfg_shdr *cfg_shdr = NULL;
1585 	uint32_t alloc_len;
1586 	uint32_t resid_len;
1587 	uint32_t pagen, pcount;
1588 	void *viraddr;
1589 	dma_addr_t phyaddr;
1590 
1591 	/* Set up SLI4 mailbox command header fields */
1592 	memset(mbox, 0, sizeof(*mbox));
1593 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_SLI4_CONFIG);
1594 
1595 	/* Set up SLI4 ioctl command header fields */
1596 	sli4_config = &mbox->u.mqe.un.sli4_config;
1597 
1598 	/* Setup for the embedded mbox command */
1599 	if (emb) {
1600 		/* Set up main header fields */
1601 		bf_set(lpfc_mbox_hdr_emb, &sli4_config->header.cfg_mhdr, 1);
1602 		sli4_config->header.cfg_mhdr.payload_length =
1603 					LPFC_MBX_CMD_HDR_LENGTH + length;
1604 		/* Set up sub-header fields following main header */
1605 		bf_set(lpfc_mbox_hdr_opcode,
1606 			&sli4_config->header.cfg_shdr.request, opcode);
1607 		bf_set(lpfc_mbox_hdr_subsystem,
1608 			&sli4_config->header.cfg_shdr.request, subsystem);
1609 		sli4_config->header.cfg_shdr.request.request_length = length;
1610 		return length;
1611 	}
1612 
1613 	/* Setup for the none-embedded mbox command */
1614 	pcount = (PAGE_ALIGN(length))/PAGE_SIZE;
1615 	pcount = (pcount > LPFC_SLI4_MBX_SGE_MAX_PAGES) ?
1616 				LPFC_SLI4_MBX_SGE_MAX_PAGES : pcount;
1617 	/* Allocate record for keeping SGE virtual addresses */
1618 	mbox->sge_array = kmalloc(sizeof(struct lpfc_mbx_nembed_sge_virt),
1619 				  GFP_KERNEL);
1620 	if (!mbox->sge_array)
1621 		return 0;
1622 
1623 	for (pagen = 0, alloc_len = 0; pagen < pcount; pagen++) {
1624 		/* The DMA memory is always allocated in the length of a
1625 		 * page even though the last SGE might not fill up to a
1626 		 * page, this is used as a priori size of PAGE_SIZE for
1627 		 * the later DMA memory free.
1628 		 */
1629 		viraddr = dma_alloc_coherent(&phba->pcidev->dev, PAGE_SIZE,
1630 					     &phyaddr, GFP_KERNEL);
1631 		/* In case of malloc fails, proceed with whatever we have */
1632 		if (!viraddr)
1633 			break;
1634 		memset(viraddr, 0, PAGE_SIZE);
1635 		mbox->sge_array->addr[pagen] = viraddr;
1636 		/* Keep the first page for later sub-header construction */
1637 		if (pagen == 0)
1638 			cfg_shdr = (union lpfc_sli4_cfg_shdr *)viraddr;
1639 		resid_len = length - alloc_len;
1640 		if (resid_len > PAGE_SIZE) {
1641 			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1642 					      PAGE_SIZE);
1643 			alloc_len += PAGE_SIZE;
1644 		} else {
1645 			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1646 					      resid_len);
1647 			alloc_len = length;
1648 		}
1649 	}
1650 
1651 	/* Set up main header fields in mailbox command */
1652 	sli4_config->header.cfg_mhdr.payload_length = alloc_len;
1653 	bf_set(lpfc_mbox_hdr_sge_cnt, &sli4_config->header.cfg_mhdr, pagen);
1654 
1655 	/* Set up sub-header fields into the first page */
1656 	if (pagen > 0) {
1657 		bf_set(lpfc_mbox_hdr_opcode, &cfg_shdr->request, opcode);
1658 		bf_set(lpfc_mbox_hdr_subsystem, &cfg_shdr->request, subsystem);
1659 		cfg_shdr->request.request_length =
1660 				alloc_len - sizeof(union  lpfc_sli4_cfg_shdr);
1661 	}
1662 	/* The sub-header is in DMA memory, which needs endian converstion */
1663 	lpfc_sli_pcimem_bcopy(cfg_shdr, cfg_shdr,
1664 			      sizeof(union  lpfc_sli4_cfg_shdr));
1665 
1666 	return alloc_len;
1667 }
1668 
1669 /**
1670  * lpfc_sli4_mbox_opcode_get - Get the opcode from a sli4 mailbox command
1671  * @phba: pointer to lpfc hba data structure.
1672  * @mbox: pointer to lpfc mbox command.
1673  *
1674  * This routine gets the opcode from a SLI4 specific mailbox command for
1675  * sending IOCTL command. If the mailbox command is not MBX_SLI4_CONFIG
1676  * (0x9B) or if the IOCTL sub-header is not present, opcode 0x0 shall be
1677  * returned.
1678  **/
1679 uint8_t
1680 lpfc_sli4_mbox_opcode_get(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1681 {
1682 	struct lpfc_mbx_sli4_config *sli4_cfg;
1683 	union lpfc_sli4_cfg_shdr *cfg_shdr;
1684 
1685 	if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
1686 		return 0;
1687 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
1688 
1689 	/* For embedded mbox command, get opcode from embedded sub-header*/
1690 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1691 		cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
1692 		return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
1693 	}
1694 
1695 	/* For non-embedded mbox command, get opcode from first dma page */
1696 	if (unlikely(!mbox->sge_array))
1697 		return 0;
1698 	cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
1699 	return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
1700 }
1701 
1702 /**
1703  * lpfc_request_features: Configure SLI4 REQUEST_FEATURES mailbox
1704  * @mboxq: pointer to lpfc mbox command.
1705  *
1706  * This routine sets up the mailbox for an SLI4 REQUEST_FEATURES
1707  * mailbox command.
1708  **/
1709 void
1710 lpfc_request_features(struct lpfc_hba *phba, struct lpfcMboxq *mboxq)
1711 {
1712 	/* Set up SLI4 mailbox command header fields */
1713 	memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
1714 	bf_set(lpfc_mqe_command, &mboxq->u.mqe, MBX_SLI4_REQ_FTRS);
1715 
1716 	/* Set up host requested features. */
1717 	bf_set(lpfc_mbx_rq_ftr_rq_fcpi, &mboxq->u.mqe.un.req_ftrs, 1);
1718 
1719 	if (phba->cfg_enable_fip)
1720 		bf_set(lpfc_mbx_rq_ftr_rq_ifip, &mboxq->u.mqe.un.req_ftrs, 0);
1721 	else
1722 		bf_set(lpfc_mbx_rq_ftr_rq_ifip, &mboxq->u.mqe.un.req_ftrs, 1);
1723 
1724 	/* Enable DIF (block guard) only if configured to do so. */
1725 	if (phba->cfg_enable_bg)
1726 		bf_set(lpfc_mbx_rq_ftr_rq_dif, &mboxq->u.mqe.un.req_ftrs, 1);
1727 
1728 	/* Enable NPIV only if configured to do so. */
1729 	if (phba->max_vpi && phba->cfg_enable_npiv)
1730 		bf_set(lpfc_mbx_rq_ftr_rq_npiv, &mboxq->u.mqe.un.req_ftrs, 1);
1731 
1732 	return;
1733 }
1734 
1735 /**
1736  * lpfc_init_vfi - Initialize the INIT_VFI mailbox command
1737  * @mbox: pointer to lpfc mbox command to initialize.
1738  * @vport: Vport associated with the VF.
1739  *
1740  * This routine initializes @mbox to all zeros and then fills in the mailbox
1741  * fields from @vport. INIT_VFI configures virtual fabrics identified by VFI
1742  * in the context of an FCF. The driver issues this command to setup a VFI
1743  * before issuing a FLOGI to login to the VSAN. The driver should also issue a
1744  * REG_VFI after a successful VSAN login.
1745  **/
1746 void
1747 lpfc_init_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
1748 {
1749 	struct lpfc_mbx_init_vfi *init_vfi;
1750 
1751 	memset(mbox, 0, sizeof(*mbox));
1752 	init_vfi = &mbox->u.mqe.un.init_vfi;
1753 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VFI);
1754 	bf_set(lpfc_init_vfi_vr, init_vfi, 1);
1755 	bf_set(lpfc_init_vfi_vt, init_vfi, 1);
1756 	bf_set(lpfc_init_vfi_vfi, init_vfi, vport->vfi + vport->phba->vfi_base);
1757 	bf_set(lpfc_init_vfi_fcfi, init_vfi, vport->phba->fcf.fcfi);
1758 }
1759 
1760 /**
1761  * lpfc_reg_vfi - Initialize the REG_VFI mailbox command
1762  * @mbox: pointer to lpfc mbox command to initialize.
1763  * @vport: vport associated with the VF.
1764  * @phys: BDE DMA bus address used to send the service parameters to the HBA.
1765  *
1766  * This routine initializes @mbox to all zeros and then fills in the mailbox
1767  * fields from @vport, and uses @buf as a DMAable buffer to send the vport's
1768  * fc service parameters to the HBA for this VFI. REG_VFI configures virtual
1769  * fabrics identified by VFI in the context of an FCF.
1770  **/
1771 void
1772 lpfc_reg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport, dma_addr_t phys)
1773 {
1774 	struct lpfc_mbx_reg_vfi *reg_vfi;
1775 
1776 	memset(mbox, 0, sizeof(*mbox));
1777 	reg_vfi = &mbox->u.mqe.un.reg_vfi;
1778 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_VFI);
1779 	bf_set(lpfc_reg_vfi_vp, reg_vfi, 1);
1780 	bf_set(lpfc_reg_vfi_vfi, reg_vfi, vport->vfi + vport->phba->vfi_base);
1781 	bf_set(lpfc_reg_vfi_fcfi, reg_vfi, vport->phba->fcf.fcfi);
1782 	bf_set(lpfc_reg_vfi_vpi, reg_vfi, vport->vpi + vport->phba->vpi_base);
1783 	reg_vfi->bde.addrHigh = putPaddrHigh(phys);
1784 	reg_vfi->bde.addrLow = putPaddrLow(phys);
1785 	reg_vfi->bde.tus.f.bdeSize = sizeof(vport->fc_sparam);
1786 	reg_vfi->bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1787 	bf_set(lpfc_reg_vfi_nport_id, reg_vfi, vport->fc_myDID);
1788 }
1789 
1790 /**
1791  * lpfc_init_vpi - Initialize the INIT_VPI mailbox command
1792  * @mbox: pointer to lpfc mbox command to initialize.
1793  * @vpi: VPI to be initialized.
1794  *
1795  * The INIT_VPI mailbox command supports virtual N_Ports. The driver uses the
1796  * command to activate a virtual N_Port. The HBA assigns a MAC address to use
1797  * with the virtual N Port.  The SLI Host issues this command before issuing a
1798  * FDISC to connect to the Fabric. The SLI Host should issue a REG_VPI after a
1799  * successful virtual NPort login.
1800  **/
1801 void
1802 lpfc_init_vpi(struct lpfcMboxq *mbox, uint16_t vpi)
1803 {
1804 	memset(mbox, 0, sizeof(*mbox));
1805 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VPI);
1806 	bf_set(lpfc_init_vpi_vpi, &mbox->u.mqe.un.init_vpi, vpi);
1807 }
1808 
1809 /**
1810  * lpfc_unreg_vfi - Initialize the UNREG_VFI mailbox command
1811  * @mbox: pointer to lpfc mbox command to initialize.
1812  * @vfi: VFI to be unregistered.
1813  *
1814  * The UNREG_VFI mailbox command causes the SLI Host to put a virtual fabric
1815  * (logical NPort) into the inactive state. The SLI Host must have logged out
1816  * and unregistered all remote N_Ports to abort any activity on the virtual
1817  * fabric. The SLI Port posts the mailbox response after marking the virtual
1818  * fabric inactive.
1819  **/
1820 void
1821 lpfc_unreg_vfi(struct lpfcMboxq *mbox, uint16_t vfi)
1822 {
1823 	memset(mbox, 0, sizeof(*mbox));
1824 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_VFI);
1825 	bf_set(lpfc_unreg_vfi_vfi, &mbox->u.mqe.un.unreg_vfi, vfi);
1826 }
1827 
1828 /**
1829  * lpfc_dump_fcoe_param - Dump config region 23 to get FCoe parameters.
1830  * @phba: pointer to the hba structure containing.
1831  * @mbox: pointer to lpfc mbox command to initialize.
1832  *
1833  * This function create a SLI4 dump mailbox command to dump FCoE
1834  * parameters stored in region 23.
1835  **/
1836 int
1837 lpfc_dump_fcoe_param(struct lpfc_hba *phba,
1838 		struct lpfcMboxq *mbox)
1839 {
1840 	struct lpfc_dmabuf *mp = NULL;
1841 	MAILBOX_t *mb;
1842 
1843 	memset(mbox, 0, sizeof(*mbox));
1844 	mb = &mbox->u.mb;
1845 
1846 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1847 	if (mp)
1848 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1849 
1850 	if (!mp || !mp->virt) {
1851 		kfree(mp);
1852 		/* dump_fcoe_param failed to allocate memory */
1853 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
1854 			"2569 lpfc_dump_fcoe_param: memory"
1855 			" allocation failed \n");
1856 		return 1;
1857 	}
1858 
1859 	memset(mp->virt, 0, LPFC_BPL_SIZE);
1860 	INIT_LIST_HEAD(&mp->list);
1861 
1862 	/* save address for completion */
1863 	mbox->context1 = (uint8_t *) mp;
1864 
1865 	mb->mbxCommand = MBX_DUMP_MEMORY;
1866 	mb->un.varDmp.type = DMP_NV_PARAMS;
1867 	mb->un.varDmp.region_id = DMP_REGION_FCOEPARAM;
1868 	mb->un.varDmp.sli4_length = DMP_FCOEPARAM_RGN_SIZE;
1869 	mb->un.varWords[3] = putPaddrLow(mp->phys);
1870 	mb->un.varWords[4] = putPaddrHigh(mp->phys);
1871 	return 0;
1872 }
1873 
1874 /**
1875  * lpfc_reg_fcfi - Initialize the REG_FCFI mailbox command
1876  * @phba: pointer to the hba structure containing the FCF index and RQ ID.
1877  * @mbox: pointer to lpfc mbox command to initialize.
1878  *
1879  * The REG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs). The
1880  * SLI Host uses the command to activate an FCF after it has acquired FCF
1881  * information via a READ_FCF mailbox command. This mailbox command also is used
1882  * to indicate where received unsolicited frames from this FCF will be sent. By
1883  * default this routine will set up the FCF to forward all unsolicited frames
1884  * the the RQ ID passed in the @phba. This can be overridden by the caller for
1885  * more complicated setups.
1886  **/
1887 void
1888 lpfc_reg_fcfi(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1889 {
1890 	struct lpfc_mbx_reg_fcfi *reg_fcfi;
1891 
1892 	memset(mbox, 0, sizeof(*mbox));
1893 	reg_fcfi = &mbox->u.mqe.un.reg_fcfi;
1894 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI);
1895 	bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi, phba->sli4_hba.hdr_rq->queue_id);
1896 	bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi, REG_FCF_INVALID_QID);
1897 	bf_set(lpfc_reg_fcfi_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
1898 	bf_set(lpfc_reg_fcfi_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
1899 	bf_set(lpfc_reg_fcfi_info_index, reg_fcfi, phba->fcf.fcf_indx);
1900 	/* reg_fcf addr mode is bit wise inverted value of fcf addr_mode */
1901 	bf_set(lpfc_reg_fcfi_mam, reg_fcfi,
1902 		(~phba->fcf.addr_mode) & 0x3);
1903 	if (phba->fcf.fcf_flag & FCF_VALID_VLAN) {
1904 		bf_set(lpfc_reg_fcfi_vv, reg_fcfi, 1);
1905 		bf_set(lpfc_reg_fcfi_vlan_tag, reg_fcfi, phba->fcf.vlan_id);
1906 	}
1907 }
1908 
1909 /**
1910  * lpfc_unreg_fcfi - Initialize the UNREG_FCFI mailbox command
1911  * @mbox: pointer to lpfc mbox command to initialize.
1912  * @fcfi: FCFI to be unregistered.
1913  *
1914  * The UNREG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs).
1915  * The SLI Host uses the command to inactivate an FCFI.
1916  **/
1917 void
1918 lpfc_unreg_fcfi(struct lpfcMboxq *mbox, uint16_t fcfi)
1919 {
1920 	memset(mbox, 0, sizeof(*mbox));
1921 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_FCFI);
1922 	bf_set(lpfc_unreg_fcfi, &mbox->u.mqe.un.unreg_fcfi, fcfi);
1923 }
1924 
1925 /**
1926  * lpfc_resume_rpi - Initialize the RESUME_RPI mailbox command
1927  * @mbox: pointer to lpfc mbox command to initialize.
1928  * @ndlp: The nodelist structure that describes the RPI to resume.
1929  *
1930  * The RESUME_RPI mailbox command is used to restart I/O to an RPI after a
1931  * link event.
1932  **/
1933 void
1934 lpfc_resume_rpi(struct lpfcMboxq *mbox, struct lpfc_nodelist *ndlp)
1935 {
1936 	struct lpfc_mbx_resume_rpi *resume_rpi;
1937 
1938 	memset(mbox, 0, sizeof(*mbox));
1939 	resume_rpi = &mbox->u.mqe.un.resume_rpi;
1940 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_RESUME_RPI);
1941 	bf_set(lpfc_resume_rpi_rpi, resume_rpi, ndlp->nlp_rpi);
1942 	bf_set(lpfc_resume_rpi_vpi, resume_rpi,
1943 	       ndlp->vport->vpi + ndlp->vport->phba->vpi_base);
1944 	bf_set(lpfc_resume_rpi_vfi, resume_rpi,
1945 	       ndlp->vport->vfi + ndlp->vport->phba->vfi_base);
1946 }
1947