aic94xx_sds.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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/delay.h>
  29. #include "aic94xx.h"
  30. #include "aic94xx_reg.h"
  31. /* ---------- OCM stuff ---------- */
  32. struct asd_ocm_dir_ent {
  33. u8 type;
  34. u8 offs[3];
  35. u8 _r1;
  36. u8 size[3];
  37. } __attribute__ ((packed));
  38. struct asd_ocm_dir {
  39. char sig[2];
  40. u8 _r1[2];
  41. u8 major; /* 0 */
  42. u8 minor; /* 0 */
  43. u8 _r2;
  44. u8 num_de;
  45. struct asd_ocm_dir_ent entry[15];
  46. } __attribute__ ((packed));
  47. #define OCM_DE_OCM_DIR 0x00
  48. #define OCM_DE_WIN_DRVR 0x01
  49. #define OCM_DE_BIOS_CHIM 0x02
  50. #define OCM_DE_RAID_ENGN 0x03
  51. #define OCM_DE_BIOS_INTL 0x04
  52. #define OCM_DE_BIOS_CHIM_OSM 0x05
  53. #define OCM_DE_BIOS_CHIM_DYNAMIC 0x06
  54. #define OCM_DE_ADDC2C_RES0 0x07
  55. #define OCM_DE_ADDC2C_RES1 0x08
  56. #define OCM_DE_ADDC2C_RES2 0x09
  57. #define OCM_DE_ADDC2C_RES3 0x0A
  58. #define OCM_INIT_DIR_ENTRIES 5
  59. /***************************************************************************
  60. * OCM dircetory default
  61. ***************************************************************************/
  62. static struct asd_ocm_dir OCMDirInit =
  63. {
  64. .sig = {0x4D, 0x4F}, /* signature */
  65. .num_de = OCM_INIT_DIR_ENTRIES, /* no. of directory entries */
  66. };
  67. /***************************************************************************
  68. * OCM dircetory Entries default
  69. ***************************************************************************/
  70. static struct asd_ocm_dir_ent OCMDirEntriesInit[OCM_INIT_DIR_ENTRIES] =
  71. {
  72. {
  73. .type = (OCM_DE_ADDC2C_RES0), /* Entry type */
  74. .offs = {128}, /* Offset */
  75. .size = {0, 4}, /* size */
  76. },
  77. {
  78. .type = (OCM_DE_ADDC2C_RES1), /* Entry type */
  79. .offs = {128, 4}, /* Offset */
  80. .size = {0, 4}, /* size */
  81. },
  82. {
  83. .type = (OCM_DE_ADDC2C_RES2), /* Entry type */
  84. .offs = {128, 8}, /* Offset */
  85. .size = {0, 4}, /* size */
  86. },
  87. {
  88. .type = (OCM_DE_ADDC2C_RES3), /* Entry type */
  89. .offs = {128, 12}, /* Offset */
  90. .size = {0, 4}, /* size */
  91. },
  92. {
  93. .type = (OCM_DE_WIN_DRVR), /* Entry type */
  94. .offs = {128, 16}, /* Offset */
  95. .size = {128, 235, 1}, /* size */
  96. },
  97. };
  98. struct asd_bios_chim_struct {
  99. char sig[4];
  100. u8 major; /* 1 */
  101. u8 minor; /* 0 */
  102. u8 bios_major;
  103. u8 bios_minor;
  104. __le32 bios_build;
  105. u8 flags;
  106. u8 pci_slot;
  107. __le16 ue_num;
  108. __le16 ue_size;
  109. u8 _r[14];
  110. /* The unit element array is right here.
  111. */
  112. } __attribute__ ((packed));
  113. /**
  114. * asd_read_ocm_seg - read an on chip memory (OCM) segment
  115. * @asd_ha: pointer to the host adapter structure
  116. * @buffer: where to write the read data
  117. * @offs: offset into OCM where to read from
  118. * @size: how many bytes to read
  119. *
  120. * Return the number of bytes not read. Return 0 on success.
  121. */
  122. static int asd_read_ocm_seg(struct asd_ha_struct *asd_ha, void *buffer,
  123. u32 offs, int size)
  124. {
  125. u8 *p = buffer;
  126. if (unlikely(asd_ha->iospace))
  127. asd_read_reg_string(asd_ha, buffer, offs+OCM_BASE_ADDR, size);
  128. else {
  129. for ( ; size > 0; size--, offs++, p++)
  130. *p = asd_read_ocm_byte(asd_ha, offs);
  131. }
  132. return size;
  133. }
  134. static int asd_read_ocm_dir(struct asd_ha_struct *asd_ha,
  135. struct asd_ocm_dir *dir, u32 offs)
  136. {
  137. int err = asd_read_ocm_seg(asd_ha, dir, offs, sizeof(*dir));
  138. if (err) {
  139. ASD_DPRINTK("couldn't read ocm segment\n");
  140. return err;
  141. }
  142. if (dir->sig[0] != 'M' || dir->sig[1] != 'O') {
  143. ASD_DPRINTK("no valid dir signature(%c%c) at start of OCM\n",
  144. dir->sig[0], dir->sig[1]);
  145. return -ENOENT;
  146. }
  147. if (dir->major != 0) {
  148. asd_printk("unsupported major version of ocm dir:0x%x\n",
  149. dir->major);
  150. return -ENOENT;
  151. }
  152. dir->num_de &= 0xf;
  153. return 0;
  154. }
  155. /**
  156. * asd_write_ocm_seg - write an on chip memory (OCM) segment
  157. * @asd_ha: pointer to the host adapter structure
  158. * @buffer: where to read the write data
  159. * @offs: offset into OCM to write to
  160. * @size: how many bytes to write
  161. *
  162. * Return the number of bytes not written. Return 0 on success.
  163. */
  164. static void asd_write_ocm_seg(struct asd_ha_struct *asd_ha, void *buffer,
  165. u32 offs, int size)
  166. {
  167. u8 *p = buffer;
  168. if (unlikely(asd_ha->iospace))
  169. asd_write_reg_string(asd_ha, buffer, offs+OCM_BASE_ADDR, size);
  170. else {
  171. for ( ; size > 0; size--, offs++, p++)
  172. asd_write_ocm_byte(asd_ha, offs, *p);
  173. }
  174. return;
  175. }
  176. #define THREE_TO_NUM(X) ((X)[0] | ((X)[1] << 8) | ((X)[2] << 16))
  177. static int asd_find_dir_entry(struct asd_ocm_dir *dir, u8 type,
  178. u32 *offs, u32 *size)
  179. {
  180. int i;
  181. struct asd_ocm_dir_ent *ent;
  182. for (i = 0; i < dir->num_de; i++) {
  183. if (dir->entry[i].type == type)
  184. break;
  185. }
  186. if (i >= dir->num_de)
  187. return -ENOENT;
  188. ent = &dir->entry[i];
  189. *offs = (u32) THREE_TO_NUM(ent->offs);
  190. *size = (u32) THREE_TO_NUM(ent->size);
  191. return 0;
  192. }
  193. #define OCM_BIOS_CHIM_DE 2
  194. #define BC_BIOS_PRESENT 1
  195. static int asd_get_bios_chim(struct asd_ha_struct *asd_ha,
  196. struct asd_ocm_dir *dir)
  197. {
  198. int err;
  199. struct asd_bios_chim_struct *bc_struct;
  200. u32 offs, size;
  201. err = asd_find_dir_entry(dir, OCM_BIOS_CHIM_DE, &offs, &size);
  202. if (err) {
  203. ASD_DPRINTK("couldn't find BIOS_CHIM dir ent\n");
  204. goto out;
  205. }
  206. err = -ENOMEM;
  207. bc_struct = kmalloc(sizeof(*bc_struct), GFP_KERNEL);
  208. if (!bc_struct) {
  209. asd_printk("no memory for bios_chim struct\n");
  210. goto out;
  211. }
  212. err = asd_read_ocm_seg(asd_ha, (void *)bc_struct, offs,
  213. sizeof(*bc_struct));
  214. if (err) {
  215. ASD_DPRINTK("couldn't read ocm segment\n");
  216. goto out2;
  217. }
  218. if (strncmp(bc_struct->sig, "SOIB", 4)
  219. && strncmp(bc_struct->sig, "IPSA", 4)) {
  220. ASD_DPRINTK("BIOS_CHIM entry has no valid sig(%c%c%c%c)\n",
  221. bc_struct->sig[0], bc_struct->sig[1],
  222. bc_struct->sig[2], bc_struct->sig[3]);
  223. err = -ENOENT;
  224. goto out2;
  225. }
  226. if (bc_struct->major != 1) {
  227. asd_printk("BIOS_CHIM unsupported major version:0x%x\n",
  228. bc_struct->major);
  229. err = -ENOENT;
  230. goto out2;
  231. }
  232. if (bc_struct->flags & BC_BIOS_PRESENT) {
  233. asd_ha->hw_prof.bios.present = 1;
  234. asd_ha->hw_prof.bios.maj = bc_struct->bios_major;
  235. asd_ha->hw_prof.bios.min = bc_struct->bios_minor;
  236. asd_ha->hw_prof.bios.bld = le32_to_cpu(bc_struct->bios_build);
  237. ASD_DPRINTK("BIOS present (%d,%d), %d\n",
  238. asd_ha->hw_prof.bios.maj,
  239. asd_ha->hw_prof.bios.min,
  240. asd_ha->hw_prof.bios.bld);
  241. }
  242. asd_ha->hw_prof.ue.num = le16_to_cpu(bc_struct->ue_num);
  243. asd_ha->hw_prof.ue.size= le16_to_cpu(bc_struct->ue_size);
  244. ASD_DPRINTK("ue num:%d, ue size:%d\n", asd_ha->hw_prof.ue.num,
  245. asd_ha->hw_prof.ue.size);
  246. size = asd_ha->hw_prof.ue.num * asd_ha->hw_prof.ue.size;
  247. if (size > 0) {
  248. err = -ENOMEM;
  249. asd_ha->hw_prof.ue.area = kmalloc(size, GFP_KERNEL);
  250. if (!asd_ha->hw_prof.ue.area)
  251. goto out2;
  252. err = asd_read_ocm_seg(asd_ha, (void *)asd_ha->hw_prof.ue.area,
  253. offs + sizeof(*bc_struct), size);
  254. if (err) {
  255. kfree(asd_ha->hw_prof.ue.area);
  256. asd_ha->hw_prof.ue.area = NULL;
  257. asd_ha->hw_prof.ue.num = 0;
  258. asd_ha->hw_prof.ue.size = 0;
  259. ASD_DPRINTK("couldn't read ue entries(%d)\n", err);
  260. }
  261. }
  262. out2:
  263. kfree(bc_struct);
  264. out:
  265. return err;
  266. }
  267. static void
  268. asd_hwi_initialize_ocm_dir (struct asd_ha_struct *asd_ha)
  269. {
  270. int i;
  271. /* Zero OCM */
  272. for (i = 0; i < OCM_MAX_SIZE; i += 4)
  273. asd_write_ocm_dword(asd_ha, i, 0);
  274. /* Write Dir */
  275. asd_write_ocm_seg(asd_ha, &OCMDirInit, 0,
  276. sizeof(struct asd_ocm_dir));
  277. /* Write Dir Entries */
  278. for (i = 0; i < OCM_INIT_DIR_ENTRIES; i++)
  279. asd_write_ocm_seg(asd_ha, &OCMDirEntriesInit[i],
  280. sizeof(struct asd_ocm_dir) +
  281. (i * sizeof(struct asd_ocm_dir_ent))
  282. , sizeof(struct asd_ocm_dir_ent));
  283. }
  284. static int
  285. asd_hwi_check_ocm_access (struct asd_ha_struct *asd_ha)
  286. {
  287. struct pci_dev *pcidev = asd_ha->pcidev;
  288. u32 reg;
  289. int err = 0;
  290. u32 v;
  291. /* check if OCM has been initialized by BIOS */
  292. reg = asd_read_reg_dword(asd_ha, EXSICNFGR);
  293. if (!(reg & OCMINITIALIZED)) {
  294. err = pci_read_config_dword(pcidev, PCIC_INTRPT_STAT, &v);
  295. if (err) {
  296. asd_printk("couldn't access PCIC_INTRPT_STAT of %s\n",
  297. pci_name(pcidev));
  298. goto out;
  299. }
  300. printk(KERN_INFO "OCM is not initialized by BIOS,"
  301. "reinitialize it and ignore it, current IntrptStatus"
  302. "is 0x%x\n", v);
  303. if (v)
  304. err = pci_write_config_dword(pcidev,
  305. PCIC_INTRPT_STAT, v);
  306. if (err) {
  307. asd_printk("couldn't write PCIC_INTRPT_STAT of %s\n",
  308. pci_name(pcidev));
  309. goto out;
  310. }
  311. asd_hwi_initialize_ocm_dir(asd_ha);
  312. }
  313. out:
  314. return err;
  315. }
  316. /**
  317. * asd_read_ocm - read on chip memory (OCM)
  318. * @asd_ha: pointer to the host adapter structure
  319. */
  320. int asd_read_ocm(struct asd_ha_struct *asd_ha)
  321. {
  322. int err;
  323. struct asd_ocm_dir *dir;
  324. if (asd_hwi_check_ocm_access(asd_ha))
  325. return -1;
  326. dir = kmalloc(sizeof(*dir), GFP_KERNEL);
  327. if (!dir) {
  328. asd_printk("no memory for ocm dir\n");
  329. return -ENOMEM;
  330. }
  331. err = asd_read_ocm_dir(asd_ha, dir, 0);
  332. if (err)
  333. goto out;
  334. err = asd_get_bios_chim(asd_ha, dir);
  335. out:
  336. kfree(dir);
  337. return err;
  338. }
  339. /* ---------- FLASH stuff ---------- */
  340. #define FLASH_RESET 0xF0
  341. #define FLASH_MANUF_AMD 1
  342. #define FLASH_SIZE 0x200000
  343. #define FLASH_DIR_COOKIE "*** ADAPTEC FLASH DIRECTORY *** "
  344. #define FLASH_NEXT_ENTRY_OFFS 0x2000
  345. #define FLASH_MAX_DIR_ENTRIES 32
  346. #define FLASH_DE_TYPE_MASK 0x3FFFFFFF
  347. #define FLASH_DE_MS 0x120
  348. #define FLASH_DE_CTRL_A_USER 0xE0
  349. struct asd_flash_de {
  350. __le32 type;
  351. __le32 offs;
  352. __le32 pad_size;
  353. __le32 image_size;
  354. __le32 chksum;
  355. u8 _r[12];
  356. u8 version[32];
  357. } __attribute__ ((packed));
  358. struct asd_flash_dir {
  359. u8 cookie[32];
  360. __le32 rev; /* 2 */
  361. __le32 chksum;
  362. __le32 chksum_antidote;
  363. __le32 bld;
  364. u8 bld_id[32]; /* build id data */
  365. u8 ver_data[32]; /* date and time of build */
  366. __le32 ae_mask;
  367. __le32 v_mask;
  368. __le32 oc_mask;
  369. u8 _r[20];
  370. struct asd_flash_de dir_entry[FLASH_MAX_DIR_ENTRIES];
  371. } __attribute__ ((packed));
  372. struct asd_manuf_sec {
  373. char sig[2]; /* 'S', 'M' */
  374. u16 offs_next;
  375. u8 maj; /* 0 */
  376. u8 min; /* 0 */
  377. u16 chksum;
  378. u16 size;
  379. u8 _r[6];
  380. u8 sas_addr[SAS_ADDR_SIZE];
  381. u8 pcba_sn[ASD_PCBA_SN_SIZE];
  382. /* Here start the other segments */
  383. u8 linked_list[0];
  384. } __attribute__ ((packed));
  385. struct asd_manuf_phy_desc {
  386. u8 state; /* low 4 bits */
  387. #define MS_PHY_STATE_ENABLEABLE 0
  388. #define MS_PHY_STATE_REPORTED 1
  389. #define MS_PHY_STATE_HIDDEN 2
  390. u8 phy_id;
  391. u16 _r;
  392. u8 phy_control_0; /* mode 5 reg 0x160 */
  393. u8 phy_control_1; /* mode 5 reg 0x161 */
  394. u8 phy_control_2; /* mode 5 reg 0x162 */
  395. u8 phy_control_3; /* mode 5 reg 0x163 */
  396. } __attribute__ ((packed));
  397. struct asd_manuf_phy_param {
  398. char sig[2]; /* 'P', 'M' */
  399. u16 next;
  400. u8 maj; /* 0 */
  401. u8 min; /* 2 */
  402. u8 num_phy_desc; /* 8 */
  403. u8 phy_desc_size; /* 8 */
  404. u8 _r[3];
  405. u8 usage_model_id;
  406. u32 _r2;
  407. struct asd_manuf_phy_desc phy_desc[ASD_MAX_PHYS];
  408. } __attribute__ ((packed));
  409. #if 0
  410. static const char *asd_sb_type[] = {
  411. "unknown",
  412. "SGPIO",
  413. [2 ... 0x7F] = "unknown",
  414. [0x80] = "ADPT_I2C",
  415. [0x81 ... 0xFF] = "VENDOR_UNIQUExx"
  416. };
  417. #endif
  418. struct asd_ms_sb_desc {
  419. u8 type;
  420. u8 node_desc_index;
  421. u8 conn_desc_index;
  422. u8 _recvd[0];
  423. } __attribute__ ((packed));
  424. #if 0
  425. static const char *asd_conn_type[] = {
  426. [0 ... 7] = "unknown",
  427. "SFF8470",
  428. "SFF8482",
  429. "SFF8484",
  430. [0x80] = "PCIX_DAUGHTER0",
  431. [0x81] = "SAS_DAUGHTER0",
  432. [0x82 ... 0xFF] = "VENDOR_UNIQUExx"
  433. };
  434. static const char *asd_conn_location[] = {
  435. "unknown",
  436. "internal",
  437. "external",
  438. "board_to_board",
  439. };
  440. #endif
  441. struct asd_ms_conn_desc {
  442. u8 type;
  443. u8 location;
  444. u8 num_sideband_desc;
  445. u8 size_sideband_desc;
  446. u32 _resvd;
  447. u8 name[16];
  448. struct asd_ms_sb_desc sb_desc[0];
  449. } __attribute__ ((packed));
  450. struct asd_nd_phy_desc {
  451. u8 vp_attch_type;
  452. u8 attch_specific[0];
  453. } __attribute__ ((packed));
  454. #if 0
  455. static const char *asd_node_type[] = {
  456. "IOP",
  457. "IO_CONTROLLER",
  458. "EXPANDER",
  459. "PORT_MULTIPLIER",
  460. "PORT_MULTIPLEXER",
  461. "MULTI_DROP_I2C_BUS",
  462. };
  463. #endif
  464. struct asd_ms_node_desc {
  465. u8 type;
  466. u8 num_phy_desc;
  467. u8 size_phy_desc;
  468. u8 _resvd;
  469. u8 name[16];
  470. struct asd_nd_phy_desc phy_desc[0];
  471. } __attribute__ ((packed));
  472. struct asd_ms_conn_map {
  473. char sig[2]; /* 'M', 'C' */
  474. __le16 next;
  475. u8 maj; /* 0 */
  476. u8 min; /* 0 */
  477. __le16 cm_size; /* size of this struct */
  478. u8 num_conn;
  479. u8 conn_size;
  480. u8 num_nodes;
  481. u8 usage_model_id;
  482. u32 _resvd;
  483. struct asd_ms_conn_desc conn_desc[0];
  484. struct asd_ms_node_desc node_desc[0];
  485. } __attribute__ ((packed));
  486. struct asd_ctrla_phy_entry {
  487. u8 sas_addr[SAS_ADDR_SIZE];
  488. u8 sas_link_rates; /* max in hi bits, min in low bits */
  489. u8 flags;
  490. u8 sata_link_rates;
  491. u8 _r[5];
  492. } __attribute__ ((packed));
  493. struct asd_ctrla_phy_settings {
  494. u8 id0; /* P'h'y */
  495. u8 _r;
  496. u16 next;
  497. u8 num_phys; /* number of PHYs in the PCI function */
  498. u8 _r2[3];
  499. struct asd_ctrla_phy_entry phy_ent[ASD_MAX_PHYS];
  500. } __attribute__ ((packed));
  501. struct asd_ll_el {
  502. u8 id0;
  503. u8 id1;
  504. __le16 next;
  505. u8 something_here[0];
  506. } __attribute__ ((packed));
  507. static int asd_poll_flash(struct asd_ha_struct *asd_ha)
  508. {
  509. int c;
  510. u8 d;
  511. for (c = 5000; c > 0; c--) {
  512. d = asd_read_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar);
  513. d ^= asd_read_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar);
  514. if (!d)
  515. return 0;
  516. udelay(5);
  517. }
  518. return -ENOENT;
  519. }
  520. static int asd_reset_flash(struct asd_ha_struct *asd_ha)
  521. {
  522. int err;
  523. err = asd_poll_flash(asd_ha);
  524. if (err)
  525. return err;
  526. asd_write_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar, FLASH_RESET);
  527. err = asd_poll_flash(asd_ha);
  528. return err;
  529. }
  530. static inline int asd_read_flash_seg(struct asd_ha_struct *asd_ha,
  531. void *buffer, u32 offs, int size)
  532. {
  533. asd_read_reg_string(asd_ha, buffer, asd_ha->hw_prof.flash.bar+offs,
  534. size);
  535. return 0;
  536. }
  537. /**
  538. * asd_find_flash_dir - finds and reads the flash directory
  539. * @asd_ha: pointer to the host adapter structure
  540. * @flash_dir: pointer to flash directory structure
  541. *
  542. * If found, the flash directory segment will be copied to
  543. * @flash_dir. Return 1 if found, 0 if not.
  544. */
  545. static int asd_find_flash_dir(struct asd_ha_struct *asd_ha,
  546. struct asd_flash_dir *flash_dir)
  547. {
  548. u32 v;
  549. for (v = 0; v < FLASH_SIZE; v += FLASH_NEXT_ENTRY_OFFS) {
  550. asd_read_flash_seg(asd_ha, flash_dir, v,
  551. sizeof(FLASH_DIR_COOKIE)-1);
  552. if (memcmp(flash_dir->cookie, FLASH_DIR_COOKIE,
  553. sizeof(FLASH_DIR_COOKIE)-1) == 0) {
  554. asd_ha->hw_prof.flash.dir_offs = v;
  555. asd_read_flash_seg(asd_ha, flash_dir, v,
  556. sizeof(*flash_dir));
  557. return 1;
  558. }
  559. }
  560. return 0;
  561. }
  562. static int asd_flash_getid(struct asd_ha_struct *asd_ha)
  563. {
  564. int err = 0;
  565. u32 reg, inc;
  566. reg = asd_read_reg_dword(asd_ha, EXSICNFGR);
  567. if (!(reg & FLASHEX)) {
  568. ASD_DPRINTK("flash doesn't exist\n");
  569. return -ENOENT;
  570. }
  571. if (pci_read_config_dword(asd_ha->pcidev, PCI_CONF_FLSH_BAR,
  572. &asd_ha->hw_prof.flash.bar)) {
  573. asd_printk("couldn't read PCI_CONF_FLSH_BAR of %s\n",
  574. pci_name(asd_ha->pcidev));
  575. return -ENOENT;
  576. }
  577. asd_ha->hw_prof.flash.present = 1;
  578. asd_ha->hw_prof.flash.wide = reg & FLASHW ? 1 : 0;
  579. err = asd_reset_flash(asd_ha);
  580. if (err) {
  581. ASD_DPRINTK("couldn't reset flash(%d)\n", err);
  582. return err;
  583. }
  584. /* Get flash info. This would most likely be AMD Am29LV family flash.
  585. * First try the sequence for word mode. It is the same as for
  586. * 008B (byte mode only), 160B (word mode) and 800D (word mode).
  587. */
  588. reg = asd_ha->hw_prof.flash.bar;
  589. inc = asd_ha->hw_prof.flash.wide ? 2 : 1;
  590. asd_write_reg_byte(asd_ha, reg + 0x555, 0xAA);
  591. asd_write_reg_byte(asd_ha, reg + 0x2AA, 0x55);
  592. asd_write_reg_byte(asd_ha, reg + 0x555, 0x90);
  593. asd_ha->hw_prof.flash.manuf = asd_read_reg_byte(asd_ha, reg);
  594. asd_ha->hw_prof.flash.dev_id= asd_read_reg_byte(asd_ha,reg+inc);
  595. asd_ha->hw_prof.flash.sec_prot = asd_read_reg_byte(asd_ha,reg+inc+inc);
  596. /* Get out of autoselect mode. */
  597. err = asd_reset_flash(asd_ha);
  598. if (asd_ha->hw_prof.flash.manuf == FLASH_MANUF_AMD) {
  599. ASD_DPRINTK("0Found FLASH(%d) manuf:%d, dev_id:0x%x, "
  600. "sec_prot:%d\n",
  601. asd_ha->hw_prof.flash.wide ? 16 : 8,
  602. asd_ha->hw_prof.flash.manuf,
  603. asd_ha->hw_prof.flash.dev_id,
  604. asd_ha->hw_prof.flash.sec_prot);
  605. return 0;
  606. }
  607. /* Ok, try the sequence for byte mode of 160B and 800D.
  608. * We may actually never need this.
  609. */
  610. asd_write_reg_byte(asd_ha, reg + 0xAAA, 0xAA);
  611. asd_write_reg_byte(asd_ha, reg + 0x555, 0x55);
  612. asd_write_reg_byte(asd_ha, reg + 0xAAA, 0x90);
  613. asd_ha->hw_prof.flash.manuf = asd_read_reg_byte(asd_ha, reg);
  614. asd_ha->hw_prof.flash.dev_id = asd_read_reg_byte(asd_ha, reg + 2);
  615. asd_ha->hw_prof.flash.sec_prot = asd_read_reg_byte(asd_ha, reg + 4);
  616. err = asd_reset_flash(asd_ha);
  617. if (asd_ha->hw_prof.flash.manuf == FLASH_MANUF_AMD) {
  618. ASD_DPRINTK("1Found FLASH(%d) manuf:%d, dev_id:0x%x, "
  619. "sec_prot:%d\n",
  620. asd_ha->hw_prof.flash.wide ? 16 : 8,
  621. asd_ha->hw_prof.flash.manuf,
  622. asd_ha->hw_prof.flash.dev_id,
  623. asd_ha->hw_prof.flash.sec_prot);
  624. return 0;
  625. }
  626. return -ENOENT;
  627. }
  628. static u16 asd_calc_flash_chksum(u16 *p, int size)
  629. {
  630. u16 chksum = 0;
  631. while (size-- > 0)
  632. chksum += *p++;
  633. return chksum;
  634. }
  635. static int asd_find_flash_de(struct asd_flash_dir *flash_dir, u32 entry_type,
  636. u32 *offs, u32 *size)
  637. {
  638. int i;
  639. struct asd_flash_de *de;
  640. for (i = 0; i < FLASH_MAX_DIR_ENTRIES; i++) {
  641. u32 type = le32_to_cpu(flash_dir->dir_entry[i].type);
  642. type &= FLASH_DE_TYPE_MASK;
  643. if (type == entry_type)
  644. break;
  645. }
  646. if (i >= FLASH_MAX_DIR_ENTRIES)
  647. return -ENOENT;
  648. de = &flash_dir->dir_entry[i];
  649. *offs = le32_to_cpu(de->offs);
  650. *size = le32_to_cpu(de->pad_size);
  651. return 0;
  652. }
  653. static int asd_validate_ms(struct asd_manuf_sec *ms)
  654. {
  655. if (ms->sig[0] != 'S' || ms->sig[1] != 'M') {
  656. ASD_DPRINTK("manuf sec: no valid sig(%c%c)\n",
  657. ms->sig[0], ms->sig[1]);
  658. return -ENOENT;
  659. }
  660. if (ms->maj != 0) {
  661. asd_printk("unsupported manuf. sector. major version:%x\n",
  662. ms->maj);
  663. return -ENOENT;
  664. }
  665. ms->offs_next = le16_to_cpu((__force __le16) ms->offs_next);
  666. ms->chksum = le16_to_cpu((__force __le16) ms->chksum);
  667. ms->size = le16_to_cpu((__force __le16) ms->size);
  668. if (asd_calc_flash_chksum((u16 *)ms, ms->size/2)) {
  669. asd_printk("failed manuf sector checksum\n");
  670. }
  671. return 0;
  672. }
  673. static int asd_ms_get_sas_addr(struct asd_ha_struct *asd_ha,
  674. struct asd_manuf_sec *ms)
  675. {
  676. memcpy(asd_ha->hw_prof.sas_addr, ms->sas_addr, SAS_ADDR_SIZE);
  677. return 0;
  678. }
  679. static int asd_ms_get_pcba_sn(struct asd_ha_struct *asd_ha,
  680. struct asd_manuf_sec *ms)
  681. {
  682. memcpy(asd_ha->hw_prof.pcba_sn, ms->pcba_sn, ASD_PCBA_SN_SIZE);
  683. asd_ha->hw_prof.pcba_sn[ASD_PCBA_SN_SIZE] = '\0';
  684. return 0;
  685. }
  686. /**
  687. * asd_find_ll_by_id - find a linked list entry by its id
  688. * @start: void pointer to the first element in the linked list
  689. * @id0: the first byte of the id (offs 0)
  690. * @id1: the second byte of the id (offs 1)
  691. *
  692. * @start has to be the _base_ element start, since the
  693. * linked list entries's offset is from this pointer.
  694. * Some linked list entries use only the first id, in which case
  695. * you can pass 0xFF for the second.
  696. */
  697. static void *asd_find_ll_by_id(void * const start, const u8 id0, const u8 id1)
  698. {
  699. struct asd_ll_el *el = start;
  700. do {
  701. switch (id1) {
  702. default:
  703. if (el->id1 == id1)
  704. case 0xFF:
  705. if (el->id0 == id0)
  706. return el;
  707. }
  708. el = start + le16_to_cpu(el->next);
  709. } while (el != start);
  710. return NULL;
  711. }
  712. /**
  713. * asd_ms_get_phy_params - get phy parameters from the manufacturing sector
  714. * @asd_ha: pointer to the host adapter structure
  715. * @manuf_sec: pointer to the manufacturing sector
  716. *
  717. * The manufacturing sector contans also the linked list of sub-segments,
  718. * since when it was read, its size was taken from the flash directory,
  719. * not from the structure size.
  720. *
  721. * HIDDEN phys do not count in the total count. REPORTED phys cannot
  722. * be enabled but are reported and counted towards the total.
  723. * ENEBLEABLE phys are enabled by default and count towards the total.
  724. * The absolute total phy number is ASD_MAX_PHYS. hw_prof->num_phys
  725. * merely specifies the number of phys the host adapter decided to
  726. * report. E.g., it is possible for phys 0, 1 and 2 to be HIDDEN,
  727. * phys 3, 4 and 5 to be REPORTED and phys 6 and 7 to be ENEBLEABLE.
  728. * In this case ASD_MAX_PHYS is 8, hw_prof->num_phys is 5, and only 2
  729. * are actually enabled (enabled by default, max number of phys
  730. * enableable in this case).
  731. */
  732. static int asd_ms_get_phy_params(struct asd_ha_struct *asd_ha,
  733. struct asd_manuf_sec *manuf_sec)
  734. {
  735. int i;
  736. int en_phys = 0;
  737. int rep_phys = 0;
  738. struct asd_manuf_phy_param *phy_param;
  739. struct asd_manuf_phy_param dflt_phy_param;
  740. phy_param = asd_find_ll_by_id(manuf_sec, 'P', 'M');
  741. if (!phy_param) {
  742. ASD_DPRINTK("ms: no phy parameters found\n");
  743. ASD_DPRINTK("ms: Creating default phy parameters\n");
  744. dflt_phy_param.sig[0] = 'P';
  745. dflt_phy_param.sig[1] = 'M';
  746. dflt_phy_param.maj = 0;
  747. dflt_phy_param.min = 2;
  748. dflt_phy_param.num_phy_desc = 8;
  749. dflt_phy_param.phy_desc_size = sizeof(struct asd_manuf_phy_desc);
  750. for (i =0; i < ASD_MAX_PHYS; i++) {
  751. dflt_phy_param.phy_desc[i].state = 0;
  752. dflt_phy_param.phy_desc[i].phy_id = i;
  753. dflt_phy_param.phy_desc[i].phy_control_0 = 0xf6;
  754. dflt_phy_param.phy_desc[i].phy_control_1 = 0x10;
  755. dflt_phy_param.phy_desc[i].phy_control_2 = 0x43;
  756. dflt_phy_param.phy_desc[i].phy_control_3 = 0xeb;
  757. }
  758. phy_param = &dflt_phy_param;
  759. }
  760. if (phy_param->maj != 0) {
  761. asd_printk("unsupported manuf. phy param major version:0x%x\n",
  762. phy_param->maj);
  763. return -ENOENT;
  764. }
  765. ASD_DPRINTK("ms: num_phy_desc: %d\n", phy_param->num_phy_desc);
  766. asd_ha->hw_prof.enabled_phys = 0;
  767. for (i = 0; i < phy_param->num_phy_desc; i++) {
  768. struct asd_manuf_phy_desc *pd = &phy_param->phy_desc[i];
  769. switch (pd->state & 0xF) {
  770. case MS_PHY_STATE_HIDDEN:
  771. ASD_DPRINTK("ms: phy%d: HIDDEN\n", i);
  772. continue;
  773. case MS_PHY_STATE_REPORTED:
  774. ASD_DPRINTK("ms: phy%d: REPORTED\n", i);
  775. asd_ha->hw_prof.enabled_phys &= ~(1 << i);
  776. rep_phys++;
  777. continue;
  778. case MS_PHY_STATE_ENABLEABLE:
  779. ASD_DPRINTK("ms: phy%d: ENEBLEABLE\n", i);
  780. asd_ha->hw_prof.enabled_phys |= (1 << i);
  781. en_phys++;
  782. break;
  783. }
  784. asd_ha->hw_prof.phy_desc[i].phy_control_0 = pd->phy_control_0;
  785. asd_ha->hw_prof.phy_desc[i].phy_control_1 = pd->phy_control_1;
  786. asd_ha->hw_prof.phy_desc[i].phy_control_2 = pd->phy_control_2;
  787. asd_ha->hw_prof.phy_desc[i].phy_control_3 = pd->phy_control_3;
  788. }
  789. asd_ha->hw_prof.max_phys = rep_phys + en_phys;
  790. asd_ha->hw_prof.num_phys = en_phys;
  791. ASD_DPRINTK("ms: max_phys:0x%x, num_phys:0x%x\n",
  792. asd_ha->hw_prof.max_phys, asd_ha->hw_prof.num_phys);
  793. ASD_DPRINTK("ms: enabled_phys:0x%x\n", asd_ha->hw_prof.enabled_phys);
  794. return 0;
  795. }
  796. static int asd_ms_get_connector_map(struct asd_ha_struct *asd_ha,
  797. struct asd_manuf_sec *manuf_sec)
  798. {
  799. struct asd_ms_conn_map *cm;
  800. cm = asd_find_ll_by_id(manuf_sec, 'M', 'C');
  801. if (!cm) {
  802. ASD_DPRINTK("ms: no connector map found\n");
  803. return 0;
  804. }
  805. if (cm->maj != 0) {
  806. ASD_DPRINTK("ms: unsupported: connector map major version 0x%x"
  807. "\n", cm->maj);
  808. return -ENOENT;
  809. }
  810. /* XXX */
  811. return 0;
  812. }
  813. /**
  814. * asd_process_ms - find and extract information from the manufacturing sector
  815. * @asd_ha: pointer to the host adapter structure
  816. * @flash_dir: pointer to the flash directory
  817. */
  818. static int asd_process_ms(struct asd_ha_struct *asd_ha,
  819. struct asd_flash_dir *flash_dir)
  820. {
  821. int err;
  822. struct asd_manuf_sec *manuf_sec;
  823. u32 offs, size;
  824. err = asd_find_flash_de(flash_dir, FLASH_DE_MS, &offs, &size);
  825. if (err) {
  826. ASD_DPRINTK("Couldn't find the manuf. sector\n");
  827. goto out;
  828. }
  829. if (size == 0)
  830. goto out;
  831. err = -ENOMEM;
  832. manuf_sec = kmalloc(size, GFP_KERNEL);
  833. if (!manuf_sec) {
  834. ASD_DPRINTK("no mem for manuf sector\n");
  835. goto out;
  836. }
  837. err = asd_read_flash_seg(asd_ha, (void *)manuf_sec, offs, size);
  838. if (err) {
  839. ASD_DPRINTK("couldn't read manuf sector at 0x%x, size 0x%x\n",
  840. offs, size);
  841. goto out2;
  842. }
  843. err = asd_validate_ms(manuf_sec);
  844. if (err) {
  845. ASD_DPRINTK("couldn't validate manuf sector\n");
  846. goto out2;
  847. }
  848. err = asd_ms_get_sas_addr(asd_ha, manuf_sec);
  849. if (err) {
  850. ASD_DPRINTK("couldn't read the SAS_ADDR\n");
  851. goto out2;
  852. }
  853. ASD_DPRINTK("manuf sect SAS_ADDR %llx\n",
  854. SAS_ADDR(asd_ha->hw_prof.sas_addr));
  855. err = asd_ms_get_pcba_sn(asd_ha, manuf_sec);
  856. if (err) {
  857. ASD_DPRINTK("couldn't read the PCBA SN\n");
  858. goto out2;
  859. }
  860. ASD_DPRINTK("manuf sect PCBA SN %s\n", asd_ha->hw_prof.pcba_sn);
  861. err = asd_ms_get_phy_params(asd_ha, manuf_sec);
  862. if (err) {
  863. ASD_DPRINTK("ms: couldn't get phy parameters\n");
  864. goto out2;
  865. }
  866. err = asd_ms_get_connector_map(asd_ha, manuf_sec);
  867. if (err) {
  868. ASD_DPRINTK("ms: couldn't get connector map\n");
  869. goto out2;
  870. }
  871. out2:
  872. kfree(manuf_sec);
  873. out:
  874. return err;
  875. }
  876. static int asd_process_ctrla_phy_settings(struct asd_ha_struct *asd_ha,
  877. struct asd_ctrla_phy_settings *ps)
  878. {
  879. int i;
  880. for (i = 0; i < ps->num_phys; i++) {
  881. struct asd_ctrla_phy_entry *pe = &ps->phy_ent[i];
  882. if (!PHY_ENABLED(asd_ha, i))
  883. continue;
  884. if (*(u64 *)pe->sas_addr == 0) {
  885. asd_ha->hw_prof.enabled_phys &= ~(1 << i);
  886. continue;
  887. }
  888. /* This is the SAS address which should be sent in IDENTIFY. */
  889. memcpy(asd_ha->hw_prof.phy_desc[i].sas_addr, pe->sas_addr,
  890. SAS_ADDR_SIZE);
  891. asd_ha->hw_prof.phy_desc[i].max_sas_lrate =
  892. (pe->sas_link_rates & 0xF0) >> 4;
  893. asd_ha->hw_prof.phy_desc[i].min_sas_lrate =
  894. (pe->sas_link_rates & 0x0F);
  895. asd_ha->hw_prof.phy_desc[i].max_sata_lrate =
  896. (pe->sata_link_rates & 0xF0) >> 4;
  897. asd_ha->hw_prof.phy_desc[i].min_sata_lrate =
  898. (pe->sata_link_rates & 0x0F);
  899. asd_ha->hw_prof.phy_desc[i].flags = pe->flags;
  900. ASD_DPRINTK("ctrla: phy%d: sas_addr: %llx, sas rate:0x%x-0x%x,"
  901. " sata rate:0x%x-0x%x, flags:0x%x\n",
  902. i,
  903. SAS_ADDR(asd_ha->hw_prof.phy_desc[i].sas_addr),
  904. asd_ha->hw_prof.phy_desc[i].max_sas_lrate,
  905. asd_ha->hw_prof.phy_desc[i].min_sas_lrate,
  906. asd_ha->hw_prof.phy_desc[i].max_sata_lrate,
  907. asd_ha->hw_prof.phy_desc[i].min_sata_lrate,
  908. asd_ha->hw_prof.phy_desc[i].flags);
  909. }
  910. return 0;
  911. }
  912. /**
  913. * asd_process_ctrl_a_user - process CTRL-A user settings
  914. * @asd_ha: pointer to the host adapter structure
  915. * @flash_dir: pointer to the flash directory
  916. */
  917. static int asd_process_ctrl_a_user(struct asd_ha_struct *asd_ha,
  918. struct asd_flash_dir *flash_dir)
  919. {
  920. int err, i;
  921. u32 offs, size;
  922. struct asd_ll_el *el;
  923. struct asd_ctrla_phy_settings *ps;
  924. struct asd_ctrla_phy_settings dflt_ps;
  925. err = asd_find_flash_de(flash_dir, FLASH_DE_CTRL_A_USER, &offs, &size);
  926. if (err) {
  927. ASD_DPRINTK("couldn't find CTRL-A user settings section\n");
  928. ASD_DPRINTK("Creating default CTRL-A user settings section\n");
  929. dflt_ps.id0 = 'h';
  930. dflt_ps.num_phys = 8;
  931. for (i =0; i < ASD_MAX_PHYS; i++) {
  932. memcpy(dflt_ps.phy_ent[i].sas_addr,
  933. asd_ha->hw_prof.sas_addr, SAS_ADDR_SIZE);
  934. dflt_ps.phy_ent[i].sas_link_rates = 0x98;
  935. dflt_ps.phy_ent[i].flags = 0x0;
  936. dflt_ps.phy_ent[i].sata_link_rates = 0x0;
  937. }
  938. size = sizeof(struct asd_ctrla_phy_settings);
  939. ps = &dflt_ps;
  940. }
  941. if (size == 0)
  942. goto out;
  943. err = -ENOMEM;
  944. el = kmalloc(size, GFP_KERNEL);
  945. if (!el) {
  946. ASD_DPRINTK("no mem for ctrla user settings section\n");
  947. goto out;
  948. }
  949. err = asd_read_flash_seg(asd_ha, (void *)el, offs, size);
  950. if (err) {
  951. ASD_DPRINTK("couldn't read ctrla phy settings section\n");
  952. goto out2;
  953. }
  954. err = -ENOENT;
  955. ps = asd_find_ll_by_id(el, 'h', 0xFF);
  956. if (!ps) {
  957. ASD_DPRINTK("couldn't find ctrla phy settings struct\n");
  958. goto out2;
  959. }
  960. err = asd_process_ctrla_phy_settings(asd_ha, ps);
  961. if (err) {
  962. ASD_DPRINTK("couldn't process ctrla phy settings\n");
  963. goto out2;
  964. }
  965. out2:
  966. kfree(el);
  967. out:
  968. return err;
  969. }
  970. /**
  971. * asd_read_flash - read flash memory
  972. * @asd_ha: pointer to the host adapter structure
  973. */
  974. int asd_read_flash(struct asd_ha_struct *asd_ha)
  975. {
  976. int err;
  977. struct asd_flash_dir *flash_dir;
  978. err = asd_flash_getid(asd_ha);
  979. if (err)
  980. return err;
  981. flash_dir = kmalloc(sizeof(*flash_dir), GFP_KERNEL);
  982. if (!flash_dir)
  983. return -ENOMEM;
  984. err = -ENOENT;
  985. if (!asd_find_flash_dir(asd_ha, flash_dir)) {
  986. ASD_DPRINTK("couldn't find flash directory\n");
  987. goto out;
  988. }
  989. if (le32_to_cpu(flash_dir->rev) != 2) {
  990. asd_printk("unsupported flash dir version:0x%x\n",
  991. le32_to_cpu(flash_dir->rev));
  992. goto out;
  993. }
  994. err = asd_process_ms(asd_ha, flash_dir);
  995. if (err) {
  996. ASD_DPRINTK("couldn't process manuf sector settings\n");
  997. goto out;
  998. }
  999. err = asd_process_ctrl_a_user(asd_ha, flash_dir);
  1000. if (err) {
  1001. ASD_DPRINTK("couldn't process CTRL-A user settings\n");
  1002. goto out;
  1003. }
  1004. out:
  1005. kfree(flash_dir);
  1006. return err;
  1007. }