pm8001_sas.c 36 KB

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