esas2r_flash.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 (a->flags & AF_DEGRADED_MODE)
  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. esas2r_lock_clear_flags(&a->flags, AF_FLASH_LOCK);
  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 (a->flags & AF_DEGRADED_MODE)
  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) valdiate each component header. check the img_type and
  591. * length fields
  592. * 5) valdiate 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 (a->flags & AF_DEGRADED_MODE) {
  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. timeout = a->flags &
  804. (AF_CHPRST_PENDING | AF_DISC_PENDING) ? 40000 : 5000;
  805. while (true) {
  806. intstat = esas2r_read_register_dword(a, MU_INT_STATUS_OUT);
  807. if (intstat & MU_INTSTAT_DRBL) {
  808. /* Got a doorbell interrupt. Check for the function */
  809. doorbell =
  810. esas2r_read_register_dword(a, MU_DOORBELL_OUT);
  811. esas2r_write_register_dword(a, MU_DOORBELL_OUT,
  812. doorbell);
  813. if (doorbell & function)
  814. break;
  815. }
  816. schedule_timeout_interruptible(msecs_to_jiffies(100));
  817. if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
  818. /*
  819. * Iimeout. If we were requesting flash access,
  820. * indicate we are done so the firmware knows we gave
  821. * up. If this was a REQ, we also need to re-enable
  822. * chip interrupts.
  823. */
  824. if (function == DRBL_FLASH_REQ) {
  825. esas2r_hdebug("flash access timeout");
  826. esas2r_write_register_dword(a, MU_DOORBELL_IN,
  827. DRBL_FLASH_DONE);
  828. esas2r_enable_chip_interrupts(a);
  829. } else {
  830. esas2r_hdebug("flash release timeout");
  831. }
  832. return false;
  833. }
  834. }
  835. /* if we're done, re-enable chip interrupts */
  836. if (function == DRBL_FLASH_DONE)
  837. esas2r_enable_chip_interrupts(a);
  838. return true;
  839. }
  840. #define WINDOW_SIZE ((signed int)MW_DATA_WINDOW_SIZE)
  841. bool esas2r_read_flash_block(struct esas2r_adapter *a,
  842. void *to,
  843. u32 from,
  844. u32 size)
  845. {
  846. u8 *end = (u8 *)to;
  847. /* Try to acquire access to the flash */
  848. if (!esas2r_flash_access(a, DRBL_FLASH_REQ))
  849. return false;
  850. while (size) {
  851. u32 len;
  852. u32 offset;
  853. u32 iatvr;
  854. if (a->flags2 & AF2_SERIAL_FLASH)
  855. iatvr = MW_DATA_ADDR_SER_FLASH + (from & -WINDOW_SIZE);
  856. else
  857. iatvr = MW_DATA_ADDR_PAR_FLASH + (from & -WINDOW_SIZE);
  858. esas2r_map_data_window(a, iatvr);
  859. offset = from & (WINDOW_SIZE - 1);
  860. len = size;
  861. if (len > WINDOW_SIZE - offset)
  862. len = WINDOW_SIZE - offset;
  863. from += len;
  864. size -= len;
  865. while (len--) {
  866. *end++ = esas2r_read_data_byte(a, offset);
  867. offset++;
  868. }
  869. }
  870. /* Release flash access */
  871. esas2r_flash_access(a, DRBL_FLASH_DONE);
  872. return true;
  873. }
  874. bool esas2r_read_flash_rev(struct esas2r_adapter *a)
  875. {
  876. u8 bytes[256];
  877. u16 *pw;
  878. u16 *pwstart;
  879. u16 type;
  880. u16 size;
  881. u32 sz;
  882. sz = sizeof(bytes);
  883. pw = (u16 *)(bytes + sz);
  884. pwstart = (u16 *)bytes + 2;
  885. if (!esas2r_read_flash_block(a, bytes, FLS_OFFSET_CPYR - sz, sz))
  886. goto invalid_rev;
  887. while (pw >= pwstart) {
  888. pw--;
  889. type = le16_to_cpu(*pw);
  890. pw--;
  891. size = le16_to_cpu(*pw);
  892. pw -= size / 2;
  893. if (type == FBT_CPYR
  894. || type == FBT_SETUP
  895. || pw < pwstart)
  896. continue;
  897. if (type == FBT_FLASH_VER)
  898. a->flash_ver = le32_to_cpu(*(u32 *)pw);
  899. break;
  900. }
  901. invalid_rev:
  902. return esas2r_print_flash_rev(a);
  903. }
  904. bool esas2r_print_flash_rev(struct esas2r_adapter *a)
  905. {
  906. u16 year = LOWORD(a->flash_ver);
  907. u8 day = LOBYTE(HIWORD(a->flash_ver));
  908. u8 month = HIBYTE(HIWORD(a->flash_ver));
  909. if (day == 0
  910. || month == 0
  911. || day > 31
  912. || month > 12
  913. || year < 2006
  914. || year > 9999) {
  915. strcpy(a->flash_rev, "not found");
  916. a->flash_ver = 0;
  917. return false;
  918. }
  919. sprintf(a->flash_rev, "%02d/%02d/%04d", month, day, year);
  920. esas2r_hdebug("flash version: %s", a->flash_rev);
  921. return true;
  922. }
  923. /*
  924. * Find the type of boot image type that is currently in the flash.
  925. * The chip only has a 64 KB PCI-e expansion ROM
  926. * size so only one image can be flashed at a time.
  927. */
  928. bool esas2r_read_image_type(struct esas2r_adapter *a)
  929. {
  930. u8 bytes[256];
  931. struct esas2r_boot_image *bi;
  932. struct esas2r_boot_header *bh;
  933. u32 sz;
  934. u32 len;
  935. u32 offset;
  936. /* Start at the base of the boot images and look for a valid image */
  937. sz = sizeof(bytes);
  938. len = FLS_LENGTH_BOOT;
  939. offset = 0;
  940. while (true) {
  941. if (!esas2r_read_flash_block(a, bytes, FLS_OFFSET_BOOT +
  942. offset,
  943. sz))
  944. goto invalid_rev;
  945. bi = (struct esas2r_boot_image *)bytes;
  946. bh = (struct esas2r_boot_header *)((u8 *)bi +
  947. le16_to_cpu(
  948. bi->header_offset));
  949. if (bi->signature != cpu_to_le16(0xAA55))
  950. goto invalid_rev;
  951. if (bh->code_type == CODE_TYPE_PC) {
  952. strcpy(a->image_type, "BIOS");
  953. return true;
  954. } else if (bh->code_type == CODE_TYPE_EFI) {
  955. struct esas2r_efi_image *ei;
  956. /*
  957. * So we have an EFI image. There are several types
  958. * so see which architecture we have.
  959. */
  960. ei = (struct esas2r_efi_image *)bytes;
  961. switch (le16_to_cpu(ei->machine_type)) {
  962. case EFI_MACHINE_IA32:
  963. strcpy(a->image_type, "EFI 32-bit");
  964. return true;
  965. case EFI_MACHINE_IA64:
  966. strcpy(a->image_type, "EFI itanium");
  967. return true;
  968. case EFI_MACHINE_X64:
  969. strcpy(a->image_type, "EFI 64-bit");
  970. return true;
  971. case EFI_MACHINE_EBC:
  972. strcpy(a->image_type, "EFI EBC");
  973. return true;
  974. default:
  975. goto invalid_rev;
  976. }
  977. } else {
  978. u32 thislen;
  979. /* jump to the next image */
  980. thislen = (u32)le16_to_cpu(bh->image_length) * 512;
  981. if (thislen == 0
  982. || thislen + offset > len
  983. || bh->indicator == INDICATOR_LAST)
  984. break;
  985. offset += thislen;
  986. }
  987. }
  988. invalid_rev:
  989. strcpy(a->image_type, "no boot images");
  990. return false;
  991. }
  992. /*
  993. * Read and validate current NVRAM parameters by accessing
  994. * physical NVRAM directly. if currently stored parameters are
  995. * invalid, use the defaults.
  996. */
  997. bool esas2r_nvram_read_direct(struct esas2r_adapter *a)
  998. {
  999. bool result;
  1000. if (down_interruptible(&a->nvram_semaphore))
  1001. return false;
  1002. if (!esas2r_read_flash_block(a, a->nvram, FLS_OFFSET_NVR,
  1003. sizeof(struct esas2r_sas_nvram))) {
  1004. esas2r_hdebug("NVRAM read failed, using defaults");
  1005. return false;
  1006. }
  1007. result = esas2r_nvram_validate(a);
  1008. up(&a->nvram_semaphore);
  1009. return result;
  1010. }
  1011. /* Interrupt callback to process NVRAM completions. */
  1012. static void esas2r_nvram_callback(struct esas2r_adapter *a,
  1013. struct esas2r_request *rq)
  1014. {
  1015. struct atto_vda_flash_req *vrq = &rq->vrq->flash;
  1016. if (rq->req_stat == RS_SUCCESS) {
  1017. /* last request was successful. see what to do now. */
  1018. switch (vrq->sub_func) {
  1019. case VDA_FLASH_BEGINW:
  1020. vrq->sub_func = VDA_FLASH_WRITE;
  1021. rq->req_stat = RS_PENDING;
  1022. break;
  1023. case VDA_FLASH_WRITE:
  1024. vrq->sub_func = VDA_FLASH_COMMIT;
  1025. rq->req_stat = RS_PENDING;
  1026. break;
  1027. case VDA_FLASH_READ:
  1028. esas2r_nvram_validate(a);
  1029. break;
  1030. case VDA_FLASH_COMMIT:
  1031. default:
  1032. break;
  1033. }
  1034. }
  1035. if (rq->req_stat != RS_PENDING) {
  1036. /* update the NVRAM state */
  1037. if (rq->req_stat == RS_SUCCESS)
  1038. esas2r_lock_set_flags(&a->flags, AF_NVR_VALID);
  1039. else
  1040. esas2r_lock_clear_flags(&a->flags, AF_NVR_VALID);
  1041. esas2r_enable_heartbeat(a);
  1042. up(&a->nvram_semaphore);
  1043. }
  1044. }
  1045. /*
  1046. * Write the contents of nvram to the adapter's physical NVRAM.
  1047. * The cached copy of the NVRAM is also updated.
  1048. */
  1049. bool esas2r_nvram_write(struct esas2r_adapter *a, struct esas2r_request *rq,
  1050. struct esas2r_sas_nvram *nvram)
  1051. {
  1052. struct esas2r_sas_nvram *n = nvram;
  1053. u8 sas_address_bytes[8];
  1054. u32 *sas_address_dwords = (u32 *)&sas_address_bytes[0];
  1055. struct atto_vda_flash_req *vrq = &rq->vrq->flash;
  1056. if (a->flags & AF_DEGRADED_MODE)
  1057. return false;
  1058. if (down_interruptible(&a->nvram_semaphore))
  1059. return false;
  1060. if (n == NULL)
  1061. n = a->nvram;
  1062. /* check the validity of the settings */
  1063. if (n->version > SASNVR_VERSION) {
  1064. up(&a->nvram_semaphore);
  1065. return false;
  1066. }
  1067. memcpy(&sas_address_bytes[0], n->sas_addr, 8);
  1068. if (sas_address_bytes[0] != 0x50
  1069. || sas_address_bytes[1] != 0x01
  1070. || sas_address_bytes[2] != 0x08
  1071. || (sas_address_bytes[3] & 0xF0) != 0x60
  1072. || ((sas_address_bytes[3] & 0x0F) | sas_address_dwords[1]) == 0) {
  1073. up(&a->nvram_semaphore);
  1074. return false;
  1075. }
  1076. if (n->spin_up_delay > SASNVR_SPINUP_MAX)
  1077. n->spin_up_delay = SASNVR_SPINUP_MAX;
  1078. n->version = SASNVR_VERSION;
  1079. n->checksum = n->checksum - esas2r_nvramcalc_cksum(n);
  1080. memcpy(a->nvram, n, sizeof(struct esas2r_sas_nvram));
  1081. /* write the NVRAM */
  1082. n = a->nvram;
  1083. esas2r_disable_heartbeat(a);
  1084. esas2r_build_flash_req(a,
  1085. rq,
  1086. VDA_FLASH_BEGINW,
  1087. esas2r_nvramcalc_xor_cksum(n),
  1088. FLS_OFFSET_NVR,
  1089. sizeof(struct esas2r_sas_nvram));
  1090. if (a->flags & AF_LEGACY_SGE_MODE) {
  1091. vrq->data.sge[0].length =
  1092. cpu_to_le32(SGE_LAST |
  1093. sizeof(struct esas2r_sas_nvram));
  1094. vrq->data.sge[0].address = cpu_to_le64(
  1095. a->uncached_phys + (u64)((u8 *)n - a->uncached));
  1096. } else {
  1097. vrq->data.prde[0].ctl_len =
  1098. cpu_to_le32(sizeof(struct esas2r_sas_nvram));
  1099. vrq->data.prde[0].address = cpu_to_le64(
  1100. a->uncached_phys
  1101. + (u64)((u8 *)n - a->uncached));
  1102. }
  1103. rq->interrupt_cb = esas2r_nvram_callback;
  1104. esas2r_start_request(a, rq);
  1105. return true;
  1106. }
  1107. /* Validate the cached NVRAM. if the NVRAM is invalid, load the defaults. */
  1108. bool esas2r_nvram_validate(struct esas2r_adapter *a)
  1109. {
  1110. struct esas2r_sas_nvram *n = a->nvram;
  1111. bool rslt = false;
  1112. if (n->signature[0] != 'E'
  1113. || n->signature[1] != 'S'
  1114. || n->signature[2] != 'A'
  1115. || n->signature[3] != 'S') {
  1116. esas2r_hdebug("invalid NVRAM signature");
  1117. } else if (esas2r_nvramcalc_cksum(n)) {
  1118. esas2r_hdebug("invalid NVRAM checksum");
  1119. } else if (n->version > SASNVR_VERSION) {
  1120. esas2r_hdebug("invalid NVRAM version");
  1121. } else {
  1122. esas2r_lock_set_flags(&a->flags, AF_NVR_VALID);
  1123. rslt = true;
  1124. }
  1125. if (rslt == false) {
  1126. esas2r_hdebug("using defaults");
  1127. esas2r_nvram_set_defaults(a);
  1128. }
  1129. return rslt;
  1130. }
  1131. /*
  1132. * Set the cached NVRAM to defaults. note that this function sets the default
  1133. * NVRAM when it has been determined that the physical NVRAM is invalid.
  1134. * In this case, the SAS address is fabricated.
  1135. */
  1136. void esas2r_nvram_set_defaults(struct esas2r_adapter *a)
  1137. {
  1138. struct esas2r_sas_nvram *n = a->nvram;
  1139. u32 time = jiffies_to_msecs(jiffies);
  1140. esas2r_lock_clear_flags(&a->flags, AF_NVR_VALID);
  1141. *n = default_sas_nvram;
  1142. n->sas_addr[3] |= 0x0F;
  1143. n->sas_addr[4] = HIBYTE(LOWORD(time));
  1144. n->sas_addr[5] = LOBYTE(LOWORD(time));
  1145. n->sas_addr[6] = a->pcid->bus->number;
  1146. n->sas_addr[7] = a->pcid->devfn;
  1147. }
  1148. void esas2r_nvram_get_defaults(struct esas2r_adapter *a,
  1149. struct esas2r_sas_nvram *nvram)
  1150. {
  1151. u8 sas_addr[8];
  1152. /*
  1153. * in case we are copying the defaults into the adapter, copy the SAS
  1154. * address out first.
  1155. */
  1156. memcpy(&sas_addr[0], a->nvram->sas_addr, 8);
  1157. *nvram = default_sas_nvram;
  1158. memcpy(&nvram->sas_addr[0], &sas_addr[0], 8);
  1159. }
  1160. bool esas2r_fm_api(struct esas2r_adapter *a, struct esas2r_flash_img *fi,
  1161. struct esas2r_request *rq, struct esas2r_sg_context *sgc)
  1162. {
  1163. struct esas2r_flash_context *fc = &a->flash_context;
  1164. u8 j;
  1165. struct esas2r_component_header *ch;
  1166. if (esas2r_lock_set_flags(&a->flags, AF_FLASH_LOCK) & AF_FLASH_LOCK) {
  1167. /* flag was already set */
  1168. fi->status = FI_STAT_BUSY;
  1169. return false;
  1170. }
  1171. memcpy(&fc->sgc, sgc, sizeof(struct esas2r_sg_context));
  1172. sgc = &fc->sgc;
  1173. fc->fi = fi;
  1174. fc->sgc_offset = sgc->cur_offset;
  1175. rq->req_stat = RS_SUCCESS;
  1176. rq->interrupt_cx = fc;
  1177. switch (fi->fi_version) {
  1178. case FI_VERSION_1:
  1179. fc->scratch = ((struct esas2r_flash_img *)fi)->scratch_buf;
  1180. fc->num_comps = FI_NUM_COMPS_V1;
  1181. fc->fi_hdr_len = sizeof(struct esas2r_flash_img);
  1182. break;
  1183. default:
  1184. return complete_fmapi_req(a, rq, FI_STAT_IMG_VER);
  1185. }
  1186. if (a->flags & AF_DEGRADED_MODE)
  1187. return complete_fmapi_req(a, rq, FI_STAT_DEGRADED);
  1188. switch (fi->action) {
  1189. case FI_ACT_DOWN: /* Download the components */
  1190. /* Verify the format of the flash image */
  1191. if (!verify_fi(a, fc))
  1192. return complete_fmapi_req(a, rq, fi->status);
  1193. /* Adjust the BIOS fields that are dependent on the HBA */
  1194. ch = &fi->cmp_hdr[CH_IT_BIOS];
  1195. if (ch->length)
  1196. fix_bios(a, fi);
  1197. /* Adjust the EFI fields that are dependent on the HBA */
  1198. ch = &fi->cmp_hdr[CH_IT_EFI];
  1199. if (ch->length)
  1200. fix_efi(a, fi);
  1201. /*
  1202. * Since the image was just modified, compute the checksum on
  1203. * the modified image. First update the CRC for the composite
  1204. * expansion ROM image.
  1205. */
  1206. fi->checksum = calc_fi_checksum(fc);
  1207. /* Disable the heartbeat */
  1208. esas2r_disable_heartbeat(a);
  1209. /* Now start up the download sequence */
  1210. fc->task = FMTSK_ERASE_BOOT;
  1211. fc->func = VDA_FLASH_BEGINW;
  1212. fc->comp_typ = CH_IT_CFG;
  1213. fc->flsh_addr = FLS_OFFSET_BOOT;
  1214. fc->sgc.length = FLS_LENGTH_BOOT;
  1215. fc->sgc.cur_offset = NULL;
  1216. /* Setup the callback address */
  1217. fc->interrupt_cb = fw_download_proc;
  1218. break;
  1219. case FI_ACT_UPSZ: /* Get upload sizes */
  1220. fi->adap_typ = get_fi_adap_type(a);
  1221. fi->flags = 0;
  1222. fi->num_comps = fc->num_comps;
  1223. fi->length = fc->fi_hdr_len;
  1224. /* Report the type of boot image in the rel_version string */
  1225. memcpy(fi->rel_version, a->image_type,
  1226. sizeof(fi->rel_version));
  1227. /* Build the component headers */
  1228. for (j = 0, ch = fi->cmp_hdr;
  1229. j < fi->num_comps;
  1230. j++, ch++) {
  1231. ch->img_type = j;
  1232. ch->status = CH_STAT_PENDING;
  1233. ch->length = 0;
  1234. ch->version = 0xffffffff;
  1235. ch->image_offset = 0;
  1236. ch->pad[0] = 0;
  1237. ch->pad[1] = 0;
  1238. }
  1239. if (a->flash_ver != 0) {
  1240. fi->cmp_hdr[CH_IT_BIOS].version =
  1241. fi->cmp_hdr[CH_IT_MAC].version =
  1242. fi->cmp_hdr[CH_IT_EFI].version =
  1243. fi->cmp_hdr[CH_IT_CFG].version
  1244. = a->flash_ver;
  1245. fi->cmp_hdr[CH_IT_BIOS].status =
  1246. fi->cmp_hdr[CH_IT_MAC].status =
  1247. fi->cmp_hdr[CH_IT_EFI].status =
  1248. fi->cmp_hdr[CH_IT_CFG].status =
  1249. CH_STAT_SUCCESS;
  1250. return complete_fmapi_req(a, rq, FI_STAT_SUCCESS);
  1251. }
  1252. /* fall through */
  1253. case FI_ACT_UP: /* Upload the components */
  1254. default:
  1255. return complete_fmapi_req(a, rq, FI_STAT_INVALID);
  1256. }
  1257. /*
  1258. * If we make it here, fc has been setup to do the first task. Call
  1259. * load_image to format the request, start it, and get out. The
  1260. * interrupt code will call the callback when the first message is
  1261. * complete.
  1262. */
  1263. if (!load_image(a, rq))
  1264. return complete_fmapi_req(a, rq, FI_STAT_FAILED);
  1265. esas2r_start_request(a, rq);
  1266. return true;
  1267. }