sas_discover.c 16 KB

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