ibmvscsi.c 60.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* ------------------------------------------------------------
 * ibmvscsi.c
 * (C) Copyright IBM Corporation 1994, 2004
 * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
 *          Santiago Leon (santil@us.ibm.com)
 *          Dave Boutcher (sleddog@us.ibm.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * ------------------------------------------------------------
 * Emulation of a SCSI host adapter for Virtual I/O devices
 *
 * This driver supports the SCSI adapter implemented by the IBM
 * Power5 firmware.  That SCSI adapter is not a physical adapter,
 * but allows Linux SCSI peripheral drivers to directly
 * access devices in another logical partition on the physical system.
 *
 * The virtual adapter(s) are present in the open firmware device
 * tree just like real adapters.
 *
 * One of the capabilities provided on these systems is the ability
 * to DMA between partitions.  The architecture states that for VSCSI,
 * the server side is allowed to DMA to and from the client.  The client
 * is never trusted to DMA to or from the server directly.
 *
 * Messages are sent between partitions on a "Command/Response Queue" 
 * (CRQ), which is just a buffer of 16 byte entries in the receiver's 
 * Senders cannot access the buffer directly, but send messages by
 * making a hypervisor call and passing in the 16 bytes.  The hypervisor
 * puts the message in the next 16 byte space in round-robbin fashion,
 * turns on the high order bit of the message (the valid bit), and 
 * generates an interrupt to the receiver (if interrupts are turned on.) 
 * The receiver just turns off the valid bit when they have copied out
 * the message.
 *
 * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
 * (IU) (as defined in the T10 standard available at www.t10.org), gets 
 * a DMA address for the message, and sends it to the server as the
 * payload of a CRQ message.  The server DMAs the SRP IU and processes it,
 * including doing any additional data transfers.  When it is done, it
 * DMAs the SRP response back to the same address as the request came from,
 * and sends a CRQ message back to inform the client that the request has
 * completed.
 *
 * Note that some of the underlying infrastructure is different between
 * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
 * the older iSeries hypervisor models.  To support both, some low level
 * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
 * The Makefile should pick one, not two, not zero, of these.
 *
 * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
 * interfaces.  It would be really nice to abstract this above an RDMA
 * layer.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
73
#include <linux/of.h>
74
#include <asm/firmware.h>
L
Linus Torvalds 已提交
75 76 77 78 79
#include <asm/vio.h>
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_device.h>
80
#include <scsi/scsi_transport_srp.h>
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89
#include "ibmvscsi.h"

/* The values below are somewhat arbitrary default values, but 
 * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
 * Note that there are 3 bits of channel value, 6 bits of id, and
 * 5 bits of LUN.
 */
static int max_id = 64;
static int max_channel = 3;
90 91 92 93 94
static int init_timeout = 300;
static int login_timeout = 60;
static int info_timeout = 30;
static int abort_timeout = 60;
static int reset_timeout = 60;
95
static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
96
static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
97
static int fast_fail = 1;
98
static int client_reserve = 1;
L
Linus Torvalds 已提交
99

100 101
static struct scsi_transport_template *ibmvscsi_transport_template;

102
#define IBMVSCSI_VERSION "1.5.8"
L
Linus Torvalds 已提交
103

104 105
static struct ibmvscsi_ops *ibmvscsi_ops;

L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116
MODULE_DESCRIPTION("IBM Virtual SCSI");
MODULE_AUTHOR("Dave Boutcher");
MODULE_LICENSE("GPL");
MODULE_VERSION(IBMVSCSI_VERSION);

module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(max_channel, "Largest channel value");
module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
117
module_param_named(max_requests, max_requests, int, S_IRUGO);
L
Linus Torvalds 已提交
118
MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
119 120
module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
121 122
module_param_named(client_reserve, client_reserve, int, S_IRUGO );
MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

/* ------------------------------------------------------------
 * Routines for the event pool and event structs
 */
/**
 * initialize_event_pool: - Allocates and initializes the event pool for a host
 * @pool:	event_pool to be initialized
 * @size:	Number of events in pool
 * @hostdata:	ibmvscsi_host_data who owns the event pool
 *
 * Returns zero on success.
*/
static int initialize_event_pool(struct event_pool *pool,
				 int size, struct ibmvscsi_host_data *hostdata)
{
	int i;

	pool->size = size;
	pool->next = 0;
142
	pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	if (!pool->events)
		return -ENOMEM;

	pool->iu_storage =
	    dma_alloc_coherent(hostdata->dev,
			       pool->size * sizeof(*pool->iu_storage),
			       &pool->iu_token, 0);
	if (!pool->iu_storage) {
		kfree(pool->events);
		return -ENOMEM;
	}

	for (i = 0; i < pool->size; ++i) {
		struct srp_event_struct *evt = &pool->events[i];
		memset(&evt->crq, 0x00, sizeof(evt->crq));
		atomic_set(&evt->free, 1);
		evt->crq.valid = 0x80;
		evt->crq.IU_length = sizeof(*evt->xfer_iu);
		evt->crq.IU_data_ptr = pool->iu_token + 
			sizeof(*evt->xfer_iu) * i;
		evt->xfer_iu = pool->iu_storage + i;
		evt->hostdata = hostdata;
165 166
		evt->ext_list = NULL;
		evt->ext_list_token = 0;
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	}

	return 0;
}

/**
 * release_event_pool: - Frees memory of an event pool of a host
 * @pool:	event_pool to be released
 * @hostdata:	ibmvscsi_host_data who owns the even pool
 *
 * Returns zero on success.
*/
static void release_event_pool(struct event_pool *pool,
			       struct ibmvscsi_host_data *hostdata)
{
	int i, in_use = 0;
183
	for (i = 0; i < pool->size; ++i) {
L
Linus Torvalds 已提交
184 185
		if (atomic_read(&pool->events[i].free) != 1)
			++in_use;
186 187
		if (pool->events[i].ext_list) {
			dma_free_coherent(hostdata->dev,
188
				  SG_ALL * sizeof(struct srp_direct_buf),
189 190 191 192
				  pool->events[i].ext_list,
				  pool->events[i].ext_list_token);
		}
	}
L
Linus Torvalds 已提交
193
	if (in_use)
194 195
		dev_warn(hostdata->dev, "releasing event pool with %d "
			 "events still in use?\n", in_use);
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	kfree(pool->events);
	dma_free_coherent(hostdata->dev,
			  pool->size * sizeof(*pool->iu_storage),
			  pool->iu_storage, pool->iu_token);
}

/**
 * valid_event_struct: - Determines if event is valid.
 * @pool:	event_pool that contains the event
 * @evt:	srp_event_struct to be checked for validity
 *
 * Returns zero if event is invalid, one otherwise.
*/
static int valid_event_struct(struct event_pool *pool,
				struct srp_event_struct *evt)
{
	int index = evt - pool->events;
	if (index < 0 || index >= pool->size)	/* outside of bounds */
		return 0;
	if (evt != pool->events + index)	/* unaligned */
		return 0;
	return 1;
}

/**
 * ibmvscsi_free-event_struct: - Changes status of event to "free"
 * @pool:	event_pool that contains the event
 * @evt:	srp_event_struct to be modified
 *
*/
static void free_event_struct(struct event_pool *pool,
				       struct srp_event_struct *evt)
{
	if (!valid_event_struct(pool, evt)) {
230 231
		dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
			"(not in pool %p)\n", evt, pool->events);
L
Linus Torvalds 已提交
232 233 234
		return;
	}
	if (atomic_inc_return(&evt->free) != 1) {
235 236
		dev_err(evt->hostdata->dev, "Freeing event_struct %p "
			"which is not in use!\n", evt);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
		return;
	}
}

/**
 * get_evt_struct: - Gets the next free event in pool
 * @pool:	event_pool that contains the events to be searched
 *
 * Returns the next event in "free" state, and NULL if none are free.
 * Note that no synchronization is done here, we assume the host_lock
 * will syncrhonze things.
*/
static struct srp_event_struct *get_event_struct(struct event_pool *pool)
{
	int i;
	int poolsize = pool->size;
	int offset = pool->next;

	for (i = 0; i < poolsize; i++) {
		offset = (offset + 1) % poolsize;
		if (!atomic_dec_if_positive(&pool->events[offset].free)) {
			pool->next = offset;
			return &pool->events[offset];
		}
	}

	printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
	return NULL;
}

/**
 * init_event_struct: Initialize fields in an event struct that are always 
 *                    required.
 * @evt:        The event
 * @done:       Routine to call when the event is responded to
 * @format:     SRP or MAD format
 * @timeout:    timeout value set in the CRQ
 */
static void init_event_struct(struct srp_event_struct *evt_struct,
			      void (*done) (struct srp_event_struct *),
			      u8 format,
			      int timeout)
{
	evt_struct->cmnd = NULL;
	evt_struct->cmnd_done = NULL;
	evt_struct->sync_srp = NULL;
	evt_struct->crq.format = format;
	evt_struct->crq.timeout = timeout;
	evt_struct->done = done;
}

