osd_initiator.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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. break;
  970. }
  971. cur_bytes += inc;
  972. cur_p += inc;
  973. ++oa;
  974. }
  975. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  976. *nelem = n;
  977. return returned_bytes - cur_bytes;
  978. }
  979. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  980. /*
  981. * Attributes Page-mode
  982. */
  983. int osd_req_add_get_attr_page(struct osd_request *or,
  984. u32 page_id, void *attar_page, unsigned max_page_len,
  985. const struct osd_attr *set_one_attr)
  986. {
  987. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  988. if (or->attributes_mode &&
  989. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  990. WARN_ON(1);
  991. return -EINVAL;
  992. }
  993. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  994. or->get_attr.buff = attar_page;
  995. or->get_attr.total_bytes = max_page_len;
  996. or->set_attr.buff = set_one_attr->val_ptr;
  997. or->set_attr.total_bytes = set_one_attr->len;
  998. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  999. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  1000. /* ocdb->attrs_page.get_attr_offset; */
  1001. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  1002. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  1003. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  1004. /* ocdb->attrs_page.set_attr_offset; */
  1005. return 0;
  1006. }
  1007. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  1008. static int _osd_req_finalize_attr_page(struct osd_request *or)
  1009. {
  1010. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1011. unsigned in_padding, out_padding;
  1012. int ret;
  1013. /* returned page */
  1014. cdbh->attrs_page.get_attr_offset =
  1015. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  1016. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  1017. &or->in);
  1018. if (ret)
  1019. return ret;
  1020. /* set one value */
  1021. cdbh->attrs_page.set_attr_offset =
  1022. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  1023. ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
  1024. &or->out);
  1025. return ret;
  1026. }
  1027. static inline void osd_sec_parms_set_out_offset(bool is_v1,
  1028. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1029. {
  1030. if (is_v1)
  1031. sec_parms->v1.data_out_integrity_check_offset = offset;
  1032. else
  1033. sec_parms->v2.data_out_integrity_check_offset = offset;
  1034. }
  1035. static inline void osd_sec_parms_set_in_offset(bool is_v1,
  1036. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1037. {
  1038. if (is_v1)
  1039. sec_parms->v1.data_in_integrity_check_offset = offset;
  1040. else
  1041. sec_parms->v2.data_in_integrity_check_offset = offset;
  1042. }
  1043. static int _osd_req_finalize_data_integrity(struct osd_request *or,
  1044. bool has_in, bool has_out, u64 out_data_bytes, const u8 *cap_key)
  1045. {
  1046. struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
  1047. int ret;
  1048. if (!osd_is_sec_alldata(sec_parms))
  1049. return 0;
  1050. if (has_out) {
  1051. struct _osd_req_data_segment seg = {
  1052. .buff = &or->out_data_integ,
  1053. .total_bytes = sizeof(or->out_data_integ),
  1054. };
  1055. unsigned pad;
  1056. or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
  1057. or->out_data_integ.set_attributes_bytes = cpu_to_be64(
  1058. or->set_attr.total_bytes);
  1059. or->out_data_integ.get_attributes_bytes = cpu_to_be64(
  1060. or->enc_get_attr.total_bytes);
  1061. osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
  1062. osd_req_encode_offset(or, or->out.total_bytes, &pad));
  1063. ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
  1064. &or->out);
  1065. if (ret)
  1066. return ret;
  1067. or->out.last_seg = NULL;
  1068. /* they are now all chained to request sign them all together */
  1069. osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
  1070. cap_key);
  1071. }
  1072. if (has_in) {
  1073. struct _osd_req_data_segment seg = {
  1074. .buff = &or->in_data_integ,
  1075. .total_bytes = sizeof(or->in_data_integ),
  1076. };
  1077. unsigned pad;
  1078. osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
  1079. osd_req_encode_offset(or, or->in.total_bytes, &pad));
  1080. ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
  1081. &or->in);
  1082. if (ret)
  1083. return ret;
  1084. or->in.last_seg = NULL;
  1085. }
  1086. return 0;
  1087. }
  1088. /*
  1089. * osd_finalize_request and helpers
  1090. */
  1091. static struct request *_make_request(struct request_queue *q, bool has_write,
  1092. struct _osd_io_info *oii, gfp_t flags)
  1093. {
  1094. if (oii->bio)
  1095. return blk_make_request(q, oii->bio, flags);
  1096. else {
  1097. struct request *req;
  1098. req = blk_get_request(q, has_write ? WRITE : READ, flags);
  1099. if (unlikely(!req))
  1100. return ERR_PTR(-ENOMEM);
  1101. return req;
  1102. }
  1103. }
  1104. static int _init_blk_request(struct osd_request *or,
  1105. bool has_in, bool has_out)
  1106. {
  1107. gfp_t flags = or->alloc_flags;
  1108. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  1109. struct request_queue *q = scsi_device->request_queue;
  1110. struct request *req;
  1111. int ret;
  1112. req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
  1113. if (IS_ERR(req)) {
  1114. ret = PTR_ERR(req);
  1115. goto out;
  1116. }
  1117. or->request = req;
  1118. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1119. req->cmd_flags |= REQ_QUIET;
  1120. req->timeout = or->timeout;
  1121. req->retries = or->retries;
  1122. req->sense = or->sense;
  1123. req->sense_len = 0;
  1124. if (has_out) {
  1125. or->out.req = req;
  1126. if (has_in) {
  1127. /* allocate bidi request */
  1128. req = _make_request(q, false, &or->in, flags);
  1129. if (IS_ERR(req)) {
  1130. OSD_DEBUG("blk_get_request for bidi failed\n");
  1131. ret = PTR_ERR(req);
  1132. goto out;
  1133. }
  1134. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1135. or->in.req = or->request->next_rq = req;
  1136. }
  1137. } else if (has_in)
  1138. or->in.req = req;
  1139. ret = 0;
  1140. out:
  1141. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  1142. or, has_in, has_out, ret, or->request);
  1143. return ret;
  1144. }
  1145. int osd_finalize_request(struct osd_request *or,
  1146. u8 options, const void *cap, const u8 *cap_key)
  1147. {
  1148. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1149. bool has_in, has_out;
  1150. u64 out_data_bytes = or->out.total_bytes;
  1151. int ret;
  1152. if (options & OSD_REQ_FUA)
  1153. cdbh->options |= OSD_CDB_FUA;
  1154. if (options & OSD_REQ_DPO)
  1155. cdbh->options |= OSD_CDB_DPO;
  1156. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  1157. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  1158. osd_set_caps(&or->cdb, cap);
  1159. has_in = or->in.bio || or->get_attr.total_bytes;
  1160. has_out = or->out.bio || or->set_attr.total_bytes ||
  1161. or->enc_get_attr.total_bytes;
  1162. ret = _init_blk_request(or, has_in, has_out);
  1163. if (ret) {
  1164. OSD_DEBUG("_init_blk_request failed\n");
  1165. return ret;
  1166. }
  1167. or->out.pad_buff = sg_out_pad_buffer;
  1168. or->in.pad_buff = sg_in_pad_buffer;
  1169. if (!or->attributes_mode)
  1170. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  1171. cdbh->command_specific_options |= or->attributes_mode;
  1172. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1173. ret = _osd_req_finalize_attr_page(or);
  1174. } else {
  1175. /* TODO: I think that for the GET_ATTR command these 2 should
  1176. * be reversed to keep them in execution order (for embeded
  1177. * targets with low memory footprint)
  1178. */
  1179. ret = _osd_req_finalize_set_attr_list(or);
  1180. if (ret) {
  1181. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  1182. return ret;
  1183. }
  1184. ret = _osd_req_finalize_get_attr_list(or);
  1185. if (ret) {
  1186. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  1187. return ret;
  1188. }
  1189. }
  1190. ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
  1191. out_data_bytes, cap_key);
  1192. if (ret)
  1193. return ret;
  1194. osd_sec_sign_cdb(&or->cdb, cap_key);
  1195. or->request->cmd = or->cdb.buff;
  1196. or->request->cmd_len = _osd_req_cdb_len(or);
  1197. return 0;
  1198. }
  1199. EXPORT_SYMBOL(osd_finalize_request);
  1200. #define OSD_SENSE_PRINT1(fmt, a...) \
  1201. do { \
  1202. if (__cur_sense_need_output) \
  1203. OSD_ERR(fmt, ##a); \
  1204. } while (0)
  1205. #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
  1206. int osd_req_decode_sense_full(struct osd_request *or,
  1207. struct osd_sense_info *osi, bool silent,
  1208. struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
  1209. struct osd_attr *bad_attr_list, int max_attr)
  1210. {
  1211. int sense_len, original_sense_len;
  1212. struct osd_sense_info local_osi;
  1213. struct scsi_sense_descriptor_based *ssdb;
  1214. void *cur_descriptor;
  1215. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
  1216. const bool __cur_sense_need_output = false;
  1217. #else
  1218. bool __cur_sense_need_output = !silent;
  1219. #endif
  1220. if (!or->request->errors)
  1221. return 0;
  1222. ssdb = or->request->sense;
  1223. sense_len = or->request->sense_len;
  1224. if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
  1225. OSD_ERR("Block-layer returned error(0x%x) but "
  1226. "sense_len(%u) || key(%d) is empty\n",
  1227. or->request->errors, sense_len, ssdb->sense_key);
  1228. return -EIO;
  1229. }
  1230. if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
  1231. OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
  1232. ssdb->response_code, sense_len);
  1233. return -EIO;
  1234. }
  1235. osi = osi ? : &local_osi;
  1236. memset(osi, 0, sizeof(*osi));
  1237. osi->key = ssdb->sense_key;
  1238. osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
  1239. original_sense_len = ssdb->additional_sense_length + 8;
  1240. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
  1241. if (__cur_sense_need_output)
  1242. __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
  1243. #endif
  1244. OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
  1245. "additional_code=0x%x\n",
  1246. osi->key, original_sense_len, sense_len,
  1247. osi->additional_code);
  1248. if (original_sense_len < sense_len)
  1249. sense_len = original_sense_len;
  1250. cur_descriptor = ssdb->ssd;
  1251. sense_len -= sizeof(*ssdb);
  1252. while (sense_len > 0) {
  1253. struct scsi_sense_descriptor *ssd = cur_descriptor;
  1254. int cur_len = ssd->additional_length + 2;
  1255. sense_len -= cur_len;
  1256. if (sense_len < 0)
  1257. break; /* sense was truncated */
  1258. switch (ssd->descriptor_type) {
  1259. case scsi_sense_information:
  1260. case scsi_sense_command_specific_information:
  1261. {
  1262. struct scsi_sense_command_specific_data_descriptor
  1263. *sscd = cur_descriptor;
  1264. osi->command_info =
  1265. get_unaligned_be64(&sscd->information) ;
  1266. OSD_SENSE_PRINT2(
  1267. "command_specific_information 0x%llx \n",
  1268. _LLU(osi->command_info));
  1269. break;
  1270. }
  1271. case scsi_sense_key_specific:
  1272. {
  1273. struct scsi_sense_key_specific_data_descriptor
  1274. *ssks = cur_descriptor;
  1275. osi->sense_info = get_unaligned_be16(&ssks->value);
  1276. OSD_SENSE_PRINT2(
  1277. "sense_key_specific_information %u"
  1278. "sksv_cd_bpv_bp (0x%x)\n",
  1279. osi->sense_info, ssks->sksv_cd_bpv_bp);
  1280. break;
  1281. }
  1282. case osd_sense_object_identification:
  1283. { /*FIXME: Keep first not last, Store in array*/
  1284. struct osd_sense_identification_data_descriptor
  1285. *osidd = cur_descriptor;
  1286. osi->not_initiated_command_functions =
  1287. le32_to_cpu(osidd->not_initiated_functions);
  1288. osi->completed_command_functions =
  1289. le32_to_cpu(osidd->completed_functions);
  1290. osi->obj.partition = be64_to_cpu(osidd->partition_id);
  1291. osi->obj.id = be64_to_cpu(osidd->object_id);
  1292. OSD_SENSE_PRINT2(
  1293. "object_identification pid=0x%llx oid=0x%llx\n",
  1294. _LLU(osi->obj.partition), _LLU(osi->obj.id));
  1295. OSD_SENSE_PRINT2(
  1296. "not_initiated_bits(%x) "
  1297. "completed_command_bits(%x)\n",
  1298. osi->not_initiated_command_functions,
  1299. osi->completed_command_functions);
  1300. break;
  1301. }
  1302. case osd_sense_response_integrity_check:
  1303. {
  1304. struct osd_sense_response_integrity_check_descriptor
  1305. *osricd = cur_descriptor;
  1306. const unsigned len =
  1307. sizeof(osricd->integrity_check_value);
  1308. char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
  1309. hex_dump_to_buffer(osricd->integrity_check_value, len,
  1310. 32, 1, key_dump, sizeof(key_dump), true);
  1311. OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
  1312. }
  1313. case osd_sense_attribute_identification:
  1314. {
  1315. struct osd_sense_attributes_data_descriptor
  1316. *osadd = cur_descriptor;
  1317. unsigned len = min(cur_len, sense_len);
  1318. struct osd_sense_attr *pattr = osadd->sense_attrs;
  1319. while (len >= sizeof(*pattr)) {
  1320. u32 attr_page = be32_to_cpu(pattr->attr_page);
  1321. u32 attr_id = be32_to_cpu(pattr->attr_id);
  1322. if (!osi->attr.attr_page) {
  1323. osi->attr.attr_page = attr_page;
  1324. osi->attr.attr_id = attr_id;
  1325. }
  1326. if (bad_attr_list && max_attr) {
  1327. bad_attr_list->attr_page = attr_page;
  1328. bad_attr_list->attr_id = attr_id;
  1329. bad_attr_list++;
  1330. max_attr--;
  1331. }
  1332. len -= sizeof(*pattr);
  1333. OSD_SENSE_PRINT2(
  1334. "osd_sense_attribute_identification"
  1335. "attr_page=0x%x attr_id=0x%x\n",
  1336. attr_page, attr_id);
  1337. }
  1338. }
  1339. /*These are not legal for OSD*/
  1340. case scsi_sense_field_replaceable_unit:
  1341. OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
  1342. break;
  1343. case scsi_sense_stream_commands:
  1344. OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
  1345. break;
  1346. case scsi_sense_block_commands:
  1347. OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
  1348. break;
  1349. case scsi_sense_ata_return:
  1350. OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
  1351. break;
  1352. default:
  1353. if (ssd->descriptor_type <= scsi_sense_Reserved_last)
  1354. OSD_SENSE_PRINT2(
  1355. "scsi_sense Reserved descriptor (0x%x)",
  1356. ssd->descriptor_type);
  1357. else
  1358. OSD_SENSE_PRINT2(
  1359. "scsi_sense Vendor descriptor (0x%x)",
  1360. ssd->descriptor_type);
  1361. }
  1362. cur_descriptor += cur_len;
  1363. }
  1364. return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
  1365. }
  1366. EXPORT_SYMBOL(osd_req_decode_sense_full);
  1367. /*
  1368. * Implementation of osd_sec.h API
  1369. * TODO: Move to a separate osd_sec.c file at a later stage.
  1370. */
  1371. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  1372. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  1373. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  1374. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  1375. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  1376. };
  1377. enum { OSD_SEC_CAP_V2_ALL_CAPS =
  1378. OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
  1379. };
  1380. void osd_sec_init_nosec_doall_caps(void *caps,
  1381. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  1382. {
  1383. struct osd_capability *cap = caps;
  1384. u8 type;
  1385. u8 descriptor_type;
  1386. if (likely(obj->id)) {
  1387. if (unlikely(is_collection)) {
  1388. type = OSD_SEC_OBJ_COLLECTION;
  1389. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  1390. OSD_SEC_OBJ_DESC_COL;
  1391. } else {
  1392. type = OSD_SEC_OBJ_USER;
  1393. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  1394. }
  1395. WARN_ON(!obj->partition);
  1396. } else {
  1397. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  1398. OSD_SEC_OBJ_ROOT;
  1399. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  1400. }
  1401. memset(cap, 0, sizeof(*cap));
  1402. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  1403. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  1404. cap->h.security_method = OSD_SEC_NOSEC;
  1405. /* cap->expiration_time;
  1406. cap->AUDIT[30-10];
  1407. cap->discriminator[42-30];
  1408. cap->object_created_time; */
  1409. cap->h.object_type = type;
  1410. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  1411. cap->h.object_descriptor_type = descriptor_type;
  1412. cap->od.obj_desc.policy_access_tag = 0;
  1413. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  1414. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  1415. }
  1416. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  1417. /* FIXME: Extract version from caps pointer.
  1418. * Also Pete's target only supports caps from OSDv1 for now
  1419. */
  1420. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  1421. {
  1422. bool is_ver1 = true;
  1423. /* NOTE: They start at same address */
  1424. memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
  1425. }
  1426. bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
  1427. {
  1428. return false;
  1429. }
  1430. void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
  1431. {
  1432. }
  1433. void osd_sec_sign_data(void *data_integ __unused,
  1434. struct bio *bio __unused, const u8 *cap_key __unused)
  1435. {
  1436. }
  1437. /*
  1438. * Declared in osd_protocol.h
  1439. * 4.12.5 Data-In and Data-Out buffer offsets
  1440. * byte offset = mantissa * (2^(exponent+8))
  1441. * Returns the smallest allowed encoded offset that contains given @offset
  1442. * The actual encoded offset returned is @offset + *@padding.
  1443. */
  1444. osd_cdb_offset __osd_encode_offset(
  1445. u64 offset, unsigned *padding, int min_shift, int max_shift)
  1446. {
  1447. u64 try_offset = -1, mod, align;
  1448. osd_cdb_offset be32_offset;
  1449. int shift;
  1450. *padding = 0;
  1451. if (!offset)
  1452. return 0;
  1453. for (shift = min_shift; shift < max_shift; ++shift) {
  1454. try_offset = offset >> shift;
  1455. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  1456. break;
  1457. }
  1458. BUG_ON(shift == max_shift);
  1459. align = 1 << shift;
  1460. mod = offset & (align - 1);
  1461. if (mod) {
  1462. *padding = align - mod;
  1463. try_offset += 1;
  1464. }
  1465. try_offset |= ((shift - 8) & 0xf) << 28;
  1466. be32_offset = cpu_to_be32((u32)try_offset);
  1467. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  1468. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  1469. be32_offset, *padding);
  1470. return be32_offset;
  1471. }