Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 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: |
Boaz Harrosh | aa281ac | 2014-10-19 19:38:58 +0300 | [diff] [blame] | 10 | * Boaz Harrosh <ooo@electrozaur.com> |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Paul Gortmaker | acf3368f | 2011-05-27 09:47:43 -0400 | [diff] [blame] | 43 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 44 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 45 | #include <scsi/osd_initiator.h> |
| 46 | #include <scsi/osd_sec.h> |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 47 | #include <scsi/osd_attributes.h> |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 48 | #include <scsi/osd_sense.h> |
| 49 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 50 | #include <scsi/scsi_device.h> |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 51 | #include <scsi/scsi_request.h> |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 52 | |
| 53 | #include "osd_debug.h" |
| 54 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 55 | #ifndef __unused |
| 56 | # define __unused __attribute__((unused)) |
| 57 | #endif |
| 58 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 59 | enum { OSD_REQ_RETRIES = 1 }; |
| 60 | |
Boaz Harrosh | aa281ac | 2014-10-19 19:38:58 +0300 | [diff] [blame] | 61 | MODULE_AUTHOR("Boaz Harrosh <ooo@electrozaur.com>"); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 62 | MODULE_DESCRIPTION("open-osd initiator library libosd.ko"); |
| 63 | MODULE_LICENSE("GPL"); |
| 64 | |
| 65 | static inline void build_test(void) |
| 66 | { |
| 67 | /* structures were not packed */ |
| 68 | BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN); |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 69 | BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 70 | BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN); |
| 71 | } |
| 72 | |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 73 | static const char *_osd_ver_desc(struct osd_request *or) |
| 74 | { |
| 75 | return osd_req_is_ver1(or) ? "OSD1" : "OSD2"; |
| 76 | } |
| 77 | |
| 78 | #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len) |
| 79 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 80 | static int _osd_get_print_system_info(struct osd_dev *od, |
| 81 | void *caps, struct osd_dev_info *odi) |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 82 | { |
| 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), |
| 97 | }; |
| 98 | void *iter = NULL, *pFirst; |
| 99 | int nelem = ARRAY_SIZE(get_attrs), a = 0; |
| 100 | int ret; |
| 101 | |
| 102 | or = osd_start_request(od, GFP_KERNEL); |
| 103 | if (!or) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | /* get attrs */ |
| 107 | osd_req_get_attributes(or, &osd_root_object); |
| 108 | osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs)); |
| 109 | |
| 110 | ret = osd_finalize_request(or, 0, caps, NULL); |
| 111 | if (ret) |
| 112 | goto out; |
| 113 | |
| 114 | ret = osd_execute_request(or); |
| 115 | if (ret) { |
| 116 | OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret); |
| 117 | goto out; |
| 118 | } |
| 119 | |
| 120 | osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter); |
| 121 | |
| 122 | OSD_INFO("Detected %s device\n", |
| 123 | _osd_ver_desc(or)); |
| 124 | |
| 125 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 126 | OSD_INFO("VENDOR_IDENTIFICATION [%s]\n", |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 127 | (char *)pFirst); |
| 128 | |
| 129 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 130 | OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n", |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 131 | (char *)pFirst); |
| 132 | |
| 133 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 134 | OSD_INFO("PRODUCT_MODEL [%s]\n", |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 135 | (char *)pFirst); |
| 136 | |
| 137 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 138 | OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n", |
Boaz Harrosh | 97218a1 | 2009-02-08 18:02:22 +0200 | [diff] [blame] | 139 | pFirst ? get_unaligned_be32(pFirst) : ~0U); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 140 | |
| 141 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 142 | OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n", |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 143 | (char *)pFirst); |
| 144 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 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); |
Dan Carpenter | 6d1f662 | 2013-01-30 10:06:02 +0300 | [diff] [blame] | 148 | if (!odi->osdname) { |
| 149 | ret = -ENOMEM; |
| 150 | goto out; |
| 151 | } |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 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); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 155 | a++; |
| 156 | |
| 157 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 158 | OSD_INFO("TOTAL_CAPACITY [0x%llx]\n", |
Boaz Harrosh | 97218a1 | 2009-02-08 18:02:22 +0200 | [diff] [blame] | 159 | pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 160 | |
| 161 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 162 | OSD_INFO("USED_CAPACITY [0x%llx]\n", |
Boaz Harrosh | 97218a1 | 2009-02-08 18:02:22 +0200 | [diff] [blame] | 163 | pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 164 | |
| 165 | pFirst = get_attrs[a++].val_ptr; |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 166 | OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n", |
Boaz Harrosh | 97218a1 | 2009-02-08 18:02:22 +0200 | [diff] [blame] | 167 | pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL); |
| 168 | |
| 169 | if (a >= nelem) |
| 170 | goto out; |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 171 | |
| 172 | /* FIXME: Where are the time utilities */ |
| 173 | pFirst = get_attrs[a++].val_ptr; |
Rasmus Villemoes | 9a2fcad | 2015-12-08 15:25:16 +0100 | [diff] [blame] | 174 | OSD_INFO("CLOCK [0x%6phN]\n", pFirst); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 175 | |
| 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 */ |
| 179 | |
| 180 | hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1, |
| 181 | sid_dump, sizeof(sid_dump), true); |
Boaz Harrosh | de6b203 | 2009-05-24 20:01:26 +0300 | [diff] [blame] | 182 | OSD_INFO("OSD_SYSTEM_ID(%d)\n" |
| 183 | " [%s]\n", len, sid_dump); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 184 | |
| 185 | if (unlikely(len > sizeof(odi->systemid))) { |
| 186 | OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). " |
Masanari Iida | d939be3 | 2015-02-27 23:52:31 +0900 | [diff] [blame] | 187 | "device identification might not work\n", len); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 188 | len = sizeof(odi->systemid); |
| 189 | } |
| 190 | odi->systemid_len = len; |
| 191 | memcpy(odi->systemid, get_attrs[a].val_ptr, len); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 192 | a++; |
| 193 | } |
| 194 | out: |
| 195 | osd_end_request(or); |
| 196 | return ret; |
| 197 | } |
| 198 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 199 | int osd_auto_detect_ver(struct osd_dev *od, |
| 200 | void *caps, struct osd_dev_info *odi) |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 201 | { |
| 202 | int ret; |
| 203 | |
| 204 | /* Auto-detect the osd version */ |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 205 | ret = _osd_get_print_system_info(od, caps, odi); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 206 | if (ret) { |
| 207 | osd_dev_set_ver(od, OSD_VER1); |
| 208 | OSD_DEBUG("converting to OSD1\n"); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 209 | ret = _osd_get_print_system_info(od, caps, odi); |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | EXPORT_SYMBOL(osd_auto_detect_ver); |
| 215 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 216 | static unsigned _osd_req_cdb_len(struct osd_request *or) |
| 217 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 218 | return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 221 | static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len) |
| 222 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 223 | return osd_req_is_ver1(or) ? |
| 224 | osdv1_attr_list_elem_size(len) : |
| 225 | osdv2_attr_list_elem_size(len); |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Boaz Harrosh | 71f32e3 | 2009-04-19 19:11:42 +0300 | [diff] [blame] | 228 | static void _osd_req_alist_elem_encode(struct osd_request *or, |
| 229 | void *attr_last, const struct osd_attr *oa) |
| 230 | { |
| 231 | if (osd_req_is_ver1(or)) { |
| 232 | struct osdv1_attributes_list_element *attr = attr_last; |
| 233 | |
| 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); |
| 238 | } else { |
| 239 | struct osdv2_attributes_list_element *attr = attr_last; |
| 240 | |
| 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); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static int _osd_req_alist_elem_decode(struct osd_request *or, |
| 249 | void *cur_p, struct osd_attr *oa, unsigned max_bytes) |
| 250 | { |
| 251 | unsigned inc; |
| 252 | if (osd_req_is_ver1(or)) { |
| 253 | struct osdv1_attributes_list_element *attr = cur_p; |
| 254 | |
| 255 | if (max_bytes < sizeof(*attr)) |
| 256 | return -1; |
| 257 | |
| 258 | oa->len = be16_to_cpu(attr->attr_bytes); |
| 259 | inc = _osd_req_alist_elem_size(or, oa->len); |
| 260 | if (inc > max_bytes) |
| 261 | return -1; |
| 262 | |
| 263 | oa->attr_page = be32_to_cpu(attr->attr_page); |
| 264 | oa->attr_id = be32_to_cpu(attr->attr_id); |
| 265 | |
| 266 | /* OSD1: On empty attributes we return a pointer to 2 bytes |
| 267 | * of zeros. This keeps similar behaviour with OSD2. |
| 268 | * (See below) |
| 269 | */ |
| 270 | oa->val_ptr = likely(oa->len) ? attr->attr_val : |
| 271 | (u8 *)&attr->attr_bytes; |
| 272 | } else { |
| 273 | struct osdv2_attributes_list_element *attr = cur_p; |
| 274 | |
| 275 | if (max_bytes < sizeof(*attr)) |
| 276 | return -1; |
| 277 | |
| 278 | oa->len = be16_to_cpu(attr->attr_bytes); |
| 279 | inc = _osd_req_alist_elem_size(or, oa->len); |
| 280 | if (inc > max_bytes) |
| 281 | return -1; |
| 282 | |
| 283 | oa->attr_page = be32_to_cpu(attr->attr_page); |
| 284 | oa->attr_id = be32_to_cpu(attr->attr_id); |
| 285 | |
Boaz Harrosh | e9da4d7 | 2009-04-19 19:17:54 +0300 | [diff] [blame] | 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. |
| 290 | */ |
| 291 | oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved; |
Boaz Harrosh | 71f32e3 | 2009-04-19 19:11:42 +0300 | [diff] [blame] | 292 | } |
| 293 | return inc; |
| 294 | } |
| 295 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 296 | static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head) |
| 297 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 298 | return osd_req_is_ver1(or) ? |
| 299 | osdv1_list_size(list_head) : |
| 300 | osdv2_list_size(list_head); |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static unsigned _osd_req_sizeof_alist_header(struct osd_request *or) |
| 304 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 305 | return osd_req_is_ver1(or) ? |
| 306 | sizeof(struct osdv1_attributes_list_header) : |
| 307 | sizeof(struct osdv2_attributes_list_header); |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void _osd_req_set_alist_type(struct osd_request *or, |
| 311 | void *list, int list_type) |
| 312 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 313 | if (osd_req_is_ver1(or)) { |
| 314 | struct osdv1_attributes_list_header *attr_list = list; |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 315 | |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 316 | memset(attr_list, 0, sizeof(*attr_list)); |
| 317 | attr_list->type = list_type; |
| 318 | } else { |
| 319 | struct osdv2_attributes_list_header *attr_list = list; |
| 320 | |
| 321 | memset(attr_list, 0, sizeof(*attr_list)); |
| 322 | attr_list->type = list_type; |
| 323 | } |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static bool _osd_req_is_alist_type(struct osd_request *or, |
| 327 | void *list, int list_type) |
| 328 | { |
| 329 | if (!list) |
| 330 | return false; |
| 331 | |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 332 | if (osd_req_is_ver1(or)) { |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 333 | struct osdv1_attributes_list_header *attr_list = list; |
| 334 | |
| 335 | return attr_list->type == list_type; |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 336 | } else { |
| 337 | struct osdv2_attributes_list_header *attr_list = list; |
| 338 | |
| 339 | return attr_list->type == list_type; |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 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) |
| 346 | { |
| 347 | struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); |
| 348 | |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 349 | if (osd_req_is_ver1(or)) { |
| 350 | cdbh->v1.list_identifier = list->list_identifier; |
| 351 | cdbh->v1.start_address = list->continuation_id; |
| 352 | } else { |
| 353 | cdbh->v2.list_identifier = list->list_identifier; |
| 354 | cdbh->v2.start_address = list->continuation_id; |
| 355 | } |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 358 | static osd_cdb_offset osd_req_encode_offset(struct osd_request *or, |
| 359 | u64 offset, unsigned *padding) |
| 360 | { |
| 361 | return __osd_encode_offset(offset, padding, |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 362 | osd_req_is_ver1(or) ? |
| 363 | OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT, |
| 364 | OSD_OFFSET_MAX_SHIFT); |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 367 | static struct osd_security_parameters * |
| 368 | _osd_req_sec_params(struct osd_request *or) |
| 369 | { |
| 370 | struct osd_cdb *ocdb = &or->cdb; |
| 371 | |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 372 | if (osd_req_is_ver1(or)) |
Boaz Harrosh | f8d3a64 | 2009-04-19 19:13:39 +0300 | [diff] [blame] | 373 | return (struct osd_security_parameters *)&ocdb->v1.sec_params; |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 374 | else |
Boaz Harrosh | f8d3a64 | 2009-04-19 19:13:39 +0300 | [diff] [blame] | 375 | return (struct osd_security_parameters *)&ocdb->v2.sec_params; |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 378 | void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device) |
| 379 | { |
| 380 | memset(osdd, 0, sizeof(*osdd)); |
| 381 | osdd->scsi_device = scsi_device; |
| 382 | osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT; |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 383 | #ifdef OSD_VER1_SUPPORT |
| 384 | osdd->version = OSD_VER2; |
| 385 | #endif |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 386 | /* TODO: Allocate pools for osd_request attributes ... */ |
| 387 | } |
| 388 | EXPORT_SYMBOL(osd_dev_init); |
| 389 | |
| 390 | void osd_dev_fini(struct osd_dev *osdd) |
| 391 | { |
| 392 | /* TODO: De-allocate pools */ |
| 393 | |
| 394 | osdd->scsi_device = NULL; |
| 395 | } |
| 396 | EXPORT_SYMBOL(osd_dev_fini); |
| 397 | |
| 398 | static struct osd_request *_osd_request_alloc(gfp_t gfp) |
| 399 | { |
| 400 | struct osd_request *or; |
| 401 | |
| 402 | /* TODO: Use mempool with one saved request */ |
| 403 | or = kzalloc(sizeof(*or), gfp); |
| 404 | return or; |
| 405 | } |
| 406 | |
| 407 | static void _osd_request_free(struct osd_request *or) |
| 408 | { |
| 409 | kfree(or); |
| 410 | } |
| 411 | |
| 412 | struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp) |
| 413 | { |
| 414 | struct osd_request *or; |
| 415 | |
| 416 | or = _osd_request_alloc(gfp); |
| 417 | if (!or) |
| 418 | return NULL; |
| 419 | |
| 420 | or->osd_dev = dev; |
| 421 | or->alloc_flags = gfp; |
| 422 | or->timeout = dev->def_timeout; |
| 423 | or->retries = OSD_REQ_RETRIES; |
| 424 | |
| 425 | return or; |
| 426 | } |
| 427 | EXPORT_SYMBOL(osd_start_request); |
| 428 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 429 | static void _osd_free_seg(struct osd_request *or __unused, |
| 430 | struct _osd_req_data_segment *seg) |
| 431 | { |
| 432 | if (!seg->buff || !seg->alloc_size) |
| 433 | return; |
| 434 | |
| 435 | kfree(seg->buff); |
| 436 | seg->buff = NULL; |
| 437 | seg->alloc_size = 0; |
| 438 | } |
| 439 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 440 | static void _put_request(struct request *rq) |
Boaz Harrosh | 8c0bacc | 2009-03-31 20:05:29 +0300 | [diff] [blame] | 441 | { |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 442 | /* |
| 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 |
| 446 | * code paths. |
| 447 | */ |
| 448 | if (unlikely(rq->bio)) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 449 | blk_end_request(rq, BLK_STS_IOERR, blk_rq_bytes(rq)); |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 450 | else |
| 451 | blk_put_request(rq); |
Boaz Harrosh | 8c0bacc | 2009-03-31 20:05:29 +0300 | [diff] [blame] | 452 | } |
| 453 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 454 | void osd_end_request(struct osd_request *or) |
| 455 | { |
| 456 | struct request *rq = or->request; |
| 457 | |
| 458 | if (rq) { |
| 459 | if (rq->next_rq) { |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 460 | _put_request(rq->next_rq); |
Boaz Harrosh | 8c0bacc | 2009-03-31 20:05:29 +0300 | [diff] [blame] | 461 | rq->next_rq = NULL; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 464 | _put_request(rq); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 465 | } |
Boaz Harrosh | c4df46c | 2010-10-19 14:21:34 +0200 | [diff] [blame] | 466 | |
| 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); |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 470 | _osd_free_seg(or, &or->cdb_cont); |
Boaz Harrosh | c4df46c | 2010-10-19 14:21:34 +0200 | [diff] [blame] | 471 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 472 | _osd_request_free(or); |
| 473 | } |
| 474 | EXPORT_SYMBOL(osd_end_request); |
| 475 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 476 | static void _set_error_resid(struct osd_request *or, struct request *req, |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 477 | blk_status_t error) |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 478 | { |
| 479 | or->async_error = error; |
Christoph Hellwig | 10f64ec | 2017-06-03 09:37:55 +0200 | [diff] [blame] | 480 | or->req_errors = scsi_req(req)->result; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 481 | or->sense_len = scsi_req(req)->sense_len; |
| 482 | if (or->sense_len) |
| 483 | memcpy(or->sense, scsi_req(req)->sense, or->sense_len); |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 484 | if (or->out.req) |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 485 | or->out.residual = scsi_req(or->out.req)->resid_len; |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 486 | if (or->in.req) |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 487 | or->in.residual = scsi_req(or->in.req)->resid_len; |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 490 | int osd_execute_request(struct osd_request *or) |
| 491 | { |
Christoph Hellwig | b7819b9 | 2017-04-20 16:02:55 +0200 | [diff] [blame] | 492 | blk_execute_rq(or->request->q, NULL, or->request, 0); |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 493 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 494 | if (scsi_req(or->request)->result) { |
| 495 | _set_error_resid(or, or->request, BLK_STS_IOERR); |
| 496 | return -EIO; |
| 497 | } |
| 498 | |
| 499 | _set_error_resid(or, or->request, BLK_STS_OK); |
| 500 | return 0; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 501 | } |
| 502 | EXPORT_SYMBOL(osd_execute_request); |
| 503 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 504 | static void osd_request_async_done(struct request *req, blk_status_t error) |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 505 | { |
| 506 | struct osd_request *or = req->end_io_data; |
| 507 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 508 | _set_error_resid(or, req, error); |
| 509 | if (req->next_rq) { |
| 510 | __blk_put_request(req->q, req->next_rq); |
| 511 | req->next_rq = NULL; |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 512 | } |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 513 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 514 | __blk_put_request(req->q, req); |
| 515 | or->request = NULL; |
| 516 | or->in.req = NULL; |
| 517 | or->out.req = NULL; |
| 518 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 519 | if (or->async_done) |
| 520 | or->async_done(or, or->async_private); |
| 521 | else |
| 522 | osd_end_request(or); |
| 523 | } |
| 524 | |
| 525 | int osd_execute_request_async(struct osd_request *or, |
| 526 | osd_req_done_fn *done, void *private) |
| 527 | { |
| 528 | or->request->end_io_data = or; |
| 529 | or->async_private = private; |
| 530 | or->async_done = done; |
| 531 | |
| 532 | blk_execute_rq_nowait(or->request->q, NULL, or->request, 0, |
| 533 | osd_request_async_done); |
| 534 | return 0; |
| 535 | } |
| 536 | EXPORT_SYMBOL(osd_execute_request_async); |
| 537 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 538 | u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT]; |
| 539 | u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT]; |
| 540 | |
| 541 | static int _osd_realloc_seg(struct osd_request *or, |
| 542 | struct _osd_req_data_segment *seg, unsigned max_bytes) |
| 543 | { |
| 544 | void *buff; |
| 545 | |
| 546 | if (seg->alloc_size >= max_bytes) |
| 547 | return 0; |
| 548 | |
| 549 | buff = krealloc(seg->buff, max_bytes, or->alloc_flags); |
| 550 | if (!buff) { |
| 551 | OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes, |
| 552 | seg->alloc_size); |
| 553 | return -ENOMEM; |
| 554 | } |
| 555 | |
| 556 | memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size); |
| 557 | seg->buff = buff; |
| 558 | seg->alloc_size = max_bytes; |
| 559 | return 0; |
| 560 | } |
| 561 | |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 562 | static int _alloc_cdb_cont(struct osd_request *or, unsigned total_bytes) |
| 563 | { |
| 564 | OSD_DEBUG("total_bytes=%d\n", total_bytes); |
| 565 | return _osd_realloc_seg(or, &or->cdb_cont, total_bytes); |
| 566 | } |
| 567 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 568 | static int _alloc_set_attr_list(struct osd_request *or, |
| 569 | const struct osd_attr *oa, unsigned nelem, unsigned add_bytes) |
| 570 | { |
| 571 | unsigned total_bytes = add_bytes; |
| 572 | |
| 573 | for (; nelem; --nelem, ++oa) |
| 574 | total_bytes += _osd_req_alist_elem_size(or, oa->len); |
| 575 | |
| 576 | OSD_DEBUG("total_bytes=%d\n", total_bytes); |
| 577 | return _osd_realloc_seg(or, &or->set_attr, total_bytes); |
| 578 | } |
| 579 | |
| 580 | static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes) |
| 581 | { |
| 582 | OSD_DEBUG("total_bytes=%d\n", max_bytes); |
| 583 | return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes); |
| 584 | } |
| 585 | |
| 586 | static int _alloc_get_attr_list(struct osd_request *or) |
| 587 | { |
| 588 | OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes); |
| 589 | return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes); |
| 590 | } |
| 591 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 592 | /* |
| 593 | * Common to all OSD commands |
| 594 | */ |
| 595 | |
| 596 | static void _osdv1_req_encode_common(struct osd_request *or, |
| 597 | __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len) |
| 598 | { |
| 599 | struct osdv1_cdb *ocdb = &or->cdb.v1; |
| 600 | |
| 601 | /* |
| 602 | * For speed, the commands |
| 603 | * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C |
| 604 | * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D |
| 605 | * are not supported here. Should pass zero and set after the call |
| 606 | */ |
| 607 | act &= cpu_to_be16(~0x0080); /* V1 action code */ |
| 608 | |
| 609 | OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act)); |
| 610 | |
| 611 | ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD; |
| 612 | ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH; |
| 613 | ocdb->h.varlen_cdb.service_action = act; |
| 614 | |
| 615 | ocdb->h.partition = cpu_to_be64(obj->partition); |
| 616 | ocdb->h.object = cpu_to_be64(obj->id); |
| 617 | ocdb->h.v1.length = cpu_to_be64(len); |
| 618 | ocdb->h.v1.start_address = cpu_to_be64(offset); |
| 619 | } |
| 620 | |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 621 | static void _osdv2_req_encode_common(struct osd_request *or, |
| 622 | __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len) |
| 623 | { |
| 624 | struct osdv2_cdb *ocdb = &or->cdb.v2; |
| 625 | |
| 626 | OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act)); |
| 627 | |
| 628 | ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD; |
| 629 | ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH; |
| 630 | ocdb->h.varlen_cdb.service_action = act; |
| 631 | |
| 632 | ocdb->h.partition = cpu_to_be64(obj->partition); |
| 633 | ocdb->h.object = cpu_to_be64(obj->id); |
| 634 | ocdb->h.v2.length = cpu_to_be64(len); |
| 635 | ocdb->h.v2.start_address = cpu_to_be64(offset); |
| 636 | } |
| 637 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 638 | static void _osd_req_encode_common(struct osd_request *or, |
| 639 | __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len) |
| 640 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 641 | if (osd_req_is_ver1(or)) |
| 642 | _osdv1_req_encode_common(or, act, obj, offset, len); |
| 643 | else |
| 644 | _osdv2_req_encode_common(or, act, obj, offset, len); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Device commands |
| 649 | */ |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 650 | /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */ |
| 651 | /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */ |
| 652 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 653 | void osd_req_format(struct osd_request *or, u64 tot_capacity) |
| 654 | { |
| 655 | _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0, |
| 656 | tot_capacity); |
| 657 | } |
| 658 | EXPORT_SYMBOL(osd_req_format); |
| 659 | |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 660 | int osd_req_list_dev_partitions(struct osd_request *or, |
| 661 | osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem) |
| 662 | { |
| 663 | return osd_req_list_partition_objects(or, 0, initial_id, list, nelem); |
| 664 | } |
| 665 | EXPORT_SYMBOL(osd_req_list_dev_partitions); |
| 666 | |
| 667 | static void _osd_req_encode_flush(struct osd_request *or, |
| 668 | enum osd_options_flush_scope_values op) |
| 669 | { |
| 670 | struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb); |
| 671 | |
| 672 | ocdb->command_specific_options = op; |
| 673 | } |
| 674 | |
| 675 | void osd_req_flush_obsd(struct osd_request *or, |
| 676 | enum osd_options_flush_scope_values op) |
| 677 | { |
| 678 | _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0); |
| 679 | _osd_req_encode_flush(or, op); |
| 680 | } |
| 681 | EXPORT_SYMBOL(osd_req_flush_obsd); |
| 682 | |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 683 | /*TODO: void osd_req_perform_scsi_command(struct osd_request *, |
| 684 | const u8 *cdb, ...); */ |
| 685 | /*TODO: void osd_req_task_management(struct osd_request *, ...); */ |
| 686 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 687 | /* |
| 688 | * Partition commands |
| 689 | */ |
| 690 | static void _osd_req_encode_partition(struct osd_request *or, |
| 691 | __be16 act, osd_id partition) |
| 692 | { |
| 693 | struct osd_obj_id par = { |
| 694 | .partition = partition, |
| 695 | .id = 0, |
| 696 | }; |
| 697 | |
| 698 | _osd_req_encode_common(or, act, &par, 0, 0); |
| 699 | } |
| 700 | |
| 701 | void osd_req_create_partition(struct osd_request *or, osd_id partition) |
| 702 | { |
| 703 | _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition); |
| 704 | } |
| 705 | EXPORT_SYMBOL(osd_req_create_partition); |
| 706 | |
| 707 | void osd_req_remove_partition(struct osd_request *or, osd_id partition) |
| 708 | { |
| 709 | _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition); |
| 710 | } |
| 711 | EXPORT_SYMBOL(osd_req_remove_partition); |
| 712 | |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 713 | /*TODO: void osd_req_set_partition_key(struct osd_request *, |
| 714 | osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE], |
| 715 | u8 seed[OSD_CRYPTO_SEED_SIZE]); */ |
| 716 | |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 717 | static int _osd_req_list_objects(struct osd_request *or, |
| 718 | __be16 action, const struct osd_obj_id *obj, osd_id initial_id, |
| 719 | struct osd_obj_id_list *list, unsigned nelem) |
| 720 | { |
Boaz Harrosh | fc2fac5 | 2009-05-24 20:04:43 +0300 | [diff] [blame] | 721 | struct request_queue *q = osd_request_queue(or->osd_dev); |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 722 | u64 len = nelem * sizeof(osd_id) + sizeof(*list); |
| 723 | struct bio *bio; |
| 724 | |
| 725 | _osd_req_encode_common(or, action, obj, (u64)initial_id, len); |
| 726 | |
| 727 | if (list->list_identifier) |
| 728 | _osd_req_encode_olist(or, list); |
| 729 | |
| 730 | WARN_ON(or->in.bio); |
| 731 | bio = bio_map_kern(q, list, len, or->alloc_flags); |
Dan Carpenter | bf5e84f | 2009-04-19 19:07:47 +0300 | [diff] [blame] | 732 | if (IS_ERR(bio)) { |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 733 | OSD_ERR("!!! Failed to allocate list_objects BIO\n"); |
Dan Carpenter | bf5e84f | 2009-04-19 19:07:47 +0300 | [diff] [blame] | 734 | return PTR_ERR(bio); |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 737 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 738 | or->in.bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 739 | or->in.total_bytes = bio->bi_iter.bi_size; |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | int osd_req_list_partition_collections(struct osd_request *or, |
| 744 | osd_id partition, osd_id initial_id, struct osd_obj_id_list *list, |
| 745 | unsigned nelem) |
| 746 | { |
| 747 | struct osd_obj_id par = { |
| 748 | .partition = partition, |
| 749 | .id = 0, |
| 750 | }; |
| 751 | |
| 752 | return osd_req_list_collection_objects(or, &par, initial_id, list, |
| 753 | nelem); |
| 754 | } |
| 755 | EXPORT_SYMBOL(osd_req_list_partition_collections); |
| 756 | |
| 757 | int osd_req_list_partition_objects(struct osd_request *or, |
| 758 | osd_id partition, osd_id initial_id, struct osd_obj_id_list *list, |
| 759 | unsigned nelem) |
| 760 | { |
| 761 | struct osd_obj_id par = { |
| 762 | .partition = partition, |
| 763 | .id = 0, |
| 764 | }; |
| 765 | |
| 766 | return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list, |
| 767 | nelem); |
| 768 | } |
| 769 | EXPORT_SYMBOL(osd_req_list_partition_objects); |
| 770 | |
| 771 | void osd_req_flush_partition(struct osd_request *or, |
| 772 | osd_id partition, enum osd_options_flush_scope_values op) |
| 773 | { |
| 774 | _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition); |
| 775 | _osd_req_encode_flush(or, op); |
| 776 | } |
| 777 | EXPORT_SYMBOL(osd_req_flush_partition); |
| 778 | |
| 779 | /* |
| 780 | * Collection commands |
| 781 | */ |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 782 | /*TODO: void osd_req_create_collection(struct osd_request *, |
| 783 | const struct osd_obj_id *); */ |
| 784 | /*TODO: void osd_req_remove_collection(struct osd_request *, |
| 785 | const struct osd_obj_id *); */ |
| 786 | |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 787 | int osd_req_list_collection_objects(struct osd_request *or, |
| 788 | const struct osd_obj_id *obj, osd_id initial_id, |
| 789 | struct osd_obj_id_list *list, unsigned nelem) |
| 790 | { |
| 791 | return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj, |
| 792 | initial_id, list, nelem); |
| 793 | } |
| 794 | EXPORT_SYMBOL(osd_req_list_collection_objects); |
| 795 | |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 796 | /*TODO: void query(struct osd_request *, ...); V2 */ |
| 797 | |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 798 | void osd_req_flush_collection(struct osd_request *or, |
| 799 | const struct osd_obj_id *obj, enum osd_options_flush_scope_values op) |
| 800 | { |
| 801 | _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0); |
| 802 | _osd_req_encode_flush(or, op); |
| 803 | } |
| 804 | EXPORT_SYMBOL(osd_req_flush_collection); |
| 805 | |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 806 | /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */ |
| 807 | /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */ |
| 808 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 809 | /* |
| 810 | * Object commands |
| 811 | */ |
| 812 | void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj) |
| 813 | { |
| 814 | _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0); |
| 815 | } |
| 816 | EXPORT_SYMBOL(osd_req_create_object); |
| 817 | |
| 818 | void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj) |
| 819 | { |
| 820 | _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0); |
| 821 | } |
| 822 | EXPORT_SYMBOL(osd_req_remove_object); |
| 823 | |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 824 | |
| 825 | /*TODO: void osd_req_create_multi(struct osd_request *or, |
| 826 | struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem); |
| 827 | */ |
| 828 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 829 | void osd_req_write(struct osd_request *or, |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 830 | const struct osd_obj_id *obj, u64 offset, |
| 831 | struct bio *bio, u64 len) |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 832 | { |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 833 | _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 834 | WARN_ON(or->out.bio || or->out.total_bytes); |
Mike Christie | a8ebb05 | 2016-06-05 14:31:45 -0500 | [diff] [blame] | 835 | WARN_ON(!op_is_write(bio_op(bio))); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 836 | or->out.bio = bio; |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 837 | or->out.total_bytes = len; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 838 | } |
| 839 | EXPORT_SYMBOL(osd_req_write); |
| 840 | |
Boaz Harrosh | 0e35afb | 2009-05-24 20:02:22 +0300 | [diff] [blame] | 841 | int osd_req_write_kern(struct osd_request *or, |
| 842 | const struct osd_obj_id *obj, u64 offset, void* buff, u64 len) |
| 843 | { |
Boaz Harrosh | fc2fac5 | 2009-05-24 20:04:43 +0300 | [diff] [blame] | 844 | struct request_queue *req_q = osd_request_queue(or->osd_dev); |
Boaz Harrosh | 0e35afb | 2009-05-24 20:02:22 +0300 | [diff] [blame] | 845 | struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL); |
| 846 | |
| 847 | if (IS_ERR(bio)) |
| 848 | return PTR_ERR(bio); |
| 849 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 850 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 851 | osd_req_write(or, obj, offset, bio, len); |
Boaz Harrosh | 0e35afb | 2009-05-24 20:02:22 +0300 | [diff] [blame] | 852 | return 0; |
| 853 | } |
| 854 | EXPORT_SYMBOL(osd_req_write_kern); |
| 855 | |
Boaz Harrosh | ae30c99 | 2009-01-25 17:07:14 +0200 | [diff] [blame] | 856 | /*TODO: void osd_req_append(struct osd_request *, |
| 857 | const struct osd_obj_id *, struct bio *data_out); */ |
| 858 | /*TODO: void osd_req_create_write(struct osd_request *, |
| 859 | const struct osd_obj_id *, struct bio *data_out, u64 offset); */ |
| 860 | /*TODO: void osd_req_clear(struct osd_request *, |
| 861 | const struct osd_obj_id *, u64 offset, u64 len); */ |
| 862 | /*TODO: void osd_req_punch(struct osd_request *, |
| 863 | const struct osd_obj_id *, u64 offset, u64 len); V2 */ |
| 864 | |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 865 | void osd_req_flush_object(struct osd_request *or, |
| 866 | const struct osd_obj_id *obj, enum osd_options_flush_scope_values op, |
| 867 | /*V2*/ u64 offset, /*V2*/ u64 len) |
| 868 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 869 | if (unlikely(osd_req_is_ver1(or) && (offset || len))) { |
| 870 | OSD_DEBUG("OSD Ver1 flush on specific range ignored\n"); |
| 871 | offset = 0; |
| 872 | len = 0; |
| 873 | } |
| 874 | |
Boaz Harrosh | 3e08613 | 2009-01-25 17:05:07 +0200 | [diff] [blame] | 875 | _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len); |
| 876 | _osd_req_encode_flush(or, op); |
| 877 | } |
| 878 | EXPORT_SYMBOL(osd_req_flush_object); |
| 879 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 880 | void osd_req_read(struct osd_request *or, |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 881 | const struct osd_obj_id *obj, u64 offset, |
| 882 | struct bio *bio, u64 len) |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 883 | { |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 884 | _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 885 | WARN_ON(or->in.bio || or->in.total_bytes); |
Mike Christie | a8ebb05 | 2016-06-05 14:31:45 -0500 | [diff] [blame] | 886 | WARN_ON(op_is_write(bio_op(bio))); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 887 | or->in.bio = bio; |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 888 | or->in.total_bytes = len; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 889 | } |
| 890 | EXPORT_SYMBOL(osd_req_read); |
| 891 | |
Boaz Harrosh | 0e35afb | 2009-05-24 20:02:22 +0300 | [diff] [blame] | 892 | int osd_req_read_kern(struct osd_request *or, |
| 893 | const struct osd_obj_id *obj, u64 offset, void* buff, u64 len) |
| 894 | { |
Boaz Harrosh | fc2fac5 | 2009-05-24 20:04:43 +0300 | [diff] [blame] | 895 | struct request_queue *req_q = osd_request_queue(or->osd_dev); |
Boaz Harrosh | 0e35afb | 2009-05-24 20:02:22 +0300 | [diff] [blame] | 896 | struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL); |
| 897 | |
| 898 | if (IS_ERR(bio)) |
| 899 | return PTR_ERR(bio); |
| 900 | |
Boaz Harrosh | 62f469b | 2009-05-24 20:04:26 +0300 | [diff] [blame] | 901 | osd_req_read(or, obj, offset, bio, len); |
Boaz Harrosh | 0e35afb | 2009-05-24 20:02:22 +0300 | [diff] [blame] | 902 | return 0; |
| 903 | } |
| 904 | EXPORT_SYMBOL(osd_req_read_kern); |
| 905 | |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 906 | static int _add_sg_continuation_descriptor(struct osd_request *or, |
| 907 | const struct osd_sg_entry *sglist, unsigned numentries, u64 *len) |
| 908 | { |
| 909 | struct osd_sg_continuation_descriptor *oscd; |
| 910 | u32 oscd_size; |
| 911 | unsigned i; |
| 912 | int ret; |
| 913 | |
| 914 | oscd_size = sizeof(*oscd) + numentries * sizeof(oscd->entries[0]); |
| 915 | |
| 916 | if (!or->cdb_cont.total_bytes) { |
| 917 | /* First time, jump over the header, we will write to: |
| 918 | * cdb_cont.buff + cdb_cont.total_bytes |
| 919 | */ |
| 920 | or->cdb_cont.total_bytes = |
| 921 | sizeof(struct osd_continuation_segment_header); |
| 922 | } |
| 923 | |
| 924 | ret = _alloc_cdb_cont(or, or->cdb_cont.total_bytes + oscd_size); |
| 925 | if (unlikely(ret)) |
| 926 | return ret; |
| 927 | |
| 928 | oscd = or->cdb_cont.buff + or->cdb_cont.total_bytes; |
| 929 | oscd->hdr.type = cpu_to_be16(SCATTER_GATHER_LIST); |
| 930 | oscd->hdr.pad_length = 0; |
| 931 | oscd->hdr.length = cpu_to_be32(oscd_size - sizeof(*oscd)); |
| 932 | |
| 933 | *len = 0; |
| 934 | /* copy the sg entries and convert to network byte order */ |
| 935 | for (i = 0; i < numentries; i++) { |
| 936 | oscd->entries[i].offset = cpu_to_be64(sglist[i].offset); |
| 937 | oscd->entries[i].len = cpu_to_be64(sglist[i].len); |
| 938 | *len += sglist[i].len; |
| 939 | } |
| 940 | |
| 941 | or->cdb_cont.total_bytes += oscd_size; |
| 942 | OSD_DEBUG("total_bytes=%d oscd_size=%d numentries=%d\n", |
| 943 | or->cdb_cont.total_bytes, oscd_size, numentries); |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static int _osd_req_finalize_cdb_cont(struct osd_request *or, const u8 *cap_key) |
| 948 | { |
| 949 | struct request_queue *req_q = osd_request_queue(or->osd_dev); |
| 950 | struct bio *bio; |
| 951 | struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); |
| 952 | struct osd_continuation_segment_header *cont_seg_hdr; |
| 953 | |
| 954 | if (!or->cdb_cont.total_bytes) |
| 955 | return 0; |
| 956 | |
| 957 | cont_seg_hdr = or->cdb_cont.buff; |
| 958 | cont_seg_hdr->format = CDB_CONTINUATION_FORMAT_V2; |
| 959 | cont_seg_hdr->service_action = cdbh->varlen_cdb.service_action; |
| 960 | |
| 961 | /* create a bio for continuation segment */ |
| 962 | bio = bio_map_kern(req_q, or->cdb_cont.buff, or->cdb_cont.total_bytes, |
| 963 | GFP_KERNEL); |
Dan Carpenter | 057f02a | 2010-11-12 07:31:46 +0300 | [diff] [blame] | 964 | if (IS_ERR(bio)) |
| 965 | return PTR_ERR(bio); |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 966 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 967 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 968 | |
| 969 | /* integrity check the continuation before the bio is linked |
| 970 | * with the other data segments since the continuation |
| 971 | * integrity is separate from the other data segments. |
| 972 | */ |
| 973 | osd_sec_sign_data(cont_seg_hdr->integrity_check, bio, cap_key); |
| 974 | |
| 975 | cdbh->v2.cdb_continuation_length = cpu_to_be32(or->cdb_cont.total_bytes); |
| 976 | |
| 977 | /* we can't use _req_append_segment, because we need to link in the |
| 978 | * continuation bio to the head of the bio list - the |
| 979 | * continuation segment (if it exists) is always the first segment in |
| 980 | * the out data buffer. |
| 981 | */ |
| 982 | bio->bi_next = or->out.bio; |
| 983 | or->out.bio = bio; |
| 984 | or->out.total_bytes += or->cdb_cont.total_bytes; |
| 985 | |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | /* osd_req_write_sg: Takes a @bio that points to the data out buffer and an |
| 990 | * @sglist that has the scatter gather entries. Scatter-gather enables a write |
| 991 | * of multiple none-contiguous areas of an object, in a single call. The extents |
| 992 | * may overlap and/or be in any order. The only constrain is that: |
| 993 | * total_bytes(sglist) >= total_bytes(bio) |
| 994 | */ |
| 995 | int osd_req_write_sg(struct osd_request *or, |
| 996 | const struct osd_obj_id *obj, struct bio *bio, |
| 997 | const struct osd_sg_entry *sglist, unsigned numentries) |
| 998 | { |
| 999 | u64 len; |
| 1000 | int ret = _add_sg_continuation_descriptor(or, sglist, numentries, &len); |
| 1001 | |
| 1002 | if (ret) |
| 1003 | return ret; |
| 1004 | osd_req_write(or, obj, 0, bio, len); |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | EXPORT_SYMBOL(osd_req_write_sg); |
| 1009 | |
| 1010 | /* osd_req_read_sg: Read multiple extents of an object into @bio |
| 1011 | * See osd_req_write_sg |
| 1012 | */ |
| 1013 | int osd_req_read_sg(struct osd_request *or, |
| 1014 | const struct osd_obj_id *obj, struct bio *bio, |
| 1015 | const struct osd_sg_entry *sglist, unsigned numentries) |
| 1016 | { |
| 1017 | u64 len; |
Boaz Harrosh | 4977c82 | 2011-01-23 17:53:24 +0200 | [diff] [blame] | 1018 | u64 off; |
| 1019 | int ret; |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1020 | |
Boaz Harrosh | 4977c82 | 2011-01-23 17:53:24 +0200 | [diff] [blame] | 1021 | if (numentries > 1) { |
| 1022 | off = 0; |
| 1023 | ret = _add_sg_continuation_descriptor(or, sglist, numentries, |
| 1024 | &len); |
| 1025 | if (ret) |
| 1026 | return ret; |
| 1027 | } else { |
| 1028 | /* Optimize the case of single segment, read_sg is a |
| 1029 | * bidi operation. |
| 1030 | */ |
| 1031 | len = sglist->len; |
| 1032 | off = sglist->offset; |
| 1033 | } |
| 1034 | osd_req_read(or, obj, off, bio, len); |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | EXPORT_SYMBOL(osd_req_read_sg); |
| 1039 | |
Boaz Harrosh | 6dd1d8a | 2010-10-19 16:13:50 +0200 | [diff] [blame] | 1040 | /* SG-list write/read Kern API |
| 1041 | * |
| 1042 | * osd_req_{write,read}_sg_kern takes an array of @buff pointers and an array |
| 1043 | * of sg_entries. @numentries indicates how many pointers and sg_entries there |
| 1044 | * are. By requiring an array of buff pointers. This allows a caller to do a |
| 1045 | * single write/read and scatter into multiple buffers. |
| 1046 | * NOTE: Each buffer + len should not cross a page boundary. |
| 1047 | */ |
| 1048 | static struct bio *_create_sg_bios(struct osd_request *or, |
| 1049 | void **buff, const struct osd_sg_entry *sglist, unsigned numentries) |
| 1050 | { |
| 1051 | struct request_queue *q = osd_request_queue(or->osd_dev); |
| 1052 | struct bio *bio; |
| 1053 | unsigned i; |
| 1054 | |
| 1055 | bio = bio_kmalloc(GFP_KERNEL, numentries); |
| 1056 | if (unlikely(!bio)) { |
Masanari Iida | 1051e9b | 2013-03-31 01:23:50 +0900 | [diff] [blame] | 1057 | OSD_DEBUG("Failed to allocate BIO size=%u\n", numentries); |
Boaz Harrosh | 6dd1d8a | 2010-10-19 16:13:50 +0200 | [diff] [blame] | 1058 | return ERR_PTR(-ENOMEM); |
| 1059 | } |
| 1060 | |
| 1061 | for (i = 0; i < numentries; i++) { |
| 1062 | unsigned offset = offset_in_page(buff[i]); |
| 1063 | struct page *page = virt_to_page(buff[i]); |
| 1064 | unsigned len = sglist[i].len; |
| 1065 | unsigned added_len; |
| 1066 | |
| 1067 | BUG_ON(offset + len > PAGE_SIZE); |
| 1068 | added_len = bio_add_pc_page(q, bio, page, len, offset); |
| 1069 | if (unlikely(len != added_len)) { |
| 1070 | OSD_DEBUG("bio_add_pc_page len(%d) != added_len(%d)\n", |
| 1071 | len, added_len); |
| 1072 | bio_put(bio); |
| 1073 | return ERR_PTR(-ENOMEM); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | return bio; |
| 1078 | } |
| 1079 | |
| 1080 | int osd_req_write_sg_kern(struct osd_request *or, |
| 1081 | const struct osd_obj_id *obj, void **buff, |
| 1082 | const struct osd_sg_entry *sglist, unsigned numentries) |
| 1083 | { |
| 1084 | struct bio *bio = _create_sg_bios(or, buff, sglist, numentries); |
| 1085 | if (IS_ERR(bio)) |
| 1086 | return PTR_ERR(bio); |
| 1087 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 1088 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Boaz Harrosh | 6dd1d8a | 2010-10-19 16:13:50 +0200 | [diff] [blame] | 1089 | osd_req_write_sg(or, obj, bio, sglist, numentries); |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | EXPORT_SYMBOL(osd_req_write_sg_kern); |
| 1094 | |
| 1095 | int osd_req_read_sg_kern(struct osd_request *or, |
| 1096 | const struct osd_obj_id *obj, void **buff, |
| 1097 | const struct osd_sg_entry *sglist, unsigned numentries) |
| 1098 | { |
| 1099 | struct bio *bio = _create_sg_bios(or, buff, sglist, numentries); |
| 1100 | if (IS_ERR(bio)) |
| 1101 | return PTR_ERR(bio); |
| 1102 | |
| 1103 | osd_req_read_sg(or, obj, bio, sglist, numentries); |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | EXPORT_SYMBOL(osd_req_read_sg_kern); |
| 1108 | |
| 1109 | |
| 1110 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1111 | void osd_req_get_attributes(struct osd_request *or, |
| 1112 | const struct osd_obj_id *obj) |
| 1113 | { |
| 1114 | _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0); |
| 1115 | } |
| 1116 | EXPORT_SYMBOL(osd_req_get_attributes); |
| 1117 | |
| 1118 | void osd_req_set_attributes(struct osd_request *or, |
| 1119 | const struct osd_obj_id *obj) |
| 1120 | { |
| 1121 | _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0); |
| 1122 | } |
| 1123 | EXPORT_SYMBOL(osd_req_set_attributes); |
| 1124 | |
| 1125 | /* |
| 1126 | * Attributes List-mode |
| 1127 | */ |
| 1128 | |
| 1129 | int osd_req_add_set_attr_list(struct osd_request *or, |
| 1130 | const struct osd_attr *oa, unsigned nelem) |
| 1131 | { |
| 1132 | unsigned total_bytes = or->set_attr.total_bytes; |
| 1133 | void *attr_last; |
| 1134 | int ret; |
| 1135 | |
| 1136 | if (or->attributes_mode && |
| 1137 | or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) { |
| 1138 | WARN_ON(1); |
| 1139 | return -EINVAL; |
| 1140 | } |
| 1141 | or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS; |
| 1142 | |
| 1143 | if (!total_bytes) { /* first-time: allocate and put list header */ |
| 1144 | total_bytes = _osd_req_sizeof_alist_header(or); |
| 1145 | ret = _alloc_set_attr_list(or, oa, nelem, total_bytes); |
| 1146 | if (ret) |
| 1147 | return ret; |
| 1148 | _osd_req_set_alist_type(or, or->set_attr.buff, |
| 1149 | OSD_ATTR_LIST_SET_RETRIEVE); |
| 1150 | } |
| 1151 | attr_last = or->set_attr.buff + total_bytes; |
| 1152 | |
| 1153 | for (; nelem; --nelem) { |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1154 | unsigned elem_size = _osd_req_alist_elem_size(or, oa->len); |
| 1155 | |
| 1156 | total_bytes += elem_size; |
| 1157 | if (unlikely(or->set_attr.alloc_size < total_bytes)) { |
| 1158 | or->set_attr.total_bytes = total_bytes - elem_size; |
| 1159 | ret = _alloc_set_attr_list(or, oa, nelem, total_bytes); |
| 1160 | if (ret) |
| 1161 | return ret; |
| 1162 | attr_last = |
| 1163 | or->set_attr.buff + or->set_attr.total_bytes; |
| 1164 | } |
| 1165 | |
Boaz Harrosh | 71f32e3 | 2009-04-19 19:11:42 +0300 | [diff] [blame] | 1166 | _osd_req_alist_elem_encode(or, attr_last, oa); |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1167 | |
| 1168 | attr_last += elem_size; |
| 1169 | ++oa; |
| 1170 | } |
| 1171 | |
| 1172 | or->set_attr.total_bytes = total_bytes; |
| 1173 | return 0; |
| 1174 | } |
| 1175 | EXPORT_SYMBOL(osd_req_add_set_attr_list); |
| 1176 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1177 | static int _req_append_segment(struct osd_request *or, |
| 1178 | unsigned padding, struct _osd_req_data_segment *seg, |
| 1179 | struct _osd_req_data_segment *last_seg, struct _osd_io_info *io) |
| 1180 | { |
| 1181 | void *pad_buff; |
| 1182 | int ret; |
| 1183 | |
| 1184 | if (padding) { |
| 1185 | /* check if we can just add it to last buffer */ |
| 1186 | if (last_seg && |
| 1187 | (padding <= last_seg->alloc_size - last_seg->total_bytes)) |
| 1188 | pad_buff = last_seg->buff + last_seg->total_bytes; |
| 1189 | else |
| 1190 | pad_buff = io->pad_buff; |
| 1191 | |
Boaz Harrosh | bc38bf1 | 2009-05-17 18:56:17 +0300 | [diff] [blame] | 1192 | ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding, |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1193 | or->alloc_flags); |
| 1194 | if (ret) |
| 1195 | return ret; |
| 1196 | io->total_bytes += padding; |
| 1197 | } |
| 1198 | |
Boaz Harrosh | bc38bf1 | 2009-05-17 18:56:17 +0300 | [diff] [blame] | 1199 | ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes, |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1200 | or->alloc_flags); |
| 1201 | if (ret) |
| 1202 | return ret; |
| 1203 | |
| 1204 | io->total_bytes += seg->total_bytes; |
| 1205 | OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff, |
| 1206 | seg->total_bytes); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static int _osd_req_finalize_set_attr_list(struct osd_request *or) |
| 1211 | { |
| 1212 | struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); |
| 1213 | unsigned padding; |
| 1214 | int ret; |
| 1215 | |
| 1216 | if (!or->set_attr.total_bytes) { |
| 1217 | cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes); |
| 1222 | cdbh->attrs_list.set_attr_offset = |
| 1223 | osd_req_encode_offset(or, or->out.total_bytes, &padding); |
| 1224 | |
| 1225 | ret = _req_append_segment(or, padding, &or->set_attr, |
| 1226 | or->out.last_seg, &or->out); |
| 1227 | if (ret) |
| 1228 | return ret; |
| 1229 | |
| 1230 | or->out.last_seg = &or->set_attr; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | int osd_req_add_get_attr_list(struct osd_request *or, |
| 1235 | const struct osd_attr *oa, unsigned nelem) |
| 1236 | { |
| 1237 | unsigned total_bytes = or->enc_get_attr.total_bytes; |
| 1238 | void *attr_last; |
| 1239 | int ret; |
| 1240 | |
| 1241 | if (or->attributes_mode && |
| 1242 | or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) { |
| 1243 | WARN_ON(1); |
| 1244 | return -EINVAL; |
| 1245 | } |
| 1246 | or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS; |
| 1247 | |
| 1248 | /* first time calc data-in list header size */ |
| 1249 | if (!or->get_attr.total_bytes) |
| 1250 | or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or); |
| 1251 | |
| 1252 | /* calc data-out info */ |
| 1253 | if (!total_bytes) { /* first-time: allocate and put list header */ |
| 1254 | unsigned max_bytes; |
| 1255 | |
| 1256 | total_bytes = _osd_req_sizeof_alist_header(or); |
| 1257 | max_bytes = total_bytes + |
| 1258 | nelem * sizeof(struct osd_attributes_list_attrid); |
| 1259 | ret = _alloc_get_attr_desc(or, max_bytes); |
| 1260 | if (ret) |
| 1261 | return ret; |
| 1262 | |
| 1263 | _osd_req_set_alist_type(or, or->enc_get_attr.buff, |
| 1264 | OSD_ATTR_LIST_GET); |
| 1265 | } |
| 1266 | attr_last = or->enc_get_attr.buff + total_bytes; |
| 1267 | |
| 1268 | for (; nelem; --nelem) { |
| 1269 | struct osd_attributes_list_attrid *attrid; |
| 1270 | const unsigned cur_size = sizeof(*attrid); |
| 1271 | |
| 1272 | total_bytes += cur_size; |
| 1273 | if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) { |
| 1274 | or->enc_get_attr.total_bytes = total_bytes - cur_size; |
| 1275 | ret = _alloc_get_attr_desc(or, |
| 1276 | total_bytes + nelem * sizeof(*attrid)); |
| 1277 | if (ret) |
| 1278 | return ret; |
| 1279 | attr_last = or->enc_get_attr.buff + |
| 1280 | or->enc_get_attr.total_bytes; |
| 1281 | } |
| 1282 | |
| 1283 | attrid = attr_last; |
| 1284 | attrid->attr_page = cpu_to_be32(oa->attr_page); |
| 1285 | attrid->attr_id = cpu_to_be32(oa->attr_id); |
| 1286 | |
| 1287 | attr_last += cur_size; |
| 1288 | |
| 1289 | /* calc data-in size */ |
| 1290 | or->get_attr.total_bytes += |
| 1291 | _osd_req_alist_elem_size(or, oa->len); |
| 1292 | ++oa; |
| 1293 | } |
| 1294 | |
| 1295 | or->enc_get_attr.total_bytes = total_bytes; |
| 1296 | |
| 1297 | OSD_DEBUG( |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1298 | "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%zu)\n", |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1299 | or->get_attr.total_bytes, |
| 1300 | or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or), |
| 1301 | or->enc_get_attr.total_bytes, |
| 1302 | (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or)) |
| 1303 | / sizeof(struct osd_attributes_list_attrid)); |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | EXPORT_SYMBOL(osd_req_add_get_attr_list); |
| 1308 | |
| 1309 | static int _osd_req_finalize_get_attr_list(struct osd_request *or) |
| 1310 | { |
| 1311 | struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); |
| 1312 | unsigned out_padding; |
| 1313 | unsigned in_padding; |
| 1314 | int ret; |
| 1315 | |
| 1316 | if (!or->enc_get_attr.total_bytes) { |
| 1317 | cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED; |
| 1318 | cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED; |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | ret = _alloc_get_attr_list(or); |
| 1323 | if (ret) |
| 1324 | return ret; |
| 1325 | |
| 1326 | /* The out-going buffer info update */ |
| 1327 | OSD_DEBUG("out-going\n"); |
| 1328 | cdbh->attrs_list.get_attr_desc_bytes = |
| 1329 | cpu_to_be32(or->enc_get_attr.total_bytes); |
| 1330 | |
| 1331 | cdbh->attrs_list.get_attr_desc_offset = |
| 1332 | osd_req_encode_offset(or, or->out.total_bytes, &out_padding); |
| 1333 | |
| 1334 | ret = _req_append_segment(or, out_padding, &or->enc_get_attr, |
| 1335 | or->out.last_seg, &or->out); |
| 1336 | if (ret) |
| 1337 | return ret; |
| 1338 | or->out.last_seg = &or->enc_get_attr; |
| 1339 | |
| 1340 | /* The incoming buffer info update */ |
| 1341 | OSD_DEBUG("in-coming\n"); |
| 1342 | cdbh->attrs_list.get_attr_alloc_length = |
| 1343 | cpu_to_be32(or->get_attr.total_bytes); |
| 1344 | |
| 1345 | cdbh->attrs_list.get_attr_offset = |
| 1346 | osd_req_encode_offset(or, or->in.total_bytes, &in_padding); |
| 1347 | |
| 1348 | ret = _req_append_segment(or, in_padding, &or->get_attr, NULL, |
| 1349 | &or->in); |
| 1350 | if (ret) |
| 1351 | return ret; |
| 1352 | or->in.last_seg = &or->get_attr; |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | int osd_req_decode_get_attr_list(struct osd_request *or, |
| 1358 | struct osd_attr *oa, int *nelem, void **iterator) |
| 1359 | { |
| 1360 | unsigned cur_bytes, returned_bytes; |
| 1361 | int n; |
| 1362 | const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or); |
| 1363 | void *cur_p; |
| 1364 | |
| 1365 | if (!_osd_req_is_alist_type(or, or->get_attr.buff, |
| 1366 | OSD_ATTR_LIST_SET_RETRIEVE)) { |
| 1367 | oa->attr_page = 0; |
| 1368 | oa->attr_id = 0; |
| 1369 | oa->val_ptr = NULL; |
| 1370 | oa->len = 0; |
| 1371 | *iterator = NULL; |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | if (*iterator) { |
| 1376 | BUG_ON((*iterator < or->get_attr.buff) || |
| 1377 | (or->get_attr.buff + or->get_attr.alloc_size < *iterator)); |
| 1378 | cur_p = *iterator; |
| 1379 | cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list; |
| 1380 | returned_bytes = or->get_attr.total_bytes; |
| 1381 | } else { /* first time decode the list header */ |
| 1382 | cur_bytes = sizeof_attr_list; |
| 1383 | returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) + |
| 1384 | sizeof_attr_list; |
| 1385 | |
| 1386 | cur_p = or->get_attr.buff + sizeof_attr_list; |
| 1387 | |
| 1388 | if (returned_bytes > or->get_attr.alloc_size) { |
| 1389 | OSD_DEBUG("target report: space was not big enough! " |
| 1390 | "Allocate=%u Needed=%u\n", |
| 1391 | or->get_attr.alloc_size, |
| 1392 | returned_bytes + sizeof_attr_list); |
| 1393 | |
| 1394 | returned_bytes = |
| 1395 | or->get_attr.alloc_size - sizeof_attr_list; |
| 1396 | } |
| 1397 | or->get_attr.total_bytes = returned_bytes; |
| 1398 | } |
| 1399 | |
| 1400 | for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) { |
Boaz Harrosh | 71f32e3 | 2009-04-19 19:11:42 +0300 | [diff] [blame] | 1401 | int inc = _osd_req_alist_elem_decode(or, cur_p, oa, |
| 1402 | returned_bytes - cur_bytes); |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1403 | |
Boaz Harrosh | 71f32e3 | 2009-04-19 19:11:42 +0300 | [diff] [blame] | 1404 | if (inc < 0) { |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1405 | OSD_ERR("BAD FOOD from target. list not valid!" |
| 1406 | "c=%d r=%d n=%d\n", |
| 1407 | cur_bytes, returned_bytes, n); |
| 1408 | oa->val_ptr = NULL; |
Boaz Harrosh | eff2149 | 2009-11-16 20:47:47 +0200 | [diff] [blame] | 1409 | cur_bytes = returned_bytes; /* break the caller loop */ |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1410 | break; |
| 1411 | } |
| 1412 | |
Boaz Harrosh | 71f32e3 | 2009-04-19 19:11:42 +0300 | [diff] [blame] | 1413 | cur_bytes += inc; |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1414 | cur_p += inc; |
| 1415 | ++oa; |
| 1416 | } |
| 1417 | |
| 1418 | *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL; |
| 1419 | *nelem = n; |
| 1420 | return returned_bytes - cur_bytes; |
| 1421 | } |
| 1422 | EXPORT_SYMBOL(osd_req_decode_get_attr_list); |
| 1423 | |
| 1424 | /* |
| 1425 | * Attributes Page-mode |
| 1426 | */ |
| 1427 | |
| 1428 | int osd_req_add_get_attr_page(struct osd_request *or, |
| 1429 | u32 page_id, void *attar_page, unsigned max_page_len, |
| 1430 | const struct osd_attr *set_one_attr) |
| 1431 | { |
| 1432 | struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); |
| 1433 | |
| 1434 | if (or->attributes_mode && |
| 1435 | or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) { |
| 1436 | WARN_ON(1); |
| 1437 | return -EINVAL; |
| 1438 | } |
| 1439 | or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE; |
| 1440 | |
| 1441 | or->get_attr.buff = attar_page; |
| 1442 | or->get_attr.total_bytes = max_page_len; |
| 1443 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1444 | cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id); |
| 1445 | cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len); |
Boaz Harrosh | ec6a0a4 | 2010-10-19 14:20:31 +0200 | [diff] [blame] | 1446 | |
| 1447 | if (!set_one_attr || !set_one_attr->attr_page) |
| 1448 | return 0; /* The set is optional */ |
| 1449 | |
| 1450 | or->set_attr.buff = set_one_attr->val_ptr; |
| 1451 | or->set_attr.total_bytes = set_one_attr->len; |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1452 | |
| 1453 | cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page); |
| 1454 | cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id); |
| 1455 | cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len); |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1456 | return 0; |
| 1457 | } |
| 1458 | EXPORT_SYMBOL(osd_req_add_get_attr_page); |
| 1459 | |
| 1460 | static int _osd_req_finalize_attr_page(struct osd_request *or) |
| 1461 | { |
| 1462 | struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); |
| 1463 | unsigned in_padding, out_padding; |
| 1464 | int ret; |
| 1465 | |
| 1466 | /* returned page */ |
| 1467 | cdbh->attrs_page.get_attr_offset = |
| 1468 | osd_req_encode_offset(or, or->in.total_bytes, &in_padding); |
| 1469 | |
| 1470 | ret = _req_append_segment(or, in_padding, &or->get_attr, NULL, |
| 1471 | &or->in); |
| 1472 | if (ret) |
| 1473 | return ret; |
| 1474 | |
Boaz Harrosh | ec6a0a4 | 2010-10-19 14:20:31 +0200 | [diff] [blame] | 1475 | if (or->set_attr.total_bytes == 0) |
| 1476 | return 0; |
| 1477 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1478 | /* set one value */ |
| 1479 | cdbh->attrs_page.set_attr_offset = |
| 1480 | osd_req_encode_offset(or, or->out.total_bytes, &out_padding); |
| 1481 | |
Boaz Harrosh | ec6a0a4 | 2010-10-19 14:20:31 +0200 | [diff] [blame] | 1482 | ret = _req_append_segment(or, out_padding, &or->set_attr, NULL, |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1483 | &or->out); |
| 1484 | return ret; |
| 1485 | } |
| 1486 | |
Boaz Harrosh | f8d3a64 | 2009-04-19 19:13:39 +0300 | [diff] [blame] | 1487 | static inline void osd_sec_parms_set_out_offset(bool is_v1, |
| 1488 | struct osd_security_parameters *sec_parms, osd_cdb_offset offset) |
| 1489 | { |
| 1490 | if (is_v1) |
| 1491 | sec_parms->v1.data_out_integrity_check_offset = offset; |
| 1492 | else |
| 1493 | sec_parms->v2.data_out_integrity_check_offset = offset; |
| 1494 | } |
| 1495 | |
| 1496 | static inline void osd_sec_parms_set_in_offset(bool is_v1, |
| 1497 | struct osd_security_parameters *sec_parms, osd_cdb_offset offset) |
| 1498 | { |
| 1499 | if (is_v1) |
| 1500 | sec_parms->v1.data_in_integrity_check_offset = offset; |
| 1501 | else |
| 1502 | sec_parms->v2.data_in_integrity_check_offset = offset; |
| 1503 | } |
| 1504 | |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 1505 | static int _osd_req_finalize_data_integrity(struct osd_request *or, |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1506 | bool has_in, bool has_out, struct bio *out_data_bio, u64 out_data_bytes, |
| 1507 | const u8 *cap_key) |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 1508 | { |
| 1509 | struct osd_security_parameters *sec_parms = _osd_req_sec_params(or); |
| 1510 | int ret; |
| 1511 | |
| 1512 | if (!osd_is_sec_alldata(sec_parms)) |
| 1513 | return 0; |
| 1514 | |
| 1515 | if (has_out) { |
| 1516 | struct _osd_req_data_segment seg = { |
| 1517 | .buff = &or->out_data_integ, |
| 1518 | .total_bytes = sizeof(or->out_data_integ), |
| 1519 | }; |
| 1520 | unsigned pad; |
| 1521 | |
Boaz Harrosh | 546881a | 2009-05-24 20:04:00 +0300 | [diff] [blame] | 1522 | or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes); |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 1523 | or->out_data_integ.set_attributes_bytes = cpu_to_be64( |
| 1524 | or->set_attr.total_bytes); |
| 1525 | or->out_data_integ.get_attributes_bytes = cpu_to_be64( |
| 1526 | or->enc_get_attr.total_bytes); |
| 1527 | |
Boaz Harrosh | f8d3a64 | 2009-04-19 19:13:39 +0300 | [diff] [blame] | 1528 | osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms, |
| 1529 | osd_req_encode_offset(or, or->out.total_bytes, &pad)); |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 1530 | |
| 1531 | ret = _req_append_segment(or, pad, &seg, or->out.last_seg, |
| 1532 | &or->out); |
| 1533 | if (ret) |
| 1534 | return ret; |
| 1535 | or->out.last_seg = NULL; |
| 1536 | |
| 1537 | /* they are now all chained to request sign them all together */ |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1538 | osd_sec_sign_data(&or->out_data_integ, out_data_bio, |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 1539 | cap_key); |
| 1540 | } |
| 1541 | |
| 1542 | if (has_in) { |
| 1543 | struct _osd_req_data_segment seg = { |
| 1544 | .buff = &or->in_data_integ, |
| 1545 | .total_bytes = sizeof(or->in_data_integ), |
| 1546 | }; |
| 1547 | unsigned pad; |
| 1548 | |
Boaz Harrosh | f8d3a64 | 2009-04-19 19:13:39 +0300 | [diff] [blame] | 1549 | osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms, |
| 1550 | osd_req_encode_offset(or, or->in.total_bytes, &pad)); |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 1551 | |
| 1552 | ret = _req_append_segment(or, pad, &seg, or->in.last_seg, |
| 1553 | &or->in); |
| 1554 | if (ret) |
| 1555 | return ret; |
| 1556 | |
| 1557 | or->in.last_seg = NULL; |
| 1558 | } |
| 1559 | |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1563 | /* |
| 1564 | * osd_finalize_request and helpers |
| 1565 | */ |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1566 | static struct request *_make_request(struct request_queue *q, bool has_write, |
| 1567 | struct _osd_io_info *oii, gfp_t flags) |
| 1568 | { |
Christoph Hellwig | 4613c5f | 2016-07-19 11:31:53 +0200 | [diff] [blame] | 1569 | struct request *req; |
| 1570 | struct bio *bio = oii->bio; |
| 1571 | int ret; |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1572 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 1573 | req = blk_get_request(q, has_write ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, |
| 1574 | flags); |
Christoph Hellwig | 4613c5f | 2016-07-19 11:31:53 +0200 | [diff] [blame] | 1575 | if (IS_ERR(req)) |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1576 | return req; |
Christoph Hellwig | 4613c5f | 2016-07-19 11:31:53 +0200 | [diff] [blame] | 1577 | |
| 1578 | for_each_bio(bio) { |
Jens Axboe | 0abc2a1 | 2017-12-18 15:40:44 +0800 | [diff] [blame] | 1579 | struct bio *bounce_bio = bio; |
| 1580 | |
| 1581 | ret = blk_rq_append_bio(req, &bounce_bio); |
Christoph Hellwig | 4613c5f | 2016-07-19 11:31:53 +0200 | [diff] [blame] | 1582 | if (ret) |
| 1583 | return ERR_PTR(ret); |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1584 | } |
Christoph Hellwig | 4613c5f | 2016-07-19 11:31:53 +0200 | [diff] [blame] | 1585 | |
| 1586 | return req; |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1587 | } |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1588 | |
| 1589 | static int _init_blk_request(struct osd_request *or, |
| 1590 | bool has_in, bool has_out) |
| 1591 | { |
| 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; |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1596 | int ret; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1597 | |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1598 | req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags); |
| 1599 | if (IS_ERR(req)) { |
| 1600 | ret = PTR_ERR(req); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1601 | goto out; |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1602 | } |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1603 | |
| 1604 | or->request = req; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1605 | req->rq_flags |= RQF_QUIET; |
Boaz Harrosh | 0330679 | 2009-05-24 20:05:43 +0300 | [diff] [blame] | 1606 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1607 | req->timeout = or->timeout; |
Christoph Hellwig | 64c7f1d | 2017-04-05 19:18:12 +0200 | [diff] [blame] | 1608 | scsi_req(req)->retries = or->retries; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1609 | |
| 1610 | if (has_out) { |
| 1611 | or->out.req = req; |
| 1612 | if (has_in) { |
| 1613 | /* allocate bidi request */ |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1614 | req = _make_request(q, false, &or->in, flags); |
| 1615 | if (IS_ERR(req)) { |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1616 | OSD_DEBUG("blk_get_request for bidi failed\n"); |
Boaz Harrosh | c29b70f | 2009-05-17 18:58:41 +0300 | [diff] [blame] | 1617 | ret = PTR_ERR(req); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1618 | goto out; |
| 1619 | } |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1620 | or->in.req = or->request->next_rq = req; |
| 1621 | } |
| 1622 | } else if (has_in) |
| 1623 | or->in.req = req; |
| 1624 | |
| 1625 | ret = 0; |
| 1626 | out: |
| 1627 | OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n", |
| 1628 | or, has_in, has_out, ret, or->request); |
| 1629 | return ret; |
| 1630 | } |
| 1631 | |
| 1632 | int osd_finalize_request(struct osd_request *or, |
| 1633 | u8 options, const void *cap, const u8 *cap_key) |
| 1634 | { |
| 1635 | struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb); |
| 1636 | bool has_in, has_out; |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1637 | /* Save for data_integrity without the cdb_continuation */ |
| 1638 | struct bio *out_data_bio = or->out.bio; |
Boaz Harrosh | 546881a | 2009-05-24 20:04:00 +0300 | [diff] [blame] | 1639 | u64 out_data_bytes = or->out.total_bytes; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1640 | int ret; |
| 1641 | |
| 1642 | if (options & OSD_REQ_FUA) |
| 1643 | cdbh->options |= OSD_CDB_FUA; |
| 1644 | |
| 1645 | if (options & OSD_REQ_DPO) |
| 1646 | cdbh->options |= OSD_CDB_DPO; |
| 1647 | |
| 1648 | if (options & OSD_REQ_BYPASS_TIMESTAMPS) |
| 1649 | cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS; |
| 1650 | |
| 1651 | osd_set_caps(&or->cdb, cap); |
| 1652 | |
| 1653 | has_in = or->in.bio || or->get_attr.total_bytes; |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1654 | has_out = or->out.bio || or->cdb_cont.total_bytes || |
| 1655 | or->set_attr.total_bytes || or->enc_get_attr.total_bytes; |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1656 | |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1657 | ret = _osd_req_finalize_cdb_cont(or, cap_key); |
| 1658 | if (ret) { |
| 1659 | OSD_DEBUG("_osd_req_finalize_cdb_cont failed\n"); |
| 1660 | return ret; |
| 1661 | } |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1662 | ret = _init_blk_request(or, has_in, has_out); |
| 1663 | if (ret) { |
| 1664 | OSD_DEBUG("_init_blk_request failed\n"); |
| 1665 | return ret; |
| 1666 | } |
| 1667 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1668 | or->out.pad_buff = sg_out_pad_buffer; |
| 1669 | or->in.pad_buff = sg_in_pad_buffer; |
| 1670 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1671 | if (!or->attributes_mode) |
| 1672 | or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS; |
| 1673 | cdbh->command_specific_options |= or->attributes_mode; |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1674 | if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) { |
| 1675 | ret = _osd_req_finalize_attr_page(or); |
Boaz Harrosh | 98e1e0f | 2010-02-19 11:46:24 -0800 | [diff] [blame] | 1676 | if (ret) { |
| 1677 | OSD_DEBUG("_osd_req_finalize_attr_page failed\n"); |
| 1678 | return ret; |
| 1679 | } |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1680 | } else { |
| 1681 | /* TODO: I think that for the GET_ATTR command these 2 should |
Masahiro Yamada | b43daed | 2017-02-27 14:29:09 -0800 | [diff] [blame] | 1682 | * be reversed to keep them in execution order (for embedded |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 1683 | * targets with low memory footprint) |
| 1684 | */ |
| 1685 | ret = _osd_req_finalize_set_attr_list(or); |
| 1686 | if (ret) { |
| 1687 | OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n"); |
| 1688 | return ret; |
| 1689 | } |
| 1690 | |
| 1691 | ret = _osd_req_finalize_get_attr_list(or); |
| 1692 | if (ret) { |
| 1693 | OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n"); |
| 1694 | return ret; |
| 1695 | } |
| 1696 | } |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1697 | |
Boaz Harrosh | 546881a | 2009-05-24 20:04:00 +0300 | [diff] [blame] | 1698 | ret = _osd_req_finalize_data_integrity(or, has_in, has_out, |
Boaz Harrosh | e96e72c | 2010-10-19 14:22:21 +0200 | [diff] [blame] | 1699 | out_data_bio, out_data_bytes, |
| 1700 | cap_key); |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 1701 | if (ret) |
| 1702 | return ret; |
| 1703 | |
| 1704 | osd_sec_sign_cdb(&or->cdb, cap_key); |
| 1705 | |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 1706 | scsi_req(or->request)->cmd = or->cdb.buff; |
| 1707 | scsi_req(or->request)->cmd_len = _osd_req_cdb_len(or); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1708 | |
| 1709 | return 0; |
| 1710 | } |
| 1711 | EXPORT_SYMBOL(osd_finalize_request); |
| 1712 | |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1713 | static bool _is_osd_security_code(int code) |
| 1714 | { |
| 1715 | return (code == osd_security_audit_value_frozen) || |
| 1716 | (code == osd_security_working_key_frozen) || |
| 1717 | (code == osd_nonce_not_unique) || |
| 1718 | (code == osd_nonce_timestamp_out_of_range) || |
| 1719 | (code == osd_invalid_dataout_buffer_integrity_check_value); |
| 1720 | } |
| 1721 | |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1722 | #define OSD_SENSE_PRINT1(fmt, a...) \ |
| 1723 | do { \ |
| 1724 | if (__cur_sense_need_output) \ |
| 1725 | OSD_ERR(fmt, ##a); \ |
| 1726 | } while (0) |
| 1727 | |
| 1728 | #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a) |
| 1729 | |
| 1730 | int osd_req_decode_sense_full(struct osd_request *or, |
| 1731 | struct osd_sense_info *osi, bool silent, |
| 1732 | struct osd_obj_id *bad_obj_list __unused, int max_obj __unused, |
| 1733 | struct osd_attr *bad_attr_list, int max_attr) |
| 1734 | { |
| 1735 | int sense_len, original_sense_len; |
| 1736 | struct osd_sense_info local_osi; |
| 1737 | struct scsi_sense_descriptor_based *ssdb; |
| 1738 | void *cur_descriptor; |
| 1739 | #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0) |
| 1740 | const bool __cur_sense_need_output = false; |
| 1741 | #else |
| 1742 | bool __cur_sense_need_output = !silent; |
| 1743 | #endif |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1744 | int ret; |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1745 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 1746 | if (likely(!or->req_errors)) |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1747 | return 0; |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1748 | |
| 1749 | osi = osi ? : &local_osi; |
| 1750 | memset(osi, 0, sizeof(*osi)); |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1751 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 1752 | ssdb = (typeof(ssdb))or->sense; |
| 1753 | sense_len = or->sense_len; |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1754 | if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) { |
| 1755 | OSD_ERR("Block-layer returned error(0x%x) but " |
| 1756 | "sense_len(%u) || key(%d) is empty\n", |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 1757 | or->req_errors, sense_len, ssdb->sense_key); |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1758 | goto analyze; |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) { |
| 1762 | OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n", |
| 1763 | ssdb->response_code, sense_len); |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1764 | goto analyze; |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1765 | } |
| 1766 | |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1767 | osi->key = ssdb->sense_key; |
| 1768 | osi->additional_code = be16_to_cpu(ssdb->additional_sense_code); |
| 1769 | original_sense_len = ssdb->additional_sense_length + 8; |
| 1770 | |
| 1771 | #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1) |
| 1772 | if (__cur_sense_need_output) |
| 1773 | __cur_sense_need_output = (osi->key > scsi_sk_recovered_error); |
| 1774 | #endif |
| 1775 | OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) " |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1776 | "additional_code=0x%x async_error=%d errors=0x%x\n", |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1777 | osi->key, original_sense_len, sense_len, |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1778 | osi->additional_code, or->async_error, |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 1779 | or->req_errors); |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1780 | |
| 1781 | if (original_sense_len < sense_len) |
| 1782 | sense_len = original_sense_len; |
| 1783 | |
| 1784 | cur_descriptor = ssdb->ssd; |
| 1785 | sense_len -= sizeof(*ssdb); |
| 1786 | while (sense_len > 0) { |
| 1787 | struct scsi_sense_descriptor *ssd = cur_descriptor; |
| 1788 | int cur_len = ssd->additional_length + 2; |
| 1789 | |
| 1790 | sense_len -= cur_len; |
| 1791 | |
| 1792 | if (sense_len < 0) |
| 1793 | break; /* sense was truncated */ |
| 1794 | |
| 1795 | switch (ssd->descriptor_type) { |
| 1796 | case scsi_sense_information: |
| 1797 | case scsi_sense_command_specific_information: |
| 1798 | { |
| 1799 | struct scsi_sense_command_specific_data_descriptor |
| 1800 | *sscd = cur_descriptor; |
| 1801 | |
| 1802 | osi->command_info = |
| 1803 | get_unaligned_be64(&sscd->information) ; |
| 1804 | OSD_SENSE_PRINT2( |
| 1805 | "command_specific_information 0x%llx \n", |
| 1806 | _LLU(osi->command_info)); |
| 1807 | break; |
| 1808 | } |
| 1809 | case scsi_sense_key_specific: |
| 1810 | { |
| 1811 | struct scsi_sense_key_specific_data_descriptor |
| 1812 | *ssks = cur_descriptor; |
| 1813 | |
| 1814 | osi->sense_info = get_unaligned_be16(&ssks->value); |
| 1815 | OSD_SENSE_PRINT2( |
| 1816 | "sense_key_specific_information %u" |
| 1817 | "sksv_cd_bpv_bp (0x%x)\n", |
| 1818 | osi->sense_info, ssks->sksv_cd_bpv_bp); |
| 1819 | break; |
| 1820 | } |
| 1821 | case osd_sense_object_identification: |
| 1822 | { /*FIXME: Keep first not last, Store in array*/ |
| 1823 | struct osd_sense_identification_data_descriptor |
| 1824 | *osidd = cur_descriptor; |
| 1825 | |
| 1826 | osi->not_initiated_command_functions = |
| 1827 | le32_to_cpu(osidd->not_initiated_functions); |
| 1828 | osi->completed_command_functions = |
| 1829 | le32_to_cpu(osidd->completed_functions); |
| 1830 | osi->obj.partition = be64_to_cpu(osidd->partition_id); |
| 1831 | osi->obj.id = be64_to_cpu(osidd->object_id); |
| 1832 | OSD_SENSE_PRINT2( |
| 1833 | "object_identification pid=0x%llx oid=0x%llx\n", |
| 1834 | _LLU(osi->obj.partition), _LLU(osi->obj.id)); |
| 1835 | OSD_SENSE_PRINT2( |
| 1836 | "not_initiated_bits(%x) " |
| 1837 | "completed_command_bits(%x)\n", |
| 1838 | osi->not_initiated_command_functions, |
| 1839 | osi->completed_command_functions); |
| 1840 | break; |
| 1841 | } |
| 1842 | case osd_sense_response_integrity_check: |
| 1843 | { |
| 1844 | struct osd_sense_response_integrity_check_descriptor |
| 1845 | *osricd = cur_descriptor; |
| 1846 | const unsigned len = |
| 1847 | sizeof(osricd->integrity_check_value); |
| 1848 | char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */ |
| 1849 | |
| 1850 | hex_dump_to_buffer(osricd->integrity_check_value, len, |
| 1851 | 32, 1, key_dump, sizeof(key_dump), true); |
| 1852 | OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump); |
| 1853 | } |
| 1854 | case osd_sense_attribute_identification: |
| 1855 | { |
| 1856 | struct osd_sense_attributes_data_descriptor |
| 1857 | *osadd = cur_descriptor; |
Boaz Harrosh | 71ecb74 | 2009-11-16 20:47:07 +0200 | [diff] [blame] | 1858 | unsigned len = min(cur_len, sense_len); |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1859 | struct osd_sense_attr *pattr = osadd->sense_attrs; |
| 1860 | |
Boaz Harrosh | 71ecb74 | 2009-11-16 20:47:07 +0200 | [diff] [blame] | 1861 | while (len >= sizeof(*pattr)) { |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1862 | u32 attr_page = be32_to_cpu(pattr->attr_page); |
| 1863 | u32 attr_id = be32_to_cpu(pattr->attr_id); |
| 1864 | |
Boaz Harrosh | 71ecb74 | 2009-11-16 20:47:07 +0200 | [diff] [blame] | 1865 | if (!osi->attr.attr_page) { |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1866 | osi->attr.attr_page = attr_page; |
| 1867 | osi->attr.attr_id = attr_id; |
| 1868 | } |
| 1869 | |
| 1870 | if (bad_attr_list && max_attr) { |
| 1871 | bad_attr_list->attr_page = attr_page; |
| 1872 | bad_attr_list->attr_id = attr_id; |
| 1873 | bad_attr_list++; |
| 1874 | max_attr--; |
| 1875 | } |
Boaz Harrosh | 71ecb74 | 2009-11-16 20:47:07 +0200 | [diff] [blame] | 1876 | |
| 1877 | len -= sizeof(*pattr); |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1878 | OSD_SENSE_PRINT2( |
| 1879 | "osd_sense_attribute_identification" |
| 1880 | "attr_page=0x%x attr_id=0x%x\n", |
| 1881 | attr_page, attr_id); |
| 1882 | } |
| 1883 | } |
| 1884 | /*These are not legal for OSD*/ |
| 1885 | case scsi_sense_field_replaceable_unit: |
| 1886 | OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n"); |
| 1887 | break; |
| 1888 | case scsi_sense_stream_commands: |
| 1889 | OSD_SENSE_PRINT2("scsi_sense_stream_commands\n"); |
| 1890 | break; |
| 1891 | case scsi_sense_block_commands: |
| 1892 | OSD_SENSE_PRINT2("scsi_sense_block_commands\n"); |
| 1893 | break; |
| 1894 | case scsi_sense_ata_return: |
| 1895 | OSD_SENSE_PRINT2("scsi_sense_ata_return\n"); |
| 1896 | break; |
| 1897 | default: |
| 1898 | if (ssd->descriptor_type <= scsi_sense_Reserved_last) |
| 1899 | OSD_SENSE_PRINT2( |
| 1900 | "scsi_sense Reserved descriptor (0x%x)", |
| 1901 | ssd->descriptor_type); |
| 1902 | else |
| 1903 | OSD_SENSE_PRINT2( |
| 1904 | "scsi_sense Vendor descriptor (0x%x)", |
| 1905 | ssd->descriptor_type); |
| 1906 | } |
| 1907 | |
| 1908 | cur_descriptor += cur_len; |
| 1909 | } |
| 1910 | |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1911 | analyze: |
| 1912 | if (!osi->key) { |
| 1913 | /* scsi sense is Empty, the request was never issued to target |
| 1914 | * linux return code might tell us what happened. |
| 1915 | */ |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1916 | if (or->async_error == BLK_STS_RESOURCE) |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1917 | osi->osd_err_pri = OSD_ERR_PRI_RESOURCE; |
| 1918 | else |
| 1919 | osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE; |
| 1920 | ret = or->async_error; |
| 1921 | } else if (osi->key <= scsi_sk_recovered_error) { |
| 1922 | osi->osd_err_pri = 0; |
| 1923 | ret = 0; |
| 1924 | } else if (osi->additional_code == scsi_invalid_field_in_cdb) { |
| 1925 | if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) { |
| 1926 | osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES; |
| 1927 | ret = -EFAULT; /* caller should recover from this */ |
| 1928 | } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) { |
| 1929 | osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND; |
| 1930 | ret = -ENOENT; |
| 1931 | } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) { |
| 1932 | osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS; |
| 1933 | ret = -EACCES; |
| 1934 | } else { |
| 1935 | osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED; |
| 1936 | ret = -EINVAL; |
| 1937 | } |
| 1938 | } else if (osi->additional_code == osd_quota_error) { |
| 1939 | osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE; |
| 1940 | ret = -ENOSPC; |
| 1941 | } else if (_is_osd_security_code(osi->additional_code)) { |
| 1942 | osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED; |
| 1943 | ret = -EINVAL; |
| 1944 | } else { |
| 1945 | osi->osd_err_pri = OSD_ERR_PRI_EIO; |
| 1946 | ret = -EIO; |
| 1947 | } |
| 1948 | |
Boaz Harrosh | 5d0961f | 2009-12-01 17:36:21 +0200 | [diff] [blame] | 1949 | if (!or->out.residual) |
| 1950 | or->out.residual = or->out.total_bytes; |
| 1951 | if (!or->in.residual) |
| 1952 | or->in.residual = or->in.total_bytes; |
Boaz Harrosh | aa9fffb | 2009-11-16 20:48:38 +0200 | [diff] [blame] | 1953 | |
| 1954 | return ret; |
Boaz Harrosh | 98f3aea | 2009-01-25 17:15:16 +0200 | [diff] [blame] | 1955 | } |
| 1956 | EXPORT_SYMBOL(osd_req_decode_sense_full); |
| 1957 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1958 | /* |
| 1959 | * Implementation of osd_sec.h API |
| 1960 | * TODO: Move to a separate osd_sec.c file at a later stage. |
| 1961 | */ |
| 1962 | |
| 1963 | enum { OSD_SEC_CAP_V1_ALL_CAPS = |
| 1964 | OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE | |
| 1965 | OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR | |
| 1966 | OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC | |
| 1967 | OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT |
| 1968 | }; |
| 1969 | |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 1970 | enum { OSD_SEC_CAP_V2_ALL_CAPS = |
| 1971 | OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT |
| 1972 | }; |
| 1973 | |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 1974 | void osd_sec_init_nosec_doall_caps(void *caps, |
| 1975 | const struct osd_obj_id *obj, bool is_collection, const bool is_v1) |
| 1976 | { |
| 1977 | struct osd_capability *cap = caps; |
| 1978 | u8 type; |
| 1979 | u8 descriptor_type; |
| 1980 | |
| 1981 | if (likely(obj->id)) { |
| 1982 | if (unlikely(is_collection)) { |
| 1983 | type = OSD_SEC_OBJ_COLLECTION; |
| 1984 | descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ : |
| 1985 | OSD_SEC_OBJ_DESC_COL; |
| 1986 | } else { |
| 1987 | type = OSD_SEC_OBJ_USER; |
| 1988 | descriptor_type = OSD_SEC_OBJ_DESC_OBJ; |
| 1989 | } |
| 1990 | WARN_ON(!obj->partition); |
| 1991 | } else { |
| 1992 | type = obj->partition ? OSD_SEC_OBJ_PARTITION : |
| 1993 | OSD_SEC_OBJ_ROOT; |
| 1994 | descriptor_type = OSD_SEC_OBJ_DESC_PAR; |
| 1995 | } |
| 1996 | |
| 1997 | memset(cap, 0, sizeof(*cap)); |
| 1998 | |
| 1999 | cap->h.format = OSD_SEC_CAP_FORMAT_VER1; |
| 2000 | cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */ |
| 2001 | cap->h.security_method = OSD_SEC_NOSEC; |
| 2002 | /* cap->expiration_time; |
| 2003 | cap->AUDIT[30-10]; |
| 2004 | cap->discriminator[42-30]; |
| 2005 | cap->object_created_time; */ |
| 2006 | cap->h.object_type = type; |
| 2007 | osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS); |
| 2008 | cap->h.object_descriptor_type = descriptor_type; |
| 2009 | cap->od.obj_desc.policy_access_tag = 0; |
| 2010 | cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition); |
| 2011 | cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id); |
| 2012 | } |
| 2013 | EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps); |
| 2014 | |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 2015 | /* FIXME: Extract version from caps pointer. |
| 2016 | * Also Pete's target only supports caps from OSDv1 for now |
| 2017 | */ |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 2018 | void osd_set_caps(struct osd_cdb *cdb, const void *caps) |
| 2019 | { |
Boaz Harrosh | c6572c9 | 2009-01-25 17:09:40 +0200 | [diff] [blame] | 2020 | /* NOTE: They start at same address */ |
Sudip Mukherjee | ba690c7 | 2016-02-24 16:51:28 +0530 | [diff] [blame] | 2021 | memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN); |
Boaz Harrosh | 02941a5 | 2009-01-25 16:55:30 +0200 | [diff] [blame] | 2022 | } |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 2023 | |
Boaz Harrosh | 345c435 | 2009-01-25 17:03:07 +0200 | [diff] [blame] | 2024 | bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused) |
| 2025 | { |
| 2026 | return false; |
| 2027 | } |
| 2028 | |
| 2029 | void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused) |
| 2030 | { |
| 2031 | } |
| 2032 | |
| 2033 | void osd_sec_sign_data(void *data_integ __unused, |
| 2034 | struct bio *bio __unused, const u8 *cap_key __unused) |
| 2035 | { |
| 2036 | } |
| 2037 | |
Boaz Harrosh | 4ef1a3d | 2009-01-25 16:59:50 +0200 | [diff] [blame] | 2038 | /* |
| 2039 | * Declared in osd_protocol.h |
| 2040 | * 4.12.5 Data-In and Data-Out buffer offsets |
| 2041 | * byte offset = mantissa * (2^(exponent+8)) |
| 2042 | * Returns the smallest allowed encoded offset that contains given @offset |
| 2043 | * The actual encoded offset returned is @offset + *@padding. |
| 2044 | */ |
| 2045 | osd_cdb_offset __osd_encode_offset( |
| 2046 | u64 offset, unsigned *padding, int min_shift, int max_shift) |
| 2047 | { |
| 2048 | u64 try_offset = -1, mod, align; |
| 2049 | osd_cdb_offset be32_offset; |
| 2050 | int shift; |
| 2051 | |
| 2052 | *padding = 0; |
| 2053 | if (!offset) |
| 2054 | return 0; |
| 2055 | |
| 2056 | for (shift = min_shift; shift < max_shift; ++shift) { |
| 2057 | try_offset = offset >> shift; |
| 2058 | if (try_offset < (1 << OSD_OFFSET_MAX_BITS)) |
| 2059 | break; |
| 2060 | } |
| 2061 | |
| 2062 | BUG_ON(shift == max_shift); |
| 2063 | |
| 2064 | align = 1 << shift; |
| 2065 | mod = offset & (align - 1); |
| 2066 | if (mod) { |
| 2067 | *padding = align - mod; |
| 2068 | try_offset += 1; |
| 2069 | } |
| 2070 | |
| 2071 | try_offset |= ((shift - 8) & 0xf) << 28; |
| 2072 | be32_offset = cpu_to_be32((u32)try_offset); |
| 2073 | |
| 2074 | OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n", |
| 2075 | _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift, |
| 2076 | be32_offset, *padding); |
| 2077 | return be32_offset; |
| 2078 | } |