3 * linux/drivers/scsi/esas2r/esas2r_flash.c
4 * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
6 * Copyright (c) 2001-2013 ATTO Technology, Inc.
7 * (mailto:linuxdrivers@attotech.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
47 /* local macro defs */
48 #define esas2r_nvramcalc_cksum(n) \
49 (esas2r_calc_byte_cksum((u8 *)(n), sizeof(struct esas2r_sas_nvram), \
51 #define esas2r_nvramcalc_xor_cksum(n) \
52 (esas2r_calc_byte_xor_cksum((u8 *)(n), \
53 sizeof(struct esas2r_sas_nvram), 0))
55 #define ESAS2R_FS_DRVR_VER 2
57 static struct esas2r_sas_nvram default_sas_nvram
= {
58 { 'E', 'S', 'A', 'S' }, /* signature */
59 SASNVR_VERSION
, /* version */
61 31, /* max_lun_for_target */
62 SASNVR_PCILAT_MAX
, /* pci_latency */
63 SASNVR1_BOOT_DRVR
, /* options1 */
64 SASNVR2_HEARTBEAT
| SASNVR2_SINGLE_BUS
/* options2 */
65 | SASNVR2_SW_MUX_CTRL
,
66 SASNVR_COAL_DIS
, /* int_coalescing */
67 SASNVR_CMDTHR_NONE
, /* cmd_throttle */
68 3, /* dev_wait_time */
69 1, /* dev_wait_count */
70 0, /* spin_up_delay */
71 0, /* ssp_align_rate */
72 { 0x50, 0x01, 0x08, 0x60, /* sas_addr */
73 0x00, 0x00, 0x00, 0x00 },
74 { SASNVR_SPEED_AUTO
}, /* phy_speed */
75 { SASNVR_MUX_DISABLED
}, /* SAS multiplexing */
76 { 0 }, /* phy_flags */
77 SASNVR_SORT_SAS_ADDR
, /* sort_type */
78 3, /* dpm_reqcmd_lmt */
79 3, /* dpm_stndby_time */
80 0, /* dpm_active_time */
81 { 0 }, /* phy_target_id */
82 SASNVR_VSMH_DISABLED
, /* virt_ses_mode */
83 SASNVR_RWM_DEFAULT
, /* read_write_mode */
84 0, /* link down timeout */
88 static u8 cmd_to_fls_func
[] = {
97 static u8
esas2r_calc_byte_xor_cksum(u8
*addr
, u32 len
, u8 seed
)
100 u8
*p
= (u8
*)&cksum
;
103 if (((uintptr_t)addr
& 3) == 0)
106 cksum
= cksum
^ *addr
;
110 while (len
>= sizeof(u32
)) {
111 cksum
= cksum
^ *(u32
*)addr
;
116 cksum
= cksum
^ *addr
;
119 return p
[0] ^ p
[1] ^ p
[2] ^ p
[3];
122 static u8
esas2r_calc_byte_cksum(void *addr
, u32 len
, u8 seed
)
128 cksum
= cksum
+ p
[len
];
132 /* Interrupt callback to process FM API write requests. */
133 static void esas2r_fmapi_callback(struct esas2r_adapter
*a
,
134 struct esas2r_request
*rq
)
136 struct atto_vda_flash_req
*vrq
= &rq
->vrq
->flash
;
137 struct esas2r_flash_context
*fc
=
138 (struct esas2r_flash_context
*)rq
->interrupt_cx
;
140 if (rq
->req_stat
== RS_SUCCESS
) {
141 /* Last request was successful. See what to do now. */
142 switch (vrq
->sub_func
) {
143 case VDA_FLASH_BEGINW
:
144 if (fc
->sgc
.cur_offset
== NULL
)
147 vrq
->sub_func
= VDA_FLASH_WRITE
;
148 rq
->req_stat
= RS_PENDING
;
151 case VDA_FLASH_WRITE
:
153 vrq
->sub_func
= VDA_FLASH_COMMIT
;
154 rq
->req_stat
= RS_PENDING
;
155 rq
->interrupt_cb
= fc
->interrupt_cb
;
163 if (rq
->req_stat
!= RS_PENDING
)
165 * All done. call the real callback to complete the FM API
166 * request. We should only get here if a BEGINW or WRITE
169 (*fc
->interrupt_cb
)(a
, rq
);
173 * Build a flash request based on the flash context. The request status
174 * is filled in on an error.
176 static void build_flash_msg(struct esas2r_adapter
*a
,
177 struct esas2r_request
*rq
)
179 struct esas2r_flash_context
*fc
=
180 (struct esas2r_flash_context
*)rq
->interrupt_cx
;
181 struct esas2r_sg_context
*sgc
= &fc
->sgc
;
184 /* calculate the checksum */
185 if (fc
->func
== VDA_FLASH_BEGINW
) {
187 cksum
= esas2r_calc_byte_xor_cksum(sgc
->cur_offset
,
190 rq
->interrupt_cb
= esas2r_fmapi_callback
;
192 rq
->interrupt_cb
= fc
->interrupt_cb
;
194 esas2r_build_flash_req(a
,
201 esas2r_rq_free_sg_lists(rq
, a
);
204 * remember the length we asked for. we have to keep track of
205 * the current amount done so we know how much to compare when
206 * doing the verification phase.
208 fc
->curr_len
= fc
->sgc
.length
;
210 if (sgc
->cur_offset
) {
211 /* setup the S/G context to build the S/G table */
212 esas2r_sgc_init(sgc
, a
, rq
, &rq
->vrq
->flash
.data
.sge
[0]);
214 if (!esas2r_build_sg_list(a
, rq
, sgc
)) {
215 rq
->req_stat
= RS_BUSY
;
222 /* update the flsh_addr to the next one to write to */
223 fc
->flsh_addr
+= fc
->curr_len
;
226 /* determine the method to process the flash request */
227 static bool load_image(struct esas2r_adapter
*a
, struct esas2r_request
*rq
)
230 * assume we have more to do. if we return with the status set to
231 * RS_PENDING, FM API tasks will continue.
233 rq
->req_stat
= RS_PENDING
;
234 if (a
->flags
& AF_DEGRADED_MODE
)
235 /* not suppported for now */;
237 build_flash_msg(a
, rq
);
239 return rq
->req_stat
== RS_PENDING
;
242 /* boot image fixer uppers called before downloading the image. */
243 static void fix_bios(struct esas2r_adapter
*a
, struct esas2r_flash_img
*fi
)
245 struct esas2r_component_header
*ch
= &fi
->cmp_hdr
[CH_IT_BIOS
];
246 struct esas2r_pc_image
*pi
;
247 struct esas2r_boot_header
*bh
;
249 pi
= (struct esas2r_pc_image
*)((u8
*)fi
+ ch
->image_offset
);
251 (struct esas2r_boot_header
*)((u8
*)pi
+
252 le16_to_cpu(pi
->header_offset
));
253 bh
->device_id
= cpu_to_le16(a
->pcid
->device
);
255 /* Recalculate the checksum in the PNP header if there */
256 if (pi
->pnp_offset
) {
257 u8
*pnp_header_bytes
=
258 ((u8
*)pi
+ le16_to_cpu(pi
->pnp_offset
));
260 /* Identifier - dword that starts at byte 10 */
261 *((u32
*)&pnp_header_bytes
[10]) =
262 cpu_to_le32(MAKEDWORD(a
->pcid
->subsystem_vendor
,
263 a
->pcid
->subsystem_device
));
265 /* Checksum - byte 9 */
266 pnp_header_bytes
[9] -= esas2r_calc_byte_cksum(pnp_header_bytes
,
270 /* Recalculate the checksum needed by the PC */
271 pi
->checksum
= pi
->checksum
-
272 esas2r_calc_byte_cksum((u8
*)pi
, ch
->length
, 0);
275 static void fix_efi(struct esas2r_adapter
*a
, struct esas2r_flash_img
*fi
)
277 struct esas2r_component_header
*ch
= &fi
->cmp_hdr
[CH_IT_EFI
];
278 u32 len
= ch
->length
;
279 u32 offset
= ch
->image_offset
;
280 struct esas2r_efi_image
*ei
;
281 struct esas2r_boot_header
*bh
;
286 ei
= (struct esas2r_efi_image
*)((u8
*)fi
+ offset
);
287 bh
= (struct esas2r_boot_header
*)((u8
*)ei
+
290 bh
->device_id
= cpu_to_le16(a
->pcid
->device
);
291 thislen
= (u32
)le16_to_cpu(bh
->image_length
) * 512;
301 /* Complete a FM API request with the specified status. */
302 static bool complete_fmapi_req(struct esas2r_adapter
*a
,
303 struct esas2r_request
*rq
, u8 fi_stat
)
305 struct esas2r_flash_context
*fc
=
306 (struct esas2r_flash_context
*)rq
->interrupt_cx
;
307 struct esas2r_flash_img
*fi
= fc
->fi
;
309 fi
->status
= fi_stat
;
310 fi
->driver_error
= rq
->req_stat
;
311 rq
->interrupt_cb
= NULL
;
312 rq
->req_stat
= RS_SUCCESS
;
314 if (fi_stat
!= FI_STAT_IMG_VER
)
315 memset(fc
->scratch
, 0, FM_BUF_SZ
);
317 esas2r_enable_heartbeat(a
);
318 esas2r_lock_clear_flags(&a
->flags
, AF_FLASH_LOCK
);
322 /* Process each phase of the flash download process. */
323 static void fw_download_proc(struct esas2r_adapter
*a
,
324 struct esas2r_request
*rq
)
326 struct esas2r_flash_context
*fc
=
327 (struct esas2r_flash_context
*)rq
->interrupt_cx
;
328 struct esas2r_flash_img
*fi
= fc
->fi
;
329 struct esas2r_component_header
*ch
;
333 /* If the previous operation failed, just return. */
334 if (rq
->req_stat
!= RS_SUCCESS
)
338 * If an upload just completed and the compare length is non-zero,
339 * then we just read back part of the image we just wrote. verify the
340 * section and continue reading until the entire image is verified.
342 if (fc
->func
== VDA_FLASH_READ
344 ch
= &fi
->cmp_hdr
[fc
->comp_typ
];
347 q
= (u8
*)fi
/* start of the whole gob */
348 + ch
->image_offset
/* start of the current image */
349 + ch
->length
/* end of the current image */
350 - fc
->cmp_len
; /* where we are now */
353 * NOTE - curr_len is the exact count of bytes for the read
354 * even when the end is read and its not a full buffer
356 for (len
= fc
->curr_len
; len
; len
--)
360 fc
->cmp_len
-= fc
->curr_len
; /* # left to compare */
362 /* Update fc and determine the length for the next upload */
363 if (fc
->cmp_len
> FM_BUF_SZ
)
364 fc
->sgc
.length
= FM_BUF_SZ
;
366 fc
->sgc
.length
= fc
->cmp_len
;
368 fc
->sgc
.cur_offset
= fc
->sgc_offset
+
369 ((u8
*)fc
->scratch
- (u8
*)fi
);
373 * This code uses a 'while' statement since the next component may
374 * have a length = zero. This can happen since some components are
375 * not required. At the end of this 'while' we set up the length
376 * for the next request and therefore sgc.length can be = 0.
378 while (fc
->sgc
.length
== 0) {
379 ch
= &fi
->cmp_hdr
[fc
->comp_typ
];
382 case FMTSK_ERASE_BOOT
:
383 /* the BIOS image is written next */
384 ch
= &fi
->cmp_hdr
[CH_IT_BIOS
];
388 fc
->task
= FMTSK_WRTBIOS
;
389 fc
->func
= VDA_FLASH_BEGINW
;
390 fc
->comp_typ
= CH_IT_BIOS
;
391 fc
->flsh_addr
= FLS_OFFSET_BOOT
;
392 fc
->sgc
.length
= ch
->length
;
393 fc
->sgc
.cur_offset
= fc
->sgc_offset
+
399 * The BIOS image has been written - read it and
402 fc
->task
= FMTSK_READBIOS
;
403 fc
->func
= VDA_FLASH_READ
;
404 fc
->flsh_addr
= FLS_OFFSET_BOOT
;
405 fc
->cmp_len
= ch
->length
;
406 fc
->sgc
.length
= FM_BUF_SZ
;
407 fc
->sgc
.cur_offset
= fc
->sgc_offset
408 + ((u8
*)fc
->scratch
-
415 * Mark the component header status for the image
418 ch
->status
= CH_STAT_SUCCESS
;
420 /* The MAC image is written next */
421 ch
= &fi
->cmp_hdr
[CH_IT_MAC
];
425 fc
->task
= FMTSK_WRTMAC
;
426 fc
->func
= VDA_FLASH_BEGINW
;
427 fc
->comp_typ
= CH_IT_MAC
;
428 fc
->flsh_addr
= FLS_OFFSET_BOOT
429 + fi
->cmp_hdr
[CH_IT_BIOS
].length
;
430 fc
->sgc
.length
= ch
->length
;
431 fc
->sgc
.cur_offset
= fc
->sgc_offset
+
436 /* The MAC image has been written - read and verify */
437 fc
->task
= FMTSK_READMAC
;
438 fc
->func
= VDA_FLASH_READ
;
439 fc
->flsh_addr
-= ch
->length
;
440 fc
->cmp_len
= ch
->length
;
441 fc
->sgc
.length
= FM_BUF_SZ
;
442 fc
->sgc
.cur_offset
= fc
->sgc_offset
443 + ((u8
*)fc
->scratch
-
450 * Mark the component header status for the image
453 ch
->status
= CH_STAT_SUCCESS
;
455 /* The EFI image is written next */
456 ch
= &fi
->cmp_hdr
[CH_IT_EFI
];
460 fc
->task
= FMTSK_WRTEFI
;
461 fc
->func
= VDA_FLASH_BEGINW
;
462 fc
->comp_typ
= CH_IT_EFI
;
463 fc
->flsh_addr
= FLS_OFFSET_BOOT
464 + fi
->cmp_hdr
[CH_IT_BIOS
].length
465 + fi
->cmp_hdr
[CH_IT_MAC
].length
;
466 fc
->sgc
.length
= ch
->length
;
467 fc
->sgc
.cur_offset
= fc
->sgc_offset
+
472 /* The EFI image has been written - read and verify */
473 fc
->task
= FMTSK_READEFI
;
474 fc
->func
= VDA_FLASH_READ
;
475 fc
->flsh_addr
-= ch
->length
;
476 fc
->cmp_len
= ch
->length
;
477 fc
->sgc
.length
= FM_BUF_SZ
;
478 fc
->sgc
.cur_offset
= fc
->sgc_offset
479 + ((u8
*)fc
->scratch
-
486 * Mark the component header status for the image
489 ch
->status
= CH_STAT_SUCCESS
;
491 /* The CFG image is written next */
492 ch
= &fi
->cmp_hdr
[CH_IT_CFG
];
496 fc
->task
= FMTSK_WRTCFG
;
497 fc
->func
= VDA_FLASH_BEGINW
;
498 fc
->comp_typ
= CH_IT_CFG
;
499 fc
->flsh_addr
= FLS_OFFSET_CPYR
- ch
->length
;
500 fc
->sgc
.length
= ch
->length
;
501 fc
->sgc
.cur_offset
= fc
->sgc_offset
+
506 /* The CFG image has been written - read and verify */
507 fc
->task
= FMTSK_READCFG
;
508 fc
->func
= VDA_FLASH_READ
;
509 fc
->flsh_addr
= FLS_OFFSET_CPYR
- ch
->length
;
510 fc
->cmp_len
= ch
->length
;
511 fc
->sgc
.length
= FM_BUF_SZ
;
512 fc
->sgc
.cur_offset
= fc
->sgc_offset
513 + ((u8
*)fc
->scratch
-
520 * Mark the component header status for the image
523 ch
->status
= CH_STAT_SUCCESS
;
526 * The download is complete. If in degraded mode,
527 * attempt a chip reset.
529 if (a
->flags
& AF_DEGRADED_MODE
)
530 esas2r_local_reset_adapter(a
);
532 a
->flash_ver
= fi
->cmp_hdr
[CH_IT_BIOS
].version
;
533 esas2r_print_flash_rev(a
);
535 /* Update the type of boot image on the card */
536 memcpy(a
->image_type
, fi
->rel_version
,
537 sizeof(fi
->rel_version
));
538 complete_fmapi_req(a
, rq
, FI_STAT_SUCCESS
);
542 /* If verifying, don't try reading more than what's there */
543 if (fc
->func
== VDA_FLASH_READ
544 && fc
->sgc
.length
> fc
->cmp_len
)
545 fc
->sgc
.length
= fc
->cmp_len
;
548 /* Build the request to perform the next action */
549 if (!load_image(a
, rq
)) {
551 if (fc
->comp_typ
< fi
->num_comps
) {
552 ch
= &fi
->cmp_hdr
[fc
->comp_typ
];
553 ch
->status
= CH_STAT_FAILED
;
556 complete_fmapi_req(a
, rq
, FI_STAT_FAILED
);
560 /* Determine the flash image adaptyp for this adapter */
561 static u8
get_fi_adap_type(struct esas2r_adapter
*a
)
565 /* use the device ID to get the correct adap_typ for this HBA */
566 switch (a
->pcid
->device
) {
567 case ATTO_DID_INTEL_IOP348
:
568 type
= FI_AT_SUN_LAKE
;
571 case ATTO_DID_MV_88RC9580
:
572 case ATTO_DID_MV_88RC9580TS
:
573 case ATTO_DID_MV_88RC9580TSE
:
574 case ATTO_DID_MV_88RC9580TL
:
575 type
= FI_AT_MV_9580
;
586 /* Size of config + copyright + flash_ver images, 0 for failure. */
587 static u32
chk_cfg(u8
*cfg
, u32 length
, u32
*flash_ver
)
589 u16
*pw
= (u16
*)cfg
- 1;
603 type
= le16_to_cpu(*pw
--);
604 size
= le16_to_cpu(*pw
--);
608 && type
!= FBT_FLASH_VER
)
611 if (type
== FBT_FLASH_VER
613 *flash_ver
= le32_to_cpu(*(u32
*)(pw
- 1));
615 sz
+= size
+ (2 * sizeof(u16
));
616 pw
-= size
/ sizeof(u16
);
618 if (sz
> len
- (2 * sizeof(u16
)))
622 /* See if we are comparing the size to the specified length */
623 if (length
&& sz
!= length
)
629 /* Verify that the boot image is valid */
630 static u8
chk_boot(u8
*boot_img
, u32 length
)
632 struct esas2r_boot_image
*bi
= (struct esas2r_boot_image
*)boot_img
;
633 u16 hdroffset
= le16_to_cpu(bi
->header_offset
);
634 struct esas2r_boot_header
*bh
;
636 if (bi
->signature
!= le16_to_cpu(0xaa55)
638 (long)(65536L - sizeof(struct esas2r_boot_header
))
640 || (hdroffset
< sizeof(struct esas2r_boot_image
))
641 || ((u32
)hdroffset
+ sizeof(struct esas2r_boot_header
) > length
))
644 bh
= (struct esas2r_boot_header
*)((char *)bi
+ hdroffset
);
646 if (bh
->signature
[0] != 'P'
647 || bh
->signature
[1] != 'C'
648 || bh
->signature
[2] != 'I'
649 || bh
->signature
[3] != 'R'
650 || le16_to_cpu(bh
->struct_length
) <
651 (u16
)sizeof(struct esas2r_boot_header
)
652 || bh
->class_code
[2] != 0x01
653 || bh
->class_code
[1] != 0x04
654 || bh
->class_code
[0] != 0x00
655 || (bh
->code_type
!= CODE_TYPE_PC
656 && bh
->code_type
!= CODE_TYPE_OPEN
657 && bh
->code_type
!= CODE_TYPE_EFI
))
660 return bh
->code_type
;
663 /* The sum of all the WORDS of the image */
664 static u16
calc_fi_checksum(struct esas2r_flash_context
*fc
)
666 struct esas2r_flash_img
*fi
= fc
->fi
;
671 for (len
= (fi
->length
- fc
->fi_hdr_len
) / 2,
672 pw
= (u16
*)((u8
*)fi
+ fc
->fi_hdr_len
),
676 cksum
= cksum
+ le16_to_cpu(*pw
);
682 * Verify the flash image structure. The following verifications will
684 * 1) verify the fi_version is correct
685 * 2) verify the checksum of the entire image.
686 * 3) validate the adap_typ, action and length fields.
687 * 4) valdiate each component header. check the img_type and
689 * 5) valdiate each component image. validate signatures and
692 static bool verify_fi(struct esas2r_adapter
*a
,
693 struct esas2r_flash_context
*fc
)
695 struct esas2r_flash_img
*fi
= fc
->fi
;
700 struct esas2r_component_header
*ch
;
702 /* Verify the length - length must even since we do a word checksum */
706 || len
< fc
->fi_hdr_len
) {
707 fi
->status
= FI_STAT_LENGTH
;
711 /* Get adapter type and verify type in flash image */
712 type
= get_fi_adap_type(a
);
713 if ((type
== FI_AT_UNKNWN
) || (fi
->adap_typ
!= type
)) {
714 fi
->status
= FI_STAT_ADAPTYP
;
719 * Loop through each component and verify the img_type and length
720 * fields. Keep a running count of the sizes sooze we can verify total
721 * size to additive size.
725 for (i
= 0, len
= 0, ch
= fi
->cmp_hdr
;
731 * Verify that the component header has the same index as the
732 * image type. The headers must be ordered correctly
734 if (i
!= ch
->img_type
) {
736 ch
->status
= CH_STAT_INVALID
;
740 switch (ch
->img_type
) {
746 type
= CODE_TYPE_OPEN
;
750 type
= CODE_TYPE_EFI
;
754 switch (ch
->img_type
) {
762 if (ch
->length
& 0x1ff)
765 /* Test if component image is present */
769 /* Image is present - verify the image */
770 if (chk_boot((u8
*)fi
+ ch
->image_offset
, ch
->length
)
778 /* Test if component image is present */
779 if (ch
->length
== 0) {
784 /* Image is present - verify the image */
785 if (!chk_cfg((u8
*)fi
+ ch
->image_offset
+ ch
->length
,
793 fi
->status
= FI_STAT_UNKNOWN
;
799 ch
->status
= CH_STAT_INVALID
;
801 ch
->status
= CH_STAT_PENDING
;
807 fi
->status
= FI_STAT_MISSING
;
811 /* Compare fi->length to the sum of ch->length fields */
812 if (len
!= fi
->length
- fc
->fi_hdr_len
) {
813 fi
->status
= FI_STAT_LENGTH
;
817 /* Compute the checksum - it should come out zero */
818 if (fi
->checksum
!= calc_fi_checksum(fc
)) {
819 fi
->status
= FI_STAT_CHKSUM
;
826 /* Fill in the FS IOCTL response data from a completed request. */
827 static void esas2r_complete_fs_ioctl(struct esas2r_adapter
*a
,
828 struct esas2r_request
*rq
)
830 struct esas2r_ioctl_fs
*fs
=
831 (struct esas2r_ioctl_fs
*)rq
->interrupt_cx
;
833 if (rq
->vrq
->flash
.sub_func
== VDA_FLASH_COMMIT
)
834 esas2r_enable_heartbeat(a
);
836 fs
->driver_error
= rq
->req_stat
;
838 if (fs
->driver_error
== RS_SUCCESS
)
839 fs
->status
= ATTO_STS_SUCCESS
;
841 fs
->status
= ATTO_STS_FAILED
;
844 /* Prepare an FS IOCTL request to be sent to the firmware. */
845 bool esas2r_process_fs_ioctl(struct esas2r_adapter
*a
,
846 struct esas2r_ioctl_fs
*fs
,
847 struct esas2r_request
*rq
,
848 struct esas2r_sg_context
*sgc
)
850 u8 cmdcnt
= (u8
)ARRAY_SIZE(cmd_to_fls_func
);
851 struct esas2r_ioctlfs_command
*fsc
= &fs
->command
;
855 fs
->status
= ATTO_STS_FAILED
;
856 fs
->driver_error
= RS_PENDING
;
858 if (fs
->version
> ESAS2R_FS_VER
) {
859 fs
->status
= ATTO_STS_INV_VERSION
;
863 if (fsc
->command
>= cmdcnt
) {
864 fs
->status
= ATTO_STS_INV_FUNC
;
868 func
= cmd_to_fls_func
[fsc
->command
];
870 fs
->status
= ATTO_STS_INV_FUNC
;
874 if (fsc
->command
!= ESAS2R_FS_CMD_CANCEL
) {
875 if ((a
->pcid
->device
!= ATTO_DID_MV_88RC9580
876 || fs
->adap_type
!= ESAS2R_FS_AT_ESASRAID2
)
877 && (a
->pcid
->device
!= ATTO_DID_MV_88RC9580TS
878 || fs
->adap_type
!= ESAS2R_FS_AT_TSSASRAID2
)
879 && (a
->pcid
->device
!= ATTO_DID_MV_88RC9580TSE
880 || fs
->adap_type
!= ESAS2R_FS_AT_TSSASRAID2E
)
881 && (a
->pcid
->device
!= ATTO_DID_MV_88RC9580TL
882 || fs
->adap_type
!= ESAS2R_FS_AT_TLSASHBA
)) {
883 fs
->status
= ATTO_STS_INV_ADAPTER
;
887 if (fs
->driver_ver
> ESAS2R_FS_DRVR_VER
) {
888 fs
->status
= ATTO_STS_INV_DRVR_VER
;
893 if (a
->flags
& AF_DEGRADED_MODE
) {
894 fs
->status
= ATTO_STS_DEGRADED
;
898 rq
->interrupt_cb
= esas2r_complete_fs_ioctl
;
899 rq
->interrupt_cx
= fs
;
900 datalen
= le32_to_cpu(fsc
->length
);
901 esas2r_build_flash_req(a
,
905 le32_to_cpu(fsc
->flash_addr
),
908 if (func
== VDA_FLASH_WRITE
909 || func
== VDA_FLASH_READ
) {
911 fs
->status
= ATTO_STS_INV_FUNC
;
915 esas2r_sgc_init(sgc
, a
, rq
, rq
->vrq
->flash
.data
.sge
);
916 sgc
->length
= datalen
;
918 if (!esas2r_build_sg_list(a
, rq
, sgc
)) {
919 fs
->status
= ATTO_STS_OUT_OF_RSRC
;
924 if (func
== VDA_FLASH_COMMIT
)
925 esas2r_disable_heartbeat(a
);
927 esas2r_start_request(a
, rq
);
932 static bool esas2r_flash_access(struct esas2r_adapter
*a
, u32 function
)
939 /* Disable chip interrupts awhile */
940 if (function
== DRBL_FLASH_REQ
)
941 esas2r_disable_chip_interrupts(a
);
943 /* Issue the request to the firmware */
944 esas2r_write_register_dword(a
, MU_DOORBELL_IN
, function
);
946 /* Now wait for the firmware to process it */
947 starttime
= jiffies_to_msecs(jiffies
);
949 (AF_CHPRST_PENDING
| AF_DISC_PENDING
) ? 40000 : 5000;
952 intstat
= esas2r_read_register_dword(a
, MU_INT_STATUS_OUT
);
954 if (intstat
& MU_INTSTAT_DRBL
) {
955 /* Got a doorbell interrupt. Check for the function */
957 esas2r_read_register_dword(a
, MU_DOORBELL_OUT
);
958 esas2r_write_register_dword(a
, MU_DOORBELL_OUT
,
960 if (doorbell
& function
)
964 schedule_timeout_interruptible(msecs_to_jiffies(100));
966 if ((jiffies_to_msecs(jiffies
) - starttime
) > timeout
) {
968 * Iimeout. If we were requesting flash access,
969 * indicate we are done so the firmware knows we gave
970 * up. If this was a REQ, we also need to re-enable
973 if (function
== DRBL_FLASH_REQ
) {
974 esas2r_hdebug("flash access timeout");
975 esas2r_write_register_dword(a
, MU_DOORBELL_IN
,
977 esas2r_enable_chip_interrupts(a
);
979 esas2r_hdebug("flash release timeout");
986 /* if we're done, re-enable chip interrupts */
987 if (function
== DRBL_FLASH_DONE
)
988 esas2r_enable_chip_interrupts(a
);
993 #define WINDOW_SIZE ((signed int)MW_DATA_WINDOW_SIZE)
995 bool esas2r_read_flash_block(struct esas2r_adapter
*a
,
1002 /* Try to acquire access to the flash */
1003 if (!esas2r_flash_access(a
, DRBL_FLASH_REQ
))
1011 if (a
->flags2
& AF2_SERIAL_FLASH
)
1012 iatvr
= MW_DATA_ADDR_SER_FLASH
+ (from
& -WINDOW_SIZE
);
1014 iatvr
= MW_DATA_ADDR_PAR_FLASH
+ (from
& -WINDOW_SIZE
);
1016 esas2r_map_data_window(a
, iatvr
);
1017 offset
= from
& (WINDOW_SIZE
- 1);
1020 if (len
> WINDOW_SIZE
- offset
)
1021 len
= WINDOW_SIZE
- offset
;
1027 *end
++ = esas2r_read_data_byte(a
, offset
);
1032 /* Release flash access */
1033 esas2r_flash_access(a
, DRBL_FLASH_DONE
);
1037 bool esas2r_read_flash_rev(struct esas2r_adapter
*a
)
1047 pw
= (u16
*)(bytes
+ sz
);
1048 pwstart
= (u16
*)bytes
+ 2;
1050 if (!esas2r_read_flash_block(a
, bytes
, FLS_OFFSET_CPYR
- sz
, sz
))
1053 while (pw
>= pwstart
) {
1055 type
= le16_to_cpu(*pw
);
1057 size
= le16_to_cpu(*pw
);
1060 if (type
== FBT_CPYR
1061 || type
== FBT_SETUP
1065 if (type
== FBT_FLASH_VER
)
1066 a
->flash_ver
= le32_to_cpu(*(u32
*)pw
);
1072 return esas2r_print_flash_rev(a
);
1075 bool esas2r_print_flash_rev(struct esas2r_adapter
*a
)
1077 u16 year
= LOWORD(a
->flash_ver
);
1078 u8 day
= LOBYTE(HIWORD(a
->flash_ver
));
1079 u8 month
= HIBYTE(HIWORD(a
->flash_ver
));
1087 strcpy(a
->flash_rev
, "not found");
1092 sprintf(a
->flash_rev
, "%02d/%02d/%04d", month
, day
, year
);
1093 esas2r_hdebug("flash version: %s", a
->flash_rev
);
1098 * Find the type of boot image type that is currently in the flash.
1099 * The chip only has a 64 KB PCI-e expansion ROM
1100 * size so only one image can be flashed at a time.
1102 bool esas2r_read_image_type(struct esas2r_adapter
*a
)
1105 struct esas2r_boot_image
*bi
;
1106 struct esas2r_boot_header
*bh
;
1111 /* Start at the base of the boot images and look for a valid image */
1113 len
= FLS_LENGTH_BOOT
;
1117 if (!esas2r_read_flash_block(a
, bytes
, FLS_OFFSET_BOOT
+
1122 bi
= (struct esas2r_boot_image
*)bytes
;
1123 bh
= (struct esas2r_boot_header
*)((u8
*)bi
+
1125 bi
->header_offset
));
1126 if (bi
->signature
!= cpu_to_le16(0xAA55))
1129 if (bh
->code_type
== CODE_TYPE_PC
) {
1130 strcpy(a
->image_type
, "BIOS");
1133 } else if (bh
->code_type
== CODE_TYPE_EFI
) {
1134 struct esas2r_efi_image
*ei
;
1137 * So we have an EFI image. There are several types
1138 * so see which architecture we have.
1140 ei
= (struct esas2r_efi_image
*)bytes
;
1142 switch (le16_to_cpu(ei
->machine_type
)) {
1143 case EFI_MACHINE_IA32
:
1144 strcpy(a
->image_type
, "EFI 32-bit");
1147 case EFI_MACHINE_IA64
:
1148 strcpy(a
->image_type
, "EFI itanium");
1151 case EFI_MACHINE_X64
:
1152 strcpy(a
->image_type
, "EFI 64-bit");
1155 case EFI_MACHINE_EBC
:
1156 strcpy(a
->image_type
, "EFI EBC");
1165 /* jump to the next image */
1166 thislen
= (u32
)le16_to_cpu(bh
->image_length
) * 512;
1168 || thislen
+ offset
> len
1169 || bh
->indicator
== INDICATOR_LAST
)
1177 strcpy(a
->image_type
, "no boot images");
1182 * Read and validate current NVRAM parameters by accessing
1183 * physical NVRAM directly. if currently stored parameters are
1184 * invalid, use the defaults.
1186 bool esas2r_nvram_read_direct(struct esas2r_adapter
*a
)
1190 if (down_interruptible(&a
->nvram_semaphore
))
1193 if (!esas2r_read_flash_block(a
, a
->nvram
, FLS_OFFSET_NVR
,
1194 sizeof(struct esas2r_sas_nvram
))) {
1195 esas2r_hdebug("NVRAM read failed, using defaults");
1199 result
= esas2r_nvram_validate(a
);
1201 up(&a
->nvram_semaphore
);
1206 /* Interrupt callback to process NVRAM completions. */
1207 static void esas2r_nvram_callback(struct esas2r_adapter
*a
,
1208 struct esas2r_request
*rq
)
1210 struct atto_vda_flash_req
*vrq
= &rq
->vrq
->flash
;
1212 if (rq
->req_stat
== RS_SUCCESS
) {
1213 /* last request was successful. see what to do now. */
1215 switch (vrq
->sub_func
) {
1216 case VDA_FLASH_BEGINW
:
1217 vrq
->sub_func
= VDA_FLASH_WRITE
;
1218 rq
->req_stat
= RS_PENDING
;
1221 case VDA_FLASH_WRITE
:
1222 vrq
->sub_func
= VDA_FLASH_COMMIT
;
1223 rq
->req_stat
= RS_PENDING
;
1226 case VDA_FLASH_READ
:
1227 esas2r_nvram_validate(a
);
1230 case VDA_FLASH_COMMIT
:
1236 if (rq
->req_stat
!= RS_PENDING
) {
1237 /* update the NVRAM state */
1238 if (rq
->req_stat
== RS_SUCCESS
)
1239 esas2r_lock_set_flags(&a
->flags
, AF_NVR_VALID
);
1241 esas2r_lock_clear_flags(&a
->flags
, AF_NVR_VALID
);
1243 esas2r_enable_heartbeat(a
);
1245 up(&a
->nvram_semaphore
);
1250 * Write the contents of nvram to the adapter's physical NVRAM.
1251 * The cached copy of the NVRAM is also updated.
1253 bool esas2r_nvram_write(struct esas2r_adapter
*a
, struct esas2r_request
*rq
,
1254 struct esas2r_sas_nvram
*nvram
)
1256 struct esas2r_sas_nvram
*n
= nvram
;
1257 u8 sas_address_bytes
[8];
1258 u32
*sas_address_dwords
= (u32
*)&sas_address_bytes
[0];
1259 struct atto_vda_flash_req
*vrq
= &rq
->vrq
->flash
;
1261 if (a
->flags
& AF_DEGRADED_MODE
)
1264 if (down_interruptible(&a
->nvram_semaphore
))
1270 /* check the validity of the settings */
1271 if (n
->version
> SASNVR_VERSION
) {
1272 up(&a
->nvram_semaphore
);
1276 memcpy(&sas_address_bytes
[0], n
->sas_addr
, 8);
1278 if (sas_address_bytes
[0] != 0x50
1279 || sas_address_bytes
[1] != 0x01
1280 || sas_address_bytes
[2] != 0x08
1281 || (sas_address_bytes
[3] & 0xF0) != 0x60
1282 || ((sas_address_bytes
[3] & 0x0F) | sas_address_dwords
[1]) == 0) {
1283 up(&a
->nvram_semaphore
);
1287 if (n
->spin_up_delay
> SASNVR_SPINUP_MAX
)
1288 n
->spin_up_delay
= SASNVR_SPINUP_MAX
;
1290 n
->version
= SASNVR_VERSION
;
1291 n
->checksum
= n
->checksum
- esas2r_nvramcalc_cksum(n
);
1292 memcpy(a
->nvram
, n
, sizeof(struct esas2r_sas_nvram
));
1294 /* write the NVRAM */
1296 esas2r_disable_heartbeat(a
);
1298 esas2r_build_flash_req(a
,
1301 esas2r_nvramcalc_xor_cksum(n
),
1303 sizeof(struct esas2r_sas_nvram
));
1305 if (a
->flags
& AF_LEGACY_SGE_MODE
) {
1307 vrq
->data
.sge
[0].length
=
1308 cpu_to_le32(SGE_LAST
|
1309 sizeof(struct esas2r_sas_nvram
));
1310 vrq
->data
.sge
[0].address
= cpu_to_le64(
1311 a
->uncached_phys
+ (u64
)((u8
*)n
- a
->uncached
));
1313 vrq
->data
.prde
[0].ctl_len
=
1314 cpu_to_le32(sizeof(struct esas2r_sas_nvram
));
1315 vrq
->data
.prde
[0].address
= cpu_to_le64(
1317 + (u64
)((u8
*)n
- a
->uncached
));
1319 rq
->interrupt_cb
= esas2r_nvram_callback
;
1320 esas2r_start_request(a
, rq
);
1324 /* Validate the cached NVRAM. if the NVRAM is invalid, load the defaults. */
1325 bool esas2r_nvram_validate(struct esas2r_adapter
*a
)
1327 struct esas2r_sas_nvram
*n
= a
->nvram
;
1330 if (n
->signature
[0] != 'E'
1331 || n
->signature
[1] != 'S'
1332 || n
->signature
[2] != 'A'
1333 || n
->signature
[3] != 'S') {
1334 esas2r_hdebug("invalid NVRAM signature");
1335 } else if (esas2r_nvramcalc_cksum(n
)) {
1336 esas2r_hdebug("invalid NVRAM checksum");
1337 } else if (n
->version
> SASNVR_VERSION
) {
1338 esas2r_hdebug("invalid NVRAM version");
1340 esas2r_lock_set_flags(&a
->flags
, AF_NVR_VALID
);
1344 if (rslt
== false) {
1345 esas2r_hdebug("using defaults");
1346 esas2r_nvram_set_defaults(a
);
1353 * Set the cached NVRAM to defaults. note that this function sets the default
1354 * NVRAM when it has been determined that the physical NVRAM is invalid.
1355 * In this case, the SAS address is fabricated.
1357 void esas2r_nvram_set_defaults(struct esas2r_adapter
*a
)
1359 struct esas2r_sas_nvram
*n
= a
->nvram
;
1360 u32 time
= jiffies_to_msecs(jiffies
);
1362 esas2r_lock_clear_flags(&a
->flags
, AF_NVR_VALID
);
1363 *n
= default_sas_nvram
;
1364 n
->sas_addr
[3] |= 0x0F;
1365 n
->sas_addr
[4] = HIBYTE(LOWORD(time
));
1366 n
->sas_addr
[5] = LOBYTE(LOWORD(time
));
1367 n
->sas_addr
[6] = a
->pcid
->bus
->number
;
1368 n
->sas_addr
[7] = a
->pcid
->devfn
;
1371 void esas2r_nvram_get_defaults(struct esas2r_adapter
*a
,
1372 struct esas2r_sas_nvram
*nvram
)
1377 * in case we are copying the defaults into the adapter, copy the SAS
1378 * address out first.
1380 memcpy(&sas_addr
[0], a
->nvram
->sas_addr
, 8);
1381 *nvram
= default_sas_nvram
;
1382 memcpy(&nvram
->sas_addr
[0], &sas_addr
[0], 8);
1385 bool esas2r_fm_api(struct esas2r_adapter
*a
, struct esas2r_flash_img
*fi
,
1386 struct esas2r_request
*rq
, struct esas2r_sg_context
*sgc
)
1388 struct esas2r_flash_context
*fc
= &a
->flash_context
;
1390 struct esas2r_component_header
*ch
;
1392 if (esas2r_lock_set_flags(&a
->flags
, AF_FLASH_LOCK
) & AF_FLASH_LOCK
) {
1393 /* flag was already set */
1394 fi
->status
= FI_STAT_BUSY
;
1398 memcpy(&fc
->sgc
, sgc
, sizeof(struct esas2r_sg_context
));
1401 fc
->sgc_offset
= sgc
->cur_offset
;
1402 rq
->req_stat
= RS_SUCCESS
;
1403 rq
->interrupt_cx
= fc
;
1405 switch (fi
->fi_version
) {
1407 fc
->scratch
= ((struct esas2r_flash_img
*)fi
)->scratch_buf
;
1408 fc
->num_comps
= FI_NUM_COMPS_V1
;
1409 fc
->fi_hdr_len
= sizeof(struct esas2r_flash_img
);
1413 return complete_fmapi_req(a
, rq
, FI_STAT_IMG_VER
);
1416 if (a
->flags
& AF_DEGRADED_MODE
)
1417 return complete_fmapi_req(a
, rq
, FI_STAT_DEGRADED
);
1419 switch (fi
->action
) {
1420 case FI_ACT_DOWN
: /* Download the components */
1421 /* Verify the format of the flash image */
1422 if (!verify_fi(a
, fc
))
1423 return complete_fmapi_req(a
, rq
, fi
->status
);
1425 /* Adjust the BIOS fields that are dependent on the HBA */
1426 ch
= &fi
->cmp_hdr
[CH_IT_BIOS
];
1431 /* Adjust the EFI fields that are dependent on the HBA */
1432 ch
= &fi
->cmp_hdr
[CH_IT_EFI
];
1438 * Since the image was just modified, compute the checksum on
1439 * the modified image. First update the CRC for the composite
1440 * expansion ROM image.
1442 fi
->checksum
= calc_fi_checksum(fc
);
1444 /* Disable the heartbeat */
1445 esas2r_disable_heartbeat(a
);
1447 /* Now start up the download sequence */
1448 fc
->task
= FMTSK_ERASE_BOOT
;
1449 fc
->func
= VDA_FLASH_BEGINW
;
1450 fc
->comp_typ
= CH_IT_CFG
;
1451 fc
->flsh_addr
= FLS_OFFSET_BOOT
;
1452 fc
->sgc
.length
= FLS_LENGTH_BOOT
;
1453 fc
->sgc
.cur_offset
= NULL
;
1455 /* Setup the callback address */
1456 fc
->interrupt_cb
= fw_download_proc
;
1459 case FI_ACT_UPSZ
: /* Get upload sizes */
1460 fi
->adap_typ
= get_fi_adap_type(a
);
1462 fi
->num_comps
= fc
->num_comps
;
1463 fi
->length
= fc
->fi_hdr_len
;
1465 /* Report the type of boot image in the rel_version string */
1466 memcpy(fi
->rel_version
, a
->image_type
,
1467 sizeof(fi
->rel_version
));
1469 /* Build the component headers */
1470 for (j
= 0, ch
= fi
->cmp_hdr
;
1474 ch
->status
= CH_STAT_PENDING
;
1476 ch
->version
= 0xffffffff;
1477 ch
->image_offset
= 0;
1482 if (a
->flash_ver
!= 0) {
1483 fi
->cmp_hdr
[CH_IT_BIOS
].version
=
1484 fi
->cmp_hdr
[CH_IT_MAC
].version
=
1485 fi
->cmp_hdr
[CH_IT_EFI
].version
=
1486 fi
->cmp_hdr
[CH_IT_CFG
].version
1489 fi
->cmp_hdr
[CH_IT_BIOS
].status
=
1490 fi
->cmp_hdr
[CH_IT_MAC
].status
=
1491 fi
->cmp_hdr
[CH_IT_EFI
].status
=
1492 fi
->cmp_hdr
[CH_IT_CFG
].status
=
1495 return complete_fmapi_req(a
, rq
, FI_STAT_SUCCESS
);
1500 case FI_ACT_UP
: /* Upload the components */
1502 return complete_fmapi_req(a
, rq
, FI_STAT_INVALID
);
1506 * If we make it here, fc has been setup to do the first task. Call
1507 * load_image to format the request, start it, and get out. The
1508 * interrupt code will call the callback when the first message is
1511 if (!load_image(a
, rq
))
1512 return complete_fmapi_req(a
, rq
, FI_STAT_FAILED
);
1514 esas2r_start_request(a
, rq
);