scsi_transport_sas.c 45 KB

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