scsi_transport_sas.c 51 KB

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