pm8001_sas.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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. /**
  43. * pm8001_find_tag - from sas task to find out tag that belongs to this task
  44. * @task: the task sent to the LLDD
  45. * @tag: the found tag associated with the task
  46. */
  47. static int pm8001_find_tag(struct sas_task *task, u32 *tag)
  48. {
  49. if (task->lldd_task) {
  50. struct pm8001_ccb_info *ccb;
  51. ccb = task->lldd_task;
  52. *tag = ccb->ccb_tag;
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. /**
  58. * pm8001_tag_free - free the no more needed tag
  59. * @pm8001_ha: our hba struct
  60. * @tag: the found tag associated with the task
  61. */
  62. void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
  63. {
  64. void *bitmap = pm8001_ha->tags;
  65. clear_bit(tag, bitmap);
  66. }
  67. /**
  68. * pm8001_tag_alloc - allocate a empty tag for task used.
  69. * @pm8001_ha: our hba struct
  70. * @tag_out: the found empty tag .
  71. */
  72. inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
  73. {
  74. unsigned int tag;
  75. void *bitmap = pm8001_ha->tags;
  76. unsigned long flags;
  77. spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
  78. tag = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
  79. if (tag >= pm8001_ha->tags_num) {
  80. spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
  81. return -SAS_QUEUE_FULL;
  82. }
  83. set_bit(tag, bitmap);
  84. spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
  85. *tag_out = tag;
  86. return 0;
  87. }
  88. void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
  89. {
  90. int i;
  91. for (i = 0; i < pm8001_ha->tags_num; ++i)
  92. pm8001_tag_free(pm8001_ha, i);
  93. }
  94. /**
  95. * pm8001_mem_alloc - allocate memory for pm8001.
  96. * @pdev: pci device.
  97. * @virt_addr: the allocated virtual address
  98. * @pphys_addr_hi: the physical address high byte address.
  99. * @pphys_addr_lo: the physical address low byte address.
  100. * @mem_size: memory size.
  101. */
  102. int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
  103. dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
  104. u32 *pphys_addr_lo, u32 mem_size, u32 align)
  105. {
  106. caddr_t mem_virt_alloc;
  107. dma_addr_t mem_dma_handle;
  108. u64 phys_align;
  109. u64 align_offset = 0;
  110. if (align)
  111. align_offset = (dma_addr_t)align - 1;
  112. mem_virt_alloc = pci_zalloc_consistent(pdev, mem_size + align,
  113. &mem_dma_handle);
  114. if (!mem_virt_alloc) {
  115. pm8001_printk("memory allocation error\n");
  116. return -1;
  117. }
  118. *pphys_addr = mem_dma_handle;
  119. phys_align = (*pphys_addr + align_offset) & ~align_offset;
  120. *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
  121. *pphys_addr_hi = upper_32_bits(phys_align);
  122. *pphys_addr_lo = lower_32_bits(phys_align);
  123. return 0;
  124. }
  125. /**
  126. * pm8001_find_ha_by_dev - from domain device which come from sas layer to
  127. * find out our hba struct.
  128. * @dev: the domain device which from sas layer.
  129. */
  130. static
  131. struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
  132. {
  133. struct sas_ha_struct *sha = dev->port->ha;
  134. struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
  135. return pm8001_ha;
  136. }
  137. /**
  138. * pm8001_phy_control - this function should be registered to
  139. * sas_domain_function_template to provide libsas used, note: this is just
  140. * control the HBA phy rather than other expander phy if you want control
  141. * other phy, you should use SMP command.
  142. * @sas_phy: which phy in HBA phys.
  143. * @func: the operation.
  144. * @funcdata: always NULL.
  145. */
  146. int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
  147. void *funcdata)
  148. {
  149. int rc = 0, phy_id = sas_phy->id;
  150. struct pm8001_hba_info *pm8001_ha = NULL;
  151. struct sas_phy_linkrates *rates;
  152. DECLARE_COMPLETION_ONSTACK(completion);
  153. unsigned long flags;
  154. pm8001_ha = sas_phy->ha->lldd_ha;
  155. pm8001_ha->phy[phy_id].enable_completion = &completion;
  156. switch (func) {
  157. case PHY_FUNC_SET_LINK_RATE:
  158. rates = funcdata;
  159. if (rates->minimum_linkrate) {
  160. pm8001_ha->phy[phy_id].minimum_linkrate =
  161. rates->minimum_linkrate;
  162. }
  163. if (rates->maximum_linkrate) {
  164. pm8001_ha->phy[phy_id].maximum_linkrate =
  165. rates->maximum_linkrate;
  166. }
  167. if (pm8001_ha->phy[phy_id].phy_state == 0) {
  168. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
  169. wait_for_completion(&completion);
  170. }
  171. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  172. PHY_LINK_RESET);
  173. break;
  174. case PHY_FUNC_HARD_RESET:
  175. if (pm8001_ha->phy[phy_id].phy_state == 0) {
  176. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
  177. wait_for_completion(&completion);
  178. }
  179. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  180. PHY_HARD_RESET);
  181. break;
  182. case PHY_FUNC_LINK_RESET:
  183. if (pm8001_ha->phy[phy_id].phy_state == 0) {
  184. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
  185. wait_for_completion(&completion);
  186. }
  187. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  188. PHY_LINK_RESET);
  189. break;
  190. case PHY_FUNC_RELEASE_SPINUP_HOLD:
  191. PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
  192. PHY_LINK_RESET);
  193. break;
  194. case PHY_FUNC_DISABLE:
  195. PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
  196. break;
  197. case PHY_FUNC_GET_EVENTS:
  198. spin_lock_irqsave(&pm8001_ha->lock, flags);
  199. if (pm8001_ha->chip_id == chip_8001) {
  200. if (-1 == pm8001_bar4_shift(pm8001_ha,
  201. (phy_id < 4) ? 0x30000 : 0x40000)) {
  202. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  203. return -EINVAL;
  204. }
  205. }
  206. {
  207. struct sas_phy *phy = sas_phy->phy;
  208. uint32_t *qp = (uint32_t *)(((char *)
  209. pm8001_ha->io_mem[2].memvirtaddr)
  210. + 0x1034 + (0x4000 * (phy_id & 3)));
  211. phy->invalid_dword_count = qp[0];
  212. phy->running_disparity_error_count = qp[1];
  213. phy->loss_of_dword_sync_count = qp[3];
  214. phy->phy_reset_problem_count = qp[4];
  215. }
  216. if (pm8001_ha->chip_id == chip_8001)
  217. pm8001_bar4_shift(pm8001_ha, 0);
  218. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  219. return 0;
  220. default:
  221. rc = -EOPNOTSUPP;
  222. }
  223. msleep(300);
  224. return rc;
  225. }
  226. /**
  227. * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
  228. * command to HBA.
  229. * @shost: the scsi host data.
  230. */
  231. void pm8001_scan_start(struct Scsi_Host *shost)
  232. {
  233. int i;
  234. struct pm8001_hba_info *pm8001_ha;
  235. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  236. pm8001_ha = sha->lldd_ha;
  237. /* SAS_RE_INITIALIZATION not available in SPCv/ve */
  238. if (pm8001_ha->chip_id == chip_8001)
  239. PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
  240. for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
  241. PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
  242. }
  243. int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
  244. {
  245. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  246. /* give the phy enabling interrupt event time to come in (1s
  247. * is empirically about all it takes) */
  248. if (time < HZ)
  249. return 0;
  250. /* Wait for discovery to finish */
  251. sas_drain_work(ha);
  252. return 1;
  253. }
  254. /**
  255. * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
  256. * @pm8001_ha: our hba card information
  257. * @ccb: the ccb which attached to smp task
  258. */
  259. static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
  260. struct pm8001_ccb_info *ccb)
  261. {
  262. return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
  263. }
  264. u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
  265. {
  266. struct ata_queued_cmd *qc = task->uldd_task;
  267. if (qc) {
  268. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  269. qc->tf.command == ATA_CMD_FPDMA_READ) {
  270. *tag = qc->tag;
  271. return 1;
  272. }
  273. }
  274. return 0;
  275. }
  276. /**
  277. * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
  278. * @pm8001_ha: our hba card information
  279. * @ccb: the ccb which attached to sata task
  280. */
  281. static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
  282. struct pm8001_ccb_info *ccb)
  283. {
  284. return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
  285. }
  286. /**
  287. * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
  288. * @pm8001_ha: our hba card information
  289. * @ccb: the ccb which attached to TM
  290. * @tmf: the task management IU
  291. */
  292. static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
  293. struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
  294. {
  295. return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
  296. }
  297. /**
  298. * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
  299. * @pm8001_ha: our hba card information
  300. * @ccb: the ccb which attached to ssp task
  301. */
  302. static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
  303. struct pm8001_ccb_info *ccb)
  304. {
  305. return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
  306. }
  307. /* Find the local port id that's attached to this device */
  308. static int sas_find_local_port_id(struct domain_device *dev)
  309. {
  310. struct domain_device *pdev = dev->parent;
  311. /* Directly attached device */
  312. if (!pdev)
  313. return dev->port->id;
  314. while (pdev) {
  315. struct domain_device *pdev_p = pdev->parent;
  316. if (!pdev_p)
  317. return pdev->port->id;
  318. pdev = pdev->parent;
  319. }
  320. return 0;
  321. }
  322. /**
  323. * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
  324. * @task: the task to be execute.
  325. * @num: if can_queue great than 1, the task can be queued up. for SMP task,
  326. * we always execute one one time.
  327. * @gfp_flags: gfp_flags.
  328. * @is_tmf: if it is task management task.
  329. * @tmf: the task management IU
  330. */
  331. #define DEV_IS_GONE(pm8001_dev) \
  332. ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
  333. static int pm8001_task_exec(struct sas_task *task,
  334. gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
  335. {
  336. struct domain_device *dev = task->dev;
  337. struct pm8001_hba_info *pm8001_ha;
  338. struct pm8001_device *pm8001_dev;
  339. struct pm8001_port *port = NULL;
  340. struct sas_task *t = task;
  341. struct pm8001_ccb_info *ccb;
  342. u32 tag = 0xdeadbeef, rc, n_elem = 0;
  343. unsigned long flags = 0;
  344. if (!dev->port) {
  345. struct task_status_struct *tsm = &t->task_status;
  346. tsm->resp = SAS_TASK_UNDELIVERED;
  347. tsm->stat = SAS_PHY_DOWN;
  348. if (dev->dev_type != SAS_SATA_DEV)
  349. t->task_done(t);
  350. return 0;
  351. }
  352. pm8001_ha = pm8001_find_ha_by_dev(task->dev);
  353. PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
  354. spin_lock_irqsave(&pm8001_ha->lock, flags);
  355. do {
  356. dev = t->dev;
  357. pm8001_dev = dev->lldd_dev;
  358. port = &pm8001_ha->port[sas_find_local_port_id(dev)];
  359. if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
  360. if (sas_protocol_ata(t->task_proto)) {
  361. struct task_status_struct *ts = &t->task_status;
  362. ts->resp = SAS_TASK_UNDELIVERED;
  363. ts->stat = SAS_PHY_DOWN;
  364. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  365. t->task_done(t);
  366. spin_lock_irqsave(&pm8001_ha->lock, flags);
  367. continue;
  368. } else {
  369. struct task_status_struct *ts = &t->task_status;
  370. ts->resp = SAS_TASK_UNDELIVERED;
  371. ts->stat = SAS_PHY_DOWN;
  372. t->task_done(t);
  373. continue;
  374. }
  375. }
  376. rc = pm8001_tag_alloc(pm8001_ha, &tag);
  377. if (rc)
  378. goto err_out;
  379. ccb = &pm8001_ha->ccb_info[tag];
  380. if (!sas_protocol_ata(t->task_proto)) {
  381. if (t->num_scatter) {
  382. n_elem = dma_map_sg(pm8001_ha->dev,
  383. t->scatter,
  384. t->num_scatter,
  385. t->data_dir);
  386. if (!n_elem) {
  387. rc = -ENOMEM;
  388. goto err_out_tag;
  389. }
  390. }
  391. } else {
  392. n_elem = t->num_scatter;
  393. }
  394. t->lldd_task = ccb;
  395. ccb->n_elem = n_elem;
  396. ccb->ccb_tag = tag;
  397. ccb->task = t;
  398. ccb->device = pm8001_dev;
  399. switch (t->task_proto) {
  400. case SAS_PROTOCOL_SMP:
  401. rc = pm8001_task_prep_smp(pm8001_ha, ccb);
  402. break;
  403. case SAS_PROTOCOL_SSP:
  404. if (is_tmf)
  405. rc = pm8001_task_prep_ssp_tm(pm8001_ha,
  406. ccb, tmf);
  407. else
  408. rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
  409. break;
  410. case SAS_PROTOCOL_SATA:
  411. case SAS_PROTOCOL_STP:
  412. rc = pm8001_task_prep_ata(pm8001_ha, ccb);
  413. break;
  414. default:
  415. dev_printk(KERN_ERR, pm8001_ha->dev,
  416. "unknown sas_task proto: 0x%x\n",
  417. t->task_proto);
  418. rc = -EINVAL;
  419. break;
  420. }
  421. if (rc) {
  422. PM8001_IO_DBG(pm8001_ha,
  423. pm8001_printk("rc is %x\n", rc));
  424. goto err_out_tag;
  425. }
  426. /* TODO: select normal or high priority */
  427. spin_lock(&t->task_state_lock);
  428. t->task_state_flags |= SAS_TASK_AT_INITIATOR;
  429. spin_unlock(&t->task_state_lock);
  430. pm8001_dev->running_req++;
  431. } while (0);
  432. rc = 0;
  433. goto out_done;
  434. err_out_tag:
  435. pm8001_tag_free(pm8001_ha, tag);
  436. err_out:
  437. dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
  438. if (!sas_protocol_ata(t->task_proto))
  439. if (n_elem)
  440. dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
  441. t->data_dir);
  442. out_done:
  443. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  444. return rc;
  445. }
  446. /**
  447. * pm8001_queue_command - register for upper layer used, all IO commands sent
  448. * to HBA are from this interface.
  449. * @task: the task to be execute.
  450. * @gfp_flags: gfp_flags
  451. */
  452. int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
  453. {
  454. return pm8001_task_exec(task, gfp_flags, 0, NULL);
  455. }
  456. /**
  457. * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
  458. * @pm8001_ha: our hba card information
  459. * @ccb: the ccb which attached to ssp task
  460. * @task: the task to be free.
  461. * @ccb_idx: ccb index.
  462. */
  463. void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
  464. struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
  465. {
  466. if (!ccb->task)
  467. return;
  468. if (!sas_protocol_ata(task->task_proto))
  469. if (ccb->n_elem)
  470. dma_unmap_sg(pm8001_ha->dev, task->scatter,
  471. task->num_scatter, task->data_dir);
  472. switch (task->task_proto) {
  473. case SAS_PROTOCOL_SMP:
  474. dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
  475. PCI_DMA_FROMDEVICE);
  476. dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
  477. PCI_DMA_TODEVICE);
  478. break;
  479. case SAS_PROTOCOL_SATA:
  480. case SAS_PROTOCOL_STP:
  481. case SAS_PROTOCOL_SSP:
  482. default:
  483. /* do nothing */
  484. break;
  485. }
  486. task->lldd_task = NULL;
  487. ccb->task = NULL;
  488. ccb->ccb_tag = 0xFFFFFFFF;
  489. ccb->open_retry = 0;
  490. pm8001_tag_free(pm8001_ha, ccb_idx);
  491. }
  492. /**
  493. * pm8001_alloc_dev - find a empty pm8001_device
  494. * @pm8001_ha: our hba card information
  495. */
  496. struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
  497. {
  498. u32 dev;
  499. for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
  500. if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
  501. pm8001_ha->devices[dev].id = dev;
  502. return &pm8001_ha->devices[dev];
  503. }
  504. }
  505. if (dev == PM8001_MAX_DEVICES) {
  506. PM8001_FAIL_DBG(pm8001_ha,
  507. pm8001_printk("max support %d devices, ignore ..\n",
  508. PM8001_MAX_DEVICES));
  509. }
  510. return NULL;
  511. }
  512. /**
  513. * pm8001_find_dev - find a matching pm8001_device
  514. * @pm8001_ha: our hba card information
  515. */
  516. struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
  517. u32 device_id)
  518. {
  519. u32 dev;
  520. for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
  521. if (pm8001_ha->devices[dev].device_id == device_id)
  522. return &pm8001_ha->devices[dev];
  523. }
  524. if (dev == PM8001_MAX_DEVICES) {
  525. PM8001_FAIL_DBG(pm8001_ha, pm8001_printk("NO MATCHING "
  526. "DEVICE FOUND !!!\n"));
  527. }
  528. return NULL;
  529. }
  530. static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
  531. {
  532. u32 id = pm8001_dev->id;
  533. memset(pm8001_dev, 0, sizeof(*pm8001_dev));
  534. pm8001_dev->id = id;
  535. pm8001_dev->dev_type = SAS_PHY_UNUSED;
  536. pm8001_dev->device_id = PM8001_MAX_DEVICES;
  537. pm8001_dev->sas_device = NULL;
  538. }
  539. /**
  540. * pm8001_dev_found_notify - libsas notify a device is found.
  541. * @dev: the device structure which sas layer used.
  542. *
  543. * when libsas find a sas domain device, it should tell the LLDD that
  544. * device is found, and then LLDD register this device to HBA firmware
  545. * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
  546. * device ID(according to device's sas address) and returned it to LLDD. From
  547. * now on, we communicate with HBA FW with the device ID which HBA assigned
  548. * rather than sas address. it is the necessary step for our HBA but it is
  549. * the optional for other HBA driver.
  550. */
  551. static int pm8001_dev_found_notify(struct domain_device *dev)
  552. {
  553. unsigned long flags = 0;
  554. int res = 0;
  555. struct pm8001_hba_info *pm8001_ha = NULL;
  556. struct domain_device *parent_dev = dev->parent;
  557. struct pm8001_device *pm8001_device;
  558. DECLARE_COMPLETION_ONSTACK(completion);
  559. u32 flag = 0;
  560. pm8001_ha = pm8001_find_ha_by_dev(dev);
  561. spin_lock_irqsave(&pm8001_ha->lock, flags);
  562. pm8001_device = pm8001_alloc_dev(pm8001_ha);
  563. if (!pm8001_device) {
  564. res = -1;
  565. goto found_out;
  566. }
  567. pm8001_device->sas_device = dev;
  568. dev->lldd_dev = pm8001_device;
  569. pm8001_device->dev_type = dev->dev_type;
  570. pm8001_device->dcompletion = &completion;
  571. if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
  572. int phy_id;
  573. struct ex_phy *phy;
  574. for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
  575. phy_id++) {
  576. phy = &parent_dev->ex_dev.ex_phy[phy_id];
  577. if (SAS_ADDR(phy->attached_sas_addr)
  578. == SAS_ADDR(dev->sas_addr)) {
  579. pm8001_device->attached_phy = phy_id;
  580. break;
  581. }
  582. }
  583. if (phy_id == parent_dev->ex_dev.num_phys) {
  584. PM8001_FAIL_DBG(pm8001_ha,
  585. pm8001_printk("Error: no attached dev:%016llx"
  586. " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
  587. SAS_ADDR(parent_dev->sas_addr)));
  588. res = -1;
  589. }
  590. } else {
  591. if (dev->dev_type == SAS_SATA_DEV) {
  592. pm8001_device->attached_phy =
  593. dev->rphy->identify.phy_identifier;
  594. flag = 1; /* directly sata*/
  595. }
  596. } /*register this device to HBA*/
  597. PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
  598. PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
  599. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  600. wait_for_completion(&completion);
  601. if (dev->dev_type == SAS_END_DEVICE)
  602. msleep(50);
  603. pm8001_ha->flags = PM8001F_RUN_TIME;
  604. return 0;
  605. found_out:
  606. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  607. return res;
  608. }
  609. int pm8001_dev_found(struct domain_device *dev)
  610. {
  611. return pm8001_dev_found_notify(dev);
  612. }
  613. void pm8001_task_done(struct sas_task *task)
  614. {
  615. if (!del_timer(&task->slow_task->timer))
  616. return;
  617. complete(&task->slow_task->completion);
  618. }
  619. static void pm8001_tmf_timedout(unsigned long data)
  620. {
  621. struct sas_task *task = (struct sas_task *)data;
  622. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  623. complete(&task->slow_task->completion);
  624. }
  625. #define PM8001_TASK_TIMEOUT 20
  626. /**
  627. * pm8001_exec_internal_tmf_task - execute some task management commands.
  628. * @dev: the wanted device.
  629. * @tmf: which task management wanted to be take.
  630. * @para_len: para_len.
  631. * @parameter: ssp task parameter.
  632. *
  633. * when errors or exception happened, we may want to do something, for example
  634. * abort the issued task which result in this execption, it is done by calling
  635. * this function, note it is also with the task execute interface.
  636. */
  637. static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
  638. void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
  639. {
  640. int res, retry;
  641. struct sas_task *task = NULL;
  642. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  643. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  644. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  645. for (retry = 0; retry < 3; retry++) {
  646. task = sas_alloc_slow_task(GFP_KERNEL);
  647. if (!task)
  648. return -ENOMEM;
  649. task->dev = dev;
  650. task->task_proto = dev->tproto;
  651. memcpy(&task->ssp_task, parameter, para_len);
  652. task->task_done = pm8001_task_done;
  653. task->slow_task->timer.data = (unsigned long)task;
  654. task->slow_task->timer.function = pm8001_tmf_timedout;
  655. task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
  656. add_timer(&task->slow_task->timer);
  657. res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf);
  658. if (res) {
  659. del_timer(&task->slow_task->timer);
  660. PM8001_FAIL_DBG(pm8001_ha,
  661. pm8001_printk("Executing internal task "
  662. "failed\n"));
  663. goto ex_err;
  664. }
  665. wait_for_completion(&task->slow_task->completion);
  666. if (pm8001_ha->chip_id != chip_8001) {
  667. pm8001_dev->setds_completion = &completion_setstate;
  668. PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  669. pm8001_dev, 0x01);
  670. wait_for_completion(&completion_setstate);
  671. }
  672. res = -TMF_RESP_FUNC_FAILED;
  673. /* Even TMF timed out, return direct. */
  674. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  675. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  676. PM8001_FAIL_DBG(pm8001_ha,
  677. pm8001_printk("TMF task[%x]timeout.\n",
  678. tmf->tmf));
  679. goto ex_err;
  680. }
  681. }
  682. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  683. task->task_status.stat == SAM_STAT_GOOD) {
  684. res = TMF_RESP_FUNC_COMPLETE;
  685. break;
  686. }
  687. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  688. task->task_status.stat == SAS_DATA_UNDERRUN) {
  689. /* no error, but return the number of bytes of
  690. * underrun */
  691. res = task->task_status.residual;
  692. break;
  693. }
  694. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  695. task->task_status.stat == SAS_DATA_OVERRUN) {
  696. PM8001_FAIL_DBG(pm8001_ha,
  697. pm8001_printk("Blocked task error.\n"));
  698. res = -EMSGSIZE;
  699. break;
  700. } else {
  701. PM8001_EH_DBG(pm8001_ha,
  702. pm8001_printk(" Task to dev %016llx response:"
  703. "0x%x status 0x%x\n",
  704. SAS_ADDR(dev->sas_addr),
  705. task->task_status.resp,
  706. task->task_status.stat));
  707. sas_free_task(task);
  708. task = NULL;
  709. }
  710. }
  711. ex_err:
  712. BUG_ON(retry == 3 && task != NULL);
  713. sas_free_task(task);
  714. return res;
  715. }
  716. static int
  717. pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
  718. struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
  719. u32 task_tag)
  720. {
  721. int res, retry;
  722. u32 ccb_tag;
  723. struct pm8001_ccb_info *ccb;
  724. struct sas_task *task = NULL;
  725. for (retry = 0; retry < 3; retry++) {
  726. task = sas_alloc_slow_task(GFP_KERNEL);
  727. if (!task)
  728. return -ENOMEM;
  729. task->dev = dev;
  730. task->task_proto = dev->tproto;
  731. task->task_done = pm8001_task_done;
  732. task->slow_task->timer.data = (unsigned long)task;
  733. task->slow_task->timer.function = pm8001_tmf_timedout;
  734. task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
  735. add_timer(&task->slow_task->timer);
  736. res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
  737. if (res)
  738. return res;
  739. ccb = &pm8001_ha->ccb_info[ccb_tag];
  740. ccb->device = pm8001_dev;
  741. ccb->ccb_tag = ccb_tag;
  742. ccb->task = task;
  743. ccb->n_elem = 0;
  744. res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
  745. pm8001_dev, flag, task_tag, ccb_tag);
  746. if (res) {
  747. del_timer(&task->slow_task->timer);
  748. PM8001_FAIL_DBG(pm8001_ha,
  749. pm8001_printk("Executing internal task "
  750. "failed\n"));
  751. goto ex_err;
  752. }
  753. wait_for_completion(&task->slow_task->completion);
  754. res = TMF_RESP_FUNC_FAILED;
  755. /* Even TMF timed out, return direct. */
  756. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  757. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  758. PM8001_FAIL_DBG(pm8001_ha,
  759. pm8001_printk("TMF task timeout.\n"));
  760. goto ex_err;
  761. }
  762. }
  763. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  764. task->task_status.stat == SAM_STAT_GOOD) {
  765. res = TMF_RESP_FUNC_COMPLETE;
  766. break;
  767. } else {
  768. PM8001_EH_DBG(pm8001_ha,
  769. pm8001_printk(" Task to dev %016llx response: "
  770. "0x%x status 0x%x\n",
  771. SAS_ADDR(dev->sas_addr),
  772. task->task_status.resp,
  773. task->task_status.stat));
  774. sas_free_task(task);
  775. task = NULL;
  776. }
  777. }
  778. ex_err:
  779. BUG_ON(retry == 3 && task != NULL);
  780. sas_free_task(task);
  781. return res;
  782. }
  783. /**
  784. * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
  785. * @dev: the device structure which sas layer used.
  786. */
  787. static void pm8001_dev_gone_notify(struct domain_device *dev)
  788. {
  789. unsigned long flags = 0;
  790. struct pm8001_hba_info *pm8001_ha;
  791. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  792. pm8001_ha = pm8001_find_ha_by_dev(dev);
  793. spin_lock_irqsave(&pm8001_ha->lock, flags);
  794. if (pm8001_dev) {
  795. u32 device_id = pm8001_dev->device_id;
  796. PM8001_DISC_DBG(pm8001_ha,
  797. pm8001_printk("found dev[%d:%x] is gone.\n",
  798. pm8001_dev->device_id, pm8001_dev->dev_type));
  799. if (pm8001_dev->running_req) {
  800. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  801. pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  802. dev, 1, 0);
  803. spin_lock_irqsave(&pm8001_ha->lock, flags);
  804. }
  805. PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
  806. pm8001_free_dev(pm8001_dev);
  807. } else {
  808. PM8001_DISC_DBG(pm8001_ha,
  809. pm8001_printk("Found dev has gone.\n"));
  810. }
  811. dev->lldd_dev = NULL;
  812. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  813. }
  814. void pm8001_dev_gone(struct domain_device *dev)
  815. {
  816. pm8001_dev_gone_notify(dev);
  817. }
  818. static int pm8001_issue_ssp_tmf(struct domain_device *dev,
  819. u8 *lun, struct pm8001_tmf_task *tmf)
  820. {
  821. struct sas_ssp_task ssp_task;
  822. if (!(dev->tproto & SAS_PROTOCOL_SSP))
  823. return TMF_RESP_FUNC_ESUPP;
  824. strncpy((u8 *)&ssp_task.LUN, lun, 8);
  825. return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
  826. tmf);
  827. }
  828. /* retry commands by ha, by task and/or by device */
  829. void pm8001_open_reject_retry(
  830. struct pm8001_hba_info *pm8001_ha,
  831. struct sas_task *task_to_close,
  832. struct pm8001_device *device_to_close)
  833. {
  834. int i;
  835. unsigned long flags;
  836. if (pm8001_ha == NULL)
  837. return;
  838. spin_lock_irqsave(&pm8001_ha->lock, flags);
  839. for (i = 0; i < PM8001_MAX_CCB; i++) {
  840. struct sas_task *task;
  841. struct task_status_struct *ts;
  842. struct pm8001_device *pm8001_dev;
  843. unsigned long flags1;
  844. u32 tag;
  845. struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
  846. pm8001_dev = ccb->device;
  847. if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
  848. continue;
  849. if (!device_to_close) {
  850. uintptr_t d = (uintptr_t)pm8001_dev
  851. - (uintptr_t)&pm8001_ha->devices;
  852. if (((d % sizeof(*pm8001_dev)) != 0)
  853. || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
  854. continue;
  855. } else if (pm8001_dev != device_to_close)
  856. continue;
  857. tag = ccb->ccb_tag;
  858. if (!tag || (tag == 0xFFFFFFFF))
  859. continue;
  860. task = ccb->task;
  861. if (!task || !task->task_done)
  862. continue;
  863. if (task_to_close && (task != task_to_close))
  864. continue;
  865. ts = &task->task_status;
  866. ts->resp = SAS_TASK_COMPLETE;
  867. /* Force the midlayer to retry */
  868. ts->stat = SAS_OPEN_REJECT;
  869. ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
  870. if (pm8001_dev)
  871. pm8001_dev->running_req--;
  872. spin_lock_irqsave(&task->task_state_lock, flags1);
  873. task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
  874. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  875. task->task_state_flags |= SAS_TASK_STATE_DONE;
  876. if (unlikely((task->task_state_flags
  877. & SAS_TASK_STATE_ABORTED))) {
  878. spin_unlock_irqrestore(&task->task_state_lock,
  879. flags1);
  880. pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
  881. } else {
  882. spin_unlock_irqrestore(&task->task_state_lock,
  883. flags1);
  884. pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
  885. mb();/* in order to force CPU ordering */
  886. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  887. task->task_done(task);
  888. spin_lock_irqsave(&pm8001_ha->lock, flags);
  889. }
  890. }
  891. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  892. }
  893. /**
  894. * Standard mandates link reset for ATA (type 0) and hard reset for
  895. * SSP (type 1) , only for RECOVERY
  896. */
  897. int pm8001_I_T_nexus_reset(struct domain_device *dev)
  898. {
  899. int rc = TMF_RESP_FUNC_FAILED;
  900. struct pm8001_device *pm8001_dev;
  901. struct pm8001_hba_info *pm8001_ha;
  902. struct sas_phy *phy;
  903. if (!dev || !dev->lldd_dev)
  904. return -ENODEV;
  905. pm8001_dev = dev->lldd_dev;
  906. pm8001_ha = pm8001_find_ha_by_dev(dev);
  907. phy = sas_get_local_phy(dev);
  908. if (dev_is_sata(dev)) {
  909. if (scsi_is_sas_phy_local(phy)) {
  910. rc = 0;
  911. goto out;
  912. }
  913. rc = sas_phy_reset(phy, 1);
  914. if (rc) {
  915. PM8001_EH_DBG(pm8001_ha,
  916. pm8001_printk("phy reset failed for device %x\n"
  917. "with rc %d\n", pm8001_dev->device_id, rc));
  918. rc = TMF_RESP_FUNC_FAILED;
  919. goto out;
  920. }
  921. msleep(2000);
  922. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  923. dev, 1, 0);
  924. if (rc) {
  925. PM8001_EH_DBG(pm8001_ha,
  926. pm8001_printk("task abort failed %x\n"
  927. "with rc %d\n", pm8001_dev->device_id, rc));
  928. rc = TMF_RESP_FUNC_FAILED;
  929. }
  930. } else {
  931. rc = sas_phy_reset(phy, 1);
  932. msleep(2000);
  933. }
  934. PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
  935. pm8001_dev->device_id, rc));
  936. out:
  937. sas_put_local_phy(phy);
  938. return rc;
  939. }
  940. /*
  941. * This function handle the IT_NEXUS_XXX event or completion
  942. * status code for SSP/SATA/SMP I/O request.
  943. */
  944. int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
  945. {
  946. int rc = TMF_RESP_FUNC_FAILED;
  947. struct pm8001_device *pm8001_dev;
  948. struct pm8001_hba_info *pm8001_ha;
  949. struct sas_phy *phy;
  950. u32 device_id = 0;
  951. if (!dev || !dev->lldd_dev)
  952. return -1;
  953. pm8001_dev = dev->lldd_dev;
  954. device_id = pm8001_dev->device_id;
  955. pm8001_ha = pm8001_find_ha_by_dev(dev);
  956. PM8001_EH_DBG(pm8001_ha,
  957. pm8001_printk("I_T_Nexus handler invoked !!"));
  958. phy = sas_get_local_phy(dev);
  959. if (dev_is_sata(dev)) {
  960. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  961. if (scsi_is_sas_phy_local(phy)) {
  962. rc = 0;
  963. goto out;
  964. }
  965. /* send internal ssp/sata/smp abort command to FW */
  966. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  967. dev, 1, 0);
  968. msleep(100);
  969. /* deregister the target device */
  970. pm8001_dev_gone_notify(dev);
  971. msleep(200);
  972. /*send phy reset to hard reset target */
  973. rc = sas_phy_reset(phy, 1);
  974. msleep(2000);
  975. pm8001_dev->setds_completion = &completion_setstate;
  976. wait_for_completion(&completion_setstate);
  977. } else {
  978. /* send internal ssp/sata/smp abort command to FW */
  979. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  980. dev, 1, 0);
  981. msleep(100);
  982. /* deregister the target device */
  983. pm8001_dev_gone_notify(dev);
  984. msleep(200);
  985. /*send phy reset to hard reset target */
  986. rc = sas_phy_reset(phy, 1);
  987. msleep(2000);
  988. }
  989. PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
  990. pm8001_dev->device_id, rc));
  991. out:
  992. sas_put_local_phy(phy);
  993. return rc;
  994. }
  995. /* mandatory SAM-3, the task reset the specified LUN*/
  996. int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
  997. {
  998. int rc = TMF_RESP_FUNC_FAILED;
  999. struct pm8001_tmf_task tmf_task;
  1000. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  1001. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  1002. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  1003. if (dev_is_sata(dev)) {
  1004. struct sas_phy *phy = sas_get_local_phy(dev);
  1005. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  1006. dev, 1, 0);
  1007. rc = sas_phy_reset(phy, 1);
  1008. sas_put_local_phy(phy);
  1009. pm8001_dev->setds_completion = &completion_setstate;
  1010. rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  1011. pm8001_dev, 0x01);
  1012. wait_for_completion(&completion_setstate);
  1013. } else {
  1014. tmf_task.tmf = TMF_LU_RESET;
  1015. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1016. }
  1017. /* If failed, fall-through I_T_Nexus reset */
  1018. PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
  1019. pm8001_dev->device_id, rc));
  1020. return rc;
  1021. }
  1022. /* optional SAM-3 */
  1023. int pm8001_query_task(struct sas_task *task)
  1024. {
  1025. u32 tag = 0xdeadbeef;
  1026. int i = 0;
  1027. struct scsi_lun lun;
  1028. struct pm8001_tmf_task tmf_task;
  1029. int rc = TMF_RESP_FUNC_FAILED;
  1030. if (unlikely(!task || !task->lldd_task || !task->dev))
  1031. return rc;
  1032. if (task->task_proto & SAS_PROTOCOL_SSP) {
  1033. struct scsi_cmnd *cmnd = task->uldd_task;
  1034. struct domain_device *dev = task->dev;
  1035. struct pm8001_hba_info *pm8001_ha =
  1036. pm8001_find_ha_by_dev(dev);
  1037. int_to_scsilun(cmnd->device->lun, &lun);
  1038. rc = pm8001_find_tag(task, &tag);
  1039. if (rc == 0) {
  1040. rc = TMF_RESP_FUNC_FAILED;
  1041. return rc;
  1042. }
  1043. PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
  1044. for (i = 0; i < 16; i++)
  1045. printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
  1046. printk(KERN_INFO "]\n");
  1047. tmf_task.tmf = TMF_QUERY_TASK;
  1048. tmf_task.tag_of_task_to_be_managed = tag;
  1049. rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1050. switch (rc) {
  1051. /* The task is still in Lun, release it then */
  1052. case TMF_RESP_FUNC_SUCC:
  1053. PM8001_EH_DBG(pm8001_ha,
  1054. pm8001_printk("The task is still in Lun\n"));
  1055. break;
  1056. /* The task is not in Lun or failed, reset the phy */
  1057. case TMF_RESP_FUNC_FAILED:
  1058. case TMF_RESP_FUNC_COMPLETE:
  1059. PM8001_EH_DBG(pm8001_ha,
  1060. pm8001_printk("The task is not in Lun or failed,"
  1061. " reset the phy\n"));
  1062. break;
  1063. }
  1064. }
  1065. pm8001_printk(":rc= %d\n", rc);
  1066. return rc;
  1067. }
  1068. /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
  1069. int pm8001_abort_task(struct sas_task *task)
  1070. {
  1071. unsigned long flags;
  1072. u32 tag = 0xdeadbeef;
  1073. u32 device_id;
  1074. struct domain_device *dev ;
  1075. struct pm8001_hba_info *pm8001_ha = NULL;
  1076. struct pm8001_ccb_info *ccb;
  1077. struct scsi_lun lun;
  1078. struct pm8001_device *pm8001_dev;
  1079. struct pm8001_tmf_task tmf_task;
  1080. int rc = TMF_RESP_FUNC_FAILED;
  1081. if (unlikely(!task || !task->lldd_task || !task->dev))
  1082. return rc;
  1083. spin_lock_irqsave(&task->task_state_lock, flags);
  1084. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  1085. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1086. rc = TMF_RESP_FUNC_COMPLETE;
  1087. goto out;
  1088. }
  1089. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1090. if (task->task_proto & SAS_PROTOCOL_SSP) {
  1091. struct scsi_cmnd *cmnd = task->uldd_task;
  1092. dev = task->dev;
  1093. ccb = task->lldd_task;
  1094. pm8001_dev = dev->lldd_dev;
  1095. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1096. int_to_scsilun(cmnd->device->lun, &lun);
  1097. rc = pm8001_find_tag(task, &tag);
  1098. if (rc == 0) {
  1099. printk(KERN_INFO "No such tag in %s\n", __func__);
  1100. rc = TMF_RESP_FUNC_FAILED;
  1101. return rc;
  1102. }
  1103. device_id = pm8001_dev->device_id;
  1104. PM8001_EH_DBG(pm8001_ha,
  1105. pm8001_printk("abort io to deviceid= %d\n", device_id));
  1106. tmf_task.tmf = TMF_ABORT_TASK;
  1107. tmf_task.tag_of_task_to_be_managed = tag;
  1108. rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1109. pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1110. pm8001_dev->sas_device, 0, tag);
  1111. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  1112. task->task_proto & SAS_PROTOCOL_STP) {
  1113. dev = task->dev;
  1114. pm8001_dev = dev->lldd_dev;
  1115. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1116. rc = pm8001_find_tag(task, &tag);
  1117. if (rc == 0) {
  1118. printk(KERN_INFO "No such tag in %s\n", __func__);
  1119. rc = TMF_RESP_FUNC_FAILED;
  1120. return rc;
  1121. }
  1122. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1123. pm8001_dev->sas_device, 0, tag);
  1124. } else if (task->task_proto & SAS_PROTOCOL_SMP) {
  1125. /* SMP */
  1126. dev = task->dev;
  1127. pm8001_dev = dev->lldd_dev;
  1128. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1129. rc = pm8001_find_tag(task, &tag);
  1130. if (rc == 0) {
  1131. printk(KERN_INFO "No such tag in %s\n", __func__);
  1132. rc = TMF_RESP_FUNC_FAILED;
  1133. return rc;
  1134. }
  1135. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1136. pm8001_dev->sas_device, 0, tag);
  1137. }
  1138. out:
  1139. if (rc != TMF_RESP_FUNC_COMPLETE)
  1140. pm8001_printk("rc= %d\n", rc);
  1141. return rc;
  1142. }
  1143. int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
  1144. {
  1145. int rc = TMF_RESP_FUNC_FAILED;
  1146. struct pm8001_tmf_task tmf_task;
  1147. tmf_task.tmf = TMF_ABORT_TASK_SET;
  1148. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1149. return rc;
  1150. }
  1151. int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
  1152. {
  1153. int rc = TMF_RESP_FUNC_FAILED;
  1154. struct pm8001_tmf_task tmf_task;
  1155. tmf_task.tmf = TMF_CLEAR_ACA;
  1156. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1157. return rc;
  1158. }
  1159. int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
  1160. {
  1161. int rc = TMF_RESP_FUNC_FAILED;
  1162. struct pm8001_tmf_task tmf_task;
  1163. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  1164. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  1165. PM8001_EH_DBG(pm8001_ha,
  1166. pm8001_printk("I_T_L_Q clear task set[%x]\n",
  1167. pm8001_dev->device_id));
  1168. tmf_task.tmf = TMF_CLEAR_TASK_SET;
  1169. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1170. return rc;
  1171. }