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