sas_discover.c 14 KB

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