scsi_transport_sas.c 52 KB

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