pm8001_sas.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  425. rc = pm8001_task_prep_ata(pm8001_ha, ccb);
  426. break;
  427. default:
  428. dev_printk(KERN_ERR, pm8001_ha->dev,
  429. "unknown sas_task proto: 0x%x\n",
  430. t->task_proto);
  431. rc = -EINVAL;
  432. break;
  433. }
  434. if (rc) {
  435. PM8001_IO_DBG(pm8001_ha,
  436. pm8001_printk("rc is %x\n", rc));
  437. goto err_out_tag;
  438. }
  439. /* TODO: select normal or high priority */
  440. spin_lock(&t->task_state_lock);
  441. t->task_state_flags |= SAS_TASK_AT_INITIATOR;
  442. spin_unlock(&t->task_state_lock);
  443. pm8001_dev->running_req++;
  444. if (n > 1)
  445. t = list_entry(t->list.next, struct sas_task, list);
  446. } while (--n);
  447. rc = 0;
  448. goto out_done;
  449. err_out_tag:
  450. pm8001_tag_free(pm8001_ha, tag);
  451. err_out:
  452. dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
  453. if (!sas_protocol_ata(t->task_proto))
  454. if (n_elem)
  455. dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
  456. t->data_dir);
  457. out_done:
  458. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  459. return rc;
  460. }
  461. /**
  462. * pm8001_queue_command - register for upper layer used, all IO commands sent
  463. * to HBA are from this interface.
  464. * @task: the task to be execute.
  465. * @num: if can_queue great than 1, the task can be queued up. for SMP task,
  466. * we always execute one one time
  467. * @gfp_flags: gfp_flags
  468. */
  469. int pm8001_queue_command(struct sas_task *task, const int num,
  470. gfp_t gfp_flags)
  471. {
  472. return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
  473. }
  474. void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
  475. {
  476. pm8001_tag_clear(pm8001_ha, ccb_idx);
  477. }
  478. /**
  479. * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
  480. * @pm8001_ha: our hba card information
  481. * @ccb: the ccb which attached to ssp task
  482. * @task: the task to be free.
  483. * @ccb_idx: ccb index.
  484. */
  485. void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
  486. struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
  487. {
  488. if (!ccb->task)
  489. return;
  490. if (!sas_protocol_ata(task->task_proto))
  491. if (ccb->n_elem)
  492. dma_unmap_sg(pm8001_ha->dev, task->scatter,
  493. task->num_scatter, task->data_dir);
  494. switch (task->task_proto) {
  495. case SAS_PROTOCOL_SMP:
  496. dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
  497. PCI_DMA_FROMDEVICE);
  498. dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
  499. PCI_DMA_TODEVICE);
  500. break;
  501. case SAS_PROTOCOL_SATA:
  502. case SAS_PROTOCOL_STP:
  503. case SAS_PROTOCOL_SSP:
  504. default:
  505. /* do nothing */
  506. break;
  507. }
  508. task->lldd_task = NULL;
  509. ccb->task = NULL;
  510. ccb->ccb_tag = 0xFFFFFFFF;
  511. ccb->open_retry = 0;
  512. pm8001_ccb_free(pm8001_ha, ccb_idx);
  513. }
  514. /**
  515. * pm8001_alloc_dev - find a empty pm8001_device
  516. * @pm8001_ha: our hba card information
  517. */
  518. struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
  519. {
  520. u32 dev;
  521. for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
  522. if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
  523. pm8001_ha->devices[dev].id = dev;
  524. return &pm8001_ha->devices[dev];
  525. }
  526. }
  527. if (dev == PM8001_MAX_DEVICES) {
  528. PM8001_FAIL_DBG(pm8001_ha,
  529. pm8001_printk("max support %d devices, ignore ..\n",
  530. PM8001_MAX_DEVICES));
  531. }
  532. return NULL;
  533. }
  534. /**
  535. * pm8001_find_dev - find a matching pm8001_device
  536. * @pm8001_ha: our hba card information
  537. */
  538. struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
  539. u32 device_id)
  540. {
  541. u32 dev;
  542. for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
  543. if (pm8001_ha->devices[dev].device_id == device_id)
  544. return &pm8001_ha->devices[dev];
  545. }
  546. if (dev == PM8001_MAX_DEVICES) {
  547. PM8001_FAIL_DBG(pm8001_ha, pm8001_printk("NO MATCHING "
  548. "DEVICE FOUND !!!\n"));
  549. }
  550. return NULL;
  551. }
  552. static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
  553. {
  554. u32 id = pm8001_dev->id;
  555. memset(pm8001_dev, 0, sizeof(*pm8001_dev));
  556. pm8001_dev->id = id;
  557. pm8001_dev->dev_type = SAS_PHY_UNUSED;
  558. pm8001_dev->device_id = PM8001_MAX_DEVICES;
  559. pm8001_dev->sas_device = NULL;
  560. }
  561. /**
  562. * pm8001_dev_found_notify - libsas notify a device is found.
  563. * @dev: the device structure which sas layer used.
  564. *
  565. * when libsas find a sas domain device, it should tell the LLDD that
  566. * device is found, and then LLDD register this device to HBA firmware
  567. * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
  568. * device ID(according to device's sas address) and returned it to LLDD. From
  569. * now on, we communicate with HBA FW with the device ID which HBA assigned
  570. * rather than sas address. it is the necessary step for our HBA but it is
  571. * the optional for other HBA driver.
  572. */
  573. static int pm8001_dev_found_notify(struct domain_device *dev)
  574. {
  575. unsigned long flags = 0;
  576. int res = 0;
  577. struct pm8001_hba_info *pm8001_ha = NULL;
  578. struct domain_device *parent_dev = dev->parent;
  579. struct pm8001_device *pm8001_device;
  580. DECLARE_COMPLETION_ONSTACK(completion);
  581. u32 flag = 0;
  582. pm8001_ha = pm8001_find_ha_by_dev(dev);
  583. spin_lock_irqsave(&pm8001_ha->lock, flags);
  584. pm8001_device = pm8001_alloc_dev(pm8001_ha);
  585. if (!pm8001_device) {
  586. res = -1;
  587. goto found_out;
  588. }
  589. pm8001_device->sas_device = dev;
  590. dev->lldd_dev = pm8001_device;
  591. pm8001_device->dev_type = dev->dev_type;
  592. pm8001_device->dcompletion = &completion;
  593. if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
  594. int phy_id;
  595. struct ex_phy *phy;
  596. for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
  597. phy_id++) {
  598. phy = &parent_dev->ex_dev.ex_phy[phy_id];
  599. if (SAS_ADDR(phy->attached_sas_addr)
  600. == SAS_ADDR(dev->sas_addr)) {
  601. pm8001_device->attached_phy = phy_id;
  602. break;
  603. }
  604. }
  605. if (phy_id == parent_dev->ex_dev.num_phys) {
  606. PM8001_FAIL_DBG(pm8001_ha,
  607. pm8001_printk("Error: no attached dev:%016llx"
  608. " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
  609. SAS_ADDR(parent_dev->sas_addr)));
  610. res = -1;
  611. }
  612. } else {
  613. if (dev->dev_type == SAS_SATA_DEV) {
  614. pm8001_device->attached_phy =
  615. dev->rphy->identify.phy_identifier;
  616. flag = 1; /* directly sata*/
  617. }
  618. } /*register this device to HBA*/
  619. PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
  620. PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
  621. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  622. wait_for_completion(&completion);
  623. if (dev->dev_type == SAS_END_DEVICE)
  624. msleep(50);
  625. pm8001_ha->flags = PM8001F_RUN_TIME;
  626. return 0;
  627. found_out:
  628. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  629. return res;
  630. }
  631. int pm8001_dev_found(struct domain_device *dev)
  632. {
  633. return pm8001_dev_found_notify(dev);
  634. }
  635. void pm8001_task_done(struct sas_task *task)
  636. {
  637. if (!del_timer(&task->slow_task->timer))
  638. return;
  639. complete(&task->slow_task->completion);
  640. }
  641. static void pm8001_tmf_timedout(unsigned long data)
  642. {
  643. struct sas_task *task = (struct sas_task *)data;
  644. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  645. complete(&task->slow_task->completion);
  646. }
  647. #define PM8001_TASK_TIMEOUT 20
  648. /**
  649. * pm8001_exec_internal_tmf_task - execute some task management commands.
  650. * @dev: the wanted device.
  651. * @tmf: which task management wanted to be take.
  652. * @para_len: para_len.
  653. * @parameter: ssp task parameter.
  654. *
  655. * when errors or exception happened, we may want to do something, for example
  656. * abort the issued task which result in this execption, it is done by calling
  657. * this function, note it is also with the task execute interface.
  658. */
  659. static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
  660. void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
  661. {
  662. int res, retry;
  663. struct sas_task *task = NULL;
  664. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  665. for (retry = 0; retry < 3; retry++) {
  666. task = sas_alloc_slow_task(GFP_KERNEL);
  667. if (!task)
  668. return -ENOMEM;
  669. task->dev = dev;
  670. task->task_proto = dev->tproto;
  671. memcpy(&task->ssp_task, parameter, para_len);
  672. task->task_done = pm8001_task_done;
  673. task->slow_task->timer.data = (unsigned long)task;
  674. task->slow_task->timer.function = pm8001_tmf_timedout;
  675. task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
  676. add_timer(&task->slow_task->timer);
  677. res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
  678. if (res) {
  679. del_timer(&task->slow_task->timer);
  680. PM8001_FAIL_DBG(pm8001_ha,
  681. pm8001_printk("Executing internal task "
  682. "failed\n"));
  683. goto ex_err;
  684. }
  685. wait_for_completion(&task->slow_task->completion);
  686. res = -TMF_RESP_FUNC_FAILED;
  687. /* Even TMF timed out, return direct. */
  688. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  689. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  690. PM8001_FAIL_DBG(pm8001_ha,
  691. pm8001_printk("TMF task[%x]timeout.\n",
  692. tmf->tmf));
  693. goto ex_err;
  694. }
  695. }
  696. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  697. task->task_status.stat == SAM_STAT_GOOD) {
  698. res = TMF_RESP_FUNC_COMPLETE;
  699. break;
  700. }
  701. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  702. task->task_status.stat == SAS_DATA_UNDERRUN) {
  703. /* no error, but return the number of bytes of
  704. * underrun */
  705. res = task->task_status.residual;
  706. break;
  707. }
  708. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  709. task->task_status.stat == SAS_DATA_OVERRUN) {
  710. PM8001_FAIL_DBG(pm8001_ha,
  711. pm8001_printk("Blocked task error.\n"));
  712. res = -EMSGSIZE;
  713. break;
  714. } else {
  715. PM8001_EH_DBG(pm8001_ha,
  716. pm8001_printk(" Task to dev %016llx response:"
  717. "0x%x status 0x%x\n",
  718. SAS_ADDR(dev->sas_addr),
  719. task->task_status.resp,
  720. task->task_status.stat));
  721. sas_free_task(task);
  722. task = NULL;
  723. }
  724. }
  725. ex_err:
  726. BUG_ON(retry == 3 && task != NULL);
  727. sas_free_task(task);
  728. return res;
  729. }
  730. static int
  731. pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
  732. struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
  733. u32 task_tag)
  734. {
  735. int res, retry;
  736. u32 ccb_tag;
  737. struct pm8001_ccb_info *ccb;
  738. struct sas_task *task = NULL;
  739. for (retry = 0; retry < 3; retry++) {
  740. task = sas_alloc_slow_task(GFP_KERNEL);
  741. if (!task)
  742. return -ENOMEM;
  743. task->dev = dev;
  744. task->task_proto = dev->tproto;
  745. task->task_done = pm8001_task_done;
  746. task->slow_task->timer.data = (unsigned long)task;
  747. task->slow_task->timer.function = pm8001_tmf_timedout;
  748. task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
  749. add_timer(&task->slow_task->timer);
  750. res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
  751. if (res)
  752. return res;
  753. ccb = &pm8001_ha->ccb_info[ccb_tag];
  754. ccb->device = pm8001_dev;
  755. ccb->ccb_tag = ccb_tag;
  756. ccb->task = task;
  757. res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
  758. pm8001_dev, flag, task_tag, ccb_tag);
  759. if (res) {
  760. del_timer(&task->slow_task->timer);
  761. PM8001_FAIL_DBG(pm8001_ha,
  762. pm8001_printk("Executing internal task "
  763. "failed\n"));
  764. goto ex_err;
  765. }
  766. wait_for_completion(&task->slow_task->completion);
  767. res = TMF_RESP_FUNC_FAILED;
  768. /* Even TMF timed out, return direct. */
  769. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  770. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  771. PM8001_FAIL_DBG(pm8001_ha,
  772. pm8001_printk("TMF task timeout.\n"));
  773. goto ex_err;
  774. }
  775. }
  776. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  777. task->task_status.stat == SAM_STAT_GOOD) {
  778. res = TMF_RESP_FUNC_COMPLETE;
  779. break;
  780. } else {
  781. PM8001_EH_DBG(pm8001_ha,
  782. pm8001_printk(" Task to dev %016llx response: "
  783. "0x%x status 0x%x\n",
  784. SAS_ADDR(dev->sas_addr),
  785. task->task_status.resp,
  786. task->task_status.stat));
  787. sas_free_task(task);
  788. task = NULL;
  789. }
  790. }
  791. ex_err:
  792. BUG_ON(retry == 3 && task != NULL);
  793. sas_free_task(task);
  794. return res;
  795. }
  796. /**
  797. * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
  798. * @dev: the device structure which sas layer used.
  799. */
  800. static void pm8001_dev_gone_notify(struct domain_device *dev)
  801. {
  802. unsigned long flags = 0;
  803. u32 tag;
  804. struct pm8001_hba_info *pm8001_ha;
  805. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  806. pm8001_ha = pm8001_find_ha_by_dev(dev);
  807. spin_lock_irqsave(&pm8001_ha->lock, flags);
  808. pm8001_tag_alloc(pm8001_ha, &tag);
  809. if (pm8001_dev) {
  810. u32 device_id = pm8001_dev->device_id;
  811. PM8001_DISC_DBG(pm8001_ha,
  812. pm8001_printk("found dev[%d:%x] is gone.\n",
  813. pm8001_dev->device_id, pm8001_dev->dev_type));
  814. if (pm8001_dev->running_req) {
  815. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  816. pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  817. dev, 1, 0);
  818. spin_lock_irqsave(&pm8001_ha->lock, flags);
  819. }
  820. PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
  821. pm8001_free_dev(pm8001_dev);
  822. } else {
  823. PM8001_DISC_DBG(pm8001_ha,
  824. pm8001_printk("Found dev has gone.\n"));
  825. }
  826. dev->lldd_dev = NULL;
  827. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  828. }
  829. void pm8001_dev_gone(struct domain_device *dev)
  830. {
  831. pm8001_dev_gone_notify(dev);
  832. }
  833. static int pm8001_issue_ssp_tmf(struct domain_device *dev,
  834. u8 *lun, struct pm8001_tmf_task *tmf)
  835. {
  836. struct sas_ssp_task ssp_task;
  837. if (!(dev->tproto & SAS_PROTOCOL_SSP))
  838. return TMF_RESP_FUNC_ESUPP;
  839. strncpy((u8 *)&ssp_task.LUN, lun, 8);
  840. return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
  841. tmf);
  842. }
  843. /* retry commands by ha, by task and/or by device */
  844. void pm8001_open_reject_retry(
  845. struct pm8001_hba_info *pm8001_ha,
  846. struct sas_task *task_to_close,
  847. struct pm8001_device *device_to_close)
  848. {
  849. int i;
  850. unsigned long flags;
  851. if (pm8001_ha == NULL)
  852. return;
  853. spin_lock_irqsave(&pm8001_ha->lock, flags);
  854. for (i = 0; i < PM8001_MAX_CCB; i++) {
  855. struct sas_task *task;
  856. struct task_status_struct *ts;
  857. struct pm8001_device *pm8001_dev;
  858. unsigned long flags1;
  859. u32 tag;
  860. struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
  861. pm8001_dev = ccb->device;
  862. if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
  863. continue;
  864. if (!device_to_close) {
  865. uintptr_t d = (uintptr_t)pm8001_dev
  866. - (uintptr_t)&pm8001_ha->devices;
  867. if (((d % sizeof(*pm8001_dev)) != 0)
  868. || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
  869. continue;
  870. } else if (pm8001_dev != device_to_close)
  871. continue;
  872. tag = ccb->ccb_tag;
  873. if (!tag || (tag == 0xFFFFFFFF))
  874. continue;
  875. task = ccb->task;
  876. if (!task || !task->task_done)
  877. continue;
  878. if (task_to_close && (task != task_to_close))
  879. continue;
  880. ts = &task->task_status;
  881. ts->resp = SAS_TASK_COMPLETE;
  882. /* Force the midlayer to retry */
  883. ts->stat = SAS_OPEN_REJECT;
  884. ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
  885. if (pm8001_dev)
  886. pm8001_dev->running_req--;
  887. spin_lock_irqsave(&task->task_state_lock, flags1);
  888. task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
  889. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  890. task->task_state_flags |= SAS_TASK_STATE_DONE;
  891. if (unlikely((task->task_state_flags
  892. & SAS_TASK_STATE_ABORTED))) {
  893. spin_unlock_irqrestore(&task->task_state_lock,
  894. flags1);
  895. pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
  896. } else {
  897. spin_unlock_irqrestore(&task->task_state_lock,
  898. flags1);
  899. pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
  900. mb();/* in order to force CPU ordering */
  901. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  902. task->task_done(task);
  903. spin_lock_irqsave(&pm8001_ha->lock, flags);
  904. }
  905. }
  906. spin_unlock_irqrestore(&pm8001_ha->lock, flags);
  907. }
  908. /**
  909. * Standard mandates link reset for ATA (type 0) and hard reset for
  910. * SSP (type 1) , only for RECOVERY
  911. */
  912. int pm8001_I_T_nexus_reset(struct domain_device *dev)
  913. {
  914. int rc = TMF_RESP_FUNC_FAILED;
  915. struct pm8001_device *pm8001_dev;
  916. struct pm8001_hba_info *pm8001_ha;
  917. struct sas_phy *phy;
  918. if (!dev || !dev->lldd_dev)
  919. return -ENODEV;
  920. pm8001_dev = dev->lldd_dev;
  921. pm8001_ha = pm8001_find_ha_by_dev(dev);
  922. phy = sas_get_local_phy(dev);
  923. if (dev_is_sata(dev)) {
  924. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  925. if (scsi_is_sas_phy_local(phy)) {
  926. rc = 0;
  927. goto out;
  928. }
  929. rc = sas_phy_reset(phy, 1);
  930. msleep(2000);
  931. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  932. dev, 1, 0);
  933. pm8001_dev->setds_completion = &completion_setstate;
  934. rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  935. pm8001_dev, 0x01);
  936. wait_for_completion(&completion_setstate);
  937. } else {
  938. rc = sas_phy_reset(phy, 1);
  939. msleep(2000);
  940. }
  941. PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
  942. pm8001_dev->device_id, rc));
  943. out:
  944. sas_put_local_phy(phy);
  945. return rc;
  946. }
  947. /*
  948. * This function handle the IT_NEXUS_XXX event or completion
  949. * status code for SSP/SATA/SMP I/O request.
  950. */
  951. int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
  952. {
  953. int rc = TMF_RESP_FUNC_FAILED;
  954. struct pm8001_device *pm8001_dev;
  955. struct pm8001_hba_info *pm8001_ha;
  956. struct sas_phy *phy;
  957. u32 device_id = 0;
  958. if (!dev || !dev->lldd_dev)
  959. return -1;
  960. pm8001_dev = dev->lldd_dev;
  961. device_id = pm8001_dev->device_id;
  962. pm8001_ha = pm8001_find_ha_by_dev(dev);
  963. PM8001_EH_DBG(pm8001_ha,
  964. pm8001_printk("I_T_Nexus handler invoked !!"));
  965. phy = sas_get_local_phy(dev);
  966. if (dev_is_sata(dev)) {
  967. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  968. if (scsi_is_sas_phy_local(phy)) {
  969. rc = 0;
  970. goto out;
  971. }
  972. /* send internal ssp/sata/smp abort command to FW */
  973. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  974. dev, 1, 0);
  975. msleep(100);
  976. /* deregister the target device */
  977. pm8001_dev_gone_notify(dev);
  978. msleep(200);
  979. /*send phy reset to hard reset target */
  980. rc = sas_phy_reset(phy, 1);
  981. msleep(2000);
  982. pm8001_dev->setds_completion = &completion_setstate;
  983. wait_for_completion(&completion_setstate);
  984. } else {
  985. /* send internal ssp/sata/smp abort command to FW */
  986. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  987. dev, 1, 0);
  988. msleep(100);
  989. /* deregister the target device */
  990. pm8001_dev_gone_notify(dev);
  991. msleep(200);
  992. /*send phy reset to hard reset target */
  993. rc = sas_phy_reset(phy, 1);
  994. msleep(2000);
  995. }
  996. PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
  997. pm8001_dev->device_id, rc));
  998. out:
  999. sas_put_local_phy(phy);
  1000. return rc;
  1001. }
  1002. /* mandatory SAM-3, the task reset the specified LUN*/
  1003. int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
  1004. {
  1005. int rc = TMF_RESP_FUNC_FAILED;
  1006. struct pm8001_tmf_task tmf_task;
  1007. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  1008. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  1009. if (dev_is_sata(dev)) {
  1010. struct sas_phy *phy = sas_get_local_phy(dev);
  1011. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  1012. dev, 1, 0);
  1013. rc = sas_phy_reset(phy, 1);
  1014. sas_put_local_phy(phy);
  1015. rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  1016. pm8001_dev, 0x01);
  1017. msleep(2000);
  1018. } else {
  1019. tmf_task.tmf = TMF_LU_RESET;
  1020. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1021. }
  1022. /* If failed, fall-through I_T_Nexus reset */
  1023. PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
  1024. pm8001_dev->device_id, rc));
  1025. return rc;
  1026. }
  1027. /* optional SAM-3 */
  1028. int pm8001_query_task(struct sas_task *task)
  1029. {
  1030. u32 tag = 0xdeadbeef;
  1031. int i = 0;
  1032. struct scsi_lun lun;
  1033. struct pm8001_tmf_task tmf_task;
  1034. int rc = TMF_RESP_FUNC_FAILED;
  1035. if (unlikely(!task || !task->lldd_task || !task->dev))
  1036. return rc;
  1037. if (task->task_proto & SAS_PROTOCOL_SSP) {
  1038. struct scsi_cmnd *cmnd = task->uldd_task;
  1039. struct domain_device *dev = task->dev;
  1040. struct pm8001_hba_info *pm8001_ha =
  1041. pm8001_find_ha_by_dev(dev);
  1042. int_to_scsilun(cmnd->device->lun, &lun);
  1043. rc = pm8001_find_tag(task, &tag);
  1044. if (rc == 0) {
  1045. rc = TMF_RESP_FUNC_FAILED;
  1046. return rc;
  1047. }
  1048. PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
  1049. for (i = 0; i < 16; i++)
  1050. printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
  1051. printk(KERN_INFO "]\n");
  1052. tmf_task.tmf = TMF_QUERY_TASK;
  1053. tmf_task.tag_of_task_to_be_managed = tag;
  1054. rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1055. switch (rc) {
  1056. /* The task is still in Lun, release it then */
  1057. case TMF_RESP_FUNC_SUCC:
  1058. PM8001_EH_DBG(pm8001_ha,
  1059. pm8001_printk("The task is still in Lun\n"));
  1060. break;
  1061. /* The task is not in Lun or failed, reset the phy */
  1062. case TMF_RESP_FUNC_FAILED:
  1063. case TMF_RESP_FUNC_COMPLETE:
  1064. PM8001_EH_DBG(pm8001_ha,
  1065. pm8001_printk("The task is not in Lun or failed,"
  1066. " reset the phy\n"));
  1067. break;
  1068. }
  1069. }
  1070. pm8001_printk(":rc= %d\n", rc);
  1071. return rc;
  1072. }
  1073. /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
  1074. int pm8001_abort_task(struct sas_task *task)
  1075. {
  1076. unsigned long flags;
  1077. u32 tag = 0xdeadbeef;
  1078. u32 device_id;
  1079. struct domain_device *dev ;
  1080. struct pm8001_hba_info *pm8001_ha = NULL;
  1081. struct pm8001_ccb_info *ccb;
  1082. struct scsi_lun lun;
  1083. struct pm8001_device *pm8001_dev;
  1084. struct pm8001_tmf_task tmf_task;
  1085. int rc = TMF_RESP_FUNC_FAILED;
  1086. if (unlikely(!task || !task->lldd_task || !task->dev))
  1087. return rc;
  1088. spin_lock_irqsave(&task->task_state_lock, flags);
  1089. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  1090. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1091. rc = TMF_RESP_FUNC_COMPLETE;
  1092. goto out;
  1093. }
  1094. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1095. if (task->task_proto & SAS_PROTOCOL_SSP) {
  1096. struct scsi_cmnd *cmnd = task->uldd_task;
  1097. dev = task->dev;
  1098. ccb = task->lldd_task;
  1099. pm8001_dev = dev->lldd_dev;
  1100. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1101. int_to_scsilun(cmnd->device->lun, &lun);
  1102. rc = pm8001_find_tag(task, &tag);
  1103. if (rc == 0) {
  1104. printk(KERN_INFO "No such tag in %s\n", __func__);
  1105. rc = TMF_RESP_FUNC_FAILED;
  1106. return rc;
  1107. }
  1108. device_id = pm8001_dev->device_id;
  1109. PM8001_EH_DBG(pm8001_ha,
  1110. pm8001_printk("abort io to deviceid= %d\n", device_id));
  1111. tmf_task.tmf = TMF_ABORT_TASK;
  1112. tmf_task.tag_of_task_to_be_managed = tag;
  1113. rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1114. pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1115. pm8001_dev->sas_device, 0, tag);
  1116. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  1117. task->task_proto & SAS_PROTOCOL_STP) {
  1118. dev = task->dev;
  1119. pm8001_dev = dev->lldd_dev;
  1120. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1121. rc = pm8001_find_tag(task, &tag);
  1122. if (rc == 0) {
  1123. printk(KERN_INFO "No such tag in %s\n", __func__);
  1124. rc = TMF_RESP_FUNC_FAILED;
  1125. return rc;
  1126. }
  1127. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1128. pm8001_dev->sas_device, 0, tag);
  1129. } else if (task->task_proto & SAS_PROTOCOL_SMP) {
  1130. /* SMP */
  1131. dev = task->dev;
  1132. pm8001_dev = dev->lldd_dev;
  1133. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1134. rc = pm8001_find_tag(task, &tag);
  1135. if (rc == 0) {
  1136. printk(KERN_INFO "No such tag in %s\n", __func__);
  1137. rc = TMF_RESP_FUNC_FAILED;
  1138. return rc;
  1139. }
  1140. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1141. pm8001_dev->sas_device, 0, tag);
  1142. }
  1143. out:
  1144. if (rc != TMF_RESP_FUNC_COMPLETE)
  1145. pm8001_printk("rc= %d\n", rc);
  1146. return rc;
  1147. }
  1148. int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
  1149. {
  1150. int rc = TMF_RESP_FUNC_FAILED;
  1151. struct pm8001_tmf_task tmf_task;
  1152. tmf_task.tmf = TMF_ABORT_TASK_SET;
  1153. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1154. return rc;
  1155. }
  1156. int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
  1157. {
  1158. int rc = TMF_RESP_FUNC_FAILED;
  1159. struct pm8001_tmf_task tmf_task;
  1160. tmf_task.tmf = TMF_CLEAR_ACA;
  1161. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1162. return rc;
  1163. }
  1164. int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
  1165. {
  1166. int rc = TMF_RESP_FUNC_FAILED;
  1167. struct pm8001_tmf_task tmf_task;
  1168. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  1169. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  1170. PM8001_EH_DBG(pm8001_ha,
  1171. pm8001_printk("I_T_L_Q clear task set[%x]\n",
  1172. pm8001_dev->device_id));
  1173. tmf_task.tmf = TMF_CLEAR_TASK_SET;
  1174. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1175. return rc;
  1176. }