sas_discover.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_internal.h"
  32. /* ---------- Basic task processing for discovery purposes ---------- */
  33. void sas_init_dev(struct domain_device *dev)
  34. {
  35. INIT_LIST_HEAD(&dev->siblings);
  36. INIT_LIST_HEAD(&dev->dev_list_node);
  37. switch (dev->dev_type) {
  38. case SAS_END_DEV:
  39. break;
  40. case EDGE_DEV:
  41. case FANOUT_DEV:
  42. INIT_LIST_HEAD(&dev->ex_dev.children);
  43. break;
  44. case SATA_DEV:
  45. case SATA_PM:
  46. case SATA_PM_PORT:
  47. INIT_LIST_HEAD(&dev->sata_dev.children);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. /* ---------- Domain device discovery ---------- */
  54. /**
  55. * sas_get_port_device -- Discover devices which caused port creation
  56. * @port: pointer to struct sas_port of interest
  57. *
  58. * Devices directly attached to a HA port, have no parent. This is
  59. * how we know they are (domain) "root" devices. All other devices
  60. * do, and should have their "parent" pointer set appropriately as
  61. * soon as a child device is discovered.
  62. */
  63. static int sas_get_port_device(struct asd_sas_port *port)
  64. {
  65. unsigned long flags;
  66. struct asd_sas_phy *phy;
  67. struct sas_rphy *rphy;
  68. struct domain_device *dev;
  69. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  70. if (!dev)
  71. return -ENOMEM;
  72. spin_lock_irqsave(&port->phy_list_lock, flags);
  73. if (list_empty(&port->phy_list)) {
  74. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  75. kfree(dev);
  76. return -ENODEV;
  77. }
  78. phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
  79. spin_lock(&phy->frame_rcvd_lock);
  80. memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
  81. (size_t)phy->frame_rcvd_size));
  82. spin_unlock(&phy->frame_rcvd_lock);
  83. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  84. if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
  85. struct dev_to_host_fis *fis =
  86. (struct dev_to_host_fis *) dev->frame_rcvd;
  87. if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
  88. fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
  89. && (fis->device & ~0x10) == 0)
  90. dev->dev_type = SATA_PM;
  91. else
  92. dev->dev_type = SATA_DEV;
  93. dev->tproto = SAS_PROTOCOL_SATA;
  94. } else {
  95. struct sas_identify_frame *id =
  96. (struct sas_identify_frame *) dev->frame_rcvd;
  97. dev->dev_type = id->dev_type;
  98. dev->iproto = id->initiator_bits;
  99. dev->tproto = id->target_bits;
  100. }
  101. sas_init_dev(dev);
  102. switch (dev->dev_type) {
  103. case SAS_END_DEV:
  104. case SATA_DEV:
  105. rphy = sas_end_device_alloc(port->port);
  106. break;
  107. case EDGE_DEV:
  108. rphy = sas_expander_alloc(port->port,
  109. SAS_EDGE_EXPANDER_DEVICE);
  110. break;
  111. case FANOUT_DEV:
  112. rphy = sas_expander_alloc(port->port,
  113. SAS_FANOUT_EXPANDER_DEVICE);
  114. break;
  115. default:
  116. printk("ERROR: Unidentified device type %d\n", dev->dev_type);
  117. rphy = NULL;
  118. break;
  119. }
  120. if (!rphy) {
  121. kfree(dev);
  122. return -ENODEV;
  123. }
  124. rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
  125. memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
  126. sas_fill_in_rphy(dev, rphy);
  127. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  128. port->port_dev = dev;
  129. dev->port = port;
  130. dev->linkrate = port->linkrate;
  131. dev->min_linkrate = port->linkrate;
  132. dev->max_linkrate = port->linkrate;
  133. dev->pathways = port->num_phys;
  134. memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
  135. memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
  136. memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
  137. port->disc.max_level = 0;
  138. dev->rphy = rphy;
  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. return 0;
  143. }
  144. /* ---------- Discover and Revalidate ---------- */
  145. int sas_notify_lldd_dev_found(struct domain_device *dev)
  146. {
  147. int res = 0;
  148. struct sas_ha_struct *sas_ha = dev->port->ha;
  149. struct Scsi_Host *shost = sas_ha->core.shost;
  150. struct sas_internal *i = to_sas_internal(shost->transportt);
  151. if (i->dft->lldd_dev_found) {
  152. res = i->dft->lldd_dev_found(dev);
  153. if (res) {
  154. printk("sas: driver on pcidev %s cannot handle "
  155. "device %llx, error:%d\n",
  156. dev_name(sas_ha->dev),
  157. SAS_ADDR(dev->sas_addr), res);
  158. }
  159. }
  160. return res;
  161. }
  162. void sas_notify_lldd_dev_gone(struct domain_device *dev)
  163. {
  164. struct sas_ha_struct *sas_ha = dev->port->ha;
  165. struct Scsi_Host *shost = sas_ha->core.shost;
  166. struct sas_internal *i = to_sas_internal(shost->transportt);
  167. if (i->dft->lldd_dev_gone)
  168. i->dft->lldd_dev_gone(dev);
  169. }
  170. /* ---------- Common/dispatchers ---------- */
  171. /**
  172. * sas_discover_end_dev -- discover an end device (SSP, etc)
  173. * @end: pointer to domain device of interest
  174. *
  175. * See comment in sas_discover_sata().
  176. */
  177. int sas_discover_end_dev(struct domain_device *dev)
  178. {
  179. int res;
  180. res = sas_notify_lldd_dev_found(dev);
  181. if (res)
  182. goto out_err2;
  183. res = sas_rphy_add(dev->rphy);
  184. if (res)
  185. goto out_err;
  186. return 0;
  187. out_err:
  188. sas_notify_lldd_dev_gone(dev);
  189. out_err2:
  190. return res;
  191. }
  192. /* ---------- Device registration and unregistration ---------- */
  193. static inline void sas_unregister_common_dev(struct domain_device *dev)
  194. {
  195. sas_notify_lldd_dev_gone(dev);
  196. if (!dev->parent)
  197. dev->port->port_dev = NULL;
  198. else
  199. list_del_init(&dev->siblings);
  200. list_del_init(&dev->dev_list_node);
  201. }
  202. void sas_unregister_dev(struct domain_device *dev)
  203. {
  204. if (dev->rphy) {
  205. sas_remove_children(&dev->rphy->dev);
  206. sas_rphy_delete(dev->rphy);
  207. dev->rphy = NULL;
  208. }
  209. if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV) {
  210. /* remove the phys and ports, everything else should be gone */
  211. kfree(dev->ex_dev.ex_phy);
  212. dev->ex_dev.ex_phy = NULL;
  213. }
  214. sas_unregister_common_dev(dev);
  215. }
  216. void sas_unregister_domain_devices(struct asd_sas_port *port)
  217. {
  218. struct domain_device *dev, *n;
  219. list_for_each_entry_safe_reverse(dev,n,&port->dev_list,dev_list_node)
  220. sas_unregister_dev(dev);
  221. port->port->rphy = NULL;
  222. }
  223. /* ---------- Discovery and Revalidation ---------- */
  224. /**
  225. * sas_discover_domain -- discover the domain
  226. * @port: port to the domain of interest
  227. *
  228. * NOTE: this process _must_ quit (return) as soon as any connection
  229. * errors are encountered. Connection recovery is done elsewhere.
  230. * Discover process only interrogates devices in order to discover the
  231. * domain.
  232. */
  233. static void sas_discover_domain(struct work_struct *work)
  234. {
  235. struct domain_device *dev;
  236. int error = 0;
  237. struct sas_discovery_event *ev =
  238. container_of(work, struct sas_discovery_event, work);
  239. struct asd_sas_port *port = ev->port;
  240. sas_begin_event(DISCE_DISCOVER_DOMAIN, &port->disc.disc_event_lock,
  241. &port->disc.pending);
  242. if (port->port_dev)
  243. return;
  244. error = sas_get_port_device(port);
  245. if (error)
  246. return;
  247. dev = port->port_dev;
  248. SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
  249. task_pid_nr(current));
  250. switch (dev->dev_type) {
  251. case SAS_END_DEV:
  252. error = sas_discover_end_dev(dev);
  253. break;
  254. case EDGE_DEV:
  255. case FANOUT_DEV:
  256. error = sas_discover_root_expander(dev);
  257. break;
  258. case SATA_DEV:
  259. case SATA_PM:
  260. #ifdef CONFIG_SCSI_SAS_ATA
  261. error = sas_discover_sata(dev);
  262. break;
  263. #else
  264. SAS_DPRINTK("ATA device seen but CONFIG_SCSI_SAS_ATA=N so cannot attach\n");
  265. /* Fall through */
  266. #endif
  267. default:
  268. error = -ENXIO;
  269. SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
  270. break;
  271. }
  272. if (error) {
  273. sas_rphy_free(dev->rphy);
  274. dev->rphy = NULL;
  275. spin_lock_irq(&port->dev_list_lock);
  276. list_del_init(&dev->dev_list_node);
  277. spin_unlock_irq(&port->dev_list_lock);
  278. kfree(dev); /* not kobject_register-ed yet */
  279. port->port_dev = NULL;
  280. }
  281. SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
  282. task_pid_nr(current), error);
  283. }
  284. static void sas_revalidate_domain(struct work_struct *work)
  285. {
  286. int res = 0;
  287. struct sas_discovery_event *ev =
  288. container_of(work, struct sas_discovery_event, work);
  289. struct asd_sas_port *port = ev->port;
  290. sas_begin_event(DISCE_REVALIDATE_DOMAIN, &port->disc.disc_event_lock,
  291. &port->disc.pending);
  292. SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
  293. task_pid_nr(current));
  294. if (port->port_dev)
  295. res = sas_ex_revalidate_domain(port->port_dev);
  296. SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
  297. port->id, task_pid_nr(current), res);
  298. }
  299. /* ---------- Events ---------- */
  300. int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
  301. {
  302. struct sas_discovery *disc;
  303. if (!port)
  304. return 0;
  305. disc = &port->disc;
  306. BUG_ON(ev >= DISC_NUM_EVENTS);
  307. sas_queue_event(ev, &disc->disc_event_lock, &disc->pending,
  308. &disc->disc_work[ev].work, port->ha);
  309. return 0;
  310. }
  311. /**
  312. * sas_init_disc -- initialize the discovery struct in the port
  313. * @port: pointer to struct port
  314. *
  315. * Called when the ports are being initialized.
  316. */
  317. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
  318. {
  319. int i;
  320. static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
  321. [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
  322. [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
  323. };
  324. spin_lock_init(&disc->disc_event_lock);
  325. disc->pending = 0;
  326. for (i = 0; i < DISC_NUM_EVENTS; i++) {
  327. INIT_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
  328. disc->disc_work[i].port = port;
  329. }
  330. }