1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * http://www.opensource.org/licenses/cddl1.txt.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2004-2012 Emulex. All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <emlxs.h>
28 
29 /* Required for EMLXS_CONTEXT in EMLXS_MSGF calls */
30 EMLXS_MSG_DEF(EMLXS_DOWNLOAD_C);
31 
32 #define	MAX_BOOTID	10
33 
34 static uint32_t	emlxs_erase_fcode_flash(emlxs_hba_t *hba);
35 
36 static uint32_t	emlxs_write_fcode_flash(emlxs_hba_t *hba,
37 			PIMAGE_HDR ImageHdr, caddr_t Buffer);
38 
39 static int32_t	emlxs_build_parms(caddr_t Buffer, PWAKE_UP_PARMS AbsWakeUpParms,
40 			uint32_t BufferSize, PAIF_HDR AifHeader);
41 static uint32_t	emlxs_validate_image(emlxs_hba_t *hba, caddr_t Buffer,
42 			uint32_t Size, emlxs_fw_image_t *fw_image);
43 static void	emlxs_format_dump(emlxs_hba_t *hba, MAILBOXQ *mbq,
44 			uint32_t Type, uint32_t RegionId, uint32_t WordCnt,
45 			uint32_t BaseAddr);
46 static uint32_t	emlxs_start_abs_download(emlxs_hba_t *hba, PAIF_HDR AifHdr,
47 			caddr_t Buffer, uint32_t len,
48 			PWAKE_UP_PARMS WakeUpParms);
49 static uint32_t	emlxs_start_abs_download_2mb(emlxs_hba_t *hba, caddr_t buffer,
50 			uint32_t len, uint32_t offline,
51 			emlxs_fw_image_t *fw_image);
52 static uint32_t	emlxs_proc_abs_2mb(emlxs_hba_t *hba,
53 			caddr_t EntireBuffer, uint32_t FileType,
54 			uint32_t extType);
55 static void	emlxs_format_load_area_cmd(MAILBOXQ *mbq, uint32_t Base,
56 			uint32_t DlByteCount, uint32_t Function,
57 			uint32_t Complete, uint32_t DataOffset, uint32_t AreaId,
58 			uint8_t MbxCmd, uint32_t StepCmd);
59 static uint32_t	emlxs_build_parms_2mb_bwc(emlxs_hba_t *hba, PAIF_HDR AifHdr,
60 			uint32_t extType, PWAKE_UP_PARMS AbsWakeUpParms);
61 static uint32_t	emlxs_update_exp_rom(emlxs_hba_t *hba,
62 			PWAKE_UP_PARMS WakeUpParms);
63 extern uint32_t	emlxs_get_max_sram(emlxs_hba_t *hba, uint32_t *MaxRbusSize,
64 			uint32_t *MaxIbusSize);
65 static void	emlxs_format_prog_flash(MAILBOXQ *mbq, uint32_t Base,
66 			uint32_t DlByteCount, uint32_t Function,
67 			uint32_t Complete, uint32_t BdeAddress,
68 			uint32_t BdeSize, PROG_ID *ProgId, uint32_t keep);
69 static void	emlxs_format_update_parms(MAILBOXQ *mbq,
70 			PWAKE_UP_PARMS WakeUpParms);
71 static void	emlxs_format_update_pci_cfg(emlxs_hba_t *hba, MAILBOXQ *mbq,
72 			uint32_t region_id, uint32_t size);
73 static uint32_t	emlxs_update_wakeup_parms(emlxs_hba_t *hba,
74 			PWAKE_UP_PARMS AbsWakeUpParms,
75 			PWAKE_UP_PARMS WakeUpParms);
76 static uint32_t	emlxs_update_boot_wakeup_parms(emlxs_hba_t *hba,
77 			PWAKE_UP_PARMS WakeUpParms, PROG_ID *id,
78 			uint32_t proc_erom);
79 static uint32_t	emlxs_update_ff_wakeup_parms(emlxs_hba_t *hba,
80 			PWAKE_UP_PARMS WakeUpParms, PROG_ID *id);
81 static uint32_t	emlxs_update_sli1_wakeup_parms(emlxs_hba_t *hba,
82 			PWAKE_UP_PARMS WakeUpParms, PROG_ID *id);
83 static uint32_t	emlxs_update_sli2_wakeup_parms(emlxs_hba_t *hba,
84 			PWAKE_UP_PARMS WakeUpParms, PROG_ID *id);
85 static uint32_t	emlxs_update_sli3_wakeup_parms(emlxs_hba_t *hba,
86 			PWAKE_UP_PARMS WakeUpParms, PROG_ID *id);
87 static uint32_t	emlxs_update_sli4_wakeup_parms(emlxs_hba_t *hba,
88 			PWAKE_UP_PARMS WakeUpParms, PROG_ID *id);
89 static uint32_t	emlxs_start_rel_download(emlxs_hba_t *hba, PIMAGE_HDR ImageHdr,
90 			caddr_t Buffer, PWAKE_UP_PARMS WakeUpParms,
91 			uint32_t dwc_flag);
92 static uint32_t	emlxs_read_load_list(emlxs_hba_t *hba, LOAD_LIST *LoadList);
93 
94 static uint32_t	emlxs_valid_cksum(uint32_t *StartAddr, uint32_t *EndAddr);
95 
96 static void	emlxs_disp_aif_header(emlxs_hba_t *hba, PAIF_HDR AifHdr);
97 
98 static void	emlxs_dump_image_header(emlxs_hba_t *hba, PIMAGE_HDR image);
99 
100 
101 static uint32_t	emlxs_type_check(uint32_t type);
102 
103 static uint32_t	emlxs_kern_check(emlxs_hba_t *hba, uint32_t version);
104 
105 static uint32_t	emlxs_stub_check(emlxs_hba_t *hba, uint32_t version);
106 
107 static uint32_t	emlxs_sli1_check(emlxs_hba_t *hba, uint32_t version);
108 
109 static uint32_t	emlxs_sli2_check(emlxs_hba_t *hba, uint32_t version);
110 
111 static uint32_t	emlxs_sli3_check(emlxs_hba_t *hba, uint32_t version);
112 
113 static uint32_t	emlxs_sli4_check(emlxs_hba_t *hba, uint32_t version);
114 
115 static uint32_t	emlxs_bios_check(emlxs_hba_t *hba, uint32_t version);
116 
117 static uint32_t	emlxs_sbus_fcode_check(emlxs_hba_t *hba, uint32_t version);
118 
119 static uint32_t	emlxs_validate_version(emlxs_hba_t *hba,
120 			emlxs_fw_file_t *file, uint32_t id, uint32_t type,
121 			char *file_type);
122 static uint32_t emlxs_be2_validate_image(emlxs_hba_t *hba, caddr_t buffer,
123 			uint32_t len, emlxs_be_fw_image_t *fw_image);
124 static uint32_t emlxs_be3_validate_image(emlxs_hba_t *hba, caddr_t buffer,
125 			uint32_t len, emlxs_be_fw_image_t *fw_image);
126 
127 
128 static int32_t emlxs_be_verify_phy(emlxs_hba_t *hba, emlxs_be_fw_file_t *file,
129 			MAILBOXQ *mbq, MATCHMAP *mp);
130 static int32_t emlxs_be_verify_crc(emlxs_hba_t *hba,
131 			emlxs_be_fw_file_t *file,
132 			MAILBOXQ *mbq, MATCHMAP *mp);
133 static int32_t emlxs_be_flash_image(emlxs_hba_t *hba, caddr_t buffer,
134 			emlxs_be_fw_file_t *file, MAILBOXQ *mbq, MATCHMAP *mp);
135 static int32_t emlxs_be_fw_download(emlxs_hba_t *hba, caddr_t buffer,
136 			uint32_t len, uint32_t offline);
137 static int32_t emlxs_obj_fw_download(emlxs_hba_t *hba, caddr_t buffer,
138 			uint32_t len, uint32_t offline);
139 static int32_t emlxs_obj_flash_image(emlxs_hba_t *hba, caddr_t buffer,
140 			uint32_t size, MAILBOXQ *mbq, MATCHMAP *mp,
141 			uint32_t *change_status);
142 static uint32_t emlxs_obj_validate_image(emlxs_hba_t *hba, caddr_t buffer,
143 			uint32_t len, emlxs_obj_header_t *obj_hdr);
144 static uint32_t emlxs_be_version(caddr_t buffer, uint32_t size,
145 			uint32_t *plus_flag);
146 static uint32_t emlxs_proc_rel_2mb(emlxs_hba_t *hba, caddr_t buffer,
147 			emlxs_fw_image_t *fw_image);
148 static uint32_t emlxs_delete_load_entry(emlxs_hba_t *hba, PROG_ID *progId);
149 
150 static void emlxs_verify_image(emlxs_hba_t *hba, emlxs_fw_image_t *image);
151 
152 static uint32_t emlxs_clean_flash(emlxs_hba_t *hba,
153 			PWAKE_UP_PARMS OldWakeUpParms,
154 			PWAKE_UP_PARMS NewWakeUpParms);
155 
156 /* ************************************************************************* */
157 
158 extern int32_t
emlxs_fw_download(emlxs_hba_t * hba,caddr_t buffer,uint32_t len,uint32_t offline)159 emlxs_fw_download(emlxs_hba_t *hba, caddr_t buffer, uint32_t len,
160     uint32_t offline)
161 {
162 	emlxs_port_t *port = &PPORT;
163 	uint32_t *Uptr;
164 	IMAGE_HDR ImageHdr;
165 	AIF_HDR AifHdr;
166 	uint32_t ImageType;
167 	WAKE_UP_PARMS WakeUpParms;
168 	uint32_t rval = 0;
169 	emlxs_fw_image_t fw_image;
170 	uint32_t i;
171 
172 #ifdef EMLXS_LITTLE_ENDIAN
173 	caddr_t local_buffer;
174 	uint32_t *bptr1;
175 	uint32_t *bptr2;
176 #endif /* EMLXS_LITTLE_ENDIAN */
177 
178 	if (hba->sli_mode == EMLXS_HBA_SLI4_MODE) {
179 		if (hba->model_info.chip & EMLXS_BE_CHIPS) {
180 			rval = emlxs_be_fw_download(hba, buffer, len, offline);
181 		} else {
182 			rval = emlxs_obj_fw_download(hba, buffer, len, offline);
183 		}
184 		return (rval);
185 	}
186 
187 	if (buffer == NULL || len == 0) {
188 		return (EMLXS_IMAGE_BAD);
189 	}
190 
191 #ifdef EMLXS_LITTLE_ENDIAN
192 	/* We need to swap the image buffer before we start */
193 
194 	/*
195 	 * Use KM_SLEEP to allocate a temporary buffer
196 	 */
197 	local_buffer = (caddr_t)kmem_zalloc(len, KM_SLEEP);
198 
199 	/* Perform a 32 bit swap of the image */
200 	bptr1 = (uint32_t *)local_buffer;
201 	bptr2 = (uint32_t *)buffer;
202 	for (i = 0; i < (len / 4); i++) {
203 		*bptr1 = LE_SWAP32(*bptr2);
204 		bptr1++;
205 		bptr2++;
206 	}
207 
208 	/* Replace the original buffer */
209 	buffer = local_buffer;
210 #endif /* EMLXS_LITTLE_ENDIAN */
211 
212 	bzero(&fw_image, sizeof (emlxs_fw_image_t));
213 	for (i = 0; i < MAX_PROG_TYPES; i++) {
214 		(void) strlcpy(fw_image.prog[i].label, "none",
215 		    sizeof (fw_image.prog[i].label));
216 	}
217 
218 	/* Validate image */
219 	if ((rval = emlxs_validate_image(hba, buffer, len, &fw_image))) {
220 		goto done;
221 	}
222 
223 	/* Verify image */
224 	emlxs_verify_image(hba, &fw_image);
225 
226 	/* Get image type */
227 	Uptr = (uint32_t *)buffer;
228 	ImageType = *Uptr;
229 
230 	/*
231 	 * Pegasus and beyond FW download is done differently
232 	 * for absolute download.
233 	 */
234 
235 	/* Check for absolute image */
236 	if ((ImageType == NOP_IMAGE_TYPE) &&
237 	    !(hba->model_info.chip &
238 	    (EMLXS_DRAGONFLY_CHIP | EMLXS_CENTAUR_CHIP))) {
239 		/*
240 		 * Because 2Mb flash download file format is different from
241 		 * 512k, it needs to be handled differently
242 		 */
243 		if (rval = emlxs_start_abs_download_2mb(hba, buffer, len,
244 		    offline, &fw_image)) {
245 			goto done;
246 		}
247 
248 		/* Offline already handled */
249 		offline = 0;
250 
251 		goto SLI_DOWNLOAD_EXIT;
252 	}
253 
254 	/* Pre-pegasus adapters only */
255 
256 	/* Initialize headers */
257 	if (ImageType == NOP_IMAGE_TYPE) {
258 		bcopy(buffer, &AifHdr, sizeof (AIF_HDR));
259 		bzero((void *)&ImageHdr, sizeof (IMAGE_HDR));
260 	} else { /* PRG file */
261 		bzero((void *)&AifHdr, sizeof (AIF_HDR));
262 		bcopy(buffer, &ImageHdr, sizeof (IMAGE_HDR));
263 	}
264 
265 	/* Everything checks out, now to just do it */
266 
267 	if (offline) {
268 		if (emlxs_offline(hba, 0) != FC_SUCCESS) {
269 			offline = 0;
270 
271 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
272 			    "Unable to take adapter offline.");
273 
274 			rval = EMLXS_OFFLINE_FAILED;
275 			goto SLI_DOWNLOAD_EXIT;
276 		}
277 
278 		if (EMLXS_SLI_HBA_RESET(hba, 1, 1, 0) != FC_SUCCESS) {
279 			offline = 0;
280 
281 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
282 			    "Unable to restart adapter.");
283 
284 			rval = EMLXS_OFFLINE_FAILED;
285 			goto SLI_DOWNLOAD_EXIT;
286 		}
287 	}
288 
289 	/* Pre-pegasus adapters */
290 
291 	if (ImageHdr.Id.Type == SBUS_FCODE) {
292 		/* Erase Flash */
293 		if (emlxs_erase_fcode_flash(hba)) {
294 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
295 			    "Unable to erase flash.");
296 
297 			rval = EMLXS_IMAGE_FAILED;
298 			goto SLI_DOWNLOAD_EXIT;
299 		}
300 
301 		/* Write FCODE */
302 		if (emlxs_write_fcode_flash(hba, &ImageHdr, buffer)) {
303 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
304 			    "Unable to write flash.");
305 
306 			rval = EMLXS_IMAGE_FAILED;
307 			goto SLI_DOWNLOAD_EXIT;
308 		}
309 
310 		goto SLI_DOWNLOAD_EXIT;
311 	}
312 
313 	/* Pre-pegasus PCI adapters */
314 
315 	if (emlxs_read_wakeup_parms(hba, &WakeUpParms, 1)) {
316 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
317 		    "Unable to get parameters.");
318 
319 		rval = EMLXS_IMAGE_FAILED;
320 
321 		goto SLI_DOWNLOAD_EXIT;
322 	}
323 
324 	if (ImageType == NOP_IMAGE_TYPE) {
325 		if (emlxs_start_abs_download(hba, &AifHdr,
326 		    buffer, len, &WakeUpParms)) {
327 			EMLXS_MSGF(EMLXS_CONTEXT,
328 			    &emlxs_download_failed_msg,
329 			    "Failed to program flash.");
330 
331 			rval = EMLXS_IMAGE_FAILED;
332 
333 			goto SLI_DOWNLOAD_EXIT;
334 		}
335 
336 	} else { /* Relative PRG file */
337 		if (emlxs_start_rel_download(hba, &ImageHdr, buffer,
338 		    &WakeUpParms, 0)) {
339 			EMLXS_MSGF(EMLXS_CONTEXT,
340 			    &emlxs_download_failed_msg,
341 			    "Failed to program flash.");
342 
343 			rval = EMLXS_IMAGE_FAILED;
344 
345 			goto SLI_DOWNLOAD_EXIT;
346 		}
347 	}
348 
349 SLI_DOWNLOAD_EXIT:
350 
351 	if (offline) {
352 		(void) emlxs_online(hba);
353 	}
354 
355 	if (rval == 0) {
356 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_complete_msg,
357 		    "Status good.");
358 	}
359 
360 done:
361 
362 #ifdef EMLXS_LITTLE_ENDIAN
363 	/* Free the local buffer */
364 	kmem_free(local_buffer, len);
365 #endif /* EMLXS_LITTLE_ENDIAN */
366 
367 	return (rval);
368 
369 } /* emlxs_fw_download */
370 
371 
372 extern void
emlxs_memset(uint8_t * buffer,uint8_t value,uint32_t size)373 emlxs_memset(uint8_t *buffer, uint8_t value, uint32_t size)
374 {
375 	while (size--) {
376 		*buffer++ = value;
377 	}
378 
379 } /* emlxs_memset () */
380 
381 
382 static int32_t
emlxs_be_flash_image(emlxs_hba_t * hba,caddr_t buffer,emlxs_be_fw_file_t * file,MAILBOXQ * mbq,MATCHMAP * mp)383 emlxs_be_flash_image(emlxs_hba_t *hba, caddr_t buffer,
384     emlxs_be_fw_file_t *file, MAILBOXQ *mbq, MATCHMAP *mp)
385 {
386 	emlxs_port_t *port = &PPORT;
387 	uint8_t *image_ptr;
388 	uint32_t *wptr;
389 	uint8_t *payload;
390 	MAILBOX4 *mb;
391 	IOCTL_COMMON_FLASHROM *flashrom;
392 	mbox_req_hdr_t	*hdr_req;
393 	uint32_t	image_size;
394 	uint32_t	block_size;
395 	uint32_t	xfer_size;
396 	uint32_t	block_offset;
397 	uint32_t	count;
398 	uint32_t	rval = 0;
399 
400 	if (file->image_size == 0) {
401 		return (0);
402 	}
403 
404 	image_ptr  = (uint8_t *)buffer + file->image_offset;
405 	image_size = file->image_size;
406 	block_size = file->block_size;
407 	block_offset = 0;
408 	mb = (MAILBOX4*)mbq;
409 
410 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
411 	    "%s: Downloading...", file->label);
412 
413 	while (block_size) {
414 		bzero((void *) mb, MAILBOX_CMD_SLI4_BSIZE);
415 		bzero((void *) mp->virt, mp->size);
416 
417 		xfer_size = min(BE_MAX_XFER_SIZE, block_size);
418 
419 		mb->un.varSLIConfig.be.embedded = 0;
420 		mbq->nonembed = (void *)mp;
421 		mbq->mbox_cmpl = NULL;
422 
423 		mb->mbxCommand = MBX_SLI_CONFIG;
424 		mb->mbxOwner = OWN_HOST;
425 
426 		hdr_req = (mbox_req_hdr_t *)mp->virt;
427 		hdr_req->subsystem = IOCTL_SUBSYSTEM_COMMON;
428 		hdr_req->opcode = COMMON_OPCODE_WRITE_FLASHROM;
429 		hdr_req->timeout = 0;
430 		hdr_req->req_length = sizeof (IOCTL_COMMON_FLASHROM) +
431 		    xfer_size;
432 
433 		flashrom = (IOCTL_COMMON_FLASHROM *)(hdr_req + 1);
434 
435 		if (file->type == MGMT_FLASHROM_OPTYPE_PHY_FIRMWARE) {
436 			flashrom->params.opcode = ((block_size == xfer_size)?
437 			    MGMT_PHY_FLASHROM_OPCODE_FLASH:
438 			    MGMT_PHY_FLASHROM_OPCODE_SAVE);
439 			flashrom->params.optype = 0; /* ignored */
440 		} else {
441 			flashrom->params.opcode = ((block_size == xfer_size)?
442 			    MGMT_FLASHROM_OPCODE_FLASH:
443 			    MGMT_FLASHROM_OPCODE_SAVE);
444 			flashrom->params.optype = file->type;
445 		}
446 
447 		flashrom->params.data_buffer_size = xfer_size;
448 		flashrom->params.offset = block_offset;
449 
450 		/* Build data buffer payload */
451 		payload = (uint8_t *)(&flashrom->params.data_buffer);
452 		emlxs_memset(payload, 0xff, xfer_size);
453 
454 		/* Copy remaining image into payload */
455 		if (image_size) {
456 			count = min(image_size, xfer_size);
457 			BE_SWAP32_BCOPY(image_ptr, payload, count);
458 			image_size -= count;
459 			image_ptr  += count;
460 		}
461 
462 		if ((flashrom->params.opcode == MGMT_FLASHROM_OPCODE_FLASH) ||
463 		    (flashrom->params.opcode ==
464 		    MGMT_PHY_FLASHROM_OPCODE_FLASH)) {
465 			wptr = (uint32_t *)&payload[(xfer_size - 12)];
466 
467 			wptr[0] = file->load_address;
468 			wptr[1] = file->image_size;
469 			wptr[2] = file->block_crc;
470 		}
471 
472 		/* Send write request */
473 		if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbq, MBX_WAIT, 0) !=
474 		    MBX_SUCCESS) {
475 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
476 			    "%s: Unable to download image. status=%x",
477 			    file->label, mb->mbxStatus);
478 			rval = EMLXS_IMAGE_FAILED;
479 			goto done;
480 		}
481 
482 		block_size -= xfer_size;
483 		block_offset += xfer_size;
484 	}
485 
486 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
487 	    "%s: Download complete.", file->label);
488 done:
489 
490 	return (rval);
491 
492 } /* emlxs_be_flash_image() */
493 
494 
495 
496 
497 static int32_t
emlxs_be_verify_crc(emlxs_hba_t * hba,emlxs_be_fw_file_t * file,MAILBOXQ * mbq,MATCHMAP * mp)498 emlxs_be_verify_crc(emlxs_hba_t *hba,
499     emlxs_be_fw_file_t *file, MAILBOXQ *mbq, MATCHMAP *mp)
500 {
501 	emlxs_port_t *port = &PPORT;
502 	uint32_t *wptr;
503 	uint8_t *payload;
504 	MAILBOX4 *mb;
505 	IOCTL_COMMON_FLASHROM *flashrom;
506 	mbox_req_hdr_t	*hdr_req;
507 	uint32_t	xfer_size;
508 	uint32_t	block_offset;
509 	uint32_t	rval = 0;
510 	uint32_t	value;
511 
512 	if (file->type == MGMT_FLASHROM_OPTYPE_PHY_FIRMWARE) {
513 		/* PHY Firmware can't be verified */
514 		return (1);
515 	}
516 
517 	xfer_size = 8;
518 	block_offset = file->block_size - xfer_size;
519 	mb = (MAILBOX4*)mbq;
520 
521 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
522 	    "%s: Verifying CRC...", file->label);
523 
524 	bzero((void *) mb, MAILBOX_CMD_SLI4_BSIZE);
525 	bzero((void *) mp->virt, mp->size);
526 
527 	mb->un.varSLIConfig.be.embedded = 0;
528 	mbq->nonembed = (void *)mp;
529 	mbq->mbox_cmpl = NULL;
530 
531 	mb->mbxCommand = MBX_SLI_CONFIG;
532 	mb->mbxOwner = OWN_HOST;
533 
534 	hdr_req = (mbox_req_hdr_t *)mp->virt;
535 	hdr_req->subsystem = IOCTL_SUBSYSTEM_COMMON;
536 	hdr_req->opcode = COMMON_OPCODE_READ_FLASHROM;
537 	hdr_req->timeout = 0;
538 	hdr_req->req_length = sizeof (IOCTL_COMMON_FLASHROM) +
539 	    xfer_size;
540 
541 	flashrom = (IOCTL_COMMON_FLASHROM *)(hdr_req + 1);
542 	flashrom->params.opcode = MGMT_FLASHROM_OPCODE_REPORT;
543 	flashrom->params.optype = file->type;
544 	flashrom->params.data_buffer_size = xfer_size;
545 	flashrom->params.offset = block_offset;
546 
547 	/* Send read request */
548 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbq, MBX_WAIT, 0) !=
549 	    MBX_SUCCESS) {
550 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
551 		    "%s: Unable to read CRC. status=%x",
552 		    file->label, mb->mbxStatus);
553 
554 		rval = 2;
555 		goto done;
556 	}
557 
558 	payload = (uint8_t *)(&flashrom->params.data_buffer);
559 	wptr = (uint32_t *)(payload + xfer_size - 8);
560 
561 	/* Verify image size */
562 	value = *wptr++;
563 	if (value != file->image_size) {
564 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
565 		    "%s: Image size mismatch. %08x != %08x",
566 		    file->label, value, file->image_size);
567 
568 		rval = 1;
569 		goto done;
570 	}
571 
572 	/* Verify block crc */
573 	value = *wptr;
574 	if (value != file->block_crc) {
575 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
576 		    "%s: CRC mismatch. %08x != %08x",
577 		    file->label, value, file->block_crc);
578 		rval = 1;
579 	}
580 
581 done:
582 
583 	if (rval == 0) {
584 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
585 		    "%s: CRC verified.", file->label);
586 	}
587 
588 	return (rval);
589 
590 } /* emlxs_be_verify_crc() */
591 
592 
593 static int32_t
emlxs_be_verify_phy(emlxs_hba_t * hba,emlxs_be_fw_file_t * file,MAILBOXQ * mbq,MATCHMAP * mp)594 emlxs_be_verify_phy(emlxs_hba_t *hba,
595     emlxs_be_fw_file_t *file, MAILBOXQ *mbq, MATCHMAP *mp)
596 {
597 	emlxs_port_t *port = &PPORT;
598 	MAILBOX4 *mb;
599 	IOCTL_COMMON_GET_PHY_DETAILS *phy;
600 	mbox_req_hdr_t	*hdr_req;
601 	uint32_t	rval = 0;
602 
603 	if (file->type != MGMT_FLASHROM_OPTYPE_PHY_FIRMWARE) {
604 		return (1);
605 	}
606 
607 	mb = (MAILBOX4*)mbq;
608 
609 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
610 	    "%s: Getting PHY Details...", file->label);
611 
612 	bzero((void *) mb, MAILBOX_CMD_SLI4_BSIZE);
613 	bzero((void *) mp->virt, mp->size);
614 
615 	mb->un.varSLIConfig.be.embedded = 0;
616 	mbq->nonembed = (void *)mp;
617 	mbq->mbox_cmpl = NULL;
618 
619 	mb->mbxCommand = MBX_SLI_CONFIG;
620 	mb->mbxOwner = OWN_HOST;
621 
622 	hdr_req = (mbox_req_hdr_t *)mp->virt;
623 	hdr_req->subsystem = IOCTL_SUBSYSTEM_COMMON;
624 	hdr_req->opcode = COMMON_OPCODE_GET_PHY_DETAILS;
625 	hdr_req->timeout = 0;
626 	hdr_req->req_length = sizeof (IOCTL_COMMON_GET_PHY_DETAILS);
627 
628 	phy = (IOCTL_COMMON_GET_PHY_DETAILS *)(hdr_req + 1);
629 
630 	/* Send read request */
631 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbq, MBX_WAIT, 0) !=
632 	    MBX_SUCCESS) {
633 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
634 		    "%s: Unable to get PHY details. status=%x",
635 		    file->label, mb->mbxStatus);
636 
637 		rval = 2;
638 		goto done;
639 	}
640 
641 	if ((phy->params.response.phy_type != PHY_TN_8022) ||
642 	    (phy->params.response.interface_type != BASET_10GB_TYPE)) {
643 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
644 		    "%s: PHY not applicable. %08x,%08x",
645 		    file->label,
646 		    phy->params.response.phy_type,
647 		    phy->params.response.interface_type);
648 
649 		rval = 1;
650 	}
651 
652 done:
653 
654 	if (rval == 0) {
655 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
656 		    "%s: PHY verified. %x,%x",
657 		    file->label,
658 		    phy->params.response.phy_type,
659 		    phy->params.response.interface_type);
660 	}
661 
662 	return (rval);
663 
664 } /* emlxs_be_verify_phy() */
665 
666 
667 extern int32_t
emlxs_be_read_fw_version(emlxs_hba_t * hba,emlxs_firmware_t * fw)668 emlxs_be_read_fw_version(emlxs_hba_t *hba, emlxs_firmware_t *fw)
669 {
670 	emlxs_port_t *port = &PPORT;
671 	MAILBOXQ *mbq = NULL;
672 	MATCHMAP *mp = NULL;
673 	MAILBOX4 *mb;
674 	uint32_t *wptr;
675 	uint8_t *payload;
676 	IOCTL_COMMON_FLASHROM *flashrom;
677 	mbox_req_hdr_t	*hdr_req;
678 	uint32_t	xfer_size;
679 	uint32_t	block_offset;
680 	uint32_t	rval = 0;
681 
682 	bzero((void *) fw, sizeof (emlxs_firmware_t));
683 
684 	if ((mbq = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
685 	    KM_SLEEP)) == NULL) {
686 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_sli_detail_msg,
687 		    "read_fw_version: Unable to allocate mailbox buffer.");
688 
689 		rval = 1;
690 		goto done;
691 	}
692 
693 	if ((mp = emlxs_mem_buf_alloc(hba, (sizeof (mbox_req_hdr_t) +
694 	    sizeof (IOCTL_COMMON_FLASHROM) + 32))) == NULL) {
695 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_sli_detail_msg,
696 		    "read_fw_version: Unable to allocate payload buffer.");
697 
698 		rval = EMLXS_IMAGE_FAILED;
699 		goto done;
700 	}
701 
702 	mb = (MAILBOX4*)mbq;
703 
704 	/* Read CRC and size */
705 	xfer_size = 8;
706 	block_offset = 0x140000 - xfer_size;
707 
708 	bzero((void *) mb, MAILBOX_CMD_SLI4_BSIZE);
709 	bzero((void *) mp->virt, mp->size);
710 
711 	mb->un.varSLIConfig.be.embedded = 0;
712 	mbq->nonembed = (void *)mp;
713 	mbq->mbox_cmpl = NULL;
714 
715 	mb->mbxCommand = MBX_SLI_CONFIG;
716 	mb->mbxOwner = OWN_HOST;
717 
718 	hdr_req = (mbox_req_hdr_t *)mp->virt;
719 	hdr_req->subsystem = IOCTL_SUBSYSTEM_COMMON;
720 	hdr_req->opcode = COMMON_OPCODE_READ_FLASHROM;
721 	hdr_req->timeout = 0;
722 	hdr_req->req_length = sizeof (IOCTL_COMMON_FLASHROM) +
723 	    xfer_size;
724 
725 	flashrom = (IOCTL_COMMON_FLASHROM *)(hdr_req + 1);
726 	flashrom->params.opcode = MGMT_FLASHROM_OPCODE_REPORT;
727 	flashrom->params.optype = MGMT_FLASHROM_OPTYPE_FCOE_FIRMWARE;
728 	flashrom->params.data_buffer_size = xfer_size;
729 	flashrom->params.offset = block_offset;
730 
731 	/* Send read request */
732 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbq, MBX_WAIT, 0) !=
733 	    MBX_SUCCESS) {
734 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_sli_detail_msg,
735 		    "read_fw_version: Unable to read CRC. status=%x",
736 		    mb->mbxStatus);
737 
738 		rval = 1;
739 		goto done;
740 	}
741 
742 	payload = (uint8_t *)(&flashrom->params.data_buffer);
743 
744 	wptr = (uint32_t *)payload;
745 	fw->size = *wptr++; /* image size */
746 	fw->sli4 = *wptr;   /* block crc */
747 	fw->kern = *wptr;
748 	fw->stub = *wptr;
749 
750 	/* Read version label */
751 	xfer_size = 32;
752 	block_offset = 0x30;
753 
754 	bzero((void *) mb, MAILBOX_CMD_SLI4_BSIZE);
755 	bzero((void *) mp->virt, mp->size);
756 
757 	mb->un.varSLIConfig.be.embedded = 0;
758 	mbq->nonembed = (void *)mp;
759 	mbq->mbox_cmpl = NULL;
760 
761 	mb->mbxCommand = MBX_SLI_CONFIG;
762 	mb->mbxOwner = OWN_HOST;
763 
764 	hdr_req = (mbox_req_hdr_t *)mp->virt;
765 	hdr_req->subsystem = IOCTL_SUBSYSTEM_COMMON;
766 	hdr_req->opcode = COMMON_OPCODE_READ_FLASHROM;
767 	hdr_req->timeout = 0;
768 	hdr_req->req_length = sizeof (IOCTL_COMMON_FLASHROM) +
769 	    xfer_size;
770 
771 	flashrom = (IOCTL_COMMON_FLASHROM *)(hdr_req + 1);
772 	flashrom->params.opcode = MGMT_FLASHROM_OPCODE_REPORT;
773 	flashrom->params.optype = MGMT_FLASHROM_OPTYPE_FCOE_FIRMWARE;
774 	flashrom->params.data_buffer_size = xfer_size;
775 	flashrom->params.offset = block_offset;
776 
777 	/* Send read request */
778 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbq, MBX_WAIT, 0) !=
779 	    MBX_SUCCESS) {
780 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_sli_detail_msg,
781 		    "read_fw_version: Unable to read version string. status=%x",
782 		    mb->mbxStatus);
783 
784 		rval = 1;
785 		goto done;
786 	}
787 
788 	payload = (uint8_t *)(&flashrom->params.data_buffer);
789 	BE_SWAP32_BCOPY(payload, (uint8_t *)fw->label, 32);
790 
791 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_sli_detail_msg,
792 	    "FCOE FIRMWARE: size=%x version=%s (0x%08x)",
793 	    fw->size, fw->label, fw->sli4);
794 
795 done:
796 
797 	if (mbq) {
798 		emlxs_mem_put(hba, MEM_MBOX, (void *)mbq);
799 	}
800 
801 	if (mp) {
802 		emlxs_mem_buf_free(hba, mp);
803 	}
804 
805 	return (rval);
806 
807 } /* emlxs_be_read_fw_version() */
808 
809 
810 static uint32_t
emlxs_be_version(caddr_t buffer,uint32_t size,uint32_t * plus_flag)811 emlxs_be_version(caddr_t buffer, uint32_t size, uint32_t *plus_flag)
812 {
813 	emlxs_be2_ufi_header_t *ufi_hdr;
814 	char signature[BE2_SIGNATURE_SIZE];
815 	uint32_t be_version = 0;
816 
817 	if (size < sizeof (emlxs_be2_ufi_header_t)) {
818 		return (0);
819 	}
820 	ufi_hdr = (emlxs_be2_ufi_header_t *)buffer;
821 
822 	(void) snprintf(signature, BE2_SIGNATURE_SIZE, "%s+", BE_SIGNATURE);
823 
824 	/* Check if this is a UFI image */
825 	if (strncmp(signature, (char *)ufi_hdr->signature,
826 	    strlen(BE_SIGNATURE)) != 0) {
827 		return (0);
828 	}
829 
830 	/* Check if this is a UFI plus image */
831 	if (plus_flag) {
832 		/* Check if this is a UFI plus image */
833 		if (strncmp(signature, (char *)ufi_hdr->signature,
834 		    strlen(BE_SIGNATURE)+1) == 0) {
835 			*plus_flag = 1;
836 		} else {
837 			*plus_flag = 0;
838 		}
839 	}
840 
841 	if ((ufi_hdr->build[0] >= '1') && (ufi_hdr->build[0] <= '9')) {
842 		be_version = ufi_hdr->build[0] - '0';
843 	}
844 
845 	return (be_version);
846 
847 } /* emlxs_be_version() */
848 
849 
850 static uint32_t
emlxs_be2_validate_image(emlxs_hba_t * hba,caddr_t buffer,uint32_t len,emlxs_be_fw_image_t * fw_image)851 emlxs_be2_validate_image(emlxs_hba_t *hba, caddr_t buffer,
852     uint32_t len, emlxs_be_fw_image_t *fw_image)
853 {
854 	emlxs_port_t *port = &PPORT;
855 	emlxs_be2_ufi_header_t *ufi_hdr;
856 	emlxs_be2_flash_dir_t *flash_dir;
857 	emlxs_be2_flash_entry_t *entry;
858 	uint8_t *bptr;
859 	uint32_t *wptr;
860 	uint32_t i;
861 	uint32_t k;
862 	uint32_t mask;
863 	uint32_t value;
864 	uint32_t image_size;
865 	emlxs_be_fw_file_t *file;
866 	emlxs_be_fw_file_t *file2;
867 	uint32_t ufi_plus = 0;
868 	uint32_t be_version = 0;
869 	uint32_t found;
870 
871 	bzero(fw_image, sizeof (emlxs_be_fw_image_t));
872 
873 	if (hba->model_info.chip != EMLXS_BE2_CHIP) {
874 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
875 		    "Invalid adapter model.");
876 		return (EMLXS_IMAGE_INCOMPATIBLE);
877 	}
878 
879 	if (len < (sizeof (emlxs_be2_ufi_header_t) +
880 	    sizeof (emlxs_be2_flash_dir_t))) {
881 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
882 		    "Image too small. (%d < %d)",
883 		    len, (sizeof (emlxs_be2_ufi_header_t) +
884 		    sizeof (emlxs_be2_flash_dir_t)));
885 		return (EMLXS_IMAGE_BAD);
886 	}
887 
888 	be_version = emlxs_be_version(buffer, len, &ufi_plus);
889 
890 	/* Check if this is a standard BE2 image */
891 	if (be_version != 2) {
892 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
893 		    "Invalid image provided.");
894 		return (EMLXS_IMAGE_INCOMPATIBLE);
895 	}
896 
897 	ufi_hdr = (emlxs_be2_ufi_header_t *)buffer;
898 
899 #ifdef EMLXS_BIG_ENDIAN
900 	/* Big Endian Swapping */
901 	/* Swap ufi header */
902 	ufi_hdr->checksum =
903 	    SWAP32(ufi_hdr->checksum);
904 	ufi_hdr->antidote =
905 	    SWAP32(ufi_hdr->antidote);
906 	ufi_hdr->controller.vendor_id =
907 	    SWAP32(ufi_hdr->controller.vendor_id);
908 	ufi_hdr->controller.device_id =
909 	    SWAP32(ufi_hdr->controller.device_id);
910 	ufi_hdr->controller.sub_vendor_id =
911 	    SWAP32(ufi_hdr->controller.sub_vendor_id);
912 	ufi_hdr->controller.sub_device_id =
913 	    SWAP32(ufi_hdr->controller.sub_device_id);
914 	ufi_hdr->file_length =
915 	    SWAP32(ufi_hdr->file_length);
916 	ufi_hdr->chunk_num =
917 	    SWAP32(ufi_hdr->chunk_num);
918 	ufi_hdr->chunk_cnt =
919 	    SWAP32(ufi_hdr->chunk_cnt);
920 	ufi_hdr->image_cnt =
921 	    SWAP32(ufi_hdr->image_cnt);
922 #endif /* EMLXS_BIG_ENDIAN */
923 
924 	if (len != ufi_hdr->file_length) {
925 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
926 		    "Invalid image size (%d != %d)",
927 		    len, ufi_hdr->file_length);
928 
929 		return (EMLXS_IMAGE_BAD);
930 	}
931 
932 	/* Scan for flash dir signature */
933 	bptr = (uint8_t *)buffer;
934 	flash_dir = NULL;
935 	for (i = 0; i < len; i++, bptr++) {
936 		if (strncmp((char *)bptr, BE_DIR_SIGNATURE,
937 		    sizeof (BE_DIR_SIGNATURE)) == 0) {
938 			flash_dir = (emlxs_be2_flash_dir_t *)bptr;
939 			break;
940 		}
941 	}
942 
943 	if (!flash_dir) {
944 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
945 		    "Unable to find flash directory.");
946 
947 		return (EMLXS_IMAGE_BAD);
948 	}
949 
950 #ifdef EMLXS_BIG_ENDIAN
951 	/* Big Endian Swapping */
952 	/* Swap flash dir */
953 	flash_dir->header.format_rev =
954 	    SWAP32(flash_dir->header.format_rev);
955 	flash_dir->header.checksum =
956 	    SWAP32(flash_dir->header.checksum);
957 	flash_dir->header.antidote =
958 	    SWAP32(flash_dir->header.antidote);
959 	flash_dir->header.build_num =
960 	    SWAP32(flash_dir->header.build_num);
961 	flash_dir->header.active_entry_mask =
962 	    SWAP32(flash_dir->header.active_entry_mask);
963 	flash_dir->header.valid_entry_mask =
964 	    SWAP32(flash_dir->header.valid_entry_mask);
965 	flash_dir->header.orig_content_mask =
966 	    SWAP32(flash_dir->header.orig_content_mask);
967 	flash_dir->header.resv0 = SWAP32(flash_dir->header.resv0);
968 	flash_dir->header.resv1 = SWAP32(flash_dir->header.resv1);
969 	flash_dir->header.resv2 = SWAP32(flash_dir->header.resv2);
970 	flash_dir->header.resv3 = SWAP32(flash_dir->header.resv3);
971 	flash_dir->header.resv4 = SWAP32(flash_dir->header.resv4);
972 
973 	for (i = 0; i < BE_CONTROLLER_SIZE; i++) {
974 		flash_dir->header.controller[i].vendor_id =
975 		    SWAP32(flash_dir->header.controller[i].vendor_id);
976 		flash_dir->header.controller[i].device_id =
977 		    SWAP32(flash_dir->header.controller[i].device_id);
978 		flash_dir->header.controller[i].sub_vendor_id =
979 		    SWAP32(flash_dir->header.controller[i].sub_vendor_id);
980 		flash_dir->header.controller[i].sub_device_id =
981 		    SWAP32(flash_dir->header.controller[i].sub_device_id);
982 	}
983 
984 	for (i = 0, mask = 1; i < BE_FLASH_ENTRIES; i++,  mask <<= 1) {
985 
986 		if (!(flash_dir->header.valid_entry_mask & mask)) {
987 			continue;
988 		}
989 
990 		entry = &flash_dir->entry[i];
991 
992 		if ((entry->type == 0) ||
993 		    (entry->type == (uint32_t)-1) ||
994 		    (entry->image_size == 0)) {
995 			continue;
996 		}
997 
998 		flash_dir->entry[i].type =
999 		    SWAP32(flash_dir->entry[i].type);
1000 		flash_dir->entry[i].offset =
1001 		    SWAP32(flash_dir->entry[i].offset);
1002 		flash_dir->entry[i].pad_size =
1003 		    SWAP32(flash_dir->entry[i].pad_size);
1004 		flash_dir->entry[i].image_size =
1005 		    SWAP32(flash_dir->entry[i].image_size);
1006 		flash_dir->entry[i].checksum =
1007 		    SWAP32(flash_dir->entry[i].checksum);
1008 		flash_dir->entry[i].entry_point =
1009 		    SWAP32(flash_dir->entry[i].entry_point);
1010 		flash_dir->entry[i].resv0 =
1011 		    SWAP32(flash_dir->entry[i].resv0);
1012 		flash_dir->entry[i].resv1 =
1013 		    SWAP32(flash_dir->entry[i].resv1);
1014 	}
1015 #endif /* EMLXS_BIG_ENDIAN */
1016 
1017 	/* Verify adapter model */
1018 	found = 0;
1019 	for (i = 0; i < BE_CONTROLLER_SIZE; i++) {
1020 		if (flash_dir->header.controller[i].device_id ==
1021 		    hba->model_info.device_id) {
1022 			found = 1;
1023 		}
1024 	}
1025 
1026 	if (!found) {
1027 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1028 		    "Invalid adapter device id=0x%x.",
1029 		    hba->model_info.device_id);
1030 		return (EMLXS_IMAGE_INCOMPATIBLE);
1031 	}
1032 
1033 	/* Build fw_image table */
1034 	fw_image->be_version = 2;
1035 	fw_image->ufi_plus = ufi_plus;
1036 	for (i = 0, mask = 1; i < BE_FLASH_ENTRIES; i++, mask <<= 1) {
1037 
1038 		if (!(flash_dir->header.valid_entry_mask & mask)) {
1039 			continue;
1040 		}
1041 
1042 		entry = &flash_dir->entry[i];
1043 
1044 		if ((entry->type == 0) ||
1045 		    (entry->type == (uint32_t)-1) ||
1046 		    (entry->image_size == 0)) {
1047 			continue;
1048 		}
1049 
1050 		switch (entry->type) {
1051 		case BE_FLASHTYPE_REDBOOT:
1052 			file = &fw_image->file[REDBOOT_FLASHTYPE];
1053 			(void) strlcpy(file->label, "REDBOOT",
1054 			    sizeof (file->label));
1055 			file->type = MGMT_FLASHROM_OPTYPE_REDBOOT;
1056 			break;
1057 		case BE_FLASHTYPE_ISCSI_BIOS:
1058 			file = &fw_image->file[ISCSI_BIOS_FLASHTYPE];
1059 			(void) strlcpy(file->label, "ISCSI BIOS",
1060 			    sizeof (file->label));
1061 			file->type = MGMT_FLASHROM_OPTYPE_ISCSI_BIOS;
1062 			break;
1063 		case BE_FLASHTYPE_PXE_BIOS:
1064 			file = &fw_image->file[PXE_BIOS_FLASHTYPE];
1065 			(void) strlcpy(file->label, "PXE BIOS",
1066 			    sizeof (file->label));
1067 			file->type = MGMT_FLASHROM_OPTYPE_PXE_BIOS;
1068 			break;
1069 		case BE_FLASHTYPE_FCOE_BIOS:
1070 			file = &fw_image->file[FCOE_BIOS_FLASHTYPE];
1071 			(void) strlcpy(file->label, "FCOE BIOS",
1072 			    sizeof (file->label));
1073 			file->type = MGMT_FLASHROM_OPTYPE_FCOE_BIOS;
1074 			break;
1075 		case BE_FLASHTYPE_ISCSI_FIRMWARE:
1076 			file = &fw_image->file[ISCSI_FIRMWARE_FLASHTYPE];
1077 			(void) strlcpy(file->label, "ISCSI FIRMWARE",
1078 			    sizeof (file->label));
1079 			file->type = MGMT_FLASHROM_OPTYPE_ISCSI_FIRMWARE;
1080 			break;
1081 		case BE_FLASHTYPE_FCOE_FIRMWARE:
1082 			file = &fw_image->file[FCOE_FIRMWARE_FLASHTYPE];
1083 			(void) strlcpy(file->label, "FCOE FIRMWARE",
1084 			    sizeof (file->label));
1085 			file->type = MGMT_FLASHROM_OPTYPE_FCOE_FIRMWARE;
1086 			break;
1087 		case BE_FLASHTYPE_FCOE_BACKUP:
1088 		case BE_FLASHTYPE_ISCSI_BACKUP:
1089 			continue;
1090 
1091 		default:
1092 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1093 			    "Unknown image type found.  type=%x",
1094 			    entry->type);
1095 			continue;
1096 		}
1097 
1098 		file->be_version = fw_image->be_version;
1099 		file->ufi_plus = fw_image->ufi_plus;
1100 		file->image_size = entry->image_size;
1101 		image_size = BE_SWAP32(entry->image_size);
1102 
1103 		if (ufi_plus) {
1104 			file->image_offset = entry->offset;
1105 			file->block_size   = entry->pad_size;
1106 			file->block_crc    = entry->checksum;
1107 			file->load_address = entry->entry_point;
1108 
1109 		} else {
1110 			file->image_offset = entry->offset +
1111 			    sizeof (emlxs_be2_ufi_header_t);
1112 
1113 			/* Get entry block size and crc */
1114 			k = file->image_offset + file->image_size;
1115 			k &= 0xFFFFFFFC;
1116 
1117 			wptr = (uint32_t *)(buffer +  k);
1118 			for (; k < len; k += 4) {
1119 				if (*wptr++ == image_size) {
1120 					/* Calculate block_size */
1121 					file->block_size = (k + 8) -
1122 					    file->image_offset;
1123 
1124 					/* Read load_address */
1125 					value = *(wptr - 2);
1126 					file->load_address = BE_SWAP32(value);
1127 
1128 					/* Read block_crc */
1129 					value = *wptr;
1130 					file->block_crc = BE_SWAP32(value);
1131 
1132 					break;
1133 				}
1134 			}
1135 
1136 			if (k >= len) {
1137 				EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1138 				    "%s: End of block not found. offset=%x",
1139 				    file->label, file->image_offset);
1140 
1141 				bzero(fw_image, sizeof (emlxs_be_fw_image_t));
1142 				return (EMLXS_IMAGE_BAD);
1143 			}
1144 		}
1145 
1146 		/* Make sure image will fit in block specified */
1147 		if (file->image_size + 12 > file->block_size) {
1148 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1149 			    "%s: Image too large for block. image=%x block=%x",
1150 			    file->label, file->image_size, file->block_size);
1151 
1152 			bzero(fw_image, sizeof (emlxs_be_fw_image_t));
1153 			return (EMLXS_IMAGE_BAD);
1154 		}
1155 
1156 		/* Automatically create a backup file entry for firmware */
1157 		if (file->type == MGMT_FLASHROM_OPTYPE_FCOE_FIRMWARE) {
1158 			file2 = &fw_image->file[FCOE_BACKUP_FLASHTYPE];
1159 
1160 			bcopy((uint8_t *)file, (uint8_t *)file2,
1161 			    sizeof (emlxs_be_fw_file_t));
1162 			file2->type = MGMT_FLASHROM_OPTYPE_FCOE_BACKUP;
1163 			(void) strlcpy(file2->label, "FCOE BACKUP",
1164 			    sizeof (file2->label));
1165 
1166 			/* Save FCOE version info */
1167 			bptr = (uint8_t *)buffer + file->image_offset + 0x30;
1168 			(void) strncpy(fw_image->fcoe_label, (char *)bptr,
1169 			    BE_VERSION_SIZE);
1170 			fw_image->fcoe_version = file->block_crc;
1171 
1172 		} else if (file->type ==
1173 		    MGMT_FLASHROM_OPTYPE_ISCSI_FIRMWARE) {
1174 			file2 = &fw_image->file[ISCSI_BACKUP_FLASHTYPE];
1175 
1176 			bcopy((uint8_t *)file, (uint8_t *)file2,
1177 			    sizeof (emlxs_be_fw_file_t));
1178 			file2->type = MGMT_FLASHROM_OPTYPE_ISCSI_BACKUP;
1179 			(void) strlcpy(file2->label, "ISCSI BACKUP",
1180 			    sizeof (file2->label));
1181 
1182 			/* Save ISCSI version info */
1183 			bptr = (uint8_t *)buffer + file->image_offset + 0x30;
1184 			(void) strncpy(fw_image->iscsi_label, (char *)bptr,
1185 			    BE_VERSION_SIZE);
1186 			fw_image->iscsi_version = file->block_crc;
1187 		}
1188 	}
1189 
1190 	if (fw_image->fcoe_version == 0) {
1191 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1192 		    "Unable to find FCOE firmware component.");
1193 
1194 		bzero(fw_image, sizeof (emlxs_be_fw_image_t));
1195 		return (EMLXS_IMAGE_BAD);
1196 	}
1197 
1198 	/* Display contents */
1199 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
1200 	    "BE2 UFI Image: %08x, %s", fw_image->fcoe_version,
1201 	    fw_image->fcoe_label);
1202 
1203 	for (i = 0; i < BE_MAX_FLASHTYPES; i++) {
1204 		file = &fw_image->file[i];
1205 
1206 		if (file->image_size == 0) {
1207 			continue;
1208 		}
1209 
1210 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
1211 		    "%s: be=%x%s type=%x block=%x image=%x offset=%x crc=%x "
1212 		    "load=%x",
1213 		    file->label, file->be_version, (file->ufi_plus)?"+":"",
1214 		    file->type, file->block_size, file->image_size,
1215 		    file->image_offset, file->block_crc, file->load_address);
1216 	}
1217 
1218 	return (0);
1219 
1220 } /* emlxs_be2_validate_image() */
1221 
1222 
1223 static uint32_t
emlxs_be3_validate_image(emlxs_hba_t * hba,caddr_t buffer,uint32_t len,emlxs_be_fw_image_t * fw_image)1224 emlxs_be3_validate_image(emlxs_hba_t *hba, caddr_t buffer,
1225     uint32_t len, emlxs_be_fw_image_t *fw_image)
1226 {
1227 	emlxs_port_t *port = &PPORT;
1228 	emlxs_be3_ufi_header_t *ufi_hdr;
1229 	emlxs_be3_flash_dir_t *flash_dir;
1230 	emlxs_be3_flash_entry_t *entry;
1231 	emlxs_be3_image_header_t *flash_image_hdr;
1232 	emlxs_be3_image_header_t *image_hdr;
1233 	uint8_t *bptr;
1234 	uint32_t *wptr;
1235 	uint32_t i;
1236 	uint32_t value;
1237 	emlxs_be_fw_file_t *file;
1238 	emlxs_be_fw_file_t *file2;
1239 	uint32_t ufi_plus = 0;
1240 	uint32_t be_version = 0;
1241 	uint32_t found;
1242 
1243 	bzero(fw_image, sizeof (emlxs_be_fw_image_t));
1244 
1245 	if (hba->model_info.chip != EMLXS_BE3_CHIP) {
1246 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1247 		    "Invalid adapter model.");
1248 		return (EMLXS_IMAGE_INCOMPATIBLE);
1249 	}
1250 
1251 	if (len < (sizeof (emlxs_be3_ufi_header_t) +
1252 	    sizeof (emlxs_be3_flash_dir_t))) {
1253 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1254 		    "Image too small. (%d < %d)",
1255 		    len, (sizeof (emlxs_be3_ufi_header_t) +
1256 		    sizeof (emlxs_be3_flash_dir_t)));
1257 		return (EMLXS_IMAGE_BAD);
1258 	}
1259 
1260 	be_version = emlxs_be_version(buffer, len, &ufi_plus);
1261 
1262 	/* Check if this is a standard BE3 image */
1263 	if (be_version != 3) {
1264 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1265 		    "Invalid image provided.");
1266 		return (EMLXS_IMAGE_INCOMPATIBLE);
1267 	}
1268 
1269 	ufi_hdr = (emlxs_be3_ufi_header_t *)buffer;
1270 
1271 #ifdef EMLXS_BIG_ENDIAN
1272 	/* Big Endian Swapping */
1273 	/* Swap ufi header */
1274 	ufi_hdr->ufi_version =
1275 	    SWAP32(ufi_hdr->ufi_version);
1276 	ufi_hdr->file_length =
1277 	    SWAP32(ufi_hdr->file_length);
1278 	ufi_hdr->checksum =
1279 	    SWAP32(ufi_hdr->checksum);
1280 	ufi_hdr->antidote =
1281 	    SWAP32(ufi_hdr->antidote);
1282 	ufi_hdr->image_cnt =
1283 	    SWAP32(ufi_hdr->image_cnt);
1284 #endif /* EMLXS_BIG_ENDIAN */
1285 
1286 	if (len != ufi_hdr->file_length) {
1287 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1288 		    "Invalid image size (%d != %d)",
1289 		    len, ufi_hdr->file_length);
1290 
1291 		return (EMLXS_IMAGE_BAD);
1292 	}
1293 
1294 	flash_image_hdr = NULL;
1295 	image_hdr = (emlxs_be3_image_header_t *)(buffer +
1296 	    sizeof (emlxs_be3_ufi_header_t));
1297 	for (i = 0; i < ufi_hdr->image_cnt; i++, image_hdr++) {
1298 #ifdef EMLXS_BIG_ENDIAN
1299 		image_hdr->id = SWAP32(image_hdr->id);
1300 		image_hdr->offset = SWAP32(image_hdr->offset);
1301 		image_hdr->length = SWAP32(image_hdr->length);
1302 		image_hdr->checksum = SWAP32(image_hdr->checksum);
1303 #endif /* EMLXS_BIG_ENDIAN */
1304 
1305 		if (image_hdr->id == UFI_BE3_FLASH_ID) {
1306 			flash_image_hdr = image_hdr;
1307 		}
1308 	}
1309 
1310 	if (!flash_image_hdr) {
1311 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1312 		    "No flash image found.");
1313 
1314 		return (EMLXS_IMAGE_BAD);
1315 	}
1316 
1317 	/* Scan for flash dir signature */
1318 	bptr = (uint8_t *)buffer + flash_image_hdr->offset;
1319 	flash_dir = NULL;
1320 	for (i = 0; i < flash_image_hdr->length; i++, bptr++) {
1321 		if (strncmp((char *)bptr, BE_DIR_SIGNATURE,
1322 		    sizeof (BE_DIR_SIGNATURE)) == 0) {
1323 			flash_dir = (emlxs_be3_flash_dir_t *)bptr;
1324 			break;
1325 		}
1326 	}
1327 
1328 	if (!flash_dir) {
1329 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1330 		    "Unable to find flash directory.");
1331 
1332 		return (EMLXS_IMAGE_BAD);
1333 	}
1334 
1335 #ifdef EMLXS_BIG_ENDIAN
1336 	/* Big Endian Swapping */
1337 	/* Swap flash dir */
1338 	flash_dir->header.format_rev =
1339 	    SWAP32(flash_dir->header.format_rev);
1340 	flash_dir->header.checksum =
1341 	    SWAP32(flash_dir->header.checksum);
1342 	flash_dir->header.antidote =
1343 	    SWAP32(flash_dir->header.antidote);
1344 	flash_dir->header.entry_count =
1345 	    SWAP32(flash_dir->header.entry_count);
1346 	flash_dir->header.resv0 = SWAP32(flash_dir->header.resv0);
1347 	flash_dir->header.resv1 = SWAP32(flash_dir->header.resv1);
1348 	flash_dir->header.resv2 = SWAP32(flash_dir->header.resv2);
1349 	flash_dir->header.resv3 = SWAP32(flash_dir->header.resv3);
1350 
1351 	for (i = 0; i < BE_CONTROLLER_SIZE; i++) {
1352 		flash_dir->header.controller[i].vendor_id =
1353 		    SWAP32(flash_dir->header.controller[i].vendor_id);
1354 		flash_dir->header.controller[i].device_id =
1355 		    SWAP32(flash_dir->header.controller[i].device_id);
1356 		flash_dir->header.controller[i].sub_vendor_id =
1357 		    SWAP32(flash_dir->header.controller[i].sub_vendor_id);
1358 		flash_dir->header.controller[i].sub_device_id =
1359 		    SWAP32(flash_dir->header.controller[i].sub_device_id);
1360 	}
1361 
1362 	for (i = 0; i < flash_dir->header.entry_count; i++) {
1363 		entry = &flash_dir->entry[i];
1364 
1365 		if ((entry->type == 0) ||
1366 		    (entry->type == (uint32_t)-1) ||
1367 		    (entry->image_size == 0)) {
1368 			continue;
1369 		}
1370 
1371 		flash_dir->entry[i].type =
1372 		    SWAP32(flash_dir->entry[i].type);
1373 		flash_dir->entry[i].offset =
1374 		    SWAP32(flash_dir->entry[i].offset);
1375 		flash_dir->entry[i].block_size =
1376 		    SWAP32(flash_dir->entry[i].block_size);
1377 		flash_dir->entry[i].image_size =
1378 		    SWAP32(flash_dir->entry[i].image_size);
1379 		flash_dir->entry[i].checksum =
1380 		    SWAP32(flash_dir->entry[i].checksum);
1381 		flash_dir->entry[i].entry_point =
1382 		    SWAP32(flash_dir->entry[i].entry_point);
1383 		flash_dir->entry[i].resv0 =
1384 		    SWAP32(flash_dir->entry[i].resv0);
1385 		flash_dir->entry[i].resv1 =
1386 		    SWAP32(flash_dir->entry[i].resv1);
1387 	}
1388 #endif /* EMLXS_BIG_ENDIAN */
1389 
1390 	/* Verify image checksum */
1391 	if (flash_dir->header.checksum != flash_image_hdr->checksum) {
1392 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1393 		    "Invalid flash directory checksum. (%x != %x)\n",
1394 		    flash_dir->header.checksum, flash_image_hdr->checksum);
1395 		return (EMLXS_IMAGE_BAD);
1396 	}
1397 
1398 	/* Verify adapter model */
1399 	found = 0;
1400 	for (i = 0; i < BE_CONTROLLER_SIZE; i++) {
1401 		if (flash_dir->header.controller[i].device_id ==
1402 		    hba->model_info.device_id) {
1403 			found = 1;
1404 		}
1405 	}
1406 
1407 	if (!found) {
1408 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1409 		    "Invalid adapter device id=0x%x.",
1410 		    hba->model_info.device_id);
1411 		return (EMLXS_IMAGE_INCOMPATIBLE);
1412 	}
1413 
1414 	/* Build fw_image table */
1415 	fw_image->be_version = 3;
1416 	fw_image->ufi_plus = ufi_plus;
1417 	for (i = 0; i < flash_dir->header.entry_count; i++) {
1418 		entry = &flash_dir->entry[i];
1419 
1420 		if ((entry->type == 0) ||
1421 		    (entry->type == (uint32_t)-1) ||
1422 		    (entry->image_size == 0)) {
1423 			continue;
1424 		}
1425 
1426 		switch (entry->type) {
1427 		case BE_FLASHTYPE_REDBOOT:
1428 			file = &fw_image->file[REDBOOT_FLASHTYPE];
1429 			(void) strlcpy(file->label, "REDBOOT",
1430 			    sizeof (file->label));
1431 			file->type = MGMT_FLASHROM_OPTYPE_REDBOOT;
1432 			break;
1433 		case BE_FLASHTYPE_ISCSI_BIOS:
1434 			file = &fw_image->file[ISCSI_BIOS_FLASHTYPE];
1435 			(void) strlcpy(file->label, "ISCSI BIOS",
1436 			    sizeof (file->label));
1437 			file->type = MGMT_FLASHROM_OPTYPE_ISCSI_BIOS;
1438 			break;
1439 		case BE_FLASHTYPE_PXE_BIOS:
1440 			file = &fw_image->file[PXE_BIOS_FLASHTYPE];
1441 			(void) strlcpy(file->label, "PXE BIOS",
1442 			    sizeof (file->label));
1443 			file->type = MGMT_FLASHROM_OPTYPE_PXE_BIOS;
1444 			break;
1445 		case BE_FLASHTYPE_FCOE_BIOS:
1446 			file = &fw_image->file[FCOE_BIOS_FLASHTYPE];
1447 			(void) strlcpy(file->label, "FCOE BIOS",
1448 			    sizeof (file->label));
1449 			file->type = MGMT_FLASHROM_OPTYPE_FCOE_BIOS;
1450 			break;
1451 		case BE_FLASHTYPE_ISCSI_FIRMWARE:
1452 			file = &fw_image->file[ISCSI_FIRMWARE_FLASHTYPE];
1453 			(void) strlcpy(file->label, "ISCSI FIRMWARE",
1454 			    sizeof (file->label));
1455 			file->type = MGMT_FLASHROM_OPTYPE_ISCSI_FIRMWARE;
1456 			break;
1457 		case BE_FLASHTYPE_FCOE_FIRMWARE:
1458 			file = &fw_image->file[FCOE_FIRMWARE_FLASHTYPE];
1459 			(void) strlcpy(file->label, "FCOE FIRMWARE",
1460 			    sizeof (file->label));
1461 			file->type = MGMT_FLASHROM_OPTYPE_FCOE_FIRMWARE;
1462 			break;
1463 		case BE_FLASHTYPE_NCSI_FIRMWARE:
1464 			file = &fw_image->file[NCSI_FIRMWARE_FLASHTYPE];
1465 			(void) strlcpy(file->label, "NCSI FIRMWARE",
1466 			    sizeof (file->label));
1467 			file->type = MGMT_FLASHROM_OPTYPE_NCSI_FIRMWARE;
1468 			break;
1469 		case BE_FLASHTYPE_FLASH_ISM:
1470 		case BE_FLASHTYPE_FCOE_BACKUP:
1471 		case BE_FLASHTYPE_ISCSI_BACKUP:
1472 			continue;
1473 		case BE_FLASHTYPE_PHY_FIRMWARE:
1474 			file = &fw_image->file[PHY_FIRMWARE_FLASHTYPE];
1475 			(void) strlcpy(file->label, "PHY FIRMWARE",
1476 			    sizeof (file->label));
1477 			file->type = MGMT_FLASHROM_OPTYPE_PHY_FIRMWARE;
1478 			break;
1479 
1480 		default:
1481 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1482 			    "Unknown image type found.  type=%x",
1483 			    entry->type);
1484 			continue;
1485 		}
1486 
1487 		file->be_version = fw_image->be_version;
1488 		file->ufi_plus = fw_image->ufi_plus;
1489 		file->image_size = entry->image_size;
1490 
1491 		if (ufi_plus) {
1492 			file->image_offset = entry->offset;
1493 			file->block_size   = entry->block_size;
1494 			file->block_crc    = entry->checksum;
1495 			file->load_address = entry->entry_point;
1496 		} else {
1497 			file->image_offset = entry->offset +
1498 			    flash_image_hdr->offset;
1499 			file->block_size   = entry->block_size;
1500 
1501 			wptr = (uint32_t *)(buffer +  file->image_offset +
1502 			    file->block_size);
1503 
1504 			/* Read load address */
1505 			value = *(wptr - 3);
1506 			file->load_address = BE_SWAP32(value);
1507 
1508 			/* Read block_crc */
1509 			value = *(wptr - 1);
1510 			file->block_crc = BE_SWAP32(value);
1511 		}
1512 
1513 		/* Make sure image will fit in block specified */
1514 		if (file->image_size + 12 > file->block_size) {
1515 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1516 			    "%s: Image too large for block. image=%x block=%x",
1517 			    file->label, file->image_size, file->block_size);
1518 
1519 			bzero(fw_image, sizeof (emlxs_be_fw_image_t));
1520 			return (EMLXS_IMAGE_BAD);
1521 		}
1522 
1523 		/* Automatically create a backup file entry for firmware */
1524 		if (file->type == MGMT_FLASHROM_OPTYPE_FCOE_FIRMWARE) {
1525 			file2 = &fw_image->file[FCOE_BACKUP_FLASHTYPE];
1526 
1527 			bcopy((uint8_t *)file, (uint8_t *)file2,
1528 			    sizeof (emlxs_be_fw_file_t));
1529 			file2->type = MGMT_FLASHROM_OPTYPE_FCOE_BACKUP;
1530 			(void) strlcpy(file2->label, "FCOE BACKUP",
1531 			    sizeof (file2->label));
1532 
1533 			/* Save FCOE version info */
1534 			bptr = (uint8_t *)buffer + file->image_offset + 0x30;
1535 			(void) strncpy(fw_image->fcoe_label, (char *)bptr,
1536 			    BE_VERSION_SIZE);
1537 			fw_image->fcoe_version = file->block_crc;
1538 
1539 		} else if (file->type ==
1540 		    MGMT_FLASHROM_OPTYPE_ISCSI_FIRMWARE) {
1541 			file2 = &fw_image->file[ISCSI_BACKUP_FLASHTYPE];
1542 
1543 			bcopy((uint8_t *)file, (uint8_t *)file2,
1544 			    sizeof (emlxs_be_fw_file_t));
1545 			file2->type = MGMT_FLASHROM_OPTYPE_ISCSI_BACKUP;
1546 			(void) strlcpy(file2->label, "ISCSI BACKUP",
1547 			    sizeof (file->label));
1548 
1549 			/* Save ISCSI version info */
1550 			bptr = (uint8_t *)buffer + file->image_offset + 0x30;
1551 			(void) strncpy(fw_image->iscsi_label, (char *)bptr,
1552 			    BE_VERSION_SIZE);
1553 			fw_image->iscsi_version = file->block_crc;
1554 		}
1555 	}
1556 
1557 	if (fw_image->fcoe_version == 0) {
1558 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1559 		    "Unable to find FCOE firmware component.");
1560 
1561 		bzero(fw_image, sizeof (emlxs_be_fw_image_t));
1562 		return (EMLXS_IMAGE_BAD);
1563 	}
1564 
1565 	/* Display contents */
1566 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
1567 	    "BE3 UFI Image: %08x, %s", fw_image->fcoe_version,
1568 	    fw_image->fcoe_label);
1569 
1570 	for (i = 0; i < BE_MAX_FLASHTYPES; i++) {
1571 		file = &fw_image->file[i];
1572 
1573 		if (file->image_size == 0) {
1574 			continue;
1575 		}
1576 
1577 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_msg,
1578 		    "%s: be=%x%s type=%x block=%x image=%x offset=%x crc=%x "
1579 		    "load=%x",
1580 		    file->label, file->be_version, (file->ufi_plus)? "+":"",
1581 		    file->type, file->block_size, file->image_size,
1582 		    file->image_offset, file->block_crc, file->load_address);
1583 	}
1584 
1585 	return (0);
1586 
1587 } /* emlxs_be3_validate_image() */
1588 
1589 
1590 static int32_t
emlxs_be_fw_download(emlxs_hba_t * hba,caddr_t buffer,uint32_t len,uint32_t offline)1591 emlxs_be_fw_download(emlxs_hba_t *hba, caddr_t buffer, uint32_t len,
1592     uint32_t offline)
1593 {
1594 	emlxs_port_t *port = &PPORT;
1595 	uint32_t i;
1596 	uint32_t update = 0;
1597 	uint32_t rval = 0;
1598 	MAILBOXQ *mbq = NULL;
1599 	MATCHMAP *mp = NULL;
1600 	emlxs_be_fw_image_t fw_image;
1601 	emlxs_be_fw_file_t *file;
1602 	uint32_t be_version;
1603 
1604 	/* For now we will not take the driver offline during a download */
1605 	offline = 0;
1606 
1607 	if (hba->sli_mode != EMLXS_HBA_SLI4_MODE) {
1608 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1609 		    "Invalid sli_mode. mode=%d", hba->sli_mode);
1610 		return (EMLXS_IMAGE_INCOMPATIBLE);
1611 	}
1612 
1613 	if (!(hba->model_info.chip & EMLXS_BE_CHIPS)) {
1614 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1615 		    "Invalid adapter model. chip=%x", hba->model_info.chip);
1616 		return (EMLXS_IMAGE_INCOMPATIBLE);
1617 	}
1618 
1619 	if (buffer == NULL || len == 0) {
1620 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1621 		    "Empty buffer provided. buf=%p size=%d", buffer, len);
1622 		return (EMLXS_IMAGE_BAD);
1623 	}
1624 
1625 	be_version = emlxs_be_version(buffer, len, 0);
1626 
1627 	switch (be_version) {
1628 	case 0:
1629 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1630 		    "Invalid image provided. Non-UFI format.");
1631 		return (EMLXS_IMAGE_INCOMPATIBLE);
1632 	case 2:
1633 		rval = emlxs_be2_validate_image(hba, buffer, len, &fw_image);
1634 		if (rval) {
1635 			return (rval);
1636 		}
1637 		break;
1638 	case 3:
1639 		rval = emlxs_be3_validate_image(hba, buffer, len, &fw_image);
1640 		if (rval) {
1641 			return (rval);
1642 		}
1643 		break;
1644 	default:
1645 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1646 		    "Invalid image provided. Unknown BE version. (%x)",
1647 		    be_version);
1648 		return (EMLXS_IMAGE_INCOMPATIBLE);
1649 	}
1650 
1651 	/* Allocate resources */
1652 
1653 	if ((mbq = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
1654 	    KM_SLEEP)) == NULL) {
1655 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
1656 		    "Unable to allocate mailbox buffer.");
1657 
1658 		offline = 0;
1659 		rval = EMLXS_IMAGE_FAILED;
1660 		goto done;
1661 	}
1662 
1663 	if ((mp = emlxs_mem_buf_alloc(hba, (sizeof (mbox_req_hdr_t) +
1664 	    sizeof (IOCTL_COMMON_FLASHROM) + BE_MAX_XFER_SIZE))) == NULL) {
1665 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
1666 		    "Unable to allocate flash buffer.");
1667 
1668 		offline = 0;
1669 		rval = EMLXS_IMAGE_FAILED;
1670 		goto done;
1671 	}
1672 
1673 	/* Check if update is required */
1674 	for (i = 0; i < BE_MAX_FLASHTYPES; i++) {
1675 		file = &fw_image.file[i];
1676 
1677 		if (file->image_size == 0) {
1678 			continue;
1679 		}
1680 
1681 		if (file->type == MGMT_FLASHROM_OPTYPE_PHY_FIRMWARE) {
1682 			rval = emlxs_be_verify_phy(hba, file, mbq, mp);
1683 
1684 			if (rval != 0) {
1685 				/* Do not update */
1686 				file->image_size = 0;
1687 				continue;
1688 			}
1689 		} else {
1690 			rval = emlxs_be_verify_crc(hba, file, mbq, mp);
1691 			if (rval == 0) {
1692 				/* Do not update */
1693 				file->image_size = 0;
1694 				continue;
1695 			}
1696 		}
1697 
1698 		update++;
1699 	}
1700 
1701 	if (!update) {
1702 		offline = 0;
1703 		rval = 0;
1704 		goto done;
1705 	}
1706 
1707 	/*
1708 	 * Everything checks out, now to just do it
1709 	 */
1710 	if (offline) {
1711 		if (emlxs_offline(hba, 0) != FC_SUCCESS) {
1712 
1713 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
1714 			    "Unable to take adapter offline.");
1715 
1716 			offline = 0;
1717 			rval = EMLXS_OFFLINE_FAILED;
1718 			goto done;
1719 		}
1720 	}
1721 
1722 	/* Download entries which require update */
1723 	for (i = 0; i < BE_MAX_FLASHTYPES; i++) {
1724 		file = &fw_image.file[i];
1725 
1726 		if (file->image_size == 0) {
1727 			continue;
1728 		}
1729 
1730 		rval = emlxs_be_flash_image(hba, buffer, file, mbq, mp);
1731 
1732 		if (rval != 0) {
1733 			goto done;
1734 		}
1735 	}
1736 
1737 done:
1738 	if (mbq) {
1739 		emlxs_mem_put(hba, MEM_MBOX, (void *)mbq);
1740 	}
1741 
1742 	if (mp) {
1743 		emlxs_mem_buf_free(hba, mp);
1744 	}
1745 
1746 	if (offline) {
1747 		(void) emlxs_online(hba);
1748 	}
1749 
1750 	if (rval == 0) {
1751 		if (update) {
1752 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_complete_msg,
1753 			    "Status good.");
1754 
1755 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_fw_updated_msg,
1756 			    "The new firmware will not be activated until "
1757 			    "the adapter is power cycled: %s",
1758 			    fw_image.fcoe_label);
1759 
1760 		} else {
1761 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
1762 			    "No firmware update required.");
1763 		}
1764 	}
1765 
1766 	return (rval);
1767 
1768 } /* emlxs_be_fw_download() */
1769 
1770 
1771 static int32_t
emlxs_obj_flash_image(emlxs_hba_t * hba,caddr_t buffer,uint32_t size,MAILBOXQ * mbq,MATCHMAP * mp,uint32_t * change_status)1772 emlxs_obj_flash_image(emlxs_hba_t *hba, caddr_t buffer, uint32_t size,
1773     MAILBOXQ *mbq, MATCHMAP *mp, uint32_t *change_status)
1774 {
1775 	emlxs_port_t *port = &PPORT;
1776 	uint8_t *image_ptr;
1777 	MAILBOX4 *mb;
1778 	mbox_req_hdr_t	*hdr_req;
1779 	mbox_rsp_hdr_t	*hdr_rsp;
1780 	uint32_t	image_size;
1781 	uint32_t	xfer_size;
1782 	uint32_t	image_offset;
1783 	uint32_t	rval = 0;
1784 	IOCTL_COMMON_WRITE_OBJECT *write_obj;
1785 	uint32_t 	cstatus = 0;
1786 
1787 	if (!buffer || size == 0) {
1788 		return (0);
1789 	}
1790 
1791 	image_ptr  = (uint8_t *)buffer;
1792 	image_size = size;
1793 	image_offset = 0;
1794 	mb = (MAILBOX4*)mbq;
1795 
1796 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
1797 	    "OBJ File: Downloading...");
1798 
1799 	while (image_size) {
1800 		bzero((void *) mb, MAILBOX_CMD_SLI4_BSIZE);
1801 		bzero((void *) mp->virt, mp->size);
1802 
1803 		xfer_size = min(OBJ_MAX_XFER_SIZE, image_size);
1804 
1805 		mb->un.varSLIConfig.be.embedded = 1;
1806 		mbq->nonembed  = NULL;
1807 		mbq->mbox_cmpl = NULL;
1808 
1809 		mb->mbxCommand = MBX_SLI_CONFIG;
1810 		mb->mbxOwner = OWN_HOST;
1811 
1812 		hdr_req = (mbox_req_hdr_t *)
1813 		    &mb->un.varSLIConfig.be.un_hdr.hdr_req;
1814 		hdr_req->subsystem = IOCTL_SUBSYSTEM_COMMON;
1815 		hdr_req->opcode = COMMON_OPCODE_WRITE_OBJ;
1816 		hdr_req->timeout = 0;
1817 
1818 		write_obj = (IOCTL_COMMON_WRITE_OBJECT *)(hdr_req + 1);
1819 		write_obj->params.request.EOF =
1820 		    ((xfer_size == image_size)? 1:0);
1821 		write_obj->params.request.desired_write_length = xfer_size;
1822 		write_obj->params.request.write_offset = image_offset;
1823 
1824 		(void) strlcpy((char *)write_obj->params.request.object_name,
1825 		    "/prg", sizeof (write_obj->params.request.object_name));
1826 		BE_SWAP32_BUFFER((uint8_t *)
1827 		    write_obj->params.request.object_name,
1828 		    sizeof (write_obj->params.request.object_name));
1829 
1830 		write_obj->params.request.buffer_desc_count = 1;
1831 		write_obj->params.request.buffer_length = xfer_size;
1832 		write_obj->params.request.buffer_addrlo = PADDR_LO(mp->phys);
1833 		write_obj->params.request.buffer_addrhi = PADDR_HI(mp->phys);
1834 
1835 		hdr_req->req_length = 116 +
1836 		    (write_obj->params.request.buffer_desc_count * 12);
1837 
1838 		bcopy(image_ptr, mp->virt, xfer_size);
1839 
1840 		hdr_rsp = (mbox_rsp_hdr_t *)
1841 		    &mb->un.varSLIConfig.be.un_hdr.hdr_rsp;
1842 		write_obj = (IOCTL_COMMON_WRITE_OBJECT *)(hdr_rsp + 1);
1843 
1844 		/* Send write request */
1845 		if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbq, MBX_WAIT, 0) !=
1846 		    MBX_SUCCESS) {
1847 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
1848 			    "OBJ File: Unable to download image. status=%x "
1849 			    "(%x,%x)",
1850 			    mb->mbxStatus, hdr_rsp->status,
1851 			    hdr_rsp->extra_status);
1852 
1853 			return (EMLXS_IMAGE_FAILED);
1854 		}
1855 
1856 		/* Check header status */
1857 		if (hdr_rsp->status) {
1858 			if ((hdr_rsp->status == MBX_RSP_STATUS_FAILED) &&
1859 			    (hdr_rsp->extra_status ==
1860 			    MGMT_ADDI_STATUS_INCOMPATIBLE)) {
1861 				EMLXS_MSGF(EMLXS_CONTEXT,
1862 				    &emlxs_download_failed_msg,
1863 				    "OBJ File: Image file incompatible with "
1864 				    "adapter hardware.");
1865 				return (EMLXS_IMAGE_INCOMPATIBLE);
1866 			}
1867 
1868 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
1869 			    "OBJ File: Unable to download image. "
1870 			    "hdr_status=%x,%x size=%d,%d",
1871 			    hdr_rsp->status, hdr_rsp->extra_status,
1872 			    write_obj->params.response.actual_write_length,
1873 			    xfer_size);
1874 
1875 			return (EMLXS_IMAGE_FAILED);
1876 		}
1877 
1878 		/* Check response length */
1879 		if (write_obj->params.response.actual_write_length == 0) {
1880 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
1881 			    "OBJ File: No data actually written.");
1882 
1883 			return (EMLXS_IMAGE_FAILED);
1884 		}
1885 
1886 		if (write_obj->params.response.actual_write_length >
1887 		    xfer_size) {
1888 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
1889 			    "OBJ File: Mismatch in data xfer size. "
1890 			    "size=%d,%d offset=%d",
1891 			    write_obj->params.response.actual_write_length,
1892 			    xfer_size, image_offset);
1893 
1894 			return (EMLXS_IMAGE_FAILED);
1895 		}
1896 
1897 		/* Set xfer_size to actual write length */
1898 		xfer_size = write_obj->params.response.actual_write_length;
1899 
1900 		image_ptr  += xfer_size;
1901 		image_offset += xfer_size;
1902 		image_size -= xfer_size;
1903 
1904 		if (image_size == 0) {
1905 			cstatus = write_obj->params.response.change_status;
1906 		}
1907 	}
1908 
1909 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
1910 	    "OBJ File: Download complete. (cstatus=%d)",
1911 	    cstatus);
1912 
1913 	if (change_status) {
1914 		*change_status = cstatus;
1915 	}
1916 
1917 	return (rval);
1918 
1919 } /* emlxs_obj_flash_image() */
1920 
1921 
1922 static uint32_t
emlxs_obj_validate_image(emlxs_hba_t * hba,caddr_t buffer,uint32_t len,emlxs_obj_header_t * obj_hdr_in)1923 emlxs_obj_validate_image(emlxs_hba_t *hba, caddr_t buffer, uint32_t len,
1924     emlxs_obj_header_t *obj_hdr_in)
1925 {
1926 	emlxs_port_t *port = &PPORT;
1927 	emlxs_obj_header_t obj_hdr;
1928 
1929 	if (obj_hdr_in) {
1930 		bzero(obj_hdr_in, sizeof (emlxs_obj_header_t));
1931 	}
1932 
1933 	if (hba->sli_mode != EMLXS_HBA_SLI4_MODE) {
1934 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1935 		    "Invalid sli_mode. mode=%d", hba->sli_mode);
1936 		return (EMLXS_IMAGE_INCOMPATIBLE);
1937 	}
1938 
1939 	if (hba->model_info.chip & EMLXS_BE_CHIPS) {
1940 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1941 		    "Invalid adapter model. chip=%x", hba->model_info.chip);
1942 		return (EMLXS_IMAGE_INCOMPATIBLE);
1943 	}
1944 
1945 	if (len < sizeof (emlxs_obj_header_t)) {
1946 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1947 		    "Image too small. (%d < %d)",
1948 		    len,  sizeof (emlxs_obj_header_t));
1949 
1950 		return (EMLXS_IMAGE_BAD);
1951 	}
1952 
1953 	bcopy(buffer, (uint8_t *)&obj_hdr, sizeof (emlxs_obj_header_t));
1954 
1955 	/* Swap first 3 words */
1956 	LE_SWAP32_BUFFER((uint8_t *)&obj_hdr, 12);
1957 
1958 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
1959 	    "Object Header: size=%d magic=%04x,%04x type=%02x id=%02x",
1960 	    obj_hdr.FileSize,
1961 	    obj_hdr.MagicNumHi, obj_hdr.MagicNumLo,
1962 	    obj_hdr.FileType,
1963 	    obj_hdr.Id);
1964 
1965 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
1966 	    "Object Header: Date=%s Rev=%s",
1967 	    obj_hdr.Date,
1968 	    obj_hdr.Revision);
1969 
1970 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
1971 	    "Object Header: Name=%s",
1972 	    obj_hdr.RevName);
1973 
1974 	if ((obj_hdr.MagicNumHi != OBJ_MAGIC_NUM_HI) ||
1975 	    (obj_hdr.MagicNumLo != OBJ_MAGIC_NUM_LO)) {
1976 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1977 		    "Wrong Magic Number: %x,%x",
1978 		    obj_hdr.MagicNumHi, obj_hdr.MagicNumLo);
1979 
1980 		return (EMLXS_IMAGE_INCOMPATIBLE);
1981 	}
1982 
1983 	if (obj_hdr.FileSize != len) {
1984 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
1985 		    "Image too small. (%d < %d)",
1986 		    len, obj_hdr.FileSize);
1987 
1988 		return (EMLXS_IMAGE_BAD);
1989 	}
1990 
1991 	if ((hba->model_info.chip & EMLXS_LANCER_CHIP) &&
1992 	    (obj_hdr.Id != OBJ_LANCER_ID)) {
1993 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
1994 		    "Invalid adapter model. chip=%x fwid=%x",
1995 		    hba->model_info.chip,
1996 		    obj_hdr.Id);
1997 		return (EMLXS_IMAGE_INCOMPATIBLE);
1998 	}
1999 
2000 	if (obj_hdr_in) {
2001 		bcopy(&obj_hdr, obj_hdr_in, sizeof (emlxs_obj_header_t));
2002 	}
2003 
2004 	return (0);
2005 
2006 } /* emlxs_obj_validate_image() */
2007 
2008 
2009 static int32_t
emlxs_obj_fw_download(emlxs_hba_t * hba,caddr_t buffer,uint32_t len,uint32_t offline)2010 emlxs_obj_fw_download(emlxs_hba_t *hba, caddr_t buffer, uint32_t len,
2011     uint32_t offline)
2012 {
2013 	emlxs_port_t *port = &PPORT;
2014 	uint32_t rval = 0;
2015 	MAILBOXQ *mbq = NULL;
2016 	MATCHMAP *mp = NULL;
2017 	uint32_t change_status = 0;
2018 
2019 	/* For now we will not take the driver offline during a download */
2020 	offline = 0;
2021 
2022 	if (hba->sli_mode != EMLXS_HBA_SLI4_MODE) {
2023 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
2024 		    "Invalid sli_mode. mode=%d", hba->sli_mode);
2025 		return (EMLXS_IMAGE_INCOMPATIBLE);
2026 	}
2027 
2028 	if (hba->model_info.chip & EMLXS_BE_CHIPS) {
2029 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
2030 		    "Invalid adapter model. chip=%x", hba->model_info.chip);
2031 		return (EMLXS_IMAGE_INCOMPATIBLE);
2032 	}
2033 
2034 	if (buffer == NULL || len == 0) {
2035 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
2036 		    "Empty buffer provided. buf=%p size=%d", buffer, len);
2037 		return (EMLXS_IMAGE_BAD);
2038 	}
2039 
2040 	rval = emlxs_obj_validate_image(hba, buffer, len, 0);
2041 
2042 	if (rval) {
2043 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_incompat_msg,
2044 		    "Invalid image provided.");
2045 		return (EMLXS_IMAGE_INCOMPATIBLE);
2046 	}
2047 
2048 	/* Allocate resources */
2049 
2050 	if ((mbq = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2051 	    KM_SLEEP)) == NULL) {
2052 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2053 		    "Unable to allocate mailbox buffer.");
2054 
2055 		offline = 0;
2056 		rval = EMLXS_IMAGE_FAILED;
2057 		goto done;
2058 	}
2059 
2060 	if ((mp = emlxs_mem_buf_alloc(hba, OBJ_MAX_XFER_SIZE)) == NULL) {
2061 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2062 		    "Unable to allocate flash buffer.");
2063 
2064 		offline = 0;
2065 		rval = EMLXS_IMAGE_FAILED;
2066 		goto done;
2067 	}
2068 
2069 	/*
2070 	 * Everything checks out, now to just do it
2071 	 */
2072 	if (offline) {
2073 		if (emlxs_offline(hba, 0) != FC_SUCCESS) {
2074 
2075 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2076 			    "Unable to take adapter offline.");
2077 
2078 			offline = 0;
2079 			rval = EMLXS_OFFLINE_FAILED;
2080 			goto done;
2081 		}
2082 	}
2083 
2084 	rval = emlxs_obj_flash_image(hba, buffer, len, mbq, mp, &change_status);
2085 
2086 done:
2087 	if (mbq) {
2088 		emlxs_mem_put(hba, MEM_MBOX, (void *)mbq);
2089 	}
2090 
2091 	if (mp) {
2092 		emlxs_mem_buf_free(hba, mp);
2093 	}
2094 
2095 	if (offline) {
2096 		(void) emlxs_online(hba);
2097 	}
2098 
2099 	if (rval == 0) {
2100 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_complete_msg,
2101 		    "Status good.");
2102 
2103 		switch (change_status) {
2104 		case CS_NO_RESET:
2105 			break;
2106 
2107 		case CS_REBOOT_RQD:
2108 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_fw_updated_msg,
2109 			    "The new firmware will not be activated until "
2110 			    "the adapter is power cycled.");
2111 			rval = EMLXS_REBOOT_REQUIRED;
2112 			break;
2113 
2114 		case CS_FW_RESET_RQD:
2115 		case CS_PROTO_RESET_RQD:
2116 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_fw_updated_msg,
2117 			    "Resetting all ports to activate new firmware.");
2118 
2119 			emlxs_sli4_hba_reset_all(hba, 0);
2120 		}
2121 	}
2122 
2123 	return (rval);
2124 
2125 } /* emlxs_obj_fw_download() */
2126 
2127 
2128 extern int32_t
emlxs_cfl_download(emlxs_hba_t * hba,uint32_t region,caddr_t buffer,uint32_t len)2129 emlxs_cfl_download(emlxs_hba_t *hba, uint32_t region, caddr_t buffer,
2130     uint32_t len)
2131 {
2132 	emlxs_port_t *port = &PPORT;
2133 	MAILBOXQ *mbox = NULL;
2134 	MAILBOX *mb;
2135 	uint32_t rval = 0;
2136 	uint32_t region_id;
2137 	uint32_t id;
2138 #ifdef EMLXS_BIG_ENDIAN
2139 	caddr_t local_buffer;
2140 	uint32_t *bptr1;
2141 	uint32_t *bptr2;
2142 	uint32_t i;
2143 #endif /* EMLXS_BIG_ENDIAN */
2144 
2145 	if (buffer == NULL || len == 0) {
2146 		return (EMLXS_IMAGE_BAD);
2147 	}
2148 
2149 #ifdef EMLXS_BIG_ENDIAN
2150 	/* We need to swap the image buffer before we start */
2151 
2152 	/*
2153 	 * Use KM_SLEEP to allocate a temporary buffer
2154 	 */
2155 	local_buffer = (caddr_t)kmem_zalloc(len, KM_SLEEP);
2156 
2157 	/* Perform a 32 bit swap of the image */
2158 	bptr1 = (uint32_t *)local_buffer;
2159 	bptr2 = (uint32_t *)buffer;
2160 
2161 	for (i = 0; i < (len / 4); i++) {
2162 		*bptr1 = SWAP32(*bptr2);
2163 		bptr1++;
2164 		bptr2++;
2165 	}
2166 
2167 	/* Replace the original buffer */
2168 	buffer = local_buffer;
2169 
2170 #endif /* EMLXS_BIG_ENDIAN */
2171 
2172 	if (len > 128) {
2173 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
2174 		    "Invalid image length: 0x%x > 128", len);
2175 
2176 		return (EMLXS_IMAGE_BAD);
2177 	}
2178 
2179 	/* Check the region number */
2180 	if ((region > 2) && (region != 0xff)) {
2181 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
2182 		    "Invalid region id: 0x%x", region);
2183 
2184 		return (EMLXS_IMAGE_BAD);
2185 
2186 	}
2187 
2188 	/* Check the image vendor id */
2189 	id = *(int32_t *)buffer;
2190 	if ((id & 0xffff) != 0x10df) {
2191 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_bad_msg,
2192 		    "Invalid image id: 0x%x", id);
2193 
2194 		return (EMLXS_IMAGE_BAD);
2195 	}
2196 
2197 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2198 	    KM_NOSLEEP)) == NULL) {
2199 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2200 		    "Unable to allocate mailbox buffer.");
2201 
2202 		rval = 1;
2203 
2204 		goto done;
2205 	}
2206 
2207 	mb = (MAILBOX *)mbox;
2208 
2209 	/*
2210 	 * Everything checks out, now to just do it
2211 	 */
2212 	if (emlxs_offline(hba, 0) != FC_SUCCESS) {
2213 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2214 		    "Unable to take HBA offline.");
2215 
2216 		rval = EMLXS_OFFLINE_FAILED;
2217 
2218 		goto done;
2219 	}
2220 
2221 	if (EMLXS_SLI_HBA_RESET(hba, 1, 1, 0) != FC_SUCCESS) {
2222 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2223 		    "Unable to restart adapter.");
2224 
2225 		rval = EMLXS_OFFLINE_FAILED;
2226 
2227 		goto done;
2228 	}
2229 
2230 	/* Check if default region is requested */
2231 	if (region == 0xff) {
2232 		/*
2233 		 * Sun-branded Helios and Zypher have different
2234 		 * default PCI region
2235 		 */
2236 		if ((hba->model_info.flags & EMLXS_ORACLE_BRANDED) &&
2237 		    (hba->model_info.chip &
2238 		    (EMLXS_HELIOS_CHIP | EMLXS_ZEPHYR_CHIP))) {
2239 			region = 2;
2240 		} else {
2241 			region = 0;
2242 		}
2243 	}
2244 
2245 	/* Set region id based on PCI region requested */
2246 	region_id = DEF_PCI_CFG_REGION_ID + region;
2247 
2248 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
2249 	    "PCI configuration: PCI%d region=%d id=0x%x size=%d", region,
2250 	    region_id, id, len);
2251 
2252 	/* Copy the data buffer to SLIM */
2253 	WRITE_SLIM_COPY(hba, (uint32_t *)buffer,
2254 	    (volatile uint32_t *)((volatile char *)hba->sli.sli3.slim_addr +
2255 	    sizeof (MAILBOX)), (len / sizeof (uint32_t)));
2256 
2257 #ifdef FMA_SUPPORT
2258 	if (emlxs_fm_check_acc_handle(hba, hba->sli.sli3.slim_acc_handle)
2259 	    != DDI_FM_OK) {
2260 		EMLXS_MSGF(EMLXS_CONTEXT,
2261 		    &emlxs_invalid_access_handle_msg, NULL);
2262 		rval = 1;
2263 	}
2264 #endif  /* FMA_SUPPORT */
2265 
2266 	emlxs_format_update_pci_cfg(hba, mbox, region_id, len);
2267 
2268 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2269 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2270 		    "Unable to update PCI configuration: Mailbox cmd=%x "
2271 		    "status=%x info=%d", mb->mbxCommand, mb->mbxStatus,
2272 		    mb->un.varUpdateCfg.rsp_info);
2273 
2274 		rval = 1;
2275 	}
2276 
2277 	(void) emlxs_online(hba);
2278 
2279 	if (rval == 0) {
2280 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_complete_msg,
2281 		    "Status good.");
2282 	}
2283 
2284 done:
2285 
2286 	if (mbox) {
2287 		kmem_free(mbox, sizeof (MAILBOXQ));
2288 	}
2289 
2290 #ifdef EMLXS_BIG_ENDIAN
2291 	/* Free the local buffer */
2292 	kmem_free(local_buffer, len);
2293 #endif /* EMLXS_BIG_ENDIAN */
2294 
2295 	return (rval);
2296 
2297 } /* emlxs_cfl_download */
2298 
2299 
2300 static uint32_t
emlxs_valid_cksum(uint32_t * StartAddr,uint32_t * EndAddr)2301 emlxs_valid_cksum(uint32_t *StartAddr, uint32_t *EndAddr)
2302 {
2303 	uint32_t Temp;
2304 	uint32_t CkSum;
2305 
2306 	EndAddr++;
2307 	CkSum = SLI_CKSUM_SEED;
2308 
2309 	CkSum = (CkSum >> 1) | (CkSum << 31);
2310 	while (StartAddr != EndAddr) {
2311 		CkSum = (CkSum << 1) | (CkSum >> 31);
2312 		Temp = *StartAddr;
2313 
2314 		CkSum ^= Temp;
2315 		StartAddr++;
2316 	}
2317 
2318 	return (CkSum << 1) | (CkSum >> 31);
2319 
2320 } /* emlxs_valid_cksum() */
2321 
2322 
2323 static void
emlxs_disp_aif_header(emlxs_hba_t * hba,PAIF_HDR AifHdr)2324 emlxs_disp_aif_header(emlxs_hba_t *hba, PAIF_HDR AifHdr)
2325 {
2326 	emlxs_port_t *port = &PPORT;
2327 
2328 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg, "AIF Header: ");
2329 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2330 	    "AIF Header: compress_br = 0x%x", AifHdr->CompressBr);
2331 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2332 	    "AIF Header: reloc_br = 0x%x", AifHdr->RelocBr);
2333 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2334 	    "AIF Header: zinit_br = 0x%x", AifHdr->ZinitBr);
2335 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2336 	    "AIF Header: entry_br = 0x%x", AifHdr->EntryBr);
2337 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2338 	    "AIF Header: area_id = 0x%x", AifHdr->Area_ID);
2339 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2340 	    "AIF Header: rosize = 0x%x", AifHdr->RoSize);
2341 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2342 	    "AIF Header: dbgsize = 0x%x", AifHdr->DbgSize);
2343 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2344 	    "AIF Header: zinitsize = 0x%x", AifHdr->ZinitSize);
2345 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2346 	    "AIF Header: dbgtype = 0x%x", AifHdr->DbgType);
2347 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2348 	    "AIF Header: imagebase = 0x%x", AifHdr->ImageBase);
2349 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2350 	    "AIF Header: area_size = 0x%x", AifHdr->Area_Size);
2351 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2352 	    "AIF Header: address_mode = 0x%x", AifHdr->AddressMode);
2353 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2354 	    "AIF Header: database = 0x%x", AifHdr->DataBase);
2355 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2356 	    "AIF Header: aversion = 0x%x", AifHdr->AVersion);
2357 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2358 	    "AIF Header: spare2 = 0x%x", AifHdr->Spare2);
2359 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2360 	    "AIF Header: debug_swi = 0x%x", AifHdr->DebugSwi);
2361 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2362 	    "AIF Header: zinitcode[0] = 0x%x", AifHdr->ZinitCode[0]);
2363 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2364 	    "AIF Header: zinitcode[1] = 0x%x", AifHdr->ZinitCode[1]);
2365 
2366 } /* emlxs_disp_aif_header() */
2367 
2368 
2369 
2370 static void
emlxs_dump_image_header(emlxs_hba_t * hba,PIMAGE_HDR image)2371 emlxs_dump_image_header(emlxs_hba_t *hba, PIMAGE_HDR image)
2372 {
2373 	emlxs_port_t *port = &PPORT;
2374 
2375 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg, "Img Header: ");
2376 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2377 	    "Img Header: BlockSize = 0x%x", image->BlockSize);
2378 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2379 	    "Img Header: PROG_ID Type = 0x%x", image->Id.Type);
2380 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2381 	    "Img Header: PROG_ID Id = 0x%x", image->Id.Id);
2382 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2383 	    "Img Header: PROG_ID Ver = 0x%x", image->Id.Ver);
2384 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2385 	    "Img Header: PROG_ID Rev = 0x%x", image->Id.Rev);
2386 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2387 	    "Img Header: PROG_ID revcomp = 0x%x", image->Id.un.revcomp);
2388 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2389 	    "Img Header: Flags = 0x%x", image->Flags);
2390 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2391 	    "Img Header: EntryAdr = 0x%x", image->EntryAdr);
2392 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2393 	    "Img Header: InitAdr = 0x%x", image->InitAdr);
2394 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2395 	    "Img Header: ExitAdr = 0x%x", image->ExitAdr);
2396 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2397 	    "Img Header: ImageBase = 0x%x", image->ImageBase);
2398 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2399 	    "Img Header: ImageSize = 0x%x", image->ImageSize);
2400 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2401 	    "Img Header: ZinitSize = 0x%x", image->ZinitSize);
2402 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2403 	    "Img Header: RelocSize = 0x%x", image->RelocSize);
2404 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2405 	    "Img Header: HdrCks = 0x%x", image->HdrCks);
2406 
2407 } /* emlxs_dump_image_header() */
2408 
2409 
2410 static void
emlxs_format_dump(emlxs_hba_t * hba,MAILBOXQ * mbq,uint32_t Type,uint32_t RegionId,uint32_t WordCount,uint32_t BaseAddr)2411 emlxs_format_dump(emlxs_hba_t *hba, MAILBOXQ *mbq, uint32_t Type,
2412     uint32_t RegionId, uint32_t WordCount, uint32_t BaseAddr)
2413 {
2414 
2415 	if (hba->sli_mode == EMLXS_HBA_SLI4_MODE) {
2416 		MAILBOX4 *mb = (MAILBOX4 *)mbq;
2417 
2418 		/* Clear the local dump_region */
2419 		bzero(hba->sli.sli4.dump_region.virt,
2420 		    hba->sli.sli4.dump_region.size);
2421 
2422 		bzero((void *) mb, MAILBOX_CMD_SLI4_BSIZE);
2423 
2424 		mb->mbxCommand = MBX_DUMP_MEMORY;
2425 		mb->un.varDmp4.type = Type;
2426 		mb->un.varDmp4.entry_index = BaseAddr;
2427 		mb->un.varDmp4.region_id = RegionId;
2428 
2429 		mb->un.varDmp4.available_cnt = min((WordCount*4),
2430 		    hba->sli.sli4.dump_region.size);
2431 		mb->un.varDmp4.addrHigh =
2432 		    PADDR_HI(hba->sli.sli4.dump_region.phys);
2433 		mb->un.varDmp4.addrLow =
2434 		    PADDR_LO(hba->sli.sli4.dump_region.phys);
2435 		mb->un.varDmp4.rsp_cnt = 0;
2436 
2437 		mb->mbxOwner = OWN_HOST;
2438 
2439 	} else {
2440 		MAILBOX *mb = (MAILBOX *)mbq;
2441 
2442 		bzero((void *)mb, MAILBOX_CMD_BSIZE);
2443 
2444 		mb->mbxCommand = MBX_DUMP_MEMORY;
2445 		mb->un.varDmp.type = Type;
2446 		mb->un.varDmp.region_id = RegionId;
2447 		mb->un.varDmp.word_cnt = WordCount;
2448 		mb->un.varDmp.base_adr = BaseAddr;
2449 		mb->mbxOwner = OWN_HOST;
2450 	}
2451 
2452 	mbq->mbox_cmpl = NULL; /* no cmpl needed */
2453 
2454 	return;
2455 
2456 } /* emlxs_format_dump() */
2457 
2458 
2459 /* ARGSUSED */
2460 static uint32_t
emlxs_start_abs_download(emlxs_hba_t * hba,PAIF_HDR AifHdr,caddr_t Buffer,uint32_t len,PWAKE_UP_PARMS WakeUpParms)2461 emlxs_start_abs_download(emlxs_hba_t *hba,
2462     PAIF_HDR AifHdr,
2463     caddr_t Buffer,
2464     uint32_t len,
2465     PWAKE_UP_PARMS WakeUpParms)
2466 {
2467 	emlxs_port_t *port = &PPORT;
2468 	uint32_t DlByteCount = AifHdr->RoSize + AifHdr->RwSize;
2469 	uint32_t *Src;
2470 	uint32_t *Dst;
2471 	caddr_t DataBuffer = NULL;
2472 	MAILBOXQ *mbox;
2473 	MAILBOX *mb;
2474 	uint32_t rval = 1;
2475 	uint32_t SegSize = DL_SLIM_SEG_BYTE_COUNT;
2476 	uint32_t DlToAddr = AifHdr->ImageBase;
2477 	uint32_t DlCount;
2478 	uint32_t i;
2479 	WAKE_UP_PARMS AbsWakeUpParms;
2480 	int32_t AbsChangeParams;
2481 
2482 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2483 	    "Performing absolute download...");
2484 
2485 	if ((DataBuffer = (caddr_t)kmem_zalloc(DL_SLIM_SEG_BYTE_COUNT,
2486 	    KM_NOSLEEP)) == NULL) {
2487 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2488 		    "Unable to allocate data buffer.");
2489 
2490 		return (rval);
2491 	}
2492 
2493 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2494 	    KM_NOSLEEP)) == NULL) {
2495 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2496 		    "Unable to allocate mailbox buffer.");
2497 
2498 		kmem_free(DataBuffer, DL_SLIM_SEG_BYTE_COUNT);
2499 
2500 		return (rval);
2501 	}
2502 	mb = (MAILBOX *)mbox;
2503 
2504 	AbsChangeParams = emlxs_build_parms(Buffer,
2505 	    &AbsWakeUpParms, len, AifHdr);
2506 
2507 	Buffer += sizeof (AIF_HDR);
2508 
2509 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg, "Erasing flash...");
2510 
2511 	if (AifHdr->ImageBase == 0x20000) {
2512 		/* DWC File */
2513 		emlxs_format_prog_flash(mbox, 0x20000, 0x50000, ERASE_FLASH, 0,
2514 		    0, 0, NULL, 0);
2515 	} else {
2516 		emlxs_format_prog_flash(mbox, DlToAddr, DlByteCount,
2517 		    ERASE_FLASH, 0, 0, 0, NULL, 0);
2518 	}
2519 
2520 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2521 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2522 		    "Unable to erase Flash: Mailbox cmd=%x status=%x",
2523 		    mb->mbxCommand, mb->mbxStatus);
2524 
2525 		rval = 1;
2526 
2527 		goto EXIT_ABS_DOWNLOAD;
2528 	}
2529 
2530 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_image_detail_msg,
2531 	    "Programming flash...");
2532 
2533 	while (DlByteCount) {
2534 
2535 		if (DlByteCount > SegSize) {
2536 			DlCount = SegSize;
2537 		} else {
2538 			DlCount = DlByteCount;
2539 		}
2540 		DlByteCount -= DlCount;
2541 
2542 		Dst = (uint32_t *)DataBuffer;
2543 		Src = (uint32_t *)Buffer;
2544 
2545 		for (i = 0; i < (DlCount / 4); i++) {
2546 			*Dst = *Src;
2547 			Dst++;
2548 			Src++;
2549 		}
2550 
2551 		WRITE_SLIM_COPY(hba, (uint32_t *)DataBuffer,
2552 		    (volatile uint32_t *)
2553 		    ((volatile char *)hba->sli.sli3.slim_addr +
2554 		    sizeof (MAILBOX)), (DlCount / sizeof (uint32_t)));
2555 
2556 		emlxs_format_prog_flash(mbox, DlToAddr, DlCount,
2557 		    PROGRAM_FLASH, (DlByteCount) ? 0 : 1, 0, DlCount, NULL, 0);
2558 
2559 		if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) !=
2560 		    MBX_SUCCESS) {
2561 			EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2562 			    "Unable to program Flash: Mailbox cmd=%x status=%x",
2563 			    mb->mbxCommand, mb->mbxStatus);
2564 
2565 			rval = 1;
2566 
2567 			goto EXIT_ABS_DOWNLOAD;
2568 		}
2569 
2570 		Buffer += DlCount;
2571 		DlToAddr += DlCount;
2572 	}
2573 
2574 #ifdef FMA_SUPPORT
2575 	if (emlxs_fm_check_acc_handle(hba, hba->sli.sli3.slim_acc_handle)
2576 	    != DDI_FM_OK) {
2577 		EMLXS_MSGF(EMLXS_CONTEXT,
2578 		    &emlxs_invalid_access_handle_msg, NULL);
2579 
2580 		rval = 1;
2581 
2582 		goto EXIT_ABS_DOWNLOAD;
2583 	}
2584 #endif  /* FMA_SUPPORT */
2585 
2586 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg, "Updating params...");
2587 
2588 	if (AbsChangeParams) {
2589 		rval =
2590 		    emlxs_update_wakeup_parms(hba, &AbsWakeUpParms,
2591 		    WakeUpParms);
2592 	}
2593 
2594 EXIT_ABS_DOWNLOAD:
2595 	if (DataBuffer) {
2596 		kmem_free(DataBuffer, DL_SLIM_SEG_BYTE_COUNT);
2597 	}
2598 
2599 	if (mbox) {
2600 		kmem_free(mbox, sizeof (MAILBOXQ));
2601 	}
2602 
2603 	return (rval);
2604 
2605 } /* emlxs_start_abs_download() */
2606 
2607 
2608 /* ARGSUSED */
2609 static void
emlxs_format_prog_flash(MAILBOXQ * mbq,uint32_t Base,uint32_t DlByteCount,uint32_t Function,uint32_t Complete,uint32_t BdeAddress,uint32_t BdeSize,PROG_ID * ProgId,uint32_t keep)2610 emlxs_format_prog_flash(MAILBOXQ *mbq,
2611     uint32_t Base,
2612     uint32_t DlByteCount,
2613     uint32_t Function,
2614     uint32_t Complete,
2615     uint32_t BdeAddress,
2616     uint32_t BdeSize,
2617     PROG_ID *ProgId,
2618     uint32_t keep)
2619 {
2620 	MAILBOX *mb = (MAILBOX *)mbq;
2621 
2622 	bzero((void *)mb, MAILBOX_CMD_BSIZE);
2623 
2624 	if (ProgId) {
2625 		mb->mbxCommand = MBX_DOWN_LOAD;
2626 	} else {
2627 		mb->mbxCommand = MBX_LOAD_SM;
2628 	}
2629 
2630 	mb->un.varLdSM.load_cmplt = Complete;
2631 	mb->un.varLdSM.method = DL_FROM_SLIM;
2632 	mb->un.varLdSM.update_flash = 1;
2633 	mb->un.varLdSM.erase_or_prog = Function;
2634 	mb->un.varLdSM.dl_to_adr = Base;
2635 	mb->un.varLdSM.dl_len = DlByteCount;
2636 	mb->un.varLdSM.keep = keep;
2637 
2638 	if (BdeSize) {
2639 		mb->un.varLdSM.un.dl_from_slim_offset = DL_FROM_SLIM_OFFSET;
2640 	} else if (ProgId) {
2641 		mb->un.varLdSM.un.prog_id = *ProgId;
2642 	} else {
2643 		mb->un.varLdSM.un.dl_from_slim_offset = 0;
2644 	}
2645 
2646 	mb->mbxOwner = OWN_HOST;
2647 	mbq->mbox_cmpl = NULL;
2648 
2649 } /* emlxs_format_prog_flash() */
2650 
2651 
2652 static void
emlxs_format_update_parms(MAILBOXQ * mbq,PWAKE_UP_PARMS WakeUpParms)2653 emlxs_format_update_parms(MAILBOXQ *mbq, PWAKE_UP_PARMS WakeUpParms)
2654 {
2655 	MAILBOX *mb = (MAILBOX *)mbq;
2656 
2657 	bzero((void *)mb, MAILBOX_CMD_BSIZE);
2658 
2659 	mb->mbxCommand = MBX_UPDATE_CFG;
2660 	mb->un.varUpdateCfg.req_type = UPDATE_DATA;
2661 	mb->un.varUpdateCfg.region_id = WAKE_UP_PARMS_REGION_ID;
2662 	mb->un.varUpdateCfg.entry_len = sizeof (WAKE_UP_PARMS);
2663 	mb->un.varUpdateCfg.byte_len = sizeof (WAKE_UP_PARMS);
2664 
2665 	bcopy((caddr_t)WakeUpParms,
2666 	    (caddr_t)&(mb->un.varUpdateCfg.cfg_data),
2667 	    sizeof (WAKE_UP_PARMS));
2668 	mbq->mbox_cmpl = NULL;
2669 
2670 } /* emlxs_format_update_parms () */
2671 
2672 
2673 /* ARGSUSED */
2674 static void
emlxs_format_update_pci_cfg(emlxs_hba_t * hba,MAILBOXQ * mbq,uint32_t region_id,uint32_t size)2675 emlxs_format_update_pci_cfg(emlxs_hba_t *hba, MAILBOXQ *mbq,
2676     uint32_t region_id, uint32_t size)
2677 {
2678 	MAILBOX *mb = (MAILBOX *)mbq;
2679 
2680 	bzero((void *)mb, MAILBOX_CMD_BSIZE);
2681 
2682 	mb->mbxCommand = MBX_UPDATE_CFG;
2683 	mb->un.varUpdateCfg.Vbit = 1;
2684 	mb->un.varUpdateCfg.Obit = 1;
2685 	mb->un.varUpdateCfg.cfg_data = DL_FROM_SLIM_OFFSET;
2686 	mb->un.varUpdateCfg.req_type = UPDATE_DATA;
2687 	mb->un.varUpdateCfg.region_id = region_id;
2688 	mb->un.varUpdateCfg.entry_len = size;
2689 	mb->un.varUpdateCfg.byte_len = size;
2690 	mbq->mbox_cmpl = NULL;
2691 
2692 } /* emlxs_format_update_pci_cfg() */
2693 
2694 
2695 
2696 static uint32_t
emlxs_update_boot_wakeup_parms(emlxs_hba_t * hba,PWAKE_UP_PARMS WakeUpParms,PROG_ID * prog_id,uint32_t proc_erom)2697 emlxs_update_boot_wakeup_parms(emlxs_hba_t *hba, PWAKE_UP_PARMS WakeUpParms,
2698     PROG_ID * prog_id, uint32_t proc_erom)
2699 {
2700 	emlxs_port_t *port = &PPORT;
2701 	MAILBOX *mb;
2702 	MAILBOXQ *mbox;
2703 	uint32_t rval = 0;
2704 	PROG_ID old_prog_id;
2705 
2706 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2707 	    KM_NOSLEEP)) == NULL) {
2708 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2709 		    "Unable to allocate mailbox buffer.");
2710 
2711 		return (1);
2712 	}
2713 
2714 	mb = (MAILBOX *)mbox;
2715 
2716 	if (proc_erom && !(hba->model_info.chip &
2717 	    (EMLXS_DRAGONFLY_CHIP | EMLXS_CENTAUR_CHIP))) {
2718 		WakeUpParms->u1.EROM_prog_id = *prog_id;
2719 		(void) emlxs_update_exp_rom(hba, WakeUpParms);
2720 	}
2721 
2722 	old_prog_id = WakeUpParms->u0.boot_bios_id;
2723 	WakeUpParms->u0.boot_bios_id = *prog_id;
2724 
2725 	emlxs_format_update_parms(mbox, WakeUpParms);
2726 
2727 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2728 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2729 		    "Unable to update boot wakeup parms: Mailbox cmd=%x "
2730 		    "status=%x", mb->mbxCommand, mb->mbxStatus);
2731 
2732 		WakeUpParms->u0.boot_bios_id = old_prog_id;
2733 		rval = 1;
2734 	}
2735 
2736 	if (mbox) {
2737 		kmem_free(mbox, sizeof (MAILBOXQ));
2738 	}
2739 
2740 	return (rval);
2741 
2742 } /* emlxs_update_boot_wakeup_parms() */
2743 
2744 
2745 
2746 static uint32_t
emlxs_update_ff_wakeup_parms(emlxs_hba_t * hba,PWAKE_UP_PARMS WakeUpParms,PROG_ID * prog_id)2747 emlxs_update_ff_wakeup_parms(emlxs_hba_t *hba, PWAKE_UP_PARMS WakeUpParms,
2748     PROG_ID *prog_id)
2749 {
2750 	emlxs_port_t *port = &PPORT;
2751 	uint32_t rval = 0;
2752 	MAILBOXQ *mbox;
2753 	MAILBOX *mb;
2754 	PROG_ID old_prog_id;
2755 
2756 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2757 	    KM_NOSLEEP)) == NULL) {
2758 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2759 		    "Unable to allocate mailbox buffer.");
2760 
2761 		return (1);
2762 	}
2763 
2764 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
2765 	    "FF: Updating parms...");
2766 
2767 	mb = (MAILBOX *)mbox;
2768 
2769 	old_prog_id = WakeUpParms->prog_id;
2770 	WakeUpParms->prog_id = *prog_id;
2771 
2772 	emlxs_format_update_parms(mbox, WakeUpParms);
2773 
2774 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2775 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2776 		    "Unable to update wakeup parameters: Mailbox cmd=%x "
2777 		    "status=%x", mb->mbxCommand, mb->mbxStatus);
2778 
2779 		WakeUpParms->prog_id = old_prog_id;
2780 		rval = 1;
2781 	}
2782 
2783 	if (mbox) {
2784 		kmem_free(mbox, sizeof (MAILBOXQ));
2785 	}
2786 
2787 	return (rval);
2788 
2789 } /* emlxs_update_ff_wakeup_parms() */
2790 
2791 
2792 static uint32_t
emlxs_update_sli1_wakeup_parms(emlxs_hba_t * hba,PWAKE_UP_PARMS WakeUpParms,PROG_ID * prog_id)2793 emlxs_update_sli1_wakeup_parms(emlxs_hba_t *hba, PWAKE_UP_PARMS WakeUpParms,
2794     PROG_ID * prog_id)
2795 {
2796 	emlxs_port_t *port = &PPORT;
2797 	uint32_t rval = 0;
2798 	MAILBOXQ *mbox;
2799 	MAILBOX *mb;
2800 	PROG_ID old_prog_id;
2801 
2802 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2803 	    KM_NOSLEEP)) == NULL) {
2804 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2805 		    "Unable to allocate mailbox buffer.");
2806 
2807 		return (1);
2808 	}
2809 
2810 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
2811 	    "SLI1: Updating parms...");
2812 
2813 	mb = (MAILBOX *)mbox;
2814 
2815 	old_prog_id = WakeUpParms->sli1_prog_id;
2816 	WakeUpParms->sli1_prog_id = *prog_id;
2817 
2818 	emlxs_format_update_parms(mbox, WakeUpParms);
2819 
2820 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2821 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2822 		    "Unable to update wakeup parameters. Mailbox cmd=%x "
2823 		    "status=%x", mb->mbxCommand, mb->mbxStatus);
2824 
2825 		WakeUpParms->sli1_prog_id = old_prog_id;
2826 		rval = 1;
2827 	}
2828 
2829 	if (mbox) {
2830 		kmem_free(mbox, sizeof (MAILBOXQ));
2831 	}
2832 
2833 	return (rval);
2834 
2835 } /* emlxs_update_sli1_wakeup_parms() */
2836 
2837 
2838 static uint32_t
emlxs_update_sli2_wakeup_parms(emlxs_hba_t * hba,PWAKE_UP_PARMS WakeUpParms,PROG_ID * prog_id)2839 emlxs_update_sli2_wakeup_parms(emlxs_hba_t *hba, PWAKE_UP_PARMS WakeUpParms,
2840     PROG_ID * prog_id)
2841 {
2842 	emlxs_port_t *port = &PPORT;
2843 	uint32_t rval = 0;
2844 	MAILBOXQ *mbox;
2845 	MAILBOX *mb;
2846 	PROG_ID old_prog_id;
2847 
2848 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2849 	    KM_NOSLEEP)) == NULL) {
2850 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2851 		    "Unable to allocate mailbox buffer.");
2852 
2853 		return (1);
2854 	}
2855 
2856 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
2857 	    "SLI2: Updating parms...");
2858 
2859 	mb = (MAILBOX *)mbox;
2860 
2861 	old_prog_id = WakeUpParms->sli2_prog_id;
2862 	WakeUpParms->sli2_prog_id = *prog_id;
2863 
2864 	emlxs_format_update_parms(mbox, WakeUpParms);
2865 
2866 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2867 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2868 		    "Unable to update wakeup parameters. Mailbox cmd=%x "
2869 		    "status=%x", mb->mbxCommand, mb->mbxStatus);
2870 
2871 		WakeUpParms->sli2_prog_id = old_prog_id;
2872 		rval = 1;
2873 	}
2874 
2875 	if (mbox) {
2876 		kmem_free(mbox, sizeof (MAILBOXQ));
2877 	}
2878 
2879 	return (rval);
2880 
2881 } /* emlxs_update_sli2_wakeup_parms() */
2882 
2883 
2884 static uint32_t
emlxs_update_sli3_wakeup_parms(emlxs_hba_t * hba,PWAKE_UP_PARMS WakeUpParms,PROG_ID * prog_id)2885 emlxs_update_sli3_wakeup_parms(emlxs_hba_t *hba, PWAKE_UP_PARMS WakeUpParms,
2886     PROG_ID *prog_id)
2887 {
2888 	emlxs_port_t *port = &PPORT;
2889 	uint32_t rval = 0;
2890 	MAILBOXQ *mbox;
2891 	MAILBOX *mb;
2892 	PROG_ID old_prog_id;
2893 
2894 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2895 	    KM_NOSLEEP)) == NULL) {
2896 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2897 		    "Unable to allocate mailbox buffer.");
2898 
2899 		return (1);
2900 	}
2901 
2902 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
2903 	    "SLI3: Updating parms...");
2904 
2905 	mb = (MAILBOX *)mbox;
2906 
2907 	old_prog_id = WakeUpParms->sli3_prog_id;
2908 	WakeUpParms->sli3_prog_id = *prog_id;
2909 
2910 	emlxs_format_update_parms(mbox, WakeUpParms);
2911 
2912 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2913 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2914 		    "Unable to update wakeup parameters. Mailbox cmd=%x "
2915 		    "status=%x", mb->mbxCommand, mb->mbxStatus);
2916 
2917 		WakeUpParms->sli3_prog_id = old_prog_id;
2918 		rval = 1;
2919 	}
2920 
2921 	if (mbox) {
2922 		kmem_free(mbox, sizeof (MAILBOXQ));
2923 	}
2924 
2925 	return (rval);
2926 
2927 } /* emlxs_update_sli3_wakeup_parms() */
2928 
2929 
2930 static uint32_t
emlxs_update_sli4_wakeup_parms(emlxs_hba_t * hba,PWAKE_UP_PARMS WakeUpParms,PROG_ID * prog_id)2931 emlxs_update_sli4_wakeup_parms(emlxs_hba_t *hba, PWAKE_UP_PARMS WakeUpParms,
2932     PROG_ID *prog_id)
2933 {
2934 	emlxs_port_t *port = &PPORT;
2935 	uint32_t rval = 0;
2936 	MAILBOXQ *mbox;
2937 	MAILBOX *mb;
2938 	PROG_ID old_prog_id;
2939 
2940 	if ((mbox = (MAILBOXQ *)kmem_zalloc(sizeof (MAILBOXQ),
2941 	    KM_NOSLEEP)) == NULL) {
2942 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2943 		    "Unable to allocate mailbox buffer.");
2944 
2945 		return (1);
2946 	}
2947 
2948 	EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_msg,
2949 	    "SLI4: Updating parms...");
2950 
2951 	mb = (MAILBOX *)mbox;
2952 
2953 	old_prog_id = WakeUpParms->sli4_prog_id;
2954 	WakeUpParms->sli4_prog_id = *prog_id;
2955 
2956 	emlxs_format_update_parms(mbox, WakeUpParms);
2957 
2958 	if (EMLXS_SLI_ISSUE_MBOX_CMD(hba, mbox, MBX_WAIT, 0) != MBX_SUCCESS) {
2959 		EMLXS_MSGF(EMLXS_CONTEXT, &emlxs_download_failed_msg,
2960 		    "Unable to update wakeup parameters. Mailbox cmd=%x "
2961 		    "status=%x", mb->mbxCommand, mb->mbxStatus);
2962 
2963 		WakeUpParms->sli4_prog_id = old_prog_id;
2964&