scsi_transport_sas.c 51 KB

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