/* ------------------------------------------------------------
 * Routines for receiving SCSI responses from the hosting partition
 */

/**
 * set_srp_direction: Set the fields in the srp related to data
 *     direction and number of buffers based on the direction in
 *     the scsi_cmnd and the number of buffers
 */
static void set_srp_direction(struct scsi_cmnd *cmd,
			      struct srp_cmd *srp_cmd, 
			      int numbuf)
{
301 302
	u8 fmt;

L
Linus Torvalds 已提交
303 304 305
	if (numbuf == 0)
		return;
	
306 307 308 309 310 311
	if (numbuf == 1)
		fmt = SRP_DATA_DESC_DIRECT;
	else {
		fmt = SRP_DATA_DESC_INDIRECT;
		numbuf = min(numbuf, MAX_INDIRECT_BUFS);

L
Linus Torvalds 已提交
312
		if (cmd->sc_data_direction == DMA_TO_DEVICE)
313 314 315
			srp_cmd->data_out_desc_cnt = numbuf;
		else
			srp_cmd->data_in_desc_cnt = numbuf;
L
Linus Torvalds 已提交
316
	}
317 318 319 320 321

	if (cmd->sc_data_direction == DMA_TO_DEVICE)
		srp_cmd->buf_fmt = fmt << 4;
	else
		srp_cmd->buf_fmt = fmt;
L
Linus Torvalds 已提交
322 323
}

324
static void unmap_sg_list(int num_entries,
325
		struct device *dev,
326 327
		struct srp_direct_buf *md)
{
328 329
	int i;

330 331
	for (i = 0; i < num_entries; ++i)
		dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
332 333
}

L
Linus Torvalds 已提交
334 335 336 337 338 339
/**
 * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
 * @cmd:	srp_cmd whose additional_data member will be unmapped
 * @dev:	device for which the memory is mapped
 *
*/
340 341 342
static void unmap_cmd_data(struct srp_cmd *cmd,
			   struct srp_event_struct *evt_struct,
			   struct device *dev)
L
Linus Torvalds 已提交
343
{
344 345 346 347 348 349
	u8 out_fmt, in_fmt;

	out_fmt = cmd->buf_fmt >> 4;
	in_fmt = cmd->buf_fmt & ((1U << 4) - 1);

	if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
L
Linus Torvalds 已提交
350
		return;
351 352 353 354 355
	else if (out_fmt == SRP_DATA_DESC_DIRECT ||
		 in_fmt == SRP_DATA_DESC_DIRECT) {
		struct srp_direct_buf *data =
			(struct srp_direct_buf *) cmd->add_data;
		dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
L
Linus Torvalds 已提交
356
	} else {
357 358 359 360
		struct srp_indirect_buf *indirect =
			(struct srp_indirect_buf *) cmd->add_data;
		int num_mapped = indirect->table_desc.len /
			sizeof(struct srp_direct_buf);
361 362

		if (num_mapped <= MAX_INDIRECT_BUFS) {
363
			unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
364
			return;
L
Linus Torvalds 已提交
365
		}
366 367

		unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
L
Linus Torvalds 已提交
368 369 370
	}
}

371
static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
372
		       struct srp_direct_buf *md)
373 374
{
	int i;
375
	struct scatterlist *sg;
376 377
	u64 total_length = 0;

378
	scsi_for_each_sg(cmd, sg, nseg, i) {
379
		struct srp_direct_buf *descr = md + i;
380 381
		descr->va = sg_dma_address(sg);
		descr->len = sg_dma_len(sg);
382
		descr->key = 0;
383
		total_length += sg_dma_len(sg);
384 385 386 387
 	}
	return total_length;
}

L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397
/**
 * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
 * @cmd:	Scsi_Cmnd with the scatterlist
 * @srp_cmd:	srp_cmd that contains the memory descriptor
 * @dev:	device for which to map dma memory
 *
 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
 * Returns 1 on success.
*/
static int map_sg_data(struct scsi_cmnd *cmd,
398
		       struct srp_event_struct *evt_struct,
L
Linus Torvalds 已提交
399 400 401
		       struct srp_cmd *srp_cmd, struct device *dev)
{

402
	int sg_mapped;
L
Linus Torvalds 已提交
403
	u64 total_length = 0;
404 405 406 407
	struct srp_direct_buf *data =
		(struct srp_direct_buf *) srp_cmd->add_data;
	struct srp_indirect_buf *indirect =
		(struct srp_indirect_buf *) data;
L
Linus Torvalds 已提交
408

409 410 411 412
	sg_mapped = scsi_dma_map(cmd);
	if (!sg_mapped)
		return 1;
	else if (sg_mapped < 0)
L
Linus Torvalds 已提交
413 414 415 416 417 418
		return 0;

	set_srp_direction(cmd, srp_cmd, sg_mapped);

	/* special case; we can use a single direct descriptor */
	if (sg_mapped == 1) {
419
		map_sg_list(cmd, sg_mapped, data);
L
Linus Torvalds 已提交
420 421 422
		return 1;
	}

423 424 425
	indirect->table_desc.va = 0;
	indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
	indirect->table_desc.key = 0;
426 427

	if (sg_mapped <= MAX_INDIRECT_BUFS) {
428
		total_length = map_sg_list(cmd, sg_mapped,
429 430
					   &indirect->desc_list[0]);
		indirect->len = total_length;
431
		return 1;
L
Linus Torvalds 已提交
432 433
	}

434 435
	/* get indirect table */
	if (!evt_struct->ext_list) {
436
		evt_struct->ext_list = (struct srp_direct_buf *)
437
			dma_alloc_coherent(dev,
438 439
					   SG_ALL * sizeof(struct srp_direct_buf),
					   &evt_struct->ext_list_token, 0);
440
		if (!evt_struct->ext_list) {
441 442 443 444
			if (!firmware_has_feature(FW_FEATURE_CMO))
				sdev_printk(KERN_ERR, cmd->device,
				            "Can't allocate memory "
				            "for indirect table\n");
445
			scsi_dma_unmap(cmd);
446 447 448 449
			return 0;
		}
	}

450
	total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
451

452 453 454 455 456
	indirect->len = total_length;
	indirect->table_desc.va = evt_struct->ext_list_token;
	indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
	memcpy(indirect->desc_list, evt_struct->ext_list,
	       MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
457
 	return 1;
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466 467 468 469
}

/**
 * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
 * @cmd:	struct scsi_cmnd with the memory to be mapped
 * @srp_cmd:	srp_cmd that contains the memory descriptor
 * @dev:	dma device for which to map dma memory
 *
 * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds 
 * Returns 1 on success.
*/
static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
470
				struct srp_event_struct *evt_struct,
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479
				struct srp_cmd *srp_cmd, struct device *dev)
{
	switch (cmd->sc_data_direction) {
	case DMA_FROM_DEVICE:
	case DMA_TO_DEVICE:
		break;
	case DMA_NONE:
		return 1;
	case DMA_BIDIRECTIONAL:
480 481
		sdev_printk(KERN_ERR, cmd->device,
			    "Can't map DMA_BIDIRECTIONAL to read/write\n");
L
Linus Torvalds 已提交
482 483
		return 0;
	default:
484 485 486
		sdev_printk(KERN_ERR, cmd->device,
			    "Unknown data direction 0x%02x; can't map!\n",
			    cmd->sc_data_direction);
L
Linus Torvalds 已提交
487 488 489
		return 0;
	}

490
	return map_sg_data(cmd, evt_struct, srp_cmd, dev);
L
Linus Torvalds 已提交
491 492
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
/**
 * purge_requests: Our virtual adapter just shut down.  purge any sent requests
 * @hostdata:    the adapter
 */
static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
{
	struct srp_event_struct *tmp_evt, *pos;
	unsigned long flags;

	spin_lock_irqsave(hostdata->host->host_lock, flags);
	list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
		list_del(&tmp_evt->list);
		del_timer(&tmp_evt->timer);
		if (tmp_evt->cmnd) {
			tmp_evt->cmnd->result = (error_code << 16);
			unmap_cmd_data(&tmp_evt->iu.srp.cmd,
				       tmp_evt,
				       tmp_evt->hostdata->dev);
			if (tmp_evt->cmnd_done)
				tmp_evt->cmnd_done(tmp_evt->cmnd);
		} else if (tmp_evt->done)
			tmp_evt->done(tmp_evt);
		free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
	}
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
}

