pm8001_sas.c 38 KB

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