scsi_transport_sas.c 52 KB

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