aic94xx_scb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Aic94xx SAS/SATA driver SCB management.
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  9. * This file is part of the aic94xx driver.
  10. *
  11. * The aic94xx driver is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; version 2 of the
  14. * License.
  15. *
  16. * The aic94xx driver is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with the aic94xx driver; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. */
  26. #include <linux/pci.h>
  27. #include "aic94xx.h"
  28. #include "aic94xx_reg.h"
  29. #include "aic94xx_hwi.h"
  30. #include "aic94xx_seq.h"
  31. #include "aic94xx_dump.h"
  32. /* ---------- EMPTY SCB ---------- */
  33. #define DL_PHY_MASK 7
  34. #define BYTES_DMAED 0
  35. #define PRIMITIVE_RECVD 0x08
  36. #define PHY_EVENT 0x10
  37. #define LINK_RESET_ERROR 0x18
  38. #define TIMER_EVENT 0x20
  39. #define REQ_TASK_ABORT 0xF0
  40. #define REQ_DEVICE_RESET 0xF1
  41. #define SIGNAL_NCQ_ERROR 0xF2
  42. #define CLEAR_NCQ_ERROR 0xF3
  43. #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE \
  44. | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
  45. | CURRENT_OOB_ERROR)
  46. static inline void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
  47. {
  48. struct sas_phy *sas_phy = phy->sas_phy.phy;
  49. switch (oob_mode & 7) {
  50. case PHY_SPEED_60:
  51. /* FIXME: sas transport class doesn't have this */
  52. phy->sas_phy.linkrate = SAS_LINK_RATE_6_0_GBPS;
  53. phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
  54. break;
  55. case PHY_SPEED_30:
  56. phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
  57. phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  58. break;
  59. case PHY_SPEED_15:
  60. phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
  61. phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  62. break;
  63. }
  64. sas_phy->negotiated_linkrate = phy->sas_phy.linkrate;
  65. sas_phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  66. sas_phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  67. sas_phy->maximum_linkrate = phy->phy_desc->max_sas_lrate;
  68. sas_phy->minimum_linkrate = phy->phy_desc->min_sas_lrate;
  69. if (oob_mode & SAS_MODE)
  70. phy->sas_phy.oob_mode = SAS_OOB_MODE;
  71. else if (oob_mode & SATA_MODE)
  72. phy->sas_phy.oob_mode = SATA_OOB_MODE;
  73. }
  74. static inline void asd_phy_event_tasklet(struct asd_ascb *ascb,
  75. struct done_list_struct *dl)
  76. {
  77. struct asd_ha_struct *asd_ha = ascb->ha;
  78. struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
  79. int phy_id = dl->status_block[0] & DL_PHY_MASK;
  80. struct asd_phy *phy = &asd_ha->phys[phy_id];
  81. u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
  82. u8 oob_mode = dl->status_block[2];
  83. switch (oob_status) {
  84. case CURRENT_LOSS_OF_SIGNAL:
  85. /* directly attached device was removed */
  86. ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
  87. asd_turn_led(asd_ha, phy_id, 0);
  88. sas_phy_disconnected(&phy->sas_phy);
  89. sas_ha->notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL);
  90. break;
  91. case CURRENT_OOB_DONE:
  92. /* hot plugged device */
  93. asd_turn_led(asd_ha, phy_id, 1);
  94. get_lrate_mode(phy, oob_mode);
  95. ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
  96. phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
  97. sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE);
  98. break;
  99. case CURRENT_SPINUP_HOLD:
  100. /* hot plug SATA, no COMWAKE sent */
  101. asd_turn_led(asd_ha, phy_id, 1);
  102. sas_ha->notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD);
  103. break;
  104. case CURRENT_GTO_TIMEOUT:
  105. case CURRENT_OOB_ERROR:
  106. ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
  107. dl->status_block[1]);
  108. asd_turn_led(asd_ha, phy_id, 0);
  109. sas_phy_disconnected(&phy->sas_phy);
  110. sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR);
  111. break;
  112. }
  113. }
  114. /* If phys are enabled sparsely, this will do the right thing. */
  115. static inline unsigned ord_phy(struct asd_ha_struct *asd_ha,
  116. struct asd_phy *phy)
  117. {
  118. u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
  119. int i, k = 0;
  120. for_each_phy(enabled_mask, enabled_mask, i) {
  121. if (&asd_ha->phys[i] == phy)
  122. return k;
  123. k++;
  124. }
  125. return 0;
  126. }
  127. /**
  128. * asd_get_attached_sas_addr -- extract/generate attached SAS address
  129. * phy: pointer to asd_phy
  130. * sas_addr: pointer to buffer where the SAS address is to be written
  131. *
  132. * This function extracts the SAS address from an IDENTIFY frame
  133. * received. If OOB is SATA, then a SAS address is generated from the
  134. * HA tables.
  135. *
  136. * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
  137. * buffer.
  138. */
  139. static inline void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
  140. {
  141. if (phy->sas_phy.frame_rcvd[0] == 0x34
  142. && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
  143. struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
  144. /* FIS device-to-host */
  145. u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
  146. addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
  147. *(__be64 *)sas_addr = cpu_to_be64(addr);
  148. } else {
  149. struct sas_identify_frame *idframe =
  150. (void *) phy->sas_phy.frame_rcvd;
  151. memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
  152. }
  153. }
  154. static inline void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
  155. struct done_list_struct *dl,
  156. int edb_id, int phy_id)
  157. {
  158. unsigned long flags;
  159. int edb_el = edb_id + ascb->edb_index;
  160. struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
  161. struct asd_phy *phy = &ascb->ha->phys[phy_id];
  162. struct sas_ha_struct *sas_ha = phy->sas_phy.ha;
  163. u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
  164. size = min(size, (u16) sizeof(phy->frame_rcvd));
  165. spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
  166. memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
  167. phy->sas_phy.frame_rcvd_size = size;
  168. asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
  169. spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
  170. asd_dump_frame_rcvd(phy, dl);
  171. sas_ha->notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
  172. }
  173. static inline void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
  174. struct done_list_struct *dl,
  175. int phy_id)
  176. {
  177. struct asd_ha_struct *asd_ha = ascb->ha;
  178. struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
  179. struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
  180. u8 lr_error = dl->status_block[1];
  181. u8 retries_left = dl->status_block[2];
  182. switch (lr_error) {
  183. case 0:
  184. ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
  185. break;
  186. case 1:
  187. ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
  188. break;
  189. case 2:
  190. ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
  191. break;
  192. case 3:
  193. ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
  194. break;
  195. default:
  196. ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
  197. phy_id, lr_error);
  198. break;
  199. }
  200. asd_turn_led(asd_ha, phy_id, 0);
  201. sas_phy_disconnected(sas_phy);
  202. sas_ha->notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
  203. if (retries_left == 0) {
  204. int num = 1;
  205. struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
  206. GFP_ATOMIC);
  207. if (!cp) {
  208. asd_printk("%s: out of memory\n", __FUNCTION__);
  209. goto out;
  210. }
  211. ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
  212. phy_id);
  213. asd_build_control_phy(cp, phy_id, ENABLE_PHY);
  214. if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
  215. asd_ascb_free(cp);
  216. }
  217. out:
  218. ;
  219. }
  220. static inline void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
  221. struct done_list_struct *dl,
  222. int phy_id)
  223. {
  224. unsigned long flags;
  225. struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
  226. struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
  227. u8 reg = dl->status_block[1];
  228. u32 cont = dl->status_block[2] << ((reg & 3)*8);
  229. reg &= ~3;
  230. switch (reg) {
  231. case LmPRMSTAT0BYTE0:
  232. switch (cont) {
  233. case LmBROADCH:
  234. case LmBROADRVCH0:
  235. case LmBROADRVCH1:
  236. case LmBROADSES:
  237. ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
  238. phy_id, cont);
  239. spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
  240. sas_phy->sas_prim = ffs(cont);
  241. spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
  242. sas_ha->notify_port_event(sas_phy,PORTE_BROADCAST_RCVD);
  243. break;
  244. case LmUNKNOWNP:
  245. ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
  246. break;
  247. default:
  248. ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
  249. phy_id, reg, cont);
  250. break;
  251. }
  252. break;
  253. case LmPRMSTAT1BYTE0:
  254. switch (cont) {
  255. case LmHARDRST:
  256. ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
  257. phy_id);
  258. /* The sequencer disables all phys on that port.
  259. * We have to re-enable the phys ourselves. */
  260. sas_ha->notify_port_event(sas_phy, PORTE_HARD_RESET);
  261. break;
  262. default:
  263. ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
  264. phy_id, reg, cont);
  265. break;
  266. }
  267. break;
  268. default:
  269. ASD_DPRINTK("unknown primitive register:0x%x\n",
  270. dl->status_block[1]);
  271. break;
  272. }
  273. }
  274. /**
  275. * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
  276. * @ascb: pointer to Empty SCB
  277. * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
  278. *
  279. * After an EDB has been invalidated, if all EDBs in this ESCB have been
  280. * invalidated, the ESCB is posted back to the sequencer.
  281. * Context is tasklet/IRQ.
  282. */
  283. void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
  284. {
  285. struct asd_seq_data *seq = &ascb->ha->seq;
  286. struct empty_scb *escb = &ascb->scb->escb;
  287. struct sg_el *eb = &escb->eb[edb_id];
  288. struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
  289. memset(edb->vaddr, 0, ASD_EDB_SIZE);
  290. eb->flags |= ELEMENT_NOT_VALID;
  291. escb->num_valid--;
  292. if (escb->num_valid == 0) {
  293. int i;
  294. /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
  295. "dma_handle: 0x%08llx, next: 0x%08llx, "
  296. "index:%d, opcode:0x%02x\n",
  297. ascb->dma_scb.vaddr,
  298. (u64)ascb->dma_scb.dma_handle,
  299. le64_to_cpu(ascb->scb->header.next_scb),
  300. le16_to_cpu(ascb->scb->header.index),
  301. ascb->scb->header.opcode);
  302. */
  303. escb->num_valid = ASD_EDBS_PER_SCB;
  304. for (i = 0; i < ASD_EDBS_PER_SCB; i++)
  305. escb->eb[i].flags = 0;
  306. if (!list_empty(&ascb->list))
  307. list_del_init(&ascb->list);
  308. i = asd_post_escb_list(ascb->ha, ascb, 1);
  309. if (i)
  310. asd_printk("couldn't post escb, err:%d\n", i);
  311. }
  312. }
  313. static void escb_tasklet_complete(struct asd_ascb *ascb,
  314. struct done_list_struct *dl)
  315. {
  316. struct asd_ha_struct *asd_ha = ascb->ha;
  317. struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
  318. int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
  319. u8 sb_opcode = dl->status_block[0];
  320. int phy_id = sb_opcode & DL_PHY_MASK;
  321. struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
  322. if (edb > 6 || edb < 0) {
  323. ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
  324. edb, dl->opcode);
  325. ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
  326. sb_opcode, phy_id);
  327. ASD_DPRINTK("escb: vaddr: 0x%p, "
  328. "dma_handle: 0x%llx, next: 0x%llx, "
  329. "index:%d, opcode:0x%02x\n",
  330. ascb->dma_scb.vaddr,
  331. (unsigned long long)ascb->dma_scb.dma_handle,
  332. (unsigned long long)
  333. le64_to_cpu(ascb->scb->header.next_scb),
  334. le16_to_cpu(ascb->scb->header.index),
  335. ascb->scb->header.opcode);
  336. }
  337. sb_opcode &= ~DL_PHY_MASK;
  338. switch (sb_opcode) {
  339. case BYTES_DMAED:
  340. ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __FUNCTION__, phy_id);
  341. asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
  342. break;
  343. case PRIMITIVE_RECVD:
  344. ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __FUNCTION__,
  345. phy_id);
  346. asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
  347. break;
  348. case PHY_EVENT:
  349. ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __FUNCTION__, phy_id);
  350. asd_phy_event_tasklet(ascb, dl);
  351. break;
  352. case LINK_RESET_ERROR:
  353. ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __FUNCTION__,
  354. phy_id);
  355. asd_link_reset_err_tasklet(ascb, dl, phy_id);
  356. break;
  357. case TIMER_EVENT:
  358. ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
  359. __FUNCTION__, phy_id);
  360. asd_turn_led(asd_ha, phy_id, 0);
  361. /* the device is gone */
  362. sas_phy_disconnected(sas_phy);
  363. sas_ha->notify_port_event(sas_phy, PORTE_TIMER_EVENT);
  364. break;
  365. case REQ_TASK_ABORT:
  366. ASD_DPRINTK("%s: phy%d: REQ_TASK_ABORT\n", __FUNCTION__,
  367. phy_id);
  368. break;
  369. case REQ_DEVICE_RESET:
  370. ASD_DPRINTK("%s: phy%d: REQ_DEVICE_RESET\n", __FUNCTION__,
  371. phy_id);
  372. break;
  373. case SIGNAL_NCQ_ERROR:
  374. ASD_DPRINTK("%s: phy%d: SIGNAL_NCQ_ERROR\n", __FUNCTION__,
  375. phy_id);
  376. break;
  377. case CLEAR_NCQ_ERROR:
  378. ASD_DPRINTK("%s: phy%d: CLEAR_NCQ_ERROR\n", __FUNCTION__,
  379. phy_id);
  380. break;
  381. default:
  382. ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __FUNCTION__,
  383. phy_id, sb_opcode);
  384. ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
  385. edb, dl->opcode);
  386. ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
  387. sb_opcode, phy_id);
  388. ASD_DPRINTK("escb: vaddr: 0x%p, "
  389. "dma_handle: 0x%llx, next: 0x%llx, "
  390. "index:%d, opcode:0x%02x\n",
  391. ascb->dma_scb.vaddr,
  392. (unsigned long long)ascb->dma_scb.dma_handle,
  393. (unsigned long long)
  394. le64_to_cpu(ascb->scb->header.next_scb),
  395. le16_to_cpu(ascb->scb->header.index),
  396. ascb->scb->header.opcode);
  397. break;
  398. }
  399. asd_invalidate_edb(ascb, edb);
  400. }
  401. int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
  402. {
  403. struct asd_seq_data *seq = &asd_ha->seq;
  404. int i;
  405. for (i = 0; i < seq->num_escbs; i++)
  406. seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
  407. ASD_DPRINTK("posting %d escbs\n", i);
  408. return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
  409. }
  410. /* ---------- CONTROL PHY ---------- */
  411. #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE \
  412. | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
  413. | CURRENT_OOB_ERROR)
  414. /**
  415. * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
  416. * @ascb: pointer to an ascb
  417. * @dl: pointer to the done list entry
  418. *
  419. * This function completes a CONTROL PHY scb and frees the ascb.
  420. * A note on LEDs:
  421. * - an LED blinks if there is IO though it,
  422. * - if a device is connected to the LED, it is lit,
  423. * - if no device is connected to the LED, is is dimmed (off).
  424. */
  425. static void control_phy_tasklet_complete(struct asd_ascb *ascb,
  426. struct done_list_struct *dl)
  427. {
  428. struct asd_ha_struct *asd_ha = ascb->ha;
  429. struct scb *scb = ascb->scb;
  430. struct control_phy *control_phy = &scb->control_phy;
  431. u8 phy_id = control_phy->phy_id;
  432. struct asd_phy *phy = &ascb->ha->phys[phy_id];
  433. u8 status = dl->status_block[0];
  434. u8 oob_status = dl->status_block[1];
  435. u8 oob_mode = dl->status_block[2];
  436. /* u8 oob_signals= dl->status_block[3]; */
  437. if (status != 0) {
  438. ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
  439. __FUNCTION__, phy_id, status);
  440. goto out;
  441. }
  442. switch (control_phy->sub_func) {
  443. case DISABLE_PHY:
  444. asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
  445. asd_turn_led(asd_ha, phy_id, 0);
  446. asd_control_led(asd_ha, phy_id, 0);
  447. ASD_DPRINTK("%s: disable phy%d\n", __FUNCTION__, phy_id);
  448. break;
  449. case ENABLE_PHY:
  450. asd_control_led(asd_ha, phy_id, 1);
  451. if (oob_status & CURRENT_OOB_DONE) {
  452. asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
  453. get_lrate_mode(phy, oob_mode);
  454. asd_turn_led(asd_ha, phy_id, 1);
  455. ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
  456. __FUNCTION__, phy_id,phy->sas_phy.linkrate,
  457. phy->sas_phy.iproto);
  458. } else if (oob_status & CURRENT_SPINUP_HOLD) {
  459. asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
  460. asd_turn_led(asd_ha, phy_id, 1);
  461. ASD_DPRINTK("%s: phy%d, spinup hold\n", __FUNCTION__,
  462. phy_id);
  463. } else if (oob_status & CURRENT_ERR_MASK) {
  464. asd_turn_led(asd_ha, phy_id, 0);
  465. ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
  466. __FUNCTION__, phy_id, oob_status);
  467. } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
  468. | CURRENT_DEVICE_PRESENT)) {
  469. asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
  470. asd_turn_led(asd_ha, phy_id, 1);
  471. ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
  472. __FUNCTION__, phy_id);
  473. } else {
  474. asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
  475. asd_turn_led(asd_ha, phy_id, 0);
  476. ASD_DPRINTK("%s: phy%d: no device present: "
  477. "oob_status:0x%x\n",
  478. __FUNCTION__, phy_id, oob_status);
  479. }
  480. break;
  481. case RELEASE_SPINUP_HOLD:
  482. case PHY_NO_OP:
  483. case EXECUTE_HARD_RESET:
  484. ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __FUNCTION__,
  485. phy_id, control_phy->sub_func);
  486. /* XXX finish */
  487. break;
  488. default:
  489. ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __FUNCTION__,
  490. phy_id, control_phy->sub_func);
  491. break;
  492. }
  493. out:
  494. asd_ascb_free(ascb);
  495. }
  496. static inline void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
  497. {
  498. /* disable all speeds, then enable defaults */
  499. *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
  500. | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
  501. switch (pd->max_sas_lrate) {
  502. case SAS_LINK_RATE_6_0_GBPS:
  503. *speed_mask &= ~SAS_SPEED_60_DIS;
  504. default:
  505. case SAS_LINK_RATE_3_0_GBPS:
  506. *speed_mask &= ~SAS_SPEED_30_DIS;
  507. case SAS_LINK_RATE_1_5_GBPS:
  508. *speed_mask &= ~SAS_SPEED_15_DIS;
  509. }
  510. switch (pd->min_sas_lrate) {
  511. case SAS_LINK_RATE_6_0_GBPS:
  512. *speed_mask |= SAS_SPEED_30_DIS;
  513. case SAS_LINK_RATE_3_0_GBPS:
  514. *speed_mask |= SAS_SPEED_15_DIS;
  515. default:
  516. case SAS_LINK_RATE_1_5_GBPS:
  517. /* nothing to do */
  518. ;
  519. }
  520. switch (pd->max_sata_lrate) {
  521. case SAS_LINK_RATE_3_0_GBPS:
  522. *speed_mask &= ~SATA_SPEED_30_DIS;
  523. default:
  524. case SAS_LINK_RATE_1_5_GBPS:
  525. *speed_mask &= ~SATA_SPEED_15_DIS;
  526. }
  527. switch (pd->min_sata_lrate) {
  528. case SAS_LINK_RATE_3_0_GBPS:
  529. *speed_mask |= SATA_SPEED_15_DIS;
  530. default:
  531. case SAS_LINK_RATE_1_5_GBPS:
  532. /* nothing to do */
  533. ;
  534. }
  535. }
  536. /**
  537. * asd_build_control_phy -- build a CONTROL PHY SCB
  538. * @ascb: pointer to an ascb
  539. * @phy_id: phy id to control, integer
  540. * @subfunc: subfunction, what to actually to do the phy
  541. *
  542. * This function builds a CONTROL PHY scb. No allocation of any kind
  543. * is performed. @ascb is allocated with the list function.
  544. * The caller can override the ascb->tasklet_complete to point
  545. * to its own callback function. It must call asd_ascb_free()
  546. * at its tasklet complete function.
  547. * See the default implementation.
  548. */
  549. void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
  550. {
  551. struct asd_phy *phy = &ascb->ha->phys[phy_id];
  552. struct scb *scb = ascb->scb;
  553. struct control_phy *control_phy = &scb->control_phy;
  554. scb->header.opcode = CONTROL_PHY;
  555. control_phy->phy_id = (u8) phy_id;
  556. control_phy->sub_func = subfunc;
  557. switch (subfunc) {
  558. case EXECUTE_HARD_RESET: /* 0x81 */
  559. case ENABLE_PHY: /* 0x01 */
  560. /* decide hot plug delay */
  561. control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
  562. /* decide speed mask */
  563. set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
  564. /* initiator port settings are in the hi nibble */
  565. if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
  566. control_phy->port_type = SAS_PROTO_ALL << 4;
  567. else if (phy->sas_phy.role == PHY_ROLE_TARGET)
  568. control_phy->port_type = SAS_PROTO_ALL;
  569. else
  570. control_phy->port_type =
  571. (SAS_PROTO_ALL << 4) | SAS_PROTO_ALL;
  572. /* link reset retries, this should be nominal */
  573. control_phy->link_reset_retries = 10;
  574. case RELEASE_SPINUP_HOLD: /* 0x02 */
  575. /* decide the func_mask */
  576. control_phy->func_mask = FUNCTION_MASK_DEFAULT;
  577. if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
  578. control_phy->func_mask &= ~SPINUP_HOLD_DIS;
  579. else
  580. control_phy->func_mask |= SPINUP_HOLD_DIS;
  581. }
  582. control_phy->conn_handle = cpu_to_le16(0xFFFF);
  583. ascb->tasklet_complete = control_phy_tasklet_complete;
  584. }
  585. /* ---------- INITIATE LINK ADM TASK ---------- */
  586. static void link_adm_tasklet_complete(struct asd_ascb *ascb,
  587. struct done_list_struct *dl)
  588. {
  589. u8 opcode = dl->opcode;
  590. struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
  591. u8 phy_id = link_adm->phy_id;
  592. if (opcode != TC_NO_ERROR) {
  593. asd_printk("phy%d: link adm task 0x%x completed with error "
  594. "0x%x\n", phy_id, link_adm->sub_func, opcode);
  595. }
  596. ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
  597. phy_id, link_adm->sub_func, opcode);
  598. asd_ascb_free(ascb);
  599. }
  600. void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
  601. u8 subfunc)
  602. {
  603. struct scb *scb = ascb->scb;
  604. struct initiate_link_adm *link_adm = &scb->link_adm;
  605. scb->header.opcode = INITIATE_LINK_ADM_TASK;
  606. link_adm->phy_id = phy_id;
  607. link_adm->sub_func = subfunc;
  608. link_adm->conn_handle = cpu_to_le16(0xFFFF);
  609. ascb->tasklet_complete = link_adm_tasklet_complete;
  610. }
  611. /* ---------- SCB timer ---------- */
  612. /**
  613. * asd_ascb_timedout -- called when a pending SCB's timer has expired
  614. * @data: unsigned long, a pointer to the ascb in question
  615. *
  616. * This is the default timeout function which does the most necessary.
  617. * Upper layers can implement their own timeout function, say to free
  618. * resources they have with this SCB, and then call this one at the
  619. * end of their timeout function. To do this, one should initialize
  620. * the ascb->timer.{function, data, expires} prior to calling the post
  621. * funcion. The timer is started by the post function.
  622. */
  623. void asd_ascb_timedout(unsigned long data)
  624. {
  625. struct asd_ascb *ascb = (void *) data;
  626. struct asd_seq_data *seq = &ascb->ha->seq;
  627. unsigned long flags;
  628. ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
  629. spin_lock_irqsave(&seq->pend_q_lock, flags);
  630. seq->pending--;
  631. list_del_init(&ascb->list);
  632. spin_unlock_irqrestore(&seq->pend_q_lock, flags);
  633. asd_ascb_free(ascb);
  634. }
  635. /* ---------- CONTROL PHY ---------- */
  636. /* Given the spec value, return a driver value. */
  637. static const int phy_func_table[] = {
  638. [PHY_FUNC_NOP] = PHY_NO_OP,
  639. [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
  640. [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
  641. [PHY_FUNC_DISABLE] = DISABLE_PHY,
  642. [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
  643. };
  644. int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
  645. {
  646. struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
  647. struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
  648. struct asd_ascb *ascb;
  649. struct sas_phy_linkrates *rates;
  650. int res = 1;
  651. switch (func) {
  652. case PHY_FUNC_CLEAR_ERROR_LOG:
  653. return -ENOSYS;
  654. case PHY_FUNC_SET_LINK_RATE:
  655. rates = arg;
  656. if (rates->minimum_linkrate) {
  657. pd->min_sas_lrate = rates->minimum_linkrate;
  658. pd->min_sata_lrate = rates->minimum_linkrate;
  659. }
  660. if (rates->maximum_linkrate) {
  661. pd->max_sas_lrate = rates->maximum_linkrate;
  662. pd->max_sata_lrate = rates->maximum_linkrate;
  663. }
  664. func = PHY_FUNC_LINK_RESET;
  665. break;
  666. default:
  667. break;
  668. }
  669. ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
  670. if (!ascb)
  671. return -ENOMEM;
  672. asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
  673. res = asd_post_ascb_list(asd_ha, ascb , 1);
  674. if (res)
  675. asd_ascb_free(ascb);
  676. return res;
  677. }