/**
 * ibmvscsi_reset_host - Reset the connection to the server
 * @hostdata:	struct ibmvscsi_host_data to reset
*/
static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
{
	scsi_block_requests(hostdata->host);
	atomic_set(&hostdata->request_limit, 0);

	purge_requests(hostdata, DID_ERROR);
530 531
	if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
	    (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	    (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
		atomic_set(&hostdata->request_limit, -1);
		dev_err(hostdata->dev, "error after reset\n");
	}

	scsi_unblock_requests(hostdata->host);
}

/**
 * ibmvscsi_timeout - Internal command timeout handler
 * @evt_struct:	struct srp_event_struct that timed out
 *
 * Called when an internally generated command times out
*/
static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
{
	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;

	dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
		evt_struct->iu.srp.cmd.opcode);

	ibmvscsi_reset_host(hostdata);
}


L
Linus Torvalds 已提交
557 558 559 560 561 562 563
/* ------------------------------------------------------------
 * Routines for sending and receiving SRPs
 */
/**
 * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
 * @evt_struct:	evt_struct to be sent
 * @hostdata:	ibmvscsi_host_data of host
564
 * @timeout:	timeout in seconds - 0 means do not time command
L
Linus Torvalds 已提交
565 566 567 568 569
 *
 * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
 * Note that this routine assumes that host_lock is held for synchronization
*/
static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
570 571
				   struct ibmvscsi_host_data *hostdata,
				   unsigned long timeout)
L
Linus Torvalds 已提交
572 573
{
	u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
574
	int request_status = 0;
L
Linus Torvalds 已提交
575 576
	int rc;

577 578
	/* If we have exhausted our request limit, just fail this request,
	 * unless it is for a reset or abort.
L
Linus Torvalds 已提交
579 580 581 582
	 * Note that there are rare cases involving driver generated requests 
	 * (such as task management requests) that the mid layer may think we
	 * can handle more requests (can_queue) when we actually can't
	 */
583 584 585 586 587 588 589 590
	if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
		request_status =
			atomic_dec_if_positive(&hostdata->request_limit);
		/* If request limit was -1 when we started, it is now even
		 * less than that
		 */
		if (request_status < -1)
			goto send_error;
591
		/* Otherwise, we may have run out of requests. */
592 593 594 595 596 597 598
		/* If request limit was 0 when we started the adapter is in the
		 * process of performing a login with the server adapter, or
		 * we may have run out of requests.
		 */
		else if (request_status == -1 &&
		         evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
			goto send_busy;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		/* Abort and reset calls should make it through.
		 * Nothing except abort and reset should use the last two
		 * slots unless we had two or less to begin with.
		 */
		else if (request_status < 2 &&
		         evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
			/* In the case that we have less than two requests
			 * available, check the server limit as a combination
			 * of the request limit and the number of requests
			 * in-flight (the size of the send list).  If the
			 * server limit is greater than 2, return busy so
			 * that the last two are reserved for reset and abort.
			 */
			int server_limit = request_status;
			struct srp_event_struct *tmp_evt;

			list_for_each_entry(tmp_evt, &hostdata->sent, list) {
				server_limit++;
			}

			if (server_limit > 2)
				goto send_busy;
		}
622
	}
L
Linus Torvalds 已提交
623 624 625

	/* Copy the IU into the transfer area */
	*evt_struct->xfer_iu = evt_struct->iu;
626
	evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
L
Linus Torvalds 已提交
627 628 629 630 631 632 633

	/* Add this to the sent list.  We need to do this 
	 * before we actually send 
	 * in case it comes back REALLY fast
	 */
	list_add_tail(&evt_struct->list, &hostdata->sent);

634 635 636 637 638 639 640 641
	init_timer(&evt_struct->timer);
	if (timeout) {
		evt_struct->timer.data = (unsigned long) evt_struct;
		evt_struct->timer.expires = jiffies + (timeout * HZ);
		evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
		add_timer(&evt_struct->timer);
	}

L
Linus Torvalds 已提交
642
	if ((rc =
643
	     ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
L
Linus Torvalds 已提交
644
		list_del(&evt_struct->list);
645
		del_timer(&evt_struct->timer);
L
Linus Torvalds 已提交
646

647 648 649 650 651 652 653 654 655 656
		/* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
		 * Firmware will send a CRQ with a transport event (0xFF) to
		 * tell this client what has happened to the transport.  This
		 * will be handled in ibmvscsi_handle_crq()
		 */
		if (rc == H_CLOSED) {
			dev_warn(hostdata->dev, "send warning. "
			         "Receive queue closed, will retry.\n");
			goto send_busy;
		}
657
		dev_err(hostdata->dev, "send error %d\n", rc);
658
		atomic_inc(&hostdata->request_limit);
L
Linus Torvalds 已提交
659 660 661 662 663
		goto send_error;
	}

	return 0;

664
 send_busy:
665
	unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
L
Linus Torvalds 已提交
666 667

	free_event_struct(&hostdata->pool, evt_struct);
668 669
	if (request_status != -1)
		atomic_inc(&hostdata->request_limit);
670
	return SCSI_MLQUEUE_HOST_BUSY;
671 672 673 674 675 676 677 678 679 680 681 682

 send_error:
	unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);

	if (evt_struct->cmnd != NULL) {
		evt_struct->cmnd->result = DID_ERROR << 16;
		evt_struct->cmnd_done(evt_struct->cmnd);
	} else if (evt_struct->done)
		evt_struct->done(evt_struct);

	free_event_struct(&hostdata->pool, evt_struct);
	return 0;
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696
}

/**
 * handle_cmd_rsp: -  Handle responses from commands
 * @evt_struct:	srp_event_struct to be handled
 *
 * Used as a callback by when sending scsi cmds.
 * Gets called by ibmvscsi_handle_crq()
*/
static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
{
	struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
	struct scsi_cmnd *cmnd = evt_struct->cmnd;

697
	if (unlikely(rsp->opcode != SRP_RSP)) {
L
Linus Torvalds 已提交
698
		if (printk_ratelimit())
699 700
			dev_warn(evt_struct->hostdata->dev,
				 "bad SRP RSP type %d\n", rsp->opcode);
L
Linus Torvalds 已提交
701 702 703
	}
	
	if (cmnd) {
704
		cmnd->result |= rsp->status;
L
Linus Torvalds 已提交
705 706
		if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
			memcpy(cmnd->sense_buffer,
707 708
			       rsp->data,
			       rsp->sense_data_len);
L
Linus Torvalds 已提交
709
		unmap_cmd_data(&evt_struct->iu.srp.cmd, 
710
			       evt_struct, 
L
Linus Torvalds 已提交
711 712
			       evt_struct->hostdata->dev);

713
		if (rsp->flags & SRP_RSP_FLAG_DOOVER)
714
			scsi_set_resid(cmnd, rsp->data_out_res_cnt);
715
		else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
716
			scsi_set_resid(cmnd, rsp->data_in_res_cnt);
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
	}

	if (evt_struct->cmnd_done)
		evt_struct->cmnd_done(cmnd);
}

/**
 * lun_from_dev: - Returns the lun of the scsi device
 * @dev:	struct scsi_device
 *
*/
static inline u16 lun_from_dev(struct scsi_device *dev)
{
	return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
}

/**
 * ibmvscsi_queue: - The queuecommand function of the scsi template 
 * @cmd:	struct scsi_cmnd to be executed
 * @done:	Callback function to be called when cmd is completed
*/
static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
				 void (*done) (struct scsi_cmnd *))
{
	struct srp_cmd *srp_cmd;
	struct srp_event_struct *evt_struct;
743
	struct srp_indirect_buf *indirect;
744
	struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
L
Linus Torvalds 已提交
745
	u16 lun = lun_from_dev(cmnd->device);
746
	u8 out_fmt, in_fmt;
L
Linus Torvalds 已提交
747

748
	cmnd->result = (DID_OK << 16);
L
Linus Torvalds 已提交
749 750 751 752 753 754
	evt_struct = get_event_struct(&hostdata->pool);
	if (!evt_struct)
		return SCSI_MLQUEUE_HOST_BUSY;

	/* Set up the actual SRP IU */
	srp_cmd = &evt_struct->iu.srp.cmd;
755 756
	memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
	srp_cmd->opcode = SRP_CMD;
757
	memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
L
Linus Torvalds 已提交
758 759
	srp_cmd->lun = ((u64) lun) << 48;

760
	if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
761 762 763
		if (!firmware_has_feature(FW_FEATURE_CMO))
			sdev_printk(KERN_ERR, cmnd->device,
			            "couldn't convert cmd to srp_cmd\n");
L
Linus Torvalds 已提交
764 765 766 767
		free_event_struct(&hostdata->pool, evt_struct);
		return SCSI_MLQUEUE_HOST_BUSY;
	}

768 769 770
	init_event_struct(evt_struct,
			  handle_cmd_rsp,
			  VIOSRP_SRP_FORMAT,
J
Jens Axboe 已提交
771
			  cmnd->request->timeout/HZ);
772 773 774 775

	evt_struct->cmnd = cmnd;
	evt_struct->cmnd_done = done;

