blob: 5d9457b7e455121996d9585f12d8d4600db53062 [file] [log] [blame]
Boaz Harrosh02941a52009-01-25 16:55:30 +02001/*
2 * osd_initiator - Main body of the osd initiator library.
3 *
4 * Note: The file does not contain the advanced security functionality which
5 * is only needed by the security_manager's initiators.
6 *
7 * Copyright (C) 2008 Panasas Inc. All rights reserved.
8 *
9 * Authors:
10 * Boaz Harrosh <bharrosh@panasas.com>
11 * Benny Halevy <bhalevy@panasas.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the Panasas company nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include <scsi/osd_initiator.h>
43#include <scsi/osd_sec.h>
44#include <scsi/scsi_device.h>
45
46#include "osd_debug.h"
47
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020048#ifndef __unused
49# define __unused __attribute__((unused))
50#endif
51
Boaz Harrosh02941a52009-01-25 16:55:30 +020052enum { OSD_REQ_RETRIES = 1 };
53
54MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
55MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
56MODULE_LICENSE("GPL");
57
58static inline void build_test(void)
59{
60 /* structures were not packed */
61 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
62 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
63}
64
65static unsigned _osd_req_cdb_len(struct osd_request *or)
66{
67 return OSDv1_TOTAL_CDB_LEN;
68}
69
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020070static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
71{
72 return osdv1_attr_list_elem_size(len);
73}
74
75static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
76{
77 return osdv1_list_size(list_head);
78}
79
80static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
81{
82 return sizeof(struct osdv1_attributes_list_header);
83}
84
85static void _osd_req_set_alist_type(struct osd_request *or,
86 void *list, int list_type)
87{
88 struct osdv1_attributes_list_header *attr_list = list;
89
90 memset(attr_list, 0, sizeof(*attr_list));
91 attr_list->type = list_type;
92}
93
94static bool _osd_req_is_alist_type(struct osd_request *or,
95 void *list, int list_type)
96{
97 if (!list)
98 return false;
99
100 if (1) {
101 struct osdv1_attributes_list_header *attr_list = list;
102
103 return attr_list->type == list_type;
104 }
105}
106
Boaz Harrosh3e086132009-01-25 17:05:07 +0200107/* This is for List-objects not Attributes-Lists */
108static void _osd_req_encode_olist(struct osd_request *or,
109 struct osd_obj_id_list *list)
110{
111 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
112
113 cdbh->v1.list_identifier = list->list_identifier;
114 cdbh->v1.start_address = list->continuation_id;
115}
116
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200117static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
118 u64 offset, unsigned *padding)
119{
120 return __osd_encode_offset(offset, padding,
121 OSDv1_OFFSET_MIN_SHIFT, OSD_OFFSET_MAX_SHIFT);
122}
123
Boaz Harrosh345c4352009-01-25 17:03:07 +0200124static struct osd_security_parameters *
125_osd_req_sec_params(struct osd_request *or)
126{
127 struct osd_cdb *ocdb = &or->cdb;
128
129 return &ocdb->v1.sec_params;
130}
131
Boaz Harrosh02941a52009-01-25 16:55:30 +0200132void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
133{
134 memset(osdd, 0, sizeof(*osdd));
135 osdd->scsi_device = scsi_device;
136 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
137 /* TODO: Allocate pools for osd_request attributes ... */
138}
139EXPORT_SYMBOL(osd_dev_init);
140
141void osd_dev_fini(struct osd_dev *osdd)
142{
143 /* TODO: De-allocate pools */
144
145 osdd->scsi_device = NULL;
146}
147EXPORT_SYMBOL(osd_dev_fini);
148
149static struct osd_request *_osd_request_alloc(gfp_t gfp)
150{
151 struct osd_request *or;
152
153 /* TODO: Use mempool with one saved request */
154 or = kzalloc(sizeof(*or), gfp);
155 return or;
156}
157
158static void _osd_request_free(struct osd_request *or)
159{
160 kfree(or);
161}
162
163struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
164{
165 struct osd_request *or;
166
167 or = _osd_request_alloc(gfp);
168 if (!or)
169 return NULL;
170
171 or->osd_dev = dev;
172 or->alloc_flags = gfp;
173 or->timeout = dev->def_timeout;
174 or->retries = OSD_REQ_RETRIES;
175
176 return or;
177}
178EXPORT_SYMBOL(osd_start_request);
179
180/*
181 * If osd_finalize_request() was called but the request was not executed through
182 * the block layer, then we must release BIOs.
183 */
184static void _abort_unexecuted_bios(struct request *rq)
185{
186 struct bio *bio;
187
188 while ((bio = rq->bio) != NULL) {
189 rq->bio = bio->bi_next;
190 bio_endio(bio, 0);
191 }
192}
193
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200194static void _osd_free_seg(struct osd_request *or __unused,
195 struct _osd_req_data_segment *seg)
196{
197 if (!seg->buff || !seg->alloc_size)
198 return;
199
200 kfree(seg->buff);
201 seg->buff = NULL;
202 seg->alloc_size = 0;
203}
204
Boaz Harrosh02941a52009-01-25 16:55:30 +0200205void osd_end_request(struct osd_request *or)
206{
207 struct request *rq = or->request;
208
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200209 _osd_free_seg(or, &or->set_attr);
210 _osd_free_seg(or, &or->enc_get_attr);
211 _osd_free_seg(or, &or->get_attr);
212
Boaz Harrosh02941a52009-01-25 16:55:30 +0200213 if (rq) {
214 if (rq->next_rq) {
215 _abort_unexecuted_bios(rq->next_rq);
216 blk_put_request(rq->next_rq);
217 }
218
219 _abort_unexecuted_bios(rq);
220 blk_put_request(rq);
221 }
222 _osd_request_free(or);
223}
224EXPORT_SYMBOL(osd_end_request);
225
226int osd_execute_request(struct osd_request *or)
227{
228 return blk_execute_rq(or->request->q, NULL, or->request, 0);
229}
230EXPORT_SYMBOL(osd_execute_request);
231
232static void osd_request_async_done(struct request *req, int error)
233{
234 struct osd_request *or = req->end_io_data;
235
236 or->async_error = error;
237
238 if (error)
239 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
240
241 if (or->async_done)
242 or->async_done(or, or->async_private);
243 else
244 osd_end_request(or);
245}
246
247int osd_execute_request_async(struct osd_request *or,
248 osd_req_done_fn *done, void *private)
249{
250 or->request->end_io_data = or;
251 or->async_private = private;
252 or->async_done = done;
253
254 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
255 osd_request_async_done);
256 return 0;
257}
258EXPORT_SYMBOL(osd_execute_request_async);
259
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200260u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
261u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
262
263static int _osd_realloc_seg(struct osd_request *or,
264 struct _osd_req_data_segment *seg, unsigned max_bytes)
265{
266 void *buff;
267
268 if (seg->alloc_size >= max_bytes)
269 return 0;
270
271 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
272 if (!buff) {
273 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
274 seg->alloc_size);
275 return -ENOMEM;
276 }
277
278 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
279 seg->buff = buff;
280 seg->alloc_size = max_bytes;
281 return 0;
282}
283
284static int _alloc_set_attr_list(struct osd_request *or,
285 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
286{
287 unsigned total_bytes = add_bytes;
288
289 for (; nelem; --nelem, ++oa)
290 total_bytes += _osd_req_alist_elem_size(or, oa->len);
291
292 OSD_DEBUG("total_bytes=%d\n", total_bytes);
293 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
294}
295
296static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
297{
298 OSD_DEBUG("total_bytes=%d\n", max_bytes);
299 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
300}
301
302static int _alloc_get_attr_list(struct osd_request *or)
303{
304 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
305 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
306}
307
Boaz Harrosh02941a52009-01-25 16:55:30 +0200308/*
309 * Common to all OSD commands
310 */
311
312static void _osdv1_req_encode_common(struct osd_request *or,
313 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
314{
315 struct osdv1_cdb *ocdb = &or->cdb.v1;
316
317 /*
318 * For speed, the commands
319 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
320 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
321 * are not supported here. Should pass zero and set after the call
322 */
323 act &= cpu_to_be16(~0x0080); /* V1 action code */
324
325 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
326
327 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
328 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
329 ocdb->h.varlen_cdb.service_action = act;
330
331 ocdb->h.partition = cpu_to_be64(obj->partition);
332 ocdb->h.object = cpu_to_be64(obj->id);
333 ocdb->h.v1.length = cpu_to_be64(len);
334 ocdb->h.v1.start_address = cpu_to_be64(offset);
335}
336
337static void _osd_req_encode_common(struct osd_request *or,
338 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
339{
340 _osdv1_req_encode_common(or, act, obj, offset, len);
341}
342
343/*
344 * Device commands
345 */
346void osd_req_format(struct osd_request *or, u64 tot_capacity)
347{
348 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
349 tot_capacity);
350}
351EXPORT_SYMBOL(osd_req_format);
352
Boaz Harrosh3e086132009-01-25 17:05:07 +0200353int osd_req_list_dev_partitions(struct osd_request *or,
354 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
355{
356 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
357}
358EXPORT_SYMBOL(osd_req_list_dev_partitions);
359
360static void _osd_req_encode_flush(struct osd_request *or,
361 enum osd_options_flush_scope_values op)
362{
363 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
364
365 ocdb->command_specific_options = op;
366}
367
368void osd_req_flush_obsd(struct osd_request *or,
369 enum osd_options_flush_scope_values op)
370{
371 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
372 _osd_req_encode_flush(or, op);
373}
374EXPORT_SYMBOL(osd_req_flush_obsd);
375
Boaz Harrosh02941a52009-01-25 16:55:30 +0200376/*
377 * Partition commands
378 */
379static void _osd_req_encode_partition(struct osd_request *or,
380 __be16 act, osd_id partition)
381{
382 struct osd_obj_id par = {
383 .partition = partition,
384 .id = 0,
385 };
386
387 _osd_req_encode_common(or, act, &par, 0, 0);
388}
389
390void osd_req_create_partition(struct osd_request *or, osd_id partition)
391{
392 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
393}
394EXPORT_SYMBOL(osd_req_create_partition);
395
396void osd_req_remove_partition(struct osd_request *or, osd_id partition)
397{
398 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
399}
400EXPORT_SYMBOL(osd_req_remove_partition);
401
Boaz Harrosh3e086132009-01-25 17:05:07 +0200402static int _osd_req_list_objects(struct osd_request *or,
403 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
404 struct osd_obj_id_list *list, unsigned nelem)
405{
406 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
407 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
408 struct bio *bio;
409
410 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
411
412 if (list->list_identifier)
413 _osd_req_encode_olist(or, list);
414
415 WARN_ON(or->in.bio);
416 bio = bio_map_kern(q, list, len, or->alloc_flags);
417 if (!bio) {
418 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
419 return -ENOMEM;
420 }
421
422 bio->bi_rw &= ~(1 << BIO_RW);
423 or->in.bio = bio;
424 or->in.total_bytes = bio->bi_size;
425 return 0;
426}
427
428int osd_req_list_partition_collections(struct osd_request *or,
429 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
430 unsigned nelem)
431{
432 struct osd_obj_id par = {
433 .partition = partition,
434 .id = 0,
435 };
436
437 return osd_req_list_collection_objects(or, &par, initial_id, list,
438 nelem);
439}
440EXPORT_SYMBOL(osd_req_list_partition_collections);
441
442int osd_req_list_partition_objects(struct osd_request *or,
443 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
444 unsigned nelem)
445{
446 struct osd_obj_id par = {
447 .partition = partition,
448 .id = 0,
449 };
450
451 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
452 nelem);
453}
454EXPORT_SYMBOL(osd_req_list_partition_objects);
455
456void osd_req_flush_partition(struct osd_request *or,
457 osd_id partition, enum osd_options_flush_scope_values op)
458{
459 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
460 _osd_req_encode_flush(or, op);
461}
462EXPORT_SYMBOL(osd_req_flush_partition);
463
464/*
465 * Collection commands
466 */
467int osd_req_list_collection_objects(struct osd_request *or,
468 const struct osd_obj_id *obj, osd_id initial_id,
469 struct osd_obj_id_list *list, unsigned nelem)
470{
471 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
472 initial_id, list, nelem);
473}
474EXPORT_SYMBOL(osd_req_list_collection_objects);
475
476void osd_req_flush_collection(struct osd_request *or,
477 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
478{
479 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
480 _osd_req_encode_flush(or, op);
481}
482EXPORT_SYMBOL(osd_req_flush_collection);
483
Boaz Harrosh02941a52009-01-25 16:55:30 +0200484/*
485 * Object commands
486 */
487void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
488{
489 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
490}
491EXPORT_SYMBOL(osd_req_create_object);
492
493void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
494{
495 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
496}
497EXPORT_SYMBOL(osd_req_remove_object);
498
499void osd_req_write(struct osd_request *or,
500 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
501{
502 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
503 WARN_ON(or->out.bio || or->out.total_bytes);
504 bio->bi_rw |= (1 << BIO_RW);
505 or->out.bio = bio;
506 or->out.total_bytes = bio->bi_size;
507}
508EXPORT_SYMBOL(osd_req_write);
509
Boaz Harrosh3e086132009-01-25 17:05:07 +0200510void osd_req_flush_object(struct osd_request *or,
511 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
512 /*V2*/ u64 offset, /*V2*/ u64 len)
513{
514 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
515 _osd_req_encode_flush(or, op);
516}
517EXPORT_SYMBOL(osd_req_flush_object);
518
Boaz Harrosh02941a52009-01-25 16:55:30 +0200519void osd_req_read(struct osd_request *or,
520 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
521{
522 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
523 WARN_ON(or->in.bio || or->in.total_bytes);
524 bio->bi_rw &= ~(1 << BIO_RW);
525 or->in.bio = bio;
526 or->in.total_bytes = bio->bi_size;
527}
528EXPORT_SYMBOL(osd_req_read);
529
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200530void osd_req_get_attributes(struct osd_request *or,
531 const struct osd_obj_id *obj)
532{
533 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
534}
535EXPORT_SYMBOL(osd_req_get_attributes);
536
537void osd_req_set_attributes(struct osd_request *or,
538 const struct osd_obj_id *obj)
539{
540 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
541}
542EXPORT_SYMBOL(osd_req_set_attributes);
543
544/*
545 * Attributes List-mode
546 */
547
548int osd_req_add_set_attr_list(struct osd_request *or,
549 const struct osd_attr *oa, unsigned nelem)
550{
551 unsigned total_bytes = or->set_attr.total_bytes;
552 void *attr_last;
553 int ret;
554
555 if (or->attributes_mode &&
556 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
557 WARN_ON(1);
558 return -EINVAL;
559 }
560 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
561
562 if (!total_bytes) { /* first-time: allocate and put list header */
563 total_bytes = _osd_req_sizeof_alist_header(or);
564 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
565 if (ret)
566 return ret;
567 _osd_req_set_alist_type(or, or->set_attr.buff,
568 OSD_ATTR_LIST_SET_RETRIEVE);
569 }
570 attr_last = or->set_attr.buff + total_bytes;
571
572 for (; nelem; --nelem) {
573 struct osd_attributes_list_element *attr;
574 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
575
576 total_bytes += elem_size;
577 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
578 or->set_attr.total_bytes = total_bytes - elem_size;
579 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
580 if (ret)
581 return ret;
582 attr_last =
583 or->set_attr.buff + or->set_attr.total_bytes;
584 }
585
586 attr = attr_last;
587 attr->attr_page = cpu_to_be32(oa->attr_page);
588 attr->attr_id = cpu_to_be32(oa->attr_id);
589 attr->attr_bytes = cpu_to_be16(oa->len);
590 memcpy(attr->attr_val, oa->val_ptr, oa->len);
591
592 attr_last += elem_size;
593 ++oa;
594 }
595
596 or->set_attr.total_bytes = total_bytes;
597 return 0;
598}
599EXPORT_SYMBOL(osd_req_add_set_attr_list);
600
601static int _append_map_kern(struct request *req,
602 void *buff, unsigned len, gfp_t flags)
603{
604 struct bio *bio;
605 int ret;
606
607 bio = bio_map_kern(req->q, buff, len, flags);
608 if (IS_ERR(bio)) {
609 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
610 PTR_ERR(bio));
611 return PTR_ERR(bio);
612 }
613 ret = blk_rq_append_bio(req->q, req, bio);
614 if (ret) {
615 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
616 bio_put(bio);
617 }
618 return ret;
619}
620
621static int _req_append_segment(struct osd_request *or,
622 unsigned padding, struct _osd_req_data_segment *seg,
623 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
624{
625 void *pad_buff;
626 int ret;
627
628 if (padding) {
629 /* check if we can just add it to last buffer */
630 if (last_seg &&
631 (padding <= last_seg->alloc_size - last_seg->total_bytes))
632 pad_buff = last_seg->buff + last_seg->total_bytes;
633 else
634 pad_buff = io->pad_buff;
635
636 ret = _append_map_kern(io->req, pad_buff, padding,
637 or->alloc_flags);
638 if (ret)
639 return ret;
640 io->total_bytes += padding;
641 }
642
643 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
644 or->alloc_flags);
645 if (ret)
646 return ret;
647
648 io->total_bytes += seg->total_bytes;
649 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
650 seg->total_bytes);
651 return 0;
652}
653
654static int _osd_req_finalize_set_attr_list(struct osd_request *or)
655{
656 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
657 unsigned padding;
658 int ret;
659
660 if (!or->set_attr.total_bytes) {
661 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
662 return 0;
663 }
664
665 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
666 cdbh->attrs_list.set_attr_offset =
667 osd_req_encode_offset(or, or->out.total_bytes, &padding);
668
669 ret = _req_append_segment(or, padding, &or->set_attr,
670 or->out.last_seg, &or->out);
671 if (ret)
672 return ret;
673
674 or->out.last_seg = &or->set_attr;
675 return 0;
676}
677
678int osd_req_add_get_attr_list(struct osd_request *or,
679 const struct osd_attr *oa, unsigned nelem)
680{
681 unsigned total_bytes = or->enc_get_attr.total_bytes;
682 void *attr_last;
683 int ret;
684
685 if (or->attributes_mode &&
686 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
687 WARN_ON(1);
688 return -EINVAL;
689 }
690 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
691
692 /* first time calc data-in list header size */
693 if (!or->get_attr.total_bytes)
694 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
695
696 /* calc data-out info */
697 if (!total_bytes) { /* first-time: allocate and put list header */
698 unsigned max_bytes;
699
700 total_bytes = _osd_req_sizeof_alist_header(or);
701 max_bytes = total_bytes +
702 nelem * sizeof(struct osd_attributes_list_attrid);
703 ret = _alloc_get_attr_desc(or, max_bytes);
704 if (ret)
705 return ret;
706
707 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
708 OSD_ATTR_LIST_GET);
709 }
710 attr_last = or->enc_get_attr.buff + total_bytes;
711
712 for (; nelem; --nelem) {
713 struct osd_attributes_list_attrid *attrid;
714 const unsigned cur_size = sizeof(*attrid);
715
716 total_bytes += cur_size;
717 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
718 or->enc_get_attr.total_bytes = total_bytes - cur_size;
719 ret = _alloc_get_attr_desc(or,
720 total_bytes + nelem * sizeof(*attrid));
721 if (ret)
722 return ret;
723 attr_last = or->enc_get_attr.buff +
724 or->enc_get_attr.total_bytes;
725 }
726
727 attrid = attr_last;
728 attrid->attr_page = cpu_to_be32(oa->attr_page);
729 attrid->attr_id = cpu_to_be32(oa->attr_id);
730
731 attr_last += cur_size;
732
733 /* calc data-in size */
734 or->get_attr.total_bytes +=
735 _osd_req_alist_elem_size(or, oa->len);
736 ++oa;
737 }
738
739 or->enc_get_attr.total_bytes = total_bytes;
740
741 OSD_DEBUG(
742 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
743 or->get_attr.total_bytes,
744 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
745 or->enc_get_attr.total_bytes,
746 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
747 / sizeof(struct osd_attributes_list_attrid));
748
749 return 0;
750}
751EXPORT_SYMBOL(osd_req_add_get_attr_list);
752
753static int _osd_req_finalize_get_attr_list(struct osd_request *or)
754{
755 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
756 unsigned out_padding;
757 unsigned in_padding;
758 int ret;
759
760 if (!or->enc_get_attr.total_bytes) {
761 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
762 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
763 return 0;
764 }
765
766 ret = _alloc_get_attr_list(or);
767 if (ret)
768 return ret;
769
770 /* The out-going buffer info update */
771 OSD_DEBUG("out-going\n");
772 cdbh->attrs_list.get_attr_desc_bytes =
773 cpu_to_be32(or->enc_get_attr.total_bytes);
774
775 cdbh->attrs_list.get_attr_desc_offset =
776 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
777
778 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
779 or->out.last_seg, &or->out);
780 if (ret)
781 return ret;
782 or->out.last_seg = &or->enc_get_attr;
783
784 /* The incoming buffer info update */
785 OSD_DEBUG("in-coming\n");
786 cdbh->attrs_list.get_attr_alloc_length =
787 cpu_to_be32(or->get_attr.total_bytes);
788
789 cdbh->attrs_list.get_attr_offset =
790 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
791
792 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
793 &or->in);
794 if (ret)
795 return ret;
796 or->in.last_seg = &or->get_attr;
797
798 return 0;
799}
800
801int osd_req_decode_get_attr_list(struct osd_request *or,
802 struct osd_attr *oa, int *nelem, void **iterator)
803{
804 unsigned cur_bytes, returned_bytes;
805 int n;
806 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
807 void *cur_p;
808
809 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
810 OSD_ATTR_LIST_SET_RETRIEVE)) {
811 oa->attr_page = 0;
812 oa->attr_id = 0;
813 oa->val_ptr = NULL;
814 oa->len = 0;
815 *iterator = NULL;
816 return 0;
817 }
818
819 if (*iterator) {
820 BUG_ON((*iterator < or->get_attr.buff) ||
821 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
822 cur_p = *iterator;
823 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
824 returned_bytes = or->get_attr.total_bytes;
825 } else { /* first time decode the list header */
826 cur_bytes = sizeof_attr_list;
827 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
828 sizeof_attr_list;
829
830 cur_p = or->get_attr.buff + sizeof_attr_list;
831
832 if (returned_bytes > or->get_attr.alloc_size) {
833 OSD_DEBUG("target report: space was not big enough! "
834 "Allocate=%u Needed=%u\n",
835 or->get_attr.alloc_size,
836 returned_bytes + sizeof_attr_list);
837
838 returned_bytes =
839 or->get_attr.alloc_size - sizeof_attr_list;
840 }
841 or->get_attr.total_bytes = returned_bytes;
842 }
843
844 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
845 struct osd_attributes_list_element *attr = cur_p;
846 unsigned inc;
847
848 oa->len = be16_to_cpu(attr->attr_bytes);
849 inc = _osd_req_alist_elem_size(or, oa->len);
850 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
851 oa->len, inc, cur_bytes);
852 cur_bytes += inc;
853 if (cur_bytes > returned_bytes) {
854 OSD_ERR("BAD FOOD from target. list not valid!"
855 "c=%d r=%d n=%d\n",
856 cur_bytes, returned_bytes, n);
857 oa->val_ptr = NULL;
858 break;
859 }
860
861 oa->attr_page = be32_to_cpu(attr->attr_page);
862 oa->attr_id = be32_to_cpu(attr->attr_id);
863 oa->val_ptr = attr->attr_val;
864
865 cur_p += inc;
866 ++oa;
867 }
868
869 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
870 *nelem = n;
871 return returned_bytes - cur_bytes;
872}
873EXPORT_SYMBOL(osd_req_decode_get_attr_list);
874
875/*
876 * Attributes Page-mode
877 */
878
879int osd_req_add_get_attr_page(struct osd_request *or,
880 u32 page_id, void *attar_page, unsigned max_page_len,
881 const struct osd_attr *set_one_attr)
882{
883 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
884
885 if (or->attributes_mode &&
886 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
887 WARN_ON(1);
888 return -EINVAL;
889 }
890 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
891
892 or->get_attr.buff = attar_page;
893 or->get_attr.total_bytes = max_page_len;
894
895 or->set_attr.buff = set_one_attr->val_ptr;
896 or->set_attr.total_bytes = set_one_attr->len;
897
898 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
899 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
900 /* ocdb->attrs_page.get_attr_offset; */
901
902 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
903 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
904 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
905 /* ocdb->attrs_page.set_attr_offset; */
906 return 0;
907}
908EXPORT_SYMBOL(osd_req_add_get_attr_page);
909
910static int _osd_req_finalize_attr_page(struct osd_request *or)
911{
912 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
913 unsigned in_padding, out_padding;
914 int ret;
915
916 /* returned page */
917 cdbh->attrs_page.get_attr_offset =
918 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
919
920 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
921 &or->in);
922 if (ret)
923 return ret;
924
925 /* set one value */
926 cdbh->attrs_page.set_attr_offset =
927 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
928
929 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
930 &or->out);
931 return ret;
932}
933
Boaz Harrosh345c4352009-01-25 17:03:07 +0200934static int _osd_req_finalize_data_integrity(struct osd_request *or,
935 bool has_in, bool has_out, const u8 *cap_key)
936{
937 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
938 int ret;
939
940 if (!osd_is_sec_alldata(sec_parms))
941 return 0;
942
943 if (has_out) {
944 struct _osd_req_data_segment seg = {
945 .buff = &or->out_data_integ,
946 .total_bytes = sizeof(or->out_data_integ),
947 };
948 unsigned pad;
949
950 or->out_data_integ.data_bytes = cpu_to_be64(
951 or->out.bio ? or->out.bio->bi_size : 0);
952 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
953 or->set_attr.total_bytes);
954 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
955 or->enc_get_attr.total_bytes);
956
957 sec_parms->data_out_integrity_check_offset =
958 osd_req_encode_offset(or, or->out.total_bytes, &pad);
959
960 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
961 &or->out);
962 if (ret)
963 return ret;
964 or->out.last_seg = NULL;
965
966 /* they are now all chained to request sign them all together */
967 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
968 cap_key);
969 }
970
971 if (has_in) {
972 struct _osd_req_data_segment seg = {
973 .buff = &or->in_data_integ,
974 .total_bytes = sizeof(or->in_data_integ),
975 };
976 unsigned pad;
977
978 sec_parms->data_in_integrity_check_offset =
979 osd_req_encode_offset(or, or->in.total_bytes, &pad);
980
981 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
982 &or->in);
983 if (ret)
984 return ret;
985
986 or->in.last_seg = NULL;
987 }
988
989 return 0;
990}
991
Boaz Harrosh02941a52009-01-25 16:55:30 +0200992/*
993 * osd_finalize_request and helpers
994 */
995
996static int _init_blk_request(struct osd_request *or,
997 bool has_in, bool has_out)
998{
999 gfp_t flags = or->alloc_flags;
1000 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1001 struct request_queue *q = scsi_device->request_queue;
1002 struct request *req;
1003 int ret = -ENOMEM;
1004
1005 req = blk_get_request(q, has_out, flags);
1006 if (!req)
1007 goto out;
1008
1009 or->request = req;
1010 req->cmd_type = REQ_TYPE_BLOCK_PC;
1011 req->timeout = or->timeout;
1012 req->retries = or->retries;
1013 req->sense = or->sense;
1014 req->sense_len = 0;
1015
1016 if (has_out) {
1017 or->out.req = req;
1018 if (has_in) {
1019 /* allocate bidi request */
1020 req = blk_get_request(q, READ, flags);
1021 if (!req) {
1022 OSD_DEBUG("blk_get_request for bidi failed\n");
1023 goto out;
1024 }
1025 req->cmd_type = REQ_TYPE_BLOCK_PC;
1026 or->in.req = or->request->next_rq = req;
1027 }
1028 } else if (has_in)
1029 or->in.req = req;
1030
1031 ret = 0;
1032out:
1033 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1034 or, has_in, has_out, ret, or->request);
1035 return ret;
1036}
1037
1038int osd_finalize_request(struct osd_request *or,
1039 u8 options, const void *cap, const u8 *cap_key)
1040{
1041 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1042 bool has_in, has_out;
1043 int ret;
1044
1045 if (options & OSD_REQ_FUA)
1046 cdbh->options |= OSD_CDB_FUA;
1047
1048 if (options & OSD_REQ_DPO)
1049 cdbh->options |= OSD_CDB_DPO;
1050
1051 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1052 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1053
1054 osd_set_caps(&or->cdb, cap);
1055
1056 has_in = or->in.bio || or->get_attr.total_bytes;
1057 has_out = or->out.bio || or->set_attr.total_bytes ||
1058 or->enc_get_attr.total_bytes;
1059
1060 ret = _init_blk_request(or, has_in, has_out);
1061 if (ret) {
1062 OSD_DEBUG("_init_blk_request failed\n");
1063 return ret;
1064 }
1065
1066 if (or->out.bio) {
1067 ret = blk_rq_append_bio(or->request->q, or->out.req,
1068 or->out.bio);
1069 if (ret) {
1070 OSD_DEBUG("blk_rq_append_bio out failed\n");
1071 return ret;
1072 }
1073 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1074 _LLU(or->out.total_bytes), or->out.req->data_len);
1075 }
1076 if (or->in.bio) {
1077 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1078 if (ret) {
1079 OSD_DEBUG("blk_rq_append_bio in failed\n");
1080 return ret;
1081 }
1082 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1083 _LLU(or->in.total_bytes), or->in.req->data_len);
1084 }
1085
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001086 or->out.pad_buff = sg_out_pad_buffer;
1087 or->in.pad_buff = sg_in_pad_buffer;
1088
Boaz Harrosh02941a52009-01-25 16:55:30 +02001089 if (!or->attributes_mode)
1090 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1091 cdbh->command_specific_options |= or->attributes_mode;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001092 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1093 ret = _osd_req_finalize_attr_page(or);
1094 } else {
1095 /* TODO: I think that for the GET_ATTR command these 2 should
1096 * be reversed to keep them in execution order (for embeded
1097 * targets with low memory footprint)
1098 */
1099 ret = _osd_req_finalize_set_attr_list(or);
1100 if (ret) {
1101 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1102 return ret;
1103 }
1104
1105 ret = _osd_req_finalize_get_attr_list(or);
1106 if (ret) {
1107 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1108 return ret;
1109 }
1110 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001111
Boaz Harrosh345c4352009-01-25 17:03:07 +02001112 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1113 if (ret)
1114 return ret;
1115
1116 osd_sec_sign_cdb(&or->cdb, cap_key);
1117
Boaz Harrosh02941a52009-01-25 16:55:30 +02001118 or->request->cmd = or->cdb.buff;
1119 or->request->cmd_len = _osd_req_cdb_len(or);
1120
1121 return 0;
1122}
1123EXPORT_SYMBOL(osd_finalize_request);
1124
1125/*
1126 * Implementation of osd_sec.h API
1127 * TODO: Move to a separate osd_sec.c file at a later stage.
1128 */
1129
1130enum { OSD_SEC_CAP_V1_ALL_CAPS =
1131 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1132 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1133 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1134 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1135};
1136
1137void osd_sec_init_nosec_doall_caps(void *caps,
1138 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1139{
1140 struct osd_capability *cap = caps;
1141 u8 type;
1142 u8 descriptor_type;
1143
1144 if (likely(obj->id)) {
1145 if (unlikely(is_collection)) {
1146 type = OSD_SEC_OBJ_COLLECTION;
1147 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1148 OSD_SEC_OBJ_DESC_COL;
1149 } else {
1150 type = OSD_SEC_OBJ_USER;
1151 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1152 }
1153 WARN_ON(!obj->partition);
1154 } else {
1155 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1156 OSD_SEC_OBJ_ROOT;
1157 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1158 }
1159
1160 memset(cap, 0, sizeof(*cap));
1161
1162 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1163 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1164 cap->h.security_method = OSD_SEC_NOSEC;
1165/* cap->expiration_time;
1166 cap->AUDIT[30-10];
1167 cap->discriminator[42-30];
1168 cap->object_created_time; */
1169 cap->h.object_type = type;
1170 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1171 cap->h.object_descriptor_type = descriptor_type;
1172 cap->od.obj_desc.policy_access_tag = 0;
1173 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1174 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1175}
1176EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1177
1178void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1179{
1180 memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
1181}
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001182
Boaz Harrosh345c4352009-01-25 17:03:07 +02001183bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1184{
1185 return false;
1186}
1187
1188void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1189{
1190}
1191
1192void osd_sec_sign_data(void *data_integ __unused,
1193 struct bio *bio __unused, const u8 *cap_key __unused)
1194{
1195}
1196
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001197/*
1198 * Declared in osd_protocol.h
1199 * 4.12.5 Data-In and Data-Out buffer offsets
1200 * byte offset = mantissa * (2^(exponent+8))
1201 * Returns the smallest allowed encoded offset that contains given @offset
1202 * The actual encoded offset returned is @offset + *@padding.
1203 */
1204osd_cdb_offset __osd_encode_offset(
1205 u64 offset, unsigned *padding, int min_shift, int max_shift)
1206{
1207 u64 try_offset = -1, mod, align;
1208 osd_cdb_offset be32_offset;
1209 int shift;
1210
1211 *padding = 0;
1212 if (!offset)
1213 return 0;
1214
1215 for (shift = min_shift; shift < max_shift; ++shift) {
1216 try_offset = offset >> shift;
1217 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1218 break;
1219 }
1220
1221 BUG_ON(shift == max_shift);
1222
1223 align = 1 << shift;
1224 mod = offset & (align - 1);
1225 if (mod) {
1226 *padding = align - mod;
1227 try_offset += 1;
1228 }
1229
1230 try_offset |= ((shift - 8) & 0xf) << 28;
1231 be32_offset = cpu_to_be32((u32)try_offset);
1232
1233 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1234 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1235 be32_offset, *padding);
1236 return be32_offset;
1237}