blob: 2e9be83a697feb90a36fbccfac58599ce19af58a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------
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 <asm/vio.h>
74#include <scsi/scsi.h>
75#include <scsi/scsi_cmnd.h>
76#include <scsi/scsi_host.h>
77#include <scsi/scsi_device.h>
78#include "ibmvscsi.h"
79
80/* The values below are somewhat arbitrary default values, but
81 * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
82 * Note that there are 3 bits of channel value, 6 bits of id, and
83 * 5 bits of LUN.
84 */
85static int max_id = 64;
86static int max_channel = 3;
87static int init_timeout = 5;
88static int max_requests = 50;
89
Dave C Boutcher2b541f82006-01-19 13:34:44 -060090#define IBMVSCSI_VERSION "1.5.8"
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92MODULE_DESCRIPTION("IBM Virtual SCSI");
93MODULE_AUTHOR("Dave Boutcher");
94MODULE_LICENSE("GPL");
95MODULE_VERSION(IBMVSCSI_VERSION);
96
97module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
98MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
99module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
100MODULE_PARM_DESC(max_channel, "Largest channel value");
101module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
102MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
103module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
105
106/* ------------------------------------------------------------
107 * Routines for the event pool and event structs
108 */
109/**
110 * initialize_event_pool: - Allocates and initializes the event pool for a host
111 * @pool: event_pool to be initialized
112 * @size: Number of events in pool
113 * @hostdata: ibmvscsi_host_data who owns the event pool
114 *
115 * Returns zero on success.
116*/
117static int initialize_event_pool(struct event_pool *pool,
118 int size, struct ibmvscsi_host_data *hostdata)
119{
120 int i;
121
122 pool->size = size;
123 pool->next = 0;
124 pool->events = kmalloc(pool->size * sizeof(*pool->events), GFP_KERNEL);
125 if (!pool->events)
126 return -ENOMEM;
127 memset(pool->events, 0x00, pool->size * sizeof(*pool->events));
128
129 pool->iu_storage =
130 dma_alloc_coherent(hostdata->dev,
131 pool->size * sizeof(*pool->iu_storage),
132 &pool->iu_token, 0);
133 if (!pool->iu_storage) {
134 kfree(pool->events);
135 return -ENOMEM;
136 }
137
138 for (i = 0; i < pool->size; ++i) {
139 struct srp_event_struct *evt = &pool->events[i];
140 memset(&evt->crq, 0x00, sizeof(evt->crq));
141 atomic_set(&evt->free, 1);
142 evt->crq.valid = 0x80;
143 evt->crq.IU_length = sizeof(*evt->xfer_iu);
144 evt->crq.IU_data_ptr = pool->iu_token +
145 sizeof(*evt->xfer_iu) * i;
146 evt->xfer_iu = pool->iu_storage + i;
147 evt->hostdata = hostdata;
James Bottomley4dddbc22005-09-06 17:11:54 -0500148 evt->ext_list = NULL;
149 evt->ext_list_token = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
152 return 0;
153}
154
155/**
156 * release_event_pool: - Frees memory of an event pool of a host
157 * @pool: event_pool to be released
158 * @hostdata: ibmvscsi_host_data who owns the even pool
159 *
160 * Returns zero on success.
161*/
162static void release_event_pool(struct event_pool *pool,
163 struct ibmvscsi_host_data *hostdata)
164{
165 int i, in_use = 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500166 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (atomic_read(&pool->events[i].free) != 1)
168 ++in_use;
James Bottomley4dddbc22005-09-06 17:11:54 -0500169 if (pool->events[i].ext_list) {
170 dma_free_coherent(hostdata->dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900171 SG_ALL * sizeof(struct srp_direct_buf),
James Bottomley4dddbc22005-09-06 17:11:54 -0500172 pool->events[i].ext_list,
173 pool->events[i].ext_list_token);
174 }
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (in_use)
177 printk(KERN_WARNING
178 "ibmvscsi: releasing event pool with %d "
179 "events still in use?\n", in_use);
180 kfree(pool->events);
181 dma_free_coherent(hostdata->dev,
182 pool->size * sizeof(*pool->iu_storage),
183 pool->iu_storage, pool->iu_token);
184}
185
186/**
187 * valid_event_struct: - Determines if event is valid.
188 * @pool: event_pool that contains the event
189 * @evt: srp_event_struct to be checked for validity
190 *
191 * Returns zero if event is invalid, one otherwise.
192*/
193static int valid_event_struct(struct event_pool *pool,
194 struct srp_event_struct *evt)
195{
196 int index = evt - pool->events;
197 if (index < 0 || index >= pool->size) /* outside of bounds */
198 return 0;
199 if (evt != pool->events + index) /* unaligned */
200 return 0;
201 return 1;
202}
203
204/**
205 * ibmvscsi_free-event_struct: - Changes status of event to "free"
206 * @pool: event_pool that contains the event
207 * @evt: srp_event_struct to be modified
208 *
209*/
210static void free_event_struct(struct event_pool *pool,
211 struct srp_event_struct *evt)
212{
213 if (!valid_event_struct(pool, evt)) {
214 printk(KERN_ERR
215 "ibmvscsi: Freeing invalid event_struct %p "
216 "(not in pool %p)\n", evt, pool->events);
217 return;
218 }
219 if (atomic_inc_return(&evt->free) != 1) {
220 printk(KERN_ERR
221 "ibmvscsi: Freeing event_struct %p "
222 "which is not in use!\n", evt);
223 return;
224 }
225}
226
227/**
228 * get_evt_struct: - Gets the next free event in pool
229 * @pool: event_pool that contains the events to be searched
230 *
231 * Returns the next event in "free" state, and NULL if none are free.
232 * Note that no synchronization is done here, we assume the host_lock
233 * will syncrhonze things.
234*/
235static struct srp_event_struct *get_event_struct(struct event_pool *pool)
236{
237 int i;
238 int poolsize = pool->size;
239 int offset = pool->next;
240
241 for (i = 0; i < poolsize; i++) {
242 offset = (offset + 1) % poolsize;
243 if (!atomic_dec_if_positive(&pool->events[offset].free)) {
244 pool->next = offset;
245 return &pool->events[offset];
246 }
247 }
248
249 printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
250 return NULL;
251}
252
253/**
254 * init_event_struct: Initialize fields in an event struct that are always
255 * required.
256 * @evt: The event
257 * @done: Routine to call when the event is responded to
258 * @format: SRP or MAD format
259 * @timeout: timeout value set in the CRQ
260 */
261static void init_event_struct(struct srp_event_struct *evt_struct,
262 void (*done) (struct srp_event_struct *),
263 u8 format,
264 int timeout)
265{
266 evt_struct->cmnd = NULL;
267 evt_struct->cmnd_done = NULL;
268 evt_struct->sync_srp = NULL;
269 evt_struct->crq.format = format;
270 evt_struct->crq.timeout = timeout;
271 evt_struct->done = done;
272}
273
274/* ------------------------------------------------------------
275 * Routines for receiving SCSI responses from the hosting partition
276 */
277
278/**
279 * set_srp_direction: Set the fields in the srp related to data
280 * direction and number of buffers based on the direction in
281 * the scsi_cmnd and the number of buffers
282 */
283static void set_srp_direction(struct scsi_cmnd *cmd,
284 struct srp_cmd *srp_cmd,
285 int numbuf)
286{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900287 u8 fmt;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (numbuf == 0)
290 return;
291
FUJITA Tomonorief265672006-03-26 03:57:14 +0900292 if (numbuf == 1)
293 fmt = SRP_DATA_DESC_DIRECT;
294 else {
295 fmt = SRP_DATA_DESC_INDIRECT;
296 numbuf = min(numbuf, MAX_INDIRECT_BUFS);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (cmd->sc_data_direction == DMA_TO_DEVICE)
FUJITA Tomonorief265672006-03-26 03:57:14 +0900299 srp_cmd->data_out_desc_cnt = numbuf;
300 else
301 srp_cmd->data_in_desc_cnt = numbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
FUJITA Tomonorief265672006-03-26 03:57:14 +0900303
304 if (cmd->sc_data_direction == DMA_TO_DEVICE)
305 srp_cmd->buf_fmt = fmt << 4;
306 else
307 srp_cmd->buf_fmt = fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
FUJITA Tomonorief265672006-03-26 03:57:14 +0900310static void unmap_sg_list(int num_entries,
James Bottomley4dddbc22005-09-06 17:11:54 -0500311 struct device *dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900312 struct srp_direct_buf *md)
313{
James Bottomley4dddbc22005-09-06 17:11:54 -0500314 int i;
315
FUJITA Tomonorief265672006-03-26 03:57:14 +0900316 for (i = 0; i < num_entries; ++i)
317 dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
James Bottomley4dddbc22005-09-06 17:11:54 -0500318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/**
321 * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
322 * @cmd: srp_cmd whose additional_data member will be unmapped
323 * @dev: device for which the memory is mapped
324 *
325*/
James Bottomley4dddbc22005-09-06 17:11:54 -0500326static void unmap_cmd_data(struct srp_cmd *cmd,
327 struct srp_event_struct *evt_struct,
328 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900330 u8 out_fmt, in_fmt;
331
332 out_fmt = cmd->buf_fmt >> 4;
333 in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
334
335 if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900337 else if (out_fmt == SRP_DATA_DESC_DIRECT ||
338 in_fmt == SRP_DATA_DESC_DIRECT) {
339 struct srp_direct_buf *data =
340 (struct srp_direct_buf *) cmd->add_data;
341 dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 } else {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900343 struct srp_indirect_buf *indirect =
344 (struct srp_indirect_buf *) cmd->add_data;
345 int num_mapped = indirect->table_desc.len /
346 sizeof(struct srp_direct_buf);
James Bottomley4dddbc22005-09-06 17:11:54 -0500347
348 if (num_mapped <= MAX_INDIRECT_BUFS) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900349 unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
James Bottomley4dddbc22005-09-06 17:11:54 -0500350 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
James Bottomley4dddbc22005-09-06 17:11:54 -0500352
353 unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355}
356
James Bottomley4dddbc22005-09-06 17:11:54 -0500357static int map_sg_list(int num_entries,
358 struct scatterlist *sg,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900359 struct srp_direct_buf *md)
James Bottomley4dddbc22005-09-06 17:11:54 -0500360{
361 int i;
362 u64 total_length = 0;
363
364 for (i = 0; i < num_entries; ++i) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900365 struct srp_direct_buf *descr = md + i;
James Bottomley4dddbc22005-09-06 17:11:54 -0500366 struct scatterlist *sg_entry = &sg[i];
FUJITA Tomonorief265672006-03-26 03:57:14 +0900367 descr->va = sg_dma_address(sg_entry);
368 descr->len = sg_dma_len(sg_entry);
369 descr->key = 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500370 total_length += sg_dma_len(sg_entry);
371 }
372 return total_length;
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
376 * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
377 * @cmd: Scsi_Cmnd with the scatterlist
378 * @srp_cmd: srp_cmd that contains the memory descriptor
379 * @dev: device for which to map dma memory
380 *
381 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
382 * Returns 1 on success.
383*/
384static int map_sg_data(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500385 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct srp_cmd *srp_cmd, struct device *dev)
387{
388
James Bottomley4dddbc22005-09-06 17:11:54 -0500389 int sg_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 u64 total_length = 0;
391 struct scatterlist *sg = cmd->request_buffer;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900392 struct srp_direct_buf *data =
393 (struct srp_direct_buf *) srp_cmd->add_data;
394 struct srp_indirect_buf *indirect =
395 (struct srp_indirect_buf *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 sg_mapped = dma_map_sg(dev, sg, cmd->use_sg, DMA_BIDIRECTIONAL);
398
399 if (sg_mapped == 0)
400 return 0;
401
402 set_srp_direction(cmd, srp_cmd, sg_mapped);
403
404 /* special case; we can use a single direct descriptor */
405 if (sg_mapped == 1) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900406 data->va = sg_dma_address(&sg[0]);
407 data->len = sg_dma_len(&sg[0]);
408 data->key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 1;
410 }
411
James Bottomley4dddbc22005-09-06 17:11:54 -0500412 if (sg_mapped > SG_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 printk(KERN_ERR
414 "ibmvscsi: More than %d mapped sg entries, got %d\n",
James Bottomley4dddbc22005-09-06 17:11:54 -0500415 SG_ALL, sg_mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
417 }
418
FUJITA Tomonorief265672006-03-26 03:57:14 +0900419 indirect->table_desc.va = 0;
420 indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
421 indirect->table_desc.key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
James Bottomley4dddbc22005-09-06 17:11:54 -0500423 if (sg_mapped <= MAX_INDIRECT_BUFS) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900424 total_length = map_sg_list(sg_mapped, sg,
425 &indirect->desc_list[0]);
426 indirect->len = total_length;
James Bottomley4dddbc22005-09-06 17:11:54 -0500427 return 1;
428 }
429
430 /* get indirect table */
431 if (!evt_struct->ext_list) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900432 evt_struct->ext_list = (struct srp_direct_buf *)
James Bottomley4dddbc22005-09-06 17:11:54 -0500433 dma_alloc_coherent(dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900434 SG_ALL * sizeof(struct srp_direct_buf),
435 &evt_struct->ext_list_token, 0);
James Bottomley4dddbc22005-09-06 17:11:54 -0500436 if (!evt_struct->ext_list) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900437 printk(KERN_ERR
438 "ibmvscsi: Can't allocate memory for indirect table\n");
James Bottomley4dddbc22005-09-06 17:11:54 -0500439 return 0;
440
441 }
442 }
443
444 total_length = map_sg_list(sg_mapped, sg, evt_struct->ext_list);
445
FUJITA Tomonorief265672006-03-26 03:57:14 +0900446 indirect->len = total_length;
447 indirect->table_desc.va = evt_struct->ext_list_token;
448 indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
449 memcpy(indirect->desc_list, evt_struct->ext_list,
450 MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
James Bottomley4dddbc22005-09-06 17:11:54 -0500451
452 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455/**
456 * map_single_data: - Maps memory and initializes memory decriptor fields
457 * @cmd: struct scsi_cmnd with the memory to be mapped
458 * @srp_cmd: srp_cmd that contains the memory descriptor
459 * @dev: device for which to map dma memory
460 *
461 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
462 * Returns 1 on success.
463*/
464static int map_single_data(struct scsi_cmnd *cmd,
465 struct srp_cmd *srp_cmd, struct device *dev)
466{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900467 struct srp_direct_buf *data =
468 (struct srp_direct_buf *) srp_cmd->add_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
FUJITA Tomonorief265672006-03-26 03:57:14 +0900470 data->va =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dma_map_single(dev, cmd->request_buffer,
472 cmd->request_bufflen,
473 DMA_BIDIRECTIONAL);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900474 if (dma_mapping_error(data->va)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 printk(KERN_ERR
476 "ibmvscsi: Unable to map request_buffer for command!\n");
477 return 0;
478 }
FUJITA Tomonorief265672006-03-26 03:57:14 +0900479 data->len = cmd->request_bufflen;
480 data->key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 set_srp_direction(cmd, srp_cmd, 1);
483
484 return 1;
485}
486
487/**
488 * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
489 * @cmd: struct scsi_cmnd with the memory to be mapped
490 * @srp_cmd: srp_cmd that contains the memory descriptor
491 * @dev: dma device for which to map dma memory
492 *
493 * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
494 * Returns 1 on success.
495*/
496static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500497 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct srp_cmd *srp_cmd, struct device *dev)
499{
500 switch (cmd->sc_data_direction) {
501 case DMA_FROM_DEVICE:
502 case DMA_TO_DEVICE:
503 break;
504 case DMA_NONE:
505 return 1;
506 case DMA_BIDIRECTIONAL:
507 printk(KERN_ERR
508 "ibmvscsi: Can't map DMA_BIDIRECTIONAL to read/write\n");
509 return 0;
510 default:
511 printk(KERN_ERR
512 "ibmvscsi: Unknown data direction 0x%02x; can't map!\n",
513 cmd->sc_data_direction);
514 return 0;
515 }
516
517 if (!cmd->request_buffer)
518 return 1;
519 if (cmd->use_sg)
James Bottomley4dddbc22005-09-06 17:11:54 -0500520 return map_sg_data(cmd, evt_struct, srp_cmd, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return map_single_data(cmd, srp_cmd, dev);
522}
523
524/* ------------------------------------------------------------
525 * Routines for sending and receiving SRPs
526 */
527/**
528 * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
529 * @evt_struct: evt_struct to be sent
530 * @hostdata: ibmvscsi_host_data of host
531 *
532 * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
533 * Note that this routine assumes that host_lock is held for synchronization
534*/
535static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
536 struct ibmvscsi_host_data *hostdata)
537{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
539 int rc;
540
541 /* If we have exhausted our request limit, just fail this request.
542 * Note that there are rare cases involving driver generated requests
543 * (such as task management requests) that the mid layer may think we
544 * can handle more requests (can_queue) when we actually can't
545 */
546 if ((evt_struct->crq.format == VIOSRP_SRP_FORMAT) &&
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600547 (atomic_dec_if_positive(&hostdata->request_limit) < 0))
548 goto send_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Copy the IU into the transfer area */
551 *evt_struct->xfer_iu = evt_struct->iu;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900552 evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* Add this to the sent list. We need to do this
555 * before we actually send
556 * in case it comes back REALLY fast
557 */
558 list_add_tail(&evt_struct->list, &hostdata->sent);
559
560 if ((rc =
561 ibmvscsi_send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
562 list_del(&evt_struct->list);
563
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600564 printk(KERN_ERR "ibmvscsi: send error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc);
566 goto send_error;
567 }
568
569 return 0;
570
571 send_error:
James Bottomley4dddbc22005-09-06 17:11:54 -0500572 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 free_event_struct(&hostdata->pool, evt_struct);
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600575 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/**
579 * handle_cmd_rsp: - Handle responses from commands
580 * @evt_struct: srp_event_struct to be handled
581 *
582 * Used as a callback by when sending scsi cmds.
583 * Gets called by ibmvscsi_handle_crq()
584*/
585static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
586{
587 struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
588 struct scsi_cmnd *cmnd = evt_struct->cmnd;
589
FUJITA Tomonorief265672006-03-26 03:57:14 +0900590 if (unlikely(rsp->opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (printk_ratelimit())
592 printk(KERN_WARNING
593 "ibmvscsi: bad SRP RSP type %d\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +0900594 rsp->opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 if (cmnd) {
598 cmnd->result = rsp->status;
599 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
600 memcpy(cmnd->sense_buffer,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900601 rsp->data,
602 rsp->sense_data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unmap_cmd_data(&evt_struct->iu.srp.cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500604 evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 evt_struct->hostdata->dev);
606
FUJITA Tomonorief265672006-03-26 03:57:14 +0900607 if (rsp->flags & SRP_RSP_FLAG_DOOVER)
608 cmnd->resid = rsp->data_out_res_cnt;
609 else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
610 cmnd->resid = rsp->data_in_res_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 if (evt_struct->cmnd_done)
614 evt_struct->cmnd_done(cmnd);
615}
616
617/**
618 * lun_from_dev: - Returns the lun of the scsi device
619 * @dev: struct scsi_device
620 *
621*/
622static inline u16 lun_from_dev(struct scsi_device *dev)
623{
624 return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
625}
626
627/**
628 * ibmvscsi_queue: - The queuecommand function of the scsi template
629 * @cmd: struct scsi_cmnd to be executed
630 * @done: Callback function to be called when cmd is completed
631*/
632static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
633 void (*done) (struct scsi_cmnd *))
634{
635 struct srp_cmd *srp_cmd;
636 struct srp_event_struct *evt_struct;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900637 struct srp_indirect_buf *indirect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct ibmvscsi_host_data *hostdata =
639 (struct ibmvscsi_host_data *)&cmnd->device->host->hostdata;
640 u16 lun = lun_from_dev(cmnd->device);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900641 u8 out_fmt, in_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 evt_struct = get_event_struct(&hostdata->pool);
644 if (!evt_struct)
645 return SCSI_MLQUEUE_HOST_BUSY;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* Set up the actual SRP IU */
648 srp_cmd = &evt_struct->iu.srp.cmd;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900649 memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
650 srp_cmd->opcode = SRP_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(cmnd->cmnd));
652 srp_cmd->lun = ((u64) lun) << 48;
653
James Bottomley4dddbc22005-09-06 17:11:54 -0500654 if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 printk(KERN_ERR "ibmvscsi: couldn't convert cmd to srp_cmd\n");
656 free_event_struct(&hostdata->pool, evt_struct);
657 return SCSI_MLQUEUE_HOST_BUSY;
658 }
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 init_event_struct(evt_struct,
661 handle_cmd_rsp,
662 VIOSRP_SRP_FORMAT,
Dave C Boutcher8224bfa2005-08-22 14:38:26 -0500663 cmnd->timeout_per_command/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 evt_struct->cmnd = cmnd;
666 evt_struct->cmnd_done = done;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* Fix up dma address of the buffer itself */
FUJITA Tomonorief265672006-03-26 03:57:14 +0900669 indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
670 out_fmt = srp_cmd->buf_fmt >> 4;
671 in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
672 if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
673 out_fmt == SRP_DATA_DESC_INDIRECT) &&
674 indirect->table_desc.va == 0) {
675 indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
676 offsetof(struct srp_cmd, add_data) +
677 offsetof(struct srp_indirect_buf, desc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 return ibmvscsi_send_srp_event(evt_struct, hostdata);
681}
682
683/* ------------------------------------------------------------
684 * Routines for driver initialization
685 */
686/**
687 * adapter_info_rsp: - Handle response to MAD adapter info request
688 * @evt_struct: srp_event_struct with the response
689 *
690 * Used as a "done" callback by when sending adapter_info. Gets called
691 * by ibmvscsi_handle_crq()
692*/
693static void adapter_info_rsp(struct srp_event_struct *evt_struct)
694{
695 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
696 dma_unmap_single(hostdata->dev,
697 evt_struct->iu.mad.adapter_info.buffer,
698 evt_struct->iu.mad.adapter_info.common.length,
699 DMA_BIDIRECTIONAL);
700
701 if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
702 printk("ibmvscsi: error %d getting adapter info\n",
703 evt_struct->xfer_iu->mad.adapter_info.common.status);
704 } else {
705 printk("ibmvscsi: host srp version: %s, "
706 "host partition %s (%d), OS %d, max io %u\n",
707 hostdata->madapter_info.srp_version,
708 hostdata->madapter_info.partition_name,
709 hostdata->madapter_info.partition_number,
710 hostdata->madapter_info.os_type,
711 hostdata->madapter_info.port_max_txu[0]);
712
713 if (hostdata->madapter_info.port_max_txu[0])
714 hostdata->host->max_sectors =
715 hostdata->madapter_info.port_max_txu[0] >> 9;
Dave C Boutcher154fb612005-09-13 10:09:02 -0500716
717 if (hostdata->madapter_info.os_type == 3 &&
718 strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
719 printk("ibmvscsi: host (Ver. %s) doesn't support large"
720 "transfers\n",
721 hostdata->madapter_info.srp_version);
722 printk("ibmvscsi: limiting scatterlists to %d\n",
723 MAX_INDIRECT_BUFS);
724 hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727}
728
729/**
730 * send_mad_adapter_info: - Sends the mad adapter info request
731 * and stores the result so it can be retrieved with
732 * sysfs. We COULD consider causing a failure if the
733 * returned SRP version doesn't match ours.
734 * @hostdata: ibmvscsi_host_data of host
735 *
736 * Returns zero if successful.
737*/
738static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
739{
740 struct viosrp_adapter_info *req;
741 struct srp_event_struct *evt_struct;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +0900742 dma_addr_t addr;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 evt_struct = get_event_struct(&hostdata->pool);
745 if (!evt_struct) {
746 printk(KERN_ERR "ibmvscsi: couldn't allocate an event "
747 "for ADAPTER_INFO_REQ!\n");
748 return;
749 }
750
751 init_event_struct(evt_struct,
752 adapter_info_rsp,
753 VIOSRP_MAD_FORMAT,
754 init_timeout * HZ);
755
756 req = &evt_struct->iu.mad.adapter_info;
757 memset(req, 0x00, sizeof(*req));
758
759 req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
760 req->common.length = sizeof(hostdata->madapter_info);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +0900761 req->buffer = addr = dma_map_single(hostdata->dev,
762 &hostdata->madapter_info,
763 sizeof(hostdata->madapter_info),
764 DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (dma_mapping_error(req->buffer)) {
767 printk(KERN_ERR
768 "ibmvscsi: Unable to map request_buffer "
769 "for adapter_info!\n");
770 free_event_struct(&hostdata->pool, evt_struct);
771 return;
772 }
773
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +0900774 if (ibmvscsi_send_srp_event(evt_struct, hostdata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 printk(KERN_ERR "ibmvscsi: couldn't send ADAPTER_INFO_REQ!\n");
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +0900776 dma_unmap_single(hostdata->dev,
777 addr,
778 sizeof(hostdata->madapter_info),
779 DMA_BIDIRECTIONAL);
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781};
782
783/**
784 * login_rsp: - Handle response to SRP login request
785 * @evt_struct: srp_event_struct with the response
786 *
787 * Used as a "done" callback by when sending srp_login. Gets called
788 * by ibmvscsi_handle_crq()
789*/
790static void login_rsp(struct srp_event_struct *evt_struct)
791{
792 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900793 switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
794 case SRP_LOGIN_RSP: /* it worked! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900796 case SRP_LOGIN_REJ: /* refused! */
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600797 printk(KERN_INFO "ibmvscsi: SRP_LOGIN_REJ reason %u\n",
798 evt_struct->xfer_iu->srp.login_rej.reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* Login failed. */
800 atomic_set(&hostdata->request_limit, -1);
801 return;
802 default:
803 printk(KERN_ERR
804 "ibmvscsi: Invalid login response typecode 0x%02x!\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +0900805 evt_struct->xfer_iu->srp.login_rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* Login failed. */
807 atomic_set(&hostdata->request_limit, -1);
808 return;
809 }
810
811 printk(KERN_INFO "ibmvscsi: SRP_LOGIN succeeded\n");
812
FUJITA Tomonorief265672006-03-26 03:57:14 +0900813 if (evt_struct->xfer_iu->srp.login_rsp.req_lim_delta >
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 (max_requests - 2))
FUJITA Tomonorief265672006-03-26 03:57:14 +0900815 evt_struct->xfer_iu->srp.login_rsp.req_lim_delta =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 max_requests - 2;
817
818 /* Now we know what the real request-limit is */
819 atomic_set(&hostdata->request_limit,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900820 evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 hostdata->host->can_queue =
FUJITA Tomonorief265672006-03-26 03:57:14 +0900823 evt_struct->xfer_iu->srp.login_rsp.req_lim_delta - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (hostdata->host->can_queue < 1) {
826 printk(KERN_ERR "ibmvscsi: Invalid request_limit_delta\n");
827 return;
828 }
829
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600830 /* If we had any pending I/Os, kick them */
831 scsi_unblock_requests(hostdata->host);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 send_mad_adapter_info(hostdata);
834 return;
835}
836
837/**
838 * send_srp_login: - Sends the srp login
839 * @hostdata: ibmvscsi_host_data of host
840 *
841 * Returns zero if successful.
842*/
843static int send_srp_login(struct ibmvscsi_host_data *hostdata)
844{
845 int rc;
846 unsigned long flags;
847 struct srp_login_req *login;
848 struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
849 if (!evt_struct) {
850 printk(KERN_ERR
851 "ibmvscsi: couldn't allocate an event for login req!\n");
852 return FAILED;
853 }
854
855 init_event_struct(evt_struct,
856 login_rsp,
857 VIOSRP_SRP_FORMAT,
858 init_timeout * HZ);
859
860 login = &evt_struct->iu.srp.login_req;
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600861 memset(login, 0x00, sizeof(struct srp_login_req));
FUJITA Tomonorief265672006-03-26 03:57:14 +0900862 login->opcode = SRP_LOGIN_REQ;
863 login->req_it_iu_len = sizeof(union srp_iu);
864 login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dave C Boutcher9b833e42006-03-23 13:47:07 -0600866 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* Start out with a request limit of 1, since this is negotiated in
868 * the login request we are just sending
869 */
870 atomic_set(&hostdata->request_limit, 1);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
873 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Dave C Boutcher9b833e42006-03-23 13:47:07 -0600874 printk("ibmvscsic: sent SRP login\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return rc;
876};
877
878/**
879 * sync_completion: Signal that a synchronous command has completed
880 * Note that after returning from this call, the evt_struct is freed.
881 * the caller waiting on this completion shouldn't touch the evt_struct
882 * again.
883 */
884static void sync_completion(struct srp_event_struct *evt_struct)
885{
886 /* copy the response back */
887 if (evt_struct->sync_srp)
888 *evt_struct->sync_srp = *evt_struct->xfer_iu;
889
890 complete(&evt_struct->comp);
891}
892
893/**
894 * ibmvscsi_abort: Abort a command...from scsi host template
895 * send this over to the server and wait synchronously for the response
896 */
897static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
898{
899 struct ibmvscsi_host_data *hostdata =
900 (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
901 struct srp_tsk_mgmt *tsk_mgmt;
902 struct srp_event_struct *evt;
903 struct srp_event_struct *tmp_evt, *found_evt;
904 union viosrp_iu srp_rsp;
905 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500906 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 u16 lun = lun_from_dev(cmd->device);
908
909 /* First, find this command in our sent list so we can figure
910 * out the correct tag
911 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500912 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 found_evt = NULL;
914 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
915 if (tmp_evt->cmnd == cmd) {
916 found_evt = tmp_evt;
917 break;
918 }
919 }
920
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500921 if (!found_evt) {
922 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return FAILED;
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 evt = get_event_struct(&hostdata->pool);
927 if (evt == NULL) {
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500928 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 printk(KERN_ERR "ibmvscsi: failed to allocate abort event\n");
930 return FAILED;
931 }
932
933 init_event_struct(evt,
934 sync_completion,
935 VIOSRP_SRP_FORMAT,
936 init_timeout * HZ);
937
938 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
939
940 /* Set up an abort SRP command */
941 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
FUJITA Tomonorief265672006-03-26 03:57:14 +0900942 tsk_mgmt->opcode = SRP_TSK_MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 tsk_mgmt->lun = ((u64) lun) << 48;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900944 tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
945 tsk_mgmt->task_tag = (u64) found_evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 printk(KERN_INFO "ibmvscsi: aborting command. lun 0x%lx, tag 0x%lx\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +0900948 tsk_mgmt->lun, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 evt->sync_srp = &srp_rsp;
951 init_completion(&evt->comp);
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500952 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata);
953 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
954 if (rsp_rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 printk(KERN_ERR "ibmvscsi: failed to send abort() event\n");
956 return FAILED;
957 }
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +0900962 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (printk_ratelimit())
964 printk(KERN_WARNING
965 "ibmvscsi: abort bad SRP RSP type %d\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +0900966 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return FAILED;
968 }
969
FUJITA Tomonorief265672006-03-26 03:57:14 +0900970 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
971 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 else
973 rsp_rc = srp_rsp.srp.rsp.status;
974
975 if (rsp_rc) {
976 if (printk_ratelimit())
977 printk(KERN_WARNING
FUJITA Tomonorief265672006-03-26 03:57:14 +0900978 "ibmvscsi: abort code %d for task tag 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 rsp_rc,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900980 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return FAILED;
982 }
983
984 /* Because we dropped the spinlock above, it's possible
985 * The event is no longer in our list. Make sure it didn't
986 * complete while we were aborting
987 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500988 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 found_evt = NULL;
990 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
991 if (tmp_evt->cmnd == cmd) {
992 found_evt = tmp_evt;
993 break;
994 }
995 }
996
997 if (found_evt == NULL) {
Dave C Boutcherbe042f22005-08-15 16:52:58 -0500998 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 printk(KERN_INFO
1000 "ibmvscsi: aborted task tag 0x%lx completed\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +09001001 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return SUCCESS;
1003 }
1004
1005 printk(KERN_INFO
1006 "ibmvscsi: successfully aborted task tag 0x%lx\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +09001007 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 cmd->result = (DID_ABORT << 16);
1010 list_del(&found_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001011 unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1012 found_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 free_event_struct(&found_evt->hostdata->pool, found_evt);
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001014 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 atomic_inc(&hostdata->request_limit);
1016 return SUCCESS;
1017}
1018
1019/**
1020 * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
1021 * template send this over to the server and wait synchronously for the
1022 * response
1023 */
1024static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1025{
1026 struct ibmvscsi_host_data *hostdata =
1027 (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
1028
1029 struct srp_tsk_mgmt *tsk_mgmt;
1030 struct srp_event_struct *evt;
1031 struct srp_event_struct *tmp_evt, *pos;
1032 union viosrp_iu srp_rsp;
1033 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001034 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 u16 lun = lun_from_dev(cmd->device);
1036
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001037 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 evt = get_event_struct(&hostdata->pool);
1039 if (evt == NULL) {
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001040 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 printk(KERN_ERR "ibmvscsi: failed to allocate reset event\n");
1042 return FAILED;
1043 }
1044
1045 init_event_struct(evt,
1046 sync_completion,
1047 VIOSRP_SRP_FORMAT,
1048 init_timeout * HZ);
1049
1050 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1051
1052 /* Set up a lun reset SRP command */
1053 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
FUJITA Tomonorief265672006-03-26 03:57:14 +09001054 tsk_mgmt->opcode = SRP_TSK_MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 tsk_mgmt->lun = ((u64) lun) << 48;
FUJITA Tomonorief265672006-03-26 03:57:14 +09001056 tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 printk(KERN_INFO "ibmvscsi: resetting device. lun 0x%lx\n",
1059 tsk_mgmt->lun);
1060
1061 evt->sync_srp = &srp_rsp;
1062 init_completion(&evt->comp);
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001063 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata);
1064 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1065 if (rsp_rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 printk(KERN_ERR "ibmvscsi: failed to send reset event\n");
1067 return FAILED;
1068 }
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001073 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (printk_ratelimit())
1075 printk(KERN_WARNING
1076 "ibmvscsi: reset bad SRP RSP type %d\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +09001077 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return FAILED;
1079 }
1080
FUJITA Tomonorief265672006-03-26 03:57:14 +09001081 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1082 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 else
1084 rsp_rc = srp_rsp.srp.rsp.status;
1085
1086 if (rsp_rc) {
1087 if (printk_ratelimit())
1088 printk(KERN_WARNING
1089 "ibmvscsi: reset code %d for task tag 0x%lx\n",
FUJITA Tomonorief265672006-03-26 03:57:14 +09001090 rsp_rc, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return FAILED;
1092 }
1093
1094 /* We need to find all commands for this LUN that have not yet been
1095 * responded to, and fail them with DID_RESET
1096 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001097 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1099 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1100 if (tmp_evt->cmnd)
1101 tmp_evt->cmnd->result = (DID_RESET << 16);
1102 list_del(&tmp_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001103 unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1104 tmp_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 free_event_struct(&tmp_evt->hostdata->pool,
1106 tmp_evt);
1107 atomic_inc(&hostdata->request_limit);
1108 if (tmp_evt->cmnd_done)
1109 tmp_evt->cmnd_done(tmp_evt->cmnd);
1110 else if (tmp_evt->done)
1111 tmp_evt->done(tmp_evt);
1112 }
1113 }
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001114 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return SUCCESS;
1116}
1117
1118/**
1119 * purge_requests: Our virtual adapter just shut down. purge any sent requests
1120 * @hostdata: the adapter
1121 */
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001122static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct srp_event_struct *tmp_evt, *pos;
1125 unsigned long flags;
1126
1127 spin_lock_irqsave(hostdata->host->host_lock, flags);
1128 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1129 list_del(&tmp_evt->list);
1130 if (tmp_evt->cmnd) {
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001131 tmp_evt->cmnd->result = (error_code << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 unmap_cmd_data(&tmp_evt->iu.srp.cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -05001133 tmp_evt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 tmp_evt->hostdata->dev);
1135 if (tmp_evt->cmnd_done)
1136 tmp_evt->cmnd_done(tmp_evt->cmnd);
1137 } else {
1138 if (tmp_evt->done) {
1139 tmp_evt->done(tmp_evt);
1140 }
1141 }
1142 free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
1143 }
1144 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1145}
1146
1147/**
1148 * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1149 * @crq: Command/Response queue
1150 * @hostdata: ibmvscsi_host_data of host
1151 *
1152*/
1153void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1154 struct ibmvscsi_host_data *hostdata)
1155{
1156 unsigned long flags;
1157 struct srp_event_struct *evt_struct =
1158 (struct srp_event_struct *)crq->IU_data_ptr;
1159 switch (crq->valid) {
1160 case 0xC0: /* initialization */
1161 switch (crq->format) {
1162 case 0x01: /* Initialization message */
1163 printk(KERN_INFO "ibmvscsi: partner initialized\n");
1164 /* Send back a response */
1165 if (ibmvscsi_send_crq(hostdata,
1166 0xC002000000000000LL, 0) == 0) {
1167 /* Now login */
1168 send_srp_login(hostdata);
1169 } else {
1170 printk(KERN_ERR
1171 "ibmvscsi: Unable to send init rsp\n");
1172 }
1173
1174 break;
1175 case 0x02: /* Initialization response */
1176 printk(KERN_INFO
1177 "ibmvscsi: partner initialization complete\n");
1178
1179 /* Now login */
1180 send_srp_login(hostdata);
1181 break;
1182 default:
1183 printk(KERN_ERR "ibmvscsi: unknown crq message type\n");
1184 }
1185 return;
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001186 case 0xFF: /* Hypervisor telling us the connection is closed */
1187 scsi_block_requests(hostdata->host);
1188 if (crq->format == 0x06) {
1189 /* We need to re-setup the interpartition connection */
1190 printk(KERN_INFO
1191 "ibmvscsi: Re-enabling adapter!\n");
Dave C Boutcher9b833e42006-03-23 13:47:07 -06001192 atomic_set(&hostdata->request_limit, -1);
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001193 purge_requests(hostdata, DID_REQUEUE);
1194 if (ibmvscsi_reenable_crq_queue(&hostdata->queue,
1195 hostdata) == 0)
1196 if (ibmvscsi_send_crq(hostdata,
1197 0xC001000000000000LL, 0))
1198 printk(KERN_ERR
1199 "ibmvscsi: transmit error after"
1200 " enable\n");
1201 } else {
1202 printk(KERN_INFO
1203 "ibmvscsi: Virtual adapter failed rc %d!\n",
1204 crq->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001206 atomic_set(&hostdata->request_limit, -1);
1207 purge_requests(hostdata, DID_ERROR);
1208 ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
1209 }
1210 scsi_unblock_requests(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return;
1212 case 0x80: /* real payload */
1213 break;
1214 default:
1215 printk(KERN_ERR
1216 "ibmvscsi: got an invalid message type 0x%02x\n",
1217 crq->valid);
1218 return;
1219 }
1220
1221 /* The only kind of payload CRQs we should get are responses to
1222 * things we send. Make sure this response is to something we
1223 * actually sent
1224 */
1225 if (!valid_event_struct(&hostdata->pool, evt_struct)) {
1226 printk(KERN_ERR
1227 "ibmvscsi: returned correlation_token 0x%p is invalid!\n",
1228 (void *)crq->IU_data_ptr);
1229 return;
1230 }
1231
1232 if (atomic_read(&evt_struct->free)) {
1233 printk(KERN_ERR
1234 "ibmvscsi: received duplicate correlation_token 0x%p!\n",
1235 (void *)crq->IU_data_ptr);
1236 return;
1237 }
1238
1239 if (crq->format == VIOSRP_SRP_FORMAT)
FUJITA Tomonorief265672006-03-26 03:57:14 +09001240 atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 &hostdata->request_limit);
1242
1243 if (evt_struct->done)
1244 evt_struct->done(evt_struct);
1245 else
1246 printk(KERN_ERR
1247 "ibmvscsi: returned done() is NULL; not running it!\n");
1248
1249 /*
1250 * Lock the host_lock before messing with these structures, since we
1251 * are running in a task context
1252 */
1253 spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1254 list_del(&evt_struct->list);
1255 free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1256 spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1257}
1258
1259/**
1260 * ibmvscsi_get_host_config: Send the command to the server to get host
1261 * configuration data. The data is opaque to us.
1262 */
1263static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1264 unsigned char *buffer, int length)
1265{
1266 struct viosrp_host_config *host_config;
1267 struct srp_event_struct *evt_struct;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001268 dma_addr_t addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int rc;
1270
1271 evt_struct = get_event_struct(&hostdata->pool);
1272 if (!evt_struct) {
1273 printk(KERN_ERR
1274 "ibmvscsi: could't allocate event for HOST_CONFIG!\n");
1275 return -1;
1276 }
1277
1278 init_event_struct(evt_struct,
1279 sync_completion,
1280 VIOSRP_MAD_FORMAT,
1281 init_timeout * HZ);
1282
1283 host_config = &evt_struct->iu.mad.host_config;
1284
1285 /* Set up a lun reset SRP command */
1286 memset(host_config, 0x00, sizeof(*host_config));
1287 host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1288 host_config->common.length = length;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001289 host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
1290 length,
1291 DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (dma_mapping_error(host_config->buffer)) {
1294 printk(KERN_ERR
1295 "ibmvscsi: dma_mapping error " "getting host config\n");
1296 free_event_struct(&hostdata->pool, evt_struct);
1297 return -1;
1298 }
1299
1300 init_completion(&evt_struct->comp);
1301 rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001302 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 wait_for_completion(&evt_struct->comp);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001304 dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 return rc;
1307}
1308
1309/* ------------------------------------------------------------
1310 * sysfs attributes
1311 */
1312static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
1313{
1314 struct Scsi_Host *shost = class_to_shost(class_dev);
1315 struct ibmvscsi_host_data *hostdata =
1316 (struct ibmvscsi_host_data *)shost->hostdata;
1317 int len;
1318
1319 len = snprintf(buf, PAGE_SIZE, "%s\n",
1320 hostdata->madapter_info.srp_version);
1321 return len;
1322}
1323
1324static struct class_device_attribute ibmvscsi_host_srp_version = {
1325 .attr = {
1326 .name = "srp_version",
1327 .mode = S_IRUGO,
1328 },
1329 .show = show_host_srp_version,
1330};
1331
1332static ssize_t show_host_partition_name(struct class_device *class_dev,
1333 char *buf)
1334{
1335 struct Scsi_Host *shost = class_to_shost(class_dev);
1336 struct ibmvscsi_host_data *hostdata =
1337 (struct ibmvscsi_host_data *)shost->hostdata;
1338 int len;
1339
1340 len = snprintf(buf, PAGE_SIZE, "%s\n",
1341 hostdata->madapter_info.partition_name);
1342 return len;
1343}
1344
1345static struct class_device_attribute ibmvscsi_host_partition_name = {
1346 .attr = {
1347 .name = "partition_name",
1348 .mode = S_IRUGO,
1349 },
1350 .show = show_host_partition_name,
1351};
1352
1353static ssize_t show_host_partition_number(struct class_device *class_dev,
1354 char *buf)
1355{
1356 struct Scsi_Host *shost = class_to_shost(class_dev);
1357 struct ibmvscsi_host_data *hostdata =
1358 (struct ibmvscsi_host_data *)shost->hostdata;
1359 int len;
1360
1361 len = snprintf(buf, PAGE_SIZE, "%d\n",
1362 hostdata->madapter_info.partition_number);
1363 return len;
1364}
1365
1366static struct class_device_attribute ibmvscsi_host_partition_number = {
1367 .attr = {
1368 .name = "partition_number",
1369 .mode = S_IRUGO,
1370 },
1371 .show = show_host_partition_number,
1372};
1373
1374static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
1375{
1376 struct Scsi_Host *shost = class_to_shost(class_dev);
1377 struct ibmvscsi_host_data *hostdata =
1378 (struct ibmvscsi_host_data *)shost->hostdata;
1379 int len;
1380
1381 len = snprintf(buf, PAGE_SIZE, "%d\n",
1382 hostdata->madapter_info.mad_version);
1383 return len;
1384}
1385
1386static struct class_device_attribute ibmvscsi_host_mad_version = {
1387 .attr = {
1388 .name = "mad_version",
1389 .mode = S_IRUGO,
1390 },
1391 .show = show_host_mad_version,
1392};
1393
1394static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
1395{
1396 struct Scsi_Host *shost = class_to_shost(class_dev);
1397 struct ibmvscsi_host_data *hostdata =
1398 (struct ibmvscsi_host_data *)shost->hostdata;
1399 int len;
1400
1401 len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
1402 return len;
1403}
1404
1405static struct class_device_attribute ibmvscsi_host_os_type = {
1406 .attr = {
1407 .name = "os_type",
1408 .mode = S_IRUGO,
1409 },
1410 .show = show_host_os_type,
1411};
1412
1413static ssize_t show_host_config(struct class_device *class_dev, char *buf)
1414{
1415 struct Scsi_Host *shost = class_to_shost(class_dev);
1416 struct ibmvscsi_host_data *hostdata =
1417 (struct ibmvscsi_host_data *)shost->hostdata;
1418
1419 /* returns null-terminated host config data */
1420 if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
1421 return strlen(buf);
1422 else
1423 return 0;
1424}
1425
1426static struct class_device_attribute ibmvscsi_host_config = {
1427 .attr = {
1428 .name = "config",
1429 .mode = S_IRUGO,
1430 },
1431 .show = show_host_config,
1432};
1433
1434static struct class_device_attribute *ibmvscsi_attrs[] = {
1435 &ibmvscsi_host_srp_version,
1436 &ibmvscsi_host_partition_name,
1437 &ibmvscsi_host_partition_number,
1438 &ibmvscsi_host_mad_version,
1439 &ibmvscsi_host_os_type,
1440 &ibmvscsi_host_config,
1441 NULL
1442};
1443
1444/* ------------------------------------------------------------
1445 * SCSI driver registration
1446 */
1447static struct scsi_host_template driver_template = {
1448 .module = THIS_MODULE,
1449 .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
1450 .proc_name = "ibmvscsi",
1451 .queuecommand = ibmvscsi_queuecommand,
1452 .eh_abort_handler = ibmvscsi_eh_abort_handler,
1453 .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
1454 .cmd_per_lun = 16,
1455 .can_queue = 1, /* Updated after SRP_LOGIN */
1456 .this_id = -1,
James Bottomley4dddbc22005-09-06 17:11:54 -05001457 .sg_tablesize = SG_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 .use_clustering = ENABLE_CLUSTERING,
1459 .shost_attrs = ibmvscsi_attrs,
1460};
1461
1462/**
1463 * Called by bus code for each adapter
1464 */
1465static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1466{
1467 struct ibmvscsi_host_data *hostdata;
1468 struct Scsi_Host *host;
1469 struct device *dev = &vdev->dev;
1470 unsigned long wait_switch = 0;
1471
1472 vdev->dev.driver_data = NULL;
1473
1474 host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
1475 if (!host) {
1476 printk(KERN_ERR "ibmvscsi: couldn't allocate host data\n");
1477 goto scsi_host_alloc_failed;
1478 }
1479
1480 hostdata = (struct ibmvscsi_host_data *)host->hostdata;
1481 memset(hostdata, 0x00, sizeof(*hostdata));
1482 INIT_LIST_HEAD(&hostdata->sent);
1483 hostdata->host = host;
1484 hostdata->dev = dev;
1485 atomic_set(&hostdata->request_limit, -1);
1486 hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
1487
1488 if (ibmvscsi_init_crq_queue(&hostdata->queue, hostdata,
1489 max_requests) != 0) {
1490 printk(KERN_ERR "ibmvscsi: couldn't initialize crq\n");
1491 goto init_crq_failed;
1492 }
1493 if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
1494 printk(KERN_ERR "ibmvscsi: couldn't initialize event pool\n");
1495 goto init_pool_failed;
1496 }
1497
1498 host->max_lun = 8;
1499 host->max_id = max_id;
1500 host->max_channel = max_channel;
1501
1502 if (scsi_add_host(hostdata->host, hostdata->dev))
1503 goto add_host_failed;
1504
1505 /* Try to send an initialization message. Note that this is allowed
1506 * to fail if the other end is not acive. In that case we don't
1507 * want to scan
1508 */
1509 if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0) {
1510 /*
1511 * Wait around max init_timeout secs for the adapter to finish
1512 * initializing. When we are done initializing, we will have a
1513 * valid request_limit. We don't want Linux scanning before
1514 * we are ready.
1515 */
1516 for (wait_switch = jiffies + (init_timeout * HZ);
1517 time_before(jiffies, wait_switch) &&
1518 atomic_read(&hostdata->request_limit) < 2;) {
1519
1520 msleep(10);
1521 }
1522
1523 /* if we now have a valid request_limit, initiate a scan */
1524 if (atomic_read(&hostdata->request_limit) > 0)
1525 scsi_scan_host(host);
1526 }
1527
1528 vdev->dev.driver_data = hostdata;
1529 return 0;
1530
1531 add_host_failed:
1532 release_event_pool(&hostdata->pool, hostdata);
1533 init_pool_failed:
1534 ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_requests);
1535 init_crq_failed:
1536 scsi_host_put(host);
1537 scsi_host_alloc_failed:
1538 return -1;
1539}
1540
1541static int ibmvscsi_remove(struct vio_dev *vdev)
1542{
1543 struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
1544 release_event_pool(&hostdata->pool, hostdata);
1545 ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
1546 max_requests);
1547
1548 scsi_remove_host(hostdata->host);
1549 scsi_host_put(hostdata->host);
1550
1551 return 0;
1552}
1553
1554/**
1555 * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
1556 * support.
1557 */
1558static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
1559 {"vscsi", "IBM,v-scsi"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001560 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +10001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static struct vio_driver ibmvscsi_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 .id_table = ibmvscsi_device_table,
1566 .probe = ibmvscsi_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001567 .remove = ibmvscsi_remove,
1568 .driver = {
1569 .name = "ibmvscsi",
Stephen Rothwell915124d2005-10-24 15:12:22 +10001570 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572};
1573
1574int __init ibmvscsi_module_init(void)
1575{
1576 return vio_register_driver(&ibmvscsi_driver);
1577}
1578
1579void __exit ibmvscsi_module_exit(void)
1580{
1581 vio_unregister_driver(&ibmvscsi_driver);
1582}
1583
1584module_init(ibmvscsi_module_init);
1585module_exit(ibmvscsi_module_exit);