ARM: omap: update GPIO chained IRQ handler to use entry/exit functions
[linux-2.6/cjktty.git] / drivers / scsi / lpfc / lpfc_mbox.c
blobfbab9734e9b4e94560c75165c07530b25e3131e8
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 *******************************************************************/
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_transport_fc.h>
29 #include <scsi/scsi.h>
30 #include <scsi/fc/fc_fs.h>
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_sli4.h"
36 #include "lpfc_nl.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_compat.h"
44 /**
45 * lpfc_dump_static_vport - Dump HBA's static vport information.
46 * @phba: pointer to lpfc hba data structure.
47 * @pmb: pointer to the driver internal queue element for mailbox command.
48 * @offset: offset for dumping vport info.
50 * The dump mailbox command provides a method for the device driver to obtain
51 * various types of information from the HBA device.
53 * This routine prepares the mailbox command for dumping list of static
54 * vports to be created.
55 **/
56 int
57 lpfc_dump_static_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
58 uint16_t offset)
60 MAILBOX_t *mb;
61 struct lpfc_dmabuf *mp;
63 mb = &pmb->u.mb;
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.type = DMP_NV_PARAMS;
69 mb->un.varDmp.entry_index = offset;
70 mb->un.varDmp.region_id = DMP_REGION_VPORT;
71 mb->mbxOwner = OWN_HOST;
73 /* For SLI3 HBAs data is embedded in mailbox */
74 if (phba->sli_rev != LPFC_SLI_REV4) {
75 mb->un.varDmp.cv = 1;
76 mb->un.varDmp.word_cnt = DMP_RSP_SIZE/sizeof(uint32_t);
77 return 0;
80 /* For SLI4 HBAs driver need to allocate memory */
81 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
82 if (mp)
83 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
85 if (!mp || !mp->virt) {
86 kfree(mp);
87 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
88 "2605 lpfc_dump_static_vport: memory"
89 " allocation failed\n");
90 return 1;
92 memset(mp->virt, 0, LPFC_BPL_SIZE);
93 INIT_LIST_HEAD(&mp->list);
94 /* save address for completion */
95 pmb->context2 = (uint8_t *) mp;
96 mb->un.varWords[3] = putPaddrLow(mp->phys);
97 mb->un.varWords[4] = putPaddrHigh(mp->phys);
98 mb->un.varDmp.sli4_length = sizeof(struct static_vport_info);
100 return 0;
104 * lpfc_down_link - Bring down HBAs link.
105 * @phba: pointer to lpfc hba data structure.
106 * @pmb: pointer to the driver internal queue element for mailbox command.
108 * This routine prepares a mailbox command to bring down HBA link.
110 void
111 lpfc_down_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
113 MAILBOX_t *mb;
114 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
115 mb = &pmb->u.mb;
116 mb->mbxCommand = MBX_DOWN_LINK;
117 mb->mbxOwner = OWN_HOST;
121 * lpfc_dump_mem - Prepare a mailbox command for reading a region.
122 * @phba: pointer to lpfc hba data structure.
123 * @pmb: pointer to the driver internal queue element for mailbox command.
124 * @offset: offset into the region.
125 * @region_id: config region id.
127 * The dump mailbox command provides a method for the device driver to obtain
128 * various types of information from the HBA device.
130 * This routine prepares the mailbox command for dumping HBA's config region.
132 void
133 lpfc_dump_mem(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, uint16_t offset,
134 uint16_t region_id)
136 MAILBOX_t *mb;
137 void *ctx;
139 mb = &pmb->u.mb;
140 ctx = pmb->context2;
142 /* Setup to dump VPD region */
143 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
144 mb->mbxCommand = MBX_DUMP_MEMORY;
145 mb->un.varDmp.cv = 1;
146 mb->un.varDmp.type = DMP_NV_PARAMS;
147 mb->un.varDmp.entry_index = offset;
148 mb->un.varDmp.region_id = region_id;
149 mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
150 mb->un.varDmp.co = 0;
151 mb->un.varDmp.resp_offset = 0;
152 pmb->context2 = ctx;
153 mb->mbxOwner = OWN_HOST;
154 return;
158 * lpfc_dump_wakeup_param - Prepare mailbox command for retrieving wakeup params
159 * @phba: pointer to lpfc hba data structure.
160 * @pmb: pointer to the driver internal queue element for mailbox command.
162 * This function create a dump memory mailbox command to dump wake up
163 * parameters.
165 void
166 lpfc_dump_wakeup_param(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
168 MAILBOX_t *mb;
169 void *ctx;
171 mb = &pmb->u.mb;
172 /* Save context so that we can restore after memset */
173 ctx = pmb->context2;
175 /* Setup to dump VPD region */
176 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
177 mb->mbxCommand = MBX_DUMP_MEMORY;
178 mb->mbxOwner = OWN_HOST;
179 mb->un.varDmp.cv = 1;
180 mb->un.varDmp.type = DMP_NV_PARAMS;
181 mb->un.varDmp.entry_index = 0;
182 mb->un.varDmp.region_id = WAKE_UP_PARMS_REGION_ID;
183 mb->un.varDmp.word_cnt = WAKE_UP_PARMS_WORD_SIZE;
184 mb->un.varDmp.co = 0;
185 mb->un.varDmp.resp_offset = 0;
186 pmb->context2 = ctx;
187 return;
191 * lpfc_read_nv - Prepare a mailbox command for reading HBA's NVRAM param
192 * @phba: pointer to lpfc hba data structure.
193 * @pmb: pointer to the driver internal queue element for mailbox command.
195 * The read NVRAM mailbox command returns the HBA's non-volatile parameters
196 * that are used as defaults when the Fibre Channel link is brought on-line.
198 * This routine prepares the mailbox command for reading information stored
199 * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
201 void
202 lpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
204 MAILBOX_t *mb;
206 mb = &pmb->u.mb;
207 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
208 mb->mbxCommand = MBX_READ_NV;
209 mb->mbxOwner = OWN_HOST;
210 return;
214 * lpfc_config_async - Prepare a mailbox command for enabling HBA async event
215 * @phba: pointer to lpfc hba data structure.
216 * @pmb: pointer to the driver internal queue element for mailbox command.
217 * @ring: ring number for the asynchronous event to be configured.
219 * The asynchronous event enable mailbox command is used to enable the
220 * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
221 * specifies the default ring to which events are posted.
223 * This routine prepares the mailbox command for enabling HBA asynchronous
224 * event support on a IOCB ring.
226 void
227 lpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
228 uint32_t ring)
230 MAILBOX_t *mb;
232 mb = &pmb->u.mb;
233 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
234 mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
235 mb->un.varCfgAsyncEvent.ring = ring;
236 mb->mbxOwner = OWN_HOST;
237 return;
241 * lpfc_heart_beat - Prepare a mailbox command for heart beat
242 * @phba: pointer to lpfc hba data structure.
243 * @pmb: pointer to the driver internal queue element for mailbox command.
245 * The heart beat mailbox command is used to detect an unresponsive HBA, which
246 * is defined as any device where no error attention is sent and both mailbox
247 * and rings are not processed.
249 * This routine prepares the mailbox command for issuing a heart beat in the
250 * form of mailbox command to the HBA. The timely completion of the heart
251 * beat mailbox command indicates the health of the HBA.
253 void
254 lpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
256 MAILBOX_t *mb;
258 mb = &pmb->u.mb;
259 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
260 mb->mbxCommand = MBX_HEARTBEAT;
261 mb->mbxOwner = OWN_HOST;
262 return;
266 * lpfc_read_topology - Prepare a mailbox command for reading HBA topology
267 * @phba: pointer to lpfc hba data structure.
268 * @pmb: pointer to the driver internal queue element for mailbox command.
269 * @mp: DMA buffer memory for reading the link attention information into.
271 * The read topology mailbox command is issued to read the link topology
272 * information indicated by the HBA port when the Link Event bit of the Host
273 * Attention (HSTATT) register is set to 1 (For SLI-3) or when an FC Link
274 * Attention ACQE is received from the port (For SLI-4). A Link Event
275 * Attention occurs based on an exception detected at the Fibre Channel link
276 * interface.
278 * This routine prepares the mailbox command for reading HBA link topology
279 * information. A DMA memory has been set aside and address passed to the
280 * HBA through @mp for the HBA to DMA link attention information into the
281 * memory as part of the execution of the mailbox command.
283 * Return codes
284 * 0 - Success (currently always return 0)
287 lpfc_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
288 struct lpfc_dmabuf *mp)
290 MAILBOX_t *mb;
291 struct lpfc_sli *psli;
293 psli = &phba->sli;
294 mb = &pmb->u.mb;
295 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
297 INIT_LIST_HEAD(&mp->list);
298 mb->mbxCommand = MBX_READ_TOPOLOGY;
299 mb->un.varReadTop.lilpBde64.tus.f.bdeSize = LPFC_ALPA_MAP_SIZE;
300 mb->un.varReadTop.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
301 mb->un.varReadTop.lilpBde64.addrLow = putPaddrLow(mp->phys);
303 /* Save address for later completion and set the owner to host so that
304 * the FW knows this mailbox is available for processing.
306 pmb->context1 = (uint8_t *)mp;
307 mb->mbxOwner = OWN_HOST;
308 return (0);
312 * lpfc_clear_la - Prepare a mailbox command for clearing HBA link attention
313 * @phba: pointer to lpfc hba data structure.
314 * @pmb: pointer to the driver internal queue element for mailbox command.
316 * The clear link attention mailbox command is issued to clear the link event
317 * attention condition indicated by the Link Event bit of the Host Attention
318 * (HSTATT) register. The link event attention condition is cleared only if
319 * the event tag specified matches that of the current link event counter.
320 * The current event tag is read using the read link attention event mailbox
321 * command.
323 * This routine prepares the mailbox command for clearing HBA link attention
324 * information.
326 void
327 lpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
329 MAILBOX_t *mb;
331 mb = &pmb->u.mb;
332 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
334 mb->un.varClearLA.eventTag = phba->fc_eventTag;
335 mb->mbxCommand = MBX_CLEAR_LA;
336 mb->mbxOwner = OWN_HOST;
337 return;
341 * lpfc_config_link - Prepare a mailbox command for configuring link on a HBA
342 * @phba: pointer to lpfc hba data structure.
343 * @pmb: pointer to the driver internal queue element for mailbox command.
345 * The configure link mailbox command is used before the initialize link
346 * mailbox command to override default value and to configure link-oriented
347 * parameters such as DID address and various timers. Typically, this
348 * command would be used after an F_Port login to set the returned DID address
349 * and the fabric timeout values. This command is not valid before a configure
350 * port command has configured the HBA port.
352 * This routine prepares the mailbox command for configuring link on a HBA.
354 void
355 lpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
357 struct lpfc_vport *vport = phba->pport;
358 MAILBOX_t *mb = &pmb->u.mb;
359 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
361 /* NEW_FEATURE
362 * SLI-2, Coalescing Response Feature.
364 if (phba->cfg_cr_delay) {
365 mb->un.varCfgLnk.cr = 1;
366 mb->un.varCfgLnk.ci = 1;
367 mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
368 mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
371 mb->un.varCfgLnk.myId = vport->fc_myDID;
372 mb->un.varCfgLnk.edtov = phba->fc_edtov;
373 mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
374 mb->un.varCfgLnk.ratov = phba->fc_ratov;
375 mb->un.varCfgLnk.rttov = phba->fc_rttov;
376 mb->un.varCfgLnk.altov = phba->fc_altov;
377 mb->un.varCfgLnk.crtov = phba->fc_crtov;
378 mb->un.varCfgLnk.citov = phba->fc_citov;
380 if (phba->cfg_ack0)
381 mb->un.varCfgLnk.ack0_enable = 1;
383 mb->mbxCommand = MBX_CONFIG_LINK;
384 mb->mbxOwner = OWN_HOST;
385 return;
389 * lpfc_config_msi - Prepare a mailbox command for configuring msi-x
390 * @phba: pointer to lpfc hba data structure.
391 * @pmb: pointer to the driver internal queue element for mailbox command.
393 * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
394 * MSI-X multi-message interrupt vector association to interrupt attention
395 * conditions.
397 * Return codes
398 * 0 - Success
399 * -EINVAL - Failure
402 lpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
404 MAILBOX_t *mb = &pmb->u.mb;
405 uint32_t attentionConditions[2];
407 /* Sanity check */
408 if (phba->cfg_use_msi != 2) {
409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
410 "0475 Not configured for supporting MSI-X "
411 "cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
412 return -EINVAL;
415 if (phba->sli_rev < 3) {
416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
417 "0476 HBA not supporting SLI-3 or later "
418 "SLI Revision: 0x%x\n", phba->sli_rev);
419 return -EINVAL;
422 /* Clear mailbox command fields */
423 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
426 * SLI-3, Message Signaled Interrupt Fearure.
429 /* Multi-message attention configuration */
430 attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
431 HA_LATT | HA_MBATT);
432 attentionConditions[1] = 0;
434 mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
435 mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
438 * Set up message number to HA bit association
440 #ifdef __BIG_ENDIAN_BITFIELD
441 /* RA0 (FCP Ring) */
442 mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
443 /* RA1 (Other Protocol Extra Ring) */
444 mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
445 #else /* __LITTLE_ENDIAN_BITFIELD */
446 /* RA0 (FCP Ring) */
447 mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
448 /* RA1 (Other Protocol Extra Ring) */
449 mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
450 #endif
451 /* Multi-message interrupt autoclear configuration*/
452 mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
453 mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
455 /* For now, HBA autoclear does not work reliably, disable it */
456 mb->un.varCfgMSI.autoClearHA[0] = 0;
457 mb->un.varCfgMSI.autoClearHA[1] = 0;
459 /* Set command and owner bit */
460 mb->mbxCommand = MBX_CONFIG_MSI;
461 mb->mbxOwner = OWN_HOST;
463 return 0;
467 * lpfc_init_link - Prepare a mailbox command for initialize link on a HBA
468 * @phba: pointer to lpfc hba data structure.
469 * @pmb: pointer to the driver internal queue element for mailbox command.
470 * @topology: the link topology for the link to be initialized to.
471 * @linkspeed: the link speed for the link to be initialized to.
473 * The initialize link mailbox command is used to initialize the Fibre
474 * Channel link. This command must follow a configure port command that
475 * establishes the mode of operation.
477 * This routine prepares the mailbox command for initializing link on a HBA
478 * with the specified link topology and speed.
480 void
481 lpfc_init_link(struct lpfc_hba * phba,
482 LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
484 lpfc_vpd_t *vpd;
485 struct lpfc_sli *psli;
486 MAILBOX_t *mb;
488 mb = &pmb->u.mb;
489 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
491 psli = &phba->sli;
492 switch (topology) {
493 case FLAGS_TOPOLOGY_MODE_LOOP_PT:
494 mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
495 mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
496 break;
497 case FLAGS_TOPOLOGY_MODE_PT_PT:
498 mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
499 break;
500 case FLAGS_TOPOLOGY_MODE_LOOP:
501 mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
502 break;
503 case FLAGS_TOPOLOGY_MODE_PT_LOOP:
504 mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
505 mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
506 break;
507 case FLAGS_LOCAL_LB:
508 mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
509 break;
512 /* Enable asynchronous ABTS responses from firmware */
513 mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
515 /* NEW_FEATURE
516 * Setting up the link speed
518 vpd = &phba->vpd;
519 if (vpd->rev.feaLevelHigh >= 0x02){
520 switch(linkspeed){
521 case LPFC_USER_LINK_SPEED_1G:
522 mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
523 mb->un.varInitLnk.link_speed = LINK_SPEED_1G;
524 break;
525 case LPFC_USER_LINK_SPEED_2G:
526 mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
527 mb->un.varInitLnk.link_speed = LINK_SPEED_2G;
528 break;
529 case LPFC_USER_LINK_SPEED_4G:
530 mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
531 mb->un.varInitLnk.link_speed = LINK_SPEED_4G;
532 break;
533 case LPFC_USER_LINK_SPEED_8G:
534 mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
535 mb->un.varInitLnk.link_speed = LINK_SPEED_8G;
536 break;
537 case LPFC_USER_LINK_SPEED_10G:
538 mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
539 mb->un.varInitLnk.link_speed = LINK_SPEED_10G;
540 break;
541 case LPFC_USER_LINK_SPEED_16G:
542 mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
543 mb->un.varInitLnk.link_speed = LINK_SPEED_16G;
544 break;
545 case LPFC_USER_LINK_SPEED_AUTO:
546 default:
547 mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
548 break;
552 else
553 mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
555 mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
556 mb->mbxOwner = OWN_HOST;
557 mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
558 return;
562 * lpfc_read_sparam - Prepare a mailbox command for reading HBA parameters
563 * @phba: pointer to lpfc hba data structure.
564 * @pmb: pointer to the driver internal queue element for mailbox command.
565 * @vpi: virtual N_Port identifier.
567 * The read service parameter mailbox command is used to read the HBA port
568 * service parameters. The service parameters are read into the buffer
569 * specified directly by a BDE in the mailbox command. These service
570 * parameters may then be used to build the payload of an N_Port/F_POrt
571 * login request and reply (LOGI/ACC).
573 * This routine prepares the mailbox command for reading HBA port service
574 * parameters. The DMA memory is allocated in this function and the addresses
575 * are populated into the mailbox command for the HBA to DMA the service
576 * parameters into.
578 * Return codes
579 * 0 - Success
580 * 1 - DMA memory allocation failed
583 lpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
585 struct lpfc_dmabuf *mp;
586 MAILBOX_t *mb;
587 struct lpfc_sli *psli;
589 psli = &phba->sli;
590 mb = &pmb->u.mb;
591 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
593 mb->mbxOwner = OWN_HOST;
595 /* Get a buffer to hold the HBAs Service Parameters */
597 mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
598 if (mp)
599 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
600 if (!mp || !mp->virt) {
601 kfree(mp);
602 mb->mbxCommand = MBX_READ_SPARM64;
603 /* READ_SPARAM: no buffers */
604 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
605 "0301 READ_SPARAM: no buffers\n");
606 return (1);
608 INIT_LIST_HEAD(&mp->list);
609 mb->mbxCommand = MBX_READ_SPARM64;
610 mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
611 mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
612 mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
613 mb->un.varRdSparm.vpi = vpi + phba->vpi_base;
615 /* save address for completion */
616 pmb->context1 = mp;
618 return (0);
622 * lpfc_unreg_did - Prepare a mailbox command for unregistering DID
623 * @phba: pointer to lpfc hba data structure.
624 * @vpi: virtual N_Port identifier.
625 * @did: remote port identifier.
626 * @pmb: pointer to the driver internal queue element for mailbox command.
628 * The unregister DID mailbox command is used to unregister an N_Port/F_Port
629 * login for an unknown RPI by specifying the DID of a remote port. This
630 * command frees an RPI context in the HBA port. This has the effect of
631 * performing an implicit N_Port/F_Port logout.
633 * This routine prepares the mailbox command for unregistering a remote
634 * N_Port/F_Port (DID) login.
636 void
637 lpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
638 LPFC_MBOXQ_t * pmb)
640 MAILBOX_t *mb;
642 mb = &pmb->u.mb;
643 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
645 mb->un.varUnregDID.did = did;
646 if (vpi != 0xffff)
647 vpi += phba->vpi_base;
648 mb->un.varUnregDID.vpi = vpi;
650 mb->mbxCommand = MBX_UNREG_D_ID;
651 mb->mbxOwner = OWN_HOST;
652 return;
656 * lpfc_read_config - Prepare a mailbox command for reading HBA configuration
657 * @phba: pointer to lpfc hba data structure.
658 * @pmb: pointer to the driver internal queue element for mailbox command.
660 * The read configuration mailbox command is used to read the HBA port
661 * configuration parameters. This mailbox command provides a method for
662 * seeing any parameters that may have changed via various configuration
663 * mailbox commands.
665 * This routine prepares the mailbox command for reading out HBA configuration
666 * parameters.
668 void
669 lpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
671 MAILBOX_t *mb;
673 mb = &pmb->u.mb;
674 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
676 mb->mbxCommand = MBX_READ_CONFIG;
677 mb->mbxOwner = OWN_HOST;
678 return;
682 * lpfc_read_lnk_stat - Prepare a mailbox command for reading HBA link stats
683 * @phba: pointer to lpfc hba data structure.
684 * @pmb: pointer to the driver internal queue element for mailbox command.
686 * The read link status mailbox command is used to read the link status from
687 * the HBA. Link status includes all link-related error counters. These
688 * counters are maintained by the HBA and originated in the link hardware
689 * unit. Note that all of these counters wrap.
691 * This routine prepares the mailbox command for reading out HBA link status.
693 void
694 lpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
696 MAILBOX_t *mb;
698 mb = &pmb->u.mb;
699 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
701 mb->mbxCommand = MBX_READ_LNK_STAT;
702 mb->mbxOwner = OWN_HOST;
703 return;
707 * lpfc_reg_rpi - Prepare a mailbox command for registering remote login
708 * @phba: pointer to lpfc hba data structure.
709 * @vpi: virtual N_Port identifier.
710 * @did: remote port identifier.
711 * @param: pointer to memory holding the server parameters.
712 * @pmb: pointer to the driver internal queue element for mailbox command.
713 * @rpi: the rpi to use in the registration (usually only used for SLI4.
715 * The registration login mailbox command is used to register an N_Port or
716 * F_Port login. This registration allows the HBA to cache the remote N_Port
717 * service parameters internally and thereby make the appropriate FC-2
718 * decisions. The remote port service parameters are handed off by the driver
719 * to the HBA using a descriptor entry that directly identifies a buffer in
720 * host memory. In exchange, the HBA returns an RPI identifier.
722 * This routine prepares the mailbox command for registering remote port login.
723 * The function allocates DMA buffer for passing the service parameters to the
724 * HBA with the mailbox command.
726 * Return codes
727 * 0 - Success
728 * 1 - DMA memory allocation failed
731 lpfc_reg_rpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
732 uint8_t *param, LPFC_MBOXQ_t *pmb, uint16_t rpi)
734 MAILBOX_t *mb = &pmb->u.mb;
735 uint8_t *sparam;
736 struct lpfc_dmabuf *mp;
738 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
740 mb->un.varRegLogin.rpi = 0;
741 if (phba->sli_rev == LPFC_SLI_REV4) {
742 mb->un.varRegLogin.rpi = rpi;
743 if (mb->un.varRegLogin.rpi == LPFC_RPI_ALLOC_ERROR)
744 return 1;
746 mb->un.varRegLogin.vpi = vpi + phba->vpi_base;
747 mb->un.varRegLogin.did = did;
748 mb->mbxOwner = OWN_HOST;
749 /* Get a buffer to hold NPorts Service Parameters */
750 mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
751 if (mp)
752 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
753 if (!mp || !mp->virt) {
754 kfree(mp);
755 mb->mbxCommand = MBX_REG_LOGIN64;
756 /* REG_LOGIN: no buffers */
757 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
758 "0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
759 "rpi x%x\n", vpi, did, rpi);
760 return (1);
762 INIT_LIST_HEAD(&mp->list);
763 sparam = mp->virt;
765 /* Copy param's into a new buffer */
766 memcpy(sparam, param, sizeof (struct serv_parm));
768 /* save address for completion */
769 pmb->context1 = (uint8_t *) mp;
771 mb->mbxCommand = MBX_REG_LOGIN64;
772 mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
773 mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
774 mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
776 return (0);
780 * lpfc_unreg_login - Prepare a mailbox command for unregistering remote login
781 * @phba: pointer to lpfc hba data structure.
782 * @vpi: virtual N_Port identifier.
783 * @rpi: remote port identifier
784 * @pmb: pointer to the driver internal queue element for mailbox command.
786 * The unregistration login mailbox command is used to unregister an N_Port
787 * or F_Port login. This command frees an RPI context in the HBA. It has the
788 * effect of performing an implicit N_Port/F_Port logout.
790 * This routine prepares the mailbox command for unregistering remote port
791 * login.
793 void
794 lpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
795 LPFC_MBOXQ_t * pmb)
797 MAILBOX_t *mb;
799 mb = &pmb->u.mb;
800 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
802 mb->un.varUnregLogin.rpi = (uint16_t) rpi;
803 mb->un.varUnregLogin.rsvd1 = 0;
804 mb->un.varUnregLogin.vpi = vpi + phba->vpi_base;
806 mb->mbxCommand = MBX_UNREG_LOGIN;
807 mb->mbxOwner = OWN_HOST;
809 return;
813 * lpfc_sli4_unreg_all_rpis - unregister all RPIs for a vport on SLI4 HBA.
814 * @vport: pointer to a vport object.
816 * This routine sends mailbox command to unregister all active RPIs for
817 * a vport.
819 void
820 lpfc_sli4_unreg_all_rpis(struct lpfc_vport *vport)
822 struct lpfc_hba *phba = vport->phba;
823 LPFC_MBOXQ_t *mbox;
824 int rc;
826 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
827 if (mbox) {
828 lpfc_unreg_login(phba, vport->vpi,
829 vport->vpi + phba->vpi_base, mbox);
830 mbox->u.mb.un.varUnregLogin.rsvd1 = 0x4000 ;
831 mbox->vport = vport;
832 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
833 mbox->context1 = NULL;
834 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
835 if (rc == MBX_NOT_FINISHED)
836 mempool_free(mbox, phba->mbox_mem_pool);
841 * lpfc_reg_vpi - Prepare a mailbox command for registering vport identifier
842 * @phba: pointer to lpfc hba data structure.
843 * @vpi: virtual N_Port identifier.
844 * @sid: Fibre Channel S_ID (N_Port_ID assigned to a virtual N_Port).
845 * @pmb: pointer to the driver internal queue element for mailbox command.
847 * The registration vport identifier mailbox command is used to activate a
848 * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
849 * N_Port_ID against the information in the selected virtual N_Port context
850 * block and marks it active to allow normal processing of IOCB commands and
851 * received unsolicited exchanges.
853 * This routine prepares the mailbox command for registering a virtual N_Port.
855 void
856 lpfc_reg_vpi(struct lpfc_vport *vport, LPFC_MBOXQ_t *pmb)
858 MAILBOX_t *mb = &pmb->u.mb;
859 struct lpfc_hba *phba = vport->phba;
861 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
863 * Set the re-reg VPI bit for f/w to update the MAC address.
865 if ((phba->sli_rev == LPFC_SLI_REV4) &&
866 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI))
867 mb->un.varRegVpi.upd = 1;
868 mb->un.varRegVpi.vpi = vport->vpi + vport->phba->vpi_base;
869 mb->un.varRegVpi.sid = vport->fc_myDID;
870 mb->un.varRegVpi.vfi = vport->vfi + vport->phba->vfi_base;
871 memcpy(mb->un.varRegVpi.wwn, &vport->fc_portname,
872 sizeof(struct lpfc_name));
873 mb->un.varRegVpi.wwn[0] = cpu_to_le32(mb->un.varRegVpi.wwn[0]);
874 mb->un.varRegVpi.wwn[1] = cpu_to_le32(mb->un.varRegVpi.wwn[1]);
876 mb->mbxCommand = MBX_REG_VPI;
877 mb->mbxOwner = OWN_HOST;
878 return;
883 * lpfc_unreg_vpi - Prepare a mailbox command for unregistering vport id
884 * @phba: pointer to lpfc hba data structure.
885 * @vpi: virtual N_Port identifier.
886 * @pmb: pointer to the driver internal queue element for mailbox command.
888 * The unregistration vport identifier mailbox command is used to inactivate
889 * a virtual N_Port. The driver must have logged out and unregistered all
890 * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
891 * unregisters any default RPIs associated with the specified vpi, aborting
892 * any active exchanges. The HBA will post the mailbox response after making
893 * the virtual N_Port inactive.
895 * This routine prepares the mailbox command for unregistering a virtual
896 * N_Port.
898 void
899 lpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
901 MAILBOX_t *mb = &pmb->u.mb;
902 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
904 if (phba->sli_rev < LPFC_SLI_REV4)
905 mb->un.varUnregVpi.vpi = vpi + phba->vpi_base;
906 else
907 mb->un.varUnregVpi.sli4_vpi = vpi + phba->vpi_base;
909 mb->mbxCommand = MBX_UNREG_VPI;
910 mb->mbxOwner = OWN_HOST;
911 return;
916 * lpfc_config_pcb_setup - Set up IOCB rings in the Port Control Block (PCB)
917 * @phba: pointer to lpfc hba data structure.
919 * This routine sets up and initializes the IOCB rings in the Port Control
920 * Block (PCB).
922 static void
923 lpfc_config_pcb_setup(struct lpfc_hba * phba)
925 struct lpfc_sli *psli = &phba->sli;
926 struct lpfc_sli_ring *pring;
927 PCB_t *pcbp = phba->pcb;
928 dma_addr_t pdma_addr;
929 uint32_t offset;
930 uint32_t iocbCnt = 0;
931 int i;
933 pcbp->maxRing = (psli->num_rings - 1);
935 for (i = 0; i < psli->num_rings; i++) {
936 pring = &psli->ring[i];
938 pring->sizeCiocb = phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE:
939 SLI2_IOCB_CMD_SIZE;
940 pring->sizeRiocb = phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE:
941 SLI2_IOCB_RSP_SIZE;
942 /* A ring MUST have both cmd and rsp entries defined to be
943 valid */
944 if ((pring->numCiocb == 0) || (pring->numRiocb == 0)) {
945 pcbp->rdsc[i].cmdEntries = 0;
946 pcbp->rdsc[i].rspEntries = 0;
947 pcbp->rdsc[i].cmdAddrHigh = 0;
948 pcbp->rdsc[i].rspAddrHigh = 0;
949 pcbp->rdsc[i].cmdAddrLow = 0;
950 pcbp->rdsc[i].rspAddrLow = 0;
951 pring->cmdringaddr = NULL;
952 pring->rspringaddr = NULL;
953 continue;
955 /* Command ring setup for ring */
956 pring->cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
957 pcbp->rdsc[i].cmdEntries = pring->numCiocb;
959 offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
960 (uint8_t *) phba->slim2p.virt;
961 pdma_addr = phba->slim2p.phys + offset;
962 pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
963 pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
964 iocbCnt += pring->numCiocb;
966 /* Response ring setup for ring */
967 pring->rspringaddr = (void *) &phba->IOCBs[iocbCnt];
969 pcbp->rdsc[i].rspEntries = pring->numRiocb;
970 offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
971 (uint8_t *)phba->slim2p.virt;
972 pdma_addr = phba->slim2p.phys + offset;
973 pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
974 pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
975 iocbCnt += pring->numRiocb;
980 * lpfc_read_rev - Prepare a mailbox command for reading HBA revision
981 * @phba: pointer to lpfc hba data structure.
982 * @pmb: pointer to the driver internal queue element for mailbox command.
984 * The read revision mailbox command is used to read the revision levels of
985 * the HBA components. These components include hardware units, resident
986 * firmware, and available firmware. HBAs that supports SLI-3 mode of
987 * operation provide different response information depending on the version
988 * requested by the driver.
990 * This routine prepares the mailbox command for reading HBA revision
991 * information.
993 void
994 lpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
996 MAILBOX_t *mb = &pmb->u.mb;
997 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
998 mb->un.varRdRev.cv = 1;
999 mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
1000 mb->mbxCommand = MBX_READ_REV;
1001 mb->mbxOwner = OWN_HOST;
1002 return;
1005 void
1006 lpfc_sli4_swap_str(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1008 MAILBOX_t *mb = &pmb->u.mb;
1009 struct lpfc_mqe *mqe;
1011 switch (mb->mbxCommand) {
1012 case MBX_READ_REV:
1013 mqe = &pmb->u.mqe;
1014 lpfc_sli_pcimem_bcopy(mqe->un.read_rev.fw_name,
1015 mqe->un.read_rev.fw_name, 16);
1016 lpfc_sli_pcimem_bcopy(mqe->un.read_rev.ulp_fw_name,
1017 mqe->un.read_rev.ulp_fw_name, 16);
1018 break;
1019 default:
1020 break;
1022 return;
1026 * lpfc_build_hbq_profile2 - Set up the HBQ Selection Profile 2
1027 * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1028 * @hbq_desc: pointer to the HBQ selection profile descriptor.
1030 * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
1031 * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
1032 * the Sequence Length Test using the fields in the Selection Profile 2
1033 * extension in words 20:31.
1035 static void
1036 lpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
1037 struct lpfc_hbq_init *hbq_desc)
1039 hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
1040 hbqmb->profiles.profile2.maxlen = hbq_desc->maxlen;
1041 hbqmb->profiles.profile2.seqlenoff = hbq_desc->seqlenoff;
1045 * lpfc_build_hbq_profile3 - Set up the HBQ Selection Profile 3
1046 * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1047 * @hbq_desc: pointer to the HBQ selection profile descriptor.
1049 * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
1050 * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
1051 * the Sequence Length Test and Byte Field Test using the fields in the
1052 * Selection Profile 3 extension in words 20:31.
1054 static void
1055 lpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
1056 struct lpfc_hbq_init *hbq_desc)
1058 hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
1059 hbqmb->profiles.profile3.maxlen = hbq_desc->maxlen;
1060 hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
1061 hbqmb->profiles.profile3.seqlenoff = hbq_desc->seqlenoff;
1062 memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
1063 sizeof(hbqmb->profiles.profile3.cmdmatch));
1067 * lpfc_build_hbq_profile5 - Set up the HBQ Selection Profile 5
1068 * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1069 * @hbq_desc: pointer to the HBQ selection profile descriptor.
1071 * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
1072 * HBA tests the initial frame of an incoming sequence using the frame's
1073 * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
1074 * and Byte Field Test using the fields in the Selection Profile 5 extension
1075 * words 20:31.
1077 static void
1078 lpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
1079 struct lpfc_hbq_init *hbq_desc)
1081 hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
1082 hbqmb->profiles.profile5.maxlen = hbq_desc->maxlen;
1083 hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
1084 hbqmb->profiles.profile5.seqlenoff = hbq_desc->seqlenoff;
1085 memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
1086 sizeof(hbqmb->profiles.profile5.cmdmatch));
1090 * lpfc_config_hbq - Prepare a mailbox command for configuring an HBQ
1091 * @phba: pointer to lpfc hba data structure.
1092 * @id: HBQ identifier.
1093 * @hbq_desc: pointer to the HBA descriptor data structure.
1094 * @hbq_entry_index: index of the HBQ entry data structures.
1095 * @pmb: pointer to the driver internal queue element for mailbox command.
1097 * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
1098 * an HBQ. The configuration binds events that require buffers to a particular
1099 * ring and HBQ based on a selection profile.
1101 * This routine prepares the mailbox command for configuring an HBQ.
1103 void
1104 lpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
1105 struct lpfc_hbq_init *hbq_desc,
1106 uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
1108 int i;
1109 MAILBOX_t *mb = &pmb->u.mb;
1110 struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
1112 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1113 hbqmb->hbqId = id;
1114 hbqmb->entry_count = hbq_desc->entry_count; /* # entries in HBQ */
1115 hbqmb->recvNotify = hbq_desc->rn; /* Receive
1116 * Notification */
1117 hbqmb->numMask = hbq_desc->mask_count; /* # R_CTL/TYPE masks
1118 * # in words 0-19 */
1119 hbqmb->profile = hbq_desc->profile; /* Selection profile:
1120 * 0 = all,
1121 * 7 = logentry */
1122 hbqmb->ringMask = hbq_desc->ring_mask; /* Binds HBQ to a ring
1123 * e.g. Ring0=b0001,
1124 * ring2=b0100 */
1125 hbqmb->headerLen = hbq_desc->headerLen; /* 0 if not profile 4
1126 * or 5 */
1127 hbqmb->logEntry = hbq_desc->logEntry; /* Set to 1 if this
1128 * HBQ will be used
1129 * for LogEntry
1130 * buffers */
1131 hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
1132 hbq_entry_index * sizeof(struct lpfc_hbq_entry);
1133 hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
1135 mb->mbxCommand = MBX_CONFIG_HBQ;
1136 mb->mbxOwner = OWN_HOST;
1138 /* Copy info for profiles 2,3,5. Other
1139 * profiles this area is reserved
1141 if (hbq_desc->profile == 2)
1142 lpfc_build_hbq_profile2(hbqmb, hbq_desc);
1143 else if (hbq_desc->profile == 3)
1144 lpfc_build_hbq_profile3(hbqmb, hbq_desc);
1145 else if (hbq_desc->profile == 5)
1146 lpfc_build_hbq_profile5(hbqmb, hbq_desc);
1148 /* Return if no rctl / type masks for this HBQ */
1149 if (!hbq_desc->mask_count)
1150 return;
1152 /* Otherwise we setup specific rctl / type masks for this HBQ */
1153 for (i = 0; i < hbq_desc->mask_count; i++) {
1154 hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
1155 hbqmb->hbqMasks[i].tmask = hbq_desc->hbqMasks[i].tmask;
1156 hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
1157 hbqmb->hbqMasks[i].rctlmask = hbq_desc->hbqMasks[i].rctlmask;
1160 return;
1164 * lpfc_config_ring - Prepare a mailbox command for configuring an IOCB ring
1165 * @phba: pointer to lpfc hba data structure.
1166 * @ring:
1167 * @pmb: pointer to the driver internal queue element for mailbox command.
1169 * The configure ring mailbox command is used to configure an IOCB ring. This
1170 * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
1171 * ring. This is used to map incoming sequences to a particular ring whose
1172 * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
1173 * attempt to configure a ring whose number is greater than the number
1174 * specified in the Port Control Block (PCB). It is an error to issue the
1175 * configure ring command more than once with the same ring number. The HBA
1176 * returns an error if the driver attempts this.
1178 * This routine prepares the mailbox command for configuring IOCB ring.
1180 void
1181 lpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
1183 int i;
1184 MAILBOX_t *mb = &pmb->u.mb;
1185 struct lpfc_sli *psli;
1186 struct lpfc_sli_ring *pring;
1188 memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1190 mb->un.varCfgRing.ring = ring;
1191 mb->un.varCfgRing.maxOrigXchg = 0;
1192 mb->un.varCfgRing.maxRespXchg = 0;
1193 mb->un.varCfgRing.recvNotify = 1;
1195 psli = &phba->sli;
1196 pring = &psli->ring[ring];
1197 mb->un.varCfgRing.numMask = pring->num_mask;
1198 mb->mbxCommand = MBX_CONFIG_RING;
1199 mb->mbxOwner = OWN_HOST;
1201 /* Is this ring configured for a specific profile */
1202 if (pring->prt[0].profile) {
1203 mb->un.varCfgRing.profile = pring->prt[0].profile;
1204 return;
1207 /* Otherwise we setup specific rctl / type masks for this ring */
1208 for (i = 0; i < pring->num_mask; i++) {
1209 mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
1210 if (mb->un.varCfgRing.rrRegs[i].rval != FC_RCTL_ELS_REQ)
1211 mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
1212 else
1213 mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
1214 mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
1215 mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
1218 return;
1222 * lpfc_config_port - Prepare a mailbox command for configuring port
1223 * @phba: pointer to lpfc hba data structure.
1224 * @pmb: pointer to the driver internal queue element for mailbox command.
1226 * The configure port mailbox command is used to identify the Port Control
1227 * Block (PCB) in the driver memory. After this command is issued, the
1228 * driver must not access the mailbox in the HBA without first resetting
1229 * the HBA. The HBA may copy the PCB information to internal storage for
1230 * subsequent use; the driver can not change the PCB information unless it
1231 * resets the HBA.
1233 * This routine prepares the mailbox command for configuring port.
1235 void
1236 lpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1238 MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
1239 MAILBOX_t *mb = &pmb->u.mb;
1240 dma_addr_t pdma_addr;
1241 uint32_t bar_low, bar_high;
1242 size_t offset;
1243 struct lpfc_hgp hgp;
1244 int i;
1245 uint32_t pgp_offset;
1247 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1248 mb->mbxCommand = MBX_CONFIG_PORT;
1249 mb->mbxOwner = OWN_HOST;
1251 mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
1253 offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
1254 pdma_addr = phba->slim2p.phys + offset;
1255 mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
1256 mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
1258 /* Always Host Group Pointer is in SLIM */
1259 mb->un.varCfgPort.hps = 1;
1261 /* If HBA supports SLI=3 ask for it */
1263 if (phba->sli_rev == LPFC_SLI_REV3 && phba->vpd.sli3Feat.cerbm) {
1264 if (phba->cfg_enable_bg)
1265 mb->un.varCfgPort.cbg = 1; /* configure BlockGuard */
1266 if (phba->cfg_enable_dss)
1267 mb->un.varCfgPort.cdss = 1; /* Configure Security */
1268 mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
1269 mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
1270 mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
1271 if (phba->max_vpi && phba->cfg_enable_npiv &&
1272 phba->vpd.sli3Feat.cmv) {
1273 mb->un.varCfgPort.max_vpi = LPFC_MAX_VPI;
1274 mb->un.varCfgPort.cmv = 1;
1275 } else
1276 mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
1277 } else
1278 phba->sli_rev = LPFC_SLI_REV2;
1279 mb->un.varCfgPort.sli_mode = phba->sli_rev;
1281 /* Now setup pcb */
1282 phba->pcb->type = TYPE_NATIVE_SLI2;
1283 phba->pcb->feature = FEATURE_INITIAL_SLI2;
1285 /* Setup Mailbox pointers */
1286 phba->pcb->mailBoxSize = sizeof(MAILBOX_t) + MAILBOX_EXT_SIZE;
1287 offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
1288 pdma_addr = phba->slim2p.phys + offset;
1289 phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
1290 phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
1293 * Setup Host Group ring pointer.
1295 * For efficiency reasons, the ring get/put pointers can be
1296 * placed in adapter memory (SLIM) rather than in host memory.
1297 * This allows firmware to avoid PCI reads/writes when updating
1298 * and checking pointers.
1300 * The firmware recognizes the use of SLIM memory by comparing
1301 * the address of the get/put pointers structure with that of
1302 * the SLIM BAR (BAR0).
1304 * Caution: be sure to use the PCI config space value of BAR0/BAR1
1305 * (the hardware's view of the base address), not the OS's
1306 * value of pci_resource_start() as the OS value may be a cookie
1307 * for ioremap/iomap.
1311 pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
1312 pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
1315 * Set up HGP - Port Memory
1317 * The port expects the host get/put pointers to reside in memory
1318 * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
1319 * area of SLIM. In SLI-2 mode, there's an additional 16 reserved
1320 * words (0x40 bytes). This area is not reserved if HBQs are
1321 * configured in SLI-3.
1323 * CR0Put - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
1324 * RR0Get 0xc4 0x84
1325 * CR1Put 0xc8 0x88
1326 * RR1Get 0xcc 0x8c
1327 * CR2Put 0xd0 0x90
1328 * RR2Get 0xd4 0x94
1329 * CR3Put 0xd8 0x98
1330 * RR3Get 0xdc 0x9c
1332 * Reserved 0xa0-0xbf
1333 * If HBQs configured:
1334 * HBQ 0 Put ptr 0xc0
1335 * HBQ 1 Put ptr 0xc4
1336 * HBQ 2 Put ptr 0xc8
1337 * ......
1338 * HBQ(M-1)Put Pointer 0xc0+(M-1)*4
1342 if (phba->cfg_hostmem_hgp && phba->sli_rev != 3) {
1343 phba->host_gp = &phba->mbox->us.s2.host[0];
1344 phba->hbq_put = NULL;
1345 offset = (uint8_t *)&phba->mbox->us.s2.host -
1346 (uint8_t *)phba->slim2p.virt;
1347 pdma_addr = phba->slim2p.phys + offset;
1348 phba->pcb->hgpAddrHigh = putPaddrHigh(pdma_addr);
1349 phba->pcb->hgpAddrLow = putPaddrLow(pdma_addr);
1350 } else {
1351 /* Always Host Group Pointer is in SLIM */
1352 mb->un.varCfgPort.hps = 1;
1354 if (phba->sli_rev == 3) {
1355 phba->host_gp = &mb_slim->us.s3.host[0];
1356 phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
1357 } else {
1358 phba->host_gp = &mb_slim->us.s2.host[0];
1359 phba->hbq_put = NULL;
1362 /* mask off BAR0's flag bits 0 - 3 */
1363 phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
1364 (void __iomem *)phba->host_gp -
1365 (void __iomem *)phba->MBslimaddr;
1366 if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
1367 phba->pcb->hgpAddrHigh = bar_high;
1368 else
1369 phba->pcb->hgpAddrHigh = 0;
1370 /* write HGP data to SLIM at the required longword offset */
1371 memset(&hgp, 0, sizeof(struct lpfc_hgp));
1373 for (i = 0; i < phba->sli.num_rings; i++) {
1374 lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
1375 sizeof(*phba->host_gp));
1379 /* Setup Port Group offset */
1380 if (phba->sli_rev == 3)
1381 pgp_offset = offsetof(struct lpfc_sli2_slim,
1382 mbx.us.s3_pgp.port);
1383 else
1384 pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
1385 pdma_addr = phba->slim2p.phys + pgp_offset;
1386 phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
1387 phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
1389 /* Use callback routine to setp rings in the pcb */
1390 lpfc_config_pcb_setup(phba);
1392 /* special handling for LC HBAs */
1393 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
1394 uint32_t hbainit[5];
1396 lpfc_hba_init(phba, hbainit);
1398 memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
1401 /* Swap PCB if needed */
1402 lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
1406 * lpfc_kill_board - Prepare a mailbox command for killing board
1407 * @phba: pointer to lpfc hba data structure.
1408 * @pmb: pointer to the driver internal queue element for mailbox command.
1410 * The kill board mailbox command is used to tell firmware to perform a
1411 * graceful shutdown of a channel on a specified board to prepare for reset.
1412 * When the kill board mailbox command is received, the ER3 bit is set to 1
1413 * in the Host Status register and the ER Attention bit is set to 1 in the
1414 * Host Attention register of the HBA function that received the kill board
1415 * command.
1417 * This routine prepares the mailbox command for killing the board in
1418 * preparation for a graceful shutdown.
1420 void
1421 lpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1423 MAILBOX_t *mb = &pmb->u.mb;
1425 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1426 mb->mbxCommand = MBX_KILL_BOARD;
1427 mb->mbxOwner = OWN_HOST;
1428 return;
1432 * lpfc_mbox_put - Put a mailbox cmd into the tail of driver's mailbox queue
1433 * @phba: pointer to lpfc hba data structure.
1434 * @mbq: pointer to the driver internal queue element for mailbox command.
1436 * Driver maintains a internal mailbox command queue implemented as a linked
1437 * list. When a mailbox command is issued, it shall be put into the mailbox
1438 * command queue such that they shall be processed orderly as HBA can process
1439 * one mailbox command at a time.
1441 void
1442 lpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1444 struct lpfc_sli *psli;
1446 psli = &phba->sli;
1448 list_add_tail(&mbq->list, &psli->mboxq);
1450 psli->mboxq_cnt++;
1452 return;
1456 * lpfc_mbox_get - Remove a mailbox cmd from the head of driver's mailbox queue
1457 * @phba: pointer to lpfc hba data structure.
1459 * Driver maintains a internal mailbox command queue implemented as a linked
1460 * list. When a mailbox command is issued, it shall be put into the mailbox
1461 * command queue such that they shall be processed orderly as HBA can process
1462 * one mailbox command at a time. After HBA finished processing a mailbox
1463 * command, the driver will remove a pending mailbox command from the head of
1464 * the mailbox command queue and send to the HBA for processing.
1466 * Return codes
1467 * pointer to the driver internal queue element for mailbox command.
1469 LPFC_MBOXQ_t *
1470 lpfc_mbox_get(struct lpfc_hba * phba)
1472 LPFC_MBOXQ_t *mbq = NULL;
1473 struct lpfc_sli *psli = &phba->sli;
1475 list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
1476 if (mbq)
1477 psli->mboxq_cnt--;
1479 return mbq;
1483 * __lpfc_mbox_cmpl_put - Put mailbox cmd into mailbox cmd complete list
1484 * @phba: pointer to lpfc hba data structure.
1485 * @mbq: pointer to the driver internal queue element for mailbox command.
1487 * This routine put the completed mailbox command into the mailbox command
1488 * complete list. This is the unlocked version of the routine. The mailbox
1489 * complete list is used by the driver worker thread to process mailbox
1490 * complete callback functions outside the driver interrupt handler.
1492 void
1493 __lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1495 list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
1499 * lpfc_mbox_cmpl_put - Put mailbox command into mailbox command complete list
1500 * @phba: pointer to lpfc hba data structure.
1501 * @mbq: pointer to the driver internal queue element for mailbox command.
1503 * This routine put the completed mailbox command into the mailbox command
1504 * complete list. This is the locked version of the routine. The mailbox
1505 * complete list is used by the driver worker thread to process mailbox
1506 * complete callback functions outside the driver interrupt handler.
1508 void
1509 lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1511 unsigned long iflag;
1513 /* This function expects to be called from interrupt context */
1514 spin_lock_irqsave(&phba->hbalock, iflag);
1515 __lpfc_mbox_cmpl_put(phba, mbq);
1516 spin_unlock_irqrestore(&phba->hbalock, iflag);
1517 return;
1521 * lpfc_mbox_cmd_check - Check the validality of a mailbox command
1522 * @phba: pointer to lpfc hba data structure.
1523 * @mboxq: pointer to the driver internal queue element for mailbox command.
1525 * This routine is to check whether a mailbox command is valid to be issued.
1526 * This check will be performed by both the mailbox issue API when a client
1527 * is to issue a mailbox command to the mailbox transport.
1529 * Return 0 - pass the check, -ENODEV - fail the check
1532 lpfc_mbox_cmd_check(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1534 /* Mailbox command that have a completion handler must also have a
1535 * vport specified.
1537 if (mboxq->mbox_cmpl && mboxq->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
1538 mboxq->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
1539 if (!mboxq->vport) {
1540 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1541 "1814 Mbox x%x failed, no vport\n",
1542 mboxq->u.mb.mbxCommand);
1543 dump_stack();
1544 return -ENODEV;
1547 return 0;
1551 * lpfc_mbox_dev_check - Check the device state for issuing a mailbox command
1552 * @phba: pointer to lpfc hba data structure.
1554 * This routine is to check whether the HBA device is ready for posting a
1555 * mailbox command. It is used by the mailbox transport API at the time the
1556 * to post a mailbox command to the device.
1558 * Return 0 - pass the check, -ENODEV - fail the check
1561 lpfc_mbox_dev_check(struct lpfc_hba *phba)
1563 /* If the PCI channel is in offline state, do not issue mbox */
1564 if (unlikely(pci_channel_offline(phba->pcidev)))
1565 return -ENODEV;
1567 /* If the HBA is in error state, do not issue mbox */
1568 if (phba->link_state == LPFC_HBA_ERROR)
1569 return -ENODEV;
1571 return 0;
1575 * lpfc_mbox_tmo_val - Retrieve mailbox command timeout value
1576 * @phba: pointer to lpfc hba data structure.
1577 * @cmd: mailbox command code.
1579 * This routine retrieves the proper timeout value according to the mailbox
1580 * command code.
1582 * Return codes
1583 * Timeout value to be used for the given mailbox command
1586 lpfc_mbox_tmo_val(struct lpfc_hba *phba, int cmd)
1588 switch (cmd) {
1589 case MBX_WRITE_NV: /* 0x03 */
1590 case MBX_UPDATE_CFG: /* 0x1B */
1591 case MBX_DOWN_LOAD: /* 0x1C */
1592 case MBX_DEL_LD_ENTRY: /* 0x1D */
1593 case MBX_LOAD_AREA: /* 0x81 */
1594 case MBX_WRITE_WWN: /* 0x98 */
1595 case MBX_LOAD_EXP_ROM: /* 0x9C */
1596 return LPFC_MBOX_TMO_FLASH_CMD;
1597 case MBX_SLI4_CONFIG: /* 0x9b */
1598 return LPFC_MBOX_SLI4_CONFIG_TMO;
1600 return LPFC_MBOX_TMO;
1604 * lpfc_sli4_mbx_sge_set - Set a sge entry in non-embedded mailbox command
1605 * @mbox: pointer to lpfc mbox command.
1606 * @sgentry: sge entry index.
1607 * @phyaddr: physical address for the sge
1608 * @length: Length of the sge.
1610 * This routine sets up an entry in the non-embedded mailbox command at the sge
1611 * index location.
1613 void
1614 lpfc_sli4_mbx_sge_set(struct lpfcMboxq *mbox, uint32_t sgentry,
1615 dma_addr_t phyaddr, uint32_t length)
1617 struct lpfc_mbx_nembed_cmd *nembed_sge;
1619 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1620 &mbox->u.mqe.un.nembed_cmd;
1621 nembed_sge->sge[sgentry].pa_lo = putPaddrLow(phyaddr);
1622 nembed_sge->sge[sgentry].pa_hi = putPaddrHigh(phyaddr);
1623 nembed_sge->sge[sgentry].length = length;
1627 * lpfc_sli4_mbx_sge_get - Get a sge entry from non-embedded mailbox command
1628 * @mbox: pointer to lpfc mbox command.
1629 * @sgentry: sge entry index.
1631 * This routine gets an entry from the non-embedded mailbox command at the sge
1632 * index location.
1634 void
1635 lpfc_sli4_mbx_sge_get(struct lpfcMboxq *mbox, uint32_t sgentry,
1636 struct lpfc_mbx_sge *sge)
1638 struct lpfc_mbx_nembed_cmd *nembed_sge;
1640 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1641 &mbox->u.mqe.un.nembed_cmd;
1642 sge->pa_lo = nembed_sge->sge[sgentry].pa_lo;
1643 sge->pa_hi = nembed_sge->sge[sgentry].pa_hi;
1644 sge->length = nembed_sge->sge[sgentry].length;
1648 * lpfc_sli4_mbox_cmd_free - Free a sli4 mailbox command
1649 * @phba: pointer to lpfc hba data structure.
1650 * @mbox: pointer to lpfc mbox command.
1652 * This routine frees SLI4 specific mailbox command for sending IOCTL command.
1654 void
1655 lpfc_sli4_mbox_cmd_free(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1657 struct lpfc_mbx_sli4_config *sli4_cfg;
1658 struct lpfc_mbx_sge sge;
1659 dma_addr_t phyaddr;
1660 uint32_t sgecount, sgentry;
1662 sli4_cfg = &mbox->u.mqe.un.sli4_config;
1664 /* For embedded mbox command, just free the mbox command */
1665 if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1666 mempool_free(mbox, phba->mbox_mem_pool);
1667 return;
1670 /* For non-embedded mbox command, we need to free the pages first */
1671 sgecount = bf_get(lpfc_mbox_hdr_sge_cnt, &sli4_cfg->header.cfg_mhdr);
1672 /* There is nothing we can do if there is no sge address array */
1673 if (unlikely(!mbox->sge_array)) {
1674 mempool_free(mbox, phba->mbox_mem_pool);
1675 return;
1677 /* Each non-embedded DMA memory was allocated in the length of a page */
1678 for (sgentry = 0; sgentry < sgecount; sgentry++) {
1679 lpfc_sli4_mbx_sge_get(mbox, sgentry, &sge);
1680 phyaddr = getPaddr(sge.pa_hi, sge.pa_lo);
1681 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
1682 mbox->sge_array->addr[sgentry], phyaddr);
1684 /* Free the sge address array memory */
1685 kfree(mbox->sge_array);
1686 /* Finally, free the mailbox command itself */
1687 mempool_free(mbox, phba->mbox_mem_pool);
1691 * lpfc_sli4_config - Initialize the SLI4 Config Mailbox command
1692 * @phba: pointer to lpfc hba data structure.
1693 * @mbox: pointer to lpfc mbox command.
1694 * @subsystem: The sli4 config sub mailbox subsystem.
1695 * @opcode: The sli4 config sub mailbox command opcode.
1696 * @length: Length of the sli4 config mailbox command (including sub-header).
1698 * This routine sets up the header fields of SLI4 specific mailbox command
1699 * for sending IOCTL command.
1701 * Return: the actual length of the mbox command allocated (mostly useful
1702 * for none embedded mailbox command).
1705 lpfc_sli4_config(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1706 uint8_t subsystem, uint8_t opcode, uint32_t length, bool emb)
1708 struct lpfc_mbx_sli4_config *sli4_config;
1709 union lpfc_sli4_cfg_shdr *cfg_shdr = NULL;
1710 uint32_t alloc_len;
1711 uint32_t resid_len;
1712 uint32_t pagen, pcount;
1713 void *viraddr;
1714 dma_addr_t phyaddr;
1716 /* Set up SLI4 mailbox command header fields */
1717 memset(mbox, 0, sizeof(*mbox));
1718 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_SLI4_CONFIG);
1720 /* Set up SLI4 ioctl command header fields */
1721 sli4_config = &mbox->u.mqe.un.sli4_config;
1723 /* Setup for the embedded mbox command */
1724 if (emb) {
1725 /* Set up main header fields */
1726 bf_set(lpfc_mbox_hdr_emb, &sli4_config->header.cfg_mhdr, 1);
1727 sli4_config->header.cfg_mhdr.payload_length = length;
1728 /* Set up sub-header fields following main header */
1729 bf_set(lpfc_mbox_hdr_opcode,
1730 &sli4_config->header.cfg_shdr.request, opcode);
1731 bf_set(lpfc_mbox_hdr_subsystem,
1732 &sli4_config->header.cfg_shdr.request, subsystem);
1733 sli4_config->header.cfg_shdr.request.request_length =
1734 length - LPFC_MBX_CMD_HDR_LENGTH;
1735 return length;
1738 /* Setup for the none-embedded mbox command */
1739 pcount = (PAGE_ALIGN(length))/SLI4_PAGE_SIZE;
1740 pcount = (pcount > LPFC_SLI4_MBX_SGE_MAX_PAGES) ?
1741 LPFC_SLI4_MBX_SGE_MAX_PAGES : pcount;
1742 /* Allocate record for keeping SGE virtual addresses */
1743 mbox->sge_array = kmalloc(sizeof(struct lpfc_mbx_nembed_sge_virt),
1744 GFP_KERNEL);
1745 if (!mbox->sge_array) {
1746 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1747 "2527 Failed to allocate non-embedded SGE "
1748 "array.\n");
1749 return 0;
1751 for (pagen = 0, alloc_len = 0; pagen < pcount; pagen++) {
1752 /* The DMA memory is always allocated in the length of a
1753 * page even though the last SGE might not fill up to a
1754 * page, this is used as a priori size of SLI4_PAGE_SIZE for
1755 * the later DMA memory free.
1757 viraddr = dma_alloc_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
1758 &phyaddr, GFP_KERNEL);
1759 /* In case of malloc fails, proceed with whatever we have */
1760 if (!viraddr)
1761 break;
1762 memset(viraddr, 0, SLI4_PAGE_SIZE);
1763 mbox->sge_array->addr[pagen] = viraddr;
1764 /* Keep the first page for later sub-header construction */
1765 if (pagen == 0)
1766 cfg_shdr = (union lpfc_sli4_cfg_shdr *)viraddr;
1767 resid_len = length - alloc_len;
1768 if (resid_len > SLI4_PAGE_SIZE) {
1769 lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1770 SLI4_PAGE_SIZE);
1771 alloc_len += SLI4_PAGE_SIZE;
1772 } else {
1773 lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1774 resid_len);
1775 alloc_len = length;
1779 /* Set up main header fields in mailbox command */
1780 sli4_config->header.cfg_mhdr.payload_length = alloc_len;
1781 bf_set(lpfc_mbox_hdr_sge_cnt, &sli4_config->header.cfg_mhdr, pagen);
1783 /* Set up sub-header fields into the first page */
1784 if (pagen > 0) {
1785 bf_set(lpfc_mbox_hdr_opcode, &cfg_shdr->request, opcode);
1786 bf_set(lpfc_mbox_hdr_subsystem, &cfg_shdr->request, subsystem);
1787 cfg_shdr->request.request_length =
1788 alloc_len - sizeof(union lpfc_sli4_cfg_shdr);
1790 /* The sub-header is in DMA memory, which needs endian converstion */
1791 if (cfg_shdr)
1792 lpfc_sli_pcimem_bcopy(cfg_shdr, cfg_shdr,
1793 sizeof(union lpfc_sli4_cfg_shdr));
1795 return alloc_len;
1799 * lpfc_sli4_mbox_opcode_get - Get the opcode from a sli4 mailbox command
1800 * @phba: pointer to lpfc hba data structure.
1801 * @mbox: pointer to lpfc mbox command.
1803 * This routine gets the opcode from a SLI4 specific mailbox command for
1804 * sending IOCTL command. If the mailbox command is not MBX_SLI4_CONFIG
1805 * (0x9B) or if the IOCTL sub-header is not present, opcode 0x0 shall be
1806 * returned.
1808 uint8_t
1809 lpfc_sli4_mbox_opcode_get(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1811 struct lpfc_mbx_sli4_config *sli4_cfg;
1812 union lpfc_sli4_cfg_shdr *cfg_shdr;
1814 if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
1815 return 0;
1816 sli4_cfg = &mbox->u.mqe.un.sli4_config;
1818 /* For embedded mbox command, get opcode from embedded sub-header*/
1819 if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1820 cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
1821 return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
1824 /* For non-embedded mbox command, get opcode from first dma page */
1825 if (unlikely(!mbox->sge_array))
1826 return 0;
1827 cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
1828 return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
1832 * lpfc_sli4_mbx_read_fcf_rec - Allocate and construct read fcf mbox cmd
1833 * @phba: pointer to lpfc hba data structure.
1834 * @fcf_index: index to fcf table.
1836 * This routine routine allocates and constructs non-embedded mailbox command
1837 * for reading a FCF table entry referred by @fcf_index.
1839 * Return: pointer to the mailbox command constructed if successful, otherwise
1840 * NULL.
1843 lpfc_sli4_mbx_read_fcf_rec(struct lpfc_hba *phba,
1844 struct lpfcMboxq *mboxq,
1845 uint16_t fcf_index)
1847 void *virt_addr;
1848 dma_addr_t phys_addr;
1849 uint8_t *bytep;
1850 struct lpfc_mbx_sge sge;
1851 uint32_t alloc_len, req_len;
1852 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1854 if (!mboxq)
1855 return -ENOMEM;
1857 req_len = sizeof(struct fcf_record) +
1858 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
1860 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
1861 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1862 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
1863 LPFC_SLI4_MBX_NEMBED);
1865 if (alloc_len < req_len) {
1866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1867 "0291 Allocated DMA memory size (x%x) is "
1868 "less than the requested DMA memory "
1869 "size (x%x)\n", alloc_len, req_len);
1870 return -ENOMEM;
1873 /* Get the first SGE entry from the non-embedded DMA memory. This
1874 * routine only uses a single SGE.
1876 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1877 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1878 virt_addr = mboxq->sge_array->addr[0];
1879 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1881 /* Set up command fields */
1882 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
1883 /* Perform necessary endian conversion */
1884 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1885 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
1887 return 0;
1891 * lpfc_request_features: Configure SLI4 REQUEST_FEATURES mailbox
1892 * @mboxq: pointer to lpfc mbox command.
1894 * This routine sets up the mailbox for an SLI4 REQUEST_FEATURES
1895 * mailbox command.
1897 void
1898 lpfc_request_features(struct lpfc_hba *phba, struct lpfcMboxq *mboxq)
1900 /* Set up SLI4 mailbox command header fields */
1901 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
1902 bf_set(lpfc_mqe_command, &mboxq->u.mqe, MBX_SLI4_REQ_FTRS);
1904 /* Set up host requested features. */
1905 bf_set(lpfc_mbx_rq_ftr_rq_fcpi, &mboxq->u.mqe.un.req_ftrs, 1);
1906 bf_set(lpfc_mbx_rq_ftr_rq_perfh, &mboxq->u.mqe.un.req_ftrs, 1);
1908 /* Enable DIF (block guard) only if configured to do so. */
1909 if (phba->cfg_enable_bg)
1910 bf_set(lpfc_mbx_rq_ftr_rq_dif, &mboxq->u.mqe.un.req_ftrs, 1);
1912 /* Enable NPIV only if configured to do so. */
1913 if (phba->max_vpi && phba->cfg_enable_npiv)
1914 bf_set(lpfc_mbx_rq_ftr_rq_npiv, &mboxq->u.mqe.un.req_ftrs, 1);
1916 return;
1920 * lpfc_init_vfi - Initialize the INIT_VFI mailbox command
1921 * @mbox: pointer to lpfc mbox command to initialize.
1922 * @vport: Vport associated with the VF.
1924 * This routine initializes @mbox to all zeros and then fills in the mailbox
1925 * fields from @vport. INIT_VFI configures virtual fabrics identified by VFI
1926 * in the context of an FCF. The driver issues this command to setup a VFI
1927 * before issuing a FLOGI to login to the VSAN. The driver should also issue a
1928 * REG_VFI after a successful VSAN login.
1930 void
1931 lpfc_init_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
1933 struct lpfc_mbx_init_vfi *init_vfi;
1935 memset(mbox, 0, sizeof(*mbox));
1936 mbox->vport = vport;
1937 init_vfi = &mbox->u.mqe.un.init_vfi;
1938 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VFI);
1939 bf_set(lpfc_init_vfi_vr, init_vfi, 1);
1940 bf_set(lpfc_init_vfi_vt, init_vfi, 1);
1941 bf_set(lpfc_init_vfi_vp, init_vfi, 1);
1942 bf_set(lpfc_init_vfi_vfi, init_vfi, vport->vfi + vport->phba->vfi_base);
1943 bf_set(lpfc_init_vpi_vpi, init_vfi, vport->vpi + vport->phba->vpi_base);
1944 bf_set(lpfc_init_vfi_fcfi, init_vfi, vport->phba->fcf.fcfi);
1948 * lpfc_reg_vfi - Initialize the REG_VFI mailbox command
1949 * @mbox: pointer to lpfc mbox command to initialize.
1950 * @vport: vport associated with the VF.
1951 * @phys: BDE DMA bus address used to send the service parameters to the HBA.
1953 * This routine initializes @mbox to all zeros and then fills in the mailbox
1954 * fields from @vport, and uses @buf as a DMAable buffer to send the vport's
1955 * fc service parameters to the HBA for this VFI. REG_VFI configures virtual
1956 * fabrics identified by VFI in the context of an FCF.
1958 void
1959 lpfc_reg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport, dma_addr_t phys)
1961 struct lpfc_mbx_reg_vfi *reg_vfi;
1963 memset(mbox, 0, sizeof(*mbox));
1964 reg_vfi = &mbox->u.mqe.un.reg_vfi;
1965 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_VFI);
1966 bf_set(lpfc_reg_vfi_vp, reg_vfi, 1);
1967 bf_set(lpfc_reg_vfi_vfi, reg_vfi, vport->vfi + vport->phba->vfi_base);
1968 bf_set(lpfc_reg_vfi_fcfi, reg_vfi, vport->phba->fcf.fcfi);
1969 bf_set(lpfc_reg_vfi_vpi, reg_vfi, vport->vpi + vport->phba->vpi_base);
1970 memcpy(reg_vfi->wwn, &vport->fc_portname, sizeof(struct lpfc_name));
1971 reg_vfi->wwn[0] = cpu_to_le32(reg_vfi->wwn[0]);
1972 reg_vfi->wwn[1] = cpu_to_le32(reg_vfi->wwn[1]);
1973 reg_vfi->e_d_tov = vport->phba->fc_edtov;
1974 reg_vfi->r_a_tov = vport->phba->fc_ratov;
1975 reg_vfi->bde.addrHigh = putPaddrHigh(phys);
1976 reg_vfi->bde.addrLow = putPaddrLow(phys);
1977 reg_vfi->bde.tus.f.bdeSize = sizeof(vport->fc_sparam);
1978 reg_vfi->bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1979 bf_set(lpfc_reg_vfi_nport_id, reg_vfi, vport->fc_myDID);
1983 * lpfc_init_vpi - Initialize the INIT_VPI mailbox command
1984 * @phba: pointer to the hba structure to init the VPI for.
1985 * @mbox: pointer to lpfc mbox command to initialize.
1986 * @vpi: VPI to be initialized.
1988 * The INIT_VPI mailbox command supports virtual N_Ports. The driver uses the
1989 * command to activate a virtual N_Port. The HBA assigns a MAC address to use
1990 * with the virtual N Port. The SLI Host issues this command before issuing a
1991 * FDISC to connect to the Fabric. The SLI Host should issue a REG_VPI after a
1992 * successful virtual NPort login.
1994 void
1995 lpfc_init_vpi(struct lpfc_hba *phba, struct lpfcMboxq *mbox, uint16_t vpi)
1997 memset(mbox, 0, sizeof(*mbox));
1998 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VPI);
1999 bf_set(lpfc_init_vpi_vpi, &mbox->u.mqe.un.init_vpi,
2000 vpi + phba->vpi_base);
2001 bf_set(lpfc_init_vpi_vfi, &mbox->u.mqe.un.init_vpi,
2002 phba->pport->vfi + phba->vfi_base);
2006 * lpfc_unreg_vfi - Initialize the UNREG_VFI mailbox command
2007 * @mbox: pointer to lpfc mbox command to initialize.
2008 * @vport: vport associated with the VF.
2010 * The UNREG_VFI mailbox command causes the SLI Host to put a virtual fabric
2011 * (logical NPort) into the inactive state. The SLI Host must have logged out
2012 * and unregistered all remote N_Ports to abort any activity on the virtual
2013 * fabric. The SLI Port posts the mailbox response after marking the virtual
2014 * fabric inactive.
2016 void
2017 lpfc_unreg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
2019 memset(mbox, 0, sizeof(*mbox));
2020 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_VFI);
2021 bf_set(lpfc_unreg_vfi_vfi, &mbox->u.mqe.un.unreg_vfi,
2022 vport->vfi + vport->phba->vfi_base);
2026 * lpfc_dump_fcoe_param - Dump config region 23 to get FCoe parameters.
2027 * @phba: pointer to the hba structure containing.
2028 * @mbox: pointer to lpfc mbox command to initialize.
2030 * This function create a SLI4 dump mailbox command to dump FCoE
2031 * parameters stored in region 23.
2034 lpfc_dump_fcoe_param(struct lpfc_hba *phba,
2035 struct lpfcMboxq *mbox)
2037 struct lpfc_dmabuf *mp = NULL;
2038 MAILBOX_t *mb;
2040 memset(mbox, 0, sizeof(*mbox));
2041 mb = &mbox->u.mb;
2043 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2044 if (mp)
2045 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2047 if (!mp || !mp->virt) {
2048 kfree(mp);
2049 /* dump_fcoe_param failed to allocate memory */
2050 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2051 "2569 lpfc_dump_fcoe_param: memory"
2052 " allocation failed\n");
2053 return 1;
2056 memset(mp->virt, 0, LPFC_BPL_SIZE);
2057 INIT_LIST_HEAD(&mp->list);
2059 /* save address for completion */
2060 mbox->context1 = (uint8_t *) mp;
2062 mb->mbxCommand = MBX_DUMP_MEMORY;
2063 mb->un.varDmp.type = DMP_NV_PARAMS;
2064 mb->un.varDmp.region_id = DMP_REGION_23;
2065 mb->un.varDmp.sli4_length = DMP_RGN23_SIZE;
2066 mb->un.varWords[3] = putPaddrLow(mp->phys);
2067 mb->un.varWords[4] = putPaddrHigh(mp->phys);
2068 return 0;
2072 * lpfc_reg_fcfi - Initialize the REG_FCFI mailbox command
2073 * @phba: pointer to the hba structure containing the FCF index and RQ ID.
2074 * @mbox: pointer to lpfc mbox command to initialize.
2076 * The REG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs). The
2077 * SLI Host uses the command to activate an FCF after it has acquired FCF
2078 * information via a READ_FCF mailbox command. This mailbox command also is used
2079 * to indicate where received unsolicited frames from this FCF will be sent. By
2080 * default this routine will set up the FCF to forward all unsolicited frames
2081 * the the RQ ID passed in the @phba. This can be overridden by the caller for
2082 * more complicated setups.
2084 void
2085 lpfc_reg_fcfi(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2087 struct lpfc_mbx_reg_fcfi *reg_fcfi;
2089 memset(mbox, 0, sizeof(*mbox));
2090 reg_fcfi = &mbox->u.mqe.un.reg_fcfi;
2091 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI);
2092 bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi, phba->sli4_hba.hdr_rq->queue_id);
2093 bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi, REG_FCF_INVALID_QID);
2094 bf_set(lpfc_reg_fcfi_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
2095 bf_set(lpfc_reg_fcfi_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
2096 bf_set(lpfc_reg_fcfi_info_index, reg_fcfi,
2097 phba->fcf.current_rec.fcf_indx);
2098 /* reg_fcf addr mode is bit wise inverted value of fcf addr_mode */
2099 bf_set(lpfc_reg_fcfi_mam, reg_fcfi, (~phba->fcf.addr_mode) & 0x3);
2100 if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
2101 bf_set(lpfc_reg_fcfi_vv, reg_fcfi, 1);
2102 bf_set(lpfc_reg_fcfi_vlan_tag, reg_fcfi,
2103 phba->fcf.current_rec.vlan_id);
2108 * lpfc_unreg_fcfi - Initialize the UNREG_FCFI mailbox command
2109 * @mbox: pointer to lpfc mbox command to initialize.
2110 * @fcfi: FCFI to be unregistered.
2112 * The UNREG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs).
2113 * The SLI Host uses the command to inactivate an FCFI.
2115 void
2116 lpfc_unreg_fcfi(struct lpfcMboxq *mbox, uint16_t fcfi)
2118 memset(mbox, 0, sizeof(*mbox));
2119 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_FCFI);
2120 bf_set(lpfc_unreg_fcfi, &mbox->u.mqe.un.unreg_fcfi, fcfi);
2124 * lpfc_resume_rpi - Initialize the RESUME_RPI mailbox command
2125 * @mbox: pointer to lpfc mbox command to initialize.
2126 * @ndlp: The nodelist structure that describes the RPI to resume.
2128 * The RESUME_RPI mailbox command is used to restart I/O to an RPI after a
2129 * link event.
2131 void
2132 lpfc_resume_rpi(struct lpfcMboxq *mbox, struct lpfc_nodelist *ndlp)
2134 struct lpfc_mbx_resume_rpi *resume_rpi;
2136 memset(mbox, 0, sizeof(*mbox));
2137 resume_rpi = &mbox->u.mqe.un.resume_rpi;
2138 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_RESUME_RPI);
2139 bf_set(lpfc_resume_rpi_index, resume_rpi, ndlp->nlp_rpi);
2140 bf_set(lpfc_resume_rpi_ii, resume_rpi, RESUME_INDEX_RPI);
2141 resume_rpi->event_tag = ndlp->phba->fc_eventTag;
2145 * lpfc_supported_pages - Initialize the PORT_CAPABILITIES supported pages
2146 * mailbox command.
2147 * @mbox: pointer to lpfc mbox command to initialize.
2149 * The PORT_CAPABILITIES supported pages mailbox command is issued to
2150 * retrieve the particular feature pages supported by the port.
2152 void
2153 lpfc_supported_pages(struct lpfcMboxq *mbox)
2155 struct lpfc_mbx_supp_pages *supp_pages;
2157 memset(mbox, 0, sizeof(*mbox));
2158 supp_pages = &mbox->u.mqe.un.supp_pages;
2159 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_PORT_CAPABILITIES);
2160 bf_set(cpn, supp_pages, LPFC_SUPP_PAGES);
2164 * lpfc_pc_sli4_params - Initialize the PORT_CAPABILITIES SLI4 Params mbox cmd.
2165 * @mbox: pointer to lpfc mbox command to initialize.
2167 * The PORT_CAPABILITIES SLI4 parameters mailbox command is issued to
2168 * retrieve the particular SLI4 features supported by the port.
2170 void
2171 lpfc_pc_sli4_params(struct lpfcMboxq *mbox)
2173 struct lpfc_mbx_pc_sli4_params *sli4_params;
2175 memset(mbox, 0, sizeof(*mbox));
2176 sli4_params = &mbox->u.mqe.un.sli4_params;
2177 bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_PORT_CAPABILITIES);
2178 bf_set(cpn, sli4_params, LPFC_SLI4_PARAMETERS);