libsas.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =========
  3. SAS Layer
  4. =========
  5. The SAS Layer is a management infrastructure which manages
  6. SAS LLDDs. It sits between SCSI Core and SAS LLDDs. The
  7. layout is as follows: while SCSI Core is concerned with
  8. SAM/SPC issues, and a SAS LLDD+sequencer is concerned with
  9. phy/OOB/link management, the SAS layer is concerned with:
  10. * SAS Phy/Port/HA event management (LLDD generates,
  11. SAS Layer processes),
  12. * SAS Port management (creation/destruction),
  13. * SAS Domain discovery and revalidation,
  14. * SAS Domain device management,
  15. * SCSI Host registration/unregistration,
  16. * Device registration with SCSI Core (SAS) or libata
  17. (SATA), and
  18. * Expander management and exporting expander control
  19. to user space.
  20. A SAS LLDD is a PCI device driver. It is concerned with
  21. phy/OOB management, and vendor specific tasks and generates
  22. events to the SAS layer.
  23. The SAS Layer does most SAS tasks as outlined in the SAS 1.1
  24. spec.
  25. The sas_ha_struct describes the SAS LLDD to the SAS layer.
  26. Most of it is used by the SAS Layer but a few fields need to
  27. be initialized by the LLDDs.
  28. After initializing your hardware, from the probe() function
  29. you call sas_register_ha(). It will register your LLDD with
  30. the SCSI subsystem, creating a SCSI host and it will
  31. register your SAS driver with the sysfs SAS tree it creates.
  32. It will then return. Then you enable your phys to actually
  33. start OOB (at which point your driver will start calling the
  34. notify_* event callbacks).
  35. Structure descriptions
  36. ======================
  37. ``struct sas_phy``
  38. ------------------
  39. Normally this is statically embedded to your driver's
  40. phy structure::
  41. struct my_phy {
  42. blah;
  43. struct sas_phy sas_phy;
  44. bleh;
  45. };
  46. And then all the phys are an array of my_phy in your HA
  47. struct (shown below).
  48. Then as you go along and initialize your phys you also
  49. initialize the sas_phy struct, along with your own
  50. phy structure.
  51. In general, the phys are managed by the LLDD and the ports
  52. are managed by the SAS layer. So the phys are initialized
  53. and updated by the LLDD and the ports are initialized and
  54. updated by the SAS layer.
  55. There is a scheme where the LLDD can RW certain fields,
  56. and the SAS layer can only read such ones, and vice versa.
  57. The idea is to avoid unnecessary locking.
  58. enabled
  59. - must be set (0/1)
  60. id
  61. - must be set [0,MAX_PHYS)]
  62. class, proto, type, role, oob_mode, linkrate
  63. - must be set
  64. oob_mode
  65. - you set this when OOB has finished and then notify
  66. the SAS Layer.
  67. sas_addr
  68. - this normally points to an array holding the sas
  69. address of the phy, possibly somewhere in your my_phy
  70. struct.
  71. attached_sas_addr
  72. - set this when you (LLDD) receive an
  73. IDENTIFY frame or a FIS frame, _before_ notifying the SAS
  74. layer. The idea is that sometimes the LLDD may want to fake
  75. or provide a different SAS address on that phy/port and this
  76. allows it to do this. At best you should copy the sas
  77. address from the IDENTIFY frame or maybe generate a SAS
  78. address for SATA directly attached devices. The Discover
  79. process may later change this.
  80. frame_rcvd
  81. - this is where you copy the IDENTIFY/FIS frame
  82. when you get it; you lock, copy, set frame_rcvd_size and
  83. unlock the lock, and then call the event. It is a pointer
  84. since there's no way to know your hw frame size _exactly_,
  85. so you define the actual array in your phy struct and let
  86. this pointer point to it. You copy the frame from your
  87. DMAable memory to that area holding the lock.
  88. sas_prim
  89. - this is where primitives go when they're
  90. received. See sas.h. Grab the lock, set the primitive,
  91. release the lock, notify.
  92. port
  93. - this points to the sas_port if the phy belongs
  94. to a port -- the LLDD only reads this. It points to the
  95. sas_port this phy is part of. Set by the SAS Layer.
  96. ha
  97. - may be set; the SAS layer sets it anyway.
  98. lldd_phy
  99. - you should set this to point to your phy so you
  100. can find your way around faster when the SAS layer calls one
  101. of your callbacks and passes you a phy. If the sas_phy is
  102. embedded you can also use container_of -- whatever you
  103. prefer.
  104. ``struct sas_port``
  105. -------------------
  106. The LLDD doesn't set any fields of this struct -- it only
  107. reads them. They should be self explanatory.
  108. phy_mask is 32 bit, this should be enough for now, as I
  109. haven't heard of a HA having more than 8 phys.
  110. lldd_port
  111. - I haven't found use for that -- maybe other
  112. LLDD who wish to have internal port representation can make
  113. use of this.
  114. ``struct sas_ha_struct``
  115. ------------------------
  116. It normally is statically declared in your own LLDD
  117. structure describing your adapter::
  118. struct my_sas_ha {
  119. blah;
  120. struct sas_ha_struct sas_ha;
  121. struct my_phy phys[MAX_PHYS];
  122. struct sas_port sas_ports[MAX_PHYS]; /* (1) */
  123. bleh;
  124. };
  125. (1) If your LLDD doesn't have its own port representation.
  126. What needs to be initialized (sample function given below).
  127. pcidev
  128. ^^^^^^
  129. sas_addr
  130. - since the SAS layer doesn't want to mess with
  131. memory allocation, etc, this points to statically
  132. allocated array somewhere (say in your host adapter
  133. structure) and holds the SAS address of the host
  134. adapter as given by you or the manufacturer, etc.
  135. sas_port
  136. ^^^^^^^^
  137. sas_phy
  138. - an array of pointers to structures. (see
  139. note above on sas_addr).
  140. These must be set. See more notes below.
  141. num_phys
  142. - the number of phys present in the sas_phy array,
  143. and the number of ports present in the sas_port
  144. array. There can be a maximum num_phys ports (one per
  145. port) so we drop the num_ports, and only use
  146. num_phys.
  147. The event interface::
  148. /* LLDD calls these to notify the class of an event. */
  149. void sas_notify_port_event(struct sas_phy *, enum port_event);
  150. void sas_notify_phy_event(struct sas_phy *, enum phy_event);
  151. void sas_notify_port_event_gfp(struct sas_phy *, enum port_event, gfp_t);
  152. void sas_notify_phy_event_gfp(struct sas_phy *, enum phy_event, gfp_t);
  153. The port notification::
  154. /* The class calls these to notify the LLDD of an event. */
  155. void (*lldd_port_formed)(struct sas_phy *);
  156. void (*lldd_port_deformed)(struct sas_phy *);
  157. If the LLDD wants notification when a port has been formed
  158. or deformed it sets those to a function satisfying the type.
  159. A SAS LLDD should also implement at least one of the Task
  160. Management Functions (TMFs) described in SAM::
  161. /* Task Management Functions. Must be called from process context. */
  162. int (*lldd_abort_task)(struct sas_task *);
  163. int (*lldd_abort_task_set)(struct domain_device *, u8 *lun);
  164. int (*lldd_clear_aca)(struct domain_device *, u8 *lun);
  165. int (*lldd_clear_task_set)(struct domain_device *, u8 *lun);
  166. int (*lldd_I_T_nexus_reset)(struct domain_device *);
  167. int (*lldd_lu_reset)(struct domain_device *, u8 *lun);
  168. int (*lldd_query_task)(struct sas_task *);
  169. For more information please read SAM from T10.org.
  170. Port and Adapter management::
  171. /* Port and Adapter management */
  172. int (*lldd_clear_nexus_port)(struct sas_port *);
  173. int (*lldd_clear_nexus_ha)(struct sas_ha_struct *);
  174. A SAS LLDD should implement at least one of those.
  175. Phy management::
  176. /* Phy management */
  177. int (*lldd_control_phy)(struct sas_phy *, enum phy_func);
  178. lldd_ha
  179. - set this to point to your HA struct. You can also
  180. use container_of if you embedded it as shown above.
  181. A sample initialization and registration function
  182. can look like this (called last thing from probe())
  183. *but* before you enable the phys to do OOB::
  184. static int register_sas_ha(struct my_sas_ha *my_ha)
  185. {
  186. int i;
  187. static struct sas_phy *sas_phys[MAX_PHYS];
  188. static struct sas_port *sas_ports[MAX_PHYS];
  189. my_ha->sas_ha.sas_addr = &my_ha->sas_addr[0];
  190. for (i = 0; i < MAX_PHYS; i++) {
  191. sas_phys[i] = &my_ha->phys[i].sas_phy;
  192. sas_ports[i] = &my_ha->sas_ports[i];
  193. }
  194. my_ha->sas_ha.sas_phy = sas_phys;
  195. my_ha->sas_ha.sas_port = sas_ports;
  196. my_ha->sas_ha.num_phys = MAX_PHYS;
  197. my_ha->sas_ha.lldd_port_formed = my_port_formed;
  198. my_ha->sas_ha.lldd_dev_found = my_dev_found;
  199. my_ha->sas_ha.lldd_dev_gone = my_dev_gone;
  200. my_ha->sas_ha.lldd_execute_task = my_execute_task;
  201. my_ha->sas_ha.lldd_abort_task = my_abort_task;
  202. my_ha->sas_ha.lldd_abort_task_set = my_abort_task_set;
  203. my_ha->sas_ha.lldd_clear_aca = my_clear_aca;
  204. my_ha->sas_ha.lldd_clear_task_set = my_clear_task_set;
  205. my_ha->sas_ha.lldd_I_T_nexus_reset= NULL; (2)
  206. my_ha->sas_ha.lldd_lu_reset = my_lu_reset;
  207. my_ha->sas_ha.lldd_query_task = my_query_task;
  208. my_ha->sas_ha.lldd_clear_nexus_port = my_clear_nexus_port;
  209. my_ha->sas_ha.lldd_clear_nexus_ha = my_clear_nexus_ha;
  210. my_ha->sas_ha.lldd_control_phy = my_control_phy;
  211. return sas_register_ha(&my_ha->sas_ha);
  212. }
  213. (2) SAS 1.1 does not define I_T Nexus Reset TMF.
  214. Events
  215. ======
  216. Events are **the only way** a SAS LLDD notifies the SAS layer
  217. of anything. There is no other method or way a LLDD to tell
  218. the SAS layer of anything happening internally or in the SAS
  219. domain.
  220. Phy events::
  221. PHYE_LOSS_OF_SIGNAL, (C)
  222. PHYE_OOB_DONE,
  223. PHYE_OOB_ERROR, (C)
  224. PHYE_SPINUP_HOLD.
  225. Port events, passed on a _phy_::
  226. PORTE_BYTES_DMAED, (M)
  227. PORTE_BROADCAST_RCVD, (E)
  228. PORTE_LINK_RESET_ERR, (C)
  229. PORTE_TIMER_EVENT, (C)
  230. PORTE_HARD_RESET.
  231. Host Adapter event:
  232. HAE_RESET
  233. A SAS LLDD should be able to generate
  234. - at least one event from group C (choice),
  235. - events marked M (mandatory) are mandatory (only one),
  236. - events marked E (expander) if it wants the SAS layer
  237. to handle domain revalidation (only one such).
  238. - Unmarked events are optional.
  239. Meaning:
  240. HAE_RESET
  241. - when your HA got internal error and was reset.
  242. PORTE_BYTES_DMAED
  243. - on receiving an IDENTIFY/FIS frame
  244. PORTE_BROADCAST_RCVD
  245. - on receiving a primitive
  246. PORTE_LINK_RESET_ERR
  247. - timer expired, loss of signal, loss of DWS, etc. [1]_
  248. PORTE_TIMER_EVENT
  249. - DWS reset timeout timer expired [1]_
  250. PORTE_HARD_RESET
  251. - Hard Reset primitive received.
  252. PHYE_LOSS_OF_SIGNAL
  253. - the device is gone [1]_
  254. PHYE_OOB_DONE
  255. - OOB went fine and oob_mode is valid
  256. PHYE_OOB_ERROR
  257. - Error while doing OOB, the device probably
  258. got disconnected. [1]_
  259. PHYE_SPINUP_HOLD
  260. - SATA is present, COMWAKE not sent.
  261. .. [1] should set/clear the appropriate fields in the phy,
  262. or alternatively call the inlined sas_phy_disconnected()
  263. which is just a helper, from their tasklet.
  264. The Execute Command SCSI RPC::
  265. int (*lldd_execute_task)(struct sas_task *, gfp_t gfp_flags);
  266. Used to queue a task to the SAS LLDD. @task is the task to be executed.
  267. @gfp_mask is the gfp_mask defining the context of the caller.
  268. This function should implement the Execute Command SCSI RPC,
  269. That is, when lldd_execute_task() is called, the command
  270. go out on the transport *immediately*. There is *no*
  271. queuing of any sort and at any level in a SAS LLDD.
  272. Returns:
  273. * -SAS_QUEUE_FULL, -ENOMEM, nothing was queued;
  274. * 0, the task(s) were queued.
  275. ::
  276. struct sas_task {
  277. dev -- the device this task is destined to
  278. task_proto -- _one_ of enum sas_proto
  279. scatter -- pointer to scatter gather list array
  280. num_scatter -- number of elements in scatter
  281. total_xfer_len -- total number of bytes expected to be transferred
  282. data_dir -- PCI_DMA_...
  283. task_done -- callback when the task has finished execution
  284. };
  285. Discovery
  286. =========
  287. The sysfs tree has the following purposes:
  288. a) It shows you the physical layout of the SAS domain at
  289. the current time, i.e. how the domain looks in the
  290. physical world right now.
  291. b) Shows some device parameters _at_discovery_time_.
  292. This is a link to the tree(1) program, very useful in
  293. viewing the SAS domain:
  294. ftp://mama.indstate.edu/linux/tree/
  295. I expect user space applications to actually create a
  296. graphical interface of this.
  297. That is, the sysfs domain tree doesn't show or keep state if
  298. you e.g., change the meaning of the READY LED MEANING
  299. setting, but it does show you the current connection status
  300. of the domain device.
  301. Keeping internal device state changes is responsibility of
  302. upper layers (Command set drivers) and user space.
  303. When a device or devices are unplugged from the domain, this
  304. is reflected in the sysfs tree immediately, and the device(s)
  305. removed from the system.
  306. The structure domain_device describes any device in the SAS
  307. domain. It is completely managed by the SAS layer. A task
  308. points to a domain device, this is how the SAS LLDD knows
  309. where to send the task(s) to. A SAS LLDD only reads the
  310. contents of the domain_device structure, but it never creates
  311. or destroys one.
  312. Expander management from User Space
  313. ===================================
  314. In each expander directory in sysfs, there is a file called
  315. "smp_portal". It is a binary sysfs attribute file, which
  316. implements an SMP portal (Note: this is *NOT* an SMP port),
  317. to which user space applications can send SMP requests and
  318. receive SMP responses.
  319. Functionality is deceptively simple:
  320. 1. Build the SMP frame you want to send. The format and layout
  321. is described in the SAS spec. Leave the CRC field equal 0.
  322. open(2)
  323. 2. Open the expander's SMP portal sysfs file in RW mode.
  324. write(2)
  325. 3. Write the frame you built in 1.
  326. read(2)
  327. 4. Read the amount of data you expect to receive for the frame you built.
  328. If you receive different amount of data you expected to receive,
  329. then there was some kind of error.
  330. close(2)
  331. All this process is shown in detail in the function do_smp_func()
  332. and its callers, in the file "expander_conf.c".
  333. The kernel functionality is implemented in the file
  334. "sas_expander.c".
  335. The program "expander_conf.c" implements this. It takes one
  336. argument, the sysfs file name of the SMP portal to the
  337. expander, and gives expander information, including routing
  338. tables.
  339. The SMP portal gives you complete control of the expander,
  340. so please be careful.