1 /* ------------------------------------------------------------
3 * (C) Copyright IBM Corporation 1994, 2004
4 * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
5 * Santiago Leon (santil@us.ibm.com)
6 * Dave Boutcher (sleddog@us.ibm.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * ------------------------------------------------------------
24 * Emulation of a SCSI host adapter for Virtual I/O devices
26 * This driver supports the SCSI adapter implemented by the IBM
27 * Power5 firmware. That SCSI adapter is not a physical adapter,
28 * but allows Linux SCSI peripheral drivers to directly
29 * access devices in another logical partition on the physical system.
31 * The virtual adapter(s) are present in the open firmware device
32 * tree just like real adapters.
34 * One of the capabilities provided on these systems is the ability
35 * to DMA between partitions. The architecture states that for VSCSI,
36 * the server side is allowed to DMA to and from the client. The client
37 * is never trusted to DMA to or from the server directly.
39 * Messages are sent between partitions on a "Command/Response Queue"
40 * (CRQ), which is just a buffer of 16 byte entries in the receiver's
41 * Senders cannot access the buffer directly, but send messages by
42 * making a hypervisor call and passing in the 16 bytes. The hypervisor
43 * puts the message in the next 16 byte space in round-robbin fashion,
44 * turns on the high order bit of the message (the valid bit), and
45 * generates an interrupt to the receiver (if interrupts are turned on.)
46 * The receiver just turns off the valid bit when they have copied out
49 * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
50 * (IU) (as defined in the T10 standard available at www.t10.org), gets
51 * a DMA address for the message, and sends it to the server as the
52 * payload of a CRQ message. The server DMAs the SRP IU and processes it,
53 * including doing any additional data transfers. When it is done, it
54 * DMAs the SRP response back to the same address as the request came from,
55 * and sends a CRQ message back to inform the client that the request has
58 * Note that some of the underlying infrastructure is different between
59 * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
60 * the older iSeries hypervisor models. To support both, some low level
61 * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
62 * The Makefile should pick one, not two, not zero, of these.
64 * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
65 * interfaces. It would be really nice to abstract this above an RDMA
69 #include <linux/module.h>
70 #include <linux/moduleparam.h>
71 #include <linux/dma-mapping.h>
72 #include <linux/delay.h>
74 #include <scsi/scsi.h>
75 #include <scsi/scsi_cmnd.h>
76 #include <scsi/scsi_host.h>
77 #include <scsi/scsi_device.h>
80 /* The values below are somewhat arbitrary default values, but
81 * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
82 * Note that there are 3 bits of channel value, 6 bits of id, and
85 static int max_id = 64;
86 static int max_channel = 3;
87 static int init_timeout = 5;
88 static int max_requests = 50;
90 #define IBMVSCSI_VERSION "1.5.6"
92 MODULE_DESCRIPTION("IBM Virtual SCSI");
93 MODULE_AUTHOR("Dave Boutcher");
94 MODULE_LICENSE("GPL");
95 MODULE_VERSION(IBMVSCSI_VERSION);
97 module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
98 MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
99 module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
100 MODULE_PARM_DESC(max_channel, "Largest channel value");
101 module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
102 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
103 module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
104 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
106 /* ------------------------------------------------------------
107 * Routines for the event pool and event structs
110 * initialize_event_pool: - Allocates and initializes the event pool for a host
111 * @pool: event_pool to be initialized
112 * @size: Number of events in pool
113 * @hostdata: ibmvscsi_host_data who owns the event pool
115 * Returns zero on success.
117 static int initialize_event_pool(struct event_pool *pool,
118 int size, struct ibmvscsi_host_data *hostdata)
124 pool->events = kmalloc(pool->size * sizeof(*pool->events), GFP_KERNEL);
127 memset(pool->events, 0x00, pool->size * sizeof(*pool->events));
130 dma_alloc_coherent(hostdata->dev,
131 pool->size * sizeof(*pool->iu_storage),
133 if (!pool->iu_storage) {
138 for (i = 0; i < pool->size; ++i) {
139 struct srp_event_struct *evt = &pool->events[i];
140 memset(&evt->crq, 0x00, sizeof(evt->crq));
141 atomic_set(&evt->free, 1);
142 evt->crq.valid = 0x80;
143 evt->crq.IU_length = sizeof(*evt->xfer_iu);
144 evt->crq.IU_data_ptr = pool->iu_token +
145 sizeof(*evt->xfer_iu) * i;
146 evt->xfer_iu = pool->iu_storage + i;
147 evt->hostdata = hostdata;
154 * release_event_pool: - Frees memory of an event pool of a host
155 * @pool: event_pool to be released
156 * @hostdata: ibmvscsi_host_data who owns the even pool
158 * Returns zero on success.
160 static void release_event_pool(struct event_pool *pool,
161 struct ibmvscsi_host_data *hostdata)
164 for (i = 0; i < pool->size; ++i)
165 if (atomic_read(&pool->events[i].free) != 1)
169 "ibmvscsi: releasing event pool with %d "
170 "events still in use?\n", in_use);
172 dma_free_coherent(hostdata->dev,
173 pool->size * sizeof(*pool->iu_storage),
174 pool->iu_storage, pool->iu_token);
178 * valid_event_struct: - Determines if event is valid.
179 * @pool: event_pool that contains the event
180 * @evt: srp_event_struct to be checked for validity
182 * Returns zero if event is invalid, one otherwise.
184 static int valid_event_struct(struct event_pool *pool,
185 struct srp_event_struct *evt)
187 int index = evt - pool->events;
188 if (index < 0 || index >= pool->size) /* outside of bounds */
190 if (evt != pool->events + index) /* unaligned */
196 * ibmvscsi_free-event_struct: - Changes status of event to "free"
197 * @pool: event_pool that contains the event
198 * @evt: srp_event_struct to be modified
201 static void free_event_struct(struct event_pool *pool,
202 struct srp_event_struct *evt)
204 if (!valid_event_struct(pool, evt)) {
206 "ibmvscsi: Freeing invalid event_struct %p "
207 "(not in pool %p)\n", evt, pool->events);
210 if (atomic_inc_return(&evt->free) != 1) {
212 "ibmvscsi: Freeing event_struct %p "
213 "which is not in use!\n", evt);
219 * get_evt_struct: - Gets the next free event in pool
220 * @pool: event_pool that contains the events to be searched
222 * Returns the next event in "free" state, and NULL if none are free.
223 * Note that no synchronization is done here, we assume the host_lock
224 * will syncrhonze things.
226 static struct srp_event_struct *get_event_struct(struct event_pool *pool)
229 int poolsize = pool->size;
230 int offset = pool->next;
232 for (i = 0; i < poolsize; i++) {
233 offset = (offset + 1) % poolsize;
234 if (!atomic_dec_if_positive(&pool->events[offset].free)) {
236 return &pool->events[offset];
240 printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
245 * init_event_struct: Initialize fields in an event struct that are always
248 * @done: Routine to call when the event is responded to
249 * @format: SRP or MAD format
250 * @timeout: timeout value set in the CRQ
252 static void init_event_struct(struct srp_event_struct *evt_struct,
253 void (*done) (struct srp_event_struct *),
257 evt_struct->cmnd = NULL;
258 evt_struct->cmnd_done = NULL;
259 evt_struct->sync_srp = NULL;
260 evt_struct->crq.format = format;
261 evt_struct->crq.timeout = timeout;
262 evt_struct->done = done;
265 /* ------------------------------------------------------------
266 * Routines for receiving SCSI responses from the hosting partition
270 * set_srp_direction: Set the fields in the srp related to data
271 * direction and number of buffers based on the direction in
272 * the scsi_cmnd and the number of buffers
274 static void set_srp_direction(struct scsi_cmnd *cmd,
275 struct srp_cmd *srp_cmd,
282 if (cmd->sc_data_direction == DMA_TO_DEVICE)
283 srp_cmd->data_out_format = SRP_DIRECT_BUFFER;
285 srp_cmd->data_in_format = SRP_DIRECT_BUFFER;
287 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
288 srp_cmd->data_out_format = SRP_INDIRECT_BUFFER;
289 srp_cmd->data_out_count = numbuf;
291 srp_cmd->data_in_format = SRP_INDIRECT_BUFFER;
292 srp_cmd->data_in_count = numbuf;
298 * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
299 * @cmd: srp_cmd whose additional_data member will be unmapped
300 * @dev: device for which the memory is mapped
303 static void unmap_cmd_data(struct srp_cmd *cmd, struct device *dev)
307 if ((cmd->data_out_format == SRP_NO_BUFFER) &&
308 (cmd->data_in_format == SRP_NO_BUFFER))
310 else if ((cmd->data_out_format == SRP_DIRECT_BUFFER) ||
311 (cmd->data_in_format == SRP_DIRECT_BUFFER)) {
312 struct memory_descriptor *data =
313 (struct memory_descriptor *)cmd->additional_data;
314 dma_unmap_single(dev, data->virtual_address, data->length,
317 struct indirect_descriptor *indirect =
318 (struct indirect_descriptor *)cmd->additional_data;
319 int num_mapped = indirect->head.length /
320 sizeof(indirect->list[0]);
321 for (i = 0; i < num_mapped; ++i) {
322 struct memory_descriptor *data = &indirect->list[i];
323 dma_unmap_single(dev,
324 data->virtual_address,
325 data->length, DMA_BIDIRECTIONAL);
331 * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
332 * @cmd: Scsi_Cmnd with the scatterlist
333 * @srp_cmd: srp_cmd that contains the memory descriptor
334 * @dev: device for which to map dma memory
336 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
337 * Returns 1 on success.
339 static int map_sg_data(struct scsi_cmnd *cmd,
340 struct srp_cmd *srp_cmd, struct device *dev)
344 u64 total_length = 0;
345 struct scatterlist *sg = cmd->request_buffer;
346 struct memory_descriptor *data =
347 (struct memory_descriptor *)srp_cmd->additional_data;
348 struct indirect_descriptor *indirect =
349 (struct indirect_descriptor *)data;
351 sg_mapped = dma_map_sg(dev, sg, cmd->use_sg, DMA_BIDIRECTIONAL);
356 set_srp_direction(cmd, srp_cmd, sg_mapped);
358 /* special case; we can use a single direct descriptor */
359 if (sg_mapped == 1) {
360 data->virtual_address = sg_dma_address(&sg[0]);
361 data->length = sg_dma_len(&sg[0]);
362 data->memory_handle = 0;
366 if (sg_mapped > MAX_INDIRECT_BUFS) {
368 "ibmvscsi: More than %d mapped sg entries, got %d\n",
369 MAX_INDIRECT_BUFS, sg_mapped);
373 indirect->head.virtual_address = 0;
374 indirect->head.length = sg_mapped * sizeof(indirect->list[0]);
375 indirect->head.memory_handle = 0;
376 for (i = 0; i < sg_mapped; ++i) {
377 struct memory_descriptor *descr = &indirect->list[i];
378 struct scatterlist *sg_entry = &sg[i];
379 descr->virtual_address = sg_dma_address(sg_entry);
380 descr->length = sg_dma_len(sg_entry);
381 descr->memory_handle = 0;
382 total_length += sg_dma_len(sg_entry);
384 indirect->total_length = total_length;
390 * map_single_data: - Maps memory and initializes memory decriptor fields
391 * @cmd: struct scsi_cmnd with the memory to be mapped
392 * @srp_cmd: srp_cmd that contains the memory descriptor
393 * @dev: device for which to map dma memory
395 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
396 * Returns 1 on success.
398 static int map_single_data(struct scsi_cmnd *cmd,
399 struct srp_cmd *srp_cmd, struct device *dev)
401 struct memory_descriptor *data =
402 (struct memory_descriptor *)srp_cmd->additional_data;
404 data->virtual_address =
405 dma_map_single(dev, cmd->request_buffer,
406 cmd->request_bufflen,
408 if (dma_mapping_error(data->virtual_address)) {
410 "ibmvscsi: Unable to map request_buffer for command!\n");
413 data->length = cmd->request_bufflen;
414 data->memory_handle = 0;
416 set_srp_direction(cmd, srp_cmd, 1);
422 * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
423 * @cmd: struct scsi_cmnd with the memory to be mapped
424 * @srp_cmd: srp_cmd that contains the memory descriptor
425 * @dev: dma device for which to map dma memory
427 * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
428 * Returns 1 on success.
430 static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
431 struct srp_cmd *srp_cmd, struct device *dev)
433 switch (cmd->sc_data_direction) {
434 case DMA_FROM_DEVICE:
439 case DMA_BIDIRECTIONAL:
441 "ibmvscsi: Can't map DMA_BIDIRECTIONAL to read/write\n");
445 "ibmvscsi: Unknown data direction 0x%02x; can't map!\n",
446 cmd->sc_data_direction);
450 if (!cmd->request_buffer)
453 return map_sg_data(cmd, srp_cmd, dev);
454 return map_single_data(cmd, srp_cmd, dev);
457 /* ------------------------------------------------------------
458 * Routines for sending and receiving SRPs
461 * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
462 * @evt_struct: evt_struct to be sent
463 * @hostdata: ibmvscsi_host_data of host
465 * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
466 * Note that this routine assumes that host_lock is held for synchronization
468 static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
469 struct ibmvscsi_host_data *hostdata)
471 struct scsi_cmnd *cmnd;
472 u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
475 /* If we have exhausted our request limit, just fail this request.
476 * Note that there are rare cases involving driver generated requests
477 * (such as task management requests) that the mid layer may think we
478 * can handle more requests (can_queue) when we actually can't
480 if ((evt_struct->crq.format == VIOSRP_SRP_FORMAT) &&
481 (atomic_dec_if_positive(&hostdata->request_limit) < 0)) {
482 /* See if the adapter is disabled */
483 if (atomic_read(&hostdata->request_limit) < 0)
487 "ibmvscsi: Warning, request_limit exceeded\n");
488 unmap_cmd_data(&evt_struct->iu.srp.cmd,
490 free_event_struct(&hostdata->pool, evt_struct);
491 return SCSI_MLQUEUE_HOST_BUSY;
494 /* Copy the IU into the transfer area */
495 *evt_struct->xfer_iu = evt_struct->iu;
496 evt_struct->xfer_iu->srp.generic.tag = (u64)evt_struct;
498 /* Add this to the sent list. We need to do this
499 * before we actually send
500 * in case it comes back REALLY fast
502 list_add_tail(&evt_struct->list, &hostdata->sent);
505 ibmvscsi_send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
506 list_del(&evt_struct->list);
508 printk(KERN_ERR "ibmvscsi: failed to send event struct rc %d\n",
516 unmap_cmd_data(&evt_struct->iu.srp.cmd, hostdata->dev);
518 if ((cmnd = evt_struct->cmnd) != NULL) {
519 cmnd->result = DID_ERROR << 16;
520 evt_struct->cmnd_done(cmnd);
521 } else if (evt_struct->done)
522 evt_struct->done(evt_struct);
524 free_event_struct(&hostdata->pool, evt_struct);
529 * handle_cmd_rsp: - Handle responses from commands
530 * @evt_struct: srp_event_struct to be handled
532 * Used as a callback by when sending scsi cmds.
533 * Gets called by ibmvscsi_handle_crq()
535 static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
537 struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
538 struct scsi_cmnd *cmnd = evt_struct->cmnd;
540 if (unlikely(rsp->type != SRP_RSP_TYPE)) {
541 if (printk_ratelimit())
543 "ibmvscsi: bad SRP RSP type %d\n",
548 cmnd->result = rsp->status;
549 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
550 memcpy(cmnd->sense_buffer,
551 rsp->sense_and_response_data,
552 rsp->sense_data_list_length);
553 unmap_cmd_data(&evt_struct->iu.srp.cmd,
554 evt_struct->hostdata->dev);
557 cmnd->resid = rsp->data_out_residual_count;
558 else if (rsp->diover)
559 cmnd->resid = rsp->data_in_residual_count;
562 if (evt_struct->cmnd_done)
563 evt_struct->cmnd_done(cmnd);
567 * lun_from_dev: - Returns the lun of the scsi device
568 * @dev: struct scsi_device
571 static inline u16 lun_from_dev(struct scsi_device *dev)
573 return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
577 * ibmvscsi_queue: - The queuecommand function of the scsi template
578 * @cmd: struct scsi_cmnd to be executed
579 * @done: Callback function to be called when cmd is completed
581 static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
582 void (*done) (struct scsi_cmnd *))
584 struct srp_cmd *srp_cmd;
585 struct srp_event_struct *evt_struct;
586 struct ibmvscsi_host_data *hostdata =
587 (struct ibmvscsi_host_data *)&cmnd->device->host->hostdata;
588 u16 lun = lun_from_dev(cmnd->device);
590 evt_struct = get_event_struct(&hostdata->pool);
592 return SCSI_MLQUEUE_HOST_BUSY;
594 init_event_struct(evt_struct,
599 evt_struct->cmnd = cmnd;
600 evt_struct->cmnd_done = done;
602 /* Set up the actual SRP IU */
603 srp_cmd = &evt_struct->iu.srp.cmd;
604 memset(srp_cmd, 0x00, sizeof(*srp_cmd));
605 srp_cmd->type = SRP_CMD_TYPE;
606 memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(cmnd->cmnd));
607 srp_cmd->lun = ((u64) lun) << 48;
609 if (!map_data_for_srp_cmd(cmnd, srp_cmd, hostdata->dev)) {
610 printk(KERN_ERR "ibmvscsi: couldn't convert cmd to srp_cmd\n");
611 free_event_struct(&hostdata->pool, evt_struct);
612 return SCSI_MLQUEUE_HOST_BUSY;
615 /* Fix up dma address of the buffer itself */
616 if ((srp_cmd->data_out_format == SRP_INDIRECT_BUFFER) ||
617 (srp_cmd->data_in_format == SRP_INDIRECT_BUFFER)) {
618 struct indirect_descriptor *indirect =
619 (struct indirect_descriptor *)srp_cmd->additional_data;
620 indirect->head.virtual_address = evt_struct->crq.IU_data_ptr +
621 offsetof(struct srp_cmd, additional_data) +
622 offsetof(struct indirect_descriptor, list);
625 return ibmvscsi_send_srp_event(evt_struct, hostdata);
628 /* ------------------------------------------------------------
629 * Routines for driver initialization
632 * adapter_info_rsp: - Handle response to MAD adapter info request
633 * @evt_struct: srp_event_struct with the response
635 * Used as a "done" callback by when sending adapter_info. Gets called
636 * by ibmvscsi_handle_crq()
638 static void adapter_info_rsp(struct srp_event_struct *evt_struct)
640 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
641 dma_unmap_single(hostdata->dev,
642 evt_struct->iu.mad.adapter_info.buffer,
643 evt_struct->iu.mad.adapter_info.common.length,
646 if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
647 printk("ibmvscsi: error %d getting adapter info\n",
648 evt_struct->xfer_iu->mad.adapter_info.common.status);
650 printk("ibmvscsi: host srp version: %s, "
651 "host partition %s (%d), OS %d, max io %u\n",
652 hostdata->madapter_info.srp_version,
653 hostdata->madapter_info.partition_name,
654 hostdata->madapter_info.partition_number,
655 hostdata->madapter_info.os_type,
656 hostdata->madapter_info.port_max_txu[0]);
658 if (hostdata->madapter_info.port_max_txu[0])
659 hostdata->host->max_sectors =
660 hostdata->madapter_info.port_max_txu[0] >> 9;
665 * send_mad_adapter_info: - Sends the mad adapter info request
666 * and stores the result so it can be retrieved with
667 * sysfs. We COULD consider causing a failure if the
668 * returned SRP version doesn't match ours.
669 * @hostdata: ibmvscsi_host_data of host
671 * Returns zero if successful.
673 static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
675 struct viosrp_adapter_info *req;
676 struct srp_event_struct *evt_struct;
678 evt_struct = get_event_struct(&hostdata->pool);
680 printk(KERN_ERR "ibmvscsi: couldn't allocate an event "
681 "for ADAPTER_INFO_REQ!\n");
685 init_event_struct(evt_struct,
690 req = &evt_struct->iu.mad.adapter_info;
691 memset(req, 0x00, sizeof(*req));
693 req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
694 req->common.length = sizeof(hostdata->madapter_info);
695 req->buffer = dma_map_single(hostdata->dev,
696 &hostdata->madapter_info,
697 sizeof(hostdata->madapter_info),
700 if (dma_mapping_error(req->buffer)) {
702 "ibmvscsi: Unable to map request_buffer "
703 "for adapter_info!\n");
704 free_event_struct(&hostdata->pool, evt_struct);
708 if (ibmvscsi_send_srp_event(evt_struct, hostdata))
709 printk(KERN_ERR "ibmvscsi: couldn't send ADAPTER_INFO_REQ!\n");
713 * login_rsp: - Handle response to SRP login request
714 * @evt_struct: srp_event_struct with the response
716 * Used as a "done" callback by when sending srp_login. Gets called
717 * by ibmvscsi_handle_crq()
719 static void login_rsp(struct srp_event_struct *evt_struct)
721 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
722 switch (evt_struct->xfer_iu->srp.generic.type) {
723 case SRP_LOGIN_RSP_TYPE: /* it worked! */
725 case SRP_LOGIN_REJ_TYPE: /* refused! */
726 printk(KERN_INFO "ibmvscsi: SRP_LOGIN_REQ rejected\n");
728 atomic_set(&hostdata->request_limit, -1);
732 "ibmvscsi: Invalid login response typecode 0x%02x!\n",
733 evt_struct->xfer_iu->srp.generic.type);
735 atomic_set(&hostdata->request_limit, -1);
739 printk(KERN_INFO "ibmvscsi: SRP_LOGIN succeeded\n");
741 if (evt_struct->xfer_iu->srp.login_rsp.request_limit_delta >
743 evt_struct->xfer_iu->srp.login_rsp.request_limit_delta =
746 /* Now we know what the real request-limit is */
747 atomic_set(&hostdata->request_limit,
748 evt_struct->xfer_iu->srp.login_rsp.request_limit_delta);
750 hostdata->host->can_queue =
751 evt_struct->xfer_iu->srp.login_rsp.request_limit_delta - 2;
753 if (hostdata->host->can_queue < 1) {
754 printk(KERN_ERR "ibmvscsi: Invalid request_limit_delta\n");
758 send_mad_adapter_info(hostdata);
763 * send_srp_login: - Sends the srp login
764 * @hostdata: ibmvscsi_host_data of host
766 * Returns zero if successful.
768 static int send_srp_login(struct ibmvscsi_host_data *hostdata)
772 struct srp_login_req *login;
773 struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
776 "ibmvscsi: couldn't allocate an event for login req!\n");
780 init_event_struct(evt_struct,
785 login = &evt_struct->iu.srp.login_req;
786 login->type = SRP_LOGIN_REQ_TYPE;
787 login->max_requested_initiator_to_target_iulen = sizeof(union srp_iu);
788 login->required_buffer_formats = 0x0006;
790 /* Start out with a request limit of 1, since this is negotiated in
791 * the login request we are just sending
793 atomic_set(&hostdata->request_limit, 1);
795 spin_lock_irqsave(hostdata->host->host_lock, flags);
796 rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
797 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
802 * sync_completion: Signal that a synchronous command has completed
803 * Note that after returning from this call, the evt_struct is freed.
804 * the caller waiting on this completion shouldn't touch the evt_struct
807 static void sync_completion(struct srp_event_struct *evt_struct)
809 /* copy the response back */
810 if (evt_struct->sync_srp)
811 *evt_struct->sync_srp = *evt_struct->xfer_iu;
813 complete(&evt_struct->comp);
817 * ibmvscsi_abort: Abort a command...from scsi host template
818 * send this over to the server and wait synchronously for the response
820 static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
822 struct ibmvscsi_host_data *hostdata =
823 (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
824 struct srp_tsk_mgmt *tsk_mgmt;
825 struct srp_event_struct *evt;
826 struct srp_event_struct *tmp_evt, *found_evt;
827 union viosrp_iu srp_rsp;
829 u16 lun = lun_from_dev(cmd->device);
831 /* First, find this command in our sent list so we can figure
832 * out the correct tag
835 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
836 if (tmp_evt->cmnd == cmd) {
845 evt = get_event_struct(&hostdata->pool);
847 printk(KERN_ERR "ibmvscsi: failed to allocate abort event\n");
851 init_event_struct(evt,
856 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
858 /* Set up an abort SRP command */
859 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
860 tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
861 tsk_mgmt->lun = ((u64) lun) << 48;
862 tsk_mgmt->task_mgmt_flags = 0x01; /* ABORT TASK */
863 tsk_mgmt->managed_task_tag = (u64) found_evt;
865 printk(KERN_INFO "ibmvscsi: aborting command. lun 0x%lx, tag 0x%lx\n",
866 tsk_mgmt->lun, tsk_mgmt->managed_task_tag);
868 evt->sync_srp = &srp_rsp;
869 init_completion(&evt->comp);
870 if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
871 printk(KERN_ERR "ibmvscsi: failed to send abort() event\n");
875 wait_for_completion(&evt->comp);
877 /* make sure we got a good response */
878 if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
879 if (printk_ratelimit())
881 "ibmvscsi: abort bad SRP RSP type %d\n",
882 srp_rsp.srp.generic.type);
886 if (srp_rsp.srp.rsp.rspvalid)
887 rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
889 rsp_rc = srp_rsp.srp.rsp.status;
892 if (printk_ratelimit())
894 "ibmvscsi: abort code %d for task tag 0x%lx\n",
896 tsk_mgmt->managed_task_tag);
900 /* Because we dropped the spinlock above, it's possible
901 * The event is no longer in our list. Make sure it didn't
902 * complete while we were aborting
905 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
906 if (tmp_evt->cmnd == cmd) {
912 if (found_evt == NULL) {
914 "ibmvscsi: aborted task tag 0x%lx completed\n",
915 tsk_mgmt->managed_task_tag);
920 "ibmvscsi: successfully aborted task tag 0x%lx\n",
921 tsk_mgmt->managed_task_tag);
923 cmd->result = (DID_ABORT << 16);
924 list_del(&found_evt->list);
925 unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt->hostdata->dev);
926 free_event_struct(&found_evt->hostdata->pool, found_evt);
927 atomic_inc(&hostdata->request_limit);
932 * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
933 * template send this over to the server and wait synchronously for the
936 static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
938 struct ibmvscsi_host_data *hostdata =
939 (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
941 struct srp_tsk_mgmt *tsk_mgmt;
942 struct srp_event_struct *evt;
943 struct srp_event_struct *tmp_evt, *pos;
944 union viosrp_iu srp_rsp;
946 u16 lun = lun_from_dev(cmd->device);
948 evt = get_event_struct(&hostdata->pool);
950 printk(KERN_ERR "ibmvscsi: failed to allocate reset event\n");
954 init_event_struct(evt,
959 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
961 /* Set up a lun reset SRP command */
962 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
963 tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
964 tsk_mgmt->lun = ((u64) lun) << 48;
965 tsk_mgmt->task_mgmt_flags = 0x08; /* LUN RESET */
967 printk(KERN_INFO "ibmvscsi: resetting device. lun 0x%lx\n",
970 evt->sync_srp = &srp_rsp;
971 init_completion(&evt->comp);
972 if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
973 printk(KERN_ERR "ibmvscsi: failed to send reset event\n");
977 wait_for_completion(&evt->comp);
979 /* make sure we got a good response */
980 if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
981 if (printk_ratelimit())
983 "ibmvscsi: reset bad SRP RSP type %d\n",
984 srp_rsp.srp.generic.type);
988 if (srp_rsp.srp.rsp.rspvalid)
989 rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
991 rsp_rc = srp_rsp.srp.rsp.status;
994 if (printk_ratelimit())
996 "ibmvscsi: reset code %d for task tag 0x%lx\n",
998 tsk_mgmt->managed_task_tag);
1002 /* We need to find all commands for this LUN that have not yet been
1003 * responded to, and fail them with DID_RESET
1005 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1006 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1008 tmp_evt->cmnd->result = (DID_RESET << 16);
1009 list_del(&tmp_evt->list);
1010 unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt->hostdata->dev);
1011 free_event_struct(&tmp_evt->hostdata->pool,
1013 atomic_inc(&hostdata->request_limit);
1014 if (tmp_evt->cmnd_done)
1015 tmp_evt->cmnd_done(tmp_evt->cmnd);
1016 else if (tmp_evt->done)
1017 tmp_evt->done(tmp_evt);
1024 * purge_requests: Our virtual adapter just shut down. purge any sent requests
1025 * @hostdata: the adapter
1027 static void purge_requests(struct ibmvscsi_host_data *hostdata)
1029 struct srp_event_struct *tmp_evt, *pos;
1030 unsigned long flags;
1032 spin_lock_irqsave(hostdata->host->host_lock, flags);
1033 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1034 list_del(&tmp_evt->list);
1035 if (tmp_evt->cmnd) {
1036 tmp_evt->cmnd->result = (DID_ERROR << 16);
1037 unmap_cmd_data(&tmp_evt->iu.srp.cmd,
1038 tmp_evt->hostdata->dev);
1039 if (tmp_evt->cmnd_done)
1040 tmp_evt->cmnd_done(tmp_evt->cmnd);
1042 if (tmp_evt->done) {
1043 tmp_evt->done(tmp_evt);
1046 free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
1048 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1052 * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1053 * @crq: Command/Response queue
1054 * @hostdata: ibmvscsi_host_data of host
1057 void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1058 struct ibmvscsi_host_data *hostdata)
1060 unsigned long flags;
1061 struct srp_event_struct *evt_struct =
1062 (struct srp_event_struct *)crq->IU_data_ptr;
1063 switch (crq->valid) {
1064 case 0xC0: /* initialization */
1065 switch (crq->format) {
1066 case 0x01: /* Initialization message */
1067 printk(KERN_INFO "ibmvscsi: partner initialized\n");
1068 /* Send back a response */
1069 if (ibmvscsi_send_crq(hostdata,
1070 0xC002000000000000LL, 0) == 0) {
1072 send_srp_login(hostdata);
1075 "ibmvscsi: Unable to send init rsp\n");
1079 case 0x02: /* Initialization response */
1081 "ibmvscsi: partner initialization complete\n");
1084 send_srp_login(hostdata);
1087 printk(KERN_ERR "ibmvscsi: unknown crq message type\n");
1090 case 0xFF: /* Hypervisor telling us the connection is closed */
1091 printk(KERN_INFO "ibmvscsi: Virtual adapter failed!\n");
1093 atomic_set(&hostdata->request_limit, -1);
1094 purge_requests(hostdata);
1095 ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
1097 case 0x80: /* real payload */
1101 "ibmvscsi: got an invalid message type 0x%02x\n",
1106 /* The only kind of payload CRQs we should get are responses to
1107 * things we send. Make sure this response is to something we
1110 if (!valid_event_struct(&hostdata->pool, evt_struct)) {
1112 "ibmvscsi: returned correlation_token 0x%p is invalid!\n",
1113 (void *)crq->IU_data_ptr);
1117 if (atomic_read(&evt_struct->free)) {
1119 "ibmvscsi: received duplicate correlation_token 0x%p!\n",
1120 (void *)crq->IU_data_ptr);
1124 if (crq->format == VIOSRP_SRP_FORMAT)
1125 atomic_add(evt_struct->xfer_iu->srp.rsp.request_limit_delta,
1126 &hostdata->request_limit);
1128 if (evt_struct->done)
1129 evt_struct->done(evt_struct);
1132 "ibmvscsi: returned done() is NULL; not running it!\n");
1135 * Lock the host_lock before messing with these structures, since we
1136 * are running in a task context
1138 spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1139 list_del(&evt_struct->list);
1140 free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1141 spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1145 * ibmvscsi_get_host_config: Send the command to the server to get host
1146 * configuration data. The data is opaque to us.
1148 static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1149 unsigned char *buffer, int length)
1151 struct viosrp_host_config *host_config;
1152 struct srp_event_struct *evt_struct;
1155 evt_struct = get_event_struct(&hostdata->pool);
1158 "ibmvscsi: could't allocate event for HOST_CONFIG!\n");
1162 init_event_struct(evt_struct,
1167 host_config = &evt_struct->iu.mad.host_config;
1169 /* Set up a lun reset SRP command */
1170 memset(host_config, 0x00, sizeof(*host_config));
1171 host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1172 host_config->common.length = length;
1173 host_config->buffer = dma_map_single(hostdata->dev, buffer, length,
1176 if (dma_mapping_error(host_config->buffer)) {
1178 "ibmvscsi: dma_mapping error " "getting host config\n");
1179 free_event_struct(&hostdata->pool, evt_struct);
1183 init_completion(&evt_struct->comp);
1184 rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
1186 wait_for_completion(&evt_struct->comp);
1187 dma_unmap_single(hostdata->dev, host_config->buffer,
1188 length, DMA_BIDIRECTIONAL);
1194 /* ------------------------------------------------------------
1197 static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
1199 struct Scsi_Host *shost = class_to_shost(class_dev);
1200 struct ibmvscsi_host_data *hostdata =
1201 (struct ibmvscsi_host_data *)shost->hostdata;
1204 len = snprintf(buf, PAGE_SIZE, "%s\n",
1205 hostdata->madapter_info.srp_version);
1209 static struct class_device_attribute ibmvscsi_host_srp_version = {
1211 .name = "srp_version",
1214 .show = show_host_srp_version,
1217 static ssize_t show_host_partition_name(struct class_device *class_dev,
1220 struct Scsi_Host *shost = class_to_shost(class_dev);
1221 struct ibmvscsi_host_data *hostdata =
1222 (struct ibmvscsi_host_data *)shost->hostdata;
1225 len = snprintf(buf, PAGE_SIZE, "%s\n",
1226 hostdata->madapter_info.partition_name);
1230 static struct class_device_attribute ibmvscsi_host_partition_name = {
1232 .name = "partition_name",
1235 .show = show_host_partition_name,
1238 static ssize_t show_host_partition_number(struct class_device *class_dev,
1241 struct Scsi_Host *shost = class_to_shost(class_dev);
1242 struct ibmvscsi_host_data *hostdata =
1243 (struct ibmvscsi_host_data *)shost->hostdata;
1246 len = snprintf(buf, PAGE_SIZE, "%d\n",
1247 hostdata->madapter_info.partition_number);
1251 static struct class_device_attribute ibmvscsi_host_partition_number = {
1253 .name = "partition_number",
1256 .show = show_host_partition_number,
1259 static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
1261 struct Scsi_Host *shost = class_to_shost(class_dev);
1262 struct ibmvscsi_host_data *hostdata =
1263 (struct ibmvscsi_host_data *)shost->hostdata;
1266 len = snprintf(buf, PAGE_SIZE, "%d\n",
1267 hostdata->madapter_info.mad_version);
1271 static struct class_device_attribute ibmvscsi_host_mad_version = {
1273 .name = "mad_version",
1276 .show = show_host_mad_version,
1279 static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
1281 struct Scsi_Host *shost = class_to_shost(class_dev);
1282 struct ibmvscsi_host_data *hostdata =
1283 (struct ibmvscsi_host_data *)shost->hostdata;
1286 len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
1290 static struct class_device_attribute ibmvscsi_host_os_type = {
1295 .show = show_host_os_type,
1298 static ssize_t show_host_config(struct class_device *class_dev, char *buf)
1300 struct Scsi_Host *shost = class_to_shost(class_dev);
1301 struct ibmvscsi_host_data *hostdata =
1302 (struct ibmvscsi_host_data *)shost->hostdata;
1304 /* returns null-terminated host config data */
1305 if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
1311 static struct class_device_attribute ibmvscsi_host_config = {
1316 .show = show_host_config,
1319 static struct class_device_attribute *ibmvscsi_attrs[] = {
1320 &ibmvscsi_host_srp_version,
1321 &ibmvscsi_host_partition_name,
1322 &ibmvscsi_host_partition_number,
1323 &ibmvscsi_host_mad_version,
1324 &ibmvscsi_host_os_type,
1325 &ibmvscsi_host_config,
1329 /* ------------------------------------------------------------
1330 * SCSI driver registration
1332 static struct scsi_host_template driver_template = {
1333 .module = THIS_MODULE,
1334 .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
1335 .proc_name = "ibmvscsi",
1336 .queuecommand = ibmvscsi_queuecommand,
1337 .eh_abort_handler = ibmvscsi_eh_abort_handler,
1338 .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
1340 .can_queue = 1, /* Updated after SRP_LOGIN */
1342 .sg_tablesize = MAX_INDIRECT_BUFS,
1343 .use_clustering = ENABLE_CLUSTERING,
1344 .shost_attrs = ibmvscsi_attrs,
1348 * Called by bus code for each adapter
1350 static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1352 struct ibmvscsi_host_data *hostdata;
1353 struct Scsi_Host *host;
1354 struct device *dev = &vdev->dev;
1355 unsigned long wait_switch = 0;
1357 vdev->dev.driver_data = NULL;
1359 host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
1361 printk(KERN_ERR "ibmvscsi: couldn't allocate host data\n");
1362 goto scsi_host_alloc_failed;
1365 hostdata = (struct ibmvscsi_host_data *)host->hostdata;
1366 memset(hostdata, 0x00, sizeof(*hostdata));
1367 INIT_LIST_HEAD(&hostdata->sent);
1368 hostdata->host = host;
1369 hostdata->dev = dev;
1370 atomic_set(&hostdata->request_limit, -1);
1371 hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
1373 if (ibmvscsi_init_crq_queue(&hostdata->queue, hostdata,
1374 max_requests) != 0) {
1375 printk(KERN_ERR "ibmvscsi: couldn't initialize crq\n");
1376 goto init_crq_failed;
1378 if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
1379 printk(KERN_ERR "ibmvscsi: couldn't initialize event pool\n");
1380 goto init_pool_failed;
1384 host->max_id = max_id;
1385 host->max_channel = max_channel;
1387 if (scsi_add_host(hostdata->host, hostdata->dev))
1388 goto add_host_failed;
1390 /* Try to send an initialization message. Note that this is allowed
1391 * to fail if the other end is not acive. In that case we don't
1394 if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0) {
1396 * Wait around max init_timeout secs for the adapter to finish
1397 * initializing. When we are done initializing, we will have a
1398 * valid request_limit. We don't want Linux scanning before
1401 for (wait_switch = jiffies + (init_timeout * HZ);
1402 time_before(jiffies, wait_switch) &&
1403 atomic_read(&hostdata->request_limit) < 2;) {
1408 /* if we now have a valid request_limit, initiate a scan */
1409 if (atomic_read(&hostdata->request_limit) > 0)
1410 scsi_scan_host(host);
1413 vdev->dev.driver_data = hostdata;
1417 release_event_pool(&hostdata->pool, hostdata);
1419 ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_requests);
1421 scsi_host_put(host);
1422 scsi_host_alloc_failed:
1426 static int ibmvscsi_remove(struct vio_dev *vdev)
1428 struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
1429 release_event_pool(&hostdata->pool, hostdata);
1430 ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
1433 scsi_remove_host(hostdata->host);
1434 scsi_host_put(hostdata->host);
1440 * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
1443 static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
1444 {"vscsi", "IBM,v-scsi"},
1448 MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
1449 static struct vio_driver ibmvscsi_driver = {
1451 .id_table = ibmvscsi_device_table,
1452 .probe = ibmvscsi_probe,
1453 .remove = ibmvscsi_remove
1456 int __init ibmvscsi_module_init(void)
1458 return vio_register_driver(&ibmvscsi_driver);
1461 void __exit ibmvscsi_module_exit(void)
1463 vio_unregister_driver(&ibmvscsi_driver);
1466 module_init(ibmvscsi_module_init);
1467 module_exit(ibmvscsi_module_exit);