target_core_device.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /*******************************************************************************
  2. * Filename: target_core_device.c (based on iscsi_target_device.c)
  3. *
  4. * This file contains the TCM Virtual Device and Disk Transport
  5. * agnostic related functions.
  6. *
  7. * (c) Copyright 2003-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. ******************************************************************************/
  26. #include <linux/net.h>
  27. #include <linux/string.h>
  28. #include <linux/delay.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/kthread.h>
  33. #include <linux/in.h>
  34. #include <linux/export.h>
  35. #include <net/sock.h>
  36. #include <net/tcp.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_device.h>
  39. #include <target/target_core_base.h>
  40. #include <target/target_core_backend.h>
  41. #include <target/target_core_fabric.h>
  42. #include "target_core_internal.h"
  43. #include "target_core_alua.h"
  44. #include "target_core_pr.h"
  45. #include "target_core_ua.h"
  46. DEFINE_MUTEX(g_device_mutex);
  47. LIST_HEAD(g_device_list);
  48. static struct se_hba *lun0_hba;
  49. /* not static, needed by tpg.c */
  50. struct se_device *g_lun0_dev;
  51. sense_reason_t
  52. transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  53. {
  54. struct se_lun *se_lun = NULL;
  55. struct se_session *se_sess = se_cmd->se_sess;
  56. struct se_device *dev;
  57. unsigned long flags;
  58. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  59. return TCM_NON_EXISTENT_LUN;
  60. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  61. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  62. if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  63. struct se_dev_entry *deve = se_cmd->se_deve;
  64. deve->total_cmds++;
  65. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  66. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
  67. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  68. " Access for 0x%08x\n",
  69. se_cmd->se_tfo->get_fabric_name(),
  70. unpacked_lun);
  71. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  72. return TCM_WRITE_PROTECTED;
  73. }
  74. if (se_cmd->data_direction == DMA_TO_DEVICE)
  75. deve->write_bytes += se_cmd->data_length;
  76. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  77. deve->read_bytes += se_cmd->data_length;
  78. se_lun = deve->se_lun;
  79. se_cmd->se_lun = deve->se_lun;
  80. se_cmd->pr_res_key = deve->pr_res_key;
  81. se_cmd->orig_fe_lun = unpacked_lun;
  82. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  83. percpu_ref_get(&se_lun->lun_ref);
  84. se_cmd->lun_ref_active = true;
  85. }
  86. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  87. if (!se_lun) {
  88. /*
  89. * Use the se_portal_group->tpg_virt_lun0 to allow for
  90. * REPORT_LUNS, et al to be returned when no active
  91. * MappedLUN=0 exists for this Initiator Port.
  92. */
  93. if (unpacked_lun != 0) {
  94. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  95. " Access for 0x%08x\n",
  96. se_cmd->se_tfo->get_fabric_name(),
  97. unpacked_lun);
  98. return TCM_NON_EXISTENT_LUN;
  99. }
  100. /*
  101. * Force WRITE PROTECT for virtual LUN 0
  102. */
  103. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  104. (se_cmd->data_direction != DMA_NONE))
  105. return TCM_WRITE_PROTECTED;
  106. se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  107. se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  108. se_cmd->orig_fe_lun = 0;
  109. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  110. percpu_ref_get(&se_lun->lun_ref);
  111. se_cmd->lun_ref_active = true;
  112. }
  113. /* Directly associate cmd with se_dev */
  114. se_cmd->se_dev = se_lun->lun_se_dev;
  115. dev = se_lun->lun_se_dev;
  116. atomic_long_inc(&dev->num_cmds);
  117. if (se_cmd->data_direction == DMA_TO_DEVICE)
  118. atomic_long_add(se_cmd->data_length, &dev->write_bytes);
  119. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  120. atomic_long_add(se_cmd->data_length, &dev->read_bytes);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  124. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  125. {
  126. struct se_dev_entry *deve;
  127. struct se_lun *se_lun = NULL;
  128. struct se_session *se_sess = se_cmd->se_sess;
  129. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  130. unsigned long flags;
  131. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  132. return -ENODEV;
  133. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  134. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  135. deve = se_cmd->se_deve;
  136. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  137. se_tmr->tmr_lun = deve->se_lun;
  138. se_cmd->se_lun = deve->se_lun;
  139. se_lun = deve->se_lun;
  140. se_cmd->pr_res_key = deve->pr_res_key;
  141. se_cmd->orig_fe_lun = unpacked_lun;
  142. }
  143. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  144. if (!se_lun) {
  145. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  146. " Access for 0x%08x\n",
  147. se_cmd->se_tfo->get_fabric_name(),
  148. unpacked_lun);
  149. return -ENODEV;
  150. }
  151. /* Directly associate cmd with se_dev */
  152. se_cmd->se_dev = se_lun->lun_se_dev;
  153. se_tmr->tmr_dev = se_lun->lun_se_dev;
  154. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  155. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  156. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  160. /*
  161. * This function is called from core_scsi3_emulate_pro_register_and_move()
  162. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
  163. * when a matching rtpi is found.
  164. */
  165. struct se_dev_entry *core_get_se_deve_from_rtpi(
  166. struct se_node_acl *nacl,
  167. u16 rtpi)
  168. {
  169. struct se_dev_entry *deve;
  170. struct se_lun *lun;
  171. struct se_port *port;
  172. struct se_portal_group *tpg = nacl->se_tpg;
  173. u32 i;
  174. spin_lock_irq(&nacl->device_list_lock);
  175. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  176. deve = nacl->device_list[i];
  177. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  178. continue;
  179. lun = deve->se_lun;
  180. if (!lun) {
  181. pr_err("%s device entries device pointer is"
  182. " NULL, but Initiator has access.\n",
  183. tpg->se_tpg_tfo->get_fabric_name());
  184. continue;
  185. }
  186. port = lun->lun_sep;
  187. if (!port) {
  188. pr_err("%s device entries device pointer is"
  189. " NULL, but Initiator has access.\n",
  190. tpg->se_tpg_tfo->get_fabric_name());
  191. continue;
  192. }
  193. if (port->sep_rtpi != rtpi)
  194. continue;
  195. atomic_inc(&deve->pr_ref_count);
  196. smp_mb__after_atomic_inc();
  197. spin_unlock_irq(&nacl->device_list_lock);
  198. return deve;
  199. }
  200. spin_unlock_irq(&nacl->device_list_lock);
  201. return NULL;
  202. }
  203. int core_free_device_list_for_node(
  204. struct se_node_acl *nacl,
  205. struct se_portal_group *tpg)
  206. {
  207. struct se_dev_entry *deve;
  208. struct se_lun *lun;
  209. u32 i;
  210. if (!nacl->device_list)
  211. return 0;
  212. spin_lock_irq(&nacl->device_list_lock);
  213. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  214. deve = nacl->device_list[i];
  215. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  216. continue;
  217. if (!deve->se_lun) {
  218. pr_err("%s device entries device pointer is"
  219. " NULL, but Initiator has access.\n",
  220. tpg->se_tpg_tfo->get_fabric_name());
  221. continue;
  222. }
  223. lun = deve->se_lun;
  224. spin_unlock_irq(&nacl->device_list_lock);
  225. core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
  226. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  227. spin_lock_irq(&nacl->device_list_lock);
  228. }
  229. spin_unlock_irq(&nacl->device_list_lock);
  230. array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
  231. nacl->device_list = NULL;
  232. return 0;
  233. }
  234. void core_update_device_list_access(
  235. u32 mapped_lun,
  236. u32 lun_access,
  237. struct se_node_acl *nacl)
  238. {
  239. struct se_dev_entry *deve;
  240. spin_lock_irq(&nacl->device_list_lock);
  241. deve = nacl->device_list[mapped_lun];
  242. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  243. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  244. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  245. } else {
  246. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  247. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  248. }
  249. spin_unlock_irq(&nacl->device_list_lock);
  250. }
  251. /* core_enable_device_list_for_node():
  252. *
  253. *
  254. */
  255. int core_enable_device_list_for_node(
  256. struct se_lun *lun,
  257. struct se_lun_acl *lun_acl,
  258. u32 mapped_lun,
  259. u32 lun_access,
  260. struct se_node_acl *nacl,
  261. struct se_portal_group *tpg)
  262. {
  263. struct se_port *port = lun->lun_sep;
  264. struct se_dev_entry *deve;
  265. spin_lock_irq(&nacl->device_list_lock);
  266. deve = nacl->device_list[mapped_lun];
  267. /*
  268. * Check if the call is handling demo mode -> explicit LUN ACL
  269. * transition. This transition must be for the same struct se_lun
  270. * + mapped_lun that was setup in demo mode..
  271. */
  272. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  273. if (deve->se_lun_acl != NULL) {
  274. pr_err("struct se_dev_entry->se_lun_acl"
  275. " already set for demo mode -> explicit"
  276. " LUN ACL transition\n");
  277. spin_unlock_irq(&nacl->device_list_lock);
  278. return -EINVAL;
  279. }
  280. if (deve->se_lun != lun) {
  281. pr_err("struct se_dev_entry->se_lun does"
  282. " match passed struct se_lun for demo mode"
  283. " -> explicit LUN ACL transition\n");
  284. spin_unlock_irq(&nacl->device_list_lock);
  285. return -EINVAL;
  286. }
  287. deve->se_lun_acl = lun_acl;
  288. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  289. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  290. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  291. } else {
  292. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  293. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  294. }
  295. spin_unlock_irq(&nacl->device_list_lock);
  296. return 0;
  297. }
  298. deve->se_lun = lun;
  299. deve->se_lun_acl = lun_acl;
  300. deve->mapped_lun = mapped_lun;
  301. deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
  302. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  303. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  304. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  305. } else {
  306. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  307. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  308. }
  309. deve->creation_time = get_jiffies_64();
  310. deve->attach_count++;
  311. spin_unlock_irq(&nacl->device_list_lock);
  312. spin_lock_bh(&port->sep_alua_lock);
  313. list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
  314. spin_unlock_bh(&port->sep_alua_lock);
  315. return 0;
  316. }
  317. /* core_disable_device_list_for_node():
  318. *
  319. *
  320. */
  321. int core_disable_device_list_for_node(
  322. struct se_lun *lun,
  323. struct se_lun_acl *lun_acl,
  324. u32 mapped_lun,
  325. u32 lun_access,
  326. struct se_node_acl *nacl,
  327. struct se_portal_group *tpg)
  328. {
  329. struct se_port *port = lun->lun_sep;
  330. struct se_dev_entry *deve = nacl->device_list[mapped_lun];
  331. /*
  332. * If the MappedLUN entry is being disabled, the entry in
  333. * port->sep_alua_list must be removed now before clearing the
  334. * struct se_dev_entry pointers below as logic in
  335. * core_alua_do_transition_tg_pt() depends on these being present.
  336. *
  337. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  338. * that have not been explicitly converted to MappedLUNs ->
  339. * struct se_lun_acl, but we remove deve->alua_port_list from
  340. * port->sep_alua_list. This also means that active UAs and
  341. * NodeACL context specific PR metadata for demo-mode
  342. * MappedLUN *deve will be released below..
  343. */
  344. spin_lock_bh(&port->sep_alua_lock);
  345. list_del(&deve->alua_port_list);
  346. spin_unlock_bh(&port->sep_alua_lock);
  347. /*
  348. * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
  349. * PR operation to complete.
  350. */
  351. while (atomic_read(&deve->pr_ref_count) != 0)
  352. cpu_relax();
  353. spin_lock_irq(&nacl->device_list_lock);
  354. /*
  355. * Disable struct se_dev_entry LUN ACL mapping
  356. */
  357. core_scsi3_ua_release_all(deve);
  358. deve->se_lun = NULL;
  359. deve->se_lun_acl = NULL;
  360. deve->lun_flags = 0;
  361. deve->creation_time = 0;
  362. deve->attach_count--;
  363. spin_unlock_irq(&nacl->device_list_lock);
  364. core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
  365. return 0;
  366. }
  367. /* core_clear_lun_from_tpg():
  368. *
  369. *
  370. */
  371. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  372. {
  373. struct se_node_acl *nacl;
  374. struct se_dev_entry *deve;
  375. u32 i;
  376. spin_lock_irq(&tpg->acl_node_lock);
  377. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  378. spin_unlock_irq(&tpg->acl_node_lock);
  379. spin_lock_irq(&nacl->device_list_lock);
  380. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  381. deve = nacl->device_list[i];
  382. if (lun != deve->se_lun)
  383. continue;
  384. spin_unlock_irq(&nacl->device_list_lock);
  385. core_disable_device_list_for_node(lun, NULL,
  386. deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
  387. nacl, tpg);
  388. spin_lock_irq(&nacl->device_list_lock);
  389. }
  390. spin_unlock_irq(&nacl->device_list_lock);
  391. spin_lock_irq(&tpg->acl_node_lock);
  392. }
  393. spin_unlock_irq(&tpg->acl_node_lock);
  394. }
  395. static struct se_port *core_alloc_port(struct se_device *dev)
  396. {
  397. struct se_port *port, *port_tmp;
  398. port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
  399. if (!port) {
  400. pr_err("Unable to allocate struct se_port\n");
  401. return ERR_PTR(-ENOMEM);
  402. }
  403. INIT_LIST_HEAD(&port->sep_alua_list);
  404. INIT_LIST_HEAD(&port->sep_list);
  405. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  406. spin_lock_init(&port->sep_alua_lock);
  407. mutex_init(&port->sep_tg_pt_md_mutex);
  408. spin_lock(&dev->se_port_lock);
  409. if (dev->dev_port_count == 0x0000ffff) {
  410. pr_warn("Reached dev->dev_port_count =="
  411. " 0x0000ffff\n");
  412. spin_unlock(&dev->se_port_lock);
  413. return ERR_PTR(-ENOSPC);
  414. }
  415. again:
  416. /*
  417. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  418. * Here is the table from spc4r17 section 7.7.3.8.
  419. *
  420. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  421. *
  422. * Code Description
  423. * 0h Reserved
  424. * 1h Relative port 1, historically known as port A
  425. * 2h Relative port 2, historically known as port B
  426. * 3h to FFFFh Relative port 3 through 65 535
  427. */
  428. port->sep_rtpi = dev->dev_rpti_counter++;
  429. if (!port->sep_rtpi)
  430. goto again;
  431. list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
  432. /*
  433. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  434. * for 16-bit wrap..
  435. */
  436. if (port->sep_rtpi == port_tmp->sep_rtpi)
  437. goto again;
  438. }
  439. spin_unlock(&dev->se_port_lock);
  440. return port;
  441. }
  442. static void core_export_port(
  443. struct se_device *dev,
  444. struct se_portal_group *tpg,
  445. struct se_port *port,
  446. struct se_lun *lun)
  447. {
  448. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
  449. spin_lock(&dev->se_port_lock);
  450. spin_lock(&lun->lun_sep_lock);
  451. port->sep_tpg = tpg;
  452. port->sep_lun = lun;
  453. lun->lun_sep = port;
  454. spin_unlock(&lun->lun_sep_lock);
  455. list_add_tail(&port->sep_list, &dev->dev_sep_list);
  456. spin_unlock(&dev->se_port_lock);
  457. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
  458. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  459. tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
  460. if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
  461. pr_err("Unable to allocate t10_alua_tg_pt"
  462. "_gp_member_t\n");
  463. return;
  464. }
  465. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  466. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  467. dev->t10_alua.default_tg_pt_gp);
  468. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  469. pr_debug("%s/%s: Adding to default ALUA Target Port"
  470. " Group: alua/default_tg_pt_gp\n",
  471. dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
  472. }
  473. dev->dev_port_count++;
  474. port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
  475. }
  476. /*
  477. * Called with struct se_device->se_port_lock spinlock held.
  478. */
  479. static void core_release_port(struct se_device *dev, struct se_port *port)
  480. __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
  481. {
  482. /*
  483. * Wait for any port reference for PR ALL_TG_PT=1 operation
  484. * to complete in __core_scsi3_alloc_registration()
  485. */
  486. spin_unlock(&dev->se_port_lock);
  487. if (atomic_read(&port->sep_tg_pt_ref_cnt))
  488. cpu_relax();
  489. spin_lock(&dev->se_port_lock);
  490. core_alua_free_tg_pt_gp_mem(port);
  491. list_del(&port->sep_list);
  492. dev->dev_port_count--;
  493. kfree(port);
  494. }
  495. int core_dev_export(
  496. struct se_device *dev,
  497. struct se_portal_group *tpg,
  498. struct se_lun *lun)
  499. {
  500. struct se_hba *hba = dev->se_hba;
  501. struct se_port *port;
  502. port = core_alloc_port(dev);
  503. if (IS_ERR(port))
  504. return PTR_ERR(port);
  505. lun->lun_se_dev = dev;
  506. spin_lock(&hba->device_lock);
  507. dev->export_count++;
  508. spin_unlock(&hba->device_lock);
  509. core_export_port(dev, tpg, port, lun);
  510. return 0;
  511. }
  512. void core_dev_unexport(
  513. struct se_device *dev,
  514. struct se_portal_group *tpg,
  515. struct se_lun *lun)
  516. {
  517. struct se_hba *hba = dev->se_hba;
  518. struct se_port *port = lun->lun_sep;
  519. spin_lock(&lun->lun_sep_lock);
  520. if (lun->lun_se_dev == NULL) {
  521. spin_unlock(&lun->lun_sep_lock);
  522. return;
  523. }
  524. spin_unlock(&lun->lun_sep_lock);
  525. spin_lock(&dev->se_port_lock);
  526. core_release_port(dev, port);
  527. spin_unlock(&dev->se_port_lock);
  528. spin_lock(&hba->device_lock);
  529. dev->export_count--;
  530. spin_unlock(&hba->device_lock);
  531. lun->lun_se_dev = NULL;
  532. }
  533. static void se_release_vpd_for_dev(struct se_device *dev)
  534. {
  535. struct t10_vpd *vpd, *vpd_tmp;
  536. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  537. list_for_each_entry_safe(vpd, vpd_tmp,
  538. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  539. list_del(&vpd->vpd_list);
  540. kfree(vpd);
  541. }
  542. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  543. }
  544. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  545. {
  546. u32 aligned_max_sectors;
  547. u32 alignment;
  548. /*
  549. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  550. * transport_allocate_data_tasks() operation.
  551. */
  552. alignment = max(1ul, PAGE_SIZE / block_size);
  553. aligned_max_sectors = rounddown(max_sectors, alignment);
  554. if (max_sectors != aligned_max_sectors)
  555. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  556. max_sectors, aligned_max_sectors);
  557. return aligned_max_sectors;
  558. }
  559. int se_dev_set_max_unmap_lba_count(
  560. struct se_device *dev,
  561. u32 max_unmap_lba_count)
  562. {
  563. dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
  564. pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
  565. dev, dev->dev_attrib.max_unmap_lba_count);
  566. return 0;
  567. }
  568. int se_dev_set_max_unmap_block_desc_count(
  569. struct se_device *dev,
  570. u32 max_unmap_block_desc_count)
  571. {
  572. dev->dev_attrib.max_unmap_block_desc_count =
  573. max_unmap_block_desc_count;
  574. pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
  575. dev, dev->dev_attrib.max_unmap_block_desc_count);
  576. return 0;
  577. }
  578. int se_dev_set_unmap_granularity(
  579. struct se_device *dev,
  580. u32 unmap_granularity)
  581. {
  582. dev->dev_attrib.unmap_granularity = unmap_granularity;
  583. pr_debug("dev[%p]: Set unmap_granularity: %u\n",
  584. dev, dev->dev_attrib.unmap_granularity);
  585. return 0;
  586. }
  587. int se_dev_set_unmap_granularity_alignment(
  588. struct se_device *dev,
  589. u32 unmap_granularity_alignment)
  590. {
  591. dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
  592. pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
  593. dev, dev->dev_attrib.unmap_granularity_alignment);
  594. return 0;
  595. }
  596. int se_dev_set_max_write_same_len(
  597. struct se_device *dev,
  598. u32 max_write_same_len)
  599. {
  600. dev->dev_attrib.max_write_same_len = max_write_same_len;
  601. pr_debug("dev[%p]: Set max_write_same_len: %u\n",
  602. dev, dev->dev_attrib.max_write_same_len);
  603. return 0;
  604. }
  605. static void dev_set_t10_wwn_model_alias(struct se_device *dev)
  606. {
  607. const char *configname;
  608. configname = config_item_name(&dev->dev_group.cg_item);
  609. if (strlen(configname) >= 16) {
  610. pr_warn("dev[%p]: Backstore name '%s' is too long for "
  611. "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
  612. configname);
  613. }
  614. snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
  615. }
  616. int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
  617. {
  618. if (dev->export_count) {
  619. pr_err("dev[%p]: Unable to change model alias"
  620. " while export_count is %d\n",
  621. dev, dev->export_count);
  622. return -EINVAL;
  623. }
  624. if (flag != 0 && flag != 1) {
  625. pr_err("Illegal value %d\n", flag);
  626. return -EINVAL;
  627. }
  628. if (flag) {
  629. dev_set_t10_wwn_model_alias(dev);
  630. } else {
  631. strncpy(&dev->t10_wwn.model[0],
  632. dev->transport->inquiry_prod, 16);
  633. }
  634. dev->dev_attrib.emulate_model_alias = flag;
  635. return 0;
  636. }
  637. int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
  638. {
  639. if (flag != 0 && flag != 1) {
  640. pr_err("Illegal value %d\n", flag);
  641. return -EINVAL;
  642. }
  643. if (flag) {
  644. pr_err("dpo_emulated not supported\n");
  645. return -EINVAL;
  646. }
  647. return 0;
  648. }
  649. int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
  650. {
  651. if (flag != 0 && flag != 1) {
  652. pr_err("Illegal value %d\n", flag);
  653. return -EINVAL;
  654. }
  655. if (flag &&
  656. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  657. pr_err("emulate_fua_write not supported for pSCSI\n");
  658. return -EINVAL;
  659. }
  660. dev->dev_attrib.emulate_fua_write = flag;
  661. pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
  662. dev, dev->dev_attrib.emulate_fua_write);
  663. return 0;
  664. }
  665. int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
  666. {
  667. if (flag != 0 && flag != 1) {
  668. pr_err("Illegal value %d\n", flag);
  669. return -EINVAL;
  670. }
  671. if (flag) {
  672. pr_err("ua read emulated not supported\n");
  673. return -EINVAL;
  674. }
  675. return 0;
  676. }
  677. int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
  678. {
  679. if (flag != 0 && flag != 1) {
  680. pr_err("Illegal value %d\n", flag);
  681. return -EINVAL;
  682. }
  683. if (flag &&
  684. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  685. pr_err("emulate_write_cache not supported for pSCSI\n");
  686. return -EINVAL;
  687. }
  688. if (dev->transport->get_write_cache) {
  689. pr_warn("emulate_write_cache cannot be changed when underlying"
  690. " HW reports WriteCacheEnabled, ignoring request\n");
  691. return 0;
  692. }
  693. dev->dev_attrib.emulate_write_cache = flag;
  694. pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
  695. dev, dev->dev_attrib.emulate_write_cache);
  696. return 0;
  697. }
  698. int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
  699. {
  700. if ((flag != 0) && (flag != 1) && (flag != 2)) {
  701. pr_err("Illegal value %d\n", flag);
  702. return -EINVAL;
  703. }
  704. if (dev->export_count) {
  705. pr_err("dev[%p]: Unable to change SE Device"
  706. " UA_INTRLCK_CTRL while export_count is %d\n",
  707. dev, dev->export_count);
  708. return -EINVAL;
  709. }
  710. dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
  711. pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
  712. dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
  713. return 0;
  714. }
  715. int se_dev_set_emulate_tas(struct se_device *dev, int flag)
  716. {
  717. if ((flag != 0) && (flag != 1)) {
  718. pr_err("Illegal value %d\n", flag);
  719. return -EINVAL;
  720. }
  721. if (dev->export_count) {
  722. pr_err("dev[%p]: Unable to change SE Device TAS while"
  723. " export_count is %d\n",
  724. dev, dev->export_count);
  725. return -EINVAL;
  726. }
  727. dev->dev_attrib.emulate_tas = flag;
  728. pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
  729. dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
  730. return 0;
  731. }
  732. int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
  733. {
  734. if ((flag != 0) && (flag != 1)) {
  735. pr_err("Illegal value %d\n", flag);
  736. return -EINVAL;
  737. }
  738. /*
  739. * We expect this value to be non-zero when generic Block Layer
  740. * Discard supported is detected iblock_create_virtdevice().
  741. */
  742. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  743. pr_err("Generic Block Discard not supported\n");
  744. return -ENOSYS;
  745. }
  746. dev->dev_attrib.emulate_tpu = flag;
  747. pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
  748. dev, flag);
  749. return 0;
  750. }
  751. int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
  752. {
  753. if ((flag != 0) && (flag != 1)) {
  754. pr_err("Illegal value %d\n", flag);
  755. return -EINVAL;
  756. }
  757. /*
  758. * We expect this value to be non-zero when generic Block Layer
  759. * Discard supported is detected iblock_create_virtdevice().
  760. */
  761. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  762. pr_err("Generic Block Discard not supported\n");
  763. return -ENOSYS;
  764. }
  765. dev->dev_attrib.emulate_tpws = flag;
  766. pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
  767. dev, flag);
  768. return 0;
  769. }
  770. int se_dev_set_emulate_caw(struct se_device *dev, int flag)
  771. {
  772. if (flag != 0 && flag != 1) {
  773. pr_err("Illegal value %d\n", flag);
  774. return -EINVAL;
  775. }
  776. dev->dev_attrib.emulate_caw = flag;
  777. pr_debug("dev[%p]: SE Device CompareAndWrite (AtomicTestandSet): %d\n",
  778. dev, flag);
  779. return 0;
  780. }
  781. int se_dev_set_emulate_3pc(struct se_device *dev, int flag)
  782. {
  783. if (flag != 0 && flag != 1) {
  784. pr_err("Illegal value %d\n", flag);
  785. return -EINVAL;
  786. }
  787. dev->dev_attrib.emulate_3pc = flag;
  788. pr_debug("dev[%p]: SE Device 3rd Party Copy (EXTENDED_COPY): %d\n",
  789. dev, flag);
  790. return 0;
  791. }
  792. int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
  793. {
  794. if ((flag != 0) && (flag != 1)) {
  795. pr_err("Illegal value %d\n", flag);
  796. return -EINVAL;
  797. }
  798. dev->dev_attrib.enforce_pr_isids = flag;
  799. pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
  800. (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
  801. return 0;
  802. }
  803. int se_dev_set_is_nonrot(struct se_device *dev, int flag)
  804. {
  805. if ((flag != 0) && (flag != 1)) {
  806. printk(KERN_ERR "Illegal value %d\n", flag);
  807. return -EINVAL;
  808. }
  809. dev->dev_attrib.is_nonrot = flag;
  810. pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
  811. dev, flag);
  812. return 0;
  813. }
  814. int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
  815. {
  816. if (flag != 0) {
  817. printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
  818. " reordering not implemented\n", dev);
  819. return -ENOSYS;
  820. }
  821. dev->dev_attrib.emulate_rest_reord = flag;
  822. pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
  823. return 0;
  824. }
  825. /*
  826. * Note, this can only be called on unexported SE Device Object.
  827. */
  828. int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
  829. {
  830. if (dev->export_count) {
  831. pr_err("dev[%p]: Unable to change SE Device TCQ while"
  832. " export_count is %d\n",
  833. dev, dev->export_count);
  834. return -EINVAL;
  835. }
  836. if (!queue_depth) {
  837. pr_err("dev[%p]: Illegal ZERO value for queue"
  838. "_depth\n", dev);
  839. return -EINVAL;
  840. }
  841. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  842. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  843. pr_err("dev[%p]: Passed queue_depth: %u"
  844. " exceeds TCM/SE_Device TCQ: %u\n",
  845. dev, queue_depth,
  846. dev->dev_attrib.hw_queue_depth);
  847. return -EINVAL;
  848. }
  849. } else {
  850. if (queue_depth > dev->dev_attrib.queue_depth) {
  851. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  852. pr_err("dev[%p]: Passed queue_depth:"
  853. " %u exceeds TCM/SE_Device MAX"
  854. " TCQ: %u\n", dev, queue_depth,
  855. dev->dev_attrib.hw_queue_depth);
  856. return -EINVAL;
  857. }
  858. }
  859. }
  860. dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
  861. pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
  862. dev, queue_depth);
  863. return 0;
  864. }
  865. int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
  866. {
  867. int block_size = dev->dev_attrib.block_size;
  868. if (dev->export_count) {
  869. pr_err("dev[%p]: Unable to change SE Device"
  870. " fabric_max_sectors while export_count is %d\n",
  871. dev, dev->export_count);
  872. return -EINVAL;
  873. }
  874. if (!fabric_max_sectors) {
  875. pr_err("dev[%p]: Illegal ZERO value for"
  876. " fabric_max_sectors\n", dev);
  877. return -EINVAL;
  878. }
  879. if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
  880. pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
  881. " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
  882. DA_STATUS_MAX_SECTORS_MIN);
  883. return -EINVAL;
  884. }
  885. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  886. if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
  887. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  888. " greater than TCM/SE_Device max_sectors:"
  889. " %u\n", dev, fabric_max_sectors,
  890. dev->dev_attrib.hw_max_sectors);
  891. return -EINVAL;
  892. }
  893. } else {
  894. if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
  895. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  896. " greater than DA_STATUS_MAX_SECTORS_MAX:"
  897. " %u\n", dev, fabric_max_sectors,
  898. DA_STATUS_MAX_SECTORS_MAX);
  899. return -EINVAL;
  900. }
  901. }
  902. /*
  903. * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
  904. */
  905. if (!block_size) {
  906. block_size = 512;
  907. pr_warn("Defaulting to 512 for zero block_size\n");
  908. }
  909. fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
  910. block_size);
  911. dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
  912. pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
  913. dev, fabric_max_sectors);
  914. return 0;
  915. }
  916. int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
  917. {
  918. if (dev->export_count) {
  919. pr_err("dev[%p]: Unable to change SE Device"
  920. " optimal_sectors while export_count is %d\n",
  921. dev, dev->export_count);
  922. return -EINVAL;
  923. }
  924. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  925. pr_err("dev[%p]: Passed optimal_sectors cannot be"
  926. " changed for TCM/pSCSI\n", dev);
  927. return -EINVAL;
  928. }
  929. if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
  930. pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
  931. " greater than fabric_max_sectors: %u\n", dev,
  932. optimal_sectors, dev->dev_attrib.fabric_max_sectors);
  933. return -EINVAL;
  934. }
  935. dev->dev_attrib.optimal_sectors = optimal_sectors;
  936. pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
  937. dev, optimal_sectors);
  938. return 0;
  939. }
  940. int se_dev_set_block_size(struct se_device *dev, u32 block_size)
  941. {
  942. if (dev->export_count) {
  943. pr_err("dev[%p]: Unable to change SE Device block_size"
  944. " while export_count is %d\n",
  945. dev, dev->export_count);
  946. return -EINVAL;
  947. }
  948. if ((block_size != 512) &&
  949. (block_size != 1024) &&
  950. (block_size != 2048) &&
  951. (block_size != 4096)) {
  952. pr_err("dev[%p]: Illegal value for block_device: %u"
  953. " for SE device, must be 512, 1024, 2048 or 4096\n",
  954. dev, block_size);
  955. return -EINVAL;
  956. }
  957. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  958. pr_err("dev[%p]: Not allowed to change block_size for"
  959. " Physical Device, use for Linux/SCSI to change"
  960. " block_size for underlying hardware\n", dev);
  961. return -EINVAL;
  962. }
  963. dev->dev_attrib.block_size = block_size;
  964. pr_debug("dev[%p]: SE Device block_size changed to %u\n",
  965. dev, block_size);
  966. return 0;
  967. }
  968. struct se_lun *core_dev_add_lun(
  969. struct se_portal_group *tpg,
  970. struct se_device *dev,
  971. u32 lun)
  972. {
  973. struct se_lun *lun_p;
  974. int rc;
  975. lun_p = core_tpg_pre_addlun(tpg, lun);
  976. if (IS_ERR(lun_p))
  977. return lun_p;
  978. rc = core_tpg_post_addlun(tpg, lun_p,
  979. TRANSPORT_LUNFLAGS_READ_WRITE, dev);
  980. if (rc < 0)
  981. return ERR_PTR(rc);
  982. pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
  983. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  984. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
  985. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  986. /*
  987. * Update LUN maps for dynamically added initiators when
  988. * generate_node_acl is enabled.
  989. */
  990. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  991. struct se_node_acl *acl;
  992. spin_lock_irq(&tpg->acl_node_lock);
  993. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  994. if (acl->dynamic_node_acl &&
  995. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  996. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  997. spin_unlock_irq(&tpg->acl_node_lock);
  998. core_tpg_add_node_to_devs(acl, tpg);
  999. spin_lock_irq(&tpg->acl_node_lock);
  1000. }
  1001. }
  1002. spin_unlock_irq(&tpg->acl_node_lock);
  1003. }
  1004. return lun_p;
  1005. }
  1006. /* core_dev_del_lun():
  1007. *
  1008. *
  1009. */
  1010. int core_dev_del_lun(
  1011. struct se_portal_group *tpg,
  1012. u32 unpacked_lun)
  1013. {
  1014. struct se_lun *lun;
  1015. lun = core_tpg_pre_dellun(tpg, unpacked_lun);
  1016. if (IS_ERR(lun))
  1017. return PTR_ERR(lun);
  1018. core_tpg_post_dellun(tpg, lun);
  1019. pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
  1020. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  1021. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
  1022. tpg->se_tpg_tfo->get_fabric_name());
  1023. return 0;
  1024. }
  1025. struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
  1026. {
  1027. struct se_lun *lun;
  1028. spin_lock(&tpg->tpg_lun_lock);
  1029. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1030. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
  1031. "_PER_TPG-1: %u for Target Portal Group: %hu\n",
  1032. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1033. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1034. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1035. spin_unlock(&tpg->tpg_lun_lock);
  1036. return NULL;
  1037. }
  1038. lun = tpg->tpg_lun_list[unpacked_lun];
  1039. if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
  1040. pr_err("%s Logical Unit Number: %u is not free on"
  1041. " Target Portal Group: %hu, ignoring request.\n",
  1042. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1043. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1044. spin_unlock(&tpg->tpg_lun_lock);
  1045. return NULL;
  1046. }
  1047. spin_unlock(&tpg->tpg_lun_lock);
  1048. return lun;
  1049. }
  1050. /* core_dev_get_lun():
  1051. *
  1052. *
  1053. */
  1054. static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
  1055. {
  1056. struct se_lun *lun;
  1057. spin_lock(&tpg->tpg_lun_lock);
  1058. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1059. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
  1060. "_TPG-1: %u for Target Portal Group: %hu\n",
  1061. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1062. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1063. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1064. spin_unlock(&tpg->tpg_lun_lock);
  1065. return NULL;
  1066. }
  1067. lun = tpg->tpg_lun_list[unpacked_lun];
  1068. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
  1069. pr_err("%s Logical Unit Number: %u is not active on"
  1070. " Target Portal Group: %hu, ignoring request.\n",
  1071. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1072. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1073. spin_unlock(&tpg->tpg_lun_lock);
  1074. return NULL;
  1075. }
  1076. spin_unlock(&tpg->tpg_lun_lock);
  1077. return lun;
  1078. }
  1079. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  1080. struct se_portal_group *tpg,
  1081. struct se_node_acl *nacl,
  1082. u32 mapped_lun,
  1083. int *ret)
  1084. {
  1085. struct se_lun_acl *lacl;
  1086. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  1087. pr_err("%s InitiatorName exceeds maximum size.\n",
  1088. tpg->se_tpg_tfo->get_fabric_name());
  1089. *ret = -EOVERFLOW;
  1090. return NULL;
  1091. }
  1092. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  1093. if (!lacl) {
  1094. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  1095. *ret = -ENOMEM;
  1096. return NULL;
  1097. }
  1098. INIT_LIST_HEAD(&lacl->lacl_list);
  1099. lacl->mapped_lun = mapped_lun;
  1100. lacl->se_lun_nacl = nacl;
  1101. snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
  1102. nacl->initiatorname);
  1103. return lacl;
  1104. }
  1105. int core_dev_add_initiator_node_lun_acl(
  1106. struct se_portal_group *tpg,
  1107. struct se_lun_acl *lacl,
  1108. u32 unpacked_lun,
  1109. u32 lun_access)
  1110. {
  1111. struct se_lun *lun;
  1112. struct se_node_acl *nacl;
  1113. lun = core_dev_get_lun(tpg, unpacked_lun);
  1114. if (!lun) {
  1115. pr_err("%s Logical Unit Number: %u is not active on"
  1116. " Target Portal Group: %hu, ignoring request.\n",
  1117. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1118. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1119. return -EINVAL;
  1120. }
  1121. nacl = lacl->se_lun_nacl;
  1122. if (!nacl)
  1123. return -EINVAL;
  1124. if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
  1125. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
  1126. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  1127. lacl->se_lun = lun;
  1128. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  1129. lun_access, nacl, tpg) < 0)
  1130. return -EINVAL;
  1131. spin_lock(&lun->lun_acl_lock);
  1132. list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
  1133. atomic_inc(&lun->lun_acl_count);
  1134. smp_mb__after_atomic_inc();
  1135. spin_unlock(&lun->lun_acl_lock);
  1136. pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
  1137. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1138. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
  1139. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
  1140. lacl->initiatorname);
  1141. /*
  1142. * Check to see if there are any existing persistent reservation APTPL
  1143. * pre-registrations that need to be enabled for this LUN ACL..
  1144. */
  1145. core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
  1146. return 0;
  1147. }
  1148. /* core_dev_del_initiator_node_lun_acl():
  1149. *
  1150. *
  1151. */
  1152. int core_dev_del_initiator_node_lun_acl(
  1153. struct se_portal_group *tpg,
  1154. struct se_lun *lun,
  1155. struct se_lun_acl *lacl)
  1156. {
  1157. struct se_node_acl *nacl;
  1158. nacl = lacl->se_lun_nacl;
  1159. if (!nacl)
  1160. return -EINVAL;
  1161. spin_lock(&lun->lun_acl_lock);
  1162. list_del(&lacl->lacl_list);
  1163. atomic_dec(&lun->lun_acl_count);
  1164. smp_mb__after_atomic_dec();
  1165. spin_unlock(&lun->lun_acl_lock);
  1166. core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
  1167. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  1168. lacl->se_lun = NULL;
  1169. pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
  1170. " InitiatorNode: %s Mapped LUN: %u\n",
  1171. tpg->se_tpg_tfo->get_fabric_name(),
  1172. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1173. lacl->initiatorname, lacl->mapped_lun);
  1174. return 0;
  1175. }
  1176. void core_dev_free_initiator_node_lun_acl(
  1177. struct se_portal_group *tpg,
  1178. struct se_lun_acl *lacl)
  1179. {
  1180. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  1181. " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1182. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1183. tpg->se_tpg_tfo->get_fabric_name(),
  1184. lacl->initiatorname, lacl->mapped_lun);
  1185. kfree(lacl);
  1186. }
  1187. static void scsi_dump_inquiry(struct se_device *dev)
  1188. {
  1189. struct t10_wwn *wwn = &dev->t10_wwn;
  1190. char buf[17];
  1191. int i, device_type;
  1192. /*
  1193. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1194. */
  1195. for (i = 0; i < 8; i++)
  1196. if (wwn->vendor[i] >= 0x20)
  1197. buf[i] = wwn->vendor[i];
  1198. else
  1199. buf[i] = ' ';
  1200. buf[i] = '\0';
  1201. pr_debug(" Vendor: %s\n", buf);
  1202. for (i = 0; i < 16; i++)
  1203. if (wwn->model[i] >= 0x20)
  1204. buf[i] = wwn->model[i];
  1205. else
  1206. buf[i] = ' ';
  1207. buf[i] = '\0';
  1208. pr_debug(" Model: %s\n", buf);
  1209. for (i = 0; i < 4; i++)
  1210. if (wwn->revision[i] >= 0x20)
  1211. buf[i] = wwn->revision[i];
  1212. else
  1213. buf[i] = ' ';
  1214. buf[i] = '\0';
  1215. pr_debug(" Revision: %s\n", buf);
  1216. device_type = dev->transport->get_device_type(dev);
  1217. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1218. }
  1219. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  1220. {
  1221. struct se_device *dev;
  1222. struct se_lun *xcopy_lun;
  1223. dev = hba->transport->alloc_device(hba, name);
  1224. if (!dev)
  1225. return NULL;
  1226. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  1227. dev->se_hba = hba;
  1228. dev->transport = hba->transport;
  1229. INIT_LIST_HEAD(&dev->dev_list);
  1230. INIT_LIST_HEAD(&dev->dev_sep_list);
  1231. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1232. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1233. INIT_LIST_HEAD(&dev->state_list);
  1234. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1235. INIT_LIST_HEAD(&dev->g_dev_node);
  1236. spin_lock_init(&dev->execute_task_lock);
  1237. spin_lock_init(&dev->delayed_cmd_lock);
  1238. spin_lock_init(&dev->dev_reservation_lock);
  1239. spin_lock_init(&dev->se_port_lock);
  1240. spin_lock_init(&dev->se_tmr_lock);
  1241. spin_lock_init(&dev->qf_cmd_lock);
  1242. sema_init(&dev->caw_sem, 1);
  1243. atomic_set(&dev->dev_ordered_id, 0);
  1244. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  1245. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  1246. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  1247. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  1248. spin_lock_init(&dev->t10_pr.registration_lock);
  1249. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  1250. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  1251. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  1252. dev->t10_wwn.t10_dev = dev;
  1253. dev->t10_alua.t10_dev = dev;
  1254. dev->dev_attrib.da_dev = dev;
  1255. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  1256. dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
  1257. dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
  1258. dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
  1259. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  1260. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  1261. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  1262. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  1263. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  1264. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  1265. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  1266. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  1267. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  1268. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  1269. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  1270. dev->dev_attrib.max_unmap_block_desc_count =
  1271. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  1272. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  1273. dev->dev_attrib.unmap_granularity_alignment =
  1274. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  1275. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  1276. dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
  1277. dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
  1278. xcopy_lun = &dev->xcopy_lun;
  1279. xcopy_lun->lun_se_dev = dev;
  1280. init_completion(&xcopy_lun->lun_shutdown_comp);
  1281. INIT_LIST_HEAD(&xcopy_lun->lun_acl_list);
  1282. spin_lock_init(&xcopy_lun->lun_acl_lock);
  1283. spin_lock_init(&xcopy_lun->lun_sep_lock);
  1284. init_completion(&xcopy_lun->lun_ref_comp);
  1285. return dev;
  1286. }
  1287. int target_configure_device(struct se_device *dev)
  1288. {
  1289. struct se_hba *hba = dev->se_hba;
  1290. int ret;
  1291. if (dev->dev_flags & DF_CONFIGURED) {
  1292. pr_err("se_dev->se_dev_ptr already set for storage"
  1293. " object\n");
  1294. return -EEXIST;
  1295. }
  1296. ret = dev->transport->configure_device(dev);
  1297. if (ret)
  1298. goto out;
  1299. dev->dev_flags |= DF_CONFIGURED;
  1300. /*
  1301. * XXX: there is not much point to have two different values here..
  1302. */
  1303. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  1304. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  1305. /*
  1306. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  1307. */
  1308. dev->dev_attrib.hw_max_sectors =
  1309. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  1310. dev->dev_attrib.hw_block_size);
  1311. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1312. dev->creation_time = get_jiffies_64();
  1313. ret = core_setup_alua(dev);
  1314. if (ret)
  1315. goto out;
  1316. /*
  1317. * Startup the struct se_device processing thread
  1318. */
  1319. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  1320. dev->transport->name);
  1321. if (!dev->tmr_wq) {
  1322. pr_err("Unable to create tmr workqueue for %s\n",
  1323. dev->transport->name);
  1324. ret = -ENOMEM;
  1325. goto out_free_alua;
  1326. }
  1327. /*
  1328. * Setup work_queue for QUEUE_FULL
  1329. */
  1330. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1331. /*
  1332. * Preload the initial INQUIRY const values if we are doing
  1333. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1334. * passthrough because this is being provided by the backend LLD.
  1335. */
  1336. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1337. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1338. strncpy(&dev->t10_wwn.model[0],
  1339. dev->transport->inquiry_prod, 16);
  1340. strncpy(&dev->t10_wwn.revision[0],
  1341. dev->transport->inquiry_rev, 4);
  1342. }
  1343. scsi_dump_inquiry(dev);
  1344. spin_lock(&hba->device_lock);
  1345. hba->dev_count++;
  1346. spin_unlock(&hba->device_lock);
  1347. mutex_lock(&g_device_mutex);
  1348. list_add_tail(&dev->g_dev_node, &g_device_list);
  1349. mutex_unlock(&g_device_mutex);
  1350. return 0;
  1351. out_free_alua:
  1352. core_alua_free_lu_gp_mem(dev);
  1353. out:
  1354. se_release_vpd_for_dev(dev);
  1355. return ret;
  1356. }
  1357. void target_free_device(struct se_device *dev)
  1358. {
  1359. struct se_hba *hba = dev->se_hba;
  1360. WARN_ON(!list_empty(&dev->dev_sep_list));
  1361. if (dev->dev_flags & DF_CONFIGURED) {
  1362. destroy_workqueue(dev->tmr_wq);
  1363. mutex_lock(&g_device_mutex);
  1364. list_del(&dev->g_dev_node);
  1365. mutex_unlock(&g_device_mutex);
  1366. spin_lock(&hba->device_lock);
  1367. hba->dev_count--;
  1368. spin_unlock(&hba->device_lock);
  1369. }
  1370. core_alua_free_lu_gp_mem(dev);
  1371. core_scsi3_free_all_registrations(dev);
  1372. se_release_vpd_for_dev(dev);
  1373. dev->transport->free_device(dev);
  1374. }
  1375. int core_dev_setup_virtual_lun0(void)
  1376. {
  1377. struct se_hba *hba;
  1378. struct se_device *dev;
  1379. char buf[] = "rd_pages=8,rd_nullio=1";
  1380. int ret;
  1381. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  1382. if (IS_ERR(hba))
  1383. return PTR_ERR(hba);
  1384. dev = target_alloc_device(hba, "virt_lun0");
  1385. if (!dev) {
  1386. ret = -ENOMEM;
  1387. goto out_free_hba;
  1388. }
  1389. hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
  1390. ret = target_configure_device(dev);
  1391. if (ret)
  1392. goto out_free_se_dev;
  1393. lun0_hba = hba;
  1394. g_lun0_dev = dev;
  1395. return 0;
  1396. out_free_se_dev:
  1397. target_free_device(dev);
  1398. out_free_hba:
  1399. core_delete_hba(hba);
  1400. return ret;
  1401. }
  1402. void core_dev_release_virtual_lun0(void)
  1403. {
  1404. struct se_hba *hba = lun0_hba;
  1405. if (!hba)
  1406. return;
  1407. if (g_lun0_dev)
  1408. target_free_device(g_lun0_dev);
  1409. core_delete_hba(hba);
  1410. }