aic94xx_scb.c 26 KB

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