osd_initiator.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. /*
  2. * osd_initiator - Main body of the osd initiator library.
  3. *
  4. * Note: The file does not contain the advanced security functionality which
  5. * is only needed by the security_manager's initiators.
  6. *
  7. * Copyright (C) 2008 Panasas Inc. All rights reserved.
  8. *
  9. * Authors:
  10. * Boaz Harrosh <ooo@electrozaur.com>
  11. * Benny Halevy <bhalevy@panasas.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. *
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. * 3. Neither the name of the Panasas company nor the names of its
  26. * contributors may be used to endorse or promote products derived
  27. * from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  36. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  37. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  39. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/slab.h>
  42. #include <linux/module.h>
  43. #include <scsi/osd_initiator.h>
  44. #include <scsi/osd_sec.h>
  45. #include <scsi/osd_attributes.h>
  46. #include <scsi/osd_sense.h>
  47. #include <scsi/scsi_device.h>
  48. #include "osd_debug.h"
  49. #ifndef __unused
  50. # define __unused __attribute__((unused))
  51. #endif
  52. enum { OSD_REQ_RETRIES = 1 };
  53. MODULE_AUTHOR("Boaz Harrosh <ooo@electrozaur.com>");
  54. MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
  55. MODULE_LICENSE("GPL");
  56. static inline void build_test(void)
  57. {
  58. /* structures were not packed */
  59. BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
  60. BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
  61. BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
  62. }
  63. static const char *_osd_ver_desc(struct osd_request *or)
  64. {
  65. return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
  66. }
  67. #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
  68. static int _osd_get_print_system_info(struct osd_dev *od,
  69. void *caps, struct osd_dev_info *odi)
  70. {
  71. struct osd_request *or;
  72. struct osd_attr get_attrs[] = {
  73. ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
  74. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
  75. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
  76. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
  77. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
  78. ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
  79. ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
  80. ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
  81. ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
  82. ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
  83. /* IBM-OSD-SIM Has a bug with this one put it last */
  84. ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
  85. };
  86. void *iter = NULL, *pFirst;
  87. int nelem = ARRAY_SIZE(get_attrs), a = 0;
  88. int ret;
  89. or = osd_start_request(od, GFP_KERNEL);
  90. if (!or)
  91. return -ENOMEM;
  92. /* get attrs */
  93. osd_req_get_attributes(or, &osd_root_object);
  94. osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
  95. ret = osd_finalize_request(or, 0, caps, NULL);
  96. if (ret)
  97. goto out;
  98. ret = osd_execute_request(or);
  99. if (ret) {
  100. OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
  101. goto out;
  102. }
  103. osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
  104. OSD_INFO("Detected %s device\n",
  105. _osd_ver_desc(or));
  106. pFirst = get_attrs[a++].val_ptr;
  107. OSD_INFO("VENDOR_IDENTIFICATION [%s]\n",
  108. (char *)pFirst);
  109. pFirst = get_attrs[a++].val_ptr;
  110. OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
  111. (char *)pFirst);
  112. pFirst = get_attrs[a++].val_ptr;
  113. OSD_INFO("PRODUCT_MODEL [%s]\n",
  114. (char *)pFirst);
  115. pFirst = get_attrs[a++].val_ptr;
  116. OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
  117. pFirst ? get_unaligned_be32(pFirst) : ~0U);
  118. pFirst = get_attrs[a++].val_ptr;
  119. OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
  120. (char *)pFirst);
  121. odi->osdname_len = get_attrs[a].len;
  122. /* Avoid NULL for memcmp optimization 0-length is good enough */
  123. odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL);
  124. if (!odi->osdname) {
  125. ret = -ENOMEM;
  126. goto out;
  127. }
  128. if (odi->osdname_len)
  129. memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len);
  130. OSD_INFO("OSD_NAME [%s]\n", odi->osdname);
  131. a++;
  132. pFirst = get_attrs[a++].val_ptr;
  133. OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
  134. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  135. pFirst = get_attrs[a++].val_ptr;
  136. OSD_INFO("USED_CAPACITY [0x%llx]\n",
  137. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  138. pFirst = get_attrs[a++].val_ptr;
  139. OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n",
  140. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  141. if (a >= nelem)
  142. goto out;
  143. /* FIXME: Where are the time utilities */
  144. pFirst = get_attrs[a++].val_ptr;
  145. OSD_INFO("CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
  146. ((char *)pFirst)[0], ((char *)pFirst)[1],
  147. ((char *)pFirst)[2], ((char *)pFirst)[3],
  148. ((char *)pFirst)[4], ((char *)pFirst)[5]);
  149. if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
  150. unsigned len = get_attrs[a].len;
  151. char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
  152. hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
  153. sid_dump, sizeof(sid_dump), true);
  154. OSD_INFO("OSD_SYSTEM_ID(%d)\n"
  155. " [%s]\n", len, sid_dump);
  156. if (unlikely(len > sizeof(odi->systemid))) {
  157. OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). "
  158. "device identification might not work\n", len);
  159. len = sizeof(odi->systemid);
  160. }
  161. odi->systemid_len = len;
  162. memcpy(odi->systemid, get_attrs[a].val_ptr, len);
  163. a++;
  164. }
  165. out:
  166. osd_end_request(or);
  167. return ret;
  168. }
  169. int osd_auto_detect_ver(struct osd_dev *od,
  170. void *caps, struct osd_dev_info *odi)
  171. {
  172. int ret;
  173. /* Auto-detect the osd version */
  174. ret = _osd_get_print_system_info(od, caps, odi);
  175. if (ret) {
  176. osd_dev_set_ver(od, OSD_VER1);
  177. OSD_DEBUG("converting to OSD1\n");
  178. ret = _osd_get_print_system_info(od, caps, odi);
  179. }
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(osd_auto_detect_ver);
  183. static unsigned _osd_req_cdb_len(struct osd_request *or)
  184. {
  185. return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
  186. }
  187. static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
  188. {
  189. return osd_req_is_ver1(or) ?
  190. osdv1_attr_list_elem_size(len) :
  191. osdv2_attr_list_elem_size(len);
  192. }
  193. static void _osd_req_alist_elem_encode(struct osd_request *or,
  194. void *attr_last, const struct osd_attr *oa)
  195. {
  196. if (osd_req_is_ver1(or)) {
  197. struct osdv1_attributes_list_element *attr = attr_last;
  198. attr->attr_page = cpu_to_be32(oa->attr_page);
  199. attr->attr_id = cpu_to_be32(oa->attr_id);
  200. attr->attr_bytes = cpu_to_be16(oa->len);
  201. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  202. } else {
  203. struct osdv2_attributes_list_element *attr = attr_last;
  204. attr->attr_page = cpu_to_be32(oa->attr_page);
  205. attr->attr_id = cpu_to_be32(oa->attr_id);
  206. attr->attr_bytes = cpu_to_be16(oa->len);
  207. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  208. }
  209. }
  210. static int _osd_req_alist_elem_decode(struct osd_request *or,
  211. void *cur_p, struct osd_attr *oa, unsigned max_bytes)
  212. {
  213. unsigned inc;
  214. if (osd_req_is_ver1(or)) {
  215. struct osdv1_attributes_list_element *attr = cur_p;
  216. if (max_bytes < sizeof(*attr))
  217. return -1;
  218. oa->len = be16_to_cpu(attr->attr_bytes);
  219. inc = _osd_req_alist_elem_size(or, oa->len);
  220. if (inc > max_bytes)
  221. return -1;
  222. oa->attr_page = be32_to_cpu(attr->attr_page);
  223. oa->attr_id = be32_to_cpu(attr->attr_id);
  224. /* OSD1: On empty attributes we return a pointer to 2 bytes
  225. * of zeros. This keeps similar behaviour with OSD2.
  226. * (See below)
  227. */
  228. oa->val_ptr = likely(oa->len) ? attr->attr_val :
  229. (u8 *)&attr->attr_bytes;
  230. } else {
  231. struct osdv2_attributes_list_element *attr = cur_p;
  232. if (max_bytes < sizeof(*attr))
  233. return -1;
  234. oa->len = be16_to_cpu(attr->attr_bytes);
  235. inc = _osd_req_alist_elem_size(or, oa->len);
  236. if (inc > max_bytes)
  237. return -1;
  238. oa->attr_page = be32_to_cpu(attr->attr_page);
  239. oa->attr_id = be32_to_cpu(attr->attr_id);
  240. /* OSD2: For convenience, on empty attributes, we return 8 bytes
  241. * of zeros here. This keeps the same behaviour with OSD2r04,
  242. * and is nice with null terminating ASCII fields.
  243. * oa->val_ptr == NULL marks the end-of-list, or error.
  244. */
  245. oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
  246. }
  247. return inc;
  248. }
  249. static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
  250. {
  251. return osd_req_is_ver1(or) ?
  252. osdv1_list_size(list_head) :
  253. osdv2_list_size(list_head);
  254. }
  255. static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
  256. {
  257. return osd_req_is_ver1(or) ?
  258. sizeof(struct osdv1_attributes_list_header) :
  259. sizeof(struct osdv2_attributes_list_header);
  260. }
  261. static void _osd_req_set_alist_type(struct osd_request *or,
  262. void *list, int list_type)
  263. {
  264. if (osd_req_is_ver1(or)) {
  265. struct osdv1_attributes_list_header *attr_list = list;
  266. memset(attr_list, 0, sizeof(*attr_list));
  267. attr_list->type = list_type;
  268. } else {
  269. struct osdv2_attributes_list_header *attr_list = list;
  270. memset(attr_list, 0, sizeof(*attr_list));
  271. attr_list->type = list_type;
  272. }
  273. }
  274. static bool _osd_req_is_alist_type(struct osd_request *or,
  275. void *list, int list_type)
  276. {
  277. if (!list)
  278. return false;
  279. if (osd_req_is_ver1(or)) {
  280. struct osdv1_attributes_list_header *attr_list = list;
  281. return attr_list->type == list_type;
  282. } else {
  283. struct osdv2_attributes_list_header *attr_list = list;
  284. return attr_list->type == list_type;
  285. }
  286. }
  287. /* This is for List-objects not Attributes-Lists */
  288. static void _osd_req_encode_olist(struct osd_request *or,
  289. struct osd_obj_id_list *list)
  290. {
  291. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  292. if (osd_req_is_ver1(or)) {
  293. cdbh->v1.list_identifier = list->list_identifier;
  294. cdbh->v1.start_address = list->continuation_id;
  295. } else {
  296. cdbh->v2.list_identifier = list->list_identifier;
  297. cdbh->v2.start_address = list->continuation_id;
  298. }
  299. }
  300. static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
  301. u64 offset, unsigned *padding)
  302. {
  303. return __osd_encode_offset(offset, padding,
  304. osd_req_is_ver1(or) ?
  305. OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
  306. OSD_OFFSET_MAX_SHIFT);
  307. }
  308. static struct osd_security_parameters *
  309. _osd_req_sec_params(struct osd_request *or)
  310. {
  311. struct osd_cdb *ocdb = &or->cdb;
  312. if (osd_req_is_ver1(or))
  313. return (struct osd_security_parameters *)&ocdb->v1.sec_params;
  314. else
  315. return (struct osd_security_parameters *)&ocdb->v2.sec_params;
  316. }
  317. void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
  318. {
  319. memset(osdd, 0, sizeof(*osdd));
  320. osdd->scsi_device = scsi_device;
  321. osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
  322. #ifdef OSD_VER1_SUPPORT
  323. osdd->version = OSD_VER2;
  324. #endif
  325. /* TODO: Allocate pools for osd_request attributes ... */
  326. }
  327. EXPORT_SYMBOL(osd_dev_init);
  328. void osd_dev_fini(struct osd_dev *osdd)
  329. {
  330. /* TODO: De-allocate pools */
  331. osdd->scsi_device = NULL;
  332. }
  333. EXPORT_SYMBOL(osd_dev_fini);
  334. static struct osd_request *_osd_request_alloc(gfp_t gfp)
  335. {
  336. struct osd_request *or;
  337. /* TODO: Use mempool with one saved request */
  338. or = kzalloc(sizeof(*or), gfp);
  339. return or;
  340. }
  341. static void _osd_request_free(struct osd_request *or)
  342. {
  343. kfree(or);
  344. }
  345. struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
  346. {
  347. struct osd_request *or;
  348. or = _osd_request_alloc(gfp);
  349. if (!or)
  350. return NULL;
  351. or->osd_dev = dev;
  352. or->alloc_flags = gfp;
  353. or->timeout = dev->def_timeout;
  354. or->retries = OSD_REQ_RETRIES;
  355. return or;
  356. }
  357. EXPORT_SYMBOL(osd_start_request);
  358. static void _osd_free_seg(struct osd_request *or __unused,
  359. struct _osd_req_data_segment *seg)
  360. {
  361. if (!seg->buff || !seg->alloc_size)
  362. return;
  363. kfree(seg->buff);
  364. seg->buff = NULL;
  365. seg->alloc_size = 0;
  366. }
  367. static void _put_request(struct request *rq)
  368. {
  369. /*
  370. * If osd_finalize_request() was called but the request was not
  371. * executed through the block layer, then we must release BIOs.
  372. * TODO: Keep error code in or->async_error. Need to audit all
  373. * code paths.
  374. */
  375. if (unlikely(rq->bio))
  376. blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
  377. else
  378. blk_put_request(rq);
  379. }
  380. void osd_end_request(struct osd_request *or)
  381. {
  382. struct request *rq = or->request;
  383. if (rq) {
  384. if (rq->next_rq) {
  385. _put_request(rq->next_rq);
  386. rq->next_rq = NULL;
  387. }
  388. _put_request(rq);
  389. }
  390. _osd_free_seg(or, &or->get_attr);
  391. _osd_free_seg(or, &or->enc_get_attr);
  392. _osd_free_seg(or, &or->set_attr);
  393. _osd_free_seg(or, &or->cdb_cont);
  394. _osd_request_free(or);
  395. }
  396. EXPORT_SYMBOL(osd_end_request);
  397. static void _set_error_resid(struct osd_request *or, struct request *req,
  398. int error)
  399. {
  400. or->async_error = error;
  401. or->req_errors = req->errors ? : error;
  402. or->sense_len = req->sense_len;
  403. if (or->out.req)
  404. or->out.residual = or->out.req->resid_len;
  405. if (or->in.req)
  406. or->in.residual = or->in.req->resid_len;
  407. }
  408. int osd_execute_request(struct osd_request *or)
  409. {
  410. int error = blk_execute_rq(or->request->q, NULL, or->request, 0);
  411. _set_error_resid(or, or->request, error);
  412. return error;
  413. }
  414. EXPORT_SYMBOL(osd_execute_request);
  415. static void osd_request_async_done(struct request *req, int error)
  416. {
  417. struct osd_request *or = req->end_io_data;
  418. _set_error_resid(or, req, error);
  419. if (req->next_rq) {
  420. __blk_put_request(req->q, req->next_rq);
  421. req->next_rq = NULL;
  422. }
  423. __blk_put_request(req->q, req);
  424. or->request = NULL;
  425. or->in.req = NULL;
  426. or->out.req = NULL;
  427. if (or->async_done)
  428. or->async_done(or, or->async_private);
  429. else
  430. osd_end_request(or);
  431. }
  432. int osd_execute_request_async(struct osd_request *or,
  433. osd_req_done_fn *done, void *private)
  434. {
  435. or->request->end_io_data = or;
  436. or->async_private = private;
  437. or->async_done = done;
  438. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  439. osd_request_async_done);
  440. return 0;
  441. }
  442. EXPORT_SYMBOL(osd_execute_request_async);
  443. u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  444. u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  445. static int _osd_realloc_seg(struct osd_request *or,
  446. struct _osd_req_data_segment *seg, unsigned max_bytes)
  447. {
  448. void *buff;
  449. if (seg->alloc_size >= max_bytes)
  450. return 0;
  451. buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
  452. if (!buff) {
  453. OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
  454. seg->alloc_size);
  455. return -ENOMEM;
  456. }
  457. memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
  458. seg->buff = buff;
  459. seg->alloc_size = max_bytes;
  460. return 0;
  461. }
  462. static int _alloc_cdb_cont(struct osd_request *or, unsigned total_bytes)
  463. {
  464. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  465. return _osd_realloc_seg(or, &or->cdb_cont, total_bytes);
  466. }
  467. static int _alloc_set_attr_list(struct osd_request *or,
  468. const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
  469. {
  470. unsigned total_bytes = add_bytes;
  471. for (; nelem; --nelem, ++oa)
  472. total_bytes += _osd_req_alist_elem_size(or, oa->len);
  473. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  474. return _osd_realloc_seg(or, &or->set_attr, total_bytes);
  475. }
  476. static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
  477. {
  478. OSD_DEBUG("total_bytes=%d\n", max_bytes);
  479. return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
  480. }
  481. static int _alloc_get_attr_list(struct osd_request *or)
  482. {
  483. OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
  484. return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
  485. }
  486. /*
  487. * Common to all OSD commands
  488. */
  489. static void _osdv1_req_encode_common(struct osd_request *or,
  490. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  491. {
  492. struct osdv1_cdb *ocdb = &or->cdb.v1;
  493. /*
  494. * For speed, the commands
  495. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  496. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  497. * are not supported here. Should pass zero and set after the call
  498. */
  499. act &= cpu_to_be16(~0x0080); /* V1 action code */
  500. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  501. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  502. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  503. ocdb->h.varlen_cdb.service_action = act;
  504. ocdb->h.partition = cpu_to_be64(obj->partition);
  505. ocdb->h.object = cpu_to_be64(obj->id);
  506. ocdb->h.v1.length = cpu_to_be64(len);
  507. ocdb->h.v1.start_address = cpu_to_be64(offset);
  508. }
  509. static void _osdv2_req_encode_common(struct osd_request *or,
  510. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  511. {
  512. struct osdv2_cdb *ocdb = &or->cdb.v2;
  513. OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
  514. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  515. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  516. ocdb->h.varlen_cdb.service_action = act;
  517. ocdb->h.partition = cpu_to_be64(obj->partition);
  518. ocdb->h.object = cpu_to_be64(obj->id);
  519. ocdb->h.v2.length = cpu_to_be64(len);
  520. ocdb->h.v2.start_address = cpu_to_be64(offset);
  521. }
  522. static void _osd_req_encode_common(struct osd_request *or,
  523. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  524. {
  525. if (osd_req_is_ver1(or))
  526. _osdv1_req_encode_common(or, act, obj, offset, len);
  527. else
  528. _osdv2_req_encode_common(or, act, obj, offset, len);
  529. }
  530. /*
  531. * Device commands
  532. */
  533. /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
  534. /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
  535. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  536. {
  537. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  538. tot_capacity);
  539. }
  540. EXPORT_SYMBOL(osd_req_format);
  541. int osd_req_list_dev_partitions(struct osd_request *or,
  542. osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
  543. {
  544. return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
  545. }
  546. EXPORT_SYMBOL(osd_req_list_dev_partitions);
  547. static void _osd_req_encode_flush(struct osd_request *or,
  548. enum osd_options_flush_scope_values op)
  549. {
  550. struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
  551. ocdb->command_specific_options = op;
  552. }
  553. void osd_req_flush_obsd(struct osd_request *or,
  554. enum osd_options_flush_scope_values op)
  555. {
  556. _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
  557. _osd_req_encode_flush(or, op);
  558. }
  559. EXPORT_SYMBOL(osd_req_flush_obsd);
  560. /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
  561. const u8 *cdb, ...); */
  562. /*TODO: void osd_req_task_management(struct osd_request *, ...); */
  563. /*
  564. * Partition commands
  565. */
  566. static void _osd_req_encode_partition(struct osd_request *or,
  567. __be16 act, osd_id partition)
  568. {
  569. struct osd_obj_id par = {
  570. .partition = partition,
  571. .id = 0,
  572. };
  573. _osd_req_encode_common(or, act, &par, 0, 0);
  574. }
  575. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  576. {
  577. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  578. }
  579. EXPORT_SYMBOL(osd_req_create_partition);
  580. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  581. {
  582. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  583. }
  584. EXPORT_SYMBOL(osd_req_remove_partition);
  585. /*TODO: void osd_req_set_partition_key(struct osd_request *,
  586. osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
  587. u8 seed[OSD_CRYPTO_SEED_SIZE]); */
  588. static int _osd_req_list_objects(struct osd_request *or,
  589. __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
  590. struct osd_obj_id_list *list, unsigned nelem)
  591. {
  592. struct request_queue *q = osd_request_queue(or->osd_dev);
  593. u64 len = nelem * sizeof(osd_id) + sizeof(*list);
  594. struct bio *bio;
  595. _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
  596. if (list->list_identifier)
  597. _osd_req_encode_olist(or, list);
  598. WARN_ON(or->in.bio);
  599. bio = bio_map_kern(q, list, len, or->alloc_flags);
  600. if (IS_ERR(bio)) {
  601. OSD_ERR("!!! Failed to allocate list_objects BIO\n");
  602. return PTR_ERR(bio);
  603. }
  604. bio->bi_rw &= ~REQ_WRITE;
  605. or->in.bio = bio;
  606. or->in.total_bytes = bio->bi_iter.bi_size;
  607. return 0;
  608. }
  609. int osd_req_list_partition_collections(struct osd_request *or,
  610. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  611. unsigned nelem)
  612. {
  613. struct osd_obj_id par = {
  614. .partition = partition,
  615. .id = 0,
  616. };
  617. return osd_req_list_collection_objects(or, &par, initial_id, list,
  618. nelem);
  619. }
  620. EXPORT_SYMBOL(osd_req_list_partition_collections);
  621. int osd_req_list_partition_objects(struct osd_request *or,
  622. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  623. unsigned nelem)
  624. {
  625. struct osd_obj_id par = {
  626. .partition = partition,
  627. .id = 0,
  628. };
  629. return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
  630. nelem);
  631. }
  632. EXPORT_SYMBOL(osd_req_list_partition_objects);
  633. void osd_req_flush_partition(struct osd_request *or,
  634. osd_id partition, enum osd_options_flush_scope_values op)
  635. {
  636. _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
  637. _osd_req_encode_flush(or, op);
  638. }
  639. EXPORT_SYMBOL(osd_req_flush_partition);
  640. /*
  641. * Collection commands
  642. */
  643. /*TODO: void osd_req_create_collection(struct osd_request *,
  644. const struct osd_obj_id *); */
  645. /*TODO: void osd_req_remove_collection(struct osd_request *,
  646. const struct osd_obj_id *); */
  647. int osd_req_list_collection_objects(struct osd_request *or,
  648. const struct osd_obj_id *obj, osd_id initial_id,
  649. struct osd_obj_id_list *list, unsigned nelem)
  650. {
  651. return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
  652. initial_id, list, nelem);
  653. }
  654. EXPORT_SYMBOL(osd_req_list_collection_objects);
  655. /*TODO: void query(struct osd_request *, ...); V2 */
  656. void osd_req_flush_collection(struct osd_request *or,
  657. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
  658. {
  659. _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
  660. _osd_req_encode_flush(or, op);
  661. }
  662. EXPORT_SYMBOL(osd_req_flush_collection);
  663. /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
  664. /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
  665. /*
  666. * Object commands
  667. */
  668. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  669. {
  670. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  671. }
  672. EXPORT_SYMBOL(osd_req_create_object);
  673. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  674. {
  675. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  676. }
  677. EXPORT_SYMBOL(osd_req_remove_object);
  678. /*TODO: void osd_req_create_multi(struct osd_request *or,
  679. struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
  680. */
  681. void osd_req_write(struct osd_request *or,
  682. const struct osd_obj_id *obj, u64 offset,
  683. struct bio *bio, u64 len)
  684. {
  685. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
  686. WARN_ON(or->out.bio || or->out.total_bytes);
  687. WARN_ON(0 == (bio->bi_rw & REQ_WRITE));
  688. or->out.bio = bio;
  689. or->out.total_bytes = len;
  690. }
  691. EXPORT_SYMBOL(osd_req_write);
  692. int osd_req_write_kern(struct osd_request *or,
  693. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  694. {
  695. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  696. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  697. if (IS_ERR(bio))
  698. return PTR_ERR(bio);
  699. bio->bi_rw |= REQ_WRITE; /* FIXME: bio_set_dir() */
  700. osd_req_write(or, obj, offset, bio, len);
  701. return 0;
  702. }
  703. EXPORT_SYMBOL(osd_req_write_kern);
  704. /*TODO: void osd_req_append(struct osd_request *,
  705. const struct osd_obj_id *, struct bio *data_out); */
  706. /*TODO: void osd_req_create_write(struct osd_request *,
  707. const struct osd_obj_id *, struct bio *data_out, u64 offset); */
  708. /*TODO: void osd_req_clear(struct osd_request *,
  709. const struct osd_obj_id *, u64 offset, u64 len); */
  710. /*TODO: void osd_req_punch(struct osd_request *,
  711. const struct osd_obj_id *, u64 offset, u64 len); V2 */
  712. void osd_req_flush_object(struct osd_request *or,
  713. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
  714. /*V2*/ u64 offset, /*V2*/ u64 len)
  715. {
  716. if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
  717. OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
  718. offset = 0;
  719. len = 0;
  720. }
  721. _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
  722. _osd_req_encode_flush(or, op);
  723. }
  724. EXPORT_SYMBOL(osd_req_flush_object);
  725. void osd_req_read(struct osd_request *or,
  726. const struct osd_obj_id *obj, u64 offset,
  727. struct bio *bio, u64 len)
  728. {
  729. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
  730. WARN_ON(or->in.bio || or->in.total_bytes);
  731. WARN_ON(bio->bi_rw & REQ_WRITE);
  732. or->in.bio = bio;
  733. or->in.total_bytes = len;
  734. }
  735. EXPORT_SYMBOL(osd_req_read);
  736. int osd_req_read_kern(struct osd_request *or,
  737. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  738. {
  739. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  740. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  741. if (IS_ERR(bio))
  742. return PTR_ERR(bio);
  743. osd_req_read(or, obj, offset, bio, len);
  744. return 0;
  745. }
  746. EXPORT_SYMBOL(osd_req_read_kern);
  747. static int _add_sg_continuation_descriptor(struct osd_request *or,
  748. const struct osd_sg_entry *sglist, unsigned numentries, u64 *len)
  749. {
  750. struct osd_sg_continuation_descriptor *oscd;
  751. u32 oscd_size;
  752. unsigned i;
  753. int ret;
  754. oscd_size = sizeof(*oscd) + numentries * sizeof(oscd->entries[0]);
  755. if (!or->cdb_cont.total_bytes) {
  756. /* First time, jump over the header, we will write to:
  757. * cdb_cont.buff + cdb_cont.total_bytes
  758. */
  759. or->cdb_cont.total_bytes =
  760. sizeof(struct osd_continuation_segment_header);
  761. }
  762. ret = _alloc_cdb_cont(or, or->cdb_cont.total_bytes + oscd_size);
  763. if (unlikely(ret))
  764. return ret;
  765. oscd = or->cdb_cont.buff + or->cdb_cont.total_bytes;
  766. oscd->hdr.type = cpu_to_be16(SCATTER_GATHER_LIST);
  767. oscd->hdr.pad_length = 0;
  768. oscd->hdr.length = cpu_to_be32(oscd_size - sizeof(*oscd));
  769. *len = 0;
  770. /* copy the sg entries and convert to network byte order */
  771. for (i = 0; i < numentries; i++) {
  772. oscd->entries[i].offset = cpu_to_be64(sglist[i].offset);
  773. oscd->entries[i].len = cpu_to_be64(sglist[i].len);
  774. *len += sglist[i].len;
  775. }
  776. or->cdb_cont.total_bytes += oscd_size;
  777. OSD_DEBUG("total_bytes=%d oscd_size=%d numentries=%d\n",
  778. or->cdb_cont.total_bytes, oscd_size, numentries);
  779. return 0;
  780. }
  781. static int _osd_req_finalize_cdb_cont(struct osd_request *or, const u8 *cap_key)
  782. {
  783. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  784. struct bio *bio;
  785. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  786. struct osd_continuation_segment_header *cont_seg_hdr;
  787. if (!or->cdb_cont.total_bytes)
  788. return 0;
  789. cont_seg_hdr = or->cdb_cont.buff;
  790. cont_seg_hdr->format = CDB_CONTINUATION_FORMAT_V2;
  791. cont_seg_hdr->service_action = cdbh->varlen_cdb.service_action;
  792. /* create a bio for continuation segment */
  793. bio = bio_map_kern(req_q, or->cdb_cont.buff, or->cdb_cont.total_bytes,
  794. GFP_KERNEL);
  795. if (IS_ERR(bio))
  796. return PTR_ERR(bio);
  797. bio->bi_rw |= REQ_WRITE;
  798. /* integrity check the continuation before the bio is linked
  799. * with the other data segments since the continuation
  800. * integrity is separate from the other data segments.
  801. */
  802. osd_sec_sign_data(cont_seg_hdr->integrity_check, bio, cap_key);
  803. cdbh->v2.cdb_continuation_length = cpu_to_be32(or->cdb_cont.total_bytes);
  804. /* we can't use _req_append_segment, because we need to link in the
  805. * continuation bio to the head of the bio list - the
  806. * continuation segment (if it exists) is always the first segment in
  807. * the out data buffer.
  808. */
  809. bio->bi_next = or->out.bio;
  810. or->out.bio = bio;
  811. or->out.total_bytes += or->cdb_cont.total_bytes;
  812. return 0;
  813. }
  814. /* osd_req_write_sg: Takes a @bio that points to the data out buffer and an
  815. * @sglist that has the scatter gather entries. Scatter-gather enables a write
  816. * of multiple none-contiguous areas of an object, in a single call. The extents
  817. * may overlap and/or be in any order. The only constrain is that:
  818. * total_bytes(sglist) >= total_bytes(bio)
  819. */
  820. int osd_req_write_sg(struct osd_request *or,
  821. const struct osd_obj_id *obj, struct bio *bio,
  822. const struct osd_sg_entry *sglist, unsigned numentries)
  823. {
  824. u64 len;
  825. int ret = _add_sg_continuation_descriptor(or, sglist, numentries, &len);
  826. if (ret)
  827. return ret;
  828. osd_req_write(or, obj, 0, bio, len);
  829. return 0;
  830. }
  831. EXPORT_SYMBOL(osd_req_write_sg);
  832. /* osd_req_read_sg: Read multiple extents of an object into @bio
  833. * See osd_req_write_sg
  834. */
  835. int osd_req_read_sg(struct osd_request *or,
  836. const struct osd_obj_id *obj, struct bio *bio,
  837. const struct osd_sg_entry *sglist, unsigned numentries)
  838. {
  839. u64 len;
  840. u64 off;
  841. int ret;
  842. if (numentries > 1) {
  843. off = 0;
  844. ret = _add_sg_continuation_descriptor(or, sglist, numentries,
  845. &len);
  846. if (ret)
  847. return ret;
  848. } else {
  849. /* Optimize the case of single segment, read_sg is a
  850. * bidi operation.
  851. */
  852. len = sglist->len;
  853. off = sglist->offset;
  854. }
  855. osd_req_read(or, obj, off, bio, len);
  856. return 0;
  857. }
  858. EXPORT_SYMBOL(osd_req_read_sg);
  859. /* SG-list write/read Kern API
  860. *
  861. * osd_req_{write,read}_sg_kern takes an array of @buff pointers and an array
  862. * of sg_entries. @numentries indicates how many pointers and sg_entries there
  863. * are. By requiring an array of buff pointers. This allows a caller to do a
  864. * single write/read and scatter into multiple buffers.
  865. * NOTE: Each buffer + len should not cross a page boundary.
  866. */
  867. static struct bio *_create_sg_bios(struct osd_request *or,
  868. void **buff, const struct osd_sg_entry *sglist, unsigned numentries)
  869. {
  870. struct request_queue *q = osd_request_queue(or->osd_dev);
  871. struct bio *bio;
  872. unsigned i;
  873. bio = bio_kmalloc(GFP_KERNEL, numentries);
  874. if (unlikely(!bio)) {
  875. OSD_DEBUG("Failed to allocate BIO size=%u\n", numentries);
  876. return ERR_PTR(-ENOMEM);
  877. }
  878. for (i = 0; i < numentries; i++) {
  879. unsigned offset = offset_in_page(buff[i]);
  880. struct page *page = virt_to_page(buff[i]);
  881. unsigned len = sglist[i].len;
  882. unsigned added_len;
  883. BUG_ON(offset + len > PAGE_SIZE);
  884. added_len = bio_add_pc_page(q, bio, page, len, offset);
  885. if (unlikely(len != added_len)) {
  886. OSD_DEBUG("bio_add_pc_page len(%d) != added_len(%d)\n",
  887. len, added_len);
  888. bio_put(bio);
  889. return ERR_PTR(-ENOMEM);
  890. }
  891. }
  892. return bio;
  893. }
  894. int osd_req_write_sg_kern(struct osd_request *or,
  895. const struct osd_obj_id *obj, void **buff,
  896. const struct osd_sg_entry *sglist, unsigned numentries)
  897. {
  898. struct bio *bio = _create_sg_bios(or, buff, sglist, numentries);
  899. if (IS_ERR(bio))
  900. return PTR_ERR(bio);
  901. bio->bi_rw |= REQ_WRITE;
  902. osd_req_write_sg(or, obj, bio, sglist, numentries);
  903. return 0;
  904. }
  905. EXPORT_SYMBOL(osd_req_write_sg_kern);
  906. int osd_req_read_sg_kern(struct osd_request *or,
  907. const struct osd_obj_id *obj, void **buff,
  908. const struct osd_sg_entry *sglist, unsigned numentries)
  909. {
  910. struct bio *bio = _create_sg_bios(or, buff, sglist, numentries);
  911. if (IS_ERR(bio))
  912. return PTR_ERR(bio);
  913. osd_req_read_sg(or, obj, bio, sglist, numentries);
  914. return 0;
  915. }
  916. EXPORT_SYMBOL(osd_req_read_sg_kern);
  917. void osd_req_get_attributes(struct osd_request *or,
  918. const struct osd_obj_id *obj)
  919. {
  920. _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
  921. }
  922. EXPORT_SYMBOL(osd_req_get_attributes);
  923. void osd_req_set_attributes(struct osd_request *or,
  924. const struct osd_obj_id *obj)
  925. {
  926. _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
  927. }
  928. EXPORT_SYMBOL(osd_req_set_attributes);
  929. /*
  930. * Attributes List-mode
  931. */
  932. int osd_req_add_set_attr_list(struct osd_request *or,
  933. const struct osd_attr *oa, unsigned nelem)
  934. {
  935. unsigned total_bytes = or->set_attr.total_bytes;
  936. void *attr_last;
  937. int ret;
  938. if (or->attributes_mode &&
  939. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  940. WARN_ON(1);
  941. return -EINVAL;
  942. }
  943. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  944. if (!total_bytes) { /* first-time: allocate and put list header */
  945. total_bytes = _osd_req_sizeof_alist_header(or);
  946. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  947. if (ret)
  948. return ret;
  949. _osd_req_set_alist_type(or, or->set_attr.buff,
  950. OSD_ATTR_LIST_SET_RETRIEVE);
  951. }
  952. attr_last = or->set_attr.buff + total_bytes;
  953. for (; nelem; --nelem) {
  954. unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
  955. total_bytes += elem_size;
  956. if (unlikely(or->set_attr.alloc_size < total_bytes)) {
  957. or->set_attr.total_bytes = total_bytes - elem_size;
  958. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  959. if (ret)
  960. return ret;
  961. attr_last =
  962. or->set_attr.buff + or->set_attr.total_bytes;
  963. }
  964. _osd_req_alist_elem_encode(or, attr_last, oa);
  965. attr_last += elem_size;
  966. ++oa;
  967. }
  968. or->set_attr.total_bytes = total_bytes;
  969. return 0;
  970. }
  971. EXPORT_SYMBOL(osd_req_add_set_attr_list);
  972. static int _req_append_segment(struct osd_request *or,
  973. unsigned padding, struct _osd_req_data_segment *seg,
  974. struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
  975. {
  976. void *pad_buff;
  977. int ret;
  978. if (padding) {
  979. /* check if we can just add it to last buffer */
  980. if (last_seg &&
  981. (padding <= last_seg->alloc_size - last_seg->total_bytes))
  982. pad_buff = last_seg->buff + last_seg->total_bytes;
  983. else
  984. pad_buff = io->pad_buff;
  985. ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding,
  986. or->alloc_flags);
  987. if (ret)
  988. return ret;
  989. io->total_bytes += padding;
  990. }
  991. ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
  992. or->alloc_flags);
  993. if (ret)
  994. return ret;
  995. io->total_bytes += seg->total_bytes;
  996. OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
  997. seg->total_bytes);
  998. return 0;
  999. }
  1000. static int _osd_req_finalize_set_attr_list(struct osd_request *or)
  1001. {
  1002. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1003. unsigned padding;
  1004. int ret;
  1005. if (!or->set_attr.total_bytes) {
  1006. cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
  1007. return 0;
  1008. }
  1009. cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
  1010. cdbh->attrs_list.set_attr_offset =
  1011. osd_req_encode_offset(or, or->out.total_bytes, &padding);
  1012. ret = _req_append_segment(or, padding, &or->set_attr,
  1013. or->out.last_seg, &or->out);
  1014. if (ret)
  1015. return ret;
  1016. or->out.last_seg = &or->set_attr;
  1017. return 0;
  1018. }
  1019. int osd_req_add_get_attr_list(struct osd_request *or,
  1020. const struct osd_attr *oa, unsigned nelem)
  1021. {
  1022. unsigned total_bytes = or->enc_get_attr.total_bytes;
  1023. void *attr_last;
  1024. int ret;
  1025. if (or->attributes_mode &&
  1026. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  1027. WARN_ON(1);
  1028. return -EINVAL;
  1029. }
  1030. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  1031. /* first time calc data-in list header size */
  1032. if (!or->get_attr.total_bytes)
  1033. or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
  1034. /* calc data-out info */
  1035. if (!total_bytes) { /* first-time: allocate and put list header */
  1036. unsigned max_bytes;
  1037. total_bytes = _osd_req_sizeof_alist_header(or);
  1038. max_bytes = total_bytes +
  1039. nelem * sizeof(struct osd_attributes_list_attrid);
  1040. ret = _alloc_get_attr_desc(or, max_bytes);
  1041. if (ret)
  1042. return ret;
  1043. _osd_req_set_alist_type(or, or->enc_get_attr.buff,
  1044. OSD_ATTR_LIST_GET);
  1045. }
  1046. attr_last = or->enc_get_attr.buff + total_bytes;
  1047. for (; nelem; --nelem) {
  1048. struct osd_attributes_list_attrid *attrid;
  1049. const unsigned cur_size = sizeof(*attrid);
  1050. total_bytes += cur_size;
  1051. if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
  1052. or->enc_get_attr.total_bytes = total_bytes - cur_size;
  1053. ret = _alloc_get_attr_desc(or,
  1054. total_bytes + nelem * sizeof(*attrid));
  1055. if (ret)
  1056. return ret;
  1057. attr_last = or->enc_get_attr.buff +
  1058. or->enc_get_attr.total_bytes;
  1059. }
  1060. attrid = attr_last;
  1061. attrid->attr_page = cpu_to_be32(oa->attr_page);
  1062. attrid->attr_id = cpu_to_be32(oa->attr_id);
  1063. attr_last += cur_size;
  1064. /* calc data-in size */
  1065. or->get_attr.total_bytes +=
  1066. _osd_req_alist_elem_size(or, oa->len);
  1067. ++oa;
  1068. }
  1069. or->enc_get_attr.total_bytes = total_bytes;
  1070. OSD_DEBUG(
  1071. "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
  1072. or->get_attr.total_bytes,
  1073. or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
  1074. or->enc_get_attr.total_bytes,
  1075. (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
  1076. / sizeof(struct osd_attributes_list_attrid));
  1077. return 0;
  1078. }
  1079. EXPORT_SYMBOL(osd_req_add_get_attr_list);
  1080. static int _osd_req_finalize_get_attr_list(struct osd_request *or)
  1081. {
  1082. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1083. unsigned out_padding;
  1084. unsigned in_padding;
  1085. int ret;
  1086. if (!or->enc_get_attr.total_bytes) {
  1087. cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
  1088. cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
  1089. return 0;
  1090. }
  1091. ret = _alloc_get_attr_list(or);
  1092. if (ret)
  1093. return ret;
  1094. /* The out-going buffer info update */
  1095. OSD_DEBUG("out-going\n");
  1096. cdbh->attrs_list.get_attr_desc_bytes =
  1097. cpu_to_be32(or->enc_get_attr.total_bytes);
  1098. cdbh->attrs_list.get_attr_desc_offset =
  1099. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  1100. ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
  1101. or->out.last_seg, &or->out);
  1102. if (ret)
  1103. return ret;
  1104. or->out.last_seg = &or->enc_get_attr;
  1105. /* The incoming buffer info update */
  1106. OSD_DEBUG("in-coming\n");
  1107. cdbh->attrs_list.get_attr_alloc_length =
  1108. cpu_to_be32(or->get_attr.total_bytes);
  1109. cdbh->attrs_list.get_attr_offset =
  1110. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  1111. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  1112. &or->in);
  1113. if (ret)
  1114. return ret;
  1115. or->in.last_seg = &or->get_attr;
  1116. return 0;
  1117. }
  1118. int osd_req_decode_get_attr_list(struct osd_request *or,
  1119. struct osd_attr *oa, int *nelem, void **iterator)
  1120. {
  1121. unsigned cur_bytes, returned_bytes;
  1122. int n;
  1123. const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
  1124. void *cur_p;
  1125. if (!_osd_req_is_alist_type(or, or->get_attr.buff,
  1126. OSD_ATTR_LIST_SET_RETRIEVE)) {
  1127. oa->attr_page = 0;
  1128. oa->attr_id = 0;
  1129. oa->val_ptr = NULL;
  1130. oa->len = 0;
  1131. *iterator = NULL;
  1132. return 0;
  1133. }
  1134. if (*iterator) {
  1135. BUG_ON((*iterator < or->get_attr.buff) ||
  1136. (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
  1137. cur_p = *iterator;
  1138. cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
  1139. returned_bytes = or->get_attr.total_bytes;
  1140. } else { /* first time decode the list header */
  1141. cur_bytes = sizeof_attr_list;
  1142. returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
  1143. sizeof_attr_list;
  1144. cur_p = or->get_attr.buff + sizeof_attr_list;
  1145. if (returned_bytes > or->get_attr.alloc_size) {
  1146. OSD_DEBUG("target report: space was not big enough! "
  1147. "Allocate=%u Needed=%u\n",
  1148. or->get_attr.alloc_size,
  1149. returned_bytes + sizeof_attr_list);
  1150. returned_bytes =
  1151. or->get_attr.alloc_size - sizeof_attr_list;
  1152. }
  1153. or->get_attr.total_bytes = returned_bytes;
  1154. }
  1155. for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
  1156. int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
  1157. returned_bytes - cur_bytes);
  1158. if (inc < 0) {
  1159. OSD_ERR("BAD FOOD from target. list not valid!"
  1160. "c=%d r=%d n=%d\n",
  1161. cur_bytes, returned_bytes, n);
  1162. oa->val_ptr = NULL;
  1163. cur_bytes = returned_bytes; /* break the caller loop */
  1164. break;
  1165. }
  1166. cur_bytes += inc;
  1167. cur_p += inc;
  1168. ++oa;
  1169. }
  1170. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  1171. *nelem = n;
  1172. return returned_bytes - cur_bytes;
  1173. }
  1174. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  1175. /*
  1176. * Attributes Page-mode
  1177. */
  1178. int osd_req_add_get_attr_page(struct osd_request *or,
  1179. u32 page_id, void *attar_page, unsigned max_page_len,
  1180. const struct osd_attr *set_one_attr)
  1181. {
  1182. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1183. if (or->attributes_mode &&
  1184. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1185. WARN_ON(1);
  1186. return -EINVAL;
  1187. }
  1188. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  1189. or->get_attr.buff = attar_page;
  1190. or->get_attr.total_bytes = max_page_len;
  1191. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  1192. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  1193. if (!set_one_attr || !set_one_attr->attr_page)
  1194. return 0; /* The set is optional */
  1195. or->set_attr.buff = set_one_attr->val_ptr;
  1196. or->set_attr.total_bytes = set_one_attr->len;
  1197. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  1198. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  1199. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  1200. return 0;
  1201. }
  1202. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  1203. static int _osd_req_finalize_attr_page(struct osd_request *or)
  1204. {
  1205. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1206. unsigned in_padding, out_padding;
  1207. int ret;
  1208. /* returned page */
  1209. cdbh->attrs_page.get_attr_offset =
  1210. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  1211. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  1212. &or->in);
  1213. if (ret)
  1214. return ret;
  1215. if (or->set_attr.total_bytes == 0)
  1216. return 0;
  1217. /* set one value */
  1218. cdbh->attrs_page.set_attr_offset =
  1219. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  1220. ret = _req_append_segment(or, out_padding, &or->set_attr, NULL,
  1221. &or->out);
  1222. return ret;
  1223. }
  1224. static inline void osd_sec_parms_set_out_offset(bool is_v1,
  1225. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1226. {
  1227. if (is_v1)
  1228. sec_parms->v1.data_out_integrity_check_offset = offset;
  1229. else
  1230. sec_parms->v2.data_out_integrity_check_offset = offset;
  1231. }
  1232. static inline void osd_sec_parms_set_in_offset(bool is_v1,
  1233. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1234. {
  1235. if (is_v1)
  1236. sec_parms->v1.data_in_integrity_check_offset = offset;
  1237. else
  1238. sec_parms->v2.data_in_integrity_check_offset = offset;
  1239. }
  1240. static int _osd_req_finalize_data_integrity(struct osd_request *or,
  1241. bool has_in, bool has_out, struct bio *out_data_bio, u64 out_data_bytes,
  1242. const u8 *cap_key)
  1243. {
  1244. struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
  1245. int ret;
  1246. if (!osd_is_sec_alldata(sec_parms))
  1247. return 0;
  1248. if (has_out) {
  1249. struct _osd_req_data_segment seg = {
  1250. .buff = &or->out_data_integ,
  1251. .total_bytes = sizeof(or->out_data_integ),
  1252. };
  1253. unsigned pad;
  1254. or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
  1255. or->out_data_integ.set_attributes_bytes = cpu_to_be64(
  1256. or->set_attr.total_bytes);
  1257. or->out_data_integ.get_attributes_bytes = cpu_to_be64(
  1258. or->enc_get_attr.total_bytes);
  1259. osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
  1260. osd_req_encode_offset(or, or->out.total_bytes, &pad));
  1261. ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
  1262. &or->out);
  1263. if (ret)
  1264. return ret;
  1265. or->out.last_seg = NULL;
  1266. /* they are now all chained to request sign them all together */
  1267. osd_sec_sign_data(&or->out_data_integ, out_data_bio,
  1268. cap_key);
  1269. }
  1270. if (has_in) {
  1271. struct _osd_req_data_segment seg = {
  1272. .buff = &or->in_data_integ,
  1273. .total_bytes = sizeof(or->in_data_integ),
  1274. };
  1275. unsigned pad;
  1276. osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
  1277. osd_req_encode_offset(or, or->in.total_bytes, &pad));
  1278. ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
  1279. &or->in);
  1280. if (ret)
  1281. return ret;
  1282. or->in.last_seg = NULL;
  1283. }
  1284. return 0;
  1285. }
  1286. /*
  1287. * osd_finalize_request and helpers
  1288. */
  1289. static struct request *_make_request(struct request_queue *q, bool has_write,
  1290. struct _osd_io_info *oii, gfp_t flags)
  1291. {
  1292. if (oii->bio)
  1293. return blk_make_request(q, oii->bio, flags);
  1294. else {
  1295. struct request *req;
  1296. req = blk_get_request(q, has_write ? WRITE : READ, flags);
  1297. if (IS_ERR(req))
  1298. return req;
  1299. blk_rq_set_block_pc(req);
  1300. return req;
  1301. }
  1302. }
  1303. static int _init_blk_request(struct osd_request *or,
  1304. bool has_in, bool has_out)
  1305. {
  1306. gfp_t flags = or->alloc_flags;
  1307. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  1308. struct request_queue *q = scsi_device->request_queue;
  1309. struct request *req;
  1310. int ret;
  1311. req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
  1312. if (IS_ERR(req)) {
  1313. ret = PTR_ERR(req);
  1314. goto out;
  1315. }
  1316. or->request = req;
  1317. req->cmd_flags |= REQ_QUIET;
  1318. req->timeout = or->timeout;
  1319. req->retries = or->retries;
  1320. req->sense = or->sense;
  1321. req->sense_len = 0;
  1322. if (has_out) {
  1323. or->out.req = req;
  1324. if (has_in) {
  1325. /* allocate bidi request */
  1326. req = _make_request(q, false, &or->in, flags);
  1327. if (IS_ERR(req)) {
  1328. OSD_DEBUG("blk_get_request for bidi failed\n");
  1329. ret = PTR_ERR(req);
  1330. goto out;
  1331. }
  1332. blk_rq_set_block_pc(req);
  1333. or->in.req = or->request->next_rq = req;
  1334. }
  1335. } else if (has_in)
  1336. or->in.req = req;
  1337. ret = 0;
  1338. out:
  1339. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  1340. or, has_in, has_out, ret, or->request);
  1341. return ret;
  1342. }
  1343. int osd_finalize_request(struct osd_request *or,
  1344. u8 options, const void *cap, const u8 *cap_key)
  1345. {
  1346. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1347. bool has_in, has_out;
  1348. /* Save for data_integrity without the cdb_continuation */
  1349. struct bio *out_data_bio = or->out.bio;
  1350. u64 out_data_bytes = or->out.total_bytes;
  1351. int ret;
  1352. if (options & OSD_REQ_FUA)
  1353. cdbh->options |= OSD_CDB_FUA;
  1354. if (options & OSD_REQ_DPO)
  1355. cdbh->options |= OSD_CDB_DPO;
  1356. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  1357. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  1358. osd_set_caps(&or->cdb, cap);
  1359. has_in = or->in.bio || or->get_attr.total_bytes;
  1360. has_out = or->out.bio || or->cdb_cont.total_bytes ||
  1361. or->set_attr.total_bytes || or->enc_get_attr.total_bytes;
  1362. ret = _osd_req_finalize_cdb_cont(or, cap_key);
  1363. if (ret) {
  1364. OSD_DEBUG("_osd_req_finalize_cdb_cont failed\n");
  1365. return ret;
  1366. }
  1367. ret = _init_blk_request(or, has_in, has_out);
  1368. if (ret) {
  1369. OSD_DEBUG("_init_blk_request failed\n");
  1370. return ret;
  1371. }
  1372. or->out.pad_buff = sg_out_pad_buffer;
  1373. or->in.pad_buff = sg_in_pad_buffer;
  1374. if (!or->attributes_mode)
  1375. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  1376. cdbh->command_specific_options |= or->attributes_mode;
  1377. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1378. ret = _osd_req_finalize_attr_page(or);
  1379. if (ret) {
  1380. OSD_DEBUG("_osd_req_finalize_attr_page failed\n");
  1381. return ret;
  1382. }
  1383. } else {
  1384. /* TODO: I think that for the GET_ATTR command these 2 should
  1385. * be reversed to keep them in execution order (for embeded
  1386. * targets with low memory footprint)
  1387. */
  1388. ret = _osd_req_finalize_set_attr_list(or);
  1389. if (ret) {
  1390. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  1391. return ret;
  1392. }
  1393. ret = _osd_req_finalize_get_attr_list(or);
  1394. if (ret) {
  1395. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  1396. return ret;
  1397. }
  1398. }
  1399. ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
  1400. out_data_bio, out_data_bytes,
  1401. cap_key);
  1402. if (ret)
  1403. return ret;
  1404. osd_sec_sign_cdb(&or->cdb, cap_key);
  1405. or->request->cmd = or->cdb.buff;
  1406. or->request->cmd_len = _osd_req_cdb_len(or);
  1407. return 0;
  1408. }
  1409. EXPORT_SYMBOL(osd_finalize_request);
  1410. static bool _is_osd_security_code(int code)
  1411. {
  1412. return (code == osd_security_audit_value_frozen) ||
  1413. (code == osd_security_working_key_frozen) ||
  1414. (code == osd_nonce_not_unique) ||
  1415. (code == osd_nonce_timestamp_out_of_range) ||
  1416. (code == osd_invalid_dataout_buffer_integrity_check_value);
  1417. }
  1418. #define OSD_SENSE_PRINT1(fmt, a...) \
  1419. do { \
  1420. if (__cur_sense_need_output) \
  1421. OSD_ERR(fmt, ##a); \
  1422. } while (0)
  1423. #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
  1424. int osd_req_decode_sense_full(struct osd_request *or,
  1425. struct osd_sense_info *osi, bool silent,
  1426. struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
  1427. struct osd_attr *bad_attr_list, int max_attr)
  1428. {
  1429. int sense_len, original_sense_len;
  1430. struct osd_sense_info local_osi;
  1431. struct scsi_sense_descriptor_based *ssdb;
  1432. void *cur_descriptor;
  1433. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
  1434. const bool __cur_sense_need_output = false;
  1435. #else
  1436. bool __cur_sense_need_output = !silent;
  1437. #endif
  1438. int ret;
  1439. if (likely(!or->req_errors))
  1440. return 0;
  1441. osi = osi ? : &local_osi;
  1442. memset(osi, 0, sizeof(*osi));
  1443. ssdb = (typeof(ssdb))or->sense;
  1444. sense_len = or->sense_len;
  1445. if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
  1446. OSD_ERR("Block-layer returned error(0x%x) but "
  1447. "sense_len(%u) || key(%d) is empty\n",
  1448. or->req_errors, sense_len, ssdb->sense_key);
  1449. goto analyze;
  1450. }
  1451. if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
  1452. OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
  1453. ssdb->response_code, sense_len);
  1454. goto analyze;
  1455. }
  1456. osi->key = ssdb->sense_key;
  1457. osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
  1458. original_sense_len = ssdb->additional_sense_length + 8;
  1459. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
  1460. if (__cur_sense_need_output)
  1461. __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
  1462. #endif
  1463. OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
  1464. "additional_code=0x%x async_error=%d errors=0x%x\n",
  1465. osi->key, original_sense_len, sense_len,
  1466. osi->additional_code, or->async_error,
  1467. or->req_errors);
  1468. if (original_sense_len < sense_len)
  1469. sense_len = original_sense_len;
  1470. cur_descriptor = ssdb->ssd;
  1471. sense_len -= sizeof(*ssdb);
  1472. while (sense_len > 0) {
  1473. struct scsi_sense_descriptor *ssd = cur_descriptor;
  1474. int cur_len = ssd->additional_length + 2;
  1475. sense_len -= cur_len;
  1476. if (sense_len < 0)
  1477. break; /* sense was truncated */
  1478. switch (ssd->descriptor_type) {
  1479. case scsi_sense_information:
  1480. case scsi_sense_command_specific_information:
  1481. {
  1482. struct scsi_sense_command_specific_data_descriptor
  1483. *sscd = cur_descriptor;
  1484. osi->command_info =
  1485. get_unaligned_be64(&sscd->information) ;
  1486. OSD_SENSE_PRINT2(
  1487. "command_specific_information 0x%llx \n",
  1488. _LLU(osi->command_info));
  1489. break;
  1490. }
  1491. case scsi_sense_key_specific:
  1492. {
  1493. struct scsi_sense_key_specific_data_descriptor
  1494. *ssks = cur_descriptor;
  1495. osi->sense_info = get_unaligned_be16(&ssks->value);
  1496. OSD_SENSE_PRINT2(
  1497. "sense_key_specific_information %u"
  1498. "sksv_cd_bpv_bp (0x%x)\n",
  1499. osi->sense_info, ssks->sksv_cd_bpv_bp);
  1500. break;
  1501. }
  1502. case osd_sense_object_identification:
  1503. { /*FIXME: Keep first not last, Store in array*/
  1504. struct osd_sense_identification_data_descriptor
  1505. *osidd = cur_descriptor;
  1506. osi->not_initiated_command_functions =
  1507. le32_to_cpu(osidd->not_initiated_functions);
  1508. osi->completed_command_functions =
  1509. le32_to_cpu(osidd->completed_functions);
  1510. osi->obj.partition = be64_to_cpu(osidd->partition_id);
  1511. osi->obj.id = be64_to_cpu(osidd->object_id);
  1512. OSD_SENSE_PRINT2(
  1513. "object_identification pid=0x%llx oid=0x%llx\n",
  1514. _LLU(osi->obj.partition), _LLU(osi->obj.id));
  1515. OSD_SENSE_PRINT2(
  1516. "not_initiated_bits(%x) "
  1517. "completed_command_bits(%x)\n",
  1518. osi->not_initiated_command_functions,
  1519. osi->completed_command_functions);
  1520. break;
  1521. }
  1522. case osd_sense_response_integrity_check:
  1523. {
  1524. struct osd_sense_response_integrity_check_descriptor
  1525. *osricd = cur_descriptor;
  1526. const unsigned len =
  1527. sizeof(osricd->integrity_check_value);
  1528. char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
  1529. hex_dump_to_buffer(osricd->integrity_check_value, len,
  1530. 32, 1, key_dump, sizeof(key_dump), true);
  1531. OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
  1532. }
  1533. case osd_sense_attribute_identification:
  1534. {
  1535. struct osd_sense_attributes_data_descriptor
  1536. *osadd = cur_descriptor;
  1537. unsigned len = min(cur_len, sense_len);
  1538. struct osd_sense_attr *pattr = osadd->sense_attrs;
  1539. while (len >= sizeof(*pattr)) {
  1540. u32 attr_page = be32_to_cpu(pattr->attr_page);
  1541. u32 attr_id = be32_to_cpu(pattr->attr_id);
  1542. if (!osi->attr.attr_page) {
  1543. osi->attr.attr_page = attr_page;
  1544. osi->attr.attr_id = attr_id;
  1545. }
  1546. if (bad_attr_list && max_attr) {
  1547. bad_attr_list->attr_page = attr_page;
  1548. bad_attr_list->attr_id = attr_id;
  1549. bad_attr_list++;
  1550. max_attr--;
  1551. }
  1552. len -= sizeof(*pattr);
  1553. OSD_SENSE_PRINT2(
  1554. "osd_sense_attribute_identification"
  1555. "attr_page=0x%x attr_id=0x%x\n",
  1556. attr_page, attr_id);
  1557. }
  1558. }
  1559. /*These are not legal for OSD*/
  1560. case scsi_sense_field_replaceable_unit:
  1561. OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
  1562. break;
  1563. case scsi_sense_stream_commands:
  1564. OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
  1565. break;
  1566. case scsi_sense_block_commands:
  1567. OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
  1568. break;
  1569. case scsi_sense_ata_return:
  1570. OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
  1571. break;
  1572. default:
  1573. if (ssd->descriptor_type <= scsi_sense_Reserved_last)
  1574. OSD_SENSE_PRINT2(
  1575. "scsi_sense Reserved descriptor (0x%x)",
  1576. ssd->descriptor_type);
  1577. else
  1578. OSD_SENSE_PRINT2(
  1579. "scsi_sense Vendor descriptor (0x%x)",
  1580. ssd->descriptor_type);
  1581. }
  1582. cur_descriptor += cur_len;
  1583. }
  1584. analyze:
  1585. if (!osi->key) {
  1586. /* scsi sense is Empty, the request was never issued to target
  1587. * linux return code might tell us what happened.
  1588. */
  1589. if (or->async_error == -ENOMEM)
  1590. osi->osd_err_pri = OSD_ERR_PRI_RESOURCE;
  1591. else
  1592. osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE;
  1593. ret = or->async_error;
  1594. } else if (osi->key <= scsi_sk_recovered_error) {
  1595. osi->osd_err_pri = 0;
  1596. ret = 0;
  1597. } else if (osi->additional_code == scsi_invalid_field_in_cdb) {
  1598. if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) {
  1599. osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES;
  1600. ret = -EFAULT; /* caller should recover from this */
  1601. } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) {
  1602. osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND;
  1603. ret = -ENOENT;
  1604. } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) {
  1605. osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS;
  1606. ret = -EACCES;
  1607. } else {
  1608. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1609. ret = -EINVAL;
  1610. }
  1611. } else if (osi->additional_code == osd_quota_error) {
  1612. osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE;
  1613. ret = -ENOSPC;
  1614. } else if (_is_osd_security_code(osi->additional_code)) {
  1615. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1616. ret = -EINVAL;
  1617. } else {
  1618. osi->osd_err_pri = OSD_ERR_PRI_EIO;
  1619. ret = -EIO;
  1620. }
  1621. if (!or->out.residual)
  1622. or->out.residual = or->out.total_bytes;
  1623. if (!or->in.residual)
  1624. or->in.residual = or->in.total_bytes;
  1625. return ret;
  1626. }
  1627. EXPORT_SYMBOL(osd_req_decode_sense_full);
  1628. /*
  1629. * Implementation of osd_sec.h API
  1630. * TODO: Move to a separate osd_sec.c file at a later stage.
  1631. */
  1632. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  1633. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  1634. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  1635. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  1636. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  1637. };
  1638. enum { OSD_SEC_CAP_V2_ALL_CAPS =
  1639. OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
  1640. };
  1641. void osd_sec_init_nosec_doall_caps(void *caps,
  1642. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  1643. {
  1644. struct osd_capability *cap = caps;
  1645. u8 type;
  1646. u8 descriptor_type;
  1647. if (likely(obj->id)) {
  1648. if (unlikely(is_collection)) {
  1649. type = OSD_SEC_OBJ_COLLECTION;
  1650. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  1651. OSD_SEC_OBJ_DESC_COL;
  1652. } else {
  1653. type = OSD_SEC_OBJ_USER;
  1654. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  1655. }
  1656. WARN_ON(!obj->partition);
  1657. } else {
  1658. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  1659. OSD_SEC_OBJ_ROOT;
  1660. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  1661. }
  1662. memset(cap, 0, sizeof(*cap));
  1663. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  1664. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  1665. cap->h.security_method = OSD_SEC_NOSEC;
  1666. /* cap->expiration_time;
  1667. cap->AUDIT[30-10];
  1668. cap->discriminator[42-30];
  1669. cap->object_created_time; */
  1670. cap->h.object_type = type;
  1671. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  1672. cap->h.object_descriptor_type = descriptor_type;
  1673. cap->od.obj_desc.policy_access_tag = 0;
  1674. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  1675. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  1676. }
  1677. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  1678. /* FIXME: Extract version from caps pointer.
  1679. * Also Pete's target only supports caps from OSDv1 for now
  1680. */
  1681. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  1682. {
  1683. bool is_ver1 = true;
  1684. /* NOTE: They start at same address */
  1685. memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
  1686. }
  1687. bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
  1688. {
  1689. return false;
  1690. }
  1691. void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
  1692. {
  1693. }
  1694. void osd_sec_sign_data(void *data_integ __unused,
  1695. struct bio *bio __unused, const u8 *cap_key __unused)
  1696. {
  1697. }
  1698. /*
  1699. * Declared in osd_protocol.h
  1700. * 4.12.5 Data-In and Data-Out buffer offsets
  1701. * byte offset = mantissa * (2^(exponent+8))
  1702. * Returns the smallest allowed encoded offset that contains given @offset
  1703. * The actual encoded offset returned is @offset + *@padding.
  1704. */
  1705. osd_cdb_offset __osd_encode_offset(
  1706. u64 offset, unsigned *padding, int min_shift, int max_shift)
  1707. {
  1708. u64 try_offset = -1, mod, align;
  1709. osd_cdb_offset be32_offset;
  1710. int shift;
  1711. *padding = 0;
  1712. if (!offset)
  1713. return 0;
  1714. for (shift = min_shift; shift < max_shift; ++shift) {
  1715. try_offset = offset >> shift;
  1716. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  1717. break;
  1718. }
  1719. BUG_ON(shift == max_shift);
  1720. align = 1 << shift;
  1721. mod = offset & (align - 1);
  1722. if (mod) {
  1723. *padding = align - mod;
  1724. try_offset += 1;
  1725. }
  1726. try_offset |= ((shift - 8) & 0xf) << 28;
  1727. be32_offset = cpu_to_be32((u32)try_offset);
  1728. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  1729. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  1730. be32_offset, *padding);
  1731. return be32_offset;
  1732. }