sas_discover.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 <[email protected]>
  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->core.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->core.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->core.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. 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_sas_tport_delete(dev->sata_dev.ap);
  258. ata_sas_port_destroy(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. void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
  306. {
  307. if (!test_bit(SAS_DEV_DESTROY, &dev->state) &&
  308. !list_empty(&dev->disco_list_node)) {
  309. /* this rphy never saw sas_rphy_add */
  310. list_del_init(&dev->disco_list_node);
  311. sas_rphy_free(dev->rphy);
  312. sas_unregister_common_dev(port, dev);
  313. return;
  314. }
  315. if (!test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
  316. sas_rphy_unlink(dev->rphy);
  317. list_move_tail(&dev->disco_list_node, &port->destroy_list);
  318. }
  319. }
  320. void sas_unregister_domain_devices(struct asd_sas_port *port, int gone)
  321. {
  322. struct domain_device *dev, *n;
  323. list_for_each_entry_safe_reverse(dev, n, &port->dev_list, dev_list_node) {
  324. if (gone)
  325. set_bit(SAS_DEV_GONE, &dev->state);
  326. sas_unregister_dev(port, dev);
  327. }
  328. list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
  329. sas_unregister_dev(port, dev);
  330. port->port->rphy = NULL;
  331. }
  332. void sas_device_set_phy(struct domain_device *dev, struct sas_port *port)
  333. {
  334. struct sas_ha_struct *ha;
  335. struct sas_phy *new_phy;
  336. if (!dev)
  337. return;
  338. ha = dev->port->ha;
  339. new_phy = sas_port_get_phy(port);
  340. /* pin and record last seen phy */
  341. spin_lock_irq(&ha->phy_port_lock);
  342. if (new_phy) {
  343. sas_port_put_phy(dev->phy);
  344. dev->phy = new_phy;
  345. }
  346. spin_unlock_irq(&ha->phy_port_lock);
  347. }
  348. /* ---------- Discovery and Revalidation ---------- */
  349. /**
  350. * sas_discover_domain - discover the domain
  351. * @work: work structure embedded in port domain device.
  352. *
  353. * NOTE: this process _must_ quit (return) as soon as any connection
  354. * errors are encountered. Connection recovery is done elsewhere.
  355. * Discover process only interrogates devices in order to discover the
  356. * domain.
  357. */
  358. static void sas_discover_domain(struct work_struct *work)
  359. {
  360. struct domain_device *dev;
  361. int error = 0;
  362. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  363. struct asd_sas_port *port = ev->port;
  364. clear_bit(DISCE_DISCOVER_DOMAIN, &port->disc.pending);
  365. if (port->port_dev)
  366. return;
  367. error = sas_get_port_device(port);
  368. if (error)
  369. return;
  370. dev = port->port_dev;
  371. pr_debug("DOING DISCOVERY on port %d, pid:%d\n", port->id,
  372. task_pid_nr(current));
  373. switch (dev->dev_type) {
  374. case SAS_END_DEVICE:
  375. error = sas_discover_end_dev(dev);
  376. break;
  377. case SAS_EDGE_EXPANDER_DEVICE:
  378. case SAS_FANOUT_EXPANDER_DEVICE:
  379. error = sas_discover_root_expander(dev);
  380. break;
  381. case SAS_SATA_DEV:
  382. case SAS_SATA_PM:
  383. #ifdef CONFIG_SCSI_SAS_ATA
  384. error = sas_discover_sata(dev);
  385. break;
  386. #else
  387. pr_notice("ATA device seen but CONFIG_SCSI_SAS_ATA=N so cannot attach\n");
  388. fallthrough;
  389. #endif
  390. /* Fall through - only for the #else condition above. */
  391. default:
  392. error = -ENXIO;
  393. pr_err("unhandled device %d\n", dev->dev_type);
  394. break;
  395. }
  396. if (error) {
  397. sas_rphy_free(dev->rphy);
  398. list_del_init(&dev->disco_list_node);
  399. spin_lock_irq(&port->dev_list_lock);
  400. list_del_init(&dev->dev_list_node);
  401. spin_unlock_irq(&port->dev_list_lock);
  402. sas_put_device(dev);
  403. port->port_dev = NULL;
  404. }
  405. sas_probe_devices(port);
  406. pr_debug("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
  407. task_pid_nr(current), error);
  408. }
  409. static void sas_revalidate_domain(struct work_struct *work)
  410. {
  411. int res = 0;
  412. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  413. struct asd_sas_port *port = ev->port;
  414. struct sas_ha_struct *ha = port->ha;
  415. struct domain_device *ddev = port->port_dev;
  416. /* prevent revalidation from finding sata links in recovery */
  417. mutex_lock(&ha->disco_mutex);
  418. if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
  419. pr_debug("REVALIDATION DEFERRED on port %d, pid:%d\n",
  420. port->id, task_pid_nr(current));
  421. goto out;
  422. }
  423. clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
  424. pr_debug("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
  425. task_pid_nr(current));
  426. if (ddev && dev_is_expander(ddev->dev_type))
  427. res = sas_ex_revalidate_domain(ddev);
  428. pr_debug("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
  429. port->id, task_pid_nr(current), res);
  430. out:
  431. mutex_unlock(&ha->disco_mutex);
  432. sas_destruct_devices(port);
  433. sas_destruct_ports(port);
  434. sas_probe_devices(port);
  435. }
  436. /* ---------- Events ---------- */
  437. static void sas_chain_work(struct sas_ha_struct *ha, struct sas_work *sw)
  438. {
  439. /* chained work is not subject to SA_HA_DRAINING or
  440. * SAS_HA_REGISTERED, because it is either submitted in the
  441. * workqueue, or known to be submitted from a context that is
  442. * not racing against draining
  443. */
  444. queue_work(ha->disco_q, &sw->work);
  445. }
  446. static void sas_chain_event(int event, unsigned long *pending,
  447. struct sas_work *sw,
  448. struct sas_ha_struct *ha)
  449. {
  450. if (!test_and_set_bit(event, pending)) {
  451. unsigned long flags;
  452. spin_lock_irqsave(&ha->lock, flags);
  453. sas_chain_work(ha, sw);
  454. spin_unlock_irqrestore(&ha->lock, flags);
  455. }
  456. }
  457. void sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
  458. {
  459. struct sas_discovery *disc;
  460. if (!port)
  461. return;
  462. disc = &port->disc;
  463. BUG_ON(ev >= DISC_NUM_EVENTS);
  464. sas_chain_event(ev, &disc->pending, &disc->disc_work[ev].work, port->ha);
  465. }
  466. /**
  467. * sas_init_disc - initialize the discovery struct in the port
  468. * @disc: port discovery structure
  469. * @port: pointer to struct port
  470. *
  471. * Called when the ports are being initialized.
  472. */
  473. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
  474. {
  475. int i;
  476. static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
  477. [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
  478. [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
  479. [DISCE_SUSPEND] = sas_suspend_devices,
  480. [DISCE_RESUME] = sas_resume_devices,
  481. };
  482. disc->pending = 0;
  483. for (i = 0; i < DISC_NUM_EVENTS; i++) {
  484. INIT_SAS_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
  485. disc->disc_work[i].port = port;
  486. }
  487. }