aic94xx_sds.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /*
  2. * Aic94xx SAS/SATA driver access to shared data structures and memory
  3. * maps.
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  7. *
  8. * This file is licensed under GPLv2.
  9. *
  10. * This file is part of the aic94xx driver.
  11. *
  12. * The aic94xx driver is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; version 2 of the
  15. * License.
  16. *
  17. * The aic94xx driver is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with the aic94xx driver; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  25. *
  26. */
  27. #include <linux/pci.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include "aic94xx.h"
  31. #include "aic94xx_reg.h"
  32. #include "aic94xx_sds.h"
  33. /* ---------- OCM stuff ---------- */
  34. struct asd_ocm_dir_ent {
  35. u8 type;
  36. u8 offs[3];
  37. u8 _r1;
  38. u8 size[3];
  39. } __attribute__ ((packed));
  40. struct asd_ocm_dir {
  41. char sig[2];
  42. u8 _r1[2];
  43. u8 major; /* 0 */
  44. u8 minor; /* 0 */
  45. u8 _r2;
  46. u8 num_de;
  47. struct asd_ocm_dir_ent entry[15];
  48. } __attribute__ ((packed));
  49. #define OCM_DE_OCM_DIR 0x00
  50. #define OCM_DE_WIN_DRVR 0x01
  51. #define OCM_DE_BIOS_CHIM 0x02
  52. #define OCM_DE_RAID_ENGN 0x03
  53. #define OCM_DE_BIOS_INTL 0x04
  54. #define OCM_DE_BIOS_CHIM_OSM 0x05
  55. #define OCM_DE_BIOS_CHIM_DYNAMIC 0x06
  56. #define OCM_DE_ADDC2C_RES0 0x07
  57. #define OCM_DE_ADDC2C_RES1 0x08
  58. #define OCM_DE_ADDC2C_RES2 0x09
  59. #define OCM_DE_ADDC2C_RES3 0x0A
  60. #define OCM_INIT_DIR_ENTRIES 5
  61. /***************************************************************************
  62. * OCM directory default
  63. ***************************************************************************/
  64. static struct asd_ocm_dir OCMDirInit =
  65. {
  66. .sig = {0x4D, 0x4F}, /* signature */
  67. .num_de = OCM_INIT_DIR_ENTRIES, /* no. of directory entries */
  68. };
  69. /***************************************************************************
  70. * OCM directory Entries default
  71. ***************************************************************************/
  72. static struct asd_ocm_dir_ent OCMDirEntriesInit[OCM_INIT_DIR_ENTRIES] =
  73. {
  74. {
  75. .type = (OCM_DE_ADDC2C_RES0), /* Entry type */
  76. .offs = {128}, /* Offset */
  77. .size = {0, 4}, /* size */
  78. },
  79. {
  80. .type = (OCM_DE_ADDC2C_RES1), /* Entry type */
  81. .offs = {128, 4}, /* Offset */
  82. .size = {0, 4}, /* size */
  83. },
  84. {
  85. .type = (OCM_DE_ADDC2C_RES2), /* Entry type */
  86. .offs = {128, 8}, /* Offset */
  87. .size = {0, 4}, /* size */
  88. },
  89. {
  90. .type = (OCM_DE_ADDC2C_RES3), /* Entry type */
  91. .offs = {128, 12}, /* Offset */
  92. .size = {0, 4}, /* size */
  93. },
  94. {
  95. .type = (OCM_DE_WIN_DRVR), /* Entry type */
  96. .offs = {128, 16}, /* Offset */
  97. .size = {128, 235, 1}, /* size */
  98. },
  99. };
  100. struct asd_bios_chim_struct {
  101. char sig[4];
  102. u8 major; /* 1 */
  103. u8 minor; /* 0 */
  104. u8 bios_major;
  105. u8 bios_minor;
  106. __le32 bios_build;
  107. u8 flags;
  108. u8 pci_slot;
  109. __le16 ue_num;
  110. __le16 ue_size;
  111. u8 _r[14];
  112. /* The unit element array is right here.
  113. */
  114. } __attribute__ ((packed));
  115. /**
  116. * asd_read_ocm_seg - read an on chip memory (OCM) segment
  117. * @asd_ha: pointer to the host adapter structure
  118. * @buffer: where to write the read data
  119. * @offs: offset into OCM where to read from
  120. * @size: how many bytes to read
  121. *
  122. * Return the number of bytes not read. Return 0 on success.
  123. */
  124. static int asd_read_ocm_seg(struct asd_ha_struct *asd_ha, void *buffer,
  125. u32 offs, int size)
  126. {
  127. u8 *p = buffer;
  128. if (unlikely(asd_ha->iospace))
  129. asd_read_reg_string(asd_ha, buffer, offs+OCM_BASE_ADDR, size);
  130. else {
  131. for ( ; size > 0; size--, offs++, p++)
  132. *p = asd_read_ocm_byte(asd_ha, offs);
  133. }
  134. return size;
  135. }
  136. static int asd_read_ocm_dir(struct asd_ha_struct *asd_ha,
  137. struct asd_ocm_dir *dir, u32 offs)
  138. {
  139. int err = asd_read_ocm_seg(asd_ha, dir, offs, sizeof(*dir));
  140. if (err) {
  141. ASD_DPRINTK("couldn't read ocm segment\n");
  142. return err;
  143. }
  144. if (dir->sig[0] != 'M' || dir->sig[1] != 'O') {
  145. ASD_DPRINTK("no valid dir signature(%c%c) at start of OCM\n",
  146. dir->sig[0], dir->sig[1]);
  147. return -ENOENT;
  148. }
  149. if (dir->major != 0) {
  150. asd_printk("unsupported major version of ocm dir:0x%x\n",
  151. dir->major);
  152. return -ENOENT;
  153. }
  154. dir->num_de &= 0xf;
  155. return 0;
  156. }
  157. /**
  158. * asd_write_ocm_seg - write an on chip memory (OCM) segment
  159. * @asd_ha: pointer to the host adapter structure
  160. * @buffer: where to read the write data
  161. * @offs: offset into OCM to write to
  162. * @size: how many bytes to write
  163. *
  164. * Return the number of bytes not written. Return 0 on success.
  165. */
  166. static void asd_write_ocm_seg(struct asd_ha_struct *asd_ha, void *buffer,
  167. u32 offs, int size)
  168. {
  169. u8 *p = buffer;
  170. if (unlikely(asd_ha->iospace))
  171. asd_write_reg_string(asd_ha, buffer, offs+OCM_BASE_ADDR, size);
  172. else {
  173. for ( ; size > 0; size--, offs++, p++)
  174. asd_write_ocm_byte(asd_ha, offs, *p);
  175. }
  176. return;
  177. }
  178. #define THREE_TO_NUM(X) ((X)[0] | ((X)[1] << 8) | ((X)[2] << 16))
  179. static int asd_find_dir_entry(struct asd_ocm_dir *dir, u8 type,
  180. u32 *offs, u32 *size)
  181. {
  182. int i;
  183. struct asd_ocm_dir_ent *ent;
  184. for (i = 0; i < dir->num_de; i++) {
  185. if (dir->entry[i].type == type)
  186. break;
  187. }
  188. if (i >= dir->num_de)
  189. return -ENOENT;
  190. ent = &dir->entry[i];
  191. *offs = (u32) THREE_TO_NUM(ent->offs);
  192. *size = (u32) THREE_TO_NUM(ent->size);
  193. return 0;
  194. }
  195. #define OCM_BIOS_CHIM_DE 2
  196. #define BC_BIOS_PRESENT 1
  197. static int asd_get_bios_chim(struct asd_ha_struct *asd_ha,
  198. struct asd_ocm_dir *dir)
  199. {
  200. int err;
  201. struct asd_bios_chim_struct *bc_struct;
  202. u32 offs, size;
  203. err = asd_find_dir_entry(dir, OCM_BIOS_CHIM_DE, &offs, &size);
  204. if (err) {
  205. ASD_DPRINTK("couldn't find BIOS_CHIM dir ent\n");
  206. goto out;
  207. }
  208. err = -ENOMEM;
  209. bc_struct = kmalloc(sizeof(*bc_struct), GFP_KERNEL);
  210. if (!bc_struct) {
  211. asd_printk("no memory for bios_chim struct\n");
  212. goto out;
  213. }
  214. err = asd_read_ocm_seg(asd_ha, (void *)bc_struct, offs,
  215. sizeof(*bc_struct));
  216. if (err) {
  217. ASD_DPRINTK("couldn't read ocm segment\n");
  218. goto out2;
  219. }
  220. if (strncmp(bc_struct->sig, "SOIB", 4)
  221. && strncmp(bc_struct->sig, "IPSA", 4)) {
  222. ASD_DPRINTK("BIOS_CHIM entry has no valid sig(%c%c%c%c)\n",
  223. bc_struct->sig[0], bc_struct->sig[1],
  224. bc_struct->sig[2], bc_struct->sig[3]);
  225. err = -ENOENT;
  226. goto out2;
  227. }
  228. if (bc_struct->major != 1) {
  229. asd_printk("BIOS_CHIM unsupported major version:0x%x\n",
  230. bc_struct->major);
  231. err = -ENOENT;
  232. goto out2;
  233. }
  234. if (bc_struct->flags & BC_BIOS_PRESENT) {
  235. asd_ha->hw_prof.bios.present = 1;
  236. asd_ha->hw_prof.bios.maj = bc_struct->bios_major;
  237. asd_ha->hw_prof.bios.min = bc_struct->bios_minor;
  238. asd_ha->hw_prof.bios.bld = le32_to_cpu(bc_struct->bios_build);
  239. ASD_DPRINTK("BIOS present (%d,%d), %d\n",
  240. asd_ha->hw_prof.bios.maj,
  241. asd_ha->hw_prof.bios.min,
  242. asd_ha->hw_prof.bios.bld);
  243. }
  244. asd_ha->hw_prof.ue.num = le16_to_cpu(bc_struct->ue_num);
  245. asd_ha->hw_prof.ue.size= le16_to_cpu(bc_struct->ue_size);
  246. ASD_DPRINTK("ue num:%d, ue size:%d\n", asd_ha->hw_prof.ue.num,
  247. asd_ha->hw_prof.ue.size);
  248. size = asd_ha->hw_prof.ue.num * asd_ha->hw_prof.ue.size;
  249. if (size > 0) {
  250. err = -ENOMEM;
  251. asd_ha->hw_prof.ue.area = kmalloc(size, GFP_KERNEL);
  252. if (!asd_ha->hw_prof.ue.area)
  253. goto out2;
  254. err = asd_read_ocm_seg(asd_ha, (void *)asd_ha->hw_prof.ue.area,
  255. offs + sizeof(*bc_struct), size);
  256. if (err) {
  257. kfree(asd_ha->hw_prof.ue.area);
  258. asd_ha->hw_prof.ue.area = NULL;
  259. asd_ha->hw_prof.ue.num = 0;
  260. asd_ha->hw_prof.ue.size = 0;
  261. ASD_DPRINTK("couldn't read ue entries(%d)\n", err);
  262. }
  263. }
  264. out2:
  265. kfree(bc_struct);
  266. out:
  267. return err;
  268. }
  269. static void
  270. asd_hwi_initialize_ocm_dir (struct asd_ha_struct *asd_ha)
  271. {
  272. int i;
  273. /* Zero OCM */
  274. for (i = 0; i < OCM_MAX_SIZE; i += 4)
  275. asd_write_ocm_dword(asd_ha, i, 0);
  276. /* Write Dir */
  277. asd_write_ocm_seg(asd_ha, &OCMDirInit, 0,
  278. sizeof(struct asd_ocm_dir));
  279. /* Write Dir Entries */
  280. for (i = 0; i < OCM_INIT_DIR_ENTRIES; i++)
  281. asd_write_ocm_seg(asd_ha, &OCMDirEntriesInit[i],
  282. sizeof(struct asd_ocm_dir) +
  283. (i * sizeof(struct asd_ocm_dir_ent))
  284. , sizeof(struct asd_ocm_dir_ent));
  285. }
  286. static int
  287. asd_hwi_check_ocm_access (struct asd_ha_struct *asd_ha)
  288. {
  289. struct pci_dev *pcidev = asd_ha->pcidev;
  290. u32 reg;
  291. int err = 0;
  292. u32 v;
  293. /* check if OCM has been initialized by BIOS */
  294. reg = asd_read_reg_dword(asd_ha, EXSICNFGR);
  295. if (!(reg & OCMINITIALIZED)) {
  296. err = pci_read_config_dword(pcidev, PCIC_INTRPT_STAT, &v);
  297. if (err) {
  298. asd_printk("couldn't access PCIC_INTRPT_STAT of %s\n",
  299. pci_name(pcidev));
  300. goto out;
  301. }
  302. printk(KERN_INFO "OCM is not initialized by BIOS,"
  303. "reinitialize it and ignore it, current IntrptStatus"
  304. "is 0x%x\n", v);
  305. if (v)
  306. err = pci_write_config_dword(pcidev,
  307. PCIC_INTRPT_STAT, v);
  308. if (err) {
  309. asd_printk("couldn't write PCIC_INTRPT_STAT of %s\n",
  310. pci_name(pcidev));
  311. goto out;
  312. }
  313. asd_hwi_initialize_ocm_dir(asd_ha);
  314. }
  315. out:
  316. return err;
  317. }
  318. /**
  319. * asd_read_ocm - read on chip memory (OCM)
  320. * @asd_ha: pointer to the host adapter structure
  321. */
  322. int asd_read_ocm(struct asd_ha_struct *asd_ha)
  323. {
  324. int err;
  325. struct asd_ocm_dir *dir;
  326. if (asd_hwi_check_ocm_access(asd_ha))
  327. return -1;
  328. dir = kmalloc(sizeof(*dir), GFP_KERNEL);
  329. if (!dir) {
  330. asd_printk("no memory for ocm dir\n");
  331. return -ENOMEM;
  332. }
  333. err = asd_read_ocm_dir(asd_ha, dir, 0);
  334. if (err)
  335. goto out;
  336. err = asd_get_bios_chim(asd_ha, dir);
  337. out:
  338. kfree(dir);
  339. return err;
  340. }
  341. /* ---------- FLASH stuff ---------- */
  342. #define FLASH_RESET 0xF0
  343. #define ASD_FLASH_SIZE 0x200000
  344. #define FLASH_DIR_COOKIE "*** ADAPTEC FLASH DIRECTORY *** "
  345. #define FLASH_NEXT_ENTRY_OFFS 0x2000
  346. #define FLASH_MAX_DIR_ENTRIES 32
  347. #define FLASH_DE_TYPE_MASK 0x3FFFFFFF
  348. #define FLASH_DE_MS 0x120
  349. #define FLASH_DE_CTRL_A_USER 0xE0
  350. struct asd_flash_de {
  351. __le32 type;
  352. __le32 offs;
  353. __le32 pad_size;
  354. __le32 image_size;
  355. __le32 chksum;
  356. u8 _r[12];
  357. u8 version[32];
  358. } __attribute__ ((packed));
  359. struct asd_flash_dir {
  360. u8 cookie[32];
  361. __le32 rev; /* 2 */
  362. __le32 chksum;
  363. __le32 chksum_antidote;
  364. __le32 bld;
  365. u8 bld_id[32]; /* build id data */
  366. u8 ver_data[32]; /* date and time of build */
  367. __le32 ae_mask;
  368. __le32 v_mask;
  369. __le32 oc_mask;
  370. u8 _r[20];
  371. struct asd_flash_de dir_entry[FLASH_MAX_DIR_ENTRIES];
  372. } __attribute__ ((packed));
  373. struct asd_manuf_sec {
  374. char sig[2]; /* 'S', 'M' */
  375. u16 offs_next;
  376. u8 maj; /* 0 */
  377. u8 min; /* 0 */
  378. u16 chksum;
  379. u16 size;
  380. u8 _r[6];
  381. u8 sas_addr[SAS_ADDR_SIZE];
  382. u8 pcba_sn[ASD_PCBA_SN_SIZE];
  383. /* Here start the other segments */
  384. u8 linked_list[0];
  385. } __attribute__ ((packed));
  386. struct asd_manuf_phy_desc {
  387. u8 state; /* low 4 bits */
  388. #define MS_PHY_STATE_ENABLED 0
  389. #define MS_PHY_STATE_REPORTED 1
  390. #define MS_PHY_STATE_HIDDEN 2
  391. u8 phy_id;
  392. u16 _r;
  393. u8 phy_control_0; /* mode 5 reg 0x160 */
  394. u8 phy_control_1; /* mode 5 reg 0x161 */
  395. u8 phy_control_2; /* mode 5 reg 0x162 */
  396. u8 phy_control_3; /* mode 5 reg 0x163 */
  397. } __attribute__ ((packed));
  398. struct asd_manuf_phy_param {
  399. char sig[2]; /* 'P', 'M' */
  400. u16 next;
  401. u8 maj; /* 0 */
  402. u8 min; /* 2 */
  403. u8 num_phy_desc; /* 8 */
  404. u8 phy_desc_size; /* 8 */
  405. u8 _r[3];
  406. u8 usage_model_id;
  407. u32 _r2;
  408. struct asd_manuf_phy_desc phy_desc[ASD_MAX_PHYS];
  409. } __attribute__ ((packed));
  410. #if 0
  411. static const char *asd_sb_type[] = {
  412. "unknown",
  413. "SGPIO",
  414. [2 ... 0x7F] = "unknown",
  415. [0x80] = "ADPT_I2C",
  416. [0x81 ... 0xFF] = "VENDOR_UNIQUExx"
  417. };
  418. #endif
  419. struct asd_ms_sb_desc {
  420. u8 type;
  421. u8 node_desc_index;
  422. u8 conn_desc_index;
  423. u8 _recvd[0];
  424. } __attribute__ ((packed));
  425. #if 0
  426. static const char *asd_conn_type[] = {
  427. [0 ... 7] = "unknown",
  428. "SFF8470",
  429. "SFF8482",
  430. "SFF8484",
  431. [0x80] = "PCIX_DAUGHTER0",
  432. [0x81] = "SAS_DAUGHTER0",
  433. [0x82 ... 0xFF] = "VENDOR_UNIQUExx"
  434. };
  435. static const char *asd_conn_location[] = {
  436. "unknown",
  437. "internal",
  438. "external",
  439. "board_to_board",
  440. };
  441. #endif
  442. struct asd_ms_conn_desc {
  443. u8 type;
  444. u8 location;
  445. u8 num_sideband_desc;
  446. u8 size_sideband_desc;
  447. u32 _resvd;
  448. u8 name[16];
  449. struct asd_ms_sb_desc sb_desc[0];
  450. } __attribute__ ((packed));
  451. struct asd_nd_phy_desc {
  452. u8 vp_attch_type;
  453. u8 attch_specific[0];
  454. } __attribute__ ((packed));
  455. #if 0
  456. static const char *asd_node_type[] = {
  457. "IOP",
  458. "IO_CONTROLLER",
  459. "EXPANDER",
  460. "PORT_MULTIPLIER",
  461. "PORT_MULTIPLEXER",
  462. "MULTI_DROP_I2C_BUS",
  463. };
  464. #endif
  465. struct asd_ms_node_desc {
  466. u8 type;
  467. u8 num_phy_desc;
  468. u8 size_phy_desc;
  469. u8 _resvd;
  470. u8 name[16];
  471. struct asd_nd_phy_desc phy_desc[0];
  472. } __attribute__ ((packed));
  473. struct asd_ms_conn_map {
  474. char sig[2]; /* 'M', 'C' */
  475. __le16 next;
  476. u8 maj; /* 0 */
  477. u8 min; /* 0 */
  478. __le16 cm_size; /* size of this struct */
  479. u8 num_conn;
  480. u8 conn_size;
  481. u8 num_nodes;
  482. u8 usage_model_id;
  483. u32 _resvd;
  484. struct asd_ms_conn_desc conn_desc[0];
  485. struct asd_ms_node_desc node_desc[0];
  486. } __attribute__ ((packed));
  487. struct asd_ctrla_phy_entry {
  488. u8 sas_addr[SAS_ADDR_SIZE];
  489. u8 sas_link_rates; /* max in hi bits, min in low bits */
  490. u8 flags;
  491. u8 sata_link_rates;
  492. u8 _r[5];
  493. } __attribute__ ((packed));
  494. struct asd_ctrla_phy_settings {
  495. u8 id0; /* P'h'y */
  496. u8 _r;
  497. u16 next;
  498. u8 num_phys; /* number of PHYs in the PCI function */
  499. u8 _r2[3];
  500. struct asd_ctrla_phy_entry phy_ent[ASD_MAX_PHYS];
  501. } __attribute__ ((packed));
  502. struct asd_ll_el {
  503. u8 id0;
  504. u8 id1;
  505. __le16 next;
  506. u8 something_here[0];
  507. } __attribute__ ((packed));
  508. static int asd_poll_flash(struct asd_ha_struct *asd_ha)
  509. {
  510. int c;
  511. u8 d;
  512. for (c = 5000; c > 0; c--) {
  513. d = asd_read_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar);
  514. d ^= asd_read_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar);
  515. if (!d)
  516. return 0;
  517. udelay(5);
  518. }
  519. return -ENOENT;
  520. }
  521. static int asd_reset_flash(struct asd_ha_struct *asd_ha)
  522. {
  523. int err;
  524. err = asd_poll_flash(asd_ha);
  525. if (err)
  526. return err;
  527. asd_write_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar, FLASH_RESET);
  528. err = asd_poll_flash(asd_ha);
  529. return err;
  530. }
  531. static int asd_read_flash_seg(struct asd_ha_struct *asd_ha,
  532. void *buffer, u32 offs, int size)
  533. {
  534. asd_read_reg_string(asd_ha, buffer, asd_ha->hw_prof.flash.bar+offs,
  535. size);
  536. return 0;
  537. }
  538. /**
  539. * asd_find_flash_dir - finds and reads the flash directory
  540. * @asd_ha: pointer to the host adapter structure
  541. * @flash_dir: pointer to flash directory structure
  542. *
  543. * If found, the flash directory segment will be copied to
  544. * @flash_dir. Return 1 if found, 0 if not.
  545. */
  546. static int asd_find_flash_dir(struct asd_ha_struct *asd_ha,
  547. struct asd_flash_dir *flash_dir)
  548. {
  549. u32 v;
  550. for (v = 0; v < ASD_FLASH_SIZE; v += FLASH_NEXT_ENTRY_OFFS) {
  551. asd_read_flash_seg(asd_ha, flash_dir, v,
  552. sizeof(FLASH_DIR_COOKIE)-1);
  553. if (memcmp(flash_dir->cookie, FLASH_DIR_COOKIE,
  554. sizeof(FLASH_DIR_COOKIE)-1) == 0) {
  555. asd_ha->hw_prof.flash.dir_offs = v;
  556. asd_read_flash_seg(asd_ha, flash_dir, v,
  557. sizeof(*flash_dir));
  558. return 1;
  559. }
  560. }
  561. return 0;
  562. }
  563. static int asd_flash_getid(struct asd_ha_struct *asd_ha)
  564. {
  565. int err = 0;
  566. u32 reg;
  567. reg = asd_read_reg_dword(asd_ha, EXSICNFGR);
  568. if (pci_read_config_dword(asd_ha->pcidev, PCI_CONF_FLSH_BAR,
  569. &asd_ha->hw_prof.flash.bar)) {
  570. asd_printk("couldn't read PCI_CONF_FLSH_BAR of %s\n",
  571. pci_name(asd_ha->pcidev));
  572. return -ENOENT;
  573. }
  574. asd_ha->hw_prof.flash.present = 1;
  575. asd_ha->hw_prof.flash.wide = reg & FLASHW ? 1 : 0;
  576. err = asd_reset_flash(asd_ha);
  577. if (err) {
  578. ASD_DPRINTK("couldn't reset flash(%d)\n", err);
  579. return err;
  580. }
  581. return 0;
  582. }
  583. static u16 asd_calc_flash_chksum(u16 *p, int size)
  584. {
  585. u16 chksum = 0;
  586. while (size-- > 0)
  587. chksum += *p++;
  588. return chksum;
  589. }
  590. static int asd_find_flash_de(struct asd_flash_dir *flash_dir, u32 entry_type,
  591. u32 *offs, u32 *size)
  592. {
  593. int i;
  594. struct asd_flash_de *de;
  595. for (i = 0; i < FLASH_MAX_DIR_ENTRIES; i++) {
  596. u32 type = le32_to_cpu(flash_dir->dir_entry[i].type);
  597. type &= FLASH_DE_TYPE_MASK;
  598. if (type == entry_type)
  599. break;
  600. }
  601. if (i >= FLASH_MAX_DIR_ENTRIES)
  602. return -ENOENT;
  603. de = &flash_dir->dir_entry[i];
  604. *offs = le32_to_cpu(de->offs);
  605. *size = le32_to_cpu(de->pad_size);
  606. return 0;
  607. }
  608. static int asd_validate_ms(struct asd_manuf_sec *ms)
  609. {
  610. if (ms->sig[0] != 'S' || ms->sig[1] != 'M') {
  611. ASD_DPRINTK("manuf sec: no valid sig(%c%c)\n",
  612. ms->sig[0], ms->sig[1]);
  613. return -ENOENT;
  614. }
  615. if (ms->maj != 0) {
  616. asd_printk("unsupported manuf. sector. major version:%x\n",
  617. ms->maj);
  618. return -ENOENT;
  619. }
  620. ms->offs_next = le16_to_cpu((__force __le16) ms->offs_next);
  621. ms->chksum = le16_to_cpu((__force __le16) ms->chksum);
  622. ms->size = le16_to_cpu((__force __le16) ms->size);
  623. if (asd_calc_flash_chksum((u16 *)ms, ms->size/2)) {
  624. asd_printk("failed manuf sector checksum\n");
  625. }
  626. return 0;
  627. }
  628. static int asd_ms_get_sas_addr(struct asd_ha_struct *asd_ha,
  629. struct asd_manuf_sec *ms)
  630. {
  631. memcpy(asd_ha->hw_prof.sas_addr, ms->sas_addr, SAS_ADDR_SIZE);
  632. return 0;
  633. }
  634. static int asd_ms_get_pcba_sn(struct asd_ha_struct *asd_ha,
  635. struct asd_manuf_sec *ms)
  636. {
  637. memcpy(asd_ha->hw_prof.pcba_sn, ms->pcba_sn, ASD_PCBA_SN_SIZE);
  638. asd_ha->hw_prof.pcba_sn[ASD_PCBA_SN_SIZE] = '\0';
  639. return 0;
  640. }
  641. /**
  642. * asd_find_ll_by_id - find a linked list entry by its id
  643. * @start: void pointer to the first element in the linked list
  644. * @id0: the first byte of the id (offs 0)
  645. * @id1: the second byte of the id (offs 1)
  646. *
  647. * @start has to be the _base_ element start, since the
  648. * linked list entries's offset is from this pointer.
  649. * Some linked list entries use only the first id, in which case
  650. * you can pass 0xFF for the second.
  651. */
  652. static void *asd_find_ll_by_id(void * const start, const u8 id0, const u8 id1)
  653. {
  654. struct asd_ll_el *el = start;
  655. do {
  656. switch (id1) {
  657. default:
  658. if (el->id1 == id1)
  659. case 0xFF:
  660. if (el->id0 == id0)
  661. return el;
  662. }
  663. el = start + le16_to_cpu(el->next);
  664. } while (el != start);
  665. return NULL;
  666. }
  667. /**
  668. * asd_ms_get_phy_params - get phy parameters from the manufacturing sector
  669. * @asd_ha: pointer to the host adapter structure
  670. * @manuf_sec: pointer to the manufacturing sector
  671. *
  672. * The manufacturing sector contans also the linked list of sub-segments,
  673. * since when it was read, its size was taken from the flash directory,
  674. * not from the structure size.
  675. *
  676. * HIDDEN phys do not count in the total count. REPORTED phys cannot
  677. * be enabled but are reported and counted towards the total.
  678. * ENABLED phys are enabled by default and count towards the total.
  679. * The absolute total phy number is ASD_MAX_PHYS. hw_prof->num_phys
  680. * merely specifies the number of phys the host adapter decided to
  681. * report. E.g., it is possible for phys 0, 1 and 2 to be HIDDEN,
  682. * phys 3, 4 and 5 to be REPORTED and phys 6 and 7 to be ENABLED.
  683. * In this case ASD_MAX_PHYS is 8, hw_prof->num_phys is 5, and only 2
  684. * are actually enabled (enabled by default, max number of phys
  685. * enableable in this case).
  686. */
  687. static int asd_ms_get_phy_params(struct asd_ha_struct *asd_ha,
  688. struct asd_manuf_sec *manuf_sec)
  689. {
  690. int i;
  691. int en_phys = 0;
  692. int rep_phys = 0;
  693. struct asd_manuf_phy_param *phy_param;
  694. struct asd_manuf_phy_param dflt_phy_param;
  695. phy_param = asd_find_ll_by_id(manuf_sec, 'P', 'M');
  696. if (!phy_param) {
  697. ASD_DPRINTK("ms: no phy parameters found\n");
  698. ASD_DPRINTK("ms: Creating default phy parameters\n");
  699. dflt_phy_param.sig[0] = 'P';
  700. dflt_phy_param.sig[1] = 'M';
  701. dflt_phy_param.maj = 0;
  702. dflt_phy_param.min = 2;
  703. dflt_phy_param.num_phy_desc = 8;
  704. dflt_phy_param.phy_desc_size = sizeof(struct asd_manuf_phy_desc);
  705. for (i =0; i < ASD_MAX_PHYS; i++) {
  706. dflt_phy_param.phy_desc[i].state = 0;
  707. dflt_phy_param.phy_desc[i].phy_id = i;
  708. dflt_phy_param.phy_desc[i].phy_control_0 = 0xf6;
  709. dflt_phy_param.phy_desc[i].phy_control_1 = 0x10;
  710. dflt_phy_param.phy_desc[i].phy_control_2 = 0x43;
  711. dflt_phy_param.phy_desc[i].phy_control_3 = 0xeb;
  712. }
  713. phy_param = &dflt_phy_param;
  714. }
  715. if (phy_param->maj != 0) {
  716. asd_printk("unsupported manuf. phy param major version:0x%x\n",
  717. phy_param->maj);
  718. return -ENOENT;
  719. }
  720. ASD_DPRINTK("ms: num_phy_desc: %d\n", phy_param->num_phy_desc);
  721. asd_ha->hw_prof.enabled_phys = 0;
  722. for (i = 0; i < phy_param->num_phy_desc; i++) {
  723. struct asd_manuf_phy_desc *pd = &phy_param->phy_desc[i];
  724. switch (pd->state & 0xF) {
  725. case MS_PHY_STATE_HIDDEN:
  726. ASD_DPRINTK("ms: phy%d: HIDDEN\n", i);
  727. continue;
  728. case MS_PHY_STATE_REPORTED:
  729. ASD_DPRINTK("ms: phy%d: REPORTED\n", i);
  730. asd_ha->hw_prof.enabled_phys &= ~(1 << i);
  731. rep_phys++;
  732. continue;
  733. case MS_PHY_STATE_ENABLED:
  734. ASD_DPRINTK("ms: phy%d: ENABLED\n", i);
  735. asd_ha->hw_prof.enabled_phys |= (1 << i);
  736. en_phys++;
  737. break;
  738. }
  739. asd_ha->hw_prof.phy_desc[i].phy_control_0 = pd->phy_control_0;
  740. asd_ha->hw_prof.phy_desc[i].phy_control_1 = pd->phy_control_1;
  741. asd_ha->hw_prof.phy_desc[i].phy_control_2 = pd->phy_control_2;
  742. asd_ha->hw_prof.phy_desc[i].phy_control_3 = pd->phy_control_3;
  743. }
  744. asd_ha->hw_prof.max_phys = rep_phys + en_phys;
  745. asd_ha->hw_prof.num_phys = en_phys;
  746. ASD_DPRINTK("ms: max_phys:0x%x, num_phys:0x%x\n",
  747. asd_ha->hw_prof.max_phys, asd_ha->hw_prof.num_phys);
  748. ASD_DPRINTK("ms: enabled_phys:0x%x\n", asd_ha->hw_prof.enabled_phys);
  749. return 0;
  750. }
  751. static int asd_ms_get_connector_map(struct asd_ha_struct *asd_ha,
  752. struct asd_manuf_sec *manuf_sec)
  753. {
  754. struct asd_ms_conn_map *cm;
  755. cm = asd_find_ll_by_id(manuf_sec, 'M', 'C');
  756. if (!cm) {
  757. ASD_DPRINTK("ms: no connector map found\n");
  758. return 0;
  759. }
  760. if (cm->maj != 0) {
  761. ASD_DPRINTK("ms: unsupported: connector map major version 0x%x"
  762. "\n", cm->maj);
  763. return -ENOENT;
  764. }
  765. /* XXX */
  766. return 0;
  767. }
  768. /**
  769. * asd_process_ms - find and extract information from the manufacturing sector
  770. * @asd_ha: pointer to the host adapter structure
  771. * @flash_dir: pointer to the flash directory
  772. */
  773. static int asd_process_ms(struct asd_ha_struct *asd_ha,
  774. struct asd_flash_dir *flash_dir)
  775. {
  776. int err;
  777. struct asd_manuf_sec *manuf_sec;
  778. u32 offs, size;
  779. err = asd_find_flash_de(flash_dir, FLASH_DE_MS, &offs, &size);
  780. if (err) {
  781. ASD_DPRINTK("Couldn't find the manuf. sector\n");
  782. goto out;
  783. }
  784. if (size == 0)
  785. goto out;
  786. err = -ENOMEM;
  787. manuf_sec = kmalloc(size, GFP_KERNEL);
  788. if (!manuf_sec) {
  789. ASD_DPRINTK("no mem for manuf sector\n");
  790. goto out;
  791. }
  792. err = asd_read_flash_seg(asd_ha, (void *)manuf_sec, offs, size);
  793. if (err) {
  794. ASD_DPRINTK("couldn't read manuf sector at 0x%x, size 0x%x\n",
  795. offs, size);
  796. goto out2;
  797. }
  798. err = asd_validate_ms(manuf_sec);
  799. if (err) {
  800. ASD_DPRINTK("couldn't validate manuf sector\n");
  801. goto out2;
  802. }
  803. err = asd_ms_get_sas_addr(asd_ha, manuf_sec);
  804. if (err) {
  805. ASD_DPRINTK("couldn't read the SAS_ADDR\n");
  806. goto out2;
  807. }
  808. ASD_DPRINTK("manuf sect SAS_ADDR %llx\n",
  809. SAS_ADDR(asd_ha->hw_prof.sas_addr));
  810. err = asd_ms_get_pcba_sn(asd_ha, manuf_sec);
  811. if (err) {
  812. ASD_DPRINTK("couldn't read the PCBA SN\n");
  813. goto out2;
  814. }
  815. ASD_DPRINTK("manuf sect PCBA SN %s\n", asd_ha->hw_prof.pcba_sn);
  816. err = asd_ms_get_phy_params(asd_ha, manuf_sec);
  817. if (err) {
  818. ASD_DPRINTK("ms: couldn't get phy parameters\n");
  819. goto out2;
  820. }
  821. err = asd_ms_get_connector_map(asd_ha, manuf_sec);
  822. if (err) {
  823. ASD_DPRINTK("ms: couldn't get connector map\n");
  824. goto out2;
  825. }
  826. out2:
  827. kfree(manuf_sec);
  828. out:
  829. return err;
  830. }
  831. static int asd_process_ctrla_phy_settings(struct asd_ha_struct *asd_ha,
  832. struct asd_ctrla_phy_settings *ps)
  833. {
  834. int i;
  835. for (i = 0; i < ps->num_phys; i++) {
  836. struct asd_ctrla_phy_entry *pe = &ps->phy_ent[i];
  837. if (!PHY_ENABLED(asd_ha, i))
  838. continue;
  839. if (*(u64 *)pe->sas_addr == 0) {
  840. asd_ha->hw_prof.enabled_phys &= ~(1 << i);
  841. continue;
  842. }
  843. /* This is the SAS address which should be sent in IDENTIFY. */
  844. memcpy(asd_ha->hw_prof.phy_desc[i].sas_addr, pe->sas_addr,
  845. SAS_ADDR_SIZE);
  846. asd_ha->hw_prof.phy_desc[i].max_sas_lrate =
  847. (pe->sas_link_rates & 0xF0) >> 4;
  848. asd_ha->hw_prof.phy_desc[i].min_sas_lrate =
  849. (pe->sas_link_rates & 0x0F);
  850. asd_ha->hw_prof.phy_desc[i].max_sata_lrate =
  851. (pe->sata_link_rates & 0xF0) >> 4;
  852. asd_ha->hw_prof.phy_desc[i].min_sata_lrate =
  853. (pe->sata_link_rates & 0x0F);
  854. asd_ha->hw_prof.phy_desc[i].flags = pe->flags;
  855. ASD_DPRINTK("ctrla: phy%d: sas_addr: %llx, sas rate:0x%x-0x%x,"
  856. " sata rate:0x%x-0x%x, flags:0x%x\n",
  857. i,
  858. SAS_ADDR(asd_ha->hw_prof.phy_desc[i].sas_addr),
  859. asd_ha->hw_prof.phy_desc[i].max_sas_lrate,
  860. asd_ha->hw_prof.phy_desc[i].min_sas_lrate,
  861. asd_ha->hw_prof.phy_desc[i].max_sata_lrate,
  862. asd_ha->hw_prof.phy_desc[i].min_sata_lrate,
  863. asd_ha->hw_prof.phy_desc[i].flags);
  864. }
  865. return 0;
  866. }
  867. /**
  868. * asd_process_ctrl_a_user - process CTRL-A user settings
  869. * @asd_ha: pointer to the host adapter structure
  870. * @flash_dir: pointer to the flash directory
  871. */
  872. static int asd_process_ctrl_a_user(struct asd_ha_struct *asd_ha,
  873. struct asd_flash_dir *flash_dir)
  874. {
  875. int err, i;
  876. u32 offs, size;
  877. struct asd_ll_el *el = NULL;
  878. struct asd_ctrla_phy_settings *ps;
  879. struct asd_ctrla_phy_settings dflt_ps;
  880. err = asd_find_flash_de(flash_dir, FLASH_DE_CTRL_A_USER, &offs, &size);
  881. if (err) {
  882. ASD_DPRINTK("couldn't find CTRL-A user settings section\n");
  883. ASD_DPRINTK("Creating default CTRL-A user settings section\n");
  884. dflt_ps.id0 = 'h';
  885. dflt_ps.num_phys = 8;
  886. for (i =0; i < ASD_MAX_PHYS; i++) {
  887. memcpy(dflt_ps.phy_ent[i].sas_addr,
  888. asd_ha->hw_prof.sas_addr, SAS_ADDR_SIZE);
  889. dflt_ps.phy_ent[i].sas_link_rates = 0x98;
  890. dflt_ps.phy_ent[i].flags = 0x0;
  891. dflt_ps.phy_ent[i].sata_link_rates = 0x0;
  892. }
  893. size = sizeof(struct asd_ctrla_phy_settings);
  894. ps = &dflt_ps;
  895. goto out_process;
  896. }
  897. if (size == 0)
  898. goto out;
  899. err = -ENOMEM;
  900. el = kmalloc(size, GFP_KERNEL);
  901. if (!el) {
  902. ASD_DPRINTK("no mem for ctrla user settings section\n");
  903. goto out;
  904. }
  905. err = asd_read_flash_seg(asd_ha, (void *)el, offs, size);
  906. if (err) {
  907. ASD_DPRINTK("couldn't read ctrla phy settings section\n");
  908. goto out2;
  909. }
  910. err = -ENOENT;
  911. ps = asd_find_ll_by_id(el, 'h', 0xFF);
  912. if (!ps) {
  913. ASD_DPRINTK("couldn't find ctrla phy settings struct\n");
  914. goto out2;
  915. }
  916. out_process:
  917. err = asd_process_ctrla_phy_settings(asd_ha, ps);
  918. if (err) {
  919. ASD_DPRINTK("couldn't process ctrla phy settings\n");
  920. goto out2;
  921. }
  922. out2:
  923. kfree(el);
  924. out:
  925. return err;
  926. }
  927. /**
  928. * asd_read_flash - read flash memory
  929. * @asd_ha: pointer to the host adapter structure
  930. */
  931. int asd_read_flash(struct asd_ha_struct *asd_ha)
  932. {
  933. int err;
  934. struct asd_flash_dir *flash_dir;
  935. err = asd_flash_getid(asd_ha);
  936. if (err)
  937. return err;
  938. flash_dir = kmalloc(sizeof(*flash_dir), GFP_KERNEL);
  939. if (!flash_dir)
  940. return -ENOMEM;
  941. err = -ENOENT;
  942. if (!asd_find_flash_dir(asd_ha, flash_dir)) {
  943. ASD_DPRINTK("couldn't find flash directory\n");
  944. goto out;
  945. }
  946. if (le32_to_cpu(flash_dir->rev) != 2) {
  947. asd_printk("unsupported flash dir version:0x%x\n",
  948. le32_to_cpu(flash_dir->rev));
  949. goto out;
  950. }
  951. err = asd_process_ms(asd_ha, flash_dir);
  952. if (err) {
  953. ASD_DPRINTK("couldn't process manuf sector settings\n");
  954. goto out;
  955. }
  956. err = asd_process_ctrl_a_user(asd_ha, flash_dir);
  957. if (err) {
  958. ASD_DPRINTK("couldn't process CTRL-A user settings\n");
  959. goto out;
  960. }
  961. out:
  962. kfree(flash_dir);
  963. return err;
  964. }
  965. /**
  966. * asd_verify_flash_seg - verify data with flash memory
  967. * @asd_ha: pointer to the host adapter structure
  968. * @src: pointer to the source data to be verified
  969. * @dest_offset: offset from flash memory
  970. * @bytes_to_verify: total bytes to verify
  971. */
  972. int asd_verify_flash_seg(struct asd_ha_struct *asd_ha,
  973. const void *src, u32 dest_offset, u32 bytes_to_verify)
  974. {
  975. const u8 *src_buf;
  976. u8 flash_char;
  977. int err;
  978. u32 nv_offset, reg, i;
  979. reg = asd_ha->hw_prof.flash.bar;
  980. src_buf = NULL;
  981. err = FLASH_OK;
  982. nv_offset = dest_offset;
  983. src_buf = (const u8 *)src;
  984. for (i = 0; i < bytes_to_verify; i++) {
  985. flash_char = asd_read_reg_byte(asd_ha, reg + nv_offset + i);
  986. if (flash_char != src_buf[i]) {
  987. err = FAIL_VERIFY;
  988. break;
  989. }
  990. }
  991. return err;
  992. }
  993. /**
  994. * asd_write_flash_seg - write data into flash memory
  995. * @asd_ha: pointer to the host adapter structure
  996. * @src: pointer to the source data to be written
  997. * @dest_offset: offset from flash memory
  998. * @bytes_to_write: total bytes to write
  999. */
  1000. int asd_write_flash_seg(struct asd_ha_struct *asd_ha,
  1001. const void *src, u32 dest_offset, u32 bytes_to_write)
  1002. {
  1003. const u8 *src_buf;
  1004. u32 nv_offset, reg, i;
  1005. int err;
  1006. reg = asd_ha->hw_prof.flash.bar;
  1007. src_buf = NULL;
  1008. err = asd_check_flash_type(asd_ha);
  1009. if (err) {
  1010. ASD_DPRINTK("couldn't find the type of flash. err=%d\n", err);
  1011. return err;
  1012. }
  1013. nv_offset = dest_offset;
  1014. err = asd_erase_nv_sector(asd_ha, nv_offset, bytes_to_write);
  1015. if (err) {
  1016. ASD_DPRINTK("Erase failed at offset:0x%x\n",
  1017. nv_offset);
  1018. return err;
  1019. }
  1020. err = asd_reset_flash(asd_ha);
  1021. if (err) {
  1022. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1023. return err;
  1024. }
  1025. src_buf = (const u8 *)src;
  1026. for (i = 0; i < bytes_to_write; i++) {
  1027. /* Setup program command sequence */
  1028. switch (asd_ha->hw_prof.flash.method) {
  1029. case FLASH_METHOD_A:
  1030. {
  1031. asd_write_reg_byte(asd_ha,
  1032. (reg + 0xAAA), 0xAA);
  1033. asd_write_reg_byte(asd_ha,
  1034. (reg + 0x555), 0x55);
  1035. asd_write_reg_byte(asd_ha,
  1036. (reg + 0xAAA), 0xA0);
  1037. asd_write_reg_byte(asd_ha,
  1038. (reg + nv_offset + i),
  1039. (*(src_buf + i)));
  1040. break;
  1041. }
  1042. case FLASH_METHOD_B:
  1043. {
  1044. asd_write_reg_byte(asd_ha,
  1045. (reg + 0x555), 0xAA);
  1046. asd_write_reg_byte(asd_ha,
  1047. (reg + 0x2AA), 0x55);
  1048. asd_write_reg_byte(asd_ha,
  1049. (reg + 0x555), 0xA0);
  1050. asd_write_reg_byte(asd_ha,
  1051. (reg + nv_offset + i),
  1052. (*(src_buf + i)));
  1053. break;
  1054. }
  1055. default:
  1056. break;
  1057. }
  1058. if (asd_chk_write_status(asd_ha,
  1059. (nv_offset + i), 0) != 0) {
  1060. ASD_DPRINTK("aicx: Write failed at offset:0x%x\n",
  1061. reg + nv_offset + i);
  1062. return FAIL_WRITE_FLASH;
  1063. }
  1064. }
  1065. err = asd_reset_flash(asd_ha);
  1066. if (err) {
  1067. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1068. return err;
  1069. }
  1070. return 0;
  1071. }
  1072. int asd_chk_write_status(struct asd_ha_struct *asd_ha,
  1073. u32 sector_addr, u8 erase_flag)
  1074. {
  1075. u32 reg;
  1076. u32 loop_cnt;
  1077. u8 nv_data1, nv_data2;
  1078. u8 toggle_bit1;
  1079. /*
  1080. * Read from DQ2 requires sector address
  1081. * while it's dont care for DQ6
  1082. */
  1083. reg = asd_ha->hw_prof.flash.bar;
  1084. for (loop_cnt = 0; loop_cnt < 50000; loop_cnt++) {
  1085. nv_data1 = asd_read_reg_byte(asd_ha, reg);
  1086. nv_data2 = asd_read_reg_byte(asd_ha, reg);
  1087. toggle_bit1 = ((nv_data1 & FLASH_STATUS_BIT_MASK_DQ6)
  1088. ^ (nv_data2 & FLASH_STATUS_BIT_MASK_DQ6));
  1089. if (toggle_bit1 == 0) {
  1090. return 0;
  1091. } else {
  1092. if (nv_data2 & FLASH_STATUS_BIT_MASK_DQ5) {
  1093. nv_data1 = asd_read_reg_byte(asd_ha,
  1094. reg);
  1095. nv_data2 = asd_read_reg_byte(asd_ha,
  1096. reg);
  1097. toggle_bit1 =
  1098. ((nv_data1 & FLASH_STATUS_BIT_MASK_DQ6)
  1099. ^ (nv_data2 & FLASH_STATUS_BIT_MASK_DQ6));
  1100. if (toggle_bit1 == 0)
  1101. return 0;
  1102. }
  1103. }
  1104. /*
  1105. * ERASE is a sector-by-sector operation and requires
  1106. * more time to finish while WRITE is byte-byte-byte
  1107. * operation and takes lesser time to finish.
  1108. *
  1109. * For some strange reason a reduced ERASE delay gives different
  1110. * behaviour across different spirit boards. Hence we set
  1111. * a optimum balance of 50mus for ERASE which works well
  1112. * across all boards.
  1113. */
  1114. if (erase_flag) {
  1115. udelay(FLASH_STATUS_ERASE_DELAY_COUNT);
  1116. } else {
  1117. udelay(FLASH_STATUS_WRITE_DELAY_COUNT);
  1118. }
  1119. }
  1120. return -1;
  1121. }
  1122. /**
  1123. * asd_hwi_erase_nv_sector - Erase the flash memory sectors.
  1124. * @asd_ha: pointer to the host adapter structure
  1125. * @flash_addr: pointer to offset from flash memory
  1126. * @size: total bytes to erase.
  1127. */
  1128. int asd_erase_nv_sector(struct asd_ha_struct *asd_ha, u32 flash_addr, u32 size)
  1129. {
  1130. u32 reg;
  1131. u32 sector_addr;
  1132. reg = asd_ha->hw_prof.flash.bar;
  1133. /* sector staring address */
  1134. sector_addr = flash_addr & FLASH_SECTOR_SIZE_MASK;
  1135. /*
  1136. * Erasing an flash sector needs to be done in six consecutive
  1137. * write cyles.
  1138. */
  1139. while (sector_addr < flash_addr+size) {
  1140. switch (asd_ha->hw_prof.flash.method) {
  1141. case FLASH_METHOD_A:
  1142. asd_write_reg_byte(asd_ha, (reg + 0xAAA), 0xAA);
  1143. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x55);
  1144. asd_write_reg_byte(asd_ha, (reg + 0xAAA), 0x80);
  1145. asd_write_reg_byte(asd_ha, (reg + 0xAAA), 0xAA);
  1146. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x55);
  1147. asd_write_reg_byte(asd_ha, (reg + sector_addr), 0x30);
  1148. break;
  1149. case FLASH_METHOD_B:
  1150. asd_write_reg_byte(asd_ha, (reg + 0x555), 0xAA);
  1151. asd_write_reg_byte(asd_ha, (reg + 0x2AA), 0x55);
  1152. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x80);
  1153. asd_write_reg_byte(asd_ha, (reg + 0x555), 0xAA);
  1154. asd_write_reg_byte(asd_ha, (reg + 0x2AA), 0x55);
  1155. asd_write_reg_byte(asd_ha, (reg + sector_addr), 0x30);
  1156. break;
  1157. default:
  1158. break;
  1159. }
  1160. if (asd_chk_write_status(asd_ha, sector_addr, 1) != 0)
  1161. return FAIL_ERASE_FLASH;
  1162. sector_addr += FLASH_SECTOR_SIZE;
  1163. }
  1164. return 0;
  1165. }
  1166. int asd_check_flash_type(struct asd_ha_struct *asd_ha)
  1167. {
  1168. u8 manuf_id;
  1169. u8 dev_id;
  1170. u8 sec_prot;
  1171. u32 inc;
  1172. u32 reg;
  1173. int err;
  1174. /* get Flash memory base address */
  1175. reg = asd_ha->hw_prof.flash.bar;
  1176. /* Determine flash info */
  1177. err = asd_reset_flash(asd_ha);
  1178. if (err) {
  1179. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1180. return err;
  1181. }
  1182. asd_ha->hw_prof.flash.method = FLASH_METHOD_UNKNOWN;
  1183. asd_ha->hw_prof.flash.manuf = FLASH_MANUF_ID_UNKNOWN;
  1184. asd_ha->hw_prof.flash.dev_id = FLASH_DEV_ID_UNKNOWN;
  1185. /* Get flash info. This would most likely be AMD Am29LV family flash.
  1186. * First try the sequence for word mode. It is the same as for
  1187. * 008B (byte mode only), 160B (word mode) and 800D (word mode).
  1188. */
  1189. inc = asd_ha->hw_prof.flash.wide ? 2 : 1;
  1190. asd_write_reg_byte(asd_ha, reg + 0xAAA, 0xAA);
  1191. asd_write_reg_byte(asd_ha, reg + 0x555, 0x55);
  1192. asd_write_reg_byte(asd_ha, reg + 0xAAA, 0x90);
  1193. manuf_id = asd_read_reg_byte(asd_ha, reg);
  1194. dev_id = asd_read_reg_byte(asd_ha, reg + inc);
  1195. sec_prot = asd_read_reg_byte(asd_ha, reg + inc + inc);
  1196. /* Get out of autoselect mode. */
  1197. err = asd_reset_flash(asd_ha);
  1198. if (err) {
  1199. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1200. return err;
  1201. }
  1202. ASD_DPRINTK("Flash MethodA manuf_id(0x%x) dev_id(0x%x) "
  1203. "sec_prot(0x%x)\n", manuf_id, dev_id, sec_prot);
  1204. err = asd_reset_flash(asd_ha);
  1205. if (err != 0)
  1206. return err;
  1207. switch (manuf_id) {
  1208. case FLASH_MANUF_ID_AMD:
  1209. switch (sec_prot) {
  1210. case FLASH_DEV_ID_AM29LV800DT:
  1211. case FLASH_DEV_ID_AM29LV640MT:
  1212. case FLASH_DEV_ID_AM29F800B:
  1213. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1214. break;
  1215. default:
  1216. break;
  1217. }
  1218. break;
  1219. case FLASH_MANUF_ID_ST:
  1220. switch (sec_prot) {
  1221. case FLASH_DEV_ID_STM29W800DT:
  1222. case FLASH_DEV_ID_STM29LV640:
  1223. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1224. break;
  1225. default:
  1226. break;
  1227. }
  1228. break;
  1229. case FLASH_MANUF_ID_FUJITSU:
  1230. switch (sec_prot) {
  1231. case FLASH_DEV_ID_MBM29LV800TE:
  1232. case FLASH_DEV_ID_MBM29DL800TA:
  1233. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1234. break;
  1235. }
  1236. break;
  1237. case FLASH_MANUF_ID_MACRONIX:
  1238. switch (sec_prot) {
  1239. case FLASH_DEV_ID_MX29LV800BT:
  1240. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1241. break;
  1242. }
  1243. break;
  1244. }
  1245. if (asd_ha->hw_prof.flash.method == FLASH_METHOD_UNKNOWN) {
  1246. err = asd_reset_flash(asd_ha);
  1247. if (err) {
  1248. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1249. return err;
  1250. }
  1251. /* Issue Unlock sequence for AM29LV008BT */
  1252. asd_write_reg_byte(asd_ha, (reg + 0x555), 0xAA);
  1253. asd_write_reg_byte(asd_ha, (reg + 0x2AA), 0x55);
  1254. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x90);
  1255. manuf_id = asd_read_reg_byte(asd_ha, reg);
  1256. dev_id = asd_read_reg_byte(asd_ha, reg + inc);
  1257. sec_prot = asd_read_reg_byte(asd_ha, reg + inc + inc);
  1258. ASD_DPRINTK("Flash MethodB manuf_id(0x%x) dev_id(0x%x) sec_prot"
  1259. "(0x%x)\n", manuf_id, dev_id, sec_prot);
  1260. err = asd_reset_flash(asd_ha);
  1261. if (err != 0) {
  1262. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1263. return err;
  1264. }
  1265. switch (manuf_id) {
  1266. case FLASH_MANUF_ID_AMD:
  1267. switch (dev_id) {
  1268. case FLASH_DEV_ID_AM29LV008BT:
  1269. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1270. break;
  1271. default:
  1272. break;
  1273. }
  1274. break;
  1275. case FLASH_MANUF_ID_ST:
  1276. switch (dev_id) {
  1277. case FLASH_DEV_ID_STM29008:
  1278. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1279. break;
  1280. default:
  1281. break;
  1282. }
  1283. break;
  1284. case FLASH_MANUF_ID_FUJITSU:
  1285. switch (dev_id) {
  1286. case FLASH_DEV_ID_MBM29LV008TA:
  1287. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1288. break;
  1289. }
  1290. break;
  1291. case FLASH_MANUF_ID_INTEL:
  1292. switch (dev_id) {
  1293. case FLASH_DEV_ID_I28LV00TAT:
  1294. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1295. break;
  1296. }
  1297. break;
  1298. case FLASH_MANUF_ID_MACRONIX:
  1299. switch (dev_id) {
  1300. case FLASH_DEV_ID_I28LV00TAT:
  1301. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1302. break;
  1303. }
  1304. break;
  1305. default:
  1306. return FAIL_FIND_FLASH_ID;
  1307. }
  1308. }
  1309. if (asd_ha->hw_prof.flash.method == FLASH_METHOD_UNKNOWN)
  1310. return FAIL_FIND_FLASH_ID;
  1311. asd_ha->hw_prof.flash.manuf = manuf_id;
  1312. asd_ha->hw_prof.flash.dev_id = dev_id;
  1313. asd_ha->hw_prof.flash.sec_prot = sec_prot;
  1314. return 0;
  1315. }