scsi_transport_sas.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 <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 5
  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 = kmalloc(sizeof(*phy), GFP_KERNEL);
  336. if (!phy)
  337. return NULL;
  338. memset(phy, 0, sizeof(*phy));
  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. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  457. initiator_port_protocols);
  458. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  459. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  460. unsigned long long);
  461. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  462. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  463. "sas_rphy", NULL, NULL, NULL);
  464. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  465. {
  466. struct Scsi_Host *shost;
  467. struct sas_internal *i;
  468. if (!scsi_is_sas_rphy(dev))
  469. return 0;
  470. shost = dev_to_shost(dev->parent->parent);
  471. if (!shost->transportt)
  472. return 0;
  473. if (shost->transportt->host_attrs.ac.class !=
  474. &sas_host_class.class)
  475. return 0;
  476. i = to_sas_internal(shost->transportt);
  477. return &i->rphy_attr_cont.ac == cont;
  478. }
  479. static void sas_rphy_release(struct device *dev)
  480. {
  481. struct sas_rphy *rphy = dev_to_rphy(dev);
  482. put_device(dev->parent);
  483. kfree(rphy);
  484. }
  485. /**
  486. * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure
  487. * @parent: SAS PHY this remote PHY is conneted to
  488. *
  489. * Allocates an SAS remote PHY structure, connected to @parent.
  490. *
  491. * Returns:
  492. * SAS PHY allocated or %NULL if the allocation failed.
  493. */
  494. struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
  495. {
  496. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  497. struct sas_rphy *rphy;
  498. rphy = kmalloc(sizeof(*rphy), GFP_KERNEL);
  499. if (!rphy) {
  500. put_device(&parent->dev);
  501. return NULL;
  502. }
  503. memset(rphy, 0, sizeof(*rphy));
  504. device_initialize(&rphy->dev);
  505. rphy->dev.parent = get_device(&parent->dev);
  506. rphy->dev.release = sas_rphy_release;
  507. sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d",
  508. shost->host_no, parent->port_identifier, parent->number);
  509. transport_setup_device(&rphy->dev);
  510. return rphy;
  511. }
  512. EXPORT_SYMBOL(sas_rphy_alloc);
  513. /**
  514. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  515. * @rphy: The remote PHY to be added
  516. *
  517. * Publishes a SAS remote PHY to the rest of the system.
  518. */
  519. int sas_rphy_add(struct sas_rphy *rphy)
  520. {
  521. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  522. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  523. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  524. struct sas_identify *identify = &rphy->identify;
  525. int error;
  526. if (parent->rphy)
  527. return -ENXIO;
  528. parent->rphy = rphy;
  529. error = device_add(&rphy->dev);
  530. if (error)
  531. return error;
  532. transport_add_device(&rphy->dev);
  533. transport_configure_device(&rphy->dev);
  534. mutex_lock(&sas_host->lock);
  535. list_add_tail(&rphy->list, &sas_host->rphy_list);
  536. if (identify->device_type == SAS_END_DEVICE &&
  537. (identify->target_port_protocols &
  538. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  539. rphy->scsi_target_id = sas_host->next_target_id++;
  540. else
  541. rphy->scsi_target_id = -1;
  542. mutex_unlock(&sas_host->lock);
  543. if (rphy->scsi_target_id != -1) {
  544. scsi_scan_target(&rphy->dev, parent->port_identifier,
  545. rphy->scsi_target_id, ~0, 0);
  546. }
  547. return 0;
  548. }
  549. EXPORT_SYMBOL(sas_rphy_add);
  550. /**
  551. * sas_rphy_free -- free a SAS remote PHY
  552. * @rphy SAS remote PHY to free
  553. *
  554. * Frees the specified SAS remote PHY.
  555. *
  556. * Note:
  557. * This function must only be called on a remote
  558. * PHY that has not sucessfully been added using
  559. * sas_rphy_add().
  560. */
  561. void sas_rphy_free(struct sas_rphy *rphy)
  562. {
  563. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  564. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  565. mutex_lock(&sas_host->lock);
  566. list_del(&rphy->list);
  567. mutex_unlock(&sas_host->lock);
  568. transport_destroy_device(&rphy->dev);
  569. put_device(rphy->dev.parent);
  570. put_device(rphy->dev.parent);
  571. put_device(rphy->dev.parent);
  572. kfree(rphy);
  573. }
  574. EXPORT_SYMBOL(sas_rphy_free);
  575. /**
  576. * sas_rphy_delete -- remove SAS remote PHY
  577. * @rphy: SAS remote PHY to remove
  578. *
  579. * Removes the specified SAS remote PHY.
  580. */
  581. void
  582. sas_rphy_delete(struct sas_rphy *rphy)
  583. {
  584. struct device *dev = &rphy->dev;
  585. struct sas_phy *parent = dev_to_phy(dev->parent);
  586. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  587. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  588. switch (rphy->identify.device_type) {
  589. case SAS_END_DEVICE:
  590. scsi_remove_target(dev);
  591. break;
  592. case SAS_EDGE_EXPANDER_DEVICE:
  593. case SAS_FANOUT_EXPANDER_DEVICE:
  594. device_for_each_child(dev, NULL, do_sas_phy_delete);
  595. break;
  596. default:
  597. break;
  598. }
  599. transport_remove_device(dev);
  600. device_del(dev);
  601. transport_destroy_device(dev);
  602. mutex_lock(&sas_host->lock);
  603. list_del(&rphy->list);
  604. mutex_unlock(&sas_host->lock);
  605. parent->rphy = NULL;
  606. put_device(&parent->dev);
  607. }
  608. EXPORT_SYMBOL(sas_rphy_delete);
  609. /**
  610. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  611. * @dev: device to check
  612. *
  613. * Returns:
  614. * %1 if the device represents a SAS remote PHY, %0 else
  615. */
  616. int scsi_is_sas_rphy(const struct device *dev)
  617. {
  618. return dev->release == sas_rphy_release;
  619. }
  620. EXPORT_SYMBOL(scsi_is_sas_rphy);
  621. /*
  622. * SCSI scan helper
  623. */
  624. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  625. uint id, uint lun)
  626. {
  627. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  628. struct sas_rphy *rphy;
  629. mutex_lock(&sas_host->lock);
  630. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  631. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  632. if (rphy->scsi_target_id == -1)
  633. continue;
  634. if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
  635. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  636. scsi_scan_target(&rphy->dev, parent->port_identifier,
  637. rphy->scsi_target_id, lun, 1);
  638. }
  639. }
  640. mutex_unlock(&sas_host->lock);
  641. return 0;
  642. }
  643. /*
  644. * Setup / Teardown code
  645. */
  646. #define SETUP_RPORT_ATTRIBUTE(field) \
  647. i->private_rphy_attrs[count] = class_device_attr_##field; \
  648. i->private_rphy_attrs[count].attr.mode = S_IRUGO; \
  649. i->private_rphy_attrs[count].store = NULL; \
  650. i->rphy_attrs[count] = &i->private_rphy_attrs[count]; \
  651. count++
  652. #define SETUP_PORT_ATTRIBUTE(field) \
  653. i->private_phy_attrs[count] = class_device_attr_##field; \
  654. i->private_phy_attrs[count].attr.mode = S_IRUGO; \
  655. i->private_phy_attrs[count].store = NULL; \
  656. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  657. count++
  658. #define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
  659. i->private_phy_attrs[count] = class_device_attr_##field; \
  660. i->private_phy_attrs[count].attr.mode = S_IWUGO; \
  661. i->private_phy_attrs[count].show = NULL; \
  662. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  663. count++
  664. /**
  665. * sas_attach_transport -- instantiate SAS transport template
  666. * @ft: SAS transport class function template
  667. */
  668. struct scsi_transport_template *
  669. sas_attach_transport(struct sas_function_template *ft)
  670. {
  671. struct sas_internal *i;
  672. int count;
  673. i = kmalloc(sizeof(struct sas_internal), GFP_KERNEL);
  674. if (!i)
  675. return NULL;
  676. memset(i, 0, sizeof(struct sas_internal));
  677. i->t.user_scan = sas_user_scan;
  678. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  679. i->t.host_attrs.ac.class = &sas_host_class.class;
  680. i->t.host_attrs.ac.match = sas_host_match;
  681. transport_container_register(&i->t.host_attrs);
  682. i->t.host_size = sizeof(struct sas_host_attrs);
  683. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  684. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  685. i->phy_attr_cont.ac.match = sas_phy_match;
  686. transport_container_register(&i->phy_attr_cont);
  687. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  688. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  689. i->rphy_attr_cont.ac.match = sas_rphy_match;
  690. transport_container_register(&i->rphy_attr_cont);
  691. i->f = ft;
  692. count = 0;
  693. i->host_attrs[count] = NULL;
  694. count = 0;
  695. SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
  696. SETUP_PORT_ATTRIBUTE(target_port_protocols);
  697. SETUP_PORT_ATTRIBUTE(device_type);
  698. SETUP_PORT_ATTRIBUTE(sas_address);
  699. SETUP_PORT_ATTRIBUTE(phy_identifier);
  700. SETUP_PORT_ATTRIBUTE(port_identifier);
  701. SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
  702. SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
  703. SETUP_PORT_ATTRIBUTE(minimum_linkrate);
  704. SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
  705. SETUP_PORT_ATTRIBUTE(maximum_linkrate);
  706. SETUP_PORT_ATTRIBUTE(invalid_dword_count);
  707. SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
  708. SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
  709. SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
  710. SETUP_PORT_ATTRIBUTE_WRONLY(link_reset);
  711. SETUP_PORT_ATTRIBUTE_WRONLY(hard_reset);
  712. i->phy_attrs[count] = NULL;
  713. count = 0;
  714. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  715. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  716. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  717. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  718. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  719. i->rphy_attrs[count] = NULL;
  720. return &i->t;
  721. }
  722. EXPORT_SYMBOL(sas_attach_transport);
  723. /**
  724. * sas_release_transport -- release SAS transport template instance
  725. * @t: transport template instance
  726. */
  727. void sas_release_transport(struct scsi_transport_template *t)
  728. {
  729. struct sas_internal *i = to_sas_internal(t);
  730. transport_container_unregister(&i->t.host_attrs);
  731. transport_container_unregister(&i->phy_attr_cont);
  732. transport_container_unregister(&i->rphy_attr_cont);
  733. kfree(i);
  734. }
  735. EXPORT_SYMBOL(sas_release_transport);
  736. static __init int sas_transport_init(void)
  737. {
  738. int error;
  739. error = transport_class_register(&sas_host_class);
  740. if (error)
  741. goto out;
  742. error = transport_class_register(&sas_phy_class);
  743. if (error)
  744. goto out_unregister_transport;
  745. error = transport_class_register(&sas_rphy_class);
  746. if (error)
  747. goto out_unregister_phy;
  748. return 0;
  749. out_unregister_phy:
  750. transport_class_unregister(&sas_phy_class);
  751. out_unregister_transport:
  752. transport_class_unregister(&sas_host_class);
  753. out:
  754. return error;
  755. }
  756. static void __exit sas_transport_exit(void)
  757. {
  758. transport_class_unregister(&sas_host_class);
  759. transport_class_unregister(&sas_phy_class);
  760. transport_class_unregister(&sas_rphy_class);
  761. }
  762. MODULE_AUTHOR("Christoph Hellwig");
  763. MODULE_DESCRIPTION("SAS Transphy Attributes");
  764. MODULE_LICENSE("GPL");
  765. module_init(sas_transport_init);
  766. module_exit(sas_transport_exit);