osd_initiator.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  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 <bharrosh@panasas.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 <scsi/osd_initiator.h>
  42. #include <scsi/osd_sec.h>
  43. #include <scsi/osd_attributes.h>
  44. #include <scsi/osd_sense.h>
  45. #include <scsi/scsi_device.h>
  46. #include "osd_debug.h"
  47. #ifndef __unused
  48. # define __unused __attribute__((unused))
  49. #endif
  50. enum { OSD_REQ_RETRIES = 1 };
  51. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  52. MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
  53. MODULE_LICENSE("GPL");
  54. static inline void build_test(void)
  55. {
  56. /* structures were not packed */
  57. BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
  58. BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
  59. BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
  60. }
  61. static const char *_osd_ver_desc(struct osd_request *or)
  62. {
  63. return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
  64. }
  65. #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
  66. static int _osd_get_print_system_info(struct osd_dev *od,
  67. void *caps, struct osd_dev_info *odi)
  68. {
  69. struct osd_request *or;
  70. struct osd_attr get_attrs[] = {
  71. ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
  72. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
  73. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
  74. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
  75. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
  76. ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
  77. ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
  78. ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
  79. ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
  80. ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
  81. /* IBM-OSD-SIM Has a bug with this one put it last */
  82. ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
  83. };
  84. void *iter = NULL, *pFirst;
  85. int nelem = ARRAY_SIZE(get_attrs), a = 0;
  86. int ret;
  87. or = osd_start_request(od, GFP_KERNEL);
  88. if (!or)
  89. return -ENOMEM;
  90. /* get attrs */
  91. osd_req_get_attributes(or, &osd_root_object);
  92. osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
  93. ret = osd_finalize_request(or, 0, caps, NULL);
  94. if (ret)
  95. goto out;
  96. ret = osd_execute_request(or);
  97. if (ret) {
  98. OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
  99. goto out;
  100. }
  101. osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
  102. OSD_INFO("Detected %s device\n",
  103. _osd_ver_desc(or));
  104. pFirst = get_attrs[a++].val_ptr;
  105. OSD_INFO("VENDOR_IDENTIFICATION [%s]\n",
  106. (char *)pFirst);
  107. pFirst = get_attrs[a++].val_ptr;
  108. OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
  109. (char *)pFirst);
  110. pFirst = get_attrs[a++].val_ptr;
  111. OSD_INFO("PRODUCT_MODEL [%s]\n",
  112. (char *)pFirst);
  113. pFirst = get_attrs[a++].val_ptr;
  114. OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
  115. pFirst ? get_unaligned_be32(pFirst) : ~0U);
  116. pFirst = get_attrs[a++].val_ptr;
  117. OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
  118. (char *)pFirst);
  119. odi->osdname_len = get_attrs[a].len;
  120. /* Avoid NULL for memcmp optimization 0-length is good enough */
  121. odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL);
  122. if (odi->osdname_len)
  123. memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len);
  124. OSD_INFO("OSD_NAME [%s]\n", odi->osdname);
  125. a++;
  126. pFirst = get_attrs[a++].val_ptr;
  127. OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
  128. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  129. pFirst = get_attrs[a++].val_ptr;
  130. OSD_INFO("USED_CAPACITY [0x%llx]\n",
  131. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  132. pFirst = get_attrs[a++].val_ptr;
  133. OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n",
  134. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  135. if (a >= nelem)
  136. goto out;
  137. /* FIXME: Where are the time utilities */
  138. pFirst = get_attrs[a++].val_ptr;
  139. OSD_INFO("CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
  140. ((char *)pFirst)[0], ((char *)pFirst)[1],
  141. ((char *)pFirst)[2], ((char *)pFirst)[3],
  142. ((char *)pFirst)[4], ((char *)pFirst)[5]);
  143. if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
  144. unsigned len = get_attrs[a].len;
  145. char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
  146. hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
  147. sid_dump, sizeof(sid_dump), true);
  148. OSD_INFO("OSD_SYSTEM_ID(%d)\n"
  149. " [%s]\n", len, sid_dump);
  150. if (unlikely(len > sizeof(odi->systemid))) {
  151. OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). "
  152. "device idetification might not work\n", len);
  153. len = sizeof(odi->systemid);
  154. }
  155. odi->systemid_len = len;
  156. memcpy(odi->systemid, get_attrs[a].val_ptr, len);
  157. a++;
  158. }
  159. out:
  160. osd_end_request(or);
  161. return ret;
  162. }
  163. int osd_auto_detect_ver(struct osd_dev *od,
  164. void *caps, struct osd_dev_info *odi)
  165. {
  166. int ret;
  167. /* Auto-detect the osd version */
  168. ret = _osd_get_print_system_info(od, caps, odi);
  169. if (ret) {
  170. osd_dev_set_ver(od, OSD_VER1);
  171. OSD_DEBUG("converting to OSD1\n");
  172. ret = _osd_get_print_system_info(od, caps, odi);
  173. }
  174. return ret;
  175. }
  176. EXPORT_SYMBOL(osd_auto_detect_ver);
  177. static unsigned _osd_req_cdb_len(struct osd_request *or)
  178. {
  179. return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
  180. }
  181. static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
  182. {
  183. return osd_req_is_ver1(or) ?
  184. osdv1_attr_list_elem_size(len) :
  185. osdv2_attr_list_elem_size(len);
  186. }
  187. static void _osd_req_alist_elem_encode(struct osd_request *or,
  188. void *attr_last, const struct osd_attr *oa)
  189. {
  190. if (osd_req_is_ver1(or)) {
  191. struct osdv1_attributes_list_element *attr = attr_last;
  192. attr->attr_page = cpu_to_be32(oa->attr_page);
  193. attr->attr_id = cpu_to_be32(oa->attr_id);
  194. attr->attr_bytes = cpu_to_be16(oa->len);
  195. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  196. } else {
  197. struct osdv2_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. }
  203. }
  204. static int _osd_req_alist_elem_decode(struct osd_request *or,
  205. void *cur_p, struct osd_attr *oa, unsigned max_bytes)
  206. {
  207. unsigned inc;
  208. if (osd_req_is_ver1(or)) {
  209. struct osdv1_attributes_list_element *attr = cur_p;
  210. if (max_bytes < sizeof(*attr))
  211. return -1;
  212. oa->len = be16_to_cpu(attr->attr_bytes);
  213. inc = _osd_req_alist_elem_size(or, oa->len);
  214. if (inc > max_bytes)
  215. return -1;
  216. oa->attr_page = be32_to_cpu(attr->attr_page);
  217. oa->attr_id = be32_to_cpu(attr->attr_id);
  218. /* OSD1: On empty attributes we return a pointer to 2 bytes
  219. * of zeros. This keeps similar behaviour with OSD2.
  220. * (See below)
  221. */
  222. oa->val_ptr = likely(oa->len) ? attr->attr_val :
  223. (u8 *)&attr->attr_bytes;
  224. } else {
  225. struct osdv2_attributes_list_element *attr = cur_p;
  226. if (max_bytes < sizeof(*attr))
  227. return -1;
  228. oa->len = be16_to_cpu(attr->attr_bytes);
  229. inc = _osd_req_alist_elem_size(or, oa->len);
  230. if (inc > max_bytes)
  231. return -1;
  232. oa->attr_page = be32_to_cpu(attr->attr_page);
  233. oa->attr_id = be32_to_cpu(attr->attr_id);
  234. /* OSD2: For convenience, on empty attributes, we return 8 bytes
  235. * of zeros here. This keeps the same behaviour with OSD2r04,
  236. * and is nice with null terminating ASCII fields.
  237. * oa->val_ptr == NULL marks the end-of-list, or error.
  238. */
  239. oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
  240. }
  241. return inc;
  242. }
  243. static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
  244. {
  245. return osd_req_is_ver1(or) ?
  246. osdv1_list_size(list_head) :
  247. osdv2_list_size(list_head);
  248. }
  249. static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
  250. {
  251. return osd_req_is_ver1(or) ?
  252. sizeof(struct osdv1_attributes_list_header) :
  253. sizeof(struct osdv2_attributes_list_header);
  254. }
  255. static void _osd_req_set_alist_type(struct osd_request *or,
  256. void *list, int list_type)
  257. {
  258. if (osd_req_is_ver1(or)) {
  259. struct osdv1_attributes_list_header *attr_list = list;
  260. memset(attr_list, 0, sizeof(*attr_list));
  261. attr_list->type = list_type;
  262. } else {
  263. struct osdv2_attributes_list_header *attr_list = list;
  264. memset(attr_list, 0, sizeof(*attr_list));
  265. attr_list->type = list_type;
  266. }
  267. }
  268. static bool _osd_req_is_alist_type(struct osd_request *or,
  269. void *list, int list_type)
  270. {
  271. if (!list)
  272. return false;
  273. if (osd_req_is_ver1(or)) {
  274. struct osdv1_attributes_list_header *attr_list = list;
  275. return attr_list->type == list_type;
  276. } else {
  277. struct osdv2_attributes_list_header *attr_list = list;
  278. return attr_list->type == list_type;
  279. }
  280. }
  281. /* This is for List-objects not Attributes-Lists */
  282. static void _osd_req_encode_olist(struct osd_request *or,
  283. struct osd_obj_id_list *list)
  284. {
  285. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  286. if (osd_req_is_ver1(or)) {
  287. cdbh->v1.list_identifier = list->list_identifier;
  288. cdbh->v1.start_address = list->continuation_id;
  289. } else {
  290. cdbh->v2.list_identifier = list->list_identifier;
  291. cdbh->v2.start_address = list->continuation_id;
  292. }
  293. }
  294. static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
  295. u64 offset, unsigned *padding)
  296. {
  297. return __osd_encode_offset(offset, padding,
  298. osd_req_is_ver1(or) ?
  299. OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
  300. OSD_OFFSET_MAX_SHIFT);
  301. }
  302. static struct osd_security_parameters *
  303. _osd_req_sec_params(struct osd_request *or)
  304. {
  305. struct osd_cdb *ocdb = &or->cdb;
  306. if (osd_req_is_ver1(or))
  307. return (struct osd_security_parameters *)&ocdb->v1.sec_params;
  308. else
  309. return (struct osd_security_parameters *)&ocdb->v2.sec_params;
  310. }
  311. void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
  312. {
  313. memset(osdd, 0, sizeof(*osdd));
  314. osdd->scsi_device = scsi_device;
  315. osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
  316. #ifdef OSD_VER1_SUPPORT
  317. osdd->version = OSD_VER2;
  318. #endif
  319. /* TODO: Allocate pools for osd_request attributes ... */
  320. }
  321. EXPORT_SYMBOL(osd_dev_init);
  322. void osd_dev_fini(struct osd_dev *osdd)
  323. {
  324. /* TODO: De-allocate pools */
  325. osdd->scsi_device = NULL;
  326. }
  327. EXPORT_SYMBOL(osd_dev_fini);
  328. static struct osd_request *_osd_request_alloc(gfp_t gfp)
  329. {
  330. struct osd_request *or;
  331. /* TODO: Use mempool with one saved request */
  332. or = kzalloc(sizeof(*or), gfp);
  333. return or;
  334. }
  335. static void _osd_request_free(struct osd_request *or)
  336. {
  337. kfree(or);
  338. }
  339. struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
  340. {
  341. struct osd_request *or;
  342. or = _osd_request_alloc(gfp);
  343. if (!or)
  344. return NULL;
  345. or->osd_dev = dev;
  346. or->alloc_flags = gfp;
  347. or->timeout = dev->def_timeout;
  348. or->retries = OSD_REQ_RETRIES;
  349. return or;
  350. }
  351. EXPORT_SYMBOL(osd_start_request);
  352. static void _osd_free_seg(struct osd_request *or __unused,
  353. struct _osd_req_data_segment *seg)
  354. {
  355. if (!seg->buff || !seg->alloc_size)
  356. return;
  357. kfree(seg->buff);
  358. seg->buff = NULL;
  359. seg->alloc_size = 0;
  360. }
  361. static void _put_request(struct request *rq , bool is_async)
  362. {
  363. if (is_async) {
  364. WARN_ON(rq->bio);
  365. __blk_put_request(rq->q, rq);
  366. } else {
  367. /*
  368. * If osd_finalize_request() was called but the request was not
  369. * executed through the block layer, then we must release BIOs.
  370. * TODO: Keep error code in or->async_error. Need to audit all
  371. * code paths.
  372. */
  373. if (unlikely(rq->bio))
  374. blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
  375. else
  376. blk_put_request(rq);
  377. }
  378. }
  379. void osd_end_request(struct osd_request *or)
  380. {
  381. struct request *rq = or->request;
  382. /* IMPORTANT: make sure this agrees with osd_execute_request_async */
  383. bool is_async = (or->request->end_io_data == or);
  384. _osd_free_seg(or, &or->set_attr);
  385. _osd_free_seg(or, &or->enc_get_attr);
  386. _osd_free_seg(or, &or->get_attr);
  387. if (rq) {
  388. if (rq->next_rq) {
  389. _put_request(rq->next_rq, is_async);
  390. rq->next_rq = NULL;
  391. }
  392. _put_request(rq, is_async);
  393. }
  394. _osd_request_free(or);
  395. }
  396. EXPORT_SYMBOL(osd_end_request);
  397. int osd_execute_request(struct osd_request *or)
  398. {
  399. return blk_execute_rq(or->request->q, NULL, or->request, 0);
  400. }
  401. EXPORT_SYMBOL(osd_execute_request);
  402. static void osd_request_async_done(struct request *req, int error)
  403. {
  404. struct osd_request *or = req->end_io_data;
  405. or->async_error = error;
  406. if (error)
  407. OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
  408. if (or->async_done)
  409. or->async_done(or, or->async_private);
  410. else
  411. osd_end_request(or);
  412. }
  413. int osd_execute_request_async(struct osd_request *or,
  414. osd_req_done_fn *done, void *private)
  415. {
  416. or->request->end_io_data = or;
  417. or->async_private = private;
  418. or->async_done = done;
  419. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  420. osd_request_async_done);
  421. return 0;
  422. }
  423. EXPORT_SYMBOL(osd_execute_request_async);
  424. u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  425. u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  426. static int _osd_realloc_seg(struct osd_request *or,
  427. struct _osd_req_data_segment *seg, unsigned max_bytes)
  428. {
  429. void *buff;
  430. if (seg->alloc_size >= max_bytes)
  431. return 0;
  432. buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
  433. if (!buff) {
  434. OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
  435. seg->alloc_size);
  436. return -ENOMEM;
  437. }
  438. memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
  439. seg->buff = buff;
  440. seg->alloc_size = max_bytes;
  441. return 0;
  442. }
  443. static int _alloc_set_attr_list(struct osd_request *or,
  444. const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
  445. {
  446. unsigned total_bytes = add_bytes;
  447. for (; nelem; --nelem, ++oa)
  448. total_bytes += _osd_req_alist_elem_size(or, oa->len);
  449. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  450. return _osd_realloc_seg(or, &or->set_attr, total_bytes);
  451. }
  452. static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
  453. {
  454. OSD_DEBUG("total_bytes=%d\n", max_bytes);
  455. return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
  456. }
  457. static int _alloc_get_attr_list(struct osd_request *or)
  458. {
  459. OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
  460. return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
  461. }
  462. /*
  463. * Common to all OSD commands
  464. */
  465. static void _osdv1_req_encode_common(struct osd_request *or,
  466. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  467. {
  468. struct osdv1_cdb *ocdb = &or->cdb.v1;
  469. /*
  470. * For speed, the commands
  471. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  472. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  473. * are not supported here. Should pass zero and set after the call
  474. */
  475. act &= cpu_to_be16(~0x0080); /* V1 action code */
  476. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  477. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  478. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  479. ocdb->h.varlen_cdb.service_action = act;
  480. ocdb->h.partition = cpu_to_be64(obj->partition);
  481. ocdb->h.object = cpu_to_be64(obj->id);
  482. ocdb->h.v1.length = cpu_to_be64(len);
  483. ocdb->h.v1.start_address = cpu_to_be64(offset);
  484. }
  485. static void _osdv2_req_encode_common(struct osd_request *or,
  486. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  487. {
  488. struct osdv2_cdb *ocdb = &or->cdb.v2;
  489. OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
  490. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  491. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  492. ocdb->h.varlen_cdb.service_action = act;
  493. ocdb->h.partition = cpu_to_be64(obj->partition);
  494. ocdb->h.object = cpu_to_be64(obj->id);
  495. ocdb->h.v2.length = cpu_to_be64(len);
  496. ocdb->h.v2.start_address = cpu_to_be64(offset);
  497. }
  498. static void _osd_req_encode_common(struct osd_request *or,
  499. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  500. {
  501. if (osd_req_is_ver1(or))
  502. _osdv1_req_encode_common(or, act, obj, offset, len);
  503. else
  504. _osdv2_req_encode_common(or, act, obj, offset, len);
  505. }
  506. /*
  507. * Device commands
  508. */
  509. /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
  510. /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
  511. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  512. {
  513. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  514. tot_capacity);
  515. }
  516. EXPORT_SYMBOL(osd_req_format);
  517. int osd_req_list_dev_partitions(struct osd_request *or,
  518. osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
  519. {
  520. return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
  521. }
  522. EXPORT_SYMBOL(osd_req_list_dev_partitions);
  523. static void _osd_req_encode_flush(struct osd_request *or,
  524. enum osd_options_flush_scope_values op)
  525. {
  526. struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
  527. ocdb->command_specific_options = op;
  528. }
  529. void osd_req_flush_obsd(struct osd_request *or,
  530. enum osd_options_flush_scope_values op)
  531. {
  532. _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
  533. _osd_req_encode_flush(or, op);
  534. }
  535. EXPORT_SYMBOL(osd_req_flush_obsd);
  536. /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
  537. const u8 *cdb, ...); */
  538. /*TODO: void osd_req_task_management(struct osd_request *, ...); */
  539. /*
  540. * Partition commands
  541. */
  542. static void _osd_req_encode_partition(struct osd_request *or,
  543. __be16 act, osd_id partition)
  544. {
  545. struct osd_obj_id par = {
  546. .partition = partition,
  547. .id = 0,
  548. };
  549. _osd_req_encode_common(or, act, &par, 0, 0);
  550. }
  551. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  552. {
  553. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  554. }
  555. EXPORT_SYMBOL(osd_req_create_partition);
  556. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  557. {
  558. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  559. }
  560. EXPORT_SYMBOL(osd_req_remove_partition);
  561. /*TODO: void osd_req_set_partition_key(struct osd_request *,
  562. osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
  563. u8 seed[OSD_CRYPTO_SEED_SIZE]); */
  564. static int _osd_req_list_objects(struct osd_request *or,
  565. __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
  566. struct osd_obj_id_list *list, unsigned nelem)
  567. {
  568. struct request_queue *q = osd_request_queue(or->osd_dev);
  569. u64 len = nelem * sizeof(osd_id) + sizeof(*list);
  570. struct bio *bio;
  571. _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
  572. if (list->list_identifier)
  573. _osd_req_encode_olist(or, list);
  574. WARN_ON(or->in.bio);
  575. bio = bio_map_kern(q, list, len, or->alloc_flags);
  576. if (IS_ERR(bio)) {
  577. OSD_ERR("!!! Failed to allocate list_objects BIO\n");
  578. return PTR_ERR(bio);
  579. }
  580. bio->bi_rw &= ~(1 << BIO_RW);
  581. or->in.bio = bio;
  582. or->in.total_bytes = bio->bi_size;
  583. return 0;
  584. }
  585. int osd_req_list_partition_collections(struct osd_request *or,
  586. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  587. unsigned nelem)
  588. {
  589. struct osd_obj_id par = {
  590. .partition = partition,
  591. .id = 0,
  592. };
  593. return osd_req_list_collection_objects(or, &par, initial_id, list,
  594. nelem);
  595. }
  596. EXPORT_SYMBOL(osd_req_list_partition_collections);
  597. int osd_req_list_partition_objects(struct osd_request *or,
  598. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  599. unsigned nelem)
  600. {
  601. struct osd_obj_id par = {
  602. .partition = partition,
  603. .id = 0,
  604. };
  605. return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
  606. nelem);
  607. }
  608. EXPORT_SYMBOL(osd_req_list_partition_objects);
  609. void osd_req_flush_partition(struct osd_request *or,
  610. osd_id partition, enum osd_options_flush_scope_values op)
  611. {
  612. _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
  613. _osd_req_encode_flush(or, op);
  614. }
  615. EXPORT_SYMBOL(osd_req_flush_partition);
  616. /*
  617. * Collection commands
  618. */
  619. /*TODO: void osd_req_create_collection(struct osd_request *,
  620. const struct osd_obj_id *); */
  621. /*TODO: void osd_req_remove_collection(struct osd_request *,
  622. const struct osd_obj_id *); */
  623. int osd_req_list_collection_objects(struct osd_request *or,
  624. const struct osd_obj_id *obj, osd_id initial_id,
  625. struct osd_obj_id_list *list, unsigned nelem)
  626. {
  627. return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
  628. initial_id, list, nelem);
  629. }
  630. EXPORT_SYMBOL(osd_req_list_collection_objects);
  631. /*TODO: void query(struct osd_request *, ...); V2 */
  632. void osd_req_flush_collection(struct osd_request *or,
  633. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
  634. {
  635. _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
  636. _osd_req_encode_flush(or, op);
  637. }
  638. EXPORT_SYMBOL(osd_req_flush_collection);
  639. /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
  640. /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
  641. /*
  642. * Object commands
  643. */
  644. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  645. {
  646. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  647. }
  648. EXPORT_SYMBOL(osd_req_create_object);
  649. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  650. {
  651. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  652. }
  653. EXPORT_SYMBOL(osd_req_remove_object);
  654. /*TODO: void osd_req_create_multi(struct osd_request *or,
  655. struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
  656. */
  657. void osd_req_write(struct osd_request *or,
  658. const struct osd_obj_id *obj, u64 offset,
  659. struct bio *bio, u64 len)
  660. {
  661. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
  662. WARN_ON(or->out.bio || or->out.total_bytes);
  663. WARN_ON(0 == bio_rw_flagged(bio, BIO_RW));
  664. or->out.bio = bio;
  665. or->out.total_bytes = len;
  666. }
  667. EXPORT_SYMBOL(osd_req_write);
  668. int osd_req_write_kern(struct osd_request *or,
  669. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  670. {
  671. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  672. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  673. if (IS_ERR(bio))
  674. return PTR_ERR(bio);
  675. bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
  676. osd_req_write(or, obj, offset, bio, len);
  677. return 0;
  678. }
  679. EXPORT_SYMBOL(osd_req_write_kern);
  680. /*TODO: void osd_req_append(struct osd_request *,
  681. const struct osd_obj_id *, struct bio *data_out); */
  682. /*TODO: void osd_req_create_write(struct osd_request *,
  683. const struct osd_obj_id *, struct bio *data_out, u64 offset); */
  684. /*TODO: void osd_req_clear(struct osd_request *,
  685. const struct osd_obj_id *, u64 offset, u64 len); */
  686. /*TODO: void osd_req_punch(struct osd_request *,
  687. const struct osd_obj_id *, u64 offset, u64 len); V2 */
  688. void osd_req_flush_object(struct osd_request *or,
  689. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
  690. /*V2*/ u64 offset, /*V2*/ u64 len)
  691. {
  692. if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
  693. OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
  694. offset = 0;
  695. len = 0;
  696. }
  697. _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
  698. _osd_req_encode_flush(or, op);
  699. }
  700. EXPORT_SYMBOL(osd_req_flush_object);
  701. void osd_req_read(struct osd_request *or,
  702. const struct osd_obj_id *obj, u64 offset,
  703. struct bio *bio, u64 len)
  704. {
  705. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
  706. WARN_ON(or->in.bio || or->in.total_bytes);
  707. WARN_ON(1 == bio_rw_flagged(bio, BIO_RW));
  708. or->in.bio = bio;
  709. or->in.total_bytes = len;
  710. }
  711. EXPORT_SYMBOL(osd_req_read);
  712. int osd_req_read_kern(struct osd_request *or,
  713. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  714. {
  715. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  716. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  717. if (IS_ERR(bio))
  718. return PTR_ERR(bio);
  719. osd_req_read(or, obj, offset, bio, len);
  720. return 0;
  721. }
  722. EXPORT_SYMBOL(osd_req_read_kern);
  723. void osd_req_get_attributes(struct osd_request *or,
  724. const struct osd_obj_id *obj)
  725. {
  726. _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
  727. }
  728. EXPORT_SYMBOL(osd_req_get_attributes);
  729. void osd_req_set_attributes(struct osd_request *or,
  730. const struct osd_obj_id *obj)
  731. {
  732. _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
  733. }
  734. EXPORT_SYMBOL(osd_req_set_attributes);
  735. /*
  736. * Attributes List-mode
  737. */
  738. int osd_req_add_set_attr_list(struct osd_request *or,
  739. const struct osd_attr *oa, unsigned nelem)
  740. {
  741. unsigned total_bytes = or->set_attr.total_bytes;
  742. void *attr_last;
  743. int ret;
  744. if (or->attributes_mode &&
  745. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  746. WARN_ON(1);
  747. return -EINVAL;
  748. }
  749. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  750. if (!total_bytes) { /* first-time: allocate and put list header */
  751. total_bytes = _osd_req_sizeof_alist_header(or);
  752. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  753. if (ret)
  754. return ret;
  755. _osd_req_set_alist_type(or, or->set_attr.buff,
  756. OSD_ATTR_LIST_SET_RETRIEVE);
  757. }
  758. attr_last = or->set_attr.buff + total_bytes;
  759. for (; nelem; --nelem) {
  760. unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
  761. total_bytes += elem_size;
  762. if (unlikely(or->set_attr.alloc_size < total_bytes)) {
  763. or->set_attr.total_bytes = total_bytes - elem_size;
  764. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  765. if (ret)
  766. return ret;
  767. attr_last =
  768. or->set_attr.buff + or->set_attr.total_bytes;
  769. }
  770. _osd_req_alist_elem_encode(or, attr_last, oa);
  771. attr_last += elem_size;
  772. ++oa;
  773. }
  774. or->set_attr.total_bytes = total_bytes;
  775. return 0;
  776. }
  777. EXPORT_SYMBOL(osd_req_add_set_attr_list);
  778. static int _req_append_segment(struct osd_request *or,
  779. unsigned padding, struct _osd_req_data_segment *seg,
  780. struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
  781. {
  782. void *pad_buff;
  783. int ret;
  784. if (padding) {
  785. /* check if we can just add it to last buffer */
  786. if (last_seg &&
  787. (padding <= last_seg->alloc_size - last_seg->total_bytes))
  788. pad_buff = last_seg->buff + last_seg->total_bytes;
  789. else
  790. pad_buff = io->pad_buff;
  791. ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding,
  792. or->alloc_flags);
  793. if (ret)
  794. return ret;
  795. io->total_bytes += padding;
  796. }
  797. ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
  798. or->alloc_flags);
  799. if (ret)
  800. return ret;
  801. io->total_bytes += seg->total_bytes;
  802. OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
  803. seg->total_bytes);
  804. return 0;
  805. }
  806. static int _osd_req_finalize_set_attr_list(struct osd_request *or)
  807. {
  808. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  809. unsigned padding;
  810. int ret;
  811. if (!or->set_attr.total_bytes) {
  812. cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
  813. return 0;
  814. }
  815. cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
  816. cdbh->attrs_list.set_attr_offset =
  817. osd_req_encode_offset(or, or->out.total_bytes, &padding);
  818. ret = _req_append_segment(or, padding, &or->set_attr,
  819. or->out.last_seg, &or->out);
  820. if (ret)
  821. return ret;
  822. or->out.last_seg = &or->set_attr;
  823. return 0;
  824. }
  825. int osd_req_add_get_attr_list(struct osd_request *or,
  826. const struct osd_attr *oa, unsigned nelem)
  827. {
  828. unsigned total_bytes = or->enc_get_attr.total_bytes;
  829. void *attr_last;
  830. int ret;
  831. if (or->attributes_mode &&
  832. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  833. WARN_ON(1);
  834. return -EINVAL;
  835. }
  836. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  837. /* first time calc data-in list header size */
  838. if (!or->get_attr.total_bytes)
  839. or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
  840. /* calc data-out info */
  841. if (!total_bytes) { /* first-time: allocate and put list header */
  842. unsigned max_bytes;
  843. total_bytes = _osd_req_sizeof_alist_header(or);
  844. max_bytes = total_bytes +
  845. nelem * sizeof(struct osd_attributes_list_attrid);
  846. ret = _alloc_get_attr_desc(or, max_bytes);
  847. if (ret)
  848. return ret;
  849. _osd_req_set_alist_type(or, or->enc_get_attr.buff,
  850. OSD_ATTR_LIST_GET);
  851. }
  852. attr_last = or->enc_get_attr.buff + total_bytes;
  853. for (; nelem; --nelem) {
  854. struct osd_attributes_list_attrid *attrid;
  855. const unsigned cur_size = sizeof(*attrid);
  856. total_bytes += cur_size;
  857. if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
  858. or->enc_get_attr.total_bytes = total_bytes - cur_size;
  859. ret = _alloc_get_attr_desc(or,
  860. total_bytes + nelem * sizeof(*attrid));
  861. if (ret)
  862. return ret;
  863. attr_last = or->enc_get_attr.buff +
  864. or->enc_get_attr.total_bytes;
  865. }
  866. attrid = attr_last;
  867. attrid->attr_page = cpu_to_be32(oa->attr_page);
  868. attrid->attr_id = cpu_to_be32(oa->attr_id);
  869. attr_last += cur_size;
  870. /* calc data-in size */
  871. or->get_attr.total_bytes +=
  872. _osd_req_alist_elem_size(or, oa->len);
  873. ++oa;
  874. }
  875. or->enc_get_attr.total_bytes = total_bytes;
  876. OSD_DEBUG(
  877. "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
  878. or->get_attr.total_bytes,
  879. or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
  880. or->enc_get_attr.total_bytes,
  881. (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
  882. / sizeof(struct osd_attributes_list_attrid));
  883. return 0;
  884. }
  885. EXPORT_SYMBOL(osd_req_add_get_attr_list);
  886. static int _osd_req_finalize_get_attr_list(struct osd_request *or)
  887. {
  888. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  889. unsigned out_padding;
  890. unsigned in_padding;
  891. int ret;
  892. if (!or->enc_get_attr.total_bytes) {
  893. cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
  894. cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
  895. return 0;
  896. }
  897. ret = _alloc_get_attr_list(or);
  898. if (ret)
  899. return ret;
  900. /* The out-going buffer info update */
  901. OSD_DEBUG("out-going\n");
  902. cdbh->attrs_list.get_attr_desc_bytes =
  903. cpu_to_be32(or->enc_get_attr.total_bytes);
  904. cdbh->attrs_list.get_attr_desc_offset =
  905. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  906. ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
  907. or->out.last_seg, &or->out);
  908. if (ret)
  909. return ret;
  910. or->out.last_seg = &or->enc_get_attr;
  911. /* The incoming buffer info update */
  912. OSD_DEBUG("in-coming\n");
  913. cdbh->attrs_list.get_attr_alloc_length =
  914. cpu_to_be32(or->get_attr.total_bytes);
  915. cdbh->attrs_list.get_attr_offset =
  916. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  917. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  918. &or->in);
  919. if (ret)
  920. return ret;
  921. or->in.last_seg = &or->get_attr;
  922. return 0;
  923. }
  924. int osd_req_decode_get_attr_list(struct osd_request *or,
  925. struct osd_attr *oa, int *nelem, void **iterator)
  926. {
  927. unsigned cur_bytes, returned_bytes;
  928. int n;
  929. const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
  930. void *cur_p;
  931. if (!_osd_req_is_alist_type(or, or->get_attr.buff,
  932. OSD_ATTR_LIST_SET_RETRIEVE)) {
  933. oa->attr_page = 0;
  934. oa->attr_id = 0;
  935. oa->val_ptr = NULL;
  936. oa->len = 0;
  937. *iterator = NULL;
  938. return 0;
  939. }
  940. if (*iterator) {
  941. BUG_ON((*iterator < or->get_attr.buff) ||
  942. (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
  943. cur_p = *iterator;
  944. cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
  945. returned_bytes = or->get_attr.total_bytes;
  946. } else { /* first time decode the list header */
  947. cur_bytes = sizeof_attr_list;
  948. returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
  949. sizeof_attr_list;
  950. cur_p = or->get_attr.buff + sizeof_attr_list;
  951. if (returned_bytes > or->get_attr.alloc_size) {
  952. OSD_DEBUG("target report: space was not big enough! "
  953. "Allocate=%u Needed=%u\n",
  954. or->get_attr.alloc_size,
  955. returned_bytes + sizeof_attr_list);
  956. returned_bytes =
  957. or->get_attr.alloc_size - sizeof_attr_list;
  958. }
  959. or->get_attr.total_bytes = returned_bytes;
  960. }
  961. for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
  962. int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
  963. returned_bytes - cur_bytes);
  964. if (inc < 0) {
  965. OSD_ERR("BAD FOOD from target. list not valid!"
  966. "c=%d r=%d n=%d\n",
  967. cur_bytes, returned_bytes, n);
  968. oa->val_ptr = NULL;
  969. cur_bytes = returned_bytes; /* break the caller loop */
  970. break;
  971. }
  972. cur_bytes += inc;
  973. cur_p += inc;
  974. ++oa;
  975. }
  976. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  977. *nelem = n;
  978. return returned_bytes - cur_bytes;
  979. }
  980. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  981. /*
  982. * Attributes Page-mode
  983. */
  984. int osd_req_add_get_attr_page(struct osd_request *or,
  985. u32 page_id, void *attar_page, unsigned max_page_len,
  986. const struct osd_attr *set_one_attr)
  987. {
  988. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  989. if (or->attributes_mode &&
  990. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  991. WARN_ON(1);
  992. return -EINVAL;
  993. }
  994. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  995. or->get_attr.buff = attar_page;
  996. or->get_attr.total_bytes = max_page_len;
  997. or->set_attr.buff = set_one_attr->val_ptr;
  998. or->set_attr.total_bytes = set_one_attr->len;
  999. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  1000. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  1001. /* ocdb->attrs_page.get_attr_offset; */
  1002. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  1003. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  1004. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  1005. /* ocdb->attrs_page.set_attr_offset; */
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  1009. static int _osd_req_finalize_attr_page(struct osd_request *or)
  1010. {
  1011. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1012. unsigned in_padding, out_padding;
  1013. int ret;
  1014. /* returned page */
  1015. cdbh->attrs_page.get_attr_offset =
  1016. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  1017. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  1018. &or->in);
  1019. if (ret)
  1020. return ret;
  1021. /* set one value */
  1022. cdbh->attrs_page.set_attr_offset =
  1023. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  1024. ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
  1025. &or->out);
  1026. return ret;
  1027. }
  1028. static inline void osd_sec_parms_set_out_offset(bool is_v1,
  1029. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1030. {
  1031. if (is_v1)
  1032. sec_parms->v1.data_out_integrity_check_offset = offset;
  1033. else
  1034. sec_parms->v2.data_out_integrity_check_offset = offset;
  1035. }
  1036. static inline void osd_sec_parms_set_in_offset(bool is_v1,
  1037. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1038. {
  1039. if (is_v1)
  1040. sec_parms->v1.data_in_integrity_check_offset = offset;
  1041. else
  1042. sec_parms->v2.data_in_integrity_check_offset = offset;
  1043. }
  1044. static int _osd_req_finalize_data_integrity(struct osd_request *or,
  1045. bool has_in, bool has_out, u64 out_data_bytes, const u8 *cap_key)
  1046. {
  1047. struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
  1048. int ret;
  1049. if (!osd_is_sec_alldata(sec_parms))
  1050. return 0;
  1051. if (has_out) {
  1052. struct _osd_req_data_segment seg = {
  1053. .buff = &or->out_data_integ,
  1054. .total_bytes = sizeof(or->out_data_integ),
  1055. };
  1056. unsigned pad;
  1057. or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
  1058. or->out_data_integ.set_attributes_bytes = cpu_to_be64(
  1059. or->set_attr.total_bytes);
  1060. or->out_data_integ.get_attributes_bytes = cpu_to_be64(
  1061. or->enc_get_attr.total_bytes);
  1062. osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
  1063. osd_req_encode_offset(or, or->out.total_bytes, &pad));
  1064. ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
  1065. &or->out);
  1066. if (ret)
  1067. return ret;
  1068. or->out.last_seg = NULL;
  1069. /* they are now all chained to request sign them all together */
  1070. osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
  1071. cap_key);
  1072. }
  1073. if (has_in) {
  1074. struct _osd_req_data_segment seg = {
  1075. .buff = &or->in_data_integ,
  1076. .total_bytes = sizeof(or->in_data_integ),
  1077. };
  1078. unsigned pad;
  1079. osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
  1080. osd_req_encode_offset(or, or->in.total_bytes, &pad));
  1081. ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
  1082. &or->in);
  1083. if (ret)
  1084. return ret;
  1085. or->in.last_seg = NULL;
  1086. }
  1087. return 0;
  1088. }
  1089. /*
  1090. * osd_finalize_request and helpers
  1091. */
  1092. static struct request *_make_request(struct request_queue *q, bool has_write,
  1093. struct _osd_io_info *oii, gfp_t flags)
  1094. {
  1095. if (oii->bio)
  1096. return blk_make_request(q, oii->bio, flags);
  1097. else {
  1098. struct request *req;
  1099. req = blk_get_request(q, has_write ? WRITE : READ, flags);
  1100. if (unlikely(!req))
  1101. return ERR_PTR(-ENOMEM);
  1102. return req;
  1103. }
  1104. }
  1105. static int _init_blk_request(struct osd_request *or,
  1106. bool has_in, bool has_out)
  1107. {
  1108. gfp_t flags = or->alloc_flags;
  1109. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  1110. struct request_queue *q = scsi_device->request_queue;
  1111. struct request *req;
  1112. int ret;
  1113. req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
  1114. if (IS_ERR(req)) {
  1115. ret = PTR_ERR(req);
  1116. goto out;
  1117. }
  1118. or->request = req;
  1119. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1120. req->cmd_flags |= REQ_QUIET;
  1121. req->timeout = or->timeout;
  1122. req->retries = or->retries;
  1123. req->sense = or->sense;
  1124. req->sense_len = 0;
  1125. if (has_out) {
  1126. or->out.req = req;
  1127. if (has_in) {
  1128. /* allocate bidi request */
  1129. req = _make_request(q, false, &or->in, flags);
  1130. if (IS_ERR(req)) {
  1131. OSD_DEBUG("blk_get_request for bidi failed\n");
  1132. ret = PTR_ERR(req);
  1133. goto out;
  1134. }
  1135. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1136. or->in.req = or->request->next_rq = req;
  1137. }
  1138. } else if (has_in)
  1139. or->in.req = req;
  1140. ret = 0;
  1141. out:
  1142. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  1143. or, has_in, has_out, ret, or->request);
  1144. return ret;
  1145. }
  1146. int osd_finalize_request(struct osd_request *or,
  1147. u8 options, const void *cap, const u8 *cap_key)
  1148. {
  1149. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1150. bool has_in, has_out;
  1151. u64 out_data_bytes = or->out.total_bytes;
  1152. int ret;
  1153. if (options & OSD_REQ_FUA)
  1154. cdbh->options |= OSD_CDB_FUA;
  1155. if (options & OSD_REQ_DPO)
  1156. cdbh->options |= OSD_CDB_DPO;
  1157. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  1158. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  1159. osd_set_caps(&or->cdb, cap);
  1160. has_in = or->in.bio || or->get_attr.total_bytes;
  1161. has_out = or->out.bio || or->set_attr.total_bytes ||
  1162. or->enc_get_attr.total_bytes;
  1163. ret = _init_blk_request(or, has_in, has_out);
  1164. if (ret) {
  1165. OSD_DEBUG("_init_blk_request failed\n");
  1166. return ret;
  1167. }
  1168. or->out.pad_buff = sg_out_pad_buffer;
  1169. or->in.pad_buff = sg_in_pad_buffer;
  1170. if (!or->attributes_mode)
  1171. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  1172. cdbh->command_specific_options |= or->attributes_mode;
  1173. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1174. ret = _osd_req_finalize_attr_page(or);
  1175. } else {
  1176. /* TODO: I think that for the GET_ATTR command these 2 should
  1177. * be reversed to keep them in execution order (for embeded
  1178. * targets with low memory footprint)
  1179. */
  1180. ret = _osd_req_finalize_set_attr_list(or);
  1181. if (ret) {
  1182. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  1183. return ret;
  1184. }
  1185. ret = _osd_req_finalize_get_attr_list(or);
  1186. if (ret) {
  1187. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  1188. return ret;
  1189. }
  1190. }
  1191. ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
  1192. out_data_bytes, cap_key);
  1193. if (ret)
  1194. return ret;
  1195. osd_sec_sign_cdb(&or->cdb, cap_key);
  1196. or->request->cmd = or->cdb.buff;
  1197. or->request->cmd_len = _osd_req_cdb_len(or);
  1198. return 0;
  1199. }
  1200. EXPORT_SYMBOL(osd_finalize_request);
  1201. #define OSD_SENSE_PRINT1(fmt, a...) \
  1202. do { \
  1203. if (__cur_sense_need_output) \
  1204. OSD_ERR(fmt, ##a); \
  1205. } while (0)
  1206. #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
  1207. int osd_req_decode_sense_full(struct osd_request *or,
  1208. struct osd_sense_info *osi, bool silent,
  1209. struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
  1210. struct osd_attr *bad_attr_list, int max_attr)
  1211. {
  1212. int sense_len, original_sense_len;
  1213. struct osd_sense_info local_osi;
  1214. struct scsi_sense_descriptor_based *ssdb;
  1215. void *cur_descriptor;
  1216. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
  1217. const bool __cur_sense_need_output = false;
  1218. #else
  1219. bool __cur_sense_need_output = !silent;
  1220. #endif
  1221. if (!or->request->errors)
  1222. return 0;
  1223. ssdb = or->request->sense;
  1224. sense_len = or->request->sense_len;
  1225. if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
  1226. OSD_ERR("Block-layer returned error(0x%x) but "
  1227. "sense_len(%u) || key(%d) is empty\n",
  1228. or->request->errors, sense_len, ssdb->sense_key);
  1229. return -EIO;
  1230. }
  1231. if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
  1232. OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
  1233. ssdb->response_code, sense_len);
  1234. return -EIO;
  1235. }
  1236. osi = osi ? : &local_osi;
  1237. memset(osi, 0, sizeof(*osi));
  1238. osi->key = ssdb->sense_key;
  1239. osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
  1240. original_sense_len = ssdb->additional_sense_length + 8;
  1241. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
  1242. if (__cur_sense_need_output)
  1243. __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
  1244. #endif
  1245. OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
  1246. "additional_code=0x%x\n",
  1247. osi->key, original_sense_len, sense_len,
  1248. osi->additional_code);
  1249. if (original_sense_len < sense_len)
  1250. sense_len = original_sense_len;
  1251. cur_descriptor = ssdb->ssd;
  1252. sense_len -= sizeof(*ssdb);
  1253. while (sense_len > 0) {
  1254. struct scsi_sense_descriptor *ssd = cur_descriptor;
  1255. int cur_len = ssd->additional_length + 2;
  1256. sense_len -= cur_len;
  1257. if (sense_len < 0)
  1258. break; /* sense was truncated */
  1259. switch (ssd->descriptor_type) {
  1260. case scsi_sense_information:
  1261. case scsi_sense_command_specific_information:
  1262. {
  1263. struct scsi_sense_command_specific_data_descriptor
  1264. *sscd = cur_descriptor;
  1265. osi->command_info =
  1266. get_unaligned_be64(&sscd->information) ;
  1267. OSD_SENSE_PRINT2(
  1268. "command_specific_information 0x%llx \n",
  1269. _LLU(osi->command_info));
  1270. break;
  1271. }
  1272. case scsi_sense_key_specific:
  1273. {
  1274. struct scsi_sense_key_specific_data_descriptor
  1275. *ssks = cur_descriptor;
  1276. osi->sense_info = get_unaligned_be16(&ssks->value);
  1277. OSD_SENSE_PRINT2(
  1278. "sense_key_specific_information %u"
  1279. "sksv_cd_bpv_bp (0x%x)\n",
  1280. osi->sense_info, ssks->sksv_cd_bpv_bp);
  1281. break;
  1282. }
  1283. case osd_sense_object_identification:
  1284. { /*FIXME: Keep first not last, Store in array*/
  1285. struct osd_sense_identification_data_descriptor
  1286. *osidd = cur_descriptor;
  1287. osi->not_initiated_command_functions =
  1288. le32_to_cpu(osidd->not_initiated_functions);
  1289. osi->completed_command_functions =
  1290. le32_to_cpu(osidd->completed_functions);
  1291. osi->obj.partition = be64_to_cpu(osidd->partition_id);
  1292. osi->obj.id = be64_to_cpu(osidd->object_id);
  1293. OSD_SENSE_PRINT2(
  1294. "object_identification pid=0x%llx oid=0x%llx\n",
  1295. _LLU(osi->obj.partition), _LLU(osi->obj.id));
  1296. OSD_SENSE_PRINT2(
  1297. "not_initiated_bits(%x) "
  1298. "completed_command_bits(%x)\n",
  1299. osi->not_initiated_command_functions,
  1300. osi->completed_command_functions);
  1301. break;
  1302. }
  1303. case osd_sense_response_integrity_check:
  1304. {
  1305. struct osd_sense_response_integrity_check_descriptor
  1306. *osricd = cur_descriptor;
  1307. const unsigned len =
  1308. sizeof(osricd->integrity_check_value);
  1309. char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
  1310. hex_dump_to_buffer(osricd->integrity_check_value, len,
  1311. 32, 1, key_dump, sizeof(key_dump), true);
  1312. OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
  1313. }
  1314. case osd_sense_attribute_identification:
  1315. {
  1316. struct osd_sense_attributes_data_descriptor
  1317. *osadd = cur_descriptor;
  1318. unsigned len = min(cur_len, sense_len);
  1319. struct osd_sense_attr *pattr = osadd->sense_attrs;
  1320. while (len >= sizeof(*pattr)) {
  1321. u32 attr_page = be32_to_cpu(pattr->attr_page);
  1322. u32 attr_id = be32_to_cpu(pattr->attr_id);
  1323. if (!osi->attr.attr_page) {
  1324. osi->attr.attr_page = attr_page;
  1325. osi->attr.attr_id = attr_id;
  1326. }
  1327. if (bad_attr_list && max_attr) {
  1328. bad_attr_list->attr_page = attr_page;
  1329. bad_attr_list->attr_id = attr_id;
  1330. bad_attr_list++;
  1331. max_attr--;
  1332. }
  1333. len -= sizeof(*pattr);
  1334. OSD_SENSE_PRINT2(
  1335. "osd_sense_attribute_identification"
  1336. "attr_page=0x%x attr_id=0x%x\n",
  1337. attr_page, attr_id);
  1338. }
  1339. }
  1340. /*These are not legal for OSD*/
  1341. case scsi_sense_field_replaceable_unit:
  1342. OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
  1343. break;
  1344. case scsi_sense_stream_commands:
  1345. OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
  1346. break;
  1347. case scsi_sense_block_commands:
  1348. OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
  1349. break;
  1350. case scsi_sense_ata_return:
  1351. OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
  1352. break;
  1353. default:
  1354. if (ssd->descriptor_type <= scsi_sense_Reserved_last)
  1355. OSD_SENSE_PRINT2(
  1356. "scsi_sense Reserved descriptor (0x%x)",
  1357. ssd->descriptor_type);
  1358. else
  1359. OSD_SENSE_PRINT2(
  1360. "scsi_sense Vendor descriptor (0x%x)",
  1361. ssd->descriptor_type);
  1362. }
  1363. cur_descriptor += cur_len;
  1364. }
  1365. return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
  1366. }
  1367. EXPORT_SYMBOL(osd_req_decode_sense_full);
  1368. /*
  1369. * Implementation of osd_sec.h API
  1370. * TODO: Move to a separate osd_sec.c file at a later stage.
  1371. */
  1372. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  1373. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  1374. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  1375. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  1376. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  1377. };
  1378. enum { OSD_SEC_CAP_V2_ALL_CAPS =
  1379. OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
  1380. };
  1381. void osd_sec_init_nosec_doall_caps(void *caps,
  1382. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  1383. {
  1384. struct osd_capability *cap = caps;
  1385. u8 type;
  1386. u8 descriptor_type;
  1387. if (likely(obj->id)) {
  1388. if (unlikely(is_collection)) {
  1389. type = OSD_SEC_OBJ_COLLECTION;
  1390. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  1391. OSD_SEC_OBJ_DESC_COL;
  1392. } else {
  1393. type = OSD_SEC_OBJ_USER;
  1394. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  1395. }
  1396. WARN_ON(!obj->partition);
  1397. } else {
  1398. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  1399. OSD_SEC_OBJ_ROOT;
  1400. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  1401. }
  1402. memset(cap, 0, sizeof(*cap));
  1403. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  1404. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  1405. cap->h.security_method = OSD_SEC_NOSEC;
  1406. /* cap->expiration_time;
  1407. cap->AUDIT[30-10];
  1408. cap->discriminator[42-30];
  1409. cap->object_created_time; */
  1410. cap->h.object_type = type;
  1411. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  1412. cap->h.object_descriptor_type = descriptor_type;
  1413. cap->od.obj_desc.policy_access_tag = 0;
  1414. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  1415. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  1416. }
  1417. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  1418. /* FIXME: Extract version from caps pointer.
  1419. * Also Pete's target only supports caps from OSDv1 for now
  1420. */
  1421. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  1422. {
  1423. bool is_ver1 = true;
  1424. /* NOTE: They start at same address */
  1425. memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
  1426. }
  1427. bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
  1428. {
  1429. return false;
  1430. }
  1431. void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
  1432. {
  1433. }
  1434. void osd_sec_sign_data(void *data_integ __unused,
  1435. struct bio *bio __unused, const u8 *cap_key __unused)
  1436. {
  1437. }
  1438. /*
  1439. * Declared in osd_protocol.h
  1440. * 4.12.5 Data-In and Data-Out buffer offsets
  1441. * byte offset = mantissa * (2^(exponent+8))
  1442. * Returns the smallest allowed encoded offset that contains given @offset
  1443. * The actual encoded offset returned is @offset + *@padding.
  1444. */
  1445. osd_cdb_offset __osd_encode_offset(
  1446. u64 offset, unsigned *padding, int min_shift, int max_shift)
  1447. {
  1448. u64 try_offset = -1, mod, align;
  1449. osd_cdb_offset be32_offset;
  1450. int shift;
  1451. *padding = 0;
  1452. if (!offset)
  1453. return 0;
  1454. for (shift = min_shift; shift < max_shift; ++shift) {
  1455. try_offset = offset >> shift;
  1456. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  1457. break;
  1458. }
  1459. BUG_ON(shift == max_shift);
  1460. align = 1 << shift;
  1461. mod = offset & (align - 1);
  1462. if (mod) {
  1463. *padding = align - mod;
  1464. try_offset += 1;
  1465. }
  1466. try_offset |= ((shift - 8) & 0xf) << 28;
  1467. be32_offset = cpu_to_be32((u32)try_offset);
  1468. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  1469. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  1470. be32_offset, *padding);
  1471. return be32_offset;
  1472. }