target_core_user.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /*
  2. * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
  3. * Copyright (C) 2014 Red Hat, Inc.
  4. * Copyright (C) 2015 Arrikto, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/spinlock.h>
  20. #include <linux/module.h>
  21. #include <linux/idr.h>
  22. #include <linux/kernel.h>
  23. #include <linux/timer.h>
  24. #include <linux/parser.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/uio_driver.h>
  27. #include <linux/stringify.h>
  28. #include <linux/bitops.h>
  29. #include <net/genetlink.h>
  30. #include <scsi/scsi_common.h>
  31. #include <scsi/scsi_proto.h>
  32. #include <target/target_core_base.h>
  33. #include <target/target_core_fabric.h>
  34. #include <target/target_core_backend.h>
  35. #include <linux/target_core_user.h>
  36. /*
  37. * Define a shared-memory interface for LIO to pass SCSI commands and
  38. * data to userspace for processing. This is to allow backends that
  39. * are too complex for in-kernel support to be possible.
  40. *
  41. * It uses the UIO framework to do a lot of the device-creation and
  42. * introspection work for us.
  43. *
  44. * See the .h file for how the ring is laid out. Note that while the
  45. * command ring is defined, the particulars of the data area are
  46. * not. Offset values in the command entry point to other locations
  47. * internal to the mmap()ed area. There is separate space outside the
  48. * command ring for data buffers. This leaves maximum flexibility for
  49. * moving buffer allocations, or even page flipping or other
  50. * allocation techniques, without altering the command ring layout.
  51. *
  52. * SECURITY:
  53. * The user process must be assumed to be malicious. There's no way to
  54. * prevent it breaking the command ring protocol if it wants, but in
  55. * order to prevent other issues we must only ever read *data* from
  56. * the shared memory area, not offsets or sizes. This applies to
  57. * command ring entries as well as the mailbox. Extra code needed for
  58. * this may have a 'UAM' comment.
  59. */
  60. #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
  61. #define DATA_BLOCK_BITS 256
  62. #define DATA_BLOCK_SIZE 4096
  63. #define CMDR_SIZE (16 * 4096)
  64. #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
  65. #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
  66. static struct device *tcmu_root_device;
  67. struct tcmu_hba {
  68. u32 host_id;
  69. };
  70. #define TCMU_CONFIG_LEN 256
  71. struct tcmu_dev {
  72. struct se_device se_dev;
  73. char *name;
  74. struct se_hba *hba;
  75. #define TCMU_DEV_BIT_OPEN 0
  76. #define TCMU_DEV_BIT_BROKEN 1
  77. unsigned long flags;
  78. struct uio_info uio_info;
  79. struct tcmu_mailbox *mb_addr;
  80. size_t dev_size;
  81. u32 cmdr_size;
  82. u32 cmdr_last_cleaned;
  83. /* Offset of data area from start of mb */
  84. /* Must add data_off and mb_addr to get the address */
  85. size_t data_off;
  86. size_t data_size;
  87. DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
  88. wait_queue_head_t wait_cmdr;
  89. /* TODO should this be a mutex? */
  90. spinlock_t cmdr_lock;
  91. struct idr commands;
  92. spinlock_t commands_lock;
  93. struct timer_list timeout;
  94. char dev_config[TCMU_CONFIG_LEN];
  95. };
  96. #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
  97. #define CMDR_OFF sizeof(struct tcmu_mailbox)
  98. struct tcmu_cmd {
  99. struct se_cmd *se_cmd;
  100. struct tcmu_dev *tcmu_dev;
  101. uint16_t cmd_id;
  102. /* Can't use se_cmd when cleaning up expired cmds, because if
  103. cmd has been completed then accessing se_cmd is off limits */
  104. DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
  105. unsigned long deadline;
  106. #define TCMU_CMD_BIT_EXPIRED 0
  107. unsigned long flags;
  108. };
  109. static struct kmem_cache *tcmu_cmd_cache;
  110. /* multicast group */
  111. enum tcmu_multicast_groups {
  112. TCMU_MCGRP_CONFIG,
  113. };
  114. static const struct genl_multicast_group tcmu_mcgrps[] = {
  115. [TCMU_MCGRP_CONFIG] = { .name = "config", },
  116. };
  117. /* Our generic netlink family */
  118. static struct genl_family tcmu_genl_family = {
  119. .id = GENL_ID_GENERATE,
  120. .hdrsize = 0,
  121. .name = "TCM-USER",
  122. .version = 1,
  123. .maxattr = TCMU_ATTR_MAX,
  124. .mcgrps = tcmu_mcgrps,
  125. .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
  126. .netnsok = true,
  127. };
  128. static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
  129. {
  130. struct se_device *se_dev = se_cmd->se_dev;
  131. struct tcmu_dev *udev = TCMU_DEV(se_dev);
  132. struct tcmu_cmd *tcmu_cmd;
  133. int cmd_id;
  134. tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
  135. if (!tcmu_cmd)
  136. return NULL;
  137. tcmu_cmd->se_cmd = se_cmd;
  138. tcmu_cmd->tcmu_dev = udev;
  139. tcmu_cmd->deadline = jiffies + msecs_to_jiffies(TCMU_TIME_OUT);
  140. idr_preload(GFP_KERNEL);
  141. spin_lock_irq(&udev->commands_lock);
  142. cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
  143. USHRT_MAX, GFP_NOWAIT);
  144. spin_unlock_irq(&udev->commands_lock);
  145. idr_preload_end();
  146. if (cmd_id < 0) {
  147. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  148. return NULL;
  149. }
  150. tcmu_cmd->cmd_id = cmd_id;
  151. return tcmu_cmd;
  152. }
  153. static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
  154. {
  155. unsigned long offset = offset_in_page(vaddr);
  156. size = round_up(size+offset, PAGE_SIZE);
  157. vaddr -= offset;
  158. while (size) {
  159. flush_dcache_page(virt_to_page(vaddr));
  160. size -= PAGE_SIZE;
  161. }
  162. }
  163. /*
  164. * Some ring helper functions. We don't assume size is a power of 2 so
  165. * we can't use circ_buf.h.
  166. */
  167. static inline size_t spc_used(size_t head, size_t tail, size_t size)
  168. {
  169. int diff = head - tail;
  170. if (diff >= 0)
  171. return diff;
  172. else
  173. return size + diff;
  174. }
  175. static inline size_t spc_free(size_t head, size_t tail, size_t size)
  176. {
  177. /* Keep 1 byte unused or we can't tell full from empty */
  178. return (size - spc_used(head, tail, size) - 1);
  179. }
  180. static inline size_t head_to_end(size_t head, size_t size)
  181. {
  182. return size - head;
  183. }
  184. static inline void new_iov(struct iovec **iov, int *iov_cnt,
  185. struct tcmu_dev *udev)
  186. {
  187. struct iovec *iovec;
  188. if (*iov_cnt != 0)
  189. (*iov)++;
  190. (*iov_cnt)++;
  191. iovec = *iov;
  192. memset(iovec, 0, sizeof(struct iovec));
  193. }
  194. #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
  195. /* offset is relative to mb_addr */
  196. static inline size_t get_block_offset(struct tcmu_dev *dev,
  197. int block, int remaining)
  198. {
  199. return dev->data_off + block * DATA_BLOCK_SIZE +
  200. DATA_BLOCK_SIZE - remaining;
  201. }
  202. static inline size_t iov_tail(struct tcmu_dev *udev, struct iovec *iov)
  203. {
  204. return (size_t)iov->iov_base + iov->iov_len;
  205. }
  206. static void alloc_and_scatter_data_area(struct tcmu_dev *udev,
  207. struct scatterlist *data_sg, unsigned int data_nents,
  208. struct iovec **iov, int *iov_cnt, bool copy_data)
  209. {
  210. int i, block;
  211. int block_remaining = 0;
  212. void *from, *to;
  213. size_t copy_bytes, to_offset;
  214. struct scatterlist *sg;
  215. for_each_sg(data_sg, sg, data_nents, i) {
  216. int sg_remaining = sg->length;
  217. from = kmap_atomic(sg_page(sg)) + sg->offset;
  218. while (sg_remaining > 0) {
  219. if (block_remaining == 0) {
  220. block = find_first_zero_bit(udev->data_bitmap,
  221. DATA_BLOCK_BITS);
  222. block_remaining = DATA_BLOCK_SIZE;
  223. set_bit(block, udev->data_bitmap);
  224. }
  225. copy_bytes = min_t(size_t, sg_remaining,
  226. block_remaining);
  227. to_offset = get_block_offset(udev, block,
  228. block_remaining);
  229. to = (void *)udev->mb_addr + to_offset;
  230. if (*iov_cnt != 0 &&
  231. to_offset == iov_tail(udev, *iov)) {
  232. (*iov)->iov_len += copy_bytes;
  233. } else {
  234. new_iov(iov, iov_cnt, udev);
  235. (*iov)->iov_base = (void __user *) to_offset;
  236. (*iov)->iov_len = copy_bytes;
  237. }
  238. if (copy_data) {
  239. memcpy(to, from + sg->length - sg_remaining,
  240. copy_bytes);
  241. tcmu_flush_dcache_range(to, copy_bytes);
  242. }
  243. sg_remaining -= copy_bytes;
  244. block_remaining -= copy_bytes;
  245. }
  246. kunmap_atomic(from - sg->offset);
  247. }
  248. }
  249. static void free_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd)
  250. {
  251. bitmap_xor(udev->data_bitmap, udev->data_bitmap, cmd->data_bitmap,
  252. DATA_BLOCK_BITS);
  253. }
  254. static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
  255. bool bidi)
  256. {
  257. struct se_cmd *se_cmd = cmd->se_cmd;
  258. int i, block;
  259. int block_remaining = 0;
  260. void *from, *to;
  261. size_t copy_bytes, from_offset;
  262. struct scatterlist *sg, *data_sg;
  263. unsigned int data_nents;
  264. DECLARE_BITMAP(bitmap, DATA_BLOCK_BITS);
  265. bitmap_copy(bitmap, cmd->data_bitmap, DATA_BLOCK_BITS);
  266. if (!bidi) {
  267. data_sg = se_cmd->t_data_sg;
  268. data_nents = se_cmd->t_data_nents;
  269. } else {
  270. uint32_t count;
  271. /*
  272. * For bidi case, the first count blocks are for Data-Out
  273. * buffer blocks, and before gathering the Data-In buffer
  274. * the Data-Out buffer blocks should be discarded.
  275. */
  276. count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
  277. while (count--) {
  278. block = find_first_bit(bitmap, DATA_BLOCK_BITS);
  279. clear_bit(block, bitmap);
  280. }
  281. data_sg = se_cmd->t_bidi_data_sg;
  282. data_nents = se_cmd->t_bidi_data_nents;
  283. }
  284. for_each_sg(data_sg, sg, data_nents, i) {
  285. int sg_remaining = sg->length;
  286. to = kmap_atomic(sg_page(sg)) + sg->offset;
  287. while (sg_remaining > 0) {
  288. if (block_remaining == 0) {
  289. block = find_first_bit(bitmap,
  290. DATA_BLOCK_BITS);
  291. block_remaining = DATA_BLOCK_SIZE;
  292. clear_bit(block, bitmap);
  293. }
  294. copy_bytes = min_t(size_t, sg_remaining,
  295. block_remaining);
  296. from_offset = get_block_offset(udev, block,
  297. block_remaining);
  298. from = (void *) udev->mb_addr + from_offset;
  299. tcmu_flush_dcache_range(from, copy_bytes);
  300. memcpy(to + sg->length - sg_remaining, from,
  301. copy_bytes);
  302. sg_remaining -= copy_bytes;
  303. block_remaining -= copy_bytes;
  304. }
  305. kunmap_atomic(to - sg->offset);
  306. }
  307. }
  308. static inline size_t spc_bitmap_free(unsigned long *bitmap)
  309. {
  310. return DATA_BLOCK_SIZE * (DATA_BLOCK_BITS -
  311. bitmap_weight(bitmap, DATA_BLOCK_BITS));
  312. }
  313. /*
  314. * We can't queue a command until we have space available on the cmd ring *and*
  315. * space available on the data area.
  316. *
  317. * Called with ring lock held.
  318. */
  319. static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed)
  320. {
  321. struct tcmu_mailbox *mb = udev->mb_addr;
  322. size_t space, cmd_needed;
  323. u32 cmd_head;
  324. tcmu_flush_dcache_range(mb, sizeof(*mb));
  325. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  326. /*
  327. * If cmd end-of-ring space is too small then we need space for a NOP plus
  328. * original cmd - cmds are internally contiguous.
  329. */
  330. if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
  331. cmd_needed = cmd_size;
  332. else
  333. cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
  334. space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
  335. if (space < cmd_needed) {
  336. pr_debug("no cmd space: %u %u %u\n", cmd_head,
  337. udev->cmdr_last_cleaned, udev->cmdr_size);
  338. return false;
  339. }
  340. space = spc_bitmap_free(udev->data_bitmap);
  341. if (space < data_needed) {
  342. pr_debug("no data space: only %zu available, but ask for %zu\n",
  343. space, data_needed);
  344. return false;
  345. }
  346. return true;
  347. }
  348. static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
  349. {
  350. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  351. size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
  352. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  353. BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
  354. data_length += round_up(se_cmd->t_bidi_data_sg->length,
  355. DATA_BLOCK_SIZE);
  356. }
  357. return data_length;
  358. }
  359. static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
  360. {
  361. size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
  362. return data_length / DATA_BLOCK_SIZE;
  363. }
  364. static sense_reason_t
  365. tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
  366. {
  367. struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
  368. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  369. size_t base_command_size, command_size;
  370. struct tcmu_mailbox *mb;
  371. struct tcmu_cmd_entry *entry;
  372. struct iovec *iov;
  373. int iov_cnt;
  374. uint32_t cmd_head;
  375. uint64_t cdb_off;
  376. bool copy_to_data_area;
  377. size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
  378. DECLARE_BITMAP(old_bitmap, DATA_BLOCK_BITS);
  379. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
  380. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  381. /*
  382. * Must be a certain minimum size for response sense info, but
  383. * also may be larger if the iov array is large.
  384. *
  385. * We prepare way too many iovs for potential uses here, because it's
  386. * expensive to tell how many regions are freed in the bitmap
  387. */
  388. base_command_size = max(offsetof(struct tcmu_cmd_entry,
  389. req.iov[tcmu_cmd_get_block_cnt(tcmu_cmd)]),
  390. sizeof(struct tcmu_cmd_entry));
  391. command_size = base_command_size
  392. + round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE);
  393. WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
  394. spin_lock_irq(&udev->cmdr_lock);
  395. mb = udev->mb_addr;
  396. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  397. if ((command_size > (udev->cmdr_size / 2)) ||
  398. data_length > udev->data_size) {
  399. pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
  400. "cmd ring/data area\n", command_size, data_length,
  401. udev->cmdr_size, udev->data_size);
  402. spin_unlock_irq(&udev->cmdr_lock);
  403. return TCM_INVALID_CDB_FIELD;
  404. }
  405. while (!is_ring_space_avail(udev, command_size, data_length)) {
  406. int ret;
  407. DEFINE_WAIT(__wait);
  408. prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
  409. pr_debug("sleeping for ring space\n");
  410. spin_unlock_irq(&udev->cmdr_lock);
  411. ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
  412. finish_wait(&udev->wait_cmdr, &__wait);
  413. if (!ret) {
  414. pr_warn("tcmu: command timed out\n");
  415. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  416. }
  417. spin_lock_irq(&udev->cmdr_lock);
  418. /* We dropped cmdr_lock, cmd_head is stale */
  419. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  420. }
  421. /* Insert a PAD if end-of-ring space is too small */
  422. if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
  423. size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
  424. entry = (void *) mb + CMDR_OFF + cmd_head;
  425. tcmu_flush_dcache_range(entry, sizeof(*entry));
  426. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
  427. tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
  428. entry->hdr.cmd_id = 0; /* not used for PAD */
  429. entry->hdr.kflags = 0;
  430. entry->hdr.uflags = 0;
  431. UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
  432. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  433. WARN_ON(cmd_head != 0);
  434. }
  435. entry = (void *) mb + CMDR_OFF + cmd_head;
  436. tcmu_flush_dcache_range(entry, sizeof(*entry));
  437. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
  438. tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
  439. entry->hdr.cmd_id = tcmu_cmd->cmd_id;
  440. entry->hdr.kflags = 0;
  441. entry->hdr.uflags = 0;
  442. bitmap_copy(old_bitmap, udev->data_bitmap, DATA_BLOCK_BITS);
  443. /* Handle allocating space from the data area */
  444. iov = &entry->req.iov[0];
  445. iov_cnt = 0;
  446. copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
  447. || se_cmd->se_cmd_flags & SCF_BIDI);
  448. alloc_and_scatter_data_area(udev, se_cmd->t_data_sg,
  449. se_cmd->t_data_nents, &iov, &iov_cnt, copy_to_data_area);
  450. entry->req.iov_cnt = iov_cnt;
  451. entry->req.iov_dif_cnt = 0;
  452. /* Handle BIDI commands */
  453. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  454. iov_cnt = 0;
  455. iov++;
  456. alloc_and_scatter_data_area(udev, se_cmd->t_bidi_data_sg,
  457. se_cmd->t_bidi_data_nents, &iov, &iov_cnt,
  458. false);
  459. entry->req.iov_bidi_cnt = iov_cnt;
  460. }
  461. /* cmd's data_bitmap is what changed in process */
  462. bitmap_xor(tcmu_cmd->data_bitmap, old_bitmap, udev->data_bitmap,
  463. DATA_BLOCK_BITS);
  464. /* All offsets relative to mb_addr, not start of entry! */
  465. cdb_off = CMDR_OFF + cmd_head + base_command_size;
  466. memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
  467. entry->req.cdb_off = cdb_off;
  468. tcmu_flush_dcache_range(entry, sizeof(*entry));
  469. UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
  470. tcmu_flush_dcache_range(mb, sizeof(*mb));
  471. spin_unlock_irq(&udev->cmdr_lock);
  472. /* TODO: only if FLUSH and FUA? */
  473. uio_event_notify(&udev->uio_info);
  474. mod_timer(&udev->timeout,
  475. round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT)));
  476. return TCM_NO_SENSE;
  477. }
  478. static sense_reason_t
  479. tcmu_queue_cmd(struct se_cmd *se_cmd)
  480. {
  481. struct se_device *se_dev = se_cmd->se_dev;
  482. struct tcmu_dev *udev = TCMU_DEV(se_dev);
  483. struct tcmu_cmd *tcmu_cmd;
  484. int ret;
  485. tcmu_cmd = tcmu_alloc_cmd(se_cmd);
  486. if (!tcmu_cmd)
  487. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  488. ret = tcmu_queue_cmd_ring(tcmu_cmd);
  489. if (ret != TCM_NO_SENSE) {
  490. pr_err("TCMU: Could not queue command\n");
  491. spin_lock_irq(&udev->commands_lock);
  492. idr_remove(&udev->commands, tcmu_cmd->cmd_id);
  493. spin_unlock_irq(&udev->commands_lock);
  494. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  495. }
  496. return ret;
  497. }
  498. static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
  499. {
  500. struct se_cmd *se_cmd = cmd->se_cmd;
  501. struct tcmu_dev *udev = cmd->tcmu_dev;
  502. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
  503. /*
  504. * cmd has been completed already from timeout, just reclaim
  505. * data area space and free cmd
  506. */
  507. free_data_area(udev, cmd);
  508. kmem_cache_free(tcmu_cmd_cache, cmd);
  509. return;
  510. }
  511. if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
  512. free_data_area(udev, cmd);
  513. pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
  514. cmd->se_cmd);
  515. entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
  516. } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
  517. memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer,
  518. se_cmd->scsi_sense_length);
  519. free_data_area(udev, cmd);
  520. } else if (se_cmd->se_cmd_flags & SCF_BIDI) {
  521. /* Get Data-In buffer before clean up */
  522. gather_data_area(udev, cmd, true);
  523. free_data_area(udev, cmd);
  524. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  525. gather_data_area(udev, cmd, false);
  526. free_data_area(udev, cmd);
  527. } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
  528. free_data_area(udev, cmd);
  529. } else if (se_cmd->data_direction != DMA_NONE) {
  530. pr_warn("TCMU: data direction was %d!\n",
  531. se_cmd->data_direction);
  532. }
  533. target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
  534. cmd->se_cmd = NULL;
  535. kmem_cache_free(tcmu_cmd_cache, cmd);
  536. }
  537. static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
  538. {
  539. struct tcmu_mailbox *mb;
  540. unsigned long flags;
  541. int handled = 0;
  542. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
  543. pr_err("ring broken, not handling completions\n");
  544. return 0;
  545. }
  546. spin_lock_irqsave(&udev->cmdr_lock, flags);
  547. mb = udev->mb_addr;
  548. tcmu_flush_dcache_range(mb, sizeof(*mb));
  549. while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
  550. struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
  551. struct tcmu_cmd *cmd;
  552. tcmu_flush_dcache_range(entry, sizeof(*entry));
  553. if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
  554. UPDATE_HEAD(udev->cmdr_last_cleaned,
  555. tcmu_hdr_get_len(entry->hdr.len_op),
  556. udev->cmdr_size);
  557. continue;
  558. }
  559. WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
  560. spin_lock(&udev->commands_lock);
  561. cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
  562. if (cmd)
  563. idr_remove(&udev->commands, cmd->cmd_id);
  564. spin_unlock(&udev->commands_lock);
  565. if (!cmd) {
  566. pr_err("cmd_id not found, ring is broken\n");
  567. set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
  568. break;
  569. }
  570. tcmu_handle_completion(cmd, entry);
  571. UPDATE_HEAD(udev->cmdr_last_cleaned,
  572. tcmu_hdr_get_len(entry->hdr.len_op),
  573. udev->cmdr_size);
  574. handled++;
  575. }
  576. if (mb->cmd_tail == mb->cmd_head)
  577. del_timer(&udev->timeout); /* no more pending cmds */
  578. spin_unlock_irqrestore(&udev->cmdr_lock, flags);
  579. wake_up(&udev->wait_cmdr);
  580. return handled;
  581. }
  582. static int tcmu_check_expired_cmd(int id, void *p, void *data)
  583. {
  584. struct tcmu_cmd *cmd = p;
  585. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  586. return 0;
  587. if (!time_after(jiffies, cmd->deadline))
  588. return 0;
  589. set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
  590. target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
  591. cmd->se_cmd = NULL;
  592. return 0;
  593. }
  594. static void tcmu_device_timedout(unsigned long data)
  595. {
  596. struct tcmu_dev *udev = (struct tcmu_dev *)data;
  597. unsigned long flags;
  598. int handled;
  599. handled = tcmu_handle_completions(udev);
  600. pr_warn("%d completions handled from timeout\n", handled);
  601. spin_lock_irqsave(&udev->commands_lock, flags);
  602. idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
  603. spin_unlock_irqrestore(&udev->commands_lock, flags);
  604. /*
  605. * We don't need to wakeup threads on wait_cmdr since they have their
  606. * own timeout.
  607. */
  608. }
  609. static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
  610. {
  611. struct tcmu_hba *tcmu_hba;
  612. tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
  613. if (!tcmu_hba)
  614. return -ENOMEM;
  615. tcmu_hba->host_id = host_id;
  616. hba->hba_ptr = tcmu_hba;
  617. return 0;
  618. }
  619. static void tcmu_detach_hba(struct se_hba *hba)
  620. {
  621. kfree(hba->hba_ptr);
  622. hba->hba_ptr = NULL;
  623. }
  624. static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
  625. {
  626. struct tcmu_dev *udev;
  627. udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
  628. if (!udev)
  629. return NULL;
  630. udev->name = kstrdup(name, GFP_KERNEL);
  631. if (!udev->name) {
  632. kfree(udev);
  633. return NULL;
  634. }
  635. udev->hba = hba;
  636. init_waitqueue_head(&udev->wait_cmdr);
  637. spin_lock_init(&udev->cmdr_lock);
  638. idr_init(&udev->commands);
  639. spin_lock_init(&udev->commands_lock);
  640. setup_timer(&udev->timeout, tcmu_device_timedout,
  641. (unsigned long)udev);
  642. return &udev->se_dev;
  643. }
  644. static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
  645. {
  646. struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
  647. tcmu_handle_completions(tcmu_dev);
  648. return 0;
  649. }
  650. /*
  651. * mmap code from uio.c. Copied here because we want to hook mmap()
  652. * and this stuff must come along.
  653. */
  654. static int tcmu_find_mem_index(struct vm_area_struct *vma)
  655. {
  656. struct tcmu_dev *udev = vma->vm_private_data;
  657. struct uio_info *info = &udev->uio_info;
  658. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  659. if (info->mem[vma->vm_pgoff].size == 0)
  660. return -1;
  661. return (int)vma->vm_pgoff;
  662. }
  663. return -1;
  664. }
  665. static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  666. {
  667. struct tcmu_dev *udev = vma->vm_private_data;
  668. struct uio_info *info = &udev->uio_info;
  669. struct page *page;
  670. unsigned long offset;
  671. void *addr;
  672. int mi = tcmu_find_mem_index(vma);
  673. if (mi < 0)
  674. return VM_FAULT_SIGBUS;
  675. /*
  676. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  677. * to use mem[N].
  678. */
  679. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  680. addr = (void *)(unsigned long)info->mem[mi].addr + offset;
  681. if (info->mem[mi].memtype == UIO_MEM_LOGICAL)
  682. page = virt_to_page(addr);
  683. else
  684. page = vmalloc_to_page(addr);
  685. get_page(page);
  686. vmf->page = page;
  687. return 0;
  688. }
  689. static const struct vm_operations_struct tcmu_vm_ops = {
  690. .fault = tcmu_vma_fault,
  691. };
  692. static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
  693. {
  694. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  695. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  696. vma->vm_ops = &tcmu_vm_ops;
  697. vma->vm_private_data = udev;
  698. /* Ensure the mmap is exactly the right size */
  699. if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
  700. return -EINVAL;
  701. return 0;
  702. }
  703. static int tcmu_open(struct uio_info *info, struct inode *inode)
  704. {
  705. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  706. /* O_EXCL not supported for char devs, so fake it? */
  707. if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
  708. return -EBUSY;
  709. pr_debug("open\n");
  710. return 0;
  711. }
  712. static int tcmu_release(struct uio_info *info, struct inode *inode)
  713. {
  714. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  715. clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
  716. pr_debug("close\n");
  717. return 0;
  718. }
  719. static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
  720. {
  721. struct sk_buff *skb;
  722. void *msg_header;
  723. int ret = -ENOMEM;
  724. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  725. if (!skb)
  726. return ret;
  727. msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
  728. if (!msg_header)
  729. goto free_skb;
  730. ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
  731. if (ret < 0)
  732. goto free_skb;
  733. ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
  734. if (ret < 0)
  735. goto free_skb;
  736. genlmsg_end(skb, msg_header);
  737. ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
  738. TCMU_MCGRP_CONFIG, GFP_KERNEL);
  739. /* We don't care if no one is listening */
  740. if (ret == -ESRCH)
  741. ret = 0;
  742. return ret;
  743. free_skb:
  744. nlmsg_free(skb);
  745. return ret;
  746. }
  747. static int tcmu_configure_device(struct se_device *dev)
  748. {
  749. struct tcmu_dev *udev = TCMU_DEV(dev);
  750. struct tcmu_hba *hba = udev->hba->hba_ptr;
  751. struct uio_info *info;
  752. struct tcmu_mailbox *mb;
  753. size_t size;
  754. size_t used;
  755. int ret = 0;
  756. char *str;
  757. info = &udev->uio_info;
  758. size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
  759. udev->dev_config);
  760. size += 1; /* for \0 */
  761. str = kmalloc(size, GFP_KERNEL);
  762. if (!str)
  763. return -ENOMEM;
  764. used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
  765. if (udev->dev_config[0])
  766. snprintf(str + used, size - used, "/%s", udev->dev_config);
  767. info->name = str;
  768. udev->mb_addr = vzalloc(TCMU_RING_SIZE);
  769. if (!udev->mb_addr) {
  770. ret = -ENOMEM;
  771. goto err_vzalloc;
  772. }
  773. /* mailbox fits in first part of CMDR space */
  774. udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
  775. udev->data_off = CMDR_SIZE;
  776. udev->data_size = TCMU_RING_SIZE - CMDR_SIZE;
  777. mb = udev->mb_addr;
  778. mb->version = TCMU_MAILBOX_VERSION;
  779. mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
  780. mb->cmdr_off = CMDR_OFF;
  781. mb->cmdr_size = udev->cmdr_size;
  782. WARN_ON(!PAGE_ALIGNED(udev->data_off));
  783. WARN_ON(udev->data_size % PAGE_SIZE);
  784. WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
  785. info->version = __stringify(TCMU_MAILBOX_VERSION);
  786. info->mem[0].name = "tcm-user command & data buffer";
  787. info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
  788. info->mem[0].size = TCMU_RING_SIZE;
  789. info->mem[0].memtype = UIO_MEM_VIRTUAL;
  790. info->irqcontrol = tcmu_irqcontrol;
  791. info->irq = UIO_IRQ_CUSTOM;
  792. info->mmap = tcmu_mmap;
  793. info->open = tcmu_open;
  794. info->release = tcmu_release;
  795. ret = uio_register_device(tcmu_root_device, info);
  796. if (ret)
  797. goto err_register;
  798. /* User can set hw_block_size before enable the device */
  799. if (dev->dev_attrib.hw_block_size == 0)
  800. dev->dev_attrib.hw_block_size = 512;
  801. /* Other attributes can be configured in userspace */
  802. dev->dev_attrib.hw_max_sectors = 128;
  803. dev->dev_attrib.hw_queue_depth = 128;
  804. ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
  805. udev->uio_info.uio_dev->minor);
  806. if (ret)
  807. goto err_netlink;
  808. return 0;
  809. err_netlink:
  810. uio_unregister_device(&udev->uio_info);
  811. err_register:
  812. vfree(udev->mb_addr);
  813. err_vzalloc:
  814. kfree(info->name);
  815. return ret;
  816. }
  817. static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
  818. {
  819. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
  820. kmem_cache_free(tcmu_cmd_cache, cmd);
  821. return 0;
  822. }
  823. return -EINVAL;
  824. }
  825. static void tcmu_dev_call_rcu(struct rcu_head *p)
  826. {
  827. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  828. struct tcmu_dev *udev = TCMU_DEV(dev);
  829. kfree(udev);
  830. }
  831. static void tcmu_free_device(struct se_device *dev)
  832. {
  833. struct tcmu_dev *udev = TCMU_DEV(dev);
  834. struct tcmu_cmd *cmd;
  835. bool all_expired = true;
  836. int i;
  837. del_timer_sync(&udev->timeout);
  838. vfree(udev->mb_addr);
  839. /* Upper layer should drain all requests before calling this */
  840. spin_lock_irq(&udev->commands_lock);
  841. idr_for_each_entry(&udev->commands, cmd, i) {
  842. if (tcmu_check_and_free_pending_cmd(cmd) != 0)
  843. all_expired = false;
  844. }
  845. idr_destroy(&udev->commands);
  846. spin_unlock_irq(&udev->commands_lock);
  847. WARN_ON(!all_expired);
  848. /* Device was configured */
  849. if (udev->uio_info.uio_dev) {
  850. tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
  851. udev->uio_info.uio_dev->minor);
  852. uio_unregister_device(&udev->uio_info);
  853. kfree(udev->uio_info.name);
  854. kfree(udev->name);
  855. }
  856. call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
  857. }
  858. enum {
  859. Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err,
  860. };
  861. static match_table_t tokens = {
  862. {Opt_dev_config, "dev_config=%s"},
  863. {Opt_dev_size, "dev_size=%u"},
  864. {Opt_hw_block_size, "hw_block_size=%u"},
  865. {Opt_err, NULL}
  866. };
  867. static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
  868. const char *page, ssize_t count)
  869. {
  870. struct tcmu_dev *udev = TCMU_DEV(dev);
  871. char *orig, *ptr, *opts, *arg_p;
  872. substring_t args[MAX_OPT_ARGS];
  873. int ret = 0, token;
  874. unsigned long tmp_ul;
  875. opts = kstrdup(page, GFP_KERNEL);
  876. if (!opts)
  877. return -ENOMEM;
  878. orig = opts;
  879. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  880. if (!*ptr)
  881. continue;
  882. token = match_token(ptr, tokens, args);
  883. switch (token) {
  884. case Opt_dev_config:
  885. if (match_strlcpy(udev->dev_config, &args[0],
  886. TCMU_CONFIG_LEN) == 0) {
  887. ret = -EINVAL;
  888. break;
  889. }
  890. pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
  891. break;
  892. case Opt_dev_size:
  893. arg_p = match_strdup(&args[0]);
  894. if (!arg_p) {
  895. ret = -ENOMEM;
  896. break;
  897. }
  898. ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
  899. kfree(arg_p);
  900. if (ret < 0)
  901. pr_err("kstrtoul() failed for dev_size=\n");
  902. break;
  903. case Opt_hw_block_size:
  904. arg_p = match_strdup(&args[0]);
  905. if (!arg_p) {
  906. ret = -ENOMEM;
  907. break;
  908. }
  909. ret = kstrtoul(arg_p, 0, &tmp_ul);
  910. kfree(arg_p);
  911. if (ret < 0) {
  912. pr_err("kstrtoul() failed for hw_block_size=\n");
  913. break;
  914. }
  915. if (!tmp_ul) {
  916. pr_err("hw_block_size must be nonzero\n");
  917. break;
  918. }
  919. dev->dev_attrib.hw_block_size = tmp_ul;
  920. break;
  921. default:
  922. break;
  923. }
  924. }
  925. kfree(orig);
  926. return (!ret) ? count : ret;
  927. }
  928. static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
  929. {
  930. struct tcmu_dev *udev = TCMU_DEV(dev);
  931. ssize_t bl = 0;
  932. bl = sprintf(b + bl, "Config: %s ",
  933. udev->dev_config[0] ? udev->dev_config : "NULL");
  934. bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
  935. return bl;
  936. }
  937. static sector_t tcmu_get_blocks(struct se_device *dev)
  938. {
  939. struct tcmu_dev *udev = TCMU_DEV(dev);
  940. return div_u64(udev->dev_size - dev->dev_attrib.block_size,
  941. dev->dev_attrib.block_size);
  942. }
  943. static sense_reason_t
  944. tcmu_parse_cdb(struct se_cmd *cmd)
  945. {
  946. return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
  947. }
  948. static const struct target_backend_ops tcmu_ops = {
  949. .name = "user",
  950. .owner = THIS_MODULE,
  951. .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
  952. .attach_hba = tcmu_attach_hba,
  953. .detach_hba = tcmu_detach_hba,
  954. .alloc_device = tcmu_alloc_device,
  955. .configure_device = tcmu_configure_device,
  956. .free_device = tcmu_free_device,
  957. .parse_cdb = tcmu_parse_cdb,
  958. .set_configfs_dev_params = tcmu_set_configfs_dev_params,
  959. .show_configfs_dev_params = tcmu_show_configfs_dev_params,
  960. .get_device_type = sbc_get_device_type,
  961. .get_blocks = tcmu_get_blocks,
  962. .tb_dev_attrib_attrs = passthrough_attrib_attrs,
  963. };
  964. static int __init tcmu_module_init(void)
  965. {
  966. int ret;
  967. BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
  968. tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
  969. sizeof(struct tcmu_cmd),
  970. __alignof__(struct tcmu_cmd),
  971. 0, NULL);
  972. if (!tcmu_cmd_cache)
  973. return -ENOMEM;
  974. tcmu_root_device = root_device_register("tcm_user");
  975. if (IS_ERR(tcmu_root_device)) {
  976. ret = PTR_ERR(tcmu_root_device);
  977. goto out_free_cache;
  978. }
  979. ret = genl_register_family(&tcmu_genl_family);
  980. if (ret < 0) {
  981. goto out_unreg_device;
  982. }
  983. ret = transport_backend_register(&tcmu_ops);
  984. if (ret)
  985. goto out_unreg_genl;
  986. return 0;
  987. out_unreg_genl:
  988. genl_unregister_family(&tcmu_genl_family);
  989. out_unreg_device:
  990. root_device_unregister(tcmu_root_device);
  991. out_free_cache:
  992. kmem_cache_destroy(tcmu_cmd_cache);
  993. return ret;
  994. }
  995. static void __exit tcmu_module_exit(void)
  996. {
  997. target_backend_unregister(&tcmu_ops);
  998. genl_unregister_family(&tcmu_genl_family);
  999. root_device_unregister(tcmu_root_device);
  1000. kmem_cache_destroy(tcmu_cmd_cache);
  1001. }
  1002. MODULE_DESCRIPTION("TCM USER subsystem plugin");
  1003. MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
  1004. MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
  1005. MODULE_LICENSE("GPL");
  1006. module_init(tcmu_module_init);
  1007. module_exit(tcmu_module_exit);