esas2r_flash.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_flash.c
  3. * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
  4. *
  5. * Copyright (c) 2001-2013 ATTO Technology, Inc.
  6. * (mailto:linuxdrivers@attotech.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. *
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include "esas2r.h"
  44. /* local macro defs */
  45. #define esas2r_nvramcalc_cksum(n) \
  46. (esas2r_calc_byte_cksum((u8 *)(n), sizeof(struct esas2r_sas_nvram), \
  47. SASNVR_CKSUM_SEED))
  48. #define esas2r_nvramcalc_xor_cksum(n) \
  49. (esas2r_calc_byte_xor_cksum((u8 *)(n), \
  50. sizeof(struct esas2r_sas_nvram), 0))
  51. #define ESAS2R_FS_DRVR_VER 2
  52. static struct esas2r_sas_nvram default_sas_nvram = {
  53. { 'E', 'S', 'A', 'S' }, /* signature */
  54. SASNVR_VERSION, /* version */
  55. 0, /* checksum */
  56. 31, /* max_lun_for_target */
  57. SASNVR_PCILAT_MAX, /* pci_latency */
  58. SASNVR1_BOOT_DRVR, /* options1 */
  59. SASNVR2_HEARTBEAT | SASNVR2_SINGLE_BUS /* options2 */
  60. | SASNVR2_SW_MUX_CTRL,
  61. SASNVR_COAL_DIS, /* int_coalescing */
  62. SASNVR_CMDTHR_NONE, /* cmd_throttle */
  63. 3, /* dev_wait_time */
  64. 1, /* dev_wait_count */
  65. 0, /* spin_up_delay */
  66. 0, /* ssp_align_rate */
  67. { 0x50, 0x01, 0x08, 0x60, /* sas_addr */
  68. 0x00, 0x00, 0x00, 0x00 },
  69. { SASNVR_SPEED_AUTO }, /* phy_speed */
  70. { SASNVR_MUX_DISABLED }, /* SAS multiplexing */
  71. { 0 }, /* phy_flags */
  72. SASNVR_SORT_SAS_ADDR, /* sort_type */
  73. 3, /* dpm_reqcmd_lmt */
  74. 3, /* dpm_stndby_time */
  75. 0, /* dpm_active_time */
  76. { 0 }, /* phy_target_id */
  77. SASNVR_VSMH_DISABLED, /* virt_ses_mode */
  78. SASNVR_RWM_DEFAULT, /* read_write_mode */
  79. 0, /* link down timeout */
  80. { 0 } /* reserved */
  81. };
  82. static u8 cmd_to_fls_func[] = {
  83. 0xFF,
  84. VDA_FLASH_READ,
  85. VDA_FLASH_BEGINW,
  86. VDA_FLASH_WRITE,
  87. VDA_FLASH_COMMIT,
  88. VDA_FLASH_CANCEL
  89. };
  90. static u8 esas2r_calc_byte_xor_cksum(u8 *addr, u32 len, u8 seed)
  91. {
  92. u32 cksum = seed;
  93. u8 *p = (u8 *)&cksum;
  94. while (len) {
  95. if (((uintptr_t)addr & 3) == 0)
  96. break;
  97. cksum = cksum ^ *addr;
  98. addr++;
  99. len--;
  100. }
  101. while (len >= sizeof(u32)) {
  102. cksum = cksum ^ *(u32 *)addr;
  103. addr += 4;
  104. len -= 4;
  105. }
  106. while (len--) {
  107. cksum = cksum ^ *addr;
  108. addr++;
  109. }
  110. return p[0] ^ p[1] ^ p[2] ^ p[3];
  111. }
  112. static u8 esas2r_calc_byte_cksum(void *addr, u32 len, u8 seed)
  113. {
  114. u8 *p = (u8 *)addr;
  115. u8 cksum = seed;
  116. while (len--)
  117. cksum = cksum + p[len];
  118. return cksum;
  119. }
  120. /* Interrupt callback to process FM API write requests. */
  121. static void esas2r_fmapi_callback(struct esas2r_adapter *a,
  122. struct esas2r_request *rq)
  123. {
  124. struct atto_vda_flash_req *vrq = &rq->vrq->flash;
  125. struct esas2r_flash_context *fc =
  126. (struct esas2r_flash_context *)rq->interrupt_cx;
  127. if (rq->req_stat == RS_SUCCESS) {
  128. /* Last request was successful. See what to do now. */
  129. switch (vrq->sub_func) {
  130. case VDA_FLASH_BEGINW:
  131. if (fc->sgc.cur_offset == NULL)
  132. goto commit;
  133. vrq->sub_func = VDA_FLASH_WRITE;
  134. rq->req_stat = RS_PENDING;
  135. break;
  136. case VDA_FLASH_WRITE:
  137. commit:
  138. vrq->sub_func = VDA_FLASH_COMMIT;
  139. rq->req_stat = RS_PENDING;
  140. rq->interrupt_cb = fc->interrupt_cb;
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. if (rq->req_stat != RS_PENDING)
  147. /*
  148. * All done. call the real callback to complete the FM API
  149. * request. We should only get here if a BEGINW or WRITE
  150. * operation failed.
  151. */
  152. (*fc->interrupt_cb)(a, rq);
  153. }
  154. /*
  155. * Build a flash request based on the flash context. The request status
  156. * is filled in on an error.
  157. */
  158. static void build_flash_msg(struct esas2r_adapter *a,
  159. struct esas2r_request *rq)
  160. {
  161. struct esas2r_flash_context *fc =
  162. (struct esas2r_flash_context *)rq->interrupt_cx;
  163. struct esas2r_sg_context *sgc = &fc->sgc;
  164. u8 cksum = 0;
  165. /* calculate the checksum */
  166. if (fc->func == VDA_FLASH_BEGINW) {
  167. if (sgc->cur_offset)
  168. cksum = esas2r_calc_byte_xor_cksum(sgc->cur_offset,
  169. sgc->length,
  170. 0);
  171. rq->interrupt_cb = esas2r_fmapi_callback;
  172. } else {
  173. rq->interrupt_cb = fc->interrupt_cb;
  174. }
  175. esas2r_build_flash_req(a,
  176. rq,
  177. fc->func,
  178. cksum,
  179. fc->flsh_addr,
  180. sgc->length);
  181. esas2r_rq_free_sg_lists(rq, a);
  182. /*
  183. * remember the length we asked for. we have to keep track of
  184. * the current amount done so we know how much to compare when
  185. * doing the verification phase.
  186. */
  187. fc->curr_len = fc->sgc.length;
  188. if (sgc->cur_offset) {
  189. /* setup the S/G context to build the S/G table */
  190. esas2r_sgc_init(sgc, a, rq, &rq->vrq->flash.data.sge[0]);
  191. if (!esas2r_build_sg_list(a, rq, sgc)) {
  192. rq->req_stat = RS_BUSY;
  193. return;
  194. }
  195. } else {
  196. fc->sgc.length = 0;
  197. }
  198. /* update the flsh_addr to the next one to write to */
  199. fc->flsh_addr += fc->curr_len;
  200. }
  201. /* determine the method to process the flash request */
  202. static bool load_image(struct esas2r_adapter *a, struct esas2r_request *rq)
  203. {
  204. /*
  205. * assume we have more to do. if we return with the status set to
  206. * RS_PENDING, FM API tasks will continue.
  207. */
  208. rq->req_stat = RS_PENDING;
  209. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  210. /* not suppported for now */;
  211. else
  212. build_flash_msg(a, rq);
  213. return rq->req_stat == RS_PENDING;
  214. }
  215. /* boot image fixer uppers called before downloading the image. */
  216. static void fix_bios(struct esas2r_adapter *a, struct esas2r_flash_img *fi)
  217. {
  218. struct esas2r_component_header *ch = &fi->cmp_hdr[CH_IT_BIOS];
  219. struct esas2r_pc_image *pi;
  220. struct esas2r_boot_header *bh;
  221. pi = (struct esas2r_pc_image *)((u8 *)fi + ch->image_offset);
  222. bh =
  223. (struct esas2r_boot_header *)((u8 *)pi +
  224. le16_to_cpu(pi->header_offset));
  225. bh->device_id = cpu_to_le16(a->pcid->device);
  226. /* Recalculate the checksum in the PNP header if there */
  227. if (pi->pnp_offset) {
  228. u8 *pnp_header_bytes =
  229. ((u8 *)pi + le16_to_cpu(pi->pnp_offset));
  230. /* Identifier - dword that starts at byte 10 */
  231. *((u32 *)&pnp_header_bytes[10]) =
  232. cpu_to_le32(MAKEDWORD(a->pcid->subsystem_vendor,
  233. a->pcid->subsystem_device));
  234. /* Checksum - byte 9 */
  235. pnp_header_bytes[9] -= esas2r_calc_byte_cksum(pnp_header_bytes,
  236. 32, 0);
  237. }
  238. /* Recalculate the checksum needed by the PC */
  239. pi->checksum = pi->checksum -
  240. esas2r_calc_byte_cksum((u8 *)pi, ch->length, 0);
  241. }
  242. static void fix_efi(struct esas2r_adapter *a, struct esas2r_flash_img *fi)
  243. {
  244. struct esas2r_component_header *ch = &fi->cmp_hdr[CH_IT_EFI];
  245. u32 len = ch->length;
  246. u32 offset = ch->image_offset;
  247. struct esas2r_efi_image *ei;
  248. struct esas2r_boot_header *bh;
  249. while (len) {
  250. u32 thislen;
  251. ei = (struct esas2r_efi_image *)((u8 *)fi + offset);
  252. bh = (struct esas2r_boot_header *)((u8 *)ei +
  253. le16_to_cpu(
  254. ei->header_offset));
  255. bh->device_id = cpu_to_le16(a->pcid->device);
  256. thislen = (u32)le16_to_cpu(bh->image_length) * 512;
  257. if (thislen > len)
  258. break;
  259. len -= thislen;
  260. offset += thislen;
  261. }
  262. }
  263. /* Complete a FM API request with the specified status. */
  264. static bool complete_fmapi_req(struct esas2r_adapter *a,
  265. struct esas2r_request *rq, u8 fi_stat)
  266. {
  267. struct esas2r_flash_context *fc =
  268. (struct esas2r_flash_context *)rq->interrupt_cx;
  269. struct esas2r_flash_img *fi = fc->fi;
  270. fi->status = fi_stat;
  271. fi->driver_error = rq->req_stat;
  272. rq->interrupt_cb = NULL;
  273. rq->req_stat = RS_SUCCESS;
  274. if (fi_stat != FI_STAT_IMG_VER)
  275. memset(fc->scratch, 0, FM_BUF_SZ);
  276. esas2r_enable_heartbeat(a);
  277. clear_bit(AF_FLASH_LOCK, &a->flags);
  278. return false;
  279. }
  280. /* Process each phase of the flash download process. */
  281. static void fw_download_proc(struct esas2r_adapter *a,
  282. struct esas2r_request *rq)
  283. {
  284. struct esas2r_flash_context *fc =
  285. (struct esas2r_flash_context *)rq->interrupt_cx;
  286. struct esas2r_flash_img *fi = fc->fi;
  287. struct esas2r_component_header *ch;
  288. u32 len;
  289. u8 *p, *q;
  290. /* If the previous operation failed, just return. */
  291. if (rq->req_stat != RS_SUCCESS)
  292. goto error;
  293. /*
  294. * If an upload just completed and the compare length is non-zero,
  295. * then we just read back part of the image we just wrote. verify the
  296. * section and continue reading until the entire image is verified.
  297. */
  298. if (fc->func == VDA_FLASH_READ
  299. && fc->cmp_len) {
  300. ch = &fi->cmp_hdr[fc->comp_typ];
  301. p = fc->scratch;
  302. q = (u8 *)fi /* start of the whole gob */
  303. + ch->image_offset /* start of the current image */
  304. + ch->length /* end of the current image */
  305. - fc->cmp_len; /* where we are now */
  306. /*
  307. * NOTE - curr_len is the exact count of bytes for the read
  308. * even when the end is read and its not a full buffer
  309. */
  310. for (len = fc->curr_len; len; len--)
  311. if (*p++ != *q++)
  312. goto error;
  313. fc->cmp_len -= fc->curr_len; /* # left to compare */
  314. /* Update fc and determine the length for the next upload */
  315. if (fc->cmp_len > FM_BUF_SZ)
  316. fc->sgc.length = FM_BUF_SZ;
  317. else
  318. fc->sgc.length = fc->cmp_len;
  319. fc->sgc.cur_offset = fc->sgc_offset +
  320. ((u8 *)fc->scratch - (u8 *)fi);
  321. }
  322. /*
  323. * This code uses a 'while' statement since the next component may
  324. * have a length = zero. This can happen since some components are
  325. * not required. At the end of this 'while' we set up the length
  326. * for the next request and therefore sgc.length can be = 0.
  327. */
  328. while (fc->sgc.length == 0) {
  329. ch = &fi->cmp_hdr[fc->comp_typ];
  330. switch (fc->task) {
  331. case FMTSK_ERASE_BOOT:
  332. /* the BIOS image is written next */
  333. ch = &fi->cmp_hdr[CH_IT_BIOS];
  334. if (ch->length == 0)
  335. goto no_bios;
  336. fc->task = FMTSK_WRTBIOS;
  337. fc->func = VDA_FLASH_BEGINW;
  338. fc->comp_typ = CH_IT_BIOS;
  339. fc->flsh_addr = FLS_OFFSET_BOOT;
  340. fc->sgc.length = ch->length;
  341. fc->sgc.cur_offset = fc->sgc_offset +
  342. ch->image_offset;
  343. break;
  344. case FMTSK_WRTBIOS:
  345. /*
  346. * The BIOS image has been written - read it and
  347. * verify it
  348. */
  349. fc->task = FMTSK_READBIOS;
  350. fc->func = VDA_FLASH_READ;
  351. fc->flsh_addr = FLS_OFFSET_BOOT;
  352. fc->cmp_len = ch->length;
  353. fc->sgc.length = FM_BUF_SZ;
  354. fc->sgc.cur_offset = fc->sgc_offset
  355. + ((u8 *)fc->scratch -
  356. (u8 *)fi);
  357. break;
  358. case FMTSK_READBIOS:
  359. no_bios:
  360. /*
  361. * Mark the component header status for the image
  362. * completed
  363. */
  364. ch->status = CH_STAT_SUCCESS;
  365. /* The MAC image is written next */
  366. ch = &fi->cmp_hdr[CH_IT_MAC];
  367. if (ch->length == 0)
  368. goto no_mac;
  369. fc->task = FMTSK_WRTMAC;
  370. fc->func = VDA_FLASH_BEGINW;
  371. fc->comp_typ = CH_IT_MAC;
  372. fc->flsh_addr = FLS_OFFSET_BOOT
  373. + fi->cmp_hdr[CH_IT_BIOS].length;
  374. fc->sgc.length = ch->length;
  375. fc->sgc.cur_offset = fc->sgc_offset +
  376. ch->image_offset;
  377. break;
  378. case FMTSK_WRTMAC:
  379. /* The MAC image has been written - read and verify */
  380. fc->task = FMTSK_READMAC;
  381. fc->func = VDA_FLASH_READ;
  382. fc->flsh_addr -= ch->length;
  383. fc->cmp_len = ch->length;
  384. fc->sgc.length = FM_BUF_SZ;
  385. fc->sgc.cur_offset = fc->sgc_offset
  386. + ((u8 *)fc->scratch -
  387. (u8 *)fi);
  388. break;
  389. case FMTSK_READMAC:
  390. no_mac:
  391. /*
  392. * Mark the component header status for the image
  393. * completed
  394. */
  395. ch->status = CH_STAT_SUCCESS;
  396. /* The EFI image is written next */
  397. ch = &fi->cmp_hdr[CH_IT_EFI];
  398. if (ch->length == 0)
  399. goto no_efi;
  400. fc->task = FMTSK_WRTEFI;
  401. fc->func = VDA_FLASH_BEGINW;
  402. fc->comp_typ = CH_IT_EFI;
  403. fc->flsh_addr = FLS_OFFSET_BOOT
  404. + fi->cmp_hdr[CH_IT_BIOS].length
  405. + fi->cmp_hdr[CH_IT_MAC].length;
  406. fc->sgc.length = ch->length;
  407. fc->sgc.cur_offset = fc->sgc_offset +
  408. ch->image_offset;
  409. break;
  410. case FMTSK_WRTEFI:
  411. /* The EFI image has been written - read and verify */
  412. fc->task = FMTSK_READEFI;
  413. fc->func = VDA_FLASH_READ;
  414. fc->flsh_addr -= ch->length;
  415. fc->cmp_len = ch->length;
  416. fc->sgc.length = FM_BUF_SZ;
  417. fc->sgc.cur_offset = fc->sgc_offset
  418. + ((u8 *)fc->scratch -
  419. (u8 *)fi);
  420. break;
  421. case FMTSK_READEFI:
  422. no_efi:
  423. /*
  424. * Mark the component header status for the image
  425. * completed
  426. */
  427. ch->status = CH_STAT_SUCCESS;
  428. /* The CFG image is written next */
  429. ch = &fi->cmp_hdr[CH_IT_CFG];
  430. if (ch->length == 0)
  431. goto no_cfg;
  432. fc->task = FMTSK_WRTCFG;
  433. fc->func = VDA_FLASH_BEGINW;
  434. fc->comp_typ = CH_IT_CFG;
  435. fc->flsh_addr = FLS_OFFSET_CPYR - ch->length;
  436. fc->sgc.length = ch->length;
  437. fc->sgc.cur_offset = fc->sgc_offset +
  438. ch->image_offset;
  439. break;
  440. case FMTSK_WRTCFG:
  441. /* The CFG image has been written - read and verify */
  442. fc->task = FMTSK_READCFG;
  443. fc->func = VDA_FLASH_READ;
  444. fc->flsh_addr = FLS_OFFSET_CPYR - ch->length;
  445. fc->cmp_len = ch->length;
  446. fc->sgc.length = FM_BUF_SZ;
  447. fc->sgc.cur_offset = fc->sgc_offset
  448. + ((u8 *)fc->scratch -
  449. (u8 *)fi);
  450. break;
  451. case FMTSK_READCFG:
  452. no_cfg:
  453. /*
  454. * Mark the component header status for the image
  455. * completed
  456. */
  457. ch->status = CH_STAT_SUCCESS;
  458. /*
  459. * The download is complete. If in degraded mode,
  460. * attempt a chip reset.
  461. */
  462. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  463. esas2r_local_reset_adapter(a);
  464. a->flash_ver = fi->cmp_hdr[CH_IT_BIOS].version;
  465. esas2r_print_flash_rev(a);
  466. /* Update the type of boot image on the card */
  467. memcpy(a->image_type, fi->rel_version,
  468. sizeof(fi->rel_version));
  469. complete_fmapi_req(a, rq, FI_STAT_SUCCESS);
  470. return;
  471. }
  472. /* If verifying, don't try reading more than what's there */
  473. if (fc->func == VDA_FLASH_READ
  474. && fc->sgc.length > fc->cmp_len)
  475. fc->sgc.length = fc->cmp_len;
  476. }
  477. /* Build the request to perform the next action */
  478. if (!load_image(a, rq)) {
  479. error:
  480. if (fc->comp_typ < fi->num_comps) {
  481. ch = &fi->cmp_hdr[fc->comp_typ];
  482. ch->status = CH_STAT_FAILED;
  483. }
  484. complete_fmapi_req(a, rq, FI_STAT_FAILED);
  485. }
  486. }
  487. /* Determine the flash image adaptyp for this adapter */
  488. static u8 get_fi_adap_type(struct esas2r_adapter *a)
  489. {
  490. u8 type;
  491. /* use the device ID to get the correct adap_typ for this HBA */
  492. switch (a->pcid->device) {
  493. case ATTO_DID_INTEL_IOP348:
  494. type = FI_AT_SUN_LAKE;
  495. break;
  496. case ATTO_DID_MV_88RC9580:
  497. case ATTO_DID_MV_88RC9580TS:
  498. case ATTO_DID_MV_88RC9580TSE:
  499. case ATTO_DID_MV_88RC9580TL:
  500. type = FI_AT_MV_9580;
  501. break;
  502. default:
  503. type = FI_AT_UNKNWN;
  504. break;
  505. }
  506. return type;
  507. }
  508. /* Size of config + copyright + flash_ver images, 0 for failure. */
  509. static u32 chk_cfg(u8 *cfg, u32 length, u32 *flash_ver)
  510. {
  511. u16 *pw = (u16 *)cfg - 1;
  512. u32 sz = 0;
  513. u32 len = length;
  514. if (len == 0)
  515. len = FM_BUF_SZ;
  516. if (flash_ver)
  517. *flash_ver = 0;
  518. while (true) {
  519. u16 type;
  520. u16 size;
  521. type = le16_to_cpu(*pw--);
  522. size = le16_to_cpu(*pw--);
  523. if (type != FBT_CPYR
  524. && type != FBT_SETUP
  525. && type != FBT_FLASH_VER)
  526. break;
  527. if (type == FBT_FLASH_VER
  528. && flash_ver)
  529. *flash_ver = le32_to_cpu(*(u32 *)(pw - 1));
  530. sz += size + (2 * sizeof(u16));
  531. pw -= size / sizeof(u16);
  532. if (sz > len - (2 * sizeof(u16)))
  533. break;
  534. }
  535. /* See if we are comparing the size to the specified length */
  536. if (length && sz != length)
  537. return 0;
  538. return sz;
  539. }
  540. /* Verify that the boot image is valid */
  541. static u8 chk_boot(u8 *boot_img, u32 length)
  542. {
  543. struct esas2r_boot_image *bi = (struct esas2r_boot_image *)boot_img;
  544. u16 hdroffset = le16_to_cpu(bi->header_offset);
  545. struct esas2r_boot_header *bh;
  546. if (bi->signature != le16_to_cpu(0xaa55)
  547. || (long)hdroffset >
  548. (long)(65536L - sizeof(struct esas2r_boot_header))
  549. || (hdroffset & 3)
  550. || (hdroffset < sizeof(struct esas2r_boot_image))
  551. || ((u32)hdroffset + sizeof(struct esas2r_boot_header) > length))
  552. return 0xff;
  553. bh = (struct esas2r_boot_header *)((char *)bi + hdroffset);
  554. if (bh->signature[0] != 'P'
  555. || bh->signature[1] != 'C'
  556. || bh->signature[2] != 'I'
  557. || bh->signature[3] != 'R'
  558. || le16_to_cpu(bh->struct_length) <
  559. (u16)sizeof(struct esas2r_boot_header)
  560. || bh->class_code[2] != 0x01
  561. || bh->class_code[1] != 0x04
  562. || bh->class_code[0] != 0x00
  563. || (bh->code_type != CODE_TYPE_PC
  564. && bh->code_type != CODE_TYPE_OPEN
  565. && bh->code_type != CODE_TYPE_EFI))
  566. return 0xff;
  567. return bh->code_type;
  568. }
  569. /* The sum of all the WORDS of the image */
  570. static u16 calc_fi_checksum(struct esas2r_flash_context *fc)
  571. {
  572. struct esas2r_flash_img *fi = fc->fi;
  573. u16 cksum;
  574. u32 len;
  575. u16 *pw;
  576. for (len = (fi->length - fc->fi_hdr_len) / 2,
  577. pw = (u16 *)((u8 *)fi + fc->fi_hdr_len),
  578. cksum = 0;
  579. len;
  580. len--, pw++)
  581. cksum = cksum + le16_to_cpu(*pw);
  582. return cksum;
  583. }
  584. /*
  585. * Verify the flash image structure. The following verifications will
  586. * be performed:
  587. * 1) verify the fi_version is correct
  588. * 2) verify the checksum of the entire image.
  589. * 3) validate the adap_typ, action and length fields.
  590. * 4) validate each component header. check the img_type and
  591. * length fields
  592. * 5) validate each component image. validate signatures and
  593. * local checksums
  594. */
  595. static bool verify_fi(struct esas2r_adapter *a,
  596. struct esas2r_flash_context *fc)
  597. {
  598. struct esas2r_flash_img *fi = fc->fi;
  599. u8 type;
  600. bool imgerr;
  601. u16 i;
  602. u32 len;
  603. struct esas2r_component_header *ch;
  604. /* Verify the length - length must even since we do a word checksum */
  605. len = fi->length;
  606. if ((len & 1)
  607. || len < fc->fi_hdr_len) {
  608. fi->status = FI_STAT_LENGTH;
  609. return false;
  610. }
  611. /* Get adapter type and verify type in flash image */
  612. type = get_fi_adap_type(a);
  613. if ((type == FI_AT_UNKNWN) || (fi->adap_typ != type)) {
  614. fi->status = FI_STAT_ADAPTYP;
  615. return false;
  616. }
  617. /*
  618. * Loop through each component and verify the img_type and length
  619. * fields. Keep a running count of the sizes sooze we can verify total
  620. * size to additive size.
  621. */
  622. imgerr = false;
  623. for (i = 0, len = 0, ch = fi->cmp_hdr;
  624. i < fi->num_comps;
  625. i++, ch++) {
  626. bool cmperr = false;
  627. /*
  628. * Verify that the component header has the same index as the
  629. * image type. The headers must be ordered correctly
  630. */
  631. if (i != ch->img_type) {
  632. imgerr = true;
  633. ch->status = CH_STAT_INVALID;
  634. continue;
  635. }
  636. switch (ch->img_type) {
  637. case CH_IT_BIOS:
  638. type = CODE_TYPE_PC;
  639. break;
  640. case CH_IT_MAC:
  641. type = CODE_TYPE_OPEN;
  642. break;
  643. case CH_IT_EFI:
  644. type = CODE_TYPE_EFI;
  645. break;
  646. }
  647. switch (ch->img_type) {
  648. case CH_IT_FW:
  649. case CH_IT_NVR:
  650. break;
  651. case CH_IT_BIOS:
  652. case CH_IT_MAC:
  653. case CH_IT_EFI:
  654. if (ch->length & 0x1ff)
  655. cmperr = true;
  656. /* Test if component image is present */
  657. if (ch->length == 0)
  658. break;
  659. /* Image is present - verify the image */
  660. if (chk_boot((u8 *)fi + ch->image_offset, ch->length)
  661. != type)
  662. cmperr = true;
  663. break;
  664. case CH_IT_CFG:
  665. /* Test if component image is present */
  666. if (ch->length == 0) {
  667. cmperr = true;
  668. break;
  669. }
  670. /* Image is present - verify the image */
  671. if (!chk_cfg((u8 *)fi + ch->image_offset + ch->length,
  672. ch->length, NULL))
  673. cmperr = true;
  674. break;
  675. default:
  676. fi->status = FI_STAT_UNKNOWN;
  677. return false;
  678. }
  679. if (cmperr) {
  680. imgerr = true;
  681. ch->status = CH_STAT_INVALID;
  682. } else {
  683. ch->status = CH_STAT_PENDING;
  684. len += ch->length;
  685. }
  686. }
  687. if (imgerr) {
  688. fi->status = FI_STAT_MISSING;
  689. return false;
  690. }
  691. /* Compare fi->length to the sum of ch->length fields */
  692. if (len != fi->length - fc->fi_hdr_len) {
  693. fi->status = FI_STAT_LENGTH;
  694. return false;
  695. }
  696. /* Compute the checksum - it should come out zero */
  697. if (fi->checksum != calc_fi_checksum(fc)) {
  698. fi->status = FI_STAT_CHKSUM;
  699. return false;
  700. }
  701. return true;
  702. }
  703. /* Fill in the FS IOCTL response data from a completed request. */
  704. static void esas2r_complete_fs_ioctl(struct esas2r_adapter *a,
  705. struct esas2r_request *rq)
  706. {
  707. struct esas2r_ioctl_fs *fs =
  708. (struct esas2r_ioctl_fs *)rq->interrupt_cx;
  709. if (rq->vrq->flash.sub_func == VDA_FLASH_COMMIT)
  710. esas2r_enable_heartbeat(a);
  711. fs->driver_error = rq->req_stat;
  712. if (fs->driver_error == RS_SUCCESS)
  713. fs->status = ATTO_STS_SUCCESS;
  714. else
  715. fs->status = ATTO_STS_FAILED;
  716. }
  717. /* Prepare an FS IOCTL request to be sent to the firmware. */
  718. bool esas2r_process_fs_ioctl(struct esas2r_adapter *a,
  719. struct esas2r_ioctl_fs *fs,
  720. struct esas2r_request *rq,
  721. struct esas2r_sg_context *sgc)
  722. {
  723. u8 cmdcnt = (u8)ARRAY_SIZE(cmd_to_fls_func);
  724. struct esas2r_ioctlfs_command *fsc = &fs->command;
  725. u8 func = 0;
  726. u32 datalen;
  727. fs->status = ATTO_STS_FAILED;
  728. fs->driver_error = RS_PENDING;
  729. if (fs->version > ESAS2R_FS_VER) {
  730. fs->status = ATTO_STS_INV_VERSION;
  731. return false;
  732. }
  733. if (fsc->command >= cmdcnt) {
  734. fs->status = ATTO_STS_INV_FUNC;
  735. return false;
  736. }
  737. func = cmd_to_fls_func[fsc->command];
  738. if (func == 0xFF) {
  739. fs->status = ATTO_STS_INV_FUNC;
  740. return false;
  741. }
  742. if (fsc->command != ESAS2R_FS_CMD_CANCEL) {
  743. if ((a->pcid->device != ATTO_DID_MV_88RC9580
  744. || fs->adap_type != ESAS2R_FS_AT_ESASRAID2)
  745. && (a->pcid->device != ATTO_DID_MV_88RC9580TS
  746. || fs->adap_type != ESAS2R_FS_AT_TSSASRAID2)
  747. && (a->pcid->device != ATTO_DID_MV_88RC9580TSE
  748. || fs->adap_type != ESAS2R_FS_AT_TSSASRAID2E)
  749. && (a->pcid->device != ATTO_DID_MV_88RC9580TL
  750. || fs->adap_type != ESAS2R_FS_AT_TLSASHBA)) {
  751. fs->status = ATTO_STS_INV_ADAPTER;
  752. return false;
  753. }
  754. if (fs->driver_ver > ESAS2R_FS_DRVR_VER) {
  755. fs->status = ATTO_STS_INV_DRVR_VER;
  756. return false;
  757. }
  758. }
  759. if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
  760. fs->status = ATTO_STS_DEGRADED;
  761. return false;
  762. }
  763. rq->interrupt_cb = esas2r_complete_fs_ioctl;
  764. rq->interrupt_cx = fs;
  765. datalen = le32_to_cpu(fsc->length);
  766. esas2r_build_flash_req(a,
  767. rq,
  768. func,
  769. fsc->checksum,
  770. le32_to_cpu(fsc->flash_addr),
  771. datalen);
  772. if (func == VDA_FLASH_WRITE
  773. || func == VDA_FLASH_READ) {
  774. if (datalen == 0) {
  775. fs->status = ATTO_STS_INV_FUNC;
  776. return false;
  777. }
  778. esas2r_sgc_init(sgc, a, rq, rq->vrq->flash.data.sge);
  779. sgc->length = datalen;
  780. if (!esas2r_build_sg_list(a, rq, sgc)) {
  781. fs->status = ATTO_STS_OUT_OF_RSRC;
  782. return false;
  783. }
  784. }
  785. if (func == VDA_FLASH_COMMIT)
  786. esas2r_disable_heartbeat(a);
  787. esas2r_start_request(a, rq);
  788. return true;
  789. }
  790. static bool esas2r_flash_access(struct esas2r_adapter *a, u32 function)
  791. {
  792. u32 starttime;
  793. u32 timeout;
  794. u32 intstat;
  795. u32 doorbell;
  796. /* Disable chip interrupts awhile */
  797. if (function == DRBL_FLASH_REQ)
  798. esas2r_disable_chip_interrupts(a);
  799. /* Issue the request to the firmware */
  800. esas2r_write_register_dword(a, MU_DOORBELL_IN, function);
  801. /* Now wait for the firmware to process it */
  802. starttime = jiffies_to_msecs(jiffies);
  803. if (test_bit(AF_CHPRST_PENDING, &a->flags) ||
  804. test_bit(AF_DISC_PENDING, &a->flags))
  805. timeout = 40000;
  806. else
  807. timeout = 5000;
  808. while (true) {
  809. intstat = esas2r_read_register_dword(a, MU_INT_STATUS_OUT);
  810. if (intstat & MU_INTSTAT_DRBL) {
  811. /* Got a doorbell interrupt. Check for the function */
  812. doorbell =
  813. esas2r_read_register_dword(a, MU_DOORBELL_OUT);
  814. esas2r_write_register_dword(a, MU_DOORBELL_OUT,
  815. doorbell);
  816. if (doorbell & function)
  817. break;
  818. }
  819. schedule_timeout_interruptible(msecs_to_jiffies(100));
  820. if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
  821. /*
  822. * Iimeout. If we were requesting flash access,
  823. * indicate we are done so the firmware knows we gave
  824. * up. If this was a REQ, we also need to re-enable
  825. * chip interrupts.
  826. */
  827. if (function == DRBL_FLASH_REQ) {
  828. esas2r_hdebug("flash access timeout");
  829. esas2r_write_register_dword(a, MU_DOORBELL_IN,
  830. DRBL_FLASH_DONE);
  831. esas2r_enable_chip_interrupts(a);
  832. } else {
  833. esas2r_hdebug("flash release timeout");
  834. }
  835. return false;
  836. }
  837. }
  838. /* if we're done, re-enable chip interrupts */
  839. if (function == DRBL_FLASH_DONE)
  840. esas2r_enable_chip_interrupts(a);
  841. return true;
  842. }
  843. #define WINDOW_SIZE ((signed int)MW_DATA_WINDOW_SIZE)
  844. bool esas2r_read_flash_block(struct esas2r_adapter *a,
  845. void *to,
  846. u32 from,
  847. u32 size)
  848. {
  849. u8 *end = (u8 *)to;
  850. /* Try to acquire access to the flash */
  851. if (!esas2r_flash_access(a, DRBL_FLASH_REQ))
  852. return false;
  853. while (size) {
  854. u32 len;
  855. u32 offset;
  856. u32 iatvr;
  857. if (test_bit(AF2_SERIAL_FLASH, &a->flags2))
  858. iatvr = MW_DATA_ADDR_SER_FLASH + (from & -WINDOW_SIZE);
  859. else
  860. iatvr = MW_DATA_ADDR_PAR_FLASH + (from & -WINDOW_SIZE);
  861. esas2r_map_data_window(a, iatvr);
  862. offset = from & (WINDOW_SIZE - 1);
  863. len = size;
  864. if (len > WINDOW_SIZE - offset)
  865. len = WINDOW_SIZE - offset;
  866. from += len;
  867. size -= len;
  868. while (len--) {
  869. *end++ = esas2r_read_data_byte(a, offset);
  870. offset++;
  871. }
  872. }
  873. /* Release flash access */
  874. esas2r_flash_access(a, DRBL_FLASH_DONE);
  875. return true;
  876. }
  877. bool esas2r_read_flash_rev(struct esas2r_adapter *a)
  878. {
  879. u8 bytes[256];
  880. u16 *pw;
  881. u16 *pwstart;
  882. u16 type;
  883. u16 size;
  884. u32 sz;
  885. sz = sizeof(bytes);
  886. pw = (u16 *)(bytes + sz);
  887. pwstart = (u16 *)bytes + 2;
  888. if (!esas2r_read_flash_block(a, bytes, FLS_OFFSET_CPYR - sz, sz))
  889. goto invalid_rev;
  890. while (pw >= pwstart) {
  891. pw--;
  892. type = le16_to_cpu(*pw);
  893. pw--;
  894. size = le16_to_cpu(*pw);
  895. pw -= size / 2;
  896. if (type == FBT_CPYR
  897. || type == FBT_SETUP
  898. || pw < pwstart)
  899. continue;
  900. if (type == FBT_FLASH_VER)
  901. a->flash_ver = le32_to_cpu(*(u32 *)pw);
  902. break;
  903. }
  904. invalid_rev:
  905. return esas2r_print_flash_rev(a);
  906. }
  907. bool esas2r_print_flash_rev(struct esas2r_adapter *a)
  908. {
  909. u16 year = LOWORD(a->flash_ver);
  910. u8 day = LOBYTE(HIWORD(a->flash_ver));
  911. u8 month = HIBYTE(HIWORD(a->flash_ver));
  912. if (day == 0
  913. || month == 0
  914. || day > 31
  915. || month > 12
  916. || year < 2006
  917. || year > 9999) {
  918. strcpy(a->flash_rev, "not found");
  919. a->flash_ver = 0;
  920. return false;
  921. }
  922. sprintf(a->flash_rev, "%02d/%02d/%04d", month, day, year);
  923. esas2r_hdebug("flash version: %s", a->flash_rev);
  924. return true;
  925. }
  926. /*
  927. * Find the type of boot image type that is currently in the flash.
  928. * The chip only has a 64 KB PCI-e expansion ROM
  929. * size so only one image can be flashed at a time.
  930. */
  931. bool esas2r_read_image_type(struct esas2r_adapter *a)
  932. {
  933. u8 bytes[256];
  934. struct esas2r_boot_image *bi;
  935. struct esas2r_boot_header *bh;
  936. u32 sz;
  937. u32 len;
  938. u32 offset;
  939. /* Start at the base of the boot images and look for a valid image */
  940. sz = sizeof(bytes);
  941. len = FLS_LENGTH_BOOT;
  942. offset = 0;
  943. while (true) {
  944. if (!esas2r_read_flash_block(a, bytes, FLS_OFFSET_BOOT +
  945. offset,
  946. sz))
  947. goto invalid_rev;
  948. bi = (struct esas2r_boot_image *)bytes;
  949. bh = (struct esas2r_boot_header *)((u8 *)bi +
  950. le16_to_cpu(
  951. bi->header_offset));
  952. if (bi->signature != cpu_to_le16(0xAA55))
  953. goto invalid_rev;
  954. if (bh->code_type == CODE_TYPE_PC) {
  955. strcpy(a->image_type, "BIOS");
  956. return true;
  957. } else if (bh->code_type == CODE_TYPE_EFI) {
  958. struct esas2r_efi_image *ei;
  959. /*
  960. * So we have an EFI image. There are several types
  961. * so see which architecture we have.
  962. */
  963. ei = (struct esas2r_efi_image *)bytes;
  964. switch (le16_to_cpu(ei->machine_type)) {
  965. case EFI_MACHINE_IA32:
  966. strcpy(a->image_type, "EFI 32-bit");
  967. return true;
  968. case EFI_MACHINE_IA64:
  969. strcpy(a->image_type, "EFI itanium");
  970. return true;
  971. case EFI_MACHINE_X64:
  972. strcpy(a->image_type, "EFI 64-bit");
  973. return true;
  974. case EFI_MACHINE_EBC:
  975. strcpy(a->image_type, "EFI EBC");
  976. return true;
  977. default:
  978. goto invalid_rev;
  979. }
  980. } else {
  981. u32 thislen;
  982. /* jump to the next image */
  983. thislen = (u32)le16_to_cpu(bh->image_length) * 512;
  984. if (thislen == 0
  985. || thislen + offset > len
  986. || bh->indicator == INDICATOR_LAST)
  987. break;
  988. offset += thislen;
  989. }
  990. }
  991. invalid_rev:
  992. strcpy(a->image_type, "no boot images");
  993. return false;
  994. }
  995. /*
  996. * Read and validate current NVRAM parameters by accessing
  997. * physical NVRAM directly. if currently stored parameters are
  998. * invalid, use the defaults.
  999. */
  1000. bool esas2r_nvram_read_direct(struct esas2r_adapter *a)
  1001. {
  1002. bool result;
  1003. if (down_interruptible(&a->nvram_semaphore))
  1004. return false;
  1005. if (!esas2r_read_flash_block(a, a->nvram, FLS_OFFSET_NVR,
  1006. sizeof(struct esas2r_sas_nvram))) {
  1007. esas2r_hdebug("NVRAM read failed, using defaults");
  1008. return false;
  1009. }
  1010. result = esas2r_nvram_validate(a);
  1011. up(&a->nvram_semaphore);
  1012. return result;
  1013. }
  1014. /* Interrupt callback to process NVRAM completions. */
  1015. static void esas2r_nvram_callback(struct esas2r_adapter *a,
  1016. struct esas2r_request *rq)
  1017. {
  1018. struct atto_vda_flash_req *vrq = &rq->vrq->flash;
  1019. if (rq->req_stat == RS_SUCCESS) {
  1020. /* last request was successful. see what to do now. */
  1021. switch (vrq->sub_func) {
  1022. case VDA_FLASH_BEGINW:
  1023. vrq->sub_func = VDA_FLASH_WRITE;
  1024. rq->req_stat = RS_PENDING;
  1025. break;
  1026. case VDA_FLASH_WRITE:
  1027. vrq->sub_func = VDA_FLASH_COMMIT;
  1028. rq->req_stat = RS_PENDING;
  1029. break;
  1030. case VDA_FLASH_READ:
  1031. esas2r_nvram_validate(a);
  1032. break;
  1033. case VDA_FLASH_COMMIT:
  1034. default:
  1035. break;
  1036. }
  1037. }
  1038. if (rq->req_stat != RS_PENDING) {
  1039. /* update the NVRAM state */
  1040. if (rq->req_stat == RS_SUCCESS)
  1041. set_bit(AF_NVR_VALID, &a->flags);
  1042. else
  1043. clear_bit(AF_NVR_VALID, &a->flags);
  1044. esas2r_enable_heartbeat(a);
  1045. up(&a->nvram_semaphore);
  1046. }
  1047. }
  1048. /*
  1049. * Write the contents of nvram to the adapter's physical NVRAM.
  1050. * The cached copy of the NVRAM is also updated.
  1051. */
  1052. bool esas2r_nvram_write(struct esas2r_adapter *a, struct esas2r_request *rq,
  1053. struct esas2r_sas_nvram *nvram)
  1054. {
  1055. struct esas2r_sas_nvram *n = nvram;
  1056. u8 sas_address_bytes[8];
  1057. u32 *sas_address_dwords = (u32 *)&sas_address_bytes[0];
  1058. struct atto_vda_flash_req *vrq = &rq->vrq->flash;
  1059. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  1060. return false;
  1061. if (down_interruptible(&a->nvram_semaphore))
  1062. return false;
  1063. if (n == NULL)
  1064. n = a->nvram;
  1065. /* check the validity of the settings */
  1066. if (n->version > SASNVR_VERSION) {
  1067. up(&a->nvram_semaphore);
  1068. return false;
  1069. }
  1070. memcpy(&sas_address_bytes[0], n->sas_addr, 8);
  1071. if (sas_address_bytes[0] != 0x50
  1072. || sas_address_bytes[1] != 0x01
  1073. || sas_address_bytes[2] != 0x08
  1074. || (sas_address_bytes[3] & 0xF0) != 0x60
  1075. || ((sas_address_bytes[3] & 0x0F) | sas_address_dwords[1]) == 0) {
  1076. up(&a->nvram_semaphore);
  1077. return false;
  1078. }
  1079. if (n->spin_up_delay > SASNVR_SPINUP_MAX)
  1080. n->spin_up_delay = SASNVR_SPINUP_MAX;
  1081. n->version = SASNVR_VERSION;
  1082. n->checksum = n->checksum - esas2r_nvramcalc_cksum(n);
  1083. memcpy(a->nvram, n, sizeof(struct esas2r_sas_nvram));
  1084. /* write the NVRAM */
  1085. n = a->nvram;
  1086. esas2r_disable_heartbeat(a);
  1087. esas2r_build_flash_req(a,
  1088. rq,
  1089. VDA_FLASH_BEGINW,
  1090. esas2r_nvramcalc_xor_cksum(n),
  1091. FLS_OFFSET_NVR,
  1092. sizeof(struct esas2r_sas_nvram));
  1093. if (test_bit(AF_LEGACY_SGE_MODE, &a->flags)) {
  1094. vrq->data.sge[0].length =
  1095. cpu_to_le32(SGE_LAST |
  1096. sizeof(struct esas2r_sas_nvram));
  1097. vrq->data.sge[0].address = cpu_to_le64(
  1098. a->uncached_phys + (u64)((u8 *)n - a->uncached));
  1099. } else {
  1100. vrq->data.prde[0].ctl_len =
  1101. cpu_to_le32(sizeof(struct esas2r_sas_nvram));
  1102. vrq->data.prde[0].address = cpu_to_le64(
  1103. a->uncached_phys
  1104. + (u64)((u8 *)n - a->uncached));
  1105. }
  1106. rq->interrupt_cb = esas2r_nvram_callback;
  1107. esas2r_start_request(a, rq);
  1108. return true;
  1109. }
  1110. /* Validate the cached NVRAM. if the NVRAM is invalid, load the defaults. */
  1111. bool esas2r_nvram_validate(struct esas2r_adapter *a)
  1112. {
  1113. struct esas2r_sas_nvram *n = a->nvram;
  1114. bool rslt = false;
  1115. if (n->signature[0] != 'E'
  1116. || n->signature[1] != 'S'
  1117. || n->signature[2] != 'A'
  1118. || n->signature[3] != 'S') {
  1119. esas2r_hdebug("invalid NVRAM signature");
  1120. } else if (esas2r_nvramcalc_cksum(n)) {
  1121. esas2r_hdebug("invalid NVRAM checksum");
  1122. } else if (n->version > SASNVR_VERSION) {
  1123. esas2r_hdebug("invalid NVRAM version");
  1124. } else {
  1125. set_bit(AF_NVR_VALID, &a->flags);
  1126. rslt = true;
  1127. }
  1128. if (rslt == false) {
  1129. esas2r_hdebug("using defaults");
  1130. esas2r_nvram_set_defaults(a);
  1131. }
  1132. return rslt;
  1133. }
  1134. /*
  1135. * Set the cached NVRAM to defaults. note that this function sets the default
  1136. * NVRAM when it has been determined that the physical NVRAM is invalid.
  1137. * In this case, the SAS address is fabricated.
  1138. */
  1139. void esas2r_nvram_set_defaults(struct esas2r_adapter *a)
  1140. {
  1141. struct esas2r_sas_nvram *n = a->nvram;
  1142. u32 time = jiffies_to_msecs(jiffies);
  1143. clear_bit(AF_NVR_VALID, &a->flags);
  1144. *n = default_sas_nvram;
  1145. n->sas_addr[3] |= 0x0F;
  1146. n->sas_addr[4] = HIBYTE(LOWORD(time));
  1147. n->sas_addr[5] = LOBYTE(LOWORD(time));
  1148. n->sas_addr[6] = a->pcid->bus->number;
  1149. n->sas_addr[7] = a->pcid->devfn;
  1150. }
  1151. void esas2r_nvram_get_defaults(struct esas2r_adapter *a,
  1152. struct esas2r_sas_nvram *nvram)
  1153. {
  1154. u8 sas_addr[8];
  1155. /*
  1156. * in case we are copying the defaults into the adapter, copy the SAS
  1157. * address out first.
  1158. */
  1159. memcpy(&sas_addr[0], a->nvram->sas_addr, 8);
  1160. *nvram = default_sas_nvram;
  1161. memcpy(&nvram->sas_addr[0], &sas_addr[0], 8);
  1162. }
  1163. bool esas2r_fm_api(struct esas2r_adapter *a, struct esas2r_flash_img *fi,
  1164. struct esas2r_request *rq, struct esas2r_sg_context *sgc)
  1165. {
  1166. struct esas2r_flash_context *fc = &a->flash_context;
  1167. u8 j;
  1168. struct esas2r_component_header *ch;
  1169. if (test_and_set_bit(AF_FLASH_LOCK, &a->flags)) {
  1170. /* flag was already set */
  1171. fi->status = FI_STAT_BUSY;
  1172. return false;
  1173. }
  1174. memcpy(&fc->sgc, sgc, sizeof(struct esas2r_sg_context));
  1175. sgc = &fc->sgc;
  1176. fc->fi = fi;
  1177. fc->sgc_offset = sgc->cur_offset;
  1178. rq->req_stat = RS_SUCCESS;
  1179. rq->interrupt_cx = fc;
  1180. switch (fi->fi_version) {
  1181. case FI_VERSION_1:
  1182. fc->scratch = ((struct esas2r_flash_img *)fi)->scratch_buf;
  1183. fc->num_comps = FI_NUM_COMPS_V1;
  1184. fc->fi_hdr_len = sizeof(struct esas2r_flash_img);
  1185. break;
  1186. default:
  1187. return complete_fmapi_req(a, rq, FI_STAT_IMG_VER);
  1188. }
  1189. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  1190. return complete_fmapi_req(a, rq, FI_STAT_DEGRADED);
  1191. switch (fi->action) {
  1192. case FI_ACT_DOWN: /* Download the components */
  1193. /* Verify the format of the flash image */
  1194. if (!verify_fi(a, fc))
  1195. return complete_fmapi_req(a, rq, fi->status);
  1196. /* Adjust the BIOS fields that are dependent on the HBA */
  1197. ch = &fi->cmp_hdr[CH_IT_BIOS];
  1198. if (ch->length)
  1199. fix_bios(a, fi);
  1200. /* Adjust the EFI fields that are dependent on the HBA */
  1201. ch = &fi->cmp_hdr[CH_IT_EFI];
  1202. if (ch->length)
  1203. fix_efi(a, fi);
  1204. /*
  1205. * Since the image was just modified, compute the checksum on
  1206. * the modified image. First update the CRC for the composite
  1207. * expansion ROM image.
  1208. */
  1209. fi->checksum = calc_fi_checksum(fc);
  1210. /* Disable the heartbeat */
  1211. esas2r_disable_heartbeat(a);
  1212. /* Now start up the download sequence */
  1213. fc->task = FMTSK_ERASE_BOOT;
  1214. fc->func = VDA_FLASH_BEGINW;
  1215. fc->comp_typ = CH_IT_CFG;
  1216. fc->flsh_addr = FLS_OFFSET_BOOT;
  1217. fc->sgc.length = FLS_LENGTH_BOOT;
  1218. fc->sgc.cur_offset = NULL;
  1219. /* Setup the callback address */
  1220. fc->interrupt_cb = fw_download_proc;
  1221. break;
  1222. case FI_ACT_UPSZ: /* Get upload sizes */
  1223. fi->adap_typ = get_fi_adap_type(a);
  1224. fi->flags = 0;
  1225. fi->num_comps = fc->num_comps;
  1226. fi->length = fc->fi_hdr_len;
  1227. /* Report the type of boot image in the rel_version string */
  1228. memcpy(fi->rel_version, a->image_type,
  1229. sizeof(fi->rel_version));
  1230. /* Build the component headers */
  1231. for (j = 0, ch = fi->cmp_hdr;
  1232. j < fi->num_comps;
  1233. j++, ch++) {
  1234. ch->img_type = j;
  1235. ch->status = CH_STAT_PENDING;
  1236. ch->length = 0;
  1237. ch->version = 0xffffffff;
  1238. ch->image_offset = 0;
  1239. ch->pad[0] = 0;
  1240. ch->pad[1] = 0;
  1241. }
  1242. if (a->flash_ver != 0) {
  1243. fi->cmp_hdr[CH_IT_BIOS].version =
  1244. fi->cmp_hdr[CH_IT_MAC].version =
  1245. fi->cmp_hdr[CH_IT_EFI].version =
  1246. fi->cmp_hdr[CH_IT_CFG].version
  1247. = a->flash_ver;
  1248. fi->cmp_hdr[CH_IT_BIOS].status =
  1249. fi->cmp_hdr[CH_IT_MAC].status =
  1250. fi->cmp_hdr[CH_IT_EFI].status =
  1251. fi->cmp_hdr[CH_IT_CFG].status =
  1252. CH_STAT_SUCCESS;
  1253. return complete_fmapi_req(a, rq, FI_STAT_SUCCESS);
  1254. }
  1255. /* fall through */
  1256. case FI_ACT_UP: /* Upload the components */
  1257. default:
  1258. return complete_fmapi_req(a, rq, FI_STAT_INVALID);
  1259. }
  1260. /*
  1261. * If we make it here, fc has been setup to do the first task. Call
  1262. * load_image to format the request, start it, and get out. The
  1263. * interrupt code will call the callback when the first message is
  1264. * complete.
  1265. */
  1266. if (!load_image(a, rq))
  1267. return complete_fmapi_req(a, rq, FI_STAT_FAILED);
  1268. esas2r_start_request(a, rq);
  1269. return true;
  1270. }