target_core_alua.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  1. /*******************************************************************************
  2. * Filename: target_core_alua.c
  3. *
  4. * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
  5. *
  6. * Copyright (c) 2009-2010 Rising Tide Systems
  7. * Copyright (c) 2009-2010 Linux-iSCSI.org
  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/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/configfs.h>
  29. #include <linux/export.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <asm/unaligned.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_backend.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_configfs.h>
  37. #include "target_core_internal.h"
  38. #include "target_core_alua.h"
  39. #include "target_core_ua.h"
  40. static int core_alua_check_transition(int state, int *primary);
  41. static int core_alua_set_tg_pt_secondary_state(
  42. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  43. struct se_port *port, int explict, int offline);
  44. static u16 alua_lu_gps_counter;
  45. static u32 alua_lu_gps_count;
  46. static DEFINE_SPINLOCK(lu_gps_lock);
  47. static LIST_HEAD(lu_gps_list);
  48. struct t10_alua_lu_gp *default_lu_gp;
  49. /*
  50. * REPORT_TARGET_PORT_GROUPS
  51. *
  52. * See spc4r17 section 6.27
  53. */
  54. int target_emulate_report_target_port_groups(struct se_task *task)
  55. {
  56. struct se_cmd *cmd = task->task_se_cmd;
  57. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  58. struct se_port *port;
  59. struct t10_alua_tg_pt_gp *tg_pt_gp;
  60. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  61. unsigned char *buf;
  62. u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
  63. Target port group descriptor */
  64. /*
  65. * Need at least 4 bytes of response data or else we can't
  66. * even fit the return data length.
  67. */
  68. if (cmd->data_length < 4) {
  69. pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
  70. " too small\n", cmd->data_length);
  71. return -EINVAL;
  72. }
  73. buf = transport_kmap_data_sg(cmd);
  74. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  75. list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
  76. tg_pt_gp_list) {
  77. /*
  78. * Check if the Target port group and Target port descriptor list
  79. * based on tg_pt_gp_members count will fit into the response payload.
  80. * Otherwise, bump rd_len to let the initiator know we have exceeded
  81. * the allocation length and the response is truncated.
  82. */
  83. if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
  84. cmd->data_length) {
  85. rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
  86. continue;
  87. }
  88. /*
  89. * PREF: Preferred target port bit, determine if this
  90. * bit should be set for port group.
  91. */
  92. if (tg_pt_gp->tg_pt_gp_pref)
  93. buf[off] = 0x80;
  94. /*
  95. * Set the ASYMMETRIC ACCESS State
  96. */
  97. buf[off++] |= (atomic_read(
  98. &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
  99. /*
  100. * Set supported ASYMMETRIC ACCESS State bits
  101. */
  102. buf[off] = 0x80; /* T_SUP */
  103. buf[off] |= 0x40; /* O_SUP */
  104. buf[off] |= 0x8; /* U_SUP */
  105. buf[off] |= 0x4; /* S_SUP */
  106. buf[off] |= 0x2; /* AN_SUP */
  107. buf[off++] |= 0x1; /* AO_SUP */
  108. /*
  109. * TARGET PORT GROUP
  110. */
  111. buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
  112. buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
  113. off++; /* Skip over Reserved */
  114. /*
  115. * STATUS CODE
  116. */
  117. buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
  118. /*
  119. * Vendor Specific field
  120. */
  121. buf[off++] = 0x00;
  122. /*
  123. * TARGET PORT COUNT
  124. */
  125. buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
  126. rd_len += 8;
  127. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  128. list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  129. tg_pt_gp_mem_list) {
  130. port = tg_pt_gp_mem->tg_pt;
  131. /*
  132. * Start Target Port descriptor format
  133. *
  134. * See spc4r17 section 6.2.7 Table 247
  135. */
  136. off += 2; /* Skip over Obsolete */
  137. /*
  138. * Set RELATIVE TARGET PORT IDENTIFIER
  139. */
  140. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  141. buf[off++] = (port->sep_rtpi & 0xff);
  142. rd_len += 4;
  143. }
  144. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  145. }
  146. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  147. /*
  148. * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
  149. */
  150. buf[0] = ((rd_len >> 24) & 0xff);
  151. buf[1] = ((rd_len >> 16) & 0xff);
  152. buf[2] = ((rd_len >> 8) & 0xff);
  153. buf[3] = (rd_len & 0xff);
  154. transport_kunmap_data_sg(cmd);
  155. task->task_scsi_status = GOOD;
  156. transport_complete_task(task, 1);
  157. return 0;
  158. }
  159. /*
  160. * SET_TARGET_PORT_GROUPS for explict ALUA operation.
  161. *
  162. * See spc4r17 section 6.35
  163. */
  164. int target_emulate_set_target_port_groups(struct se_task *task)
  165. {
  166. struct se_cmd *cmd = task->task_se_cmd;
  167. struct se_device *dev = cmd->se_dev;
  168. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  169. struct se_port *port, *l_port = cmd->se_lun->lun_sep;
  170. struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
  171. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
  172. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
  173. unsigned char *buf;
  174. unsigned char *ptr;
  175. u32 len = 4; /* Skip over RESERVED area in header */
  176. int alua_access_state, primary = 0, rc;
  177. u16 tg_pt_id, rtpi;
  178. if (!l_port) {
  179. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  180. return -EINVAL;
  181. }
  182. buf = transport_kmap_data_sg(cmd);
  183. /*
  184. * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
  185. * for the local tg_pt_gp.
  186. */
  187. l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
  188. if (!l_tg_pt_gp_mem) {
  189. pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
  190. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  191. rc = -EINVAL;
  192. goto out;
  193. }
  194. spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  195. l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
  196. if (!l_tg_pt_gp) {
  197. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  198. pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
  199. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  200. rc = -EINVAL;
  201. goto out;
  202. }
  203. rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
  204. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  205. if (!rc) {
  206. pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
  207. " while TPGS_EXPLICT_ALUA is disabled\n");
  208. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  209. rc = -EINVAL;
  210. goto out;
  211. }
  212. ptr = &buf[4]; /* Skip over RESERVED area in header */
  213. while (len < cmd->data_length) {
  214. alua_access_state = (ptr[0] & 0x0f);
  215. /*
  216. * Check the received ALUA access state, and determine if
  217. * the state is a primary or secondary target port asymmetric
  218. * access state.
  219. */
  220. rc = core_alua_check_transition(alua_access_state, &primary);
  221. if (rc != 0) {
  222. /*
  223. * If the SET TARGET PORT GROUPS attempts to establish
  224. * an invalid combination of target port asymmetric
  225. * access states or attempts to establish an
  226. * unsupported target port asymmetric access state,
  227. * then the command shall be terminated with CHECK
  228. * CONDITION status, with the sense key set to ILLEGAL
  229. * REQUEST, and the additional sense code set to INVALID
  230. * FIELD IN PARAMETER LIST.
  231. */
  232. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  233. rc = -EINVAL;
  234. goto out;
  235. }
  236. rc = -1;
  237. /*
  238. * If the ASYMMETRIC ACCESS STATE field (see table 267)
  239. * specifies a primary target port asymmetric access state,
  240. * then the TARGET PORT GROUP OR TARGET PORT field specifies
  241. * a primary target port group for which the primary target
  242. * port asymmetric access state shall be changed. If the
  243. * ASYMMETRIC ACCESS STATE field specifies a secondary target
  244. * port asymmetric access state, then the TARGET PORT GROUP OR
  245. * TARGET PORT field specifies the relative target port
  246. * identifier (see 3.1.120) of the target port for which the
  247. * secondary target port asymmetric access state shall be
  248. * changed.
  249. */
  250. if (primary) {
  251. tg_pt_id = get_unaligned_be16(ptr + 2);
  252. /*
  253. * Locate the matching target port group ID from
  254. * the global tg_pt_gp list
  255. */
  256. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  257. list_for_each_entry(tg_pt_gp,
  258. &su_dev->t10_alua.tg_pt_gps_list,
  259. tg_pt_gp_list) {
  260. if (!tg_pt_gp->tg_pt_gp_valid_id)
  261. continue;
  262. if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
  263. continue;
  264. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  265. smp_mb__after_atomic_inc();
  266. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  267. rc = core_alua_do_port_transition(tg_pt_gp,
  268. dev, l_port, nacl,
  269. alua_access_state, 1);
  270. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  271. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  272. smp_mb__after_atomic_dec();
  273. break;
  274. }
  275. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  276. /*
  277. * If not matching target port group ID can be located
  278. * throw an exception with ASCQ: INVALID_PARAMETER_LIST
  279. */
  280. if (rc != 0) {
  281. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  282. rc = -EINVAL;
  283. goto out;
  284. }
  285. } else {
  286. /*
  287. * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
  288. * the Target Port in question for the the incoming
  289. * SET_TARGET_PORT_GROUPS op.
  290. */
  291. rtpi = get_unaligned_be16(ptr + 2);
  292. /*
  293. * Locate the matching relative target port identifer
  294. * for the struct se_device storage object.
  295. */
  296. spin_lock(&dev->se_port_lock);
  297. list_for_each_entry(port, &dev->dev_sep_list,
  298. sep_list) {
  299. if (port->sep_rtpi != rtpi)
  300. continue;
  301. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  302. spin_unlock(&dev->se_port_lock);
  303. rc = core_alua_set_tg_pt_secondary_state(
  304. tg_pt_gp_mem, port, 1, 1);
  305. spin_lock(&dev->se_port_lock);
  306. break;
  307. }
  308. spin_unlock(&dev->se_port_lock);
  309. /*
  310. * If not matching relative target port identifier can
  311. * be located, throw an exception with ASCQ:
  312. * INVALID_PARAMETER_LIST
  313. */
  314. if (rc != 0) {
  315. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  316. rc = -EINVAL;
  317. goto out;
  318. }
  319. }
  320. ptr += 4;
  321. len += 4;
  322. }
  323. out:
  324. transport_kunmap_data_sg(cmd);
  325. if (!rc) {
  326. task->task_scsi_status = GOOD;
  327. transport_complete_task(task, 1);
  328. }
  329. return rc;
  330. }
  331. static inline int core_alua_state_nonoptimized(
  332. struct se_cmd *cmd,
  333. unsigned char *cdb,
  334. int nonop_delay_msecs,
  335. u8 *alua_ascq)
  336. {
  337. /*
  338. * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
  339. * later to determine if processing of this cmd needs to be
  340. * temporarily delayed for the Active/NonOptimized primary access state.
  341. */
  342. cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
  343. cmd->alua_nonop_delay = nonop_delay_msecs;
  344. return 0;
  345. }
  346. static inline int core_alua_state_standby(
  347. struct se_cmd *cmd,
  348. unsigned char *cdb,
  349. u8 *alua_ascq)
  350. {
  351. /*
  352. * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
  353. * spc4r17 section 5.9.2.4.4
  354. */
  355. switch (cdb[0]) {
  356. case INQUIRY:
  357. case LOG_SELECT:
  358. case LOG_SENSE:
  359. case MODE_SELECT:
  360. case MODE_SENSE:
  361. case REPORT_LUNS:
  362. case RECEIVE_DIAGNOSTIC:
  363. case SEND_DIAGNOSTIC:
  364. return 0;
  365. case MAINTENANCE_IN:
  366. switch (cdb[1] & 0x1f) {
  367. case MI_REPORT_TARGET_PGS:
  368. return 0;
  369. default:
  370. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  371. return 1;
  372. }
  373. case MAINTENANCE_OUT:
  374. switch (cdb[1]) {
  375. case MO_SET_TARGET_PGS:
  376. return 0;
  377. default:
  378. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  379. return 1;
  380. }
  381. case REQUEST_SENSE:
  382. case PERSISTENT_RESERVE_IN:
  383. case PERSISTENT_RESERVE_OUT:
  384. case READ_BUFFER:
  385. case WRITE_BUFFER:
  386. return 0;
  387. default:
  388. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  389. return 1;
  390. }
  391. return 0;
  392. }
  393. static inline int core_alua_state_unavailable(
  394. struct se_cmd *cmd,
  395. unsigned char *cdb,
  396. u8 *alua_ascq)
  397. {
  398. /*
  399. * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
  400. * spc4r17 section 5.9.2.4.5
  401. */
  402. switch (cdb[0]) {
  403. case INQUIRY:
  404. case REPORT_LUNS:
  405. return 0;
  406. case MAINTENANCE_IN:
  407. switch (cdb[1] & 0x1f) {
  408. case MI_REPORT_TARGET_PGS:
  409. return 0;
  410. default:
  411. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  412. return 1;
  413. }
  414. case MAINTENANCE_OUT:
  415. switch (cdb[1]) {
  416. case MO_SET_TARGET_PGS:
  417. return 0;
  418. default:
  419. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  420. return 1;
  421. }
  422. case REQUEST_SENSE:
  423. case READ_BUFFER:
  424. case WRITE_BUFFER:
  425. return 0;
  426. default:
  427. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. static inline int core_alua_state_transition(
  433. struct se_cmd *cmd,
  434. unsigned char *cdb,
  435. u8 *alua_ascq)
  436. {
  437. /*
  438. * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
  439. * spc4r17 section 5.9.2.5
  440. */
  441. switch (cdb[0]) {
  442. case INQUIRY:
  443. case REPORT_LUNS:
  444. return 0;
  445. case MAINTENANCE_IN:
  446. switch (cdb[1] & 0x1f) {
  447. case MI_REPORT_TARGET_PGS:
  448. return 0;
  449. default:
  450. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  451. return 1;
  452. }
  453. case REQUEST_SENSE:
  454. case READ_BUFFER:
  455. case WRITE_BUFFER:
  456. return 0;
  457. default:
  458. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  459. return 1;
  460. }
  461. return 0;
  462. }
  463. /*
  464. * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
  465. * in transport_cmd_sequencer(). This function is assigned to
  466. * struct t10_alua *->state_check() in core_setup_alua()
  467. */
  468. static int core_alua_state_check_nop(
  469. struct se_cmd *cmd,
  470. unsigned char *cdb,
  471. u8 *alua_ascq)
  472. {
  473. return 0;
  474. }
  475. /*
  476. * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
  477. * This function is assigned to struct t10_alua *->state_check() in
  478. * core_setup_alua()
  479. *
  480. * Also, this function can return three different return codes to
  481. * signal transport_generic_cmd_sequencer()
  482. *
  483. * return 1: Is used to signal LUN not accecsable, and check condition/not ready
  484. * return 0: Used to signal success
  485. * reutrn -1: Used to signal failure, and invalid cdb field
  486. */
  487. static int core_alua_state_check(
  488. struct se_cmd *cmd,
  489. unsigned char *cdb,
  490. u8 *alua_ascq)
  491. {
  492. struct se_lun *lun = cmd->se_lun;
  493. struct se_port *port = lun->lun_sep;
  494. struct t10_alua_tg_pt_gp *tg_pt_gp;
  495. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  496. int out_alua_state, nonop_delay_msecs;
  497. if (!port)
  498. return 0;
  499. /*
  500. * First, check for a struct se_port specific secondary ALUA target port
  501. * access state: OFFLINE
  502. */
  503. if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
  504. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  505. pr_debug("ALUA: Got secondary offline status for local"
  506. " target port\n");
  507. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  508. return 1;
  509. }
  510. /*
  511. * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
  512. * ALUA target port group, to obtain current ALUA access state.
  513. * Otherwise look for the underlying struct se_device association with
  514. * a ALUA logical unit group.
  515. */
  516. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  517. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  518. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  519. out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  520. nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
  521. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  522. /*
  523. * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
  524. * statement so the compiler knows explicitly to check this case first.
  525. * For the Optimized ALUA access state case, we want to process the
  526. * incoming fabric cmd ASAP..
  527. */
  528. if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
  529. return 0;
  530. switch (out_alua_state) {
  531. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  532. return core_alua_state_nonoptimized(cmd, cdb,
  533. nonop_delay_msecs, alua_ascq);
  534. case ALUA_ACCESS_STATE_STANDBY:
  535. return core_alua_state_standby(cmd, cdb, alua_ascq);
  536. case ALUA_ACCESS_STATE_UNAVAILABLE:
  537. return core_alua_state_unavailable(cmd, cdb, alua_ascq);
  538. case ALUA_ACCESS_STATE_TRANSITION:
  539. return core_alua_state_transition(cmd, cdb, alua_ascq);
  540. /*
  541. * OFFLINE is a secondary ALUA target port group access state, that is
  542. * handled above with struct se_port->sep_tg_pt_secondary_offline=1
  543. */
  544. case ALUA_ACCESS_STATE_OFFLINE:
  545. default:
  546. pr_err("Unknown ALUA access state: 0x%02x\n",
  547. out_alua_state);
  548. return -EINVAL;
  549. }
  550. return 0;
  551. }
  552. /*
  553. * Check implict and explict ALUA state change request.
  554. */
  555. static int core_alua_check_transition(int state, int *primary)
  556. {
  557. switch (state) {
  558. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  559. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  560. case ALUA_ACCESS_STATE_STANDBY:
  561. case ALUA_ACCESS_STATE_UNAVAILABLE:
  562. /*
  563. * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
  564. * defined as primary target port asymmetric access states.
  565. */
  566. *primary = 1;
  567. break;
  568. case ALUA_ACCESS_STATE_OFFLINE:
  569. /*
  570. * OFFLINE state is defined as a secondary target port
  571. * asymmetric access state.
  572. */
  573. *primary = 0;
  574. break;
  575. default:
  576. pr_err("Unknown ALUA access state: 0x%02x\n", state);
  577. return -EINVAL;
  578. }
  579. return 0;
  580. }
  581. static char *core_alua_dump_state(int state)
  582. {
  583. switch (state) {
  584. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  585. return "Active/Optimized";
  586. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  587. return "Active/NonOptimized";
  588. case ALUA_ACCESS_STATE_STANDBY:
  589. return "Standby";
  590. case ALUA_ACCESS_STATE_UNAVAILABLE:
  591. return "Unavailable";
  592. case ALUA_ACCESS_STATE_OFFLINE:
  593. return "Offline";
  594. default:
  595. return "Unknown";
  596. }
  597. return NULL;
  598. }
  599. char *core_alua_dump_status(int status)
  600. {
  601. switch (status) {
  602. case ALUA_STATUS_NONE:
  603. return "None";
  604. case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
  605. return "Altered by Explict STPG";
  606. case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
  607. return "Altered by Implict ALUA";
  608. default:
  609. return "Unknown";
  610. }
  611. return NULL;
  612. }
  613. /*
  614. * Used by fabric modules to determine when we need to delay processing
  615. * for the Active/NonOptimized paths..
  616. */
  617. int core_alua_check_nonop_delay(
  618. struct se_cmd *cmd)
  619. {
  620. if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
  621. return 0;
  622. if (in_interrupt())
  623. return 0;
  624. /*
  625. * The ALUA Active/NonOptimized access state delay can be disabled
  626. * in via configfs with a value of zero
  627. */
  628. if (!cmd->alua_nonop_delay)
  629. return 0;
  630. /*
  631. * struct se_cmd->alua_nonop_delay gets set by a target port group
  632. * defined interval in core_alua_state_nonoptimized()
  633. */
  634. msleep_interruptible(cmd->alua_nonop_delay);
  635. return 0;
  636. }
  637. EXPORT_SYMBOL(core_alua_check_nonop_delay);
  638. /*
  639. * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
  640. *
  641. */
  642. static int core_alua_write_tpg_metadata(
  643. const char *path,
  644. unsigned char *md_buf,
  645. u32 md_buf_len)
  646. {
  647. mm_segment_t old_fs;
  648. struct file *file;
  649. struct iovec iov[1];
  650. int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
  651. memset(iov, 0, sizeof(struct iovec));
  652. file = filp_open(path, flags, 0600);
  653. if (IS_ERR(file) || !file || !file->f_dentry) {
  654. pr_err("filp_open(%s) for ALUA metadata failed\n",
  655. path);
  656. return -ENODEV;
  657. }
  658. iov[0].iov_base = &md_buf[0];
  659. iov[0].iov_len = md_buf_len;
  660. old_fs = get_fs();
  661. set_fs(get_ds());
  662. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  663. set_fs(old_fs);
  664. if (ret < 0) {
  665. pr_err("Error writing ALUA metadata file: %s\n", path);
  666. filp_close(file, NULL);
  667. return -EIO;
  668. }
  669. filp_close(file, NULL);
  670. return 0;
  671. }
  672. /*
  673. * Called with tg_pt_gp->tg_pt_gp_md_mutex held
  674. */
  675. static int core_alua_update_tpg_primary_metadata(
  676. struct t10_alua_tg_pt_gp *tg_pt_gp,
  677. int primary_state,
  678. unsigned char *md_buf)
  679. {
  680. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  681. struct t10_wwn *wwn = &su_dev->t10_wwn;
  682. char path[ALUA_METADATA_PATH_LEN];
  683. int len;
  684. memset(path, 0, ALUA_METADATA_PATH_LEN);
  685. len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
  686. "tg_pt_gp_id=%hu\n"
  687. "alua_access_state=0x%02x\n"
  688. "alua_access_status=0x%02x\n",
  689. tg_pt_gp->tg_pt_gp_id, primary_state,
  690. tg_pt_gp->tg_pt_gp_alua_access_status);
  691. snprintf(path, ALUA_METADATA_PATH_LEN,
  692. "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
  693. config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
  694. return core_alua_write_tpg_metadata(path, md_buf, len);
  695. }
  696. static int core_alua_do_transition_tg_pt(
  697. struct t10_alua_tg_pt_gp *tg_pt_gp,
  698. struct se_port *l_port,
  699. struct se_node_acl *nacl,
  700. unsigned char *md_buf,
  701. int new_state,
  702. int explict)
  703. {
  704. struct se_dev_entry *se_deve;
  705. struct se_lun_acl *lacl;
  706. struct se_port *port;
  707. struct t10_alua_tg_pt_gp_member *mem;
  708. int old_state = 0;
  709. /*
  710. * Save the old primary ALUA access state, and set the current state
  711. * to ALUA_ACCESS_STATE_TRANSITION.
  712. */
  713. old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  714. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  715. ALUA_ACCESS_STATE_TRANSITION);
  716. tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
  717. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  718. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  719. /*
  720. * Check for the optional ALUA primary state transition delay
  721. */
  722. if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
  723. msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  724. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  725. list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
  726. tg_pt_gp_mem_list) {
  727. port = mem->tg_pt;
  728. /*
  729. * After an implicit target port asymmetric access state
  730. * change, a device server shall establish a unit attention
  731. * condition for the initiator port associated with every I_T
  732. * nexus with the additional sense code set to ASYMMETRIC
  733. * ACCESS STATE CHAGED.
  734. *
  735. * After an explicit target port asymmetric access state
  736. * change, a device server shall establish a unit attention
  737. * condition with the additional sense code set to ASYMMETRIC
  738. * ACCESS STATE CHANGED for the initiator port associated with
  739. * every I_T nexus other than the I_T nexus on which the SET
  740. * TARGET PORT GROUPS command
  741. */
  742. atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
  743. smp_mb__after_atomic_inc();
  744. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  745. spin_lock_bh(&port->sep_alua_lock);
  746. list_for_each_entry(se_deve, &port->sep_alua_list,
  747. alua_port_list) {
  748. lacl = se_deve->se_lun_acl;
  749. /*
  750. * se_deve->se_lun_acl pointer may be NULL for a
  751. * entry created without explict Node+MappedLUN ACLs
  752. */
  753. if (!lacl)
  754. continue;
  755. if (explict &&
  756. (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
  757. (l_port != NULL) && (l_port == port))
  758. continue;
  759. core_scsi3_ua_allocate(lacl->se_lun_nacl,
  760. se_deve->mapped_lun, 0x2A,
  761. ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
  762. }
  763. spin_unlock_bh(&port->sep_alua_lock);
  764. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  765. atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
  766. smp_mb__after_atomic_dec();
  767. }
  768. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  769. /*
  770. * Update the ALUA metadata buf that has been allocated in
  771. * core_alua_do_port_transition(), this metadata will be written
  772. * to struct file.
  773. *
  774. * Note that there is the case where we do not want to update the
  775. * metadata when the saved metadata is being parsed in userspace
  776. * when setting the existing port access state and access status.
  777. *
  778. * Also note that the failure to write out the ALUA metadata to
  779. * struct file does NOT affect the actual ALUA transition.
  780. */
  781. if (tg_pt_gp->tg_pt_gp_write_metadata) {
  782. mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
  783. core_alua_update_tpg_primary_metadata(tg_pt_gp,
  784. new_state, md_buf);
  785. mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
  786. }
  787. /*
  788. * Set the current primary ALUA access state to the requested new state
  789. */
  790. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
  791. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  792. " from primary access state %s to %s\n", (explict) ? "explict" :
  793. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  794. tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
  795. core_alua_dump_state(new_state));
  796. return 0;
  797. }
  798. int core_alua_do_port_transition(
  799. struct t10_alua_tg_pt_gp *l_tg_pt_gp,
  800. struct se_device *l_dev,
  801. struct se_port *l_port,
  802. struct se_node_acl *l_nacl,
  803. int new_state,
  804. int explict)
  805. {
  806. struct se_device *dev;
  807. struct se_port *port;
  808. struct se_subsystem_dev *su_dev;
  809. struct se_node_acl *nacl;
  810. struct t10_alua_lu_gp *lu_gp;
  811. struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
  812. struct t10_alua_tg_pt_gp *tg_pt_gp;
  813. unsigned char *md_buf;
  814. int primary;
  815. if (core_alua_check_transition(new_state, &primary) != 0)
  816. return -EINVAL;
  817. md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
  818. if (!md_buf) {
  819. pr_err("Unable to allocate buf for ALUA metadata\n");
  820. return -ENOMEM;
  821. }
  822. local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
  823. spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
  824. lu_gp = local_lu_gp_mem->lu_gp;
  825. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  826. smp_mb__after_atomic_inc();
  827. spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
  828. /*
  829. * For storage objects that are members of the 'default_lu_gp',
  830. * we only do transition on the passed *l_tp_pt_gp, and not
  831. * on all of the matching target port groups IDs in default_lu_gp.
  832. */
  833. if (!lu_gp->lu_gp_id) {
  834. /*
  835. * core_alua_do_transition_tg_pt() will always return
  836. * success.
  837. */
  838. core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
  839. md_buf, new_state, explict);
  840. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  841. smp_mb__after_atomic_dec();
  842. kfree(md_buf);
  843. return 0;
  844. }
  845. /*
  846. * For all other LU groups aside from 'default_lu_gp', walk all of
  847. * the associated storage objects looking for a matching target port
  848. * group ID from the local target port group.
  849. */
  850. spin_lock(&lu_gp->lu_gp_lock);
  851. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
  852. lu_gp_mem_list) {
  853. dev = lu_gp_mem->lu_gp_mem_dev;
  854. su_dev = dev->se_sub_dev;
  855. atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
  856. smp_mb__after_atomic_inc();
  857. spin_unlock(&lu_gp->lu_gp_lock);
  858. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  859. list_for_each_entry(tg_pt_gp,
  860. &su_dev->t10_alua.tg_pt_gps_list,
  861. tg_pt_gp_list) {
  862. if (!tg_pt_gp->tg_pt_gp_valid_id)
  863. continue;
  864. /*
  865. * If the target behavior port asymmetric access state
  866. * is changed for any target port group accessiable via
  867. * a logical unit within a LU group, the target port
  868. * behavior group asymmetric access states for the same
  869. * target port group accessible via other logical units
  870. * in that LU group will also change.
  871. */
  872. if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
  873. continue;
  874. if (l_tg_pt_gp == tg_pt_gp) {
  875. port = l_port;
  876. nacl = l_nacl;
  877. } else {
  878. port = NULL;
  879. nacl = NULL;
  880. }
  881. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  882. smp_mb__after_atomic_inc();
  883. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  884. /*
  885. * core_alua_do_transition_tg_pt() will always return
  886. * success.
  887. */
  888. core_alua_do_transition_tg_pt(tg_pt_gp, port,
  889. nacl, md_buf, new_state, explict);
  890. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  891. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  892. smp_mb__after_atomic_dec();
  893. }
  894. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  895. spin_lock(&lu_gp->lu_gp_lock);
  896. atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
  897. smp_mb__after_atomic_dec();
  898. }
  899. spin_unlock(&lu_gp->lu_gp_lock);
  900. pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
  901. " Group IDs: %hu %s transition to primary state: %s\n",
  902. config_item_name(&lu_gp->lu_gp_group.cg_item),
  903. l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
  904. core_alua_dump_state(new_state));
  905. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  906. smp_mb__after_atomic_dec();
  907. kfree(md_buf);
  908. return 0;
  909. }
  910. /*
  911. * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
  912. */
  913. static int core_alua_update_tpg_secondary_metadata(
  914. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  915. struct se_port *port,
  916. unsigned char *md_buf,
  917. u32 md_buf_len)
  918. {
  919. struct se_portal_group *se_tpg = port->sep_tpg;
  920. char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
  921. int len;
  922. memset(path, 0, ALUA_METADATA_PATH_LEN);
  923. memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
  924. len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
  925. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
  926. if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
  927. snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
  928. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  929. len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
  930. "alua_tg_pt_status=0x%02x\n",
  931. atomic_read(&port->sep_tg_pt_secondary_offline),
  932. port->sep_tg_pt_secondary_stat);
  933. snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
  934. se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
  935. port->sep_lun->unpacked_lun);
  936. return core_alua_write_tpg_metadata(path, md_buf, len);
  937. }
  938. static int core_alua_set_tg_pt_secondary_state(
  939. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  940. struct se_port *port,
  941. int explict,
  942. int offline)
  943. {
  944. struct t10_alua_tg_pt_gp *tg_pt_gp;
  945. unsigned char *md_buf;
  946. u32 md_buf_len;
  947. int trans_delay_msecs;
  948. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  949. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  950. if (!tg_pt_gp) {
  951. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  952. pr_err("Unable to complete secondary state"
  953. " transition\n");
  954. return -EINVAL;
  955. }
  956. trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
  957. /*
  958. * Set the secondary ALUA target port access state to OFFLINE
  959. * or release the previously secondary state for struct se_port
  960. */
  961. if (offline)
  962. atomic_set(&port->sep_tg_pt_secondary_offline, 1);
  963. else
  964. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  965. md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
  966. port->sep_tg_pt_secondary_stat = (explict) ?
  967. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  968. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  969. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  970. " to secondary access state: %s\n", (explict) ? "explict" :
  971. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  972. tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
  973. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  974. /*
  975. * Do the optional transition delay after we set the secondary
  976. * ALUA access state.
  977. */
  978. if (trans_delay_msecs != 0)
  979. msleep_interruptible(trans_delay_msecs);
  980. /*
  981. * See if we need to update the ALUA fabric port metadata for
  982. * secondary state and status
  983. */
  984. if (port->sep_tg_pt_secondary_write_md) {
  985. md_buf = kzalloc(md_buf_len, GFP_KERNEL);
  986. if (!md_buf) {
  987. pr_err("Unable to allocate md_buf for"
  988. " secondary ALUA access metadata\n");
  989. return -ENOMEM;
  990. }
  991. mutex_lock(&port->sep_tg_pt_md_mutex);
  992. core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
  993. md_buf, md_buf_len);
  994. mutex_unlock(&port->sep_tg_pt_md_mutex);
  995. kfree(md_buf);
  996. }
  997. return 0;
  998. }
  999. struct t10_alua_lu_gp *
  1000. core_alua_allocate_lu_gp(const char *name, int def_group)
  1001. {
  1002. struct t10_alua_lu_gp *lu_gp;
  1003. lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
  1004. if (!lu_gp) {
  1005. pr_err("Unable to allocate struct t10_alua_lu_gp\n");
  1006. return ERR_PTR(-ENOMEM);
  1007. }
  1008. INIT_LIST_HEAD(&lu_gp->lu_gp_node);
  1009. INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
  1010. spin_lock_init(&lu_gp->lu_gp_lock);
  1011. atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
  1012. if (def_group) {
  1013. lu_gp->lu_gp_id = alua_lu_gps_counter++;
  1014. lu_gp->lu_gp_valid_id = 1;
  1015. alua_lu_gps_count++;
  1016. }
  1017. return lu_gp;
  1018. }
  1019. int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
  1020. {
  1021. struct t10_alua_lu_gp *lu_gp_tmp;
  1022. u16 lu_gp_id_tmp;
  1023. /*
  1024. * The lu_gp->lu_gp_id may only be set once..
  1025. */
  1026. if (lu_gp->lu_gp_valid_id) {
  1027. pr_warn("ALUA LU Group already has a valid ID,"
  1028. " ignoring request\n");
  1029. return -EINVAL;
  1030. }
  1031. spin_lock(&lu_gps_lock);
  1032. if (alua_lu_gps_count == 0x0000ffff) {
  1033. pr_err("Maximum ALUA alua_lu_gps_count:"
  1034. " 0x0000ffff reached\n");
  1035. spin_unlock(&lu_gps_lock);
  1036. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1037. return -ENOSPC;
  1038. }
  1039. again:
  1040. lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
  1041. alua_lu_gps_counter++;
  1042. list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
  1043. if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
  1044. if (!lu_gp_id)
  1045. goto again;
  1046. pr_warn("ALUA Logical Unit Group ID: %hu"
  1047. " already exists, ignoring request\n",
  1048. lu_gp_id);
  1049. spin_unlock(&lu_gps_lock);
  1050. return -EINVAL;
  1051. }
  1052. }
  1053. lu_gp->lu_gp_id = lu_gp_id_tmp;
  1054. lu_gp->lu_gp_valid_id = 1;
  1055. list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
  1056. alua_lu_gps_count++;
  1057. spin_unlock(&lu_gps_lock);
  1058. return 0;
  1059. }
  1060. static struct t10_alua_lu_gp_member *
  1061. core_alua_allocate_lu_gp_mem(struct se_device *dev)
  1062. {
  1063. struct t10_alua_lu_gp_member *lu_gp_mem;
  1064. lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
  1065. if (!lu_gp_mem) {
  1066. pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
  1067. return ERR_PTR(-ENOMEM);
  1068. }
  1069. INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
  1070. spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
  1071. atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
  1072. lu_gp_mem->lu_gp_mem_dev = dev;
  1073. dev->dev_alua_lu_gp_mem = lu_gp_mem;
  1074. return lu_gp_mem;
  1075. }
  1076. void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
  1077. {
  1078. struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
  1079. /*
  1080. * Once we have reached this point, config_item_put() has
  1081. * already been called from target_core_alua_drop_lu_gp().
  1082. *
  1083. * Here, we remove the *lu_gp from the global list so that
  1084. * no associations can be made while we are releasing
  1085. * struct t10_alua_lu_gp.
  1086. */
  1087. spin_lock(&lu_gps_lock);
  1088. list_del(&lu_gp->lu_gp_node);
  1089. alua_lu_gps_count--;
  1090. spin_unlock(&lu_gps_lock);
  1091. /*
  1092. * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
  1093. * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
  1094. * released with core_alua_put_lu_gp_from_name()
  1095. */
  1096. while (atomic_read(&lu_gp->lu_gp_ref_cnt))
  1097. cpu_relax();
  1098. /*
  1099. * Release reference to struct t10_alua_lu_gp * from all associated
  1100. * struct se_device.
  1101. */
  1102. spin_lock(&lu_gp->lu_gp_lock);
  1103. list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
  1104. &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1105. if (lu_gp_mem->lu_gp_assoc) {
  1106. list_del(&lu_gp_mem->lu_gp_mem_list);
  1107. lu_gp->lu_gp_members--;
  1108. lu_gp_mem->lu_gp_assoc = 0;
  1109. }
  1110. spin_unlock(&lu_gp->lu_gp_lock);
  1111. /*
  1112. *
  1113. * lu_gp_mem is associated with a single
  1114. * struct se_device->dev_alua_lu_gp_mem, and is released when
  1115. * struct se_device is released via core_alua_free_lu_gp_mem().
  1116. *
  1117. * If the passed lu_gp does NOT match the default_lu_gp, assume
  1118. * we want to re-assocate a given lu_gp_mem with default_lu_gp.
  1119. */
  1120. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1121. if (lu_gp != default_lu_gp)
  1122. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1123. default_lu_gp);
  1124. else
  1125. lu_gp_mem->lu_gp = NULL;
  1126. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1127. spin_lock(&lu_gp->lu_gp_lock);
  1128. }
  1129. spin_unlock(&lu_gp->lu_gp_lock);
  1130. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1131. }
  1132. void core_alua_free_lu_gp_mem(struct se_device *dev)
  1133. {
  1134. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1135. struct t10_alua *alua = &su_dev->t10_alua;
  1136. struct t10_alua_lu_gp *lu_gp;
  1137. struct t10_alua_lu_gp_member *lu_gp_mem;
  1138. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1139. return;
  1140. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1141. if (!lu_gp_mem)
  1142. return;
  1143. while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
  1144. cpu_relax();
  1145. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1146. lu_gp = lu_gp_mem->lu_gp;
  1147. if (lu_gp) {
  1148. spin_lock(&lu_gp->lu_gp_lock);
  1149. if (lu_gp_mem->lu_gp_assoc) {
  1150. list_del(&lu_gp_mem->lu_gp_mem_list);
  1151. lu_gp->lu_gp_members--;
  1152. lu_gp_mem->lu_gp_assoc = 0;
  1153. }
  1154. spin_unlock(&lu_gp->lu_gp_lock);
  1155. lu_gp_mem->lu_gp = NULL;
  1156. }
  1157. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1158. kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
  1159. }
  1160. struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
  1161. {
  1162. struct t10_alua_lu_gp *lu_gp;
  1163. struct config_item *ci;
  1164. spin_lock(&lu_gps_lock);
  1165. list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
  1166. if (!lu_gp->lu_gp_valid_id)
  1167. continue;
  1168. ci = &lu_gp->lu_gp_group.cg_item;
  1169. if (!strcmp(config_item_name(ci), name)) {
  1170. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  1171. spin_unlock(&lu_gps_lock);
  1172. return lu_gp;
  1173. }
  1174. }
  1175. spin_unlock(&lu_gps_lock);
  1176. return NULL;
  1177. }
  1178. void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
  1179. {
  1180. spin_lock(&lu_gps_lock);
  1181. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  1182. spin_unlock(&lu_gps_lock);
  1183. }
  1184. /*
  1185. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1186. */
  1187. void __core_alua_attach_lu_gp_mem(
  1188. struct t10_alua_lu_gp_member *lu_gp_mem,
  1189. struct t10_alua_lu_gp *lu_gp)
  1190. {
  1191. spin_lock(&lu_gp->lu_gp_lock);
  1192. lu_gp_mem->lu_gp = lu_gp;
  1193. lu_gp_mem->lu_gp_assoc = 1;
  1194. list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
  1195. lu_gp->lu_gp_members++;
  1196. spin_unlock(&lu_gp->lu_gp_lock);
  1197. }
  1198. /*
  1199. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1200. */
  1201. void __core_alua_drop_lu_gp_mem(
  1202. struct t10_alua_lu_gp_member *lu_gp_mem,
  1203. struct t10_alua_lu_gp *lu_gp)
  1204. {
  1205. spin_lock(&lu_gp->lu_gp_lock);
  1206. list_del(&lu_gp_mem->lu_gp_mem_list);
  1207. lu_gp_mem->lu_gp = NULL;
  1208. lu_gp_mem->lu_gp_assoc = 0;
  1209. lu_gp->lu_gp_members--;
  1210. spin_unlock(&lu_gp->lu_gp_lock);
  1211. }
  1212. struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
  1213. struct se_subsystem_dev *su_dev,
  1214. const char *name,
  1215. int def_group)
  1216. {
  1217. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1218. tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
  1219. if (!tg_pt_gp) {
  1220. pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
  1221. return NULL;
  1222. }
  1223. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
  1224. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
  1225. mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
  1226. spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
  1227. atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
  1228. tg_pt_gp->tg_pt_gp_su_dev = su_dev;
  1229. tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
  1230. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  1231. ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
  1232. /*
  1233. * Enable both explict and implict ALUA support by default
  1234. */
  1235. tg_pt_gp->tg_pt_gp_alua_access_type =
  1236. TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
  1237. /*
  1238. * Set the default Active/NonOptimized Delay in milliseconds
  1239. */
  1240. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
  1241. tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
  1242. if (def_group) {
  1243. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1244. tg_pt_gp->tg_pt_gp_id =
  1245. su_dev->t10_alua.alua_tg_pt_gps_counter++;
  1246. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1247. su_dev->t10_alua.alua_tg_pt_gps_count++;
  1248. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1249. &su_dev->t10_alua.tg_pt_gps_list);
  1250. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1251. }
  1252. return tg_pt_gp;
  1253. }
  1254. int core_alua_set_tg_pt_gp_id(
  1255. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1256. u16 tg_pt_gp_id)
  1257. {
  1258. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1259. struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
  1260. u16 tg_pt_gp_id_tmp;
  1261. /*
  1262. * The tg_pt_gp->tg_pt_gp_id may only be set once..
  1263. */
  1264. if (tg_pt_gp->tg_pt_gp_valid_id) {
  1265. pr_warn("ALUA TG PT Group already has a valid ID,"
  1266. " ignoring request\n");
  1267. return -EINVAL;
  1268. }
  1269. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1270. if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
  1271. pr_err("Maximum ALUA alua_tg_pt_gps_count:"
  1272. " 0x0000ffff reached\n");
  1273. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1274. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1275. return -ENOSPC;
  1276. }
  1277. again:
  1278. tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
  1279. su_dev->t10_alua.alua_tg_pt_gps_counter++;
  1280. list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
  1281. tg_pt_gp_list) {
  1282. if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
  1283. if (!tg_pt_gp_id)
  1284. goto again;
  1285. pr_err("ALUA Target Port Group ID: %hu already"
  1286. " exists, ignoring request\n", tg_pt_gp_id);
  1287. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1288. return -EINVAL;
  1289. }
  1290. }
  1291. tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
  1292. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1293. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1294. &su_dev->t10_alua.tg_pt_gps_list);
  1295. su_dev->t10_alua.alua_tg_pt_gps_count++;
  1296. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1297. return 0;
  1298. }
  1299. struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
  1300. struct se_port *port)
  1301. {
  1302. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1303. tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
  1304. GFP_KERNEL);
  1305. if (!tg_pt_gp_mem) {
  1306. pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
  1307. return ERR_PTR(-ENOMEM);
  1308. }
  1309. INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1310. spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1311. atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
  1312. tg_pt_gp_mem->tg_pt = port;
  1313. port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
  1314. return tg_pt_gp_mem;
  1315. }
  1316. void core_alua_free_tg_pt_gp(
  1317. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1318. {
  1319. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1320. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
  1321. /*
  1322. * Once we have reached this point, config_item_put() has already
  1323. * been called from target_core_alua_drop_tg_pt_gp().
  1324. *
  1325. * Here we remove *tg_pt_gp from the global list so that
  1326. * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
  1327. * can be made while we are releasing struct t10_alua_tg_pt_gp.
  1328. */
  1329. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1330. list_del(&tg_pt_gp->tg_pt_gp_list);
  1331. su_dev->t10_alua.alua_tg_pt_gps_counter--;
  1332. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1333. /*
  1334. * Allow a struct t10_alua_tg_pt_gp_member * referenced by
  1335. * core_alua_get_tg_pt_gp_by_name() in
  1336. * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
  1337. * to be released with core_alua_put_tg_pt_gp_from_name().
  1338. */
  1339. while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
  1340. cpu_relax();
  1341. /*
  1342. * Release reference to struct t10_alua_tg_pt_gp from all associated
  1343. * struct se_port.
  1344. */
  1345. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1346. list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
  1347. &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
  1348. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1349. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1350. tg_pt_gp->tg_pt_gp_members--;
  1351. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1352. }
  1353. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1354. /*
  1355. * tg_pt_gp_mem is associated with a single
  1356. * se_port->sep_alua_tg_pt_gp_mem, and is released via
  1357. * core_alua_free_tg_pt_gp_mem().
  1358. *
  1359. * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
  1360. * assume we want to re-assocate a given tg_pt_gp_mem with
  1361. * default_tg_pt_gp.
  1362. */
  1363. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1364. if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
  1365. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1366. su_dev->t10_alua.default_tg_pt_gp);
  1367. } else
  1368. tg_pt_gp_mem->tg_pt_gp = NULL;
  1369. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1370. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1371. }
  1372. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1373. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1374. }
  1375. void core_alua_free_tg_pt_gp_mem(struct se_port *port)
  1376. {
  1377. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1378. struct t10_alua *alua = &su_dev->t10_alua;
  1379. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1380. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1381. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1382. return;
  1383. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1384. if (!tg_pt_gp_mem)
  1385. return;
  1386. while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
  1387. cpu_relax();
  1388. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1389. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1390. if (tg_pt_gp) {
  1391. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1392. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1393. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1394. tg_pt_gp->tg_pt_gp_members--;
  1395. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1396. }
  1397. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1398. tg_pt_gp_mem->tg_pt_gp = NULL;
  1399. }
  1400. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1401. kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
  1402. }
  1403. static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
  1404. struct se_subsystem_dev *su_dev,
  1405. const char *name)
  1406. {
  1407. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1408. struct config_item *ci;
  1409. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1410. list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
  1411. tg_pt_gp_list) {
  1412. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1413. continue;
  1414. ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1415. if (!strcmp(config_item_name(ci), name)) {
  1416. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1417. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1418. return tg_pt_gp;
  1419. }
  1420. }
  1421. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1422. return NULL;
  1423. }
  1424. static void core_alua_put_tg_pt_gp_from_name(
  1425. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1426. {
  1427. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1428. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1429. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1430. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1431. }
  1432. /*
  1433. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1434. */
  1435. void __core_alua_attach_tg_pt_gp_mem(
  1436. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1437. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1438. {
  1439. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1440. tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
  1441. tg_pt_gp_mem->tg_pt_gp_assoc = 1;
  1442. list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
  1443. &tg_pt_gp->tg_pt_gp_mem_list);
  1444. tg_pt_gp->tg_pt_gp_members++;
  1445. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1446. }
  1447. /*
  1448. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1449. */
  1450. static void __core_alua_drop_tg_pt_gp_mem(
  1451. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1452. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1453. {
  1454. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1455. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1456. tg_pt_gp_mem->tg_pt_gp = NULL;
  1457. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1458. tg_pt_gp->tg_pt_gp_members--;
  1459. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1460. }
  1461. ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
  1462. {
  1463. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1464. struct config_item *tg_pt_ci;
  1465. struct t10_alua *alua = &su_dev->t10_alua;
  1466. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1467. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1468. ssize_t len = 0;
  1469. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1470. return len;
  1471. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1472. if (!tg_pt_gp_mem)
  1473. return len;
  1474. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1475. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1476. if (tg_pt_gp) {
  1477. tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1478. len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
  1479. " %hu\nTG Port Primary Access State: %s\nTG Port "
  1480. "Primary Access Status: %s\nTG Port Secondary Access"
  1481. " State: %s\nTG Port Secondary Access Status: %s\n",
  1482. config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
  1483. core_alua_dump_state(atomic_read(
  1484. &tg_pt_gp->tg_pt_gp_alua_access_state)),
  1485. core_alua_dump_status(
  1486. tg_pt_gp->tg_pt_gp_alua_access_status),
  1487. (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
  1488. "Offline" : "None",
  1489. core_alua_dump_status(port->sep_tg_pt_secondary_stat));
  1490. }
  1491. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1492. return len;
  1493. }
  1494. ssize_t core_alua_store_tg_pt_gp_info(
  1495. struct se_port *port,
  1496. const char *page,
  1497. size_t count)
  1498. {
  1499. struct se_portal_group *tpg;
  1500. struct se_lun *lun;
  1501. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1502. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
  1503. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1504. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1505. int move = 0;
  1506. tpg = port->sep_tpg;
  1507. lun = port->sep_lun;
  1508. if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
  1509. pr_warn("SPC3_ALUA_EMULATED not enabled for"
  1510. " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1511. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1512. config_item_name(&lun->lun_group.cg_item));
  1513. return -EINVAL;
  1514. }
  1515. if (count > TG_PT_GROUP_NAME_BUF) {
  1516. pr_err("ALUA Target Port Group alias too large!\n");
  1517. return -EINVAL;
  1518. }
  1519. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1520. memcpy(buf, page, count);
  1521. /*
  1522. * Any ALUA target port group alias besides "NULL" means we will be
  1523. * making a new group association.
  1524. */
  1525. if (strcmp(strstrip(buf), "NULL")) {
  1526. /*
  1527. * core_alua_get_tg_pt_gp_by_name() will increment reference to
  1528. * struct t10_alua_tg_pt_gp. This reference is released with
  1529. * core_alua_put_tg_pt_gp_from_name() below.
  1530. */
  1531. tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
  1532. strstrip(buf));
  1533. if (!tg_pt_gp_new)
  1534. return -ENODEV;
  1535. }
  1536. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1537. if (!tg_pt_gp_mem) {
  1538. if (tg_pt_gp_new)
  1539. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1540. pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
  1541. return -EINVAL;
  1542. }
  1543. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1544. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1545. if (tg_pt_gp) {
  1546. /*
  1547. * Clearing an existing tg_pt_gp association, and replacing
  1548. * with the default_tg_pt_gp.
  1549. */
  1550. if (!tg_pt_gp_new) {
  1551. pr_debug("Target_Core_ConfigFS: Moving"
  1552. " %s/tpgt_%hu/%s from ALUA Target Port Group:"
  1553. " alua/%s, ID: %hu back to"
  1554. " default_tg_pt_gp\n",
  1555. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1556. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1557. config_item_name(&lun->lun_group.cg_item),
  1558. config_item_name(
  1559. &tg_pt_gp->tg_pt_gp_group.cg_item),
  1560. tg_pt_gp->tg_pt_gp_id);
  1561. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1562. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1563. su_dev->t10_alua.default_tg_pt_gp);
  1564. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1565. return count;
  1566. }
  1567. /*
  1568. * Removing existing association of tg_pt_gp_mem with tg_pt_gp
  1569. */
  1570. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1571. move = 1;
  1572. }
  1573. /*
  1574. * Associate tg_pt_gp_mem with tg_pt_gp_new.
  1575. */
  1576. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
  1577. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1578. pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
  1579. " Target Port Group: alua/%s, ID: %hu\n", (move) ?
  1580. "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1581. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1582. config_item_name(&lun->lun_group.cg_item),
  1583. config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
  1584. tg_pt_gp_new->tg_pt_gp_id);
  1585. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1586. return count;
  1587. }
  1588. ssize_t core_alua_show_access_type(
  1589. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1590. char *page)
  1591. {
  1592. if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
  1593. (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
  1594. return sprintf(page, "Implict and Explict\n");
  1595. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
  1596. return sprintf(page, "Implict\n");
  1597. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
  1598. return sprintf(page, "Explict\n");
  1599. else
  1600. return sprintf(page, "None\n");
  1601. }
  1602. ssize_t core_alua_store_access_type(
  1603. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1604. const char *page,
  1605. size_t count)
  1606. {
  1607. unsigned long tmp;
  1608. int ret;
  1609. ret = strict_strtoul(page, 0, &tmp);
  1610. if (ret < 0) {
  1611. pr_err("Unable to extract alua_access_type\n");
  1612. return -EINVAL;
  1613. }
  1614. if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
  1615. pr_err("Illegal value for alua_access_type:"
  1616. " %lu\n", tmp);
  1617. return -EINVAL;
  1618. }
  1619. if (tmp == 3)
  1620. tg_pt_gp->tg_pt_gp_alua_access_type =
  1621. TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
  1622. else if (tmp == 2)
  1623. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
  1624. else if (tmp == 1)
  1625. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
  1626. else
  1627. tg_pt_gp->tg_pt_gp_alua_access_type = 0;
  1628. return count;
  1629. }
  1630. ssize_t core_alua_show_nonop_delay_msecs(
  1631. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1632. char *page)
  1633. {
  1634. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
  1635. }
  1636. ssize_t core_alua_store_nonop_delay_msecs(
  1637. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1638. const char *page,
  1639. size_t count)
  1640. {
  1641. unsigned long tmp;
  1642. int ret;
  1643. ret = strict_strtoul(page, 0, &tmp);
  1644. if (ret < 0) {
  1645. pr_err("Unable to extract nonop_delay_msecs\n");
  1646. return -EINVAL;
  1647. }
  1648. if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
  1649. pr_err("Passed nonop_delay_msecs: %lu, exceeds"
  1650. " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
  1651. ALUA_MAX_NONOP_DELAY_MSECS);
  1652. return -EINVAL;
  1653. }
  1654. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
  1655. return count;
  1656. }
  1657. ssize_t core_alua_show_trans_delay_msecs(
  1658. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1659. char *page)
  1660. {
  1661. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  1662. }
  1663. ssize_t core_alua_store_trans_delay_msecs(
  1664. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1665. const char *page,
  1666. size_t count)
  1667. {
  1668. unsigned long tmp;
  1669. int ret;
  1670. ret = strict_strtoul(page, 0, &tmp);
  1671. if (ret < 0) {
  1672. pr_err("Unable to extract trans_delay_msecs\n");
  1673. return -EINVAL;
  1674. }
  1675. if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
  1676. pr_err("Passed trans_delay_msecs: %lu, exceeds"
  1677. " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
  1678. ALUA_MAX_TRANS_DELAY_MSECS);
  1679. return -EINVAL;
  1680. }
  1681. tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
  1682. return count;
  1683. }
  1684. ssize_t core_alua_show_preferred_bit(
  1685. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1686. char *page)
  1687. {
  1688. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
  1689. }
  1690. ssize_t core_alua_store_preferred_bit(
  1691. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1692. const char *page,
  1693. size_t count)
  1694. {
  1695. unsigned long tmp;
  1696. int ret;
  1697. ret = strict_strtoul(page, 0, &tmp);
  1698. if (ret < 0) {
  1699. pr_err("Unable to extract preferred ALUA value\n");
  1700. return -EINVAL;
  1701. }
  1702. if ((tmp != 0) && (tmp != 1)) {
  1703. pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
  1704. return -EINVAL;
  1705. }
  1706. tg_pt_gp->tg_pt_gp_pref = (int)tmp;
  1707. return count;
  1708. }
  1709. ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
  1710. {
  1711. if (!lun->lun_sep)
  1712. return -ENODEV;
  1713. return sprintf(page, "%d\n",
  1714. atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
  1715. }
  1716. ssize_t core_alua_store_offline_bit(
  1717. struct se_lun *lun,
  1718. const char *page,
  1719. size_t count)
  1720. {
  1721. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1722. unsigned long tmp;
  1723. int ret;
  1724. if (!lun->lun_sep)
  1725. return -ENODEV;
  1726. ret = strict_strtoul(page, 0, &tmp);
  1727. if (ret < 0) {
  1728. pr_err("Unable to extract alua_tg_pt_offline value\n");
  1729. return -EINVAL;
  1730. }
  1731. if ((tmp != 0) && (tmp != 1)) {
  1732. pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
  1733. tmp);
  1734. return -EINVAL;
  1735. }
  1736. tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
  1737. if (!tg_pt_gp_mem) {
  1738. pr_err("Unable to locate *tg_pt_gp_mem\n");
  1739. return -EINVAL;
  1740. }
  1741. ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
  1742. lun->lun_sep, 0, (int)tmp);
  1743. if (ret < 0)
  1744. return -EINVAL;
  1745. return count;
  1746. }
  1747. ssize_t core_alua_show_secondary_status(
  1748. struct se_lun *lun,
  1749. char *page)
  1750. {
  1751. return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
  1752. }
  1753. ssize_t core_alua_store_secondary_status(
  1754. struct se_lun *lun,
  1755. const char *page,
  1756. size_t count)
  1757. {
  1758. unsigned long tmp;
  1759. int ret;
  1760. ret = strict_strtoul(page, 0, &tmp);
  1761. if (ret < 0) {
  1762. pr_err("Unable to extract alua_tg_pt_status\n");
  1763. return -EINVAL;
  1764. }
  1765. if ((tmp != ALUA_STATUS_NONE) &&
  1766. (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  1767. (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
  1768. pr_err("Illegal value for alua_tg_pt_status: %lu\n",
  1769. tmp);
  1770. return -EINVAL;
  1771. }
  1772. lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
  1773. return count;
  1774. }
  1775. ssize_t core_alua_show_secondary_write_metadata(
  1776. struct se_lun *lun,
  1777. char *page)
  1778. {
  1779. return sprintf(page, "%d\n",
  1780. lun->lun_sep->sep_tg_pt_secondary_write_md);
  1781. }
  1782. ssize_t core_alua_store_secondary_write_metadata(
  1783. struct se_lun *lun,
  1784. const char *page,
  1785. size_t count)
  1786. {
  1787. unsigned long tmp;
  1788. int ret;
  1789. ret = strict_strtoul(page, 0, &tmp);
  1790. if (ret < 0) {
  1791. pr_err("Unable to extract alua_tg_pt_write_md\n");
  1792. return -EINVAL;
  1793. }
  1794. if ((tmp != 0) && (tmp != 1)) {
  1795. pr_err("Illegal value for alua_tg_pt_write_md:"
  1796. " %lu\n", tmp);
  1797. return -EINVAL;
  1798. }
  1799. lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
  1800. return count;
  1801. }
  1802. int core_setup_alua(struct se_device *dev, int force_pt)
  1803. {
  1804. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1805. struct t10_alua *alua = &su_dev->t10_alua;
  1806. struct t10_alua_lu_gp_member *lu_gp_mem;
  1807. /*
  1808. * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
  1809. * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
  1810. * cause a problem because libata and some SATA RAID HBAs appear
  1811. * under Linux/SCSI, but emulate SCSI logic themselves.
  1812. */
  1813. if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  1814. !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
  1815. alua->alua_type = SPC_ALUA_PASSTHROUGH;
  1816. alua->alua_state_check = &core_alua_state_check_nop;
  1817. pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
  1818. " emulation\n", dev->transport->name);
  1819. return 0;
  1820. }
  1821. /*
  1822. * If SPC-3 or above is reported by real or emulated struct se_device,
  1823. * use emulated ALUA.
  1824. */
  1825. if (dev->transport->get_device_rev(dev) >= SCSI_3) {
  1826. pr_debug("%s: Enabling ALUA Emulation for SPC-3"
  1827. " device\n", dev->transport->name);
  1828. /*
  1829. * Associate this struct se_device with the default ALUA
  1830. * LUN Group.
  1831. */
  1832. lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
  1833. if (IS_ERR(lu_gp_mem))
  1834. return PTR_ERR(lu_gp_mem);
  1835. alua->alua_type = SPC3_ALUA_EMULATED;
  1836. alua->alua_state_check = &core_alua_state_check;
  1837. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1838. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1839. default_lu_gp);
  1840. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1841. pr_debug("%s: Adding to default ALUA LU Group:"
  1842. " core/alua/lu_gps/default_lu_gp\n",
  1843. dev->transport->name);
  1844. } else {
  1845. alua->alua_type = SPC2_ALUA_DISABLED;
  1846. alua->alua_state_check = &core_alua_state_check_nop;
  1847. pr_debug("%s: Disabling ALUA Emulation for SPC-2"
  1848. " device\n", dev->transport->name);
  1849. }
  1850. return 0;
  1851. }