1 /* Copyright (c) 2012 - 2015 UNISYS CORPORATION
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.
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
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>
25 #include "iochannel.h"
27 /* The Send and Receive Buffers of the IO Queue may both be full */
29 #define IOS_ERROR_THRESHOLD 1000
30 /* MAX_BUF = 6 lines x 10 MAXVHBA x 80 characters
31 * = 4800 bytes ~ 2^13 = 8192 bytes
34 #define MAX_PENDING_REQUESTS (MIN_NUMSIGNALS * 2)
35 #define VISORHBA_ERROR_COUNT 30
36 #define VISORHBA_OPEN_MAX 1
38 static int visorhba_queue_command_lck(struct scsi_cmnd *scsicmd,
39 void (*visorhba_cmnd_done)
40 (struct scsi_cmnd *));
42 static DEF_SCSI_QCMD(visorhba_queue_command)
44 #define visorhba_queue_command visorhba_queue_command_lck
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);
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,
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.
65 { SPAR_VHBA_CHANNEL_PROTOCOL_UUID, "sparvhba" },
66 { NULL_UUID_LE, NULL }
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.
73 static struct visor_driver visorhba_driver = {
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,
83 MODULE_DEVICE_TABLE(visorbus, visorhba_channel_types);
84 MODULE_ALIAS("visorbus:" SPAR_VHBA_CHANNEL_PROTOCOL_UUID_STR);
86 struct visor_thread_info {
87 struct task_struct *task;
88 struct completion has_stopped;
92 struct visordisk_info {
94 u32 channel, id, lun; /* Disk Path */
95 atomic_t ios_threshold;
97 struct visordisk_info *next;
101 struct uiscmdrsp cmdrsp;
102 void *sent; /* The Data being tracked */
103 char cmdtype; /* Type of pointer that is being stored */
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;
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
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 */
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;
138 struct visor_thread_info threadinfo;
142 struct visorhba_devices_open {
143 struct visorhba_devdata *devdata;
146 static struct visorhba_devices_open visorhbas_open[VISORHBA_OPEN_MAX];
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))
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
160 * Starts a thread for the device.
162 * Return 0 on success;
164 static int visor_thread_start(struct visor_thread_info *thrinfo,
165 int (*threadfn)(void *),
166 void *thrcontext, char *name)
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)) {
173 return PTR_ERR(thrinfo->task);
175 thrinfo->id = thrinfo->task->pid;
180 * add_scsipending_entry - save off io command that is pending in
182 * @devdata: Pointer to devdata
183 * @cmdtype: Specifies the type of command pending
184 * @new: The command to be saved
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
192 static int add_scsipending_entry(struct visorhba_devdata *devdata,
193 char cmdtype, void *new)
196 struct scsipending *entry;
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);
209 entry = &devdata->pending[insert_location];
210 memset(&entry->cmdrsp, 0, sizeof(entry->cmdrsp));
211 entry->cmdtype = cmdtype;
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);
219 return insert_location;
223 * del_scsipending_enty - removes an entry from the pending array
224 * @devdata: Device holding the pending array
225 * @del: Entry to remove
227 * Removes the entry pointed at by del and returns it.
228 * Returns the scsipending entry pointed at
230 static void *del_scsipending_ent(struct visorhba_devdata *devdata,
236 if (del < MAX_PENDING_REQUESTS) {
237 spin_lock_irqsave(&devdata->privlock, flags);
238 sent = devdata->pending[del].sent;
240 devdata->pending[del].cmdtype = 0;
241 devdata->pending[del].sent = NULL;
242 spin_unlock_irqrestore(&devdata->privlock, flags);
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
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.
257 static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata,
260 if (ddata->pending[ent].sent)
261 return &ddata->pending[ent].cmdrsp;
267 * forward_taskmgmt_command - send taskmegmt command to the Service
269 * @tasktype: Type of taskmgmt command
270 * @scsidev: Scsidev that issued command
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.
276 static int forward_taskmgmt_command(enum task_mgmt_types tasktype,
277 struct scsi_cmnd *scsicmd)
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;
287 if (devdata->serverdown || devdata->serverchangingstate)
290 scsicmd_id = add_scsipending_entry(devdata, CMD_SCSITASKMGMT_TYPE,
295 cmdrsp = get_scsipending_cmdrsp(devdata, scsicmd_id);
297 init_waitqueue_head(¬ifyevent);
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)¬ifyevent;
304 cmdrsp->scsitaskmgmt.notifyresult_handle = (u64)¬ifyresult;
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;
313 if (!visorchannel_signalinsert(devdata->dev->visorchannel,
316 goto err_del_scsipending_ent;
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
321 if (!wait_event_timeout(notifyevent, notifyresult != 0xffff,
322 msecs_to_jiffies(45000)))
323 goto err_del_scsipending_ent;
325 if (tasktype == TASK_MGMT_ABORT_TASK)
326 scsicmd->result = (DID_ABORT << 16);
328 scsicmd->result = (DID_RESET << 16);
330 scsicmd->scsi_done(scsicmd);
334 err_del_scsipending_ent:
335 del_scsipending_ent(devdata, scsicmd_id);
340 * visorhba_abort_handler - Send TASK_MGMT_ABORT_TASK
341 * @scsicmd: The scsicmd that needs aborted
343 * Returns SUCCESS if inserted, failure otherwise
346 static int visorhba_abort_handler(struct scsi_cmnd *scsicmd)
348 /* issue TASK_MGMT_ABORT_TASK */
349 struct scsi_device *scsidev;
350 struct visordisk_info *vdisk;
351 struct visorhba_devdata *devdata;
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);
359 atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
361 return forward_taskmgmt_command(TASK_MGMT_ABORT_TASK, scsicmd);
365 * visorhba_device_reset_handler - Send TASK_MGMT_LUN_RESET
366 * @scsicmd: The scsicmd that needs aborted
368 * Returns SUCCESS if inserted, failure otherwise
370 static int visorhba_device_reset_handler(struct scsi_cmnd *scsicmd)
372 /* issue TASK_MGMT_LUN_RESET */
373 struct scsi_device *scsidev;
374 struct visordisk_info *vdisk;
375 struct visorhba_devdata *devdata;
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);
383 atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
385 return forward_taskmgmt_command(TASK_MGMT_LUN_RESET, scsicmd);
389 * visorhba_bus_reset_handler - Send TASK_MGMT_TARGET_RESET for each
391 * @scsicmd: The scsicmd that needs aborted
395 static int visorhba_bus_reset_handler(struct scsi_cmnd *scsicmd)
397 struct scsi_device *scsidev;
398 struct visordisk_info *vdisk;
399 struct visorhba_devdata *devdata;
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);
407 atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
409 return forward_taskmgmt_command(TASK_MGMT_BUS_RESET, scsicmd);
413 * visorhba_host_reset_handler - Not supported
414 * @scsicmd: The scsicmd that needs aborted
416 * Not supported, return SUCCESS
420 visorhba_host_reset_handler(struct scsi_cmnd *scsicmd)
422 /* issue TASK_MGMT_TARGET_RESET for each target on each bus for host */
428 * @shp: Scsi host that is requesting information
430 * Returns string with info
432 static const char *visorhba_get_info(struct Scsi_Host *shp)
434 /* Return version string */
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
443 * Queues to scsicmd to the ServicePartition after converting it to a
444 * uiscmdrsp structure.
446 * Returns success if queued to the Service Partition, otherwise
450 visorhba_queue_command_lck(struct scsi_cmnd *scsicmd,
451 void (*visorhba_cmnd_done)(struct scsi_cmnd *))
453 struct uiscmdrsp *cmdrsp;
454 struct scsi_device *scsidev = scsicmd->device;
457 unsigned char *cdb = scsicmd->cmnd;
458 struct Scsi_Host *scsihost = scsidev->host;
460 struct visorhba_devdata *devdata =
461 (struct visorhba_devdata *)scsihost->hostdata;
462 struct scatterlist *sg = NULL;
463 struct scatterlist *sglist = NULL;
466 if (devdata->serverdown || devdata->serverchangingstate)
467 return SCSI_MLQUEUE_DEVICE_BUSY;
469 insert_location = add_scsipending_entry(devdata, CMD_SCSI_TYPE,
472 if (insert_location < 0)
473 return SCSI_MLQUEUE_DEVICE_BUSY;
475 cmdrsp = get_scsipending_cmdrsp(devdata, insert_location);
477 cmdrsp->cmdtype = CMD_SCSI_TYPE;
478 /* save the pending insertion location. Deletion from pending
479 * will return the scsicmd pointer for completion
481 cmdrsp->scsi.handle = insert_location;
483 /* save done function that we have call when cmd is complete */
484 scsicmd->scsi_done = visorhba_cmnd_done;
485 /* save destination */
486 cmdrsp->scsi.vdest.channel = scsidev->channel;
487 cmdrsp->scsi.vdest.id = scsidev->id;
488 cmdrsp->scsi.vdest.lun = scsidev->lun;
490 cmdrsp->scsi.data_dir = scsicmd->sc_data_direction;
491 memcpy(cmdrsp->scsi.cmnd, cdb, MAX_CMND_SIZE);
493 cmdrsp->scsi.bufflen = scsi_bufflen(scsicmd);
495 /* keep track of the max buffer length so far. */
496 if (cmdrsp->scsi.bufflen > devdata->max_buff_len)
497 devdata->max_buff_len = cmdrsp->scsi.bufflen;
499 if (scsi_sg_count(scsicmd) > MAX_PHYS_INFO) {
500 err = SCSI_MLQUEUE_DEVICE_BUSY;
501 goto err_del_scsipending_ent;
504 /* convert buffer to phys information */
505 /* buffer is scatterlist - copy it out */
506 sglist = scsi_sglist(scsicmd);
508 for_each_sg(sglist, sg, scsi_sg_count(scsicmd), i) {
509 cmdrsp->scsi.gpi_list[i].address = sg_phys(sg);
510 cmdrsp->scsi.gpi_list[i].length = sg->length;
512 cmdrsp->scsi.guest_phys_entries = scsi_sg_count(scsicmd);
515 if (!visorchannel_signalinsert(devdata->dev->visorchannel,
518 /* queue must be full and we aren't going to wait */
519 err = SCSI_MLQUEUE_DEVICE_BUSY;
520 goto err_del_scsipending_ent;
524 err_del_scsipending_ent:
525 del_scsipending_ent(devdata, insert_location);
530 * visorhba_slave_alloc - called when new disk is discovered
533 * Create a new visordisk_info structure and add it to our
536 * Returns success when created, otherwise error.
538 static int visorhba_slave_alloc(struct scsi_device *scsidev)
540 /* this is called by the midlayer before scan for new devices --
541 * LLD can alloc any struct & do init if needed.
543 struct visordisk_info *vdisk;
544 struct visordisk_info *tmpvdisk;
545 struct visorhba_devdata *devdata;
546 struct Scsi_Host *scsihost = (struct Scsi_Host *)scsidev->host;
548 devdata = (struct visorhba_devdata *)scsihost->hostdata;
550 return 0; /* even though we errored, treat as success */
552 for_each_vdisk_match(vdisk, devdata, scsidev)
553 return 0; /* already allocated return success */
555 tmpvdisk = kzalloc(sizeof(*tmpvdisk), GFP_ATOMIC);
559 tmpvdisk->channel = scsidev->channel;
560 tmpvdisk->id = scsidev->id;
561 tmpvdisk->lun = scsidev->lun;
562 vdisk->next = tmpvdisk;
567 * visorhba_slave_destroy - disk is going away
568 * @scsidev: scsi device going away
570 * Disk is going away, clean up resources.
573 static void visorhba_slave_destroy(struct scsi_device *scsidev)
575 /* midlevel calls this after device has been quiesced and
576 * before it is to be deleted.
578 struct visordisk_info *vdisk, *delvdisk;
579 struct visorhba_devdata *devdata;
580 struct Scsi_Host *scsihost = (struct Scsi_Host *)scsidev->host;
582 devdata = (struct visorhba_devdata *)scsihost->hostdata;
583 for_each_vdisk_match(vdisk, devdata, scsidev) {
584 delvdisk = vdisk->next;
585 vdisk->next = delvdisk->next;
591 static struct scsi_host_template visorhba_driver_template = {
592 .name = "Unisys Visor HBA",
593 .info = visorhba_get_info,
594 .queuecommand = visorhba_queue_command,
595 .eh_abort_handler = visorhba_abort_handler,
596 .eh_device_reset_handler = visorhba_device_reset_handler,
597 .eh_bus_reset_handler = visorhba_bus_reset_handler,
598 .eh_host_reset_handler = visorhba_host_reset_handler,
600 #define visorhba_MAX_CMNDS 128
601 .can_queue = visorhba_MAX_CMNDS,
604 .slave_alloc = visorhba_slave_alloc,
605 .slave_destroy = visorhba_slave_destroy,
606 .use_clustering = ENABLE_CLUSTERING,
610 * info_debugfs_read - debugfs interface to dump visorhba states
612 * @buf: buffer to send back to user
613 * @len: len that can be written to buf
614 * @offset: offset into buf
616 * Dumps information about the visorhba driver and devices
617 * TODO: Make this per vhba
620 static ssize_t info_debugfs_read(struct file *file, char __user *buf,
621 size_t len, loff_t *offset)
623 ssize_t bytes_read = 0;
627 struct visorhba_devdata *devdata;
632 vbuf = kzalloc(len, GFP_KERNEL);
636 for (i = 0; i < VISORHBA_OPEN_MAX; i++) {
637 if (!visorhbas_open[i].devdata)
640 devdata = visorhbas_open[i].devdata;
642 str_pos += scnprintf(vbuf + str_pos,
643 len - str_pos, "max_buff_len:%u\n",
644 devdata->max_buff_len);
646 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
647 "\ninterrupts_rcvd = %llu, interrupts_disabled = %llu\n",
648 devdata->interrupts_rcvd,
649 devdata->interrupts_disabled);
650 str_pos += scnprintf(vbuf + str_pos,
651 len - str_pos, "\ninterrupts_notme = %llu,\n",
652 devdata->interrupts_notme);
653 phys_flags_addr = virt_to_phys((__force void *)
654 devdata->flags_addr);
655 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
656 "flags_addr = %p, phys_flags_addr=0x%016llx, FeatureFlags=%llu\n",
657 devdata->flags_addr, phys_flags_addr,
658 (__le64)readq(devdata->flags_addr));
659 str_pos += scnprintf(vbuf + str_pos,
660 len - str_pos, "acquire_failed_cnt:%llu\n",
661 devdata->acquire_failed_cnt);
662 str_pos += scnprintf(vbuf + str_pos, len - str_pos, "\n");
665 bytes_read = simple_read_from_buffer(buf, len, offset, vbuf, str_pos);
671 * visorhba_serverdown_complete - Called when we are done cleaning up
673 * @work: work structure for this serverdown request
675 * Called when we are done cleanning up from serverdown, stop processing
676 * queue, fail pending IOs.
677 * Returns void when finished cleaning up
679 static void visorhba_serverdown_complete(struct visorhba_devdata *devdata)
682 struct scsipending *pendingdel = NULL;
683 struct scsi_cmnd *scsicmd = NULL;
684 struct uiscmdrsp *cmdrsp;
687 /* Stop using the IOVM response queue (queue should be drained
690 kthread_stop(devdata->threadinfo.task);
692 /* Fail commands that weren't completed */
693 spin_lock_irqsave(&devdata->privlock, flags);
694 for (i = 0; i < MAX_PENDING_REQUESTS; i++) {
695 pendingdel = &devdata->pending[i];
696 switch (pendingdel->cmdtype) {
698 scsicmd = pendingdel->sent;
699 scsicmd->result = DID_RESET << 16;
700 if (scsicmd->scsi_done)
701 scsicmd->scsi_done(scsicmd);
703 case CMD_SCSITASKMGMT_TYPE:
704 cmdrsp = pendingdel->sent;
705 cmdrsp->scsitaskmgmt.notifyresult_handle
707 wake_up_all((wait_queue_head_t *)
708 cmdrsp->scsitaskmgmt.notify_handle);
710 case CMD_VDISKMGMT_TYPE:
711 cmdrsp = pendingdel->sent;
712 cmdrsp->vdiskmgmt.notifyresult_handle
714 wake_up_all((wait_queue_head_t *)
715 cmdrsp->vdiskmgmt.notify_handle);
720 pendingdel->cmdtype = 0;
721 pendingdel->sent = NULL;
723 spin_unlock_irqrestore(&devdata->privlock, flags);
725 devdata->serverdown = true;
726 devdata->serverchangingstate = false;
730 * visorhba_serverdown - Got notified that the IOVM is down
731 * @devdata: visorhba that is being serviced by downed IOVM.
733 * Something happened to the IOVM, return immediately and
734 * schedule work cleanup work.
735 * Return SUCCESS or EINVAL
737 static int visorhba_serverdown(struct visorhba_devdata *devdata)
739 if (!devdata->serverdown && !devdata->serverchangingstate) {
740 devdata->serverchangingstate = true;
741 visorhba_serverdown_complete(devdata);
742 } else if (devdata->serverchangingstate) {
749 * do_scsi_linuxstat - scsi command returned linuxstat
750 * @cmdrsp: response from IOVM
751 * @scsicmd: Command issued.
753 * Don't log errors for disk-not-present inquiries
757 do_scsi_linuxstat(struct uiscmdrsp *cmdrsp, struct scsi_cmnd *scsicmd)
759 struct visorhba_devdata *devdata;
760 struct visordisk_info *vdisk;
761 struct scsi_device *scsidev;
762 struct sense_data *sd;
764 scsidev = scsicmd->device;
765 memcpy(scsicmd->sense_buffer, cmdrsp->scsi.sensebuf, MAX_SENSE_SIZE);
766 sd = (struct sense_data *)scsicmd->sense_buffer;
768 /* Do not log errors for disk-not-present inquiries */
769 if ((cmdrsp->scsi.cmnd[0] == INQUIRY) &&
770 (host_byte(cmdrsp->scsi.linuxstat) == DID_NO_CONNECT) &&
771 (cmdrsp->scsi.addlstat == ADDL_SEL_TIMEOUT))
773 /* Okay see what our error_count is here.... */
774 devdata = (struct visorhba_devdata *)scsidev->host->hostdata;
775 for_each_vdisk_match(vdisk, devdata, scsidev) {
776 if (atomic_read(&vdisk->error_count) < VISORHBA_ERROR_COUNT) {
777 atomic_inc(&vdisk->error_count);
778 atomic_set(&vdisk->ios_threshold, IOS_ERROR_THRESHOLD);
784 * do_scsi_nolinuxstat - scsi command didn't have linuxstat
785 * @cmdrsp: response from IOVM
786 * @scsicmd: Command issued.
788 * Handle response when no linuxstat was returned
792 do_scsi_nolinuxstat(struct uiscmdrsp *cmdrsp, struct scsi_cmnd *scsicmd)
794 struct scsi_device *scsidev;
796 struct scatterlist *sg;
799 char *this_page_orig;
801 struct visordisk_info *vdisk;
802 struct visorhba_devdata *devdata;
804 scsidev = scsicmd->device;
805 if ((cmdrsp->scsi.cmnd[0] == INQUIRY) &&
806 (cmdrsp->scsi.bufflen >= MIN_INQUIRY_RESULT_LEN)) {
807 if (cmdrsp->scsi.no_disk_result == 0)
810 buf = kzalloc(sizeof(char) * 36, GFP_KERNEL);
814 /* Linux scsi code wants a device at Lun 0
815 * to issue report luns, but we don't want
816 * a disk there so we'll present a processor
819 SET_NO_DISK_INQUIRY_RESULT(buf, cmdrsp->scsi.bufflen,
821 DEV_DISK_CAPABLE_NOT_PRESENT,
824 if (scsi_sg_count(scsicmd) == 0) {
825 memcpy(scsi_sglist(scsicmd), buf,
826 cmdrsp->scsi.bufflen);
831 sg = scsi_sglist(scsicmd);
832 for (i = 0; i < scsi_sg_count(scsicmd); i++) {
833 this_page_orig = kmap_atomic(sg_page(sg + i));
834 this_page = (void *)((unsigned long)this_page_orig |
836 memcpy(this_page, buf + bufind, sg[i].length);
837 kunmap_atomic(this_page_orig);
841 devdata = (struct visorhba_devdata *)scsidev->host->hostdata;
842 for_each_vdisk_match(vdisk, devdata, scsidev) {
843 if (atomic_read(&vdisk->ios_threshold) > 0) {
844 atomic_dec(&vdisk->ios_threshold);
845 if (atomic_read(&vdisk->ios_threshold) == 0)
846 atomic_set(&vdisk->error_count, 0);
853 * complete_scsi_command - complete a scsi command
854 * @uiscmdrsp: Response from Service Partition
855 * @scsicmd: The scsi command
857 * Response returned by the Service Partition, finish it and send
858 * completion to the scsi midlayer.
862 complete_scsi_command(struct uiscmdrsp *cmdrsp, struct scsi_cmnd *scsicmd)
864 /* take what we need out of cmdrsp and complete the scsicmd */
865 scsicmd->result = cmdrsp->scsi.linuxstat;
866 if (cmdrsp->scsi.linuxstat)
867 do_scsi_linuxstat(cmdrsp, scsicmd);
869 do_scsi_nolinuxstat(cmdrsp, scsicmd);
871 scsicmd->scsi_done(scsicmd);
874 /* DELETE VDISK TASK MGMT COMMANDS */
875 static inline void complete_vdiskmgmt_command(struct uiscmdrsp *cmdrsp)
877 /* copy the result of the taskmgmt and
878 * wake up the error handler that is waiting for this
880 cmdrsp->vdiskmgmt.notifyresult_handle = cmdrsp->vdiskmgmt.result;
881 wake_up_all((wait_queue_head_t *)cmdrsp->vdiskmgmt.notify_handle);
885 * complete_taskmgmt_command - complete task management
886 * @cmdrsp: Response from the IOVM
888 * Service Partition returned the result of the task management
889 * command. Wake up anyone waiting for it.
892 static inline void complete_taskmgmt_command(struct uiscmdrsp *cmdrsp)
894 /* copy the result of the taskgmgt and
895 * wake up the error handler that is waiting for this
897 cmdrsp->vdiskmgmt.notifyresult_handle = cmdrsp->vdiskmgmt.result;
898 wake_up_all((wait_queue_head_t *)cmdrsp->scsitaskmgmt.notify_handle);
901 static struct work_struct dar_work_queue;
902 static struct diskaddremove *dar_work_queue_head;
903 static spinlock_t dar_work_queue_lock; /* Lock to protet dar_work_queue_head */
904 static unsigned short dar_work_queue_sched;
907 * queue_disk_add_remove - IOSP has sent us a add/remove request
908 * @dar: disk add/remove request
910 * Queue the work needed to add/remove a disk.
913 static inline void queue_disk_add_remove(struct diskaddremove *dar)
917 spin_lock_irqsave(&dar_work_queue_lock, flags);
918 if (!dar_work_queue_head) {
919 dar_work_queue_head = dar;
922 dar->next = dar_work_queue_head;
923 dar_work_queue_head = dar;
925 if (!dar_work_queue_sched) {
926 schedule_work(&dar_work_queue);
927 dar_work_queue_sched = 1;
929 spin_unlock_irqrestore(&dar_work_queue_lock, flags);
933 * process_disk_notify - IOSP has sent a process disk notify event
935 * @cmdrsp: Response from the IOSP
937 * Queue it to the work queue.
940 static void process_disk_notify(struct Scsi_Host *shost,
941 struct uiscmdrsp *cmdrsp)
943 struct diskaddremove *dar;
945 dar = kzalloc(sizeof(*dar), GFP_ATOMIC);
947 dar->add = cmdrsp->disknotify.add;
949 dar->channel = cmdrsp->disknotify.channel;
950 dar->id = cmdrsp->disknotify.id;
951 dar->lun = cmdrsp->disknotify.lun;
952 queue_disk_add_remove(dar);
957 * drain_queue - pull responses out of iochannel
958 * @cmdrsp: Response from the IOSP
959 * @devdata: device that owns this iochannel
961 * Pulls responses out of the iochannel and process the responses.
965 drain_queue(struct uiscmdrsp *cmdrsp, struct visorhba_devdata *devdata)
967 struct scsi_cmnd *scsicmd;
968 struct Scsi_Host *shost = devdata->scsihost;
971 if (!visorchannel_signalremove(devdata->dev->visorchannel,
974 break; /* queue empty */
976 if (cmdrsp->cmdtype == CMD_SCSI_TYPE) {
977 /* scsicmd location is returned by the
980 scsicmd = del_scsipending_ent(devdata,
981 cmdrsp->scsi.handle);
984 /* complete the orig cmd */
985 complete_scsi_command(cmdrsp, scsicmd);
986 } else if (cmdrsp->cmdtype == CMD_SCSITASKMGMT_TYPE) {
987 if (!del_scsipending_ent(devdata,
988 cmdrsp->scsitaskmgmt.handle))
990 complete_taskmgmt_command(cmdrsp);
991 } else if (cmdrsp->cmdtype == CMD_NOTIFYGUEST_TYPE) {
992 /* The vHba pointer has no meaning in a
993 * guest partition. Let's be safe and set it
994 * to NULL now. Do not use it here!
996 cmdrsp->disknotify.v_hba = NULL;
997 process_disk_notify(shost, cmdrsp);
998 } else if (cmdrsp->cmdtype == CMD_VDISKMGMT_TYPE) {
999 if (!del_scsipending_ent(devdata,
1000 cmdrsp->vdiskmgmt.handle))
1002 complete_vdiskmgmt_command(cmdrsp);
1004 /* cmdrsp is now available for resuse */
1009 * process_incoming_rsps - Process responses from IOSP
1010 * @v: void pointer to visorhba_devdata
1012 * Main function for the thread that processes the responses
1013 * from the IO Service Partition. When the queue is empty, wait
1014 * to check to see if it is full again.
1016 static int process_incoming_rsps(void *v)
1018 struct visorhba_devdata *devdata = v;
1019 struct uiscmdrsp *cmdrsp = NULL;
1020 const int size = sizeof(*cmdrsp);
1022 cmdrsp = kmalloc(size, GFP_ATOMIC);
1027 if (kthread_should_stop())
1029 wait_event_interruptible_timeout(
1030 devdata->rsp_queue, (atomic_read(
1031 &devdata->interrupt_rcvd) == 1),
1032 msecs_to_jiffies(devdata->thread_wait_ms));
1034 drain_queue(cmdrsp, devdata);
1041 * visorhba_pause - function to handle visorbus pause messages
1042 * @dev: device that is pausing.
1043 * @complete_func: function to call when finished
1045 * Something has happened to the IO Service Partition that is
1046 * handling this device. Quiet this device and reset commands
1047 * so that the Service Partition can be corrected.
1050 static int visorhba_pause(struct visor_device *dev,
1051 visorbus_state_complete_func complete_func)
1053 struct visorhba_devdata *devdata = dev_get_drvdata(&dev->device);
1055 visorhba_serverdown(devdata);
1056 complete_func(dev, 0);
1061 * visorhba_resume - function called when the IO Service Partition is back
1062 * @dev: device that is pausing.
1063 * @complete_func: function to call when finished
1065 * Yay! The IO Service Partition is back, the channel has been wiped
1066 * so lets re-establish connection and start processing responses.
1067 * Returns 0 on success, error on failure.
1069 static int visorhba_resume(struct visor_device *dev,
1070 visorbus_state_complete_func complete_func)
1072 struct visorhba_devdata *devdata;
1074 devdata = dev_get_drvdata(&dev->device);
1078 if (devdata->serverdown && !devdata->serverchangingstate)
1079 devdata->serverchangingstate = 1;
1081 visor_thread_start(&devdata->threadinfo, process_incoming_rsps,
1082 devdata, "vhba_incming");
1084 devdata->serverdown = false;
1085 devdata->serverchangingstate = false;
1091 * visorhba_probe - device has been discovered, do acquire
1092 * @dev: visor_device that was discovered
1094 * A new HBA was discovered, do the initial connections of it.
1095 * Return 0 on success, otherwise error.
1097 static int visorhba_probe(struct visor_device *dev)
1099 struct Scsi_Host *scsihost;
1100 struct vhba_config_max max;
1101 struct visorhba_devdata *devdata = NULL;
1102 int i, err, channel_offset;
1105 scsihost = scsi_host_alloc(&visorhba_driver_template,
1110 channel_offset = offsetof(struct spar_io_channel_protocol,
1112 err = visorbus_read_channel(dev, channel_offset, &max,
1113 sizeof(struct vhba_config_max));
1115 goto err_scsi_host_put;
1117 scsihost->max_id = (unsigned)max.max_id;
1118 scsihost->max_lun = (unsigned)max.max_lun;
1119 scsihost->cmd_per_lun = (unsigned)max.cmd_per_lun;
1120 scsihost->max_sectors =
1121 (unsigned short)(max.max_io_size >> 9);
1122 scsihost->sg_tablesize =
1123 (unsigned short)(max.max_io_size / PAGE_SIZE);
1124 if (scsihost->sg_tablesize > MAX_PHYS_INFO)
1125 scsihost->sg_tablesize = MAX_PHYS_INFO;
1126 err = scsi_add_host(scsihost, &dev->device);
1128 goto err_scsi_host_put;
1130 devdata = (struct visorhba_devdata *)scsihost->hostdata;
1131 for (i = 0; i < VISORHBA_OPEN_MAX; i++) {
1132 if (!visorhbas_open[i].devdata) {
1133 visorhbas_open[i].devdata = devdata;
1139 dev_set_drvdata(&dev->device, devdata);
1141 init_waitqueue_head(&devdata->rsp_queue);
1142 spin_lock_init(&devdata->privlock);
1143 devdata->serverdown = false;
1144 devdata->serverchangingstate = false;
1145 devdata->scsihost = scsihost;
1147 channel_offset = offsetof(struct spar_io_channel_protocol,
1148 channel_header.features);
1149 err = visorbus_read_channel(dev, channel_offset, &features, 8);
1151 goto err_scsi_remove_host;
1152 features |= ULTRA_IO_CHANNEL_IS_POLLING;
1153 err = visorbus_write_channel(dev, channel_offset, &features, 8);
1155 goto err_scsi_remove_host;
1157 devdata->thread_wait_ms = 2;
1158 visor_thread_start(&devdata->threadinfo, process_incoming_rsps,
1159 devdata, "vhba_incoming");
1161 scsi_scan_host(scsihost);
1165 err_scsi_remove_host:
1166 scsi_remove_host(scsihost);
1169 scsi_host_put(scsihost);
1174 * visorhba_remove - remove a visorhba device
1175 * @dev: Device to remove
1177 * Removes the visorhba device.
1180 static void visorhba_remove(struct visor_device *dev)
1182 struct visorhba_devdata *devdata = dev_get_drvdata(&dev->device);
1183 struct Scsi_Host *scsihost = NULL;
1188 scsihost = devdata->scsihost;
1189 kthread_stop(devdata->threadinfo.task);
1190 scsi_remove_host(scsihost);
1191 scsi_host_put(scsihost);
1193 dev_set_drvdata(&dev->device, NULL);
1197 * visorhba_init - driver init routine
1199 * Initialize the visorhba driver and register it with visorbus
1200 * to handle s-Par virtual host bus adapter.
1202 static int visorhba_init(void)
1207 visorhba_debugfs_dir = debugfs_create_dir("visorhba", NULL);
1208 if (!visorhba_debugfs_dir)
1211 ret = debugfs_create_file("info", S_IRUSR, visorhba_debugfs_dir, NULL,
1212 &debugfs_info_fops);
1216 goto cleanup_debugfs;
1219 rc = visorbus_register_visor_driver(&visorhba_driver);
1221 goto cleanup_debugfs;
1226 debugfs_remove_recursive(visorhba_debugfs_dir);
1232 * visorhba_cleanup - driver exit routine
1234 * Unregister driver from the bus and free up memory.
1236 static void visorhba_exit(void)
1238 visorbus_unregister_visor_driver(&visorhba_driver);
1239 debugfs_remove_recursive(visorhba_debugfs_dir);
1242 module_init(visorhba_init);
1243 module_exit(visorhba_exit);
1245 MODULE_AUTHOR("Unisys");
1246 MODULE_LICENSE("GPL");
1247 MODULE_DESCRIPTION("s-Par hba driver");