pm8001_sas.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
  3. *
  4. * Copyright (c) 2008-2009 USI Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  14. * substantially similar to the "NO WARRANTY" disclaimer below
  15. * ("Disclaimer") and any redistribution must be conditioned upon
  16. * including a substantially similar Disclaimer requirement for further
  17. * binary redistribution.
  18. * 3. Neither the names of the above-listed copyright holders nor the names
  19. * of any contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * Alternatively, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2 as published by the Free
  24. * Software Foundation.
  25. *
  26. * NO WARRANTY
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGES.
  38. *
  39. */
  40. #include <linux/slab.h>
  41. #include "pm8001_sas.h"
  42. #include "pm80xx_tracepoints.h"
  43. /**
  44. * pm8001_find_tag - from sas task to find out tag that belongs to this task
  45. * @task: the task sent to the LLDD
  46. * @tag: the found tag associated with the task
  47. */
  48. static int pm8001_find_tag(struct sas_task *task, u32 *tag)
  49. {
  50. if (task->lldd_task) {
  51. struct pm8001_ccb_info *ccb;
  52. ccb = task->lldd_task;
  53. *tag = ccb->ccb_tag;
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. /**
  59. * pm8001_tag_free - free the no more needed tag
  60. * @pm8001_ha: our hba struct
  61. * @tag: the found tag associated with the task
  62. */
  63. void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
  64. {
  65. void *bitmap = pm8001_ha->rsvd_tags;
  66. unsigned long flags;
  67. if (tag >= PM8001_RESERVE_SLOT)
  68. return;
  69. spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
  70. __clear_bit(tag, bitmap);
  71. spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
  72. }
  73. /**
  74. * pm8001_tag_alloc - allocate a empty tag for task used.
  75. * @pm8001_ha: our hba struct
  76. * @tag_out: the found empty tag .
  77. */
  78. int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
  79. {
  80. void *bitmap = pm8001_ha->rsvd_tags;
  81. unsigned long flags;
  82. unsigned int tag;
  83. spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
  84. tag = find_first_zero_bit(bitmap, PM8001_RESERVE_SLOT);
  85. if (tag >= PM8001_RESERVE_SLOT) {
  86. spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
  87. return -SAS_QUEUE_FULL;
  88. }
  89. __set_bit(tag, bitmap);
  90. spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
  91. /* reserved tags are in the lower region of the tagset */
  92. *tag_out = tag;
  93. return 0;
  94. }
  95. static void pm80xx_get_tag_opcodes(struct sas_task *task, int *ata_op,
  96. int *ata_tag, bool *task_aborted)
  97. {
  98. unsigned long flags;
  99. struct ata_queued_cmd *qc = NULL;
  100. *ata_op = 0;
  101. *ata_tag = -1;
  102. *task_aborted = false;
  103. if (!task)
  104. return;
  105. spin_lock_irqsave(&task->task_state_lock, flags);
  106. if (unlikely((task->task_state_flags & SAS_TASK_STATE_ABORTED)))
  107. *task_aborted = true;
  108. spin_unlock_irqrestore(&task->task_state_lock, flags);
  109. if (task->task_proto == SAS_PROTOCOL_STP) {
  110. // sas_ata_qc_issue path uses SAS_PROTOCOL_STP.
  111. // This only works for scsi + libsas + libata users.
  112. qc = task->uldd_task;
  113. if (qc) {
  114. *ata_op = qc->tf.command;
  115. *ata_tag = qc->tag;
  116. }
  117. }
  118. }
  119. u32 pm80xx_get_local_phy_id(struct domain_device *dev)
  120. {
  121. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  122. if (dev_parent_is_expander(dev))
  123. return dev->parent->ex_dev.ex_phy->phy_id;
  124. return pm8001_dev->attached_phy;
  125. }
  126. void pm80xx_show_pending_commands(struct pm8001_hba_info *pm8001_ha,
  127. struct pm8001_device *target_pm8001_dev)
  128. {
  129. int i = 0, ata_op = 0, ata_tag = -1;
  130. struct pm8001_ccb_info *ccb = NULL;
  131. struct sas_task *task = NULL;
  132. struct pm8001_device *pm8001_dev = NULL;
  133. bool task_aborted;
  134. for (i = 0; i < pm8001_ha->ccb_count; i++) {
  135. ccb = &pm8001_ha->ccb_info[i];
  136. if (ccb->ccb_tag == PM8001_INVALID_TAG)
  137. continue;
  138. pm8001_dev = ccb->device;
  139. if (target_pm8001_dev && pm8001_dev &&
  140. target_pm8001_dev != pm8001_dev)
  141. continue;
  142. task = ccb->task;
  143. pm80xx_get_tag_opcodes(task, &ata_op, &ata_tag, &task_aborted);
  144. pm8001_dbg(pm8001_ha, FAIL,
  145. "tag %#x, device %#x task %p task aborted %d ata opcode %#x ata tag %d\n",
  146. ccb->ccb_tag,
  147. (pm8001_dev ? pm8001_dev->device_id : 0),
  148. task, task_aborted,
  149. ata_op, ata_tag);
  150. }
  151. }
  152. /**
  153. * pm8001_mem_alloc - allocate memory for pm8001.
  154. * @pdev: pci device.
  155. * @virt_addr: the allocated virtual address
  156. * @pphys_addr: DMA address for this device
  157. * @pphys_addr_hi: the physical address high byte address.
  158. * @pphys_addr_lo: the physical address low byte address.
  159. * @mem_size: memory size.
  160. * @align: requested byte alignment
  161. */
  162. int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
  163. dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
  164. u32 *pphys_addr_lo, u32 mem_size, u32 align)
  165. {
  166. caddr_t mem_virt_alloc;
  167. dma_addr_t mem_dma_handle;
  168. u64 phys_align;
  169. u64 align_offset = 0;
  170. if (align)
  171. align_offset = (dma_addr_t)align - 1;
  172. mem_virt_alloc = dma_alloc_coherent(&pdev->dev, mem_size + align,
  173. &mem_dma_handle, GFP_KERNEL);
  174. if (!mem_virt_alloc)
  175. return -ENOMEM;
  176. *pphys_addr = mem_dma_handle;
  177. phys_align = (*pphys_addr + align_offset) & ~align_offset;
  178. *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
  179. *pphys_addr_hi = upper_32_bits(phys_align);
  180. *pphys_addr_lo = lower_32_bits(phys_align);
  181. return 0;
  182. }
  183. /**
  184. * pm8001_find_ha_by_dev - from domain device which come from sas layer to
  185. * find out our hba struct.
  186. * @dev: the domain device which from sas layer.
  187. */
  188. static
  189. struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
  190. {
  191. struct sas_ha_struct *sha = dev->port->ha;
  192. struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
  193. return pm8001_ha;
  194. }
  195. /**
  196. * pm8001_phy_control - this function should be registered to
  197. * sas_domain_function_template to provide libsas used, note: this is just
  198. * control the HBA phy rather than other expander phy if you want control
  199. * other phy, you should use SMP command.
  200. * @sas_phy: which phy in HBA phys.
  201. * @func: the operation.
  202. * @funcdata: always NULL.
  203. */
  204. int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
  205. void *funcdata)
  206. {
  207. int rc = 0, phy_id = sas_phy->id;
  208. struct pm8001_hba_info *pm8001_ha = NULL;
  209. struct sas_phy_linkrates *rates;
  210. struct pm8001_phy *phy;
  211. DECLARE_COMPLETION_ONSTACK(completion);
  212. unsigned long flags;
  213. pm8001_ha = sas_phy->ha->lldd_ha;
  214. phy = &pm8001_ha->phy[phy_id];
  215. if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
  216. /*
  217. * If the controller is in fatal error state,
  218. * we will not get a response from the controller
  219. */
  220. pm8001_dbg(pm8001_ha, FAIL,
  221. "Phy control failed due to fatal errors\n");
  222. return -EFAULT;
  223. }
  224. switch (func) {
  225. case PHY_FUNC_SET_LINK_RATE:
  226. rates = funcdata;
  227. if (rates->minimum_linkrate) {
  228. pm8001_ha->phy[phy_id].minimum_linkrate =
  229. rates->minimum_linkrate;
  230. }
  231. if (rates->maximum_linkrate) {
  232. pm8001_ha->phy[phy_id].maximum_linkrate =
  233. rates->maximum_linkrate;
  234. }
  235. if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
  236. pm8001_ha->phy[phy_id].enable_completion = &completion;
  237. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
  238. wait_for_completion(&completion);
  239. }
  240. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  241. PHY_LINK_RESET);
  242. break;
  243. case PHY_FUNC_HARD_RESET:
  244. if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
  245. pm8001_ha->phy[phy_id].enable_completion = &completion;
  246. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
  247. wait_for_completion(&completion);
  248. }
  249. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  250. PHY_HARD_RESET);
  251. break;
  252. case PHY_FUNC_LINK_RESET:
  253. if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
  254. pm8001_ha->phy[phy_id].enable_completion = &completion;
  255. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
  256. wait_for_completion(&completion);
  257. }
  258. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  259. PHY_LINK_RESET);
  260. break;
  261. case PHY_FUNC_RELEASE_SPINUP_HOLD:
  262. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  263. PHY_LINK_RESET);
  264. break;
  265. case PHY_FUNC_DISABLE:
  266. if (pm8001_ha->chip_id != chip_8001) {
  267. if (pm8001_ha->phy[phy_id].phy_state ==
  268. PHY_STATE_LINK_UP_SPCV) {
  269. sas_phy_disconnected(&phy->sas_phy);
  270. sas_notify_phy_event(&phy->sas_phy,
  271. PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
  272. phy->phy_attached = 0;
  273. }
  274. } else {
  275. if (pm8001_ha->phy[phy_id].phy_state ==
  276. PHY_STATE_LINK_UP_SPC) {
  277. sas_phy_disconnected(&phy->sas_phy);
  278. sas_notify_phy_event(&phy->sas_phy,
  279. PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
  280. phy->phy_attached = 0;
  281. }
  282. }
  283. PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
  284. break;
  285. case PHY_FUNC_GET_EVENTS:
  286. spin_lock_irqsave(&pm8001_ha->lock, flags);
  287. if (pm8001_ha->chip_id == chip_8001) {
  288. if (-1 == pm8001_bar4_shift(pm8001_ha,
  289. (phy_id < 4) ? 0x30000 : 0x40000)) {
  290. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  291. return -EINVAL;
  292. }
  293. }
  294. {
  295. struct sas_phy *phy = sas_phy->phy;
  296. u32 __iomem *qp = pm8001_ha->io_mem[2].memvirtaddr
  297. + 0x1034 + (0x4000 * (phy_id & 3));
  298. phy->invalid_dword_count = readl(qp);
  299. phy->running_disparity_error_count = readl(&qp[1]);
  300. phy->loss_of_dword_sync_count = readl(&qp[3]);
  301. phy->phy_reset_problem_count = readl(&qp[4]);
  302. }
  303. if (pm8001_ha->chip_id == chip_8001)
  304. pm8001_bar4_shift(pm8001_ha, 0);
  305. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  306. return 0;
  307. default:
  308. pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
  309. rc = -EOPNOTSUPP;
  310. }
  311. msleep(300);
  312. return rc;
  313. }
  314. /**
  315. * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
  316. * command to HBA.
  317. * @shost: the scsi host data.
  318. */
  319. void pm8001_scan_start(struct Scsi_Host *shost)
  320. {
  321. int i;
  322. struct pm8001_hba_info *pm8001_ha;
  323. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  324. DECLARE_COMPLETION_ONSTACK(completion);
  325. pm8001_ha = sha->lldd_ha;
  326. /* SAS_RE_INITIALIZATION not available in SPCv/ve */
  327. if (pm8001_ha->chip_id == chip_8001)
  328. PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
  329. for (i = 0; i < pm8001_ha->chip->n_phy; ++i) {
  330. pm8001_ha->phy[i].enable_completion = &completion;
  331. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
  332. wait_for_completion(&completion);
  333. msleep(300);
  334. }
  335. }
  336. int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
  337. {
  338. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  339. /* give the phy enabling interrupt event time to come in (1s
  340. * is empirically about all it takes) */
  341. if (time < HZ)
  342. return 0;
  343. /* Wait for discovery to finish */
  344. sas_drain_work(ha);
  345. return 1;
  346. }
  347. /**
  348. * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
  349. * @pm8001_ha: our hba card information
  350. * @ccb: the ccb which attached to smp task
  351. */
  352. static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
  353. struct pm8001_ccb_info *ccb)
  354. {
  355. return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
  356. }
  357. u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
  358. {
  359. struct ata_queued_cmd *qc = task->uldd_task;
  360. if (qc && ata_is_ncq(qc->tf.protocol)) {
  361. *tag = qc->tag;
  362. return 1;
  363. }
  364. return 0;
  365. }
  366. /**
  367. * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
  368. * @pm8001_ha: our hba card information
  369. * @ccb: the ccb which attached to sata task
  370. */
  371. static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
  372. struct pm8001_ccb_info *ccb)
  373. {
  374. return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
  375. }
  376. /**
  377. * pm8001_task_prep_internal_abort - the dispatcher function, prepare data
  378. * for internal abort task
  379. * @pm8001_ha: our hba card information
  380. * @ccb: the ccb which attached to sata task
  381. */
  382. static int pm8001_task_prep_internal_abort(struct pm8001_hba_info *pm8001_ha,
  383. struct pm8001_ccb_info *ccb)
  384. {
  385. return PM8001_CHIP_DISP->task_abort(pm8001_ha, ccb);
  386. }
  387. /**
  388. * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
  389. * @pm8001_ha: our hba card information
  390. * @ccb: the ccb which attached to TM
  391. * @tmf: the task management IU
  392. */
  393. static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
  394. struct pm8001_ccb_info *ccb, struct sas_tmf_task *tmf)
  395. {
  396. return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
  397. }
  398. /**
  399. * pm8001_task_prep_ssp - the dispatcher function, prepare ssp data for ssp task
  400. * @pm8001_ha: our hba card information
  401. * @ccb: the ccb which attached to ssp task
  402. */
  403. static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
  404. struct pm8001_ccb_info *ccb)
  405. {
  406. return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
  407. }
  408. #define DEV_IS_GONE(pm8001_dev) \
  409. ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
  410. static int pm8001_deliver_command(struct pm8001_hba_info *pm8001_ha,
  411. struct pm8001_ccb_info *ccb)
  412. {
  413. struct sas_task *task = ccb->task;
  414. enum sas_protocol task_proto = task->task_proto;
  415. struct sas_tmf_task *tmf = task->tmf;
  416. int is_tmf = !!tmf;
  417. switch (task_proto) {
  418. case SAS_PROTOCOL_SMP:
  419. return pm8001_task_prep_smp(pm8001_ha, ccb);
  420. case SAS_PROTOCOL_SSP:
  421. if (is_tmf)
  422. return pm8001_task_prep_ssp_tm(pm8001_ha, ccb, tmf);
  423. return pm8001_task_prep_ssp(pm8001_ha, ccb);
  424. case SAS_PROTOCOL_SATA:
  425. case SAS_PROTOCOL_STP:
  426. return pm8001_task_prep_ata(pm8001_ha, ccb);
  427. case SAS_PROTOCOL_INTERNAL_ABORT:
  428. return pm8001_task_prep_internal_abort(pm8001_ha, ccb);
  429. default:
  430. dev_err(pm8001_ha->dev, "unknown sas_task proto: 0x%x\n",
  431. task_proto);
  432. }
  433. return -EINVAL;
  434. }
  435. /**
  436. * pm8001_queue_command - register for upper layer used, all IO commands sent
  437. * to HBA are from this interface.
  438. * @task: the task to be execute.
  439. * @gfp_flags: gfp_flags
  440. */
  441. int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
  442. {
  443. struct task_status_struct *ts = &task->task_status;
  444. enum sas_protocol task_proto = task->task_proto;
  445. struct domain_device *dev = task->dev;
  446. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  447. bool internal_abort = sas_is_internal_abort(task);
  448. struct pm8001_hba_info *pm8001_ha;
  449. struct pm8001_port *port;
  450. struct pm8001_ccb_info *ccb;
  451. unsigned long flags;
  452. u32 n_elem = 0;
  453. int rc = 0;
  454. if (!internal_abort && !dev->port) {
  455. ts->resp = SAS_TASK_UNDELIVERED;
  456. ts->stat = SAS_PHY_DOWN;
  457. if (dev->dev_type != SAS_SATA_DEV)
  458. task->task_done(task);
  459. return 0;
  460. }
  461. pm8001_ha = pm8001_find_ha_by_dev(dev);
  462. if (pm8001_ha->controller_fatal_error) {
  463. ts->resp = SAS_TASK_UNDELIVERED;
  464. task->task_done(task);
  465. return 0;
  466. }
  467. pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
  468. spin_lock_irqsave(&pm8001_ha->lock, flags);
  469. port = dev->port->lldd_port;
  470. if (!internal_abort &&
  471. (DEV_IS_GONE(pm8001_dev) || !port || !port->port_attached)) {
  472. ts->resp = SAS_TASK_UNDELIVERED;
  473. ts->stat = SAS_PHY_DOWN;
  474. if (sas_protocol_ata(task_proto)) {
  475. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  476. task->task_done(task);
  477. spin_lock_irqsave(&pm8001_ha->lock, flags);
  478. } else {
  479. task->task_done(task);
  480. }
  481. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  482. pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device gone\n");
  483. return 0;
  484. }
  485. ccb = pm8001_ccb_alloc(pm8001_ha, pm8001_dev, task);
  486. if (!ccb) {
  487. rc = -SAS_QUEUE_FULL;
  488. goto err_out;
  489. }
  490. if (!sas_protocol_ata(task_proto)) {
  491. if (task->num_scatter) {
  492. n_elem = dma_map_sg(pm8001_ha->dev, task->scatter,
  493. task->num_scatter, task->data_dir);
  494. if (!n_elem) {
  495. rc = -ENOMEM;
  496. goto err_out_ccb;
  497. }
  498. }
  499. } else {
  500. n_elem = task->num_scatter;
  501. }
  502. task->lldd_task = ccb;
  503. ccb->n_elem = n_elem;
  504. atomic_inc(&pm8001_dev->running_req);
  505. rc = pm8001_deliver_command(pm8001_ha, ccb);
  506. if (rc) {
  507. atomic_dec(&pm8001_dev->running_req);
  508. if (!sas_protocol_ata(task_proto) && n_elem)
  509. dma_unmap_sg(pm8001_ha->dev, task->scatter,
  510. task->num_scatter, task->data_dir);
  511. err_out_ccb:
  512. pm8001_ccb_free(pm8001_ha, ccb);
  513. err_out:
  514. pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec failed[%d]!\n", rc);
  515. }
  516. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  517. return rc;
  518. }
  519. /**
  520. * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
  521. * @pm8001_ha: our hba card information
  522. * @ccb: the ccb which attached to ssp task to free
  523. */
  524. void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
  525. struct pm8001_ccb_info *ccb)
  526. {
  527. struct sas_task *task = ccb->task;
  528. struct ata_queued_cmd *qc;
  529. struct pm8001_device *pm8001_dev;
  530. if (!task)
  531. return;
  532. if (!sas_protocol_ata(task->task_proto) && ccb->n_elem)
  533. dma_unmap_sg(pm8001_ha->dev, task->scatter,
  534. task->num_scatter, task->data_dir);
  535. switch (task->task_proto) {
  536. case SAS_PROTOCOL_SMP:
  537. dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
  538. DMA_FROM_DEVICE);
  539. dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
  540. DMA_TO_DEVICE);
  541. break;
  542. case SAS_PROTOCOL_SATA:
  543. case SAS_PROTOCOL_STP:
  544. case SAS_PROTOCOL_SSP:
  545. default:
  546. /* do nothing */
  547. break;
  548. }
  549. if (sas_protocol_ata(task->task_proto)) {
  550. /* For SCSI/ATA commands uldd_task points to ata_queued_cmd */
  551. qc = task->uldd_task;
  552. pm8001_dev = ccb->device;
  553. trace_pm80xx_request_complete(pm8001_ha->id,
  554. pm8001_dev ? pm8001_dev->attached_phy : PM8001_MAX_PHYS,
  555. ccb->ccb_tag, 0 /* ctlr_opcode not known */,
  556. qc ? qc->tf.command : 0, // ata opcode
  557. pm8001_dev ? atomic_read(&pm8001_dev->running_req) : -1);
  558. }
  559. task->lldd_task = NULL;
  560. pm8001_ccb_free(pm8001_ha, ccb);
  561. }
  562. static void pm8001_init_dev(struct pm8001_device *pm8001_dev, int id)
  563. {
  564. pm8001_dev->id = id;
  565. pm8001_dev->device_id = PM8001_MAX_DEVICES;
  566. atomic_set(&pm8001_dev->running_req, 0);
  567. }
  568. /**
  569. * pm8001_alloc_dev - find a empty pm8001_device
  570. * @pm8001_ha: our hba card information
  571. */
  572. static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
  573. {
  574. u32 dev;
  575. for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
  576. struct pm8001_device *pm8001_dev = &pm8001_ha->devices[dev];
  577. if (pm8001_dev->dev_type == SAS_PHY_UNUSED) {
  578. pm8001_init_dev(pm8001_dev, dev);
  579. return pm8001_dev;
  580. }
  581. }
  582. if (dev == PM8001_MAX_DEVICES) {
  583. pm8001_dbg(pm8001_ha, FAIL,
  584. "max support %d devices, ignore ..\n",
  585. PM8001_MAX_DEVICES);
  586. }
  587. return NULL;
  588. }
  589. /**
  590. * pm8001_find_dev - find a matching pm8001_device
  591. * @pm8001_ha: our hba card information
  592. * @device_id: device ID to match against
  593. */
  594. struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
  595. u32 device_id)
  596. {
  597. u32 dev;
  598. for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
  599. if (pm8001_ha->devices[dev].device_id == device_id)
  600. return &pm8001_ha->devices[dev];
  601. }
  602. if (dev == PM8001_MAX_DEVICES) {
  603. pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
  604. }
  605. return NULL;
  606. }
  607. void pm8001_free_dev(struct pm8001_device *pm8001_dev)
  608. {
  609. memset(pm8001_dev, 0, sizeof(*pm8001_dev));
  610. pm8001_dev->dev_type = SAS_PHY_UNUSED;
  611. pm8001_dev->device_id = PM8001_MAX_DEVICES;
  612. pm8001_dev->sas_device = NULL;
  613. }
  614. /**
  615. * pm8001_dev_found_notify - libsas notify a device is found.
  616. * @dev: the device structure which sas layer used.
  617. *
  618. * when libsas find a sas domain device, it should tell the LLDD that
  619. * device is found, and then LLDD register this device to HBA firmware
  620. * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
  621. * device ID(according to device's sas address) and returned it to LLDD. From
  622. * now on, we communicate with HBA FW with the device ID which HBA assigned
  623. * rather than sas address. it is the necessary step for our HBA but it is
  624. * the optional for other HBA driver.
  625. */
  626. static int pm8001_dev_found_notify(struct domain_device *dev)
  627. {
  628. unsigned long flags = 0;
  629. int res = 0;
  630. struct pm8001_hba_info *pm8001_ha = NULL;
  631. struct domain_device *parent_dev = dev->parent;
  632. struct pm8001_device *pm8001_device;
  633. DECLARE_COMPLETION_ONSTACK(completion);
  634. u32 flag = 0;
  635. pm8001_ha = pm8001_find_ha_by_dev(dev);
  636. spin_lock_irqsave(&pm8001_ha->lock, flags);
  637. pm8001_device = pm8001_alloc_dev(pm8001_ha);
  638. if (!pm8001_device) {
  639. res = -1;
  640. goto found_out;
  641. }
  642. pm8001_device->sas_device = dev;
  643. dev->lldd_dev = pm8001_device;
  644. pm8001_device->dev_type = dev->dev_type;
  645. pm8001_device->dcompletion = &completion;
  646. if (dev_parent_is_expander(dev)) {
  647. int phy_id;
  648. phy_id = sas_find_attached_phy_id(&parent_dev->ex_dev, dev);
  649. if (phy_id < 0) {
  650. pm8001_dbg(pm8001_ha, FAIL,
  651. "Error: no attached dev:%016llx at ex:%016llx.\n",
  652. SAS_ADDR(dev->sas_addr),
  653. SAS_ADDR(parent_dev->sas_addr));
  654. res = phy_id;
  655. } else {
  656. pm8001_device->attached_phy = phy_id;
  657. }
  658. } else {
  659. if (dev->dev_type == SAS_SATA_DEV) {
  660. pm8001_device->attached_phy =
  661. dev->rphy->identify.phy_identifier;
  662. flag = 1; /* directly sata */
  663. }
  664. } /*register this device to HBA*/
  665. pm8001_dbg(pm8001_ha, DISC, "Found device\n");
  666. PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
  667. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  668. wait_for_completion(&completion);
  669. if (dev->dev_type == SAS_END_DEVICE)
  670. msleep(50);
  671. pm8001_ha->flags = PM8001F_RUN_TIME;
  672. return 0;
  673. found_out:
  674. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  675. return res;
  676. }
  677. int pm8001_dev_found(struct domain_device *dev)
  678. {
  679. return pm8001_dev_found_notify(dev);
  680. }
  681. #define PM8001_TASK_TIMEOUT 20
  682. /**
  683. * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
  684. * @dev: the device structure which sas layer used.
  685. */
  686. static void pm8001_dev_gone_notify(struct domain_device *dev)
  687. {
  688. unsigned long flags = 0;
  689. struct pm8001_hba_info *pm8001_ha;
  690. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  691. pm8001_ha = pm8001_find_ha_by_dev(dev);
  692. spin_lock_irqsave(&pm8001_ha->lock, flags);
  693. if (pm8001_dev) {
  694. u32 device_id = pm8001_dev->device_id;
  695. pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
  696. pm8001_dev->device_id, pm8001_dev->dev_type);
  697. if (atomic_read(&pm8001_dev->running_req)) {
  698. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  699. sas_execute_internal_abort_dev(dev, 0, NULL);
  700. while (atomic_read(&pm8001_dev->running_req))
  701. msleep(20);
  702. spin_lock_irqsave(&pm8001_ha->lock, flags);
  703. }
  704. PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
  705. /*
  706. * The phy array only contains local phys. Thus, we cannot clear
  707. * phy_attached for a device behind an expander.
  708. */
  709. if (!dev_parent_is_expander(dev)) {
  710. u32 phy_id = pm80xx_get_local_phy_id(dev);
  711. pm8001_ha->phy[phy_id].phy_attached = 0;
  712. }
  713. pm8001_free_dev(pm8001_dev);
  714. } else {
  715. pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
  716. }
  717. dev->lldd_dev = NULL;
  718. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  719. }
  720. void pm8001_dev_gone(struct domain_device *dev)
  721. {
  722. pm8001_dev_gone_notify(dev);
  723. }
  724. /* retry commands by ha, by task and/or by device */
  725. void pm8001_open_reject_retry(
  726. struct pm8001_hba_info *pm8001_ha,
  727. struct sas_task *task_to_close,
  728. struct pm8001_device *device_to_close)
  729. {
  730. int i;
  731. unsigned long flags;
  732. if (pm8001_ha == NULL)
  733. return;
  734. spin_lock_irqsave(&pm8001_ha->lock, flags);
  735. for (i = 0; i < PM8001_MAX_CCB; i++) {
  736. struct sas_task *task;
  737. struct task_status_struct *ts;
  738. struct pm8001_device *pm8001_dev;
  739. unsigned long flags1;
  740. struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
  741. if (ccb->ccb_tag == PM8001_INVALID_TAG)
  742. continue;
  743. pm8001_dev = ccb->device;
  744. if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
  745. continue;
  746. if (!device_to_close) {
  747. uintptr_t d = (uintptr_t)pm8001_dev
  748. - (uintptr_t)&pm8001_ha->devices;
  749. if (((d % sizeof(*pm8001_dev)) != 0)
  750. || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
  751. continue;
  752. } else if (pm8001_dev != device_to_close)
  753. continue;
  754. task = ccb->task;
  755. if (!task || !task->task_done)
  756. continue;
  757. if (task_to_close && (task != task_to_close))
  758. continue;
  759. ts = &task->task_status;
  760. ts->resp = SAS_TASK_COMPLETE;
  761. /* Force the midlayer to retry */
  762. ts->stat = SAS_OPEN_REJECT;
  763. ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
  764. if (pm8001_dev)
  765. atomic_dec(&pm8001_dev->running_req);
  766. spin_lock_irqsave(&task->task_state_lock, flags1);
  767. task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
  768. task->task_state_flags |= SAS_TASK_STATE_DONE;
  769. if (unlikely((task->task_state_flags
  770. & SAS_TASK_STATE_ABORTED))) {
  771. spin_unlock_irqrestore(&task->task_state_lock,
  772. flags1);
  773. pm8001_ccb_task_free(pm8001_ha, ccb);
  774. } else {
  775. spin_unlock_irqrestore(&task->task_state_lock,
  776. flags1);
  777. pm8001_ccb_task_free(pm8001_ha, ccb);
  778. mb();/* in order to force CPU ordering */
  779. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  780. task->task_done(task);
  781. spin_lock_irqsave(&pm8001_ha->lock, flags);
  782. }
  783. }
  784. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  785. }
  786. /**
  787. * pm8001_I_T_nexus_reset() - reset the initiator/target connection
  788. * @dev: the device structure for the device to reset.
  789. *
  790. * Standard mandates link reset for ATA (type 0) and hard reset for
  791. * SSP (type 1), only for RECOVERY
  792. */
  793. int pm8001_I_T_nexus_reset(struct domain_device *dev)
  794. {
  795. int rc = TMF_RESP_FUNC_FAILED;
  796. struct pm8001_device *pm8001_dev;
  797. struct pm8001_hba_info *pm8001_ha;
  798. struct sas_phy *phy;
  799. if (!dev || !dev->lldd_dev)
  800. return -ENODEV;
  801. pm8001_dev = dev->lldd_dev;
  802. pm8001_ha = pm8001_find_ha_by_dev(dev);
  803. phy = sas_get_local_phy(dev);
  804. if (dev_is_sata(dev)) {
  805. if (scsi_is_sas_phy_local(phy)) {
  806. rc = 0;
  807. goto out;
  808. }
  809. rc = sas_phy_reset(phy, 1);
  810. if (rc) {
  811. pm8001_dbg(pm8001_ha, EH,
  812. "phy reset failed for device %x\n"
  813. "with rc %d\n", pm8001_dev->device_id, rc);
  814. rc = TMF_RESP_FUNC_FAILED;
  815. goto out;
  816. }
  817. msleep(2000);
  818. rc = sas_execute_internal_abort_dev(dev, 0, NULL);
  819. if (rc) {
  820. pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
  821. "with rc %d\n", pm8001_dev->device_id, rc);
  822. rc = TMF_RESP_FUNC_FAILED;
  823. }
  824. } else {
  825. rc = sas_phy_reset(phy, 1);
  826. msleep(2000);
  827. }
  828. pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
  829. pm8001_dev->device_id, rc);
  830. out:
  831. sas_put_local_phy(phy);
  832. return rc;
  833. }
  834. /*
  835. * This function handle the IT_NEXUS_XXX event or completion
  836. * status code for SSP/SATA/SMP I/O request.
  837. */
  838. int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
  839. {
  840. int rc = TMF_RESP_FUNC_FAILED;
  841. struct pm8001_device *pm8001_dev;
  842. struct pm8001_hba_info *pm8001_ha;
  843. struct sas_phy *phy;
  844. if (!dev || !dev->lldd_dev)
  845. return -1;
  846. pm8001_dev = dev->lldd_dev;
  847. pm8001_ha = pm8001_find_ha_by_dev(dev);
  848. pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
  849. phy = sas_get_local_phy(dev);
  850. if (dev_is_sata(dev)) {
  851. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  852. if (scsi_is_sas_phy_local(phy)) {
  853. rc = 0;
  854. goto out;
  855. }
  856. /* send internal ssp/sata/smp abort command to FW */
  857. sas_execute_internal_abort_dev(dev, 0, NULL);
  858. msleep(100);
  859. /* deregister the target device */
  860. pm8001_dev_gone_notify(dev);
  861. msleep(200);
  862. /*send phy reset to hard reset target */
  863. rc = sas_phy_reset(phy, 1);
  864. msleep(2000);
  865. pm8001_dev->setds_completion = &completion_setstate;
  866. wait_for_completion(&completion_setstate);
  867. } else {
  868. /* send internal ssp/sata/smp abort command to FW */
  869. sas_execute_internal_abort_dev(dev, 0, NULL);
  870. msleep(100);
  871. /* deregister the target device */
  872. pm8001_dev_gone_notify(dev);
  873. msleep(200);
  874. /*send phy reset to hard reset target */
  875. rc = sas_phy_reset(phy, 1);
  876. msleep(2000);
  877. }
  878. pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
  879. pm8001_dev->device_id, rc);
  880. out:
  881. sas_put_local_phy(phy);
  882. return rc;
  883. }
  884. /* mandatory SAM-3, the task reset the specified LUN*/
  885. int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
  886. {
  887. int rc = TMF_RESP_FUNC_FAILED;
  888. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  889. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  890. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  891. if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
  892. /*
  893. * If the controller is in fatal error state,
  894. * we will not get a response from the controller
  895. */
  896. pm8001_dbg(pm8001_ha, FAIL,
  897. "LUN reset failed due to fatal errors\n");
  898. return rc;
  899. }
  900. if (dev_is_sata(dev)) {
  901. struct sas_phy *phy = sas_get_local_phy(dev);
  902. sas_execute_internal_abort_dev(dev, 0, NULL);
  903. rc = sas_phy_reset(phy, 1);
  904. sas_put_local_phy(phy);
  905. pm8001_dev->setds_completion = &completion_setstate;
  906. rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  907. pm8001_dev, DS_OPERATIONAL);
  908. wait_for_completion(&completion_setstate);
  909. } else {
  910. rc = sas_lu_reset(dev, lun);
  911. }
  912. /* If failed, fall-through I_T_Nexus reset */
  913. pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
  914. pm8001_dev->device_id, rc);
  915. return rc;
  916. }
  917. /* optional SAM-3 */
  918. int pm8001_query_task(struct sas_task *task)
  919. {
  920. u32 tag = 0xdeadbeef;
  921. int rc = TMF_RESP_FUNC_FAILED;
  922. if (unlikely(!task || !task->lldd_task || !task->dev))
  923. return rc;
  924. if (task->task_proto & SAS_PROTOCOL_SSP) {
  925. struct scsi_cmnd *cmnd = task->uldd_task;
  926. struct domain_device *dev = task->dev;
  927. struct pm8001_hba_info *pm8001_ha =
  928. pm8001_find_ha_by_dev(dev);
  929. rc = pm8001_find_tag(task, &tag);
  930. if (rc == 0) {
  931. rc = TMF_RESP_FUNC_FAILED;
  932. return rc;
  933. }
  934. pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
  935. rc = sas_query_task(task, tag);
  936. switch (rc) {
  937. /* The task is still in Lun, release it then */
  938. case TMF_RESP_FUNC_SUCC:
  939. pm8001_dbg(pm8001_ha, EH,
  940. "The task is still in Lun\n");
  941. break;
  942. /* The task is not in Lun or failed, reset the phy */
  943. case TMF_RESP_FUNC_FAILED:
  944. case TMF_RESP_FUNC_COMPLETE:
  945. pm8001_dbg(pm8001_ha, EH,
  946. "The task is not in Lun or failed, reset the phy\n");
  947. break;
  948. }
  949. }
  950. pr_err("pm80xx: rc= %d\n", rc);
  951. return rc;
  952. }
  953. /* mandatory SAM-3, still need free task/ccb info, abort the specified task */
  954. int pm8001_abort_task(struct sas_task *task)
  955. {
  956. struct pm8001_ccb_info *ccb = task->lldd_task;
  957. unsigned long flags;
  958. u32 tag;
  959. struct domain_device *dev ;
  960. struct pm8001_hba_info *pm8001_ha;
  961. struct pm8001_device *pm8001_dev;
  962. int rc = TMF_RESP_FUNC_FAILED, ret;
  963. u32 port_id;
  964. struct sas_task_slow slow_task;
  965. if (!task->lldd_task || !task->dev)
  966. return TMF_RESP_FUNC_FAILED;
  967. dev = task->dev;
  968. pm8001_dev = dev->lldd_dev;
  969. pm8001_ha = pm8001_find_ha_by_dev(dev);
  970. if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
  971. // If the controller is seeing fatal errors
  972. // abort task will not get a response from the controller
  973. return TMF_RESP_FUNC_FAILED;
  974. }
  975. ret = pm8001_find_tag(task, &tag);
  976. if (ret == 0) {
  977. pm8001_info(pm8001_ha, "no tag for task:%p\n", task);
  978. return TMF_RESP_FUNC_FAILED;
  979. }
  980. spin_lock_irqsave(&task->task_state_lock, flags);
  981. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  982. spin_unlock_irqrestore(&task->task_state_lock, flags);
  983. return TMF_RESP_FUNC_COMPLETE;
  984. }
  985. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  986. if (task->slow_task == NULL) {
  987. init_completion(&slow_task.completion);
  988. task->slow_task = &slow_task;
  989. }
  990. spin_unlock_irqrestore(&task->task_state_lock, flags);
  991. if (task->task_proto & SAS_PROTOCOL_SSP) {
  992. rc = sas_abort_task(task, tag);
  993. sas_execute_internal_abort_single(dev, tag, 0, NULL);
  994. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  995. task->task_proto & SAS_PROTOCOL_STP) {
  996. if (pm8001_ha->chip_id == chip_8006) {
  997. DECLARE_COMPLETION_ONSTACK(completion_reset);
  998. DECLARE_COMPLETION_ONSTACK(completion);
  999. u32 phy_id = pm80xx_get_local_phy_id(dev);
  1000. struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
  1001. port_id = phy->port->port_id;
  1002. /* 1. Set Device state as Recovery */
  1003. pm8001_dev->setds_completion = &completion;
  1004. PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  1005. pm8001_dev, DS_IN_RECOVERY);
  1006. wait_for_completion(&completion);
  1007. /* 2. Send Phy Control Hard Reset */
  1008. reinit_completion(&completion);
  1009. phy->port_reset_status = PORT_RESET_TMO;
  1010. phy->reset_success = false;
  1011. phy->enable_completion = &completion;
  1012. phy->reset_completion = &completion_reset;
  1013. ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  1014. PHY_HARD_RESET);
  1015. if (ret) {
  1016. phy->enable_completion = NULL;
  1017. phy->reset_completion = NULL;
  1018. goto out;
  1019. }
  1020. /* In the case of the reset timeout/fail we still
  1021. * abort the command at the firmware. The assumption
  1022. * here is that the drive is off doing something so
  1023. * that it's not processing requests, and we want to
  1024. * avoid getting a completion for this and either
  1025. * leaking the task in libsas or losing the race and
  1026. * getting a double free.
  1027. */
  1028. pm8001_dbg(pm8001_ha, MSG,
  1029. "Waiting for local phy ctl\n");
  1030. ret = wait_for_completion_timeout(&completion,
  1031. PM8001_TASK_TIMEOUT * HZ);
  1032. if (!ret || !phy->reset_success) {
  1033. phy->enable_completion = NULL;
  1034. phy->reset_completion = NULL;
  1035. } else {
  1036. /* 3. Wait for Port Reset complete or
  1037. * Port reset TMO
  1038. */
  1039. pm8001_dbg(pm8001_ha, MSG,
  1040. "Waiting for Port reset\n");
  1041. ret = wait_for_completion_timeout(
  1042. &completion_reset,
  1043. PM8001_TASK_TIMEOUT * HZ);
  1044. if (!ret)
  1045. phy->reset_completion = NULL;
  1046. WARN_ON(phy->port_reset_status ==
  1047. PORT_RESET_TMO);
  1048. if (phy->port_reset_status == PORT_RESET_TMO) {
  1049. pm8001_dev_gone_notify(dev);
  1050. PM8001_CHIP_DISP->hw_event_ack_req(
  1051. pm8001_ha, 0,
  1052. 0x07, /*HW_EVENT_PHY_DOWN ack*/
  1053. port_id, phy_id, 0, 0);
  1054. goto out;
  1055. }
  1056. }
  1057. /*
  1058. * 4. SATA Abort ALL
  1059. * we wait for the task to be aborted so that the task
  1060. * is removed from the ccb. on success the caller is
  1061. * going to free the task.
  1062. */
  1063. ret = sas_execute_internal_abort_dev(dev, 0, NULL);
  1064. if (ret)
  1065. goto out;
  1066. ret = wait_for_completion_timeout(
  1067. &task->slow_task->completion,
  1068. PM8001_TASK_TIMEOUT * HZ);
  1069. if (!ret)
  1070. goto out;
  1071. /* 5. Set Device State as Operational */
  1072. reinit_completion(&completion);
  1073. pm8001_dev->setds_completion = &completion;
  1074. PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  1075. pm8001_dev, DS_OPERATIONAL);
  1076. wait_for_completion(&completion);
  1077. } else {
  1078. /*
  1079. * Ensure that if we see a completion for the ccb
  1080. * associated with the task which we are trying to
  1081. * abort then we should not touch the sas_task as it
  1082. * may race with libsas freeing it when return here.
  1083. */
  1084. ccb->task = NULL;
  1085. ret = sas_execute_internal_abort_single(dev, tag, 0, NULL);
  1086. }
  1087. rc = TMF_RESP_FUNC_COMPLETE;
  1088. } else if (task->task_proto & SAS_PROTOCOL_SMP) {
  1089. /* SMP */
  1090. rc = sas_execute_internal_abort_single(dev, tag, 0, NULL);
  1091. }
  1092. out:
  1093. spin_lock_irqsave(&task->task_state_lock, flags);
  1094. if (task->slow_task == &slow_task)
  1095. task->slow_task = NULL;
  1096. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1097. if (rc != TMF_RESP_FUNC_COMPLETE)
  1098. pm8001_info(pm8001_ha, "rc= %d\n", rc);
  1099. return rc;
  1100. }
  1101. int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
  1102. {
  1103. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  1104. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  1105. pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
  1106. pm8001_dev->device_id);
  1107. return sas_clear_task_set(dev, lun);
  1108. }
  1109. void pm8001_port_formed(struct asd_sas_phy *sas_phy)
  1110. {
  1111. struct sas_ha_struct *sas_ha = sas_phy->ha;
  1112. struct pm8001_hba_info *pm8001_ha = sas_ha->lldd_ha;
  1113. struct pm8001_phy *phy = sas_phy->lldd_phy;
  1114. struct asd_sas_port *sas_port = sas_phy->port;
  1115. struct pm8001_port *port = phy->port;
  1116. if (!sas_port) {
  1117. pm8001_dbg(pm8001_ha, FAIL, "Received null port\n");
  1118. return;
  1119. }
  1120. sas_port->lldd_port = port;
  1121. }
  1122. void pm8001_setds_completion(struct domain_device *dev)
  1123. {
  1124. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  1125. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  1126. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  1127. if (pm8001_ha->chip_id != chip_8001) {
  1128. pm8001_dev->setds_completion = &completion_setstate;
  1129. PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  1130. pm8001_dev, DS_OPERATIONAL);
  1131. wait_for_completion(&completion_setstate);
  1132. }
  1133. }
  1134. void pm8001_tmf_aborted(struct sas_task *task)
  1135. {
  1136. struct pm8001_ccb_info *ccb = task->lldd_task;
  1137. if (ccb)
  1138. ccb->task = NULL;
  1139. }