sas_discover.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Serial Attached SCSI (SAS) Discover process
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  7. */
  8. #include <linux/scatterlist.h>
  9. #include <linux/slab.h>
  10. #include <scsi/scsi_host.h>
  11. #include <scsi/scsi_eh.h>
  12. #include "sas_internal.h"
  13. #include <scsi/scsi_transport.h>
  14. #include <scsi/scsi_transport_sas.h>
  15. #include <scsi/sas_ata.h>
  16. #include "scsi_sas_internal.h"
  17. /* ---------- Basic task processing for discovery purposes ---------- */
  18. void sas_init_dev(struct domain_device *dev)
  19. {
  20. switch (dev->dev_type) {
  21. case SAS_END_DEVICE:
  22. INIT_LIST_HEAD(&dev->ssp_dev.eh_list_node);
  23. break;
  24. case SAS_EDGE_EXPANDER_DEVICE:
  25. case SAS_FANOUT_EXPANDER_DEVICE:
  26. INIT_LIST_HEAD(&dev->ex_dev.children);
  27. mutex_init(&dev->ex_dev.cmd_mutex);
  28. break;
  29. default:
  30. break;
  31. }
  32. }
  33. /* ---------- Domain device discovery ---------- */
  34. /**
  35. * sas_get_port_device - Discover devices which caused port creation
  36. * @port: pointer to struct sas_port of interest
  37. *
  38. * Devices directly attached to a HA port, have no parent. This is
  39. * how we know they are (domain) "root" devices. All other devices
  40. * do, and should have their "parent" pointer set appropriately as
  41. * soon as a child device is discovered.
  42. */
  43. static int sas_get_port_device(struct asd_sas_port *port)
  44. {
  45. struct asd_sas_phy *phy;
  46. struct sas_rphy *rphy;
  47. struct domain_device *dev;
  48. int rc = -ENODEV;
  49. dev = sas_alloc_device();
  50. if (!dev)
  51. return -ENOMEM;
  52. spin_lock_irq(&port->phy_list_lock);
  53. if (list_empty(&port->phy_list)) {
  54. spin_unlock_irq(&port->phy_list_lock);
  55. sas_put_device(dev);
  56. return -ENODEV;
  57. }
  58. phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
  59. spin_lock(&phy->frame_rcvd_lock);
  60. memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
  61. (size_t)phy->frame_rcvd_size));
  62. spin_unlock(&phy->frame_rcvd_lock);
  63. spin_unlock_irq(&port->phy_list_lock);
  64. if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
  65. struct dev_to_host_fis *fis =
  66. (struct dev_to_host_fis *) dev->frame_rcvd;
  67. if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
  68. fis->byte_count_low == 0x69 && fis->byte_count_high == 0x96
  69. && (fis->device & ~0x10) == 0)
  70. dev->dev_type = SAS_SATA_PM;
  71. else
  72. dev->dev_type = SAS_SATA_DEV;
  73. dev->tproto = SAS_PROTOCOL_SATA;
  74. } else if (port->oob_mode == SAS_OOB_MODE) {
  75. struct sas_identify_frame *id =
  76. (struct sas_identify_frame *) dev->frame_rcvd;
  77. dev->dev_type = id->dev_type;
  78. dev->iproto = id->initiator_bits;
  79. dev->tproto = id->target_bits;
  80. } else {
  81. /* If the oob mode is OOB_NOT_CONNECTED, the port is
  82. * disconnected due to race with PHY down. We cannot
  83. * continue to discover this port
  84. */
  85. sas_put_device(dev);
  86. pr_warn("Port %016llx is disconnected when discovering\n",
  87. SAS_ADDR(port->attached_sas_addr));
  88. return -ENODEV;
  89. }
  90. sas_init_dev(dev);
  91. dev->port = port;
  92. switch (dev->dev_type) {
  93. case SAS_SATA_DEV:
  94. rc = sas_ata_init(dev);
  95. if (rc) {
  96. rphy = NULL;
  97. break;
  98. }
  99. fallthrough;
  100. case SAS_END_DEVICE:
  101. rphy = sas_end_device_alloc(port->port);
  102. break;
  103. case SAS_EDGE_EXPANDER_DEVICE:
  104. rphy = sas_expander_alloc(port->port,
  105. SAS_EDGE_EXPANDER_DEVICE);
  106. break;
  107. case SAS_FANOUT_EXPANDER_DEVICE:
  108. rphy = sas_expander_alloc(port->port,
  109. SAS_FANOUT_EXPANDER_DEVICE);
  110. break;
  111. default:
  112. pr_warn("ERROR: Unidentified device type %d\n", dev->dev_type);
  113. rphy = NULL;
  114. break;
  115. }
  116. if (!rphy) {
  117. sas_put_device(dev);
  118. return rc;
  119. }
  120. rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
  121. memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
  122. sas_fill_in_rphy(dev, rphy);
  123. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  124. port->port_dev = dev;
  125. dev->linkrate = port->linkrate;
  126. dev->min_linkrate = port->linkrate;
  127. dev->max_linkrate = port->linkrate;
  128. dev->pathways = port->num_phys;
  129. memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
  130. memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
  131. memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
  132. port->disc.max_level = 0;
  133. sas_device_set_phy(dev, port->port);
  134. dev->rphy = rphy;
  135. get_device(&dev->rphy->dev);
  136. if (dev_is_sata(dev) || dev->dev_type == SAS_END_DEVICE)
  137. list_add_tail(&dev->disco_list_node, &port->disco_list);
  138. else {
  139. spin_lock_irq(&port->dev_list_lock);
  140. list_add_tail(&dev->dev_list_node, &port->dev_list);
  141. spin_unlock_irq(&port->dev_list_lock);
  142. }
  143. spin_lock_irq(&port->phy_list_lock);
  144. list_for_each_entry(phy, &port->phy_list, port_phy_el)
  145. sas_phy_set_target(phy, dev);
  146. spin_unlock_irq(&port->phy_list_lock);
  147. return 0;
  148. }
  149. /* ---------- Discover and Revalidate ---------- */
  150. int sas_notify_lldd_dev_found(struct domain_device *dev)
  151. {
  152. int res = 0;
  153. struct sas_ha_struct *sas_ha = dev->port->ha;
  154. struct Scsi_Host *shost = sas_ha->shost;
  155. struct sas_internal *i = to_sas_internal(shost->transportt);
  156. if (!i->dft->lldd_dev_found)
  157. return 0;
  158. res = i->dft->lldd_dev_found(dev);
  159. if (res) {
  160. pr_warn("driver on host %s cannot handle device %016llx, error:%d\n",
  161. dev_name(sas_ha->dev),
  162. SAS_ADDR(dev->sas_addr), res);
  163. return res;
  164. }
  165. set_bit(SAS_DEV_FOUND, &dev->state);
  166. kref_get(&dev->kref);
  167. return 0;
  168. }
  169. void sas_notify_lldd_dev_gone(struct domain_device *dev)
  170. {
  171. struct sas_ha_struct *sas_ha = dev->port->ha;
  172. struct Scsi_Host *shost = sas_ha->shost;
  173. struct sas_internal *i = to_sas_internal(shost->transportt);
  174. if (!i->dft->lldd_dev_gone)
  175. return;
  176. if (test_and_clear_bit(SAS_DEV_FOUND, &dev->state)) {
  177. i->dft->lldd_dev_gone(dev);
  178. sas_put_device(dev);
  179. }
  180. }
  181. static void sas_probe_devices(struct asd_sas_port *port)
  182. {
  183. struct domain_device *dev, *n;
  184. /* devices must be domain members before link recovery and probe */
  185. list_for_each_entry(dev, &port->disco_list, disco_list_node) {
  186. spin_lock_irq(&port->dev_list_lock);
  187. list_add_tail(&dev->dev_list_node, &port->dev_list);
  188. spin_unlock_irq(&port->dev_list_lock);
  189. }
  190. sas_probe_sata(port);
  191. list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
  192. int err;
  193. err = sas_rphy_add(dev->rphy);
  194. if (err)
  195. sas_fail_probe(dev, __func__, err);
  196. else
  197. list_del_init(&dev->disco_list_node);
  198. }
  199. }
  200. static void sas_suspend_devices(struct work_struct *work)
  201. {
  202. struct asd_sas_phy *phy;
  203. struct domain_device *dev;
  204. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  205. struct asd_sas_port *port = ev->port;
  206. struct Scsi_Host *shost = port->ha->shost;
  207. struct sas_internal *si = to_sas_internal(shost->transportt);
  208. clear_bit(DISCE_SUSPEND, &port->disc.pending);
  209. sas_suspend_sata(port);
  210. /* lldd is free to forget the domain_device across the
  211. * suspension, we force the issue here to keep the reference
  212. * counts aligned
  213. */
  214. list_for_each_entry(dev, &port->dev_list, dev_list_node)
  215. sas_notify_lldd_dev_gone(dev);
  216. /* we are suspending, so we know events are disabled and
  217. * phy_list is not being mutated
  218. */
  219. list_for_each_entry(phy, &port->phy_list, port_phy_el) {
  220. if (si->dft->lldd_port_deformed)
  221. si->dft->lldd_port_deformed(phy);
  222. phy->suspended = 1;
  223. port->suspended = 1;
  224. }
  225. }
  226. static void sas_resume_devices(struct work_struct *work)
  227. {
  228. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  229. struct asd_sas_port *port = ev->port;
  230. clear_bit(DISCE_RESUME, &port->disc.pending);
  231. sas_resume_sata(port);
  232. }
  233. /**
  234. * sas_discover_end_dev - discover an end device (SSP, etc)
  235. * @dev: pointer to domain device of interest
  236. *
  237. * See comment in sas_discover_sata().
  238. */
  239. static int sas_discover_end_dev(struct domain_device *dev)
  240. {
  241. return sas_notify_lldd_dev_found(dev);
  242. }
  243. /* ---------- Device registration and unregistration ---------- */
  244. void sas_free_device(struct kref *kref)
  245. {
  246. struct domain_device *dev = container_of(kref, typeof(*dev), kref);
  247. put_device(&dev->rphy->dev);
  248. dev->rphy = NULL;
  249. if (dev->parent)
  250. sas_put_device(dev->parent);
  251. sas_port_put_phy(dev->phy);
  252. dev->phy = NULL;
  253. /* remove the phys and ports, everything else should be gone */
  254. if (dev_is_expander(dev->dev_type))
  255. kfree(dev->ex_dev.ex_phy);
  256. if (dev_is_sata(dev) && dev->sata_dev.ap) {
  257. ata_tport_delete(dev->sata_dev.ap);
  258. ata_port_free(dev->sata_dev.ap);
  259. ata_host_put(dev->sata_dev.ata_host);
  260. dev->sata_dev.ata_host = NULL;
  261. dev->sata_dev.ap = NULL;
  262. }
  263. kfree(dev);
  264. }
  265. static void sas_unregister_common_dev(struct asd_sas_port *port, struct domain_device *dev)
  266. {
  267. struct sas_ha_struct *ha = port->ha;
  268. sas_notify_lldd_dev_gone(dev);
  269. if (!dev->parent)
  270. dev->port->port_dev = NULL;
  271. else
  272. list_del_init(&dev->siblings);
  273. spin_lock_irq(&port->dev_list_lock);
  274. list_del_init(&dev->dev_list_node);
  275. if (dev_is_sata(dev))
  276. sas_ata_end_eh(dev->sata_dev.ap);
  277. spin_unlock_irq(&port->dev_list_lock);
  278. spin_lock_irq(&ha->lock);
  279. if (dev->dev_type == SAS_END_DEVICE &&
  280. !list_empty(&dev->ssp_dev.eh_list_node)) {
  281. list_del_init(&dev->ssp_dev.eh_list_node);
  282. ha->eh_active--;
  283. }
  284. spin_unlock_irq(&ha->lock);
  285. sas_put_device(dev);
  286. }
  287. void sas_destruct_devices(struct asd_sas_port *port)
  288. {
  289. struct domain_device *dev, *n;
  290. list_for_each_entry_safe(dev, n, &port->destroy_list, disco_list_node) {
  291. list_del_init(&dev->disco_list_node);
  292. sas_remove_children(&dev->rphy->dev);
  293. sas_rphy_delete(dev->rphy);
  294. sas_unregister_common_dev(port, dev);
  295. }
  296. }
  297. static void sas_destruct_ports(struct asd_sas_port *port)
  298. {
  299. struct sas_port *sas_port, *p;
  300. list_for_each_entry_safe(sas_port, p, &port->sas_port_del_list, del_list) {
  301. list_del_init(&sas_port->del_list);
  302. sas_port_delete(sas_port);
  303. }
  304. }
  305. static bool sas_abort_cmd(struct request *req, void *data)
  306. {
  307. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  308. struct domain_device *dev = data;
  309. if (dev == cmd_to_domain_dev(cmd))
  310. blk_abort_request(req);
  311. return true;
  312. }
  313. static void sas_abort_device_scsi_cmds(struct domain_device *dev)
  314. {
  315. struct sas_ha_struct *sas_ha = dev->port->ha;
  316. struct Scsi_Host *shost = sas_ha->shost;
  317. if (dev_is_expander(dev->dev_type))
  318. return;
  319. /*
  320. * For removed device with active IOs, the user space applications have
  321. * to spend very long time waiting for the timeout. This is not
  322. * necessary because a removed device will not return the IOs.
  323. * Abort the inflight IOs here so that EH can be quickly kicked in.
  324. */
  325. blk_mq_tagset_busy_iter(&shost->tag_set, sas_abort_cmd, dev);
  326. }
  327. void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
  328. {
  329. if (!test_bit(SAS_DEV_DESTROY, &dev->state) &&
  330. !list_empty(&dev->disco_list_node)) {
  331. /* this rphy never saw sas_rphy_add */
  332. list_del_init(&dev->disco_list_node);
  333. sas_rphy_free(dev->rphy);
  334. sas_unregister_common_dev(port, dev);
  335. return;
  336. }
  337. if (!test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
  338. if (test_bit(SAS_DEV_GONE, &dev->state))
  339. sas_abort_device_scsi_cmds(dev);
  340. sas_rphy_unlink(dev->rphy);
  341. list_move_tail(&dev->disco_list_node, &port->destroy_list);
  342. }
  343. }
  344. void sas_unregister_domain_devices(struct asd_sas_port *port, int gone)
  345. {
  346. struct domain_device *dev, *n;
  347. list_for_each_entry_safe_reverse(dev, n, &port->dev_list, dev_list_node) {
  348. if (gone)
  349. set_bit(SAS_DEV_GONE, &dev->state);
  350. sas_unregister_dev(port, dev);
  351. }
  352. list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
  353. sas_unregister_dev(port, dev);
  354. port->port->rphy = NULL;
  355. }
  356. void sas_device_set_phy(struct domain_device *dev, struct sas_port *port)
  357. {
  358. struct sas_ha_struct *ha;
  359. struct sas_phy *new_phy;
  360. if (!dev)
  361. return;
  362. ha = dev->port->ha;
  363. new_phy = sas_port_get_phy(port);
  364. /* pin and record last seen phy */
  365. spin_lock_irq(&ha->phy_port_lock);
  366. if (new_phy) {
  367. sas_port_put_phy(dev->phy);
  368. dev->phy = new_phy;
  369. }
  370. spin_unlock_irq(&ha->phy_port_lock);
  371. }
  372. /* ---------- Discovery and Revalidation ---------- */
  373. /**
  374. * sas_discover_domain - discover the domain
  375. * @work: work structure embedded in port domain device.
  376. *
  377. * NOTE: this process _must_ quit (return) as soon as any connection
  378. * errors are encountered. Connection recovery is done elsewhere.
  379. * Discover process only interrogates devices in order to discover the
  380. * domain.
  381. */
  382. static void sas_discover_domain(struct work_struct *work)
  383. {
  384. struct domain_device *dev;
  385. int error = 0;
  386. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  387. struct asd_sas_port *port = ev->port;
  388. clear_bit(DISCE_DISCOVER_DOMAIN, &port->disc.pending);
  389. if (port->port_dev)
  390. return;
  391. error = sas_get_port_device(port);
  392. if (error)
  393. return;
  394. dev = port->port_dev;
  395. pr_debug("DOING DISCOVERY on port %d, pid:%d\n", port->id,
  396. task_pid_nr(current));
  397. switch (dev->dev_type) {
  398. case SAS_END_DEVICE:
  399. error = sas_discover_end_dev(dev);
  400. break;
  401. case SAS_EDGE_EXPANDER_DEVICE:
  402. case SAS_FANOUT_EXPANDER_DEVICE:
  403. error = sas_discover_root_expander(dev);
  404. break;
  405. case SAS_SATA_DEV:
  406. case SAS_SATA_PM:
  407. error = sas_discover_sata(dev);
  408. break;
  409. default:
  410. error = -ENXIO;
  411. pr_err("unhandled device %d\n", dev->dev_type);
  412. break;
  413. }
  414. if (error) {
  415. sas_rphy_free(dev->rphy);
  416. list_del_init(&dev->disco_list_node);
  417. spin_lock_irq(&port->dev_list_lock);
  418. list_del_init(&dev->dev_list_node);
  419. spin_unlock_irq(&port->dev_list_lock);
  420. sas_put_device(dev);
  421. port->port_dev = NULL;
  422. }
  423. sas_probe_devices(port);
  424. pr_debug("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
  425. task_pid_nr(current), error);
  426. }
  427. static void sas_revalidate_domain(struct work_struct *work)
  428. {
  429. int res = 0;
  430. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  431. struct asd_sas_port *port = ev->port;
  432. struct sas_ha_struct *ha = port->ha;
  433. struct domain_device *ddev = port->port_dev;
  434. /* prevent revalidation from finding sata links in recovery */
  435. mutex_lock(&ha->disco_mutex);
  436. if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
  437. pr_debug("REVALIDATION DEFERRED on port %d, pid:%d\n",
  438. port->id, task_pid_nr(current));
  439. goto out;
  440. }
  441. clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
  442. pr_debug("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
  443. task_pid_nr(current));
  444. if (ddev && dev_is_expander(ddev->dev_type))
  445. res = sas_ex_revalidate_domain(ddev);
  446. pr_debug("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
  447. port->id, task_pid_nr(current), res);
  448. out:
  449. mutex_unlock(&ha->disco_mutex);
  450. sas_destruct_devices(port);
  451. sas_destruct_ports(port);
  452. sas_probe_devices(port);
  453. }
  454. /* ---------- Events ---------- */
  455. static void sas_chain_work(struct sas_ha_struct *ha, struct sas_work *sw)
  456. {
  457. /* chained work is not subject to SA_HA_DRAINING or
  458. * SAS_HA_REGISTERED, because it is either submitted in the
  459. * workqueue, or known to be submitted from a context that is
  460. * not racing against draining
  461. */
  462. queue_work(ha->disco_q, &sw->work);
  463. }
  464. static void sas_chain_event(int event, unsigned long *pending,
  465. struct sas_work *sw,
  466. struct sas_ha_struct *ha)
  467. {
  468. if (!test_and_set_bit(event, pending)) {
  469. unsigned long flags;
  470. spin_lock_irqsave(&ha->lock, flags);
  471. sas_chain_work(ha, sw);
  472. spin_unlock_irqrestore(&ha->lock, flags);
  473. }
  474. }
  475. void sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
  476. {
  477. struct sas_discovery *disc;
  478. if (!port)
  479. return;
  480. disc = &port->disc;
  481. BUG_ON(ev >= DISC_NUM_EVENTS);
  482. sas_chain_event(ev, &disc->pending, &disc->disc_work[ev].work, port->ha);
  483. }
  484. /**
  485. * sas_init_disc - initialize the discovery struct in the port
  486. * @disc: port discovery structure
  487. * @port: pointer to struct port
  488. *
  489. * Called when the ports are being initialized.
  490. */
  491. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
  492. {
  493. int i;
  494. static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
  495. [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
  496. [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
  497. [DISCE_SUSPEND] = sas_suspend_devices,
  498. [DISCE_RESUME] = sas_resume_devices,
  499. };
  500. disc->pending = 0;
  501. for (i = 0; i < DISC_NUM_EVENTS; i++) {
  502. INIT_SAS_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
  503. disc->disc_work[i].port = port;
  504. }
  505. }