scsi_transport_sas.c 44 KB

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