aic94xx_scb.c 24 KB

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