L
Linus Torvalds 已提交
776
	/* Fix up dma address of the buffer itself */
777 778 779 780 781 782 783 784 785
	indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
	out_fmt = srp_cmd->buf_fmt >> 4;
	in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
	if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
	     out_fmt == SRP_DATA_DESC_INDIRECT) &&
	    indirect->table_desc.va == 0) {
		indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
			offsetof(struct srp_cmd, add_data) +
			offsetof(struct srp_indirect_buf, desc_list);
L
Linus Torvalds 已提交
786 787
	}

788
	return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
L
Linus Torvalds 已提交
789 790 791 792 793
}

/* ------------------------------------------------------------
 * Routines for driver initialization
 */
794

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
/**
 * map_persist_bufs: - Pre-map persistent data for adapter logins
 * @hostdata:   ibmvscsi_host_data of host
 *
 * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
 * Return 1 on error, 0 on success.
 */
static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
{

	hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
					     sizeof(hostdata->caps), DMA_BIDIRECTIONAL);

	if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
		dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
		return 1;
	}

	hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
						     &hostdata->madapter_info,
						     sizeof(hostdata->madapter_info),
						     DMA_BIDIRECTIONAL);
	if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
		dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
		dma_unmap_single(hostdata->dev, hostdata->caps_addr,
				 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
		return 1;
	}

	return 0;
}

/**
 * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
 * @hostdata:   ibmvscsi_host_data of host
 *
 * Unmap the capabilities and adapter info DMA buffers
 */
static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
{
	dma_unmap_single(hostdata->dev, hostdata->caps_addr,
			 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);

	dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
			 sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
}

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/**
 * login_rsp: - Handle response to SRP login request
 * @evt_struct:	srp_event_struct with the response
 *
 * Used as a "done" callback by when sending srp_login. Gets called
 * by ibmvscsi_handle_crq()
*/
static void login_rsp(struct srp_event_struct *evt_struct)
{
	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
	switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
	case SRP_LOGIN_RSP:	/* it worked! */
		break;
	case SRP_LOGIN_REJ:	/* refused! */
		dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
			 evt_struct->xfer_iu->srp.login_rej.reason);
		/* Login failed.  */
		atomic_set(&hostdata->request_limit, -1);
		return;
	default:
		dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
			evt_struct->xfer_iu->srp.login_rsp.opcode);
		/* Login failed.  */
		atomic_set(&hostdata->request_limit, -1);
		return;
	}

	dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
870
	hostdata->client_migrated = 0;
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918

	/* Now we know what the real request-limit is.
	 * This value is set rather than added to request_limit because
	 * request_limit could have been set to -1 by this client.
	 */
	atomic_set(&hostdata->request_limit,
		   evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);

	/* If we had any pending I/Os, kick them */
	scsi_unblock_requests(hostdata->host);
}

/**
 * send_srp_login: - Sends the srp login
 * @hostdata:	ibmvscsi_host_data of host
 *
 * Returns zero if successful.
*/
static int send_srp_login(struct ibmvscsi_host_data *hostdata)
{
	int rc;
	unsigned long flags;
	struct srp_login_req *login;
	struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);

	BUG_ON(!evt_struct);
	init_event_struct(evt_struct, login_rsp,
			  VIOSRP_SRP_FORMAT, login_timeout);

	login = &evt_struct->iu.srp.login_req;
	memset(login, 0, sizeof(*login));
	login->opcode = SRP_LOGIN_REQ;
	login->req_it_iu_len = sizeof(union srp_iu);
	login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;

	spin_lock_irqsave(hostdata->host->host_lock, flags);
	/* Start out with a request limit of 0, since this is negotiated in
	 * the login request we are just sending and login requests always
	 * get sent by the driver regardless of request_limit.
	 */
	atomic_set(&hostdata->request_limit, 0);

	rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
	dev_info(hostdata->dev, "sent SRP login\n");
	return rc;
};

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
/**
 * capabilities_rsp: - Handle response to MAD adapter capabilities request
 * @evt_struct:	srp_event_struct with the response
 *
 * Used as a "done" callback by when sending adapter_info.
 */
static void capabilities_rsp(struct srp_event_struct *evt_struct)
{
	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;

	if (evt_struct->xfer_iu->mad.capabilities.common.status) {
		dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
			evt_struct->xfer_iu->mad.capabilities.common.status);
	} else {
		if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
			dev_info(hostdata->dev, "Partition migration not supported\n");

		if (client_reserve) {
			if (hostdata->caps.reserve.common.server_support ==
			    SERVER_SUPPORTS_CAP)
				dev_info(hostdata->dev, "Client reserve enabled\n");
			else
				dev_info(hostdata->dev, "Client reserve not supported\n");
		}
	}

	send_srp_login(hostdata);
}

/**
 * send_mad_capabilities: - Sends the mad capabilities request
 *      and stores the result so it can be retrieved with
 * @hostdata:	ibmvscsi_host_data of host
 */
static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
{
	struct viosrp_capabilities *req;
	struct srp_event_struct *evt_struct;
	unsigned long flags;
	struct device_node *of_node = hostdata->dev->archdata.of_node;
	const char *location;

	evt_struct = get_event_struct(&hostdata->pool);
	BUG_ON(!evt_struct);

	init_event_struct(evt_struct, capabilities_rsp,
			  VIOSRP_MAD_FORMAT, info_timeout);

	req = &evt_struct->iu.mad.capabilities;
	memset(req, 0, sizeof(*req));

	hostdata->caps.flags = CAP_LIST_SUPPORTED;
	if (hostdata->client_migrated)
		hostdata->caps.flags |= CLIENT_MIGRATED;

	strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
		sizeof(hostdata->caps.name));
	hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';

	location = of_get_property(of_node, "ibm,loc-code", NULL);
	location = location ? location : dev_name(hostdata->dev);
	strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
	hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';

	req->common.type = VIOSRP_CAPABILITIES_TYPE;
	req->buffer = hostdata->caps_addr;

	hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
	hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
	hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
	hostdata->caps.migration.ecl = 1;

	if (client_reserve) {
		hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
		hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
		hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
		hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
		req->common.length = sizeof(hostdata->caps);
	} else
		req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);

	spin_lock_irqsave(hostdata->host->host_lock, flags);
	if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
		dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
};

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
/**
 * fast_fail_rsp: - Handle response to MAD enable fast fail
 * @evt_struct:	srp_event_struct with the response
 *
 * Used as a "done" callback by when sending enable fast fail. Gets called
 * by ibmvscsi_handle_crq()
 */
static void fast_fail_rsp(struct srp_event_struct *evt_struct)
{
	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
	u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;

	if (status == VIOSRP_MAD_NOT_SUPPORTED)
		dev_err(hostdata->dev, "fast_fail not supported in server\n");
	else if (status == VIOSRP_MAD_FAILED)
		dev_err(hostdata->dev, "fast_fail request failed\n");
	else if (status != VIOSRP_MAD_SUCCESS)
		dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);

1025
	send_mad_capabilities(hostdata);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
}

/**
 * init_host - Start host initialization
 * @hostdata:	ibmvscsi_host_data of host
 *
 * Returns zero if successful.
 */
static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
{
	int rc;
	unsigned long flags;
	struct viosrp_fast_fail *fast_fail_mad;
	struct srp_event_struct *evt_struct;

1041 1042 1043 1044
	if (!fast_fail) {
		send_mad_capabilities(hostdata);
		return 0;
	}
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	evt_struct = get_event_struct(&hostdata->pool);
	BUG_ON(!evt_struct);

	init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);

	fast_fail_mad = &evt_struct->iu.mad.fast_fail;
	memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
	fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
	fast_fail_mad->common.length = sizeof(*fast_fail_mad);

	spin_lock_irqsave(hostdata->host->host_lock, flags);
	rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
	return rc;
}

L
Linus Torvalds 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
/**
 * adapter_info_rsp: - Handle response to MAD adapter info request
 * @evt_struct:	srp_event_struct with the response
 *
 * Used as a "done" callback by when sending adapter_info. Gets called
 * by ibmvscsi_handle_crq()
*/
static void adapter_info_rsp(struct srp_event_struct *evt_struct)
{
	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;

	if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
1074 1075
		dev_err(hostdata->dev, "error %d getting adapter info\n",
			evt_struct->xfer_iu->mad.adapter_info.common.status);
L
Linus Torvalds 已提交
1076
	} else {
1077 1078 1079 1080 1081 1082 1083
		dev_info(hostdata->dev, "host srp version: %s, "
			 "host partition %s (%d), OS %d, max io %u\n",
			 hostdata->madapter_info.srp_version,
			 hostdata->madapter_info.partition_name,
			 hostdata->madapter_info.partition_number,
			 hostdata->madapter_info.os_type,
			 hostdata->madapter_info.port_max_txu[0]);
L
Linus Torvalds 已提交
1084 1085 1086 1087
		
		if (hostdata->madapter_info.port_max_txu[0]) 
			hostdata->host->max_sectors = 
				hostdata->madapter_info.port_max_txu[0] >> 9;
1088 1089 1090
		
		if (hostdata->madapter_info.os_type == 3 &&
		    strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
1091 1092 1093 1094
			dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
				hostdata->madapter_info.srp_version);
			dev_err(hostdata->dev, "limiting scatterlists to %d\n",
				MAX_INDIRECT_BUFS);
1095 1096
			hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
		}
