Staging: hv: fix typedefs in nvspprotocol.h
[safe/jmp/linux-2.6] / drivers / staging / hv / storvsc_drv.c
1 /*
2  *
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *
22  */
23
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/blkdev.h>
28
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_devinfo.h>
36
37 #include <scsi/scsi_dbg.h>
38
39 #include "osd.h"
40 #include "include/logging.h"
41 #include "vmbus.h"
42 #include "include/StorVscApi.h"
43
44
45 /* #defines */
46
47
48
49 /* Data types */
50
51 struct host_device_context {
52     struct work_struct          host_rescan_work;  /* must be 1st field */
53     struct device_context       *device_ctx; /* point back to our device context */
54     struct kmem_cache               *request_pool;
55     unsigned int                        port;
56     unsigned char                       path;
57     unsigned char                       target;
58 };
59
60 struct storvsc_cmd_request {
61         struct list_head        entry;
62         struct scsi_cmnd        *cmd;
63
64         unsigned int bounce_sgl_count;
65         struct scatterlist      *bounce_sgl;
66
67         struct hv_storvsc_request request;
68         /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
69         /* The extension buffer falls right here and is pointed to by request.Extension; */
70 };
71
72 struct storvsc_driver_context {
73         /* !! These must be the first 2 fields !! */
74         struct driver_context   drv_ctx;
75         struct storvsc_driver_object drv_obj;
76 };
77
78 /* Static decl */
79 static int storvsc_probe(struct device *dev);
80 static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *));
81 static int storvsc_device_alloc(struct scsi_device *);
82 static int storvsc_device_configure(struct scsi_device *);
83 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
84 static void storvsc_host_rescan_callback(struct work_struct *work);
85 static void storvsc_host_rescan(struct hv_device* device_obj);
86 static int storvsc_remove(struct device *dev);
87
88 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len);
89 static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
90 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
91 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count);
92 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count);
93
94 static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count);
95 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info);
96
97
98 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
99
100 /* The one and only one */
101 static struct storvsc_driver_context g_storvsc_drv;
102
103 /* Scsi driver */
104 static struct scsi_host_template scsi_driver = {
105         .module                                         = THIS_MODULE,
106         .name                                           = "storvsc_host_t",
107         .bios_param                                     = storvsc_get_chs,
108         .queuecommand                           = storvsc_queuecommand,
109         .eh_host_reset_handler          = storvsc_host_reset_handler,
110         .slave_alloc                            = storvsc_device_alloc,
111         .slave_configure                        = storvsc_device_configure,
112         .cmd_per_lun                            = 1,
113         .can_queue                                      = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, /* 64 max_queue * 1 target */
114         .this_id                                        = -1,
115         /* no use setting to 0 since ll_blk_rw reset it to 1 */
116         .sg_tablesize                           = MAX_MULTIPAGE_BUFFER_COUNT,/* currently 32 */
117         /* ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge into 1 sg element. If set, we must */
118         /* limit the max_segment_size to PAGE_SIZE, otherwise we may get 1 sg element that represents multiple */
119         /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
120         .use_clustering                         = ENABLE_CLUSTERING,
121         /* Make sure we dont get a sg segment crosses a page boundary */
122         .dma_boundary                           = PAGE_SIZE-1,
123 };
124
125
126 /*++
127
128 Name:   storvsc_drv_init()
129
130 Desc:   StorVsc driver initialization.
131
132 --*/
133 static int storvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
134 {
135         int ret=0;
136         struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
137         struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
138
139         DPRINT_ENTER(STORVSC_DRV);
140
141         vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
142
143         storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
144         storvsc_drv_obj->OnHostRescan = storvsc_host_rescan;
145
146         /* Callback to client driver to complete the initialization */
147         pfn_drv_init(&storvsc_drv_obj->Base);
148
149         DPRINT_INFO(STORVSC_DRV, "request extension size %u, max outstanding reqs %u", storvsc_drv_obj->RequestExtSize, storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
150
151         if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel < STORVSC_MAX_IO_REQUESTS)
152         {
153                 DPRINT_ERR(STORVSC_DRV, "The number of outstanding io requests (%d) is larger than that supported (%d) internally.",
154                         STORVSC_MAX_IO_REQUESTS, storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
155                 return -1;
156         }
157
158         drv_ctx->driver.name = storvsc_drv_obj->Base.name;
159         memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, sizeof(struct hv_guid));
160
161         drv_ctx->probe = storvsc_probe;
162         drv_ctx->remove = storvsc_remove;
163
164         /* The driver belongs to vmbus */
165         ret = vmbus_child_driver_register(drv_ctx);
166
167         DPRINT_EXIT(STORVSC_DRV);
168
169         return ret;
170 }
171
172
173 static int storvsc_drv_exit_cb(struct device *dev, void *data)
174 {
175         struct device **curr = (struct device **)data;
176         *curr = dev;
177         return 1; /* stop iterating */
178 }
179
180 /*++
181
182 Name:   storvsc_drv_exit()
183
184 Desc:
185
186 --*/
187 static void storvsc_drv_exit(void)
188 {
189         struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
190         struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
191         struct device *current_dev=NULL;
192         int ret;
193
194         DPRINT_ENTER(STORVSC_DRV);
195
196         while (1)
197         {
198                 current_dev = NULL;
199
200                 /* Get the device */
201                 ret = driver_for_each_device(&drv_ctx->driver, NULL,
202                                              (void *) &current_dev,
203                                              storvsc_drv_exit_cb);
204
205                 if (ret)
206                         DPRINT_WARN(STORVSC_DRV,
207                                     "driver_for_each_device returned %d", ret);
208
209                 if (current_dev == NULL)
210                         break;
211
212                 /* Initiate removal from the top-down */
213                 device_unregister(current_dev);
214         }
215
216         if (storvsc_drv_obj->Base.OnCleanup)
217                 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
218
219         vmbus_child_driver_unregister(drv_ctx);
220
221         DPRINT_EXIT(STORVSC_DRV);
222
223         return;
224 }
225
226 /*++
227
228 Name:   storvsc_probe()
229
230 Desc:   Add a new device for this driver
231
232 --*/
233 static int storvsc_probe(struct device *device)
234 {
235         int ret=0;
236
237         struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
238         struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
239         struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
240
241         struct device_context *device_ctx = device_to_device_context(device);
242         struct hv_device *device_obj = &device_ctx->device_obj;
243
244         struct Scsi_Host *host;
245         struct host_device_context *host_device_ctx;
246         struct storvsc_device_info device_info;
247
248         DPRINT_ENTER(STORVSC_DRV);
249
250         if (!storvsc_drv_obj->Base.OnDeviceAdd)
251                 return -1;
252
253         host = scsi_host_alloc(&scsi_driver, sizeof(struct host_device_context));
254         if (!host)
255         {
256                 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
257                 return -ENOMEM;
258         }
259
260         dev_set_drvdata(device, host);
261
262         host_device_ctx = (struct host_device_context*)host->hostdata;
263         memset(host_device_ctx, 0, sizeof(struct host_device_context));
264
265         host_device_ctx->port = host->host_no;
266         host_device_ctx->device_ctx = device_ctx;
267
268         INIT_WORK(&host_device_ctx->host_rescan_work, storvsc_host_rescan_callback);
269
270         host_device_ctx->request_pool =
271             kmem_cache_create
272             (dev_name(&device_ctx->device),
273              sizeof(struct storvsc_cmd_request) + storvsc_drv_obj->RequestExtSize,
274              0,
275              SLAB_HWCACHE_ALIGN, NULL);
276
277         if (!host_device_ctx->request_pool)
278         {
279                 scsi_host_put(host);
280                 DPRINT_EXIT(STORVSC_DRV);
281
282                 return -ENOMEM;
283         }
284
285         device_info.PortNumber = host->host_no;
286         /* Call to the vsc driver to add the device */
287         ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info);
288         if (ret != 0)
289         {
290                 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
291                 kmem_cache_destroy(host_device_ctx->request_pool);
292                 scsi_host_put(host);
293                 DPRINT_EXIT(STORVSC_DRV);
294
295                 return -1;
296         }
297
298         /* host_device_ctx->port = device_info.PortNumber; */
299         host_device_ctx->path = device_info.PathId;
300         host_device_ctx->target = device_info.TargetId;
301
302         host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;    /* max # of devices per target */
303         host->max_id = STORVSC_MAX_TARGETS;                     /* max # of targets per channel */
304         host->max_channel = STORVSC_MAX_CHANNELS -1;    /* max # of channels */
305
306         /* Register the HBA and start the scsi bus scan */
307         ret = scsi_add_host(host, device);
308         if (ret != 0)
309         {
310                 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
311
312                 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
313
314                 kmem_cache_destroy(host_device_ctx->request_pool);
315                 scsi_host_put(host);
316                 DPRINT_EXIT(STORVSC_DRV);
317
318                 return -1;
319         }
320
321         scsi_scan_host(host);
322
323         DPRINT_EXIT(STORVSC_DRV);
324
325         return ret;
326 }
327
328
329 /*++
330
331 Name:   storvsc_remove()
332
333 Desc:   Callback when our device is removed
334
335 --*/
336 static int storvsc_remove(struct device *device)
337 {
338         int ret=0;
339
340         struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
341         struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
342         struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
343
344         struct device_context *device_ctx = device_to_device_context(device);
345         struct hv_device *device_obj = &device_ctx->device_obj;
346
347         struct Scsi_Host *host = dev_get_drvdata(device);
348         struct host_device_context *host_device_ctx=(struct host_device_context*)host->hostdata;
349
350
351         DPRINT_ENTER(STORVSC_DRV);
352
353         if (!storvsc_drv_obj->Base.OnDeviceRemove)
354         {
355                 DPRINT_EXIT(STORVSC_DRV);
356                 return -1;
357         }
358
359         /* Call to the vsc driver to let it know that the device is being removed */
360         ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
361         if (ret != 0)
362         {
363                 /* TODO: */
364                 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)", ret);
365         }
366
367         if (host_device_ctx->request_pool)
368         {
369                 kmem_cache_destroy(host_device_ctx->request_pool);
370                 host_device_ctx->request_pool = NULL;
371         }
372
373         DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
374         scsi_remove_host(host);
375
376         DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
377         scsi_host_put(host);
378
379         DPRINT_EXIT(STORVSC_DRV);
380
381         return ret;
382 }
383
384 /*++
385
386 Name:   storvsc_commmand_completion()
387
388 Desc:   Command completion processing
389
390 --*/
391 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
392 {
393         struct storvsc_cmd_request *cmd_request = (struct storvsc_cmd_request*)request->Context;
394         struct scsi_cmnd *scmnd = cmd_request->cmd;
395         struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
396         void (*scsi_done_fn)(struct scsi_cmnd *);
397         struct scsi_sense_hdr sense_hdr;
398
399         ASSERT(request == &cmd_request->request);
400         ASSERT((unsigned long)scmnd->host_scribble == (unsigned long)cmd_request);
401         ASSERT(scmnd);
402         ASSERT(scmnd->scsi_done);
403
404         DPRINT_ENTER(STORVSC_DRV);
405
406         if (cmd_request->bounce_sgl_count)/* using bounce buffer */
407         {
408                 /* printk("copy_from_bounce_buffer\n"); */
409
410                 /* FIXME: We can optimize on writes by just skipping this */
411                 copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
412                 destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
413         }
414
415         scmnd->result = request->Status;
416
417         if (scmnd->result)
418         {
419                 if (scsi_normalize_sense(scmnd->sense_buffer, request->SenseBufferSize, &sense_hdr))
420                 {
421                         scsi_print_sense_hdr("storvsc", &sense_hdr);
422                 }
423         }
424
425         ASSERT(request->BytesXfer <= request->DataBuffer.Length);
426         scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
427
428         scsi_done_fn = scmnd->scsi_done;
429
430         scmnd->host_scribble = NULL;
431         scmnd->scsi_done = NULL;
432
433         /* !!DO NOT MODIFY the scmnd after this call */
434         scsi_done_fn(scmnd);
435
436         kmem_cache_free(host_device_ctx->request_pool, cmd_request);
437
438         DPRINT_EXIT(STORVSC_DRV);
439 }
440
441 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
442 {
443         int i=0;
444
445         /* No need to check */
446         if (sg_count < 2)
447                 return -1;
448
449         /* We have at least 2 sg entries */
450         for ( i=0; i<sg_count; i++ )
451         {
452                 if (i == 0) /* make sure 1st one does not have hole */
453                 {
454                         if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
455                                 return i;
456                 }
457                 else if (i == sg_count - 1) /* make sure last one does not have hole */
458                 {
459                         if (sgl[i].offset != 0)
460                                 return i;
461                 }
462                 else /* make sure no hole in the middle */
463                 {
464                         if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
465                         {
466                                 return i;
467                         }
468                 }
469         }
470         return -1;
471 }
472
473 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len)
474 {
475         int i;
476         int num_pages=0;
477         struct scatterlist* bounce_sgl;
478         struct page *page_buf;
479
480         num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
481
482         bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
483         if (!bounce_sgl)
484         {
485                 return NULL;
486         }
487
488         for(i=0; i<num_pages; i++)
489         {
490                 page_buf = alloc_page(GFP_ATOMIC);
491                 if (!page_buf)
492                 {
493                         goto cleanup;
494                 }
495                 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
496         }
497
498         return bounce_sgl;
499
500 cleanup:
501         destroy_bounce_buffer(bounce_sgl, num_pages);
502         return NULL;
503 }
504
505 static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
506 {
507         int i;
508         struct page *page_buf;
509
510         for (i=0; i<sg_count; i++)
511         {
512                 if ((page_buf = sg_page((&sgl[i]))) != NULL)
513
514                 {
515                         __free_page(page_buf);
516                 }
517         }
518
519         kfree(sgl);
520 }
521
522 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
523 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
524 {
525         int i=0,j=0;
526         unsigned long src, dest;
527         unsigned int srclen, destlen, copylen;
528         unsigned int total_copied=0;
529         unsigned long bounce_addr=0;
530         unsigned long src_addr=0;
531         unsigned long flags;
532
533         local_irq_save(flags);
534
535         for (i=0; i<orig_sgl_count; i++)
536         {
537                 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
538                 src = src_addr;
539                 srclen = orig_sgl[i].length;
540
541                 /* if (PageHighMem(orig_sgl[i].page)) */
542                 /* printk("HighMem page detected - addr %p", (void*)src); */
543
544                 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
545
546                 if (j == 0)
547                 {
548                         bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
549                 }
550
551                 while (srclen)
552                 {
553                         /* assume bounce offset always == 0 */
554                         dest = bounce_addr + bounce_sgl[j].length;
555                         destlen = PAGE_SIZE - bounce_sgl[j].length;
556
557                         copylen = min(srclen, destlen);
558                         memcpy((void*)dest, (void*)src, copylen);
559
560                         total_copied += copylen;
561                         bounce_sgl[j].length += copylen;
562                         srclen -= copylen;
563                         src += copylen;
564
565                         if (bounce_sgl[j].length == PAGE_SIZE) /* full..move to next entry */
566                         {
567                                 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
568                                 j++;
569
570                                 /* if we need to use another bounce buffer */
571                                 if (srclen || i != orig_sgl_count -1)
572                                 {
573                                         bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
574                                 }
575                         }
576                         else if (srclen == 0 && i == orig_sgl_count -1) /* unmap the last bounce that is < PAGE_SIZE */
577                         {
578                                 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
579                         }
580                 }
581
582                 kunmap_atomic((void*)(src_addr - orig_sgl[i].offset), KM_IRQ0);
583         }
584
585         local_irq_restore(flags);
586
587         return total_copied;
588 }
589
590 /* Assume the original sgl has enough room */
591 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
592 {
593         int i=0,j=0;
594         unsigned long src, dest;
595         unsigned int srclen, destlen, copylen;
596         unsigned int total_copied=0;
597         unsigned long bounce_addr=0;
598         unsigned long dest_addr=0;
599         unsigned long flags;
600
601         local_irq_save(flags);
602
603         for (i=0; i<orig_sgl_count; i++)
604         {
605                 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
606                 dest = dest_addr;
607                 destlen = orig_sgl[i].length;
608                 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
609
610                 if (j == 0)
611                 {
612                         bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
613                 }
614
615                 while (destlen)
616                 {
617                         src = bounce_addr + bounce_sgl[j].offset;
618                         srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
619
620                         copylen = min(srclen, destlen);
621                         memcpy((void*)dest, (void*)src, copylen);
622
623                         total_copied += copylen;
624                         bounce_sgl[j].offset += copylen;
625                         destlen -= copylen;
626                         dest += copylen;
627
628                         if (bounce_sgl[j].offset == bounce_sgl[j].length) /* full */
629                         {
630                                 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
631                                 j++;
632
633                                 /* if we need to use another bounce buffer */
634                                 if (destlen || i != orig_sgl_count -1)
635                                 {
636                                         bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
637                                 }
638                         }
639                         else if (destlen == 0 && i == orig_sgl_count -1) /* unmap the last bounce that is < PAGE_SIZE */
640                         {
641                                 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
642                         }
643                 }
644
645                 kunmap_atomic((void*)(dest_addr - orig_sgl[i].offset), KM_IRQ0);
646         }
647
648         local_irq_restore(flags);
649
650         return total_copied;
651 }
652
653
654 /*++
655
656 Name:   storvsc_queuecommand()
657
658 Desc:   Initiate command processing
659
660 --*/
661 static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *))
662 {
663         int ret=0;
664         struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
665         struct device_context *device_ctx=host_device_ctx->device_ctx;
666         struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
667         struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
668         struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
669
670         struct hv_storvsc_request *request;
671         struct storvsc_cmd_request *cmd_request;
672         unsigned int request_size=0;
673         int i;
674         struct scatterlist *sgl;
675
676         DPRINT_ENTER(STORVSC_DRV);
677
678         DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d queue depth %d tagged %d",
679                 scmnd,
680                 scmnd->sc_data_direction,
681                 scsi_sg_count(scmnd),
682                 scsi_sglist(scmnd),
683                 scsi_bufflen(scmnd),
684                 scmnd->device->queue_depth,
685                 scmnd->device->tagged_supported);
686
687         /* If retrying, no need to prep the cmd */
688         if (scmnd->host_scribble)
689         {
690                 ASSERT(scmnd->scsi_done != NULL);
691
692                 cmd_request = (struct storvsc_cmd_request* )scmnd->host_scribble;
693                 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", scmnd, cmd_request);
694
695                 goto retry_request;
696         }
697
698         ASSERT(scmnd->scsi_done == NULL);
699         ASSERT(scmnd->host_scribble == NULL);
700
701         scmnd->scsi_done = done;
702
703         request_size = sizeof(struct storvsc_cmd_request);
704
705         cmd_request = kmem_cache_alloc(host_device_ctx->request_pool, GFP_ATOMIC);
706         if (!cmd_request)
707         {
708                 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate storvsc_cmd_request...marking queue busy", scmnd);
709
710                 scmnd->scsi_done = NULL;
711                 return SCSI_MLQUEUE_DEVICE_BUSY;
712         }
713
714         /* Setup the cmd request */
715         cmd_request->bounce_sgl_count = 0;
716         cmd_request->bounce_sgl = NULL;
717         cmd_request->cmd = scmnd;
718
719         scmnd->host_scribble = (unsigned char*)cmd_request;
720
721         request = &cmd_request->request;
722
723         request->Extension = (void*)((unsigned long)cmd_request + request_size);
724         DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size, storvsc_drv_obj->RequestExtSize);
725
726         /* Build the SRB */
727         switch(scmnd->sc_data_direction)
728         {
729         case DMA_TO_DEVICE:
730                 request->Type = WRITE_TYPE;
731                 break;
732         case DMA_FROM_DEVICE:
733                 request->Type = READ_TYPE;
734                 break;
735         default:
736                 request->Type = UNKNOWN_TYPE;
737                 break;
738         }
739
740         request->OnIOCompletion = storvsc_commmand_completion;
741         request->Context = cmd_request;/* scmnd; */
742
743         /* request->PortId = scmnd->device->channel; */
744         request->Host = host_device_ctx->port;
745         request->Bus = scmnd->device->channel;
746         request->TargetId = scmnd->device->id;
747         request->LunId = scmnd->device->lun;
748
749         ASSERT(scmnd->cmd_len <= 16);
750         request->CdbLen = scmnd->cmd_len;
751         request->Cdb = scmnd->cmnd;
752
753         request->SenseBuffer = scmnd->sense_buffer;
754         request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
755
756
757         request->DataBuffer.Length = scsi_bufflen(scmnd);
758         if (scsi_sg_count(scmnd))
759         {
760                 sgl = (struct scatterlist*)scsi_sglist(scmnd);
761
762                 /* check if we need to bounce the sgl */
763                 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1)
764                 {
765                         DPRINT_INFO(STORVSC_DRV, "need to bounce buffer for this scmnd %p", scmnd);
766                         cmd_request->bounce_sgl = create_bounce_buffer(sgl, scsi_sg_count(scmnd), scsi_bufflen(scmnd));
767                         if (!cmd_request->bounce_sgl)
768                         {
769                                 DPRINT_ERR(STORVSC_DRV, "unable to create bounce buffer for this scmnd %p", scmnd);
770
771                                 scmnd->scsi_done = NULL;
772                                 scmnd->host_scribble = NULL;
773                                 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
774
775                                 return SCSI_MLQUEUE_HOST_BUSY;
776                         }
777
778                         cmd_request->bounce_sgl_count = ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >> PAGE_SHIFT;
779
780                         /* printk("bouncing buffer allocated %p original buffer %p\n", bounce_sgl, sgl); */
781                         /* printk("copy_to_bounce_buffer\n"); */
782                         /* FIXME: We can optimize on reads by just skipping this */
783                         copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, scsi_sg_count(scmnd));
784
785                         sgl = cmd_request->bounce_sgl;
786                 }
787
788                 request->DataBuffer.Offset = sgl[0].offset;
789
790                 for (i = 0; i < scsi_sg_count(scmnd); i++ )
791                 {
792                         DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n", i, sgl[i].length, sgl[i].offset);
793                         request->DataBuffer.PfnArray[i] = page_to_pfn(sg_page((&sgl[i])));
794                 }
795         }
796
797         else if (scsi_sglist(scmnd))
798         {
799                 ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
800                 request->DataBuffer.Offset = virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
801                 request->DataBuffer.PfnArray[0] = virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
802         }
803         else
804         {
805                 ASSERT(scsi_bufflen(scmnd) == 0);
806         }
807
808 retry_request:
809
810         /* Invokes the vsc to start an IO */
811         ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj, &cmd_request->request);
812         if (ret == -1) /* no more space */
813         {
814                 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - queue FULL...marking queue busy", scmnd);
815
816                 if (cmd_request->bounce_sgl_count)
817                 {
818                         /* FIXME: We can optimize on writes by just skipping this */
819                         copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
820                         destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
821                 }
822
823                 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
824
825                 scmnd->scsi_done = NULL;
826                 scmnd->host_scribble = NULL;
827
828                 ret = SCSI_MLQUEUE_DEVICE_BUSY;
829         }
830
831         DPRINT_EXIT(STORVSC_DRV);
832
833         return ret;
834 }
835
836 static int storvsc_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, struct bio_vec *bvec)
837 {
838         return bvec->bv_len; /* checking done by caller. */
839 }
840
841 /*++
842
843 Name:   storvsc_device_configure()
844
845 Desc:   Configure the specified scsi device
846
847 --*/
848 static int storvsc_device_alloc(struct scsi_device *sdevice)
849 {
850         DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d", sdevice, BLIST_SPARSELUN);
851         /* This enables luns to be located sparsely. Otherwise, we may not discovered them. */
852         sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
853         return 0;
854 }
855
856 static int storvsc_device_configure(struct scsi_device *sdevice)
857 {
858         DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice, sdevice->queue_depth);
859
860         DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d", sdevice, STORVSC_MAX_IO_REQUESTS);
861         scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, STORVSC_MAX_IO_REQUESTS);
862
863         DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld", sdevice, PAGE_SIZE);
864         blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
865
866         DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", sdevice);
867         blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
868
869         blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
870         /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
871
872         return 0;
873 }
874
875 /*++
876
877 Name:   storvsc_host_reset_handler()
878
879 Desc:   Reset the scsi HBA
880
881 --*/
882 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
883 {
884         int ret=SUCCESS;
885         struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
886         struct device_context *device_ctx = host_device_ctx->device_ctx;
887         struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
888         struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
889
890         struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
891
892         DPRINT_ENTER(STORVSC_DRV);
893
894         DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", scmnd->device, &device_ctx->device_obj);
895
896         /* Invokes the vsc to reset the host/bus */
897         ASSERT(storvsc_drv_obj->OnHostReset);
898         ret = storvsc_drv_obj->OnHostReset(&device_ctx->device_obj);
899         if (ret != 0)
900         {
901                 DPRINT_EXIT(STORVSC_DRV);
902                 return ret;
903         }
904
905         DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", scmnd->device, &device_ctx->device_obj);
906
907         DPRINT_EXIT(STORVSC_DRV);
908
909         return ret;
910 }
911
912 /*++
913
914 Name:   storvsc_host_rescan
915
916 Desc:   Rescan the scsi HBA
917
918 --*/
919 static void storvsc_host_rescan_callback(struct work_struct *work)
920 {
921         struct hv_device *device_obj =
922             &((struct host_device_context*)work)->device_ctx->device_obj;
923         struct device_context* device_ctx = to_device_context(device_obj);
924         struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
925         struct scsi_device *sdev;
926         struct host_device_context *host_device_ctx;
927         struct scsi_device **sdevs_remove_list;
928         unsigned int sdevs_count=0;
929         unsigned int found;
930         unsigned int i;
931         unsigned int lun_count=0;
932         unsigned int *lun_list;
933
934         DPRINT_ENTER(STORVSC_DRV);
935
936         host_device_ctx = (struct host_device_context*)host->hostdata;
937         lun_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(unsigned int), GFP_ATOMIC);
938         if (!lun_list)
939         {
940                 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
941                 return;
942         }
943
944         sdevs_remove_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(void *), GFP_ATOMIC);
945         if (!sdevs_remove_list)
946         {
947                 kfree(lun_list);
948                 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
949                 return;
950         }
951
952         DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...");
953
954         /* Rescan for new device */
955         scsi_scan_target(&host->shost_gendev, host_device_ctx->path, host_device_ctx->target, SCAN_WILD_CARD, 1);
956
957         DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
958
959         /* Use the 1st device to send the report luns cmd */
960         shost_for_each_device(sdev, host)
961         {
962                 lun_count=STORVSC_MAX_LUNS_PER_TARGET;
963                 storvsc_report_luns(sdev, lun_list, &lun_count);
964
965                 DPRINT_INFO(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, lun_count);
966                 DPRINT_INFO(STORVSC_DRV, "existing luns on scsi device (%p) host (%d)", sdev, host->host_no);
967
968                 scsi_device_put(sdev);
969                 break;
970         }
971
972         for (i=0; i<lun_count; i++)
973         {
974                 DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
975         }
976
977         /* Rescan for devices that may have been removed. */
978         /* We do not have to worry that new devices may have been added since */
979         /* this callback is serialized by the workqueue ie add/remove are done here. */
980         shost_for_each_device(sdev, host)
981         {
982                 /* See if this device is still here */
983                 found = 0;
984                 for (i=0; i<lun_count; i++)
985                 {
986                         if (sdev->lun == lun_list[i])
987                         {
988                                 found = 1;
989                                 break;
990                         }
991                 }
992                 if (!found)
993                 {
994                         DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists", sdev->lun);
995                         sdevs_remove_list[sdevs_count++] = sdev;
996                 }
997         }
998
999         /* Now remove the devices */
1000         for (i=0; i< sdevs_count; i++)
1001         {
1002                 DPRINT_INFO(STORVSC_DRV, "removing scsi device (%p) lun (%u)...",
1003                                         sdevs_remove_list[i], sdevs_remove_list[i]->lun);
1004
1005                 /* make sure it is not removed from underneath us */
1006                 if (!scsi_device_get(sdevs_remove_list[i]))
1007                 {
1008                         scsi_remove_device(sdevs_remove_list[i]);
1009                         scsi_device_put(sdevs_remove_list[i]);
1010                 }
1011         }
1012
1013         DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) target (%u) bus (%u)", device_obj, host_device_ctx->target, host_device_ctx->path);
1014
1015         kfree(lun_list);
1016         kfree(sdevs_remove_list);
1017
1018         DPRINT_EXIT(STORVSC_DRV);
1019 }
1020
1021 static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count)
1022 {
1023         int i,j;
1024         unsigned int lun=0;
1025         unsigned int num_luns;
1026         int result;
1027         unsigned char *data;
1028         struct scsi_sense_hdr sshdr;
1029         unsigned char cmd[16]={0};
1030         unsigned int report_len = 8*(STORVSC_MAX_LUNS_PER_TARGET+1); /* Add 1 to cover the report_lun header */
1031         unsigned long long *report_luns;
1032         const unsigned int in_lun_count = *lun_count;
1033
1034         *lun_count = 0;
1035
1036         report_luns = kzalloc(report_len, GFP_ATOMIC);
1037         if (!report_luns)
1038         {
1039                 return -ENOMEM;
1040         }
1041
1042         cmd[0] = REPORT_LUNS;
1043
1044         /* cmd length */
1045         *(unsigned int*)&cmd[6] = cpu_to_be32(report_len);
1046
1047         result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, (unsigned char*)report_luns, report_len, &sshdr, 30*HZ, 3, NULL);
1048         if (result != 0)
1049         {
1050                 kfree(report_luns);
1051                 return -EBUSY;
1052         }
1053
1054         /* get the length from the first four bytes */
1055         report_len = be32_to_cpu(*(unsigned int*)&report_luns[0]);
1056
1057         num_luns = (report_len / sizeof(unsigned long long));
1058         if (num_luns > in_lun_count)
1059         {
1060                 kfree(report_luns);
1061                 return -EINVAL;
1062         }
1063
1064         *lun_count = num_luns;
1065
1066         DPRINT_DBG(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, num_luns);
1067
1068         /* lun id starts at 1 */
1069         for (i=1; i< num_luns+1; i++)
1070         {
1071                 lun = 0;
1072                 data = (unsigned char*)&report_luns[i];
1073                 for (j = 0; j < sizeof(lun); j += 2)
1074                  {
1075                          lun = lun | (((data[j] << 8) | data[j + 1]) << (j * 8));
1076                  }
1077
1078                 luns[i-1] = lun;
1079         }
1080
1081         kfree(report_luns);
1082         return 0;
1083 }
1084
1085 static void storvsc_host_rescan(struct hv_device *device_obj)
1086 {
1087         struct device_context* device_ctx = to_device_context(device_obj);
1088         struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
1089         struct host_device_context *host_device_ctx;
1090
1091         DPRINT_ENTER(STORVSC_DRV);
1092
1093         host_device_ctx = (struct host_device_context*)host->hostdata;
1094
1095         DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) target (%u) bus (%u)...", device_obj, host_device_ctx->target, host_device_ctx->path);
1096
1097         /* We need to queue this since the scanning may block and the caller may be in an intr context */
1098         /* scsi_queue_work(host, &host_device_ctx->host_rescan_work); */
1099         schedule_work(&host_device_ctx->host_rescan_work);
1100         DPRINT_EXIT(STORVSC_DRV);
1101 }
1102
1103 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info)
1104 {
1105         sector_t total_sectors = capacity;
1106         sector_t cylinder_times_heads=0;
1107         sector_t temp=0;
1108
1109         int sectors_per_track=0;
1110         int heads=0;
1111         int cylinders=0;
1112         int rem=0;
1113
1114     if (total_sectors > (65535 * 16 * 255)) {
1115         total_sectors = (65535 * 16 * 255);
1116     }
1117
1118     if (total_sectors >= (65535 * 16 * 63)) {
1119         sectors_per_track = 255;
1120         heads = 16;
1121
1122                 cylinder_times_heads = total_sectors;
1123                 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1124     }
1125         else
1126         {
1127         sectors_per_track = 17;
1128
1129                 cylinder_times_heads = total_sectors;
1130         rem = sector_div(cylinder_times_heads, sectors_per_track);      /* sector_div stores the quotient in cylinder_times_heads */
1131
1132                 temp = cylinder_times_heads + 1023;
1133                 rem = sector_div(temp, 1024);   /* sector_div stores the quotient in temp */
1134
1135                 heads = temp;
1136
1137         if (heads < 4) {
1138             heads = 4;
1139         }
1140
1141         if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1142             sectors_per_track = 31;
1143             heads = 16;
1144
1145                         cylinder_times_heads = total_sectors;
1146             rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1147         }
1148
1149         if (cylinder_times_heads >= (heads * 1024)) {
1150             sectors_per_track = 63;
1151             heads = 16;
1152
1153                         cylinder_times_heads = total_sectors;
1154             rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1155         }
1156     }
1157
1158         temp = cylinder_times_heads;
1159     rem = sector_div(temp, heads); /* sector_div stores the quotient in temp */
1160         cylinders = temp;
1161
1162         info[0] = heads;
1163     info[1] = sectors_per_track;
1164     info[2] = cylinders;
1165
1166         DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
1167
1168     return 0;
1169 }
1170
1171 MODULE_LICENSE("GPL");
1172
1173 static int __init storvsc_init(void)
1174 {
1175         int ret;
1176
1177         DPRINT_ENTER(STORVSC_DRV);
1178
1179         DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
1180
1181         ret = storvsc_drv_init(StorVscInitialize);
1182
1183         DPRINT_EXIT(STORVSC_DRV);
1184
1185         return ret;
1186 }
1187
1188 static void __exit storvsc_exit(void)
1189 {
1190         DPRINT_ENTER(STORVSC_DRV);
1191
1192         storvsc_drv_exit();
1193
1194         DPRINT_ENTER(STORVSC_DRV);
1195 }
1196
1197 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
1198
1199 module_init(storvsc_init);
1200 module_exit(storvsc_exit);
1201
1202 /* eof */