sas_scsi_host.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 <linux/firmware.h>
  27. #include <linux/export.h>
  28. #include <linux/ctype.h>
  29. #include "sas_internal.h"
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_transport.h>
  36. #include <scsi/scsi_transport_sas.h>
  37. #include <scsi/sas_ata.h>
  38. #include "../scsi_sas_internal.h"
  39. #include "../scsi_transport_api.h"
  40. #include "../scsi_priv.h"
  41. #include <linux/err.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/freezer.h>
  44. #include <linux/gfp.h>
  45. #include <linux/scatterlist.h>
  46. #include <linux/libata.h>
  47. /* record final status and free the task */
  48. static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
  49. {
  50. struct task_status_struct *ts = &task->task_status;
  51. int hs = 0, stat = 0;
  52. if (ts->resp == SAS_TASK_UNDELIVERED) {
  53. /* transport error */
  54. hs = DID_NO_CONNECT;
  55. } else { /* ts->resp == SAS_TASK_COMPLETE */
  56. /* task delivered, what happened afterwards? */
  57. switch (ts->stat) {
  58. case SAS_DEV_NO_RESPONSE:
  59. case SAS_INTERRUPTED:
  60. case SAS_PHY_DOWN:
  61. case SAS_NAK_R_ERR:
  62. case SAS_OPEN_TO:
  63. hs = DID_NO_CONNECT;
  64. break;
  65. case SAS_DATA_UNDERRUN:
  66. scsi_set_resid(sc, ts->residual);
  67. if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
  68. hs = DID_ERROR;
  69. break;
  70. case SAS_DATA_OVERRUN:
  71. hs = DID_ERROR;
  72. break;
  73. case SAS_QUEUE_FULL:
  74. hs = DID_SOFT_ERROR; /* retry */
  75. break;
  76. case SAS_DEVICE_UNKNOWN:
  77. hs = DID_BAD_TARGET;
  78. break;
  79. case SAS_SG_ERR:
  80. hs = DID_PARITY;
  81. break;
  82. case SAS_OPEN_REJECT:
  83. if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
  84. hs = DID_SOFT_ERROR; /* retry */
  85. else
  86. hs = DID_ERROR;
  87. break;
  88. case SAS_PROTO_RESPONSE:
  89. SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
  90. "task; please report this\n",
  91. task->dev->port->ha->sas_ha_name);
  92. break;
  93. case SAS_ABORTED_TASK:
  94. hs = DID_ABORT;
  95. break;
  96. case SAM_STAT_CHECK_CONDITION:
  97. memcpy(sc->sense_buffer, ts->buf,
  98. min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
  99. stat = SAM_STAT_CHECK_CONDITION;
  100. break;
  101. default:
  102. stat = ts->stat;
  103. break;
  104. }
  105. }
  106. sc->result = (hs << 16) | stat;
  107. ASSIGN_SAS_TASK(sc, NULL);
  108. list_del_init(&task->list);
  109. sas_free_task(task);
  110. }
  111. static void sas_scsi_task_done(struct sas_task *task)
  112. {
  113. struct scsi_cmnd *sc = task->uldd_task;
  114. struct domain_device *dev = task->dev;
  115. struct sas_ha_struct *ha = dev->port->ha;
  116. unsigned long flags;
  117. spin_lock_irqsave(&dev->done_lock, flags);
  118. if (test_bit(SAS_HA_FROZEN, &ha->state))
  119. task = NULL;
  120. else
  121. ASSIGN_SAS_TASK(sc, NULL);
  122. spin_unlock_irqrestore(&dev->done_lock, flags);
  123. if (unlikely(!task)) {
  124. /* task will be completed by the error handler */
  125. SAS_DPRINTK("task done but aborted\n");
  126. return;
  127. }
  128. if (unlikely(!sc)) {
  129. SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
  130. list_del_init(&task->list);
  131. sas_free_task(task);
  132. return;
  133. }
  134. sas_end_task(sc, task);
  135. sc->scsi_done(sc);
  136. }
  137. static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
  138. struct domain_device *dev,
  139. gfp_t gfp_flags)
  140. {
  141. struct sas_task *task = sas_alloc_task(gfp_flags);
  142. struct scsi_lun lun;
  143. if (!task)
  144. return NULL;
  145. task->uldd_task = cmd;
  146. ASSIGN_SAS_TASK(cmd, task);
  147. task->dev = dev;
  148. task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
  149. task->ssp_task.retry_count = 1;
  150. int_to_scsilun(cmd->device->lun, &lun);
  151. memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
  152. task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
  153. memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
  154. task->scatter = scsi_sglist(cmd);
  155. task->num_scatter = scsi_sg_count(cmd);
  156. task->total_xfer_len = scsi_bufflen(cmd);
  157. task->data_dir = cmd->sc_data_direction;
  158. task->task_done = sas_scsi_task_done;
  159. return task;
  160. }
  161. int sas_queue_up(struct sas_task *task)
  162. {
  163. struct sas_ha_struct *sas_ha = task->dev->port->ha;
  164. struct scsi_core *core = &sas_ha->core;
  165. unsigned long flags;
  166. LIST_HEAD(list);
  167. spin_lock_irqsave(&core->task_queue_lock, flags);
  168. if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
  169. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  170. return -SAS_QUEUE_FULL;
  171. }
  172. list_add_tail(&task->list, &core->task_queue);
  173. core->task_queue_size += 1;
  174. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  175. wake_up_process(core->queue_thread);
  176. return 0;
  177. }
  178. int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  179. {
  180. struct sas_internal *i = to_sas_internal(host->transportt);
  181. struct domain_device *dev = cmd_to_domain_dev(cmd);
  182. struct sas_ha_struct *sas_ha = dev->port->ha;
  183. struct sas_task *task;
  184. int res = 0;
  185. /* If the device fell off, no sense in issuing commands */
  186. if (test_bit(SAS_DEV_GONE, &dev->state)) {
  187. cmd->result = DID_BAD_TARGET << 16;
  188. goto out_done;
  189. }
  190. if (dev_is_sata(dev)) {
  191. spin_lock_irq(dev->sata_dev.ap->lock);
  192. res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
  193. spin_unlock_irq(dev->sata_dev.ap->lock);
  194. return res;
  195. }
  196. task = sas_create_task(cmd, dev, GFP_ATOMIC);
  197. if (!task)
  198. return SCSI_MLQUEUE_HOST_BUSY;
  199. /* Queue up, Direct Mode or Task Collector Mode. */
  200. if (sas_ha->lldd_max_execute_num < 2)
  201. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  202. else
  203. res = sas_queue_up(task);
  204. if (res)
  205. goto out_free_task;
  206. return 0;
  207. out_free_task:
  208. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  209. ASSIGN_SAS_TASK(cmd, NULL);
  210. sas_free_task(task);
  211. if (res == -SAS_QUEUE_FULL)
  212. cmd->result = DID_SOFT_ERROR << 16; /* retry */
  213. else
  214. cmd->result = DID_ERROR << 16;
  215. out_done:
  216. cmd->scsi_done(cmd);
  217. return 0;
  218. }
  219. static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
  220. {
  221. struct sas_task *task = TO_SAS_TASK(cmd);
  222. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
  223. /* At this point, we only get called following an actual abort
  224. * of the task, so we should be guaranteed not to be racing with
  225. * any completions from the LLD. Task is freed after this.
  226. */
  227. sas_end_task(cmd, task);
  228. /* now finish the command and move it on to the error
  229. * handler done list, this also takes it off the
  230. * error handler pending list.
  231. */
  232. scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
  233. }
  234. static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
  235. {
  236. struct scsi_cmnd *cmd, *n;
  237. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  238. if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
  239. cmd->device->lun == my_cmd->device->lun)
  240. sas_eh_finish_cmd(cmd);
  241. }
  242. }
  243. static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
  244. struct domain_device *dev)
  245. {
  246. struct scsi_cmnd *cmd, *n;
  247. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  248. struct domain_device *x = cmd_to_domain_dev(cmd);
  249. if (x == dev)
  250. sas_eh_finish_cmd(cmd);
  251. }
  252. }
  253. static void sas_scsi_clear_queue_port(struct list_head *error_q,
  254. struct asd_sas_port *port)
  255. {
  256. struct scsi_cmnd *cmd, *n;
  257. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  258. struct domain_device *dev = cmd_to_domain_dev(cmd);
  259. struct asd_sas_port *x = dev->port;
  260. if (x == port)
  261. sas_eh_finish_cmd(cmd);
  262. }
  263. }
  264. enum task_disposition {
  265. TASK_IS_DONE,
  266. TASK_IS_ABORTED,
  267. TASK_IS_AT_LU,
  268. TASK_IS_NOT_AT_HA,
  269. TASK_IS_NOT_AT_LU,
  270. TASK_ABORT_FAILED,
  271. };
  272. static enum task_disposition sas_scsi_find_task(struct sas_task *task)
  273. {
  274. struct sas_ha_struct *ha = task->dev->port->ha;
  275. unsigned long flags;
  276. int i, res;
  277. struct sas_internal *si =
  278. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  279. if (ha->lldd_max_execute_num > 1) {
  280. struct scsi_core *core = &ha->core;
  281. struct sas_task *t, *n;
  282. mutex_lock(&core->task_queue_flush);
  283. spin_lock_irqsave(&core->task_queue_lock, flags);
  284. list_for_each_entry_safe(t, n, &core->task_queue, list)
  285. if (task == t) {
  286. list_del_init(&t->list);
  287. break;
  288. }
  289. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  290. mutex_unlock(&core->task_queue_flush);
  291. if (task == t)
  292. return TASK_IS_NOT_AT_HA;
  293. }
  294. for (i = 0; i < 5; i++) {
  295. SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
  296. res = si->dft->lldd_abort_task(task);
  297. spin_lock_irqsave(&task->task_state_lock, flags);
  298. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  299. spin_unlock_irqrestore(&task->task_state_lock, flags);
  300. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  301. task);
  302. return TASK_IS_DONE;
  303. }
  304. spin_unlock_irqrestore(&task->task_state_lock, flags);
  305. if (res == TMF_RESP_FUNC_COMPLETE) {
  306. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  307. __func__, task);
  308. return TASK_IS_ABORTED;
  309. } else if (si->dft->lldd_query_task) {
  310. SAS_DPRINTK("%s: querying task 0x%p\n",
  311. __func__, task);
  312. res = si->dft->lldd_query_task(task);
  313. switch (res) {
  314. case TMF_RESP_FUNC_SUCC:
  315. SAS_DPRINTK("%s: task 0x%p at LU\n",
  316. __func__, task);
  317. return TASK_IS_AT_LU;
  318. case TMF_RESP_FUNC_COMPLETE:
  319. SAS_DPRINTK("%s: task 0x%p not at LU\n",
  320. __func__, task);
  321. return TASK_IS_NOT_AT_LU;
  322. case TMF_RESP_FUNC_FAILED:
  323. SAS_DPRINTK("%s: task 0x%p failed to abort\n",
  324. __func__, task);
  325. return TASK_ABORT_FAILED;
  326. }
  327. }
  328. }
  329. return res;
  330. }
  331. static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
  332. {
  333. int res = TMF_RESP_FUNC_FAILED;
  334. struct scsi_lun lun;
  335. struct sas_internal *i =
  336. to_sas_internal(dev->port->ha->core.shost->transportt);
  337. int_to_scsilun(cmd->device->lun, &lun);
  338. SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
  339. SAS_ADDR(dev->sas_addr),
  340. cmd->device->lun);
  341. if (i->dft->lldd_abort_task_set)
  342. res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
  343. if (res == TMF_RESP_FUNC_FAILED) {
  344. if (i->dft->lldd_clear_task_set)
  345. res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
  346. }
  347. if (res == TMF_RESP_FUNC_FAILED) {
  348. if (i->dft->lldd_lu_reset)
  349. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  350. }
  351. return res;
  352. }
  353. static int sas_recover_I_T(struct domain_device *dev)
  354. {
  355. int res = TMF_RESP_FUNC_FAILED;
  356. struct sas_internal *i =
  357. to_sas_internal(dev->port->ha->core.shost->transportt);
  358. SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
  359. SAS_ADDR(dev->sas_addr));
  360. if (i->dft->lldd_I_T_nexus_reset)
  361. res = i->dft->lldd_I_T_nexus_reset(dev);
  362. return res;
  363. }
  364. /* Find the sas_phy that's attached to this device */
  365. struct sas_phy *sas_find_local_phy(struct domain_device *dev)
  366. {
  367. struct domain_device *pdev = dev->parent;
  368. struct ex_phy *exphy = NULL;
  369. int i;
  370. /* Directly attached device */
  371. if (!pdev)
  372. return dev->port->phy;
  373. /* Otherwise look in the expander */
  374. for (i = 0; i < pdev->ex_dev.num_phys; i++)
  375. if (!memcmp(dev->sas_addr,
  376. pdev->ex_dev.ex_phy[i].attached_sas_addr,
  377. SAS_ADDR_SIZE)) {
  378. exphy = &pdev->ex_dev.ex_phy[i];
  379. break;
  380. }
  381. BUG_ON(!exphy);
  382. return exphy->phy;
  383. }
  384. EXPORT_SYMBOL_GPL(sas_find_local_phy);
  385. /* Attempt to send a LUN reset message to a device */
  386. int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
  387. {
  388. struct domain_device *dev = cmd_to_domain_dev(cmd);
  389. struct sas_internal *i =
  390. to_sas_internal(dev->port->ha->core.shost->transportt);
  391. struct scsi_lun lun;
  392. int res;
  393. int_to_scsilun(cmd->device->lun, &lun);
  394. if (!i->dft->lldd_lu_reset)
  395. return FAILED;
  396. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  397. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  398. return SUCCESS;
  399. return FAILED;
  400. }
  401. /* Attempt to send a phy (bus) reset */
  402. int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  403. {
  404. struct domain_device *dev = cmd_to_domain_dev(cmd);
  405. struct sas_phy *phy = sas_find_local_phy(dev);
  406. int res;
  407. res = sas_phy_reset(phy, 1);
  408. if (res)
  409. SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
  410. kobject_name(&phy->dev.kobj),
  411. res);
  412. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  413. return SUCCESS;
  414. return FAILED;
  415. }
  416. /* Try to reset a device */
  417. static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
  418. {
  419. int res;
  420. struct Scsi_Host *shost = cmd->device->host;
  421. if (!shost->hostt->eh_device_reset_handler)
  422. goto try_bus_reset;
  423. res = shost->hostt->eh_device_reset_handler(cmd);
  424. if (res == SUCCESS)
  425. return res;
  426. try_bus_reset:
  427. if (shost->hostt->eh_bus_reset_handler)
  428. return shost->hostt->eh_bus_reset_handler(cmd);
  429. return FAILED;
  430. }
  431. static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
  432. struct list_head *work_q)
  433. {
  434. struct scsi_cmnd *cmd, *n;
  435. enum task_disposition res = TASK_IS_DONE;
  436. int tmf_resp, need_reset;
  437. struct sas_internal *i = to_sas_internal(shost->transportt);
  438. unsigned long flags;
  439. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  440. Again:
  441. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  442. struct domain_device *dev = cmd_to_domain_dev(cmd);
  443. struct sas_task *task;
  444. spin_lock_irqsave(&dev->done_lock, flags);
  445. /* by this point the lldd has either observed
  446. * SAS_HA_FROZEN and is leaving the task alone, or has
  447. * won the race with eh and decided to complete it
  448. */
  449. task = TO_SAS_TASK(cmd);
  450. spin_unlock_irqrestore(&dev->done_lock, flags);
  451. if (!task)
  452. continue;
  453. list_del_init(&cmd->eh_entry);
  454. spin_lock_irqsave(&task->task_state_lock, flags);
  455. need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
  456. spin_unlock_irqrestore(&task->task_state_lock, flags);
  457. if (need_reset) {
  458. SAS_DPRINTK("%s: task 0x%p requests reset\n",
  459. __func__, task);
  460. goto reset;
  461. }
  462. SAS_DPRINTK("trying to find task 0x%p\n", task);
  463. res = sas_scsi_find_task(task);
  464. cmd->eh_eflags = 0;
  465. switch (res) {
  466. case TASK_IS_NOT_AT_HA:
  467. SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
  468. __func__, task,
  469. cmd->retries ? "retry" : "aborted");
  470. if (cmd->retries)
  471. cmd->retries--;
  472. sas_eh_finish_cmd(cmd);
  473. continue;
  474. case TASK_IS_DONE:
  475. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  476. task);
  477. sas_eh_finish_cmd(cmd);
  478. continue;
  479. case TASK_IS_ABORTED:
  480. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  481. __func__, task);
  482. sas_eh_finish_cmd(cmd);
  483. continue;
  484. case TASK_IS_AT_LU:
  485. SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
  486. reset:
  487. tmf_resp = sas_recover_lu(task->dev, cmd);
  488. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  489. SAS_DPRINTK("dev %016llx LU %x is "
  490. "recovered\n",
  491. SAS_ADDR(task->dev),
  492. cmd->device->lun);
  493. sas_eh_finish_cmd(cmd);
  494. sas_scsi_clear_queue_lu(work_q, cmd);
  495. goto Again;
  496. }
  497. /* fallthrough */
  498. case TASK_IS_NOT_AT_LU:
  499. case TASK_ABORT_FAILED:
  500. SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
  501. task);
  502. tmf_resp = sas_recover_I_T(task->dev);
  503. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  504. struct domain_device *dev = task->dev;
  505. SAS_DPRINTK("I_T %016llx recovered\n",
  506. SAS_ADDR(task->dev->sas_addr));
  507. sas_eh_finish_cmd(cmd);
  508. sas_scsi_clear_queue_I_T(work_q, dev);
  509. goto Again;
  510. }
  511. /* Hammer time :-) */
  512. try_to_reset_cmd_device(cmd);
  513. if (i->dft->lldd_clear_nexus_port) {
  514. struct asd_sas_port *port = task->dev->port;
  515. SAS_DPRINTK("clearing nexus for port:%d\n",
  516. port->id);
  517. res = i->dft->lldd_clear_nexus_port(port);
  518. if (res == TMF_RESP_FUNC_COMPLETE) {
  519. SAS_DPRINTK("clear nexus port:%d "
  520. "succeeded\n", port->id);
  521. sas_eh_finish_cmd(cmd);
  522. sas_scsi_clear_queue_port(work_q,
  523. port);
  524. goto Again;
  525. }
  526. }
  527. if (i->dft->lldd_clear_nexus_ha) {
  528. SAS_DPRINTK("clear nexus ha\n");
  529. res = i->dft->lldd_clear_nexus_ha(ha);
  530. if (res == TMF_RESP_FUNC_COMPLETE) {
  531. SAS_DPRINTK("clear nexus ha "
  532. "succeeded\n");
  533. sas_eh_finish_cmd(cmd);
  534. goto clear_q;
  535. }
  536. }
  537. /* If we are here -- this means that no amount
  538. * of effort could recover from errors. Quite
  539. * possibly the HA just disappeared.
  540. */
  541. SAS_DPRINTK("error from device %llx, LUN %x "
  542. "couldn't be recovered in any way\n",
  543. SAS_ADDR(task->dev->sas_addr),
  544. cmd->device->lun);
  545. sas_eh_finish_cmd(cmd);
  546. goto clear_q;
  547. }
  548. }
  549. return list_empty(work_q);
  550. clear_q:
  551. SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
  552. list_for_each_entry_safe(cmd, n, work_q, eh_entry)
  553. sas_eh_finish_cmd(cmd);
  554. return list_empty(work_q);
  555. }
  556. void sas_scsi_recover_host(struct Scsi_Host *shost)
  557. {
  558. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  559. unsigned long flags;
  560. LIST_HEAD(eh_work_q);
  561. spin_lock_irqsave(shost->host_lock, flags);
  562. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  563. shost->host_eh_scheduled = 0;
  564. spin_unlock_irqrestore(shost->host_lock, flags);
  565. SAS_DPRINTK("Enter %s\n", __func__);
  566. /*
  567. * Deal with commands that still have SAS tasks (i.e. they didn't
  568. * complete via the normal sas_task completion mechanism)
  569. */
  570. set_bit(SAS_HA_FROZEN, &ha->state);
  571. if (sas_eh_handle_sas_errors(shost, &eh_work_q))
  572. goto out;
  573. /*
  574. * Now deal with SCSI commands that completed ok but have a an error
  575. * code (and hopefully sense data) attached. This is roughly what
  576. * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
  577. * command we see here has no sas_task and is thus unknown to the HA.
  578. */
  579. if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q))
  580. if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
  581. scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
  582. out:
  583. clear_bit(SAS_HA_FROZEN, &ha->state);
  584. if (ha->lldd_max_execute_num > 1)
  585. wake_up_process(ha->core.queue_thread);
  586. /* now link into libata eh --- if we have any ata devices */
  587. sas_ata_strategy_handler(shost);
  588. scsi_eh_flush_done_q(&ha->eh_done_q);
  589. SAS_DPRINTK("--- Exit %s\n", __func__);
  590. return;
  591. }
  592. enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
  593. {
  594. scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
  595. return BLK_EH_NOT_HANDLED;
  596. }
  597. int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  598. {
  599. struct domain_device *dev = sdev_to_domain_dev(sdev);
  600. if (dev_is_sata(dev))
  601. return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
  602. return -EINVAL;
  603. }
  604. struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
  605. {
  606. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
  607. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  608. struct domain_device *found_dev = NULL;
  609. int i;
  610. unsigned long flags;
  611. spin_lock_irqsave(&ha->phy_port_lock, flags);
  612. for (i = 0; i < ha->num_phys; i++) {
  613. struct asd_sas_port *port = ha->sas_port[i];
  614. struct domain_device *dev;
  615. spin_lock(&port->dev_list_lock);
  616. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  617. if (rphy == dev->rphy) {
  618. found_dev = dev;
  619. spin_unlock(&port->dev_list_lock);
  620. goto found;
  621. }
  622. }
  623. spin_unlock(&port->dev_list_lock);
  624. }
  625. found:
  626. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  627. return found_dev;
  628. }
  629. int sas_target_alloc(struct scsi_target *starget)
  630. {
  631. struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
  632. struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
  633. int res;
  634. if (!found_dev)
  635. return -ENODEV;
  636. if (dev_is_sata(found_dev)) {
  637. res = sas_ata_init_host_and_port(found_dev, starget);
  638. if (res)
  639. return res;
  640. }
  641. kref_get(&found_dev->kref);
  642. starget->hostdata = found_dev;
  643. return 0;
  644. }
  645. #define SAS_DEF_QD 256
  646. int sas_slave_configure(struct scsi_device *scsi_dev)
  647. {
  648. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  649. struct sas_ha_struct *sas_ha;
  650. BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
  651. if (dev_is_sata(dev)) {
  652. ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
  653. return 0;
  654. }
  655. sas_ha = dev->port->ha;
  656. sas_read_port_mode_page(scsi_dev);
  657. if (scsi_dev->tagged_supported) {
  658. scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
  659. scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
  660. } else {
  661. SAS_DPRINTK("device %llx, LUN %x doesn't support "
  662. "TCQ\n", SAS_ADDR(dev->sas_addr),
  663. scsi_dev->lun);
  664. scsi_dev->tagged_supported = 0;
  665. scsi_set_tag_type(scsi_dev, 0);
  666. scsi_deactivate_tcq(scsi_dev, 1);
  667. }
  668. scsi_dev->allow_restart = 1;
  669. return 0;
  670. }
  671. int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
  672. {
  673. struct domain_device *dev = sdev_to_domain_dev(sdev);
  674. if (dev_is_sata(dev))
  675. return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
  676. reason);
  677. switch (reason) {
  678. case SCSI_QDEPTH_DEFAULT:
  679. case SCSI_QDEPTH_RAMP_UP:
  680. if (!sdev->tagged_supported)
  681. depth = 1;
  682. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  683. break;
  684. case SCSI_QDEPTH_QFULL:
  685. scsi_track_queue_full(sdev, depth);
  686. break;
  687. default:
  688. return -EOPNOTSUPP;
  689. }
  690. return depth;
  691. }
  692. int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
  693. {
  694. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  695. if (dev_is_sata(dev))
  696. return -EINVAL;
  697. if (!scsi_dev->tagged_supported)
  698. return 0;
  699. scsi_deactivate_tcq(scsi_dev, 1);
  700. scsi_set_tag_type(scsi_dev, qt);
  701. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  702. return qt;
  703. }
  704. int sas_bios_param(struct scsi_device *scsi_dev,
  705. struct block_device *bdev,
  706. sector_t capacity, int *hsc)
  707. {
  708. hsc[0] = 255;
  709. hsc[1] = 63;
  710. sector_div(capacity, 255*63);
  711. hsc[2] = capacity;
  712. return 0;
  713. }
  714. /* ---------- Task Collector Thread implementation ---------- */
  715. static void sas_queue(struct sas_ha_struct *sas_ha)
  716. {
  717. struct scsi_core *core = &sas_ha->core;
  718. unsigned long flags;
  719. LIST_HEAD(q);
  720. int can_queue;
  721. int res;
  722. struct sas_internal *i = to_sas_internal(core->shost->transportt);
  723. mutex_lock(&core->task_queue_flush);
  724. spin_lock_irqsave(&core->task_queue_lock, flags);
  725. while (!kthread_should_stop() &&
  726. !list_empty(&core->task_queue) &&
  727. !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
  728. can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
  729. if (can_queue >= 0) {
  730. can_queue = core->task_queue_size;
  731. list_splice_init(&core->task_queue, &q);
  732. } else {
  733. struct list_head *a, *n;
  734. can_queue = sas_ha->lldd_queue_size;
  735. list_for_each_safe(a, n, &core->task_queue) {
  736. list_move_tail(a, &q);
  737. if (--can_queue == 0)
  738. break;
  739. }
  740. can_queue = sas_ha->lldd_queue_size;
  741. }
  742. core->task_queue_size -= can_queue;
  743. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  744. {
  745. struct sas_task *task = list_entry(q.next,
  746. struct sas_task,
  747. list);
  748. list_del_init(&q);
  749. res = i->dft->lldd_execute_task(task, can_queue,
  750. GFP_KERNEL);
  751. if (unlikely(res))
  752. __list_add(&q, task->list.prev, &task->list);
  753. }
  754. spin_lock_irqsave(&core->task_queue_lock, flags);
  755. if (res) {
  756. list_splice_init(&q, &core->task_queue); /*at head*/
  757. core->task_queue_size += can_queue;
  758. }
  759. }
  760. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  761. mutex_unlock(&core->task_queue_flush);
  762. }
  763. /**
  764. * sas_queue_thread -- The Task Collector thread
  765. * @_sas_ha: pointer to struct sas_ha
  766. */
  767. static int sas_queue_thread(void *_sas_ha)
  768. {
  769. struct sas_ha_struct *sas_ha = _sas_ha;
  770. while (1) {
  771. set_current_state(TASK_INTERRUPTIBLE);
  772. schedule();
  773. sas_queue(sas_ha);
  774. if (kthread_should_stop())
  775. break;
  776. }
  777. return 0;
  778. }
  779. int sas_init_queue(struct sas_ha_struct *sas_ha)
  780. {
  781. struct scsi_core *core = &sas_ha->core;
  782. spin_lock_init(&core->task_queue_lock);
  783. mutex_init(&core->task_queue_flush);
  784. core->task_queue_size = 0;
  785. INIT_LIST_HEAD(&core->task_queue);
  786. core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
  787. "sas_queue_%d", core->shost->host_no);
  788. if (IS_ERR(core->queue_thread))
  789. return PTR_ERR(core->queue_thread);
  790. return 0;
  791. }
  792. void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
  793. {
  794. unsigned long flags;
  795. struct scsi_core *core = &sas_ha->core;
  796. struct sas_task *task, *n;
  797. kthread_stop(core->queue_thread);
  798. if (!list_empty(&core->task_queue))
  799. SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
  800. SAS_ADDR(sas_ha->sas_addr));
  801. spin_lock_irqsave(&core->task_queue_lock, flags);
  802. list_for_each_entry_safe(task, n, &core->task_queue, list) {
  803. struct scsi_cmnd *cmd = task->uldd_task;
  804. list_del_init(&task->list);
  805. ASSIGN_SAS_TASK(cmd, NULL);
  806. sas_free_task(task);
  807. cmd->result = DID_ABORT << 16;
  808. cmd->scsi_done(cmd);
  809. }
  810. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  811. }
  812. /*
  813. * Tell an upper layer that it needs to initiate an abort for a given task.
  814. * This should only ever be called by an LLDD.
  815. */
  816. void sas_task_abort(struct sas_task *task)
  817. {
  818. struct scsi_cmnd *sc = task->uldd_task;
  819. /* Escape for libsas internal commands */
  820. if (!sc) {
  821. if (!del_timer(&task->timer))
  822. return;
  823. task->timer.function(task->timer.data);
  824. return;
  825. }
  826. if (dev_is_sata(task->dev)) {
  827. sas_ata_task_abort(task);
  828. } else {
  829. struct request_queue *q = sc->device->request_queue;
  830. unsigned long flags;
  831. spin_lock_irqsave(q->queue_lock, flags);
  832. blk_abort_request(sc->request);
  833. spin_unlock_irqrestore(q->queue_lock, flags);
  834. scsi_schedule_eh(sc->device->host);
  835. }
  836. }
  837. int sas_slave_alloc(struct scsi_device *scsi_dev)
  838. {
  839. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  840. if (dev_is_sata(dev))
  841. return ata_sas_port_init(dev->sata_dev.ap);
  842. return 0;
  843. }
  844. void sas_target_destroy(struct scsi_target *starget)
  845. {
  846. struct domain_device *found_dev = starget->hostdata;
  847. if (!found_dev)
  848. return;
  849. if (dev_is_sata(found_dev))
  850. ata_sas_port_destroy(found_dev->sata_dev.ap);
  851. starget->hostdata = NULL;
  852. sas_put_device(found_dev);
  853. }
  854. static void sas_parse_addr(u8 *sas_addr, const char *p)
  855. {
  856. int i;
  857. for (i = 0; i < SAS_ADDR_SIZE; i++) {
  858. u8 h, l;
  859. if (!*p)
  860. break;
  861. h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  862. p++;
  863. l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  864. p++;
  865. sas_addr[i] = (h<<4) | l;
  866. }
  867. }
  868. #define SAS_STRING_ADDR_SIZE 16
  869. int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
  870. {
  871. int res;
  872. const struct firmware *fw;
  873. res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
  874. if (res)
  875. return res;
  876. if (fw->size < SAS_STRING_ADDR_SIZE) {
  877. res = -ENODEV;
  878. goto out;
  879. }
  880. sas_parse_addr(addr, fw->data);
  881. out:
  882. release_firmware(fw);
  883. return res;
  884. }
  885. EXPORT_SYMBOL_GPL(sas_request_addr);
  886. EXPORT_SYMBOL_GPL(sas_queuecommand);
  887. EXPORT_SYMBOL_GPL(sas_target_alloc);
  888. EXPORT_SYMBOL_GPL(sas_slave_configure);
  889. EXPORT_SYMBOL_GPL(sas_change_queue_depth);
  890. EXPORT_SYMBOL_GPL(sas_change_queue_type);
  891. EXPORT_SYMBOL_GPL(sas_bios_param);
  892. EXPORT_SYMBOL_GPL(sas_task_abort);
  893. EXPORT_SYMBOL_GPL(sas_phy_reset);
  894. EXPORT_SYMBOL_GPL(sas_phy_enable);
  895. EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
  896. EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
  897. EXPORT_SYMBOL_GPL(sas_slave_alloc);
  898. EXPORT_SYMBOL_GPL(sas_target_destroy);
  899. EXPORT_SYMBOL_GPL(sas_ioctl);