scsi_transport_sas.c 25 KB

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