2 * osd_initiator - Main body of the osd initiator library.
4 * Note: The file does not contain the advanced security functionality which
5 * is only needed by the security_manager's initiators.
7 * Copyright (C) 2008 Panasas Inc. All rights reserved.
10 * Boaz Harrosh <bharrosh@panasas.com>
11 * Benny Halevy <bhalevy@panasas.com>
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
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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.
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.
42 #include <scsi/osd_initiator.h>
43 #include <scsi/osd_sec.h>
44 #include <scsi/scsi_device.h>
46 #include "osd_debug.h"
49 # define __unused __attribute__((unused))
52 enum { OSD_REQ_RETRIES = 1 };
54 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
55 MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
56 MODULE_LICENSE("GPL");
58 static inline void build_test(void)
60 /* structures were not packed */
61 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
62 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
65 static unsigned _osd_req_cdb_len(struct osd_request *or)
67 return OSDv1_TOTAL_CDB_LEN;
70 static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
72 return osdv1_attr_list_elem_size(len);
75 static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
77 return osdv1_list_size(list_head);
80 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
82 return sizeof(struct osdv1_attributes_list_header);
85 static void _osd_req_set_alist_type(struct osd_request *or,
86 void *list, int list_type)
88 struct osdv1_attributes_list_header *attr_list = list;
90 memset(attr_list, 0, sizeof(*attr_list));
91 attr_list->type = list_type;
94 static bool _osd_req_is_alist_type(struct osd_request *or,
95 void *list, int list_type)
101 struct osdv1_attributes_list_header *attr_list = list;
103 return attr_list->type == list_type;
107 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
108 u64 offset, unsigned *padding)
110 return __osd_encode_offset(offset, padding,
111 OSDv1_OFFSET_MIN_SHIFT, OSD_OFFSET_MAX_SHIFT);
114 static struct osd_security_parameters *
115 _osd_req_sec_params(struct osd_request *or)
117 struct osd_cdb *ocdb = &or->cdb;
119 return &ocdb->v1.sec_params;
122 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
124 memset(osdd, 0, sizeof(*osdd));
125 osdd->scsi_device = scsi_device;
126 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
127 /* TODO: Allocate pools for osd_request attributes ... */
129 EXPORT_SYMBOL(osd_dev_init);
131 void osd_dev_fini(struct osd_dev *osdd)
133 /* TODO: De-allocate pools */
135 osdd->scsi_device = NULL;
137 EXPORT_SYMBOL(osd_dev_fini);
139 static struct osd_request *_osd_request_alloc(gfp_t gfp)
141 struct osd_request *or;
143 /* TODO: Use mempool with one saved request */
144 or = kzalloc(sizeof(*or), gfp);
148 static void _osd_request_free(struct osd_request *or)
153 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
155 struct osd_request *or;
157 or = _osd_request_alloc(gfp);
162 or->alloc_flags = gfp;
163 or->timeout = dev->def_timeout;
164 or->retries = OSD_REQ_RETRIES;
168 EXPORT_SYMBOL(osd_start_request);
171 * If osd_finalize_request() was called but the request was not executed through
172 * the block layer, then we must release BIOs.
174 static void _abort_unexecuted_bios(struct request *rq)
178 while ((bio = rq->bio) != NULL) {
179 rq->bio = bio->bi_next;
184 static void _osd_free_seg(struct osd_request *or __unused,
185 struct _osd_req_data_segment *seg)
187 if (!seg->buff || !seg->alloc_size)
195 void osd_end_request(struct osd_request *or)
197 struct request *rq = or->request;
199 _osd_free_seg(or, &or->set_attr);
200 _osd_free_seg(or, &or->enc_get_attr);
201 _osd_free_seg(or, &or->get_attr);
205 _abort_unexecuted_bios(rq->next_rq);
206 blk_put_request(rq->next_rq);
209 _abort_unexecuted_bios(rq);
212 _osd_request_free(or);
214 EXPORT_SYMBOL(osd_end_request);
216 int osd_execute_request(struct osd_request *or)
218 return blk_execute_rq(or->request->q, NULL, or->request, 0);
220 EXPORT_SYMBOL(osd_execute_request);
222 static void osd_request_async_done(struct request *req, int error)
224 struct osd_request *or = req->end_io_data;
226 or->async_error = error;
229 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
232 or->async_done(or, or->async_private);
237 int osd_execute_request_async(struct osd_request *or,
238 osd_req_done_fn *done, void *private)
240 or->request->end_io_data = or;
241 or->async_private = private;
242 or->async_done = done;
244 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
245 osd_request_async_done);
248 EXPORT_SYMBOL(osd_execute_request_async);
250 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
251 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
253 static int _osd_realloc_seg(struct osd_request *or,
254 struct _osd_req_data_segment *seg, unsigned max_bytes)
258 if (seg->alloc_size >= max_bytes)
261 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
263 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
268 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
270 seg->alloc_size = max_bytes;
274 static int _alloc_set_attr_list(struct osd_request *or,
275 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
277 unsigned total_bytes = add_bytes;
279 for (; nelem; --nelem, ++oa)
280 total_bytes += _osd_req_alist_elem_size(or, oa->len);
282 OSD_DEBUG("total_bytes=%d\n", total_bytes);
283 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
286 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
288 OSD_DEBUG("total_bytes=%d\n", max_bytes);
289 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
292 static int _alloc_get_attr_list(struct osd_request *or)
294 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
295 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
299 * Common to all OSD commands
302 static void _osdv1_req_encode_common(struct osd_request *or,
303 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
305 struct osdv1_cdb *ocdb = &or->cdb.v1;
308 * For speed, the commands
309 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
310 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
311 * are not supported here. Should pass zero and set after the call
313 act &= cpu_to_be16(~0x0080); /* V1 action code */
315 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
317 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
318 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
319 ocdb->h.varlen_cdb.service_action = act;
321 ocdb->h.partition = cpu_to_be64(obj->partition);
322 ocdb->h.object = cpu_to_be64(obj->id);
323 ocdb->h.v1.length = cpu_to_be64(len);
324 ocdb->h.v1.start_address = cpu_to_be64(offset);
327 static void _osd_req_encode_common(struct osd_request *or,
328 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
330 _osdv1_req_encode_common(or, act, obj, offset, len);
336 void osd_req_format(struct osd_request *or, u64 tot_capacity)
338 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
341 EXPORT_SYMBOL(osd_req_format);
346 static void _osd_req_encode_partition(struct osd_request *or,
347 __be16 act, osd_id partition)
349 struct osd_obj_id par = {
350 .partition = partition,
354 _osd_req_encode_common(or, act, &par, 0, 0);
357 void osd_req_create_partition(struct osd_request *or, osd_id partition)
359 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
361 EXPORT_SYMBOL(osd_req_create_partition);
363 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
365 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
367 EXPORT_SYMBOL(osd_req_remove_partition);
372 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
374 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
376 EXPORT_SYMBOL(osd_req_create_object);
378 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
380 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
382 EXPORT_SYMBOL(osd_req_remove_object);
384 void osd_req_write(struct osd_request *or,
385 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
387 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
388 WARN_ON(or->out.bio || or->out.total_bytes);
389 bio->bi_rw |= (1 << BIO_RW);
391 or->out.total_bytes = bio->bi_size;
393 EXPORT_SYMBOL(osd_req_write);
395 void osd_req_read(struct osd_request *or,
396 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
398 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
399 WARN_ON(or->in.bio || or->in.total_bytes);
400 bio->bi_rw &= ~(1 << BIO_RW);
402 or->in.total_bytes = bio->bi_size;
404 EXPORT_SYMBOL(osd_req_read);
406 void osd_req_get_attributes(struct osd_request *or,
407 const struct osd_obj_id *obj)
409 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
411 EXPORT_SYMBOL(osd_req_get_attributes);
413 void osd_req_set_attributes(struct osd_request *or,
414 const struct osd_obj_id *obj)
416 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
418 EXPORT_SYMBOL(osd_req_set_attributes);
421 * Attributes List-mode
424 int osd_req_add_set_attr_list(struct osd_request *or,
425 const struct osd_attr *oa, unsigned nelem)
427 unsigned total_bytes = or->set_attr.total_bytes;
431 if (or->attributes_mode &&
432 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
436 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
438 if (!total_bytes) { /* first-time: allocate and put list header */
439 total_bytes = _osd_req_sizeof_alist_header(or);
440 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
443 _osd_req_set_alist_type(or, or->set_attr.buff,
444 OSD_ATTR_LIST_SET_RETRIEVE);
446 attr_last = or->set_attr.buff + total_bytes;
448 for (; nelem; --nelem) {
449 struct osd_attributes_list_element *attr;
450 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
452 total_bytes += elem_size;
453 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
454 or->set_attr.total_bytes = total_bytes - elem_size;
455 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
459 or->set_attr.buff + or->set_attr.total_bytes;
463 attr->attr_page = cpu_to_be32(oa->attr_page);
464 attr->attr_id = cpu_to_be32(oa->attr_id);
465 attr->attr_bytes = cpu_to_be16(oa->len);
466 memcpy(attr->attr_val, oa->val_ptr, oa->len);
468 attr_last += elem_size;
472 or->set_attr.total_bytes = total_bytes;
475 EXPORT_SYMBOL(osd_req_add_set_attr_list);
477 static int _append_map_kern(struct request *req,
478 void *buff, unsigned len, gfp_t flags)
483 bio = bio_map_kern(req->q, buff, len, flags);
485 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
489 ret = blk_rq_append_bio(req->q, req, bio);
491 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
497 static int _req_append_segment(struct osd_request *or,
498 unsigned padding, struct _osd_req_data_segment *seg,
499 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
505 /* check if we can just add it to last buffer */
507 (padding <= last_seg->alloc_size - last_seg->total_bytes))
508 pad_buff = last_seg->buff + last_seg->total_bytes;
510 pad_buff = io->pad_buff;
512 ret = _append_map_kern(io->req, pad_buff, padding,
516 io->total_bytes += padding;
519 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
524 io->total_bytes += seg->total_bytes;
525 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
530 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
532 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
536 if (!or->set_attr.total_bytes) {
537 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
541 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
542 cdbh->attrs_list.set_attr_offset =
543 osd_req_encode_offset(or, or->out.total_bytes, &padding);
545 ret = _req_append_segment(or, padding, &or->set_attr,
546 or->out.last_seg, &or->out);
550 or->out.last_seg = &or->set_attr;
554 int osd_req_add_get_attr_list(struct osd_request *or,
555 const struct osd_attr *oa, unsigned nelem)
557 unsigned total_bytes = or->enc_get_attr.total_bytes;
561 if (or->attributes_mode &&
562 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
566 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
568 /* first time calc data-in list header size */
569 if (!or->get_attr.total_bytes)
570 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
572 /* calc data-out info */
573 if (!total_bytes) { /* first-time: allocate and put list header */
576 total_bytes = _osd_req_sizeof_alist_header(or);
577 max_bytes = total_bytes +
578 nelem * sizeof(struct osd_attributes_list_attrid);
579 ret = _alloc_get_attr_desc(or, max_bytes);
583 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
586 attr_last = or->enc_get_attr.buff + total_bytes;
588 for (; nelem; --nelem) {
589 struct osd_attributes_list_attrid *attrid;
590 const unsigned cur_size = sizeof(*attrid);
592 total_bytes += cur_size;
593 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
594 or->enc_get_attr.total_bytes = total_bytes - cur_size;
595 ret = _alloc_get_attr_desc(or,
596 total_bytes + nelem * sizeof(*attrid));
599 attr_last = or->enc_get_attr.buff +
600 or->enc_get_attr.total_bytes;
604 attrid->attr_page = cpu_to_be32(oa->attr_page);
605 attrid->attr_id = cpu_to_be32(oa->attr_id);
607 attr_last += cur_size;
609 /* calc data-in size */
610 or->get_attr.total_bytes +=
611 _osd_req_alist_elem_size(or, oa->len);
615 or->enc_get_attr.total_bytes = total_bytes;
618 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
619 or->get_attr.total_bytes,
620 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
621 or->enc_get_attr.total_bytes,
622 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
623 / sizeof(struct osd_attributes_list_attrid));
627 EXPORT_SYMBOL(osd_req_add_get_attr_list);
629 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
631 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
632 unsigned out_padding;
636 if (!or->enc_get_attr.total_bytes) {
637 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
638 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
642 ret = _alloc_get_attr_list(or);
646 /* The out-going buffer info update */
647 OSD_DEBUG("out-going\n");
648 cdbh->attrs_list.get_attr_desc_bytes =
649 cpu_to_be32(or->enc_get_attr.total_bytes);
651 cdbh->attrs_list.get_attr_desc_offset =
652 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
654 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
655 or->out.last_seg, &or->out);
658 or->out.last_seg = &or->enc_get_attr;
660 /* The incoming buffer info update */
661 OSD_DEBUG("in-coming\n");
662 cdbh->attrs_list.get_attr_alloc_length =
663 cpu_to_be32(or->get_attr.total_bytes);
665 cdbh->attrs_list.get_attr_offset =
666 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
668 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
672 or->in.last_seg = &or->get_attr;
677 int osd_req_decode_get_attr_list(struct osd_request *or,
678 struct osd_attr *oa, int *nelem, void **iterator)
680 unsigned cur_bytes, returned_bytes;
682 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
685 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
686 OSD_ATTR_LIST_SET_RETRIEVE)) {
696 BUG_ON((*iterator < or->get_attr.buff) ||
697 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
699 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
700 returned_bytes = or->get_attr.total_bytes;
701 } else { /* first time decode the list header */
702 cur_bytes = sizeof_attr_list;
703 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
706 cur_p = or->get_attr.buff + sizeof_attr_list;
708 if (returned_bytes > or->get_attr.alloc_size) {
709 OSD_DEBUG("target report: space was not big enough! "
710 "Allocate=%u Needed=%u\n",
711 or->get_attr.alloc_size,
712 returned_bytes + sizeof_attr_list);
715 or->get_attr.alloc_size - sizeof_attr_list;
717 or->get_attr.total_bytes = returned_bytes;
720 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
721 struct osd_attributes_list_element *attr = cur_p;
724 oa->len = be16_to_cpu(attr->attr_bytes);
725 inc = _osd_req_alist_elem_size(or, oa->len);
726 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
727 oa->len, inc, cur_bytes);
729 if (cur_bytes > returned_bytes) {
730 OSD_ERR("BAD FOOD from target. list not valid!"
732 cur_bytes, returned_bytes, n);
737 oa->attr_page = be32_to_cpu(attr->attr_page);
738 oa->attr_id = be32_to_cpu(attr->attr_id);
739 oa->val_ptr = attr->attr_val;
745 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
747 return returned_bytes - cur_bytes;
749 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
752 * Attributes Page-mode
755 int osd_req_add_get_attr_page(struct osd_request *or,
756 u32 page_id, void *attar_page, unsigned max_page_len,
757 const struct osd_attr *set_one_attr)
759 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
761 if (or->attributes_mode &&
762 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
766 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
768 or->get_attr.buff = attar_page;
769 or->get_attr.total_bytes = max_page_len;
771 or->set_attr.buff = set_one_attr->val_ptr;
772 or->set_attr.total_bytes = set_one_attr->len;
774 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
775 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
776 /* ocdb->attrs_page.get_attr_offset; */
778 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
779 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
780 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
781 /* ocdb->attrs_page.set_attr_offset; */
784 EXPORT_SYMBOL(osd_req_add_get_attr_page);
786 static int _osd_req_finalize_attr_page(struct osd_request *or)
788 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
789 unsigned in_padding, out_padding;
793 cdbh->attrs_page.get_attr_offset =
794 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
796 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
802 cdbh->attrs_page.set_attr_offset =
803 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
805 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
810 static int _osd_req_finalize_data_integrity(struct osd_request *or,
811 bool has_in, bool has_out, const u8 *cap_key)
813 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
816 if (!osd_is_sec_alldata(sec_parms))
820 struct _osd_req_data_segment seg = {
821 .buff = &or->out_data_integ,
822 .total_bytes = sizeof(or->out_data_integ),
826 or->out_data_integ.data_bytes = cpu_to_be64(
827 or->out.bio ? or->out.bio->bi_size : 0);
828 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
829 or->set_attr.total_bytes);
830 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
831 or->enc_get_attr.total_bytes);
833 sec_parms->data_out_integrity_check_offset =
834 osd_req_encode_offset(or, or->out.total_bytes, &pad);
836 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
840 or->out.last_seg = NULL;
842 /* they are now all chained to request sign them all together */
843 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
848 struct _osd_req_data_segment seg = {
849 .buff = &or->in_data_integ,
850 .total_bytes = sizeof(or->in_data_integ),
854 sec_parms->data_in_integrity_check_offset =
855 osd_req_encode_offset(or, or->in.total_bytes, &pad);
857 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
862 or->in.last_seg = NULL;
869 * osd_finalize_request and helpers
872 static int _init_blk_request(struct osd_request *or,
873 bool has_in, bool has_out)
875 gfp_t flags = or->alloc_flags;
876 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
877 struct request_queue *q = scsi_device->request_queue;
881 req = blk_get_request(q, has_out, flags);
886 req->cmd_type = REQ_TYPE_BLOCK_PC;
887 req->timeout = or->timeout;
888 req->retries = or->retries;
889 req->sense = or->sense;
895 /* allocate bidi request */
896 req = blk_get_request(q, READ, flags);
898 OSD_DEBUG("blk_get_request for bidi failed\n");
901 req->cmd_type = REQ_TYPE_BLOCK_PC;
902 or->in.req = or->request->next_rq = req;
909 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
910 or, has_in, has_out, ret, or->request);
914 int osd_finalize_request(struct osd_request *or,
915 u8 options, const void *cap, const u8 *cap_key)
917 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
918 bool has_in, has_out;
921 if (options & OSD_REQ_FUA)
922 cdbh->options |= OSD_CDB_FUA;
924 if (options & OSD_REQ_DPO)
925 cdbh->options |= OSD_CDB_DPO;
927 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
928 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
930 osd_set_caps(&or->cdb, cap);
932 has_in = or->in.bio || or->get_attr.total_bytes;
933 has_out = or->out.bio || or->set_attr.total_bytes ||
934 or->enc_get_attr.total_bytes;
936 ret = _init_blk_request(or, has_in, has_out);
938 OSD_DEBUG("_init_blk_request failed\n");
943 ret = blk_rq_append_bio(or->request->q, or->out.req,
946 OSD_DEBUG("blk_rq_append_bio out failed\n");
949 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
950 _LLU(or->out.total_bytes), or->out.req->data_len);
953 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
955 OSD_DEBUG("blk_rq_append_bio in failed\n");
958 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
959 _LLU(or->in.total_bytes), or->in.req->data_len);
962 or->out.pad_buff = sg_out_pad_buffer;
963 or->in.pad_buff = sg_in_pad_buffer;
965 if (!or->attributes_mode)
966 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
967 cdbh->command_specific_options |= or->attributes_mode;
968 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
969 ret = _osd_req_finalize_attr_page(or);
971 /* TODO: I think that for the GET_ATTR command these 2 should
972 * be reversed to keep them in execution order (for embeded
973 * targets with low memory footprint)
975 ret = _osd_req_finalize_set_attr_list(or);
977 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
981 ret = _osd_req_finalize_get_attr_list(or);
983 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
988 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
992 osd_sec_sign_cdb(&or->cdb, cap_key);
994 or->request->cmd = or->cdb.buff;
995 or->request->cmd_len = _osd_req_cdb_len(or);
999 EXPORT_SYMBOL(osd_finalize_request);
1002 * Implementation of osd_sec.h API
1003 * TODO: Move to a separate osd_sec.c file at a later stage.
1006 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1007 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1008 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1009 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1010 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1013 void osd_sec_init_nosec_doall_caps(void *caps,
1014 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1016 struct osd_capability *cap = caps;
1020 if (likely(obj->id)) {
1021 if (unlikely(is_collection)) {
1022 type = OSD_SEC_OBJ_COLLECTION;
1023 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1024 OSD_SEC_OBJ_DESC_COL;
1026 type = OSD_SEC_OBJ_USER;
1027 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1029 WARN_ON(!obj->partition);
1031 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1033 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1036 memset(cap, 0, sizeof(*cap));
1038 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1039 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1040 cap->h.security_method = OSD_SEC_NOSEC;
1041 /* cap->expiration_time;
1043 cap->discriminator[42-30];
1044 cap->object_created_time; */
1045 cap->h.object_type = type;
1046 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1047 cap->h.object_descriptor_type = descriptor_type;
1048 cap->od.obj_desc.policy_access_tag = 0;
1049 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1050 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1052 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1054 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1056 memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
1059 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1064 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1068 void osd_sec_sign_data(void *data_integ __unused,
1069 struct bio *bio __unused, const u8 *cap_key __unused)
1074 * Declared in osd_protocol.h
1075 * 4.12.5 Data-In and Data-Out buffer offsets
1076 * byte offset = mantissa * (2^(exponent+8))
1077 * Returns the smallest allowed encoded offset that contains given @offset
1078 * The actual encoded offset returned is @offset + *@padding.
1080 osd_cdb_offset __osd_encode_offset(
1081 u64 offset, unsigned *padding, int min_shift, int max_shift)
1083 u64 try_offset = -1, mod, align;
1084 osd_cdb_offset be32_offset;
1091 for (shift = min_shift; shift < max_shift; ++shift) {
1092 try_offset = offset >> shift;
1093 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1097 BUG_ON(shift == max_shift);
1100 mod = offset & (align - 1);
1102 *padding = align - mod;
1106 try_offset |= ((shift - 8) & 0xf) << 28;
1107 be32_offset = cpu_to_be32((u32)try_offset);
1109 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1110 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1111 be32_offset, *padding);