scsi_transport_sas.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_transport.h>
  33. #include <scsi/scsi_transport_sas.h>
  34. #define SAS_HOST_ATTRS 0
  35. #define SAS_PORT_ATTRS 17
  36. #define SAS_RPORT_ATTRS 5
  37. struct sas_internal {
  38. struct scsi_transport_template t;
  39. struct sas_function_template *f;
  40. struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
  41. struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
  42. struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
  43. struct transport_container phy_attr_cont;
  44. struct transport_container rphy_attr_cont;
  45. /*
  46. * The array of null terminated pointers to attributes
  47. * needed by scsi_sysfs.c
  48. */
  49. struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
  50. struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
  51. struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
  52. };
  53. #define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t)
  54. struct sas_host_attrs {
  55. struct list_head rphy_list;
  56. spinlock_t lock;
  57. u32 next_target_id;
  58. };
  59. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  60. /*
  61. * Hack to allow attributes of the same name in different objects.
  62. */
  63. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  64. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  65. __ATTR(_name,_mode,_show,_store)
  66. /*
  67. * Pretty printing helpers
  68. */
  69. #define sas_bitfield_name_match(title, table) \
  70. static ssize_t \
  71. get_sas_##title##_names(u32 table_key, char *buf) \
  72. { \
  73. char *prefix = ""; \
  74. ssize_t len = 0; \
  75. int i; \
  76. \
  77. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  78. if (table[i].value & table_key) { \
  79. len += sprintf(buf + len, "%s%s", \
  80. prefix, table[i].name); \
  81. prefix = ", "; \
  82. } \
  83. } \
  84. len += sprintf(buf + len, "\n"); \
  85. return len; \
  86. }
  87. #define sas_bitfield_name_search(title, table) \
  88. static ssize_t \
  89. get_sas_##title##_names(u32 table_key, char *buf) \
  90. { \
  91. ssize_t len = 0; \
  92. int i; \
  93. \
  94. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  95. if (table[i].value == table_key) { \
  96. len += sprintf(buf + len, "%s", \
  97. table[i].name); \
  98. break; \
  99. } \
  100. } \
  101. len += sprintf(buf + len, "\n"); \
  102. return len; \
  103. }
  104. static struct {
  105. u32 value;
  106. char *name;
  107. } sas_device_type_names[] = {
  108. { SAS_PHY_UNUSED, "unused" },
  109. { SAS_END_DEVICE, "end device" },
  110. { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
  111. { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
  112. };
  113. sas_bitfield_name_search(device_type, sas_device_type_names)
  114. static struct {
  115. u32 value;
  116. char *name;
  117. } sas_protocol_names[] = {
  118. { SAS_PROTOCOL_SATA, "sata" },
  119. { SAS_PROTOCOL_SMP, "smp" },
  120. { SAS_PROTOCOL_STP, "stp" },
  121. { SAS_PROTOCOL_SSP, "ssp" },
  122. };
  123. sas_bitfield_name_match(protocol, sas_protocol_names)
  124. static struct {
  125. u32 value;
  126. char *name;
  127. } sas_linkspeed_names[] = {
  128. { SAS_LINK_RATE_UNKNOWN, "Unknown" },
  129. { SAS_PHY_DISABLED, "Phy disabled" },
  130. { SAS_LINK_RATE_FAILED, "Link Rate failed" },
  131. { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
  132. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  133. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  134. };
  135. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  136. /*
  137. * SAS host attributes
  138. */
  139. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  140. struct class_device *cdev)
  141. {
  142. struct Scsi_Host *shost = dev_to_shost(dev);
  143. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  144. INIT_LIST_HEAD(&sas_host->rphy_list);
  145. spin_lock_init(&sas_host->lock);
  146. sas_host->next_target_id = 0;
  147. return 0;
  148. }
  149. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  150. "sas_host", sas_host_setup, NULL, NULL);
  151. static int sas_host_match(struct attribute_container *cont,
  152. struct device *dev)
  153. {
  154. struct Scsi_Host *shost;
  155. struct sas_internal *i;
  156. if (!scsi_is_host_device(dev))
  157. return 0;
  158. shost = dev_to_shost(dev);
  159. if (!shost->transportt)
  160. return 0;
  161. if (shost->transportt->host_attrs.ac.class !=
  162. &sas_host_class.class)
  163. return 0;
  164. i = to_sas_internal(shost->transportt);
  165. return &i->t.host_attrs.ac == cont;
  166. }
  167. static int do_sas_phy_delete(struct device *dev, void *data)
  168. {
  169. if (scsi_is_sas_phy(dev))
  170. sas_phy_delete(dev_to_phy(dev));
  171. return 0;
  172. }
  173. /**
  174. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  175. * @shost: Scsi Host that is torn down
  176. *
  177. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  178. * Must be called just before scsi_remove_host for SAS HBAs.
  179. */
  180. void sas_remove_host(struct Scsi_Host *shost)
  181. {
  182. device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
  183. }
  184. EXPORT_SYMBOL(sas_remove_host);
  185. /*
  186. * SAS Port attributes
  187. */
  188. #define sas_phy_show_simple(field, name, format_string, cast) \
  189. static ssize_t \
  190. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  191. { \
  192. struct sas_phy *phy = transport_class_to_phy(cdev); \
  193. \
  194. return snprintf(buf, 20, format_string, cast phy->field); \
  195. }
  196. #define sas_phy_simple_attr(field, name, format_string, type) \
  197. sas_phy_show_simple(field, name, format_string, (type)) \
  198. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  199. #define sas_phy_show_protocol(field, name) \
  200. static ssize_t \
  201. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  202. { \
  203. struct sas_phy *phy = transport_class_to_phy(cdev); \
  204. \
  205. if (!phy->field) \
  206. return snprintf(buf, 20, "none\n"); \
  207. return get_sas_protocol_names(phy->field, buf); \
  208. }
  209. #define sas_phy_protocol_attr(field, name) \
  210. sas_phy_show_protocol(field, name) \
  211. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  212. #define sas_phy_show_linkspeed(field) \
  213. static ssize_t \
  214. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  215. { \
  216. struct sas_phy *phy = transport_class_to_phy(cdev); \
  217. \
  218. return get_sas_linkspeed_names(phy->field, buf); \
  219. }
  220. #define sas_phy_linkspeed_attr(field) \
  221. sas_phy_show_linkspeed(field) \
  222. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  223. #define sas_phy_show_linkerror(field) \
  224. static ssize_t \
  225. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  226. { \
  227. struct sas_phy *phy = transport_class_to_phy(cdev); \
  228. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  229. struct sas_internal *i = to_sas_internal(shost->transportt); \
  230. int error; \
  231. \
  232. if (!phy->local_attached) \
  233. return -EINVAL; \
  234. \
  235. error = i->f->get_linkerrors(phy); \
  236. if (error) \
  237. return error; \
  238. return snprintf(buf, 20, "%u\n", phy->field); \
  239. }
  240. #define sas_phy_linkerror_attr(field) \
  241. sas_phy_show_linkerror(field) \
  242. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  243. static ssize_t
  244. show_sas_device_type(struct class_device *cdev, char *buf)
  245. {
  246. struct sas_phy *phy = transport_class_to_phy(cdev);
  247. if (!phy->identify.device_type)
  248. return snprintf(buf, 20, "none\n");
  249. return get_sas_device_type_names(phy->identify.device_type, buf);
  250. }
  251. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  252. static ssize_t do_sas_phy_reset(struct class_device *cdev,
  253. size_t count, int hard_reset)
  254. {
  255. struct sas_phy *phy = transport_class_to_phy(cdev);
  256. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  257. struct sas_internal *i = to_sas_internal(shost->transportt);
  258. int error;
  259. if (!phy->local_attached)
  260. return -EINVAL;
  261. error = i->f->phy_reset(phy, hard_reset);
  262. if (error)
  263. return error;
  264. return count;
  265. };
  266. static ssize_t store_sas_link_reset(struct class_device *cdev,
  267. const char *buf, size_t count)
  268. {
  269. return do_sas_phy_reset(cdev, count, 0);
  270. }
  271. static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  272. static ssize_t store_sas_hard_reset(struct class_device *cdev,
  273. const char *buf, size_t count)
  274. {
  275. return do_sas_phy_reset(cdev, count, 1);
  276. }
  277. static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  278. sas_phy_protocol_attr(identify.initiator_port_protocols,
  279. initiator_port_protocols);
  280. sas_phy_protocol_attr(identify.target_port_protocols,
  281. target_port_protocols);
  282. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  283. unsigned long long);
  284. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  285. sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
  286. sas_phy_linkspeed_attr(negotiated_linkrate);
  287. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  288. sas_phy_linkspeed_attr(minimum_linkrate);
  289. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  290. sas_phy_linkspeed_attr(maximum_linkrate);
  291. sas_phy_linkerror_attr(invalid_dword_count);
  292. sas_phy_linkerror_attr(running_disparity_error_count);
  293. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  294. sas_phy_linkerror_attr(phy_reset_problem_count);
  295. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  296. "sas_phy", NULL, NULL, NULL);
  297. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  298. {
  299. struct Scsi_Host *shost;
  300. struct sas_internal *i;
  301. if (!scsi_is_sas_phy(dev))
  302. return 0;
  303. shost = dev_to_shost(dev->parent);
  304. if (!shost->transportt)
  305. return 0;
  306. if (shost->transportt->host_attrs.ac.class !=
  307. &sas_host_class.class)
  308. return 0;
  309. i = to_sas_internal(shost->transportt);
  310. return &i->phy_attr_cont.ac == cont;
  311. }
  312. static void sas_phy_release(struct device *dev)
  313. {
  314. struct sas_phy *phy = dev_to_phy(dev);
  315. put_device(dev->parent);
  316. kfree(phy);
  317. }
  318. /**
  319. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  320. * @parent: Parent device
  321. * @number: Port number
  322. *
  323. * Allocates an SAS PHY structure. It will be added in the device tree
  324. * below the device specified by @parent, which has to be either a Scsi_Host
  325. * or sas_rphy.
  326. *
  327. * Returns:
  328. * SAS PHY allocated or %NULL if the allocation failed.
  329. */
  330. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  331. {
  332. struct Scsi_Host *shost = dev_to_shost(parent);
  333. struct sas_phy *phy;
  334. phy = kmalloc(sizeof(*phy), GFP_KERNEL);
  335. if (!phy)
  336. return NULL;
  337. memset(phy, 0, sizeof(*phy));
  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. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  456. initiator_port_protocols);
  457. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  458. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  459. unsigned long long);
  460. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  461. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  462. "sas_rphy", NULL, NULL, NULL);
  463. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  464. {
  465. struct Scsi_Host *shost;
  466. struct sas_internal *i;
  467. if (!scsi_is_sas_rphy(dev))
  468. return 0;
  469. shost = dev_to_shost(dev->parent->parent);
  470. if (!shost->transportt)
  471. return 0;
  472. if (shost->transportt->host_attrs.ac.class !=
  473. &sas_host_class.class)
  474. return 0;
  475. i = to_sas_internal(shost->transportt);
  476. return &i->rphy_attr_cont.ac == cont;
  477. }
  478. static void sas_rphy_release(struct device *dev)
  479. {
  480. struct sas_rphy *rphy = dev_to_rphy(dev);
  481. put_device(dev->parent);
  482. kfree(rphy);
  483. }
  484. /**
  485. * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure
  486. * @parent: SAS PHY this remote PHY is conneted to
  487. *
  488. * Allocates an SAS remote PHY structure, connected to @parent.
  489. *
  490. * Returns:
  491. * SAS PHY allocated or %NULL if the allocation failed.
  492. */
  493. struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
  494. {
  495. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  496. struct sas_rphy *rphy;
  497. rphy = kmalloc(sizeof(*rphy), GFP_KERNEL);
  498. if (!rphy) {
  499. put_device(&parent->dev);
  500. return NULL;
  501. }
  502. memset(rphy, 0, sizeof(*rphy));
  503. device_initialize(&rphy->dev);
  504. rphy->dev.parent = get_device(&parent->dev);
  505. rphy->dev.release = sas_rphy_release;
  506. sprintf(rphy->dev.bus_id, "rphy-%d:%d",
  507. shost->host_no, parent->number);
  508. transport_setup_device(&rphy->dev);
  509. return rphy;
  510. }
  511. EXPORT_SYMBOL(sas_rphy_alloc);
  512. /**
  513. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  514. * @rphy: The remote PHY to be added
  515. *
  516. * Publishes a SAS remote PHY to the rest of the system.
  517. */
  518. int sas_rphy_add(struct sas_rphy *rphy)
  519. {
  520. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  521. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  522. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  523. struct sas_identify *identify = &rphy->identify;
  524. int error;
  525. if (parent->rphy)
  526. return -ENXIO;
  527. parent->rphy = rphy;
  528. error = device_add(&rphy->dev);
  529. if (error)
  530. return error;
  531. transport_add_device(&rphy->dev);
  532. transport_configure_device(&rphy->dev);
  533. spin_lock(&sas_host->lock);
  534. list_add_tail(&rphy->list, &sas_host->rphy_list);
  535. if (identify->device_type == SAS_END_DEVICE &&
  536. (identify->target_port_protocols &
  537. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  538. rphy->scsi_target_id = sas_host->next_target_id++;
  539. else
  540. rphy->scsi_target_id = -1;
  541. spin_unlock(&sas_host->lock);
  542. if (rphy->scsi_target_id != -1) {
  543. scsi_scan_target(&rphy->dev, parent->number,
  544. rphy->scsi_target_id, ~0, 0);
  545. }
  546. return 0;
  547. }
  548. EXPORT_SYMBOL(sas_rphy_add);
  549. /**
  550. * sas_rphy_free -- free a SAS remote PHY
  551. * @rphy SAS remote PHY to free
  552. *
  553. * Frees the specified SAS remote PHY.
  554. *
  555. * Note:
  556. * This function must only be called on a remote
  557. * PHY that has not sucessfully been added using
  558. * sas_rphy_add().
  559. */
  560. void sas_rphy_free(struct sas_rphy *rphy)
  561. {
  562. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  563. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  564. spin_lock(&sas_host->lock);
  565. list_del(&rphy->list);
  566. spin_unlock(&sas_host->lock);
  567. transport_destroy_device(&rphy->dev);
  568. put_device(rphy->dev.parent);
  569. put_device(rphy->dev.parent);
  570. put_device(rphy->dev.parent);
  571. kfree(rphy);
  572. }
  573. EXPORT_SYMBOL(sas_rphy_free);
  574. /**
  575. * sas_rphy_delete -- remove SAS remote PHY
  576. * @rphy: SAS remote PHY to remove
  577. *
  578. * Removes the specified SAS remote PHY.
  579. */
  580. void
  581. sas_rphy_delete(struct sas_rphy *rphy)
  582. {
  583. struct device *dev = &rphy->dev;
  584. struct sas_phy *parent = dev_to_phy(dev->parent);
  585. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  586. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  587. scsi_remove_target(dev);
  588. transport_remove_device(dev);
  589. device_del(dev);
  590. transport_destroy_device(dev);
  591. spin_lock(&sas_host->lock);
  592. list_del(&rphy->list);
  593. spin_unlock(&sas_host->lock);
  594. put_device(&parent->dev);
  595. }
  596. EXPORT_SYMBOL(sas_rphy_delete);
  597. /**
  598. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  599. * @dev: device to check
  600. *
  601. * Returns:
  602. * %1 if the device represents a SAS remote PHY, %0 else
  603. */
  604. int scsi_is_sas_rphy(const struct device *dev)
  605. {
  606. return dev->release == sas_rphy_release;
  607. }
  608. EXPORT_SYMBOL(scsi_is_sas_rphy);
  609. /*
  610. * SCSI scan helper
  611. */
  612. static struct device *sas_target_parent(struct Scsi_Host *shost,
  613. int channel, uint id)
  614. {
  615. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  616. struct sas_rphy *rphy;
  617. struct device *dev = NULL;
  618. spin_lock(&sas_host->lock);
  619. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  620. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  621. if (parent->number == channel &&
  622. rphy->scsi_target_id == id)
  623. dev = &rphy->dev;
  624. }
  625. spin_unlock(&sas_host->lock);
  626. return dev;
  627. }
  628. /*
  629. * Setup / Teardown code
  630. */
  631. #define SETUP_RPORT_ATTRIBUTE(field) \
  632. i->private_rphy_attrs[count] = class_device_attr_##field; \
  633. i->private_rphy_attrs[count].attr.mode = S_IRUGO; \
  634. i->private_rphy_attrs[count].store = NULL; \
  635. i->rphy_attrs[count] = &i->private_rphy_attrs[count]; \
  636. count++
  637. #define SETUP_PORT_ATTRIBUTE(field) \
  638. i->private_phy_attrs[count] = class_device_attr_##field; \
  639. i->private_phy_attrs[count].attr.mode = S_IRUGO; \
  640. i->private_phy_attrs[count].store = NULL; \
  641. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  642. count++
  643. #define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
  644. i->private_phy_attrs[count] = class_device_attr_##field; \
  645. i->private_phy_attrs[count].attr.mode = S_IWUGO; \
  646. i->private_phy_attrs[count].show = NULL; \
  647. i->phy_attrs[count] = &i->private_phy_attrs[count]; \
  648. count++
  649. /**
  650. * sas_attach_transport -- instantiate SAS transport template
  651. * @ft: SAS transport class function template
  652. */
  653. struct scsi_transport_template *
  654. sas_attach_transport(struct sas_function_template *ft)
  655. {
  656. struct sas_internal *i;
  657. int count;
  658. i = kmalloc(sizeof(struct sas_internal), GFP_KERNEL);
  659. if (!i)
  660. return NULL;
  661. memset(i, 0, sizeof(struct sas_internal));
  662. i->t.target_parent = sas_target_parent;
  663. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  664. i->t.host_attrs.ac.class = &sas_host_class.class;
  665. i->t.host_attrs.ac.match = sas_host_match;
  666. transport_container_register(&i->t.host_attrs);
  667. i->t.host_size = sizeof(struct sas_host_attrs);
  668. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  669. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  670. i->phy_attr_cont.ac.match = sas_phy_match;
  671. transport_container_register(&i->phy_attr_cont);
  672. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  673. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  674. i->rphy_attr_cont.ac.match = sas_rphy_match;
  675. transport_container_register(&i->rphy_attr_cont);
  676. i->f = ft;
  677. count = 0;
  678. i->host_attrs[count] = NULL;
  679. count = 0;
  680. SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
  681. SETUP_PORT_ATTRIBUTE(target_port_protocols);
  682. SETUP_PORT_ATTRIBUTE(device_type);
  683. SETUP_PORT_ATTRIBUTE(sas_address);
  684. SETUP_PORT_ATTRIBUTE(phy_identifier);
  685. SETUP_PORT_ATTRIBUTE(port_identifier);
  686. SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
  687. SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
  688. SETUP_PORT_ATTRIBUTE(minimum_linkrate);
  689. SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
  690. SETUP_PORT_ATTRIBUTE(maximum_linkrate);
  691. SETUP_PORT_ATTRIBUTE(invalid_dword_count);
  692. SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
  693. SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
  694. SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
  695. SETUP_PORT_ATTRIBUTE_WRONLY(link_reset);
  696. SETUP_PORT_ATTRIBUTE_WRONLY(hard_reset);
  697. i->phy_attrs[count] = NULL;
  698. count = 0;
  699. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  700. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  701. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  702. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  703. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  704. i->rphy_attrs[count] = NULL;
  705. return &i->t;
  706. }
  707. EXPORT_SYMBOL(sas_attach_transport);
  708. /**
  709. * sas_release_transport -- release SAS transport template instance
  710. * @t: transport template instance
  711. */
  712. void sas_release_transport(struct scsi_transport_template *t)
  713. {
  714. struct sas_internal *i = to_sas_internal(t);
  715. transport_container_unregister(&i->t.host_attrs);
  716. transport_container_unregister(&i->phy_attr_cont);
  717. transport_container_unregister(&i->rphy_attr_cont);
  718. kfree(i);
  719. }
  720. EXPORT_SYMBOL(sas_release_transport);
  721. static __init int sas_transport_init(void)
  722. {
  723. int error;
  724. error = transport_class_register(&sas_host_class);
  725. if (error)
  726. goto out;
  727. error = transport_class_register(&sas_phy_class);
  728. if (error)
  729. goto out_unregister_transport;
  730. error = transport_class_register(&sas_rphy_class);
  731. if (error)
  732. goto out_unregister_phy;
  733. return 0;
  734. out_unregister_phy:
  735. transport_class_unregister(&sas_phy_class);
  736. out_unregister_transport:
  737. transport_class_unregister(&sas_host_class);
  738. out:
  739. return error;
  740. }
  741. static void __exit sas_transport_exit(void)
  742. {
  743. transport_class_unregister(&sas_host_class);
  744. transport_class_unregister(&sas_phy_class);
  745. transport_class_unregister(&sas_rphy_class);
  746. }
  747. MODULE_AUTHOR("Christoph Hellwig");
  748. MODULE_DESCRIPTION("SAS Transphy Attributes");
  749. MODULE_LICENSE("GPL");
  750. module_init(sas_transport_init);
  751. module_exit(sas_transport_exit);