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