1097 1098 1099 1100 1101

		if (hostdata->madapter_info.os_type == 3) {
			enable_fast_fail(hostdata);
			return;
		}
L
Linus Torvalds 已提交
1102
	}
1103

1104
	send_srp_login(hostdata);
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
}

/**
 * send_mad_adapter_info: - Sends the mad adapter info request
 *      and stores the result so it can be retrieved with
 *      sysfs.  We COULD consider causing a failure if the
 *      returned SRP version doesn't match ours.
 * @hostdata:	ibmvscsi_host_data of host
 * 
 * Returns zero if successful.
*/
static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
{
	struct viosrp_adapter_info *req;
	struct srp_event_struct *evt_struct;
1120
	unsigned long flags;
1121

L
Linus Torvalds 已提交
1122
	evt_struct = get_event_struct(&hostdata->pool);
1123
	BUG_ON(!evt_struct);
L
Linus Torvalds 已提交
1124 1125 1126 1127

	init_event_struct(evt_struct,
			  adapter_info_rsp,
			  VIOSRP_MAD_FORMAT,
1128
			  info_timeout);
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134
	
	req = &evt_struct->iu.mad.adapter_info;
	memset(req, 0x00, sizeof(*req));
	
	req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
	req->common.length = sizeof(hostdata->madapter_info);
1135
	req->buffer = hostdata->adapter_info_addr;
L
Linus Torvalds 已提交
1136

1137
	spin_lock_irqsave(hostdata->host->host_lock, flags);
1138
	if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1139
		dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
1140
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
L
Linus Torvalds 已提交
1141 1142 1143
};

/**
1144
 * init_adapter: Start virtual adapter initialization sequence
L
Linus Torvalds 已提交
1145
 *
1146 1147
 */
static void init_adapter(struct ibmvscsi_host_data *hostdata)
L
Linus Torvalds 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
{
	send_mad_adapter_info(hostdata);
}

/**
 * sync_completion: Signal that a synchronous command has completed
 * Note that after returning from this call, the evt_struct is freed.
 * the caller waiting on this completion shouldn't touch the evt_struct
 * again.
 */
static void sync_completion(struct srp_event_struct *evt_struct)
{
	/* copy the response back */
	if (evt_struct->sync_srp)
		*evt_struct->sync_srp = *evt_struct->xfer_iu;
	
	complete(&evt_struct->comp);
}

/**
 * ibmvscsi_abort: Abort a command...from scsi host template
 * send this over to the server and wait synchronously for the response
 */
static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
{
1173
	struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178
	struct srp_tsk_mgmt *tsk_mgmt;
	struct srp_event_struct *evt;
	struct srp_event_struct *tmp_evt, *found_evt;
	union viosrp_iu srp_rsp;
	int rsp_rc;
D
Dave C Boutcher 已提交
1179
	unsigned long flags;
L
Linus Torvalds 已提交
1180
	u16 lun = lun_from_dev(cmd->device);
1181
	unsigned long wait_switch = 0;
L
Linus Torvalds 已提交
1182 1183 1184 1185

	/* First, find this command in our sent list so we can figure
	 * out the correct tag
	 */
D
Dave C Boutcher 已提交
1186
	spin_lock_irqsave(hostdata->host->host_lock, flags);
1187 1188 1189 1190 1191 1192 1193 1194
	wait_switch = jiffies + (init_timeout * HZ);
	do {
		found_evt = NULL;
		list_for_each_entry(tmp_evt, &hostdata->sent, list) {
			if (tmp_evt->cmnd == cmd) {
				found_evt = tmp_evt;
				break;
			}
L
Linus Torvalds 已提交
1195 1196
		}

1197 1198 1199 1200
		if (!found_evt) {
			spin_unlock_irqrestore(hostdata->host->host_lock, flags);
			return SUCCESS;
		}
L
Linus Torvalds 已提交
1201

1202 1203 1204 1205 1206 1207 1208
		evt = get_event_struct(&hostdata->pool);
		if (evt == NULL) {
			spin_unlock_irqrestore(hostdata->host->host_lock, flags);
			sdev_printk(KERN_ERR, cmd->device,
				"failed to allocate abort event\n");
			return FAILED;
		}
L
Linus Torvalds 已提交
1209
	
1210 1211 1212
		init_event_struct(evt,
				  sync_completion,
				  VIOSRP_SRP_FORMAT,
1213
				  abort_timeout);
L
Linus Torvalds 已提交
1214

1215
		tsk_mgmt = &evt->iu.srp.tsk_mgmt;
L
Linus Torvalds 已提交
1216
	
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		/* Set up an abort SRP command */
		memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
		tsk_mgmt->opcode = SRP_TSK_MGMT;
		tsk_mgmt->lun = ((u64) lun) << 48;
		tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
		tsk_mgmt->task_tag = (u64) found_evt;

		evt->sync_srp = &srp_rsp;

		init_completion(&evt->comp);
1227
		rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
1228 1229 1230 1231 1232 1233 1234 1235 1236

		if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
			break;

		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
		msleep(10);
		spin_lock_irqsave(hostdata->host->host_lock, flags);
	} while (time_before(jiffies, wait_switch));

D
Dave C Boutcher 已提交
1237
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1238

D
Dave C Boutcher 已提交
1239
	if (rsp_rc != 0) {
1240 1241
		sdev_printk(KERN_ERR, cmd->device,
			    "failed to send abort() event. rc=%d\n", rsp_rc);
L
Linus Torvalds 已提交
1242 1243 1244
		return FAILED;
	}

1245
	sdev_printk(KERN_INFO, cmd->device,
1246
                    "aborting command. lun 0x%llx, tag 0x%llx\n",
1247 1248
		    (((u64) lun) << 48), (u64) found_evt);

L
Linus Torvalds 已提交
1249 1250 1251
	wait_for_completion(&evt->comp);

	/* make sure we got a good response */
1252
	if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
L
Linus Torvalds 已提交
1253
		if (printk_ratelimit())
1254 1255
			sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
				    srp_rsp.srp.rsp.opcode);
L
Linus Torvalds 已提交
1256 1257 1258
		return FAILED;
	}

1259 1260
	if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
		rsp_rc = *((int *)srp_rsp.srp.rsp.data);
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265
	else
		rsp_rc = srp_rsp.srp.rsp.status;

	if (rsp_rc) {
		if (printk_ratelimit())
1266
			sdev_printk(KERN_WARNING, cmd->device,
1267
				    "abort code %d for task tag 0x%llx\n",
1268
				    rsp_rc, tsk_mgmt->task_tag);
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274 1275
		return FAILED;
	}

	/* Because we dropped the spinlock above, it's possible
	 * The event is no longer in our list.  Make sure it didn't
	 * complete while we were aborting
	 */
D
Dave C Boutcher 已提交
1276
	spin_lock_irqsave(hostdata->host->host_lock, flags);
L
Linus Torvalds 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285
	found_evt = NULL;
	list_for_each_entry(tmp_evt, &hostdata->sent, list) {
		if (tmp_evt->cmnd == cmd) {
			found_evt = tmp_evt;
			break;
		}
	}

	if (found_evt == NULL) {
D
Dave C Boutcher 已提交
1286
		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1287
		sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
1288
			    tsk_mgmt->task_tag);
L
Linus Torvalds 已提交
1289 1290 1291
		return SUCCESS;
	}

1292
	sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
1293
		    tsk_mgmt->task_tag);
L
Linus Torvalds 已提交
1294 1295 1296

	cmd->result = (DID_ABORT << 16);
	list_del(&found_evt->list);
1297 1298
	unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
		       found_evt->hostdata->dev);
L
Linus Torvalds 已提交
1299
	free_event_struct(&found_evt->hostdata->pool, found_evt);
D
Dave C Boutcher 已提交
1300
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
L
Linus Torvalds 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	atomic_inc(&hostdata->request_limit);
	return SUCCESS;
}

/**
 * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host 
 * template send this over to the server and wait synchronously for the 
 * response
 */
