1/* 2 * osd_initiator - Main body of the osd initiator library. 3 * 4 * Note: The file does not contain the advanced security functionality which 5 * is only needed by the security_manager's initiators. 6 * 7 * Copyright (C) 2008 Panasas Inc. All rights reserved. 8 * 9 * Authors: 10 * Boaz Harrosh <bharrosh@panasas.com> 11 * Benny Halevy <bhalevy@panasas.com> 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License version 2 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. Neither the name of the Panasas company nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 32 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 36 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42#include <linux/slab.h> 43 44#include <scsi/osd_initiator.h> 45#include <scsi/osd_sec.h> 46#include <scsi/osd_attributes.h> 47#include <scsi/osd_sense.h> 48 49#include <scsi/scsi_device.h> 50 51#include "osd_debug.h" 52 53#ifndef __unused 54# define __unused __attribute__((unused)) 55#endif 56 57enum { OSD_REQ_RETRIES = 1 }; 58 59MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>"); 60MODULE_DESCRIPTION("open-osd initiator library libosd.ko"); 61MODULE_LICENSE("GPL"); 62 63static inline void build_test(void) 64{ 65 /* structures were not packed */ 66 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN); 67 BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN); 68 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN); 69} 70 71static const char *_osd_ver_desc(struct osd_request *or) 72{ 73 return osd_req_is_ver1(or) ? "OSD1" : "OSD2"; 74} 75 76#define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len) 77 78static int _osd_get_print_system_info(struct osd_dev *od, 79 void *caps, struct osd_dev_info *odi) 80{ 81 struct osd_request *or; 82 struct osd_attr get_attrs[] = { 83 ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8), 84 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16), 85 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32), 86 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4), 87 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/), 88 ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/), 89 ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8), 90 ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8), 91 ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8), 92 ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6), 93 /* IBM-OSD-SIM Has a bug with this one put it last */ 94 ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20), 95 }; 96 void *iter = NULL, *pFirst; 97 int nelem = ARRAY_SIZE(get_attrs), a = 0; 98 int ret; 99 100 or = osd_start_request(od, GFP_KERNEL); 101 if (!or) 102 return -ENOMEM; 103 104 /* get attrs */ 105 osd_req_get_attributes(or, &osd_root_object); 106 osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs)); 107 108 ret = osd_finalize_request(or, 0, caps, NULL); 109 if (ret) 110 goto out; 111 112 ret = osd_execute_request(or); 113 if (ret) { 114 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret); 115 goto out; 116 } 117 118 osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter); 119 120 OSD_INFO("Detected %s device\n", 121 _osd_ver_desc(or)); 122 123 pFirst = get_attrs[a++].val_ptr; 124 OSD_INFO("VENDOR_IDENTIFICATION [%s]\n", 125 (char *)pFirst); 126 127 pFirst = get_attrs[a++].val_ptr; 128 OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n", 129 (char *)pFirst); 130 131 pFirst = get_attrs[a++].val_ptr; 132 OSD_INFO("PRODUCT_MODEL [%s]\n", 133 (char *)pFirst); 134 135 pFirst = get_attrs[a++].val_ptr; 136 OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n", 137 pFirst ? get_unaligned_be32(pFirst) : ~0U); 138 139 pFirst = get_attrs[a++].val_ptr; 140 OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n", 141 (char *)pFirst); 142 143 odi->osdname_len = get_attrs[a].len; 144 /* Avoid NULL for memcmp optimization 0-length is good enough */ 145 odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL); 146 if (odi->osdname_len) 147 memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len); 148 OSD_INFO("OSD_NAME [%s]\n", odi->osdname); 149 a++; 150 151 pFirst = get_attrs[a++].val_ptr; 152 OSD_INFO("TOTAL_CAPACITY [0x%llx]\n", 153 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL); 154 155 pFirst = get_attrs[a++].val_ptr; 156 OSD_INFO("USED_CAPACITY [0x%llx]\n", 157 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL); 158 159 pFirst = get_attrs[a++].val_ptr; 160 OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n", 161 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL); 162 163 if (a >= nelem) 164 goto out; 165 166 pFirst = get_attrs[a++].val_ptr; 167 OSD_INFO("CLOCK [0x%02x%02x%02x%02x%02x%02x]\n", 168 ((char *)pFirst)[0], ((char *)pFirst)[1], 169 ((char *)pFirst)[2], ((char *)pFirst)[3], 170 ((char *)pFirst)[4], ((char *)pFirst)[5]); 171 172 if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */ 173 unsigned len = get_attrs[a].len; 174 char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */ 175 176 hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1, 177 sid_dump, sizeof(sid_dump), true); 178 OSD_INFO("OSD_SYSTEM_ID(%d)\n" 179 " [%s]\n", len, sid_dump); 180 181 if (unlikely(len > sizeof(odi->systemid))) { 182 OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). " 183 "device idetification might not work\n", len); 184 len = sizeof(odi->systemid); 185 } 186 odi->systemid_len = len; 187 memcpy(odi->systemid, get_attrs[a].val_ptr, len); 188 a++; 189 } 190out: 191 osd_end_request(or); 192 return ret; 193} 194 195int osd_auto_detect_ver(struct osd_dev *od, 196 void *caps, struct osd_dev_info *odi) 197{ 198 int ret; 199 200 /* Auto-detect the osd version */ 201 ret = _osd_get_print_system_info(od, caps, odi); 202 if (ret) { 203 osd_dev_set_ver(od, OSD_VER1); 204 OSD_DEBUG("converting to OSD1\n"); 205 ret = _osd_get_print_system_info(od, caps, odi); 206 } 207 208 return ret; 209} 210EXPORT_SYMBOL(osd_auto_detect_ver); 211 212static unsigned _osd_req_cdb_len(struct osd_request *or) 213{ 214 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN; 215} 216 217static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len) 218{ 219 return osd_req_is_ver1(or) ? 220 osdv1_attr_list_elem_size(len) : 221 osdv2_attr_list_elem_size(len); 222} 223 224static void _osd_req_alist_elem_encode(struct osd_request *or, 225 void *attr_last, const struct osd_attr *oa) 226{ 227 if (osd_req_is_ver1(or)) { 228 struct osdv1_attributes_list_element *attr = attr_last; 229 230 attr->attr_page = cpu_to_be32(oa->attr_page); 231 attr->attr_id = cpu_to_be32(oa->attr_id); 232 attr->attr_bytes = cpu_to_be16(oa->len); 233 memcpy(attr->attr_val, oa->val_ptr, oa->len); 234 } else { 235 struct osdv2_attributes_list_element *attr = attr_last; 236 237 attr->attr_page = cpu_to_be32(oa->attr_page); 238 attr->attr_id = cpu_to_be32(oa->attr_id); 239 attr->attr_bytes = cpu_to_be16(oa->len); 240 memcpy(attr->attr_val, oa->val_ptr, oa->len); 241 } 242} 243 244static int _osd_req_alist_elem_decode(struct osd_request *or, 245 void *cur_p, struct osd_attr *oa, unsigned max_bytes) 246{ 247 unsigned inc; 248 if (osd_req_is_ver1(or)) { 249 struct osdv1_attributes_list_element *attr = cur_p; 250 251 if (max_bytes < sizeof(*attr)) 252 return -1; 253 254 oa->len = be16_to_cpu(attr->attr_bytes); 255 inc = _osd_req_alist_elem_size(or, oa->len); 256 if (inc > max_bytes) 257 return -1; 258 259 oa->attr_page = be32_to_cpu(attr->attr_page); 260 oa->attr_id = be32_to_cpu(attr->attr_id); 261 262 /* OSD1: On empty attributes we return a pointer to 2 bytes 263 * of zeros. This keeps similar behaviour with OSD2. 264 * (See below) 265 */ 266 oa->val_ptr = likely(oa->len) ? attr->attr_val : 267 (u8 *)&attr->attr_bytes; 268 } else { 269 struct osdv2_attributes_list_element *attr = cur_p; 270 271 if (max_bytes < sizeof(*attr)) 272 return -1; 273 274 oa->len = be16_to_cpu(attr->attr_bytes); 275 inc = _osd_req_alist_elem_size(or, oa->len); 276 if (inc > max_bytes) 277 return -1; 278 279 oa->attr_page = be32_to_cpu(attr->attr_page); 280 oa->attr_id = be32_to_cpu(attr->attr_id); 281 282 /* OSD2: For convenience, on empty attributes, we return 8 bytes 283 * of zeros here. This keeps the same behaviour with OSD2r04, 284 * and is nice with null terminating ASCII fields. 285 * oa->val_ptr == NULL marks the end-of-list, or error. 286 */ 287 oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved; 288 } 289 return inc; 290} 291 292static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head) 293{ 294 return osd_req_is_ver1(or) ? 295 osdv1_list_size(list_head) : 296 osdv2_list_size(list_head); 297} 298 299static unsigned _osd_req_sizeof_alist_header(struct osd_request *or) 300{ 301 return osd_req_is_ver1(or) ? 302 sizeof(struct osdv1_attributes_list_header) : 303 sizeof(struct osdv2_attributes_list_header); 304} 305 306static void _osd_req_set_alist_type(struct osd_request *or, 307 void *list, int list_type) 308{ 309 if (osd_req_is_ver1(or)) { 310 struct osdv1_attributes_list_header *attr_list = list; 311 312 memset(attr_list, 0, sizeof(*attr_list)); 313 attr_list->type = list_type; 314 } else { 315 struct osdv2_attributes_list_header *attr_list = list; 316 317 memset(attr_list, 0, sizeof(*attr_list)); 318 attr_list->type = list_type; 319 } 320} 321 322static bool _osd_req_is_alist_type(struct osd_request *or, 323 void *list, int list_type) 324{ 325 if (!list) 326 return false; 327 328 if (osd_req_is_ver1(or)) { 329 struct osdv1_attributes_list_header *attr_list = list; 330 331 return attr_list->type == list_type; 332 } else { 333 struct osdv2_attributes_list_header *attr_list = list; 334 335 return attr_list->type == list_type; 336 } 337} 338 339/* This is for List-objects not Attributes-Lists */ 340static void _osd_req_encode_olist(struct osd_request *or, 341 struct osd_obj_id_list *list) 342{ 343 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); 344 345 if (osd_req_is_ver1(or)) { 346 cdbh->v1.list_identifier = list->list_identifier; 347 cdbh->v1.start_address = list->continuation_id; 348 } else { 349 cdbh->v2.list_identifier = list->list_identifier; 350 cdbh->v2.start_address = list->continuation_id; 351 } 352} 353 354static osd_cdb_offset osd_req_encode_offset(struct osd_request *or, 355 u64 offset, unsigned *padding) 356{ 357 return __osd_encode_offset(offset, padding, 358 osd_req_is_ver1(or) ? 359 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT, 360 OSD_OFFSET_MAX_SHIFT); 361} 362 363static struct osd_security_parameters * 364_osd_req_sec_params(struct osd_request *or) 365{ 366 struct osd_cdb *ocdb = &or->cdb; 367 368 if (osd_req_is_ver1(or)) 369 return (struct osd_security_parameters *)&ocdb->v1.sec_params; 370 else 371 return (struct osd_security_parameters *)&ocdb->v2.sec_params; 372} 373 374void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device) 375{ 376 memset(osdd, 0, sizeof(*osdd)); 377 osdd->scsi_device = scsi_device; 378 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT; 379#ifdef OSD_VER1_SUPPORT 380 osdd->version = OSD_VER2; 381#endif 382 /* TODO: Allocate pools for osd_request attributes ... */ 383} 384EXPORT_SYMBOL(osd_dev_init); 385 386void osd_dev_fini(struct osd_dev *osdd) 387{ 388 /* TODO: De-allocate pools */ 389 390 osdd->scsi_device = NULL; 391} 392EXPORT_SYMBOL(osd_dev_fini); 393 394static struct osd_request *_osd_request_alloc(gfp_t gfp) 395{ 396 struct osd_request *or; 397 398 /* TODO: Use mempool with one saved request */ 399 or = kzalloc(sizeof(*or), gfp); 400 return or; 401} 402 403static void _osd_request_free(struct osd_request *or) 404{ 405 kfree(or); 406} 407 408struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp) 409{ 410 struct osd_request *or; 411 412 or = _osd_request_alloc(gfp); 413 if (!or) 414 return NULL; 415 416 or->osd_dev = dev; 417 or->alloc_flags = gfp; 418 or->timeout = dev->def_timeout; 419 or->retries = OSD_REQ_RETRIES; 420 421 return or; 422} 423EXPORT_SYMBOL(osd_start_request); 424 425static void _osd_free_seg(struct osd_request *or __unused, 426 struct _osd_req_data_segment *seg) 427{ 428 if (!seg->buff || !seg->alloc_size) 429 return; 430 431 kfree(seg->buff); 432 seg->buff = NULL; 433 seg->alloc_size = 0; 434} 435 436static void _put_request(struct request *rq) 437{ 438 /* 439 * If osd_finalize_request() was called but the request was not 440 * executed through the block layer, then we must release BIOs. 441 * TODO: Keep error code in or->async_error. Need to audit all 442 * code paths. 443 */ 444 if (unlikely(rq->bio)) 445 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq)); 446 else 447 blk_put_request(rq); 448} 449 450void osd_end_request(struct osd_request *or) 451{ 452 struct request *rq = or->request; 453 454 _osd_free_seg(or, &or->set_attr); 455 _osd_free_seg(or, &or->enc_get_attr); 456 _osd_free_seg(or, &or->get_attr); 457 458 if (rq) { 459 if (rq->next_rq) { 460 _put_request(rq->next_rq); 461 rq->next_rq = NULL; 462 } 463 464 _put_request(rq); 465 } 466 _osd_request_free(or); 467} 468EXPORT_SYMBOL(osd_end_request); 469 470static void _set_error_resid(struct osd_request *or, struct request *req, 471 int error) 472{ 473 or->async_error = error; 474 or->req_errors = req->errors ? : error; 475 or->sense_len = req->sense_len; 476 if (or->out.req) 477 or->out.residual = or->out.req->resid_len; 478 if (or->in.req) 479 or->in.residual = or->in.req->resid_len; 480} 481 482int osd_execute_request(struct osd_request *or) 483{ 484 int error = blk_execute_rq(or->request->q, NULL, or->request, 0); 485 486 _set_error_resid(or, or->request, error); 487 return error; 488} 489EXPORT_SYMBOL(osd_execute_request); 490 491static void osd_request_async_done(struct request *req, int error) 492{ 493 struct osd_request *or = req->end_io_data; 494 495 _set_error_resid(or, req, error); 496 if (req->next_rq) { 497 __blk_put_request(req->q, req->next_rq); 498 req->next_rq = NULL; 499 } 500 501 __blk_put_request(req->q, req); 502 or->request = NULL; 503 or->in.req = NULL; 504 or->out.req = NULL; 505 506 if (or->async_done) 507 or->async_done(or, or->async_private); 508 else 509 osd_end_request(or); 510} 511 512int osd_execute_request_async(struct osd_request *or, 513 osd_req_done_fn *done, void *private) 514{ 515 or->request->end_io_data = or; 516 or->async_private = private; 517 or->async_done = done; 518 519 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0, 520 osd_request_async_done); 521 return 0; 522} 523EXPORT_SYMBOL(osd_execute_request_async); 524 525u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT]; 526u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT]; 527 528static int _osd_realloc_seg(struct osd_request *or, 529 struct _osd_req_data_segment *seg, unsigned max_bytes) 530{ 531 void *buff; 532 533 if (seg->alloc_size >= max_bytes) 534 return 0; 535 536 buff = krealloc(seg->buff, max_bytes, or->alloc_flags); 537 if (!buff) { 538 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes, 539 seg->alloc_size); 540 return -ENOMEM; 541 } 542 543 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size); 544 seg->buff = buff; 545 seg->alloc_size = max_bytes; 546 return 0; 547} 548 549static int _alloc_set_attr_list(struct osd_request *or, 550 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes) 551{ 552 unsigned total_bytes = add_bytes; 553 554 for (; nelem; --nelem, ++oa) 555 total_bytes += _osd_req_alist_elem_size(or, oa->len); 556 557 OSD_DEBUG("total_bytes=%d\n", total_bytes); 558 return _osd_realloc_seg(or, &or->set_attr, total_bytes); 559} 560 561static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes) 562{ 563 OSD_DEBUG("total_bytes=%d\n", max_bytes); 564 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes); 565} 566 567static int _alloc_get_attr_list(struct osd_request *or) 568{ 569 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes); 570 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes); 571} 572 573/* 574 * Common to all OSD commands 575 */ 576 577static void _osdv1_req_encode_common(struct osd_request *or, 578 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len) 579{ 580 struct osdv1_cdb *ocdb = &or->cdb.v1; 581 582 /* 583 * For speed, the commands 584 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C 585 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D 586 * are not supported here. Should pass zero and set after the call 587 */ 588 act &= cpu_to_be16(~0x0080); /* V1 action code */ 589 590 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act)); 591 592 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD; 593 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH; 594 ocdb->h.varlen_cdb.service_action = act; 595 596 ocdb->h.partition = cpu_to_be64(obj->partition); 597 ocdb->h.object = cpu_to_be64(obj->id); 598 ocdb->h.v1.length = cpu_to_be64(len); 599 ocdb->h.v1.start_address = cpu_to_be64(offset); 600} 601 602static void _osdv2_req_encode_common(struct osd_request *or, 603 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len) 604{ 605 struct osdv2_cdb *ocdb = &or->cdb.v2; 606 607 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act)); 608 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; 612 613 ocdb->h.partition = cpu_to_be64(obj->partition); 614 ocdb->h.object = cpu_to_be64(obj->id); 615 ocdb->h.v2.length = cpu_to_be64(len); 616 ocdb->h.v2.start_address = cpu_to_be64(offset); 617} 618 619static void _osd_req_encode_common(struct osd_request *or, 620 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len) 621{ 622 if (osd_req_is_ver1(or)) 623 _osdv1_req_encode_common(or, act, obj, offset, len); 624 else 625 _osdv2_req_encode_common(or, act, obj, offset, len); 626} 627 628/* 629 * Device commands 630 */ 631/*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */ 632/*TODO: void osd_req_set_master_key(struct osd_request *, ...); */ 633 634void osd_req_format(struct osd_request *or, u64 tot_capacity) 635{ 636 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0, 637 tot_capacity); 638} 639EXPORT_SYMBOL(osd_req_format); 640 641int osd_req_list_dev_partitions(struct osd_request *or, 642 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem) 643{ 644 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem); 645} 646EXPORT_SYMBOL(osd_req_list_dev_partitions); 647 648static void _osd_req_encode_flush(struct osd_request *or, 649 enum osd_options_flush_scope_values op) 650{ 651 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb); 652 653 ocdb->command_specific_options = op; 654} 655 656void osd_req_flush_obsd(struct osd_request *or, 657 enum osd_options_flush_scope_values op) 658{ 659 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0); 660 _osd_req_encode_flush(or, op); 661} 662EXPORT_SYMBOL(osd_req_flush_obsd); 663 664/*TODO: void osd_req_perform_scsi_command(struct osd_request *, 665 const u8 *cdb, ...); */ 666/*TODO: void osd_req_task_management(struct osd_request *, ...); */ 667 668/* 669 * Partition commands 670 */ 671static void _osd_req_encode_partition(struct osd_request *or, 672 __be16 act, osd_id partition) 673{ 674 struct osd_obj_id par = { 675 .partition = partition, 676 .id = 0, 677 }; 678 679 _osd_req_encode_common(or, act, &par, 0, 0); 680} 681 682void osd_req_create_partition(struct osd_request *or, osd_id partition) 683{ 684 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition); 685} 686EXPORT_SYMBOL(osd_req_create_partition); 687 688void osd_req_remove_partition(struct osd_request *or, osd_id partition) 689{ 690 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition); 691} 692EXPORT_SYMBOL(osd_req_remove_partition); 693 694/*TODO: void osd_req_set_partition_key(struct osd_request *, 695 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE], 696 u8 seed[OSD_CRYPTO_SEED_SIZE]); */ 697 698static int _osd_req_list_objects(struct osd_request *or, 699 __be16 action, const struct osd_obj_id *obj, osd_id initial_id, 700 struct osd_obj_id_list *list, unsigned nelem) 701{ 702 struct request_queue *q = osd_request_queue(or->osd_dev); 703 u64 len = nelem * sizeof(osd_id) + sizeof(*list); 704 struct bio *bio; 705 706 _osd_req_encode_common(or, action, obj, (u64)initial_id, len); 707 708 if (list->list_identifier) 709 _osd_req_encode_olist(or, list); 710 711 WARN_ON(or->in.bio); 712 bio = bio_map_kern(q, list, len, or->alloc_flags); 713 if (IS_ERR(bio)) { 714 OSD_ERR("!!! Failed to allocate list_objects BIO\n"); 715 return PTR_ERR(bio); 716 } 717 718 bio->bi_rw &= ~REQ_WRITE; 719 or->in.bio = bio; 720 or->in.total_bytes = bio->bi_size; 721 return 0; 722} 723 724int osd_req_list_partition_collections(struct osd_request *or, 725 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list, 726 unsigned nelem) 727{ 728 struct osd_obj_id par = { 729 .partition = partition, 730 .id = 0, 731 }; 732 733 return osd_req_list_collection_objects(or, &par, initial_id, list, 734 nelem); 735} 736EXPORT_SYMBOL(osd_req_list_partition_collections); 737 738int osd_req_list_partition_objects(struct osd_request *or, 739 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list, 740 unsigned nelem) 741{ 742 struct osd_obj_id par = { 743 .partition = partition, 744 .id = 0, 745 }; 746 747 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list, 748 nelem); 749} 750EXPORT_SYMBOL(osd_req_list_partition_objects); 751 752void osd_req_flush_partition(struct osd_request *or, 753 osd_id partition, enum osd_options_flush_scope_values op) 754{ 755 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition); 756 _osd_req_encode_flush(or, op); 757} 758EXPORT_SYMBOL(osd_req_flush_partition); 759 760/* 761 * Collection commands 762 */ 763/*TODO: void osd_req_create_collection(struct osd_request *, 764 const struct osd_obj_id *); */ 765/*TODO: void osd_req_remove_collection(struct osd_request *, 766 const struct osd_obj_id *); */ 767 768int osd_req_list_collection_objects(struct osd_request *or, 769 const struct osd_obj_id *obj, osd_id initial_id, 770 struct osd_obj_id_list *list, unsigned nelem) 771{ 772 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj, 773 initial_id, list, nelem); 774} 775EXPORT_SYMBOL(osd_req_list_collection_objects); 776 777/*TODO: void query(struct osd_request *, ...); V2 */ 778 779void osd_req_flush_collection(struct osd_request *or, 780 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op) 781{ 782 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0); 783 _osd_req_encode_flush(or, op); 784} 785EXPORT_SYMBOL(osd_req_flush_collection); 786 787/*TODO: void get_member_attrs(struct osd_request *, ...); V2 */ 788/*TODO: void set_member_attrs(struct osd_request *, ...); V2 */ 789 790/* 791 * Object commands 792 */ 793void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj) 794{ 795 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0); 796} 797EXPORT_SYMBOL(osd_req_create_object); 798 799void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj) 800{ 801 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0); 802} 803EXPORT_SYMBOL(osd_req_remove_object); 804 805 806/*TODO: void osd_req_create_multi(struct osd_request *or, 807 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem); 808*/ 809 810void osd_req_write(struct osd_request *or, 811 const struct osd_obj_id *obj, u64 offset, 812 struct bio *bio, u64 len) 813{ 814 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len); 815 WARN_ON(or->out.bio || or->out.total_bytes); 816 WARN_ON(0 == (bio->bi_rw & REQ_WRITE)); 817 or->out.bio = bio; 818 or->out.total_bytes = len; 819} 820EXPORT_SYMBOL(osd_req_write); 821 822int osd_req_write_kern(struct osd_request *or, 823 const struct osd_obj_id *obj, u64 offset, void* buff, u64 len) 824{ 825 struct request_queue *req_q = osd_request_queue(or->osd_dev); 826 struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL); 827 828 if (IS_ERR(bio)) 829 return PTR_ERR(bio); 830 831 bio->bi_rw |= REQ_WRITE; 832 osd_req_write(or, obj, offset, bio, len); 833 return 0; 834} 835EXPORT_SYMBOL(osd_req_write_kern); 836 837/*TODO: void osd_req_append(struct osd_request *, 838 const struct osd_obj_id *, struct bio *data_out); */ 839/*TODO: void osd_req_create_write(struct osd_request *, 840 const struct osd_obj_id *, struct bio *data_out, u64 offset); */ 841/*TODO: void osd_req_clear(struct osd_request *, 842 const struct osd_obj_id *, u64 offset, u64 len); */ 843/*TODO: void osd_req_punch(struct osd_request *, 844 const struct osd_obj_id *, u64 offset, u64 len); V2 */ 845 846void osd_req_flush_object(struct osd_request *or, 847 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op, 848 /*V2*/ u64 offset, /*V2*/ u64 len) 849{ 850 if (unlikely(osd_req_is_ver1(or) && (offset || len))) { 851 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n"); 852 offset = 0; 853 len = 0; 854 } 855 856 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len); 857 _osd_req_encode_flush(or, op); 858} 859EXPORT_SYMBOL(osd_req_flush_object); 860 861void osd_req_read(struct osd_request *or, 862 const struct osd_obj_id *obj, u64 offset, 863 struct bio *bio, u64 len) 864{ 865 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len); 866 WARN_ON(or->in.bio || or->in.total_bytes); 867 WARN_ON(bio->bi_rw & REQ_WRITE); 868 or->in.bio = bio; 869 or->in.total_bytes = len; 870} 871EXPORT_SYMBOL(osd_req_read); 872 873int osd_req_read_kern(struct osd_request *or, 874 const struct osd_obj_id *obj, u64 offset, void* buff, u64 len) 875{ 876 struct request_queue *req_q = osd_request_queue(or->osd_dev); 877 struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL); 878 879 if (IS_ERR(bio)) 880 return PTR_ERR(bio); 881 882 osd_req_read(or, obj, offset, bio, len); 883 return 0; 884} 885EXPORT_SYMBOL(osd_req_read_kern); 886 887void osd_req_get_attributes(struct osd_request *or, 888 const struct osd_obj_id *obj) 889{ 890 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0); 891} 892EXPORT_SYMBOL(osd_req_get_attributes); 893 894void osd_req_set_attributes(struct osd_request *or, 895 const struct osd_obj_id *obj) 896{ 897 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0); 898} 899EXPORT_SYMBOL(osd_req_set_attributes); 900 901/* 902 * Attributes List-mode 903 */ 904 905int osd_req_add_set_attr_list(struct osd_request *or, 906 const struct osd_attr *oa, unsigned nelem) 907{ 908 unsigned total_bytes = or->set_attr.total_bytes; 909 void *attr_last; 910 int ret; 911 912 if (or->attributes_mode && 913 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) { 914 WARN_ON(1); 915 return -EINVAL; 916 } 917 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS; 918 919 if (!total_bytes) { /* first-time: allocate and put list header */ 920 total_bytes = _osd_req_sizeof_alist_header(or); 921 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes); 922 if (ret) 923 return ret; 924 _osd_req_set_alist_type(or, or->set_attr.buff, 925 OSD_ATTR_LIST_SET_RETRIEVE); 926 } 927 attr_last = or->set_attr.buff + total_bytes; 928 929 for (; nelem; --nelem) { 930 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len); 931 932 total_bytes += elem_size; 933 if (unlikely(or->set_attr.alloc_size < total_bytes)) { 934 or->set_attr.total_bytes = total_bytes - elem_size; 935 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes); 936 if (ret) 937 return ret; 938 attr_last = 939 or->set_attr.buff + or->set_attr.total_bytes; 940 } 941 942 _osd_req_alist_elem_encode(or, attr_last, oa); 943 944 attr_last += elem_size; 945 ++oa; 946 } 947 948 or->set_attr.total_bytes = total_bytes; 949 return 0; 950} 951EXPORT_SYMBOL(osd_req_add_set_attr_list); 952 953static int _req_append_segment(struct osd_request *or, 954 unsigned padding, struct _osd_req_data_segment *seg, 955 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io) 956{ 957 void *pad_buff; 958 int ret; 959 960 if (padding) { 961 /* check if we can just add it to last buffer */ 962 if (last_seg && 963 (padding <= last_seg->alloc_size - last_seg->total_bytes)) 964 pad_buff = last_seg->buff + last_seg->total_bytes; 965 else 966 pad_buff = io->pad_buff; 967 968 ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding, 969 or->alloc_flags); 970 if (ret) 971 return ret; 972 io->total_bytes += padding; 973 } 974 975 ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes, 976 or->alloc_flags); 977 if (ret) 978 return ret; 979 980 io->total_bytes += seg->total_bytes; 981 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff, 982 seg->total_bytes); 983 return 0; 984} 985 986static int _osd_req_finalize_set_attr_list(struct osd_request *or) 987{ 988 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); 989 unsigned padding; 990 int ret; 991 992 if (!or->set_attr.total_bytes) { 993 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED; 994 return 0; 995 } 996 997 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes); 998 cdbh->attrs_list.set_attr_offset = 999 osd_req_encode_offset(or, or->out.total_bytes, &padding); 1000 1001 ret = _req_append_segment(or, padding, &or->set_attr, 1002 or->out.last_seg, &or->out); 1003 if (ret) 1004 return ret; 1005 1006 or->out.last_seg = &or->set_attr; 1007 return 0; 1008} 1009 1010int osd_req_add_get_attr_list(struct osd_request *or, 1011 const struct osd_attr *oa, unsigned nelem) 1012{ 1013 unsigned total_bytes = or->enc_get_attr.total_bytes; 1014 void *attr_last; 1015 int ret; 1016 1017 if (or->attributes_mode && 1018 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) { 1019 WARN_ON(1); 1020 return -EINVAL; 1021 } 1022 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS; 1023 1024 /* first time calc data-in list header size */ 1025 if (!or->get_attr.total_bytes) 1026 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or); 1027 1028 /* calc data-out info */ 1029 if (!total_bytes) { /* first-time: allocate and put list header */ 1030 unsigned max_bytes; 1031 1032 total_bytes = _osd_req_sizeof_alist_header(or); 1033 max_bytes = total_bytes + 1034 nelem * sizeof(struct osd_attributes_list_attrid); 1035 ret = _alloc_get_attr_desc(or, max_bytes); 1036 if (ret) 1037 return ret; 1038 1039 _osd_req_set_alist_type(or, or->enc_get_attr.buff, 1040 OSD_ATTR_LIST_GET); 1041 } 1042 attr_last = or->enc_get_attr.buff + total_bytes; 1043 1044 for (; nelem; --nelem) { 1045 struct osd_attributes_list_attrid *attrid; 1046 const unsigned cur_size = sizeof(*attrid); 1047 1048 total_bytes += cur_size; 1049 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) { 1050 or->enc_get_attr.total_bytes = total_bytes - cur_size; 1051 ret = _alloc_get_attr_desc(or, 1052 total_bytes + nelem * sizeof(*attrid)); 1053 if (ret) 1054 return ret; 1055 attr_last = or->enc_get_attr.buff + 1056 or->enc_get_attr.total_bytes; 1057 } 1058 1059 attrid = attr_last; 1060 attrid->attr_page = cpu_to_be32(oa->attr_page); 1061 attrid->attr_id = cpu_to_be32(oa->attr_id); 1062 1063 attr_last += cur_size; 1064 1065 /* calc data-in size */ 1066 or->get_attr.total_bytes += 1067 _osd_req_alist_elem_size(or, oa->len); 1068 ++oa; 1069 } 1070 1071 or->enc_get_attr.total_bytes = total_bytes; 1072 1073 OSD_DEBUG( 1074 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n", 1075 or->get_attr.total_bytes, 1076 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or), 1077 or->enc_get_attr.total_bytes, 1078 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or)) 1079 / sizeof(struct osd_attributes_list_attrid)); 1080 1081 return 0; 1082} 1083EXPORT_SYMBOL(osd_req_add_get_attr_list); 1084 1085static int _osd_req_finalize_get_attr_list(struct osd_request *or) 1086{ 1087 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); 1088 unsigned out_padding; 1089 unsigned in_padding; 1090 int ret; 1091 1092 if (!or->enc_get_attr.total_bytes) { 1093 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED; 1094 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED; 1095 return 0; 1096 } 1097 1098 ret = _alloc_get_attr_list(or); 1099 if (ret) 1100 return ret; 1101 1102 /* The out-going buffer info update */ 1103 OSD_DEBUG("out-going\n"); 1104 cdbh->attrs_list.get_attr_desc_bytes = 1105 cpu_to_be32(or->enc_get_attr.total_bytes); 1106 1107 cdbh->attrs_list.get_attr_desc_offset = 1108 osd_req_encode_offset(or, or->out.total_bytes, &out_padding); 1109 1110 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, 1111 or->out.last_seg, &or->out); 1112 if (ret) 1113 return ret; 1114 or->out.last_seg = &or->enc_get_attr; 1115 1116 /* The incoming buffer info update */ 1117 OSD_DEBUG("in-coming\n"); 1118 cdbh->attrs_list.get_attr_alloc_length = 1119 cpu_to_be32(or->get_attr.total_bytes); 1120 1121 cdbh->attrs_list.get_attr_offset = 1122 osd_req_encode_offset(or, or->in.total_bytes, &in_padding); 1123 1124 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL, 1125 &or->in); 1126 if (ret) 1127 return ret; 1128 or->in.last_seg = &or->get_attr; 1129 1130 return 0; 1131} 1132 1133int osd_req_decode_get_attr_list(struct osd_request *or, 1134 struct osd_attr *oa, int *nelem, void **iterator) 1135{ 1136 unsigned cur_bytes, returned_bytes; 1137 int n; 1138 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or); 1139 void *cur_p; 1140 1141 if (!_osd_req_is_alist_type(or, or->get_attr.buff, 1142 OSD_ATTR_LIST_SET_RETRIEVE)) { 1143 oa->attr_page = 0; 1144 oa->attr_id = 0; 1145 oa->val_ptr = NULL; 1146 oa->len = 0; 1147 *iterator = NULL; 1148 return 0; 1149 } 1150 1151 if (*iterator) { 1152 BUG_ON((*iterator < or->get_attr.buff) || 1153 (or->get_attr.buff + or->get_attr.alloc_size < *iterator)); 1154 cur_p = *iterator; 1155 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list; 1156 returned_bytes = or->get_attr.total_bytes; 1157 } else { /* first time decode the list header */ 1158 cur_bytes = sizeof_attr_list; 1159 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) + 1160 sizeof_attr_list; 1161 1162 cur_p = or->get_attr.buff + sizeof_attr_list; 1163 1164 if (returned_bytes > or->get_attr.alloc_size) { 1165 OSD_DEBUG("target report: space was not big enough! " 1166 "Allocate=%u Needed=%u\n", 1167 or->get_attr.alloc_size, 1168 returned_bytes + sizeof_attr_list); 1169 1170 returned_bytes = 1171 or->get_attr.alloc_size - sizeof_attr_list; 1172 } 1173 or->get_attr.total_bytes = returned_bytes; 1174 } 1175 1176 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) { 1177 int inc = _osd_req_alist_elem_decode(or, cur_p, oa, 1178 returned_bytes - cur_bytes); 1179 1180 if (inc < 0) { 1181 OSD_ERR("BAD FOOD from target. list not valid!" 1182 "c=%d r=%d n=%d\n", 1183 cur_bytes, returned_bytes, n); 1184 oa->val_ptr = NULL; 1185 cur_bytes = returned_bytes; /* break the caller loop */ 1186 break; 1187 } 1188 1189 cur_bytes += inc; 1190 cur_p += inc; 1191 ++oa; 1192 } 1193 1194 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL; 1195 *nelem = n; 1196 return returned_bytes - cur_bytes; 1197} 1198EXPORT_SYMBOL(osd_req_decode_get_attr_list); 1199 1200/* 1201 * Attributes Page-mode 1202 */ 1203 1204int osd_req_add_get_attr_page(struct osd_request *or, 1205 u32 page_id, void *attar_page, unsigned max_page_len, 1206 const struct osd_attr *set_one_attr) 1207{ 1208 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); 1209 1210 if (or->attributes_mode && 1211 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) { 1212 WARN_ON(1); 1213 return -EINVAL; 1214 } 1215 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE; 1216 1217 or->get_attr.buff = attar_page; 1218 or->get_attr.total_bytes = max_page_len; 1219 1220 or->set_attr.buff = set_one_attr->val_ptr; 1221 or->set_attr.total_bytes = set_one_attr->len; 1222 1223 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id); 1224 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len); 1225 /* ocdb->attrs_page.get_attr_offset; */ 1226 1227 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page); 1228 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id); 1229 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len); 1230 /* ocdb->attrs_page.set_attr_offset; */ 1231 return 0; 1232} 1233EXPORT_SYMBOL(osd_req_add_get_attr_page); 1234 1235static int _osd_req_finalize_attr_page(struct osd_request *or) 1236{ 1237 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); 1238 unsigned in_padding, out_padding; 1239 int ret; 1240 1241 /* returned page */ 1242 cdbh->attrs_page.get_attr_offset = 1243 osd_req_encode_offset(or, or->in.total_bytes, &in_padding); 1244 1245 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL, 1246 &or->in); 1247 if (ret) 1248 return ret; 1249 1250 /* set one value */ 1251 cdbh->attrs_page.set_attr_offset = 1252 osd_req_encode_offset(or, or->out.total_bytes, &out_padding); 1253 1254 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL, 1255 &or->out); 1256 return ret; 1257} 1258 1259static inline void osd_sec_parms_set_out_offset(bool is_v1, 1260 struct osd_security_parameters *sec_parms, osd_cdb_offset offset) 1261{ 1262 if (is_v1) 1263 sec_parms->v1.data_out_integrity_check_offset = offset; 1264 else 1265 sec_parms->v2.data_out_integrity_check_offset = offset; 1266} 1267 1268static inline void osd_sec_parms_set_in_offset(bool is_v1, 1269 struct osd_security_parameters *sec_parms, osd_cdb_offset offset) 1270{ 1271 if (is_v1) 1272 sec_parms->v1.data_in_integrity_check_offset = offset; 1273 else 1274 sec_parms->v2.data_in_integrity_check_offset = offset; 1275} 1276 1277static int _osd_req_finalize_data_integrity(struct osd_request *or, 1278 bool has_in, bool has_out, u64 out_data_bytes, const u8 *cap_key) 1279{ 1280 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or); 1281 int ret; 1282 1283 if (!osd_is_sec_alldata(sec_parms)) 1284 return 0; 1285 1286 if (has_out) { 1287 struct _osd_req_data_segment seg = { 1288 .buff = &or->out_data_integ, 1289 .total_bytes = sizeof(or->out_data_integ), 1290 }; 1291 unsigned pad; 1292 1293 or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes); 1294 or->out_data_integ.set_attributes_bytes = cpu_to_be64( 1295 or->set_attr.total_bytes); 1296 or->out_data_integ.get_attributes_bytes = cpu_to_be64( 1297 or->enc_get_attr.total_bytes); 1298 1299 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms, 1300 osd_req_encode_offset(or, or->out.total_bytes, &pad)); 1301 1302 ret = _req_append_segment(or, pad, &seg, or->out.last_seg, 1303 &or->out); 1304 if (ret) 1305 return ret; 1306 or->out.last_seg = NULL; 1307 1308 /* they are now all chained to request sign them all together */ 1309 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio, 1310 cap_key); 1311 } 1312 1313 if (has_in) { 1314 struct _osd_req_data_segment seg = { 1315 .buff = &or->in_data_integ, 1316 .total_bytes = sizeof(or->in_data_integ), 1317 }; 1318 unsigned pad; 1319 1320 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms, 1321 osd_req_encode_offset(or, or->in.total_bytes, &pad)); 1322 1323 ret = _req_append_segment(or, pad, &seg, or->in.last_seg, 1324 &or->in); 1325 if (ret) 1326 return ret; 1327 1328 or->in.last_seg = NULL; 1329 } 1330 1331 return 0; 1332} 1333 1334/* 1335 * osd_finalize_request and helpers 1336 */ 1337static struct request *_make_request(struct request_queue *q, bool has_write, 1338 struct _osd_io_info *oii, gfp_t flags) 1339{ 1340 if (oii->bio) 1341 return blk_make_request(q, oii->bio, flags); 1342 else { 1343 struct request *req; 1344 1345 req = blk_get_request(q, has_write ? WRITE : READ, flags); 1346 if (unlikely(!req)) 1347 return ERR_PTR(-ENOMEM); 1348 1349 return req; 1350 } 1351} 1352 1353static int _init_blk_request(struct osd_request *or, 1354 bool has_in, bool has_out) 1355{ 1356 gfp_t flags = or->alloc_flags; 1357 struct scsi_device *scsi_device = or->osd_dev->scsi_device; 1358 struct request_queue *q = scsi_device->request_queue; 1359 struct request *req; 1360 int ret; 1361 1362 req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags); 1363 if (IS_ERR(req)) { 1364 ret = PTR_ERR(req); 1365 goto out; 1366 } 1367 1368 or->request = req; 1369 req->cmd_type = REQ_TYPE_BLOCK_PC; 1370 req->cmd_flags |= REQ_QUIET; 1371 1372 req->timeout = or->timeout; 1373 req->retries = or->retries; 1374 req->sense = or->sense; 1375 req->sense_len = 0; 1376 1377 if (has_out) { 1378 or->out.req = req; 1379 if (has_in) { 1380 /* allocate bidi request */ 1381 req = _make_request(q, false, &or->in, flags); 1382 if (IS_ERR(req)) { 1383 OSD_DEBUG("blk_get_request for bidi failed\n"); 1384 ret = PTR_ERR(req); 1385 goto out; 1386 } 1387 req->cmd_type = REQ_TYPE_BLOCK_PC; 1388 or->in.req = or->request->next_rq = req; 1389 } 1390 } else if (has_in) 1391 or->in.req = req; 1392 1393 ret = 0; 1394out: 1395 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n", 1396 or, has_in, has_out, ret, or->request); 1397 return ret; 1398} 1399 1400int osd_finalize_request(struct osd_request *or, 1401 u8 options, const void *cap, const u8 *cap_key) 1402{ 1403 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); 1404 bool has_in, has_out; 1405 u64 out_data_bytes = or->out.total_bytes; 1406 int ret; 1407 1408 if (options & OSD_REQ_FUA) 1409 cdbh->options |= OSD_CDB_FUA; 1410 1411 if (options & OSD_REQ_DPO) 1412 cdbh->options |= OSD_CDB_DPO; 1413 1414 if (options & OSD_REQ_BYPASS_TIMESTAMPS) 1415 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS; 1416 1417 osd_set_caps(&or->cdb, cap); 1418 1419 has_in = or->in.bio || or->get_attr.total_bytes; 1420 has_out = or->out.bio || or->set_attr.total_bytes || 1421 or->enc_get_attr.total_bytes; 1422 1423 ret = _init_blk_request(or, has_in, has_out); 1424 if (ret) { 1425 OSD_DEBUG("_init_blk_request failed\n"); 1426 return ret; 1427 } 1428 1429 or->out.pad_buff = sg_out_pad_buffer; 1430 or->in.pad_buff = sg_in_pad_buffer; 1431 1432 if (!or->attributes_mode) 1433 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS; 1434 cdbh->command_specific_options |= or->attributes_mode; 1435 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) { 1436 ret = _osd_req_finalize_attr_page(or); 1437 if (ret) { 1438 OSD_DEBUG("_osd_req_finalize_attr_page failed\n"); 1439 return ret; 1440 } 1441 } else { 1442 /* TODO: I think that for the GET_ATTR command these 2 should 1443 * be reversed to keep them in execution order (for embeded 1444 * targets with low memory footprint) 1445 */ 1446 ret = _osd_req_finalize_set_attr_list(or); 1447 if (ret) { 1448 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n"); 1449 return ret; 1450 } 1451 1452 ret = _osd_req_finalize_get_attr_list(or); 1453 if (ret) { 1454 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n"); 1455 return ret; 1456 } 1457 } 1458 1459 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, 1460 out_data_bytes, cap_key); 1461 if (ret) 1462 return ret; 1463 1464 osd_sec_sign_cdb(&or->cdb, cap_key); 1465 1466 or->request->cmd = or->cdb.buff; 1467 or->request->cmd_len = _osd_req_cdb_len(or); 1468 1469 return 0; 1470} 1471EXPORT_SYMBOL(osd_finalize_request); 1472 1473static bool _is_osd_security_code(int code) 1474{ 1475 return (code == osd_security_audit_value_frozen) || 1476 (code == osd_security_working_key_frozen) || 1477 (code == osd_nonce_not_unique) || 1478 (code == osd_nonce_timestamp_out_of_range) || 1479 (code == osd_invalid_dataout_buffer_integrity_check_value); 1480} 1481 1482#define OSD_SENSE_PRINT1(fmt, a...) \ 1483 do { \ 1484 if (__cur_sense_need_output) \ 1485 OSD_ERR(fmt, ##a); \ 1486 } while (0) 1487 1488#define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a) 1489 1490int osd_req_decode_sense_full(struct osd_request *or, 1491 struct osd_sense_info *osi, bool silent, 1492 struct osd_obj_id *bad_obj_list __unused, int max_obj __unused, 1493 struct osd_attr *bad_attr_list, int max_attr) 1494{ 1495 int sense_len, original_sense_len; 1496 struct osd_sense_info local_osi; 1497 struct scsi_sense_descriptor_based *ssdb; 1498 void *cur_descriptor; 1499#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0) 1500 const bool __cur_sense_need_output = false; 1501#else 1502 bool __cur_sense_need_output = !silent; 1503#endif 1504 int ret; 1505 1506 if (likely(!or->req_errors)) 1507 return 0; 1508 1509 osi = osi ? : &local_osi; 1510 memset(osi, 0, sizeof(*osi)); 1511 1512 ssdb = (typeof(ssdb))or->sense; 1513 sense_len = or->sense_len; 1514 if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) { 1515 OSD_ERR("Block-layer returned error(0x%x) but " 1516 "sense_len(%u) || key(%d) is empty\n", 1517 or->req_errors, sense_len, ssdb->sense_key); 1518 goto analyze; 1519 } 1520 1521 if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) { 1522 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n", 1523 ssdb->response_code, sense_len); 1524 goto analyze; 1525 } 1526 1527 osi->key = ssdb->sense_key; 1528 osi->additional_code = be16_to_cpu(ssdb->additional_sense_code); 1529 original_sense_len = ssdb->additional_sense_length + 8; 1530 1531#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1) 1532 if (__cur_sense_need_output) 1533 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error); 1534#endif 1535 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) " 1536 "additional_code=0x%x async_error=%d errors=0x%x\n", 1537 osi->key, original_sense_len, sense_len, 1538 osi->additional_code, or->async_error, 1539 or->req_errors); 1540 1541 if (original_sense_len < sense_len) 1542 sense_len = original_sense_len; 1543 1544 cur_descriptor = ssdb->ssd; 1545 sense_len -= sizeof(*ssdb); 1546 while (sense_len > 0) { 1547 struct scsi_sense_descriptor *ssd = cur_descriptor; 1548 int cur_len = ssd->additional_length + 2; 1549 1550 sense_len -= cur_len; 1551 1552 if (sense_len < 0) 1553 break; /* sense was truncated */ 1554 1555 switch (ssd->descriptor_type) { 1556 case scsi_sense_information: 1557 case scsi_sense_command_specific_information: 1558 { 1559 struct scsi_sense_command_specific_data_descriptor 1560 *sscd = cur_descriptor; 1561 1562 osi->command_info = 1563 get_unaligned_be64(&sscd->information) ; 1564 OSD_SENSE_PRINT2( 1565 "command_specific_information 0x%llx \n", 1566 _LLU(osi->command_info)); 1567 break; 1568 } 1569 case scsi_sense_key_specific: 1570 { 1571 struct scsi_sense_key_specific_data_descriptor 1572 *ssks = cur_descriptor; 1573 1574 osi->sense_info = get_unaligned_be16(&ssks->value); 1575 OSD_SENSE_PRINT2( 1576 "sense_key_specific_information %u" 1577 "sksv_cd_bpv_bp (0x%x)\n", 1578 osi->sense_info, ssks->sksv_cd_bpv_bp); 1579 break; 1580 } 1581 case osd_sense_object_identification: 1582 { 1583 struct osd_sense_identification_data_descriptor 1584 *osidd = cur_descriptor; 1585 1586 osi->not_initiated_command_functions = 1587 le32_to_cpu(osidd->not_initiated_functions); 1588 osi->completed_command_functions = 1589 le32_to_cpu(osidd->completed_functions); 1590 osi->obj.partition = be64_to_cpu(osidd->partition_id); 1591 osi->obj.id = be64_to_cpu(osidd->object_id); 1592 OSD_SENSE_PRINT2( 1593 "object_identification pid=0x%llx oid=0x%llx\n", 1594 _LLU(osi->obj.partition), _LLU(osi->obj.id)); 1595 OSD_SENSE_PRINT2( 1596 "not_initiated_bits(%x) " 1597 "completed_command_bits(%x)\n", 1598 osi->not_initiated_command_functions, 1599 osi->completed_command_functions); 1600 break; 1601 } 1602 case osd_sense_response_integrity_check: 1603 { 1604 struct osd_sense_response_integrity_check_descriptor 1605 *osricd = cur_descriptor; 1606 const unsigned len = 1607 sizeof(osricd->integrity_check_value); 1608 char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */ 1609 1610 hex_dump_to_buffer(osricd->integrity_check_value, len, 1611 32, 1, key_dump, sizeof(key_dump), true); 1612 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump); 1613 } 1614 case osd_sense_attribute_identification: 1615 { 1616 struct osd_sense_attributes_data_descriptor 1617 *osadd = cur_descriptor; 1618 unsigned len = min(cur_len, sense_len); 1619 struct osd_sense_attr *pattr = osadd->sense_attrs; 1620 1621 while (len >= sizeof(*pattr)) { 1622 u32 attr_page = be32_to_cpu(pattr->attr_page); 1623 u32 attr_id = be32_to_cpu(pattr->attr_id); 1624 1625 if (!osi->attr.attr_page) { 1626 osi->attr.attr_page = attr_page; 1627 osi->attr.attr_id = attr_id; 1628 } 1629 1630 if (bad_attr_list && max_attr) { 1631 bad_attr_list->attr_page = attr_page; 1632 bad_attr_list->attr_id = attr_id; 1633 bad_attr_list++; 1634 max_attr--; 1635 } 1636 1637 len -= sizeof(*pattr); 1638 OSD_SENSE_PRINT2( 1639 "osd_sense_attribute_identification" 1640 "attr_page=0x%x attr_id=0x%x\n", 1641 attr_page, attr_id); 1642 } 1643 } 1644 /*These are not legal for OSD*/ 1645 case scsi_sense_field_replaceable_unit: 1646 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n"); 1647 break; 1648 case scsi_sense_stream_commands: 1649 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n"); 1650 break; 1651 case scsi_sense_block_commands: 1652 OSD_SENSE_PRINT2("scsi_sense_block_commands\n"); 1653 break; 1654 case scsi_sense_ata_return: 1655 OSD_SENSE_PRINT2("scsi_sense_ata_return\n"); 1656 break; 1657 default: 1658 if (ssd->descriptor_type <= scsi_sense_Reserved_last) 1659 OSD_SENSE_PRINT2( 1660 "scsi_sense Reserved descriptor (0x%x)", 1661 ssd->descriptor_type); 1662 else 1663 OSD_SENSE_PRINT2( 1664 "scsi_sense Vendor descriptor (0x%x)", 1665 ssd->descriptor_type); 1666 } 1667 1668 cur_descriptor += cur_len; 1669 } 1670 1671analyze: 1672 if (!osi->key) { 1673 /* scsi sense is Empty, the request was never issued to target 1674 * linux return code might tell us what happened. 1675 */ 1676 if (or->async_error == -ENOMEM) 1677 osi->osd_err_pri = OSD_ERR_PRI_RESOURCE; 1678 else 1679 osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE; 1680 ret = or->async_error; 1681 } else if (osi->key <= scsi_sk_recovered_error) { 1682 osi->osd_err_pri = 0; 1683 ret = 0; 1684 } else if (osi->additional_code == scsi_invalid_field_in_cdb) { 1685 if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) { 1686 osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES; 1687 ret = -EFAULT; /* caller should recover from this */ 1688 } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) { 1689 osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND; 1690 ret = -ENOENT; 1691 } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) { 1692 osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS; 1693 ret = -EACCES; 1694 } else { 1695 osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED; 1696 ret = -EINVAL; 1697 } 1698 } else if (osi->additional_code == osd_quota_error) { 1699 osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE; 1700 ret = -ENOSPC; 1701 } else if (_is_osd_security_code(osi->additional_code)) { 1702 osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED; 1703 ret = -EINVAL; 1704 } else { 1705 osi->osd_err_pri = OSD_ERR_PRI_EIO; 1706 ret = -EIO; 1707 } 1708 1709 if (!or->out.residual) 1710 or->out.residual = or->out.total_bytes; 1711 if (!or->in.residual) 1712 or->in.residual = or->in.total_bytes; 1713 1714 return ret; 1715} 1716EXPORT_SYMBOL(osd_req_decode_sense_full); 1717 1718/* 1719 * Implementation of osd_sec.h API 1720 * TODO: Move to a separate osd_sec.c file at a later stage. 1721 */ 1722 1723enum { OSD_SEC_CAP_V1_ALL_CAPS = 1724 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE | 1725 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR | 1726 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC | 1727 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT 1728}; 1729 1730enum { OSD_SEC_CAP_V2_ALL_CAPS = 1731 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT 1732}; 1733 1734void osd_sec_init_nosec_doall_caps(void *caps, 1735 const struct osd_obj_id *obj, bool is_collection, const bool is_v1) 1736{ 1737 struct osd_capability *cap = caps; 1738 u8 type; 1739 u8 descriptor_type; 1740 1741 if (likely(obj->id)) { 1742 if (unlikely(is_collection)) { 1743 type = OSD_SEC_OBJ_COLLECTION; 1744 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ : 1745 OSD_SEC_OBJ_DESC_COL; 1746 } else { 1747 type = OSD_SEC_OBJ_USER; 1748 descriptor_type = OSD_SEC_OBJ_DESC_OBJ; 1749 } 1750 WARN_ON(!obj->partition); 1751 } else { 1752 type = obj->partition ? OSD_SEC_OBJ_PARTITION : 1753 OSD_SEC_OBJ_ROOT; 1754 descriptor_type = OSD_SEC_OBJ_DESC_PAR; 1755 } 1756 1757 memset(cap, 0, sizeof(*cap)); 1758 1759 cap->h.format = OSD_SEC_CAP_FORMAT_VER1; 1760 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */ 1761 cap->h.security_method = OSD_SEC_NOSEC; 1762/* cap->expiration_time; 1763 cap->AUDIT[30-10]; 1764 cap->discriminator[42-30]; 1765 cap->object_created_time; */ 1766 cap->h.object_type = type; 1767 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS); 1768 cap->h.object_descriptor_type = descriptor_type; 1769 cap->od.obj_desc.policy_access_tag = 0; 1770 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition); 1771 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id); 1772} 1773EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps); 1774 1775void osd_set_caps(struct osd_cdb *cdb, const void *caps) 1776{ 1777 bool is_ver1 = true; 1778 /* NOTE: They start at same address */ 1779 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN); 1780} 1781 1782bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused) 1783{ 1784 return false; 1785} 1786 1787void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused) 1788{ 1789} 1790 1791void osd_sec_sign_data(void *data_integ __unused, 1792 struct bio *bio __unused, const u8 *cap_key __unused) 1793{ 1794} 1795 1796/* 1797 * Declared in osd_protocol.h 1798 * 4.12.5 Data-In and Data-Out buffer offsets 1799 * byte offset = mantissa * (2^(exponent+8)) 1800 * Returns the smallest allowed encoded offset that contains given @offset 1801 * The actual encoded offset returned is @offset + *@padding. 1802 */ 1803osd_cdb_offset __osd_encode_offset( 1804 u64 offset, unsigned *padding, int min_shift, int max_shift) 1805{ 1806 u64 try_offset = -1, mod, align; 1807 osd_cdb_offset be32_offset; 1808 int shift; 1809 1810 *padding = 0; 1811 if (!offset) 1812 return 0; 1813 1814 for (shift = min_shift; shift < max_shift; ++shift) { 1815 try_offset = offset >> shift; 1816 if (try_offset < (1 << OSD_OFFSET_MAX_BITS)) 1817 break; 1818 } 1819 1820 BUG_ON(shift == max_shift); 1821 1822 align = 1 << shift; 1823 mod = offset & (align - 1); 1824 if (mod) { 1825 *padding = align - mod; 1826 try_offset += 1; 1827 } 1828 1829 try_offset |= ((shift - 8) & 0xf) << 28; 1830 be32_offset = cpu_to_be32((u32)try_offset); 1831 1832 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n", 1833 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift, 1834 be32_offset, *padding); 1835 return be32_offset; 1836} 1837