scsi_transport_sas.c 51 KB

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