Merge branch 'enable-devices' into omap-for-v4.5/fixes
[linux-2.6-microblaze.git] / drivers / staging / unisys / visorhba / visorhba_main.c
1 /* Copyright (c) 2012 - 2015 UNISYS CORPORATION
2  * All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or (at
7  * your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12  * NON INFRINGEMENT.  See the GNU General Public License for more
13  * details.
14  */
15
16 #include <linux/debugfs.h>
17 #include <linux/skbuff.h>
18 #include <linux/kthread.h>
19 #include <scsi/scsi.h>
20 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_cmnd.h>
22 #include <scsi/scsi_device.h>
23
24 #include "visorbus.h"
25 #include "iochannel.h"
26
27 /* The Send and Receive Buffers of the IO Queue may both be full */
28
29 #define IOS_ERROR_THRESHOLD     1000
30 /* MAX_BUF = 6 lines x 10 MAXVHBA x 80 characters
31  *         = 4800 bytes ~ 2^13 = 8192 bytes
32  */
33 #define MAX_BUF                 8192
34 #define MAX_PENDING_REQUESTS    (MIN_NUMSIGNALS * 2)
35 #define VISORHBA_ERROR_COUNT    30
36 #define VISORHBA_OPEN_MAX       1
37
38 static int visorhba_queue_command_lck(struct scsi_cmnd *scsicmd,
39                                       void (*visorhba_cmnd_done)
40                                             (struct scsi_cmnd *));
41 #ifdef DEF_SCSI_QCMD
42 static DEF_SCSI_QCMD(visorhba_queue_command)
43 #else
44 #define visorhba_queue_command visorhba_queue_command_lck
45 #endif
46 static int visorhba_probe(struct visor_device *dev);
47 static void visorhba_remove(struct visor_device *dev);
48 static int visorhba_pause(struct visor_device *dev,
49                           visorbus_state_complete_func complete_func);
50 static int visorhba_resume(struct visor_device *dev,
51                            visorbus_state_complete_func complete_func);
52
53 static ssize_t info_debugfs_read(struct file *file, char __user *buf,
54                                  size_t len, loff_t *offset);
55 static struct dentry *visorhba_debugfs_dir;
56 static const struct file_operations debugfs_info_fops = {
57         .read = info_debugfs_read,
58 };
59
60 /* GUIDS for HBA channel type supported by this driver */
61 static struct visor_channeltype_descriptor visorhba_channel_types[] = {
62         /* Note that the only channel type we expect to be reported by the
63          * bus driver is the SPAR_VHBA channel.
64          */
65         { SPAR_VHBA_CHANNEL_PROTOCOL_UUID, "sparvhba" },
66         { NULL_UUID_LE, NULL }
67 };
68
69 /* This is used to tell the visor bus driver which types of visor devices
70  * we support, and what functions to call when a visor device that we support
71  * is attached or removed.
72  */
73 static struct visor_driver visorhba_driver = {
74         .name = "visorhba",
75         .owner = THIS_MODULE,
76         .channel_types = visorhba_channel_types,
77         .probe = visorhba_probe,
78         .remove = visorhba_remove,
79         .pause = visorhba_pause,
80         .resume = visorhba_resume,
81         .channel_interrupt = NULL,
82 };
83 MODULE_DEVICE_TABLE(visorbus, visorhba_channel_types);
84 MODULE_ALIAS("visorbus:" SPAR_VHBA_CHANNEL_PROTOCOL_UUID_STR);
85
86 struct visor_thread_info {
87         struct task_struct *task;
88         struct completion has_stopped;
89         int id;
90 };
91
92 struct visordisk_info {
93         u32 valid;
94         u32 channel, id, lun;   /* Disk Path */
95         atomic_t ios_threshold;
96         atomic_t error_count;
97         struct visordisk_info *next;
98 };
99
100 struct scsipending {
101         struct uiscmdrsp cmdrsp;
102         void *sent;             /* The Data being tracked */
103         char cmdtype;           /* Type of pointer that is being stored */
104 };
105
106 /* Work Data for dar_work_queue */
107 struct diskaddremove {
108         u8 add;                 /* 0-remove, 1-add */
109         struct Scsi_Host *shost; /* Scsi Host for this visorhba instance */
110         u32 channel, id, lun;   /* Disk Path */
111         struct diskaddremove *next;
112 };
113
114 /* Each scsi_host has a host_data area that contains this struct. */
115 struct visorhba_devdata {
116         struct Scsi_Host *scsihost;
117         struct visor_device *dev;
118         struct list_head dev_info_list;
119         /* Tracks the requests that have been forwarded to
120          * the IOVM and haven't returned yet
121          */
122         struct scsipending pending[MAX_PENDING_REQUESTS];
123         /* Start search for next pending free slot here */
124         unsigned int nextinsert;
125         spinlock_t privlock; /* lock to protect data in devdata */
126         bool serverdown;
127         bool serverchangingstate;
128         unsigned long long acquire_failed_cnt;
129         unsigned long long interrupts_rcvd;
130         unsigned long long interrupts_notme;
131         unsigned long long interrupts_disabled;
132         u64 __iomem *flags_addr;
133         atomic_t interrupt_rcvd;
134         wait_queue_head_t rsp_queue;
135         struct visordisk_info head;
136         unsigned int max_buff_len;
137         int devnum;
138         struct visor_thread_info threadinfo;
139         int thread_wait_ms;
140 };
141
142 struct visorhba_devices_open {
143         struct visorhba_devdata *devdata;
144 };
145
146 static struct visorhba_devices_open visorhbas_open[VISORHBA_OPEN_MAX];
147
148 #define for_each_vdisk_match(iter, list, match)                   \
149         for (iter = &list->head; iter->next; iter = iter->next) \
150                 if ((iter->channel == match->channel) &&                  \
151                     (iter->id == match->id) &&                    \
152                     (iter->lun == match->lun))
153 /**
154  *      visor_thread_start - starts a thread for the device
155  *      @thrinfo: The thread to start
156  *      @threadfn: Function the thread starts
157  *      @thrcontext: Context to pass to the thread, i.e. devdata
158  *      @name: string describing name of thread
159  *
160  *      Starts a thread for the device.
161  *
162  *      Return 0 on success;
163  */
164 static int visor_thread_start(struct visor_thread_info *thrinfo,
165                               int (*threadfn)(void *),
166                               void *thrcontext, char *name)
167 {
168         /* used to stop the thread */
169         init_completion(&thrinfo->has_stopped);
170         thrinfo->task = kthread_run(threadfn, thrcontext, name);
171         if (IS_ERR(thrinfo->task)) {
172                 thrinfo->id = 0;
173                 return PTR_ERR(thrinfo->task);
174         }
175         thrinfo->id = thrinfo->task->pid;
176         return 0;
177 }
178
179 /**
180  *      add_scsipending_entry - save off io command that is pending in
181  *                              Service Partition
182  *      @devdata: Pointer to devdata
183  *      @cmdtype: Specifies the type of command pending
184  *      @new:   The command to be saved
185  *
186  *      Saves off the io command that is being handled by the Service
187  *      Partition so that it can be handled when it completes. If new is
188  *      NULL it is assumed the entry refers only to the cmdrsp.
189  *      Returns insert_location where entry was added,
190  *      SCSI_MLQUEUE_DEVICE_BUSY if it can't
191  */
192 static int add_scsipending_entry(struct visorhba_devdata *devdata,
193                                  char cmdtype, void *new)
194 {
195         unsigned long flags;
196         struct scsipending *entry;
197         int insert_location;
198
199         spin_lock_irqsave(&devdata->privlock, flags);
200         insert_location = devdata->nextinsert;
201         while (devdata->pending[insert_location].sent) {
202                 insert_location = (insert_location + 1) % MAX_PENDING_REQUESTS;
203                 if (insert_location == (int)devdata->nextinsert) {
204                         spin_unlock_irqrestore(&devdata->privlock, flags);
205                         return -1;
206                 }
207         }
208
209         entry = &devdata->pending[insert_location];
210         memset(&entry->cmdrsp, 0, sizeof(entry->cmdrsp));
211         entry->cmdtype = cmdtype;
212         if (new)
213                 entry->sent = new;
214         else /* wants to send cmdrsp */
215                 entry->sent = &entry->cmdrsp;
216         devdata->nextinsert = (insert_location + 1) % MAX_PENDING_REQUESTS;
217         spin_unlock_irqrestore(&devdata->privlock, flags);
218
219         return insert_location;
220 }
221
222 /**
223  *      del_scsipending_enty - removes an entry from the pending array
224  *      @devdata: Device holding the pending array
225  *      @del: Entry to remove
226  *
227  *      Removes the entry pointed at by del and returns it.
228  *      Returns the scsipending entry pointed at
229  */
230 static void *del_scsipending_ent(struct visorhba_devdata *devdata,
231                                  int del)
232 {
233         unsigned long flags;
234         void *sent = NULL;
235
236         if (del < MAX_PENDING_REQUESTS) {
237                 spin_lock_irqsave(&devdata->privlock, flags);
238                 sent = devdata->pending[del].sent;
239
240                 devdata->pending[del].cmdtype = 0;
241                 devdata->pending[del].sent = NULL;
242                 spin_unlock_irqrestore(&devdata->privlock, flags);
243         }
244
245         return sent;
246 }
247
248 /**
249  *      get_scsipending_cmdrsp - return the cmdrsp stored in a pending entry
250  *      #ddata: Device holding the pending array
251  *      @ent: Entry that stores the cmdrsp
252  *
253  *      Each scsipending entry has a cmdrsp in it. The cmdrsp is only valid
254  *      if the "sent" field is not NULL
255  *      Returns a pointer to the cmdrsp.
256  */
257 static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata,
258                                                 int ent)
259 {
260         if (ddata->pending[ent].sent)
261                 return &ddata->pending[ent].cmdrsp;
262
263         return NULL;
264 }
265
266 /**
267  *      forward_taskmgmt_command - send taskmegmt command to the Service
268  *                                 Partition
269  *      @tasktype: Type of taskmgmt command
270  *      @scsidev: Scsidev that issued command
271  *
272  *      Create a cmdrsp packet and send it to the Serivce Partition
273  *      that will service this request.
274  *      Returns whether the command was queued successfully or not.
275  */
276 static int forward_taskmgmt_command(enum task_mgmt_types tasktype,
277                                     struct scsi_cmnd *scsicmd)
278 {
279         struct uiscmdrsp *cmdrsp;
280         struct scsi_device *scsidev = scsicmd->device;
281         struct visorhba_devdata *devdata =
282                 (struct visorhba_devdata *)scsidev->host->hostdata;
283         int notifyresult = 0xffff;
284         wait_queue_head_t notifyevent;
285         int scsicmd_id = 0;
286
287         if (devdata->serverdown || devdata->serverchangingstate)
288                 return FAILED;
289
290         scsicmd_id = add_scsipending_entry(devdata, CMD_SCSITASKMGMT_TYPE,
291                                            NULL);
292         if (scsicmd_id < 0)
293                 return FAILED;
294
295         cmdrsp = get_scsipending_cmdrsp(devdata, scsicmd_id);
296
297         init_waitqueue_head(&notifyevent);
298
299         /* issue TASK_MGMT_ABORT_TASK */
300         cmdrsp->cmdtype = CMD_SCSITASKMGMT_TYPE;
301         /* specify the event that has to be triggered when this */
302         /* cmd is complete */
303         cmdrsp->scsitaskmgmt.notify_handle = (u64)&notifyevent;
304         cmdrsp->scsitaskmgmt.notifyresult_handle = (u64)&notifyresult;
305
306         /* save destination */
307         cmdrsp->scsitaskmgmt.tasktype = tasktype;
308         cmdrsp->scsitaskmgmt.vdest.channel = scsidev->channel;
309         cmdrsp->scsitaskmgmt.vdest.id = scsidev->id;
310         cmdrsp->scsitaskmgmt.vdest.lun = scsidev->lun;
311         cmdrsp->scsitaskmgmt.handle = scsicmd_id;
312
313         if (!visorchannel_signalinsert(devdata->dev->visorchannel,
314                                        IOCHAN_TO_IOPART,
315                                        cmdrsp))
316                 goto err_del_scsipending_ent;
317
318         /* It can take the Service Partition up to 35 seconds to complete
319          * an IO in some cases, so wait 45 seconds and error out
320          */
321         if (!wait_event_timeout(notifyevent, notifyresult != 0xffff,
322                                 msecs_to_jiffies(45000)))
323                 goto err_del_scsipending_ent;
324
325         if (tasktype == TASK_MGMT_ABORT_TASK)
326                 scsicmd->result = (DID_ABORT << 16);
327         else
328                 scsicmd->result = (DID_RESET << 16);
329
330         scsicmd->scsi_done(scsicmd);
331
332         return SUCCESS;
333
334 err_del_scsipending_ent:
335         del_scsipending_ent(devdata, scsicmd_id);
336         return FAILED;
337 }
338
339 /**
340  *      visorhba_abort_handler - Send TASK_MGMT_ABORT_TASK
341  *      @scsicmd: The scsicmd that needs aborted
342  *
343  *      Returns SUCCESS if inserted, failure otherwise
344  *
345  */
346 static int visorhba_abort_handler(struct scsi_cmnd *scsicmd)
347 {
348         /* issue TASK_MGMT_ABORT_TASK */
349         struct scsi_device *scsidev;
350         struct visordisk_info *vdisk;
351         struct visorhba_devdata *devdata;
352
353         scsidev = scsicmd->device;
354         devdata = (struct visorhba_devdata *)scsidev->host->hostdata;
355         for_each_vdisk_match(vdisk, devdata, scsidev) {
356                 if (atomic_read(&vdisk->error_count) < VISORHBA_ERROR_COUNT)
357                         atomic_inc(&vdisk->error_count);
358                 else
359                         atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
360         }
361         return forward_taskmgmt_command(TASK_MGMT_ABORT_TASK, scsicmd);
362 }
363
364 /**
365  *      visorhba_device_reset_handler - Send TASK_MGMT_LUN_RESET
366  *      @scsicmd: The scsicmd that needs aborted
367  *
368  *      Returns SUCCESS if inserted, failure otherwise
369  */
370 static int visorhba_device_reset_handler(struct scsi_cmnd *scsicmd)
371 {
372         /* issue TASK_MGMT_LUN_RESET */
373         struct scsi_device *scsidev;
374         struct visordisk_info *vdisk;
375         struct visorhba_devdata *devdata;
376
377         scsidev = scsicmd->device;
378         devdata = (struct visorhba_devdata *)scsidev->host->hostdata;
379         for_each_vdisk_match(vdisk, devdata, scsidev) {
380                 if (atomic_read(&vdisk->error_count) < VISORHBA_ERROR_COUNT)
381                         atomic_inc(&vdisk->error_count);
382                 else
383                         atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
384         }
385         return forward_taskmgmt_command(TASK_MGMT_LUN_RESET, scsicmd);
386 }
387
388 /**
389  *      visorhba_bus_reset_handler - Send TASK_MGMT_TARGET_RESET for each
390  *                                   target on the bus
391  *      @scsicmd: The scsicmd that needs aborted
392  *
393  *      Returns SUCCESS
394  */
395 static int visorhba_bus_reset_handler(struct scsi_cmnd *scsicmd)
396 {
397         struct scsi_device *scsidev;
398         struct visordisk_info *vdisk;
399         struct visorhba_devdata *devdata;
400
401         scsidev = scsicmd->device;
402         devdata = (struct visorhba_devdata *)scsidev->host->hostdata;
403         for_each_vdisk_match(vdisk, devdata, scsidev) {
404                 if (atomic_read(&vdisk->error_count) < VISORHBA_ERROR_COUNT)
405                         atomic_inc(&vdisk->error_count);
406                 else
407                         atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
408         }
409         return forward_taskmgmt_command(TASK_MGMT_BUS_RESET, scsicmd);
410 }
411
412 /**
413  *      visorhba_host_reset_handler - Not supported
414  *      @scsicmd: The scsicmd that needs aborted
415  *
416  *      Not supported, return SUCCESS
417  *      Returns SUCCESS
418  */
419 static int
420 visorhba_host_reset_handler(struct scsi_cmnd *scsicmd)
421 {
422         /* issue TASK_MGMT_TARGET_RESET for each target on each bus for host */
423         return SUCCESS;
424 }
425
426 /**
427  *      visorhba_get_info
428  *      @shp: Scsi host that is requesting information
429  *
430  *      Returns string with info
431  */
432 static const char *visorhba_get_info(struct Scsi_Host *shp)
433 {
434         /* Return version string */
435         return "visorhba";
436 }
437
438 /**
439  *      visorhba_queue_command_lck -- queues command to the Service Partition
440  *      @scsicmd: Command to be queued
441  *      @vsiorhba_cmnd_done: Done command to call when scsicmd is returned
442  *
443  *      Queues to scsicmd to the ServicePartition after converting it to a
444  *      uiscmdrsp structure.
445  *
446  *      Returns success if queued to the Service Partition, otherwise
447  *      failure.
448  */
449 static int
450 visorhba_queue_command_lck(struct scsi_cmnd *scsicmd,
451                            void (*visorhba_cmnd_done)(struct scsi_cmnd *))
452 {
453         struct uiscmdrsp *cmdrsp;
454         struct scsi_device *scsidev = scsicmd->device;
455         int insert_location;
456         unsigned char *cdb = scsicmd->cmnd;
457         struct Scsi_Host *scsihost = scsidev->host;
458         unsigned int i;
459         struct visorhba_devdata *devdata =
460                 (struct visorhba_devdata *)scsihost->hostdata;
461         struct scatterlist *sg = NULL;
462         struct scatterlist *sglist = NULL;
463
464         if (devdata->serverdown || devdata->serverchangingstate)
465                 return SCSI_MLQUEUE_DEVICE_BUSY;
466
467         insert_location = add_scsipending_entry(devdata, CMD_SCSI_TYPE,
468                                                 (void *)scsicmd);
469
470         if (insert_location < 0)
471                 return SCSI_MLQUEUE_DEVICE_BUSY;
472
473         cmdrsp = get_scsipending_cmdrsp(devdata, insert_location);
474
475         cmdrsp->cmdtype = CMD_SCSI_TYPE;
476         /* save the pending insertion location. Deletion from pending
477          * will return the scsicmd pointer for completion
478          */
479         cmdrsp->scsi.handle = insert_location;
480
481         /* save done function that we have call when cmd is complete */
482         scsicmd->scsi_done = visorhba_cmnd_done;
483         /* save destination */
484         cmdrsp->scsi.vdest.channel = scsidev->channel;
485         cmdrsp->scsi.vdest.id = scsidev->id;
486         cmdrsp->scsi.vdest.lun = scsidev->lun;
487         /* save datadir */
488         cmdrsp->scsi.data_dir = scsicmd->sc_data_direction;
489         memcpy(cmdrsp->scsi.cmnd, cdb, MAX_CMND_SIZE);
490
491         cmdrsp->scsi.bufflen = scsi_bufflen(scsicmd);
492
493         /* keep track of the max buffer length so far. */
494         if (cmdrsp->scsi.bufflen > devdata->max_buff_len)
495                 devdata->max_buff_len = cmdrsp->scsi.bufflen;
496
497         if (scsi_sg_count(scsicmd) > MAX_PHYS_INFO)
498                 goto err_del_scsipending_ent;
499
500         /* convert buffer to phys information  */
501         /* buffer is scatterlist - copy it out */
502         sglist = scsi_sglist(scsicmd);
503
504         for_each_sg(sglist, sg, scsi_sg_count(scsicmd), i) {
505                 cmdrsp->scsi.gpi_list[i].address = sg_phys(sg);
506                 cmdrsp->scsi.gpi_list[i].length = sg->length;
507         }
508         cmdrsp->scsi.guest_phys_entries = scsi_sg_count(scsicmd);
509
510         if (!visorchannel_signalinsert(devdata->dev->visorchannel,
511                                        IOCHAN_TO_IOPART,
512                                        cmdrsp))
513                 /* queue must be full and we aren't going to wait */
514                 goto err_del_scsipending_ent;
515
516         return 0;
517
518 err_del_scsipending_ent:
519         del_scsipending_ent(devdata, insert_location);
520         return SCSI_MLQUEUE_DEVICE_BUSY;
521 }
522
523 /**
524  *      visorhba_slave_alloc - called when new disk is discovered
525  *      @scsidev: New disk
526  *
527  *      Create a new visordisk_info structure and add it to our
528  *      list of vdisks.
529  *
530  *      Returns success when created, otherwise error.
531  */
532 static int visorhba_slave_alloc(struct scsi_device *scsidev)
533 {
534         /* this is called by the midlayer before scan for new devices --
535          * LLD can alloc any struct & do init if needed.
536          */
537         struct visordisk_info *vdisk;
538         struct visordisk_info *tmpvdisk;
539         struct visorhba_devdata *devdata;
540         struct Scsi_Host *scsihost = (struct Scsi_Host *)scsidev->host;
541
542         devdata = (struct visorhba_devdata *)scsihost->hostdata;
543         if (!devdata)
544                 return 0; /* even though we errored, treat as success */
545
546         for_each_vdisk_match(vdisk, devdata, scsidev)
547                 return 0; /* already allocated return success */
548
549         tmpvdisk = kzalloc(sizeof(*tmpvdisk), GFP_ATOMIC);
550         if (!tmpvdisk)
551                 return -ENOMEM;
552
553         tmpvdisk->channel = scsidev->channel;
554         tmpvdisk->id = scsidev->id;
555         tmpvdisk->lun = scsidev->lun;
556         vdisk->next = tmpvdisk;
557         return 0;
558 }
559
560 /**
561  *      visorhba_slave_destroy - disk is going away
562  *      @scsidev: scsi device going away
563  *
564  *      Disk is going away, clean up resources.
565  *      Returns void.
566  */
567 static void visorhba_slave_destroy(struct scsi_device *scsidev)
568 {
569         /* midlevel calls this after device has been quiesced and
570          * before it is to be deleted.
571          */
572         struct visordisk_info *vdisk, *delvdisk;
573         struct visorhba_devdata *devdata;
574         struct Scsi_Host *scsihost = (struct Scsi_Host *)scsidev->host;
575
576         devdata = (struct visorhba_devdata *)scsihost->hostdata;
577         for_each_vdisk_match(vdisk, devdata, scsidev) {
578                 delvdisk = vdisk->next;
579                 vdisk->next = delvdisk->next;
580                 kfree(delvdisk);
581                 return;
582         }
583 }
584
585 static struct scsi_host_template visorhba_driver_template = {
586         .name = "Unisys Visor HBA",
587         .info = visorhba_get_info,
588         .queuecommand = visorhba_queue_command,
589         .eh_abort_handler = visorhba_abort_handler,
590         .eh_device_reset_handler = visorhba_device_reset_handler,
591         .eh_bus_reset_handler = visorhba_bus_reset_handler,
592         .eh_host_reset_handler = visorhba_host_reset_handler,
593         .shost_attrs = NULL,
594 #define visorhba_MAX_CMNDS 128
595         .can_queue = visorhba_MAX_CMNDS,
596         .sg_tablesize = 64,
597         .this_id = -1,
598         .slave_alloc = visorhba_slave_alloc,
599         .slave_destroy = visorhba_slave_destroy,
600         .use_clustering = ENABLE_CLUSTERING,
601 };
602
603 /**
604  *      info_debugfs_read - debugfs interface to dump visorhba states
605  *      @file: Debug file
606  *      @buf: buffer to send back to user
607  *      @len: len that can be written to buf
608  *      @offset: offset into buf
609  *
610  *      Dumps information about the visorhba driver and devices
611  *      TODO: Make this per vhba
612  *      Returns bytes_read
613  */
614 static ssize_t info_debugfs_read(struct file *file, char __user *buf,
615                                  size_t len, loff_t *offset)
616 {
617         ssize_t bytes_read = 0;
618         int str_pos = 0;
619         u64 phys_flags_addr;
620         int i;
621         struct visorhba_devdata *devdata;
622         char *vbuf;
623
624         if (len > MAX_BUF)
625                 len = MAX_BUF;
626         vbuf = kzalloc(len, GFP_KERNEL);
627         if (!vbuf)
628                 return -ENOMEM;
629
630         for (i = 0; i < VISORHBA_OPEN_MAX; i++) {
631                 if (!visorhbas_open[i].devdata)
632                         continue;
633
634                 devdata = visorhbas_open[i].devdata;
635
636                 str_pos += scnprintf(vbuf + str_pos,
637                                 len - str_pos, "max_buff_len:%u\n",
638                                 devdata->max_buff_len);
639
640                 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
641                                 "\ninterrupts_rcvd = %llu, interrupts_disabled = %llu\n",
642                                 devdata->interrupts_rcvd,
643                                 devdata->interrupts_disabled);
644                 str_pos += scnprintf(vbuf + str_pos,
645                                 len - str_pos, "\ninterrupts_notme = %llu,\n",
646                                 devdata->interrupts_notme);
647                 phys_flags_addr = virt_to_phys((__force  void *)
648                                                devdata->flags_addr);
649                 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
650                                 "flags_addr = %p, phys_flags_addr=0x%016llx, FeatureFlags=%llu\n",
651                                 devdata->flags_addr, phys_flags_addr,
652                                 (__le64)readq(devdata->flags_addr));
653                 str_pos += scnprintf(vbuf + str_pos,
654                         len - str_pos, "acquire_failed_cnt:%llu\n",
655                         devdata->acquire_failed_cnt);
656                 str_pos += scnprintf(vbuf + str_pos, len - str_pos, "\n");
657         }
658
659         bytes_read = simple_read_from_buffer(buf, len, offset, vbuf, str_pos);
660         kfree(vbuf);
661         return bytes_read;
662 }
663
664 /**
665  *      visorhba_serverdown_complete - Called when we are done cleaning up
666  *                                     from serverdown
667  *      @work: work structure for this serverdown request
668  *
669  *      Called when we are done cleanning up from serverdown, stop processing
670  *      queue, fail pending IOs.
671  *      Returns void when finished cleaning up
672  */
673 static void visorhba_serverdown_complete(struct visorhba_devdata *devdata)
674 {
675         int i;
676         struct scsipending *pendingdel = NULL;
677         struct scsi_cmnd *scsicmd = NULL;
678         struct uiscmdrsp *cmdrsp;
679         unsigned long flags;
680
681         /* Stop using the IOVM response queue (queue should be drained
682          * by the end)
683          */
684         kthread_stop(devdata->threadinfo.task);
685
686         /* Fail commands that weren't completed */
687         spin_lock_irqsave(&devdata->privlock, flags);
688         for (i = 0; i < MAX_PENDING_REQUESTS; i++) {
689                 pendingdel = &devdata->pending[i];
690                 switch (pendingdel->cmdtype) {
691                 case CMD_SCSI_TYPE:
692                         scsicmd = pendingdel->sent;
693                         scsicmd->result = DID_RESET << 16;
694                         if (scsicmd->scsi_done)
695                                 scsicmd->scsi_done(scsicmd);
696                         break;
697                 case CMD_SCSITASKMGMT_TYPE:
698                         cmdrsp = pendingdel->sent;
699                         cmdrsp->scsitaskmgmt.notifyresult_handle
700                                                         = TASK_MGMT_FAILED;
701                         wake_up_all((wait_queue_head_t *)
702                                     cmdrsp->scsitaskmgmt.notify_handle);
703                         break;
704                 case CMD_VDISKMGMT_TYPE:
705                         cmdrsp = pendingdel->sent;
706                         cmdrsp->vdiskmgmt.notifyresult_handle
707                                                         = VDISK_MGMT_FAILED;
708                         wake_up_all((wait_queue_head_t *)
709                                     cmdrsp->vdiskmgmt.notify_handle);
710                         break;
711                 default:
712                         break;
713                 }
714                 pendingdel->cmdtype = 0;
715                 pendingdel->sent = NULL;
716         }
717         spin_unlock_irqrestore(&devdata->privlock, flags);
718
719         devdata->serverdown = true;
720         devdata->serverchangingstate = false;
721 }
722
723 /**
724  *      visorhba_serverdown - Got notified that the IOVM is down
725  *      @devdata: visorhba that is being serviced by downed IOVM.
726  *
727  *      Something happened to the IOVM, return immediately and
728  *      schedule work cleanup work.
729  *      Return SUCCESS or EINVAL
730  */
731 static int visorhba_serverdown(struct visorhba_devdata *devdata)
732 {
733         if (!devdata->serverdown && !devdata->serverchangingstate) {
734                 devdata->serverchangingstate = true;
735                 visorhba_serverdown_complete(devdata);
736         } else if (devdata->serverchangingstate) {
737                 return -EINVAL;
738         }
739         return 0;
740 }
741
742 /**
743  *      do_scsi_linuxstat - scsi command returned linuxstat
744  *      @cmdrsp: response from IOVM
745  *      @scsicmd: Command issued.
746  *
747  *      Don't log errors for disk-not-present inquiries
748  *      Returns void
749  */
750 static void
751 do_scsi_linuxstat(struct uiscmdrsp *cmdrsp, struct scsi_cmnd *scsicmd)
752 {
753         struct visorhba_devdata *devdata;
754         struct visordisk_info *vdisk;
755         struct scsi_device *scsidev;
756
757         scsidev = scsicmd->device;
758         memcpy(scsicmd->sense_buffer, cmdrsp->scsi.sensebuf, MAX_SENSE_SIZE);
759
760         /* Do not log errors for disk-not-present inquiries */
761         if ((cmdrsp->scsi.cmnd[0] == INQUIRY) &&
762             (host_byte(cmdrsp->scsi.linuxstat) == DID_NO_CONNECT) &&
763             (cmdrsp->scsi.addlstat == ADDL_SEL_TIMEOUT))
764                 return;
765         /* Okay see what our error_count is here.... */
766         devdata = (struct visorhba_devdata *)scsidev->host->hostdata;
767         for_each_vdisk_match(vdisk, devdata, scsidev) {
768                 if (atomic_read(&vdisk->error_count) < VISORHBA_ERROR_COUNT) {
769                         atomic_inc(&vdisk->error_count);
770                         atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
771                 }
772         }
773 }
774
775 /**
776  *      do_scsi_nolinuxstat - scsi command didn't have linuxstat
777  *      @cmdrsp: response from IOVM
778  *      @scsicmd: Command issued.
779  *
780  *      Handle response when no linuxstat was returned
781  *      Returns void
782  */
783 static void
784 do_scsi_nolinuxstat(struct uiscmdrsp *cmdrsp, struct scsi_cmnd *scsicmd)
785 {
786         struct scsi_device *scsidev;
787         unsigned char buf[36];
788         struct scatterlist *sg;
789         unsigned int i;
790         char *this_page;
791         char *this_page_orig;
792         int bufind = 0;
793         struct visordisk_info *vdisk;
794         struct visorhba_devdata *devdata;
795
796         scsidev = scsicmd->device;
797         if ((cmdrsp->scsi.cmnd[0] == INQUIRY) &&
798             (cmdrsp->scsi.bufflen >= MIN_INQUIRY_RESULT_LEN)) {
799                 if (cmdrsp->scsi.no_disk_result == 0)
800                         return;
801
802                 /* Linux scsi code wants a device at Lun 0
803                  * to issue report luns, but we don't want
804                  * a disk there so we'll present a processor
805                  * there.
806                  */
807                 SET_NO_DISK_INQUIRY_RESULT(buf, cmdrsp->scsi.bufflen,
808                                            scsidev->lun,
809                                            DEV_DISK_CAPABLE_NOT_PRESENT,
810                                            DEV_NOT_CAPABLE);
811
812                 if (scsi_sg_count(scsicmd) == 0) {
813                         memcpy(scsi_sglist(scsicmd), buf,
814                                cmdrsp->scsi.bufflen);
815                         return;
816                 }
817
818                 sg = scsi_sglist(scsicmd);
819                 for (i = 0; i < scsi_sg_count(scsicmd); i++) {
820                         this_page_orig = kmap_atomic(sg_page(sg + i));
821                         this_page = (void *)((unsigned long)this_page_orig |
822                                              sg[i].offset);
823                         memcpy(this_page, buf + bufind, sg[i].length);
824                         kunmap_atomic(this_page_orig);
825                 }
826         } else {
827                 devdata = (struct visorhba_devdata *)scsidev->host->hostdata;
828                 for_each_vdisk_match(vdisk, devdata, scsidev) {
829                         if (atomic_read(&vdisk->ios_threshold) > 0) {
830                                 atomic_dec(&vdisk->ios_threshold);
831                                 if (atomic_read(&vdisk->ios_threshold) == 0)
832                                         atomic_set(&vdisk->error_count, 0);
833                         }
834                 }
835         }
836 }
837
838 /**
839  *      complete_scsi_command - complete a scsi command
840  *      @uiscmdrsp: Response from Service Partition
841  *      @scsicmd: The scsi command
842  *
843  *      Response returned by the Service Partition, finish it and send
844  *      completion to the scsi midlayer.
845  *      Returns void.
846  */
847 static void
848 complete_scsi_command(struct uiscmdrsp *cmdrsp, struct scsi_cmnd *scsicmd)
849 {
850         /* take what we need out of cmdrsp and complete the scsicmd */
851         scsicmd->result = cmdrsp->scsi.linuxstat;
852         if (cmdrsp->scsi.linuxstat)
853                 do_scsi_linuxstat(cmdrsp, scsicmd);
854         else
855                 do_scsi_nolinuxstat(cmdrsp, scsicmd);
856
857         scsicmd->scsi_done(scsicmd);
858 }
859
860 /* DELETE VDISK TASK MGMT COMMANDS */
861 static inline void complete_vdiskmgmt_command(struct uiscmdrsp *cmdrsp)
862 {
863         /* copy the result of the taskmgmt and
864          * wake up the error handler that is waiting for this
865          */
866         cmdrsp->vdiskmgmt.notifyresult_handle = cmdrsp->vdiskmgmt.result;
867         wake_up_all((wait_queue_head_t *)cmdrsp->vdiskmgmt.notify_handle);
868 }
869
870 /**
871  *      complete_taskmgmt_command - complete task management
872  *      @cmdrsp: Response from the IOVM
873  *
874  *      Service Partition returned the result of the task management
875  *      command. Wake up anyone waiting for it.
876  *      Returns void
877  */
878 static inline void complete_taskmgmt_command(struct uiscmdrsp *cmdrsp)
879 {
880         /* copy the result of the taskgmgt and
881          * wake up the error handler that is waiting for this
882          */
883         cmdrsp->vdiskmgmt.notifyresult_handle = cmdrsp->vdiskmgmt.result;
884         wake_up_all((wait_queue_head_t *)cmdrsp->scsitaskmgmt.notify_handle);
885 }
886
887 static struct work_struct dar_work_queue;
888 static struct diskaddremove *dar_work_queue_head;
889 static spinlock_t dar_work_queue_lock; /* Lock to protet dar_work_queue_head */
890 static unsigned short dar_work_queue_sched;
891
892 /**
893  *      queue_disk_add_remove - IOSP has sent us a add/remove request
894  *      @dar: disk add/remove request
895  *
896  *      Queue the work needed to add/remove a disk.
897  *      Returns void
898  */
899 static inline void queue_disk_add_remove(struct diskaddremove *dar)
900 {
901         unsigned long flags;
902
903         spin_lock_irqsave(&dar_work_queue_lock, flags);
904         if (!dar_work_queue_head) {
905                 dar_work_queue_head = dar;
906                 dar->next = NULL;
907         } else {
908                 dar->next = dar_work_queue_head;
909                 dar_work_queue_head = dar;
910         }
911         if (!dar_work_queue_sched) {
912                 schedule_work(&dar_work_queue);
913                 dar_work_queue_sched = 1;
914         }
915         spin_unlock_irqrestore(&dar_work_queue_lock, flags);
916 }
917
918 /**
919  *      process_disk_notify - IOSP has sent a process disk notify event
920  *      @shost: Scsi hot
921  *      @cmdrsp: Response from the IOSP
922  *
923  *      Queue it to the work queue.
924  *      Return void.
925  */
926 static void process_disk_notify(struct Scsi_Host *shost,
927                                 struct uiscmdrsp *cmdrsp)
928 {
929         struct diskaddremove *dar;
930
931         dar = kzalloc(sizeof(*dar), GFP_ATOMIC);
932         if (dar) {
933                 dar->add = cmdrsp->disknotify.add;
934                 dar->shost = shost;
935                 dar->channel = cmdrsp->disknotify.channel;
936                 dar->id = cmdrsp->disknotify.id;
937                 dar->lun = cmdrsp->disknotify.lun;
938                 queue_disk_add_remove(dar);
939         }
940 }
941
942 /**
943  *      drain_queue - pull responses out of iochannel
944  *      @cmdrsp: Response from the IOSP
945  *      @devdata: device that owns this iochannel
946  *
947  *      Pulls responses out of the iochannel and process the responses.
948  *      Restuns void
949  */
950 static void
951 drain_queue(struct uiscmdrsp *cmdrsp, struct visorhba_devdata *devdata)
952 {
953         struct scsi_cmnd *scsicmd;
954         struct Scsi_Host *shost = devdata->scsihost;
955
956         while (1) {
957                 if (!visorchannel_signalremove(devdata->dev->visorchannel,
958                                                IOCHAN_FROM_IOPART,
959                                                cmdrsp))
960                         break; /* queue empty */
961
962                 if (cmdrsp->cmdtype == CMD_SCSI_TYPE) {
963                         /* scsicmd location is returned by the
964                          * deletion
965                          */
966                         scsicmd = del_scsipending_ent(devdata,
967                                                       cmdrsp->scsi.handle);
968                         if (!scsicmd)
969                                 break;
970                         /* complete the orig cmd */
971                         complete_scsi_command(cmdrsp, scsicmd);
972                 } else if (cmdrsp->cmdtype == CMD_SCSITASKMGMT_TYPE) {
973                         if (!del_scsipending_ent(devdata,
974                                                  cmdrsp->scsitaskmgmt.handle))
975                                 break;
976                         complete_taskmgmt_command(cmdrsp);
977                 } else if (cmdrsp->cmdtype == CMD_NOTIFYGUEST_TYPE) {
978                         /* The vHba pointer has no meaning in a
979                          * guest partition. Let's be safe and set it
980                          * to NULL now. Do not use it here!
981                          */
982                         cmdrsp->disknotify.v_hba = NULL;
983                         process_disk_notify(shost, cmdrsp);
984                 } else if (cmdrsp->cmdtype == CMD_VDISKMGMT_TYPE) {
985                         if (!del_scsipending_ent(devdata,
986                                                  cmdrsp->vdiskmgmt.handle))
987                                 break;
988                         complete_vdiskmgmt_command(cmdrsp);
989                 }
990                 /* cmdrsp is now available for resuse */
991         }
992 }
993
994 /**
995  *      process_incoming_rsps - Process responses from IOSP
996  *      @v: void pointer to visorhba_devdata
997  *
998  *      Main function for the thread that processes the responses
999  *      from the IO Service Partition. When the queue is empty, wait
1000  *      to check to see if it is full again.
1001  */
1002 static int process_incoming_rsps(void *v)
1003 {
1004         struct visorhba_devdata *devdata = v;
1005         struct uiscmdrsp *cmdrsp = NULL;
1006         const int size = sizeof(*cmdrsp);
1007
1008         cmdrsp = kmalloc(size, GFP_ATOMIC);
1009         if (!cmdrsp)
1010                 return -ENOMEM;
1011
1012         while (1) {
1013                 if (kthread_should_stop())
1014                         break;
1015                 wait_event_interruptible_timeout(
1016                         devdata->rsp_queue, (atomic_read(
1017                                              &devdata->interrupt_rcvd) == 1),
1018                                 msecs_to_jiffies(devdata->thread_wait_ms));
1019                 /* drain queue */
1020                 drain_queue(cmdrsp, devdata);
1021         }
1022         kfree(cmdrsp);
1023         return 0;
1024 }
1025
1026 /**
1027  *      visorhba_pause - function to handle visorbus pause messages
1028  *      @dev: device that is pausing.
1029  *      @complete_func: function to call when finished
1030  *
1031  *      Something has happened to the IO Service Partition that is
1032  *      handling this device. Quiet this device and reset commands
1033  *      so that the Service Partition can be corrected.
1034  *      Returns SUCCESS
1035  */
1036 static int visorhba_pause(struct visor_device *dev,
1037                           visorbus_state_complete_func complete_func)
1038 {
1039         struct visorhba_devdata *devdata = dev_get_drvdata(&dev->device);
1040
1041         visorhba_serverdown(devdata);
1042         complete_func(dev, 0);
1043         return 0;
1044 }
1045
1046 /**
1047  *      visorhba_resume - function called when the IO Service Partition is back
1048  *      @dev: device that is pausing.
1049  *      @complete_func: function to call when finished
1050  *
1051  *      Yay! The IO Service Partition is back, the channel has been wiped
1052  *      so lets re-establish connection and start processing responses.
1053  *      Returns 0 on success, error on failure.
1054  */
1055 static int visorhba_resume(struct visor_device *dev,
1056                            visorbus_state_complete_func complete_func)
1057 {
1058         struct visorhba_devdata *devdata;
1059
1060         devdata = dev_get_drvdata(&dev->device);
1061         if (!devdata)
1062                 return -EINVAL;
1063
1064         if (devdata->serverdown && !devdata->serverchangingstate)
1065                 devdata->serverchangingstate = 1;
1066
1067         visor_thread_start(&devdata->threadinfo, process_incoming_rsps,
1068                            devdata, "vhba_incming");
1069
1070         devdata->serverdown = false;
1071         devdata->serverchangingstate = false;
1072
1073         return 0;
1074 }
1075
1076 /**
1077  *      visorhba_probe - device has been discovered, do acquire
1078  *      @dev: visor_device that was discovered
1079  *
1080  *      A new HBA was discovered, do the initial connections of it.
1081  *      Return 0 on success, otherwise error.
1082  */
1083 static int visorhba_probe(struct visor_device *dev)
1084 {
1085         struct Scsi_Host *scsihost;
1086         struct vhba_config_max max;
1087         struct visorhba_devdata *devdata = NULL;
1088         int i, err, channel_offset;
1089         u64 features;
1090
1091         scsihost = scsi_host_alloc(&visorhba_driver_template,
1092                                    sizeof(*devdata));
1093         if (!scsihost)
1094                 return -ENODEV;
1095
1096         channel_offset = offsetof(struct spar_io_channel_protocol,
1097                                   vhba.max);
1098         err = visorbus_read_channel(dev, channel_offset, &max,
1099                                     sizeof(struct vhba_config_max));
1100         if (err < 0)
1101                 goto err_scsi_host_put;
1102
1103         scsihost->max_id = (unsigned)max.max_id;
1104         scsihost->max_lun = (unsigned)max.max_lun;
1105         scsihost->cmd_per_lun = (unsigned)max.cmd_per_lun;
1106         scsihost->max_sectors =
1107             (unsigned short)(max.max_io_size >> 9);
1108         scsihost->sg_tablesize =
1109             (unsigned short)(max.max_io_size / PAGE_SIZE);
1110         if (scsihost->sg_tablesize > MAX_PHYS_INFO)
1111                 scsihost->sg_tablesize = MAX_PHYS_INFO;
1112         err = scsi_add_host(scsihost, &dev->device);
1113         if (err < 0)
1114                 goto err_scsi_host_put;
1115
1116         devdata = (struct visorhba_devdata *)scsihost->hostdata;
1117         for (i = 0; i < VISORHBA_OPEN_MAX; i++) {
1118                 if (!visorhbas_open[i].devdata) {
1119                         visorhbas_open[i].devdata = devdata;
1120                         break;
1121                 }
1122         }
1123
1124         devdata->dev = dev;
1125         dev_set_drvdata(&dev->device, devdata);
1126
1127         init_waitqueue_head(&devdata->rsp_queue);
1128         spin_lock_init(&devdata->privlock);
1129         devdata->serverdown = false;
1130         devdata->serverchangingstate = false;
1131         devdata->scsihost = scsihost;
1132
1133         channel_offset = offsetof(struct spar_io_channel_protocol,
1134                                   channel_header.features);
1135         err = visorbus_read_channel(dev, channel_offset, &features, 8);
1136         if (err)
1137                 goto err_scsi_remove_host;
1138         features |= ULTRA_IO_CHANNEL_IS_POLLING;
1139         err = visorbus_write_channel(dev, channel_offset, &features, 8);
1140         if (err)
1141                 goto err_scsi_remove_host;
1142
1143         devdata->thread_wait_ms = 2;
1144         visor_thread_start(&devdata->threadinfo, process_incoming_rsps,
1145                            devdata, "vhba_incoming");
1146
1147         scsi_scan_host(scsihost);
1148
1149         return 0;
1150
1151 err_scsi_remove_host:
1152         scsi_remove_host(scsihost);
1153
1154 err_scsi_host_put:
1155         scsi_host_put(scsihost);
1156         return err;
1157 }
1158
1159 /**
1160  *      visorhba_remove - remove a visorhba device
1161  *      @dev: Device to remove
1162  *
1163  *      Removes the visorhba device.
1164  *      Returns void.
1165  */
1166 static void visorhba_remove(struct visor_device *dev)
1167 {
1168         struct visorhba_devdata *devdata = dev_get_drvdata(&dev->device);
1169         struct Scsi_Host *scsihost = NULL;
1170
1171         if (!devdata)
1172                 return;
1173
1174         scsihost = devdata->scsihost;
1175         kthread_stop(devdata->threadinfo.task);
1176         scsi_remove_host(scsihost);
1177         scsi_host_put(scsihost);
1178
1179         dev_set_drvdata(&dev->device, NULL);
1180 }
1181
1182 /**
1183  *      visorhba_init           - driver init routine
1184  *
1185  *      Initialize the visorhba driver and register it with visorbus
1186  *      to handle s-Par virtual host bus adapter.
1187  */
1188 static int visorhba_init(void)
1189 {
1190         struct dentry *ret;
1191         int rc = -ENOMEM;
1192
1193         visorhba_debugfs_dir = debugfs_create_dir("visorhba", NULL);
1194         if (!visorhba_debugfs_dir)
1195                 return -ENOMEM;
1196
1197         ret = debugfs_create_file("info", S_IRUSR, visorhba_debugfs_dir, NULL,
1198                                   &debugfs_info_fops);
1199
1200         if (!ret) {
1201                 rc = -EIO;
1202                 goto cleanup_debugfs;
1203         }
1204
1205         rc = visorbus_register_visor_driver(&visorhba_driver);
1206         if (rc)
1207                 goto cleanup_debugfs;
1208
1209         return rc;
1210
1211 cleanup_debugfs:
1212         debugfs_remove_recursive(visorhba_debugfs_dir);
1213
1214         return rc;
1215 }
1216
1217 /**
1218  *      visorhba_cleanup        - driver exit routine
1219  *
1220  *      Unregister driver from the bus and free up memory.
1221  */
1222 static void visorhba_exit(void)
1223 {
1224         visorbus_unregister_visor_driver(&visorhba_driver);
1225         debugfs_remove_recursive(visorhba_debugfs_dir);
1226 }
1227
1228 module_init(visorhba_init);
1229 module_exit(visorhba_exit);
1230
1231 MODULE_AUTHOR("Unisys");
1232 MODULE_LICENSE("GPL");
1233 MODULE_DESCRIPTION("s-Par hba driver");