scsi_transport_sas.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * Copyright (C) 2005-2006 Dell Inc.
  3. * Released under GPL v2.
  4. *
  5. * Serial Attached SCSI (SAS) transport class.
  6. *
  7. * The SAS transport class contains common code to deal with SAS HBAs,
  8. * an aproximated representation of SAS topologies in the driver model,
  9. * and various sysfs attributes to expose these topologies and managment
  10. * interfaces to userspace.
  11. *
  12. * In addition to the basic SCSI core objects this transport class
  13. * introduces two additional intermediate objects: The SAS PHY
  14. * as represented by struct sas_phy defines an "outgoing" PHY on
  15. * a SAS HBA or Expander, and the SAS remote PHY represented by
  16. * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
  17. * end device. Note that this is purely a software concept, the
  18. * underlying hardware for a PHY and a remote PHY is the exactly
  19. * the same.
  20. *
  21. * There is no concept of a SAS port in this code, users can see
  22. * what PHYs form a wide port based on the port_identifier attribute,
  23. * which is the same for all PHYs in a port.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/err.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. #include "scsi_sas_internal.h"
  36. struct sas_host_attrs {
  37. struct list_head rphy_list;
  38. struct mutex lock;
  39. u32 next_target_id;
  40. u32 next_expander_id;
  41. };
  42. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  43. /*
  44. * Hack to allow attributes of the same name in different objects.
  45. */
  46. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  47. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  48. __ATTR(_name,_mode,_show,_store)
  49. /*
  50. * Pretty printing helpers
  51. */
  52. #define sas_bitfield_name_match(title, table) \
  53. static ssize_t \
  54. get_sas_##title##_names(u32 table_key, char *buf) \
  55. { \
  56. char *prefix = ""; \
  57. ssize_t len = 0; \
  58. int i; \
  59. \
  60. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  61. if (table[i].value & table_key) { \
  62. len += sprintf(buf + len, "%s%s", \
  63. prefix, table[i].name); \
  64. prefix = ", "; \
  65. } \
  66. } \
  67. len += sprintf(buf + len, "\n"); \
  68. return len; \
  69. }
  70. #define sas_bitfield_name_search(title, table) \
  71. static ssize_t \
  72. get_sas_##title##_names(u32 table_key, char *buf) \
  73. { \
  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", \
  80. table[i].name); \
  81. break; \
  82. } \
  83. } \
  84. len += sprintf(buf + len, "\n"); \
  85. return len; \
  86. }
  87. static struct {
  88. u32 value;
  89. char *name;
  90. } sas_device_type_names[] = {
  91. { SAS_PHY_UNUSED, "unused" },
  92. { SAS_END_DEVICE, "end device" },
  93. { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
  94. { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
  95. };
  96. sas_bitfield_name_search(device_type, sas_device_type_names)
  97. static struct {
  98. u32 value;
  99. char *name;
  100. } sas_protocol_names[] = {
  101. { SAS_PROTOCOL_SATA, "sata" },
  102. { SAS_PROTOCOL_SMP, "smp" },
  103. { SAS_PROTOCOL_STP, "stp" },
  104. { SAS_PROTOCOL_SSP, "ssp" },
  105. };
  106. sas_bitfield_name_match(protocol, sas_protocol_names)
  107. static struct {
  108. u32 value;
  109. char *name;
  110. } sas_linkspeed_names[] = {
  111. { SAS_LINK_RATE_UNKNOWN, "Unknown" },
  112. { SAS_PHY_DISABLED, "Phy disabled" },
  113. { SAS_LINK_RATE_FAILED, "Link Rate failed" },
  114. { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
  115. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  116. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  117. { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
  118. };
  119. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  120. /*
  121. * SAS host attributes
  122. */
  123. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  124. struct class_device *cdev)
  125. {
  126. struct Scsi_Host *shost = dev_to_shost(dev);
  127. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  128. INIT_LIST_HEAD(&sas_host->rphy_list);
  129. mutex_init(&sas_host->lock);
  130. sas_host->next_target_id = 0;
  131. sas_host->next_expander_id = 0;
  132. return 0;
  133. }
  134. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  135. "sas_host", sas_host_setup, NULL, NULL);
  136. static int sas_host_match(struct attribute_container *cont,
  137. struct device *dev)
  138. {
  139. struct Scsi_Host *shost;
  140. struct sas_internal *i;
  141. if (!scsi_is_host_device(dev))
  142. return 0;
  143. shost = dev_to_shost(dev);
  144. if (!shost->transportt)
  145. return 0;
  146. if (shost->transportt->host_attrs.ac.class !=
  147. &sas_host_class.class)
  148. return 0;
  149. i = to_sas_internal(shost->transportt);
  150. return &i->t.host_attrs.ac == cont;
  151. }
  152. static int do_sas_phy_delete(struct device *dev, void *data)
  153. {
  154. if (scsi_is_sas_phy(dev))
  155. sas_phy_delete(dev_to_phy(dev));
  156. return 0;
  157. }
  158. /**
  159. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  160. * @shost: Scsi Host that is torn down
  161. *
  162. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  163. * Must be called just before scsi_remove_host for SAS HBAs.
  164. */
  165. void sas_remove_host(struct Scsi_Host *shost)
  166. {
  167. device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
  168. }
  169. EXPORT_SYMBOL(sas_remove_host);
  170. /*
  171. * SAS Port attributes
  172. */
  173. #define sas_phy_show_simple(field, name, format_string, cast) \
  174. static ssize_t \
  175. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  176. { \
  177. struct sas_phy *phy = transport_class_to_phy(cdev); \
  178. \
  179. return snprintf(buf, 20, format_string, cast phy->field); \
  180. }
  181. #define sas_phy_simple_attr(field, name, format_string, type) \
  182. sas_phy_show_simple(field, name, format_string, (type)) \
  183. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  184. #define sas_phy_show_protocol(field, name) \
  185. static ssize_t \
  186. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  187. { \
  188. struct sas_phy *phy = transport_class_to_phy(cdev); \
  189. \
  190. if (!phy->field) \
  191. return snprintf(buf, 20, "none\n"); \
  192. return get_sas_protocol_names(phy->field, buf); \
  193. }
  194. #define sas_phy_protocol_attr(field, name) \
  195. sas_phy_show_protocol(field, name) \
  196. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  197. #define sas_phy_show_linkspeed(field) \
  198. static ssize_t \
  199. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  200. { \
  201. struct sas_phy *phy = transport_class_to_phy(cdev); \
  202. \
  203. return get_sas_linkspeed_names(phy->field, buf); \
  204. }
  205. #define sas_phy_linkspeed_attr(field) \
  206. sas_phy_show_linkspeed(field) \
  207. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  208. #define sas_phy_show_linkerror(field) \
  209. static ssize_t \
  210. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  211. { \
  212. struct sas_phy *phy = transport_class_to_phy(cdev); \
  213. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  214. struct sas_internal *i = to_sas_internal(shost->transportt); \
  215. int error; \
  216. \
  217. if (!phy->local_attached) \
  218. return -EINVAL; \
  219. \
  220. error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
  221. if (error) \
  222. return error; \
  223. return snprintf(buf, 20, "%u\n", phy->field); \
  224. }
  225. #define sas_phy_linkerror_attr(field) \
  226. sas_phy_show_linkerror(field) \
  227. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  228. static ssize_t
  229. show_sas_device_type(struct class_device *cdev, char *buf)
  230. {
  231. struct sas_phy *phy = transport_class_to_phy(cdev);
  232. if (!phy->identify.device_type)
  233. return snprintf(buf, 20, "none\n");
  234. return get_sas_device_type_names(phy->identify.device_type, buf);
  235. }
  236. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  237. static ssize_t do_sas_phy_reset(struct class_device *cdev,
  238. size_t count, int hard_reset)
  239. {
  240. struct sas_phy *phy = transport_class_to_phy(cdev);
  241. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  242. struct sas_internal *i = to_sas_internal(shost->transportt);
  243. int error;
  244. if (!phy->local_attached)
  245. return -EINVAL;
  246. error = i->f->phy_reset(phy, hard_reset);
  247. if (error)
  248. return error;
  249. return count;
  250. };
  251. static ssize_t store_sas_link_reset(struct class_device *cdev,
  252. const char *buf, size_t count)
  253. {
  254. return do_sas_phy_reset(cdev, count, 0);
  255. }
  256. static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  257. static ssize_t store_sas_hard_reset(struct class_device *cdev,
  258. const char *buf, size_t count)
  259. {
  260. return do_sas_phy_reset(cdev, count, 1);
  261. }
  262. static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  263. sas_phy_protocol_attr(identify.initiator_port_protocols,
  264. initiator_port_protocols);
  265. sas_phy_protocol_attr(identify.target_port_protocols,
  266. target_port_protocols);
  267. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  268. unsigned long long);
  269. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  270. sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
  271. sas_phy_linkspeed_attr(negotiated_linkrate);
  272. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  273. sas_phy_linkspeed_attr(minimum_linkrate);
  274. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  275. sas_phy_linkspeed_attr(maximum_linkrate);
  276. sas_phy_linkerror_attr(invalid_dword_count);
  277. sas_phy_linkerror_attr(running_disparity_error_count);
  278. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  279. sas_phy_linkerror_attr(phy_reset_problem_count);
  280. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  281. "sas_phy", NULL, NULL, NULL);
  282. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  283. {
  284. struct Scsi_Host *shost;
  285. struct sas_internal *i;
  286. if (!scsi_is_sas_phy(dev))
  287. return 0;
  288. shost = dev_to_shost(dev->parent);
  289. if (!shost->transportt)
  290. return 0;
  291. if (shost->transportt->host_attrs.ac.class !=
  292. &sas_host_class.class)
  293. return 0;
  294. i = to_sas_internal(shost->transportt);
  295. return &i->phy_attr_cont.ac == cont;
  296. }
  297. static void sas_phy_release(struct device *dev)
  298. {
  299. struct sas_phy *phy = dev_to_phy(dev);
  300. put_device(dev->parent);
  301. kfree(phy);
  302. }
  303. /**
  304. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  305. * @parent: Parent device
  306. * @number: Phy index
  307. *
  308. * Allocates an SAS PHY structure. It will be added in the device tree
  309. * below the device specified by @parent, which has to be either a Scsi_Host
  310. * or sas_rphy.
  311. *
  312. * Returns:
  313. * SAS PHY allocated or %NULL if the allocation failed.
  314. */
  315. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  316. {
  317. struct Scsi_Host *shost = dev_to_shost(parent);
  318. struct sas_phy *phy;
  319. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  320. if (!phy)
  321. return NULL;
  322. phy->number = number;
  323. device_initialize(&phy->dev);
  324. phy->dev.parent = get_device(parent);
  325. phy->dev.release = sas_phy_release;
  326. if (scsi_is_sas_expander_device(parent)) {
  327. struct sas_rphy *rphy = dev_to_rphy(parent);
  328. sprintf(phy->dev.bus_id, "phy-%d-%d:%d", shost->host_no,
  329. rphy->scsi_target_id, number);
  330. } else
  331. sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
  332. transport_setup_device(&phy->dev);
  333. return phy;
  334. }
  335. EXPORT_SYMBOL(sas_phy_alloc);
  336. /**
  337. * sas_phy_add -- add a SAS PHY to the device hierachy
  338. * @phy: The PHY to be added
  339. *
  340. * Publishes a SAS PHY to the rest of the system.
  341. */
  342. int sas_phy_add(struct sas_phy *phy)
  343. {
  344. int error;
  345. error = device_add(&phy->dev);
  346. if (!error) {
  347. transport_add_device(&phy->dev);
  348. transport_configure_device(&phy->dev);
  349. }
  350. return error;
  351. }
  352. EXPORT_SYMBOL(sas_phy_add);
  353. /**
  354. * sas_phy_free -- free a SAS PHY
  355. * @phy: SAS PHY to free
  356. *
  357. * Frees the specified SAS PHY.
  358. *
  359. * Note:
  360. * This function must only be called on a PHY that has not
  361. * sucessfully been added using sas_phy_add().
  362. */
  363. void sas_phy_free(struct sas_phy *phy)
  364. {
  365. transport_destroy_device(&phy->dev);
  366. put_device(&phy->dev);
  367. }
  368. EXPORT_SYMBOL(sas_phy_free);
  369. /**
  370. * sas_phy_delete -- remove SAS PHY
  371. * @phy: SAS PHY to remove
  372. *
  373. * Removes the specified SAS PHY. If the SAS PHY has an
  374. * associated remote PHY it is removed before.
  375. */
  376. void
  377. sas_phy_delete(struct sas_phy *phy)
  378. {
  379. struct device *dev = &phy->dev;
  380. if (phy->rphy)
  381. sas_rphy_delete(phy->rphy);
  382. transport_remove_device(dev);
  383. device_del(dev);
  384. transport_destroy_device(dev);
  385. put_device(dev);
  386. }
  387. EXPORT_SYMBOL(sas_phy_delete);
  388. /**
  389. * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
  390. * @dev: device to check
  391. *
  392. * Returns:
  393. * %1 if the device represents a SAS PHY, %0 else
  394. */
  395. int scsi_is_sas_phy(const struct device *dev)
  396. {
  397. return dev->release == sas_phy_release;
  398. }
  399. EXPORT_SYMBOL(scsi_is_sas_phy);
  400. /*
  401. * SAS remote PHY attributes.
  402. */
  403. #define sas_rphy_show_simple(field, name, format_string, cast) \
  404. static ssize_t \
  405. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  406. { \
  407. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  408. \
  409. return snprintf(buf, 20, format_string, cast rphy->field); \
  410. }
  411. #define sas_rphy_simple_attr(field, name, format_string, type) \
  412. sas_rphy_show_simple(field, name, format_string, (type)) \
  413. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  414. show_sas_rphy_##name, NULL)
  415. #define sas_rphy_show_protocol(field, name) \
  416. static ssize_t \
  417. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  418. { \
  419. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  420. \
  421. if (!rphy->field) \
  422. return snprintf(buf, 20, "none\n"); \
  423. return get_sas_protocol_names(rphy->field, buf); \
  424. }
  425. #define sas_rphy_protocol_attr(field, name) \
  426. sas_rphy_show_protocol(field, name) \
  427. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  428. show_sas_rphy_##name, NULL)
  429. static ssize_t
  430. show_sas_rphy_device_type(struct class_device *cdev, char *buf)
  431. {
  432. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  433. if (!rphy->identify.device_type)
  434. return snprintf(buf, 20, "none\n");
  435. return get_sas_device_type_names(
  436. rphy->identify.device_type, buf);
  437. }
  438. static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  439. show_sas_rphy_device_type, NULL);
  440. static ssize_t
  441. show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
  442. {
  443. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  444. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  445. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  446. struct sas_internal *i = to_sas_internal(shost->transportt);
  447. u64 identifier;
  448. int error;
  449. /*
  450. * Only devices behind an expander are supported, because the
  451. * enclosure identifier is a SMP feature.
  452. */
  453. if (phy->local_attached)
  454. return -EINVAL;
  455. error = i->f->get_enclosure_identifier(rphy, &identifier);
  456. if (error)
  457. return error;
  458. return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
  459. }
  460. static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
  461. show_sas_rphy_enclosure_identifier, NULL);
  462. static ssize_t
  463. show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
  464. {
  465. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  466. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  467. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  468. struct sas_internal *i = to_sas_internal(shost->transportt);
  469. int val;
  470. if (phy->local_attached)
  471. return -EINVAL;
  472. val = i->f->get_bay_identifier(rphy);
  473. if (val < 0)
  474. return val;
  475. return sprintf(buf, "%d\n", val);
  476. }
  477. static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
  478. show_sas_rphy_bay_identifier, NULL);
  479. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  480. initiator_port_protocols);
  481. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  482. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  483. unsigned long long);
  484. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  485. /* only need 8 bytes of data plus header (4 or 8) */
  486. #define BUF_SIZE 64
  487. int sas_read_port_mode_page(struct scsi_device *sdev)
  488. {
  489. char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
  490. struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
  491. struct sas_end_device *rdev;
  492. struct scsi_mode_data mode_data;
  493. int res, error;
  494. BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
  495. rdev = rphy_to_end_device(rphy);
  496. if (!buffer)
  497. return -ENOMEM;
  498. res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
  499. &mode_data, NULL);
  500. error = -EINVAL;
  501. if (!scsi_status_is_good(res))
  502. goto out;
  503. msdata = buffer + mode_data.header_length +
  504. mode_data.block_descriptor_length;
  505. if (msdata - buffer > BUF_SIZE - 8)
  506. goto out;
  507. error = 0;
  508. rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
  509. rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
  510. rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
  511. out:
  512. kfree(buffer);
  513. return error;
  514. }
  515. EXPORT_SYMBOL(sas_read_port_mode_page);
  516. static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
  517. "sas_end_device", NULL, NULL, NULL);
  518. #define sas_end_dev_show_simple(field, name, format_string, cast) \
  519. static ssize_t \
  520. show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
  521. { \
  522. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  523. struct sas_end_device *rdev = rphy_to_end_device(rphy); \
  524. \
  525. return snprintf(buf, 20, format_string, cast rdev->field); \
  526. }
  527. #define sas_end_dev_simple_attr(field, name, format_string, type) \
  528. sas_end_dev_show_simple(field, name, format_string, (type)) \
  529. static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
  530. show_sas_end_dev_##name, NULL)
  531. sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
  532. sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
  533. "%d\n", int);
  534. sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
  535. "%d\n", int);
  536. static DECLARE_TRANSPORT_CLASS(sas_expander_class,
  537. "sas_expander", NULL, NULL, NULL);
  538. #define sas_expander_show_simple(field, name, format_string, cast) \
  539. static ssize_t \
  540. show_sas_expander_##name(struct class_device *cdev, char *buf) \
  541. { \
  542. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  543. struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
  544. \
  545. return snprintf(buf, 20, format_string, cast edev->field); \
  546. }
  547. #define sas_expander_simple_attr(field, name, format_string, type) \
  548. sas_expander_show_simple(field, name, format_string, (type)) \
  549. static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
  550. show_sas_expander_##name, NULL)
  551. sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
  552. sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
  553. sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
  554. sas_expander_simple_attr(component_vendor_id, component_vendor_id,
  555. "%s\n", char *);
  556. sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
  557. sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
  558. unsigned int);
  559. sas_expander_simple_attr(level, level, "%d\n", int);
  560. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  561. "sas_device", NULL, NULL, NULL);
  562. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  563. {
  564. struct Scsi_Host *shost;
  565. struct sas_internal *i;
  566. if (!scsi_is_sas_rphy(dev))
  567. return 0;
  568. shost = dev_to_shost(dev->parent->parent);
  569. if (!shost->transportt)
  570. return 0;
  571. if (shost->transportt->host_attrs.ac.class !=
  572. &sas_host_class.class)
  573. return 0;
  574. i = to_sas_internal(shost->transportt);
  575. return &i->rphy_attr_cont.ac == cont;
  576. }
  577. static int sas_end_dev_match(struct attribute_container *cont,
  578. struct device *dev)
  579. {
  580. struct Scsi_Host *shost;
  581. struct sas_internal *i;
  582. struct sas_rphy *rphy;
  583. if (!scsi_is_sas_rphy(dev))
  584. return 0;
  585. shost = dev_to_shost(dev->parent->parent);
  586. rphy = dev_to_rphy(dev);
  587. if (!shost->transportt)
  588. return 0;
  589. if (shost->transportt->host_attrs.ac.class !=
  590. &sas_host_class.class)
  591. return 0;
  592. i = to_sas_internal(shost->transportt);
  593. return &i->end_dev_attr_cont.ac == cont &&
  594. rphy->identify.device_type == SAS_END_DEVICE;
  595. }
  596. static int sas_expander_match(struct attribute_container *cont,
  597. struct device *dev)
  598. {
  599. struct Scsi_Host *shost;
  600. struct sas_internal *i;
  601. struct sas_rphy *rphy;
  602. if (!scsi_is_sas_rphy(dev))
  603. return 0;
  604. shost = dev_to_shost(dev->parent->parent);
  605. rphy = dev_to_rphy(dev);
  606. if (!shost->transportt)
  607. return 0;
  608. if (shost->transportt->host_attrs.ac.class !=
  609. &sas_host_class.class)
  610. return 0;
  611. i = to_sas_internal(shost->transportt);
  612. return &i->expander_attr_cont.ac == cont &&
  613. (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  614. rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
  615. }
  616. static void sas_expander_release(struct device *dev)
  617. {
  618. struct sas_rphy *rphy = dev_to_rphy(dev);
  619. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  620. put_device(dev->parent);
  621. kfree(edev);
  622. }
  623. static void sas_end_device_release(struct device *dev)
  624. {
  625. struct sas_rphy *rphy = dev_to_rphy(dev);
  626. struct sas_end_device *edev = rphy_to_end_device(rphy);
  627. put_device(dev->parent);
  628. kfree(edev);
  629. }
  630. /**
  631. * sas_end_device_alloc - allocate an rphy for an end device
  632. *
  633. * Allocates an SAS remote PHY structure, connected to @parent.
  634. *
  635. * Returns:
  636. * SAS PHY allocated or %NULL if the allocation failed.
  637. */
  638. struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent)
  639. {
  640. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  641. struct sas_end_device *rdev;
  642. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  643. if (!rdev) {
  644. return NULL;
  645. }
  646. device_initialize(&rdev->rphy.dev);
  647. rdev->rphy.dev.parent = get_device(&parent->dev);
  648. rdev->rphy.dev.release = sas_end_device_release;
  649. sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d-%d",
  650. shost->host_no, parent->port_identifier, parent->number);
  651. rdev->rphy.identify.device_type = SAS_END_DEVICE;
  652. transport_setup_device(&rdev->rphy.dev);
  653. return &rdev->rphy;
  654. }
  655. EXPORT_SYMBOL(sas_end_device_alloc);
  656. /**
  657. * sas_expander_alloc - allocate an rphy for an end device
  658. *
  659. * Allocates an SAS remote PHY structure, connected to @parent.
  660. *
  661. * Returns:
  662. * SAS PHY allocated or %NULL if the allocation failed.
  663. */
  664. struct sas_rphy *sas_expander_alloc(struct sas_phy *parent,
  665. enum sas_device_type type)
  666. {
  667. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  668. struct sas_expander_device *rdev;
  669. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  670. BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
  671. type != SAS_FANOUT_EXPANDER_DEVICE);
  672. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  673. if (!rdev) {
  674. return NULL;
  675. }
  676. device_initialize(&rdev->rphy.dev);
  677. rdev->rphy.dev.parent = get_device(&parent->dev);
  678. rdev->rphy.dev.release = sas_expander_release;
  679. mutex_lock(&sas_host->lock);
  680. rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
  681. mutex_unlock(&sas_host->lock);
  682. sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
  683. shost->host_no, rdev->rphy.scsi_target_id);
  684. rdev->rphy.identify.device_type = type;
  685. transport_setup_device(&rdev->rphy.dev);
  686. return &rdev->rphy;
  687. }
  688. EXPORT_SYMBOL(sas_expander_alloc);
  689. /**
  690. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  691. * @rphy: The remote PHY to be added
  692. *
  693. * Publishes a SAS remote PHY to the rest of the system.
  694. */
  695. int sas_rphy_add(struct sas_rphy *rphy)
  696. {
  697. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  698. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  699. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  700. struct sas_identify *identify = &rphy->identify;
  701. int error;
  702. if (parent->rphy)
  703. return -ENXIO;
  704. parent->rphy = rphy;
  705. error = device_add(&rphy->dev);
  706. if (error)
  707. return error;
  708. transport_add_device(&rphy->dev);
  709. transport_configure_device(&rphy->dev);
  710. mutex_lock(&sas_host->lock);
  711. list_add_tail(&rphy->list, &sas_host->rphy_list);
  712. if (identify->device_type == SAS_END_DEVICE &&
  713. (identify->target_port_protocols &
  714. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  715. rphy->scsi_target_id = sas_host->next_target_id++;
  716. else if (identify->device_type == SAS_END_DEVICE)
  717. rphy->scsi_target_id = -1;
  718. mutex_unlock(&sas_host->lock);
  719. if (identify->device_type == SAS_END_DEVICE &&
  720. rphy->scsi_target_id != -1) {
  721. scsi_scan_target(&rphy->dev, parent->port_identifier,
  722. rphy->scsi_target_id, ~0, 0);
  723. }
  724. return 0;
  725. }
  726. EXPORT_SYMBOL(sas_rphy_add);
  727. /**
  728. * sas_rphy_free -- free a SAS remote PHY
  729. * @rphy SAS remote PHY to free
  730. *
  731. * Frees the specified SAS remote PHY.
  732. *
  733. * Note:
  734. * This function must only be called on a remote
  735. * PHY that has not sucessfully been added using
  736. * sas_rphy_add().
  737. */
  738. void sas_rphy_free(struct sas_rphy *rphy)
  739. {
  740. struct device *dev = &rphy->dev;
  741. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  742. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  743. mutex_lock(&sas_host->lock);
  744. list_del(&rphy->list);
  745. mutex_unlock(&sas_host->lock);
  746. transport_destroy_device(dev);
  747. put_device(dev);
  748. }
  749. EXPORT_SYMBOL(sas_rphy_free);
  750. /**
  751. * sas_rphy_delete -- remove SAS remote PHY
  752. * @rphy: SAS remote PHY to remove
  753. *
  754. * Removes the specified SAS remote PHY.
  755. */
  756. void
  757. sas_rphy_delete(struct sas_rphy *rphy)
  758. {
  759. struct device *dev = &rphy->dev;
  760. struct sas_phy *parent = dev_to_phy(dev->parent);
  761. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  762. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  763. switch (rphy->identify.device_type) {
  764. case SAS_END_DEVICE:
  765. scsi_remove_target(dev);
  766. break;
  767. case SAS_EDGE_EXPANDER_DEVICE:
  768. case SAS_FANOUT_EXPANDER_DEVICE:
  769. device_for_each_child(dev, NULL, do_sas_phy_delete);
  770. break;
  771. default:
  772. break;
  773. }
  774. transport_remove_device(dev);
  775. device_del(dev);
  776. transport_destroy_device(dev);
  777. mutex_lock(&sas_host->lock);
  778. list_del(&rphy->list);
  779. mutex_unlock(&sas_host->lock);
  780. parent->rphy = NULL;
  781. put_device(dev);
  782. }
  783. EXPORT_SYMBOL(sas_rphy_delete);
  784. /**
  785. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  786. * @dev: device to check
  787. *
  788. * Returns:
  789. * %1 if the device represents a SAS remote PHY, %0 else
  790. */
  791. int scsi_is_sas_rphy(const struct device *dev)
  792. {
  793. return dev->release == sas_end_device_release ||
  794. dev->release == sas_expander_release;
  795. }
  796. EXPORT_SYMBOL(scsi_is_sas_rphy);
  797. /*
  798. * SCSI scan helper
  799. */
  800. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  801. uint id, uint lun)
  802. {
  803. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  804. struct sas_rphy *rphy;
  805. mutex_lock(&sas_host->lock);
  806. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  807. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  808. if (rphy->scsi_target_id == -1)
  809. continue;
  810. if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
  811. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  812. scsi_scan_target(&rphy->dev, parent->port_identifier,
  813. rphy->scsi_target_id, lun, 1);
  814. }
  815. }
  816. mutex_unlock(&sas_host->lock);
  817. return 0;
  818. }
  819. /*
  820. * Setup / Teardown code
  821. */
  822. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  823. i->private_##attrb[count] = class_device_attr_##field; \
  824. i->private_##attrb[count].attr.mode = perm; \
  825. i->private_##attrb[count].store = NULL; \
  826. i->attrb[count] = &i->private_##attrb[count]; \
  827. if (test) \
  828. count++
  829. #define SETUP_RPORT_ATTRIBUTE(field) \
  830. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
  831. #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
  832. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
  833. #define SETUP_PORT_ATTRIBUTE(field) \
  834. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
  835. #define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func) \
  836. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
  837. #define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
  838. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
  839. #define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func) \
  840. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
  841. #define SETUP_END_DEV_ATTRIBUTE(field) \
  842. SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
  843. #define SETUP_EXPANDER_ATTRIBUTE(field) \
  844. SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
  845. /**
  846. * sas_attach_transport -- instantiate SAS transport template
  847. * @ft: SAS transport class function template
  848. */
  849. struct scsi_transport_template *
  850. sas_attach_transport(struct sas_function_template *ft)
  851. {
  852. struct sas_internal *i;
  853. int count;
  854. i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
  855. if (!i)
  856. return NULL;
  857. i->t.user_scan = sas_user_scan;
  858. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  859. i->t.host_attrs.ac.class = &sas_host_class.class;
  860. i->t.host_attrs.ac.match = sas_host_match;
  861. transport_container_register(&i->t.host_attrs);
  862. i->t.host_size = sizeof(struct sas_host_attrs);
  863. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  864. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  865. i->phy_attr_cont.ac.match = sas_phy_match;
  866. transport_container_register(&i->phy_attr_cont);
  867. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  868. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  869. i->rphy_attr_cont.ac.match = sas_rphy_match;
  870. transport_container_register(&i->rphy_attr_cont);
  871. i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
  872. i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
  873. i->end_dev_attr_cont.ac.match = sas_end_dev_match;
  874. transport_container_register(&i->end_dev_attr_cont);
  875. i->expander_attr_cont.ac.class = &sas_expander_class.class;
  876. i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
  877. i->expander_attr_cont.ac.match = sas_expander_match;
  878. transport_container_register(&i->expander_attr_cont);
  879. i->f = ft;
  880. count = 0;
  881. i->host_attrs[count] = NULL;
  882. count = 0;
  883. SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
  884. SETUP_PORT_ATTRIBUTE(target_port_protocols);
  885. SETUP_PORT_ATTRIBUTE(device_type);
  886. SETUP_PORT_ATTRIBUTE(sas_address);
  887. SETUP_PORT_ATTRIBUTE(phy_identifier);
  888. SETUP_PORT_ATTRIBUTE(port_identifier);
  889. SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
  890. SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
  891. SETUP_PORT_ATTRIBUTE(minimum_linkrate);
  892. SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
  893. SETUP_PORT_ATTRIBUTE(maximum_linkrate);
  894. SETUP_PORT_ATTRIBUTE(invalid_dword_count);
  895. SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
  896. SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
  897. SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
  898. SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset);
  899. SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
  900. i->phy_attrs[count] = NULL;
  901. count = 0;
  902. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  903. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  904. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  905. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  906. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  907. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
  908. get_enclosure_identifier);
  909. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
  910. get_bay_identifier);
  911. i->rphy_attrs[count] = NULL;
  912. count = 0;
  913. SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
  914. SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
  915. SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
  916. i->end_dev_attrs[count] = NULL;
  917. count = 0;
  918. SETUP_EXPANDER_ATTRIBUTE(vendor_id);
  919. SETUP_EXPANDER_ATTRIBUTE(product_id);
  920. SETUP_EXPANDER_ATTRIBUTE(product_rev);
  921. SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
  922. SETUP_EXPANDER_ATTRIBUTE(component_id);
  923. SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
  924. SETUP_EXPANDER_ATTRIBUTE(level);
  925. i->expander_attrs[count] = NULL;
  926. return &i->t;
  927. }
  928. EXPORT_SYMBOL(sas_attach_transport);
  929. /**
  930. * sas_release_transport -- release SAS transport template instance
  931. * @t: transport template instance
  932. */
  933. void sas_release_transport(struct scsi_transport_template *t)
  934. {
  935. struct sas_internal *i = to_sas_internal(t);
  936. transport_container_unregister(&i->t.host_attrs);
  937. transport_container_unregister(&i->phy_attr_cont);
  938. transport_container_unregister(&i->rphy_attr_cont);
  939. transport_container_unregister(&i->end_dev_attr_cont);
  940. transport_container_unregister(&i->expander_attr_cont);
  941. kfree(i);
  942. }
  943. EXPORT_SYMBOL(sas_release_transport);
  944. static __init int sas_transport_init(void)
  945. {
  946. int error;
  947. error = transport_class_register(&sas_host_class);
  948. if (error)
  949. goto out;
  950. error = transport_class_register(&sas_phy_class);
  951. if (error)
  952. goto out_unregister_transport;
  953. error = transport_class_register(&sas_rphy_class);
  954. if (error)
  955. goto out_unregister_phy;
  956. error = transport_class_register(&sas_end_dev_class);
  957. if (error)
  958. goto out_unregister_rphy;
  959. error = transport_class_register(&sas_expander_class);
  960. if (error)
  961. goto out_unregister_end_dev;
  962. return 0;
  963. out_unregister_end_dev:
  964. transport_class_unregister(&sas_end_dev_class);
  965. out_unregister_rphy:
  966. transport_class_unregister(&sas_rphy_class);
  967. out_unregister_phy:
  968. transport_class_unregister(&sas_phy_class);
  969. out_unregister_transport:
  970. transport_class_unregister(&sas_host_class);
  971. out:
  972. return error;
  973. }
  974. static void __exit sas_transport_exit(void)
  975. {
  976. transport_class_unregister(&sas_host_class);
  977. transport_class_unregister(&sas_phy_class);
  978. transport_class_unregister(&sas_rphy_class);
  979. transport_class_unregister(&sas_end_dev_class);
  980. transport_class_unregister(&sas_expander_class);
  981. }
  982. MODULE_AUTHOR("Christoph Hellwig");
  983. MODULE_DESCRIPTION("SAS Transphy Attributes");
  984. MODULE_LICENSE("GPL");
  985. module_init(sas_transport_init);
  986. module_exit(sas_transport_exit);