scsi_transport_sas.c 24 KB

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