static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
{
1312
	struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
L
Linus Torvalds 已提交
1313 1314 1315 1316 1317
	struct srp_tsk_mgmt *tsk_mgmt;
	struct srp_event_struct *evt;
	struct srp_event_struct *tmp_evt, *pos;
	union viosrp_iu srp_rsp;
	int rsp_rc;
D
Dave C Boutcher 已提交
1318
	unsigned long flags;
L
Linus Torvalds 已提交
1319
	u16 lun = lun_from_dev(cmd->device);
1320
	unsigned long wait_switch = 0;
L
Linus Torvalds 已提交
1321

D
Dave C Boutcher 已提交
1322
	spin_lock_irqsave(hostdata->host->host_lock, flags);
1323 1324 1325 1326 1327 1328 1329 1330 1331
	wait_switch = jiffies + (init_timeout * HZ);
	do {
		evt = get_event_struct(&hostdata->pool);
		if (evt == NULL) {
			spin_unlock_irqrestore(hostdata->host->host_lock, flags);
			sdev_printk(KERN_ERR, cmd->device,
				"failed to allocate reset event\n");
			return FAILED;
		}
L
Linus Torvalds 已提交
1332
	
1333 1334 1335
		init_event_struct(evt,
				  sync_completion,
				  VIOSRP_SRP_FORMAT,
1336
				  reset_timeout);
L
Linus Torvalds 已提交
1337

1338
		tsk_mgmt = &evt->iu.srp.tsk_mgmt;
L
Linus Torvalds 已提交
1339

1340 1341 1342 1343 1344
		/* Set up a lun reset SRP command */
		memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
		tsk_mgmt->opcode = SRP_TSK_MGMT;
		tsk_mgmt->lun = ((u64) lun) << 48;
		tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
L
Linus Torvalds 已提交
1345

1346 1347 1348
		evt->sync_srp = &srp_rsp;

		init_completion(&evt->comp);
1349
		rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
1350 1351 1352 1353 1354 1355 1356 1357

		if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
			break;

		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
		msleep(10);
		spin_lock_irqsave(hostdata->host->host_lock, flags);
	} while (time_before(jiffies, wait_switch));
L
Linus Torvalds 已提交
1358

D
Dave C Boutcher 已提交
1359
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1360

D
Dave C Boutcher 已提交
1361
	if (rsp_rc != 0) {
1362 1363
		sdev_printk(KERN_ERR, cmd->device,
			    "failed to send reset event. rc=%d\n", rsp_rc);
L
Linus Torvalds 已提交
1364 1365 1366
		return FAILED;
	}

1367
	sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
1368 1369
		    (((u64) lun) << 48));

L
Linus Torvalds 已提交
1370 1371 1372
	wait_for_completion(&evt->comp);

	/* make sure we got a good response */
1373
	if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
L
Linus Torvalds 已提交
1374
		if (printk_ratelimit())
1375 1376
			sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
				    srp_rsp.srp.rsp.opcode);
L
Linus Torvalds 已提交
1377 1378 1379
		return FAILED;
	}

1380 1381
	if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
		rsp_rc = *((int *)srp_rsp.srp.rsp.data);
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386
	else
		rsp_rc = srp_rsp.srp.rsp.status;

	if (rsp_rc) {
		if (printk_ratelimit())
1387
			sdev_printk(KERN_WARNING, cmd->device,
1388
				    "reset code %d for task tag 0x%llx\n",
1389
				    rsp_rc, tsk_mgmt->task_tag);
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395
		return FAILED;
	}

	/* We need to find all commands for this LUN that have not yet been
	 * responded to, and fail them with DID_RESET
	 */
D
Dave C Boutcher 已提交
1396
	spin_lock_irqsave(hostdata->host->host_lock, flags);
L
Linus Torvalds 已提交
1397 1398 1399 1400 1401
	list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
		if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
			if (tmp_evt->cmnd)
				tmp_evt->cmnd->result = (DID_RESET << 16);
			list_del(&tmp_evt->list);
1402 1403
			unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
				       tmp_evt->hostdata->dev);
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412
			free_event_struct(&tmp_evt->hostdata->pool,
						   tmp_evt);
			atomic_inc(&hostdata->request_limit);
			if (tmp_evt->cmnd_done)
				tmp_evt->cmnd_done(tmp_evt->cmnd);
			else if (tmp_evt->done)
				tmp_evt->done(tmp_evt);
		}
	}
D
Dave C Boutcher 已提交
1413
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
L
Linus Torvalds 已提交
1414 1415 1416 1417
	return SUCCESS;
}

/**
1418 1419 1420 1421
 * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
 * @cmd:	struct scsi_cmnd having problems
*/
static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
L
Linus Torvalds 已提交
1422
{
1423
	unsigned long wait_switch = 0;
1424
	struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
L
Linus Torvalds 已提交
1425

1426 1427 1428 1429 1430 1431 1432 1433 1434
	dev_err(hostdata->dev, "Resetting connection due to error recovery\n");

	ibmvscsi_reset_host(hostdata);

	for (wait_switch = jiffies + (init_timeout * HZ);
	     time_before(jiffies, wait_switch) &&
		     atomic_read(&hostdata->request_limit) < 2;) {

		msleep(10);
L
Linus Torvalds 已提交
1435
	}
1436 1437 1438 1439 1440

	if (atomic_read(&hostdata->request_limit) <= 0)
		return FAILED;

	return SUCCESS;
L
Linus Torvalds 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
}

/**
 * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
 * @crq:	Command/Response queue
 * @hostdata:	ibmvscsi_host_data of host
 *
*/
void ibmvscsi_handle_crq(struct viosrp_crq *crq,
			 struct ibmvscsi_host_data *hostdata)
{
1452
	long rc;
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459
	unsigned long flags;
	struct srp_event_struct *evt_struct =
	    (struct srp_event_struct *)crq->IU_data_ptr;
	switch (crq->valid) {
	case 0xC0:		/* initialization */
		switch (crq->format) {
		case 0x01:	/* Initialization message */
1460
			dev_info(hostdata->dev, "partner initialized\n");
L
Linus Torvalds 已提交
1461
			/* Send back a response */
1462 1463
			if ((rc = ibmvscsi_ops->send_crq(hostdata,
							 0xC002000000000000LL, 0)) == 0) {
L
Linus Torvalds 已提交
1464
				/* Now login */
1465
				init_adapter(hostdata);
L
Linus Torvalds 已提交
1466
			} else {
1467
				dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
L
Linus Torvalds 已提交
1468 1469 1470 1471
			}

			break;
		case 0x02:	/* Initialization response */
1472
			dev_info(hostdata->dev, "partner initialization complete\n");
L
Linus Torvalds 已提交
1473 1474

			/* Now login */
1475
			init_adapter(hostdata);
L
Linus Torvalds 已提交
1476 1477
			break;
		default:
1478
			dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
L
Linus Torvalds 已提交
1479 1480
		}
		return;
1481 1482
	case 0xFF:	/* Hypervisor telling us the connection is closed */
		scsi_block_requests(hostdata->host);
1483
		atomic_set(&hostdata->request_limit, 0);
1484 1485
		if (crq->format == 0x06) {
			/* We need to re-setup the interpartition connection */
1486
			dev_info(hostdata->dev, "Re-enabling adapter!\n");
1487
			hostdata->client_migrated = 1;
1488
			purge_requests(hostdata, DID_REQUEUE);
1489 1490 1491 1492
			if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
							      hostdata)) ||
			    (ibmvscsi_ops->send_crq(hostdata,
						    0xC001000000000000LL, 0))) {
1493 1494
					atomic_set(&hostdata->request_limit,
						   -1);
1495
					dev_err(hostdata->dev, "error after enable\n");
1496
			}
1497
		} else {
1498 1499
			dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
				crq->format);
L
Linus Torvalds 已提交
1500

1501
			purge_requests(hostdata, DID_ERROR);
1502 1503 1504 1505
			if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
							   hostdata)) ||
			    (ibmvscsi_ops->send_crq(hostdata,
						    0xC001000000000000LL, 0))) {
1506 1507
					atomic_set(&hostdata->request_limit,
						   -1);
1508
					dev_err(hostdata->dev, "error after reset\n");
1509
			}
1510 1511
		}
		scsi_unblock_requests(hostdata->host);
L
Linus Torvalds 已提交
1512 1513 1514 1515
		return;
	case 0x80:		/* real payload */
		break;
	default:
1516 1517
		dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
			crq->valid);
L
Linus Torvalds 已提交
1518 1519 1520 1521 1522 1523 1524 1525
		return;
	}

	/* The only kind of payload CRQs we should get are responses to
	 * things we send. Make sure this response is to something we
	 * actually sent
	 */
	if (!valid_event_struct(&hostdata->pool, evt_struct)) {
1526
		dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
L
Linus Torvalds 已提交
1527 1528 1529 1530 1531
		       (void *)crq->IU_data_ptr);
		return;
	}

	if (atomic_read(&evt_struct->free)) {
1532 1533
		dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
			(void *)crq->IU_data_ptr);
L
Linus Torvalds 已提交
1534 1535 1536 1537
		return;
	}

	if (crq->format == VIOSRP_SRP_FORMAT)
