xref: /openbmc/linux/drivers/scsi/ibmvscsi/ibmvscsi.c (revision 98840f2c)
1 /* ------------------------------------------------------------
2  * ibmvscsi.c
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)
7  *
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.
12  *
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.
17  *
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
21  * USA
22  *
23  * ------------------------------------------------------------
24  * Emulation of a SCSI host adapter for Virtual I/O devices
25  *
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.
30  *
31  * The virtual adapter(s) are present in the open firmware device
32  * tree just like real adapters.
33  *
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.
38  *
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
47  * the message.
48  *
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
56  * completed.
57  *
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.
63  *
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
66  * layer.
67  */
68 
69 #include <linux/module.h>
70 #include <linux/moduleparam.h>
71 #include <linux/dma-mapping.h>
72 #include <linux/delay.h>
73 #include <linux/of.h>
74 #include <asm/firmware.h>
75 #include <asm/vio.h>
76 #include <scsi/scsi.h>
77 #include <scsi/scsi_cmnd.h>
78 #include <scsi/scsi_host.h>
79 #include <scsi/scsi_device.h>
80 #include <scsi/scsi_transport_srp.h>
81 #include "ibmvscsi.h"
82 
83 /* The values below are somewhat arbitrary default values, but
84  * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
85  * Note that there are 3 bits of channel value, 6 bits of id, and
86  * 5 bits of LUN.
87  */
88 static int max_id = 64;
89 static int max_channel = 3;
90 static int init_timeout = 300;
91 static int login_timeout = 60;
92 static int info_timeout = 30;
93 static int abort_timeout = 60;
94 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;
99 
100 static struct scsi_transport_template *ibmvscsi_transport_template;
101 
102 #define IBMVSCSI_VERSION "1.5.8"
103 
104 static struct ibmvscsi_ops *ibmvscsi_ops;
105 
106 MODULE_DESCRIPTION("IBM Virtual SCSI");
107 MODULE_AUTHOR("Dave Boutcher");
108 MODULE_LICENSE("GPL");
109 MODULE_VERSION(IBMVSCSI_VERSION);
110 
111 module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
112 MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
113 module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
114 MODULE_PARM_DESC(max_channel, "Largest channel value");
115 module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
116 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
117 module_param_named(max_requests, max_requests, int, S_IRUGO);
118 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
119 module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
120 MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
121 module_param_named(client_reserve, client_reserve, int, S_IRUGO );
122 MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
123 
124 /* ------------------------------------------------------------
125  * Routines for the event pool and event structs
126  */
127 /**
128  * initialize_event_pool: - Allocates and initializes the event pool for a host
129  * @pool:	event_pool to be initialized
130  * @size:	Number of events in pool
131  * @hostdata:	ibmvscsi_host_data who owns the event pool
132  *
133  * Returns zero on success.
134 */
135 static int initialize_event_pool(struct event_pool *pool,
136 				 int size, struct ibmvscsi_host_data *hostdata)
137 {
138 	int i;
139 
140 	pool->size = size;
141 	pool->next = 0;
142 	pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
143 	if (!pool->events)
144 		return -ENOMEM;
145 
146 	pool->iu_storage =
147 	    dma_alloc_coherent(hostdata->dev,
148 			       pool->size * sizeof(*pool->iu_storage),
149 			       &pool->iu_token, 0);
150 	if (!pool->iu_storage) {
151 		kfree(pool->events);
152 		return -ENOMEM;
153 	}
154 
155 	for (i = 0; i < pool->size; ++i) {
156 		struct srp_event_struct *evt = &pool->events[i];
157 		memset(&evt->crq, 0x00, sizeof(evt->crq));
158 		atomic_set(&evt->free, 1);
159 		evt->crq.valid = 0x80;
160 		evt->crq.IU_length = sizeof(*evt->xfer_iu);
161 		evt->crq.IU_data_ptr = pool->iu_token +
162 			sizeof(*evt->xfer_iu) * i;
163 		evt->xfer_iu = pool->iu_storage + i;
164 		evt->hostdata = hostdata;
165 		evt->ext_list = NULL;
166 		evt->ext_list_token = 0;
167 	}
168 
169 	return 0;
170 }
171 
172 /**
173  * release_event_pool: - Frees memory of an event pool of a host
174  * @pool:	event_pool to be released
175  * @hostdata:	ibmvscsi_host_data who owns the even pool
176  *
177  * Returns zero on success.
178 */
179 static void release_event_pool(struct event_pool *pool,
180 			       struct ibmvscsi_host_data *hostdata)
181 {
182 	int i, in_use = 0;
183 	for (i = 0; i < pool->size; ++i) {
184 		if (atomic_read(&pool->events[i].free) != 1)
185 			++in_use;
186 		if (pool->events[i].ext_list) {
187 			dma_free_coherent(hostdata->dev,
188 				  SG_ALL * sizeof(struct srp_direct_buf),
189 				  pool->events[i].ext_list,
190 				  pool->events[i].ext_list_token);
191 		}
192 	}
193 	if (in_use)
194 		dev_warn(hostdata->dev, "releasing event pool with %d "
195 			 "events still in use?\n", in_use);
196 	kfree(pool->events);
197 	dma_free_coherent(hostdata->dev,
198 			  pool->size * sizeof(*pool->iu_storage),
199 			  pool->iu_storage, pool->iu_token);
200 }
201 
202 /**
203  * valid_event_struct: - Determines if event is valid.
204  * @pool:	event_pool that contains the event
205  * @evt:	srp_event_struct to be checked for validity
206  *
207  * Returns zero if event is invalid, one otherwise.
208 */
209 static int valid_event_struct(struct event_pool *pool,
210 				struct srp_event_struct *evt)
211 {
212 	int index = evt - pool->events;
213 	if (index < 0 || index >= pool->size)	/* outside of bounds */
214 		return 0;
215 	if (evt != pool->events + index)	/* unaligned */
216 		return 0;
217 	return 1;
218 }
219 
220 /**
221  * ibmvscsi_free-event_struct: - Changes status of event to "free"
222  * @pool:	event_pool that contains the event
223  * @evt:	srp_event_struct to be modified
224  *
225 */
226 static void free_event_struct(struct event_pool *pool,
227 				       struct srp_event_struct *evt)
228 {
229 	if (!valid_event_struct(pool, evt)) {
230 		dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
231 			"(not in pool %p)\n", evt, pool->events);
232 		return;
233 	}
234 	if (atomic_inc_return(&evt->free) != 1) {
235 		dev_err(evt->hostdata->dev, "Freeing event_struct %p "
236 			"which is not in use!\n", evt);
237 		return;
238 	}
239 }
240 
241 /**
242  * get_evt_struct: - Gets the next free event in pool
243  * @pool:	event_pool that contains the events to be searched
244  *
245  * Returns the next event in "free" state, and NULL if none are free.
246  * Note that no synchronization is done here, we assume the host_lock
247  * will syncrhonze things.
248 */
249 static struct srp_event_struct *get_event_struct(struct event_pool *pool)
250 {
251 	int i;
252 	int poolsize = pool->size;
253 	int offset = pool->next;
254 
255 	for (i = 0; i < poolsize; i++) {
256 		offset = (offset + 1) % poolsize;
257 		if (!atomic_dec_if_positive(&pool->events[offset].free)) {
258 			pool->next = offset;
259 			return &pool->events[offset];
260 		}
261 	}
262 
263 	printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
264 	return NULL;
265 }
266 
267 /**
268  * init_event_struct: Initialize fields in an event struct that are always
269  *                    required.
270  * @evt:        The event
271  * @done:       Routine to call when the event is responded to
272  * @format:     SRP or MAD format
273  * @timeout:    timeout value set in the CRQ
274  */
275 static void init_event_struct(struct srp_event_struct *evt_struct,
276 			      void (*done) (struct srp_event_struct *),
277 			      u8 format,
278 			      int timeout)
279 {
280 	evt_struct->cmnd = NULL;
281 	evt_struct->cmnd_done = NULL;
282 	evt_struct->sync_srp = NULL;
283 	evt_struct->crq.format = format;
284 	evt_struct->crq.timeout = timeout;
285 	evt_struct->done = done;
286 }
287 
288 /* ------------------------------------------------------------
289  * Routines for receiving SCSI responses from the hosting partition
290  */
291 
292 /**
293  * set_srp_direction: Set the fields in the srp related to data
294  *     direction and number of buffers based on the direction in
295  *     the scsi_cmnd and the number of buffers
296  */
297 static void set_srp_direction(struct scsi_cmnd *cmd,
298 			      struct srp_cmd *srp_cmd,
299 			      int numbuf)
300 {
301 	u8 fmt;
302 
303 	if (numbuf == 0)
304 		return;
305 
306 	if (numbuf == 1)
307 		fmt = SRP_DATA_DESC_DIRECT;
308 	else {
309 		fmt = SRP_DATA_DESC_INDIRECT;
310 		numbuf = min(numbuf, MAX_INDIRECT_BUFS);
311 
312 		if (cmd->sc_data_direction == DMA_TO_DEVICE)
313 			srp_cmd->data_out_desc_cnt = numbuf;
314 		else
315 			srp_cmd->data_in_desc_cnt = numbuf;
316 	}
317 
318 	if (cmd->sc_data_direction == DMA_TO_DEVICE)
319 		srp_cmd->buf_fmt = fmt << 4;
320 	else
321 		srp_cmd->buf_fmt = fmt;
322 }
323 
324 static void unmap_sg_list(int num_entries,
325 		struct device *dev,
326 		struct srp_direct_buf *md)
327 {
328 	int i;
329 
330 	for (i = 0; i < num_entries; ++i)
331 		dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
332 }
333 
334 /**
335  * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
336  * @cmd:	srp_cmd whose additional_data member will be unmapped
337  * @dev:	device for which the memory is mapped
338  *
339 */
340 static void unmap_cmd_data(struct srp_cmd *cmd,
341 			   struct srp_event_struct *evt_struct,
342 			   struct device *dev)
343 {
344 	u8 out_fmt, in_fmt;
345 
346 	out_fmt = cmd->buf_fmt >> 4;
347 	in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
348 
349 	if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
350 		return;
351 	else if (out_fmt == SRP_DATA_DESC_DIRECT ||
352 		 in_fmt == SRP_DATA_DESC_DIRECT) {
353 		struct srp_direct_buf *data =
354 			(struct srp_direct_buf *) cmd->add_data;
355 		dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
356 	} else {
357 		struct srp_indirect_buf *indirect =
358 			(struct srp_indirect_buf *) cmd->add_data;
359 		int num_mapped = indirect->table_desc.len /
360 			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;
365 		}
366 
367 		unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
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 	u64 total_length = 0;
377 
378 	scsi_for_each_sg(cmd, sg, nseg, i) {
379 		struct srp_direct_buf *descr = md + i;
380 		descr->va = sg_dma_address(sg);
381 		descr->len = sg_dma_len(sg);
382 		descr->key = 0;
383 		total_length += sg_dma_len(sg);
384  	}
385 	return total_length;
386 }
387 
388 /**
389  * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
390  * @cmd:	Scsi_Cmnd with the scatterlist
391  * @srp_cmd:	srp_cmd that contains the memory descriptor
392  * @dev:	device for which to map dma memory
393  *
394  * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
395  * Returns 1 on success.
396 */
397 static int map_sg_data(struct scsi_cmnd *cmd,
398 		       struct srp_event_struct *evt_struct,
399 		       struct srp_cmd *srp_cmd, struct device *dev)
400 {
401 
402 	int sg_mapped;
403 	u64 total_length = 0;
404 	struct srp_direct_buf *data =
405 		(struct srp_direct_buf *) srp_cmd->add_data;
406 	struct srp_indirect_buf *indirect =
407 		(struct srp_indirect_buf *) data;
408 
409 	sg_mapped = scsi_dma_map(cmd);
410 	if (!sg_mapped)
411 		return 1;
412 	else if (sg_mapped < 0)
413 		return 0;
414 
415 	set_srp_direction(cmd, srp_cmd, sg_mapped);
416 
417 	/* special case; we can use a single direct descriptor */
418 	if (sg_mapped == 1) {
419 		map_sg_list(cmd, sg_mapped, data);
420 		return 1;
421 	}
422 
423 	indirect->table_desc.va = 0;
424 	indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
425 	indirect->table_desc.key = 0;
426 
427 	if (sg_mapped <= MAX_INDIRECT_BUFS) {
428 		total_length = map_sg_list(cmd, sg_mapped,
429 					   &indirect->desc_list[0]);
430 		indirect->len = total_length;
431 		return 1;
432 	}
433 
434 	/* get indirect table */
435 	if (!evt_struct->ext_list) {
436 		evt_struct->ext_list = (struct srp_direct_buf *)
437 			dma_alloc_coherent(dev,
438 					   SG_ALL * sizeof(struct srp_direct_buf),
439 					   &evt_struct->ext_list_token, 0);
440 		if (!evt_struct->ext_list) {
441 			if (!firmware_has_feature(FW_FEATURE_CMO))
442 				sdev_printk(KERN_ERR, cmd->device,
443 				            "Can't allocate memory "
444 				            "for indirect table\n");
445 			scsi_dma_unmap(cmd);
446 			return 0;
447 		}
448 	}
449 
450 	total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
451 
452 	indirect->len = total_length;
453 	indirect->table_desc.va = evt_struct->ext_list_token;
454 	indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
455 	memcpy(indirect->desc_list, evt_struct->ext_list,
456 	       MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
457  	return 1;
458 }
459 
460 /**
461  * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
462  * @cmd:	struct scsi_cmnd with the memory to be mapped
463  * @srp_cmd:	srp_cmd that contains the memory descriptor
464  * @dev:	dma device for which to map dma memory
465  *
466  * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
467  * Returns 1 on success.
468 */
469 static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
470 				struct srp_event_struct *evt_struct,
471 				struct srp_cmd *srp_cmd, struct device *dev)
472 {
473 	switch (cmd->sc_data_direction) {
474 	case DMA_FROM_DEVICE:
475 	case DMA_TO_DEVICE:
476 		break;
477 	case DMA_NONE:
478 		return 1;
479 	case DMA_BIDIRECTIONAL:
480 		sdev_printk(KERN_ERR, cmd->device,
481 			    "Can't map DMA_BIDIRECTIONAL to read/write\n");
482 		return 0;
483 	default:
484 		sdev_printk(KERN_ERR, cmd->device,
485 			    "Unknown data direction 0x%02x; can't map!\n",
486 			    cmd->sc_data_direction);
487 		return 0;
488 	}
489 
490 	return map_sg_data(cmd, evt_struct, srp_cmd, dev);
491 }
492 
493 /**
494  * purge_requests: Our virtual adapter just shut down.  purge any sent requests
495  * @hostdata:    the adapter
496  */
497 static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
498 {
499 	struct srp_event_struct *tmp_evt, *pos;
500 	unsigned long flags;
501 
502 	spin_lock_irqsave(hostdata->host->host_lock, flags);
503 	list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
504 		list_del(&tmp_evt->list);
505 		del_timer(&tmp_evt->timer);
506 		if (tmp_evt->cmnd) {
507 			tmp_evt->cmnd->result = (error_code << 16);
508 			unmap_cmd_data(&tmp_evt->iu.srp.cmd,
509 				       tmp_evt,
510 				       tmp_evt->hostdata->dev);
511 			if (tmp_evt->cmnd_done)
512 				tmp_evt->cmnd_done(tmp_evt->cmnd);
513 		} else if (tmp_evt->done)
514 			tmp_evt->done(tmp_evt);
515 		free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
516 	}
517 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
518 }
519 
520 /**
521  * ibmvscsi_reset_host - Reset the connection to the server
522  * @hostdata:	struct ibmvscsi_host_data to reset
523 */
524 static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
525 {
526 	scsi_block_requests(hostdata->host);
527 	atomic_set(&hostdata->request_limit, 0);
528 
529 	purge_requests(hostdata, DID_ERROR);
530 	if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
531 	    (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
532 	    (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
533 		atomic_set(&hostdata->request_limit, -1);
534 		dev_err(hostdata->dev, "error after reset\n");
535 	}
536 
537 	scsi_unblock_requests(hostdata->host);
538 }
539 
540 /**
541  * ibmvscsi_timeout - Internal command timeout handler
542  * @evt_struct:	struct srp_event_struct that timed out
543  *
544  * Called when an internally generated command times out
545 */
546 static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
547 {
548 	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
549 
550 	dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
551 		evt_struct->iu.srp.cmd.opcode);
552 
553 	ibmvscsi_reset_host(hostdata);
554 }
555 
556 
557 /* ------------------------------------------------------------
558  * Routines for sending and receiving SRPs
559  */
560 /**
561  * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
562  * @evt_struct:	evt_struct to be sent
563  * @hostdata:	ibmvscsi_host_data of host
564  * @timeout:	timeout in seconds - 0 means do not time command
565  *
566  * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
567  * Note that this routine assumes that host_lock is held for synchronization
568 */
569 static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
570 				   struct ibmvscsi_host_data *hostdata,
571 				   unsigned long timeout)
572 {
573 	u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
574 	int request_status = 0;
575 	int rc;
576 
577 	/* If we have exhausted our request limit, just fail this request,
578 	 * unless it is for a reset or abort.
579 	 * Note that there are rare cases involving driver generated requests
580 	 * (such as task management requests) that the mid layer may think we
581 	 * can handle more requests (can_queue) when we actually can't
582 	 */
583 	if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
584 		request_status =
585 			atomic_dec_if_positive(&hostdata->request_limit);
586 		/* If request limit was -1 when we started, it is now even
587 		 * less than that
588 		 */
589 		if (request_status < -1)
590 			goto send_error;
591 		/* Otherwise, we may have run out of requests. */
592 		/* If request limit was 0 when we started the adapter is in the
593 		 * process of performing a login with the server adapter, or
594 		 * we may have run out of requests.
595 		 */
596 		else if (request_status == -1 &&
597 		         evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
598 			goto send_busy;
599 		/* Abort and reset calls should make it through.
600 		 * Nothing except abort and reset should use the last two
601 		 * slots unless we had two or less to begin with.
602 		 */
603 		else if (request_status < 2 &&
604 		         evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
605 			/* In the case that we have less than two requests
606 			 * available, check the server limit as a combination
607 			 * of the request limit and the number of requests
608 			 * in-flight (the size of the send list).  If the
609 			 * server limit is greater than 2, return busy so
610 			 * that the last two are reserved for reset and abort.
611 			 */
612 			int server_limit = request_status;
613 			struct srp_event_struct *tmp_evt;
614 
615 			list_for_each_entry(tmp_evt, &hostdata->sent, list) {
616 				server_limit++;
617 			}
618 
619 			if (server_limit > 2)
620 				goto send_busy;
621 		}
622 	}
623 
624 	/* Copy the IU into the transfer area */
625 	*evt_struct->xfer_iu = evt_struct->iu;
626 	evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
627 
628 	/* Add this to the sent list.  We need to do this
629 	 * before we actually send
630 	 * in case it comes back REALLY fast
631 	 */
632 	list_add_tail(&evt_struct->list, &hostdata->sent);
633 
634 	init_timer(&evt_struct->timer);
635 	if (timeout) {
636 		evt_struct->timer.data = (unsigned long) evt_struct;
637 		evt_struct->timer.expires = jiffies + (timeout * HZ);
638 		evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
639 		add_timer(&evt_struct->timer);
640 	}
641 
642 	if ((rc =
643 	     ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
644 		list_del(&evt_struct->list);
645 		del_timer(&evt_struct->timer);
646 
647 		/* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
648 		 * Firmware will send a CRQ with a transport event (0xFF) to
649 		 * tell this client what has happened to the transport.  This
650 		 * will be handled in ibmvscsi_handle_crq()
651 		 */
652 		if (rc == H_CLOSED) {
653 			dev_warn(hostdata->dev, "send warning. "
654 			         "Receive queue closed, will retry.\n");
655 			goto send_busy;
656 		}
657 		dev_err(hostdata->dev, "send error %d\n", rc);
658 		atomic_inc(&hostdata->request_limit);
659 		goto send_error;
660 	}
661 
662 	return 0;
663 
664  send_busy:
665 	unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
666 
667 	free_event_struct(&hostdata->pool, evt_struct);
668 	if (request_status != -1)
669 		atomic_inc(&hostdata->request_limit);
670 	return SCSI_MLQUEUE_HOST_BUSY;
671 
672  send_error:
673 	unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
674 
675 	if (evt_struct->cmnd != NULL) {
676 		evt_struct->cmnd->result = DID_ERROR << 16;
677 		evt_struct->cmnd_done(evt_struct->cmnd);
678 	} else if (evt_struct->done)
679 		evt_struct->done(evt_struct);
680 
681 	free_event_struct(&hostdata->pool, evt_struct);
682 	return 0;
683 }
684 
685 /**
686  * handle_cmd_rsp: -  Handle responses from commands
687  * @evt_struct:	srp_event_struct to be handled
688  *
689  * Used as a callback by when sending scsi cmds.
690  * Gets called by ibmvscsi_handle_crq()
691 */
692 static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
693 {
694 	struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
695 	struct scsi_cmnd *cmnd = evt_struct->cmnd;
696 
697 	if (unlikely(rsp->opcode != SRP_RSP)) {
698 		if (printk_ratelimit())
699 			dev_warn(evt_struct->hostdata->dev,
700 				 "bad SRP RSP type %d\n", rsp->opcode);
701 	}
702 
703 	if (cmnd) {
704 		cmnd->result |= rsp->status;
705 		if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
706 			memcpy(cmnd->sense_buffer,
707 			       rsp->data,
708 			       rsp->sense_data_len);
709 		unmap_cmd_data(&evt_struct->iu.srp.cmd,
710 			       evt_struct,
711 			       evt_struct->hostdata->dev);
712 
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);
717 	}
718 
719 	if (evt_struct->cmnd_done)
720 		evt_struct->cmnd_done(cmnd);
721 }
722 
723 /**
724  * lun_from_dev: - Returns the lun of the scsi device
725  * @dev:	struct scsi_device
726  *
727 */
728 static inline u16 lun_from_dev(struct scsi_device *dev)
729 {
730 	return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
731 }
732 
733 /**
734  * ibmvscsi_queue: - The queuecommand function of the scsi template
735  * @cmd:	struct scsi_cmnd to be executed
736  * @done:	Callback function to be called when cmd is completed
737 */
738 static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
739 				 void (*done) (struct scsi_cmnd *))
740 {
741 	struct srp_cmd *srp_cmd;
742 	struct srp_event_struct *evt_struct;
743 	struct srp_indirect_buf *indirect;
744 	struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
745 	u16 lun = lun_from_dev(cmnd->device);
746 	u8 out_fmt, in_fmt;
747 
748 	cmnd->result = (DID_OK << 16);
749 	evt_struct = get_event_struct(&hostdata->pool);
750 	if (!evt_struct)
751 		return SCSI_MLQUEUE_HOST_BUSY;
752 
753 	/* Set up the actual SRP IU */
754 	srp_cmd = &evt_struct->iu.srp.cmd;
755 	memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
756 	srp_cmd->opcode = SRP_CMD;
757 	memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
758 	srp_cmd->lun = ((u64) lun) << 48;
759 
760 	if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
761 		if (!firmware_has_feature(FW_FEATURE_CMO))
762 			sdev_printk(KERN_ERR, cmnd->device,
763 			            "couldn't convert cmd to srp_cmd\n");
764 		free_event_struct(&hostdata->pool, evt_struct);
765 		return SCSI_MLQUEUE_HOST_BUSY;
766 	}
767 
768 	init_event_struct(evt_struct,
769 			  handle_cmd_rsp,
770 			  VIOSRP_SRP_FORMAT,
771 			  cmnd->request->timeout/HZ);
772 
773 	evt_struct->cmnd = cmnd;
774 	evt_struct->cmnd_done = done;
775 
776 	/* Fix up dma address of the buffer itself */
777 	indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
778 	out_fmt = srp_cmd->buf_fmt >> 4;
779 	in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
780 	if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
781 	     out_fmt == SRP_DATA_DESC_INDIRECT) &&
782 	    indirect->table_desc.va == 0) {
783 		indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
784 			offsetof(struct srp_cmd, add_data) +
785 			offsetof(struct srp_indirect_buf, desc_list);
786 	}
787 
788 	return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
789 }
790 
791 /* ------------------------------------------------------------
792  * Routines for driver initialization
793  */
794 
795 /**
796  * map_persist_bufs: - Pre-map persistent data for adapter logins
797  * @hostdata:   ibmvscsi_host_data of host
798  *
799  * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
800  * Return 1 on error, 0 on success.
801  */
802 static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
803 {
804 
805 	hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
806 					     sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
807 
808 	if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
809 		dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
810 		return 1;
811 	}
812 
813 	hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
814 						     &hostdata->madapter_info,
815 						     sizeof(hostdata->madapter_info),
816 						     DMA_BIDIRECTIONAL);
817 	if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
818 		dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
819 		dma_unmap_single(hostdata->dev, hostdata->caps_addr,
820 				 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
821 		return 1;
822 	}
823 
824 	return 0;
825 }
826 
827 /**
828  * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
829  * @hostdata:   ibmvscsi_host_data of host
830  *
831  * Unmap the capabilities and adapter info DMA buffers
832  */
833 static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
834 {
835 	dma_unmap_single(hostdata->dev, hostdata->caps_addr,
836 			 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
837 
838 	dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
839 			 sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
840 }
841 
842 /**
843  * login_rsp: - Handle response to SRP login request
844  * @evt_struct:	srp_event_struct with the response
845  *
846  * Used as a "done" callback by when sending srp_login. Gets called
847  * by ibmvscsi_handle_crq()
848 */
849 static void login_rsp(struct srp_event_struct *evt_struct)
850 {
851 	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
852 	switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
853 	case SRP_LOGIN_RSP:	/* it worked! */
854 		break;
855 	case SRP_LOGIN_REJ:	/* refused! */
856 		dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
857 			 evt_struct->xfer_iu->srp.login_rej.reason);
858 		/* Login failed.  */
859 		atomic_set(&hostdata->request_limit, -1);
860 		return;
861 	default:
862 		dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
863 			evt_struct->xfer_iu->srp.login_rsp.opcode);
864 		/* Login failed.  */
865 		atomic_set(&hostdata->request_limit, -1);
866 		return;
867 	}
868 
869 	dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
870 	hostdata->client_migrated = 0;
871 
872 	/* Now we know what the real request-limit is.
873 	 * This value is set rather than added to request_limit because
874 	 * request_limit could have been set to -1 by this client.
875 	 */
876 	atomic_set(&hostdata->request_limit,
877 		   evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
878 
879 	/* If we had any pending I/Os, kick them */
880 	scsi_unblock_requests(hostdata->host);
881 }
882 
883 /**
884  * send_srp_login: - Sends the srp login
885  * @hostdata:	ibmvscsi_host_data of host
886  *
887  * Returns zero if successful.
888 */
889 static int send_srp_login(struct ibmvscsi_host_data *hostdata)
890 {
891 	int rc;
892 	unsigned long flags;
893 	struct srp_login_req *login;
894 	struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
895 
896 	BUG_ON(!evt_struct);
897 	init_event_struct(evt_struct, login_rsp,
898 			  VIOSRP_SRP_FORMAT, login_timeout);
899 
900 	login = &evt_struct->iu.srp.login_req;
901 	memset(login, 0, sizeof(*login));
902 	login->opcode = SRP_LOGIN_REQ;
903 	login->req_it_iu_len = sizeof(union srp_iu);
904 	login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
905 
906 	spin_lock_irqsave(hostdata->host->host_lock, flags);
907 	/* Start out with a request limit of 0, since this is negotiated in
908 	 * the login request we are just sending and login requests always
909 	 * get sent by the driver regardless of request_limit.
910 	 */
911 	atomic_set(&hostdata->request_limit, 0);
912 
913 	rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
914 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
915 	dev_info(hostdata->dev, "sent SRP login\n");
916 	return rc;
917 };
918 
919 /**
920  * capabilities_rsp: - Handle response to MAD adapter capabilities request
921  * @evt_struct:	srp_event_struct with the response
922  *
923  * Used as a "done" callback by when sending adapter_info.
924  */
925 static void capabilities_rsp(struct srp_event_struct *evt_struct)
926 {
927 	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
928 
929 	if (evt_struct->xfer_iu->mad.capabilities.common.status) {
930 		dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
931 			evt_struct->xfer_iu->mad.capabilities.common.status);
932 	} else {
933 		if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
934 			dev_info(hostdata->dev, "Partition migration not supported\n");
935 
936 		if (client_reserve) {
937 			if (hostdata->caps.reserve.common.server_support ==
938 			    SERVER_SUPPORTS_CAP)
939 				dev_info(hostdata->dev, "Client reserve enabled\n");
940 			else
941 				dev_info(hostdata->dev, "Client reserve not supported\n");
942 		}
943 	}
944 
945 	send_srp_login(hostdata);
946 }
947 
948 /**
949  * send_mad_capabilities: - Sends the mad capabilities request
950  *      and stores the result so it can be retrieved with
951  * @hostdata:	ibmvscsi_host_data of host
952  */
953 static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
954 {
955 	struct viosrp_capabilities *req;
956 	struct srp_event_struct *evt_struct;
957 	unsigned long flags;
958 	struct device_node *of_node = hostdata->dev->archdata.of_node;
959 	const char *location;
960 
961 	evt_struct = get_event_struct(&hostdata->pool);
962 	BUG_ON(!evt_struct);
963 
964 	init_event_struct(evt_struct, capabilities_rsp,
965 			  VIOSRP_MAD_FORMAT, info_timeout);
966 
967 	req = &evt_struct->iu.mad.capabilities;
968 	memset(req, 0, sizeof(*req));
969 
970 	hostdata->caps.flags = CAP_LIST_SUPPORTED;
971 	if (hostdata->client_migrated)
972 		hostdata->caps.flags |= CLIENT_MIGRATED;
973 
974 	strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
975 		sizeof(hostdata->caps.name));
976 	hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
977 
978 	location = of_get_property(of_node, "ibm,loc-code", NULL);
979 	location = location ? location : dev_name(hostdata->dev);
980 	strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
981 	hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
982 
983 	req->common.type = VIOSRP_CAPABILITIES_TYPE;
984 	req->buffer = hostdata->caps_addr;
985 
986 	hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
987 	hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
988 	hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
989 	hostdata->caps.migration.ecl = 1;
990 
991 	if (client_reserve) {
992 		hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
993 		hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
994 		hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
995 		hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
996 		req->common.length = sizeof(hostdata->caps);
997 	} else
998 		req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);
999 
1000 	spin_lock_irqsave(hostdata->host->host_lock, flags);
1001 	if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1002 		dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
1003 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1004 };
1005 
1006 /**
1007  * fast_fail_rsp: - Handle response to MAD enable fast fail
1008  * @evt_struct:	srp_event_struct with the response
1009  *
1010  * Used as a "done" callback by when sending enable fast fail. Gets called
1011  * by ibmvscsi_handle_crq()
1012  */
1013 static void fast_fail_rsp(struct srp_event_struct *evt_struct)
1014 {
1015 	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1016 	u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
1017 
1018 	if (status == VIOSRP_MAD_NOT_SUPPORTED)
1019 		dev_err(hostdata->dev, "fast_fail not supported in server\n");
1020 	else if (status == VIOSRP_MAD_FAILED)
1021 		dev_err(hostdata->dev, "fast_fail request failed\n");
1022 	else if (status != VIOSRP_MAD_SUCCESS)
1023 		dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
1024 
1025 	send_mad_capabilities(hostdata);
1026 }
1027 
1028 /**
1029  * init_host - Start host initialization
1030  * @hostdata:	ibmvscsi_host_data of host
1031  *
1032  * Returns zero if successful.
1033  */
1034 static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
1035 {
1036 	int rc;
1037 	unsigned long flags;
1038 	struct viosrp_fast_fail *fast_fail_mad;
1039 	struct srp_event_struct *evt_struct;
1040 
1041 	if (!fast_fail) {
1042 		send_mad_capabilities(hostdata);
1043 		return 0;
1044 	}
1045 
1046 	evt_struct = get_event_struct(&hostdata->pool);
1047 	BUG_ON(!evt_struct);
1048 
1049 	init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
1050 
1051 	fast_fail_mad = &evt_struct->iu.mad.fast_fail;
1052 	memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
1053 	fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
1054 	fast_fail_mad->common.length = sizeof(*fast_fail_mad);
1055 
1056 	spin_lock_irqsave(hostdata->host->host_lock, flags);
1057 	rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1058 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1059 	return rc;
1060 }
1061 
1062 /**
1063  * adapter_info_rsp: - Handle response to MAD adapter info request
1064  * @evt_struct:	srp_event_struct with the response
1065  *
1066  * Used as a "done" callback by when sending adapter_info. Gets called
1067  * by ibmvscsi_handle_crq()
1068 */
1069 static void adapter_info_rsp(struct srp_event_struct *evt_struct)
1070 {
1071 	struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1072 
1073 	if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
1074 		dev_err(hostdata->dev, "error %d getting adapter info\n",
1075 			evt_struct->xfer_iu->mad.adapter_info.common.status);
1076 	} else {
1077 		dev_info(hostdata->dev, "host srp version: %s, "
1078 			 "host partition %s (%d), OS %d, max io %u\n",
1079 			 hostdata->madapter_info.srp_version,
1080 			 hostdata->madapter_info.partition_name,
1081 			 hostdata->madapter_info.partition_number,
1082 			 hostdata->madapter_info.os_type,
1083 			 hostdata->madapter_info.port_max_txu[0]);
1084 
1085 		if (hostdata->madapter_info.port_max_txu[0])
1086 			hostdata->host->max_sectors =
1087 				hostdata->madapter_info.port_max_txu[0] >> 9;
1088 
1089 		if (hostdata->madapter_info.os_type == 3 &&
1090 		    strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
1091 			dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
1092 				hostdata->madapter_info.srp_version);
1093 			dev_err(hostdata->dev, "limiting scatterlists to %d\n",
1094 				MAX_INDIRECT_BUFS);
1095 			hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
1096 		}
1097 
1098 		if (hostdata->madapter_info.os_type == 3) {
1099 			enable_fast_fail(hostdata);
1100 			return;
1101 		}
1102 	}
1103 
1104 	send_srp_login(hostdata);
1105 }
1106 
1107 /**
1108  * send_mad_adapter_info: - Sends the mad adapter info request
1109  *      and stores the result so it can be retrieved with
1110  *      sysfs.  We COULD consider causing a failure if the
1111  *      returned SRP version doesn't match ours.
1112  * @hostdata:	ibmvscsi_host_data of host
1113  *
1114  * Returns zero if successful.
1115 */
1116 static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
1117 {
1118 	struct viosrp_adapter_info *req;
1119 	struct srp_event_struct *evt_struct;
1120 	unsigned long flags;
1121 
1122 	evt_struct = get_event_struct(&hostdata->pool);
1123 	BUG_ON(!evt_struct);
1124 
1125 	init_event_struct(evt_struct,
1126 			  adapter_info_rsp,
1127 			  VIOSRP_MAD_FORMAT,
1128 			  info_timeout);
1129 
1130 	req = &evt_struct->iu.mad.adapter_info;
1131 	memset(req, 0x00, sizeof(*req));
1132 
1133 	req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
1134 	req->common.length = sizeof(hostdata->madapter_info);
1135 	req->buffer = hostdata->adapter_info_addr;
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);
1141 };
1142 
1143 /**
1144  * init_adapter: Start virtual adapter initialization sequence
1145  *
1146  */
1147 static void init_adapter(struct ibmvscsi_host_data *hostdata)
1148 {
1149 	send_mad_adapter_info(hostdata);
1150 }
1151 
1152 /**
1153  * sync_completion: Signal that a synchronous command has completed
1154  * Note that after returning from this call, the evt_struct is freed.
1155  * the caller waiting on this completion shouldn't touch the evt_struct
1156  * again.
1157  */
1158 static void sync_completion(struct srp_event_struct *evt_struct)
1159 {
1160 	/* copy the response back */
1161 	if (evt_struct->sync_srp)
1162 		*evt_struct->sync_srp = *evt_struct->xfer_iu;
1163 
1164 	complete(&evt_struct->comp);
1165 }
1166 
1167 /**
1168  * ibmvscsi_abort: Abort a command...from scsi host template
1169  * send this over to the server and wait synchronously for the response
1170  */
1171 static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
1172 {
1173 	struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1174 	struct srp_tsk_mgmt *tsk_mgmt;
1175 	struct srp_event_struct *evt;
1176 	struct srp_event_struct *tmp_evt, *found_evt;
1177 	union viosrp_iu srp_rsp;
1178 	int rsp_rc;
1179 	unsigned long flags;
1180 	u16 lun = lun_from_dev(cmd->device);
1181 	unsigned long wait_switch = 0;
1182 
1183 	/* First, find this command in our sent list so we can figure
1184 	 * out the correct tag
1185 	 */
1186 	spin_lock_irqsave(hostdata->host->host_lock, flags);
1187 	wait_switch = jiffies + (init_timeout * HZ);
1188 	do {
1189 		found_evt = NULL;
1190 		list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1191 			if (tmp_evt->cmnd == cmd) {
1192 				found_evt = tmp_evt;
1193 				break;
1194 			}
1195 		}
1196 
1197 		if (!found_evt) {
1198 			spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1199 			return SUCCESS;
1200 		}
1201 
1202 		evt = get_event_struct(&hostdata->pool);
1203 		if (evt == NULL) {
1204 			spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1205 			sdev_printk(KERN_ERR, cmd->device,
1206 				"failed to allocate abort event\n");
1207 			return FAILED;
1208 		}
1209 
1210 		init_event_struct(evt,
1211 				  sync_completion,
1212 				  VIOSRP_SRP_FORMAT,
1213 				  abort_timeout);
1214 
1215 		tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1216 
1217 		/* Set up an abort SRP command */
1218 		memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1219 		tsk_mgmt->opcode = SRP_TSK_MGMT;
1220 		tsk_mgmt->lun = ((u64) lun) << 48;
1221 		tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
1222 		tsk_mgmt->task_tag = (u64) found_evt;
1223 
1224 		evt->sync_srp = &srp_rsp;
1225 
1226 		init_completion(&evt->comp);
1227 		rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
1228 
1229 		if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1230 			break;
1231 
1232 		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1233 		msleep(10);
1234 		spin_lock_irqsave(hostdata->host->host_lock, flags);
1235 	} while (time_before(jiffies, wait_switch));
1236 
1237 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1238 
1239 	if (rsp_rc != 0) {
1240 		sdev_printk(KERN_ERR, cmd->device,
1241 			    "failed to send abort() event. rc=%d\n", rsp_rc);
1242 		return FAILED;
1243 	}
1244 
1245 	sdev_printk(KERN_INFO, cmd->device,
1246                     "aborting command. lun 0x%llx, tag 0x%llx\n",
1247 		    (((u64) lun) << 48), (u64) found_evt);
1248 
1249 	wait_for_completion(&evt->comp);
1250 
1251 	/* make sure we got a good response */
1252 	if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1253 		if (printk_ratelimit())
1254 			sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
1255 				    srp_rsp.srp.rsp.opcode);
1256 		return FAILED;
1257 	}
1258 
1259 	if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1260 		rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1261 	else
1262 		rsp_rc = srp_rsp.srp.rsp.status;
1263 
1264 	if (rsp_rc) {
1265 		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);
1269 		return FAILED;
1270 	}
1271 
1272 	/* Because we dropped the spinlock above, it's possible
1273 	 * The event is no longer in our list.  Make sure it didn't
1274 	 * complete while we were aborting
1275 	 */
1276 	spin_lock_irqsave(hostdata->host->host_lock, flags);
1277 	found_evt = NULL;
1278 	list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1279 		if (tmp_evt->cmnd == cmd) {
1280 			found_evt = tmp_evt;
1281 			break;
1282 		}
1283 	}
1284 
1285 	if (found_evt == NULL) {
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);
1289 		return SUCCESS;
1290 	}
1291 
1292 	sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
1293 		    tsk_mgmt->task_tag);
1294 
1295 	cmd->result = (DID_ABORT << 16);
1296 	list_del(&found_evt->list);
1297 	unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1298 		       found_evt->hostdata->dev);
1299 	free_event_struct(&found_evt->hostdata->pool, found_evt);
1300 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1301 	atomic_inc(&hostdata->request_limit);
1302 	return SUCCESS;
1303 }
1304 
1305 /**
1306  * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
1307  * template send this over to the server and wait synchronously for the
1308  * response
1309  */
1310 static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1311 {
1312 	struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1313 	struct srp_tsk_mgmt *tsk_mgmt;
1314 	struct srp_event_struct *evt;
1315 	struct srp_event_struct *tmp_evt, *pos;
1316 	union viosrp_iu srp_rsp;
1317 	int rsp_rc;
1318 	unsigned long flags;
1319 	u16 lun = lun_from_dev(cmd->device);
1320 	unsigned long wait_switch = 0;
1321 
1322 	spin_lock_irqsave(hostdata->host->host_lock, flags);
1323 	wait_switch = jiffies + (init_timeout * HZ);
1324 	do {
1325 		evt = get_event_struct(&hostdata->pool);
1326 		if (evt == NULL) {
1327 			spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1328 			sdev_printk(KERN_ERR, cmd->device,
1329 				"failed to allocate reset event\n");
1330 			return FAILED;
1331 		}
1332 
1333 		init_event_struct(evt,
1334 				  sync_completion,
1335 				  VIOSRP_SRP_FORMAT,
1336 				  reset_timeout);
1337 
1338 		tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1339 
1340 		/* Set up a lun reset SRP command */
1341 		memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1342 		tsk_mgmt->opcode = SRP_TSK_MGMT;
1343 		tsk_mgmt->lun = ((u64) lun) << 48;
1344 		tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
1345 
1346 		evt->sync_srp = &srp_rsp;
1347 
1348 		init_completion(&evt->comp);
1349 		rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
1350 
1351 		if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1352 			break;
1353 
1354 		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1355 		msleep(10);
1356 		spin_lock_irqsave(hostdata->host->host_lock, flags);
1357 	} while (time_before(jiffies, wait_switch));
1358 
1359 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1360 
1361 	if (rsp_rc != 0) {
1362 		sdev_printk(KERN_ERR, cmd->device,
1363 			    "failed to send reset event. rc=%d\n", rsp_rc);
1364 		return FAILED;
1365 	}
1366 
1367 	sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
1368 		    (((u64) lun) << 48));
1369 
1370 	wait_for_completion(&evt->comp);
1371 
1372 	/* make sure we got a good response */
1373 	if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1374 		if (printk_ratelimit())
1375 			sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
1376 				    srp_rsp.srp.rsp.opcode);
1377 		return FAILED;
1378 	}
1379 
1380 	if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1381 		rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1382 	else
1383 		rsp_rc = srp_rsp.srp.rsp.status;
1384 
1385 	if (rsp_rc) {
1386 		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);
1390 		return FAILED;
1391 	}
1392 
1393 	/* We need to find all commands for this LUN that have not yet been
1394 	 * responded to, and fail them with DID_RESET
1395 	 */
1396 	spin_lock_irqsave(hostdata->host->host_lock, flags);
1397 	list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1398 		if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1399 			if (tmp_evt->cmnd)
1400 				tmp_evt->cmnd->result = (DID_RESET << 16);
1401 			list_del(&tmp_evt->list);
1402 			unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1403 				       tmp_evt->hostdata->dev);
1404 			free_event_struct(&tmp_evt->hostdata->pool,
1405 						   tmp_evt);
1406 			atomic_inc(&hostdata->request_limit);
1407 			if (tmp_evt->cmnd_done)
1408 				tmp_evt->cmnd_done(tmp_evt->cmnd);
1409 			else if (tmp_evt->done)
1410 				tmp_evt->done(tmp_evt);
1411 		}
1412 	}
1413 	spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1414 	return SUCCESS;
1415 }
1416 
1417 /**
1418  * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
1419  * @cmd:	struct scsi_cmnd having problems
1420 */
1421 static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
1422 {
1423 	unsigned long wait_switch = 0;
1424 	struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1425 
1426 	dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
1427 
1428 	ibmvscsi_reset_host(hostdata);
1429 
1430 	for (wait_switch = jiffies + (init_timeout * HZ);
1431 	     time_before(jiffies, wait_switch) &&
1432 		     atomic_read(&hostdata->request_limit) < 2;) {
1433 
1434 		msleep(10);
1435 	}
1436 
1437 	if (atomic_read(&hostdata->request_limit) <= 0)
1438 		return FAILED;
1439 
1440 	return SUCCESS;
1441 }
1442 
1443 /**
1444  * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1445  * @crq:	Command/Response queue
1446  * @hostdata:	ibmvscsi_host_data of host
1447  *
1448 */
1449 void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1450 			 struct ibmvscsi_host_data *hostdata)
1451 {
1452 	long rc;
1453 	unsigned long flags;
1454 	struct srp_event_struct *evt_struct =
1455 	    (struct srp_event_struct *)crq->IU_data_ptr;
1456 	switch (crq->valid) {
1457 	case 0xC0:		/* initialization */
1458 		switch (crq->format) {
1459 		case 0x01:	/* Initialization message */
1460 			dev_info(hostdata->dev, "partner initialized\n");
1461 			/* Send back a response */
1462 			if ((rc = ibmvscsi_ops->send_crq(hostdata,
1463 							 0xC002000000000000LL, 0)) == 0) {
1464 				/* Now login */
1465 				init_adapter(hostdata);
1466 			} else {
1467 				dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
1468 			}
1469 
1470 			break;
1471 		case 0x02:	/* Initialization response */
1472 			dev_info(hostdata->dev, "partner initialization complete\n");
1473 
1474 			/* Now login */
1475 			init_adapter(hostdata);
1476 			break;
1477 		default:
1478 			dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
1479 		}
1480 		return;
1481 	case 0xFF:	/* Hypervisor telling us the connection is closed */
1482 		scsi_block_requests(hostdata->host);
1483 		atomic_set(&hostdata->request_limit, 0);
1484 		if (crq->format == 0x06) {
1485 			/* 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 			if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
1490 							      hostdata)) ||
1491 			    (ibmvscsi_ops->send_crq(hostdata,
1492 						    0xC001000000000000LL, 0))) {
1493 					atomic_set(&hostdata->request_limit,
1494 						   -1);
1495 					dev_err(hostdata->dev, "error after enable\n");
1496 			}
1497 		} else {
1498 			dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
1499 				crq->format);
1500 
1501 			purge_requests(hostdata, DID_ERROR);
1502 			if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
1503 							   hostdata)) ||
1504 			    (ibmvscsi_ops->send_crq(hostdata,
1505 						    0xC001000000000000LL, 0))) {
1506 					atomic_set(&hostdata->request_limit,
1507 						   -1);
1508 					dev_err(hostdata->dev, "error after reset\n");
1509 			}
1510 		}
1511 		scsi_unblock_requests(hostdata->host);
1512 		return;
1513 	case 0x80:		/* real payload */
1514 		break;
1515 	default:
1516 		dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
1517 			crq->valid);
1518 		return;
1519 	}
1520 
1521 	/* The only kind of payload CRQs we should get are responses to
1522 	 * things we send. Make sure this response is to something we
1523 	 * actually sent
1524 	 */
1525 	if (!valid_event_struct(&hostdata->pool, evt_struct)) {
1526 		dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
1527 		       (void *)crq->IU_data_ptr);
1528 		return;
1529 	}
1530 
1531 	if (atomic_read(&evt_struct->free)) {
1532 		dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
1533 			(void *)crq->IU_data_ptr);
1534 		return;
1535 	}
1536 
1537 	if (crq->format == VIOSRP_SRP_FORMAT)
1538 		atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
1539 			   &hostdata->request_limit);
1540 
1541 	del_timer(&evt_struct->timer);
1542 
1543 	if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
1544 		evt_struct->cmnd->result = DID_ERROR << 16;
1545 	if (evt_struct->done)
1546 		evt_struct->done(evt_struct);
1547 	else
1548 		dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
1549 
1550 	/*
1551 	 * Lock the host_lock before messing with these structures, since we
1552 	 * are running in a task context
1553 	 */
1554 	spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1555 	list_del(&evt_struct->list);
1556 	free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1557 	spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1558 }
1559 
1560 /**
1561  * ibmvscsi_get_host_config: Send the command to the server to get host
1562  * configuration data.  The data is opaque to us.
1563  */
1564 static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1565 				   unsigned char *buffer, int length)
1566 {
1567 	struct viosrp_host_config *host_config;
1568 	struct srp_event_struct *evt_struct;
1569 	unsigned long flags;
1570 	dma_addr_t addr;
1571 	int rc;
1572 
1573 	evt_struct = get_event_struct(&hostdata->pool);
1574 	if (!evt_struct) {
1575 		dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
1576 		return -1;
1577 	}
1578 
1579 	init_event_struct(evt_struct,
1580 			  sync_completion,
1581 			  VIOSRP_MAD_FORMAT,
1582 			  info_timeout);
1583 
1584 	host_config = &evt_struct->iu.mad.host_config;
1585 
1586 	/* Set up a lun reset SRP command */
1587 	memset(host_config, 0x00, sizeof(*host_config));
1588 	host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1589 	host_config->common.length = length;
1590 	host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
1591 						    length,
1592 						    DMA_BIDIRECTIONAL);
1593 
1594 	if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
1595 		if (!firmware_has_feature(FW_FEATURE_CMO))
1596 			dev_err(hostdata->dev,
1597 			        "dma_mapping error getting host config\n");
1598 		free_event_struct(&hostdata->pool, evt_struct);
1599 		return -1;
1600 	}
1601 
1602 	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)
1607 		wait_for_completion(&evt_struct->comp);
1608 	dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
1609 
1610 	return rc;
1611 }
1612 
1613 /**
1614  * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
1615  * @sdev:	struct scsi_device device to configure
1616  *
1617  * Enable allow_restart for a device if it is a disk.  Adjust the
1618  * queue_depth here also as is required by the documentation for
1619  * struct scsi_host_template.
1620  */
1621 static int ibmvscsi_slave_configure(struct scsi_device *sdev)
1622 {
1623 	struct Scsi_Host *shost = sdev->host;
1624 	unsigned long lock_flags = 0;
1625 
1626 	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 	scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
1632 	spin_unlock_irqrestore(shost->host_lock, lock_flags);
1633 	return 0;
1634 }
1635 
1636 /**
1637  * ibmvscsi_change_queue_depth - Change the device's queue depth
1638  * @sdev:	scsi device struct
1639  * @qdepth:	depth to set
1640  *
1641  * Return value:
1642  * 	actual depth set
1643  **/
1644 static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
1645 {
1646 	if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
1647 		qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
1648 
1649 	scsi_adjust_queue_depth(sdev, 0, qdepth);
1650 	return sdev->queue_depth;
1651 }
1652 
1653 /* ------------------------------------------------------------
1654  * sysfs attributes
1655  */
1656 static ssize_t show_host_vhost_loc(struct device *dev,
1657 				   struct device_attribute *attr, char *buf)
1658 {
1659 	struct Scsi_Host *shost = class_to_shost(dev);
1660 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1661 	int len;
1662 
1663 	len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
1664 		       hostdata->caps.loc);
1665 	return len;
1666 }
1667 
1668 static struct device_attribute ibmvscsi_host_vhost_loc = {
1669 	.attr = {
1670 		 .name = "vhost_loc",
1671 		 .mode = S_IRUGO,
1672 		 },
1673 	.show = show_host_vhost_loc,
1674 };
1675 
1676 static ssize_t show_host_vhost_name(struct device *dev,
1677 				    struct device_attribute *attr, char *buf)
1678 {
1679 	struct Scsi_Host *shost = class_to_shost(dev);
1680 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1681 	int len;
1682 
1683 	len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
1684 		       hostdata->caps.name);
1685 	return len;
1686 }
1687 
1688 static struct device_attribute ibmvscsi_host_vhost_name = {
1689 	.attr = {
1690 		 .name = "vhost_name",
1691 		 .mode = S_IRUGO,
1692 		 },
1693 	.show = show_host_vhost_name,
1694 };
1695 
1696 static ssize_t show_host_srp_version(struct device *dev,
1697 				     struct device_attribute *attr, char *buf)
1698 {
1699 	struct Scsi_Host *shost = class_to_shost(dev);
1700 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1701 	int len;
1702 
1703 	len = snprintf(buf, PAGE_SIZE, "%s\n",
1704 		       hostdata->madapter_info.srp_version);
1705 	return len;
1706 }
1707 
1708 static struct device_attribute ibmvscsi_host_srp_version = {
1709 	.attr = {
1710 		 .name = "srp_version",
1711 		 .mode = S_IRUGO,
1712 		 },
1713 	.show = show_host_srp_version,
1714 };
1715 
1716 static ssize_t show_host_partition_name(struct device *dev,
1717 					struct device_attribute *attr,
1718 					char *buf)
1719 {
1720 	struct Scsi_Host *shost = class_to_shost(dev);
1721 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1722 	int len;
1723 
1724 	len = snprintf(buf, PAGE_SIZE, "%s\n",
1725 		       hostdata->madapter_info.partition_name);
1726 	return len;
1727 }
1728 
1729 static struct device_attribute ibmvscsi_host_partition_name = {
1730 	.attr = {
1731 		 .name = "partition_name",
1732 		 .mode = S_IRUGO,
1733 		 },
1734 	.show = show_host_partition_name,
1735 };
1736 
1737 static ssize_t show_host_partition_number(struct device *dev,
1738 					  struct device_attribute *attr,
1739 					  char *buf)
1740 {
1741 	struct Scsi_Host *shost = class_to_shost(dev);
1742 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1743 	int len;
1744 
1745 	len = snprintf(buf, PAGE_SIZE, "%d\n",
1746 		       hostdata->madapter_info.partition_number);
1747 	return len;
1748 }
1749 
1750 static struct device_attribute ibmvscsi_host_partition_number = {
1751 	.attr = {
1752 		 .name = "partition_number",
1753 		 .mode = S_IRUGO,
1754 		 },
1755 	.show = show_host_partition_number,
1756 };
1757 
1758 static ssize_t show_host_mad_version(struct device *dev,
1759 				     struct device_attribute *attr, char *buf)
1760 {
1761 	struct Scsi_Host *shost = class_to_shost(dev);
1762 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1763 	int len;
1764 
1765 	len = snprintf(buf, PAGE_SIZE, "%d\n",
1766 		       hostdata->madapter_info.mad_version);
1767 	return len;
1768 }
1769 
1770 static struct device_attribute ibmvscsi_host_mad_version = {
1771 	.attr = {
1772 		 .name = "mad_version",
1773 		 .mode = S_IRUGO,
1774 		 },
1775 	.show = show_host_mad_version,
1776 };
1777 
1778 static ssize_t show_host_os_type(struct device *dev,
1779 				 struct device_attribute *attr, char *buf)
1780 {
1781 	struct Scsi_Host *shost = class_to_shost(dev);
1782 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1783 	int len;
1784 
1785 	len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
1786 	return len;
1787 }
1788 
1789 static struct device_attribute ibmvscsi_host_os_type = {
1790 	.attr = {
1791 		 .name = "os_type",
1792 		 .mode = S_IRUGO,
1793 		 },
1794 	.show = show_host_os_type,
1795 };
1796 
1797 static ssize_t show_host_config(struct device *dev,
1798 				struct device_attribute *attr, char *buf)
1799 {
1800 	struct Scsi_Host *shost = class_to_shost(dev);
1801 	struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1802 
1803 	/* returns null-terminated host config data */
1804 	if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
1805 		return strlen(buf);
1806 	else
1807 		return 0;
1808 }
1809 
1810 static struct device_attribute ibmvscsi_host_config = {
1811 	.attr = {
1812 		 .name = "config",
1813 		 .mode = S_IRUGO,
1814 		 },
1815 	.show = show_host_config,
1816 };
1817 
1818 static struct device_attribute *ibmvscsi_attrs[] = {
1819 	&ibmvscsi_host_vhost_loc,
1820 	&ibmvscsi_host_vhost_name,
1821 	&ibmvscsi_host_srp_version,
1822 	&ibmvscsi_host_partition_name,
1823 	&ibmvscsi_host_partition_number,
1824 	&ibmvscsi_host_mad_version,
1825 	&ibmvscsi_host_os_type,
1826 	&ibmvscsi_host_config,
1827 	NULL
1828 };
1829 
1830 /* ------------------------------------------------------------
1831  * SCSI driver registration
1832  */
1833 static struct scsi_host_template driver_template = {
1834 	.module = THIS_MODULE,
1835 	.name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
1836 	.proc_name = "ibmvscsi",
1837 	.queuecommand = ibmvscsi_queuecommand,
1838 	.eh_abort_handler = ibmvscsi_eh_abort_handler,
1839 	.eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
1840 	.eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
1841 	.slave_configure = ibmvscsi_slave_configure,
1842 	.change_queue_depth = ibmvscsi_change_queue_depth,
1843 	.cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
1844 	.can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
1845 	.this_id = -1,
1846 	.sg_tablesize = SG_ALL,
1847 	.use_clustering = ENABLE_CLUSTERING,
1848 	.shost_attrs = ibmvscsi_attrs,
1849 };
1850 
1851 /**
1852  * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
1853  *
1854  * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1855  *
1856  * Return value:
1857  *	Number of bytes of IO data the driver will need to perform well.
1858  */
1859 static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
1860 {
1861 	/* iu_storage data allocated in initialize_event_pool */
1862 	unsigned long desired_io = max_events * sizeof(union viosrp_iu);
1863 
1864 	/* add io space for sg data */
1865 	desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
1866 	                     IBMVSCSI_CMDS_PER_LUN_DEFAULT);
1867 
1868 	return desired_io;
1869 }
1870 
1871 /**
1872  * Called by bus code for each adapter
1873  */
1874 static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1875 {
1876 	struct ibmvscsi_host_data *hostdata;
1877 	struct Scsi_Host *host;
1878 	struct device *dev = &vdev->dev;
1879 	struct srp_rport_identifiers ids;
1880 	struct srp_rport *rport;
1881 	unsigned long wait_switch = 0;
1882 	int rc;
1883 
1884 	dev_set_drvdata(&vdev->dev, NULL);
1885 
1886 	host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
1887 	if (!host) {
1888 		dev_err(&vdev->dev, "couldn't allocate host data\n");
1889 		goto scsi_host_alloc_failed;
1890 	}
1891 
1892 	host->transportt = ibmvscsi_transport_template;
1893 	hostdata = shost_priv(host);
1894 	memset(hostdata, 0x00, sizeof(*hostdata));
1895 	INIT_LIST_HEAD(&hostdata->sent);
1896 	hostdata->host = host;
1897 	hostdata->dev = dev;
1898 	atomic_set(&hostdata->request_limit, -1);
1899 	hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
1900 
1901 	if (map_persist_bufs(hostdata)) {
1902 		dev_err(&vdev->dev, "couldn't map persistent buffers\n");
1903 		goto persist_bufs_failed;
1904 	}
1905 
1906 	rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
1907 	if (rc != 0 && rc != H_RESOURCE) {
1908 		dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
1909 		goto init_crq_failed;
1910 	}
1911 	if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
1912 		dev_err(&vdev->dev, "couldn't initialize event pool\n");
1913 		goto init_pool_failed;
1914 	}
1915 
1916 	host->max_lun = 8;
1917 	host->max_id = max_id;
1918 	host->max_channel = max_channel;
1919 	host->max_cmd_len = 16;
1920 
1921 	if (scsi_add_host(hostdata->host, hostdata->dev))
1922 		goto add_host_failed;
1923 
1924 	/* we don't have a proper target_port_id so let's use the fake one */
1925 	memcpy(ids.port_id, hostdata->madapter_info.partition_name,
1926 	       sizeof(ids.port_id));
1927 	ids.roles = SRP_RPORT_ROLE_TARGET;
1928 	rport = srp_rport_add(host, &ids);
1929 	if (IS_ERR(rport))
1930 		goto add_srp_port_failed;
1931 
1932 	/* Try to send an initialization message.  Note that this is allowed
1933 	 * to fail if the other end is not acive.  In that case we don't
1934 	 * want to scan
1935 	 */
1936 	if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
1937 	    || rc == H_RESOURCE) {
1938 		/*
1939 		 * Wait around max init_timeout secs for the adapter to finish
1940 		 * initializing. When we are done initializing, we will have a
1941 		 * valid request_limit.  We don't want Linux scanning before
1942 		 * we are ready.
1943 		 */
1944 		for (wait_switch = jiffies + (init_timeout * HZ);
1945 		     time_before(jiffies, wait_switch) &&
1946 		     atomic_read(&hostdata->request_limit) < 2;) {
1947 
1948 			msleep(10);
1949 		}
1950 
1951 		/* if we now have a valid request_limit, initiate a scan */
1952 		if (atomic_read(&hostdata->request_limit) > 0)
1953 			scsi_scan_host(host);
1954 	}
1955 
1956 	dev_set_drvdata(&vdev->dev, hostdata);
1957 	return 0;
1958 
1959       add_srp_port_failed:
1960 	scsi_remove_host(hostdata->host);
1961       add_host_failed:
1962 	release_event_pool(&hostdata->pool, hostdata);
1963       init_pool_failed:
1964 	ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
1965       init_crq_failed:
1966 	unmap_persist_bufs(hostdata);
1967       persist_bufs_failed:
1968 	scsi_host_put(host);
1969       scsi_host_alloc_failed:
1970 	return -1;
1971 }
1972 
1973 static int ibmvscsi_remove(struct vio_dev *vdev)
1974 {
1975 	struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
1976 	unmap_persist_bufs(hostdata);
1977 	release_event_pool(&hostdata->pool, hostdata);
1978 	ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
1979 					max_events);
1980 
1981 	srp_remove_host(hostdata->host);
1982 	scsi_remove_host(hostdata->host);
1983 	scsi_host_put(hostdata->host);
1984 
1985 	return 0;
1986 }
1987 
1988 /**
1989  * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
1990  * support.
1991  */
1992 static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
1993 	{"vscsi", "IBM,v-scsi"},
1994 	{ "", "" }
1995 };
1996 MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
1997 
1998 static struct vio_driver ibmvscsi_driver = {
1999 	.id_table = ibmvscsi_device_table,
2000 	.probe = ibmvscsi_probe,
2001 	.remove = ibmvscsi_remove,
2002 	.get_desired_dma = ibmvscsi_get_desired_dma,
2003 	.driver = {
2004 		.name = "ibmvscsi",
2005 		.owner = THIS_MODULE,
2006 	}
2007 };
2008 
2009 static struct srp_function_template ibmvscsi_transport_functions = {
2010 };
2011 
2012 int __init ibmvscsi_module_init(void)
2013 {
2014 	int ret;
2015 
2016 	/* Ensure we have two requests to do error recovery */
2017 	driver_template.can_queue = max_requests;
2018 	max_events = max_requests + 2;
2019 
2020 	if (firmware_has_feature(FW_FEATURE_ISERIES))
2021 		ibmvscsi_ops = &iseriesvscsi_ops;
2022 	else if (firmware_has_feature(FW_FEATURE_VIO))
2023 		ibmvscsi_ops = &rpavscsi_ops;
2024 	else
2025 		return -ENODEV;
2026 
2027 	ibmvscsi_transport_template =
2028 		srp_attach_transport(&ibmvscsi_transport_functions);
2029 	if (!ibmvscsi_transport_template)
2030 		return -ENOMEM;
2031 
2032 	ret = vio_register_driver(&ibmvscsi_driver);
2033 	if (ret)
2034 		srp_release_transport(ibmvscsi_transport_template);
2035 	return ret;
2036 }
2037 
2038 void __exit ibmvscsi_module_exit(void)
2039 {
2040 	vio_unregister_driver(&ibmvscsi_driver);
2041 	srp_release_transport(ibmvscsi_transport_template);
2042 }
2043 
2044 module_init(ibmvscsi_module_init);
2045 module_exit(ibmvscsi_module_exit);
2046