ibmvscsi_tgt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*******************************************************************************
  3. * IBM Virtual SCSI Target Driver
  4. * Copyright (C) 2003-2005 Dave Boutcher ([email protected]) IBM Corp.
  5. * Santiago Leon ([email protected]) IBM Corp.
  6. * Linda Xie ([email protected]) IBM Corp.
  7. *
  8. * Copyright (C) 2005-2011 FUJITA Tomonori <[email protected]>
  9. * Copyright (C) 2010 Nicholas A. Bellinger <[email protected]>
  10. * Copyright (C) 2016 Bryant G. Ly <[email protected]> IBM Corp.
  11. *
  12. * Authors: Bryant G. Ly <[email protected]>
  13. * Authors: Michael Cyr <[email protected]>
  14. *
  15. ****************************************************************************/
  16. #ifndef __H_IBMVSCSI_TGT
  17. #define __H_IBMVSCSI_TGT
  18. #include <linux/interrupt.h>
  19. #include "libsrp.h"
  20. #define SYS_ID_NAME_LEN 64
  21. #define PARTITION_NAMELEN 96
  22. #define IBMVSCSIS_NAMELEN 32
  23. #define MSG_HI 0
  24. #define MSG_LOW 1
  25. #define MAX_CMD_Q_PAGES 4
  26. #define CRQ_PER_PAGE (PAGE_SIZE / sizeof(struct viosrp_crq))
  27. /* in terms of number of elements */
  28. #define DEFAULT_CMD_Q_SIZE CRQ_PER_PAGE
  29. #define MAX_CMD_Q_SIZE (DEFAULT_CMD_Q_SIZE * MAX_CMD_Q_PAGES)
  30. #define SRP_VIOLATION 0x102 /* general error code */
  31. /*
  32. * SRP buffer formats defined as of 16.a supported by this driver.
  33. */
  34. #define SUPPORTED_FORMATS ((SRP_DATA_DESC_DIRECT << 1) | \
  35. (SRP_DATA_DESC_INDIRECT << 1))
  36. #define SCSI_LUN_ADDR_METHOD_FLAT 1
  37. struct dma_window {
  38. u32 liobn; /* Unique per vdevice */
  39. u64 tce_base; /* Physical location of the TCE table */
  40. u64 tce_size; /* Size of the TCE table in bytes */
  41. };
  42. struct target_dds {
  43. u64 unit_id; /* 64 bit will force alignment */
  44. #define NUM_DMA_WINDOWS 2
  45. #define LOCAL 0
  46. #define REMOTE 1
  47. struct dma_window window[NUM_DMA_WINDOWS];
  48. /* root node property "ibm,partition-no" */
  49. uint partition_num;
  50. char partition_name[PARTITION_NAMELEN];
  51. };
  52. #define MAX_NUM_PORTS 1
  53. #define MAX_H_COPY_RDMA (128 * 1024)
  54. #define MAX_EYE 64
  55. /* Return codes */
  56. #define ADAPT_SUCCESS 0L
  57. /* choose error codes that do not conflict with PHYP */
  58. #define ERROR -40L
  59. struct format_code {
  60. u8 reserved;
  61. u8 buffers;
  62. };
  63. struct client_info {
  64. #define SRP_VERSION "16.a"
  65. char srp_version[8];
  66. /* root node property ibm,partition-name */
  67. char partition_name[PARTITION_NAMELEN];
  68. /* root node property ibm,partition-no */
  69. u32 partition_number;
  70. /* initially 1 */
  71. u32 mad_version;
  72. u32 os_type;
  73. };
  74. /*
  75. * Changing this constant changes the number of seconds to wait before
  76. * considering the client will never service its queue again.
  77. */
  78. #define SECONDS_TO_CONSIDER_FAILED 30
  79. /*
  80. * These constants set the polling period used to determine if the client
  81. * has freed at least one element in the response queue.
  82. */
  83. #define WAIT_SECONDS 1
  84. #define WAIT_NANO_SECONDS 5000
  85. #define MAX_TIMER_POPS ((1000000 / WAIT_NANO_SECONDS) * \
  86. SECONDS_TO_CONSIDER_FAILED)
  87. /*
  88. * general purpose timer control block
  89. * which can be used for multiple functions
  90. */
  91. struct timer_cb {
  92. struct hrtimer timer;
  93. /*
  94. * how long has it been since the client
  95. * serviced the queue. The variable is incrmented
  96. * in the service_wait_q routine and cleared
  97. * in send messages
  98. */
  99. int timer_pops;
  100. /* the timer is started */
  101. bool started;
  102. };
  103. struct cmd_queue {
  104. /* kva */
  105. struct viosrp_crq *base_addr;
  106. dma_addr_t crq_token;
  107. /* used to maintain index */
  108. uint mask;
  109. /* current element */
  110. uint index;
  111. int size;
  112. };
  113. #define SCSOLNT_RESP_SHIFT 1
  114. #define UCSOLNT_RESP_SHIFT 2
  115. #define SCSOLNT BIT(SCSOLNT_RESP_SHIFT)
  116. #define UCSOLNT BIT(UCSOLNT_RESP_SHIFT)
  117. enum cmd_type {
  118. SCSI_CDB = 0x01,
  119. TASK_MANAGEMENT = 0x02,
  120. /* MAD or addressed to port 0 */
  121. ADAPTER_MAD = 0x04,
  122. UNSET_TYPE = 0x08,
  123. };
  124. struct iu_rsp {
  125. u8 format;
  126. u8 sol_not;
  127. u16 len;
  128. /* tag is just to help client identify cmd, so don't translate be/le */
  129. u64 tag;
  130. };
  131. struct ibmvscsis_cmd {
  132. struct list_head list;
  133. /* Used for TCM Core operations */
  134. struct se_cmd se_cmd;
  135. struct iu_entry *iue;
  136. struct iu_rsp rsp;
  137. struct work_struct work;
  138. struct scsi_info *adapter;
  139. struct ibmvscsis_cmd *abort_cmd;
  140. /* Sense buffer that will be mapped into outgoing status */
  141. unsigned char sense_buf[TRANSPORT_SENSE_BUFFER];
  142. u64 init_time;
  143. #define CMD_FAST_FAIL BIT(0)
  144. #define DELAY_SEND BIT(1)
  145. u32 flags;
  146. char type;
  147. };
  148. struct ibmvscsis_nexus {
  149. struct se_session *se_sess;
  150. };
  151. struct ibmvscsis_tport {
  152. /* SCSI protocol the tport is providing */
  153. u8 tport_proto_id;
  154. /* ASCII formatted WWPN for SRP Target port */
  155. char tport_name[IBMVSCSIS_NAMELEN];
  156. /* Returned by ibmvscsis_make_tport() */
  157. struct se_wwn tport_wwn;
  158. /* Returned by ibmvscsis_make_tpg() */
  159. struct se_portal_group se_tpg;
  160. /* ibmvscsis port target portal group tag for TCM */
  161. u16 tport_tpgt;
  162. /* Pointer to TCM session for I_T Nexus */
  163. struct ibmvscsis_nexus *ibmv_nexus;
  164. bool enabled;
  165. bool releasing;
  166. };
  167. struct scsi_info {
  168. struct list_head list;
  169. char eye[MAX_EYE];
  170. /* commands waiting for space on repsonse queue */
  171. struct list_head waiting_rsp;
  172. #define NO_QUEUE 0x00
  173. #define WAIT_ENABLED 0X01
  174. #define WAIT_CONNECTION 0x04
  175. /* have established a connection */
  176. #define CONNECTED 0x08
  177. /* at least one port is processing SRP IU */
  178. #define SRP_PROCESSING 0x10
  179. /* remove request received */
  180. #define UNCONFIGURING 0x20
  181. /* disconnect by letting adapter go idle, no error */
  182. #define WAIT_IDLE 0x40
  183. /* disconnecting to clear an error */
  184. #define ERR_DISCONNECT 0x80
  185. /* disconnect to clear error state, then come back up */
  186. #define ERR_DISCONNECT_RECONNECT 0x100
  187. /* disconnected after clearing an error */
  188. #define ERR_DISCONNECTED 0x200
  189. /* A series of errors caused unexpected errors */
  190. #define UNDEFINED 0x400
  191. u16 state;
  192. int fast_fail;
  193. struct target_dds dds;
  194. char *cmd_pool;
  195. /* list of free commands */
  196. struct list_head free_cmd;
  197. /* command elements ready for scheduler */
  198. struct list_head schedule_q;
  199. /* commands sent to TCM */
  200. struct list_head active_q;
  201. caddr_t *map_buf;
  202. /* ioba of map buffer */
  203. dma_addr_t map_ioba;
  204. /* allowable number of outstanding SRP requests */
  205. int request_limit;
  206. /* extra credit */
  207. int credit;
  208. /* outstanding transactions against credit limit */
  209. int debit;
  210. /* allow only one outstanding mad request */
  211. #define PROCESSING_MAD 0x00002
  212. /* Waiting to go idle */
  213. #define WAIT_FOR_IDLE 0x00004
  214. /* H_REG_CRQ called */
  215. #define CRQ_CLOSED 0x00010
  216. /* detected that client has failed */
  217. #define CLIENT_FAILED 0x00040
  218. /* detected that transport event occurred */
  219. #define TRANS_EVENT 0x00080
  220. /* don't attempt to send anything to the client */
  221. #define RESPONSE_Q_DOWN 0x00100
  222. /* request made to schedule disconnect handler */
  223. #define SCHEDULE_DISCONNECT 0x00400
  224. /* disconnect handler is scheduled */
  225. #define DISCONNECT_SCHEDULED 0x00800
  226. /* remove function is sleeping */
  227. #define CFG_SLEEPING 0x01000
  228. /* Register for Prepare for Suspend Transport Events */
  229. #define PREP_FOR_SUSPEND_ENABLED 0x02000
  230. /* Prepare for Suspend event sent */
  231. #define PREP_FOR_SUSPEND_PENDING 0x04000
  232. /* Resume from Suspend event sent */
  233. #define PREP_FOR_SUSPEND_ABORTED 0x08000
  234. /* Prepare for Suspend event overwrote another CRQ entry */
  235. #define PREP_FOR_SUSPEND_OVERWRITE 0x10000
  236. u32 flags;
  237. /* adapter lock */
  238. spinlock_t intr_lock;
  239. /* information needed to manage command queue */
  240. struct cmd_queue cmd_q;
  241. /* used in hcall to copy response back into srp buffer */
  242. u64 empty_iu_id;
  243. /* used in crq, to tag what iu the response is for */
  244. u64 empty_iu_tag;
  245. uint new_state;
  246. uint resume_state;
  247. /* control block for the response queue timer */
  248. struct timer_cb rsp_q_timer;
  249. /* keep last client to enable proper accounting */
  250. struct client_info client_data;
  251. /* what can this client do */
  252. u32 client_cap;
  253. /*
  254. * The following two fields capture state and flag changes that
  255. * can occur when the lock is given up. In the orginal design,
  256. * the lock was held during calls into phyp;
  257. * however, phyp did not meet PAPR architecture. This is
  258. * a work around.
  259. */
  260. u16 phyp_acr_state;
  261. u32 phyp_acr_flags;
  262. struct workqueue_struct *work_q;
  263. struct completion wait_idle;
  264. struct completion unconfig;
  265. struct device dev;
  266. struct vio_dev *dma_dev;
  267. struct srp_target target;
  268. struct ibmvscsis_tport tport;
  269. struct tasklet_struct work_task;
  270. struct work_struct proc_work;
  271. };
  272. /*
  273. * Provide a constant that allows software to detect the adapter is
  274. * disconnecting from the client from one of several states.
  275. */
  276. #define IS_DISCONNECTING (UNCONFIGURING | ERR_DISCONNECT_RECONNECT | \
  277. ERR_DISCONNECT)
  278. /*
  279. * Provide a constant that can be used with interrupt handling that
  280. * essentially lets the interrupt handler know that all requests should
  281. * be thrown out,
  282. */
  283. #define DONT_PROCESS_STATE (IS_DISCONNECTING | UNDEFINED | \
  284. ERR_DISCONNECTED | WAIT_IDLE)
  285. /*
  286. * If any of these flag bits are set then do not allow the interrupt
  287. * handler to schedule the off level handler.
  288. */
  289. #define BLOCK (DISCONNECT_SCHEDULED)
  290. /* State and transition events that stop the interrupt handler */
  291. #define TARGET_STOP(VSCSI) (long)(((VSCSI)->state & DONT_PROCESS_STATE) | \
  292. ((VSCSI)->flags & BLOCK))
  293. #define PREP_FOR_SUSPEND_FLAGS (PREP_FOR_SUSPEND_ENABLED | \
  294. PREP_FOR_SUSPEND_PENDING | \
  295. PREP_FOR_SUSPEND_ABORTED | \
  296. PREP_FOR_SUSPEND_OVERWRITE)
  297. /* flag bit that are not reset during disconnect */
  298. #define PRESERVE_FLAG_FIELDS (PREP_FOR_SUSPEND_FLAGS)
  299. #define vio_iu(IUE) ((union viosrp_iu *)((IUE)->sbuf->buf))
  300. #define READ_CMD(cdb) (((cdb)[0] & 0x1F) == 8)
  301. #define WRITE_CMD(cdb) (((cdb)[0] & 0x1F) == 0xA)
  302. #ifndef H_GET_PARTNER_INFO
  303. #define H_GET_PARTNER_INFO 0x0000000000000008LL
  304. #endif
  305. #ifndef H_ENABLE_PREPARE_FOR_SUSPEND
  306. #define H_ENABLE_PREPARE_FOR_SUSPEND 0x000000000000001DLL
  307. #endif
  308. #ifndef H_READY_FOR_SUSPEND
  309. #define H_READY_FOR_SUSPEND 0x000000000000001ELL
  310. #endif
  311. #define h_copy_rdma(l, sa, sb, da, db) \
  312. plpar_hcall_norets(H_COPY_RDMA, l, sa, sb, da, db)
  313. #define h_vioctl(u, o, a, u1, u2, u3, u4) \
  314. plpar_hcall_norets(H_VIOCTL, u, o, a, u1, u2)
  315. #define h_reg_crq(ua, tok, sz) \
  316. plpar_hcall_norets(H_REG_CRQ, ua, tok, sz)
  317. #define h_free_crq(ua) \
  318. plpar_hcall_norets(H_FREE_CRQ, ua)
  319. #define h_send_crq(ua, d1, d2) \
  320. plpar_hcall_norets(H_SEND_CRQ, ua, d1, d2)
  321. #endif