sas_discover.c 10 KB

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