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 /* This is for List-objects not Attributes-Lists */
108 static void _osd_req_encode_olist(struct osd_request *or,
109 struct osd_obj_id_list *list)
111 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
113 cdbh->v1.list_identifier = list->list_identifier;
114 cdbh->v1.start_address = list->continuation_id;
117 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
118 u64 offset, unsigned *padding)
120 return __osd_encode_offset(offset, padding,
121 OSDv1_OFFSET_MIN_SHIFT, OSD_OFFSET_MAX_SHIFT);
124 static struct osd_security_parameters *
125 _osd_req_sec_params(struct osd_request *or)
127 struct osd_cdb *ocdb = &or->cdb;
129 return &ocdb->v1.sec_params;
132 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
134 memset(osdd, 0, sizeof(*osdd));
135 osdd->scsi_device = scsi_device;
136 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
137 /* TODO: Allocate pools for osd_request attributes ... */
139 EXPORT_SYMBOL(osd_dev_init);
141 void osd_dev_fini(struct osd_dev *osdd)
143 /* TODO: De-allocate pools */
145 osdd->scsi_device = NULL;
147 EXPORT_SYMBOL(osd_dev_fini);
149 static struct osd_request *_osd_request_alloc(gfp_t gfp)
151 struct osd_request *or;
153 /* TODO: Use mempool with one saved request */
154 or = kzalloc(sizeof(*or), gfp);
158 static void _osd_request_free(struct osd_request *or)
163 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
165 struct osd_request *or;
167 or = _osd_request_alloc(gfp);
172 or->alloc_flags = gfp;
173 or->timeout = dev->def_timeout;
174 or->retries = OSD_REQ_RETRIES;
178 EXPORT_SYMBOL(osd_start_request);
181 * If osd_finalize_request() was called but the request was not executed through
182 * the block layer, then we must release BIOs.
184 static void _abort_unexecuted_bios(struct request *rq)
188 while ((bio = rq->bio) != NULL) {
189 rq->bio = bio->bi_next;
194 static void _osd_free_seg(struct osd_request *or __unused,
195 struct _osd_req_data_segment *seg)
197 if (!seg->buff || !seg->alloc_size)
205 void osd_end_request(struct osd_request *or)
207 struct request *rq = or->request;
209 _osd_free_seg(or, &or->set_attr);
210 _osd_free_seg(or, &or->enc_get_attr);
211 _osd_free_seg(or, &or->get_attr);
215 _abort_unexecuted_bios(rq->next_rq);
216 blk_put_request(rq->next_rq);
219 _abort_unexecuted_bios(rq);
222 _osd_request_free(or);
224 EXPORT_SYMBOL(osd_end_request);
226 int osd_execute_request(struct osd_request *or)
228 return blk_execute_rq(or->request->q, NULL, or->request, 0);
230 EXPORT_SYMBOL(osd_execute_request);
232 static void osd_request_async_done(struct request *req, int error)
234 struct osd_request *or = req->end_io_data;
236 or->async_error = error;
239 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
242 or->async_done(or, or->async_private);
247 int osd_execute_request_async(struct osd_request *or,
248 osd_req_done_fn *done, void *private)
250 or->request->end_io_data = or;
251 or->async_private = private;
252 or->async_done = done;
254 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
255 osd_request_async_done);
258 EXPORT_SYMBOL(osd_execute_request_async);
260 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
261 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
263 static int _osd_realloc_seg(struct osd_request *or,
264 struct _osd_req_data_segment *seg, unsigned max_bytes)
268 if (seg->alloc_size >= max_bytes)
271 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
273 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
278 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
280 seg->alloc_size = max_bytes;
284 static int _alloc_set_attr_list(struct osd_request *or,
285 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
287 unsigned total_bytes = add_bytes;
289 for (; nelem; --nelem, ++oa)
290 total_bytes += _osd_req_alist_elem_size(or, oa->len);
292 OSD_DEBUG("total_bytes=%d\n", total_bytes);
293 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
296 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
298 OSD_DEBUG("total_bytes=%d\n", max_bytes);
299 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
302 static int _alloc_get_attr_list(struct osd_request *or)
304 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
305 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
309 * Common to all OSD commands
312 static void _osdv1_req_encode_common(struct osd_request *or,
313 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
315 struct osdv1_cdb *ocdb = &or->cdb.v1;
318 * For speed, the commands
319 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
320 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
321 * are not supported here. Should pass zero and set after the call
323 act &= cpu_to_be16(~0x0080); /* V1 action code */
325 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
327 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
328 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
329 ocdb->h.varlen_cdb.service_action = act;
331 ocdb->h.partition = cpu_to_be64(obj->partition);
332 ocdb->h.object = cpu_to_be64(obj->id);
333 ocdb->h.v1.length = cpu_to_be64(len);
334 ocdb->h.v1.start_address = cpu_to_be64(offset);
337 static void _osd_req_encode_common(struct osd_request *or,
338 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
340 _osdv1_req_encode_common(or, act, obj, offset, len);
346 void osd_req_format(struct osd_request *or, u64 tot_capacity)
348 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
351 EXPORT_SYMBOL(osd_req_format);
353 int osd_req_list_dev_partitions(struct osd_request *or,
354 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
356 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
358 EXPORT_SYMBOL(osd_req_list_dev_partitions);
360 static void _osd_req_encode_flush(struct osd_request *or,
361 enum osd_options_flush_scope_values op)
363 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
365 ocdb->command_specific_options = op;
368 void osd_req_flush_obsd(struct osd_request *or,
369 enum osd_options_flush_scope_values op)
371 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
372 _osd_req_encode_flush(or, op);
374 EXPORT_SYMBOL(osd_req_flush_obsd);
379 static void _osd_req_encode_partition(struct osd_request *or,
380 __be16 act, osd_id partition)
382 struct osd_obj_id par = {
383 .partition = partition,
387 _osd_req_encode_common(or, act, &par, 0, 0);
390 void osd_req_create_partition(struct osd_request *or, osd_id partition)
392 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
394 EXPORT_SYMBOL(osd_req_create_partition);
396 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
398 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
400 EXPORT_SYMBOL(osd_req_remove_partition);
402 static int _osd_req_list_objects(struct osd_request *or,
403 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
404 struct osd_obj_id_list *list, unsigned nelem)
406 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
407 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
410 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
412 if (list->list_identifier)
413 _osd_req_encode_olist(or, list);
416 bio = bio_map_kern(q, list, len, or->alloc_flags);
418 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
422 bio->bi_rw &= ~(1 << BIO_RW);
424 or->in.total_bytes = bio->bi_size;
428 int osd_req_list_partition_collections(struct osd_request *or,
429 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
432 struct osd_obj_id par = {
433 .partition = partition,
437 return osd_req_list_collection_objects(or, &par, initial_id, list,
440 EXPORT_SYMBOL(osd_req_list_partition_collections);
442 int osd_req_list_partition_objects(struct osd_request *or,
443 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
446 struct osd_obj_id par = {
447 .partition = partition,
451 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
454 EXPORT_SYMBOL(osd_req_list_partition_objects);
456 void osd_req_flush_partition(struct osd_request *or,
457 osd_id partition, enum osd_options_flush_scope_values op)
459 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
460 _osd_req_encode_flush(or, op);
462 EXPORT_SYMBOL(osd_req_flush_partition);
465 * Collection commands
467 int osd_req_list_collection_objects(struct osd_request *or,
468 const struct osd_obj_id *obj, osd_id initial_id,
469 struct osd_obj_id_list *list, unsigned nelem)
471 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
472 initial_id, list, nelem);
474 EXPORT_SYMBOL(osd_req_list_collection_objects);
476 void osd_req_flush_collection(struct osd_request *or,
477 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
479 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
480 _osd_req_encode_flush(or, op);
482 EXPORT_SYMBOL(osd_req_flush_collection);
487 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
489 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
491 EXPORT_SYMBOL(osd_req_create_object);
493 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
495 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
497 EXPORT_SYMBOL(osd_req_remove_object);
499 void osd_req_write(struct osd_request *or,
500 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
502 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
503 WARN_ON(or->out.bio || or->out.total_bytes);
504 bio->bi_rw |= (1 << BIO_RW);
506 or->out.total_bytes = bio->bi_size;
508 EXPORT_SYMBOL(osd_req_write);
510 void osd_req_flush_object(struct osd_request *or,
511 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
512 /*V2*/ u64 offset, /*V2*/ u64 len)
514 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
515 _osd_req_encode_flush(or, op);
517 EXPORT_SYMBOL(osd_req_flush_object);
519 void osd_req_read(struct osd_request *or,
520 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
522 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
523 WARN_ON(or->in.bio || or->in.total_bytes);
524 bio->bi_rw &= ~(1 << BIO_RW);
526 or->in.total_bytes = bio->bi_size;
528 EXPORT_SYMBOL(osd_req_read);
530 void osd_req_get_attributes(struct osd_request *or,
531 const struct osd_obj_id *obj)
533 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
535 EXPORT_SYMBOL(osd_req_get_attributes);
537 void osd_req_set_attributes(struct osd_request *or,
538 const struct osd_obj_id *obj)
540 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
542 EXPORT_SYMBOL(osd_req_set_attributes);
545 * Attributes List-mode
548 int osd_req_add_set_attr_list(struct osd_request *or,
549 const struct osd_attr *oa, unsigned nelem)
551 unsigned total_bytes = or->set_attr.total_bytes;
555 if (or->attributes_mode &&
556 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
560 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
562 if (!total_bytes) { /* first-time: allocate and put list header */
563 total_bytes = _osd_req_sizeof_alist_header(or);
564 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
567 _osd_req_set_alist_type(or, or->set_attr.buff,
568 OSD_ATTR_LIST_SET_RETRIEVE);
570 attr_last = or->set_attr.buff + total_bytes;
572 for (; nelem; --nelem) {
573 struct osd_attributes_list_element *attr;
574 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
576 total_bytes += elem_size;
577 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
578 or->set_attr.total_bytes = total_bytes - elem_size;
579 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
583 or->set_attr.buff + or->set_attr.total_bytes;
587 attr->attr_page = cpu_to_be32(oa->attr_page);
588 attr->attr_id = cpu_to_be32(oa->attr_id);
589 attr->attr_bytes = cpu_to_be16(oa->len);
590 memcpy(attr->attr_val, oa->val_ptr, oa->len);
592 attr_last += elem_size;
596 or->set_attr.total_bytes = total_bytes;
599 EXPORT_SYMBOL(osd_req_add_set_attr_list);
601 static int _append_map_kern(struct request *req,
602 void *buff, unsigned len, gfp_t flags)
607 bio = bio_map_kern(req->q, buff, len, flags);
609 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
613 ret = blk_rq_append_bio(req->q, req, bio);
615 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
621 static int _req_append_segment(struct osd_request *or,
622 unsigned padding, struct _osd_req_data_segment *seg,
623 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
629 /* check if we can just add it to last buffer */
631 (padding <= last_seg->alloc_size - last_seg->total_bytes))
632 pad_buff = last_seg->buff + last_seg->total_bytes;
634 pad_buff = io->pad_buff;
636 ret = _append_map_kern(io->req, pad_buff, padding,
640 io->total_bytes += padding;
643 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
648 io->total_bytes += seg->total_bytes;
649 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
654 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
656 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
660 if (!or->set_attr.total_bytes) {
661 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
665 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
666 cdbh->attrs_list.set_attr_offset =
667 osd_req_encode_offset(or, or->out.total_bytes, &padding);
669 ret = _req_append_segment(or, padding, &or->set_attr,
670 or->out.last_seg, &or->out);
674 or->out.last_seg = &or->set_attr;
678 int osd_req_add_get_attr_list(struct osd_request *or,
679 const struct osd_attr *oa, unsigned nelem)
681 unsigned total_bytes = or->enc_get_attr.total_bytes;
685 if (or->attributes_mode &&
686 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
690 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
692 /* first time calc data-in list header size */
693 if (!or->get_attr.total_bytes)
694 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
696 /* calc data-out info */
697 if (!total_bytes) { /* first-time: allocate and put list header */
700 total_bytes = _osd_req_sizeof_alist_header(or);
701 max_bytes = total_bytes +
702 nelem * sizeof(struct osd_attributes_list_attrid);
703 ret = _alloc_get_attr_desc(or, max_bytes);
707 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
710 attr_last = or->enc_get_attr.buff + total_bytes;
712 for (; nelem; --nelem) {
713 struct osd_attributes_list_attrid *attrid;
714 const unsigned cur_size = sizeof(*attrid);
716 total_bytes += cur_size;
717 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
718 or->enc_get_attr.total_bytes = total_bytes - cur_size;
719 ret = _alloc_get_attr_desc(or,
720 total_bytes + nelem * sizeof(*attrid));
723 attr_last = or->enc_get_attr.buff +
724 or->enc_get_attr.total_bytes;
728 attrid->attr_page = cpu_to_be32(oa->attr_page);
729 attrid->attr_id = cpu_to_be32(oa->attr_id);
731 attr_last += cur_size;
733 /* calc data-in size */
734 or->get_attr.total_bytes +=
735 _osd_req_alist_elem_size(or, oa->len);
739 or->enc_get_attr.total_bytes = total_bytes;
742 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
743 or->get_attr.total_bytes,
744 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
745 or->enc_get_attr.total_bytes,
746 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
747 / sizeof(struct osd_attributes_list_attrid));
751 EXPORT_SYMBOL(osd_req_add_get_attr_list);
753 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
755 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
756 unsigned out_padding;
760 if (!or->enc_get_attr.total_bytes) {
761 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
762 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
766 ret = _alloc_get_attr_list(or);
770 /* The out-going buffer info update */
771 OSD_DEBUG("out-going\n");
772 cdbh->attrs_list.get_attr_desc_bytes =
773 cpu_to_be32(or->enc_get_attr.total_bytes);
775 cdbh->attrs_list.get_attr_desc_offset =
776 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
778 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
779 or->out.last_seg, &or->out);
782 or->out.last_seg = &or->enc_get_attr;
784 /* The incoming buffer info update */
785 OSD_DEBUG("in-coming\n");
786 cdbh->attrs_list.get_attr_alloc_length =
787 cpu_to_be32(or->get_attr.total_bytes);
789 cdbh->attrs_list.get_attr_offset =
790 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
792 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
796 or->in.last_seg = &or->get_attr;
801 int osd_req_decode_get_attr_list(struct osd_request *or,
802 struct osd_attr *oa, int *nelem, void **iterator)
804 unsigned cur_bytes, returned_bytes;
806 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
809 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
810 OSD_ATTR_LIST_SET_RETRIEVE)) {
820 BUG_ON((*iterator < or->get_attr.buff) ||
821 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
823 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
824 returned_bytes = or->get_attr.total_bytes;
825 } else { /* first time decode the list header */
826 cur_bytes = sizeof_attr_list;
827 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
830 cur_p = or->get_attr.buff + sizeof_attr_list;
832 if (returned_bytes > or->get_attr.alloc_size) {
833 OSD_DEBUG("target report: space was not big enough! "
834 "Allocate=%u Needed=%u\n",
835 or->get_attr.alloc_size,
836 returned_bytes + sizeof_attr_list);
839 or->get_attr.alloc_size - sizeof_attr_list;
841 or->get_attr.total_bytes = returned_bytes;
844 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
845 struct osd_attributes_list_element *attr = cur_p;
848 oa->len = be16_to_cpu(attr->attr_bytes);
849 inc = _osd_req_alist_elem_size(or, oa->len);
850 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
851 oa->len, inc, cur_bytes);
853 if (cur_bytes > returned_bytes) {
854 OSD_ERR("BAD FOOD from target. list not valid!"
856 cur_bytes, returned_bytes, n);
861 oa->attr_page = be32_to_cpu(attr->attr_page);
862 oa->attr_id = be32_to_cpu(attr->attr_id);
863 oa->val_ptr = attr->attr_val;
869 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
871 return returned_bytes - cur_bytes;
873 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
876 * Attributes Page-mode
879 int osd_req_add_get_attr_page(struct osd_request *or,
880 u32 page_id, void *attar_page, unsigned max_page_len,
881 const struct osd_attr *set_one_attr)
883 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
885 if (or->attributes_mode &&
886 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
890 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
892 or->get_attr.buff = attar_page;
893 or->get_attr.total_bytes = max_page_len;
895 or->set_attr.buff = set_one_attr->val_ptr;
896 or->set_attr.total_bytes = set_one_attr->len;
898 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
899 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
900 /* ocdb->attrs_page.get_attr_offset; */
902 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
903 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
904 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
905 /* ocdb->attrs_page.set_attr_offset; */
908 EXPORT_SYMBOL(osd_req_add_get_attr_page);
910 static int _osd_req_finalize_attr_page(struct osd_request *or)
912 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
913 unsigned in_padding, out_padding;
917 cdbh->attrs_page.get_attr_offset =
918 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
920 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
926 cdbh->attrs_page.set_attr_offset =
927 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
929 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
934 static int _osd_req_finalize_data_integrity(struct osd_request *or,
935 bool has_in, bool has_out, const u8 *cap_key)
937 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
940 if (!osd_is_sec_alldata(sec_parms))
944 struct _osd_req_data_segment seg = {
945 .buff = &or->out_data_integ,
946 .total_bytes = sizeof(or->out_data_integ),
950 or->out_data_integ.data_bytes = cpu_to_be64(
951 or->out.bio ? or->out.bio->bi_size : 0);
952 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
953 or->set_attr.total_bytes);
954 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
955 or->enc_get_attr.total_bytes);
957 sec_parms->data_out_integrity_check_offset =
958 osd_req_encode_offset(or, or->out.total_bytes, &pad);
960 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
964 or->out.last_seg = NULL;
966 /* they are now all chained to request sign them all together */
967 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
972 struct _osd_req_data_segment seg = {
973 .buff = &or->in_data_integ,
974 .total_bytes = sizeof(or->in_data_integ),
978 sec_parms->data_in_integrity_check_offset =
979 osd_req_encode_offset(or, or->in.total_bytes, &pad);
981 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
986 or->in.last_seg = NULL;
993 * osd_finalize_request and helpers
996 static int _init_blk_request(struct osd_request *or,
997 bool has_in, bool has_out)
999 gfp_t flags = or->alloc_flags;
1000 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1001 struct request_queue *q = scsi_device->request_queue;
1002 struct request *req;
1005 req = blk_get_request(q, has_out, flags);
1010 req->cmd_type = REQ_TYPE_BLOCK_PC;
1011 req->timeout = or->timeout;
1012 req->retries = or->retries;
1013 req->sense = or->sense;
1019 /* allocate bidi request */
1020 req = blk_get_request(q, READ, flags);
1022 OSD_DEBUG("blk_get_request for bidi failed\n");
1025 req->cmd_type = REQ_TYPE_BLOCK_PC;
1026 or->in.req = or->request->next_rq = req;
1033 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1034 or, has_in, has_out, ret, or->request);
1038 int osd_finalize_request(struct osd_request *or,
1039 u8 options, const void *cap, const u8 *cap_key)
1041 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1042 bool has_in, has_out;
1045 if (options & OSD_REQ_FUA)
1046 cdbh->options |= OSD_CDB_FUA;
1048 if (options & OSD_REQ_DPO)
1049 cdbh->options |= OSD_CDB_DPO;
1051 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1052 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1054 osd_set_caps(&or->cdb, cap);
1056 has_in = or->in.bio || or->get_attr.total_bytes;
1057 has_out = or->out.bio || or->set_attr.total_bytes ||
1058 or->enc_get_attr.total_bytes;
1060 ret = _init_blk_request(or, has_in, has_out);
1062 OSD_DEBUG("_init_blk_request failed\n");
1067 ret = blk_rq_append_bio(or->request->q, or->out.req,
1070 OSD_DEBUG("blk_rq_append_bio out failed\n");
1073 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1074 _LLU(or->out.total_bytes), or->out.req->data_len);
1077 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1079 OSD_DEBUG("blk_rq_append_bio in failed\n");
1082 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1083 _LLU(or->in.total_bytes), or->in.req->data_len);
1086 or->out.pad_buff = sg_out_pad_buffer;
1087 or->in.pad_buff = sg_in_pad_buffer;
1089 if (!or->attributes_mode)
1090 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1091 cdbh->command_specific_options |= or->attributes_mode;
1092 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1093 ret = _osd_req_finalize_attr_page(or);
1095 /* TODO: I think that for the GET_ATTR command these 2 should
1096 * be reversed to keep them in execution order (for embeded
1097 * targets with low memory footprint)
1099 ret = _osd_req_finalize_set_attr_list(or);
1101 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1105 ret = _osd_req_finalize_get_attr_list(or);
1107 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1112 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1116 osd_sec_sign_cdb(&or->cdb, cap_key);
1118 or->request->cmd = or->cdb.buff;
1119 or->request->cmd_len = _osd_req_cdb_len(or);
1123 EXPORT_SYMBOL(osd_finalize_request);
1126 * Implementation of osd_sec.h API
1127 * TODO: Move to a separate osd_sec.c file at a later stage.
1130 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1131 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1132 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1133 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1134 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1137 void osd_sec_init_nosec_doall_caps(void *caps,
1138 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1140 struct osd_capability *cap = caps;
1144 if (likely(obj->id)) {
1145 if (unlikely(is_collection)) {
1146 type = OSD_SEC_OBJ_COLLECTION;
1147 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1148 OSD_SEC_OBJ_DESC_COL;
1150 type = OSD_SEC_OBJ_USER;
1151 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1153 WARN_ON(!obj->partition);
1155 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1157 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1160 memset(cap, 0, sizeof(*cap));
1162 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1163 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1164 cap->h.security_method = OSD_SEC_NOSEC;
1165 /* cap->expiration_time;
1167 cap->discriminator[42-30];
1168 cap->object_created_time; */
1169 cap->h.object_type = type;
1170 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1171 cap->h.object_descriptor_type = descriptor_type;
1172 cap->od.obj_desc.policy_access_tag = 0;
1173 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1174 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1176 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1178 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1180 memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
1183 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1188 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1192 void osd_sec_sign_data(void *data_integ __unused,
1193 struct bio *bio __unused, const u8 *cap_key __unused)
1198 * Declared in osd_protocol.h
1199 * 4.12.5 Data-In and Data-Out buffer offsets
1200 * byte offset = mantissa * (2^(exponent+8))
1201 * Returns the smallest allowed encoded offset that contains given @offset
1202 * The actual encoded offset returned is @offset + *@padding.
1204 osd_cdb_offset __osd_encode_offset(
1205 u64 offset, unsigned *padding, int min_shift, int max_shift)
1207 u64 try_offset = -1, mod, align;
1208 osd_cdb_offset be32_offset;
1215 for (shift = min_shift; shift < max_shift; ++shift) {
1216 try_offset = offset >> shift;
1217 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1221 BUG_ON(shift == max_shift);
1224 mod = offset & (align - 1);
1226 *padding = align - mod;
1230 try_offset |= ((shift - 8) & 0xf) << 28;
1231 be32_offset = cpu_to_be32((u32)try_offset);
1233 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1234 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1235 be32_offset, *padding);