scsi_transport_sas.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * Copyright (C) 2005-2006 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 <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. #define SAS_HOST_ATTRS 0
  36. #define SAS_PORT_ATTRS 17
  37. #define SAS_RPORT_ATTRS 7
  38. struct sas_internal {
  39. struct scsi_transport_template t;
  40. struct sas_function_template *f;
  41. struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
  42. struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
  43. struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
  44. struct transport_container phy_attr_cont;
  45. struct transport_container rphy_attr_cont;
  46. /*
  47. * The array of null terminated pointers to attributes
  48. * needed by scsi_sysfs.c
  49. */
  50. struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
  51. struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
  52. struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
  53. };
  54. #define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t)
  55. struct sas_host_attrs {
  56. struct list_head rphy_list;
  57. struct mutex lock;
  58. u32 next_target_id;
  59. };
  60. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  61. /*
  62. * Hack to allow attributes of the same name in different objects.
  63. */
  64. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  65. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  66. __ATTR(_name,_mode,_show,_store)
  67. /*
  68. * Pretty printing helpers
  69. */
  70. #define sas_bitfield_name_match(title, table) \
  71. static ssize_t \
  72. get_sas_##title##_names(u32 table_key, char *buf) \
  73. { \
  74. char *prefix = ""; \
  75. ssize_t len = 0; \
  76. int i; \
  77. \
  78. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  79. if (table[i].value & table_key) { \
  80. len += sprintf(buf + len, "%s%s", \
  81. prefix, table[i].name); \
  82. prefix = ", "; \
  83. } \
  84. } \
  85. len += sprintf(buf + len, "\n"); \
  86. return len; \
  87. }
  88. #define sas_bitfield_name_search(title, table) \
  89. static ssize_t \
  90. get_sas_##title##_names(u32 table_key, char *buf) \
  91. { \
  92. ssize_t len = 0; \
  93. int i; \
  94. \
  95. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  96. if (table[i].value == table_key) { \
  97. len += sprintf(buf + len, "%s", \
  98. table[i].name); \
  99. break; \
  100. } \
  101. } \
  102. len += sprintf(buf + len, "\n"); \
  103. return len; \
  104. }
  105. static struct {
  106. u32 value;
  107. char *name;
  108. } sas_device_type_names[] = {
  109. { SAS_PHY_UNUSED, "unused" },
  110. { SAS_END_DEVICE, "end device" },
  111. { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
  112. { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
  113. };
  114. sas_bitfield_name_search(device_type, sas_device_type_names)
  115. static struct {
  116. u32 value;
  117. char *name;
  118. } sas_protocol_names[] = {
  119. { SAS_PROTOCOL_SATA, "sata" },
  120. { SAS_PROTOCOL_SMP, "smp" },
  121. { SAS_PROTOCOL_STP, "stp" },
  122. { SAS_PROTOCOL_SSP, "ssp" },
  123. };
  124. sas_bitfield_name_match(protocol, sas_protocol_names)
  125. static struct {
  126. u32 value;
  127. char *name;
  128. } sas_linkspeed_names[] = {
  129. { SAS_LINK_RATE_UNKNOWN, "Unknown" },
  130. { SAS_PHY_DISABLED, "Phy disabled" },
  131. { SAS_LINK_RATE_FAILED, "Link Rate failed" },
  132. { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
  133. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  134. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  135. { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
  136. };
  137. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  138. /*
  139. * SAS host attributes
  140. */
  141. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  142. struct class_device *cdev)
  143. {
  144. struct Scsi_Host *shost = dev_to_shost(dev);
  145. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  146. INIT_LIST_HEAD(&sas_host->rphy_list);
  147. mutex_init(&sas_host->lock);
  148. sas_host->next_target_id = 0;
  149. return 0;
  150. }
  151. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  152. "sas_host", sas_host_setup, NULL, NULL);
  153. static int sas_host_match(struct attribute_container *cont,
  154. struct device *dev)
  155. {
  156. struct Scsi_Host *shost;
  157. struct sas_internal *i;
  158. if (!scsi_is_host_device(dev))
  159. return 0;
  160. shost = dev_to_shost(dev);
  161. if (!shost->transportt)
  162. return 0;
  163. if (shost->transportt->host_attrs.ac.class !=
  164. &sas_host_class.class)
  165. return 0;
  166. i = to_sas_internal(shost->transportt);
  167. return &i->t.host_attrs.ac == cont;
  168. }
  169. static int do_sas_phy_delete(struct device *dev, void *data)
  170. {
  171. if (scsi_is_sas_phy(dev))
  172. sas_phy_delete(dev_to_phy(dev));
  173. return 0;
  174. }
  175. /**
  176. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  177. * @shost: Scsi Host that is torn down
  178. *
  179. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  180. * Must be called just before scsi_remove_host for SAS HBAs.
  181. */
  182. void sas_remove_host(struct Scsi_Host *shost)
  183. {
  184. device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
  185. }
  186. EXPORT_SYMBOL(sas_remove_host);
  187. /*
  188. * SAS Port attributes
  189. */
  190. #define sas_phy_show_simple(field, name, format_string, cast) \
  191. static ssize_t \
  192. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  193. { \
  194. struct sas_phy *phy = transport_class_to_phy(cdev); \
  195. \
  196. return snprintf(buf, 20, format_string, cast phy->field); \
  197. }
  198. #define sas_phy_simple_attr(field, name, format_string, type) \
  199. sas_phy_show_simple(field, name, format_string, (type)) \
  200. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  201. #define sas_phy_show_protocol(field, name) \
  202. static ssize_t \
  203. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  204. { \
  205. struct sas_phy *phy = transport_class_to_phy(cdev); \
  206. \
  207. if (!phy->field) \
  208. return snprintf(buf, 20, "none\n"); \
  209. return get_sas_protocol_names(phy->field, buf); \
  210. }
  211. #define sas_phy_protocol_attr(field, name) \
  212. sas_phy_show_protocol(field, name) \
  213. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  214. #define sas_phy_show_linkspeed(field) \
  215. static ssize_t \
  216. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  217. { \
  218. struct sas_phy *phy = transport_class_to_phy(cdev); \
  219. \
  220. return get_sas_linkspeed_names(phy->field, buf); \
  221. }
  222. #define sas_phy_linkspeed_attr(field) \
  223. sas_phy_show_linkspeed(field) \
  224. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  225. #define sas_phy_show_linkerror(field) \
  226. static ssize_t \
  227. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  228. { \
  229. struct sas_phy *phy = transport_class_to_phy(cdev); \
  230. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  231. struct sas_internal *i = to_sas_internal(shost->transportt); \
  232. int error; \
  233. \
  234. if (!phy->local_attached) \
  235. return -EINVAL; \
  236. \
  237. error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
  238. if (error) \
  239. return error; \
  240. return snprintf(buf, 20, "%u\n", phy->field); \
  241. }
  242. #define sas_phy_linkerror_attr(field) \
  243. sas_phy_show_linkerror(field) \
  244. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  245. static ssize_t
  246. show_sas_device_type(struct class_device *cdev, char *buf)
  247. {
  248. struct sas_phy *phy = transport_class_to_phy(cdev);
  249. if (!phy->identify.device_type)
  250. return snprintf(buf, 20, "none\n");
  251. return get_sas_device_type_names(phy->identify.device_type, buf);
  252. }
  253. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  254. static ssize_t do_sas_phy_reset(struct class_device *cdev,
  255. size_t count, int hard_reset)
  256. {
  257. struct sas_phy *phy = transport_class_to_phy(cdev);
  258. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  259. struct sas_internal *i = to_sas_internal(shost->transportt);
  260. int error;
  261. if (!phy->local_attached)
  262. return -EINVAL;
  263. error = i->f->phy_reset(phy, hard_reset);
  264. if (error)
  265. return error;
  266. return count;
  267. };
  268. static ssize_t store_sas_link_reset(struct class_device *cdev,
  269. const char *buf, size_t count)
  270. {
  271. return do_sas_phy_reset(cdev, count, 0);
  272. }
  273. static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  274. static ssize_t store_sas_hard_reset(struct class_device *cdev,
  275. const char *buf, size_t count)
  276. {
  277. return do_sas_phy_reset(cdev, count, 1);
  278. }
  279. static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  280. sas_phy_protocol_attr(identify.initiator_port_protocols,
  281. initiator_port_protocols);
  282. sas_phy_protocol_attr(identify.target_port_protocols,
  283. target_port_protocols);
  284. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  285. unsigned long long);
  286. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  287. sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
  288. sas_phy_linkspeed_attr(negotiated_linkrate);
  289. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  290. sas_phy_linkspeed_attr(minimum_linkrate);
  291. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  292. sas_phy_linkspeed_attr(maximum_linkrate);
  293. sas_phy_linkerror_attr(invalid_dword_count);
  294. sas_phy_linkerror_attr(running_disparity_error_count);
  295. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  296. sas_phy_linkerror_attr(phy_reset_problem_count);
  297. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  298. "sas_phy", NULL, NULL, NULL);
  299. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  300. {
  301. struct Scsi_Host *shost;
  302. struct sas_internal *i;
  303. if (!scsi_is_sas_phy(dev))
  304. return 0;
  305. shost = dev_to_shost(dev->parent);
  306. if (!shost->transportt)
  307. return 0;
  308. if (shost->transportt->host_attrs.ac.class !=
  309. &sas_host_class.class)
  310. return 0;
  311. i = to_sas_internal(shost->transportt);
  312. return &i->phy_attr_cont.ac == cont;
  313. }
  314. static void sas_phy_release(struct device *dev)
  315. {
  316. struct sas_phy *phy = dev_to_phy(dev);
  317. put_device(dev->parent);
  318. kfree(phy);
  319. }
  320. /**
  321. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  322. * @parent: Parent device
  323. * @number: Phy index
  324. *
  325. * Allocates an SAS PHY structure. It will be added in the device tree
  326. * below the device specified by @parent, which has to be either a Scsi_Host
  327. * or sas_rphy.
  328. *
  329. * Returns:
  330. * SAS PHY allocated or %NULL if the allocation failed.
  331. */
  332. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  333. {
  334. struct Scsi_Host *shost = dev_to_shost(parent);
  335. struct sas_phy *phy;
  336. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  337. if (!phy)
  338. return NULL;
  339. get_device(parent);
  340. phy->number = number;
  341. device_initialize(&phy->dev);
  342. phy->dev.parent = get_device(parent);
  343. phy->dev.release = sas_phy_release;
  344. sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
  345. transport_setup_device(&phy->dev);
  346. return phy;
  347. }
  348. EXPORT_SYMBOL(sas_phy_alloc);
  349. /**
  350. * sas_phy_add -- add a SAS PHY to the device hierachy
  351. * @phy: The PHY to be added
  352. *
  353. * Publishes a SAS PHY to the rest of the system.
  354. */
  355. int sas_phy_add(struct sas_phy *phy)
  356. {
  357. int error;
  358. error = device_add(&phy->dev);
  359. if (!error) {
  360. transport_add_device(&phy->dev);
  361. transport_configure_device(&phy->dev);
  362. }
  363. return error;
  364. }
  365. EXPORT_SYMBOL(sas_phy_add);
  366. /**
  367. * sas_phy_free -- free a SAS PHY
  368. * @phy: SAS PHY to free
  369. *
  370. * Frees the specified SAS PHY.
  371. *
  372. * Note:
  373. * This function must only be called on a PHY that has not
  374. * sucessfully been added using sas_phy_add().
  375. */
  376. void sas_phy_free(struct sas_phy *phy)
  377. {
  378. transport_destroy_device(&phy->dev);
  379. put_device(phy->dev.parent);
  380. put_device(phy->dev.parent);
  381. put_device(phy->dev.parent);
  382. kfree(phy);
  383. }
  384. EXPORT_SYMBOL(sas_phy_free);
  385. /**
  386. * sas_phy_delete -- remove SAS PHY
  387. * @phy: SAS PHY to remove
  388. *
  389. * Removes the specified SAS PHY. If the SAS PHY has an
  390. * associated remote PHY it is removed before.
  391. */
  392. void
  393. sas_phy_delete(struct sas_phy *phy)
  394. {
  395. struct device *dev = &phy->dev;
  396. if (phy->rphy)
  397. sas_rphy_delete(phy->rphy);
  398. transport_remove_device(dev);
  399. device_del(dev);
  400. transport_destroy_device(dev);
  401. put_device(dev->parent);
  402. }
  403. EXPORT_SYMBOL(sas_phy_delete);
  404. /**
  405. * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
  406. * @dev: device to check
  407. *
  408. * Returns:
  409. * %1 if the device represents a SAS PHY, %0 else
  410. */
  411. int scsi_is_sas_phy(const struct device *dev)
  412. {
  413. return dev->release == sas_phy_release;
  414. }
  415. EXPORT_SYMBOL(scsi_is_sas_phy);
  416. /*
  417. * SAS remote PHY attributes.
  418. */
  419. #define sas_rphy_show_simple(field, name, format_string, cast) \
  420. static ssize_t \
  421. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  422. { \
  423. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  424. \
  425. return snprintf(buf, 20, format_string, cast rphy->field); \
  426. }
  427. #define sas_rphy_simple_attr(field, name, format_string, type) \
  428. sas_rphy_show_simple(field, name, format_string, (type)) \
  429. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  430. show_sas_rphy_##name, NULL)
  431. #define sas_rphy_show_protocol(field, name) \
  432. static ssize_t \
  433. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  434. { \
  435. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  436. \
  437. if (!rphy->field) \
  438. return snprintf(buf, 20, "none\n"); \
  439. return get_sas_protocol_names(rphy->field, buf); \
  440. }
  441. #define sas_rphy_protocol_attr(field, name) \
  442. sas_rphy_show_protocol(field, name) \
  443. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  444. show_sas_rphy_##name, NULL)
  445. static ssize_t
  446. show_sas_rphy_device_type(struct class_device *cdev, char *buf)
  447. {
  448. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  449. if (!rphy->identify.device_type)
  450. return snprintf(buf, 20, "none\n");
  451. return get_sas_device_type_names(
  452. rphy->identify.device_type, buf);
  453. }
  454. static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  455. show_sas_rphy_device_type, NULL);
  456. static ssize_t
  457. show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
  458. {
  459. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  460. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  461. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  462. struct sas_internal *i = to_sas_internal(shost->transportt);
  463. u64 identifier;
  464. int error;
  465. /*
  466. * Only devices behind an expander are supported, because the
  467. * enclosure identifier is a SMP feature.
  468. */
  469. if (phy->local_attached)
  470. return -EINVAL;
  471. error = i->f->get_enclosure_identifier(rphy, &identifier);
  472. if (error)
  473. return error;
  474. return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
  475. }
  476. static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
  477. show_sas_rphy_enclosure_identifier, NULL);
  478. static ssize_t
  479. show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
  480. {
  481. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  482. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  483. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  484. struct sas_internal *i = to_sas_internal(shost->transportt);
  485. int val;
  486. if (phy->local_attached)
  487. return -EINVAL;
  488. val = i->f->get_bay_identifier(rphy);
  489. if (val < 0)
  490. return val;
  491. return sprintf(buf, "%d\n", val);
  492. }
  493. static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
  494. show_sas_rphy_bay_identifier, NULL);
  495. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  496. initiator_port_protocols);
  497. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  498. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  499. unsigned long long);
  500. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  501. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  502. "sas_rphy", NULL, NULL, NULL);
  503. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  504. {
  505. struct Scsi_Host *shost;
  506. struct sas_internal *i;
  507. if (!scsi_is_sas_rphy(dev))
  508. return 0;
  509. shost = dev_to_shost(dev->parent->parent);
  510. if (!shost->transportt)
  511. return 0;
  512. if (shost->transportt->host_attrs.ac.class !=
  513. &sas_host_class.class)
  514. return 0;
  515. i = to_sas_internal(shost->transportt);
  516. return &i->rphy_attr_cont.ac == cont;
  517. }
  518. static void sas_rphy_release(struct device *dev)
  519. {
  520. struct sas_rphy *rphy = dev_to_rphy(dev);
  521. put_device(dev->parent);
  522. kfree(rphy);
  523. }
  524. /**
  525. * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure
  526. * @parent: SAS PHY this remote PHY is conneted to
  527. *
  528. * Allocates an SAS remote PHY structure, connected to @parent.
  529. *
  530. * Returns:
  531. * SAS PHY allocated or %NULL if the allocation failed.
  532. */
  533. struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
  534. {
  535. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  536. struct sas_rphy *rphy;
  537. rphy = kzalloc(sizeof(*rphy), GFP_KERNEL);
  538. if (!rphy) {
  539. put_device(&parent->dev);
  540. return NULL;
  541. }
  542. device_initialize(&rphy->dev);
  543. rphy->dev.parent = get_device(&parent->dev);
  544. rphy->dev.release = sas_rphy_release;
  545. sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d",
  546. shost->host_no, parent->port_identifier, parent->number);
  547. transport_setup_device(&rphy->dev);
  548. return rphy;
  549. }
  550. EXPORT_SYMBOL(sas_rphy_alloc);
  551. /**
  552. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  553. * @rphy: The remote PHY to be added
  554. *
  555. * Publishes a SAS remote PHY to the rest of the system.
  556. */
  557. int sas_rphy_add(struct sas_rphy *rphy)
  558. {
  559. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  560. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  561. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  562. struct sas_identify *identify = &rphy->identify;
  563. int error;
  564. if (parent->rphy)
  565. return -ENXIO;
  566. parent->rphy = rphy;
  567. error = device_add(&rphy->dev);
  568. if (error)
  569. return error;
  570. transport_add_device(&rphy->dev);
  571. transport_configure_device(&rphy->dev);
  572. mutex_lock(&sas_host->lock);
  573. list_add_tail(&rphy->list, &sas_host->rphy_list);
  574. if (identify->device_type == SAS_END_DEVICE &&
  575. (identify->target_port_protocols &
  576. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  577. rphy->scsi_target_id = sas_host->next_target_id++;
  578. else
  579. rphy->scsi_target_id = -1;
  580. mutex_unlock(&sas_host->lock);
  581. if (rphy->scsi_target_id != -1) {
  582. scsi_scan_target(&rphy->dev, parent->port_identifier,
  583. rphy->scsi_target_id, ~0, 0);
  584. }
  585. return 0;
  586. }
  587. EXPORT_SYMBOL(sas_rphy_add);
  588. /**
  589. * sas_rphy_free -- free a SAS remote PHY
  590. * @rphy SAS remote PHY to free
  591. *
  592. * Frees the specified SAS remote PHY.
  593. *
  594. * Note:
  595. * This function must only be called on a remote
  596. * PHY that has not sucessfully been added using
  597. * sas_rphy_add().
  598. */
  599. void sas_rphy_free(struct sas_rphy *rphy)
  600. {
  601. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  602. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  603. mutex_lock(&sas_host->lock);
  604. list_del(&rphy->list);
  605. mutex_unlock(&sas_host->lock);
  606. transport_destroy_device(&rphy->dev);
  607. put_device(rphy->dev.parent);
  608. put_device(rphy->dev.parent);
  609. put_device(rphy->dev.parent);
  610. kfree(rphy);
  611. }
  612. EXPORT_SYMBOL(sas_rphy_free);
  613. /**
  614. * sas_rphy_delete -- remove SAS remote PHY
  615. * @rphy: SAS remote PHY to remove
  616. *
  617. * Removes the specified SAS remote PHY.
  618. */
  619. void
  620. sas_rphy_delete(struct sas_rphy *rphy)
  621. {
  622. struct device *dev = &rphy->dev;
  623. struct sas_phy *parent = dev_to_phy(dev->parent);
  624. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  625. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  626. switch (rphy->identify.device_type) {
  627. case SAS_END_DEVICE:
  628. scsi_remove_target(dev);
  629. break;
  630. case SAS_EDGE_EXPANDER_DEVICE:
  631. case SAS_FANOUT_EXPANDER_DEVICE:
  632. device_for_each_child(dev, NULL, do_sas_phy_delete);
  633. break;
  634. default:
  635. break;
  636. }
  637. transport_remove_device(dev);
  638. device_del(dev);
  639. transport_destroy_device(dev);
  640. mutex_lock(&sas_host->lock);
  641. list_del(&rphy->list);
  642. mutex_unlock(&sas_host->lock);
  643. parent->rphy = NULL;
  644. put_device(&parent->dev);
  645. }
  646. EXPORT_SYMBOL(sas_rphy_delete);
  647. /**
  648. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  649. * @dev: device to check
  650. *
  651. * Returns:
  652. * %1 if the device represents a SAS remote PHY, %0 else
  653. */
  654. int scsi_is_sas_rphy(const struct device *dev)
  655. {
  656. return dev->release == sas_rphy_release;
  657. }
  658. EXPORT_SYMBOL(scsi_is_sas_rphy);
  659. /*
  660. * SCSI scan helper
  661. */
  662. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  663. uint id, uint lun)
  664. {
  665. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  666. struct sas_rphy *rphy;
  667. mutex_lock(&sas_host->lock);
  668. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  669. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  670. if (rphy->scsi_target_id == -1)
  671. continue;
  672. if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
  673. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  674. scsi_scan_target(&rphy->dev, parent->port_identifier,
  675. rphy->scsi_target_id, lun, 1);
  676. }
  677. }
  678. mutex_unlock(&sas_host->lock);
  679. return 0;
  680. }
  681. /*
  682. * Setup / Teardown code
  683. */
  684. #define SETUP_RPORT_ATTRIBUTE(field) \
  685. i->private_rphy_attrs[count] = class_device_attr_##field; \
  686. i->private_rphy_attrs[count].attr.mode = S_IRUGO; \
  687. i->private_rphy_attrs[count].store = NULL; \
  688. i->rphy_attrs[count] = &i->private_rphy_attrs[count]; \
  689. count++
  690. #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
  691. i->private_rphy_attrs[count] = class_device_attr_##field; \
  692. i->private_rphy_attrs[count].attr.mode = S_IRUGO; \
  693. i->private_rphy_attrs[count].store = NULL; \
  694. i->rphy_attrs[count] = &i->private_rphy_attrs[count]; \
  695. if (i->f->func) \
  696. count++
  697. #define SETUP_PORT_ATTRIBUTE(field) \
  698. i->private_phy_attrs[count] = class_device_attr_##field; \
  699. i->private_phy_attrs[count].attr.mode = S_IRUGO; \
  700. i->private_phy_attrs[count].store = NULL; \
  701. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  702. count++
  703. #define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func) \
  704. i->private_phy_attrs[count] = class_device_attr_##field; \
  705. i->private_phy_attrs[count].attr.mode = S_IRUGO; \
  706. i->private_phy_attrs[count].store = NULL; \
  707. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  708. if (i->f->func) \
  709. count++
  710. #define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
  711. i->private_phy_attrs[count] = class_device_attr_##field; \
  712. i->private_phy_attrs[count].attr.mode = S_IWUGO; \
  713. i->private_phy_attrs[count].show = NULL; \
  714. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  715. count++
  716. #define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func) \
  717. i->private_phy_attrs[count] = class_device_attr_##field; \
  718. i->private_phy_attrs[count].attr.mode = S_IWUGO; \
  719. i->private_phy_attrs[count].show = NULL; \
  720. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  721. if (i->f->func) \
  722. count++
  723. /**
  724. * sas_attach_transport -- instantiate SAS transport template
  725. * @ft: SAS transport class function template
  726. */
  727. struct scsi_transport_template *
  728. sas_attach_transport(struct sas_function_template *ft)
  729. {
  730. struct sas_internal *i;
  731. int count;
  732. i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
  733. if (!i)
  734. return NULL;
  735. i->t.user_scan = sas_user_scan;
  736. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  737. i->t.host_attrs.ac.class = &sas_host_class.class;
  738. i->t.host_attrs.ac.match = sas_host_match;
  739. transport_container_register(&i->t.host_attrs);
  740. i->t.host_size = sizeof(struct sas_host_attrs);
  741. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  742. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  743. i->phy_attr_cont.ac.match = sas_phy_match;
  744. transport_container_register(&i->phy_attr_cont);
  745. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  746. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  747. i->rphy_attr_cont.ac.match = sas_rphy_match;
  748. transport_container_register(&i->rphy_attr_cont);
  749. i->f = ft;
  750. count = 0;
  751. i->host_attrs[count] = NULL;
  752. count = 0;
  753. SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
  754. SETUP_PORT_ATTRIBUTE(target_port_protocols);
  755. SETUP_PORT_ATTRIBUTE(device_type);
  756. SETUP_PORT_ATTRIBUTE(sas_address);
  757. SETUP_PORT_ATTRIBUTE(phy_identifier);
  758. SETUP_PORT_ATTRIBUTE(port_identifier);
  759. SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
  760. SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
  761. SETUP_PORT_ATTRIBUTE(minimum_linkrate);
  762. SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
  763. SETUP_PORT_ATTRIBUTE(maximum_linkrate);
  764. SETUP_PORT_ATTRIBUTE(invalid_dword_count);
  765. SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
  766. SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
  767. SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
  768. SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset);
  769. SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
  770. i->phy_attrs[count] = NULL;
  771. count = 0;
  772. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  773. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  774. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  775. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  776. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  777. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
  778. get_enclosure_identifier);
  779. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
  780. get_bay_identifier);
  781. i->rphy_attrs[count] = NULL;
  782. return &i->t;
  783. }
  784. EXPORT_SYMBOL(sas_attach_transport);
  785. /**
  786. * sas_release_transport -- release SAS transport template instance
  787. * @t: transport template instance
  788. */
  789. void sas_release_transport(struct scsi_transport_template *t)
  790. {
  791. struct sas_internal *i = to_sas_internal(t);
  792. transport_container_unregister(&i->t.host_attrs);
  793. transport_container_unregister(&i->phy_attr_cont);
  794. transport_container_unregister(&i->rphy_attr_cont);
  795. kfree(i);
  796. }
  797. EXPORT_SYMBOL(sas_release_transport);
  798. static __init int sas_transport_init(void)
  799. {
  800. int error;
  801. error = transport_class_register(&sas_host_class);
  802. if (error)
  803. goto out;
  804. error = transport_class_register(&sas_phy_class);
  805. if (error)
  806. goto out_unregister_transport;
  807. error = transport_class_register(&sas_rphy_class);
  808. if (error)
  809. goto out_unregister_phy;
  810. return 0;
  811. out_unregister_phy:
  812. transport_class_unregister(&sas_phy_class);
  813. out_unregister_transport:
  814. transport_class_unregister(&sas_host_class);
  815. out:
  816. return error;
  817. }
  818. static void __exit sas_transport_exit(void)
  819. {
  820. transport_class_unregister(&sas_host_class);
  821. transport_class_unregister(&sas_phy_class);
  822. transport_class_unregister(&sas_rphy_class);
  823. }
  824. MODULE_AUTHOR("Christoph Hellwig");
  825. MODULE_DESCRIPTION("SAS Transphy Attributes");
  826. MODULE_LICENSE("GPL");
  827. module_init(sas_transport_init);
  828. module_exit(sas_transport_exit);