libsas.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * SAS host prototypes and structures header file
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. */
  25. #ifndef _LIBSAS_H_
  26. #define _LIBSAS_H_
  27. #include <linux/timer.h>
  28. #include <linux/pci.h>
  29. #include <scsi/sas.h>
  30. #include <linux/list.h>
  31. #include <asm/semaphore.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. #include <asm/scatterlist.h>
  36. struct block_device;
  37. enum sas_class {
  38. SAS,
  39. EXPANDER
  40. };
  41. enum sas_phy_role {
  42. PHY_ROLE_NONE = 0,
  43. PHY_ROLE_TARGET = 0x40,
  44. PHY_ROLE_INITIATOR = 0x80,
  45. };
  46. enum sas_phy_type {
  47. PHY_TYPE_PHYSICAL,
  48. PHY_TYPE_VIRTUAL
  49. };
  50. /* The events are mnemonically described in sas_dump.c
  51. * so when updating/adding events here, please also
  52. * update the other file too.
  53. */
  54. enum ha_event {
  55. HAE_RESET = 0U,
  56. HA_NUM_EVENTS = 1,
  57. };
  58. enum port_event {
  59. PORTE_BYTES_DMAED = 0U,
  60. PORTE_BROADCAST_RCVD = 1,
  61. PORTE_LINK_RESET_ERR = 2,
  62. PORTE_TIMER_EVENT = 3,
  63. PORTE_HARD_RESET = 4,
  64. PORT_NUM_EVENTS = 5,
  65. };
  66. enum phy_event {
  67. PHYE_LOSS_OF_SIGNAL = 0U,
  68. PHYE_OOB_DONE = 1,
  69. PHYE_OOB_ERROR = 2,
  70. PHYE_SPINUP_HOLD = 3, /* hot plug SATA, no COMWAKE sent */
  71. PHY_NUM_EVENTS = 4,
  72. };
  73. enum discover_event {
  74. DISCE_DISCOVER_DOMAIN = 0U,
  75. DISCE_REVALIDATE_DOMAIN = 1,
  76. DISCE_PORT_GONE = 2,
  77. DISC_NUM_EVENTS = 3,
  78. };
  79. /* ---------- Expander Devices ---------- */
  80. #define ETASK 0xFA
  81. #define to_dom_device(_obj) container_of(_obj, struct domain_device, dev_obj)
  82. #define to_dev_attr(_attr) container_of(_attr, struct domain_dev_attribute,\
  83. attr)
  84. enum routing_attribute {
  85. DIRECT_ROUTING,
  86. SUBTRACTIVE_ROUTING,
  87. TABLE_ROUTING,
  88. };
  89. enum ex_phy_state {
  90. PHY_EMPTY,
  91. PHY_VACANT,
  92. PHY_NOT_PRESENT,
  93. PHY_DEVICE_DISCOVERED
  94. };
  95. struct ex_phy {
  96. int phy_id;
  97. enum ex_phy_state phy_state;
  98. enum sas_dev_type attached_dev_type;
  99. enum sas_linkrate linkrate;
  100. u8 attached_sata_host:1;
  101. u8 attached_sata_dev:1;
  102. u8 attached_sata_ps:1;
  103. enum sas_proto attached_tproto;
  104. enum sas_proto attached_iproto;
  105. u8 attached_sas_addr[SAS_ADDR_SIZE];
  106. u8 attached_phy_id;
  107. u8 phy_change_count;
  108. enum routing_attribute routing_attr;
  109. u8 virtual:1;
  110. int last_da_index;
  111. struct sas_phy *phy;
  112. struct sas_port *port;
  113. };
  114. struct expander_device {
  115. struct list_head children;
  116. u16 ex_change_count;
  117. u16 max_route_indexes;
  118. u8 num_phys;
  119. u8 configuring:1;
  120. u8 conf_route_table:1;
  121. u8 enclosure_logical_id[8];
  122. struct ex_phy *ex_phy;
  123. struct sas_port *parent_port;
  124. };
  125. /* ---------- SATA device ---------- */
  126. enum ata_command_set {
  127. ATA_COMMAND_SET = 0,
  128. ATAPI_COMMAND_SET = 1,
  129. };
  130. struct sata_device {
  131. enum ata_command_set command_set;
  132. struct smp_resp rps_resp; /* report_phy_sata_resp */
  133. __le16 *identify_device;
  134. __le16 *identify_packet_device;
  135. u8 port_no; /* port number, if this is a PM (Port) */
  136. struct list_head children; /* PM Ports if this is a PM */
  137. };
  138. /* ---------- Domain device ---------- */
  139. struct domain_device {
  140. enum sas_dev_type dev_type;
  141. enum sas_linkrate linkrate;
  142. enum sas_linkrate min_linkrate;
  143. enum sas_linkrate max_linkrate;
  144. int pathways;
  145. struct domain_device *parent;
  146. struct list_head siblings; /* devices on the same level */
  147. struct asd_sas_port *port; /* shortcut to root of the tree */
  148. struct list_head dev_list_node;
  149. enum sas_proto iproto;
  150. enum sas_proto tproto;
  151. struct sas_rphy *rphy;
  152. u8 sas_addr[SAS_ADDR_SIZE];
  153. u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
  154. u8 frame_rcvd[32];
  155. union {
  156. struct expander_device ex_dev;
  157. struct sata_device sata_dev; /* STP & directly attached */
  158. };
  159. void *lldd_dev;
  160. };
  161. struct sas_discovery_event {
  162. struct work_struct work;
  163. struct asd_sas_port *port;
  164. };
  165. struct sas_discovery {
  166. spinlock_t disc_event_lock;
  167. struct sas_discovery_event disc_work[DISC_NUM_EVENTS];
  168. unsigned long pending;
  169. u8 fanout_sas_addr[8];
  170. u8 eeds_a[8];
  171. u8 eeds_b[8];
  172. int max_level;
  173. };
  174. /* The port struct is Class:RW, driver:RO */
  175. struct asd_sas_port {
  176. /* private: */
  177. struct completion port_gone_completion;
  178. struct sas_discovery disc;
  179. struct domain_device *port_dev;
  180. spinlock_t dev_list_lock;
  181. struct list_head dev_list;
  182. enum sas_linkrate linkrate;
  183. struct sas_phy *phy;
  184. struct work_struct work;
  185. /* public: */
  186. int id;
  187. enum sas_class class;
  188. u8 sas_addr[SAS_ADDR_SIZE];
  189. u8 attached_sas_addr[SAS_ADDR_SIZE];
  190. enum sas_proto iproto;
  191. enum sas_proto tproto;
  192. enum sas_oob_mode oob_mode;
  193. spinlock_t phy_list_lock;
  194. struct list_head phy_list;
  195. int num_phys;
  196. u32 phy_mask;
  197. struct sas_ha_struct *ha;
  198. struct sas_port *port;
  199. void *lldd_port; /* not touched by the sas class code */
  200. };
  201. struct asd_sas_event {
  202. struct work_struct work;
  203. struct asd_sas_phy *phy;
  204. };
  205. /* The phy pretty much is controlled by the LLDD.
  206. * The class only reads those fields.
  207. */
  208. struct asd_sas_phy {
  209. /* private: */
  210. /* protected by ha->event_lock */
  211. struct asd_sas_event port_events[PORT_NUM_EVENTS];
  212. struct asd_sas_event phy_events[PHY_NUM_EVENTS];
  213. unsigned long port_events_pending;
  214. unsigned long phy_events_pending;
  215. int error;
  216. struct sas_phy *phy;
  217. /* public: */
  218. /* The following are class:RO, driver:R/W */
  219. int enabled; /* must be set */
  220. int id; /* must be set */
  221. enum sas_class class;
  222. enum sas_proto iproto;
  223. enum sas_proto tproto;
  224. enum sas_phy_type type;
  225. enum sas_phy_role role;
  226. enum sas_oob_mode oob_mode;
  227. enum sas_linkrate linkrate;
  228. u8 *sas_addr; /* must be set */
  229. u8 attached_sas_addr[SAS_ADDR_SIZE]; /* class:RO, driver: R/W */
  230. spinlock_t frame_rcvd_lock;
  231. u8 *frame_rcvd; /* must be set */
  232. int frame_rcvd_size;
  233. spinlock_t sas_prim_lock;
  234. u32 sas_prim;
  235. struct list_head port_phy_el; /* driver:RO */
  236. struct asd_sas_port *port; /* Class:RW, driver: RO */
  237. struct sas_ha_struct *ha; /* may be set; the class sets it anyway */
  238. void *lldd_phy; /* not touched by the sas_class_code */
  239. };
  240. struct scsi_core {
  241. struct Scsi_Host *shost;
  242. spinlock_t task_queue_lock;
  243. struct list_head task_queue;
  244. int task_queue_size;
  245. struct task_struct *queue_thread;
  246. };
  247. struct sas_ha_event {
  248. struct work_struct work;
  249. struct sas_ha_struct *ha;
  250. };
  251. enum sas_ha_state {
  252. SAS_HA_REGISTERED,
  253. SAS_HA_UNREGISTERED
  254. };
  255. struct sas_ha_struct {
  256. /* private: */
  257. spinlock_t event_lock;
  258. struct sas_ha_event ha_events[HA_NUM_EVENTS];
  259. unsigned long pending;
  260. enum sas_ha_state state;
  261. spinlock_t state_lock;
  262. struct scsi_core core;
  263. /* public: */
  264. char *sas_ha_name;
  265. struct pci_dev *pcidev; /* should be set */
  266. struct module *lldd_module; /* should be set */
  267. u8 *sas_addr; /* must be set */
  268. u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
  269. spinlock_t phy_port_lock;
  270. struct asd_sas_phy **sas_phy; /* array of valid pointers, must be set */
  271. struct asd_sas_port **sas_port; /* array of valid pointers, must be set */
  272. int num_phys; /* must be set, gt 0, static */
  273. /* The class calls this to send a task for execution. */
  274. int lldd_max_execute_num;
  275. int lldd_queue_size;
  276. /* LLDD calls these to notify the class of an event. */
  277. void (*notify_ha_event)(struct sas_ha_struct *, enum ha_event);
  278. void (*notify_port_event)(struct asd_sas_phy *, enum port_event);
  279. void (*notify_phy_event)(struct asd_sas_phy *, enum phy_event);
  280. void *lldd_ha; /* not touched by sas class code */
  281. struct list_head eh_done_q;
  282. };
  283. #define SHOST_TO_SAS_HA(_shost) (*(struct sas_ha_struct **)(_shost)->hostdata)
  284. static inline struct domain_device *
  285. starget_to_domain_dev(struct scsi_target *starget) {
  286. return starget->hostdata;
  287. }
  288. static inline struct domain_device *
  289. sdev_to_domain_dev(struct scsi_device *sdev) {
  290. return starget_to_domain_dev(sdev->sdev_target);
  291. }
  292. static inline struct domain_device *
  293. cmd_to_domain_dev(struct scsi_cmnd *cmd)
  294. {
  295. return sdev_to_domain_dev(cmd->device);
  296. }
  297. void sas_hash_addr(u8 *hashed, const u8 *sas_addr);
  298. /* Before calling a notify event, LLDD should use this function
  299. * when the link is severed (possibly from its tasklet).
  300. * The idea is that the Class only reads those, while the LLDD,
  301. * can R/W these (thus avoiding a race).
  302. */
  303. static inline void sas_phy_disconnected(struct asd_sas_phy *phy)
  304. {
  305. phy->oob_mode = OOB_NOT_CONNECTED;
  306. phy->linkrate = SAS_LINK_RATE_UNKNOWN;
  307. }
  308. /* ---------- Tasks ---------- */
  309. /*
  310. service_response | SAS_TASK_COMPLETE | SAS_TASK_UNDELIVERED |
  311. exec_status | | |
  312. ---------------------+---------------------+-----------------------+
  313. SAM_... | X | |
  314. DEV_NO_RESPONSE | X | X |
  315. INTERRUPTED | X | |
  316. QUEUE_FULL | | X |
  317. DEVICE_UNKNOWN | | X |
  318. SG_ERR | | X |
  319. ---------------------+---------------------+-----------------------+
  320. */
  321. enum service_response {
  322. SAS_TASK_COMPLETE,
  323. SAS_TASK_UNDELIVERED = -1,
  324. };
  325. enum exec_status {
  326. SAM_GOOD = 0,
  327. SAM_CHECK_COND = 2,
  328. SAM_COND_MET = 4,
  329. SAM_BUSY = 8,
  330. SAM_INTERMEDIATE = 0x10,
  331. SAM_IM_COND_MET = 0x12,
  332. SAM_RESV_CONFLICT= 0x14,
  333. SAM_TASK_SET_FULL= 0x28,
  334. SAM_ACA_ACTIVE = 0x30,
  335. SAM_TASK_ABORTED = 0x40,
  336. SAS_DEV_NO_RESPONSE = 0x80,
  337. SAS_DATA_UNDERRUN,
  338. SAS_DATA_OVERRUN,
  339. SAS_INTERRUPTED,
  340. SAS_QUEUE_FULL,
  341. SAS_DEVICE_UNKNOWN,
  342. SAS_SG_ERR,
  343. SAS_OPEN_REJECT,
  344. SAS_OPEN_TO,
  345. SAS_PROTO_RESPONSE,
  346. SAS_PHY_DOWN,
  347. SAS_NAK_R_ERR,
  348. SAS_PENDING,
  349. SAS_ABORTED_TASK,
  350. };
  351. /* When a task finishes with a response, the LLDD examines the
  352. * response:
  353. * - For an ATA task task_status_struct::stat is set to
  354. * SAS_PROTO_RESPONSE, and the task_status_struct::buf is set to the
  355. * contents of struct ata_task_resp.
  356. * - For SSP tasks, if no data is present or status/TMF response
  357. * is valid, task_status_struct::stat is set. If data is present
  358. * (SENSE data), the LLDD copies up to SAS_STATUS_BUF_SIZE, sets
  359. * task_status_struct::buf_valid_size, and task_status_struct::stat is
  360. * set to SAM_CHECK_COND.
  361. *
  362. * "buf" has format SCSI Sense for SSP task, or struct ata_task_resp
  363. * for ATA task.
  364. *
  365. * "frame_len" is the total frame length, which could be more or less
  366. * than actually copied.
  367. *
  368. * Tasks ending with response, always set the residual field.
  369. */
  370. struct ata_task_resp {
  371. u16 frame_len;
  372. u8 ending_fis[24]; /* dev to host or data-in */
  373. u32 sstatus;
  374. u32 serror;
  375. u32 scontrol;
  376. u32 sactive;
  377. };
  378. #define SAS_STATUS_BUF_SIZE 96
  379. struct task_status_struct {
  380. enum service_response resp;
  381. enum exec_status stat;
  382. int buf_valid_size;
  383. u8 buf[SAS_STATUS_BUF_SIZE];
  384. u32 residual;
  385. enum sas_open_rej_reason open_rej_reason;
  386. };
  387. /* ATA and ATAPI task queuable to a SAS LLDD.
  388. */
  389. struct sas_ata_task {
  390. struct host_to_dev_fis fis;
  391. u8 atapi_packet[16]; /* 0 if not ATAPI task */
  392. u8 retry_count; /* hardware retry, should be > 0 */
  393. u8 dma_xfer:1; /* PIO:0 or DMA:1 */
  394. u8 use_ncq:1;
  395. u8 set_affil_pol:1;
  396. u8 stp_affil_pol:1;
  397. u8 device_control_reg_update:1;
  398. };
  399. struct sas_smp_task {
  400. struct scatterlist smp_req;
  401. struct scatterlist smp_resp;
  402. };
  403. enum task_attribute {
  404. TASK_ATTR_SIMPLE = 0,
  405. TASK_ATTR_HOQ = 1,
  406. TASK_ATTR_ORDERED= 2,
  407. TASK_ATTR_ACA = 4,
  408. };
  409. struct sas_ssp_task {
  410. u8 retry_count; /* hardware retry, should be > 0 */
  411. u8 LUN[8];
  412. u8 enable_first_burst:1;
  413. enum task_attribute task_attr;
  414. u8 task_prio;
  415. u8 cdb[16];
  416. };
  417. struct sas_task {
  418. struct domain_device *dev;
  419. struct list_head list;
  420. spinlock_t task_state_lock;
  421. unsigned task_state_flags;
  422. enum sas_proto task_proto;
  423. /* Used by the discovery code. */
  424. struct timer_list timer;
  425. struct completion completion;
  426. union {
  427. struct sas_ata_task ata_task;
  428. struct sas_smp_task smp_task;
  429. struct sas_ssp_task ssp_task;
  430. };
  431. struct scatterlist *scatter;
  432. int num_scatter;
  433. u32 total_xfer_len;
  434. u8 data_dir:2; /* Use PCI_DMA_... */
  435. struct task_status_struct task_status;
  436. void (*task_done)(struct sas_task *);
  437. void *lldd_task; /* for use by LLDDs */
  438. void *uldd_task;
  439. struct work_struct abort_work;
  440. };
  441. #define SAS_TASK_STATE_PENDING 1
  442. #define SAS_TASK_STATE_DONE 2
  443. #define SAS_TASK_STATE_ABORTED 4
  444. #define SAS_TASK_NEED_DEV_RESET 8
  445. #define SAS_TASK_AT_INITIATOR 16
  446. static inline struct sas_task *sas_alloc_task(gfp_t flags)
  447. {
  448. extern struct kmem_cache *sas_task_cache;
  449. struct sas_task *task = kmem_cache_zalloc(sas_task_cache, flags);
  450. if (task) {
  451. INIT_LIST_HEAD(&task->list);
  452. spin_lock_init(&task->task_state_lock);
  453. task->task_state_flags = SAS_TASK_STATE_PENDING;
  454. init_timer(&task->timer);
  455. init_completion(&task->completion);
  456. }
  457. return task;
  458. }
  459. static inline void sas_free_task(struct sas_task *task)
  460. {
  461. if (task) {
  462. extern struct kmem_cache *sas_task_cache;
  463. BUG_ON(!list_empty(&task->list));
  464. kmem_cache_free(sas_task_cache, task);
  465. }
  466. }
  467. struct sas_domain_function_template {
  468. /* The class calls these to notify the LLDD of an event. */
  469. void (*lldd_port_formed)(struct asd_sas_phy *);
  470. void (*lldd_port_deformed)(struct asd_sas_phy *);
  471. /* The class calls these when a device is found or gone. */
  472. int (*lldd_dev_found)(struct domain_device *);
  473. void (*lldd_dev_gone)(struct domain_device *);
  474. int (*lldd_execute_task)(struct sas_task *, int num,
  475. gfp_t gfp_flags);
  476. /* Task Management Functions. Must be called from process context. */
  477. int (*lldd_abort_task)(struct sas_task *);
  478. int (*lldd_abort_task_set)(struct domain_device *, u8 *lun);
  479. int (*lldd_clear_aca)(struct domain_device *, u8 *lun);
  480. int (*lldd_clear_task_set)(struct domain_device *, u8 *lun);
  481. int (*lldd_I_T_nexus_reset)(struct domain_device *);
  482. int (*lldd_lu_reset)(struct domain_device *, u8 *lun);
  483. int (*lldd_query_task)(struct sas_task *);
  484. /* Port and Adapter management */
  485. int (*lldd_clear_nexus_port)(struct asd_sas_port *);
  486. int (*lldd_clear_nexus_ha)(struct sas_ha_struct *);
  487. /* Phy management */
  488. int (*lldd_control_phy)(struct asd_sas_phy *, enum phy_func, void *);
  489. };
  490. extern int sas_register_ha(struct sas_ha_struct *);
  491. extern int sas_unregister_ha(struct sas_ha_struct *);
  492. int sas_set_phy_speed(struct sas_phy *phy,
  493. struct sas_phy_linkrates *rates);
  494. int sas_phy_enable(struct sas_phy *phy, int enabled);
  495. int sas_phy_reset(struct sas_phy *phy, int hard_reset);
  496. extern int sas_queuecommand(struct scsi_cmnd *,
  497. void (*scsi_done)(struct scsi_cmnd *));
  498. extern int sas_target_alloc(struct scsi_target *);
  499. extern int sas_slave_alloc(struct scsi_device *);
  500. extern int sas_slave_configure(struct scsi_device *);
  501. extern void sas_slave_destroy(struct scsi_device *);
  502. extern int sas_change_queue_depth(struct scsi_device *, int new_depth);
  503. extern int sas_change_queue_type(struct scsi_device *, int qt);
  504. extern int sas_bios_param(struct scsi_device *,
  505. struct block_device *,
  506. sector_t capacity, int *hsc);
  507. extern struct scsi_transport_template *
  508. sas_domain_attach_transport(struct sas_domain_function_template *);
  509. extern void sas_domain_release_transport(struct scsi_transport_template *);
  510. int sas_discover_root_expander(struct domain_device *);
  511. void sas_init_ex_attr(void);
  512. int sas_ex_revalidate_domain(struct domain_device *);
  513. void sas_unregister_domain_devices(struct asd_sas_port *port);
  514. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *);
  515. int sas_discover_event(struct asd_sas_port *, enum discover_event ev);
  516. int sas_discover_sata(struct domain_device *);
  517. int sas_discover_end_dev(struct domain_device *);
  518. void sas_unregister_dev(struct domain_device *);
  519. void sas_init_dev(struct domain_device *);
  520. void sas_task_abort(struct sas_task *);
  521. int __sas_task_abort(struct sas_task *);
  522. int sas_eh_device_reset_handler(struct scsi_cmnd *cmd);
  523. int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd);
  524. #endif /* _SASLIB_H_ */