6849f269cd1b6b8f6d6b3899282619a4ea896dd4
[safe/jmp/linux-2.6] / 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 <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
48 #ifndef __unused
49 #    define __unused                    __attribute__((unused))
50 #endif
51
52 enum { OSD_REQ_RETRIES = 1 };
53
54 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
55 MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
56 MODULE_LICENSE("GPL");
57
58 static 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
65 static unsigned _osd_req_cdb_len(struct osd_request *or)
66 {
67         return OSDv1_TOTAL_CDB_LEN;
68 }
69
70 static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
71 {
72         return osdv1_attr_list_elem_size(len);
73 }
74
75 static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
76 {
77         return osdv1_list_size(list_head);
78 }
79
80 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
81 {
82         return sizeof(struct osdv1_attributes_list_header);
83 }
84
85 static 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
94 static 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
107 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
108         u64 offset, unsigned *padding)
109 {
110         return __osd_encode_offset(offset, padding,
111                                   OSDv1_OFFSET_MIN_SHIFT, OSD_OFFSET_MAX_SHIFT);
112 }
113
114 static struct osd_security_parameters *
115 _osd_req_sec_params(struct osd_request *or)
116 {
117         struct osd_cdb *ocdb = &or->cdb;
118
119         return &ocdb->v1.sec_params;
120 }
121
122 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
123 {
124         memset(osdd, 0, sizeof(*osdd));
125         osdd->scsi_device = scsi_device;
126         osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
127         /* TODO: Allocate pools for osd_request attributes ... */
128 }
129 EXPORT_SYMBOL(osd_dev_init);
130
131 void osd_dev_fini(struct osd_dev *osdd)
132 {
133         /* TODO: De-allocate pools */
134
135         osdd->scsi_device = NULL;
136 }
137 EXPORT_SYMBOL(osd_dev_fini);
138
139 static struct osd_request *_osd_request_alloc(gfp_t gfp)
140 {
141         struct osd_request *or;
142
143         /* TODO: Use mempool with one saved request */
144         or = kzalloc(sizeof(*or), gfp);
145         return or;
146 }
147
148 static void _osd_request_free(struct osd_request *or)
149 {
150         kfree(or);
151 }
152
153 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
154 {
155         struct osd_request *or;
156
157         or = _osd_request_alloc(gfp);
158         if (!or)
159                 return NULL;
160
161         or->osd_dev = dev;
162         or->alloc_flags = gfp;
163         or->timeout = dev->def_timeout;
164         or->retries = OSD_REQ_RETRIES;
165
166         return or;
167 }
168 EXPORT_SYMBOL(osd_start_request);
169
170 /*
171  * If osd_finalize_request() was called but the request was not executed through
172  * the block layer, then we must release BIOs.
173  */
174 static void _abort_unexecuted_bios(struct request *rq)
175 {
176         struct bio *bio;
177
178         while ((bio = rq->bio) != NULL) {
179                 rq->bio = bio->bi_next;
180                 bio_endio(bio, 0);
181         }
182 }
183
184 static void _osd_free_seg(struct osd_request *or __unused,
185         struct _osd_req_data_segment *seg)
186 {
187         if (!seg->buff || !seg->alloc_size)
188                 return;
189
190         kfree(seg->buff);
191         seg->buff = NULL;
192         seg->alloc_size = 0;
193 }
194
195 void osd_end_request(struct osd_request *or)
196 {
197         struct request *rq = or->request;
198
199         _osd_free_seg(or, &or->set_attr);
200         _osd_free_seg(or, &or->enc_get_attr);
201         _osd_free_seg(or, &or->get_attr);
202
203         if (rq) {
204                 if (rq->next_rq) {
205                         _abort_unexecuted_bios(rq->next_rq);
206                         blk_put_request(rq->next_rq);
207                 }
208
209                 _abort_unexecuted_bios(rq);
210                 blk_put_request(rq);
211         }
212         _osd_request_free(or);
213 }
214 EXPORT_SYMBOL(osd_end_request);
215
216 int osd_execute_request(struct osd_request *or)
217 {
218         return blk_execute_rq(or->request->q, NULL, or->request, 0);
219 }
220 EXPORT_SYMBOL(osd_execute_request);
221
222 static void osd_request_async_done(struct request *req, int error)
223 {
224         struct osd_request *or = req->end_io_data;
225
226         or->async_error = error;
227
228         if (error)
229                 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
230
231         if (or->async_done)
232                 or->async_done(or, or->async_private);
233         else
234                 osd_end_request(or);
235 }
236
237 int osd_execute_request_async(struct osd_request *or,
238         osd_req_done_fn *done, void *private)
239 {
240         or->request->end_io_data = or;
241         or->async_private = private;
242         or->async_done = done;
243
244         blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
245                               osd_request_async_done);
246         return 0;
247 }
248 EXPORT_SYMBOL(osd_execute_request_async);
249
250 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
251 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
252
253 static int _osd_realloc_seg(struct osd_request *or,
254         struct _osd_req_data_segment *seg, unsigned max_bytes)
255 {
256         void *buff;
257
258         if (seg->alloc_size >= max_bytes)
259                 return 0;
260
261         buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
262         if (!buff) {
263                 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
264                         seg->alloc_size);
265                 return -ENOMEM;
266         }
267
268         memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
269         seg->buff = buff;
270         seg->alloc_size = max_bytes;
271         return 0;
272 }
273
274 static int _alloc_set_attr_list(struct osd_request *or,
275         const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
276 {
277         unsigned total_bytes = add_bytes;
278
279         for (; nelem; --nelem, ++oa)
280                 total_bytes += _osd_req_alist_elem_size(or, oa->len);
281
282         OSD_DEBUG("total_bytes=%d\n", total_bytes);
283         return _osd_realloc_seg(or, &or->set_attr, total_bytes);
284 }
285
286 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
287 {
288         OSD_DEBUG("total_bytes=%d\n", max_bytes);
289         return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
290 }
291
292 static int _alloc_get_attr_list(struct osd_request *or)
293 {
294         OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
295         return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
296 }
297
298 /*
299  * Common to all OSD commands
300  */
301
302 static void _osdv1_req_encode_common(struct osd_request *or,
303         __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
304 {
305         struct osdv1_cdb *ocdb = &or->cdb.v1;
306
307         /*
308          * For speed, the commands
309          *      OSD_ACT_PERFORM_SCSI_COMMAND    , V1 0x8F7E, V2 0x8F7C
310          *      OSD_ACT_SCSI_TASK_MANAGEMENT    , V1 0x8F7F, V2 0x8F7D
311          * are not supported here. Should pass zero and set after the call
312          */
313         act &= cpu_to_be16(~0x0080); /* V1 action code */
314
315         OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
316
317         ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
318         ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
319         ocdb->h.varlen_cdb.service_action = act;
320
321         ocdb->h.partition = cpu_to_be64(obj->partition);
322         ocdb->h.object = cpu_to_be64(obj->id);
323         ocdb->h.v1.length = cpu_to_be64(len);
324         ocdb->h.v1.start_address = cpu_to_be64(offset);
325 }
326
327 static void _osd_req_encode_common(struct osd_request *or,
328         __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
329 {
330         _osdv1_req_encode_common(or, act, obj, offset, len);
331 }
332
333 /*
334  * Device commands
335  */
336 void osd_req_format(struct osd_request *or, u64 tot_capacity)
337 {
338         _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
339                                 tot_capacity);
340 }
341 EXPORT_SYMBOL(osd_req_format);
342
343 /*
344  * Partition commands
345  */
346 static void _osd_req_encode_partition(struct osd_request *or,
347         __be16 act, osd_id partition)
348 {
349         struct osd_obj_id par = {
350                 .partition = partition,
351                 .id = 0,
352         };
353
354         _osd_req_encode_common(or, act, &par, 0, 0);
355 }
356
357 void osd_req_create_partition(struct osd_request *or, osd_id partition)
358 {
359         _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
360 }
361 EXPORT_SYMBOL(osd_req_create_partition);
362
363 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
364 {
365         _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
366 }
367 EXPORT_SYMBOL(osd_req_remove_partition);
368
369 /*
370  * Object commands
371  */
372 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
373 {
374         _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
375 }
376 EXPORT_SYMBOL(osd_req_create_object);
377
378 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
379 {
380         _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
381 }
382 EXPORT_SYMBOL(osd_req_remove_object);
383
384 void osd_req_write(struct osd_request *or,
385         const struct osd_obj_id *obj, struct bio *bio, u64 offset)
386 {
387         _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
388         WARN_ON(or->out.bio || or->out.total_bytes);
389         bio->bi_rw |= (1 << BIO_RW);
390         or->out.bio = bio;
391         or->out.total_bytes = bio->bi_size;
392 }
393 EXPORT_SYMBOL(osd_req_write);
394
395 void osd_req_read(struct osd_request *or,
396         const struct osd_obj_id *obj, struct bio *bio, u64 offset)
397 {
398         _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
399         WARN_ON(or->in.bio || or->in.total_bytes);
400         bio->bi_rw &= ~(1 << BIO_RW);
401         or->in.bio = bio;
402         or->in.total_bytes = bio->bi_size;
403 }
404 EXPORT_SYMBOL(osd_req_read);
405
406 void osd_req_get_attributes(struct osd_request *or,
407         const struct osd_obj_id *obj)
408 {
409         _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
410 }
411 EXPORT_SYMBOL(osd_req_get_attributes);
412
413 void osd_req_set_attributes(struct osd_request *or,
414         const struct osd_obj_id *obj)
415 {
416         _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
417 }
418 EXPORT_SYMBOL(osd_req_set_attributes);
419
420 /*
421  * Attributes List-mode
422  */
423
424 int osd_req_add_set_attr_list(struct osd_request *or,
425         const struct osd_attr *oa, unsigned nelem)
426 {
427         unsigned total_bytes = or->set_attr.total_bytes;
428         void *attr_last;
429         int ret;
430
431         if (or->attributes_mode &&
432             or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
433                 WARN_ON(1);
434                 return -EINVAL;
435         }
436         or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
437
438         if (!total_bytes) { /* first-time: allocate and put list header */
439                 total_bytes = _osd_req_sizeof_alist_header(or);
440                 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
441                 if (ret)
442                         return ret;
443                 _osd_req_set_alist_type(or, or->set_attr.buff,
444                                         OSD_ATTR_LIST_SET_RETRIEVE);
445         }
446         attr_last = or->set_attr.buff + total_bytes;
447
448         for (; nelem; --nelem) {
449                 struct osd_attributes_list_element *attr;
450                 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
451
452                 total_bytes += elem_size;
453                 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
454                         or->set_attr.total_bytes = total_bytes - elem_size;
455                         ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
456                         if (ret)
457                                 return ret;
458                         attr_last =
459                                 or->set_attr.buff + or->set_attr.total_bytes;
460                 }
461
462                 attr = attr_last;
463                 attr->attr_page = cpu_to_be32(oa->attr_page);
464                 attr->attr_id = cpu_to_be32(oa->attr_id);
465                 attr->attr_bytes = cpu_to_be16(oa->len);
466                 memcpy(attr->attr_val, oa->val_ptr, oa->len);
467
468                 attr_last += elem_size;
469                 ++oa;
470         }
471
472         or->set_attr.total_bytes = total_bytes;
473         return 0;
474 }
475 EXPORT_SYMBOL(osd_req_add_set_attr_list);
476
477 static int _append_map_kern(struct request *req,
478         void *buff, unsigned len, gfp_t flags)
479 {
480         struct bio *bio;
481         int ret;
482
483         bio = bio_map_kern(req->q, buff, len, flags);
484         if (IS_ERR(bio)) {
485                 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
486                         PTR_ERR(bio));
487                 return PTR_ERR(bio);
488         }
489         ret = blk_rq_append_bio(req->q, req, bio);
490         if (ret) {
491                 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
492                 bio_put(bio);
493         }
494         return ret;
495 }
496
497 static int _req_append_segment(struct osd_request *or,
498         unsigned padding, struct _osd_req_data_segment *seg,
499         struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
500 {
501         void *pad_buff;
502         int ret;
503
504         if (padding) {
505                 /* check if we can just add it to last buffer */
506                 if (last_seg &&
507                     (padding <= last_seg->alloc_size - last_seg->total_bytes))
508                         pad_buff = last_seg->buff + last_seg->total_bytes;
509                 else
510                         pad_buff = io->pad_buff;
511
512                 ret = _append_map_kern(io->req, pad_buff, padding,
513                                        or->alloc_flags);
514                 if (ret)
515                         return ret;
516                 io->total_bytes += padding;
517         }
518
519         ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
520                                or->alloc_flags);
521         if (ret)
522                 return ret;
523
524         io->total_bytes += seg->total_bytes;
525         OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
526                   seg->total_bytes);
527         return 0;
528 }
529
530 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
531 {
532         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
533         unsigned padding;
534         int ret;
535
536         if (!or->set_attr.total_bytes) {
537                 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
538                 return 0;
539         }
540
541         cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
542         cdbh->attrs_list.set_attr_offset =
543                 osd_req_encode_offset(or, or->out.total_bytes, &padding);
544
545         ret = _req_append_segment(or, padding, &or->set_attr,
546                                   or->out.last_seg, &or->out);
547         if (ret)
548                 return ret;
549
550         or->out.last_seg = &or->set_attr;
551         return 0;
552 }
553
554 int osd_req_add_get_attr_list(struct osd_request *or,
555         const struct osd_attr *oa, unsigned nelem)
556 {
557         unsigned total_bytes = or->enc_get_attr.total_bytes;
558         void *attr_last;
559         int ret;
560
561         if (or->attributes_mode &&
562             or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
563                 WARN_ON(1);
564                 return -EINVAL;
565         }
566         or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
567
568         /* first time calc data-in list header size */
569         if (!or->get_attr.total_bytes)
570                 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
571
572         /* calc data-out info */
573         if (!total_bytes) { /* first-time: allocate and put list header */
574                 unsigned max_bytes;
575
576                 total_bytes = _osd_req_sizeof_alist_header(or);
577                 max_bytes = total_bytes +
578                         nelem * sizeof(struct osd_attributes_list_attrid);
579                 ret = _alloc_get_attr_desc(or, max_bytes);
580                 if (ret)
581                         return ret;
582
583                 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
584                                         OSD_ATTR_LIST_GET);
585         }
586         attr_last = or->enc_get_attr.buff + total_bytes;
587
588         for (; nelem; --nelem) {
589                 struct osd_attributes_list_attrid *attrid;
590                 const unsigned cur_size = sizeof(*attrid);
591
592                 total_bytes += cur_size;
593                 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
594                         or->enc_get_attr.total_bytes = total_bytes - cur_size;
595                         ret = _alloc_get_attr_desc(or,
596                                         total_bytes + nelem * sizeof(*attrid));
597                         if (ret)
598                                 return ret;
599                         attr_last = or->enc_get_attr.buff +
600                                 or->enc_get_attr.total_bytes;
601                 }
602
603                 attrid = attr_last;
604                 attrid->attr_page = cpu_to_be32(oa->attr_page);
605                 attrid->attr_id = cpu_to_be32(oa->attr_id);
606
607                 attr_last += cur_size;
608
609                 /* calc data-in size */
610                 or->get_attr.total_bytes +=
611                         _osd_req_alist_elem_size(or, oa->len);
612                 ++oa;
613         }
614
615         or->enc_get_attr.total_bytes = total_bytes;
616
617         OSD_DEBUG(
618                "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
619                or->get_attr.total_bytes,
620                or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
621                or->enc_get_attr.total_bytes,
622                (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
623                         / sizeof(struct osd_attributes_list_attrid));
624
625         return 0;
626 }
627 EXPORT_SYMBOL(osd_req_add_get_attr_list);
628
629 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
630 {
631         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
632         unsigned out_padding;
633         unsigned in_padding;
634         int ret;
635
636         if (!or->enc_get_attr.total_bytes) {
637                 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
638                 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
639                 return 0;
640         }
641
642         ret = _alloc_get_attr_list(or);
643         if (ret)
644                 return ret;
645
646         /* The out-going buffer info update */
647         OSD_DEBUG("out-going\n");
648         cdbh->attrs_list.get_attr_desc_bytes =
649                 cpu_to_be32(or->enc_get_attr.total_bytes);
650
651         cdbh->attrs_list.get_attr_desc_offset =
652                 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
653
654         ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
655                                   or->out.last_seg, &or->out);
656         if (ret)
657                 return ret;
658         or->out.last_seg = &or->enc_get_attr;
659
660         /* The incoming buffer info update */
661         OSD_DEBUG("in-coming\n");
662         cdbh->attrs_list.get_attr_alloc_length =
663                 cpu_to_be32(or->get_attr.total_bytes);
664
665         cdbh->attrs_list.get_attr_offset =
666                 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
667
668         ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
669                                   &or->in);
670         if (ret)
671                 return ret;
672         or->in.last_seg = &or->get_attr;
673
674         return 0;
675 }
676
677 int osd_req_decode_get_attr_list(struct osd_request *or,
678         struct osd_attr *oa, int *nelem, void **iterator)
679 {
680         unsigned cur_bytes, returned_bytes;
681         int n;
682         const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
683         void *cur_p;
684
685         if (!_osd_req_is_alist_type(or, or->get_attr.buff,
686                                     OSD_ATTR_LIST_SET_RETRIEVE)) {
687                 oa->attr_page = 0;
688                 oa->attr_id = 0;
689                 oa->val_ptr = NULL;
690                 oa->len = 0;
691                 *iterator = NULL;
692                 return 0;
693         }
694
695         if (*iterator) {
696                 BUG_ON((*iterator < or->get_attr.buff) ||
697                      (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
698                 cur_p = *iterator;
699                 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
700                 returned_bytes = or->get_attr.total_bytes;
701         } else { /* first time decode the list header */
702                 cur_bytes = sizeof_attr_list;
703                 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
704                                         sizeof_attr_list;
705
706                 cur_p = or->get_attr.buff + sizeof_attr_list;
707
708                 if (returned_bytes > or->get_attr.alloc_size) {
709                         OSD_DEBUG("target report: space was not big enough! "
710                                   "Allocate=%u Needed=%u\n",
711                                   or->get_attr.alloc_size,
712                                   returned_bytes + sizeof_attr_list);
713
714                         returned_bytes =
715                                 or->get_attr.alloc_size - sizeof_attr_list;
716                 }
717                 or->get_attr.total_bytes = returned_bytes;
718         }
719
720         for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
721                 struct osd_attributes_list_element *attr = cur_p;
722                 unsigned inc;
723
724                 oa->len = be16_to_cpu(attr->attr_bytes);
725                 inc = _osd_req_alist_elem_size(or, oa->len);
726                 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
727                           oa->len, inc, cur_bytes);
728                 cur_bytes += inc;
729                 if (cur_bytes > returned_bytes) {
730                         OSD_ERR("BAD FOOD from target. list not valid!"
731                                 "c=%d r=%d n=%d\n",
732                                 cur_bytes, returned_bytes, n);
733                         oa->val_ptr = NULL;
734                         break;
735                 }
736
737                 oa->attr_page = be32_to_cpu(attr->attr_page);
738                 oa->attr_id = be32_to_cpu(attr->attr_id);
739                 oa->val_ptr = attr->attr_val;
740
741                 cur_p += inc;
742                 ++oa;
743         }
744
745         *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
746         *nelem = n;
747         return returned_bytes - cur_bytes;
748 }
749 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
750
751 /*
752  * Attributes Page-mode
753  */
754
755 int osd_req_add_get_attr_page(struct osd_request *or,
756         u32 page_id, void *attar_page, unsigned max_page_len,
757         const struct osd_attr *set_one_attr)
758 {
759         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
760
761         if (or->attributes_mode &&
762             or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
763                 WARN_ON(1);
764                 return -EINVAL;
765         }
766         or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
767
768         or->get_attr.buff = attar_page;
769         or->get_attr.total_bytes = max_page_len;
770
771         or->set_attr.buff = set_one_attr->val_ptr;
772         or->set_attr.total_bytes = set_one_attr->len;
773
774         cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
775         cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
776         /* ocdb->attrs_page.get_attr_offset; */
777
778         cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
779         cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
780         cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
781         /* ocdb->attrs_page.set_attr_offset; */
782         return 0;
783 }
784 EXPORT_SYMBOL(osd_req_add_get_attr_page);
785
786 static int _osd_req_finalize_attr_page(struct osd_request *or)
787 {
788         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
789         unsigned in_padding, out_padding;
790         int ret;
791
792         /* returned page */
793         cdbh->attrs_page.get_attr_offset =
794                 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
795
796         ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
797                                   &or->in);
798         if (ret)
799                 return ret;
800
801         /* set one value */
802         cdbh->attrs_page.set_attr_offset =
803                 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
804
805         ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
806                                   &or->out);
807         return ret;
808 }
809
810 static int _osd_req_finalize_data_integrity(struct osd_request *or,
811         bool has_in, bool has_out, const u8 *cap_key)
812 {
813         struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
814         int ret;
815
816         if (!osd_is_sec_alldata(sec_parms))
817                 return 0;
818
819         if (has_out) {
820                 struct _osd_req_data_segment seg = {
821                         .buff = &or->out_data_integ,
822                         .total_bytes = sizeof(or->out_data_integ),
823                 };
824                 unsigned pad;
825
826                 or->out_data_integ.data_bytes = cpu_to_be64(
827                         or->out.bio ? or->out.bio->bi_size : 0);
828                 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
829                         or->set_attr.total_bytes);
830                 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
831                         or->enc_get_attr.total_bytes);
832
833                 sec_parms->data_out_integrity_check_offset =
834                         osd_req_encode_offset(or, or->out.total_bytes, &pad);
835
836                 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
837                                           &or->out);
838                 if (ret)
839                         return ret;
840                 or->out.last_seg = NULL;
841
842                 /* they are now all chained to request sign them all together */
843                 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
844                                   cap_key);
845         }
846
847         if (has_in) {
848                 struct _osd_req_data_segment seg = {
849                         .buff = &or->in_data_integ,
850                         .total_bytes = sizeof(or->in_data_integ),
851                 };
852                 unsigned pad;
853
854                 sec_parms->data_in_integrity_check_offset =
855                         osd_req_encode_offset(or, or->in.total_bytes, &pad);
856
857                 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
858                                           &or->in);
859                 if (ret)
860                         return ret;
861
862                 or->in.last_seg = NULL;
863         }
864
865         return 0;
866 }
867
868 /*
869  * osd_finalize_request and helpers
870  */
871
872 static int _init_blk_request(struct osd_request *or,
873         bool has_in, bool has_out)
874 {
875         gfp_t flags = or->alloc_flags;
876         struct scsi_device *scsi_device = or->osd_dev->scsi_device;
877         struct request_queue *q = scsi_device->request_queue;
878         struct request *req;
879         int ret = -ENOMEM;
880
881         req = blk_get_request(q, has_out, flags);
882         if (!req)
883                 goto out;
884
885         or->request = req;
886         req->cmd_type = REQ_TYPE_BLOCK_PC;
887         req->timeout = or->timeout;
888         req->retries = or->retries;
889         req->sense = or->sense;
890         req->sense_len = 0;
891
892         if (has_out) {
893                 or->out.req = req;
894                 if (has_in) {
895                         /* allocate bidi request */
896                         req = blk_get_request(q, READ, flags);
897                         if (!req) {
898                                 OSD_DEBUG("blk_get_request for bidi failed\n");
899                                 goto out;
900                         }
901                         req->cmd_type = REQ_TYPE_BLOCK_PC;
902                         or->in.req = or->request->next_rq = req;
903                 }
904         } else if (has_in)
905                 or->in.req = req;
906
907         ret = 0;
908 out:
909         OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
910                         or, has_in, has_out, ret, or->request);
911         return ret;
912 }
913
914 int osd_finalize_request(struct osd_request *or,
915         u8 options, const void *cap, const u8 *cap_key)
916 {
917         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
918         bool has_in, has_out;
919         int ret;
920
921         if (options & OSD_REQ_FUA)
922                 cdbh->options |= OSD_CDB_FUA;
923
924         if (options & OSD_REQ_DPO)
925                 cdbh->options |= OSD_CDB_DPO;
926
927         if (options & OSD_REQ_BYPASS_TIMESTAMPS)
928                 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
929
930         osd_set_caps(&or->cdb, cap);
931
932         has_in = or->in.bio || or->get_attr.total_bytes;
933         has_out = or->out.bio || or->set_attr.total_bytes ||
934                 or->enc_get_attr.total_bytes;
935
936         ret = _init_blk_request(or, has_in, has_out);
937         if (ret) {
938                 OSD_DEBUG("_init_blk_request failed\n");
939                 return ret;
940         }
941
942         if (or->out.bio) {
943                 ret = blk_rq_append_bio(or->request->q, or->out.req,
944                                         or->out.bio);
945                 if (ret) {
946                         OSD_DEBUG("blk_rq_append_bio out failed\n");
947                         return ret;
948                 }
949                 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
950                         _LLU(or->out.total_bytes), or->out.req->data_len);
951         }
952         if (or->in.bio) {
953                 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
954                 if (ret) {
955                         OSD_DEBUG("blk_rq_append_bio in failed\n");
956                         return ret;
957                 }
958                 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
959                         _LLU(or->in.total_bytes), or->in.req->data_len);
960         }
961
962         or->out.pad_buff = sg_out_pad_buffer;
963         or->in.pad_buff = sg_in_pad_buffer;
964
965         if (!or->attributes_mode)
966                 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
967         cdbh->command_specific_options |= or->attributes_mode;
968         if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
969                 ret = _osd_req_finalize_attr_page(or);
970         } else {
971                 /* TODO: I think that for the GET_ATTR command these 2 should
972                  * be reversed to keep them in execution order (for embeded
973                  * targets with low memory footprint)
974                  */
975                 ret = _osd_req_finalize_set_attr_list(or);
976                 if (ret) {
977                         OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
978                         return ret;
979                 }
980
981                 ret = _osd_req_finalize_get_attr_list(or);
982                 if (ret) {
983                         OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
984                         return ret;
985                 }
986         }
987
988         ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
989         if (ret)
990                 return ret;
991
992         osd_sec_sign_cdb(&or->cdb, cap_key);
993
994         or->request->cmd = or->cdb.buff;
995         or->request->cmd_len = _osd_req_cdb_len(or);
996
997         return 0;
998 }
999 EXPORT_SYMBOL(osd_finalize_request);
1000
1001 /*
1002  * Implementation of osd_sec.h API
1003  * TODO: Move to a separate osd_sec.c file at a later stage.
1004  */
1005
1006 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1007         OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE   |
1008         OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1009         OSD_SEC_CAP_WRITE  | OSD_SEC_CAP_READ     | OSD_SEC_CAP_POL_SEC  |
1010         OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1011 };
1012
1013 void osd_sec_init_nosec_doall_caps(void *caps,
1014         const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1015 {
1016         struct osd_capability *cap = caps;
1017         u8 type;
1018         u8 descriptor_type;
1019
1020         if (likely(obj->id)) {
1021                 if (unlikely(is_collection)) {
1022                         type = OSD_SEC_OBJ_COLLECTION;
1023                         descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1024                                                   OSD_SEC_OBJ_DESC_COL;
1025                 } else {
1026                         type = OSD_SEC_OBJ_USER;
1027                         descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1028                 }
1029                 WARN_ON(!obj->partition);
1030         } else {
1031                 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1032                                         OSD_SEC_OBJ_ROOT;
1033                 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1034         }
1035
1036         memset(cap, 0, sizeof(*cap));
1037
1038         cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1039         cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1040         cap->h.security_method = OSD_SEC_NOSEC;
1041 /*      cap->expiration_time;
1042         cap->AUDIT[30-10];
1043         cap->discriminator[42-30];
1044         cap->object_created_time; */
1045         cap->h.object_type = type;
1046         osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1047         cap->h.object_descriptor_type = descriptor_type;
1048         cap->od.obj_desc.policy_access_tag = 0;
1049         cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1050         cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1051 }
1052 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1053
1054 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1055 {
1056         memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
1057 }
1058
1059 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1060 {
1061         return false;
1062 }
1063
1064 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1065 {
1066 }
1067
1068 void osd_sec_sign_data(void *data_integ __unused,
1069                        struct bio *bio __unused, const u8 *cap_key __unused)
1070 {
1071 }
1072
1073 /*
1074  * Declared in osd_protocol.h
1075  * 4.12.5 Data-In and Data-Out buffer offsets
1076  * byte offset = mantissa * (2^(exponent+8))
1077  * Returns the smallest allowed encoded offset that contains given @offset
1078  * The actual encoded offset returned is @offset + *@padding.
1079  */
1080 osd_cdb_offset __osd_encode_offset(
1081         u64 offset, unsigned *padding, int min_shift, int max_shift)
1082 {
1083         u64 try_offset = -1, mod, align;
1084         osd_cdb_offset be32_offset;
1085         int shift;
1086
1087         *padding = 0;
1088         if (!offset)
1089                 return 0;
1090
1091         for (shift = min_shift; shift < max_shift; ++shift) {
1092                 try_offset = offset >> shift;
1093                 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1094                         break;
1095         }
1096
1097         BUG_ON(shift == max_shift);
1098
1099         align = 1 << shift;
1100         mod = offset & (align - 1);
1101         if (mod) {
1102                 *padding = align - mod;
1103                 try_offset += 1;
1104         }
1105
1106         try_offset |= ((shift - 8) & 0xf) << 28;
1107         be32_offset = cpu_to_be32((u32)try_offset);
1108
1109         OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1110                  _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1111                  be32_offset, *padding);
1112         return be32_offset;
1113 }