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 osdv2_cdb) != OSD_TOTAL_CDB_LEN);
63 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
66 static unsigned _osd_req_cdb_len(struct osd_request *or)
68 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
71 static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
73 return osd_req_is_ver1(or) ?
74 osdv1_attr_list_elem_size(len) :
75 osdv2_attr_list_elem_size(len);
78 static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
80 return osd_req_is_ver1(or) ?
81 osdv1_list_size(list_head) :
82 osdv2_list_size(list_head);
85 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
87 return osd_req_is_ver1(or) ?
88 sizeof(struct osdv1_attributes_list_header) :
89 sizeof(struct osdv2_attributes_list_header);
92 static void _osd_req_set_alist_type(struct osd_request *or,
93 void *list, int list_type)
95 if (osd_req_is_ver1(or)) {
96 struct osdv1_attributes_list_header *attr_list = list;
98 memset(attr_list, 0, sizeof(*attr_list));
99 attr_list->type = list_type;
101 struct osdv2_attributes_list_header *attr_list = list;
103 memset(attr_list, 0, sizeof(*attr_list));
104 attr_list->type = list_type;
108 static bool _osd_req_is_alist_type(struct osd_request *or,
109 void *list, int list_type)
114 if (osd_req_is_ver1(or)) {
115 struct osdv1_attributes_list_header *attr_list = list;
117 return attr_list->type == list_type;
119 struct osdv2_attributes_list_header *attr_list = list;
121 return attr_list->type == list_type;
125 /* This is for List-objects not Attributes-Lists */
126 static void _osd_req_encode_olist(struct osd_request *or,
127 struct osd_obj_id_list *list)
129 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
131 if (osd_req_is_ver1(or)) {
132 cdbh->v1.list_identifier = list->list_identifier;
133 cdbh->v1.start_address = list->continuation_id;
135 cdbh->v2.list_identifier = list->list_identifier;
136 cdbh->v2.start_address = list->continuation_id;
140 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
141 u64 offset, unsigned *padding)
143 return __osd_encode_offset(offset, padding,
144 osd_req_is_ver1(or) ?
145 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
146 OSD_OFFSET_MAX_SHIFT);
149 static struct osd_security_parameters *
150 _osd_req_sec_params(struct osd_request *or)
152 struct osd_cdb *ocdb = &or->cdb;
154 if (osd_req_is_ver1(or))
155 return &ocdb->v1.sec_params;
157 return &ocdb->v2.sec_params;
160 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
162 memset(osdd, 0, sizeof(*osdd));
163 osdd->scsi_device = scsi_device;
164 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
165 #ifdef OSD_VER1_SUPPORT
166 osdd->version = OSD_VER2;
168 /* TODO: Allocate pools for osd_request attributes ... */
170 EXPORT_SYMBOL(osd_dev_init);
172 void osd_dev_fini(struct osd_dev *osdd)
174 /* TODO: De-allocate pools */
176 osdd->scsi_device = NULL;
178 EXPORT_SYMBOL(osd_dev_fini);
180 static struct osd_request *_osd_request_alloc(gfp_t gfp)
182 struct osd_request *or;
184 /* TODO: Use mempool with one saved request */
185 or = kzalloc(sizeof(*or), gfp);
189 static void _osd_request_free(struct osd_request *or)
194 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
196 struct osd_request *or;
198 or = _osd_request_alloc(gfp);
203 or->alloc_flags = gfp;
204 or->timeout = dev->def_timeout;
205 or->retries = OSD_REQ_RETRIES;
209 EXPORT_SYMBOL(osd_start_request);
212 * If osd_finalize_request() was called but the request was not executed through
213 * the block layer, then we must release BIOs.
215 static void _abort_unexecuted_bios(struct request *rq)
219 while ((bio = rq->bio) != NULL) {
220 rq->bio = bio->bi_next;
225 static void _osd_free_seg(struct osd_request *or __unused,
226 struct _osd_req_data_segment *seg)
228 if (!seg->buff || !seg->alloc_size)
236 void osd_end_request(struct osd_request *or)
238 struct request *rq = or->request;
240 _osd_free_seg(or, &or->set_attr);
241 _osd_free_seg(or, &or->enc_get_attr);
242 _osd_free_seg(or, &or->get_attr);
246 _abort_unexecuted_bios(rq->next_rq);
247 blk_put_request(rq->next_rq);
250 _abort_unexecuted_bios(rq);
253 _osd_request_free(or);
255 EXPORT_SYMBOL(osd_end_request);
257 int osd_execute_request(struct osd_request *or)
259 return blk_execute_rq(or->request->q, NULL, or->request, 0);
261 EXPORT_SYMBOL(osd_execute_request);
263 static void osd_request_async_done(struct request *req, int error)
265 struct osd_request *or = req->end_io_data;
267 or->async_error = error;
270 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
273 or->async_done(or, or->async_private);
278 int osd_execute_request_async(struct osd_request *or,
279 osd_req_done_fn *done, void *private)
281 or->request->end_io_data = or;
282 or->async_private = private;
283 or->async_done = done;
285 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
286 osd_request_async_done);
289 EXPORT_SYMBOL(osd_execute_request_async);
291 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
292 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
294 static int _osd_realloc_seg(struct osd_request *or,
295 struct _osd_req_data_segment *seg, unsigned max_bytes)
299 if (seg->alloc_size >= max_bytes)
302 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
304 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
309 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
311 seg->alloc_size = max_bytes;
315 static int _alloc_set_attr_list(struct osd_request *or,
316 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
318 unsigned total_bytes = add_bytes;
320 for (; nelem; --nelem, ++oa)
321 total_bytes += _osd_req_alist_elem_size(or, oa->len);
323 OSD_DEBUG("total_bytes=%d\n", total_bytes);
324 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
327 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
329 OSD_DEBUG("total_bytes=%d\n", max_bytes);
330 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
333 static int _alloc_get_attr_list(struct osd_request *or)
335 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
336 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
340 * Common to all OSD commands
343 static void _osdv1_req_encode_common(struct osd_request *or,
344 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
346 struct osdv1_cdb *ocdb = &or->cdb.v1;
349 * For speed, the commands
350 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
351 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
352 * are not supported here. Should pass zero and set after the call
354 act &= cpu_to_be16(~0x0080); /* V1 action code */
356 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
358 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
359 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
360 ocdb->h.varlen_cdb.service_action = act;
362 ocdb->h.partition = cpu_to_be64(obj->partition);
363 ocdb->h.object = cpu_to_be64(obj->id);
364 ocdb->h.v1.length = cpu_to_be64(len);
365 ocdb->h.v1.start_address = cpu_to_be64(offset);
368 static void _osdv2_req_encode_common(struct osd_request *or,
369 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
371 struct osdv2_cdb *ocdb = &or->cdb.v2;
373 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
375 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
376 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
377 ocdb->h.varlen_cdb.service_action = act;
379 ocdb->h.partition = cpu_to_be64(obj->partition);
380 ocdb->h.object = cpu_to_be64(obj->id);
381 ocdb->h.v2.length = cpu_to_be64(len);
382 ocdb->h.v2.start_address = cpu_to_be64(offset);
385 static void _osd_req_encode_common(struct osd_request *or,
386 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
388 if (osd_req_is_ver1(or))
389 _osdv1_req_encode_common(or, act, obj, offset, len);
391 _osdv2_req_encode_common(or, act, obj, offset, len);
397 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
398 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
400 void osd_req_format(struct osd_request *or, u64 tot_capacity)
402 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
405 EXPORT_SYMBOL(osd_req_format);
407 int osd_req_list_dev_partitions(struct osd_request *or,
408 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
410 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
412 EXPORT_SYMBOL(osd_req_list_dev_partitions);
414 static void _osd_req_encode_flush(struct osd_request *or,
415 enum osd_options_flush_scope_values op)
417 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
419 ocdb->command_specific_options = op;
422 void osd_req_flush_obsd(struct osd_request *or,
423 enum osd_options_flush_scope_values op)
425 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
426 _osd_req_encode_flush(or, op);
428 EXPORT_SYMBOL(osd_req_flush_obsd);
430 /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
431 const u8 *cdb, ...); */
432 /*TODO: void osd_req_task_management(struct osd_request *, ...); */
437 static void _osd_req_encode_partition(struct osd_request *or,
438 __be16 act, osd_id partition)
440 struct osd_obj_id par = {
441 .partition = partition,
445 _osd_req_encode_common(or, act, &par, 0, 0);
448 void osd_req_create_partition(struct osd_request *or, osd_id partition)
450 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
452 EXPORT_SYMBOL(osd_req_create_partition);
454 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
456 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
458 EXPORT_SYMBOL(osd_req_remove_partition);
460 /*TODO: void osd_req_set_partition_key(struct osd_request *,
461 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
462 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
464 static int _osd_req_list_objects(struct osd_request *or,
465 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
466 struct osd_obj_id_list *list, unsigned nelem)
468 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
469 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
472 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
474 if (list->list_identifier)
475 _osd_req_encode_olist(or, list);
478 bio = bio_map_kern(q, list, len, or->alloc_flags);
480 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
484 bio->bi_rw &= ~(1 << BIO_RW);
486 or->in.total_bytes = bio->bi_size;
490 int osd_req_list_partition_collections(struct osd_request *or,
491 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
494 struct osd_obj_id par = {
495 .partition = partition,
499 return osd_req_list_collection_objects(or, &par, initial_id, list,
502 EXPORT_SYMBOL(osd_req_list_partition_collections);
504 int osd_req_list_partition_objects(struct osd_request *or,
505 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
508 struct osd_obj_id par = {
509 .partition = partition,
513 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
516 EXPORT_SYMBOL(osd_req_list_partition_objects);
518 void osd_req_flush_partition(struct osd_request *or,
519 osd_id partition, enum osd_options_flush_scope_values op)
521 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
522 _osd_req_encode_flush(or, op);
524 EXPORT_SYMBOL(osd_req_flush_partition);
527 * Collection commands
529 /*TODO: void osd_req_create_collection(struct osd_request *,
530 const struct osd_obj_id *); */
531 /*TODO: void osd_req_remove_collection(struct osd_request *,
532 const struct osd_obj_id *); */
534 int osd_req_list_collection_objects(struct osd_request *or,
535 const struct osd_obj_id *obj, osd_id initial_id,
536 struct osd_obj_id_list *list, unsigned nelem)
538 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
539 initial_id, list, nelem);
541 EXPORT_SYMBOL(osd_req_list_collection_objects);
543 /*TODO: void query(struct osd_request *, ...); V2 */
545 void osd_req_flush_collection(struct osd_request *or,
546 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
548 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
549 _osd_req_encode_flush(or, op);
551 EXPORT_SYMBOL(osd_req_flush_collection);
553 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
554 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
559 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
561 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
563 EXPORT_SYMBOL(osd_req_create_object);
565 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
567 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
569 EXPORT_SYMBOL(osd_req_remove_object);
572 /*TODO: void osd_req_create_multi(struct osd_request *or,
573 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
576 void osd_req_write(struct osd_request *or,
577 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
579 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
580 WARN_ON(or->out.bio || or->out.total_bytes);
581 bio->bi_rw |= (1 << BIO_RW);
583 or->out.total_bytes = bio->bi_size;
585 EXPORT_SYMBOL(osd_req_write);
587 /*TODO: void osd_req_append(struct osd_request *,
588 const struct osd_obj_id *, struct bio *data_out); */
589 /*TODO: void osd_req_create_write(struct osd_request *,
590 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
591 /*TODO: void osd_req_clear(struct osd_request *,
592 const struct osd_obj_id *, u64 offset, u64 len); */
593 /*TODO: void osd_req_punch(struct osd_request *,
594 const struct osd_obj_id *, u64 offset, u64 len); V2 */
596 void osd_req_flush_object(struct osd_request *or,
597 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
598 /*V2*/ u64 offset, /*V2*/ u64 len)
600 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
601 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
606 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
607 _osd_req_encode_flush(or, op);
609 EXPORT_SYMBOL(osd_req_flush_object);
611 void osd_req_read(struct osd_request *or,
612 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
614 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
615 WARN_ON(or->in.bio || or->in.total_bytes);
616 bio->bi_rw &= ~(1 << BIO_RW);
618 or->in.total_bytes = bio->bi_size;
620 EXPORT_SYMBOL(osd_req_read);
622 void osd_req_get_attributes(struct osd_request *or,
623 const struct osd_obj_id *obj)
625 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
627 EXPORT_SYMBOL(osd_req_get_attributes);
629 void osd_req_set_attributes(struct osd_request *or,
630 const struct osd_obj_id *obj)
632 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
634 EXPORT_SYMBOL(osd_req_set_attributes);
637 * Attributes List-mode
640 int osd_req_add_set_attr_list(struct osd_request *or,
641 const struct osd_attr *oa, unsigned nelem)
643 unsigned total_bytes = or->set_attr.total_bytes;
647 if (or->attributes_mode &&
648 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
652 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
654 if (!total_bytes) { /* first-time: allocate and put list header */
655 total_bytes = _osd_req_sizeof_alist_header(or);
656 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
659 _osd_req_set_alist_type(or, or->set_attr.buff,
660 OSD_ATTR_LIST_SET_RETRIEVE);
662 attr_last = or->set_attr.buff + total_bytes;
664 for (; nelem; --nelem) {
665 struct osd_attributes_list_element *attr;
666 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
668 total_bytes += elem_size;
669 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
670 or->set_attr.total_bytes = total_bytes - elem_size;
671 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
675 or->set_attr.buff + or->set_attr.total_bytes;
679 attr->attr_page = cpu_to_be32(oa->attr_page);
680 attr->attr_id = cpu_to_be32(oa->attr_id);
681 attr->attr_bytes = cpu_to_be16(oa->len);
682 memcpy(attr->attr_val, oa->val_ptr, oa->len);
684 attr_last += elem_size;
688 or->set_attr.total_bytes = total_bytes;
691 EXPORT_SYMBOL(osd_req_add_set_attr_list);
693 static int _append_map_kern(struct request *req,
694 void *buff, unsigned len, gfp_t flags)
699 bio = bio_map_kern(req->q, buff, len, flags);
701 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
705 ret = blk_rq_append_bio(req->q, req, bio);
707 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
713 static int _req_append_segment(struct osd_request *or,
714 unsigned padding, struct _osd_req_data_segment *seg,
715 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
721 /* check if we can just add it to last buffer */
723 (padding <= last_seg->alloc_size - last_seg->total_bytes))
724 pad_buff = last_seg->buff + last_seg->total_bytes;
726 pad_buff = io->pad_buff;
728 ret = _append_map_kern(io->req, pad_buff, padding,
732 io->total_bytes += padding;
735 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
740 io->total_bytes += seg->total_bytes;
741 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
746 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
748 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
752 if (!or->set_attr.total_bytes) {
753 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
757 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
758 cdbh->attrs_list.set_attr_offset =
759 osd_req_encode_offset(or, or->out.total_bytes, &padding);
761 ret = _req_append_segment(or, padding, &or->set_attr,
762 or->out.last_seg, &or->out);
766 or->out.last_seg = &or->set_attr;
770 int osd_req_add_get_attr_list(struct osd_request *or,
771 const struct osd_attr *oa, unsigned nelem)
773 unsigned total_bytes = or->enc_get_attr.total_bytes;
777 if (or->attributes_mode &&
778 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
782 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
784 /* first time calc data-in list header size */
785 if (!or->get_attr.total_bytes)
786 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
788 /* calc data-out info */
789 if (!total_bytes) { /* first-time: allocate and put list header */
792 total_bytes = _osd_req_sizeof_alist_header(or);
793 max_bytes = total_bytes +
794 nelem * sizeof(struct osd_attributes_list_attrid);
795 ret = _alloc_get_attr_desc(or, max_bytes);
799 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
802 attr_last = or->enc_get_attr.buff + total_bytes;
804 for (; nelem; --nelem) {
805 struct osd_attributes_list_attrid *attrid;
806 const unsigned cur_size = sizeof(*attrid);
808 total_bytes += cur_size;
809 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
810 or->enc_get_attr.total_bytes = total_bytes - cur_size;
811 ret = _alloc_get_attr_desc(or,
812 total_bytes + nelem * sizeof(*attrid));
815 attr_last = or->enc_get_attr.buff +
816 or->enc_get_attr.total_bytes;
820 attrid->attr_page = cpu_to_be32(oa->attr_page);
821 attrid->attr_id = cpu_to_be32(oa->attr_id);
823 attr_last += cur_size;
825 /* calc data-in size */
826 or->get_attr.total_bytes +=
827 _osd_req_alist_elem_size(or, oa->len);
831 or->enc_get_attr.total_bytes = total_bytes;
834 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
835 or->get_attr.total_bytes,
836 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
837 or->enc_get_attr.total_bytes,
838 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
839 / sizeof(struct osd_attributes_list_attrid));
843 EXPORT_SYMBOL(osd_req_add_get_attr_list);
845 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
847 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
848 unsigned out_padding;
852 if (!or->enc_get_attr.total_bytes) {
853 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
854 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
858 ret = _alloc_get_attr_list(or);
862 /* The out-going buffer info update */
863 OSD_DEBUG("out-going\n");
864 cdbh->attrs_list.get_attr_desc_bytes =
865 cpu_to_be32(or->enc_get_attr.total_bytes);
867 cdbh->attrs_list.get_attr_desc_offset =
868 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
870 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
871 or->out.last_seg, &or->out);
874 or->out.last_seg = &or->enc_get_attr;
876 /* The incoming buffer info update */
877 OSD_DEBUG("in-coming\n");
878 cdbh->attrs_list.get_attr_alloc_length =
879 cpu_to_be32(or->get_attr.total_bytes);
881 cdbh->attrs_list.get_attr_offset =
882 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
884 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
888 or->in.last_seg = &or->get_attr;
893 int osd_req_decode_get_attr_list(struct osd_request *or,
894 struct osd_attr *oa, int *nelem, void **iterator)
896 unsigned cur_bytes, returned_bytes;
898 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
901 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
902 OSD_ATTR_LIST_SET_RETRIEVE)) {
912 BUG_ON((*iterator < or->get_attr.buff) ||
913 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
915 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
916 returned_bytes = or->get_attr.total_bytes;
917 } else { /* first time decode the list header */
918 cur_bytes = sizeof_attr_list;
919 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
922 cur_p = or->get_attr.buff + sizeof_attr_list;
924 if (returned_bytes > or->get_attr.alloc_size) {
925 OSD_DEBUG("target report: space was not big enough! "
926 "Allocate=%u Needed=%u\n",
927 or->get_attr.alloc_size,
928 returned_bytes + sizeof_attr_list);
931 or->get_attr.alloc_size - sizeof_attr_list;
933 or->get_attr.total_bytes = returned_bytes;
936 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
937 struct osd_attributes_list_element *attr = cur_p;
940 oa->len = be16_to_cpu(attr->attr_bytes);
941 inc = _osd_req_alist_elem_size(or, oa->len);
942 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
943 oa->len, inc, cur_bytes);
945 if (cur_bytes > returned_bytes) {
946 OSD_ERR("BAD FOOD from target. list not valid!"
948 cur_bytes, returned_bytes, n);
953 oa->attr_page = be32_to_cpu(attr->attr_page);
954 oa->attr_id = be32_to_cpu(attr->attr_id);
955 oa->val_ptr = attr->attr_val;
961 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
963 return returned_bytes - cur_bytes;
965 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
968 * Attributes Page-mode
971 int osd_req_add_get_attr_page(struct osd_request *or,
972 u32 page_id, void *attar_page, unsigned max_page_len,
973 const struct osd_attr *set_one_attr)
975 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
977 if (or->attributes_mode &&
978 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
982 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
984 or->get_attr.buff = attar_page;
985 or->get_attr.total_bytes = max_page_len;
987 or->set_attr.buff = set_one_attr->val_ptr;
988 or->set_attr.total_bytes = set_one_attr->len;
990 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
991 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
992 /* ocdb->attrs_page.get_attr_offset; */
994 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
995 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
996 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
997 /* ocdb->attrs_page.set_attr_offset; */
1000 EXPORT_SYMBOL(osd_req_add_get_attr_page);
1002 static int _osd_req_finalize_attr_page(struct osd_request *or)
1004 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1005 unsigned in_padding, out_padding;
1009 cdbh->attrs_page.get_attr_offset =
1010 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1012 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1018 cdbh->attrs_page.set_attr_offset =
1019 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1021 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1026 static int _osd_req_finalize_data_integrity(struct osd_request *or,
1027 bool has_in, bool has_out, const u8 *cap_key)
1029 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1032 if (!osd_is_sec_alldata(sec_parms))
1036 struct _osd_req_data_segment seg = {
1037 .buff = &or->out_data_integ,
1038 .total_bytes = sizeof(or->out_data_integ),
1042 or->out_data_integ.data_bytes = cpu_to_be64(
1043 or->out.bio ? or->out.bio->bi_size : 0);
1044 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1045 or->set_attr.total_bytes);
1046 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1047 or->enc_get_attr.total_bytes);
1049 sec_parms->data_out_integrity_check_offset =
1050 osd_req_encode_offset(or, or->out.total_bytes, &pad);
1052 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1056 or->out.last_seg = NULL;
1058 /* they are now all chained to request sign them all together */
1059 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1064 struct _osd_req_data_segment seg = {
1065 .buff = &or->in_data_integ,
1066 .total_bytes = sizeof(or->in_data_integ),
1070 sec_parms->data_in_integrity_check_offset =
1071 osd_req_encode_offset(or, or->in.total_bytes, &pad);
1073 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1078 or->in.last_seg = NULL;
1085 * osd_finalize_request and helpers
1088 static int _init_blk_request(struct osd_request *or,
1089 bool has_in, bool has_out)
1091 gfp_t flags = or->alloc_flags;
1092 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1093 struct request_queue *q = scsi_device->request_queue;
1094 struct request *req;
1097 req = blk_get_request(q, has_out, flags);
1102 req->cmd_type = REQ_TYPE_BLOCK_PC;
1103 req->timeout = or->timeout;
1104 req->retries = or->retries;
1105 req->sense = or->sense;
1111 /* allocate bidi request */
1112 req = blk_get_request(q, READ, flags);
1114 OSD_DEBUG("blk_get_request for bidi failed\n");
1117 req->cmd_type = REQ_TYPE_BLOCK_PC;
1118 or->in.req = or->request->next_rq = req;
1125 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1126 or, has_in, has_out, ret, or->request);
1130 int osd_finalize_request(struct osd_request *or,
1131 u8 options, const void *cap, const u8 *cap_key)
1133 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1134 bool has_in, has_out;
1137 if (options & OSD_REQ_FUA)
1138 cdbh->options |= OSD_CDB_FUA;
1140 if (options & OSD_REQ_DPO)
1141 cdbh->options |= OSD_CDB_DPO;
1143 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1144 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1146 osd_set_caps(&or->cdb, cap);
1148 has_in = or->in.bio || or->get_attr.total_bytes;
1149 has_out = or->out.bio || or->set_attr.total_bytes ||
1150 or->enc_get_attr.total_bytes;
1152 ret = _init_blk_request(or, has_in, has_out);
1154 OSD_DEBUG("_init_blk_request failed\n");
1159 ret = blk_rq_append_bio(or->request->q, or->out.req,
1162 OSD_DEBUG("blk_rq_append_bio out failed\n");
1165 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1166 _LLU(or->out.total_bytes), or->out.req->data_len);
1169 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1171 OSD_DEBUG("blk_rq_append_bio in failed\n");
1174 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1175 _LLU(or->in.total_bytes), or->in.req->data_len);
1178 or->out.pad_buff = sg_out_pad_buffer;
1179 or->in.pad_buff = sg_in_pad_buffer;
1181 if (!or->attributes_mode)
1182 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1183 cdbh->command_specific_options |= or->attributes_mode;
1184 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1185 ret = _osd_req_finalize_attr_page(or);
1187 /* TODO: I think that for the GET_ATTR command these 2 should
1188 * be reversed to keep them in execution order (for embeded
1189 * targets with low memory footprint)
1191 ret = _osd_req_finalize_set_attr_list(or);
1193 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1197 ret = _osd_req_finalize_get_attr_list(or);
1199 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1204 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1208 osd_sec_sign_cdb(&or->cdb, cap_key);
1210 or->request->cmd = or->cdb.buff;
1211 or->request->cmd_len = _osd_req_cdb_len(or);
1215 EXPORT_SYMBOL(osd_finalize_request);
1218 * Implementation of osd_sec.h API
1219 * TODO: Move to a separate osd_sec.c file at a later stage.
1222 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1223 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1224 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1225 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1226 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1229 enum { OSD_SEC_CAP_V2_ALL_CAPS =
1230 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1233 void osd_sec_init_nosec_doall_caps(void *caps,
1234 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1236 struct osd_capability *cap = caps;
1240 if (likely(obj->id)) {
1241 if (unlikely(is_collection)) {
1242 type = OSD_SEC_OBJ_COLLECTION;
1243 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1244 OSD_SEC_OBJ_DESC_COL;
1246 type = OSD_SEC_OBJ_USER;
1247 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1249 WARN_ON(!obj->partition);
1251 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1253 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1256 memset(cap, 0, sizeof(*cap));
1258 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1259 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1260 cap->h.security_method = OSD_SEC_NOSEC;
1261 /* cap->expiration_time;
1263 cap->discriminator[42-30];
1264 cap->object_created_time; */
1265 cap->h.object_type = type;
1266 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1267 cap->h.object_descriptor_type = descriptor_type;
1268 cap->od.obj_desc.policy_access_tag = 0;
1269 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1270 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1272 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1274 /* FIXME: Extract version from caps pointer.
1275 * Also Pete's target only supports caps from OSDv1 for now
1277 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1279 bool is_ver1 = true;
1280 /* NOTE: They start at same address */
1281 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
1284 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1289 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1293 void osd_sec_sign_data(void *data_integ __unused,
1294 struct bio *bio __unused, const u8 *cap_key __unused)
1299 * Declared in osd_protocol.h
1300 * 4.12.5 Data-In and Data-Out buffer offsets
1301 * byte offset = mantissa * (2^(exponent+8))
1302 * Returns the smallest allowed encoded offset that contains given @offset
1303 * The actual encoded offset returned is @offset + *@padding.
1305 osd_cdb_offset __osd_encode_offset(
1306 u64 offset, unsigned *padding, int min_shift, int max_shift)
1308 u64 try_offset = -1, mod, align;
1309 osd_cdb_offset be32_offset;
1316 for (shift = min_shift; shift < max_shift; ++shift) {
1317 try_offset = offset >> shift;
1318 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1322 BUG_ON(shift == max_shift);
1325 mod = offset & (align - 1);
1327 *padding = align - mod;
1331 try_offset |= ((shift - 8) & 0xf) << 28;
1332 be32_offset = cpu_to_be32((u32)try_offset);
1334 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1335 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1336 be32_offset, *padding);