scsi_transport_sas.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * Copyright (C) 2005 Dell Inc.
  3. * Released under GPL v2.
  4. *
  5. * Serial Attached SCSI (SAS) transport class.
  6. *
  7. * The SAS transport class contains common code to deal with SAS HBAs,
  8. * an aproximated representation of SAS topologies in the driver model,
  9. * and various sysfs attributes to expose these topologies and managment
  10. * interfaces to userspace.
  11. *
  12. * In addition to the basic SCSI core objects this transport class
  13. * introduces two additional intermediate objects: The SAS PHY
  14. * as represented by struct sas_phy defines an "outgoing" PHY on
  15. * a SAS HBA or Expander, and the SAS remote PHY represented by
  16. * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
  17. * end device. Note that this is purely a software concept, the
  18. * underlying hardware for a PHY and a remote PHY is the exactly
  19. * the same.
  20. *
  21. * There is no concept of a SAS port in this code, users can see
  22. * what PHYs form a wide port based on the port_identifier attribute,
  23. * which is the same for all PHYs in a port.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/err.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport.h>
  31. #include <scsi/scsi_transport_sas.h>
  32. #define SAS_HOST_ATTRS 0
  33. #define SAS_PORT_ATTRS 11
  34. #define SAS_RPORT_ATTRS 5
  35. struct sas_internal {
  36. struct scsi_transport_template t;
  37. struct sas_function_template *f;
  38. struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
  39. struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
  40. struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
  41. struct transport_container phy_attr_cont;
  42. struct transport_container rphy_attr_cont;
  43. /*
  44. * The array of null terminated pointers to attributes
  45. * needed by scsi_sysfs.c
  46. */
  47. struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
  48. struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
  49. struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
  50. };
  51. #define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t)
  52. struct sas_host_attrs {
  53. struct list_head rphy_list;
  54. spinlock_t lock;
  55. u32 next_target_id;
  56. };
  57. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  58. /*
  59. * Hack to allow attributes of the same name in different objects.
  60. */
  61. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  62. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  63. __ATTR(_name,_mode,_show,_store)
  64. /*
  65. * Pretty printing helpers
  66. */
  67. #define sas_bitfield_name_match(title, table) \
  68. static ssize_t \
  69. get_sas_##title##_names(u32 table_key, char *buf) \
  70. { \
  71. char *prefix = ""; \
  72. ssize_t len = 0; \
  73. int i; \
  74. \
  75. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  76. if (table[i].value & table_key) { \
  77. len += sprintf(buf + len, "%s%s", \
  78. prefix, table[i].name); \
  79. prefix = ", "; \
  80. } \
  81. } \
  82. len += sprintf(buf + len, "\n"); \
  83. return len; \
  84. }
  85. #define sas_bitfield_name_search(title, table) \
  86. static ssize_t \
  87. get_sas_##title##_names(u32 table_key, char *buf) \
  88. { \
  89. ssize_t len = 0; \
  90. int i; \
  91. \
  92. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  93. if (table[i].value == table_key) { \
  94. len += sprintf(buf + len, "%s", \
  95. table[i].name); \
  96. break; \
  97. } \
  98. } \
  99. len += sprintf(buf + len, "\n"); \
  100. return len; \
  101. }
  102. static struct {
  103. u32 value;
  104. char *name;
  105. } sas_device_type_names[] = {
  106. { SAS_PHY_UNUSED, "unused" },
  107. { SAS_END_DEVICE, "end device" },
  108. { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
  109. { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
  110. };
  111. sas_bitfield_name_search(device_type, sas_device_type_names)
  112. static struct {
  113. u32 value;
  114. char *name;
  115. } sas_protocol_names[] = {
  116. { SAS_PROTOCOL_SATA, "sata" },
  117. { SAS_PROTOCOL_SMP, "smp" },
  118. { SAS_PROTOCOL_STP, "stp" },
  119. { SAS_PROTOCOL_SSP, "ssp" },
  120. };
  121. sas_bitfield_name_match(protocol, sas_protocol_names)
  122. static struct {
  123. u32 value;
  124. char *name;
  125. } sas_linkspeed_names[] = {
  126. { SAS_LINK_RATE_UNKNOWN, "Unknown" },
  127. { SAS_PHY_DISABLED, "Phy disabled" },
  128. { SAS_LINK_RATE_FAILED, "Link Rate failed" },
  129. { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
  130. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  131. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  132. };
  133. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  134. /*
  135. * SAS host attributes
  136. */
  137. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  138. struct class_device *cdev)
  139. {
  140. struct Scsi_Host *shost = dev_to_shost(dev);
  141. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  142. INIT_LIST_HEAD(&sas_host->rphy_list);
  143. spin_lock_init(&sas_host->lock);
  144. sas_host->next_target_id = 0;
  145. return 0;
  146. }
  147. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  148. "sas_host", sas_host_setup, NULL, NULL);
  149. static int sas_host_match(struct attribute_container *cont,
  150. struct device *dev)
  151. {
  152. struct Scsi_Host *shost;
  153. struct sas_internal *i;
  154. if (!scsi_is_host_device(dev))
  155. return 0;
  156. shost = dev_to_shost(dev);
  157. if (!shost->transportt)
  158. return 0;
  159. if (shost->transportt->host_attrs.ac.class !=
  160. &sas_host_class.class)
  161. return 0;
  162. i = to_sas_internal(shost->transportt);
  163. return &i->t.host_attrs.ac == cont;
  164. }
  165. static int do_sas_phy_delete(struct device *dev, void *data)
  166. {
  167. if (scsi_is_sas_phy(dev))
  168. sas_phy_delete(dev_to_phy(dev));
  169. return 0;
  170. }
  171. /**
  172. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  173. * @shost: Scsi Host that is torn down
  174. *
  175. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  176. * Must be called just before scsi_remove_host for SAS HBAs.
  177. */
  178. void sas_remove_host(struct Scsi_Host *shost)
  179. {
  180. device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
  181. }
  182. EXPORT_SYMBOL(sas_remove_host);
  183. /*
  184. * SAS Port attributes
  185. */
  186. #define sas_phy_show_simple(field, name, format_string, cast) \
  187. static ssize_t \
  188. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  189. { \
  190. struct sas_phy *phy = transport_class_to_phy(cdev); \
  191. \
  192. return snprintf(buf, 20, format_string, cast phy->field); \
  193. }
  194. #define sas_phy_simple_attr(field, name, format_string, type) \
  195. sas_phy_show_simple(field, name, format_string, (type)) \
  196. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  197. #define sas_phy_show_protocol(field, name) \
  198. static ssize_t \
  199. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  200. { \
  201. struct sas_phy *phy = transport_class_to_phy(cdev); \
  202. \
  203. if (!phy->field) \
  204. return snprintf(buf, 20, "none\n"); \
  205. return get_sas_protocol_names(phy->field, buf); \
  206. }
  207. #define sas_phy_protocol_attr(field, name) \
  208. sas_phy_show_protocol(field, name) \
  209. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  210. #define sas_phy_show_linkspeed(field) \
  211. static ssize_t \
  212. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  213. { \
  214. struct sas_phy *phy = transport_class_to_phy(cdev); \
  215. \
  216. return get_sas_linkspeed_names(phy->field, buf); \
  217. }
  218. #define sas_phy_linkspeed_attr(field) \
  219. sas_phy_show_linkspeed(field) \
  220. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  221. static ssize_t
  222. show_sas_device_type(struct class_device *cdev, char *buf)
  223. {
  224. struct sas_phy *phy = transport_class_to_phy(cdev);
  225. if (!phy->identify.device_type)
  226. return snprintf(buf, 20, "none\n");
  227. return get_sas_device_type_names(phy->identify.device_type, buf);
  228. }
  229. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  230. sas_phy_protocol_attr(identify.initiator_port_protocols,
  231. initiator_port_protocols);
  232. sas_phy_protocol_attr(identify.target_port_protocols,
  233. target_port_protocols);
  234. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  235. unsigned long long);
  236. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  237. sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
  238. sas_phy_linkspeed_attr(negotiated_linkrate);
  239. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  240. sas_phy_linkspeed_attr(minimum_linkrate);
  241. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  242. sas_phy_linkspeed_attr(maximum_linkrate);
  243. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  244. "sas_phy", NULL, NULL, NULL);
  245. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  246. {
  247. struct Scsi_Host *shost;
  248. struct sas_internal *i;
  249. if (!scsi_is_sas_phy(dev))
  250. return 0;
  251. shost = dev_to_shost(dev->parent);
  252. if (!shost->transportt)
  253. return 0;
  254. if (shost->transportt->host_attrs.ac.class !=
  255. &sas_host_class.class)
  256. return 0;
  257. i = to_sas_internal(shost->transportt);
  258. return &i->phy_attr_cont.ac == cont;
  259. }
  260. static void sas_phy_release(struct device *dev)
  261. {
  262. struct sas_phy *phy = dev_to_phy(dev);
  263. put_device(dev->parent);
  264. kfree(phy);
  265. }
  266. /**
  267. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  268. * @parent: Parent device
  269. * @number: Port number
  270. *
  271. * Allocates an SAS PHY structure. It will be added in the device tree
  272. * below the device specified by @parent, which has to be either a Scsi_Host
  273. * or sas_rphy.
  274. *
  275. * Returns:
  276. * SAS PHY allocated or %NULL if the allocation failed.
  277. */
  278. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  279. {
  280. struct Scsi_Host *shost = dev_to_shost(parent);
  281. struct sas_phy *phy;
  282. phy = kmalloc(sizeof(*phy), GFP_KERNEL);
  283. if (!phy)
  284. return NULL;
  285. memset(phy, 0, sizeof(*phy));
  286. get_device(parent);
  287. phy->number = number;
  288. device_initialize(&phy->dev);
  289. phy->dev.parent = get_device(parent);
  290. phy->dev.release = sas_phy_release;
  291. sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
  292. transport_setup_device(&phy->dev);
  293. return phy;
  294. }
  295. EXPORT_SYMBOL(sas_phy_alloc);
  296. /**
  297. * sas_phy_add -- add a SAS PHY to the device hierachy
  298. * @phy: The PHY to be added
  299. *
  300. * Publishes a SAS PHY to the rest of the system.
  301. */
  302. int sas_phy_add(struct sas_phy *phy)
  303. {
  304. int error;
  305. error = device_add(&phy->dev);
  306. if (!error) {
  307. transport_add_device(&phy->dev);
  308. transport_configure_device(&phy->dev);
  309. }
  310. return error;
  311. }
  312. EXPORT_SYMBOL(sas_phy_add);
  313. /**
  314. * sas_phy_free -- free a SAS PHY
  315. * @phy: SAS PHY to free
  316. *
  317. * Frees the specified SAS PHY.
  318. *
  319. * Note:
  320. * This function must only be called on a PHY that has not
  321. * sucessfully been added using sas_phy_add().
  322. */
  323. void sas_phy_free(struct sas_phy *phy)
  324. {
  325. transport_destroy_device(&phy->dev);
  326. put_device(phy->dev.parent);
  327. put_device(phy->dev.parent);
  328. put_device(phy->dev.parent);
  329. kfree(phy);
  330. }
  331. EXPORT_SYMBOL(sas_phy_free);
  332. /**
  333. * sas_phy_delete -- remove SAS PHY
  334. * @phy: SAS PHY to remove
  335. *
  336. * Removes the specified SAS PHY. If the SAS PHY has an
  337. * associated remote PHY it is removed before.
  338. */
  339. void
  340. sas_phy_delete(struct sas_phy *phy)
  341. {
  342. struct device *dev = &phy->dev;
  343. if (phy->rphy)
  344. sas_rphy_delete(phy->rphy);
  345. transport_remove_device(dev);
  346. device_del(dev);
  347. transport_destroy_device(dev);
  348. put_device(dev->parent);
  349. }
  350. EXPORT_SYMBOL(sas_phy_delete);
  351. /**
  352. * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
  353. * @dev: device to check
  354. *
  355. * Returns:
  356. * %1 if the device represents a SAS PHY, %0 else
  357. */
  358. int scsi_is_sas_phy(const struct device *dev)
  359. {
  360. return dev->release == sas_phy_release;
  361. }
  362. EXPORT_SYMBOL(scsi_is_sas_phy);
  363. /*
  364. * SAS remote PHY attributes.
  365. */
  366. #define sas_rphy_show_simple(field, name, format_string, cast) \
  367. static ssize_t \
  368. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  369. { \
  370. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  371. \
  372. return snprintf(buf, 20, format_string, cast rphy->field); \
  373. }
  374. #define sas_rphy_simple_attr(field, name, format_string, type) \
  375. sas_rphy_show_simple(field, name, format_string, (type)) \
  376. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  377. show_sas_rphy_##name, NULL)
  378. #define sas_rphy_show_protocol(field, name) \
  379. static ssize_t \
  380. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  381. { \
  382. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  383. \
  384. if (!rphy->field) \
  385. return snprintf(buf, 20, "none\n"); \
  386. return get_sas_protocol_names(rphy->field, buf); \
  387. }
  388. #define sas_rphy_protocol_attr(field, name) \
  389. sas_rphy_show_protocol(field, name) \
  390. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  391. show_sas_rphy_##name, NULL)
  392. static ssize_t
  393. show_sas_rphy_device_type(struct class_device *cdev, char *buf)
  394. {
  395. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  396. if (!rphy->identify.device_type)
  397. return snprintf(buf, 20, "none\n");
  398. return get_sas_device_type_names(
  399. rphy->identify.device_type, buf);
  400. }
  401. static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  402. show_sas_rphy_device_type, NULL);
  403. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  404. initiator_port_protocols);
  405. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  406. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  407. unsigned long long);
  408. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  409. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  410. "sas_rphy", NULL, NULL, NULL);
  411. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  412. {
  413. struct Scsi_Host *shost;
  414. struct sas_internal *i;
  415. if (!scsi_is_sas_rphy(dev))
  416. return 0;
  417. shost = dev_to_shost(dev->parent->parent);
  418. if (!shost->transportt)
  419. return 0;
  420. if (shost->transportt->host_attrs.ac.class !=
  421. &sas_host_class.class)
  422. return 0;
  423. i = to_sas_internal(shost->transportt);
  424. return &i->rphy_attr_cont.ac == cont;
  425. }
  426. static void sas_rphy_release(struct device *dev)
  427. {
  428. struct sas_rphy *rphy = dev_to_rphy(dev);
  429. put_device(dev->parent);
  430. kfree(rphy);
  431. }
  432. /**
  433. * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure
  434. * @parent: SAS PHY this remote PHY is conneted to
  435. *
  436. * Allocates an SAS remote PHY structure, connected to @parent.
  437. *
  438. * Returns:
  439. * SAS PHY allocated or %NULL if the allocation failed.
  440. */
  441. struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
  442. {
  443. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  444. struct sas_rphy *rphy;
  445. rphy = kmalloc(sizeof(*rphy), GFP_KERNEL);
  446. if (!rphy) {
  447. put_device(&parent->dev);
  448. return NULL;
  449. }
  450. memset(rphy, 0, sizeof(*rphy));
  451. device_initialize(&rphy->dev);
  452. rphy->dev.parent = get_device(&parent->dev);
  453. rphy->dev.release = sas_rphy_release;
  454. sprintf(rphy->dev.bus_id, "rphy-%d:%d",
  455. shost->host_no, parent->number);
  456. transport_setup_device(&rphy->dev);
  457. return rphy;
  458. }
  459. EXPORT_SYMBOL(sas_rphy_alloc);
  460. /**
  461. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  462. * @rphy: The remote PHY to be added
  463. *
  464. * Publishes a SAS remote PHY to the rest of the system.
  465. */
  466. int sas_rphy_add(struct sas_rphy *rphy)
  467. {
  468. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  469. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  470. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  471. struct sas_identify *identify = &rphy->identify;
  472. int error;
  473. if (parent->rphy)
  474. return -ENXIO;
  475. parent->rphy = rphy;
  476. error = device_add(&rphy->dev);
  477. if (error)
  478. return error;
  479. transport_add_device(&rphy->dev);
  480. transport_configure_device(&rphy->dev);
  481. spin_lock(&sas_host->lock);
  482. list_add_tail(&rphy->list, &sas_host->rphy_list);
  483. if (identify->device_type == SAS_END_DEVICE &&
  484. (identify->target_port_protocols &
  485. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  486. rphy->scsi_target_id = sas_host->next_target_id++;
  487. else
  488. rphy->scsi_target_id = -1;
  489. spin_unlock(&sas_host->lock);
  490. if (rphy->scsi_target_id != -1) {
  491. scsi_scan_target(&rphy->dev, parent->number,
  492. rphy->scsi_target_id, ~0, 0);
  493. }
  494. return 0;
  495. }
  496. EXPORT_SYMBOL(sas_rphy_add);
  497. /**
  498. * sas_rphy_free -- free a SAS remote PHY
  499. * @rphy SAS remote PHY to free
  500. *
  501. * Frees the specified SAS remote PHY.
  502. *
  503. * Note:
  504. * This function must only be called on a remote
  505. * PHY that has not sucessfully been added using
  506. * sas_rphy_add().
  507. */
  508. void sas_rphy_free(struct sas_rphy *rphy)
  509. {
  510. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  511. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  512. spin_lock(&sas_host->lock);
  513. list_del(&rphy->list);
  514. spin_unlock(&sas_host->lock);
  515. transport_destroy_device(&rphy->dev);
  516. put_device(rphy->dev.parent);
  517. put_device(rphy->dev.parent);
  518. put_device(rphy->dev.parent);
  519. kfree(rphy);
  520. }
  521. EXPORT_SYMBOL(sas_rphy_free);
  522. /**
  523. * sas_rphy_delete -- remove SAS remote PHY
  524. * @rphy: SAS remote PHY to remove
  525. *
  526. * Removes the specified SAS remote PHY.
  527. */
  528. void
  529. sas_rphy_delete(struct sas_rphy *rphy)
  530. {
  531. struct device *dev = &rphy->dev;
  532. struct sas_phy *parent = dev_to_phy(dev->parent);
  533. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  534. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  535. transport_destroy_device(&rphy->dev);
  536. scsi_remove_target(&rphy->dev);
  537. spin_lock(&sas_host->lock);
  538. list_del(&rphy->list);
  539. spin_unlock(&sas_host->lock);
  540. transport_remove_device(dev);
  541. device_del(dev);
  542. transport_destroy_device(dev);
  543. put_device(&parent->dev);
  544. }
  545. EXPORT_SYMBOL(sas_rphy_delete);
  546. /**
  547. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  548. * @dev: device to check
  549. *
  550. * Returns:
  551. * %1 if the device represents a SAS remote PHY, %0 else
  552. */
  553. int scsi_is_sas_rphy(const struct device *dev)
  554. {
  555. return dev->release == sas_rphy_release;
  556. }
  557. EXPORT_SYMBOL(scsi_is_sas_rphy);
  558. /*
  559. * SCSI scan helper
  560. */
  561. static struct device *sas_target_parent(struct Scsi_Host *shost,
  562. int channel, uint id)
  563. {
  564. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  565. struct sas_rphy *rphy;
  566. struct device *dev = NULL;
  567. spin_lock(&sas_host->lock);
  568. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  569. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  570. if (parent->number == channel &&
  571. rphy->scsi_target_id == id)
  572. dev = &rphy->dev;
  573. }
  574. spin_unlock(&sas_host->lock);
  575. return dev;
  576. }
  577. /*
  578. * Setup / Teardown code
  579. */
  580. #define SETUP_RPORT_ATTRIBUTE(field) \
  581. i->private_rphy_attrs[count] = class_device_attr_##field; \
  582. i->private_rphy_attrs[count].attr.mode = S_IRUGO; \
  583. i->private_rphy_attrs[count].store = NULL; \
  584. i->rphy_attrs[count] = &i->private_rphy_attrs[count]; \
  585. count++
  586. #define SETUP_PORT_ATTRIBUTE(field) \
  587. i->private_phy_attrs[count] = class_device_attr_##field; \
  588. i->private_phy_attrs[count].attr.mode = S_IRUGO; \
  589. i->private_phy_attrs[count].store = NULL; \
  590. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  591. count++
  592. /**
  593. * sas_attach_transport -- instantiate SAS transport template
  594. * @ft: SAS transport class function template
  595. */
  596. struct scsi_transport_template *
  597. sas_attach_transport(struct sas_function_template *ft)
  598. {
  599. struct sas_internal *i;
  600. int count;
  601. i = kmalloc(sizeof(struct sas_internal), GFP_KERNEL);
  602. if (!i)
  603. return NULL;
  604. memset(i, 0, sizeof(struct sas_internal));
  605. i->t.target_parent = sas_target_parent;
  606. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  607. i->t.host_attrs.ac.class = &sas_host_class.class;
  608. i->t.host_attrs.ac.match = sas_host_match;
  609. transport_container_register(&i->t.host_attrs);
  610. i->t.host_size = sizeof(struct sas_host_attrs);
  611. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  612. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  613. i->phy_attr_cont.ac.match = sas_phy_match;
  614. transport_container_register(&i->phy_attr_cont);
  615. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  616. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  617. i->rphy_attr_cont.ac.match = sas_rphy_match;
  618. transport_container_register(&i->rphy_attr_cont);
  619. i->f = ft;
  620. count = 0;
  621. i->host_attrs[count] = NULL;
  622. count = 0;
  623. SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
  624. SETUP_PORT_ATTRIBUTE(target_port_protocols);
  625. SETUP_PORT_ATTRIBUTE(device_type);
  626. SETUP_PORT_ATTRIBUTE(sas_address);
  627. SETUP_PORT_ATTRIBUTE(phy_identifier);
  628. SETUP_PORT_ATTRIBUTE(port_identifier);
  629. SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
  630. SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
  631. SETUP_PORT_ATTRIBUTE(minimum_linkrate);
  632. SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
  633. SETUP_PORT_ATTRIBUTE(maximum_linkrate);
  634. i->phy_attrs[count] = NULL;
  635. count = 0;
  636. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  637. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  638. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  639. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  640. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  641. i->rphy_attrs[count] = NULL;
  642. return &i->t;
  643. }
  644. EXPORT_SYMBOL(sas_attach_transport);
  645. /**
  646. * sas_release_transport -- release SAS transport template instance
  647. * @t: transport template instance
  648. */
  649. void sas_release_transport(struct scsi_transport_template *t)
  650. {
  651. struct sas_internal *i = to_sas_internal(t);
  652. transport_container_unregister(&i->t.host_attrs);
  653. transport_container_unregister(&i->phy_attr_cont);
  654. transport_container_unregister(&i->rphy_attr_cont);
  655. kfree(i);
  656. }
  657. EXPORT_SYMBOL(sas_release_transport);
  658. static __init int sas_transport_init(void)
  659. {
  660. int error;
  661. error = transport_class_register(&sas_host_class);
  662. if (error)
  663. goto out;
  664. error = transport_class_register(&sas_phy_class);
  665. if (error)
  666. goto out_unregister_transport;
  667. error = transport_class_register(&sas_rphy_class);
  668. if (error)
  669. goto out_unregister_phy;
  670. return 0;
  671. out_unregister_phy:
  672. transport_class_unregister(&sas_phy_class);
  673. out_unregister_transport:
  674. transport_class_unregister(&sas_host_class);
  675. out:
  676. return error;
  677. }
  678. static void __exit sas_transport_exit(void)
  679. {
  680. transport_class_unregister(&sas_host_class);
  681. transport_class_unregister(&sas_phy_class);
  682. transport_class_unregister(&sas_rphy_class);
  683. }
  684. MODULE_AUTHOR("Christoph Hellwig");
  685. MODULE_DESCRIPTION("SAS Transphy Attributes");
  686. MODULE_LICENSE("GPL");
  687. module_init(sas_transport_init);
  688. module_exit(sas_transport_exit);