scsi_transport_sas.c 45 KB

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