1538
		atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
L
Linus Torvalds 已提交
1539 1540
			   &hostdata->request_limit);

1541 1542
	del_timer(&evt_struct->timer);

1543
	if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
1544
		evt_struct->cmnd->result = DID_ERROR << 16;
L
Linus Torvalds 已提交
1545 1546 1547
	if (evt_struct->done)
		evt_struct->done(evt_struct);
	else
1548
		dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
L
Linus Torvalds 已提交
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568

	/*
	 * Lock the host_lock before messing with these structures, since we
	 * are running in a task context
	 */
	spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
	list_del(&evt_struct->list);
	free_event_struct(&evt_struct->hostdata->pool, evt_struct);
	spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
}

/**
 * ibmvscsi_get_host_config: Send the command to the server to get host
 * configuration data.  The data is opaque to us.
 */
static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
				   unsigned char *buffer, int length)
{
	struct viosrp_host_config *host_config;
	struct srp_event_struct *evt_struct;
1569
	unsigned long flags;
1570
	dma_addr_t addr;
L
Linus Torvalds 已提交
1571 1572 1573 1574
	int rc;

	evt_struct = get_event_struct(&hostdata->pool);
	if (!evt_struct) {
1575
		dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
L
Linus Torvalds 已提交
1576 1577 1578 1579 1580 1581
		return -1;
	}

	init_event_struct(evt_struct,
			  sync_completion,
			  VIOSRP_MAD_FORMAT,
1582
			  info_timeout);
L
Linus Torvalds 已提交
1583 1584 1585 1586 1587 1588 1589

	host_config = &evt_struct->iu.mad.host_config;

	/* Set up a lun reset SRP command */
	memset(host_config, 0x00, sizeof(*host_config));
	host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
	host_config->common.length = length;
1590 1591 1592
	host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
						    length,
						    DMA_BIDIRECTIONAL);
L
Linus Torvalds 已提交
1593

1594
	if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
1595 1596 1597
		if (!firmware_has_feature(FW_FEATURE_CMO))
			dev_err(hostdata->dev,
			        "dma_mapping error getting host config\n");
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602
		free_event_struct(&hostdata->pool, evt_struct);
		return -1;
	}

	init_completion(&evt_struct->comp);
1603
	spin_lock_irqsave(hostdata->host->host_lock, flags);
1604
	rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1605
	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1606
	if (rc == 0)
L
Linus Torvalds 已提交
1607
		wait_for_completion(&evt_struct->comp);
1608
	dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
L
Linus Torvalds 已提交
1609 1610 1611 1612

	return rc;
}

1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
/**
 * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
 * @sdev:	struct scsi_device device to configure
 *
 * Enable allow_restart for a device if it is a disk.  Adjust the
 * queue_depth here also as is required by the documentation for
 * struct scsi_host_template.
 */
static int ibmvscsi_slave_configure(struct scsi_device *sdev)
{
	struct Scsi_Host *shost = sdev->host;
	unsigned long lock_flags = 0;

	spin_lock_irqsave(shost->host_lock, lock_flags);
1627
	if (sdev->type == TYPE_DISK) {
1628
		sdev->allow_restart = 1;
1629
		blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
1630
	}
1631 1632 1633 1634 1635
	scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
	spin_unlock_irqrestore(shost->host_lock, lock_flags);
	return 0;
}

1636 1637 1638 1639
/**
 * ibmvscsi_change_queue_depth - Change the device's queue depth
 * @sdev:	scsi device struct
 * @qdepth:	depth to set
1640
 * @reason:	calling context
1641 1642 1643 1644
 *
 * Return value:
 * 	actual depth set
 **/
1645 1646
static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
				       int reason)
1647
{
1648 1649 1650
	if (reason != SCSI_QDEPTH_DEFAULT)
		return -EOPNOTSUPP;

1651 1652 1653 1654 1655 1656 1657
	if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
		qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;

	scsi_adjust_queue_depth(sdev, 0, qdepth);
	return sdev->queue_depth;
}

L
Linus Torvalds 已提交
1658 1659 1660
/* ------------------------------------------------------------
 * sysfs attributes
 */
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
static ssize_t show_host_vhost_loc(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
	int len;

	len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
		       hostdata->caps.loc);
	return len;
}

static struct device_attribute ibmvscsi_host_vhost_loc = {
	.attr = {
		 .name = "vhost_loc",
		 .mode = S_IRUGO,
		 },
	.show = show_host_vhost_loc,
};

static ssize_t show_host_vhost_name(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
	int len;

	len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
		       hostdata->caps.name);
	return len;
}

static struct device_attribute ibmvscsi_host_vhost_name = {
	.attr = {
		 .name = "vhost_name",
		 .mode = S_IRUGO,
		 },
	.show = show_host_vhost_name,
};

