scsi_transport_sas.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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. int next_port_id;
  42. };
  43. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  44. /*
  45. * Hack to allow attributes of the same name in different objects.
  46. */
  47. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  48. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  49. __ATTR(_name,_mode,_show,_store)
  50. /*
  51. * Pretty printing helpers
  52. */
  53. #define sas_bitfield_name_match(title, table) \
  54. static ssize_t \
  55. get_sas_##title##_names(u32 table_key, char *buf) \
  56. { \
  57. char *prefix = ""; \
  58. ssize_t len = 0; \
  59. int i; \
  60. \
  61. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  62. if (table[i].value & table_key) { \
  63. len += sprintf(buf + len, "%s%s", \
  64. prefix, table[i].name); \
  65. prefix = ", "; \
  66. } \
  67. } \
  68. len += sprintf(buf + len, "\n"); \
  69. return len; \
  70. }
  71. #define sas_bitfield_name_set(title, table) \
  72. static ssize_t \
  73. set_sas_##title##_names(u32 *table_key, const char *buf) \
  74. { \
  75. ssize_t len = 0; \
  76. int i; \
  77. \
  78. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  79. len = strlen(table[i].name); \
  80. if (strncmp(buf, table[i].name, len) == 0 && \
  81. (buf[len] == '\n' || buf[len] == '\0')) { \
  82. *table_key = table[i].value; \
  83. return 0; \
  84. } \
  85. } \
  86. return -EINVAL; \
  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 < ARRAY_SIZE(table); 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. { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
  136. };
  137. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  138. sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
  139. /*
  140. * SAS host attributes
  141. */
  142. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  143. struct class_device *cdev)
  144. {
  145. struct Scsi_Host *shost = dev_to_shost(dev);
  146. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  147. INIT_LIST_HEAD(&sas_host->rphy_list);
  148. mutex_init(&sas_host->lock);
  149. sas_host->next_target_id = 0;
  150. sas_host->next_expander_id = 0;
  151. sas_host->next_port_id = 0;
  152. return 0;
  153. }
  154. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  155. "sas_host", sas_host_setup, NULL, NULL);
  156. static int sas_host_match(struct attribute_container *cont,
  157. struct device *dev)
  158. {
  159. struct Scsi_Host *shost;
  160. struct sas_internal *i;
  161. if (!scsi_is_host_device(dev))
  162. return 0;
  163. shost = dev_to_shost(dev);
  164. if (!shost->transportt)
  165. return 0;
  166. if (shost->transportt->host_attrs.ac.class !=
  167. &sas_host_class.class)
  168. return 0;
  169. i = to_sas_internal(shost->transportt);
  170. return &i->t.host_attrs.ac == cont;
  171. }
  172. static int do_sas_phy_delete(struct device *dev, void *data)
  173. {
  174. int pass = (int)(unsigned long)data;
  175. if (pass == 0 && scsi_is_sas_port(dev))
  176. sas_port_delete(dev_to_sas_port(dev));
  177. else if (pass == 1 && scsi_is_sas_phy(dev))
  178. sas_phy_delete(dev_to_phy(dev));
  179. return 0;
  180. }
  181. /**
  182. * sas_remove_children -- tear down a devices SAS data structures
  183. * @dev: device belonging to the sas object
  184. *
  185. * Removes all SAS PHYs and remote PHYs for a given object
  186. */
  187. void sas_remove_children(struct device *dev)
  188. {
  189. device_for_each_child(dev, (void *)0, do_sas_phy_delete);
  190. device_for_each_child(dev, (void *)1, do_sas_phy_delete);
  191. }
  192. EXPORT_SYMBOL(sas_remove_children);
  193. /**
  194. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  195. * @shost: Scsi Host that is torn down
  196. *
  197. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  198. * Must be called just before scsi_remove_host for SAS HBAs.
  199. */
  200. void sas_remove_host(struct Scsi_Host *shost)
  201. {
  202. sas_remove_children(&shost->shost_gendev);
  203. }
  204. EXPORT_SYMBOL(sas_remove_host);
  205. /*
  206. * SAS Phy attributes
  207. */
  208. #define sas_phy_show_simple(field, name, format_string, cast) \
  209. static ssize_t \
  210. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  211. { \
  212. struct sas_phy *phy = transport_class_to_phy(cdev); \
  213. \
  214. return snprintf(buf, 20, format_string, cast phy->field); \
  215. }
  216. #define sas_phy_simple_attr(field, name, format_string, type) \
  217. sas_phy_show_simple(field, name, format_string, (type)) \
  218. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  219. #define sas_phy_show_protocol(field, name) \
  220. static ssize_t \
  221. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  222. { \
  223. struct sas_phy *phy = transport_class_to_phy(cdev); \
  224. \
  225. if (!phy->field) \
  226. return snprintf(buf, 20, "none\n"); \
  227. return get_sas_protocol_names(phy->field, buf); \
  228. }
  229. #define sas_phy_protocol_attr(field, name) \
  230. sas_phy_show_protocol(field, name) \
  231. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  232. #define sas_phy_show_linkspeed(field) \
  233. static ssize_t \
  234. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  235. { \
  236. struct sas_phy *phy = transport_class_to_phy(cdev); \
  237. \
  238. return get_sas_linkspeed_names(phy->field, buf); \
  239. }
  240. /* Fudge to tell if we're minimum or maximum */
  241. #define sas_phy_store_linkspeed(field) \
  242. static ssize_t \
  243. store_sas_phy_##field(struct class_device *cdev, const char *buf, \
  244. size_t count) \
  245. { \
  246. struct sas_phy *phy = transport_class_to_phy(cdev); \
  247. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  248. struct sas_internal *i = to_sas_internal(shost->transportt); \
  249. u32 value; \
  250. struct sas_phy_linkrates rates = {0}; \
  251. int error; \
  252. \
  253. error = set_sas_linkspeed_names(&value, buf); \
  254. if (error) \
  255. return error; \
  256. rates.field = value; \
  257. error = i->f->set_phy_speed(phy, &rates); \
  258. \
  259. return error ? error : count; \
  260. }
  261. #define sas_phy_linkspeed_rw_attr(field) \
  262. sas_phy_show_linkspeed(field) \
  263. sas_phy_store_linkspeed(field) \
  264. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
  265. store_sas_phy_##field)
  266. #define sas_phy_linkspeed_attr(field) \
  267. sas_phy_show_linkspeed(field) \
  268. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  269. #define sas_phy_show_linkerror(field) \
  270. static ssize_t \
  271. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  272. { \
  273. struct sas_phy *phy = transport_class_to_phy(cdev); \
  274. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  275. struct sas_internal *i = to_sas_internal(shost->transportt); \
  276. int error; \
  277. \
  278. error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
  279. if (error) \
  280. return error; \
  281. return snprintf(buf, 20, "%u\n", phy->field); \
  282. }
  283. #define sas_phy_linkerror_attr(field) \
  284. sas_phy_show_linkerror(field) \
  285. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  286. static ssize_t
  287. show_sas_device_type(struct class_device *cdev, char *buf)
  288. {
  289. struct sas_phy *phy = transport_class_to_phy(cdev);
  290. if (!phy->identify.device_type)
  291. return snprintf(buf, 20, "none\n");
  292. return get_sas_device_type_names(phy->identify.device_type, buf);
  293. }
  294. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  295. static ssize_t do_sas_phy_reset(struct class_device *cdev,
  296. size_t count, int hard_reset)
  297. {
  298. struct sas_phy *phy = transport_class_to_phy(cdev);
  299. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  300. struct sas_internal *i = to_sas_internal(shost->transportt);
  301. int error;
  302. error = i->f->phy_reset(phy, hard_reset);
  303. if (error)
  304. return error;
  305. return count;
  306. };
  307. static ssize_t store_sas_link_reset(struct class_device *cdev,
  308. const char *buf, size_t count)
  309. {
  310. return do_sas_phy_reset(cdev, count, 0);
  311. }
  312. static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  313. static ssize_t store_sas_hard_reset(struct class_device *cdev,
  314. const char *buf, size_t count)
  315. {
  316. return do_sas_phy_reset(cdev, count, 1);
  317. }
  318. static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  319. sas_phy_protocol_attr(identify.initiator_port_protocols,
  320. initiator_port_protocols);
  321. sas_phy_protocol_attr(identify.target_port_protocols,
  322. target_port_protocols);
  323. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  324. unsigned long long);
  325. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  326. //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
  327. sas_phy_linkspeed_attr(negotiated_linkrate);
  328. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  329. sas_phy_linkspeed_rw_attr(minimum_linkrate);
  330. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  331. sas_phy_linkspeed_rw_attr(maximum_linkrate);
  332. sas_phy_linkerror_attr(invalid_dword_count);
  333. sas_phy_linkerror_attr(running_disparity_error_count);
  334. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  335. sas_phy_linkerror_attr(phy_reset_problem_count);
  336. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  337. "sas_phy", NULL, NULL, NULL);
  338. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  339. {
  340. struct Scsi_Host *shost;
  341. struct sas_internal *i;
  342. if (!scsi_is_sas_phy(dev))
  343. return 0;
  344. shost = dev_to_shost(dev->parent);
  345. if (!shost->transportt)
  346. return 0;
  347. if (shost->transportt->host_attrs.ac.class !=
  348. &sas_host_class.class)
  349. return 0;
  350. i = to_sas_internal(shost->transportt);
  351. return &i->phy_attr_cont.ac == cont;
  352. }
  353. static void sas_phy_release(struct device *dev)
  354. {
  355. struct sas_phy *phy = dev_to_phy(dev);
  356. put_device(dev->parent);
  357. kfree(phy);
  358. }
  359. /**
  360. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  361. * @parent: Parent device
  362. * @number: Phy index
  363. *
  364. * Allocates an SAS PHY structure. It will be added in the device tree
  365. * below the device specified by @parent, which has to be either a Scsi_Host
  366. * or sas_rphy.
  367. *
  368. * Returns:
  369. * SAS PHY allocated or %NULL if the allocation failed.
  370. */
  371. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  372. {
  373. struct Scsi_Host *shost = dev_to_shost(parent);
  374. struct sas_phy *phy;
  375. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  376. if (!phy)
  377. return NULL;
  378. phy->number = number;
  379. device_initialize(&phy->dev);
  380. phy->dev.parent = get_device(parent);
  381. phy->dev.release = sas_phy_release;
  382. INIT_LIST_HEAD(&phy->port_siblings);
  383. if (scsi_is_sas_expander_device(parent)) {
  384. struct sas_rphy *rphy = dev_to_rphy(parent);
  385. sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
  386. rphy->scsi_target_id, number);
  387. } else
  388. sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
  389. transport_setup_device(&phy->dev);
  390. return phy;
  391. }
  392. EXPORT_SYMBOL(sas_phy_alloc);
  393. /**
  394. * sas_phy_add -- add a SAS PHY to the device hierachy
  395. * @phy: The PHY to be added
  396. *
  397. * Publishes a SAS PHY to the rest of the system.
  398. */
  399. int sas_phy_add(struct sas_phy *phy)
  400. {
  401. int error;
  402. error = device_add(&phy->dev);
  403. if (!error) {
  404. transport_add_device(&phy->dev);
  405. transport_configure_device(&phy->dev);
  406. }
  407. return error;
  408. }
  409. EXPORT_SYMBOL(sas_phy_add);
  410. /**
  411. * sas_phy_free -- free a SAS PHY
  412. * @phy: SAS PHY to free
  413. *
  414. * Frees the specified SAS PHY.
  415. *
  416. * Note:
  417. * This function must only be called on a PHY that has not
  418. * sucessfully been added using sas_phy_add().
  419. */
  420. void sas_phy_free(struct sas_phy *phy)
  421. {
  422. transport_destroy_device(&phy->dev);
  423. put_device(&phy->dev);
  424. }
  425. EXPORT_SYMBOL(sas_phy_free);
  426. /**
  427. * sas_phy_delete -- remove SAS PHY
  428. * @phy: SAS PHY to remove
  429. *
  430. * Removes the specified SAS PHY. If the SAS PHY has an
  431. * associated remote PHY it is removed before.
  432. */
  433. void
  434. sas_phy_delete(struct sas_phy *phy)
  435. {
  436. struct device *dev = &phy->dev;
  437. /* this happens if the phy is still part of a port when deleted */
  438. BUG_ON(!list_empty(&phy->port_siblings));
  439. transport_remove_device(dev);
  440. device_del(dev);
  441. transport_destroy_device(dev);
  442. put_device(dev);
  443. }
  444. EXPORT_SYMBOL(sas_phy_delete);
  445. /**
  446. * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
  447. * @dev: device to check
  448. *
  449. * Returns:
  450. * %1 if the device represents a SAS PHY, %0 else
  451. */
  452. int scsi_is_sas_phy(const struct device *dev)
  453. {
  454. return dev->release == sas_phy_release;
  455. }
  456. EXPORT_SYMBOL(scsi_is_sas_phy);
  457. /*
  458. * SAS Port attributes
  459. */
  460. #define sas_port_show_simple(field, name, format_string, cast) \
  461. static ssize_t \
  462. show_sas_port_##name(struct class_device *cdev, char *buf) \
  463. { \
  464. struct sas_port *port = transport_class_to_sas_port(cdev); \
  465. \
  466. return snprintf(buf, 20, format_string, cast port->field); \
  467. }
  468. #define sas_port_simple_attr(field, name, format_string, type) \
  469. sas_port_show_simple(field, name, format_string, (type)) \
  470. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
  471. sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
  472. static DECLARE_TRANSPORT_CLASS(sas_port_class,
  473. "sas_port", NULL, NULL, NULL);
  474. static int sas_port_match(struct attribute_container *cont, struct device *dev)
  475. {
  476. struct Scsi_Host *shost;
  477. struct sas_internal *i;
  478. if (!scsi_is_sas_port(dev))
  479. return 0;
  480. shost = dev_to_shost(dev->parent);
  481. if (!shost->transportt)
  482. return 0;
  483. if (shost->transportt->host_attrs.ac.class !=
  484. &sas_host_class.class)
  485. return 0;
  486. i = to_sas_internal(shost->transportt);
  487. return &i->port_attr_cont.ac == cont;
  488. }
  489. static void sas_port_release(struct device *dev)
  490. {
  491. struct sas_port *port = dev_to_sas_port(dev);
  492. BUG_ON(!list_empty(&port->phy_list));
  493. put_device(dev->parent);
  494. kfree(port);
  495. }
  496. static void sas_port_create_link(struct sas_port *port,
  497. struct sas_phy *phy)
  498. {
  499. sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, phy->dev.bus_id);
  500. sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
  501. }
  502. static void sas_port_delete_link(struct sas_port *port,
  503. struct sas_phy *phy)
  504. {
  505. sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
  506. sysfs_remove_link(&phy->dev.kobj, "port");
  507. }
  508. /** sas_port_alloc - allocate and initialize a SAS port structure
  509. *
  510. * @parent: parent device
  511. * @port_id: port number
  512. *
  513. * Allocates a SAS port structure. It will be added to the device tree
  514. * below the device specified by @parent which must be either a Scsi_Host
  515. * or a sas_expander_device.
  516. *
  517. * Returns %NULL on error
  518. */
  519. struct sas_port *sas_port_alloc(struct device *parent, int port_id)
  520. {
  521. struct Scsi_Host *shost = dev_to_shost(parent);
  522. struct sas_port *port;
  523. port = kzalloc(sizeof(*port), GFP_KERNEL);
  524. if (!port)
  525. return NULL;
  526. port->port_identifier = port_id;
  527. device_initialize(&port->dev);
  528. port->dev.parent = get_device(parent);
  529. port->dev.release = sas_port_release;
  530. mutex_init(&port->phy_list_mutex);
  531. INIT_LIST_HEAD(&port->phy_list);
  532. if (scsi_is_sas_expander_device(parent)) {
  533. struct sas_rphy *rphy = dev_to_rphy(parent);
  534. sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
  535. rphy->scsi_target_id, port->port_identifier);
  536. } else
  537. sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
  538. port->port_identifier);
  539. transport_setup_device(&port->dev);
  540. return port;
  541. }
  542. EXPORT_SYMBOL(sas_port_alloc);
  543. /** sas_port_alloc_num - allocate and initialize a SAS port structure
  544. *
  545. * @parent: parent device
  546. *
  547. * Allocates a SAS port structure and a number to go with it. This
  548. * interface is really for adapters where the port number has no
  549. * meansing, so the sas class should manage them. It will be added to
  550. * the device tree below the device specified by @parent which must be
  551. * either a Scsi_Host or a sas_expander_device.
  552. *
  553. * Returns %NULL on error
  554. */
  555. struct sas_port *sas_port_alloc_num(struct device *parent)
  556. {
  557. int index;
  558. struct Scsi_Host *shost = dev_to_shost(parent);
  559. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  560. /* FIXME: use idr for this eventually */
  561. mutex_lock(&sas_host->lock);
  562. if (scsi_is_sas_expander_device(parent)) {
  563. struct sas_rphy *rphy = dev_to_rphy(parent);
  564. struct sas_expander_device *exp = rphy_to_expander_device(rphy);
  565. index = exp->next_port_id++;
  566. } else
  567. index = sas_host->next_port_id++;
  568. mutex_unlock(&sas_host->lock);
  569. return sas_port_alloc(parent, index);
  570. }
  571. EXPORT_SYMBOL(sas_port_alloc_num);
  572. /**
  573. * sas_port_add - add a SAS port to the device hierarchy
  574. *
  575. * @port: port to be added
  576. *
  577. * publishes a port to the rest of the system
  578. */
  579. int sas_port_add(struct sas_port *port)
  580. {
  581. int error;
  582. /* No phys should be added until this is made visible */
  583. BUG_ON(!list_empty(&port->phy_list));
  584. error = device_add(&port->dev);
  585. if (error)
  586. return error;
  587. transport_add_device(&port->dev);
  588. transport_configure_device(&port->dev);
  589. return 0;
  590. }
  591. EXPORT_SYMBOL(sas_port_add);
  592. /**
  593. * sas_port_free -- free a SAS PORT
  594. * @port: SAS PORT to free
  595. *
  596. * Frees the specified SAS PORT.
  597. *
  598. * Note:
  599. * This function must only be called on a PORT that has not
  600. * sucessfully been added using sas_port_add().
  601. */
  602. void sas_port_free(struct sas_port *port)
  603. {
  604. transport_destroy_device(&port->dev);
  605. put_device(&port->dev);
  606. }
  607. EXPORT_SYMBOL(sas_port_free);
  608. /**
  609. * sas_port_delete -- remove SAS PORT
  610. * @port: SAS PORT to remove
  611. *
  612. * Removes the specified SAS PORT. If the SAS PORT has an
  613. * associated phys, unlink them from the port as well.
  614. */
  615. void sas_port_delete(struct sas_port *port)
  616. {
  617. struct device *dev = &port->dev;
  618. struct sas_phy *phy, *tmp_phy;
  619. if (port->rphy) {
  620. sas_rphy_delete(port->rphy);
  621. port->rphy = NULL;
  622. }
  623. mutex_lock(&port->phy_list_mutex);
  624. list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
  625. port_siblings) {
  626. sas_port_delete_link(port, phy);
  627. list_del_init(&phy->port_siblings);
  628. }
  629. mutex_unlock(&port->phy_list_mutex);
  630. if (port->is_backlink) {
  631. struct device *parent = port->dev.parent;
  632. sysfs_remove_link(&port->dev.kobj, parent->bus_id);
  633. port->is_backlink = 0;
  634. }
  635. transport_remove_device(dev);
  636. device_del(dev);
  637. transport_destroy_device(dev);
  638. put_device(dev);
  639. }
  640. EXPORT_SYMBOL(sas_port_delete);
  641. /**
  642. * scsi_is_sas_port -- check if a struct device represents a SAS port
  643. * @dev: device to check
  644. *
  645. * Returns:
  646. * %1 if the device represents a SAS Port, %0 else
  647. */
  648. int scsi_is_sas_port(const struct device *dev)
  649. {
  650. return dev->release == sas_port_release;
  651. }
  652. EXPORT_SYMBOL(scsi_is_sas_port);
  653. /**
  654. * sas_port_add_phy - add another phy to a port to form a wide port
  655. * @port: port to add the phy to
  656. * @phy: phy to add
  657. *
  658. * When a port is initially created, it is empty (has no phys). All
  659. * ports must have at least one phy to operated, and all wide ports
  660. * must have at least two. The current code makes no difference
  661. * between ports and wide ports, but the only object that can be
  662. * connected to a remote device is a port, so ports must be formed on
  663. * all devices with phys if they're connected to anything.
  664. */
  665. void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
  666. {
  667. mutex_lock(&port->phy_list_mutex);
  668. if (unlikely(!list_empty(&phy->port_siblings))) {
  669. /* make sure we're already on this port */
  670. struct sas_phy *tmp;
  671. list_for_each_entry(tmp, &port->phy_list, port_siblings)
  672. if (tmp == phy)
  673. break;
  674. /* If this trips, you added a phy that was already
  675. * part of a different port */
  676. if (unlikely(tmp != phy)) {
  677. dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
  678. BUG();
  679. }
  680. } else {
  681. sas_port_create_link(port, phy);
  682. list_add_tail(&phy->port_siblings, &port->phy_list);
  683. port->num_phys++;
  684. }
  685. mutex_unlock(&port->phy_list_mutex);
  686. }
  687. EXPORT_SYMBOL(sas_port_add_phy);
  688. /**
  689. * sas_port_delete_phy - remove a phy from a port or wide port
  690. * @port: port to remove the phy from
  691. * @phy: phy to remove
  692. *
  693. * This operation is used for tearing down ports again. It must be
  694. * done to every port or wide port before calling sas_port_delete.
  695. */
  696. void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
  697. {
  698. mutex_lock(&port->phy_list_mutex);
  699. sas_port_delete_link(port, phy);
  700. list_del_init(&phy->port_siblings);
  701. port->num_phys--;
  702. mutex_unlock(&port->phy_list_mutex);
  703. }
  704. EXPORT_SYMBOL(sas_port_delete_phy);
  705. void sas_port_mark_backlink(struct sas_port *port)
  706. {
  707. struct device *parent = port->dev.parent->parent->parent;
  708. if (port->is_backlink)
  709. return;
  710. port->is_backlink = 1;
  711. sysfs_create_link(&port->dev.kobj, &parent->kobj,
  712. parent->bus_id);
  713. }
  714. EXPORT_SYMBOL(sas_port_mark_backlink);
  715. /*
  716. * SAS remote PHY attributes.
  717. */
  718. #define sas_rphy_show_simple(field, name, format_string, cast) \
  719. static ssize_t \
  720. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  721. { \
  722. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  723. \
  724. return snprintf(buf, 20, format_string, cast rphy->field); \
  725. }
  726. #define sas_rphy_simple_attr(field, name, format_string, type) \
  727. sas_rphy_show_simple(field, name, format_string, (type)) \
  728. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  729. show_sas_rphy_##name, NULL)
  730. #define sas_rphy_show_protocol(field, name) \
  731. static ssize_t \
  732. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  733. { \
  734. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  735. \
  736. if (!rphy->field) \
  737. return snprintf(buf, 20, "none\n"); \
  738. return get_sas_protocol_names(rphy->field, buf); \
  739. }
  740. #define sas_rphy_protocol_attr(field, name) \
  741. sas_rphy_show_protocol(field, name) \
  742. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  743. show_sas_rphy_##name, NULL)
  744. static ssize_t
  745. show_sas_rphy_device_type(struct class_device *cdev, char *buf)
  746. {
  747. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  748. if (!rphy->identify.device_type)
  749. return snprintf(buf, 20, "none\n");
  750. return get_sas_device_type_names(
  751. rphy->identify.device_type, buf);
  752. }
  753. static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  754. show_sas_rphy_device_type, NULL);
  755. static ssize_t
  756. show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
  757. {
  758. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  759. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  760. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  761. struct sas_internal *i = to_sas_internal(shost->transportt);
  762. u64 identifier;
  763. int error;
  764. /*
  765. * Only devices behind an expander are supported, because the
  766. * enclosure identifier is a SMP feature.
  767. */
  768. if (scsi_is_sas_phy_local(phy))
  769. return -EINVAL;
  770. error = i->f->get_enclosure_identifier(rphy, &identifier);
  771. if (error)
  772. return error;
  773. return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
  774. }
  775. static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
  776. show_sas_rphy_enclosure_identifier, NULL);
  777. static ssize_t
  778. show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
  779. {
  780. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  781. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  782. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  783. struct sas_internal *i = to_sas_internal(shost->transportt);
  784. int val;
  785. if (scsi_is_sas_phy_local(phy))
  786. return -EINVAL;
  787. val = i->f->get_bay_identifier(rphy);
  788. if (val < 0)
  789. return val;
  790. return sprintf(buf, "%d\n", val);
  791. }
  792. static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
  793. show_sas_rphy_bay_identifier, NULL);
  794. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  795. initiator_port_protocols);
  796. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  797. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  798. unsigned long long);
  799. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  800. /* only need 8 bytes of data plus header (4 or 8) */
  801. #define BUF_SIZE 64
  802. int sas_read_port_mode_page(struct scsi_device *sdev)
  803. {
  804. char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
  805. struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
  806. struct sas_end_device *rdev;
  807. struct scsi_mode_data mode_data;
  808. int res, error;
  809. BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
  810. rdev = rphy_to_end_device(rphy);
  811. if (!buffer)
  812. return -ENOMEM;
  813. res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
  814. &mode_data, NULL);
  815. error = -EINVAL;
  816. if (!scsi_status_is_good(res))
  817. goto out;
  818. msdata = buffer + mode_data.header_length +
  819. mode_data.block_descriptor_length;
  820. if (msdata - buffer > BUF_SIZE - 8)
  821. goto out;
  822. error = 0;
  823. rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
  824. rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
  825. rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
  826. out:
  827. kfree(buffer);
  828. return error;
  829. }
  830. EXPORT_SYMBOL(sas_read_port_mode_page);
  831. static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
  832. "sas_end_device", NULL, NULL, NULL);
  833. #define sas_end_dev_show_simple(field, name, format_string, cast) \
  834. static ssize_t \
  835. show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
  836. { \
  837. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  838. struct sas_end_device *rdev = rphy_to_end_device(rphy); \
  839. \
  840. return snprintf(buf, 20, format_string, cast rdev->field); \
  841. }
  842. #define sas_end_dev_simple_attr(field, name, format_string, type) \
  843. sas_end_dev_show_simple(field, name, format_string, (type)) \
  844. static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
  845. show_sas_end_dev_##name, NULL)
  846. sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
  847. sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
  848. "%d\n", int);
  849. sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
  850. "%d\n", int);
  851. static DECLARE_TRANSPORT_CLASS(sas_expander_class,
  852. "sas_expander", NULL, NULL, NULL);
  853. #define sas_expander_show_simple(field, name, format_string, cast) \
  854. static ssize_t \
  855. show_sas_expander_##name(struct class_device *cdev, char *buf) \
  856. { \
  857. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  858. struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
  859. \
  860. return snprintf(buf, 20, format_string, cast edev->field); \
  861. }
  862. #define sas_expander_simple_attr(field, name, format_string, type) \
  863. sas_expander_show_simple(field, name, format_string, (type)) \
  864. static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
  865. show_sas_expander_##name, NULL)
  866. sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
  867. sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
  868. sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
  869. sas_expander_simple_attr(component_vendor_id, component_vendor_id,
  870. "%s\n", char *);
  871. sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
  872. sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
  873. unsigned int);
  874. sas_expander_simple_attr(level, level, "%d\n", int);
  875. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  876. "sas_device", NULL, NULL, NULL);
  877. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  878. {
  879. struct Scsi_Host *shost;
  880. struct sas_internal *i;
  881. if (!scsi_is_sas_rphy(dev))
  882. return 0;
  883. shost = dev_to_shost(dev->parent->parent);
  884. if (!shost->transportt)
  885. return 0;
  886. if (shost->transportt->host_attrs.ac.class !=
  887. &sas_host_class.class)
  888. return 0;
  889. i = to_sas_internal(shost->transportt);
  890. return &i->rphy_attr_cont.ac == cont;
  891. }
  892. static int sas_end_dev_match(struct attribute_container *cont,
  893. struct device *dev)
  894. {
  895. struct Scsi_Host *shost;
  896. struct sas_internal *i;
  897. struct sas_rphy *rphy;
  898. if (!scsi_is_sas_rphy(dev))
  899. return 0;
  900. shost = dev_to_shost(dev->parent->parent);
  901. rphy = dev_to_rphy(dev);
  902. if (!shost->transportt)
  903. return 0;
  904. if (shost->transportt->host_attrs.ac.class !=
  905. &sas_host_class.class)
  906. return 0;
  907. i = to_sas_internal(shost->transportt);
  908. return &i->end_dev_attr_cont.ac == cont &&
  909. rphy->identify.device_type == SAS_END_DEVICE;
  910. }
  911. static int sas_expander_match(struct attribute_container *cont,
  912. struct device *dev)
  913. {
  914. struct Scsi_Host *shost;
  915. struct sas_internal *i;
  916. struct sas_rphy *rphy;
  917. if (!scsi_is_sas_rphy(dev))
  918. return 0;
  919. shost = dev_to_shost(dev->parent->parent);
  920. rphy = dev_to_rphy(dev);
  921. if (!shost->transportt)
  922. return 0;
  923. if (shost->transportt->host_attrs.ac.class !=
  924. &sas_host_class.class)
  925. return 0;
  926. i = to_sas_internal(shost->transportt);
  927. return &i->expander_attr_cont.ac == cont &&
  928. (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  929. rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
  930. }
  931. static void sas_expander_release(struct device *dev)
  932. {
  933. struct sas_rphy *rphy = dev_to_rphy(dev);
  934. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  935. put_device(dev->parent);
  936. kfree(edev);
  937. }
  938. static void sas_end_device_release(struct device *dev)
  939. {
  940. struct sas_rphy *rphy = dev_to_rphy(dev);
  941. struct sas_end_device *edev = rphy_to_end_device(rphy);
  942. put_device(dev->parent);
  943. kfree(edev);
  944. }
  945. /**
  946. * sas_rphy_initialize - common rphy intialization
  947. * @rphy: rphy to initialise
  948. *
  949. * Used by both sas_end_device_alloc() and sas_expander_alloc() to
  950. * initialise the common rphy component of each.
  951. */
  952. static void sas_rphy_initialize(struct sas_rphy *rphy)
  953. {
  954. INIT_LIST_HEAD(&rphy->list);
  955. }
  956. /**
  957. * sas_end_device_alloc - allocate an rphy for an end device
  958. *
  959. * Allocates an SAS remote PHY structure, connected to @parent.
  960. *
  961. * Returns:
  962. * SAS PHY allocated or %NULL if the allocation failed.
  963. */
  964. struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
  965. {
  966. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  967. struct sas_end_device *rdev;
  968. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  969. if (!rdev) {
  970. return NULL;
  971. }
  972. device_initialize(&rdev->rphy.dev);
  973. rdev->rphy.dev.parent = get_device(&parent->dev);
  974. rdev->rphy.dev.release = sas_end_device_release;
  975. if (scsi_is_sas_expander_device(parent->dev.parent)) {
  976. struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
  977. sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
  978. shost->host_no, rphy->scsi_target_id, parent->port_identifier);
  979. } else
  980. sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
  981. shost->host_no, parent->port_identifier);
  982. rdev->rphy.identify.device_type = SAS_END_DEVICE;
  983. sas_rphy_initialize(&rdev->rphy);
  984. transport_setup_device(&rdev->rphy.dev);
  985. return &rdev->rphy;
  986. }
  987. EXPORT_SYMBOL(sas_end_device_alloc);
  988. /**
  989. * sas_expander_alloc - allocate an rphy for an end device
  990. *
  991. * Allocates an SAS remote PHY structure, connected to @parent.
  992. *
  993. * Returns:
  994. * SAS PHY allocated or %NULL if the allocation failed.
  995. */
  996. struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
  997. enum sas_device_type type)
  998. {
  999. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  1000. struct sas_expander_device *rdev;
  1001. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1002. BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
  1003. type != SAS_FANOUT_EXPANDER_DEVICE);
  1004. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  1005. if (!rdev) {
  1006. return NULL;
  1007. }
  1008. device_initialize(&rdev->rphy.dev);
  1009. rdev->rphy.dev.parent = get_device(&parent->dev);
  1010. rdev->rphy.dev.release = sas_expander_release;
  1011. mutex_lock(&sas_host->lock);
  1012. rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
  1013. mutex_unlock(&sas_host->lock);
  1014. sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
  1015. shost->host_no, rdev->rphy.scsi_target_id);
  1016. rdev->rphy.identify.device_type = type;
  1017. sas_rphy_initialize(&rdev->rphy);
  1018. transport_setup_device(&rdev->rphy.dev);
  1019. return &rdev->rphy;
  1020. }
  1021. EXPORT_SYMBOL(sas_expander_alloc);
  1022. /**
  1023. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  1024. * @rphy: The remote PHY to be added
  1025. *
  1026. * Publishes a SAS remote PHY to the rest of the system.
  1027. */
  1028. int sas_rphy_add(struct sas_rphy *rphy)
  1029. {
  1030. struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
  1031. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  1032. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1033. struct sas_identify *identify = &rphy->identify;
  1034. int error;
  1035. if (parent->rphy)
  1036. return -ENXIO;
  1037. parent->rphy = rphy;
  1038. error = device_add(&rphy->dev);
  1039. if (error)
  1040. return error;
  1041. transport_add_device(&rphy->dev);
  1042. transport_configure_device(&rphy->dev);
  1043. mutex_lock(&sas_host->lock);
  1044. list_add_tail(&rphy->list, &sas_host->rphy_list);
  1045. if (identify->device_type == SAS_END_DEVICE &&
  1046. (identify->target_port_protocols &
  1047. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  1048. rphy->scsi_target_id = sas_host->next_target_id++;
  1049. else if (identify->device_type == SAS_END_DEVICE)
  1050. rphy->scsi_target_id = -1;
  1051. mutex_unlock(&sas_host->lock);
  1052. if (identify->device_type == SAS_END_DEVICE &&
  1053. rphy->scsi_target_id != -1) {
  1054. scsi_scan_target(&rphy->dev, 0,
  1055. rphy->scsi_target_id, ~0, 0);
  1056. }
  1057. return 0;
  1058. }
  1059. EXPORT_SYMBOL(sas_rphy_add);
  1060. /**
  1061. * sas_rphy_free -- free a SAS remote PHY
  1062. * @rphy SAS remote PHY to free
  1063. *
  1064. * Frees the specified SAS remote PHY.
  1065. *
  1066. * Note:
  1067. * This function must only be called on a remote
  1068. * PHY that has not sucessfully been added using
  1069. * sas_rphy_add().
  1070. */
  1071. void sas_rphy_free(struct sas_rphy *rphy)
  1072. {
  1073. struct device *dev = &rphy->dev;
  1074. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  1075. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1076. mutex_lock(&sas_host->lock);
  1077. list_del(&rphy->list);
  1078. mutex_unlock(&sas_host->lock);
  1079. transport_destroy_device(dev);
  1080. put_device(dev);
  1081. }
  1082. EXPORT_SYMBOL(sas_rphy_free);
  1083. /**
  1084. * sas_rphy_delete -- remove SAS remote PHY
  1085. * @rphy: SAS remote PHY to remove
  1086. *
  1087. * Removes the specified SAS remote PHY.
  1088. */
  1089. void
  1090. sas_rphy_delete(struct sas_rphy *rphy)
  1091. {
  1092. struct device *dev = &rphy->dev;
  1093. struct sas_port *parent = dev_to_sas_port(dev->parent);
  1094. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  1095. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1096. switch (rphy->identify.device_type) {
  1097. case SAS_END_DEVICE:
  1098. scsi_remove_target(dev);
  1099. break;
  1100. case SAS_EDGE_EXPANDER_DEVICE:
  1101. case SAS_FANOUT_EXPANDER_DEVICE:
  1102. sas_remove_children(dev);
  1103. break;
  1104. default:
  1105. break;
  1106. }
  1107. transport_remove_device(dev);
  1108. device_del(dev);
  1109. transport_destroy_device(dev);
  1110. mutex_lock(&sas_host->lock);
  1111. list_del(&rphy->list);
  1112. mutex_unlock(&sas_host->lock);
  1113. parent->rphy = NULL;
  1114. put_device(dev);
  1115. }
  1116. EXPORT_SYMBOL(sas_rphy_delete);
  1117. /**
  1118. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  1119. * @dev: device to check
  1120. *
  1121. * Returns:
  1122. * %1 if the device represents a SAS remote PHY, %0 else
  1123. */
  1124. int scsi_is_sas_rphy(const struct device *dev)
  1125. {
  1126. return dev->release == sas_end_device_release ||
  1127. dev->release == sas_expander_release;
  1128. }
  1129. EXPORT_SYMBOL(scsi_is_sas_rphy);
  1130. /*
  1131. * SCSI scan helper
  1132. */
  1133. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  1134. uint id, uint lun)
  1135. {
  1136. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1137. struct sas_rphy *rphy;
  1138. mutex_lock(&sas_host->lock);
  1139. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  1140. if (rphy->identify.device_type != SAS_END_DEVICE ||
  1141. rphy->scsi_target_id == -1)
  1142. continue;
  1143. if ((channel == SCAN_WILD_CARD || channel == 0) &&
  1144. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  1145. scsi_scan_target(&rphy->dev, 0,
  1146. rphy->scsi_target_id, lun, 1);
  1147. }
  1148. }
  1149. mutex_unlock(&sas_host->lock);
  1150. return 0;
  1151. }
  1152. /*
  1153. * Setup / Teardown code
  1154. */
  1155. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  1156. i->private_##attrb[count] = class_device_attr_##field; \
  1157. i->private_##attrb[count].attr.mode = perm; \
  1158. i->attrb[count] = &i->private_##attrb[count]; \
  1159. if (test) \
  1160. count++
  1161. #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
  1162. i->private_##attrb[count] = class_device_attr_##field; \
  1163. i->private_##attrb[count].attr.mode = perm; \
  1164. if (ro_test) { \
  1165. i->private_##attrb[count].attr.mode = ro_perm; \
  1166. i->private_##attrb[count].store = NULL; \
  1167. } \
  1168. i->attrb[count] = &i->private_##attrb[count]; \
  1169. if (test) \
  1170. count++
  1171. #define SETUP_RPORT_ATTRIBUTE(field) \
  1172. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
  1173. #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
  1174. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
  1175. #define SETUP_PHY_ATTRIBUTE(field) \
  1176. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
  1177. #define SETUP_PHY_ATTRIBUTE_RW(field) \
  1178. SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
  1179. !i->f->set_phy_speed, S_IRUGO)
  1180. #define SETUP_PORT_ATTRIBUTE(field) \
  1181. SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  1182. #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
  1183. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
  1184. #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
  1185. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
  1186. #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
  1187. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
  1188. #define SETUP_END_DEV_ATTRIBUTE(field) \
  1189. SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
  1190. #define SETUP_EXPANDER_ATTRIBUTE(field) \
  1191. SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
  1192. /**
  1193. * sas_attach_transport -- instantiate SAS transport template
  1194. * @ft: SAS transport class function template
  1195. */
  1196. struct scsi_transport_template *
  1197. sas_attach_transport(struct sas_function_template *ft)
  1198. {
  1199. struct sas_internal *i;
  1200. int count;
  1201. i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
  1202. if (!i)
  1203. return NULL;
  1204. i->t.user_scan = sas_user_scan;
  1205. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1206. i->t.host_attrs.ac.class = &sas_host_class.class;
  1207. i->t.host_attrs.ac.match = sas_host_match;
  1208. transport_container_register(&i->t.host_attrs);
  1209. i->t.host_size = sizeof(struct sas_host_attrs);
  1210. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  1211. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  1212. i->phy_attr_cont.ac.match = sas_phy_match;
  1213. transport_container_register(&i->phy_attr_cont);
  1214. i->port_attr_cont.ac.class = &sas_port_class.class;
  1215. i->port_attr_cont.ac.attrs = &i->port_attrs[0];
  1216. i->port_attr_cont.ac.match = sas_port_match;
  1217. transport_container_register(&i->port_attr_cont);
  1218. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  1219. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  1220. i->rphy_attr_cont.ac.match = sas_rphy_match;
  1221. transport_container_register(&i->rphy_attr_cont);
  1222. i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
  1223. i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
  1224. i->end_dev_attr_cont.ac.match = sas_end_dev_match;
  1225. transport_container_register(&i->end_dev_attr_cont);
  1226. i->expander_attr_cont.ac.class = &sas_expander_class.class;
  1227. i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
  1228. i->expander_attr_cont.ac.match = sas_expander_match;
  1229. transport_container_register(&i->expander_attr_cont);
  1230. i->f = ft;
  1231. count = 0;
  1232. SETUP_PORT_ATTRIBUTE(num_phys);
  1233. i->host_attrs[count] = NULL;
  1234. count = 0;
  1235. SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
  1236. SETUP_PHY_ATTRIBUTE(target_port_protocols);
  1237. SETUP_PHY_ATTRIBUTE(device_type);
  1238. SETUP_PHY_ATTRIBUTE(sas_address);
  1239. SETUP_PHY_ATTRIBUTE(phy_identifier);
  1240. //SETUP_PHY_ATTRIBUTE(port_identifier);
  1241. SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
  1242. SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
  1243. SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
  1244. SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
  1245. SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
  1246. SETUP_PHY_ATTRIBUTE(invalid_dword_count);
  1247. SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
  1248. SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
  1249. SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
  1250. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
  1251. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
  1252. i->phy_attrs[count] = NULL;
  1253. count = 0;
  1254. SETUP_PORT_ATTRIBUTE(num_phys);
  1255. i->port_attrs[count] = NULL;
  1256. count = 0;
  1257. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  1258. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  1259. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  1260. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  1261. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  1262. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
  1263. get_enclosure_identifier);
  1264. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
  1265. get_bay_identifier);
  1266. i->rphy_attrs[count] = NULL;
  1267. count = 0;
  1268. SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
  1269. SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
  1270. SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
  1271. i->end_dev_attrs[count] = NULL;
  1272. count = 0;
  1273. SETUP_EXPANDER_ATTRIBUTE(vendor_id);
  1274. SETUP_EXPANDER_ATTRIBUTE(product_id);
  1275. SETUP_EXPANDER_ATTRIBUTE(product_rev);
  1276. SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
  1277. SETUP_EXPANDER_ATTRIBUTE(component_id);
  1278. SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
  1279. SETUP_EXPANDER_ATTRIBUTE(level);
  1280. i->expander_attrs[count] = NULL;
  1281. return &i->t;
  1282. }
  1283. EXPORT_SYMBOL(sas_attach_transport);
  1284. /**
  1285. * sas_release_transport -- release SAS transport template instance
  1286. * @t: transport template instance
  1287. */
  1288. void sas_release_transport(struct scsi_transport_template *t)
  1289. {
  1290. struct sas_internal *i = to_sas_internal(t);
  1291. transport_container_unregister(&i->t.host_attrs);
  1292. transport_container_unregister(&i->phy_attr_cont);
  1293. transport_container_unregister(&i->port_attr_cont);
  1294. transport_container_unregister(&i->rphy_attr_cont);
  1295. transport_container_unregister(&i->end_dev_attr_cont);
  1296. transport_container_unregister(&i->expander_attr_cont);
  1297. kfree(i);
  1298. }
  1299. EXPORT_SYMBOL(sas_release_transport);
  1300. static __init int sas_transport_init(void)
  1301. {
  1302. int error;
  1303. error = transport_class_register(&sas_host_class);
  1304. if (error)
  1305. goto out;
  1306. error = transport_class_register(&sas_phy_class);
  1307. if (error)
  1308. goto out_unregister_transport;
  1309. error = transport_class_register(&sas_port_class);
  1310. if (error)
  1311. goto out_unregister_phy;
  1312. error = transport_class_register(&sas_rphy_class);
  1313. if (error)
  1314. goto out_unregister_port;
  1315. error = transport_class_register(&sas_end_dev_class);
  1316. if (error)
  1317. goto out_unregister_rphy;
  1318. error = transport_class_register(&sas_expander_class);
  1319. if (error)
  1320. goto out_unregister_end_dev;
  1321. return 0;
  1322. out_unregister_end_dev:
  1323. transport_class_unregister(&sas_end_dev_class);
  1324. out_unregister_rphy:
  1325. transport_class_unregister(&sas_rphy_class);
  1326. out_unregister_port:
  1327. transport_class_unregister(&sas_port_class);
  1328. out_unregister_phy:
  1329. transport_class_unregister(&sas_phy_class);
  1330. out_unregister_transport:
  1331. transport_class_unregister(&sas_host_class);
  1332. out:
  1333. return error;
  1334. }
  1335. static void __exit sas_transport_exit(void)
  1336. {
  1337. transport_class_unregister(&sas_host_class);
  1338. transport_class_unregister(&sas_phy_class);
  1339. transport_class_unregister(&sas_port_class);
  1340. transport_class_unregister(&sas_rphy_class);
  1341. transport_class_unregister(&sas_end_dev_class);
  1342. transport_class_unregister(&sas_expander_class);
  1343. }
  1344. MODULE_AUTHOR("Christoph Hellwig");
  1345. MODULE_DESCRIPTION("SAS Transphy Attributes");
  1346. MODULE_LICENSE("GPL");
  1347. module_init(sas_transport_init);
  1348. module_exit(sas_transport_exit);