target_core_user.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. /*
  2. * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
  3. * Copyright (C) 2014 Red Hat, Inc.
  4. * Copyright (C) 2015 Arrikto, Inc.
  5. * Copyright (C) 2017 Chinamobile, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/spinlock.h>
  21. #include <linux/module.h>
  22. #include <linux/idr.h>
  23. #include <linux/kernel.h>
  24. #include <linux/timer.h>
  25. #include <linux/parser.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/uio_driver.h>
  28. #include <linux/radix-tree.h>
  29. #include <linux/stringify.h>
  30. #include <linux/bitops.h>
  31. #include <linux/highmem.h>
  32. #include <linux/configfs.h>
  33. #include <linux/mutex.h>
  34. #include <linux/kthread.h>
  35. #include <net/genetlink.h>
  36. #include <scsi/scsi_common.h>
  37. #include <scsi/scsi_proto.h>
  38. #include <target/target_core_base.h>
  39. #include <target/target_core_fabric.h>
  40. #include <target/target_core_backend.h>
  41. #include <linux/target_core_user.h>
  42. /*
  43. * Define a shared-memory interface for LIO to pass SCSI commands and
  44. * data to userspace for processing. This is to allow backends that
  45. * are too complex for in-kernel support to be possible.
  46. *
  47. * It uses the UIO framework to do a lot of the device-creation and
  48. * introspection work for us.
  49. *
  50. * See the .h file for how the ring is laid out. Note that while the
  51. * command ring is defined, the particulars of the data area are
  52. * not. Offset values in the command entry point to other locations
  53. * internal to the mmap()ed area. There is separate space outside the
  54. * command ring for data buffers. This leaves maximum flexibility for
  55. * moving buffer allocations, or even page flipping or other
  56. * allocation techniques, without altering the command ring layout.
  57. *
  58. * SECURITY:
  59. * The user process must be assumed to be malicious. There's no way to
  60. * prevent it breaking the command ring protocol if it wants, but in
  61. * order to prevent other issues we must only ever read *data* from
  62. * the shared memory area, not offsets or sizes. This applies to
  63. * command ring entries as well as the mailbox. Extra code needed for
  64. * this may have a 'UAM' comment.
  65. */
  66. #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
  67. /* For cmd area, the size is fixed 8MB */
  68. #define CMDR_SIZE (8 * 1024 * 1024)
  69. /*
  70. * For data area, the block size is PAGE_SIZE and
  71. * the total size is 256K * PAGE_SIZE.
  72. */
  73. #define DATA_BLOCK_SIZE PAGE_SIZE
  74. #define DATA_BLOCK_BITS (256 * 1024)
  75. #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
  76. #define DATA_BLOCK_INIT_BITS 128
  77. /* The total size of the ring is 8M + 256K * PAGE_SIZE */
  78. #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
  79. /* Default maximum of the global data blocks(512K * PAGE_SIZE) */
  80. #define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
  81. static u8 tcmu_kern_cmd_reply_supported;
  82. static struct device *tcmu_root_device;
  83. struct tcmu_hba {
  84. u32 host_id;
  85. };
  86. #define TCMU_CONFIG_LEN 256
  87. struct tcmu_nl_cmd {
  88. /* wake up thread waiting for reply */
  89. struct completion complete;
  90. int cmd;
  91. int status;
  92. };
  93. struct tcmu_dev {
  94. struct list_head node;
  95. struct kref kref;
  96. struct se_device se_dev;
  97. char *name;
  98. struct se_hba *hba;
  99. #define TCMU_DEV_BIT_OPEN 0
  100. #define TCMU_DEV_BIT_BROKEN 1
  101. unsigned long flags;
  102. struct uio_info uio_info;
  103. struct inode *inode;
  104. struct tcmu_mailbox *mb_addr;
  105. size_t dev_size;
  106. u32 cmdr_size;
  107. u32 cmdr_last_cleaned;
  108. /* Offset of data area from start of mb */
  109. /* Must add data_off and mb_addr to get the address */
  110. size_t data_off;
  111. size_t data_size;
  112. wait_queue_head_t wait_cmdr;
  113. struct mutex cmdr_lock;
  114. bool waiting_global;
  115. uint32_t dbi_max;
  116. uint32_t dbi_thresh;
  117. DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
  118. struct radix_tree_root data_blocks;
  119. struct idr commands;
  120. spinlock_t commands_lock;
  121. struct timer_list timeout;
  122. unsigned int cmd_time_out;
  123. spinlock_t nl_cmd_lock;
  124. struct tcmu_nl_cmd curr_nl_cmd;
  125. /* wake up threads waiting on curr_nl_cmd */
  126. wait_queue_head_t nl_cmd_wq;
  127. char dev_config[TCMU_CONFIG_LEN];
  128. int nl_reply_supported;
  129. };
  130. #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
  131. #define CMDR_OFF sizeof(struct tcmu_mailbox)
  132. struct tcmu_cmd {
  133. struct se_cmd *se_cmd;
  134. struct tcmu_dev *tcmu_dev;
  135. uint16_t cmd_id;
  136. /* Can't use se_cmd when cleaning up expired cmds, because if
  137. cmd has been completed then accessing se_cmd is off limits */
  138. uint32_t dbi_cnt;
  139. uint32_t dbi_cur;
  140. uint32_t *dbi;
  141. unsigned long deadline;
  142. #define TCMU_CMD_BIT_EXPIRED 0
  143. unsigned long flags;
  144. };
  145. static struct task_struct *unmap_thread;
  146. static wait_queue_head_t unmap_wait;
  147. static DEFINE_MUTEX(root_udev_mutex);
  148. static LIST_HEAD(root_udev);
  149. static atomic_t global_db_count = ATOMIC_INIT(0);
  150. static struct kmem_cache *tcmu_cmd_cache;
  151. /* multicast group */
  152. enum tcmu_multicast_groups {
  153. TCMU_MCGRP_CONFIG,
  154. };
  155. static const struct genl_multicast_group tcmu_mcgrps[] = {
  156. [TCMU_MCGRP_CONFIG] = { .name = "config", },
  157. };
  158. static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
  159. [TCMU_ATTR_DEVICE] = { .type = NLA_STRING },
  160. [TCMU_ATTR_MINOR] = { .type = NLA_U32 },
  161. [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 },
  162. [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 },
  163. [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
  164. };
  165. static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
  166. {
  167. struct se_device *dev;
  168. struct tcmu_dev *udev;
  169. struct tcmu_nl_cmd *nl_cmd;
  170. int dev_id, rc, ret = 0;
  171. bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE);
  172. if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
  173. !info->attrs[TCMU_ATTR_DEVICE_ID]) {
  174. printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
  175. return -EINVAL;
  176. }
  177. dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
  178. rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
  179. dev = target_find_device(dev_id, !is_removed);
  180. if (!dev) {
  181. printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n",
  182. completed_cmd, rc, dev_id);
  183. return -ENODEV;
  184. }
  185. udev = TCMU_DEV(dev);
  186. spin_lock(&udev->nl_cmd_lock);
  187. nl_cmd = &udev->curr_nl_cmd;
  188. pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id,
  189. nl_cmd->cmd, completed_cmd, rc);
  190. if (nl_cmd->cmd != completed_cmd) {
  191. printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n",
  192. completed_cmd, nl_cmd->cmd);
  193. ret = -EINVAL;
  194. } else {
  195. nl_cmd->status = rc;
  196. }
  197. spin_unlock(&udev->nl_cmd_lock);
  198. if (!is_removed)
  199. target_undepend_item(&dev->dev_group.cg_item);
  200. if (!ret)
  201. complete(&nl_cmd->complete);
  202. return ret;
  203. }
  204. static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
  205. {
  206. return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
  207. }
  208. static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
  209. {
  210. return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
  211. }
  212. static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
  213. struct genl_info *info)
  214. {
  215. return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
  216. }
  217. static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
  218. {
  219. if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
  220. tcmu_kern_cmd_reply_supported =
  221. nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
  222. printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
  223. tcmu_kern_cmd_reply_supported);
  224. }
  225. return 0;
  226. }
  227. static const struct genl_ops tcmu_genl_ops[] = {
  228. {
  229. .cmd = TCMU_CMD_SET_FEATURES,
  230. .flags = GENL_ADMIN_PERM,
  231. .policy = tcmu_attr_policy,
  232. .doit = tcmu_genl_set_features,
  233. },
  234. {
  235. .cmd = TCMU_CMD_ADDED_DEVICE_DONE,
  236. .flags = GENL_ADMIN_PERM,
  237. .policy = tcmu_attr_policy,
  238. .doit = tcmu_genl_add_dev_done,
  239. },
  240. {
  241. .cmd = TCMU_CMD_REMOVED_DEVICE_DONE,
  242. .flags = GENL_ADMIN_PERM,
  243. .policy = tcmu_attr_policy,
  244. .doit = tcmu_genl_rm_dev_done,
  245. },
  246. {
  247. .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE,
  248. .flags = GENL_ADMIN_PERM,
  249. .policy = tcmu_attr_policy,
  250. .doit = tcmu_genl_reconfig_dev_done,
  251. },
  252. };
  253. /* Our generic netlink family */
  254. static struct genl_family tcmu_genl_family __ro_after_init = {
  255. .module = THIS_MODULE,
  256. .hdrsize = 0,
  257. .name = "TCM-USER",
  258. .version = 2,
  259. .maxattr = TCMU_ATTR_MAX,
  260. .mcgrps = tcmu_mcgrps,
  261. .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
  262. .netnsok = true,
  263. .ops = tcmu_genl_ops,
  264. .n_ops = ARRAY_SIZE(tcmu_genl_ops),
  265. };
  266. #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
  267. #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
  268. #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
  269. #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
  270. static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
  271. {
  272. struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
  273. uint32_t i;
  274. for (i = 0; i < len; i++)
  275. clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
  276. }
  277. static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
  278. struct tcmu_cmd *tcmu_cmd)
  279. {
  280. struct page *page;
  281. int ret, dbi;
  282. dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
  283. if (dbi == udev->dbi_thresh)
  284. return false;
  285. page = radix_tree_lookup(&udev->data_blocks, dbi);
  286. if (!page) {
  287. if (atomic_add_return(1, &global_db_count) >
  288. TCMU_GLOBAL_MAX_BLOCKS) {
  289. atomic_dec(&global_db_count);
  290. return false;
  291. }
  292. /* try to get new page from the mm */
  293. page = alloc_page(GFP_KERNEL);
  294. if (!page)
  295. goto err_alloc;
  296. ret = radix_tree_insert(&udev->data_blocks, dbi, page);
  297. if (ret)
  298. goto err_insert;
  299. }
  300. if (dbi > udev->dbi_max)
  301. udev->dbi_max = dbi;
  302. set_bit(dbi, udev->data_bitmap);
  303. tcmu_cmd_set_dbi(tcmu_cmd, dbi);
  304. return true;
  305. err_insert:
  306. __free_page(page);
  307. err_alloc:
  308. atomic_dec(&global_db_count);
  309. return false;
  310. }
  311. static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
  312. struct tcmu_cmd *tcmu_cmd)
  313. {
  314. int i;
  315. udev->waiting_global = false;
  316. for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
  317. if (!tcmu_get_empty_block(udev, tcmu_cmd))
  318. goto err;
  319. }
  320. return true;
  321. err:
  322. udev->waiting_global = true;
  323. /* Try to wake up the unmap thread */
  324. wake_up(&unmap_wait);
  325. return false;
  326. }
  327. static inline struct page *
  328. tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
  329. {
  330. return radix_tree_lookup(&udev->data_blocks, dbi);
  331. }
  332. static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
  333. {
  334. kfree(tcmu_cmd->dbi);
  335. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  336. }
  337. static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
  338. {
  339. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  340. size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
  341. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  342. BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
  343. data_length += round_up(se_cmd->t_bidi_data_sg->length,
  344. DATA_BLOCK_SIZE);
  345. }
  346. return data_length;
  347. }
  348. static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
  349. {
  350. size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
  351. return data_length / DATA_BLOCK_SIZE;
  352. }
  353. static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
  354. {
  355. struct se_device *se_dev = se_cmd->se_dev;
  356. struct tcmu_dev *udev = TCMU_DEV(se_dev);
  357. struct tcmu_cmd *tcmu_cmd;
  358. tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
  359. if (!tcmu_cmd)
  360. return NULL;
  361. tcmu_cmd->se_cmd = se_cmd;
  362. tcmu_cmd->tcmu_dev = udev;
  363. tcmu_cmd_reset_dbi_cur(tcmu_cmd);
  364. tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
  365. tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
  366. GFP_KERNEL);
  367. if (!tcmu_cmd->dbi) {
  368. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  369. return NULL;
  370. }
  371. return tcmu_cmd;
  372. }
  373. static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
  374. {
  375. unsigned long offset = offset_in_page(vaddr);
  376. size = round_up(size+offset, PAGE_SIZE);
  377. vaddr -= offset;
  378. while (size) {
  379. flush_dcache_page(virt_to_page(vaddr));
  380. size -= PAGE_SIZE;
  381. }
  382. }
  383. /*
  384. * Some ring helper functions. We don't assume size is a power of 2 so
  385. * we can't use circ_buf.h.
  386. */
  387. static inline size_t spc_used(size_t head, size_t tail, size_t size)
  388. {
  389. int diff = head - tail;
  390. if (diff >= 0)
  391. return diff;
  392. else
  393. return size + diff;
  394. }
  395. static inline size_t spc_free(size_t head, size_t tail, size_t size)
  396. {
  397. /* Keep 1 byte unused or we can't tell full from empty */
  398. return (size - spc_used(head, tail, size) - 1);
  399. }
  400. static inline size_t head_to_end(size_t head, size_t size)
  401. {
  402. return size - head;
  403. }
  404. static inline void new_iov(struct iovec **iov, int *iov_cnt,
  405. struct tcmu_dev *udev)
  406. {
  407. struct iovec *iovec;
  408. if (*iov_cnt != 0)
  409. (*iov)++;
  410. (*iov_cnt)++;
  411. iovec = *iov;
  412. memset(iovec, 0, sizeof(struct iovec));
  413. }
  414. #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
  415. /* offset is relative to mb_addr */
  416. static inline size_t get_block_offset_user(struct tcmu_dev *dev,
  417. int dbi, int remaining)
  418. {
  419. return dev->data_off + dbi * DATA_BLOCK_SIZE +
  420. DATA_BLOCK_SIZE - remaining;
  421. }
  422. static inline size_t iov_tail(struct iovec *iov)
  423. {
  424. return (size_t)iov->iov_base + iov->iov_len;
  425. }
  426. static int scatter_data_area(struct tcmu_dev *udev,
  427. struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
  428. unsigned int data_nents, struct iovec **iov,
  429. int *iov_cnt, bool copy_data)
  430. {
  431. int i, dbi;
  432. int block_remaining = 0;
  433. void *from, *to = NULL;
  434. size_t copy_bytes, to_offset, offset;
  435. struct scatterlist *sg;
  436. struct page *page;
  437. for_each_sg(data_sg, sg, data_nents, i) {
  438. int sg_remaining = sg->length;
  439. from = kmap_atomic(sg_page(sg)) + sg->offset;
  440. while (sg_remaining > 0) {
  441. if (block_remaining == 0) {
  442. if (to)
  443. kunmap_atomic(to);
  444. block_remaining = DATA_BLOCK_SIZE;
  445. dbi = tcmu_cmd_get_dbi(tcmu_cmd);
  446. page = tcmu_get_block_page(udev, dbi);
  447. to = kmap_atomic(page);
  448. }
  449. copy_bytes = min_t(size_t, sg_remaining,
  450. block_remaining);
  451. to_offset = get_block_offset_user(udev, dbi,
  452. block_remaining);
  453. if (*iov_cnt != 0 &&
  454. to_offset == iov_tail(*iov)) {
  455. (*iov)->iov_len += copy_bytes;
  456. } else {
  457. new_iov(iov, iov_cnt, udev);
  458. (*iov)->iov_base = (void __user *)to_offset;
  459. (*iov)->iov_len = copy_bytes;
  460. }
  461. if (copy_data) {
  462. offset = DATA_BLOCK_SIZE - block_remaining;
  463. memcpy(to + offset,
  464. from + sg->length - sg_remaining,
  465. copy_bytes);
  466. tcmu_flush_dcache_range(to, copy_bytes);
  467. }
  468. sg_remaining -= copy_bytes;
  469. block_remaining -= copy_bytes;
  470. }
  471. kunmap_atomic(from - sg->offset);
  472. }
  473. if (to)
  474. kunmap_atomic(to);
  475. return 0;
  476. }
  477. static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
  478. bool bidi)
  479. {
  480. struct se_cmd *se_cmd = cmd->se_cmd;
  481. int i, dbi;
  482. int block_remaining = 0;
  483. void *from = NULL, *to;
  484. size_t copy_bytes, offset;
  485. struct scatterlist *sg, *data_sg;
  486. struct page *page;
  487. unsigned int data_nents;
  488. uint32_t count = 0;
  489. if (!bidi) {
  490. data_sg = se_cmd->t_data_sg;
  491. data_nents = se_cmd->t_data_nents;
  492. } else {
  493. /*
  494. * For bidi case, the first count blocks are for Data-Out
  495. * buffer blocks, and before gathering the Data-In buffer
  496. * the Data-Out buffer blocks should be discarded.
  497. */
  498. count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
  499. data_sg = se_cmd->t_bidi_data_sg;
  500. data_nents = se_cmd->t_bidi_data_nents;
  501. }
  502. tcmu_cmd_set_dbi_cur(cmd, count);
  503. for_each_sg(data_sg, sg, data_nents, i) {
  504. int sg_remaining = sg->length;
  505. to = kmap_atomic(sg_page(sg)) + sg->offset;
  506. while (sg_remaining > 0) {
  507. if (block_remaining == 0) {
  508. if (from)
  509. kunmap_atomic(from);
  510. block_remaining = DATA_BLOCK_SIZE;
  511. dbi = tcmu_cmd_get_dbi(cmd);
  512. page = tcmu_get_block_page(udev, dbi);
  513. from = kmap_atomic(page);
  514. }
  515. copy_bytes = min_t(size_t, sg_remaining,
  516. block_remaining);
  517. offset = DATA_BLOCK_SIZE - block_remaining;
  518. tcmu_flush_dcache_range(from, copy_bytes);
  519. memcpy(to + sg->length - sg_remaining, from + offset,
  520. copy_bytes);
  521. sg_remaining -= copy_bytes;
  522. block_remaining -= copy_bytes;
  523. }
  524. kunmap_atomic(to - sg->offset);
  525. }
  526. if (from)
  527. kunmap_atomic(from);
  528. }
  529. static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
  530. {
  531. return DATA_BLOCK_SIZE * (thresh - bitmap_weight(bitmap, thresh));
  532. }
  533. /*
  534. * We can't queue a command until we have space available on the cmd ring *and*
  535. * space available on the data area.
  536. *
  537. * Called with ring lock held.
  538. */
  539. static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
  540. size_t cmd_size, size_t data_needed)
  541. {
  542. struct tcmu_mailbox *mb = udev->mb_addr;
  543. uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
  544. / DATA_BLOCK_SIZE;
  545. size_t space, cmd_needed;
  546. u32 cmd_head;
  547. tcmu_flush_dcache_range(mb, sizeof(*mb));
  548. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  549. /*
  550. * If cmd end-of-ring space is too small then we need space for a NOP plus
  551. * original cmd - cmds are internally contiguous.
  552. */
  553. if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
  554. cmd_needed = cmd_size;
  555. else
  556. cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
  557. space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
  558. if (space < cmd_needed) {
  559. pr_debug("no cmd space: %u %u %u\n", cmd_head,
  560. udev->cmdr_last_cleaned, udev->cmdr_size);
  561. return false;
  562. }
  563. /* try to check and get the data blocks as needed */
  564. space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
  565. if (space < data_needed) {
  566. unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh;
  567. unsigned long grow;
  568. if (blocks_left < blocks_needed) {
  569. pr_debug("no data space: only %lu available, but ask for %zu\n",
  570. blocks_left * DATA_BLOCK_SIZE,
  571. data_needed);
  572. return false;
  573. }
  574. /* Try to expand the thresh */
  575. if (!udev->dbi_thresh) {
  576. /* From idle state */
  577. uint32_t init_thresh = DATA_BLOCK_INIT_BITS;
  578. udev->dbi_thresh = max(blocks_needed, init_thresh);
  579. } else {
  580. /*
  581. * Grow the data area by max(blocks needed,
  582. * dbi_thresh / 2), but limited to the max
  583. * DATA_BLOCK_BITS size.
  584. */
  585. grow = max(blocks_needed, udev->dbi_thresh / 2);
  586. udev->dbi_thresh += grow;
  587. if (udev->dbi_thresh > DATA_BLOCK_BITS)
  588. udev->dbi_thresh = DATA_BLOCK_BITS;
  589. }
  590. }
  591. return tcmu_get_empty_blocks(udev, cmd);
  592. }
  593. static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
  594. {
  595. return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
  596. sizeof(struct tcmu_cmd_entry));
  597. }
  598. static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
  599. size_t base_command_size)
  600. {
  601. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  602. size_t command_size;
  603. command_size = base_command_size +
  604. round_up(scsi_command_size(se_cmd->t_task_cdb),
  605. TCMU_OP_ALIGN_SIZE);
  606. WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
  607. return command_size;
  608. }
  609. static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd)
  610. {
  611. struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
  612. unsigned long tmo = udev->cmd_time_out;
  613. int cmd_id;
  614. if (tcmu_cmd->cmd_id)
  615. return 0;
  616. cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT);
  617. if (cmd_id < 0) {
  618. pr_err("tcmu: Could not allocate cmd id.\n");
  619. return cmd_id;
  620. }
  621. tcmu_cmd->cmd_id = cmd_id;
  622. if (!tmo)
  623. return 0;
  624. tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
  625. mod_timer(&udev->timeout, tcmu_cmd->deadline);
  626. return 0;
  627. }
  628. static sense_reason_t
  629. tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
  630. {
  631. struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
  632. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  633. size_t base_command_size, command_size;
  634. struct tcmu_mailbox *mb;
  635. struct tcmu_cmd_entry *entry;
  636. struct iovec *iov;
  637. int iov_cnt, ret;
  638. uint32_t cmd_head;
  639. uint64_t cdb_off;
  640. bool copy_to_data_area;
  641. size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
  642. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
  643. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  644. /*
  645. * Must be a certain minimum size for response sense info, but
  646. * also may be larger if the iov array is large.
  647. *
  648. * We prepare as many iovs as possbile for potential uses here,
  649. * because it's expensive to tell how many regions are freed in
  650. * the bitmap & global data pool, as the size calculated here
  651. * will only be used to do the checks.
  652. *
  653. * The size will be recalculated later as actually needed to save
  654. * cmd area memories.
  655. */
  656. base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
  657. command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
  658. mutex_lock(&udev->cmdr_lock);
  659. mb = udev->mb_addr;
  660. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  661. if ((command_size > (udev->cmdr_size / 2)) ||
  662. data_length > udev->data_size) {
  663. pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
  664. "cmd ring/data area\n", command_size, data_length,
  665. udev->cmdr_size, udev->data_size);
  666. mutex_unlock(&udev->cmdr_lock);
  667. return TCM_INVALID_CDB_FIELD;
  668. }
  669. while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
  670. int ret;
  671. DEFINE_WAIT(__wait);
  672. prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
  673. pr_debug("sleeping for ring space\n");
  674. mutex_unlock(&udev->cmdr_lock);
  675. if (udev->cmd_time_out)
  676. ret = schedule_timeout(
  677. msecs_to_jiffies(udev->cmd_time_out));
  678. else
  679. ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
  680. finish_wait(&udev->wait_cmdr, &__wait);
  681. if (!ret) {
  682. pr_warn("tcmu: command timed out\n");
  683. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  684. }
  685. mutex_lock(&udev->cmdr_lock);
  686. /* We dropped cmdr_lock, cmd_head is stale */
  687. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  688. }
  689. /* Insert a PAD if end-of-ring space is too small */
  690. if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
  691. size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
  692. entry = (void *) mb + CMDR_OFF + cmd_head;
  693. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
  694. tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
  695. entry->hdr.cmd_id = 0; /* not used for PAD */
  696. entry->hdr.kflags = 0;
  697. entry->hdr.uflags = 0;
  698. tcmu_flush_dcache_range(entry, sizeof(*entry));
  699. UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
  700. tcmu_flush_dcache_range(mb, sizeof(*mb));
  701. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  702. WARN_ON(cmd_head != 0);
  703. }
  704. entry = (void *) mb + CMDR_OFF + cmd_head;
  705. memset(entry, 0, command_size);
  706. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
  707. /* Handle allocating space from the data area */
  708. tcmu_cmd_reset_dbi_cur(tcmu_cmd);
  709. iov = &entry->req.iov[0];
  710. iov_cnt = 0;
  711. copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
  712. || se_cmd->se_cmd_flags & SCF_BIDI);
  713. ret = scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
  714. se_cmd->t_data_nents, &iov, &iov_cnt,
  715. copy_to_data_area);
  716. if (ret) {
  717. tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
  718. mutex_unlock(&udev->cmdr_lock);
  719. pr_err("tcmu: alloc and scatter data failed\n");
  720. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  721. }
  722. entry->req.iov_cnt = iov_cnt;
  723. /* Handle BIDI commands */
  724. iov_cnt = 0;
  725. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  726. iov++;
  727. ret = scatter_data_area(udev, tcmu_cmd,
  728. se_cmd->t_bidi_data_sg,
  729. se_cmd->t_bidi_data_nents,
  730. &iov, &iov_cnt, false);
  731. if (ret) {
  732. tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
  733. mutex_unlock(&udev->cmdr_lock);
  734. pr_err("tcmu: alloc and scatter bidi data failed\n");
  735. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  736. }
  737. }
  738. entry->req.iov_bidi_cnt = iov_cnt;
  739. ret = tcmu_setup_cmd_timer(tcmu_cmd);
  740. if (ret) {
  741. tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
  742. return TCM_OUT_OF_RESOURCES;
  743. }
  744. entry->hdr.cmd_id = tcmu_cmd->cmd_id;
  745. /*
  746. * Recalaulate the command's base size and size according
  747. * to the actual needs
  748. */
  749. base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
  750. entry->req.iov_bidi_cnt);
  751. command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
  752. tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
  753. /* All offsets relative to mb_addr, not start of entry! */
  754. cdb_off = CMDR_OFF + cmd_head + base_command_size;
  755. memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
  756. entry->req.cdb_off = cdb_off;
  757. tcmu_flush_dcache_range(entry, sizeof(*entry));
  758. UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
  759. tcmu_flush_dcache_range(mb, sizeof(*mb));
  760. mutex_unlock(&udev->cmdr_lock);
  761. /* TODO: only if FLUSH and FUA? */
  762. uio_event_notify(&udev->uio_info);
  763. if (udev->cmd_time_out)
  764. mod_timer(&udev->timeout, round_jiffies_up(jiffies +
  765. msecs_to_jiffies(udev->cmd_time_out)));
  766. return TCM_NO_SENSE;
  767. }
  768. static sense_reason_t
  769. tcmu_queue_cmd(struct se_cmd *se_cmd)
  770. {
  771. struct tcmu_cmd *tcmu_cmd;
  772. sense_reason_t ret;
  773. tcmu_cmd = tcmu_alloc_cmd(se_cmd);
  774. if (!tcmu_cmd)
  775. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  776. ret = tcmu_queue_cmd_ring(tcmu_cmd);
  777. if (ret != TCM_NO_SENSE) {
  778. pr_err("TCMU: Could not queue command\n");
  779. tcmu_free_cmd(tcmu_cmd);
  780. }
  781. return ret;
  782. }
  783. static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
  784. {
  785. struct se_cmd *se_cmd = cmd->se_cmd;
  786. struct tcmu_dev *udev = cmd->tcmu_dev;
  787. /*
  788. * cmd has been completed already from timeout, just reclaim
  789. * data area space and free cmd
  790. */
  791. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  792. goto out;
  793. tcmu_cmd_reset_dbi_cur(cmd);
  794. if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
  795. pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
  796. cmd->se_cmd);
  797. entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
  798. } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
  799. transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
  800. } else if (se_cmd->se_cmd_flags & SCF_BIDI) {
  801. /* Get Data-In buffer before clean up */
  802. gather_data_area(udev, cmd, true);
  803. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  804. gather_data_area(udev, cmd, false);
  805. } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
  806. /* TODO: */
  807. } else if (se_cmd->data_direction != DMA_NONE) {
  808. pr_warn("TCMU: data direction was %d!\n",
  809. se_cmd->data_direction);
  810. }
  811. target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
  812. out:
  813. cmd->se_cmd = NULL;
  814. tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
  815. tcmu_free_cmd(cmd);
  816. }
  817. static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
  818. {
  819. struct tcmu_mailbox *mb;
  820. int handled = 0;
  821. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
  822. pr_err("ring broken, not handling completions\n");
  823. return 0;
  824. }
  825. mb = udev->mb_addr;
  826. tcmu_flush_dcache_range(mb, sizeof(*mb));
  827. while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
  828. struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
  829. struct tcmu_cmd *cmd;
  830. tcmu_flush_dcache_range(entry, sizeof(*entry));
  831. if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
  832. UPDATE_HEAD(udev->cmdr_last_cleaned,
  833. tcmu_hdr_get_len(entry->hdr.len_op),
  834. udev->cmdr_size);
  835. continue;
  836. }
  837. WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
  838. spin_lock(&udev->commands_lock);
  839. cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
  840. spin_unlock(&udev->commands_lock);
  841. if (!cmd) {
  842. pr_err("cmd_id not found, ring is broken\n");
  843. set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
  844. break;
  845. }
  846. tcmu_handle_completion(cmd, entry);
  847. UPDATE_HEAD(udev->cmdr_last_cleaned,
  848. tcmu_hdr_get_len(entry->hdr.len_op),
  849. udev->cmdr_size);
  850. handled++;
  851. }
  852. if (mb->cmd_tail == mb->cmd_head)
  853. del_timer(&udev->timeout); /* no more pending cmds */
  854. wake_up(&udev->wait_cmdr);
  855. return handled;
  856. }
  857. static int tcmu_check_expired_cmd(int id, void *p, void *data)
  858. {
  859. struct tcmu_cmd *cmd = p;
  860. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  861. return 0;
  862. if (!time_after(jiffies, cmd->deadline))
  863. return 0;
  864. set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
  865. target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
  866. cmd->se_cmd = NULL;
  867. return 0;
  868. }
  869. static void tcmu_device_timedout(unsigned long data)
  870. {
  871. struct tcmu_dev *udev = (struct tcmu_dev *)data;
  872. unsigned long flags;
  873. spin_lock_irqsave(&udev->commands_lock, flags);
  874. idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
  875. spin_unlock_irqrestore(&udev->commands_lock, flags);
  876. /* Try to wake up the ummap thread */
  877. wake_up(&unmap_wait);
  878. /*
  879. * We don't need to wakeup threads on wait_cmdr since they have their
  880. * own timeout.
  881. */
  882. }
  883. static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
  884. {
  885. struct tcmu_hba *tcmu_hba;
  886. tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
  887. if (!tcmu_hba)
  888. return -ENOMEM;
  889. tcmu_hba->host_id = host_id;
  890. hba->hba_ptr = tcmu_hba;
  891. return 0;
  892. }
  893. static void tcmu_detach_hba(struct se_hba *hba)
  894. {
  895. kfree(hba->hba_ptr);
  896. hba->hba_ptr = NULL;
  897. }
  898. static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
  899. {
  900. struct tcmu_dev *udev;
  901. udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
  902. if (!udev)
  903. return NULL;
  904. kref_init(&udev->kref);
  905. udev->name = kstrdup(name, GFP_KERNEL);
  906. if (!udev->name) {
  907. kfree(udev);
  908. return NULL;
  909. }
  910. udev->hba = hba;
  911. udev->cmd_time_out = TCMU_TIME_OUT;
  912. init_waitqueue_head(&udev->wait_cmdr);
  913. mutex_init(&udev->cmdr_lock);
  914. idr_init(&udev->commands);
  915. spin_lock_init(&udev->commands_lock);
  916. setup_timer(&udev->timeout, tcmu_device_timedout,
  917. (unsigned long)udev);
  918. init_waitqueue_head(&udev->nl_cmd_wq);
  919. spin_lock_init(&udev->nl_cmd_lock);
  920. INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
  921. return &udev->se_dev;
  922. }
  923. static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
  924. {
  925. struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
  926. mutex_lock(&tcmu_dev->cmdr_lock);
  927. tcmu_handle_completions(tcmu_dev);
  928. mutex_unlock(&tcmu_dev->cmdr_lock);
  929. return 0;
  930. }
  931. /*
  932. * mmap code from uio.c. Copied here because we want to hook mmap()
  933. * and this stuff must come along.
  934. */
  935. static int tcmu_find_mem_index(struct vm_area_struct *vma)
  936. {
  937. struct tcmu_dev *udev = vma->vm_private_data;
  938. struct uio_info *info = &udev->uio_info;
  939. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  940. if (info->mem[vma->vm_pgoff].size == 0)
  941. return -1;
  942. return (int)vma->vm_pgoff;
  943. }
  944. return -1;
  945. }
  946. static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
  947. {
  948. struct page *page;
  949. int ret;
  950. mutex_lock(&udev->cmdr_lock);
  951. page = tcmu_get_block_page(udev, dbi);
  952. if (likely(page)) {
  953. mutex_unlock(&udev->cmdr_lock);
  954. return page;
  955. }
  956. /*
  957. * Normally it shouldn't be here:
  958. * Only when the userspace has touched the blocks which
  959. * are out of the tcmu_cmd's data iov[], and will return
  960. * one zeroed page.
  961. */
  962. pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi);
  963. pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
  964. if (dbi >= udev->dbi_thresh) {
  965. /* Extern the udev->dbi_thresh to dbi + 1 */
  966. udev->dbi_thresh = dbi + 1;
  967. udev->dbi_max = dbi;
  968. }
  969. page = radix_tree_lookup(&udev->data_blocks, dbi);
  970. if (!page) {
  971. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  972. if (!page) {
  973. mutex_unlock(&udev->cmdr_lock);
  974. return NULL;
  975. }
  976. ret = radix_tree_insert(&udev->data_blocks, dbi, page);
  977. if (ret) {
  978. mutex_unlock(&udev->cmdr_lock);
  979. __free_page(page);
  980. return NULL;
  981. }
  982. /*
  983. * Since this case is rare in page fault routine, here we
  984. * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS
  985. * to reduce possible page fault call trace.
  986. */
  987. atomic_inc(&global_db_count);
  988. }
  989. mutex_unlock(&udev->cmdr_lock);
  990. return page;
  991. }
  992. static int tcmu_vma_fault(struct vm_fault *vmf)
  993. {
  994. struct tcmu_dev *udev = vmf->vma->vm_private_data;
  995. struct uio_info *info = &udev->uio_info;
  996. struct page *page;
  997. unsigned long offset;
  998. void *addr;
  999. int mi = tcmu_find_mem_index(vmf->vma);
  1000. if (mi < 0)
  1001. return VM_FAULT_SIGBUS;
  1002. /*
  1003. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  1004. * to use mem[N].
  1005. */
  1006. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  1007. if (offset < udev->data_off) {
  1008. /* For the vmalloc()ed cmd area pages */
  1009. addr = (void *)(unsigned long)info->mem[mi].addr + offset;
  1010. page = vmalloc_to_page(addr);
  1011. } else {
  1012. uint32_t dbi;
  1013. /* For the dynamically growing data area pages */
  1014. dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
  1015. page = tcmu_try_get_block_page(udev, dbi);
  1016. if (!page)
  1017. return VM_FAULT_NOPAGE;
  1018. }
  1019. get_page(page);
  1020. vmf->page = page;
  1021. return 0;
  1022. }
  1023. static const struct vm_operations_struct tcmu_vm_ops = {
  1024. .fault = tcmu_vma_fault,
  1025. };
  1026. static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
  1027. {
  1028. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  1029. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1030. vma->vm_ops = &tcmu_vm_ops;
  1031. vma->vm_private_data = udev;
  1032. /* Ensure the mmap is exactly the right size */
  1033. if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
  1034. return -EINVAL;
  1035. return 0;
  1036. }
  1037. static int tcmu_open(struct uio_info *info, struct inode *inode)
  1038. {
  1039. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  1040. /* O_EXCL not supported for char devs, so fake it? */
  1041. if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
  1042. return -EBUSY;
  1043. udev->inode = inode;
  1044. kref_get(&udev->kref);
  1045. pr_debug("open\n");
  1046. return 0;
  1047. }
  1048. static void tcmu_dev_call_rcu(struct rcu_head *p)
  1049. {
  1050. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  1051. struct tcmu_dev *udev = TCMU_DEV(dev);
  1052. kfree(udev->uio_info.name);
  1053. kfree(udev->name);
  1054. kfree(udev);
  1055. }
  1056. static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
  1057. {
  1058. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
  1059. kmem_cache_free(tcmu_cmd_cache, cmd);
  1060. return 0;
  1061. }
  1062. return -EINVAL;
  1063. }
  1064. static void tcmu_blocks_release(struct tcmu_dev *udev)
  1065. {
  1066. int i;
  1067. struct page *page;
  1068. /* Try to release all block pages */
  1069. mutex_lock(&udev->cmdr_lock);
  1070. for (i = 0; i <= udev->dbi_max; i++) {
  1071. page = radix_tree_delete(&udev->data_blocks, i);
  1072. if (page) {
  1073. __free_page(page);
  1074. atomic_dec(&global_db_count);
  1075. }
  1076. }
  1077. mutex_unlock(&udev->cmdr_lock);
  1078. }
  1079. static void tcmu_dev_kref_release(struct kref *kref)
  1080. {
  1081. struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
  1082. struct se_device *dev = &udev->se_dev;
  1083. struct tcmu_cmd *cmd;
  1084. bool all_expired = true;
  1085. int i;
  1086. vfree(udev->mb_addr);
  1087. udev->mb_addr = NULL;
  1088. /* Upper layer should drain all requests before calling this */
  1089. spin_lock_irq(&udev->commands_lock);
  1090. idr_for_each_entry(&udev->commands, cmd, i) {
  1091. if (tcmu_check_and_free_pending_cmd(cmd) != 0)
  1092. all_expired = false;
  1093. }
  1094. idr_destroy(&udev->commands);
  1095. spin_unlock_irq(&udev->commands_lock);
  1096. WARN_ON(!all_expired);
  1097. tcmu_blocks_release(udev);
  1098. call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
  1099. }
  1100. static int tcmu_release(struct uio_info *info, struct inode *inode)
  1101. {
  1102. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  1103. clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
  1104. pr_debug("close\n");
  1105. /* release ref from open */
  1106. kref_put(&udev->kref, tcmu_dev_kref_release);
  1107. return 0;
  1108. }
  1109. static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
  1110. {
  1111. struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
  1112. if (!tcmu_kern_cmd_reply_supported)
  1113. return;
  1114. if (udev->nl_reply_supported <= 0)
  1115. return;
  1116. relock:
  1117. spin_lock(&udev->nl_cmd_lock);
  1118. if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
  1119. spin_unlock(&udev->nl_cmd_lock);
  1120. pr_debug("sleeping for open nl cmd\n");
  1121. wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC));
  1122. goto relock;
  1123. }
  1124. memset(nl_cmd, 0, sizeof(*nl_cmd));
  1125. nl_cmd->cmd = cmd;
  1126. init_completion(&nl_cmd->complete);
  1127. spin_unlock(&udev->nl_cmd_lock);
  1128. }
  1129. static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
  1130. {
  1131. struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
  1132. int ret;
  1133. DEFINE_WAIT(__wait);
  1134. if (!tcmu_kern_cmd_reply_supported)
  1135. return 0;
  1136. if (udev->nl_reply_supported <= 0)
  1137. return 0;
  1138. pr_debug("sleeping for nl reply\n");
  1139. wait_for_completion(&nl_cmd->complete);
  1140. spin_lock(&udev->nl_cmd_lock);
  1141. nl_cmd->cmd = TCMU_CMD_UNSPEC;
  1142. ret = nl_cmd->status;
  1143. nl_cmd->status = 0;
  1144. spin_unlock(&udev->nl_cmd_lock);
  1145. wake_up_all(&udev->nl_cmd_wq);
  1146. return ret;;
  1147. }
  1148. static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd,
  1149. int reconfig_attr, const void *reconfig_data)
  1150. {
  1151. struct sk_buff *skb;
  1152. void *msg_header;
  1153. int ret = -ENOMEM;
  1154. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1155. if (!skb)
  1156. return ret;
  1157. msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
  1158. if (!msg_header)
  1159. goto free_skb;
  1160. ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
  1161. if (ret < 0)
  1162. goto free_skb;
  1163. ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
  1164. if (ret < 0)
  1165. goto free_skb;
  1166. ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
  1167. if (ret < 0)
  1168. goto free_skb;
  1169. if (cmd == TCMU_CMD_RECONFIG_DEVICE) {
  1170. switch (reconfig_attr) {
  1171. case TCMU_ATTR_DEV_CFG:
  1172. ret = nla_put_string(skb, reconfig_attr, reconfig_data);
  1173. break;
  1174. case TCMU_ATTR_DEV_SIZE:
  1175. ret = nla_put_u64_64bit(skb, reconfig_attr,
  1176. *((u64 *)reconfig_data),
  1177. TCMU_ATTR_PAD);
  1178. break;
  1179. case TCMU_ATTR_WRITECACHE:
  1180. ret = nla_put_u8(skb, reconfig_attr,
  1181. *((u8 *)reconfig_data));
  1182. break;
  1183. default:
  1184. BUG();
  1185. }
  1186. if (ret < 0)
  1187. goto free_skb;
  1188. }
  1189. genlmsg_end(skb, msg_header);
  1190. tcmu_init_genl_cmd_reply(udev, cmd);
  1191. ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
  1192. TCMU_MCGRP_CONFIG, GFP_KERNEL);
  1193. /* We don't care if no one is listening */
  1194. if (ret == -ESRCH)
  1195. ret = 0;
  1196. if (!ret)
  1197. ret = tcmu_wait_genl_cmd_reply(udev);
  1198. return ret;
  1199. free_skb:
  1200. nlmsg_free(skb);
  1201. return ret;
  1202. }
  1203. static int tcmu_update_uio_info(struct tcmu_dev *udev)
  1204. {
  1205. struct tcmu_hba *hba = udev->hba->hba_ptr;
  1206. struct uio_info *info;
  1207. size_t size, used;
  1208. char *str;
  1209. info = &udev->uio_info;
  1210. size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
  1211. udev->dev_config);
  1212. size += 1; /* for \0 */
  1213. str = kmalloc(size, GFP_KERNEL);
  1214. if (!str)
  1215. return -ENOMEM;
  1216. used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
  1217. if (udev->dev_config[0])
  1218. snprintf(str + used, size - used, "/%s", udev->dev_config);
  1219. /* If the old string exists, free it */
  1220. kfree(info->name);
  1221. info->name = str;
  1222. return 0;
  1223. }
  1224. static int tcmu_configure_device(struct se_device *dev)
  1225. {
  1226. struct tcmu_dev *udev = TCMU_DEV(dev);
  1227. struct uio_info *info;
  1228. struct tcmu_mailbox *mb;
  1229. int ret = 0;
  1230. ret = tcmu_update_uio_info(udev);
  1231. if (ret)
  1232. return ret;
  1233. info = &udev->uio_info;
  1234. udev->mb_addr = vzalloc(CMDR_SIZE);
  1235. if (!udev->mb_addr) {
  1236. ret = -ENOMEM;
  1237. goto err_vzalloc;
  1238. }
  1239. /* mailbox fits in first part of CMDR space */
  1240. udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
  1241. udev->data_off = CMDR_SIZE;
  1242. udev->data_size = DATA_SIZE;
  1243. udev->dbi_thresh = 0; /* Default in Idle state */
  1244. udev->waiting_global = false;
  1245. /* Initialise the mailbox of the ring buffer */
  1246. mb = udev->mb_addr;
  1247. mb->version = TCMU_MAILBOX_VERSION;
  1248. mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
  1249. mb->cmdr_off = CMDR_OFF;
  1250. mb->cmdr_size = udev->cmdr_size;
  1251. WARN_ON(!PAGE_ALIGNED(udev->data_off));
  1252. WARN_ON(udev->data_size % PAGE_SIZE);
  1253. WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
  1254. info->version = __stringify(TCMU_MAILBOX_VERSION);
  1255. info->mem[0].name = "tcm-user command & data buffer";
  1256. info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
  1257. info->mem[0].size = TCMU_RING_SIZE;
  1258. info->mem[0].memtype = UIO_MEM_NONE;
  1259. info->irqcontrol = tcmu_irqcontrol;
  1260. info->irq = UIO_IRQ_CUSTOM;
  1261. info->mmap = tcmu_mmap;
  1262. info->open = tcmu_open;
  1263. info->release = tcmu_release;
  1264. ret = uio_register_device(tcmu_root_device, info);
  1265. if (ret)
  1266. goto err_register;
  1267. /* User can set hw_block_size before enable the device */
  1268. if (dev->dev_attrib.hw_block_size == 0)
  1269. dev->dev_attrib.hw_block_size = 512;
  1270. /* Other attributes can be configured in userspace */
  1271. if (!dev->dev_attrib.hw_max_sectors)
  1272. dev->dev_attrib.hw_max_sectors = 128;
  1273. if (!dev->dev_attrib.emulate_write_cache)
  1274. dev->dev_attrib.emulate_write_cache = 0;
  1275. dev->dev_attrib.hw_queue_depth = 128;
  1276. /* If user didn't explicitly disable netlink reply support, use
  1277. * module scope setting.
  1278. */
  1279. if (udev->nl_reply_supported >= 0)
  1280. udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
  1281. /*
  1282. * Get a ref incase userspace does a close on the uio device before
  1283. * LIO has initiated tcmu_free_device.
  1284. */
  1285. kref_get(&udev->kref);
  1286. ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL);
  1287. if (ret)
  1288. goto err_netlink;
  1289. mutex_lock(&root_udev_mutex);
  1290. list_add(&udev->node, &root_udev);
  1291. mutex_unlock(&root_udev_mutex);
  1292. return 0;
  1293. err_netlink:
  1294. kref_put(&udev->kref, tcmu_dev_kref_release);
  1295. uio_unregister_device(&udev->uio_info);
  1296. err_register:
  1297. vfree(udev->mb_addr);
  1298. udev->mb_addr = NULL;
  1299. err_vzalloc:
  1300. kfree(info->name);
  1301. info->name = NULL;
  1302. return ret;
  1303. }
  1304. static bool tcmu_dev_configured(struct tcmu_dev *udev)
  1305. {
  1306. return udev->uio_info.uio_dev ? true : false;
  1307. }
  1308. static void tcmu_free_device(struct se_device *dev)
  1309. {
  1310. struct tcmu_dev *udev = TCMU_DEV(dev);
  1311. /* release ref from init */
  1312. kref_put(&udev->kref, tcmu_dev_kref_release);
  1313. }
  1314. static void tcmu_destroy_device(struct se_device *dev)
  1315. {
  1316. struct tcmu_dev *udev = TCMU_DEV(dev);
  1317. del_timer_sync(&udev->timeout);
  1318. mutex_lock(&root_udev_mutex);
  1319. list_del(&udev->node);
  1320. mutex_unlock(&root_udev_mutex);
  1321. tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL);
  1322. uio_unregister_device(&udev->uio_info);
  1323. /* release ref from configure */
  1324. kref_put(&udev->kref, tcmu_dev_kref_release);
  1325. }
  1326. enum {
  1327. Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
  1328. Opt_nl_reply_supported, Opt_err,
  1329. };
  1330. static match_table_t tokens = {
  1331. {Opt_dev_config, "dev_config=%s"},
  1332. {Opt_dev_size, "dev_size=%u"},
  1333. {Opt_hw_block_size, "hw_block_size=%u"},
  1334. {Opt_hw_max_sectors, "hw_max_sectors=%u"},
  1335. {Opt_nl_reply_supported, "nl_reply_supported=%d"},
  1336. {Opt_err, NULL}
  1337. };
  1338. static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
  1339. {
  1340. unsigned long tmp_ul;
  1341. char *arg_p;
  1342. int ret;
  1343. arg_p = match_strdup(arg);
  1344. if (!arg_p)
  1345. return -ENOMEM;
  1346. ret = kstrtoul(arg_p, 0, &tmp_ul);
  1347. kfree(arg_p);
  1348. if (ret < 0) {
  1349. pr_err("kstrtoul() failed for dev attrib\n");
  1350. return ret;
  1351. }
  1352. if (!tmp_ul) {
  1353. pr_err("dev attrib must be nonzero\n");
  1354. return -EINVAL;
  1355. }
  1356. *dev_attrib = tmp_ul;
  1357. return 0;
  1358. }
  1359. static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
  1360. const char *page, ssize_t count)
  1361. {
  1362. struct tcmu_dev *udev = TCMU_DEV(dev);
  1363. char *orig, *ptr, *opts, *arg_p;
  1364. substring_t args[MAX_OPT_ARGS];
  1365. int ret = 0, token;
  1366. opts = kstrdup(page, GFP_KERNEL);
  1367. if (!opts)
  1368. return -ENOMEM;
  1369. orig = opts;
  1370. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  1371. if (!*ptr)
  1372. continue;
  1373. token = match_token(ptr, tokens, args);
  1374. switch (token) {
  1375. case Opt_dev_config:
  1376. if (match_strlcpy(udev->dev_config, &args[0],
  1377. TCMU_CONFIG_LEN) == 0) {
  1378. ret = -EINVAL;
  1379. break;
  1380. }
  1381. pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
  1382. break;
  1383. case Opt_dev_size:
  1384. arg_p = match_strdup(&args[0]);
  1385. if (!arg_p) {
  1386. ret = -ENOMEM;
  1387. break;
  1388. }
  1389. ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
  1390. kfree(arg_p);
  1391. if (ret < 0)
  1392. pr_err("kstrtoul() failed for dev_size=\n");
  1393. break;
  1394. case Opt_hw_block_size:
  1395. ret = tcmu_set_dev_attrib(&args[0],
  1396. &(dev->dev_attrib.hw_block_size));
  1397. break;
  1398. case Opt_hw_max_sectors:
  1399. ret = tcmu_set_dev_attrib(&args[0],
  1400. &(dev->dev_attrib.hw_max_sectors));
  1401. break;
  1402. case Opt_nl_reply_supported:
  1403. arg_p = match_strdup(&args[0]);
  1404. if (!arg_p) {
  1405. ret = -ENOMEM;
  1406. break;
  1407. }
  1408. ret = kstrtol(arg_p, 0,
  1409. (long int *) &udev->nl_reply_supported);
  1410. kfree(arg_p);
  1411. if (ret < 0)
  1412. pr_err("kstrtoul() failed for nl_reply_supported=\n");
  1413. break;
  1414. default:
  1415. break;
  1416. }
  1417. if (ret)
  1418. break;
  1419. }
  1420. kfree(orig);
  1421. return (!ret) ? count : ret;
  1422. }
  1423. static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
  1424. {
  1425. struct tcmu_dev *udev = TCMU_DEV(dev);
  1426. ssize_t bl = 0;
  1427. bl = sprintf(b + bl, "Config: %s ",
  1428. udev->dev_config[0] ? udev->dev_config : "NULL");
  1429. bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
  1430. return bl;
  1431. }
  1432. static sector_t tcmu_get_blocks(struct se_device *dev)
  1433. {
  1434. struct tcmu_dev *udev = TCMU_DEV(dev);
  1435. return div_u64(udev->dev_size - dev->dev_attrib.block_size,
  1436. dev->dev_attrib.block_size);
  1437. }
  1438. static sense_reason_t
  1439. tcmu_parse_cdb(struct se_cmd *cmd)
  1440. {
  1441. return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
  1442. }
  1443. static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
  1444. {
  1445. struct se_dev_attrib *da = container_of(to_config_group(item),
  1446. struct se_dev_attrib, da_group);
  1447. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1448. return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
  1449. }
  1450. static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
  1451. size_t count)
  1452. {
  1453. struct se_dev_attrib *da = container_of(to_config_group(item),
  1454. struct se_dev_attrib, da_group);
  1455. struct tcmu_dev *udev = container_of(da->da_dev,
  1456. struct tcmu_dev, se_dev);
  1457. u32 val;
  1458. int ret;
  1459. if (da->da_dev->export_count) {
  1460. pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
  1461. return -EINVAL;
  1462. }
  1463. ret = kstrtou32(page, 0, &val);
  1464. if (ret < 0)
  1465. return ret;
  1466. udev->cmd_time_out = val * MSEC_PER_SEC;
  1467. return count;
  1468. }
  1469. CONFIGFS_ATTR(tcmu_, cmd_time_out);
  1470. static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
  1471. {
  1472. struct se_dev_attrib *da = container_of(to_config_group(item),
  1473. struct se_dev_attrib, da_group);
  1474. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1475. return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
  1476. }
  1477. static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
  1478. size_t count)
  1479. {
  1480. struct se_dev_attrib *da = container_of(to_config_group(item),
  1481. struct se_dev_attrib, da_group);
  1482. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1483. int ret, len;
  1484. len = strlen(page);
  1485. if (!len || len > TCMU_CONFIG_LEN - 1)
  1486. return -EINVAL;
  1487. /* Check if device has been configured before */
  1488. if (tcmu_dev_configured(udev)) {
  1489. ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
  1490. TCMU_ATTR_DEV_CFG, page);
  1491. if (ret) {
  1492. pr_err("Unable to reconfigure device\n");
  1493. return ret;
  1494. }
  1495. strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
  1496. ret = tcmu_update_uio_info(udev);
  1497. if (ret)
  1498. return ret;
  1499. return count;
  1500. }
  1501. strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
  1502. return count;
  1503. }
  1504. CONFIGFS_ATTR(tcmu_, dev_config);
  1505. static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
  1506. {
  1507. struct se_dev_attrib *da = container_of(to_config_group(item),
  1508. struct se_dev_attrib, da_group);
  1509. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1510. return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size);
  1511. }
  1512. static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
  1513. size_t count)
  1514. {
  1515. struct se_dev_attrib *da = container_of(to_config_group(item),
  1516. struct se_dev_attrib, da_group);
  1517. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1518. u64 val;
  1519. int ret;
  1520. ret = kstrtou64(page, 0, &val);
  1521. if (ret < 0)
  1522. return ret;
  1523. /* Check if device has been configured before */
  1524. if (tcmu_dev_configured(udev)) {
  1525. ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
  1526. TCMU_ATTR_DEV_SIZE, &val);
  1527. if (ret) {
  1528. pr_err("Unable to reconfigure device\n");
  1529. return ret;
  1530. }
  1531. }
  1532. udev->dev_size = val;
  1533. return count;
  1534. }
  1535. CONFIGFS_ATTR(tcmu_, dev_size);
  1536. static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
  1537. char *page)
  1538. {
  1539. struct se_dev_attrib *da = container_of(to_config_group(item),
  1540. struct se_dev_attrib, da_group);
  1541. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1542. return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
  1543. }
  1544. static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
  1545. const char *page, size_t count)
  1546. {
  1547. struct se_dev_attrib *da = container_of(to_config_group(item),
  1548. struct se_dev_attrib, da_group);
  1549. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1550. s8 val;
  1551. int ret;
  1552. ret = kstrtos8(page, 0, &val);
  1553. if (ret < 0)
  1554. return ret;
  1555. udev->nl_reply_supported = val;
  1556. return count;
  1557. }
  1558. CONFIGFS_ATTR(tcmu_, nl_reply_supported);
  1559. static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
  1560. char *page)
  1561. {
  1562. struct se_dev_attrib *da = container_of(to_config_group(item),
  1563. struct se_dev_attrib, da_group);
  1564. return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
  1565. }
  1566. static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
  1567. const char *page, size_t count)
  1568. {
  1569. struct se_dev_attrib *da = container_of(to_config_group(item),
  1570. struct se_dev_attrib, da_group);
  1571. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1572. u8 val;
  1573. int ret;
  1574. ret = kstrtou8(page, 0, &val);
  1575. if (ret < 0)
  1576. return ret;
  1577. /* Check if device has been configured before */
  1578. if (tcmu_dev_configured(udev)) {
  1579. ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
  1580. TCMU_ATTR_WRITECACHE, &val);
  1581. if (ret) {
  1582. pr_err("Unable to reconfigure device\n");
  1583. return ret;
  1584. }
  1585. }
  1586. da->emulate_write_cache = val;
  1587. return count;
  1588. }
  1589. CONFIGFS_ATTR(tcmu_, emulate_write_cache);
  1590. static struct configfs_attribute *tcmu_attrib_attrs[] = {
  1591. &tcmu_attr_cmd_time_out,
  1592. &tcmu_attr_dev_config,
  1593. &tcmu_attr_dev_size,
  1594. &tcmu_attr_emulate_write_cache,
  1595. &tcmu_attr_nl_reply_supported,
  1596. NULL,
  1597. };
  1598. static struct configfs_attribute **tcmu_attrs;
  1599. static struct target_backend_ops tcmu_ops = {
  1600. .name = "user",
  1601. .owner = THIS_MODULE,
  1602. .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
  1603. .attach_hba = tcmu_attach_hba,
  1604. .detach_hba = tcmu_detach_hba,
  1605. .alloc_device = tcmu_alloc_device,
  1606. .configure_device = tcmu_configure_device,
  1607. .destroy_device = tcmu_destroy_device,
  1608. .free_device = tcmu_free_device,
  1609. .parse_cdb = tcmu_parse_cdb,
  1610. .set_configfs_dev_params = tcmu_set_configfs_dev_params,
  1611. .show_configfs_dev_params = tcmu_show_configfs_dev_params,
  1612. .get_device_type = sbc_get_device_type,
  1613. .get_blocks = tcmu_get_blocks,
  1614. .tb_dev_attrib_attrs = NULL,
  1615. };
  1616. static int unmap_thread_fn(void *data)
  1617. {
  1618. struct tcmu_dev *udev;
  1619. loff_t off;
  1620. uint32_t start, end, block;
  1621. struct page *page;
  1622. int i;
  1623. while (!kthread_should_stop()) {
  1624. DEFINE_WAIT(__wait);
  1625. prepare_to_wait(&unmap_wait, &__wait, TASK_INTERRUPTIBLE);
  1626. schedule();
  1627. finish_wait(&unmap_wait, &__wait);
  1628. if (kthread_should_stop())
  1629. break;
  1630. mutex_lock(&root_udev_mutex);
  1631. list_for_each_entry(udev, &root_udev, node) {
  1632. mutex_lock(&udev->cmdr_lock);
  1633. /* Try to complete the finished commands first */
  1634. tcmu_handle_completions(udev);
  1635. /* Skip the udevs waiting the global pool or in idle */
  1636. if (udev->waiting_global || !udev->dbi_thresh) {
  1637. mutex_unlock(&udev->cmdr_lock);
  1638. continue;
  1639. }
  1640. end = udev->dbi_max + 1;
  1641. block = find_last_bit(udev->data_bitmap, end);
  1642. if (block == udev->dbi_max) {
  1643. /*
  1644. * The last bit is dbi_max, so there is
  1645. * no need to shrink any blocks.
  1646. */
  1647. mutex_unlock(&udev->cmdr_lock);
  1648. continue;
  1649. } else if (block == end) {
  1650. /* The current udev will goto idle state */
  1651. udev->dbi_thresh = start = 0;
  1652. udev->dbi_max = 0;
  1653. } else {
  1654. udev->dbi_thresh = start = block + 1;
  1655. udev->dbi_max = block;
  1656. }
  1657. /* Here will truncate the data area from off */
  1658. off = udev->data_off + start * DATA_BLOCK_SIZE;
  1659. unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
  1660. /* Release the block pages */
  1661. for (i = start; i < end; i++) {
  1662. page = radix_tree_delete(&udev->data_blocks, i);
  1663. if (page) {
  1664. __free_page(page);
  1665. atomic_dec(&global_db_count);
  1666. }
  1667. }
  1668. mutex_unlock(&udev->cmdr_lock);
  1669. }
  1670. /*
  1671. * Try to wake up the udevs who are waiting
  1672. * for the global data pool.
  1673. */
  1674. list_for_each_entry(udev, &root_udev, node) {
  1675. if (udev->waiting_global)
  1676. wake_up(&udev->wait_cmdr);
  1677. }
  1678. mutex_unlock(&root_udev_mutex);
  1679. }
  1680. return 0;
  1681. }
  1682. static int __init tcmu_module_init(void)
  1683. {
  1684. int ret, i, k, len = 0;
  1685. BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
  1686. tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
  1687. sizeof(struct tcmu_cmd),
  1688. __alignof__(struct tcmu_cmd),
  1689. 0, NULL);
  1690. if (!tcmu_cmd_cache)
  1691. return -ENOMEM;
  1692. tcmu_root_device = root_device_register("tcm_user");
  1693. if (IS_ERR(tcmu_root_device)) {
  1694. ret = PTR_ERR(tcmu_root_device);
  1695. goto out_free_cache;
  1696. }
  1697. ret = genl_register_family(&tcmu_genl_family);
  1698. if (ret < 0) {
  1699. goto out_unreg_device;
  1700. }
  1701. for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
  1702. len += sizeof(struct configfs_attribute *);
  1703. }
  1704. for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
  1705. len += sizeof(struct configfs_attribute *);
  1706. }
  1707. len += sizeof(struct configfs_attribute *);
  1708. tcmu_attrs = kzalloc(len, GFP_KERNEL);
  1709. if (!tcmu_attrs) {
  1710. ret = -ENOMEM;
  1711. goto out_unreg_genl;
  1712. }
  1713. for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
  1714. tcmu_attrs[i] = passthrough_attrib_attrs[i];
  1715. }
  1716. for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
  1717. tcmu_attrs[i] = tcmu_attrib_attrs[k];
  1718. i++;
  1719. }
  1720. tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
  1721. ret = transport_backend_register(&tcmu_ops);
  1722. if (ret)
  1723. goto out_attrs;
  1724. init_waitqueue_head(&unmap_wait);
  1725. unmap_thread = kthread_run(unmap_thread_fn, NULL, "tcmu_unmap");
  1726. if (IS_ERR(unmap_thread)) {
  1727. ret = PTR_ERR(unmap_thread);
  1728. goto out_unreg_transport;
  1729. }
  1730. return 0;
  1731. out_unreg_transport:
  1732. target_backend_unregister(&tcmu_ops);
  1733. out_attrs:
  1734. kfree(tcmu_attrs);
  1735. out_unreg_genl:
  1736. genl_unregister_family(&tcmu_genl_family);
  1737. out_unreg_device:
  1738. root_device_unregister(tcmu_root_device);
  1739. out_free_cache:
  1740. kmem_cache_destroy(tcmu_cmd_cache);
  1741. return ret;
  1742. }
  1743. static void __exit tcmu_module_exit(void)
  1744. {
  1745. kthread_stop(unmap_thread);
  1746. target_backend_unregister(&tcmu_ops);
  1747. kfree(tcmu_attrs);
  1748. genl_unregister_family(&tcmu_genl_family);
  1749. root_device_unregister(tcmu_root_device);
  1750. kmem_cache_destroy(tcmu_cmd_cache);
  1751. }
  1752. MODULE_DESCRIPTION("TCM USER subsystem plugin");
  1753. MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
  1754. MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
  1755. MODULE_LICENSE("GPL");
  1756. module_init(tcmu_module_init);
  1757. module_exit(tcmu_module_exit);