1701 1702
static ssize_t show_host_srp_version(struct device *dev,
				     struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1703
{
1704
	struct Scsi_Host *shost = class_to_shost(dev);
1705
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
L
Linus Torvalds 已提交
1706 1707 1708 1709 1710 1711 1712
	int len;

	len = snprintf(buf, PAGE_SIZE, "%s\n",
		       hostdata->madapter_info.srp_version);
	return len;
}

1713
static struct device_attribute ibmvscsi_host_srp_version = {
L
Linus Torvalds 已提交
1714 1715 1716 1717 1718 1719 1720
	.attr = {
		 .name = "srp_version",
		 .mode = S_IRUGO,
		 },
	.show = show_host_srp_version,
};

1721 1722
static ssize_t show_host_partition_name(struct device *dev,
					struct device_attribute *attr,
L
Linus Torvalds 已提交
1723 1724
					char *buf)
{
1725
	struct Scsi_Host *shost = class_to_shost(dev);
1726
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
L
Linus Torvalds 已提交
1727 1728 1729 1730 1731 1732 1733
	int len;

	len = snprintf(buf, PAGE_SIZE, "%s\n",
		       hostdata->madapter_info.partition_name);
	return len;
}

1734
static struct device_attribute ibmvscsi_host_partition_name = {
L
Linus Torvalds 已提交
1735 1736 1737 1738 1739 1740 1741
	.attr = {
		 .name = "partition_name",
		 .mode = S_IRUGO,
		 },
	.show = show_host_partition_name,
};

1742 1743
static ssize_t show_host_partition_number(struct device *dev,
					  struct device_attribute *attr,
L
Linus Torvalds 已提交
1744 1745
					  char *buf)
{
1746
	struct Scsi_Host *shost = class_to_shost(dev);
1747
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
L
Linus Torvalds 已提交
1748 1749 1750 1751 1752 1753 1754
	int len;

	len = snprintf(buf, PAGE_SIZE, "%d\n",
		       hostdata->madapter_info.partition_number);
	return len;
}

1755
static struct device_attribute ibmvscsi_host_partition_number = {
L
Linus Torvalds 已提交
1756 1757 1758 1759 1760 1761 1762
	.attr = {
		 .name = "partition_number",
		 .mode = S_IRUGO,
		 },
	.show = show_host_partition_number,
};

1763 1764
static ssize_t show_host_mad_version(struct device *dev,
				     struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1765
{
1766
	struct Scsi_Host *shost = class_to_shost(dev);
1767
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773 1774
	int len;

	len = snprintf(buf, PAGE_SIZE, "%d\n",
		       hostdata->madapter_info.mad_version);
	return len;
}

1775
static struct device_attribute ibmvscsi_host_mad_version = {
L
Linus Torvalds 已提交
1776 1777 1778 1779 1780 1781 1782
	.attr = {
		 .name = "mad_version",
		 .mode = S_IRUGO,
		 },
	.show = show_host_mad_version,
};

1783 1784
static ssize_t show_host_os_type(struct device *dev,
				 struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1785
{
1786
	struct Scsi_Host *shost = class_to_shost(dev);
1787
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
L
Linus Torvalds 已提交
1788 1789 1790 1791 1792 1793
	int len;

	len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
	return len;
}

1794
static struct device_attribute ibmvscsi_host_os_type = {
L
Linus Torvalds 已提交
1795 1796 1797 1798 1799 1800 1801
	.attr = {
		 .name = "os_type",
		 .mode = S_IRUGO,
		 },
	.show = show_host_os_type,
};

1802 1803
static ssize_t show_host_config(struct device *dev,
				struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1804
{
1805
	struct Scsi_Host *shost = class_to_shost(dev);
1806
	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
L
Linus Torvalds 已提交
1807 1808 1809 1810 1811 1812 1813 1814

	/* returns null-terminated host config data */
	if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
		return strlen(buf);
	else
		return 0;
}

1815
static struct device_attribute ibmvscsi_host_config = {
L
Linus Torvalds 已提交
1816 1817 1818 1819 1820 1821 1822
	.attr = {
		 .name = "config",
		 .mode = S_IRUGO,
		 },
	.show = show_host_config,
};

1823
static struct device_attribute *ibmvscsi_attrs[] = {
1824 1825
	&ibmvscsi_host_vhost_loc,
	&ibmvscsi_host_vhost_name,
L
Linus Torvalds 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
	&ibmvscsi_host_srp_version,
	&ibmvscsi_host_partition_name,
	&ibmvscsi_host_partition_number,
	&ibmvscsi_host_mad_version,
	&ibmvscsi_host_os_type,
	&ibmvscsi_host_config,
	NULL
};

/* ------------------------------------------------------------
 * SCSI driver registration
 */
static struct scsi_host_template driver_template = {
	.module = THIS_MODULE,
	.name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
	.proc_name = "ibmvscsi",
	.queuecommand = ibmvscsi_queuecommand,
	.eh_abort_handler = ibmvscsi_eh_abort_handler,
	.eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
1845
	.eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
1846
	.slave_configure = ibmvscsi_slave_configure,
1847
	.change_queue_depth = ibmvscsi_change_queue_depth,
1848
	.cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
1849
	.can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
L
Linus Torvalds 已提交
1850
	.this_id = -1,
1851
	.sg_tablesize = SG_ALL,
L
Linus Torvalds 已提交
1852 1853 1854 1855
	.use_clustering = ENABLE_CLUSTERING,
	.shost_attrs = ibmvscsi_attrs,
};

1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
/**
 * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
 *
 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
 *
 * Return value:
 *	Number of bytes of IO data the driver will need to perform well.
 */
static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
{
	/* iu_storage data allocated in initialize_event_pool */
1867
	unsigned long desired_io = max_events * sizeof(union viosrp_iu);
1868 1869

	/* add io space for sg data */
1870
	desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
1871 1872 1873 1874 1875
	                     IBMVSCSI_CMDS_PER_LUN_DEFAULT);

	return desired_io;
}

L
Linus Torvalds 已提交
1876 1877 1878 1879 1880 1881 1882 1883
/**
 * Called by bus code for each adapter
 */
static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
{
	struct ibmvscsi_host_data *hostdata;
	struct Scsi_Host *host;
	struct device *dev = &vdev->dev;
1884 1885
	struct srp_rport_identifiers ids;
	struct srp_rport *rport;
L
Linus Torvalds 已提交
1886
	unsigned long wait_switch = 0;
1887
	int rc;
L
Linus Torvalds 已提交
1888

1889
	dev_set_drvdata(&vdev->dev, NULL);
L
Linus Torvalds 已提交
1890 1891 1892

	host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
	if (!host) {
1893
		dev_err(&vdev->dev, "couldn't allocate host data\n");
L
Linus Torvalds 已提交
1894 1895 1896
		goto scsi_host_alloc_failed;
	}

1897
	host->transportt = ibmvscsi_transport_template;
1898
	hostdata = shost_priv(host);
L
Linus Torvalds 已提交
1899 1900 1901 1902 1903
	memset(hostdata, 0x00, sizeof(*hostdata));
	INIT_LIST_HEAD(&hostdata->sent);
	hostdata->host = host;
	hostdata->dev = dev;
	atomic_set(&hostdata->request_limit, -1);
1904
	hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
L
Linus Torvalds 已提交
1905

1906 1907 1908 1909 1910
	if (map_persist_bufs(hostdata)) {
		dev_err(&vdev->dev, "couldn't map persistent buffers\n");
		goto persist_bufs_failed;
	}

1911
	rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
1912
	if (rc != 0 && rc != H_RESOURCE) {
1913
		dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
L
Linus Torvalds 已提交
1914 1915
		goto init_crq_failed;
	}
1916
	if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
1917
		dev_err(&vdev->dev, "couldn't initialize event pool\n");
L
Linus Torvalds 已提交
1918 1919 1920 1921 1922 1923
		goto init_pool_failed;
	}

	host->max_lun = 8;
	host->max_id = max_id;
	host->max_channel = max_channel;
1924
	host->max_cmd_len = 16;
L
Linus Torvalds 已提交
1925 1926 1927 1928

	if (scsi_add_host(hostdata->host, hostdata->dev))
		goto add_host_failed;

1929 1930 1931
	/* we don't have a proper target_port_id so let's use the fake one */
	memcpy(ids.port_id, hostdata->madapter_info.partition_name,
	       sizeof(ids.port_id));
1932
	ids.roles = SRP_RPORT_ROLE_TARGET;
1933 1934 1935 1936
	rport = srp_rport_add(host, &ids);
	if (IS_ERR(rport))
		goto add_srp_port_failed;

L
Linus Torvalds 已提交
1937 1938 1939 1940
	/* Try to send an initialization message.  Note that this is allowed
	 * to fail if the other end is not acive.  In that case we don't
	 * want to scan
	 */
1941
	if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
1942
	    || rc == H_RESOURCE) {
L
Linus Torvalds 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
		/*
		 * Wait around max init_timeout secs for the adapter to finish
		 * initializing. When we are done initializing, we will have a
		 * valid request_limit.  We don't want Linux scanning before
		 * we are ready.
		 */
		for (wait_switch = jiffies + (init_timeout * HZ);
		     time_before(jiffies, wait_switch) &&
		     atomic_read(&hostdata->request_limit) < 2;) {

			msleep(10);
		}

		/* if we now have a valid request_limit, initiate a scan */
		if (atomic_read(&hostdata->request_limit) > 0)
			scsi_scan_host(host);
	}

1961
	dev_set_drvdata(&vdev->dev, hostdata);
L
Linus Torvalds 已提交
1962 1963
	return 0;

1964 1965
      add_srp_port_failed:
	scsi_remove_host(hostdata->host);
L
Linus Torvalds 已提交
1966 1967 1968
      add_host_failed:
	release_event_pool(&hostdata->pool, hostdata);
      init_pool_failed:
1969
	ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
L
Linus Torvalds 已提交
1970
      init_crq_failed:
1971 1972
	unmap_persist_bufs(hostdata);
      persist_bufs_failed:
L
Linus Torvalds 已提交
1973 1974 1975 1976 1977 1978 1979
	scsi_host_put(host);
      scsi_host_alloc_failed:
	return -1;
}

static int ibmvscsi_remove(struct vio_dev *vdev)
{
1980
	struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
1981
	unmap_persist_bufs(hostdata);
L
Linus Torvalds 已提交
1982
	release_event_pool(&hostdata->pool, hostdata);
1983
	ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
1984
					max_events);
1985 1986

	srp_remove_host(hostdata->host);
L
Linus Torvalds 已提交
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
	scsi_remove_host(hostdata->host);
	scsi_host_put(hostdata->host);

	return 0;
}

/**
 * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we 
 * support.
 */
static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
	{"vscsi", "IBM,v-scsi"},
1999
	{ "", "" }
L
Linus Torvalds 已提交
2000 2001
};
MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
2002

L
Linus Torvalds 已提交
2003 2004 2005
static struct vio_driver ibmvscsi_driver = {
	.id_table = ibmvscsi_device_table,
	.probe = ibmvscsi_probe,
2006
	.remove = ibmvscsi_remove,
2007
	.get_desired_dma = ibmvscsi_get_desired_dma,
2008 2009
	.driver = {
		.name = "ibmvscsi",
2010
		.owner = THIS_MODULE,
2011
	}
L
Linus Torvalds 已提交
2012 2013
};

2014 2015 2016
static struct srp_function_template ibmvscsi_transport_functions = {
};

L
Linus Torvalds 已提交
2017 2018
int __init ibmvscsi_module_init(void)
{
2019 2020
	int ret;

2021 2022 2023 2024
	/* Ensure we have two requests to do error recovery */
	driver_template.can_queue = max_requests;
	max_events = max_requests + 2;

2025 2026 2027 2028 2029 2030 2031
	if (firmware_has_feature(FW_FEATURE_ISERIES))
		ibmvscsi_ops = &iseriesvscsi_ops;
	else if (firmware_has_feature(FW_FEATURE_VIO))
		ibmvscsi_ops = &rpavscsi_ops;
	else
		return -ENODEV;

2032 2033 2034 2035 2036 2037 2038 2039 2040
	ibmvscsi_transport_template =
		srp_attach_transport(&ibmvscsi_transport_functions);
	if (!ibmvscsi_transport_template)
		return -ENOMEM;

	ret = vio_register_driver(&ibmvscsi_driver);
	if (ret)
		srp_release_transport(ibmvscsi_transport_template);
	return ret;
L
Linus Torvalds 已提交
2041 2042 2043 2044 2045
}

void __exit ibmvscsi_module_exit(void)
{
	vio_unregister_driver(&ibmvscsi_driver);
2046
	srp_release_transport(ibmvscsi_transport_template);
L
Linus Torvalds 已提交
2047 2048 2049 2050
}

module_init(ibmvscsi_module_init);
module_exit(ibmvscsi_module_exit);