esas2r_flash.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. }