pm8001_sas.c 36 KB

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