pm8001_sas.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. DECLARE_COMPLETION_ONSTACK(completion_setstate);
  1017. if (dev_is_sata(dev)) {
  1018. struct sas_phy *phy = sas_get_local_phy(dev);
  1019. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
  1020. dev, 1, 0);
  1021. rc = sas_phy_reset(phy, 1);
  1022. sas_put_local_phy(phy);
  1023. pm8001_dev->setds_completion = &completion_setstate;
  1024. rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
  1025. pm8001_dev, 0x01);
  1026. wait_for_completion(&completion_setstate);
  1027. } else {
  1028. tmf_task.tmf = TMF_LU_RESET;
  1029. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1030. }
  1031. /* If failed, fall-through I_T_Nexus reset */
  1032. PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
  1033. pm8001_dev->device_id, rc));
  1034. return rc;
  1035. }
  1036. /* optional SAM-3 */
  1037. int pm8001_query_task(struct sas_task *task)
  1038. {
  1039. u32 tag = 0xdeadbeef;
  1040. int i = 0;
  1041. struct scsi_lun lun;
  1042. struct pm8001_tmf_task tmf_task;
  1043. int rc = TMF_RESP_FUNC_FAILED;
  1044. if (unlikely(!task || !task->lldd_task || !task->dev))
  1045. return rc;
  1046. if (task->task_proto & SAS_PROTOCOL_SSP) {
  1047. struct scsi_cmnd *cmnd = task->uldd_task;
  1048. struct domain_device *dev = task->dev;
  1049. struct pm8001_hba_info *pm8001_ha =
  1050. pm8001_find_ha_by_dev(dev);
  1051. int_to_scsilun(cmnd->device->lun, &lun);
  1052. rc = pm8001_find_tag(task, &tag);
  1053. if (rc == 0) {
  1054. rc = TMF_RESP_FUNC_FAILED;
  1055. return rc;
  1056. }
  1057. PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
  1058. for (i = 0; i < 16; i++)
  1059. printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
  1060. printk(KERN_INFO "]\n");
  1061. tmf_task.tmf = TMF_QUERY_TASK;
  1062. tmf_task.tag_of_task_to_be_managed = tag;
  1063. rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1064. switch (rc) {
  1065. /* The task is still in Lun, release it then */
  1066. case TMF_RESP_FUNC_SUCC:
  1067. PM8001_EH_DBG(pm8001_ha,
  1068. pm8001_printk("The task is still in Lun\n"));
  1069. break;
  1070. /* The task is not in Lun or failed, reset the phy */
  1071. case TMF_RESP_FUNC_FAILED:
  1072. case TMF_RESP_FUNC_COMPLETE:
  1073. PM8001_EH_DBG(pm8001_ha,
  1074. pm8001_printk("The task is not in Lun or failed,"
  1075. " reset the phy\n"));
  1076. break;
  1077. }
  1078. }
  1079. pm8001_printk(":rc= %d\n", rc);
  1080. return rc;
  1081. }
  1082. /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
  1083. int pm8001_abort_task(struct sas_task *task)
  1084. {
  1085. unsigned long flags;
  1086. u32 tag = 0xdeadbeef;
  1087. u32 device_id;
  1088. struct domain_device *dev ;
  1089. struct pm8001_hba_info *pm8001_ha = NULL;
  1090. struct pm8001_ccb_info *ccb;
  1091. struct scsi_lun lun;
  1092. struct pm8001_device *pm8001_dev;
  1093. struct pm8001_tmf_task tmf_task;
  1094. int rc = TMF_RESP_FUNC_FAILED;
  1095. if (unlikely(!task || !task->lldd_task || !task->dev))
  1096. return rc;
  1097. spin_lock_irqsave(&task->task_state_lock, flags);
  1098. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  1099. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1100. rc = TMF_RESP_FUNC_COMPLETE;
  1101. goto out;
  1102. }
  1103. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1104. if (task->task_proto & SAS_PROTOCOL_SSP) {
  1105. struct scsi_cmnd *cmnd = task->uldd_task;
  1106. dev = task->dev;
  1107. ccb = task->lldd_task;
  1108. pm8001_dev = dev->lldd_dev;
  1109. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1110. int_to_scsilun(cmnd->device->lun, &lun);
  1111. rc = pm8001_find_tag(task, &tag);
  1112. if (rc == 0) {
  1113. printk(KERN_INFO "No such tag in %s\n", __func__);
  1114. rc = TMF_RESP_FUNC_FAILED;
  1115. return rc;
  1116. }
  1117. device_id = pm8001_dev->device_id;
  1118. PM8001_EH_DBG(pm8001_ha,
  1119. pm8001_printk("abort io to deviceid= %d\n", device_id));
  1120. tmf_task.tmf = TMF_ABORT_TASK;
  1121. tmf_task.tag_of_task_to_be_managed = tag;
  1122. rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1123. pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1124. pm8001_dev->sas_device, 0, tag);
  1125. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  1126. task->task_proto & SAS_PROTOCOL_STP) {
  1127. dev = task->dev;
  1128. pm8001_dev = dev->lldd_dev;
  1129. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1130. rc = pm8001_find_tag(task, &tag);
  1131. if (rc == 0) {
  1132. printk(KERN_INFO "No such tag in %s\n", __func__);
  1133. rc = TMF_RESP_FUNC_FAILED;
  1134. return rc;
  1135. }
  1136. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1137. pm8001_dev->sas_device, 0, tag);
  1138. } else if (task->task_proto & SAS_PROTOCOL_SMP) {
  1139. /* SMP */
  1140. dev = task->dev;
  1141. pm8001_dev = dev->lldd_dev;
  1142. pm8001_ha = pm8001_find_ha_by_dev(dev);
  1143. rc = pm8001_find_tag(task, &tag);
  1144. if (rc == 0) {
  1145. printk(KERN_INFO "No such tag in %s\n", __func__);
  1146. rc = TMF_RESP_FUNC_FAILED;
  1147. return rc;
  1148. }
  1149. rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
  1150. pm8001_dev->sas_device, 0, tag);
  1151. }
  1152. out:
  1153. if (rc != TMF_RESP_FUNC_COMPLETE)
  1154. pm8001_printk("rc= %d\n", rc);
  1155. return rc;
  1156. }
  1157. int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
  1158. {
  1159. int rc = TMF_RESP_FUNC_FAILED;
  1160. struct pm8001_tmf_task tmf_task;
  1161. tmf_task.tmf = TMF_ABORT_TASK_SET;
  1162. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1163. return rc;
  1164. }
  1165. int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
  1166. {
  1167. int rc = TMF_RESP_FUNC_FAILED;
  1168. struct pm8001_tmf_task tmf_task;
  1169. tmf_task.tmf = TMF_CLEAR_ACA;
  1170. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1171. return rc;
  1172. }
  1173. int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
  1174. {
  1175. int rc = TMF_RESP_FUNC_FAILED;
  1176. struct pm8001_tmf_task tmf_task;
  1177. struct pm8001_device *pm8001_dev = dev->lldd_dev;
  1178. struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
  1179. PM8001_EH_DBG(pm8001_ha,
  1180. pm8001_printk("I_T_L_Q clear task set[%x]\n",
  1181. pm8001_dev->device_id));
  1182. tmf_task.tmf = TMF_CLEAR_TASK_SET;
  1183. rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
  1184. return rc;
  1185. }