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 <ooo@electrozaur.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 <linux/slab.h>
43 #include <linux/module.h>
45 #include <scsi/osd_initiator.h>
46 #include <scsi/osd_sec.h>
47 #include <scsi/osd_attributes.h>
48 #include <scsi/osd_sense.h>
50 #include <scsi/scsi_device.h>
51 #include <scsi/scsi_request.h>
53 #include "osd_debug.h"
56 # define __unused __attribute__((unused))
59 enum { OSD_REQ_RETRIES = 1 };
61 MODULE_AUTHOR("Boaz Harrosh <ooo@electrozaur.com>");
62 MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
63 MODULE_LICENSE("GPL");
65 static inline void build_test(void)
67 /* structures were not packed */
68 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
69 BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
70 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
73 static const char *_osd_ver_desc(struct osd_request *or)
75 return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
78 #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
80 static int _osd_get_print_system_info(struct osd_dev *od,
81 void *caps, struct osd_dev_info *odi)
83 struct osd_request *or;
84 struct osd_attr get_attrs[] = {
85 ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
86 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
87 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
88 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
89 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
90 ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
91 ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
92 ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
93 ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
94 ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
95 /* IBM-OSD-SIM Has a bug with this one put it last */
96 ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
98 void *iter = NULL, *pFirst;
99 int nelem = ARRAY_SIZE(get_attrs), a = 0;
102 or = osd_start_request(od, GFP_KERNEL);
107 osd_req_get_attributes(or, &osd_root_object);
108 osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
110 ret = osd_finalize_request(or, 0, caps, NULL);
114 ret = osd_execute_request(or);
116 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
120 osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
122 OSD_INFO("Detected %s device\n",
125 pFirst = get_attrs[a++].val_ptr;
126 OSD_INFO("VENDOR_IDENTIFICATION [%s]\n",
129 pFirst = get_attrs[a++].val_ptr;
130 OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
133 pFirst = get_attrs[a++].val_ptr;
134 OSD_INFO("PRODUCT_MODEL [%s]\n",
137 pFirst = get_attrs[a++].val_ptr;
138 OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
139 pFirst ? get_unaligned_be32(pFirst) : ~0U);
141 pFirst = get_attrs[a++].val_ptr;
142 OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
145 odi->osdname_len = get_attrs[a].len;
146 /* Avoid NULL for memcmp optimization 0-length is good enough */
147 odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL);
152 if (odi->osdname_len)
153 memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len);
154 OSD_INFO("OSD_NAME [%s]\n", odi->osdname);
157 pFirst = get_attrs[a++].val_ptr;
158 OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
159 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
161 pFirst = get_attrs[a++].val_ptr;
162 OSD_INFO("USED_CAPACITY [0x%llx]\n",
163 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
165 pFirst = get_attrs[a++].val_ptr;
166 OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n",
167 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
172 /* FIXME: Where are the time utilities */
173 pFirst = get_attrs[a++].val_ptr;
174 OSD_INFO("CLOCK [0x%6phN]\n", pFirst);
176 if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
177 unsigned len = get_attrs[a].len;
178 char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
180 hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
181 sid_dump, sizeof(sid_dump), true);
182 OSD_INFO("OSD_SYSTEM_ID(%d)\n"
183 " [%s]\n", len, sid_dump);
185 if (unlikely(len > sizeof(odi->systemid))) {
186 OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). "
187 "device identification might not work\n", len);
188 len = sizeof(odi->systemid);
190 odi->systemid_len = len;
191 memcpy(odi->systemid, get_attrs[a].val_ptr, len);
199 int osd_auto_detect_ver(struct osd_dev *od,
200 void *caps, struct osd_dev_info *odi)
204 /* Auto-detect the osd version */
205 ret = _osd_get_print_system_info(od, caps, odi);
207 osd_dev_set_ver(od, OSD_VER1);
208 OSD_DEBUG("converting to OSD1\n");
209 ret = _osd_get_print_system_info(od, caps, odi);
214 EXPORT_SYMBOL(osd_auto_detect_ver);
216 static unsigned _osd_req_cdb_len(struct osd_request *or)
218 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
221 static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
223 return osd_req_is_ver1(or) ?
224 osdv1_attr_list_elem_size(len) :
225 osdv2_attr_list_elem_size(len);
228 static void _osd_req_alist_elem_encode(struct osd_request *or,
229 void *attr_last, const struct osd_attr *oa)
231 if (osd_req_is_ver1(or)) {
232 struct osdv1_attributes_list_element *attr = attr_last;
234 attr->attr_page = cpu_to_be32(oa->attr_page);
235 attr->attr_id = cpu_to_be32(oa->attr_id);
236 attr->attr_bytes = cpu_to_be16(oa->len);
237 memcpy(attr->attr_val, oa->val_ptr, oa->len);
239 struct osdv2_attributes_list_element *attr = attr_last;
241 attr->attr_page = cpu_to_be32(oa->attr_page);
242 attr->attr_id = cpu_to_be32(oa->attr_id);
243 attr->attr_bytes = cpu_to_be16(oa->len);
244 memcpy(attr->attr_val, oa->val_ptr, oa->len);
248 static int _osd_req_alist_elem_decode(struct osd_request *or,
249 void *cur_p, struct osd_attr *oa, unsigned max_bytes)
252 if (osd_req_is_ver1(or)) {
253 struct osdv1_attributes_list_element *attr = cur_p;
255 if (max_bytes < sizeof(*attr))
258 oa->len = be16_to_cpu(attr->attr_bytes);
259 inc = _osd_req_alist_elem_size(or, oa->len);
263 oa->attr_page = be32_to_cpu(attr->attr_page);
264 oa->attr_id = be32_to_cpu(attr->attr_id);
266 /* OSD1: On empty attributes we return a pointer to 2 bytes
267 * of zeros. This keeps similar behaviour with OSD2.
270 oa->val_ptr = likely(oa->len) ? attr->attr_val :
271 (u8 *)&attr->attr_bytes;
273 struct osdv2_attributes_list_element *attr = cur_p;
275 if (max_bytes < sizeof(*attr))
278 oa->len = be16_to_cpu(attr->attr_bytes);
279 inc = _osd_req_alist_elem_size(or, oa->len);
283 oa->attr_page = be32_to_cpu(attr->attr_page);
284 oa->attr_id = be32_to_cpu(attr->attr_id);
286 /* OSD2: For convenience, on empty attributes, we return 8 bytes
287 * of zeros here. This keeps the same behaviour with OSD2r04,
288 * and is nice with null terminating ASCII fields.
289 * oa->val_ptr == NULL marks the end-of-list, or error.
291 oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
296 static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
298 return osd_req_is_ver1(or) ?
299 osdv1_list_size(list_head) :
300 osdv2_list_size(list_head);
303 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
305 return osd_req_is_ver1(or) ?
306 sizeof(struct osdv1_attributes_list_header) :
307 sizeof(struct osdv2_attributes_list_header);
310 static void _osd_req_set_alist_type(struct osd_request *or,
311 void *list, int list_type)
313 if (osd_req_is_ver1(or)) {
314 struct osdv1_attributes_list_header *attr_list = list;
316 memset(attr_list, 0, sizeof(*attr_list));
317 attr_list->type = list_type;
319 struct osdv2_attributes_list_header *attr_list = list;
321 memset(attr_list, 0, sizeof(*attr_list));
322 attr_list->type = list_type;
326 static bool _osd_req_is_alist_type(struct osd_request *or,
327 void *list, int list_type)
332 if (osd_req_is_ver1(or)) {
333 struct osdv1_attributes_list_header *attr_list = list;
335 return attr_list->type == list_type;
337 struct osdv2_attributes_list_header *attr_list = list;
339 return attr_list->type == list_type;
343 /* This is for List-objects not Attributes-Lists */
344 static void _osd_req_encode_olist(struct osd_request *or,
345 struct osd_obj_id_list *list)
347 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
349 if (osd_req_is_ver1(or)) {
350 cdbh->v1.list_identifier = list->list_identifier;
351 cdbh->v1.start_address = list->continuation_id;
353 cdbh->v2.list_identifier = list->list_identifier;
354 cdbh->v2.start_address = list->continuation_id;
358 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
359 u64 offset, unsigned *padding)
361 return __osd_encode_offset(offset, padding,
362 osd_req_is_ver1(or) ?
363 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
364 OSD_OFFSET_MAX_SHIFT);
367 static struct osd_security_parameters *
368 _osd_req_sec_params(struct osd_request *or)
370 struct osd_cdb *ocdb = &or->cdb;
372 if (osd_req_is_ver1(or))
373 return (struct osd_security_parameters *)&ocdb->v1.sec_params;
375 return (struct osd_security_parameters *)&ocdb->v2.sec_params;
378 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
380 memset(osdd, 0, sizeof(*osdd));
381 osdd->scsi_device = scsi_device;
382 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
383 #ifdef OSD_VER1_SUPPORT
384 osdd->version = OSD_VER2;
386 /* TODO: Allocate pools for osd_request attributes ... */
388 EXPORT_SYMBOL(osd_dev_init);
390 void osd_dev_fini(struct osd_dev *osdd)
392 /* TODO: De-allocate pools */
394 osdd->scsi_device = NULL;
396 EXPORT_SYMBOL(osd_dev_fini);
398 static struct osd_request *_osd_request_alloc(gfp_t gfp)
400 struct osd_request *or;
402 /* TODO: Use mempool with one saved request */
403 or = kzalloc(sizeof(*or), gfp);
407 static void _osd_request_free(struct osd_request *or)
412 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
414 struct osd_request *or;
416 or = _osd_request_alloc(gfp);
421 or->alloc_flags = gfp;
422 or->timeout = dev->def_timeout;
423 or->retries = OSD_REQ_RETRIES;
427 EXPORT_SYMBOL(osd_start_request);
429 static void _osd_free_seg(struct osd_request *or __unused,
430 struct _osd_req_data_segment *seg)
432 if (!seg->buff || !seg->alloc_size)
440 static void _put_request(struct request *rq)
443 * If osd_finalize_request() was called but the request was not
444 * executed through the block layer, then we must release BIOs.
445 * TODO: Keep error code in or->async_error. Need to audit all
448 if (unlikely(rq->bio))
449 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
454 void osd_end_request(struct osd_request *or)
456 struct request *rq = or->request;
460 _put_request(rq->next_rq);
467 _osd_free_seg(or, &or->get_attr);
468 _osd_free_seg(or, &or->enc_get_attr);
469 _osd_free_seg(or, &or->set_attr);
470 _osd_free_seg(or, &or->cdb_cont);
472 _osd_request_free(or);
474 EXPORT_SYMBOL(osd_end_request);
476 static void _set_error_resid(struct osd_request *or, struct request *req,
479 or->async_error = error;
480 or->req_errors = scsi_req(req)->result ? : error;
481 or->sense_len = scsi_req(req)->sense_len;
483 memcpy(or->sense, scsi_req(req)->sense, or->sense_len);
485 or->out.residual = scsi_req(or->out.req)->resid_len;
487 or->in.residual = scsi_req(or->in.req)->resid_len;
490 int osd_execute_request(struct osd_request *or)
494 blk_execute_rq(or->request->q, NULL, or->request, 0);
495 error = scsi_req(or->request)->result ? -EIO : 0;
497 _set_error_resid(or, or->request, error);
500 EXPORT_SYMBOL(osd_execute_request);
502 static void osd_request_async_done(struct request *req, int error)
504 struct osd_request *or = req->end_io_data;
506 _set_error_resid(or, req, error);
508 __blk_put_request(req->q, req->next_rq);
512 __blk_put_request(req->q, req);
518 or->async_done(or, or->async_private);
523 int osd_execute_request_async(struct osd_request *or,
524 osd_req_done_fn *done, void *private)
526 or->request->end_io_data = or;
527 or->async_private = private;
528 or->async_done = done;
530 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
531 osd_request_async_done);
534 EXPORT_SYMBOL(osd_execute_request_async);
536 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
537 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
539 static int _osd_realloc_seg(struct osd_request *or,
540 struct _osd_req_data_segment *seg, unsigned max_bytes)
544 if (seg->alloc_size >= max_bytes)
547 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
549 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
554 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
556 seg->alloc_size = max_bytes;
560 static int _alloc_cdb_cont(struct osd_request *or, unsigned total_bytes)
562 OSD_DEBUG("total_bytes=%d\n", total_bytes);
563 return _osd_realloc_seg(or, &or->cdb_cont, total_bytes);
566 static int _alloc_set_attr_list(struct osd_request *or,
567 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
569 unsigned total_bytes = add_bytes;
571 for (; nelem; --nelem, ++oa)
572 total_bytes += _osd_req_alist_elem_size(or, oa->len);
574 OSD_DEBUG("total_bytes=%d\n", total_bytes);
575 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
578 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
580 OSD_DEBUG("total_bytes=%d\n", max_bytes);
581 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
584 static int _alloc_get_attr_list(struct osd_request *or)
586 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
587 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
591 * Common to all OSD commands
594 static void _osdv1_req_encode_common(struct osd_request *or,
595 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
597 struct osdv1_cdb *ocdb = &or->cdb.v1;
600 * For speed, the commands
601 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
602 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
603 * are not supported here. Should pass zero and set after the call
605 act &= cpu_to_be16(~0x0080); /* V1 action code */
607 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
609 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
610 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
611 ocdb->h.varlen_cdb.service_action = act;
613 ocdb->h.partition = cpu_to_be64(obj->partition);
614 ocdb->h.object = cpu_to_be64(obj->id);
615 ocdb->h.v1.length = cpu_to_be64(len);
616 ocdb->h.v1.start_address = cpu_to_be64(offset);
619 static void _osdv2_req_encode_common(struct osd_request *or,
620 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
622 struct osdv2_cdb *ocdb = &or->cdb.v2;
624 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
626 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
627 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
628 ocdb->h.varlen_cdb.service_action = act;
630 ocdb->h.partition = cpu_to_be64(obj->partition);
631 ocdb->h.object = cpu_to_be64(obj->id);
632 ocdb->h.v2.length = cpu_to_be64(len);
633 ocdb->h.v2.start_address = cpu_to_be64(offset);
636 static void _osd_req_encode_common(struct osd_request *or,
637 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
639 if (osd_req_is_ver1(or))
640 _osdv1_req_encode_common(or, act, obj, offset, len);
642 _osdv2_req_encode_common(or, act, obj, offset, len);
648 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
649 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
651 void osd_req_format(struct osd_request *or, u64 tot_capacity)
653 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
656 EXPORT_SYMBOL(osd_req_format);
658 int osd_req_list_dev_partitions(struct osd_request *or,
659 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
661 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
663 EXPORT_SYMBOL(osd_req_list_dev_partitions);
665 static void _osd_req_encode_flush(struct osd_request *or,
666 enum osd_options_flush_scope_values op)
668 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
670 ocdb->command_specific_options = op;
673 void osd_req_flush_obsd(struct osd_request *or,
674 enum osd_options_flush_scope_values op)
676 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
677 _osd_req_encode_flush(or, op);
679 EXPORT_SYMBOL(osd_req_flush_obsd);
681 /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
682 const u8 *cdb, ...); */
683 /*TODO: void osd_req_task_management(struct osd_request *, ...); */
688 static void _osd_req_encode_partition(struct osd_request *or,
689 __be16 act, osd_id partition)
691 struct osd_obj_id par = {
692 .partition = partition,
696 _osd_req_encode_common(or, act, &par, 0, 0);
699 void osd_req_create_partition(struct osd_request *or, osd_id partition)
701 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
703 EXPORT_SYMBOL(osd_req_create_partition);
705 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
707 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
709 EXPORT_SYMBOL(osd_req_remove_partition);
711 /*TODO: void osd_req_set_partition_key(struct osd_request *,
712 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
713 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
715 static int _osd_req_list_objects(struct osd_request *or,
716 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
717 struct osd_obj_id_list *list, unsigned nelem)
719 struct request_queue *q = osd_request_queue(or->osd_dev);
720 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
723 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
725 if (list->list_identifier)
726 _osd_req_encode_olist(or, list);
729 bio = bio_map_kern(q, list, len, or->alloc_flags);
731 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
735 bio_set_op_attrs(bio, REQ_OP_READ, 0);
737 or->in.total_bytes = bio->bi_iter.bi_size;
741 int osd_req_list_partition_collections(struct osd_request *or,
742 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
745 struct osd_obj_id par = {
746 .partition = partition,
750 return osd_req_list_collection_objects(or, &par, initial_id, list,
753 EXPORT_SYMBOL(osd_req_list_partition_collections);
755 int osd_req_list_partition_objects(struct osd_request *or,
756 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
759 struct osd_obj_id par = {
760 .partition = partition,
764 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
767 EXPORT_SYMBOL(osd_req_list_partition_objects);
769 void osd_req_flush_partition(struct osd_request *or,
770 osd_id partition, enum osd_options_flush_scope_values op)
772 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
773 _osd_req_encode_flush(or, op);
775 EXPORT_SYMBOL(osd_req_flush_partition);
778 * Collection commands
780 /*TODO: void osd_req_create_collection(struct osd_request *,
781 const struct osd_obj_id *); */
782 /*TODO: void osd_req_remove_collection(struct osd_request *,
783 const struct osd_obj_id *); */
785 int osd_req_list_collection_objects(struct osd_request *or,
786 const struct osd_obj_id *obj, osd_id initial_id,
787 struct osd_obj_id_list *list, unsigned nelem)
789 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
790 initial_id, list, nelem);
792 EXPORT_SYMBOL(osd_req_list_collection_objects);
794 /*TODO: void query(struct osd_request *, ...); V2 */
796 void osd_req_flush_collection(struct osd_request *or,
797 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
799 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
800 _osd_req_encode_flush(or, op);
802 EXPORT_SYMBOL(osd_req_flush_collection);
804 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
805 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
810 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
812 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
814 EXPORT_SYMBOL(osd_req_create_object);
816 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
818 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
820 EXPORT_SYMBOL(osd_req_remove_object);
823 /*TODO: void osd_req_create_multi(struct osd_request *or,
824 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
827 void osd_req_write(struct osd_request *or,
828 const struct osd_obj_id *obj, u64 offset,
829 struct bio *bio, u64 len)
831 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
832 WARN_ON(or->out.bio || or->out.total_bytes);
833 WARN_ON(!op_is_write(bio_op(bio)));
835 or->out.total_bytes = len;
837 EXPORT_SYMBOL(osd_req_write);
839 int osd_req_write_kern(struct osd_request *or,
840 const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
842 struct request_queue *req_q = osd_request_queue(or->osd_dev);
843 struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
848 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
849 osd_req_write(or, obj, offset, bio, len);
852 EXPORT_SYMBOL(osd_req_write_kern);
854 /*TODO: void osd_req_append(struct osd_request *,
855 const struct osd_obj_id *, struct bio *data_out); */
856 /*TODO: void osd_req_create_write(struct osd_request *,
857 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
858 /*TODO: void osd_req_clear(struct osd_request *,
859 const struct osd_obj_id *, u64 offset, u64 len); */
860 /*TODO: void osd_req_punch(struct osd_request *,
861 const struct osd_obj_id *, u64 offset, u64 len); V2 */
863 void osd_req_flush_object(struct osd_request *or,
864 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
865 /*V2*/ u64 offset, /*V2*/ u64 len)
867 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
868 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
873 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
874 _osd_req_encode_flush(or, op);
876 EXPORT_SYMBOL(osd_req_flush_object);
878 void osd_req_read(struct osd_request *or,
879 const struct osd_obj_id *obj, u64 offset,
880 struct bio *bio, u64 len)
882 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
883 WARN_ON(or->in.bio || or->in.total_bytes);
884 WARN_ON(op_is_write(bio_op(bio)));
886 or->in.total_bytes = len;
888 EXPORT_SYMBOL(osd_req_read);
890 int osd_req_read_kern(struct osd_request *or,
891 const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
893 struct request_queue *req_q = osd_request_queue(or->osd_dev);
894 struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
899 osd_req_read(or, obj, offset, bio, len);
902 EXPORT_SYMBOL(osd_req_read_kern);
904 static int _add_sg_continuation_descriptor(struct osd_request *or,
905 const struct osd_sg_entry *sglist, unsigned numentries, u64 *len)
907 struct osd_sg_continuation_descriptor *oscd;
912 oscd_size = sizeof(*oscd) + numentries * sizeof(oscd->entries[0]);
914 if (!or->cdb_cont.total_bytes) {
915 /* First time, jump over the header, we will write to:
916 * cdb_cont.buff + cdb_cont.total_bytes
918 or->cdb_cont.total_bytes =
919 sizeof(struct osd_continuation_segment_header);
922 ret = _alloc_cdb_cont(or, or->cdb_cont.total_bytes + oscd_size);
926 oscd = or->cdb_cont.buff + or->cdb_cont.total_bytes;
927 oscd->hdr.type = cpu_to_be16(SCATTER_GATHER_LIST);
928 oscd->hdr.pad_length = 0;
929 oscd->hdr.length = cpu_to_be32(oscd_size - sizeof(*oscd));
932 /* copy the sg entries and convert to network byte order */
933 for (i = 0; i < numentries; i++) {
934 oscd->entries[i].offset = cpu_to_be64(sglist[i].offset);
935 oscd->entries[i].len = cpu_to_be64(sglist[i].len);
936 *len += sglist[i].len;
939 or->cdb_cont.total_bytes += oscd_size;
940 OSD_DEBUG("total_bytes=%d oscd_size=%d numentries=%d\n",
941 or->cdb_cont.total_bytes, oscd_size, numentries);
945 static int _osd_req_finalize_cdb_cont(struct osd_request *or, const u8 *cap_key)
947 struct request_queue *req_q = osd_request_queue(or->osd_dev);
949 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
950 struct osd_continuation_segment_header *cont_seg_hdr;
952 if (!or->cdb_cont.total_bytes)
955 cont_seg_hdr = or->cdb_cont.buff;
956 cont_seg_hdr->format = CDB_CONTINUATION_FORMAT_V2;
957 cont_seg_hdr->service_action = cdbh->varlen_cdb.service_action;
959 /* create a bio for continuation segment */
960 bio = bio_map_kern(req_q, or->cdb_cont.buff, or->cdb_cont.total_bytes,
965 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
967 /* integrity check the continuation before the bio is linked
968 * with the other data segments since the continuation
969 * integrity is separate from the other data segments.
971 osd_sec_sign_data(cont_seg_hdr->integrity_check, bio, cap_key);
973 cdbh->v2.cdb_continuation_length = cpu_to_be32(or->cdb_cont.total_bytes);
975 /* we can't use _req_append_segment, because we need to link in the
976 * continuation bio to the head of the bio list - the
977 * continuation segment (if it exists) is always the first segment in
978 * the out data buffer.
980 bio->bi_next = or->out.bio;
982 or->out.total_bytes += or->cdb_cont.total_bytes;
987 /* osd_req_write_sg: Takes a @bio that points to the data out buffer and an
988 * @sglist that has the scatter gather entries. Scatter-gather enables a write
989 * of multiple none-contiguous areas of an object, in a single call. The extents
990 * may overlap and/or be in any order. The only constrain is that:
991 * total_bytes(sglist) >= total_bytes(bio)
993 int osd_req_write_sg(struct osd_request *or,
994 const struct osd_obj_id *obj, struct bio *bio,
995 const struct osd_sg_entry *sglist, unsigned numentries)
998 int ret = _add_sg_continuation_descriptor(or, sglist, numentries, &len);
1002 osd_req_write(or, obj, 0, bio, len);
1006 EXPORT_SYMBOL(osd_req_write_sg);
1008 /* osd_req_read_sg: Read multiple extents of an object into @bio
1009 * See osd_req_write_sg
1011 int osd_req_read_sg(struct osd_request *or,
1012 const struct osd_obj_id *obj, struct bio *bio,
1013 const struct osd_sg_entry *sglist, unsigned numentries)
1019 if (numentries > 1) {
1021 ret = _add_sg_continuation_descriptor(or, sglist, numentries,
1026 /* Optimize the case of single segment, read_sg is a
1030 off = sglist->offset;
1032 osd_req_read(or, obj, off, bio, len);
1036 EXPORT_SYMBOL(osd_req_read_sg);
1038 /* SG-list write/read Kern API
1040 * osd_req_{write,read}_sg_kern takes an array of @buff pointers and an array
1041 * of sg_entries. @numentries indicates how many pointers and sg_entries there
1042 * are. By requiring an array of buff pointers. This allows a caller to do a
1043 * single write/read and scatter into multiple buffers.
1044 * NOTE: Each buffer + len should not cross a page boundary.
1046 static struct bio *_create_sg_bios(struct osd_request *or,
1047 void **buff, const struct osd_sg_entry *sglist, unsigned numentries)
1049 struct request_queue *q = osd_request_queue(or->osd_dev);
1053 bio = bio_kmalloc(GFP_KERNEL, numentries);
1054 if (unlikely(!bio)) {
1055 OSD_DEBUG("Failed to allocate BIO size=%u\n", numentries);
1056 return ERR_PTR(-ENOMEM);
1059 for (i = 0; i < numentries; i++) {
1060 unsigned offset = offset_in_page(buff[i]);
1061 struct page *page = virt_to_page(buff[i]);
1062 unsigned len = sglist[i].len;
1065 BUG_ON(offset + len > PAGE_SIZE);
1066 added_len = bio_add_pc_page(q, bio, page, len, offset);
1067 if (unlikely(len != added_len)) {
1068 OSD_DEBUG("bio_add_pc_page len(%d) != added_len(%d)\n",
1071 return ERR_PTR(-ENOMEM);
1078 int osd_req_write_sg_kern(struct osd_request *or,
1079 const struct osd_obj_id *obj, void **buff,
1080 const struct osd_sg_entry *sglist, unsigned numentries)
1082 struct bio *bio = _create_sg_bios(or, buff, sglist, numentries);
1084 return PTR_ERR(bio);
1086 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
1087 osd_req_write_sg(or, obj, bio, sglist, numentries);
1091 EXPORT_SYMBOL(osd_req_write_sg_kern);
1093 int osd_req_read_sg_kern(struct osd_request *or,
1094 const struct osd_obj_id *obj, void **buff,
1095 const struct osd_sg_entry *sglist, unsigned numentries)
1097 struct bio *bio = _create_sg_bios(or, buff, sglist, numentries);
1099 return PTR_ERR(bio);
1101 osd_req_read_sg(or, obj, bio, sglist, numentries);
1105 EXPORT_SYMBOL(osd_req_read_sg_kern);
1109 void osd_req_get_attributes(struct osd_request *or,
1110 const struct osd_obj_id *obj)
1112 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
1114 EXPORT_SYMBOL(osd_req_get_attributes);
1116 void osd_req_set_attributes(struct osd_request *or,
1117 const struct osd_obj_id *obj)
1119 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
1121 EXPORT_SYMBOL(osd_req_set_attributes);
1124 * Attributes List-mode
1127 int osd_req_add_set_attr_list(struct osd_request *or,
1128 const struct osd_attr *oa, unsigned nelem)
1130 unsigned total_bytes = or->set_attr.total_bytes;
1134 if (or->attributes_mode &&
1135 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
1139 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1141 if (!total_bytes) { /* first-time: allocate and put list header */
1142 total_bytes = _osd_req_sizeof_alist_header(or);
1143 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
1146 _osd_req_set_alist_type(or, or->set_attr.buff,
1147 OSD_ATTR_LIST_SET_RETRIEVE);
1149 attr_last = or->set_attr.buff + total_bytes;
1151 for (; nelem; --nelem) {
1152 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
1154 total_bytes += elem_size;
1155 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
1156 or->set_attr.total_bytes = total_bytes - elem_size;
1157 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
1161 or->set_attr.buff + or->set_attr.total_bytes;
1164 _osd_req_alist_elem_encode(or, attr_last, oa);
1166 attr_last += elem_size;
1170 or->set_attr.total_bytes = total_bytes;
1173 EXPORT_SYMBOL(osd_req_add_set_attr_list);
1175 static int _req_append_segment(struct osd_request *or,
1176 unsigned padding, struct _osd_req_data_segment *seg,
1177 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
1183 /* check if we can just add it to last buffer */
1185 (padding <= last_seg->alloc_size - last_seg->total_bytes))
1186 pad_buff = last_seg->buff + last_seg->total_bytes;
1188 pad_buff = io->pad_buff;
1190 ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding,
1194 io->total_bytes += padding;
1197 ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
1202 io->total_bytes += seg->total_bytes;
1203 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
1208 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
1210 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1214 if (!or->set_attr.total_bytes) {
1215 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
1219 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
1220 cdbh->attrs_list.set_attr_offset =
1221 osd_req_encode_offset(or, or->out.total_bytes, &padding);
1223 ret = _req_append_segment(or, padding, &or->set_attr,
1224 or->out.last_seg, &or->out);
1228 or->out.last_seg = &or->set_attr;
1232 int osd_req_add_get_attr_list(struct osd_request *or,
1233 const struct osd_attr *oa, unsigned nelem)
1235 unsigned total_bytes = or->enc_get_attr.total_bytes;
1239 if (or->attributes_mode &&
1240 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
1244 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1246 /* first time calc data-in list header size */
1247 if (!or->get_attr.total_bytes)
1248 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
1250 /* calc data-out info */
1251 if (!total_bytes) { /* first-time: allocate and put list header */
1254 total_bytes = _osd_req_sizeof_alist_header(or);
1255 max_bytes = total_bytes +
1256 nelem * sizeof(struct osd_attributes_list_attrid);
1257 ret = _alloc_get_attr_desc(or, max_bytes);
1261 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
1264 attr_last = or->enc_get_attr.buff + total_bytes;
1266 for (; nelem; --nelem) {
1267 struct osd_attributes_list_attrid *attrid;
1268 const unsigned cur_size = sizeof(*attrid);
1270 total_bytes += cur_size;
1271 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
1272 or->enc_get_attr.total_bytes = total_bytes - cur_size;
1273 ret = _alloc_get_attr_desc(or,
1274 total_bytes + nelem * sizeof(*attrid));
1277 attr_last = or->enc_get_attr.buff +
1278 or->enc_get_attr.total_bytes;
1282 attrid->attr_page = cpu_to_be32(oa->attr_page);
1283 attrid->attr_id = cpu_to_be32(oa->attr_id);
1285 attr_last += cur_size;
1287 /* calc data-in size */
1288 or->get_attr.total_bytes +=
1289 _osd_req_alist_elem_size(or, oa->len);
1293 or->enc_get_attr.total_bytes = total_bytes;
1296 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%zu)\n",
1297 or->get_attr.total_bytes,
1298 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
1299 or->enc_get_attr.total_bytes,
1300 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
1301 / sizeof(struct osd_attributes_list_attrid));
1305 EXPORT_SYMBOL(osd_req_add_get_attr_list);
1307 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
1309 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1310 unsigned out_padding;
1311 unsigned in_padding;
1314 if (!or->enc_get_attr.total_bytes) {
1315 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
1316 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
1320 ret = _alloc_get_attr_list(or);
1324 /* The out-going buffer info update */
1325 OSD_DEBUG("out-going\n");
1326 cdbh->attrs_list.get_attr_desc_bytes =
1327 cpu_to_be32(or->enc_get_attr.total_bytes);
1329 cdbh->attrs_list.get_attr_desc_offset =
1330 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1332 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
1333 or->out.last_seg, &or->out);
1336 or->out.last_seg = &or->enc_get_attr;
1338 /* The incoming buffer info update */
1339 OSD_DEBUG("in-coming\n");
1340 cdbh->attrs_list.get_attr_alloc_length =
1341 cpu_to_be32(or->get_attr.total_bytes);
1343 cdbh->attrs_list.get_attr_offset =
1344 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1346 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1350 or->in.last_seg = &or->get_attr;
1355 int osd_req_decode_get_attr_list(struct osd_request *or,
1356 struct osd_attr *oa, int *nelem, void **iterator)
1358 unsigned cur_bytes, returned_bytes;
1360 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1363 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1364 OSD_ATTR_LIST_SET_RETRIEVE)) {
1374 BUG_ON((*iterator < or->get_attr.buff) ||
1375 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1377 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1378 returned_bytes = or->get_attr.total_bytes;
1379 } else { /* first time decode the list header */
1380 cur_bytes = sizeof_attr_list;
1381 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1384 cur_p = or->get_attr.buff + sizeof_attr_list;
1386 if (returned_bytes > or->get_attr.alloc_size) {
1387 OSD_DEBUG("target report: space was not big enough! "
1388 "Allocate=%u Needed=%u\n",
1389 or->get_attr.alloc_size,
1390 returned_bytes + sizeof_attr_list);
1393 or->get_attr.alloc_size - sizeof_attr_list;
1395 or->get_attr.total_bytes = returned_bytes;
1398 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
1399 int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
1400 returned_bytes - cur_bytes);
1403 OSD_ERR("BAD FOOD from target. list not valid!"
1405 cur_bytes, returned_bytes, n);
1407 cur_bytes = returned_bytes; /* break the caller loop */
1416 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1418 return returned_bytes - cur_bytes;
1420 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1423 * Attributes Page-mode
1426 int osd_req_add_get_attr_page(struct osd_request *or,
1427 u32 page_id, void *attar_page, unsigned max_page_len,
1428 const struct osd_attr *set_one_attr)
1430 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1432 if (or->attributes_mode &&
1433 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1437 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1439 or->get_attr.buff = attar_page;
1440 or->get_attr.total_bytes = max_page_len;
1442 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1443 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
1445 if (!set_one_attr || !set_one_attr->attr_page)
1446 return 0; /* The set is optional */
1448 or->set_attr.buff = set_one_attr->val_ptr;
1449 or->set_attr.total_bytes = set_one_attr->len;
1451 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1452 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1453 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
1456 EXPORT_SYMBOL(osd_req_add_get_attr_page);
1458 static int _osd_req_finalize_attr_page(struct osd_request *or)
1460 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1461 unsigned in_padding, out_padding;
1465 cdbh->attrs_page.get_attr_offset =
1466 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1468 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1473 if (or->set_attr.total_bytes == 0)
1477 cdbh->attrs_page.set_attr_offset =
1478 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1480 ret = _req_append_segment(or, out_padding, &or->set_attr, NULL,
1485 static inline void osd_sec_parms_set_out_offset(bool is_v1,
1486 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1489 sec_parms->v1.data_out_integrity_check_offset = offset;
1491 sec_parms->v2.data_out_integrity_check_offset = offset;
1494 static inline void osd_sec_parms_set_in_offset(bool is_v1,
1495 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1498 sec_parms->v1.data_in_integrity_check_offset = offset;
1500 sec_parms->v2.data_in_integrity_check_offset = offset;
1503 static int _osd_req_finalize_data_integrity(struct osd_request *or,
1504 bool has_in, bool has_out, struct bio *out_data_bio, u64 out_data_bytes,
1507 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1510 if (!osd_is_sec_alldata(sec_parms))
1514 struct _osd_req_data_segment seg = {
1515 .buff = &or->out_data_integ,
1516 .total_bytes = sizeof(or->out_data_integ),
1520 or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
1521 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1522 or->set_attr.total_bytes);
1523 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1524 or->enc_get_attr.total_bytes);
1526 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
1527 osd_req_encode_offset(or, or->out.total_bytes, &pad));
1529 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1533 or->out.last_seg = NULL;
1535 /* they are now all chained to request sign them all together */
1536 osd_sec_sign_data(&or->out_data_integ, out_data_bio,
1541 struct _osd_req_data_segment seg = {
1542 .buff = &or->in_data_integ,
1543 .total_bytes = sizeof(or->in_data_integ),
1547 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
1548 osd_req_encode_offset(or, or->in.total_bytes, &pad));
1550 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1555 or->in.last_seg = NULL;
1562 * osd_finalize_request and helpers
1564 static struct request *_make_request(struct request_queue *q, bool has_write,
1565 struct _osd_io_info *oii, gfp_t flags)
1567 struct request *req;
1568 struct bio *bio = oii->bio;
1571 req = blk_get_request(q, has_write ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN,
1578 struct bio *bounce_bio = bio;
1580 blk_queue_bounce(req->q, &bounce_bio);
1581 ret = blk_rq_append_bio(req, bounce_bio);
1583 return ERR_PTR(ret);
1589 static int _init_blk_request(struct osd_request *or,
1590 bool has_in, bool has_out)
1592 gfp_t flags = or->alloc_flags;
1593 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1594 struct request_queue *q = scsi_device->request_queue;
1595 struct request *req;
1598 req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
1605 req->rq_flags |= RQF_QUIET;
1607 req->timeout = or->timeout;
1608 scsi_req(req)->retries = or->retries;
1613 /* allocate bidi request */
1614 req = _make_request(q, false, &or->in, flags);
1616 OSD_DEBUG("blk_get_request for bidi failed\n");
1621 or->in.req = or->request->next_rq = req;
1628 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1629 or, has_in, has_out, ret, or->request);
1633 int osd_finalize_request(struct osd_request *or,
1634 u8 options, const void *cap, const u8 *cap_key)
1636 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1637 bool has_in, has_out;
1638 /* Save for data_integrity without the cdb_continuation */
1639 struct bio *out_data_bio = or->out.bio;
1640 u64 out_data_bytes = or->out.total_bytes;
1643 if (options & OSD_REQ_FUA)
1644 cdbh->options |= OSD_CDB_FUA;
1646 if (options & OSD_REQ_DPO)
1647 cdbh->options |= OSD_CDB_DPO;
1649 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1650 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1652 osd_set_caps(&or->cdb, cap);
1654 has_in = or->in.bio || or->get_attr.total_bytes;
1655 has_out = or->out.bio || or->cdb_cont.total_bytes ||
1656 or->set_attr.total_bytes || or->enc_get_attr.total_bytes;
1658 ret = _osd_req_finalize_cdb_cont(or, cap_key);
1660 OSD_DEBUG("_osd_req_finalize_cdb_cont failed\n");
1663 ret = _init_blk_request(or, has_in, has_out);
1665 OSD_DEBUG("_init_blk_request failed\n");
1669 or->out.pad_buff = sg_out_pad_buffer;
1670 or->in.pad_buff = sg_in_pad_buffer;
1672 if (!or->attributes_mode)
1673 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1674 cdbh->command_specific_options |= or->attributes_mode;
1675 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1676 ret = _osd_req_finalize_attr_page(or);
1678 OSD_DEBUG("_osd_req_finalize_attr_page failed\n");
1682 /* TODO: I think that for the GET_ATTR command these 2 should
1683 * be reversed to keep them in execution order (for embedded
1684 * targets with low memory footprint)
1686 ret = _osd_req_finalize_set_attr_list(or);
1688 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1692 ret = _osd_req_finalize_get_attr_list(or);
1694 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1699 ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
1700 out_data_bio, out_data_bytes,
1705 osd_sec_sign_cdb(&or->cdb, cap_key);
1707 scsi_req(or->request)->cmd = or->cdb.buff;
1708 scsi_req(or->request)->cmd_len = _osd_req_cdb_len(or);
1712 EXPORT_SYMBOL(osd_finalize_request);
1714 static bool _is_osd_security_code(int code)
1716 return (code == osd_security_audit_value_frozen) ||
1717 (code == osd_security_working_key_frozen) ||
1718 (code == osd_nonce_not_unique) ||
1719 (code == osd_nonce_timestamp_out_of_range) ||
1720 (code == osd_invalid_dataout_buffer_integrity_check_value);
1723 #define OSD_SENSE_PRINT1(fmt, a...) \
1725 if (__cur_sense_need_output) \
1726 OSD_ERR(fmt, ##a); \
1729 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1731 int osd_req_decode_sense_full(struct osd_request *or,
1732 struct osd_sense_info *osi, bool silent,
1733 struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
1734 struct osd_attr *bad_attr_list, int max_attr)
1736 int sense_len, original_sense_len;
1737 struct osd_sense_info local_osi;
1738 struct scsi_sense_descriptor_based *ssdb;
1739 void *cur_descriptor;
1740 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1741 const bool __cur_sense_need_output = false;
1743 bool __cur_sense_need_output = !silent;
1747 if (likely(!or->req_errors))
1750 osi = osi ? : &local_osi;
1751 memset(osi, 0, sizeof(*osi));
1753 ssdb = (typeof(ssdb))or->sense;
1754 sense_len = or->sense_len;
1755 if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
1756 OSD_ERR("Block-layer returned error(0x%x) but "
1757 "sense_len(%u) || key(%d) is empty\n",
1758 or->req_errors, sense_len, ssdb->sense_key);
1762 if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
1763 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1764 ssdb->response_code, sense_len);
1768 osi->key = ssdb->sense_key;
1769 osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
1770 original_sense_len = ssdb->additional_sense_length + 8;
1772 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1773 if (__cur_sense_need_output)
1774 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
1776 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1777 "additional_code=0x%x async_error=%d errors=0x%x\n",
1778 osi->key, original_sense_len, sense_len,
1779 osi->additional_code, or->async_error,
1782 if (original_sense_len < sense_len)
1783 sense_len = original_sense_len;
1785 cur_descriptor = ssdb->ssd;
1786 sense_len -= sizeof(*ssdb);
1787 while (sense_len > 0) {
1788 struct scsi_sense_descriptor *ssd = cur_descriptor;
1789 int cur_len = ssd->additional_length + 2;
1791 sense_len -= cur_len;
1794 break; /* sense was truncated */
1796 switch (ssd->descriptor_type) {
1797 case scsi_sense_information:
1798 case scsi_sense_command_specific_information:
1800 struct scsi_sense_command_specific_data_descriptor
1801 *sscd = cur_descriptor;
1804 get_unaligned_be64(&sscd->information) ;
1806 "command_specific_information 0x%llx \n",
1807 _LLU(osi->command_info));
1810 case scsi_sense_key_specific:
1812 struct scsi_sense_key_specific_data_descriptor
1813 *ssks = cur_descriptor;
1815 osi->sense_info = get_unaligned_be16(&ssks->value);
1817 "sense_key_specific_information %u"
1818 "sksv_cd_bpv_bp (0x%x)\n",
1819 osi->sense_info, ssks->sksv_cd_bpv_bp);
1822 case osd_sense_object_identification:
1823 { /*FIXME: Keep first not last, Store in array*/
1824 struct osd_sense_identification_data_descriptor
1825 *osidd = cur_descriptor;
1827 osi->not_initiated_command_functions =
1828 le32_to_cpu(osidd->not_initiated_functions);
1829 osi->completed_command_functions =
1830 le32_to_cpu(osidd->completed_functions);
1831 osi->obj.partition = be64_to_cpu(osidd->partition_id);
1832 osi->obj.id = be64_to_cpu(osidd->object_id);
1834 "object_identification pid=0x%llx oid=0x%llx\n",
1835 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1837 "not_initiated_bits(%x) "
1838 "completed_command_bits(%x)\n",
1839 osi->not_initiated_command_functions,
1840 osi->completed_command_functions);
1843 case osd_sense_response_integrity_check:
1845 struct osd_sense_response_integrity_check_descriptor
1846 *osricd = cur_descriptor;
1847 const unsigned len =
1848 sizeof(osricd->integrity_check_value);
1849 char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
1851 hex_dump_to_buffer(osricd->integrity_check_value, len,
1852 32, 1, key_dump, sizeof(key_dump), true);
1853 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
1855 case osd_sense_attribute_identification:
1857 struct osd_sense_attributes_data_descriptor
1858 *osadd = cur_descriptor;
1859 unsigned len = min(cur_len, sense_len);
1860 struct osd_sense_attr *pattr = osadd->sense_attrs;
1862 while (len >= sizeof(*pattr)) {
1863 u32 attr_page = be32_to_cpu(pattr->attr_page);
1864 u32 attr_id = be32_to_cpu(pattr->attr_id);
1866 if (!osi->attr.attr_page) {
1867 osi->attr.attr_page = attr_page;
1868 osi->attr.attr_id = attr_id;
1871 if (bad_attr_list && max_attr) {
1872 bad_attr_list->attr_page = attr_page;
1873 bad_attr_list->attr_id = attr_id;
1878 len -= sizeof(*pattr);
1880 "osd_sense_attribute_identification"
1881 "attr_page=0x%x attr_id=0x%x\n",
1882 attr_page, attr_id);
1885 /*These are not legal for OSD*/
1886 case scsi_sense_field_replaceable_unit:
1887 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1889 case scsi_sense_stream_commands:
1890 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1892 case scsi_sense_block_commands:
1893 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1895 case scsi_sense_ata_return:
1896 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1899 if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1901 "scsi_sense Reserved descriptor (0x%x)",
1902 ssd->descriptor_type);
1905 "scsi_sense Vendor descriptor (0x%x)",
1906 ssd->descriptor_type);
1909 cur_descriptor += cur_len;
1914 /* scsi sense is Empty, the request was never issued to target
1915 * linux return code might tell us what happened.
1917 if (or->async_error == -ENOMEM)
1918 osi->osd_err_pri = OSD_ERR_PRI_RESOURCE;
1920 osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE;
1921 ret = or->async_error;
1922 } else if (osi->key <= scsi_sk_recovered_error) {
1923 osi->osd_err_pri = 0;
1925 } else if (osi->additional_code == scsi_invalid_field_in_cdb) {
1926 if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) {
1927 osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES;
1928 ret = -EFAULT; /* caller should recover from this */
1929 } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) {
1930 osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND;
1932 } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) {
1933 osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS;
1936 osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
1939 } else if (osi->additional_code == osd_quota_error) {
1940 osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE;
1942 } else if (_is_osd_security_code(osi->additional_code)) {
1943 osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
1946 osi->osd_err_pri = OSD_ERR_PRI_EIO;
1950 if (!or->out.residual)
1951 or->out.residual = or->out.total_bytes;
1952 if (!or->in.residual)
1953 or->in.residual = or->in.total_bytes;
1957 EXPORT_SYMBOL(osd_req_decode_sense_full);
1960 * Implementation of osd_sec.h API
1961 * TODO: Move to a separate osd_sec.c file at a later stage.
1964 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1965 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1966 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1967 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1968 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1971 enum { OSD_SEC_CAP_V2_ALL_CAPS =
1972 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1975 void osd_sec_init_nosec_doall_caps(void *caps,
1976 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1978 struct osd_capability *cap = caps;
1982 if (likely(obj->id)) {
1983 if (unlikely(is_collection)) {
1984 type = OSD_SEC_OBJ_COLLECTION;
1985 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1986 OSD_SEC_OBJ_DESC_COL;
1988 type = OSD_SEC_OBJ_USER;
1989 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1991 WARN_ON(!obj->partition);
1993 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1995 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1998 memset(cap, 0, sizeof(*cap));
2000 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
2001 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
2002 cap->h.security_method = OSD_SEC_NOSEC;
2003 /* cap->expiration_time;
2005 cap->discriminator[42-30];
2006 cap->object_created_time; */
2007 cap->h.object_type = type;
2008 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
2009 cap->h.object_descriptor_type = descriptor_type;
2010 cap->od.obj_desc.policy_access_tag = 0;
2011 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
2012 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
2014 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
2016 /* FIXME: Extract version from caps pointer.
2017 * Also Pete's target only supports caps from OSDv1 for now
2019 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
2021 /* NOTE: They start at same address */
2022 memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
2025 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
2030 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
2034 void osd_sec_sign_data(void *data_integ __unused,
2035 struct bio *bio __unused, const u8 *cap_key __unused)
2040 * Declared in osd_protocol.h
2041 * 4.12.5 Data-In and Data-Out buffer offsets
2042 * byte offset = mantissa * (2^(exponent+8))
2043 * Returns the smallest allowed encoded offset that contains given @offset
2044 * The actual encoded offset returned is @offset + *@padding.
2046 osd_cdb_offset __osd_encode_offset(
2047 u64 offset, unsigned *padding, int min_shift, int max_shift)
2049 u64 try_offset = -1, mod, align;
2050 osd_cdb_offset be32_offset;
2057 for (shift = min_shift; shift < max_shift; ++shift) {
2058 try_offset = offset >> shift;
2059 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
2063 BUG_ON(shift == max_shift);
2066 mod = offset & (align - 1);
2068 *padding = align - mod;
2072 try_offset |= ((shift - 8) & 0xf) << 28;
2073 be32_offset = cpu_to_be32((u32)try_offset);
2075 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
2076 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
2077 be32_offset, *padding);