sas_scsi_host.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * Serial Attached SCSI (SAS) class SCSI Host glue.
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. */
  25. #include <linux/kthread.h>
  26. #include "sas_internal.h"
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_transport.h>
  33. #include <scsi/scsi_transport_sas.h>
  34. #include "../scsi_sas_internal.h"
  35. #include "../scsi_transport_api.h"
  36. #include "../scsi_priv.h"
  37. #include <linux/err.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/freezer.h>
  40. #include <linux/scatterlist.h>
  41. #include <linux/libata.h>
  42. /* ---------- SCSI Host glue ---------- */
  43. #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
  44. #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
  45. static void sas_scsi_task_done(struct sas_task *task)
  46. {
  47. struct task_status_struct *ts = &task->task_status;
  48. struct scsi_cmnd *sc = task->uldd_task;
  49. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
  50. unsigned ts_flags = task->task_state_flags;
  51. int hs = 0, stat = 0;
  52. if (unlikely(!sc)) {
  53. SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
  54. list_del_init(&task->list);
  55. sas_free_task(task);
  56. return;
  57. }
  58. if (ts->resp == SAS_TASK_UNDELIVERED) {
  59. /* transport error */
  60. hs = DID_NO_CONNECT;
  61. } else { /* ts->resp == SAS_TASK_COMPLETE */
  62. /* task delivered, what happened afterwards? */
  63. switch (ts->stat) {
  64. case SAS_DEV_NO_RESPONSE:
  65. case SAS_INTERRUPTED:
  66. case SAS_PHY_DOWN:
  67. case SAS_NAK_R_ERR:
  68. case SAS_OPEN_TO:
  69. hs = DID_NO_CONNECT;
  70. break;
  71. case SAS_DATA_UNDERRUN:
  72. scsi_set_resid(sc, ts->residual);
  73. if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
  74. hs = DID_ERROR;
  75. break;
  76. case SAS_DATA_OVERRUN:
  77. hs = DID_ERROR;
  78. break;
  79. case SAS_QUEUE_FULL:
  80. hs = DID_SOFT_ERROR; /* retry */
  81. break;
  82. case SAS_DEVICE_UNKNOWN:
  83. hs = DID_BAD_TARGET;
  84. break;
  85. case SAS_SG_ERR:
  86. hs = DID_PARITY;
  87. break;
  88. case SAS_OPEN_REJECT:
  89. if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
  90. hs = DID_SOFT_ERROR; /* retry */
  91. else
  92. hs = DID_ERROR;
  93. break;
  94. case SAS_PROTO_RESPONSE:
  95. SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
  96. "task; please report this\n",
  97. task->dev->port->ha->sas_ha_name);
  98. break;
  99. case SAS_ABORTED_TASK:
  100. hs = DID_ABORT;
  101. break;
  102. case SAM_CHECK_COND:
  103. memcpy(sc->sense_buffer, ts->buf,
  104. max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
  105. stat = SAM_CHECK_COND;
  106. break;
  107. default:
  108. stat = ts->stat;
  109. break;
  110. }
  111. }
  112. ASSIGN_SAS_TASK(sc, NULL);
  113. sc->result = (hs << 16) | stat;
  114. list_del_init(&task->list);
  115. sas_free_task(task);
  116. /* This is very ugly but this is how SCSI Core works. */
  117. if (ts_flags & SAS_TASK_STATE_ABORTED)
  118. scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
  119. else
  120. sc->scsi_done(sc);
  121. }
  122. static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
  123. {
  124. enum task_attribute ta = TASK_ATTR_SIMPLE;
  125. if (cmd->request && blk_rq_tagged(cmd->request)) {
  126. if (cmd->device->ordered_tags &&
  127. (cmd->request->cmd_flags & REQ_HARDBARRIER))
  128. ta = TASK_ATTR_ORDERED;
  129. }
  130. return ta;
  131. }
  132. static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
  133. struct domain_device *dev,
  134. gfp_t gfp_flags)
  135. {
  136. struct sas_task *task = sas_alloc_task(gfp_flags);
  137. struct scsi_lun lun;
  138. if (!task)
  139. return NULL;
  140. *(u32 *)cmd->sense_buffer = 0;
  141. task->uldd_task = cmd;
  142. ASSIGN_SAS_TASK(cmd, task);
  143. task->dev = dev;
  144. task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
  145. task->ssp_task.retry_count = 1;
  146. int_to_scsilun(cmd->device->lun, &lun);
  147. memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
  148. task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
  149. memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
  150. task->scatter = scsi_sglist(cmd);
  151. task->num_scatter = scsi_sg_count(cmd);
  152. task->total_xfer_len = scsi_bufflen(cmd);
  153. task->data_dir = cmd->sc_data_direction;
  154. task->task_done = sas_scsi_task_done;
  155. return task;
  156. }
  157. static int sas_queue_up(struct sas_task *task)
  158. {
  159. struct sas_ha_struct *sas_ha = task->dev->port->ha;
  160. struct scsi_core *core = &sas_ha->core;
  161. unsigned long flags;
  162. LIST_HEAD(list);
  163. spin_lock_irqsave(&core->task_queue_lock, flags);
  164. if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
  165. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  166. return -SAS_QUEUE_FULL;
  167. }
  168. list_add_tail(&task->list, &core->task_queue);
  169. core->task_queue_size += 1;
  170. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  171. wake_up_process(core->queue_thread);
  172. return 0;
  173. }
  174. static inline int dev_is_sata(struct domain_device *dev)
  175. {
  176. return (dev->rphy->identify.target_port_protocols & SAS_PROTOCOL_SATA);
  177. }
  178. /**
  179. * sas_queuecommand -- Enqueue a command for processing
  180. * @parameters: See SCSI Core documentation
  181. *
  182. * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
  183. * call us without holding an IRQ spinlock...
  184. */
  185. int sas_queuecommand(struct scsi_cmnd *cmd,
  186. void (*scsi_done)(struct scsi_cmnd *))
  187. {
  188. int res = 0;
  189. struct domain_device *dev = cmd_to_domain_dev(cmd);
  190. struct Scsi_Host *host = cmd->device->host;
  191. struct sas_internal *i = to_sas_internal(host->transportt);
  192. spin_unlock_irq(host->host_lock);
  193. {
  194. struct sas_ha_struct *sas_ha = dev->port->ha;
  195. struct sas_task *task;
  196. if (dev_is_sata(dev)) {
  197. res = ata_sas_queuecmd(cmd, scsi_done,
  198. dev->sata_dev.ap);
  199. goto out;
  200. }
  201. res = -ENOMEM;
  202. task = sas_create_task(cmd, dev, GFP_ATOMIC);
  203. if (!task)
  204. goto out;
  205. cmd->scsi_done = scsi_done;
  206. /* Queue up, Direct Mode or Task Collector Mode. */
  207. if (sas_ha->lldd_max_execute_num < 2)
  208. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  209. else
  210. res = sas_queue_up(task);
  211. /* Examine */
  212. if (res) {
  213. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  214. ASSIGN_SAS_TASK(cmd, NULL);
  215. sas_free_task(task);
  216. if (res == -SAS_QUEUE_FULL) {
  217. cmd->result = DID_SOFT_ERROR << 16; /* retry */
  218. res = 0;
  219. scsi_done(cmd);
  220. }
  221. goto out;
  222. }
  223. }
  224. out:
  225. spin_lock_irq(host->host_lock);
  226. return res;
  227. }
  228. static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
  229. {
  230. struct scsi_cmnd *cmd, *n;
  231. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  232. if (cmd == my_cmd)
  233. list_del_init(&cmd->eh_entry);
  234. }
  235. }
  236. static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
  237. struct domain_device *dev)
  238. {
  239. struct scsi_cmnd *cmd, *n;
  240. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  241. struct domain_device *x = cmd_to_domain_dev(cmd);
  242. if (x == dev)
  243. list_del_init(&cmd->eh_entry);
  244. }
  245. }
  246. static void sas_scsi_clear_queue_port(struct list_head *error_q,
  247. struct asd_sas_port *port)
  248. {
  249. struct scsi_cmnd *cmd, *n;
  250. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  251. struct domain_device *dev = cmd_to_domain_dev(cmd);
  252. struct asd_sas_port *x = dev->port;
  253. if (x == port)
  254. list_del_init(&cmd->eh_entry);
  255. }
  256. }
  257. enum task_disposition {
  258. TASK_IS_DONE,
  259. TASK_IS_ABORTED,
  260. TASK_IS_AT_LU,
  261. TASK_IS_NOT_AT_LU,
  262. TASK_ABORT_FAILED,
  263. };
  264. static enum task_disposition sas_scsi_find_task(struct sas_task *task)
  265. {
  266. struct sas_ha_struct *ha = task->dev->port->ha;
  267. unsigned long flags;
  268. int i, res;
  269. struct sas_internal *si =
  270. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  271. if (ha->lldd_max_execute_num > 1) {
  272. struct scsi_core *core = &ha->core;
  273. struct sas_task *t, *n;
  274. spin_lock_irqsave(&core->task_queue_lock, flags);
  275. list_for_each_entry_safe(t, n, &core->task_queue, list) {
  276. if (task == t) {
  277. list_del_init(&t->list);
  278. spin_unlock_irqrestore(&core->task_queue_lock,
  279. flags);
  280. SAS_DPRINTK("%s: task 0x%p aborted from "
  281. "task_queue\n",
  282. __FUNCTION__, task);
  283. return TASK_IS_ABORTED;
  284. }
  285. }
  286. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  287. }
  288. for (i = 0; i < 5; i++) {
  289. SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
  290. res = si->dft->lldd_abort_task(task);
  291. spin_lock_irqsave(&task->task_state_lock, flags);
  292. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  293. spin_unlock_irqrestore(&task->task_state_lock, flags);
  294. SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
  295. task);
  296. return TASK_IS_DONE;
  297. }
  298. spin_unlock_irqrestore(&task->task_state_lock, flags);
  299. if (res == TMF_RESP_FUNC_COMPLETE) {
  300. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  301. __FUNCTION__, task);
  302. return TASK_IS_ABORTED;
  303. } else if (si->dft->lldd_query_task) {
  304. SAS_DPRINTK("%s: querying task 0x%p\n",
  305. __FUNCTION__, task);
  306. res = si->dft->lldd_query_task(task);
  307. switch (res) {
  308. case TMF_RESP_FUNC_SUCC:
  309. SAS_DPRINTK("%s: task 0x%p at LU\n",
  310. __FUNCTION__, task);
  311. return TASK_IS_AT_LU;
  312. case TMF_RESP_FUNC_COMPLETE:
  313. SAS_DPRINTK("%s: task 0x%p not at LU\n",
  314. __FUNCTION__, task);
  315. return TASK_IS_NOT_AT_LU;
  316. case TMF_RESP_FUNC_FAILED:
  317. SAS_DPRINTK("%s: task 0x%p failed to abort\n",
  318. __FUNCTION__, task);
  319. return TASK_ABORT_FAILED;
  320. }
  321. }
  322. }
  323. return res;
  324. }
  325. static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
  326. {
  327. int res = TMF_RESP_FUNC_FAILED;
  328. struct scsi_lun lun;
  329. struct sas_internal *i =
  330. to_sas_internal(dev->port->ha->core.shost->transportt);
  331. int_to_scsilun(cmd->device->lun, &lun);
  332. SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
  333. SAS_ADDR(dev->sas_addr),
  334. cmd->device->lun);
  335. if (i->dft->lldd_abort_task_set)
  336. res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
  337. if (res == TMF_RESP_FUNC_FAILED) {
  338. if (i->dft->lldd_clear_task_set)
  339. res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
  340. }
  341. if (res == TMF_RESP_FUNC_FAILED) {
  342. if (i->dft->lldd_lu_reset)
  343. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  344. }
  345. return res;
  346. }
  347. static int sas_recover_I_T(struct domain_device *dev)
  348. {
  349. int res = TMF_RESP_FUNC_FAILED;
  350. struct sas_internal *i =
  351. to_sas_internal(dev->port->ha->core.shost->transportt);
  352. SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
  353. SAS_ADDR(dev->sas_addr));
  354. if (i->dft->lldd_I_T_nexus_reset)
  355. res = i->dft->lldd_I_T_nexus_reset(dev);
  356. return res;
  357. }
  358. /* Find the sas_phy that's attached to this device */
  359. struct sas_phy *find_local_sas_phy(struct domain_device *dev)
  360. {
  361. struct domain_device *pdev = dev->parent;
  362. struct ex_phy *exphy = NULL;
  363. int i;
  364. /* Directly attached device */
  365. if (!pdev)
  366. return dev->port->phy;
  367. /* Otherwise look in the expander */
  368. for (i = 0; i < pdev->ex_dev.num_phys; i++)
  369. if (!memcmp(dev->sas_addr,
  370. pdev->ex_dev.ex_phy[i].attached_sas_addr,
  371. SAS_ADDR_SIZE)) {
  372. exphy = &pdev->ex_dev.ex_phy[i];
  373. break;
  374. }
  375. BUG_ON(!exphy);
  376. return exphy->phy;
  377. }
  378. /* Attempt to send a LUN reset message to a device */
  379. int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
  380. {
  381. struct domain_device *dev = cmd_to_domain_dev(cmd);
  382. struct sas_internal *i =
  383. to_sas_internal(dev->port->ha->core.shost->transportt);
  384. struct scsi_lun lun;
  385. int res;
  386. int_to_scsilun(cmd->device->lun, &lun);
  387. if (!i->dft->lldd_lu_reset)
  388. return FAILED;
  389. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  390. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  391. return SUCCESS;
  392. return FAILED;
  393. }
  394. /* Attempt to send a phy (bus) reset */
  395. int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  396. {
  397. struct domain_device *dev = cmd_to_domain_dev(cmd);
  398. struct sas_phy *phy = find_local_sas_phy(dev);
  399. int res;
  400. res = sas_phy_reset(phy, 1);
  401. if (res)
  402. SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
  403. phy->dev.kobj.k_name,
  404. res);
  405. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  406. return SUCCESS;
  407. return FAILED;
  408. }
  409. /* Try to reset a device */
  410. static int try_to_reset_cmd_device(struct Scsi_Host *shost,
  411. struct scsi_cmnd *cmd)
  412. {
  413. int res;
  414. if (!shost->hostt->eh_device_reset_handler)
  415. goto try_bus_reset;
  416. res = shost->hostt->eh_device_reset_handler(cmd);
  417. if (res == SUCCESS)
  418. return res;
  419. try_bus_reset:
  420. if (shost->hostt->eh_bus_reset_handler)
  421. return shost->hostt->eh_bus_reset_handler(cmd);
  422. return FAILED;
  423. }
  424. static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
  425. struct list_head *work_q,
  426. struct list_head *done_q)
  427. {
  428. struct scsi_cmnd *cmd, *n;
  429. enum task_disposition res = TASK_IS_DONE;
  430. int tmf_resp, need_reset;
  431. struct sas_internal *i = to_sas_internal(shost->transportt);
  432. unsigned long flags;
  433. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  434. Again:
  435. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  436. struct sas_task *task = TO_SAS_TASK(cmd);
  437. if (!task)
  438. continue;
  439. list_del_init(&cmd->eh_entry);
  440. spin_lock_irqsave(&task->task_state_lock, flags);
  441. need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
  442. spin_unlock_irqrestore(&task->task_state_lock, flags);
  443. SAS_DPRINTK("trying to find task 0x%p\n", task);
  444. res = sas_scsi_find_task(task);
  445. cmd->eh_eflags = 0;
  446. switch (res) {
  447. case TASK_IS_DONE:
  448. SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
  449. task);
  450. task->task_done(task);
  451. if (need_reset)
  452. try_to_reset_cmd_device(shost, cmd);
  453. continue;
  454. case TASK_IS_ABORTED:
  455. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  456. __FUNCTION__, task);
  457. task->task_done(task);
  458. if (need_reset)
  459. try_to_reset_cmd_device(shost, cmd);
  460. continue;
  461. case TASK_IS_AT_LU:
  462. SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
  463. tmf_resp = sas_recover_lu(task->dev, cmd);
  464. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  465. SAS_DPRINTK("dev %016llx LU %x is "
  466. "recovered\n",
  467. SAS_ADDR(task->dev),
  468. cmd->device->lun);
  469. task->task_done(task);
  470. if (need_reset)
  471. try_to_reset_cmd_device(shost, cmd);
  472. sas_scsi_clear_queue_lu(work_q, cmd);
  473. goto Again;
  474. }
  475. /* fallthrough */
  476. case TASK_IS_NOT_AT_LU:
  477. case TASK_ABORT_FAILED:
  478. SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
  479. task);
  480. tmf_resp = sas_recover_I_T(task->dev);
  481. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  482. SAS_DPRINTK("I_T %016llx recovered\n",
  483. SAS_ADDR(task->dev->sas_addr));
  484. task->task_done(task);
  485. if (need_reset)
  486. try_to_reset_cmd_device(shost, cmd);
  487. sas_scsi_clear_queue_I_T(work_q, task->dev);
  488. goto Again;
  489. }
  490. /* Hammer time :-) */
  491. if (i->dft->lldd_clear_nexus_port) {
  492. struct asd_sas_port *port = task->dev->port;
  493. SAS_DPRINTK("clearing nexus for port:%d\n",
  494. port->id);
  495. res = i->dft->lldd_clear_nexus_port(port);
  496. if (res == TMF_RESP_FUNC_COMPLETE) {
  497. SAS_DPRINTK("clear nexus port:%d "
  498. "succeeded\n", port->id);
  499. task->task_done(task);
  500. if (need_reset)
  501. try_to_reset_cmd_device(shost, cmd);
  502. sas_scsi_clear_queue_port(work_q,
  503. port);
  504. goto Again;
  505. }
  506. }
  507. if (i->dft->lldd_clear_nexus_ha) {
  508. SAS_DPRINTK("clear nexus ha\n");
  509. res = i->dft->lldd_clear_nexus_ha(ha);
  510. if (res == TMF_RESP_FUNC_COMPLETE) {
  511. SAS_DPRINTK("clear nexus ha "
  512. "succeeded\n");
  513. task->task_done(task);
  514. if (need_reset)
  515. try_to_reset_cmd_device(shost, cmd);
  516. goto out;
  517. }
  518. }
  519. /* If we are here -- this means that no amount
  520. * of effort could recover from errors. Quite
  521. * possibly the HA just disappeared.
  522. */
  523. SAS_DPRINTK("error from device %llx, LUN %x "
  524. "couldn't be recovered in any way\n",
  525. SAS_ADDR(task->dev->sas_addr),
  526. cmd->device->lun);
  527. task->task_done(task);
  528. if (need_reset)
  529. try_to_reset_cmd_device(shost, cmd);
  530. goto clear_q;
  531. }
  532. }
  533. out:
  534. return list_empty(work_q);
  535. clear_q:
  536. SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
  537. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  538. struct sas_task *task = TO_SAS_TASK(cmd);
  539. list_del_init(&cmd->eh_entry);
  540. task->task_done(task);
  541. }
  542. return list_empty(work_q);
  543. }
  544. void sas_scsi_recover_host(struct Scsi_Host *shost)
  545. {
  546. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  547. unsigned long flags;
  548. LIST_HEAD(eh_work_q);
  549. spin_lock_irqsave(shost->host_lock, flags);
  550. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  551. spin_unlock_irqrestore(shost->host_lock, flags);
  552. SAS_DPRINTK("Enter %s\n", __FUNCTION__);
  553. /*
  554. * Deal with commands that still have SAS tasks (i.e. they didn't
  555. * complete via the normal sas_task completion mechanism)
  556. */
  557. if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
  558. goto out;
  559. /*
  560. * Now deal with SCSI commands that completed ok but have a an error
  561. * code (and hopefully sense data) attached. This is roughly what
  562. * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
  563. * command we see here has no sas_task and is thus unknown to the HA.
  564. */
  565. if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
  566. scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
  567. out:
  568. scsi_eh_flush_done_q(&ha->eh_done_q);
  569. SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
  570. return;
  571. }
  572. enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
  573. {
  574. struct sas_task *task = TO_SAS_TASK(cmd);
  575. unsigned long flags;
  576. if (!task) {
  577. cmd->timeout_per_command /= 2;
  578. SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
  579. cmd, task, (cmd->timeout_per_command ?
  580. "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
  581. if (!cmd->timeout_per_command)
  582. return EH_NOT_HANDLED;
  583. return EH_RESET_TIMER;
  584. }
  585. spin_lock_irqsave(&task->task_state_lock, flags);
  586. BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
  587. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  588. spin_unlock_irqrestore(&task->task_state_lock, flags);
  589. SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
  590. cmd, task);
  591. return EH_HANDLED;
  592. }
  593. if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
  594. spin_unlock_irqrestore(&task->task_state_lock, flags);
  595. SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
  596. "EH_RESET_TIMER\n",
  597. cmd, task);
  598. return EH_RESET_TIMER;
  599. }
  600. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  601. spin_unlock_irqrestore(&task->task_state_lock, flags);
  602. SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
  603. cmd, task);
  604. return EH_NOT_HANDLED;
  605. }
  606. static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
  607. {
  608. /* Cheesy attempt to translate SAS errors into ATA. Hah! */
  609. /* transport error */
  610. if (ts->resp == SAS_TASK_UNDELIVERED)
  611. return AC_ERR_ATA_BUS;
  612. /* ts->resp == SAS_TASK_COMPLETE */
  613. /* task delivered, what happened afterwards? */
  614. switch (ts->stat) {
  615. case SAS_DEV_NO_RESPONSE:
  616. return AC_ERR_TIMEOUT;
  617. case SAS_INTERRUPTED:
  618. case SAS_PHY_DOWN:
  619. case SAS_NAK_R_ERR:
  620. return AC_ERR_ATA_BUS;
  621. case SAS_DATA_UNDERRUN:
  622. /*
  623. * Some programs that use the taskfile interface
  624. * (smartctl in particular) can cause underrun
  625. * problems. Ignore these errors, perhaps at our
  626. * peril.
  627. */
  628. return 0;
  629. case SAS_DATA_OVERRUN:
  630. case SAS_QUEUE_FULL:
  631. case SAS_DEVICE_UNKNOWN:
  632. case SAS_SG_ERR:
  633. return AC_ERR_INVALID;
  634. case SAM_CHECK_COND:
  635. case SAS_OPEN_TO:
  636. case SAS_OPEN_REJECT:
  637. SAS_DPRINTK("%s: Saw error %d. What to do?\n",
  638. __FUNCTION__, ts->stat);
  639. return AC_ERR_OTHER;
  640. case SAS_ABORTED_TASK:
  641. return AC_ERR_DEV;
  642. case SAS_PROTO_RESPONSE:
  643. /* This means the ending_fis has the error
  644. * value; return 0 here to collect it */
  645. return 0;
  646. default:
  647. return 0;
  648. }
  649. }
  650. static void sas_ata_task_done(struct sas_task *task)
  651. {
  652. struct ata_queued_cmd *qc = task->uldd_task;
  653. struct domain_device *dev = qc->ap->private_data;
  654. struct task_status_struct *stat = &task->task_status;
  655. struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
  656. enum ata_completion_errors ac;
  657. if (stat->stat == SAS_PROTO_RESPONSE) {
  658. ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
  659. qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
  660. dev->sata_dev.sstatus = resp->sstatus;
  661. dev->sata_dev.serror = resp->serror;
  662. dev->sata_dev.scontrol = resp->scontrol;
  663. dev->sata_dev.ap->sactive = resp->sactive;
  664. } else if (stat->stat != SAM_STAT_GOOD) {
  665. ac = sas_to_ata_err(stat);
  666. if (ac) {
  667. SAS_DPRINTK("%s: SAS error %x\n", __FUNCTION__,
  668. stat->stat);
  669. /* We saw a SAS error. Send a vague error. */
  670. qc->err_mask = ac;
  671. dev->sata_dev.tf.feature = 0x04; /* status err */
  672. dev->sata_dev.tf.command = ATA_ERR;
  673. }
  674. }
  675. ata_qc_complete(qc);
  676. list_del_init(&task->list);
  677. sas_free_task(task);
  678. }
  679. int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  680. {
  681. struct domain_device *dev = sdev_to_domain_dev(sdev);
  682. if (dev_is_sata(dev))
  683. return ata_scsi_ioctl(sdev, cmd, arg);
  684. return -EINVAL;
  685. }
  686. static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  687. {
  688. int res = -ENOMEM;
  689. struct sas_task *task;
  690. struct domain_device *dev = qc->ap->private_data;
  691. struct sas_ha_struct *sas_ha = dev->port->ha;
  692. struct Scsi_Host *host = sas_ha->core.shost;
  693. struct sas_internal *i = to_sas_internal(host->transportt);
  694. struct scatterlist *sg;
  695. unsigned int num = 0;
  696. unsigned int xfer = 0;
  697. task = sas_alloc_task(GFP_ATOMIC);
  698. if (!task)
  699. goto out;
  700. task->dev = dev;
  701. task->task_proto = SAS_PROTOCOL_STP;
  702. task->task_done = sas_ata_task_done;
  703. ata_tf_to_fis(&qc->tf, (u8*)&task->ata_task.fis, 0);
  704. task->uldd_task = qc;
  705. if (is_atapi_taskfile(&qc->tf)) {
  706. memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
  707. task->total_xfer_len = qc->nbytes + qc->pad_len;
  708. task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
  709. } else {
  710. ata_for_each_sg(sg, qc) {
  711. num++;
  712. xfer += sg->length;
  713. }
  714. task->total_xfer_len = xfer;
  715. task->num_scatter = num;
  716. }
  717. task->data_dir = qc->dma_dir;
  718. task->scatter = qc->__sg;
  719. task->ata_task.retry_count = 1;
  720. task->task_state_flags = SAS_TASK_STATE_PENDING;
  721. switch (qc->tf.protocol) {
  722. case ATA_PROT_NCQ:
  723. task->ata_task.use_ncq = 1;
  724. /* fall through */
  725. case ATA_PROT_ATAPI_DMA:
  726. case ATA_PROT_DMA:
  727. task->ata_task.dma_xfer = 1;
  728. break;
  729. }
  730. if (sas_ha->lldd_max_execute_num < 2)
  731. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  732. else
  733. res = sas_queue_up(task);
  734. /* Examine */
  735. if (res) {
  736. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  737. sas_free_task(task);
  738. if (res == -SAS_QUEUE_FULL)
  739. return -ENOMEM;
  740. }
  741. out:
  742. return res;
  743. }
  744. static u8 sas_ata_check_status(struct ata_port *ap)
  745. {
  746. struct domain_device *dev = ap->private_data;
  747. return dev->sata_dev.tf.command;
  748. }
  749. static void sas_ata_phy_reset(struct ata_port *ap)
  750. {
  751. struct domain_device *dev = ap->private_data;
  752. struct sas_internal *i =
  753. to_sas_internal(dev->port->ha->core.shost->transportt);
  754. int res = 0;
  755. if (i->dft->lldd_I_T_nexus_reset)
  756. res = i->dft->lldd_I_T_nexus_reset(dev);
  757. if (res)
  758. SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
  759. switch (dev->sata_dev.command_set) {
  760. case ATA_COMMAND_SET:
  761. SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
  762. ap->device[0].class = ATA_DEV_ATA;
  763. break;
  764. case ATAPI_COMMAND_SET:
  765. SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
  766. ap->device[0].class = ATA_DEV_ATAPI;
  767. break;
  768. default:
  769. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  770. __FUNCTION__,
  771. dev->sata_dev.command_set);
  772. ap->device[0].class = ATA_DEV_ATA;
  773. break;
  774. }
  775. ap->cbl = ATA_CBL_SATA;
  776. }
  777. static void sas_ata_post_internal(struct ata_queued_cmd *qc)
  778. {
  779. if (qc->flags & ATA_QCFLAG_FAILED)
  780. qc->err_mask |= AC_ERR_OTHER;
  781. if (qc->err_mask)
  782. SAS_DPRINTK("%s: Failure; reset phy!\n", __FUNCTION__);
  783. }
  784. static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  785. {
  786. struct domain_device *dev = ap->private_data;
  787. memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
  788. }
  789. static void sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  790. u32 val)
  791. {
  792. struct domain_device *dev = ap->private_data;
  793. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  794. switch (sc_reg_in) {
  795. case SCR_STATUS:
  796. dev->sata_dev.sstatus = val;
  797. break;
  798. case SCR_CONTROL:
  799. dev->sata_dev.scontrol = val;
  800. break;
  801. case SCR_ERROR:
  802. dev->sata_dev.serror = val;
  803. break;
  804. case SCR_ACTIVE:
  805. dev->sata_dev.ap->sactive = val;
  806. break;
  807. }
  808. }
  809. static u32 sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  810. {
  811. struct domain_device *dev = ap->private_data;
  812. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  813. switch (sc_reg_in) {
  814. case SCR_STATUS:
  815. return dev->sata_dev.sstatus;
  816. case SCR_CONTROL:
  817. return dev->sata_dev.scontrol;
  818. case SCR_ERROR:
  819. return dev->sata_dev.serror;
  820. case SCR_ACTIVE:
  821. return dev->sata_dev.ap->sactive;
  822. default:
  823. return 0xffffffffU;
  824. }
  825. }
  826. static struct ata_port_operations sas_sata_ops = {
  827. .port_disable = ata_port_disable,
  828. .check_status = sas_ata_check_status,
  829. .check_altstatus = sas_ata_check_status,
  830. .dev_select = ata_noop_dev_select,
  831. .phy_reset = sas_ata_phy_reset,
  832. .post_internal_cmd = sas_ata_post_internal,
  833. .tf_read = sas_ata_tf_read,
  834. .qc_prep = ata_noop_qc_prep,
  835. .qc_issue = sas_ata_qc_issue,
  836. .port_start = ata_sas_port_start,
  837. .port_stop = ata_sas_port_stop,
  838. .scr_read = sas_ata_scr_read,
  839. .scr_write = sas_ata_scr_write
  840. };
  841. static struct ata_port_info sata_port_info = {
  842. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
  843. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
  844. .pio_mask = 0x1f, /* PIO0-4 */
  845. .mwdma_mask = 0x07, /* MWDMA0-2 */
  846. .udma_mask = ATA_UDMA6,
  847. .port_ops = &sas_sata_ops
  848. };
  849. struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
  850. {
  851. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
  852. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  853. struct domain_device *found_dev = NULL;
  854. int i;
  855. unsigned long flags;
  856. spin_lock_irqsave(&ha->phy_port_lock, flags);
  857. for (i = 0; i < ha->num_phys; i++) {
  858. struct asd_sas_port *port = ha->sas_port[i];
  859. struct domain_device *dev;
  860. spin_lock(&port->dev_list_lock);
  861. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  862. if (rphy == dev->rphy) {
  863. found_dev = dev;
  864. spin_unlock(&port->dev_list_lock);
  865. goto found;
  866. }
  867. }
  868. spin_unlock(&port->dev_list_lock);
  869. }
  870. found:
  871. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  872. return found_dev;
  873. }
  874. static inline struct domain_device *sas_find_target(struct scsi_target *starget)
  875. {
  876. struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
  877. return sas_find_dev_by_rphy(rphy);
  878. }
  879. int sas_target_alloc(struct scsi_target *starget)
  880. {
  881. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  882. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  883. struct domain_device *found_dev = sas_find_target(starget);
  884. if (!found_dev)
  885. return -ENODEV;
  886. if (dev_is_sata(found_dev)) {
  887. struct ata_port *ap;
  888. ata_host_init(&found_dev->sata_dev.ata_host,
  889. &ha->pcidev->dev,
  890. sata_port_info.flags,
  891. &sas_sata_ops);
  892. ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
  893. &sata_port_info,
  894. shost);
  895. if (!ap) {
  896. SAS_DPRINTK("ata_sas_port_alloc failed.\n");
  897. return -ENODEV;
  898. }
  899. ap->private_data = found_dev;
  900. ap->cbl = ATA_CBL_SATA;
  901. ap->scsi_host = shost;
  902. found_dev->sata_dev.ap = ap;
  903. }
  904. starget->hostdata = found_dev;
  905. return 0;
  906. }
  907. #define SAS_DEF_QD 32
  908. #define SAS_MAX_QD 64
  909. int sas_slave_configure(struct scsi_device *scsi_dev)
  910. {
  911. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  912. struct sas_ha_struct *sas_ha;
  913. BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
  914. if (dev_is_sata(dev)) {
  915. ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
  916. return 0;
  917. }
  918. sas_ha = dev->port->ha;
  919. sas_read_port_mode_page(scsi_dev);
  920. if (scsi_dev->tagged_supported) {
  921. scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
  922. scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
  923. } else {
  924. SAS_DPRINTK("device %llx, LUN %x doesn't support "
  925. "TCQ\n", SAS_ADDR(dev->sas_addr),
  926. scsi_dev->lun);
  927. scsi_dev->tagged_supported = 0;
  928. scsi_set_tag_type(scsi_dev, 0);
  929. scsi_deactivate_tcq(scsi_dev, 1);
  930. }
  931. scsi_dev->allow_restart = 1;
  932. return 0;
  933. }
  934. void sas_slave_destroy(struct scsi_device *scsi_dev)
  935. {
  936. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  937. if (dev_is_sata(dev))
  938. ata_port_disable(dev->sata_dev.ap);
  939. }
  940. int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
  941. {
  942. int res = min(new_depth, SAS_MAX_QD);
  943. if (scsi_dev->tagged_supported)
  944. scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
  945. res);
  946. else {
  947. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  948. sas_printk("device %llx LUN %x queue depth changed to 1\n",
  949. SAS_ADDR(dev->sas_addr),
  950. scsi_dev->lun);
  951. scsi_adjust_queue_depth(scsi_dev, 0, 1);
  952. res = 1;
  953. }
  954. return res;
  955. }
  956. int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
  957. {
  958. if (!scsi_dev->tagged_supported)
  959. return 0;
  960. scsi_deactivate_tcq(scsi_dev, 1);
  961. scsi_set_tag_type(scsi_dev, qt);
  962. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  963. return qt;
  964. }
  965. int sas_bios_param(struct scsi_device *scsi_dev,
  966. struct block_device *bdev,
  967. sector_t capacity, int *hsc)
  968. {
  969. hsc[0] = 255;
  970. hsc[1] = 63;
  971. sector_div(capacity, 255*63);
  972. hsc[2] = capacity;
  973. return 0;
  974. }
  975. /* ---------- Task Collector Thread implementation ---------- */
  976. static void sas_queue(struct sas_ha_struct *sas_ha)
  977. {
  978. struct scsi_core *core = &sas_ha->core;
  979. unsigned long flags;
  980. LIST_HEAD(q);
  981. int can_queue;
  982. int res;
  983. struct sas_internal *i = to_sas_internal(core->shost->transportt);
  984. spin_lock_irqsave(&core->task_queue_lock, flags);
  985. while (!kthread_should_stop() &&
  986. !list_empty(&core->task_queue)) {
  987. can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
  988. if (can_queue >= 0) {
  989. can_queue = core->task_queue_size;
  990. list_splice_init(&core->task_queue, &q);
  991. } else {
  992. struct list_head *a, *n;
  993. can_queue = sas_ha->lldd_queue_size;
  994. list_for_each_safe(a, n, &core->task_queue) {
  995. list_move_tail(a, &q);
  996. if (--can_queue == 0)
  997. break;
  998. }
  999. can_queue = sas_ha->lldd_queue_size;
  1000. }
  1001. core->task_queue_size -= can_queue;
  1002. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  1003. {
  1004. struct sas_task *task = list_entry(q.next,
  1005. struct sas_task,
  1006. list);
  1007. list_del_init(&q);
  1008. res = i->dft->lldd_execute_task(task, can_queue,
  1009. GFP_KERNEL);
  1010. if (unlikely(res))
  1011. __list_add(&q, task->list.prev, &task->list);
  1012. }
  1013. spin_lock_irqsave(&core->task_queue_lock, flags);
  1014. if (res) {
  1015. list_splice_init(&q, &core->task_queue); /*at head*/
  1016. core->task_queue_size += can_queue;
  1017. }
  1018. }
  1019. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  1020. }
  1021. /**
  1022. * sas_queue_thread -- The Task Collector thread
  1023. * @_sas_ha: pointer to struct sas_ha
  1024. */
  1025. static int sas_queue_thread(void *_sas_ha)
  1026. {
  1027. struct sas_ha_struct *sas_ha = _sas_ha;
  1028. while (1) {
  1029. set_current_state(TASK_INTERRUPTIBLE);
  1030. schedule();
  1031. sas_queue(sas_ha);
  1032. if (kthread_should_stop())
  1033. break;
  1034. }
  1035. return 0;
  1036. }
  1037. int sas_init_queue(struct sas_ha_struct *sas_ha)
  1038. {
  1039. struct scsi_core *core = &sas_ha->core;
  1040. spin_lock_init(&core->task_queue_lock);
  1041. core->task_queue_size = 0;
  1042. INIT_LIST_HEAD(&core->task_queue);
  1043. core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
  1044. "sas_queue_%d", core->shost->host_no);
  1045. if (IS_ERR(core->queue_thread))
  1046. return PTR_ERR(core->queue_thread);
  1047. return 0;
  1048. }
  1049. void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
  1050. {
  1051. unsigned long flags;
  1052. struct scsi_core *core = &sas_ha->core;
  1053. struct sas_task *task, *n;
  1054. kthread_stop(core->queue_thread);
  1055. if (!list_empty(&core->task_queue))
  1056. SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
  1057. SAS_ADDR(sas_ha->sas_addr));
  1058. spin_lock_irqsave(&core->task_queue_lock, flags);
  1059. list_for_each_entry_safe(task, n, &core->task_queue, list) {
  1060. struct scsi_cmnd *cmd = task->uldd_task;
  1061. list_del_init(&task->list);
  1062. ASSIGN_SAS_TASK(cmd, NULL);
  1063. sas_free_task(task);
  1064. cmd->result = DID_ABORT << 16;
  1065. cmd->scsi_done(cmd);
  1066. }
  1067. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  1068. }
  1069. /*
  1070. * Call the LLDD task abort routine directly. This function is intended for
  1071. * use by upper layers that need to tell the LLDD to abort a task.
  1072. */
  1073. int __sas_task_abort(struct sas_task *task)
  1074. {
  1075. struct sas_internal *si =
  1076. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  1077. unsigned long flags;
  1078. int res;
  1079. spin_lock_irqsave(&task->task_state_lock, flags);
  1080. if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
  1081. task->task_state_flags & SAS_TASK_STATE_DONE) {
  1082. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1083. SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
  1084. task);
  1085. return 0;
  1086. }
  1087. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  1088. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1089. if (!si->dft->lldd_abort_task)
  1090. return -ENODEV;
  1091. res = si->dft->lldd_abort_task(task);
  1092. spin_lock_irqsave(&task->task_state_lock, flags);
  1093. if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
  1094. (res == TMF_RESP_FUNC_COMPLETE))
  1095. {
  1096. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1097. task->task_done(task);
  1098. return 0;
  1099. }
  1100. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  1101. task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
  1102. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1103. return -EAGAIN;
  1104. }
  1105. /*
  1106. * Tell an upper layer that it needs to initiate an abort for a given task.
  1107. * This should only ever be called by an LLDD.
  1108. */
  1109. void sas_task_abort(struct sas_task *task)
  1110. {
  1111. struct scsi_cmnd *sc = task->uldd_task;
  1112. /* Escape for libsas internal commands */
  1113. if (!sc) {
  1114. if (!del_timer(&task->timer))
  1115. return;
  1116. task->timer.function(task->timer.data);
  1117. return;
  1118. }
  1119. scsi_req_abort_cmd(sc);
  1120. scsi_schedule_eh(sc->device->host);
  1121. }
  1122. int sas_slave_alloc(struct scsi_device *scsi_dev)
  1123. {
  1124. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  1125. if (dev_is_sata(dev))
  1126. return ata_sas_port_init(dev->sata_dev.ap);
  1127. return 0;
  1128. }
  1129. void sas_target_destroy(struct scsi_target *starget)
  1130. {
  1131. struct domain_device *found_dev = sas_find_target(starget);
  1132. if (!found_dev)
  1133. return;
  1134. if (dev_is_sata(found_dev))
  1135. ata_sas_port_destroy(found_dev->sata_dev.ap);
  1136. return;
  1137. }
  1138. EXPORT_SYMBOL_GPL(sas_queuecommand);
  1139. EXPORT_SYMBOL_GPL(sas_target_alloc);
  1140. EXPORT_SYMBOL_GPL(sas_slave_configure);
  1141. EXPORT_SYMBOL_GPL(sas_slave_destroy);
  1142. EXPORT_SYMBOL_GPL(sas_change_queue_depth);
  1143. EXPORT_SYMBOL_GPL(sas_change_queue_type);
  1144. EXPORT_SYMBOL_GPL(sas_bios_param);
  1145. EXPORT_SYMBOL_GPL(__sas_task_abort);
  1146. EXPORT_SYMBOL_GPL(sas_task_abort);
  1147. EXPORT_SYMBOL_GPL(sas_phy_reset);
  1148. EXPORT_SYMBOL_GPL(sas_phy_enable);
  1149. EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
  1150. EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
  1151. EXPORT_SYMBOL_GPL(sas_slave_alloc);
  1152. EXPORT_SYMBOL_GPL(sas_target_destroy);
  1153. EXPORT_SYMBOL_GPL(sas_ioctl);