scsi_transport_sas.c 52 KB

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