sas_discover.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 {
  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. }
  98. sas_init_dev(dev);
  99. dev->port = port;
  100. switch (dev->dev_type) {
  101. case SAS_SATA_DEV:
  102. rc = sas_ata_init(dev);
  103. if (rc) {
  104. rphy = NULL;
  105. break;
  106. }
  107. /* fall through */
  108. case SAS_END_DEVICE:
  109. rphy = sas_end_device_alloc(port->port);
  110. break;
  111. case SAS_EDGE_EXPANDER_DEVICE:
  112. rphy = sas_expander_alloc(port->port,
  113. SAS_EDGE_EXPANDER_DEVICE);
  114. break;
  115. case SAS_FANOUT_EXPANDER_DEVICE:
  116. rphy = sas_expander_alloc(port->port,
  117. SAS_FANOUT_EXPANDER_DEVICE);
  118. break;
  119. default:
  120. printk("ERROR: Unidentified device type %d\n", dev->dev_type);
  121. rphy = NULL;
  122. break;
  123. }
  124. if (!rphy) {
  125. sas_put_device(dev);
  126. return rc;
  127. }
  128. rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
  129. memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
  130. sas_fill_in_rphy(dev, rphy);
  131. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  132. port->port_dev = dev;
  133. dev->linkrate = port->linkrate;
  134. dev->min_linkrate = port->linkrate;
  135. dev->max_linkrate = port->linkrate;
  136. dev->pathways = port->num_phys;
  137. memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
  138. memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
  139. memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
  140. port->disc.max_level = 0;
  141. sas_device_set_phy(dev, port->port);
  142. dev->rphy = rphy;
  143. get_device(&dev->rphy->dev);
  144. if (dev_is_sata(dev) || dev->dev_type == SAS_END_DEVICE)
  145. list_add_tail(&dev->disco_list_node, &port->disco_list);
  146. else {
  147. spin_lock_irq(&port->dev_list_lock);
  148. list_add_tail(&dev->dev_list_node, &port->dev_list);
  149. spin_unlock_irq(&port->dev_list_lock);
  150. }
  151. spin_lock_irq(&port->phy_list_lock);
  152. list_for_each_entry(phy, &port->phy_list, port_phy_el)
  153. sas_phy_set_target(phy, dev);
  154. spin_unlock_irq(&port->phy_list_lock);
  155. return 0;
  156. }
  157. /* ---------- Discover and Revalidate ---------- */
  158. int sas_notify_lldd_dev_found(struct domain_device *dev)
  159. {
  160. int res = 0;
  161. struct sas_ha_struct *sas_ha = dev->port->ha;
  162. struct Scsi_Host *shost = sas_ha->core.shost;
  163. struct sas_internal *i = to_sas_internal(shost->transportt);
  164. if (!i->dft->lldd_dev_found)
  165. return 0;
  166. res = i->dft->lldd_dev_found(dev);
  167. if (res) {
  168. printk("sas: driver on pcidev %s cannot handle "
  169. "device %llx, error:%d\n",
  170. dev_name(sas_ha->dev),
  171. SAS_ADDR(dev->sas_addr), res);
  172. }
  173. set_bit(SAS_DEV_FOUND, &dev->state);
  174. kref_get(&dev->kref);
  175. return res;
  176. }
  177. void sas_notify_lldd_dev_gone(struct domain_device *dev)
  178. {
  179. struct sas_ha_struct *sas_ha = dev->port->ha;
  180. struct Scsi_Host *shost = sas_ha->core.shost;
  181. struct sas_internal *i = to_sas_internal(shost->transportt);
  182. if (!i->dft->lldd_dev_gone)
  183. return;
  184. if (test_and_clear_bit(SAS_DEV_FOUND, &dev->state)) {
  185. i->dft->lldd_dev_gone(dev);
  186. sas_put_device(dev);
  187. }
  188. }
  189. static void sas_probe_devices(struct work_struct *work)
  190. {
  191. struct domain_device *dev, *n;
  192. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  193. struct asd_sas_port *port = ev->port;
  194. clear_bit(DISCE_PROBE, &port->disc.pending);
  195. /* devices must be domain members before link recovery and probe */
  196. list_for_each_entry(dev, &port->disco_list, disco_list_node) {
  197. spin_lock_irq(&port->dev_list_lock);
  198. list_add_tail(&dev->dev_list_node, &port->dev_list);
  199. spin_unlock_irq(&port->dev_list_lock);
  200. }
  201. sas_probe_sata(port);
  202. list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
  203. int err;
  204. err = sas_rphy_add(dev->rphy);
  205. if (err)
  206. sas_fail_probe(dev, __func__, err);
  207. else
  208. list_del_init(&dev->disco_list_node);
  209. }
  210. }
  211. static void sas_suspend_devices(struct work_struct *work)
  212. {
  213. struct asd_sas_phy *phy;
  214. struct domain_device *dev;
  215. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  216. struct asd_sas_port *port = ev->port;
  217. struct Scsi_Host *shost = port->ha->core.shost;
  218. struct sas_internal *si = to_sas_internal(shost->transportt);
  219. clear_bit(DISCE_SUSPEND, &port->disc.pending);
  220. sas_suspend_sata(port);
  221. /* lldd is free to forget the domain_device across the
  222. * suspension, we force the issue here to keep the reference
  223. * counts aligned
  224. */
  225. list_for_each_entry(dev, &port->dev_list, dev_list_node)
  226. sas_notify_lldd_dev_gone(dev);
  227. /* we are suspending, so we know events are disabled and
  228. * phy_list is not being mutated
  229. */
  230. list_for_each_entry(phy, &port->phy_list, port_phy_el) {
  231. if (si->dft->lldd_port_formed)
  232. si->dft->lldd_port_deformed(phy);
  233. phy->suspended = 1;
  234. port->suspended = 1;
  235. }
  236. }
  237. static void sas_resume_devices(struct work_struct *work)
  238. {
  239. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  240. struct asd_sas_port *port = ev->port;
  241. clear_bit(DISCE_RESUME, &port->disc.pending);
  242. sas_resume_sata(port);
  243. }
  244. /**
  245. * sas_discover_end_dev -- discover an end device (SSP, etc)
  246. * @end: pointer to domain device of interest
  247. *
  248. * See comment in sas_discover_sata().
  249. */
  250. int sas_discover_end_dev(struct domain_device *dev)
  251. {
  252. int res;
  253. res = sas_notify_lldd_dev_found(dev);
  254. if (res)
  255. return res;
  256. sas_discover_event(dev->port, DISCE_PROBE);
  257. return 0;
  258. }
  259. /* ---------- Device registration and unregistration ---------- */
  260. void sas_free_device(struct kref *kref)
  261. {
  262. struct domain_device *dev = container_of(kref, typeof(*dev), kref);
  263. put_device(&dev->rphy->dev);
  264. dev->rphy = NULL;
  265. if (dev->parent)
  266. sas_put_device(dev->parent);
  267. sas_port_put_phy(dev->phy);
  268. dev->phy = NULL;
  269. /* remove the phys and ports, everything else should be gone */
  270. if (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE || dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE)
  271. kfree(dev->ex_dev.ex_phy);
  272. if (dev_is_sata(dev) && dev->sata_dev.ap) {
  273. ata_sas_port_destroy(dev->sata_dev.ap);
  274. dev->sata_dev.ap = NULL;
  275. }
  276. kfree(dev);
  277. }
  278. static void sas_unregister_common_dev(struct asd_sas_port *port, struct domain_device *dev)
  279. {
  280. struct sas_ha_struct *ha = port->ha;
  281. sas_notify_lldd_dev_gone(dev);
  282. if (!dev->parent)
  283. dev->port->port_dev = NULL;
  284. else
  285. list_del_init(&dev->siblings);
  286. spin_lock_irq(&port->dev_list_lock);
  287. list_del_init(&dev->dev_list_node);
  288. if (dev_is_sata(dev))
  289. sas_ata_end_eh(dev->sata_dev.ap);
  290. spin_unlock_irq(&port->dev_list_lock);
  291. spin_lock_irq(&ha->lock);
  292. if (dev->dev_type == SAS_END_DEVICE &&
  293. !list_empty(&dev->ssp_dev.eh_list_node)) {
  294. list_del_init(&dev->ssp_dev.eh_list_node);
  295. ha->eh_active--;
  296. }
  297. spin_unlock_irq(&ha->lock);
  298. sas_put_device(dev);
  299. }
  300. static void sas_destruct_devices(struct work_struct *work)
  301. {
  302. struct domain_device *dev, *n;
  303. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  304. struct asd_sas_port *port = ev->port;
  305. clear_bit(DISCE_DESTRUCT, &port->disc.pending);
  306. list_for_each_entry_safe(dev, n, &port->destroy_list, disco_list_node) {
  307. list_del_init(&dev->disco_list_node);
  308. sas_remove_children(&dev->rphy->dev);
  309. sas_rphy_delete(dev->rphy);
  310. sas_unregister_common_dev(port, dev);
  311. }
  312. }
  313. void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
  314. {
  315. if (!test_bit(SAS_DEV_DESTROY, &dev->state) &&
  316. !list_empty(&dev->disco_list_node)) {
  317. /* this rphy never saw sas_rphy_add */
  318. list_del_init(&dev->disco_list_node);
  319. sas_rphy_free(dev->rphy);
  320. sas_unregister_common_dev(port, dev);
  321. return;
  322. }
  323. if (!test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
  324. sas_rphy_unlink(dev->rphy);
  325. list_move_tail(&dev->disco_list_node, &port->destroy_list);
  326. sas_discover_event(dev->port, DISCE_DESTRUCT);
  327. }
  328. }
  329. void sas_unregister_domain_devices(struct asd_sas_port *port, int gone)
  330. {
  331. struct domain_device *dev, *n;
  332. list_for_each_entry_safe_reverse(dev, n, &port->dev_list, dev_list_node) {
  333. if (gone)
  334. set_bit(SAS_DEV_GONE, &dev->state);
  335. sas_unregister_dev(port, dev);
  336. }
  337. list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
  338. sas_unregister_dev(port, dev);
  339. port->port->rphy = NULL;
  340. }
  341. void sas_device_set_phy(struct domain_device *dev, struct sas_port *port)
  342. {
  343. struct sas_ha_struct *ha;
  344. struct sas_phy *new_phy;
  345. if (!dev)
  346. return;
  347. ha = dev->port->ha;
  348. new_phy = sas_port_get_phy(port);
  349. /* pin and record last seen phy */
  350. spin_lock_irq(&ha->phy_port_lock);
  351. if (new_phy) {
  352. sas_port_put_phy(dev->phy);
  353. dev->phy = new_phy;
  354. }
  355. spin_unlock_irq(&ha->phy_port_lock);
  356. }
  357. /* ---------- Discovery and Revalidation ---------- */
  358. /**
  359. * sas_discover_domain -- discover the domain
  360. * @port: port to the domain of interest
  361. *
  362. * NOTE: this process _must_ quit (return) as soon as any connection
  363. * errors are encountered. Connection recovery is done elsewhere.
  364. * Discover process only interrogates devices in order to discover the
  365. * domain.
  366. */
  367. static void sas_discover_domain(struct work_struct *work)
  368. {
  369. struct domain_device *dev;
  370. int error = 0;
  371. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  372. struct asd_sas_port *port = ev->port;
  373. clear_bit(DISCE_DISCOVER_DOMAIN, &port->disc.pending);
  374. if (port->port_dev)
  375. return;
  376. error = sas_get_port_device(port);
  377. if (error)
  378. return;
  379. dev = port->port_dev;
  380. SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
  381. task_pid_nr(current));
  382. switch (dev->dev_type) {
  383. case SAS_END_DEVICE:
  384. error = sas_discover_end_dev(dev);
  385. break;
  386. case SAS_EDGE_EXPANDER_DEVICE:
  387. case SAS_FANOUT_EXPANDER_DEVICE:
  388. error = sas_discover_root_expander(dev);
  389. break;
  390. case SAS_SATA_DEV:
  391. case SAS_SATA_PM:
  392. #ifdef CONFIG_SCSI_SAS_ATA
  393. error = sas_discover_sata(dev);
  394. break;
  395. #else
  396. SAS_DPRINTK("ATA device seen but CONFIG_SCSI_SAS_ATA=N so cannot attach\n");
  397. /* Fall through */
  398. #endif
  399. default:
  400. error = -ENXIO;
  401. SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
  402. break;
  403. }
  404. if (error) {
  405. sas_rphy_free(dev->rphy);
  406. list_del_init(&dev->disco_list_node);
  407. spin_lock_irq(&port->dev_list_lock);
  408. list_del_init(&dev->dev_list_node);
  409. spin_unlock_irq(&port->dev_list_lock);
  410. sas_put_device(dev);
  411. port->port_dev = NULL;
  412. }
  413. SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
  414. task_pid_nr(current), error);
  415. }
  416. static void sas_revalidate_domain(struct work_struct *work)
  417. {
  418. int res = 0;
  419. struct sas_discovery_event *ev = to_sas_discovery_event(work);
  420. struct asd_sas_port *port = ev->port;
  421. struct sas_ha_struct *ha = port->ha;
  422. struct domain_device *ddev = port->port_dev;
  423. /* prevent revalidation from finding sata links in recovery */
  424. mutex_lock(&ha->disco_mutex);
  425. if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
  426. SAS_DPRINTK("REVALIDATION DEFERRED on port %d, pid:%d\n",
  427. port->id, task_pid_nr(current));
  428. goto out;
  429. }
  430. clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
  431. SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
  432. task_pid_nr(current));
  433. if (ddev && (ddev->dev_type == SAS_FANOUT_EXPANDER_DEVICE ||
  434. ddev->dev_type == SAS_EDGE_EXPANDER_DEVICE))
  435. res = sas_ex_revalidate_domain(ddev);
  436. SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
  437. port->id, task_pid_nr(current), res);
  438. out:
  439. mutex_unlock(&ha->disco_mutex);
  440. }
  441. /* ---------- Events ---------- */
  442. static void sas_chain_work(struct sas_ha_struct *ha, struct sas_work *sw)
  443. {
  444. /* chained work is not subject to SA_HA_DRAINING or
  445. * SAS_HA_REGISTERED, because it is either submitted in the
  446. * workqueue, or known to be submitted from a context that is
  447. * not racing against draining
  448. */
  449. scsi_queue_work(ha->core.shost, &sw->work);
  450. }
  451. static void sas_chain_event(int event, unsigned long *pending,
  452. struct sas_work *sw,
  453. struct sas_ha_struct *ha)
  454. {
  455. if (!test_and_set_bit(event, pending)) {
  456. unsigned long flags;
  457. spin_lock_irqsave(&ha->lock, flags);
  458. sas_chain_work(ha, sw);
  459. spin_unlock_irqrestore(&ha->lock, flags);
  460. }
  461. }
  462. int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
  463. {
  464. struct sas_discovery *disc;
  465. if (!port)
  466. return 0;
  467. disc = &port->disc;
  468. BUG_ON(ev >= DISC_NUM_EVENTS);
  469. sas_chain_event(ev, &disc->pending, &disc->disc_work[ev].work, port->ha);
  470. return 0;
  471. }
  472. /**
  473. * sas_init_disc -- initialize the discovery struct in the port
  474. * @port: pointer to struct port
  475. *
  476. * Called when the ports are being initialized.
  477. */
  478. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
  479. {
  480. int i;
  481. static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
  482. [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
  483. [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
  484. [DISCE_PROBE] = sas_probe_devices,
  485. [DISCE_SUSPEND] = sas_suspend_devices,
  486. [DISCE_RESUME] = sas_resume_devices,
  487. [DISCE_DESTRUCT] = sas_destruct_devices,
  488. };
  489. disc->pending = 0;
  490. for (i = 0; i < DISC_NUM_EVENTS; i++) {
  491. INIT_SAS_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
  492. disc->disc_work[i].port = port;
  493. }
  494. }