scsi_transport_sas.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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 managment
  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/err.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. #include "scsi_sas_internal.h"
  36. struct sas_host_attrs {
  37. struct list_head rphy_list;
  38. struct mutex lock;
  39. u32 next_target_id;
  40. u32 next_expander_id;
  41. int next_port_id;
  42. };
  43. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  44. /*
  45. * Hack to allow attributes of the same name in different objects.
  46. */
  47. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  48. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  49. __ATTR(_name,_mode,_show,_store)
  50. /*
  51. * Pretty printing helpers
  52. */
  53. #define sas_bitfield_name_match(title, table) \
  54. static ssize_t \
  55. get_sas_##title##_names(u32 table_key, char *buf) \
  56. { \
  57. char *prefix = ""; \
  58. ssize_t len = 0; \
  59. int i; \
  60. \
  61. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  62. if (table[i].value & table_key) { \
  63. len += sprintf(buf + len, "%s%s", \
  64. prefix, table[i].name); \
  65. prefix = ", "; \
  66. } \
  67. } \
  68. len += sprintf(buf + len, "\n"); \
  69. return len; \
  70. }
  71. #define sas_bitfield_name_search(title, table) \
  72. static ssize_t \
  73. get_sas_##title##_names(u32 table_key, char *buf) \
  74. { \
  75. ssize_t len = 0; \
  76. int i; \
  77. \
  78. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  79. if (table[i].value == table_key) { \
  80. len += sprintf(buf + len, "%s", \
  81. table[i].name); \
  82. break; \
  83. } \
  84. } \
  85. len += sprintf(buf + len, "\n"); \
  86. return len; \
  87. }
  88. static struct {
  89. u32 value;
  90. char *name;
  91. } sas_device_type_names[] = {
  92. { SAS_PHY_UNUSED, "unused" },
  93. { SAS_END_DEVICE, "end device" },
  94. { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
  95. { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
  96. };
  97. sas_bitfield_name_search(device_type, sas_device_type_names)
  98. static struct {
  99. u32 value;
  100. char *name;
  101. } sas_protocol_names[] = {
  102. { SAS_PROTOCOL_SATA, "sata" },
  103. { SAS_PROTOCOL_SMP, "smp" },
  104. { SAS_PROTOCOL_STP, "stp" },
  105. { SAS_PROTOCOL_SSP, "ssp" },
  106. };
  107. sas_bitfield_name_match(protocol, sas_protocol_names)
  108. static struct {
  109. u32 value;
  110. char *name;
  111. } sas_linkspeed_names[] = {
  112. { SAS_LINK_RATE_UNKNOWN, "Unknown" },
  113. { SAS_PHY_DISABLED, "Phy disabled" },
  114. { SAS_LINK_RATE_FAILED, "Link Rate failed" },
  115. { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
  116. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  117. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  118. { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
  119. };
  120. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  121. /*
  122. * SAS host attributes
  123. */
  124. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  125. struct class_device *cdev)
  126. {
  127. struct Scsi_Host *shost = dev_to_shost(dev);
  128. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  129. INIT_LIST_HEAD(&sas_host->rphy_list);
  130. mutex_init(&sas_host->lock);
  131. sas_host->next_target_id = 0;
  132. sas_host->next_expander_id = 0;
  133. sas_host->next_port_id = 0;
  134. return 0;
  135. }
  136. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  137. "sas_host", sas_host_setup, NULL, NULL);
  138. static int sas_host_match(struct attribute_container *cont,
  139. struct device *dev)
  140. {
  141. struct Scsi_Host *shost;
  142. struct sas_internal *i;
  143. if (!scsi_is_host_device(dev))
  144. return 0;
  145. shost = dev_to_shost(dev);
  146. if (!shost->transportt)
  147. return 0;
  148. if (shost->transportt->host_attrs.ac.class !=
  149. &sas_host_class.class)
  150. return 0;
  151. i = to_sas_internal(shost->transportt);
  152. return &i->t.host_attrs.ac == cont;
  153. }
  154. static int do_sas_phy_delete(struct device *dev, void *data)
  155. {
  156. int pass = (int)(unsigned long)data;
  157. if (pass == 0 && scsi_is_sas_port(dev))
  158. sas_port_delete(dev_to_sas_port(dev));
  159. else if (pass == 1 && scsi_is_sas_phy(dev))
  160. sas_phy_delete(dev_to_phy(dev));
  161. return 0;
  162. }
  163. /**
  164. * sas_remove_children -- tear down a devices SAS data structures
  165. * @dev: device belonging to the sas object
  166. *
  167. * Removes all SAS PHYs and remote PHYs for a given object
  168. */
  169. void sas_remove_children(struct device *dev)
  170. {
  171. device_for_each_child(dev, (void *)0, do_sas_phy_delete);
  172. device_for_each_child(dev, (void *)1, do_sas_phy_delete);
  173. }
  174. EXPORT_SYMBOL(sas_remove_children);
  175. /**
  176. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  177. * @shost: Scsi Host that is torn down
  178. *
  179. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  180. * Must be called just before scsi_remove_host for SAS HBAs.
  181. */
  182. void sas_remove_host(struct Scsi_Host *shost)
  183. {
  184. sas_remove_children(&shost->shost_gendev);
  185. }
  186. EXPORT_SYMBOL(sas_remove_host);
  187. /*
  188. * SAS Phy attributes
  189. */
  190. #define sas_phy_show_simple(field, name, format_string, cast) \
  191. static ssize_t \
  192. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  193. { \
  194. struct sas_phy *phy = transport_class_to_phy(cdev); \
  195. \
  196. return snprintf(buf, 20, format_string, cast phy->field); \
  197. }
  198. #define sas_phy_simple_attr(field, name, format_string, type) \
  199. sas_phy_show_simple(field, name, format_string, (type)) \
  200. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  201. #define sas_phy_show_protocol(field, name) \
  202. static ssize_t \
  203. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  204. { \
  205. struct sas_phy *phy = transport_class_to_phy(cdev); \
  206. \
  207. if (!phy->field) \
  208. return snprintf(buf, 20, "none\n"); \
  209. return get_sas_protocol_names(phy->field, buf); \
  210. }
  211. #define sas_phy_protocol_attr(field, name) \
  212. sas_phy_show_protocol(field, name) \
  213. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  214. #define sas_phy_show_linkspeed(field) \
  215. static ssize_t \
  216. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  217. { \
  218. struct sas_phy *phy = transport_class_to_phy(cdev); \
  219. \
  220. return get_sas_linkspeed_names(phy->field, buf); \
  221. }
  222. #define sas_phy_linkspeed_attr(field) \
  223. sas_phy_show_linkspeed(field) \
  224. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  225. #define sas_phy_show_linkerror(field) \
  226. static ssize_t \
  227. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  228. { \
  229. struct sas_phy *phy = transport_class_to_phy(cdev); \
  230. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  231. struct sas_internal *i = to_sas_internal(shost->transportt); \
  232. int error; \
  233. \
  234. if (!phy->local_attached) \
  235. return -EINVAL; \
  236. \
  237. error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
  238. if (error) \
  239. return error; \
  240. return snprintf(buf, 20, "%u\n", phy->field); \
  241. }
  242. #define sas_phy_linkerror_attr(field) \
  243. sas_phy_show_linkerror(field) \
  244. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  245. static ssize_t
  246. show_sas_device_type(struct class_device *cdev, char *buf)
  247. {
  248. struct sas_phy *phy = transport_class_to_phy(cdev);
  249. if (!phy->identify.device_type)
  250. return snprintf(buf, 20, "none\n");
  251. return get_sas_device_type_names(phy->identify.device_type, buf);
  252. }
  253. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  254. static ssize_t do_sas_phy_reset(struct class_device *cdev,
  255. size_t count, int hard_reset)
  256. {
  257. struct sas_phy *phy = transport_class_to_phy(cdev);
  258. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  259. struct sas_internal *i = to_sas_internal(shost->transportt);
  260. int error;
  261. if (!phy->local_attached)
  262. return -EINVAL;
  263. error = i->f->phy_reset(phy, hard_reset);
  264. if (error)
  265. return error;
  266. return count;
  267. };
  268. static ssize_t store_sas_link_reset(struct class_device *cdev,
  269. const char *buf, size_t count)
  270. {
  271. return do_sas_phy_reset(cdev, count, 0);
  272. }
  273. static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  274. static ssize_t store_sas_hard_reset(struct class_device *cdev,
  275. const char *buf, size_t count)
  276. {
  277. return do_sas_phy_reset(cdev, count, 1);
  278. }
  279. static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  280. sas_phy_protocol_attr(identify.initiator_port_protocols,
  281. initiator_port_protocols);
  282. sas_phy_protocol_attr(identify.target_port_protocols,
  283. target_port_protocols);
  284. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  285. unsigned long long);
  286. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  287. //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
  288. sas_phy_linkspeed_attr(negotiated_linkrate);
  289. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  290. sas_phy_linkspeed_attr(minimum_linkrate);
  291. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  292. sas_phy_linkspeed_attr(maximum_linkrate);
  293. sas_phy_linkerror_attr(invalid_dword_count);
  294. sas_phy_linkerror_attr(running_disparity_error_count);
  295. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  296. sas_phy_linkerror_attr(phy_reset_problem_count);
  297. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  298. "sas_phy", NULL, NULL, NULL);
  299. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  300. {
  301. struct Scsi_Host *shost;
  302. struct sas_internal *i;
  303. if (!scsi_is_sas_phy(dev))
  304. return 0;
  305. shost = dev_to_shost(dev->parent);
  306. if (!shost->transportt)
  307. return 0;
  308. if (shost->transportt->host_attrs.ac.class !=
  309. &sas_host_class.class)
  310. return 0;
  311. i = to_sas_internal(shost->transportt);
  312. return &i->phy_attr_cont.ac == cont;
  313. }
  314. static void sas_phy_release(struct device *dev)
  315. {
  316. struct sas_phy *phy = dev_to_phy(dev);
  317. put_device(dev->parent);
  318. kfree(phy);
  319. }
  320. /**
  321. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  322. * @parent: Parent device
  323. * @number: Phy index
  324. *
  325. * Allocates an SAS PHY structure. It will be added in the device tree
  326. * below the device specified by @parent, which has to be either a Scsi_Host
  327. * or sas_rphy.
  328. *
  329. * Returns:
  330. * SAS PHY allocated or %NULL if the allocation failed.
  331. */
  332. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  333. {
  334. struct Scsi_Host *shost = dev_to_shost(parent);
  335. struct sas_phy *phy;
  336. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  337. if (!phy)
  338. return NULL;
  339. phy->number = number;
  340. device_initialize(&phy->dev);
  341. phy->dev.parent = get_device(parent);
  342. phy->dev.release = sas_phy_release;
  343. INIT_LIST_HEAD(&phy->port_siblings);
  344. if (scsi_is_sas_expander_device(parent)) {
  345. struct sas_rphy *rphy = dev_to_rphy(parent);
  346. sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
  347. rphy->scsi_target_id, number);
  348. } else
  349. sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
  350. transport_setup_device(&phy->dev);
  351. return phy;
  352. }
  353. EXPORT_SYMBOL(sas_phy_alloc);
  354. /**
  355. * sas_phy_add -- add a SAS PHY to the device hierachy
  356. * @phy: The PHY to be added
  357. *
  358. * Publishes a SAS PHY to the rest of the system.
  359. */
  360. int sas_phy_add(struct sas_phy *phy)
  361. {
  362. int error;
  363. error = device_add(&phy->dev);
  364. if (!error) {
  365. transport_add_device(&phy->dev);
  366. transport_configure_device(&phy->dev);
  367. }
  368. return error;
  369. }
  370. EXPORT_SYMBOL(sas_phy_add);
  371. /**
  372. * sas_phy_free -- free a SAS PHY
  373. * @phy: SAS PHY to free
  374. *
  375. * Frees the specified SAS PHY.
  376. *
  377. * Note:
  378. * This function must only be called on a PHY that has not
  379. * sucessfully been added using sas_phy_add().
  380. */
  381. void sas_phy_free(struct sas_phy *phy)
  382. {
  383. transport_destroy_device(&phy->dev);
  384. put_device(&phy->dev);
  385. }
  386. EXPORT_SYMBOL(sas_phy_free);
  387. /**
  388. * sas_phy_delete -- remove SAS PHY
  389. * @phy: SAS PHY to remove
  390. *
  391. * Removes the specified SAS PHY. If the SAS PHY has an
  392. * associated remote PHY it is removed before.
  393. */
  394. void
  395. sas_phy_delete(struct sas_phy *phy)
  396. {
  397. struct device *dev = &phy->dev;
  398. /* this happens if the phy is still part of a port when deleted */
  399. BUG_ON(!list_empty(&phy->port_siblings));
  400. transport_remove_device(dev);
  401. device_del(dev);
  402. transport_destroy_device(dev);
  403. put_device(dev);
  404. }
  405. EXPORT_SYMBOL(sas_phy_delete);
  406. /**
  407. * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
  408. * @dev: device to check
  409. *
  410. * Returns:
  411. * %1 if the device represents a SAS PHY, %0 else
  412. */
  413. int scsi_is_sas_phy(const struct device *dev)
  414. {
  415. return dev->release == sas_phy_release;
  416. }
  417. EXPORT_SYMBOL(scsi_is_sas_phy);
  418. /*
  419. * SAS Port attributes
  420. */
  421. #define sas_port_show_simple(field, name, format_string, cast) \
  422. static ssize_t \
  423. show_sas_port_##name(struct class_device *cdev, char *buf) \
  424. { \
  425. struct sas_port *port = transport_class_to_sas_port(cdev); \
  426. \
  427. return snprintf(buf, 20, format_string, cast port->field); \
  428. }
  429. #define sas_port_simple_attr(field, name, format_string, type) \
  430. sas_port_show_simple(field, name, format_string, (type)) \
  431. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
  432. sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
  433. static DECLARE_TRANSPORT_CLASS(sas_port_class,
  434. "sas_port", NULL, NULL, NULL);
  435. static int sas_port_match(struct attribute_container *cont, struct device *dev)
  436. {
  437. struct Scsi_Host *shost;
  438. struct sas_internal *i;
  439. if (!scsi_is_sas_port(dev))
  440. return 0;
  441. shost = dev_to_shost(dev->parent);
  442. if (!shost->transportt)
  443. return 0;
  444. if (shost->transportt->host_attrs.ac.class !=
  445. &sas_host_class.class)
  446. return 0;
  447. i = to_sas_internal(shost->transportt);
  448. return &i->port_attr_cont.ac == cont;
  449. }
  450. static void sas_port_release(struct device *dev)
  451. {
  452. struct sas_port *port = dev_to_sas_port(dev);
  453. BUG_ON(!list_empty(&port->phy_list));
  454. put_device(dev->parent);
  455. kfree(port);
  456. }
  457. static void sas_port_create_link(struct sas_port *port,
  458. struct sas_phy *phy)
  459. {
  460. sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, phy->dev.bus_id);
  461. sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
  462. }
  463. static void sas_port_delete_link(struct sas_port *port,
  464. struct sas_phy *phy)
  465. {
  466. sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
  467. sysfs_remove_link(&phy->dev.kobj, "port");
  468. }
  469. /** sas_port_alloc - allocate and initialize a SAS port structure
  470. *
  471. * @parent: parent device
  472. * @port_id: port number
  473. *
  474. * Allocates a SAS port structure. It will be added to the device tree
  475. * below the device specified by @parent which must be either a Scsi_Host
  476. * or a sas_expander_device.
  477. *
  478. * Returns %NULL on error
  479. */
  480. struct sas_port *sas_port_alloc(struct device *parent, int port_id)
  481. {
  482. struct Scsi_Host *shost = dev_to_shost(parent);
  483. struct sas_port *port;
  484. port = kzalloc(sizeof(*port), GFP_KERNEL);
  485. if (!port)
  486. return NULL;
  487. port->port_identifier = port_id;
  488. device_initialize(&port->dev);
  489. port->dev.parent = get_device(parent);
  490. port->dev.release = sas_port_release;
  491. mutex_init(&port->phy_list_mutex);
  492. INIT_LIST_HEAD(&port->phy_list);
  493. if (scsi_is_sas_expander_device(parent)) {
  494. struct sas_rphy *rphy = dev_to_rphy(parent);
  495. sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
  496. rphy->scsi_target_id, port->port_identifier);
  497. } else
  498. sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
  499. port->port_identifier);
  500. transport_setup_device(&port->dev);
  501. return port;
  502. }
  503. EXPORT_SYMBOL(sas_port_alloc);
  504. /** sas_port_alloc_num - allocate and initialize a SAS port structure
  505. *
  506. * @parent: parent device
  507. *
  508. * Allocates a SAS port structure and a number to go with it. This
  509. * interface is really for adapters where the port number has no
  510. * meansing, so the sas class should manage them. It will be added to
  511. * the device tree below the device specified by @parent which must be
  512. * either a Scsi_Host or a sas_expander_device.
  513. *
  514. * Returns %NULL on error
  515. */
  516. struct sas_port *sas_port_alloc_num(struct device *parent)
  517. {
  518. int index;
  519. struct Scsi_Host *shost = dev_to_shost(parent);
  520. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  521. /* FIXME: use idr for this eventually */
  522. mutex_lock(&sas_host->lock);
  523. if (scsi_is_sas_expander_device(parent)) {
  524. struct sas_rphy *rphy = dev_to_rphy(parent);
  525. struct sas_expander_device *exp = rphy_to_expander_device(rphy);
  526. index = exp->next_port_id++;
  527. } else
  528. index = sas_host->next_port_id++;
  529. mutex_unlock(&sas_host->lock);
  530. return sas_port_alloc(parent, index);
  531. }
  532. EXPORT_SYMBOL(sas_port_alloc_num);
  533. /**
  534. * sas_port_add - add a SAS port to the device hierarchy
  535. *
  536. * @port: port to be added
  537. *
  538. * publishes a port to the rest of the system
  539. */
  540. int sas_port_add(struct sas_port *port)
  541. {
  542. int error;
  543. /* No phys should be added until this is made visible */
  544. BUG_ON(!list_empty(&port->phy_list));
  545. error = device_add(&port->dev);
  546. if (error)
  547. return error;
  548. transport_add_device(&port->dev);
  549. transport_configure_device(&port->dev);
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(sas_port_add);
  553. /**
  554. * sas_port_free -- free a SAS PORT
  555. * @port: SAS PORT to free
  556. *
  557. * Frees the specified SAS PORT.
  558. *
  559. * Note:
  560. * This function must only be called on a PORT that has not
  561. * sucessfully been added using sas_port_add().
  562. */
  563. void sas_port_free(struct sas_port *port)
  564. {
  565. transport_destroy_device(&port->dev);
  566. put_device(&port->dev);
  567. }
  568. EXPORT_SYMBOL(sas_port_free);
  569. /**
  570. * sas_port_delete -- remove SAS PORT
  571. * @port: SAS PORT to remove
  572. *
  573. * Removes the specified SAS PORT. If the SAS PORT has an
  574. * associated phys, unlink them from the port as well.
  575. */
  576. void sas_port_delete(struct sas_port *port)
  577. {
  578. struct device *dev = &port->dev;
  579. struct sas_phy *phy, *tmp_phy;
  580. if (port->rphy) {
  581. sas_rphy_delete(port->rphy);
  582. port->rphy = NULL;
  583. }
  584. mutex_lock(&port->phy_list_mutex);
  585. list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
  586. port_siblings) {
  587. sas_port_delete_link(port, phy);
  588. list_del_init(&phy->port_siblings);
  589. }
  590. mutex_unlock(&port->phy_list_mutex);
  591. if (port->is_backlink) {
  592. struct device *parent = port->dev.parent;
  593. sysfs_remove_link(&port->dev.kobj, parent->bus_id);
  594. port->is_backlink = 0;
  595. }
  596. transport_remove_device(dev);
  597. device_del(dev);
  598. transport_destroy_device(dev);
  599. put_device(dev);
  600. }
  601. EXPORT_SYMBOL(sas_port_delete);
  602. /**
  603. * scsi_is_sas_port -- check if a struct device represents a SAS port
  604. * @dev: device to check
  605. *
  606. * Returns:
  607. * %1 if the device represents a SAS Port, %0 else
  608. */
  609. int scsi_is_sas_port(const struct device *dev)
  610. {
  611. return dev->release == sas_port_release;
  612. }
  613. EXPORT_SYMBOL(scsi_is_sas_port);
  614. /**
  615. * sas_port_add_phy - add another phy to a port to form a wide port
  616. * @port: port to add the phy to
  617. * @phy: phy to add
  618. *
  619. * When a port is initially created, it is empty (has no phys). All
  620. * ports must have at least one phy to operated, and all wide ports
  621. * must have at least two. The current code makes no difference
  622. * between ports and wide ports, but the only object that can be
  623. * connected to a remote device is a port, so ports must be formed on
  624. * all devices with phys if they're connected to anything.
  625. */
  626. void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
  627. {
  628. mutex_lock(&port->phy_list_mutex);
  629. if (unlikely(!list_empty(&phy->port_siblings))) {
  630. /* make sure we're already on this port */
  631. struct sas_phy *tmp;
  632. list_for_each_entry(tmp, &port->phy_list, port_siblings)
  633. if (tmp == phy)
  634. break;
  635. /* If this trips, you added a phy that was already
  636. * part of a different port */
  637. if (unlikely(tmp != phy)) {
  638. dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
  639. BUG();
  640. }
  641. } else {
  642. sas_port_create_link(port, phy);
  643. list_add_tail(&phy->port_siblings, &port->phy_list);
  644. port->num_phys++;
  645. }
  646. mutex_unlock(&port->phy_list_mutex);
  647. }
  648. EXPORT_SYMBOL(sas_port_add_phy);
  649. /**
  650. * sas_port_delete_phy - remove a phy from a port or wide port
  651. * @port: port to remove the phy from
  652. * @phy: phy to remove
  653. *
  654. * This operation is used for tearing down ports again. It must be
  655. * done to every port or wide port before calling sas_port_delete.
  656. */
  657. void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
  658. {
  659. mutex_lock(&port->phy_list_mutex);
  660. sas_port_delete_link(port, phy);
  661. list_del_init(&phy->port_siblings);
  662. port->num_phys--;
  663. mutex_unlock(&port->phy_list_mutex);
  664. }
  665. EXPORT_SYMBOL(sas_port_delete_phy);
  666. void sas_port_mark_backlink(struct sas_port *port)
  667. {
  668. struct device *parent = port->dev.parent->parent->parent;
  669. if (port->is_backlink)
  670. return;
  671. port->is_backlink = 1;
  672. sysfs_create_link(&port->dev.kobj, &parent->kobj,
  673. parent->bus_id);
  674. }
  675. EXPORT_SYMBOL(sas_port_mark_backlink);
  676. /*
  677. * SAS remote PHY attributes.
  678. */
  679. #define sas_rphy_show_simple(field, name, format_string, cast) \
  680. static ssize_t \
  681. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  682. { \
  683. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  684. \
  685. return snprintf(buf, 20, format_string, cast rphy->field); \
  686. }
  687. #define sas_rphy_simple_attr(field, name, format_string, type) \
  688. sas_rphy_show_simple(field, name, format_string, (type)) \
  689. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  690. show_sas_rphy_##name, NULL)
  691. #define sas_rphy_show_protocol(field, name) \
  692. static ssize_t \
  693. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  694. { \
  695. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  696. \
  697. if (!rphy->field) \
  698. return snprintf(buf, 20, "none\n"); \
  699. return get_sas_protocol_names(rphy->field, buf); \
  700. }
  701. #define sas_rphy_protocol_attr(field, name) \
  702. sas_rphy_show_protocol(field, name) \
  703. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  704. show_sas_rphy_##name, NULL)
  705. static ssize_t
  706. show_sas_rphy_device_type(struct class_device *cdev, char *buf)
  707. {
  708. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  709. if (!rphy->identify.device_type)
  710. return snprintf(buf, 20, "none\n");
  711. return get_sas_device_type_names(
  712. rphy->identify.device_type, buf);
  713. }
  714. static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  715. show_sas_rphy_device_type, NULL);
  716. static ssize_t
  717. show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
  718. {
  719. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  720. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  721. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  722. struct sas_internal *i = to_sas_internal(shost->transportt);
  723. u64 identifier;
  724. int error;
  725. /*
  726. * Only devices behind an expander are supported, because the
  727. * enclosure identifier is a SMP feature.
  728. */
  729. if (phy->local_attached)
  730. return -EINVAL;
  731. error = i->f->get_enclosure_identifier(rphy, &identifier);
  732. if (error)
  733. return error;
  734. return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
  735. }
  736. static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
  737. show_sas_rphy_enclosure_identifier, NULL);
  738. static ssize_t
  739. show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
  740. {
  741. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  742. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  743. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  744. struct sas_internal *i = to_sas_internal(shost->transportt);
  745. int val;
  746. if (phy->local_attached)
  747. return -EINVAL;
  748. val = i->f->get_bay_identifier(rphy);
  749. if (val < 0)
  750. return val;
  751. return sprintf(buf, "%d\n", val);
  752. }
  753. static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
  754. show_sas_rphy_bay_identifier, NULL);
  755. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  756. initiator_port_protocols);
  757. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  758. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  759. unsigned long long);
  760. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  761. /* only need 8 bytes of data plus header (4 or 8) */
  762. #define BUF_SIZE 64
  763. int sas_read_port_mode_page(struct scsi_device *sdev)
  764. {
  765. char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
  766. struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
  767. struct sas_end_device *rdev;
  768. struct scsi_mode_data mode_data;
  769. int res, error;
  770. BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
  771. rdev = rphy_to_end_device(rphy);
  772. if (!buffer)
  773. return -ENOMEM;
  774. res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
  775. &mode_data, NULL);
  776. error = -EINVAL;
  777. if (!scsi_status_is_good(res))
  778. goto out;
  779. msdata = buffer + mode_data.header_length +
  780. mode_data.block_descriptor_length;
  781. if (msdata - buffer > BUF_SIZE - 8)
  782. goto out;
  783. error = 0;
  784. rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
  785. rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
  786. rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
  787. out:
  788. kfree(buffer);
  789. return error;
  790. }
  791. EXPORT_SYMBOL(sas_read_port_mode_page);
  792. static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
  793. "sas_end_device", NULL, NULL, NULL);
  794. #define sas_end_dev_show_simple(field, name, format_string, cast) \
  795. static ssize_t \
  796. show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
  797. { \
  798. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  799. struct sas_end_device *rdev = rphy_to_end_device(rphy); \
  800. \
  801. return snprintf(buf, 20, format_string, cast rdev->field); \
  802. }
  803. #define sas_end_dev_simple_attr(field, name, format_string, type) \
  804. sas_end_dev_show_simple(field, name, format_string, (type)) \
  805. static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
  806. show_sas_end_dev_##name, NULL)
  807. sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
  808. sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
  809. "%d\n", int);
  810. sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
  811. "%d\n", int);
  812. static DECLARE_TRANSPORT_CLASS(sas_expander_class,
  813. "sas_expander", NULL, NULL, NULL);
  814. #define sas_expander_show_simple(field, name, format_string, cast) \
  815. static ssize_t \
  816. show_sas_expander_##name(struct class_device *cdev, char *buf) \
  817. { \
  818. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  819. struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
  820. \
  821. return snprintf(buf, 20, format_string, cast edev->field); \
  822. }
  823. #define sas_expander_simple_attr(field, name, format_string, type) \
  824. sas_expander_show_simple(field, name, format_string, (type)) \
  825. static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
  826. show_sas_expander_##name, NULL)
  827. sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
  828. sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
  829. sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
  830. sas_expander_simple_attr(component_vendor_id, component_vendor_id,
  831. "%s\n", char *);
  832. sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
  833. sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
  834. unsigned int);
  835. sas_expander_simple_attr(level, level, "%d\n", int);
  836. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  837. "sas_device", NULL, NULL, NULL);
  838. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  839. {
  840. struct Scsi_Host *shost;
  841. struct sas_internal *i;
  842. if (!scsi_is_sas_rphy(dev))
  843. return 0;
  844. shost = dev_to_shost(dev->parent->parent);
  845. if (!shost->transportt)
  846. return 0;
  847. if (shost->transportt->host_attrs.ac.class !=
  848. &sas_host_class.class)
  849. return 0;
  850. i = to_sas_internal(shost->transportt);
  851. return &i->rphy_attr_cont.ac == cont;
  852. }
  853. static int sas_end_dev_match(struct attribute_container *cont,
  854. struct device *dev)
  855. {
  856. struct Scsi_Host *shost;
  857. struct sas_internal *i;
  858. struct sas_rphy *rphy;
  859. if (!scsi_is_sas_rphy(dev))
  860. return 0;
  861. shost = dev_to_shost(dev->parent->parent);
  862. rphy = dev_to_rphy(dev);
  863. if (!shost->transportt)
  864. return 0;
  865. if (shost->transportt->host_attrs.ac.class !=
  866. &sas_host_class.class)
  867. return 0;
  868. i = to_sas_internal(shost->transportt);
  869. return &i->end_dev_attr_cont.ac == cont &&
  870. rphy->identify.device_type == SAS_END_DEVICE;
  871. }
  872. static int sas_expander_match(struct attribute_container *cont,
  873. struct device *dev)
  874. {
  875. struct Scsi_Host *shost;
  876. struct sas_internal *i;
  877. struct sas_rphy *rphy;
  878. if (!scsi_is_sas_rphy(dev))
  879. return 0;
  880. shost = dev_to_shost(dev->parent->parent);
  881. rphy = dev_to_rphy(dev);
  882. if (!shost->transportt)
  883. return 0;
  884. if (shost->transportt->host_attrs.ac.class !=
  885. &sas_host_class.class)
  886. return 0;
  887. i = to_sas_internal(shost->transportt);
  888. return &i->expander_attr_cont.ac == cont &&
  889. (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  890. rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
  891. }
  892. static void sas_expander_release(struct device *dev)
  893. {
  894. struct sas_rphy *rphy = dev_to_rphy(dev);
  895. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  896. put_device(dev->parent);
  897. kfree(edev);
  898. }
  899. static void sas_end_device_release(struct device *dev)
  900. {
  901. struct sas_rphy *rphy = dev_to_rphy(dev);
  902. struct sas_end_device *edev = rphy_to_end_device(rphy);
  903. put_device(dev->parent);
  904. kfree(edev);
  905. }
  906. /**
  907. * sas_rphy_initialize - common rphy intialization
  908. * @rphy: rphy to initialise
  909. *
  910. * Used by both sas_end_device_alloc() and sas_expander_alloc() to
  911. * initialise the common rphy component of each.
  912. */
  913. static void sas_rphy_initialize(struct sas_rphy *rphy)
  914. {
  915. INIT_LIST_HEAD(&rphy->list);
  916. }
  917. /**
  918. * sas_end_device_alloc - allocate an rphy for an end device
  919. *
  920. * Allocates an SAS remote PHY structure, connected to @parent.
  921. *
  922. * Returns:
  923. * SAS PHY allocated or %NULL if the allocation failed.
  924. */
  925. struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
  926. {
  927. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  928. struct sas_end_device *rdev;
  929. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  930. if (!rdev) {
  931. return NULL;
  932. }
  933. device_initialize(&rdev->rphy.dev);
  934. rdev->rphy.dev.parent = get_device(&parent->dev);
  935. rdev->rphy.dev.release = sas_end_device_release;
  936. if (scsi_is_sas_expander_device(parent->dev.parent)) {
  937. struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
  938. sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
  939. shost->host_no, rphy->scsi_target_id, parent->port_identifier);
  940. } else
  941. sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
  942. shost->host_no, parent->port_identifier);
  943. rdev->rphy.identify.device_type = SAS_END_DEVICE;
  944. sas_rphy_initialize(&rdev->rphy);
  945. transport_setup_device(&rdev->rphy.dev);
  946. return &rdev->rphy;
  947. }
  948. EXPORT_SYMBOL(sas_end_device_alloc);
  949. /**
  950. * sas_expander_alloc - allocate an rphy for an end device
  951. *
  952. * Allocates an SAS remote PHY structure, connected to @parent.
  953. *
  954. * Returns:
  955. * SAS PHY allocated or %NULL if the allocation failed.
  956. */
  957. struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
  958. enum sas_device_type type)
  959. {
  960. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  961. struct sas_expander_device *rdev;
  962. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  963. BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
  964. type != SAS_FANOUT_EXPANDER_DEVICE);
  965. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  966. if (!rdev) {
  967. return NULL;
  968. }
  969. device_initialize(&rdev->rphy.dev);
  970. rdev->rphy.dev.parent = get_device(&parent->dev);
  971. rdev->rphy.dev.release = sas_expander_release;
  972. mutex_lock(&sas_host->lock);
  973. rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
  974. mutex_unlock(&sas_host->lock);
  975. sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
  976. shost->host_no, rdev->rphy.scsi_target_id);
  977. rdev->rphy.identify.device_type = type;
  978. sas_rphy_initialize(&rdev->rphy);
  979. transport_setup_device(&rdev->rphy.dev);
  980. return &rdev->rphy;
  981. }
  982. EXPORT_SYMBOL(sas_expander_alloc);
  983. /**
  984. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  985. * @rphy: The remote PHY to be added
  986. *
  987. * Publishes a SAS remote PHY to the rest of the system.
  988. */
  989. int sas_rphy_add(struct sas_rphy *rphy)
  990. {
  991. struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
  992. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  993. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  994. struct sas_identify *identify = &rphy->identify;
  995. int error;
  996. if (parent->rphy)
  997. return -ENXIO;
  998. parent->rphy = rphy;
  999. error = device_add(&rphy->dev);
  1000. if (error)
  1001. return error;
  1002. transport_add_device(&rphy->dev);
  1003. transport_configure_device(&rphy->dev);
  1004. mutex_lock(&sas_host->lock);
  1005. list_add_tail(&rphy->list, &sas_host->rphy_list);
  1006. if (identify->device_type == SAS_END_DEVICE &&
  1007. (identify->target_port_protocols &
  1008. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  1009. rphy->scsi_target_id = sas_host->next_target_id++;
  1010. else if (identify->device_type == SAS_END_DEVICE)
  1011. rphy->scsi_target_id = -1;
  1012. mutex_unlock(&sas_host->lock);
  1013. if (identify->device_type == SAS_END_DEVICE &&
  1014. rphy->scsi_target_id != -1) {
  1015. scsi_scan_target(&rphy->dev, 0,
  1016. rphy->scsi_target_id, ~0, 0);
  1017. }
  1018. return 0;
  1019. }
  1020. EXPORT_SYMBOL(sas_rphy_add);
  1021. /**
  1022. * sas_rphy_free -- free a SAS remote PHY
  1023. * @rphy SAS remote PHY to free
  1024. *
  1025. * Frees the specified SAS remote PHY.
  1026. *
  1027. * Note:
  1028. * This function must only be called on a remote
  1029. * PHY that has not sucessfully been added using
  1030. * sas_rphy_add().
  1031. */
  1032. void sas_rphy_free(struct sas_rphy *rphy)
  1033. {
  1034. struct device *dev = &rphy->dev;
  1035. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  1036. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1037. mutex_lock(&sas_host->lock);
  1038. list_del(&rphy->list);
  1039. mutex_unlock(&sas_host->lock);
  1040. transport_destroy_device(dev);
  1041. put_device(dev);
  1042. }
  1043. EXPORT_SYMBOL(sas_rphy_free);
  1044. /**
  1045. * sas_rphy_delete -- remove SAS remote PHY
  1046. * @rphy: SAS remote PHY to remove
  1047. *
  1048. * Removes the specified SAS remote PHY.
  1049. */
  1050. void
  1051. sas_rphy_delete(struct sas_rphy *rphy)
  1052. {
  1053. struct device *dev = &rphy->dev;
  1054. struct sas_port *parent = dev_to_sas_port(dev->parent);
  1055. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  1056. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1057. switch (rphy->identify.device_type) {
  1058. case SAS_END_DEVICE:
  1059. scsi_remove_target(dev);
  1060. break;
  1061. case SAS_EDGE_EXPANDER_DEVICE:
  1062. case SAS_FANOUT_EXPANDER_DEVICE:
  1063. sas_remove_children(dev);
  1064. break;
  1065. default:
  1066. break;
  1067. }
  1068. transport_remove_device(dev);
  1069. device_del(dev);
  1070. transport_destroy_device(dev);
  1071. mutex_lock(&sas_host->lock);
  1072. list_del(&rphy->list);
  1073. mutex_unlock(&sas_host->lock);
  1074. parent->rphy = NULL;
  1075. put_device(dev);
  1076. }
  1077. EXPORT_SYMBOL(sas_rphy_delete);
  1078. /**
  1079. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  1080. * @dev: device to check
  1081. *
  1082. * Returns:
  1083. * %1 if the device represents a SAS remote PHY, %0 else
  1084. */
  1085. int scsi_is_sas_rphy(const struct device *dev)
  1086. {
  1087. return dev->release == sas_end_device_release ||
  1088. dev->release == sas_expander_release;
  1089. }
  1090. EXPORT_SYMBOL(scsi_is_sas_rphy);
  1091. /*
  1092. * SCSI scan helper
  1093. */
  1094. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  1095. uint id, uint lun)
  1096. {
  1097. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1098. struct sas_rphy *rphy;
  1099. mutex_lock(&sas_host->lock);
  1100. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  1101. if (rphy->identify.device_type != SAS_END_DEVICE ||
  1102. rphy->scsi_target_id == -1)
  1103. continue;
  1104. if ((channel == SCAN_WILD_CARD || channel == 0) &&
  1105. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  1106. scsi_scan_target(&rphy->dev, 0,
  1107. rphy->scsi_target_id, lun, 1);
  1108. }
  1109. }
  1110. mutex_unlock(&sas_host->lock);
  1111. return 0;
  1112. }
  1113. /*
  1114. * Setup / Teardown code
  1115. */
  1116. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  1117. i->private_##attrb[count] = class_device_attr_##field; \
  1118. i->private_##attrb[count].attr.mode = perm; \
  1119. i->attrb[count] = &i->private_##attrb[count]; \
  1120. if (test) \
  1121. count++
  1122. #define SETUP_RPORT_ATTRIBUTE(field) \
  1123. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
  1124. #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
  1125. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
  1126. #define SETUP_PHY_ATTRIBUTE(field) \
  1127. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
  1128. #define SETUP_PORT_ATTRIBUTE(field) \
  1129. SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  1130. #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
  1131. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
  1132. #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
  1133. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
  1134. #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
  1135. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
  1136. #define SETUP_END_DEV_ATTRIBUTE(field) \
  1137. SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
  1138. #define SETUP_EXPANDER_ATTRIBUTE(field) \
  1139. SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
  1140. /**
  1141. * sas_attach_transport -- instantiate SAS transport template
  1142. * @ft: SAS transport class function template
  1143. */
  1144. struct scsi_transport_template *
  1145. sas_attach_transport(struct sas_function_template *ft)
  1146. {
  1147. struct sas_internal *i;
  1148. int count;
  1149. i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
  1150. if (!i)
  1151. return NULL;
  1152. i->t.user_scan = sas_user_scan;
  1153. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1154. i->t.host_attrs.ac.class = &sas_host_class.class;
  1155. i->t.host_attrs.ac.match = sas_host_match;
  1156. transport_container_register(&i->t.host_attrs);
  1157. i->t.host_size = sizeof(struct sas_host_attrs);
  1158. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  1159. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  1160. i->phy_attr_cont.ac.match = sas_phy_match;
  1161. transport_container_register(&i->phy_attr_cont);
  1162. i->port_attr_cont.ac.class = &sas_port_class.class;
  1163. i->port_attr_cont.ac.attrs = &i->port_attrs[0];
  1164. i->port_attr_cont.ac.match = sas_port_match;
  1165. transport_container_register(&i->port_attr_cont);
  1166. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  1167. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  1168. i->rphy_attr_cont.ac.match = sas_rphy_match;
  1169. transport_container_register(&i->rphy_attr_cont);
  1170. i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
  1171. i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
  1172. i->end_dev_attr_cont.ac.match = sas_end_dev_match;
  1173. transport_container_register(&i->end_dev_attr_cont);
  1174. i->expander_attr_cont.ac.class = &sas_expander_class.class;
  1175. i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
  1176. i->expander_attr_cont.ac.match = sas_expander_match;
  1177. transport_container_register(&i->expander_attr_cont);
  1178. i->f = ft;
  1179. count = 0;
  1180. SETUP_PORT_ATTRIBUTE(num_phys);
  1181. i->host_attrs[count] = NULL;
  1182. count = 0;
  1183. SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
  1184. SETUP_PHY_ATTRIBUTE(target_port_protocols);
  1185. SETUP_PHY_ATTRIBUTE(device_type);
  1186. SETUP_PHY_ATTRIBUTE(sas_address);
  1187. SETUP_PHY_ATTRIBUTE(phy_identifier);
  1188. //SETUP_PHY_ATTRIBUTE(port_identifier);
  1189. SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
  1190. SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
  1191. SETUP_PHY_ATTRIBUTE(minimum_linkrate);
  1192. SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
  1193. SETUP_PHY_ATTRIBUTE(maximum_linkrate);
  1194. SETUP_PHY_ATTRIBUTE(invalid_dword_count);
  1195. SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
  1196. SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
  1197. SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
  1198. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
  1199. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
  1200. i->phy_attrs[count] = NULL;
  1201. count = 0;
  1202. SETUP_PORT_ATTRIBUTE(num_phys);
  1203. i->port_attrs[count] = NULL;
  1204. count = 0;
  1205. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  1206. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  1207. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  1208. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  1209. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  1210. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
  1211. get_enclosure_identifier);
  1212. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
  1213. get_bay_identifier);
  1214. i->rphy_attrs[count] = NULL;
  1215. count = 0;
  1216. SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
  1217. SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
  1218. SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
  1219. i->end_dev_attrs[count] = NULL;
  1220. count = 0;
  1221. SETUP_EXPANDER_ATTRIBUTE(vendor_id);
  1222. SETUP_EXPANDER_ATTRIBUTE(product_id);
  1223. SETUP_EXPANDER_ATTRIBUTE(product_rev);
  1224. SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
  1225. SETUP_EXPANDER_ATTRIBUTE(component_id);
  1226. SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
  1227. SETUP_EXPANDER_ATTRIBUTE(level);
  1228. i->expander_attrs[count] = NULL;
  1229. return &i->t;
  1230. }
  1231. EXPORT_SYMBOL(sas_attach_transport);
  1232. /**
  1233. * sas_release_transport -- release SAS transport template instance
  1234. * @t: transport template instance
  1235. */
  1236. void sas_release_transport(struct scsi_transport_template *t)
  1237. {
  1238. struct sas_internal *i = to_sas_internal(t);
  1239. transport_container_unregister(&i->t.host_attrs);
  1240. transport_container_unregister(&i->phy_attr_cont);
  1241. transport_container_unregister(&i->port_attr_cont);
  1242. transport_container_unregister(&i->rphy_attr_cont);
  1243. transport_container_unregister(&i->end_dev_attr_cont);
  1244. transport_container_unregister(&i->expander_attr_cont);
  1245. kfree(i);
  1246. }
  1247. EXPORT_SYMBOL(sas_release_transport);
  1248. static __init int sas_transport_init(void)
  1249. {
  1250. int error;
  1251. error = transport_class_register(&sas_host_class);
  1252. if (error)
  1253. goto out;
  1254. error = transport_class_register(&sas_phy_class);
  1255. if (error)
  1256. goto out_unregister_transport;
  1257. error = transport_class_register(&sas_port_class);
  1258. if (error)
  1259. goto out_unregister_phy;
  1260. error = transport_class_register(&sas_rphy_class);
  1261. if (error)
  1262. goto out_unregister_port;
  1263. error = transport_class_register(&sas_end_dev_class);
  1264. if (error)
  1265. goto out_unregister_rphy;
  1266. error = transport_class_register(&sas_expander_class);
  1267. if (error)
  1268. goto out_unregister_end_dev;
  1269. return 0;
  1270. out_unregister_end_dev:
  1271. transport_class_unregister(&sas_end_dev_class);
  1272. out_unregister_rphy:
  1273. transport_class_unregister(&sas_rphy_class);
  1274. out_unregister_port:
  1275. transport_class_unregister(&sas_port_class);
  1276. out_unregister_phy:
  1277. transport_class_unregister(&sas_phy_class);
  1278. out_unregister_transport:
  1279. transport_class_unregister(&sas_host_class);
  1280. out:
  1281. return error;
  1282. }
  1283. static void __exit sas_transport_exit(void)
  1284. {
  1285. transport_class_unregister(&sas_host_class);
  1286. transport_class_unregister(&sas_phy_class);
  1287. transport_class_unregister(&sas_port_class);
  1288. transport_class_unregister(&sas_rphy_class);
  1289. transport_class_unregister(&sas_end_dev_class);
  1290. transport_class_unregister(&sas_expander_class);
  1291. }
  1292. MODULE_AUTHOR("Christoph Hellwig");
  1293. MODULE_DESCRIPTION("SAS Transphy Attributes");
  1294. MODULE_LICENSE("GPL");
  1295. module_init(sas_transport_init);
  1296. module_exit(sas_transport_exit);