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 (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
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 clear_bit(AF_FLASH_LOCK
, &a
->flags
);
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 (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
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 (test_bit(AF_DEGRADED_MODE
, &a
->flags
)) {
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 if (test_bit(AF_CHPRST_PENDING
, &a
->flags
) ||
950 test_bit(AF_DISC_PENDING
, &a
->flags
))
956 intstat
= esas2r_read_register_dword(a
, MU_INT_STATUS_OUT
);
958 if (intstat
& MU_INTSTAT_DRBL
) {
959 /* Got a doorbell interrupt. Check for the function */
961 esas2r_read_register_dword(a
, MU_DOORBELL_OUT
);
962 esas2r_write_register_dword(a
, MU_DOORBELL_OUT
,
964 if (doorbell
& function
)
968 schedule_timeout_interruptible(msecs_to_jiffies(100));
970 if ((jiffies_to_msecs(jiffies
) - starttime
) > timeout
) {
972 * Iimeout. If we were requesting flash access,
973 * indicate we are done so the firmware knows we gave
974 * up. If this was a REQ, we also need to re-enable
977 if (function
== DRBL_FLASH_REQ
) {
978 esas2r_hdebug("flash access timeout");
979 esas2r_write_register_dword(a
, MU_DOORBELL_IN
,
981 esas2r_enable_chip_interrupts(a
);
983 esas2r_hdebug("flash release timeout");
990 /* if we're done, re-enable chip interrupts */
991 if (function
== DRBL_FLASH_DONE
)
992 esas2r_enable_chip_interrupts(a
);
997 #define WINDOW_SIZE ((signed int)MW_DATA_WINDOW_SIZE)
999 bool esas2r_read_flash_block(struct esas2r_adapter
*a
,
1006 /* Try to acquire access to the flash */
1007 if (!esas2r_flash_access(a
, DRBL_FLASH_REQ
))
1015 if (test_bit(AF2_SERIAL_FLASH
, &a
->flags2
))
1016 iatvr
= MW_DATA_ADDR_SER_FLASH
+ (from
& -WINDOW_SIZE
);
1018 iatvr
= MW_DATA_ADDR_PAR_FLASH
+ (from
& -WINDOW_SIZE
);
1020 esas2r_map_data_window(a
, iatvr
);
1021 offset
= from
& (WINDOW_SIZE
- 1);
1024 if (len
> WINDOW_SIZE
- offset
)
1025 len
= WINDOW_SIZE
- offset
;
1031 *end
++ = esas2r_read_data_byte(a
, offset
);
1036 /* Release flash access */
1037 esas2r_flash_access(a
, DRBL_FLASH_DONE
);
1041 bool esas2r_read_flash_rev(struct esas2r_adapter
*a
)
1051 pw
= (u16
*)(bytes
+ sz
);
1052 pwstart
= (u16
*)bytes
+ 2;
1054 if (!esas2r_read_flash_block(a
, bytes
, FLS_OFFSET_CPYR
- sz
, sz
))
1057 while (pw
>= pwstart
) {
1059 type
= le16_to_cpu(*pw
);
1061 size
= le16_to_cpu(*pw
);
1064 if (type
== FBT_CPYR
1065 || type
== FBT_SETUP
1069 if (type
== FBT_FLASH_VER
)
1070 a
->flash_ver
= le32_to_cpu(*(u32
*)pw
);
1076 return esas2r_print_flash_rev(a
);
1079 bool esas2r_print_flash_rev(struct esas2r_adapter
*a
)
1081 u16 year
= LOWORD(a
->flash_ver
);
1082 u8 day
= LOBYTE(HIWORD(a
->flash_ver
));
1083 u8 month
= HIBYTE(HIWORD(a
->flash_ver
));
1091 strcpy(a
->flash_rev
, "not found");
1096 sprintf(a
->flash_rev
, "%02d/%02d/%04d", month
, day
, year
);
1097 esas2r_hdebug("flash version: %s", a
->flash_rev
);
1102 * Find the type of boot image type that is currently in the flash.
1103 * The chip only has a 64 KB PCI-e expansion ROM
1104 * size so only one image can be flashed at a time.
1106 bool esas2r_read_image_type(struct esas2r_adapter
*a
)
1109 struct esas2r_boot_image
*bi
;
1110 struct esas2r_boot_header
*bh
;
1115 /* Start at the base of the boot images and look for a valid image */
1117 len
= FLS_LENGTH_BOOT
;
1121 if (!esas2r_read_flash_block(a
, bytes
, FLS_OFFSET_BOOT
+
1126 bi
= (struct esas2r_boot_image
*)bytes
;
1127 bh
= (struct esas2r_boot_header
*)((u8
*)bi
+
1129 bi
->header_offset
));
1130 if (bi
->signature
!= cpu_to_le16(0xAA55))
1133 if (bh
->code_type
== CODE_TYPE_PC
) {
1134 strcpy(a
->image_type
, "BIOS");
1137 } else if (bh
->code_type
== CODE_TYPE_EFI
) {
1138 struct esas2r_efi_image
*ei
;
1141 * So we have an EFI image. There are several types
1142 * so see which architecture we have.
1144 ei
= (struct esas2r_efi_image
*)bytes
;
1146 switch (le16_to_cpu(ei
->machine_type
)) {
1147 case EFI_MACHINE_IA32
:
1148 strcpy(a
->image_type
, "EFI 32-bit");
1151 case EFI_MACHINE_IA64
:
1152 strcpy(a
->image_type
, "EFI itanium");
1155 case EFI_MACHINE_X64
:
1156 strcpy(a
->image_type
, "EFI 64-bit");
1159 case EFI_MACHINE_EBC
:
1160 strcpy(a
->image_type
, "EFI EBC");
1169 /* jump to the next image */
1170 thislen
= (u32
)le16_to_cpu(bh
->image_length
) * 512;
1172 || thislen
+ offset
> len
1173 || bh
->indicator
== INDICATOR_LAST
)
1181 strcpy(a
->image_type
, "no boot images");
1186 * Read and validate current NVRAM parameters by accessing
1187 * physical NVRAM directly. if currently stored parameters are
1188 * invalid, use the defaults.
1190 bool esas2r_nvram_read_direct(struct esas2r_adapter
*a
)
1194 if (down_interruptible(&a
->nvram_semaphore
))
1197 if (!esas2r_read_flash_block(a
, a
->nvram
, FLS_OFFSET_NVR
,
1198 sizeof(struct esas2r_sas_nvram
))) {
1199 esas2r_hdebug("NVRAM read failed, using defaults");
1203 result
= esas2r_nvram_validate(a
);
1205 up(&a
->nvram_semaphore
);
1210 /* Interrupt callback to process NVRAM completions. */
1211 static void esas2r_nvram_callback(struct esas2r_adapter
*a
,
1212 struct esas2r_request
*rq
)
1214 struct atto_vda_flash_req
*vrq
= &rq
->vrq
->flash
;
1216 if (rq
->req_stat
== RS_SUCCESS
) {
1217 /* last request was successful. see what to do now. */
1219 switch (vrq
->sub_func
) {
1220 case VDA_FLASH_BEGINW
:
1221 vrq
->sub_func
= VDA_FLASH_WRITE
;
1222 rq
->req_stat
= RS_PENDING
;
1225 case VDA_FLASH_WRITE
:
1226 vrq
->sub_func
= VDA_FLASH_COMMIT
;
1227 rq
->req_stat
= RS_PENDING
;
1230 case VDA_FLASH_READ
:
1231 esas2r_nvram_validate(a
);
1234 case VDA_FLASH_COMMIT
:
1240 if (rq
->req_stat
!= RS_PENDING
) {
1241 /* update the NVRAM state */
1242 if (rq
->req_stat
== RS_SUCCESS
)
1243 set_bit(AF_NVR_VALID
, &a
->flags
);
1245 clear_bit(AF_NVR_VALID
, &a
->flags
);
1247 esas2r_enable_heartbeat(a
);
1249 up(&a
->nvram_semaphore
);
1254 * Write the contents of nvram to the adapter's physical NVRAM.
1255 * The cached copy of the NVRAM is also updated.
1257 bool esas2r_nvram_write(struct esas2r_adapter
*a
, struct esas2r_request
*rq
,
1258 struct esas2r_sas_nvram
*nvram
)
1260 struct esas2r_sas_nvram
*n
= nvram
;
1261 u8 sas_address_bytes
[8];
1262 u32
*sas_address_dwords
= (u32
*)&sas_address_bytes
[0];
1263 struct atto_vda_flash_req
*vrq
= &rq
->vrq
->flash
;
1265 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
1268 if (down_interruptible(&a
->nvram_semaphore
))
1274 /* check the validity of the settings */
1275 if (n
->version
> SASNVR_VERSION
) {
1276 up(&a
->nvram_semaphore
);
1280 memcpy(&sas_address_bytes
[0], n
->sas_addr
, 8);
1282 if (sas_address_bytes
[0] != 0x50
1283 || sas_address_bytes
[1] != 0x01
1284 || sas_address_bytes
[2] != 0x08
1285 || (sas_address_bytes
[3] & 0xF0) != 0x60
1286 || ((sas_address_bytes
[3] & 0x0F) | sas_address_dwords
[1]) == 0) {
1287 up(&a
->nvram_semaphore
);
1291 if (n
->spin_up_delay
> SASNVR_SPINUP_MAX
)
1292 n
->spin_up_delay
= SASNVR_SPINUP_MAX
;
1294 n
->version
= SASNVR_VERSION
;
1295 n
->checksum
= n
->checksum
- esas2r_nvramcalc_cksum(n
);
1296 memcpy(a
->nvram
, n
, sizeof(struct esas2r_sas_nvram
));
1298 /* write the NVRAM */
1300 esas2r_disable_heartbeat(a
);
1302 esas2r_build_flash_req(a
,
1305 esas2r_nvramcalc_xor_cksum(n
),
1307 sizeof(struct esas2r_sas_nvram
));
1309 if (test_bit(AF_LEGACY_SGE_MODE
, &a
->flags
)) {
1311 vrq
->data
.sge
[0].length
=
1312 cpu_to_le32(SGE_LAST
|
1313 sizeof(struct esas2r_sas_nvram
));
1314 vrq
->data
.sge
[0].address
= cpu_to_le64(
1315 a
->uncached_phys
+ (u64
)((u8
*)n
- a
->uncached
));
1317 vrq
->data
.prde
[0].ctl_len
=
1318 cpu_to_le32(sizeof(struct esas2r_sas_nvram
));
1319 vrq
->data
.prde
[0].address
= cpu_to_le64(
1321 + (u64
)((u8
*)n
- a
->uncached
));
1323 rq
->interrupt_cb
= esas2r_nvram_callback
;
1324 esas2r_start_request(a
, rq
);
1328 /* Validate the cached NVRAM. if the NVRAM is invalid, load the defaults. */
1329 bool esas2r_nvram_validate(struct esas2r_adapter
*a
)
1331 struct esas2r_sas_nvram
*n
= a
->nvram
;
1334 if (n
->signature
[0] != 'E'
1335 || n
->signature
[1] != 'S'
1336 || n
->signature
[2] != 'A'
1337 || n
->signature
[3] != 'S') {
1338 esas2r_hdebug("invalid NVRAM signature");
1339 } else if (esas2r_nvramcalc_cksum(n
)) {
1340 esas2r_hdebug("invalid NVRAM checksum");
1341 } else if (n
->version
> SASNVR_VERSION
) {
1342 esas2r_hdebug("invalid NVRAM version");
1344 set_bit(AF_NVR_VALID
, &a
->flags
);
1348 if (rslt
== false) {
1349 esas2r_hdebug("using defaults");
1350 esas2r_nvram_set_defaults(a
);
1357 * Set the cached NVRAM to defaults. note that this function sets the default
1358 * NVRAM when it has been determined that the physical NVRAM is invalid.
1359 * In this case, the SAS address is fabricated.
1361 void esas2r_nvram_set_defaults(struct esas2r_adapter
*a
)
1363 struct esas2r_sas_nvram
*n
= a
->nvram
;
1364 u32 time
= jiffies_to_msecs(jiffies
);
1366 clear_bit(AF_NVR_VALID
, &a
->flags
);
1367 *n
= default_sas_nvram
;
1368 n
->sas_addr
[3] |= 0x0F;
1369 n
->sas_addr
[4] = HIBYTE(LOWORD(time
));
1370 n
->sas_addr
[5] = LOBYTE(LOWORD(time
));
1371 n
->sas_addr
[6] = a
->pcid
->bus
->number
;
1372 n
->sas_addr
[7] = a
->pcid
->devfn
;
1375 void esas2r_nvram_get_defaults(struct esas2r_adapter
*a
,
1376 struct esas2r_sas_nvram
*nvram
)
1381 * in case we are copying the defaults into the adapter, copy the SAS
1382 * address out first.
1384 memcpy(&sas_addr
[0], a
->nvram
->sas_addr
, 8);
1385 *nvram
= default_sas_nvram
;
1386 memcpy(&nvram
->sas_addr
[0], &sas_addr
[0], 8);
1389 bool esas2r_fm_api(struct esas2r_adapter
*a
, struct esas2r_flash_img
*fi
,
1390 struct esas2r_request
*rq
, struct esas2r_sg_context
*sgc
)
1392 struct esas2r_flash_context
*fc
= &a
->flash_context
;
1394 struct esas2r_component_header
*ch
;
1396 if (test_and_set_bit(AF_FLASH_LOCK
, &a
->flags
)) {
1397 /* flag was already set */
1398 fi
->status
= FI_STAT_BUSY
;
1402 memcpy(&fc
->sgc
, sgc
, sizeof(struct esas2r_sg_context
));
1405 fc
->sgc_offset
= sgc
->cur_offset
;
1406 rq
->req_stat
= RS_SUCCESS
;
1407 rq
->interrupt_cx
= fc
;
1409 switch (fi
->fi_version
) {
1411 fc
->scratch
= ((struct esas2r_flash_img
*)fi
)->scratch_buf
;
1412 fc
->num_comps
= FI_NUM_COMPS_V1
;
1413 fc
->fi_hdr_len
= sizeof(struct esas2r_flash_img
);
1417 return complete_fmapi_req(a
, rq
, FI_STAT_IMG_VER
);
1420 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
1421 return complete_fmapi_req(a
, rq
, FI_STAT_DEGRADED
);
1423 switch (fi
->action
) {
1424 case FI_ACT_DOWN
: /* Download the components */
1425 /* Verify the format of the flash image */
1426 if (!verify_fi(a
, fc
))
1427 return complete_fmapi_req(a
, rq
, fi
->status
);
1429 /* Adjust the BIOS fields that are dependent on the HBA */
1430 ch
= &fi
->cmp_hdr
[CH_IT_BIOS
];
1435 /* Adjust the EFI fields that are dependent on the HBA */
1436 ch
= &fi
->cmp_hdr
[CH_IT_EFI
];
1442 * Since the image was just modified, compute the checksum on
1443 * the modified image. First update the CRC for the composite
1444 * expansion ROM image.
1446 fi
->checksum
= calc_fi_checksum(fc
);
1448 /* Disable the heartbeat */
1449 esas2r_disable_heartbeat(a
);
1451 /* Now start up the download sequence */
1452 fc
->task
= FMTSK_ERASE_BOOT
;
1453 fc
->func
= VDA_FLASH_BEGINW
;
1454 fc
->comp_typ
= CH_IT_CFG
;
1455 fc
->flsh_addr
= FLS_OFFSET_BOOT
;
1456 fc
->sgc
.length
= FLS_LENGTH_BOOT
;
1457 fc
->sgc
.cur_offset
= NULL
;
1459 /* Setup the callback address */
1460 fc
->interrupt_cb
= fw_download_proc
;
1463 case FI_ACT_UPSZ
: /* Get upload sizes */
1464 fi
->adap_typ
= get_fi_adap_type(a
);
1466 fi
->num_comps
= fc
->num_comps
;
1467 fi
->length
= fc
->fi_hdr_len
;
1469 /* Report the type of boot image in the rel_version string */
1470 memcpy(fi
->rel_version
, a
->image_type
,
1471 sizeof(fi
->rel_version
));
1473 /* Build the component headers */
1474 for (j
= 0, ch
= fi
->cmp_hdr
;
1478 ch
->status
= CH_STAT_PENDING
;
1480 ch
->version
= 0xffffffff;
1481 ch
->image_offset
= 0;
1486 if (a
->flash_ver
!= 0) {
1487 fi
->cmp_hdr
[CH_IT_BIOS
].version
=
1488 fi
->cmp_hdr
[CH_IT_MAC
].version
=
1489 fi
->cmp_hdr
[CH_IT_EFI
].version
=
1490 fi
->cmp_hdr
[CH_IT_CFG
].version
1493 fi
->cmp_hdr
[CH_IT_BIOS
].status
=
1494 fi
->cmp_hdr
[CH_IT_MAC
].status
=
1495 fi
->cmp_hdr
[CH_IT_EFI
].status
=
1496 fi
->cmp_hdr
[CH_IT_CFG
].status
=
1499 return complete_fmapi_req(a
, rq
, FI_STAT_SUCCESS
);
1504 case FI_ACT_UP
: /* Upload the components */
1506 return complete_fmapi_req(a
, rq
, FI_STAT_INVALID
);
1510 * If we make it here, fc has been setup to do the first task. Call
1511 * load_image to format the request, start it, and get out. The
1512 * interrupt code will call the callback when the first message is
1515 if (!load_image(a
, rq
))
1516 return complete_fmapi_req(a
, rq
, FI_STAT_FAILED
);
1518 esas2r_start_request(a
, rq
);