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