osd_initiator.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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/scsi_device.h>
  44. #include "osd_debug.h"
  45. #ifndef __unused
  46. # define __unused __attribute__((unused))
  47. #endif
  48. enum { OSD_REQ_RETRIES = 1 };
  49. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  50. MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
  51. MODULE_LICENSE("GPL");
  52. static inline void build_test(void)
  53. {
  54. /* structures were not packed */
  55. BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
  56. BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
  57. }
  58. static unsigned _osd_req_cdb_len(struct osd_request *or)
  59. {
  60. return OSDv1_TOTAL_CDB_LEN;
  61. }
  62. static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
  63. {
  64. return osdv1_attr_list_elem_size(len);
  65. }
  66. static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
  67. {
  68. return osdv1_list_size(list_head);
  69. }
  70. static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
  71. {
  72. return sizeof(struct osdv1_attributes_list_header);
  73. }
  74. static void _osd_req_set_alist_type(struct osd_request *or,
  75. void *list, int list_type)
  76. {
  77. struct osdv1_attributes_list_header *attr_list = list;
  78. memset(attr_list, 0, sizeof(*attr_list));
  79. attr_list->type = list_type;
  80. }
  81. static bool _osd_req_is_alist_type(struct osd_request *or,
  82. void *list, int list_type)
  83. {
  84. if (!list)
  85. return false;
  86. if (1) {
  87. struct osdv1_attributes_list_header *attr_list = list;
  88. return attr_list->type == list_type;
  89. }
  90. }
  91. static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
  92. u64 offset, unsigned *padding)
  93. {
  94. return __osd_encode_offset(offset, padding,
  95. OSDv1_OFFSET_MIN_SHIFT, OSD_OFFSET_MAX_SHIFT);
  96. }
  97. static struct osd_security_parameters *
  98. _osd_req_sec_params(struct osd_request *or)
  99. {
  100. struct osd_cdb *ocdb = &or->cdb;
  101. return &ocdb->v1.sec_params;
  102. }
  103. void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
  104. {
  105. memset(osdd, 0, sizeof(*osdd));
  106. osdd->scsi_device = scsi_device;
  107. osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
  108. /* TODO: Allocate pools for osd_request attributes ... */
  109. }
  110. EXPORT_SYMBOL(osd_dev_init);
  111. void osd_dev_fini(struct osd_dev *osdd)
  112. {
  113. /* TODO: De-allocate pools */
  114. osdd->scsi_device = NULL;
  115. }
  116. EXPORT_SYMBOL(osd_dev_fini);
  117. static struct osd_request *_osd_request_alloc(gfp_t gfp)
  118. {
  119. struct osd_request *or;
  120. /* TODO: Use mempool with one saved request */
  121. or = kzalloc(sizeof(*or), gfp);
  122. return or;
  123. }
  124. static void _osd_request_free(struct osd_request *or)
  125. {
  126. kfree(or);
  127. }
  128. struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
  129. {
  130. struct osd_request *or;
  131. or = _osd_request_alloc(gfp);
  132. if (!or)
  133. return NULL;
  134. or->osd_dev = dev;
  135. or->alloc_flags = gfp;
  136. or->timeout = dev->def_timeout;
  137. or->retries = OSD_REQ_RETRIES;
  138. return or;
  139. }
  140. EXPORT_SYMBOL(osd_start_request);
  141. /*
  142. * If osd_finalize_request() was called but the request was not executed through
  143. * the block layer, then we must release BIOs.
  144. */
  145. static void _abort_unexecuted_bios(struct request *rq)
  146. {
  147. struct bio *bio;
  148. while ((bio = rq->bio) != NULL) {
  149. rq->bio = bio->bi_next;
  150. bio_endio(bio, 0);
  151. }
  152. }
  153. static void _osd_free_seg(struct osd_request *or __unused,
  154. struct _osd_req_data_segment *seg)
  155. {
  156. if (!seg->buff || !seg->alloc_size)
  157. return;
  158. kfree(seg->buff);
  159. seg->buff = NULL;
  160. seg->alloc_size = 0;
  161. }
  162. void osd_end_request(struct osd_request *or)
  163. {
  164. struct request *rq = or->request;
  165. _osd_free_seg(or, &or->set_attr);
  166. _osd_free_seg(or, &or->enc_get_attr);
  167. _osd_free_seg(or, &or->get_attr);
  168. if (rq) {
  169. if (rq->next_rq) {
  170. _abort_unexecuted_bios(rq->next_rq);
  171. blk_put_request(rq->next_rq);
  172. }
  173. _abort_unexecuted_bios(rq);
  174. blk_put_request(rq);
  175. }
  176. _osd_request_free(or);
  177. }
  178. EXPORT_SYMBOL(osd_end_request);
  179. int osd_execute_request(struct osd_request *or)
  180. {
  181. return blk_execute_rq(or->request->q, NULL, or->request, 0);
  182. }
  183. EXPORT_SYMBOL(osd_execute_request);
  184. static void osd_request_async_done(struct request *req, int error)
  185. {
  186. struct osd_request *or = req->end_io_data;
  187. or->async_error = error;
  188. if (error)
  189. OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
  190. if (or->async_done)
  191. or->async_done(or, or->async_private);
  192. else
  193. osd_end_request(or);
  194. }
  195. int osd_execute_request_async(struct osd_request *or,
  196. osd_req_done_fn *done, void *private)
  197. {
  198. or->request->end_io_data = or;
  199. or->async_private = private;
  200. or->async_done = done;
  201. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  202. osd_request_async_done);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(osd_execute_request_async);
  206. u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  207. u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  208. static int _osd_realloc_seg(struct osd_request *or,
  209. struct _osd_req_data_segment *seg, unsigned max_bytes)
  210. {
  211. void *buff;
  212. if (seg->alloc_size >= max_bytes)
  213. return 0;
  214. buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
  215. if (!buff) {
  216. OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
  217. seg->alloc_size);
  218. return -ENOMEM;
  219. }
  220. memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
  221. seg->buff = buff;
  222. seg->alloc_size = max_bytes;
  223. return 0;
  224. }
  225. static int _alloc_set_attr_list(struct osd_request *or,
  226. const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
  227. {
  228. unsigned total_bytes = add_bytes;
  229. for (; nelem; --nelem, ++oa)
  230. total_bytes += _osd_req_alist_elem_size(or, oa->len);
  231. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  232. return _osd_realloc_seg(or, &or->set_attr, total_bytes);
  233. }
  234. static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
  235. {
  236. OSD_DEBUG("total_bytes=%d\n", max_bytes);
  237. return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
  238. }
  239. static int _alloc_get_attr_list(struct osd_request *or)
  240. {
  241. OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
  242. return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
  243. }
  244. /*
  245. * Common to all OSD commands
  246. */
  247. static void _osdv1_req_encode_common(struct osd_request *or,
  248. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  249. {
  250. struct osdv1_cdb *ocdb = &or->cdb.v1;
  251. /*
  252. * For speed, the commands
  253. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  254. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  255. * are not supported here. Should pass zero and set after the call
  256. */
  257. act &= cpu_to_be16(~0x0080); /* V1 action code */
  258. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  259. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  260. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  261. ocdb->h.varlen_cdb.service_action = act;
  262. ocdb->h.partition = cpu_to_be64(obj->partition);
  263. ocdb->h.object = cpu_to_be64(obj->id);
  264. ocdb->h.v1.length = cpu_to_be64(len);
  265. ocdb->h.v1.start_address = cpu_to_be64(offset);
  266. }
  267. static void _osd_req_encode_common(struct osd_request *or,
  268. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  269. {
  270. _osdv1_req_encode_common(or, act, obj, offset, len);
  271. }
  272. /*
  273. * Device commands
  274. */
  275. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  276. {
  277. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  278. tot_capacity);
  279. }
  280. EXPORT_SYMBOL(osd_req_format);
  281. /*
  282. * Partition commands
  283. */
  284. static void _osd_req_encode_partition(struct osd_request *or,
  285. __be16 act, osd_id partition)
  286. {
  287. struct osd_obj_id par = {
  288. .partition = partition,
  289. .id = 0,
  290. };
  291. _osd_req_encode_common(or, act, &par, 0, 0);
  292. }
  293. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  294. {
  295. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  296. }
  297. EXPORT_SYMBOL(osd_req_create_partition);
  298. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  299. {
  300. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  301. }
  302. EXPORT_SYMBOL(osd_req_remove_partition);
  303. /*
  304. * Object commands
  305. */
  306. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  307. {
  308. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  309. }
  310. EXPORT_SYMBOL(osd_req_create_object);
  311. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  312. {
  313. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  314. }
  315. EXPORT_SYMBOL(osd_req_remove_object);
  316. void osd_req_write(struct osd_request *or,
  317. const struct osd_obj_id *obj, struct bio *bio, u64 offset)
  318. {
  319. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
  320. WARN_ON(or->out.bio || or->out.total_bytes);
  321. bio->bi_rw |= (1 << BIO_RW);
  322. or->out.bio = bio;
  323. or->out.total_bytes = bio->bi_size;
  324. }
  325. EXPORT_SYMBOL(osd_req_write);
  326. void osd_req_read(struct osd_request *or,
  327. const struct osd_obj_id *obj, struct bio *bio, u64 offset)
  328. {
  329. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
  330. WARN_ON(or->in.bio || or->in.total_bytes);
  331. bio->bi_rw &= ~(1 << BIO_RW);
  332. or->in.bio = bio;
  333. or->in.total_bytes = bio->bi_size;
  334. }
  335. EXPORT_SYMBOL(osd_req_read);
  336. void osd_req_get_attributes(struct osd_request *or,
  337. const struct osd_obj_id *obj)
  338. {
  339. _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
  340. }
  341. EXPORT_SYMBOL(osd_req_get_attributes);
  342. void osd_req_set_attributes(struct osd_request *or,
  343. const struct osd_obj_id *obj)
  344. {
  345. _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
  346. }
  347. EXPORT_SYMBOL(osd_req_set_attributes);
  348. /*
  349. * Attributes List-mode
  350. */
  351. int osd_req_add_set_attr_list(struct osd_request *or,
  352. const struct osd_attr *oa, unsigned nelem)
  353. {
  354. unsigned total_bytes = or->set_attr.total_bytes;
  355. void *attr_last;
  356. int ret;
  357. if (or->attributes_mode &&
  358. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  359. WARN_ON(1);
  360. return -EINVAL;
  361. }
  362. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  363. if (!total_bytes) { /* first-time: allocate and put list header */
  364. total_bytes = _osd_req_sizeof_alist_header(or);
  365. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  366. if (ret)
  367. return ret;
  368. _osd_req_set_alist_type(or, or->set_attr.buff,
  369. OSD_ATTR_LIST_SET_RETRIEVE);
  370. }
  371. attr_last = or->set_attr.buff + total_bytes;
  372. for (; nelem; --nelem) {
  373. struct osd_attributes_list_element *attr;
  374. unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
  375. total_bytes += elem_size;
  376. if (unlikely(or->set_attr.alloc_size < total_bytes)) {
  377. or->set_attr.total_bytes = total_bytes - elem_size;
  378. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  379. if (ret)
  380. return ret;
  381. attr_last =
  382. or->set_attr.buff + or->set_attr.total_bytes;
  383. }
  384. attr = attr_last;
  385. attr->attr_page = cpu_to_be32(oa->attr_page);
  386. attr->attr_id = cpu_to_be32(oa->attr_id);
  387. attr->attr_bytes = cpu_to_be16(oa->len);
  388. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  389. attr_last += elem_size;
  390. ++oa;
  391. }
  392. or->set_attr.total_bytes = total_bytes;
  393. return 0;
  394. }
  395. EXPORT_SYMBOL(osd_req_add_set_attr_list);
  396. static int _append_map_kern(struct request *req,
  397. void *buff, unsigned len, gfp_t flags)
  398. {
  399. struct bio *bio;
  400. int ret;
  401. bio = bio_map_kern(req->q, buff, len, flags);
  402. if (IS_ERR(bio)) {
  403. OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
  404. PTR_ERR(bio));
  405. return PTR_ERR(bio);
  406. }
  407. ret = blk_rq_append_bio(req->q, req, bio);
  408. if (ret) {
  409. OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
  410. bio_put(bio);
  411. }
  412. return ret;
  413. }
  414. static int _req_append_segment(struct osd_request *or,
  415. unsigned padding, struct _osd_req_data_segment *seg,
  416. struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
  417. {
  418. void *pad_buff;
  419. int ret;
  420. if (padding) {
  421. /* check if we can just add it to last buffer */
  422. if (last_seg &&
  423. (padding <= last_seg->alloc_size - last_seg->total_bytes))
  424. pad_buff = last_seg->buff + last_seg->total_bytes;
  425. else
  426. pad_buff = io->pad_buff;
  427. ret = _append_map_kern(io->req, pad_buff, padding,
  428. or->alloc_flags);
  429. if (ret)
  430. return ret;
  431. io->total_bytes += padding;
  432. }
  433. ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
  434. or->alloc_flags);
  435. if (ret)
  436. return ret;
  437. io->total_bytes += seg->total_bytes;
  438. OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
  439. seg->total_bytes);
  440. return 0;
  441. }
  442. static int _osd_req_finalize_set_attr_list(struct osd_request *or)
  443. {
  444. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  445. unsigned padding;
  446. int ret;
  447. if (!or->set_attr.total_bytes) {
  448. cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
  449. return 0;
  450. }
  451. cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
  452. cdbh->attrs_list.set_attr_offset =
  453. osd_req_encode_offset(or, or->out.total_bytes, &padding);
  454. ret = _req_append_segment(or, padding, &or->set_attr,
  455. or->out.last_seg, &or->out);
  456. if (ret)
  457. return ret;
  458. or->out.last_seg = &or->set_attr;
  459. return 0;
  460. }
  461. int osd_req_add_get_attr_list(struct osd_request *or,
  462. const struct osd_attr *oa, unsigned nelem)
  463. {
  464. unsigned total_bytes = or->enc_get_attr.total_bytes;
  465. void *attr_last;
  466. int ret;
  467. if (or->attributes_mode &&
  468. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  469. WARN_ON(1);
  470. return -EINVAL;
  471. }
  472. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  473. /* first time calc data-in list header size */
  474. if (!or->get_attr.total_bytes)
  475. or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
  476. /* calc data-out info */
  477. if (!total_bytes) { /* first-time: allocate and put list header */
  478. unsigned max_bytes;
  479. total_bytes = _osd_req_sizeof_alist_header(or);
  480. max_bytes = total_bytes +
  481. nelem * sizeof(struct osd_attributes_list_attrid);
  482. ret = _alloc_get_attr_desc(or, max_bytes);
  483. if (ret)
  484. return ret;
  485. _osd_req_set_alist_type(or, or->enc_get_attr.buff,
  486. OSD_ATTR_LIST_GET);
  487. }
  488. attr_last = or->enc_get_attr.buff + total_bytes;
  489. for (; nelem; --nelem) {
  490. struct osd_attributes_list_attrid *attrid;
  491. const unsigned cur_size = sizeof(*attrid);
  492. total_bytes += cur_size;
  493. if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
  494. or->enc_get_attr.total_bytes = total_bytes - cur_size;
  495. ret = _alloc_get_attr_desc(or,
  496. total_bytes + nelem * sizeof(*attrid));
  497. if (ret)
  498. return ret;
  499. attr_last = or->enc_get_attr.buff +
  500. or->enc_get_attr.total_bytes;
  501. }
  502. attrid = attr_last;
  503. attrid->attr_page = cpu_to_be32(oa->attr_page);
  504. attrid->attr_id = cpu_to_be32(oa->attr_id);
  505. attr_last += cur_size;
  506. /* calc data-in size */
  507. or->get_attr.total_bytes +=
  508. _osd_req_alist_elem_size(or, oa->len);
  509. ++oa;
  510. }
  511. or->enc_get_attr.total_bytes = total_bytes;
  512. OSD_DEBUG(
  513. "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
  514. or->get_attr.total_bytes,
  515. or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
  516. or->enc_get_attr.total_bytes,
  517. (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
  518. / sizeof(struct osd_attributes_list_attrid));
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(osd_req_add_get_attr_list);
  522. static int _osd_req_finalize_get_attr_list(struct osd_request *or)
  523. {
  524. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  525. unsigned out_padding;
  526. unsigned in_padding;
  527. int ret;
  528. if (!or->enc_get_attr.total_bytes) {
  529. cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
  530. cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
  531. return 0;
  532. }
  533. ret = _alloc_get_attr_list(or);
  534. if (ret)
  535. return ret;
  536. /* The out-going buffer info update */
  537. OSD_DEBUG("out-going\n");
  538. cdbh->attrs_list.get_attr_desc_bytes =
  539. cpu_to_be32(or->enc_get_attr.total_bytes);
  540. cdbh->attrs_list.get_attr_desc_offset =
  541. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  542. ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
  543. or->out.last_seg, &or->out);
  544. if (ret)
  545. return ret;
  546. or->out.last_seg = &or->enc_get_attr;
  547. /* The incoming buffer info update */
  548. OSD_DEBUG("in-coming\n");
  549. cdbh->attrs_list.get_attr_alloc_length =
  550. cpu_to_be32(or->get_attr.total_bytes);
  551. cdbh->attrs_list.get_attr_offset =
  552. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  553. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  554. &or->in);
  555. if (ret)
  556. return ret;
  557. or->in.last_seg = &or->get_attr;
  558. return 0;
  559. }
  560. int osd_req_decode_get_attr_list(struct osd_request *or,
  561. struct osd_attr *oa, int *nelem, void **iterator)
  562. {
  563. unsigned cur_bytes, returned_bytes;
  564. int n;
  565. const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
  566. void *cur_p;
  567. if (!_osd_req_is_alist_type(or, or->get_attr.buff,
  568. OSD_ATTR_LIST_SET_RETRIEVE)) {
  569. oa->attr_page = 0;
  570. oa->attr_id = 0;
  571. oa->val_ptr = NULL;
  572. oa->len = 0;
  573. *iterator = NULL;
  574. return 0;
  575. }
  576. if (*iterator) {
  577. BUG_ON((*iterator < or->get_attr.buff) ||
  578. (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
  579. cur_p = *iterator;
  580. cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
  581. returned_bytes = or->get_attr.total_bytes;
  582. } else { /* first time decode the list header */
  583. cur_bytes = sizeof_attr_list;
  584. returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
  585. sizeof_attr_list;
  586. cur_p = or->get_attr.buff + sizeof_attr_list;
  587. if (returned_bytes > or->get_attr.alloc_size) {
  588. OSD_DEBUG("target report: space was not big enough! "
  589. "Allocate=%u Needed=%u\n",
  590. or->get_attr.alloc_size,
  591. returned_bytes + sizeof_attr_list);
  592. returned_bytes =
  593. or->get_attr.alloc_size - sizeof_attr_list;
  594. }
  595. or->get_attr.total_bytes = returned_bytes;
  596. }
  597. for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
  598. struct osd_attributes_list_element *attr = cur_p;
  599. unsigned inc;
  600. oa->len = be16_to_cpu(attr->attr_bytes);
  601. inc = _osd_req_alist_elem_size(or, oa->len);
  602. OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
  603. oa->len, inc, cur_bytes);
  604. cur_bytes += inc;
  605. if (cur_bytes > returned_bytes) {
  606. OSD_ERR("BAD FOOD from target. list not valid!"
  607. "c=%d r=%d n=%d\n",
  608. cur_bytes, returned_bytes, n);
  609. oa->val_ptr = NULL;
  610. break;
  611. }
  612. oa->attr_page = be32_to_cpu(attr->attr_page);
  613. oa->attr_id = be32_to_cpu(attr->attr_id);
  614. oa->val_ptr = attr->attr_val;
  615. cur_p += inc;
  616. ++oa;
  617. }
  618. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  619. *nelem = n;
  620. return returned_bytes - cur_bytes;
  621. }
  622. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  623. /*
  624. * Attributes Page-mode
  625. */
  626. int osd_req_add_get_attr_page(struct osd_request *or,
  627. u32 page_id, void *attar_page, unsigned max_page_len,
  628. const struct osd_attr *set_one_attr)
  629. {
  630. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  631. if (or->attributes_mode &&
  632. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  633. WARN_ON(1);
  634. return -EINVAL;
  635. }
  636. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  637. or->get_attr.buff = attar_page;
  638. or->get_attr.total_bytes = max_page_len;
  639. or->set_attr.buff = set_one_attr->val_ptr;
  640. or->set_attr.total_bytes = set_one_attr->len;
  641. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  642. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  643. /* ocdb->attrs_page.get_attr_offset; */
  644. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  645. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  646. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  647. /* ocdb->attrs_page.set_attr_offset; */
  648. return 0;
  649. }
  650. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  651. static int _osd_req_finalize_attr_page(struct osd_request *or)
  652. {
  653. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  654. unsigned in_padding, out_padding;
  655. int ret;
  656. /* returned page */
  657. cdbh->attrs_page.get_attr_offset =
  658. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  659. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  660. &or->in);
  661. if (ret)
  662. return ret;
  663. /* set one value */
  664. cdbh->attrs_page.set_attr_offset =
  665. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  666. ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
  667. &or->out);
  668. return ret;
  669. }
  670. static int _osd_req_finalize_data_integrity(struct osd_request *or,
  671. bool has_in, bool has_out, const u8 *cap_key)
  672. {
  673. struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
  674. int ret;
  675. if (!osd_is_sec_alldata(sec_parms))
  676. return 0;
  677. if (has_out) {
  678. struct _osd_req_data_segment seg = {
  679. .buff = &or->out_data_integ,
  680. .total_bytes = sizeof(or->out_data_integ),
  681. };
  682. unsigned pad;
  683. or->out_data_integ.data_bytes = cpu_to_be64(
  684. or->out.bio ? or->out.bio->bi_size : 0);
  685. or->out_data_integ.set_attributes_bytes = cpu_to_be64(
  686. or->set_attr.total_bytes);
  687. or->out_data_integ.get_attributes_bytes = cpu_to_be64(
  688. or->enc_get_attr.total_bytes);
  689. sec_parms->data_out_integrity_check_offset =
  690. osd_req_encode_offset(or, or->out.total_bytes, &pad);
  691. ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
  692. &or->out);
  693. if (ret)
  694. return ret;
  695. or->out.last_seg = NULL;
  696. /* they are now all chained to request sign them all together */
  697. osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
  698. cap_key);
  699. }
  700. if (has_in) {
  701. struct _osd_req_data_segment seg = {
  702. .buff = &or->in_data_integ,
  703. .total_bytes = sizeof(or->in_data_integ),
  704. };
  705. unsigned pad;
  706. sec_parms->data_in_integrity_check_offset =
  707. osd_req_encode_offset(or, or->in.total_bytes, &pad);
  708. ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
  709. &or->in);
  710. if (ret)
  711. return ret;
  712. or->in.last_seg = NULL;
  713. }
  714. return 0;
  715. }
  716. /*
  717. * osd_finalize_request and helpers
  718. */
  719. static int _init_blk_request(struct osd_request *or,
  720. bool has_in, bool has_out)
  721. {
  722. gfp_t flags = or->alloc_flags;
  723. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  724. struct request_queue *q = scsi_device->request_queue;
  725. struct request *req;
  726. int ret = -ENOMEM;
  727. req = blk_get_request(q, has_out, flags);
  728. if (!req)
  729. goto out;
  730. or->request = req;
  731. req->cmd_type = REQ_TYPE_BLOCK_PC;
  732. req->timeout = or->timeout;
  733. req->retries = or->retries;
  734. req->sense = or->sense;
  735. req->sense_len = 0;
  736. if (has_out) {
  737. or->out.req = req;
  738. if (has_in) {
  739. /* allocate bidi request */
  740. req = blk_get_request(q, READ, flags);
  741. if (!req) {
  742. OSD_DEBUG("blk_get_request for bidi failed\n");
  743. goto out;
  744. }
  745. req->cmd_type = REQ_TYPE_BLOCK_PC;
  746. or->in.req = or->request->next_rq = req;
  747. }
  748. } else if (has_in)
  749. or->in.req = req;
  750. ret = 0;
  751. out:
  752. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  753. or, has_in, has_out, ret, or->request);
  754. return ret;
  755. }
  756. int osd_finalize_request(struct osd_request *or,
  757. u8 options, const void *cap, const u8 *cap_key)
  758. {
  759. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  760. bool has_in, has_out;
  761. int ret;
  762. if (options & OSD_REQ_FUA)
  763. cdbh->options |= OSD_CDB_FUA;
  764. if (options & OSD_REQ_DPO)
  765. cdbh->options |= OSD_CDB_DPO;
  766. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  767. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  768. osd_set_caps(&or->cdb, cap);
  769. has_in = or->in.bio || or->get_attr.total_bytes;
  770. has_out = or->out.bio || or->set_attr.total_bytes ||
  771. or->enc_get_attr.total_bytes;
  772. ret = _init_blk_request(or, has_in, has_out);
  773. if (ret) {
  774. OSD_DEBUG("_init_blk_request failed\n");
  775. return ret;
  776. }
  777. if (or->out.bio) {
  778. ret = blk_rq_append_bio(or->request->q, or->out.req,
  779. or->out.bio);
  780. if (ret) {
  781. OSD_DEBUG("blk_rq_append_bio out failed\n");
  782. return ret;
  783. }
  784. OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
  785. _LLU(or->out.total_bytes), or->out.req->data_len);
  786. }
  787. if (or->in.bio) {
  788. ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
  789. if (ret) {
  790. OSD_DEBUG("blk_rq_append_bio in failed\n");
  791. return ret;
  792. }
  793. OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
  794. _LLU(or->in.total_bytes), or->in.req->data_len);
  795. }
  796. or->out.pad_buff = sg_out_pad_buffer;
  797. or->in.pad_buff = sg_in_pad_buffer;
  798. if (!or->attributes_mode)
  799. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  800. cdbh->command_specific_options |= or->attributes_mode;
  801. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  802. ret = _osd_req_finalize_attr_page(or);
  803. } else {
  804. /* TODO: I think that for the GET_ATTR command these 2 should
  805. * be reversed to keep them in execution order (for embeded
  806. * targets with low memory footprint)
  807. */
  808. ret = _osd_req_finalize_set_attr_list(or);
  809. if (ret) {
  810. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  811. return ret;
  812. }
  813. ret = _osd_req_finalize_get_attr_list(or);
  814. if (ret) {
  815. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  816. return ret;
  817. }
  818. }
  819. ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
  820. if (ret)
  821. return ret;
  822. osd_sec_sign_cdb(&or->cdb, cap_key);
  823. or->request->cmd = or->cdb.buff;
  824. or->request->cmd_len = _osd_req_cdb_len(or);
  825. return 0;
  826. }
  827. EXPORT_SYMBOL(osd_finalize_request);
  828. /*
  829. * Implementation of osd_sec.h API
  830. * TODO: Move to a separate osd_sec.c file at a later stage.
  831. */
  832. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  833. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  834. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  835. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  836. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  837. };
  838. void osd_sec_init_nosec_doall_caps(void *caps,
  839. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  840. {
  841. struct osd_capability *cap = caps;
  842. u8 type;
  843. u8 descriptor_type;
  844. if (likely(obj->id)) {
  845. if (unlikely(is_collection)) {
  846. type = OSD_SEC_OBJ_COLLECTION;
  847. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  848. OSD_SEC_OBJ_DESC_COL;
  849. } else {
  850. type = OSD_SEC_OBJ_USER;
  851. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  852. }
  853. WARN_ON(!obj->partition);
  854. } else {
  855. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  856. OSD_SEC_OBJ_ROOT;
  857. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  858. }
  859. memset(cap, 0, sizeof(*cap));
  860. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  861. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  862. cap->h.security_method = OSD_SEC_NOSEC;
  863. /* cap->expiration_time;
  864. cap->AUDIT[30-10];
  865. cap->discriminator[42-30];
  866. cap->object_created_time; */
  867. cap->h.object_type = type;
  868. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  869. cap->h.object_descriptor_type = descriptor_type;
  870. cap->od.obj_desc.policy_access_tag = 0;
  871. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  872. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  873. }
  874. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  875. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  876. {
  877. memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
  878. }
  879. bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
  880. {
  881. return false;
  882. }
  883. void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
  884. {
  885. }
  886. void osd_sec_sign_data(void *data_integ __unused,
  887. struct bio *bio __unused, const u8 *cap_key __unused)
  888. {
  889. }
  890. /*
  891. * Declared in osd_protocol.h
  892. * 4.12.5 Data-In and Data-Out buffer offsets
  893. * byte offset = mantissa * (2^(exponent+8))
  894. * Returns the smallest allowed encoded offset that contains given @offset
  895. * The actual encoded offset returned is @offset + *@padding.
  896. */
  897. osd_cdb_offset __osd_encode_offset(
  898. u64 offset, unsigned *padding, int min_shift, int max_shift)
  899. {
  900. u64 try_offset = -1, mod, align;
  901. osd_cdb_offset be32_offset;
  902. int shift;
  903. *padding = 0;
  904. if (!offset)
  905. return 0;
  906. for (shift = min_shift; shift < max_shift; ++shift) {
  907. try_offset = offset >> shift;
  908. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  909. break;
  910. }
  911. BUG_ON(shift == max_shift);
  912. align = 1 << shift;
  913. mod = offset & (align - 1);
  914. if (mod) {
  915. *padding = align - mod;
  916. try_offset += 1;
  917. }
  918. try_offset |= ((shift - 8) & 0xf) << 28;
  919. be32_offset = cpu_to_be32((u32)try_offset);
  920. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  921. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  922. be32_offset, *padding);
  923. return be32_offset;
  924. }