blob: 822fbc32a2ae3bb9afb363c73d31e610ec95d1e3 [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>
David Woodhoused3849d52007-09-22 08:29:36 +100073#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/vio.h>
Robert Jennings7912a0a2008-07-24 04:35:27 +100075#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <scsi/scsi.h>
77#include <scsi/scsi_cmnd.h>
78#include <scsi/scsi_host.h>
79#include <scsi/scsi_device.h>
FUJITA Tomonori4d680412007-06-27 16:32:50 +090080#include <scsi/scsi_transport_srp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#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 */
88static int max_id = 64;
89static int max_channel = 3;
Robert Jenningse1a5ce52009-06-08 16:19:03 -050090static int init_timeout = 300;
91static int login_timeout = 60;
92static int info_timeout = 30;
93static int abort_timeout = 60;
94static int reset_timeout = 60;
Robert Jenningsa897ff22007-03-28 12:45:46 -050095static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
Brian King4f10aae2008-12-17 17:19:33 -060096static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
Robert Jenningsc1988e32009-06-08 16:19:07 -050097static int fast_fail = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
FUJITA Tomonori4d680412007-06-27 16:32:50 +090099static struct scsi_transport_template *ibmvscsi_transport_template;
100
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600101#define IBMVSCSI_VERSION "1.5.8"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
David Woodhoused3849d52007-09-22 08:29:36 +1000103static struct ibmvscsi_ops *ibmvscsi_ops;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105MODULE_DESCRIPTION("IBM Virtual SCSI");
106MODULE_AUTHOR("Dave Boutcher");
107MODULE_LICENSE("GPL");
108MODULE_VERSION(IBMVSCSI_VERSION);
109
110module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
111MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
112module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
113MODULE_PARM_DESC(max_channel, "Largest channel value");
114module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
115MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
Brian King21465ed2008-12-08 17:01:47 -0600116module_param_named(max_requests, max_requests, int, S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
Robert Jenningsc1988e32009-06-08 16:19:07 -0500118module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
119MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/* ------------------------------------------------------------
122 * Routines for the event pool and event structs
123 */
124/**
125 * initialize_event_pool: - Allocates and initializes the event pool for a host
126 * @pool: event_pool to be initialized
127 * @size: Number of events in pool
128 * @hostdata: ibmvscsi_host_data who owns the event pool
129 *
130 * Returns zero on success.
131*/
132static int initialize_event_pool(struct event_pool *pool,
133 int size, struct ibmvscsi_host_data *hostdata)
134{
135 int i;
136
137 pool->size = size;
138 pool->next = 0;
FUJITA Tomonori4c021dd132006-04-07 19:10:03 +0900139 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!pool->events)
141 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 pool->iu_storage =
144 dma_alloc_coherent(hostdata->dev,
145 pool->size * sizeof(*pool->iu_storage),
146 &pool->iu_token, 0);
147 if (!pool->iu_storage) {
148 kfree(pool->events);
149 return -ENOMEM;
150 }
151
152 for (i = 0; i < pool->size; ++i) {
153 struct srp_event_struct *evt = &pool->events[i];
154 memset(&evt->crq, 0x00, sizeof(evt->crq));
155 atomic_set(&evt->free, 1);
156 evt->crq.valid = 0x80;
157 evt->crq.IU_length = sizeof(*evt->xfer_iu);
158 evt->crq.IU_data_ptr = pool->iu_token +
159 sizeof(*evt->xfer_iu) * i;
160 evt->xfer_iu = pool->iu_storage + i;
161 evt->hostdata = hostdata;
James Bottomley4dddbc22005-09-06 17:11:54 -0500162 evt->ext_list = NULL;
163 evt->ext_list_token = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
166 return 0;
167}
168
169/**
170 * release_event_pool: - Frees memory of an event pool of a host
171 * @pool: event_pool to be released
172 * @hostdata: ibmvscsi_host_data who owns the even pool
173 *
174 * Returns zero on success.
175*/
176static void release_event_pool(struct event_pool *pool,
177 struct ibmvscsi_host_data *hostdata)
178{
179 int i, in_use = 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500180 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (atomic_read(&pool->events[i].free) != 1)
182 ++in_use;
James Bottomley4dddbc22005-09-06 17:11:54 -0500183 if (pool->events[i].ext_list) {
184 dma_free_coherent(hostdata->dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900185 SG_ALL * sizeof(struct srp_direct_buf),
James Bottomley4dddbc22005-09-06 17:11:54 -0500186 pool->events[i].ext_list,
187 pool->events[i].ext_list_token);
188 }
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (in_use)
Brian King6c0a60e2007-06-13 17:12:19 -0500191 dev_warn(hostdata->dev, "releasing event pool with %d "
192 "events still in use?\n", in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 kfree(pool->events);
194 dma_free_coherent(hostdata->dev,
195 pool->size * sizeof(*pool->iu_storage),
196 pool->iu_storage, pool->iu_token);
197}
198
199/**
200 * valid_event_struct: - Determines if event is valid.
201 * @pool: event_pool that contains the event
202 * @evt: srp_event_struct to be checked for validity
203 *
204 * Returns zero if event is invalid, one otherwise.
205*/
206static int valid_event_struct(struct event_pool *pool,
207 struct srp_event_struct *evt)
208{
209 int index = evt - pool->events;
210 if (index < 0 || index >= pool->size) /* outside of bounds */
211 return 0;
212 if (evt != pool->events + index) /* unaligned */
213 return 0;
214 return 1;
215}
216
217/**
218 * ibmvscsi_free-event_struct: - Changes status of event to "free"
219 * @pool: event_pool that contains the event
220 * @evt: srp_event_struct to be modified
221 *
222*/
223static void free_event_struct(struct event_pool *pool,
224 struct srp_event_struct *evt)
225{
226 if (!valid_event_struct(pool, evt)) {
Brian King6c0a60e2007-06-13 17:12:19 -0500227 dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
228 "(not in pool %p)\n", evt, pool->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return;
230 }
231 if (atomic_inc_return(&evt->free) != 1) {
Brian King6c0a60e2007-06-13 17:12:19 -0500232 dev_err(evt->hostdata->dev, "Freeing event_struct %p "
233 "which is not in use!\n", evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return;
235 }
236}
237
238/**
239 * get_evt_struct: - Gets the next free event in pool
240 * @pool: event_pool that contains the events to be searched
241 *
242 * Returns the next event in "free" state, and NULL if none are free.
243 * Note that no synchronization is done here, we assume the host_lock
244 * will syncrhonze things.
245*/
246static struct srp_event_struct *get_event_struct(struct event_pool *pool)
247{
248 int i;
249 int poolsize = pool->size;
250 int offset = pool->next;
251
252 for (i = 0; i < poolsize; i++) {
253 offset = (offset + 1) % poolsize;
254 if (!atomic_dec_if_positive(&pool->events[offset].free)) {
255 pool->next = offset;
256 return &pool->events[offset];
257 }
258 }
259
260 printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
261 return NULL;
262}
263
264/**
265 * init_event_struct: Initialize fields in an event struct that are always
266 * required.
267 * @evt: The event
268 * @done: Routine to call when the event is responded to
269 * @format: SRP or MAD format
270 * @timeout: timeout value set in the CRQ
271 */
272static void init_event_struct(struct srp_event_struct *evt_struct,
273 void (*done) (struct srp_event_struct *),
274 u8 format,
275 int timeout)
276{
277 evt_struct->cmnd = NULL;
278 evt_struct->cmnd_done = NULL;
279 evt_struct->sync_srp = NULL;
280 evt_struct->crq.format = format;
281 evt_struct->crq.timeout = timeout;
282 evt_struct->done = done;
283}
284
285/* ------------------------------------------------------------
286 * Routines for receiving SCSI responses from the hosting partition
287 */
288
289/**
290 * set_srp_direction: Set the fields in the srp related to data
291 * direction and number of buffers based on the direction in
292 * the scsi_cmnd and the number of buffers
293 */
294static void set_srp_direction(struct scsi_cmnd *cmd,
295 struct srp_cmd *srp_cmd,
296 int numbuf)
297{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900298 u8 fmt;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (numbuf == 0)
301 return;
302
FUJITA Tomonorief265672006-03-26 03:57:14 +0900303 if (numbuf == 1)
304 fmt = SRP_DATA_DESC_DIRECT;
305 else {
306 fmt = SRP_DATA_DESC_INDIRECT;
307 numbuf = min(numbuf, MAX_INDIRECT_BUFS);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (cmd->sc_data_direction == DMA_TO_DEVICE)
FUJITA Tomonorief265672006-03-26 03:57:14 +0900310 srp_cmd->data_out_desc_cnt = numbuf;
311 else
312 srp_cmd->data_in_desc_cnt = numbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
FUJITA Tomonorief265672006-03-26 03:57:14 +0900314
315 if (cmd->sc_data_direction == DMA_TO_DEVICE)
316 srp_cmd->buf_fmt = fmt << 4;
317 else
318 srp_cmd->buf_fmt = fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
FUJITA Tomonorief265672006-03-26 03:57:14 +0900321static void unmap_sg_list(int num_entries,
James Bottomley4dddbc22005-09-06 17:11:54 -0500322 struct device *dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900323 struct srp_direct_buf *md)
324{
James Bottomley4dddbc22005-09-06 17:11:54 -0500325 int i;
326
FUJITA Tomonorief265672006-03-26 03:57:14 +0900327 for (i = 0; i < num_entries; ++i)
328 dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
James Bottomley4dddbc22005-09-06 17:11:54 -0500329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/**
332 * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
333 * @cmd: srp_cmd whose additional_data member will be unmapped
334 * @dev: device for which the memory is mapped
335 *
336*/
James Bottomley4dddbc22005-09-06 17:11:54 -0500337static void unmap_cmd_data(struct srp_cmd *cmd,
338 struct srp_event_struct *evt_struct,
339 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900341 u8 out_fmt, in_fmt;
342
343 out_fmt = cmd->buf_fmt >> 4;
344 in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
345
346 if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900348 else if (out_fmt == SRP_DATA_DESC_DIRECT ||
349 in_fmt == SRP_DATA_DESC_DIRECT) {
350 struct srp_direct_buf *data =
351 (struct srp_direct_buf *) cmd->add_data;
352 dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 } else {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900354 struct srp_indirect_buf *indirect =
355 (struct srp_indirect_buf *) cmd->add_data;
356 int num_mapped = indirect->table_desc.len /
357 sizeof(struct srp_direct_buf);
James Bottomley4dddbc22005-09-06 17:11:54 -0500358
359 if (num_mapped <= MAX_INDIRECT_BUFS) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900360 unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
James Bottomley4dddbc22005-09-06 17:11:54 -0500361 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
James Bottomley4dddbc22005-09-06 17:11:54 -0500363
364 unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366}
367
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900368static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900369 struct srp_direct_buf *md)
James Bottomley4dddbc22005-09-06 17:11:54 -0500370{
371 int i;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900372 struct scatterlist *sg;
James Bottomley4dddbc22005-09-06 17:11:54 -0500373 u64 total_length = 0;
374
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900375 scsi_for_each_sg(cmd, sg, nseg, i) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900376 struct srp_direct_buf *descr = md + i;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900377 descr->va = sg_dma_address(sg);
378 descr->len = sg_dma_len(sg);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900379 descr->key = 0;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900380 total_length += sg_dma_len(sg);
James Bottomley4dddbc22005-09-06 17:11:54 -0500381 }
382 return total_length;
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/**
386 * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
387 * @cmd: Scsi_Cmnd with the scatterlist
388 * @srp_cmd: srp_cmd that contains the memory descriptor
389 * @dev: device for which to map dma memory
390 *
391 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
392 * Returns 1 on success.
393*/
394static int map_sg_data(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500395 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct srp_cmd *srp_cmd, struct device *dev)
397{
398
James Bottomley4dddbc22005-09-06 17:11:54 -0500399 int sg_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 u64 total_length = 0;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900401 struct srp_direct_buf *data =
402 (struct srp_direct_buf *) srp_cmd->add_data;
403 struct srp_indirect_buf *indirect =
404 (struct srp_indirect_buf *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900406 sg_mapped = scsi_dma_map(cmd);
407 if (!sg_mapped)
408 return 1;
409 else if (sg_mapped < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411
412 set_srp_direction(cmd, srp_cmd, sg_mapped);
413
414 /* special case; we can use a single direct descriptor */
415 if (sg_mapped == 1) {
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900416 map_sg_list(cmd, sg_mapped, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 1;
418 }
419
FUJITA Tomonorief265672006-03-26 03:57:14 +0900420 indirect->table_desc.va = 0;
421 indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
422 indirect->table_desc.key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
James Bottomley4dddbc22005-09-06 17:11:54 -0500424 if (sg_mapped <= MAX_INDIRECT_BUFS) {
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900425 total_length = map_sg_list(cmd, sg_mapped,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900426 &indirect->desc_list[0]);
427 indirect->len = total_length;
James Bottomley4dddbc22005-09-06 17:11:54 -0500428 return 1;
429 }
430
431 /* get indirect table */
432 if (!evt_struct->ext_list) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900433 evt_struct->ext_list = (struct srp_direct_buf *)
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900434 dma_alloc_coherent(dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900435 SG_ALL * sizeof(struct srp_direct_buf),
436 &evt_struct->ext_list_token, 0);
James Bottomley4dddbc22005-09-06 17:11:54 -0500437 if (!evt_struct->ext_list) {
Robert Jennings7912a0a2008-07-24 04:35:27 +1000438 if (!firmware_has_feature(FW_FEATURE_CMO))
439 sdev_printk(KERN_ERR, cmd->device,
440 "Can't allocate memory "
441 "for indirect table\n");
Robert Jenningse637d552009-01-22 13:40:09 -0600442 scsi_dma_unmap(cmd);
James Bottomley4dddbc22005-09-06 17:11:54 -0500443 return 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500444 }
445 }
446
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900447 total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
James Bottomley4dddbc22005-09-06 17:11:54 -0500448
FUJITA Tomonorief265672006-03-26 03:57:14 +0900449 indirect->len = total_length;
450 indirect->table_desc.va = evt_struct->ext_list_token;
451 indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
452 memcpy(indirect->desc_list, evt_struct->ext_list,
453 MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
James Bottomley4dddbc22005-09-06 17:11:54 -0500454 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
459 * @cmd: struct scsi_cmnd with the memory to be mapped
460 * @srp_cmd: srp_cmd that contains the memory descriptor
461 * @dev: dma device for which to map dma memory
462 *
463 * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
464 * Returns 1 on success.
465*/
466static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500467 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct srp_cmd *srp_cmd, struct device *dev)
469{
470 switch (cmd->sc_data_direction) {
471 case DMA_FROM_DEVICE:
472 case DMA_TO_DEVICE:
473 break;
474 case DMA_NONE:
475 return 1;
476 case DMA_BIDIRECTIONAL:
Brian King6c0a60e2007-06-13 17:12:19 -0500477 sdev_printk(KERN_ERR, cmd->device,
478 "Can't map DMA_BIDIRECTIONAL to read/write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480 default:
Brian King6c0a60e2007-06-13 17:12:19 -0500481 sdev_printk(KERN_ERR, cmd->device,
482 "Unknown data direction 0x%02x; can't map!\n",
483 cmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485 }
486
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900487 return map_sg_data(cmd, evt_struct, srp_cmd, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Brian King3d0e91f2007-06-13 17:12:26 -0500490/**
491 * purge_requests: Our virtual adapter just shut down. purge any sent requests
492 * @hostdata: the adapter
493 */
494static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
495{
496 struct srp_event_struct *tmp_evt, *pos;
497 unsigned long flags;
498
499 spin_lock_irqsave(hostdata->host->host_lock, flags);
500 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
501 list_del(&tmp_evt->list);
502 del_timer(&tmp_evt->timer);
503 if (tmp_evt->cmnd) {
504 tmp_evt->cmnd->result = (error_code << 16);
505 unmap_cmd_data(&tmp_evt->iu.srp.cmd,
506 tmp_evt,
507 tmp_evt->hostdata->dev);
508 if (tmp_evt->cmnd_done)
509 tmp_evt->cmnd_done(tmp_evt->cmnd);
510 } else if (tmp_evt->done)
511 tmp_evt->done(tmp_evt);
512 free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
513 }
514 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
515}
516
517/**
518 * ibmvscsi_reset_host - Reset the connection to the server
519 * @hostdata: struct ibmvscsi_host_data to reset
520*/
521static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
522{
523 scsi_block_requests(hostdata->host);
524 atomic_set(&hostdata->request_limit, 0);
525
526 purge_requests(hostdata, DID_ERROR);
David Woodhoused3849d52007-09-22 08:29:36 +1000527 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
528 (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
Brian King3d0e91f2007-06-13 17:12:26 -0500529 (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
530 atomic_set(&hostdata->request_limit, -1);
531 dev_err(hostdata->dev, "error after reset\n");
532 }
533
534 scsi_unblock_requests(hostdata->host);
535}
536
537/**
538 * ibmvscsi_timeout - Internal command timeout handler
539 * @evt_struct: struct srp_event_struct that timed out
540 *
541 * Called when an internally generated command times out
542*/
543static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
544{
545 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
546
547 dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
548 evt_struct->iu.srp.cmd.opcode);
549
550 ibmvscsi_reset_host(hostdata);
551}
552
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/* ------------------------------------------------------------
555 * Routines for sending and receiving SRPs
556 */
557/**
558 * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
559 * @evt_struct: evt_struct to be sent
560 * @hostdata: ibmvscsi_host_data of host
Brian King3d0e91f2007-06-13 17:12:26 -0500561 * @timeout: timeout in seconds - 0 means do not time command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *
563 * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
564 * Note that this routine assumes that host_lock is held for synchronization
565*/
566static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
Brian King3d0e91f2007-06-13 17:12:26 -0500567 struct ibmvscsi_host_data *hostdata,
568 unsigned long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
Robert Jennings3c887e82007-10-30 11:37:07 -0500571 int request_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int rc;
573
Robert Jenningsa897ff22007-03-28 12:45:46 -0500574 /* If we have exhausted our request limit, just fail this request,
575 * unless it is for a reset or abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * Note that there are rare cases involving driver generated requests
577 * (such as task management requests) that the mid layer may think we
578 * can handle more requests (can_queue) when we actually can't
579 */
Dave C Boutchercefbda22006-06-12 21:22:51 -0500580 if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
581 request_status =
582 atomic_dec_if_positive(&hostdata->request_limit);
583 /* If request limit was -1 when we started, it is now even
584 * less than that
585 */
586 if (request_status < -1)
587 goto send_error;
Robert Jenningsa897ff22007-03-28 12:45:46 -0500588 /* Otherwise, we may have run out of requests. */
Robert Jennings3c887e82007-10-30 11:37:07 -0500589 /* If request limit was 0 when we started the adapter is in the
590 * process of performing a login with the server adapter, or
591 * we may have run out of requests.
592 */
593 else if (request_status == -1 &&
594 evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
595 goto send_busy;
Robert Jenningsa897ff22007-03-28 12:45:46 -0500596 /* Abort and reset calls should make it through.
597 * Nothing except abort and reset should use the last two
598 * slots unless we had two or less to begin with.
599 */
600 else if (request_status < 2 &&
601 evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
602 /* In the case that we have less than two requests
603 * available, check the server limit as a combination
604 * of the request limit and the number of requests
605 * in-flight (the size of the send list). If the
606 * server limit is greater than 2, return busy so
607 * that the last two are reserved for reset and abort.
608 */
609 int server_limit = request_status;
610 struct srp_event_struct *tmp_evt;
611
612 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
613 server_limit++;
614 }
615
616 if (server_limit > 2)
617 goto send_busy;
618 }
Dave C Boutchercefbda22006-06-12 21:22:51 -0500619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* Copy the IU into the transfer area */
622 *evt_struct->xfer_iu = evt_struct->iu;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900623 evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* Add this to the sent list. We need to do this
626 * before we actually send
627 * in case it comes back REALLY fast
628 */
629 list_add_tail(&evt_struct->list, &hostdata->sent);
630
Brian King3d0e91f2007-06-13 17:12:26 -0500631 init_timer(&evt_struct->timer);
632 if (timeout) {
633 evt_struct->timer.data = (unsigned long) evt_struct;
634 evt_struct->timer.expires = jiffies + (timeout * HZ);
635 evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
636 add_timer(&evt_struct->timer);
637 }
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if ((rc =
David Woodhoused3849d52007-09-22 08:29:36 +1000640 ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 list_del(&evt_struct->list);
Brian King3d0e91f2007-06-13 17:12:26 -0500642 del_timer(&evt_struct->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Robert Jennings860784c2007-11-12 09:00:23 -0600644 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
645 * Firmware will send a CRQ with a transport event (0xFF) to
646 * tell this client what has happened to the transport. This
647 * will be handled in ibmvscsi_handle_crq()
648 */
649 if (rc == H_CLOSED) {
650 dev_warn(hostdata->dev, "send warning. "
651 "Receive queue closed, will retry.\n");
652 goto send_busy;
653 }
Brian King6c0a60e2007-06-13 17:12:19 -0500654 dev_err(hostdata->dev, "send error %d\n", rc);
Robert Jenningsa897ff22007-03-28 12:45:46 -0500655 atomic_inc(&hostdata->request_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 goto send_error;
657 }
658
659 return 0;
660
Dave C Boutchercefbda22006-06-12 21:22:51 -0500661 send_busy:
James Bottomley4dddbc22005-09-06 17:11:54 -0500662 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 free_event_struct(&hostdata->pool, evt_struct);
Robert Jennings3c887e82007-10-30 11:37:07 -0500665 if (request_status != -1)
666 atomic_inc(&hostdata->request_limit);
Robert Jenningsa897ff22007-03-28 12:45:46 -0500667 return SCSI_MLQUEUE_HOST_BUSY;
Dave C Boutchercefbda22006-06-12 21:22:51 -0500668
669 send_error:
670 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
671
672 if (evt_struct->cmnd != NULL) {
673 evt_struct->cmnd->result = DID_ERROR << 16;
674 evt_struct->cmnd_done(evt_struct->cmnd);
675 } else if (evt_struct->done)
676 evt_struct->done(evt_struct);
677
678 free_event_struct(&hostdata->pool, evt_struct);
679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/**
683 * handle_cmd_rsp: - Handle responses from commands
684 * @evt_struct: srp_event_struct to be handled
685 *
686 * Used as a callback by when sending scsi cmds.
687 * Gets called by ibmvscsi_handle_crq()
688*/
689static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
690{
691 struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
692 struct scsi_cmnd *cmnd = evt_struct->cmnd;
693
FUJITA Tomonorief265672006-03-26 03:57:14 +0900694 if (unlikely(rsp->opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -0500696 dev_warn(evt_struct->hostdata->dev,
697 "bad SRP RSP type %d\n", rsp->opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
700 if (cmnd) {
Brian Kingc3a3b552008-04-25 16:58:29 -0500701 cmnd->result |= rsp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
703 memcpy(cmnd->sense_buffer,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900704 rsp->data,
705 rsp->sense_data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 unmap_cmd_data(&evt_struct->iu.srp.cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500707 evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 evt_struct->hostdata->dev);
709
FUJITA Tomonorief265672006-03-26 03:57:14 +0900710 if (rsp->flags & SRP_RSP_FLAG_DOOVER)
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900711 scsi_set_resid(cmnd, rsp->data_out_res_cnt);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900712 else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900713 scsi_set_resid(cmnd, rsp->data_in_res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
716 if (evt_struct->cmnd_done)
717 evt_struct->cmnd_done(cmnd);
718}
719
720/**
721 * lun_from_dev: - Returns the lun of the scsi device
722 * @dev: struct scsi_device
723 *
724*/
725static inline u16 lun_from_dev(struct scsi_device *dev)
726{
727 return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
728}
729
730/**
731 * ibmvscsi_queue: - The queuecommand function of the scsi template
732 * @cmd: struct scsi_cmnd to be executed
733 * @done: Callback function to be called when cmd is completed
734*/
735static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
736 void (*done) (struct scsi_cmnd *))
737{
738 struct srp_cmd *srp_cmd;
739 struct srp_event_struct *evt_struct;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900740 struct srp_indirect_buf *indirect;
FUJITA Tomonori7603e022007-07-23 09:28:40 +0900741 struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 u16 lun = lun_from_dev(cmnd->device);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900743 u8 out_fmt, in_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Brian Kingc3a3b552008-04-25 16:58:29 -0500745 cmnd->result = (DID_OK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 evt_struct = get_event_struct(&hostdata->pool);
747 if (!evt_struct)
748 return SCSI_MLQUEUE_HOST_BUSY;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Set up the actual SRP IU */
751 srp_cmd = &evt_struct->iu.srp.cmd;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900752 memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
753 srp_cmd->opcode = SRP_CMD;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300754 memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 srp_cmd->lun = ((u64) lun) << 48;
756
James Bottomley4dddbc22005-09-06 17:11:54 -0500757 if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
Robert Jennings7912a0a2008-07-24 04:35:27 +1000758 if (!firmware_has_feature(FW_FEATURE_CMO))
759 sdev_printk(KERN_ERR, cmnd->device,
760 "couldn't convert cmd to srp_cmd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 free_event_struct(&hostdata->pool, evt_struct);
762 return SCSI_MLQUEUE_HOST_BUSY;
763 }
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 init_event_struct(evt_struct,
766 handle_cmd_rsp,
767 VIOSRP_SRP_FORMAT,
Jens Axboe242f9dc2008-09-14 05:55:09 -0700768 cmnd->request->timeout/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 evt_struct->cmnd = cmnd;
771 evt_struct->cmnd_done = done;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* Fix up dma address of the buffer itself */
FUJITA Tomonorief265672006-03-26 03:57:14 +0900774 indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
775 out_fmt = srp_cmd->buf_fmt >> 4;
776 in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
777 if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
778 out_fmt == SRP_DATA_DESC_INDIRECT) &&
779 indirect->table_desc.va == 0) {
780 indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
781 offsetof(struct srp_cmd, add_data) +
782 offsetof(struct srp_indirect_buf, desc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Brian King3d0e91f2007-06-13 17:12:26 -0500785 return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788/* ------------------------------------------------------------
789 * Routines for driver initialization
790 */
Brian King3507e132009-06-08 16:19:04 -0500791
792/**
793 * login_rsp: - Handle response to SRP login request
794 * @evt_struct: srp_event_struct with the response
795 *
796 * Used as a "done" callback by when sending srp_login. Gets called
797 * by ibmvscsi_handle_crq()
798*/
799static void login_rsp(struct srp_event_struct *evt_struct)
800{
801 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
802 switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
803 case SRP_LOGIN_RSP: /* it worked! */
804 break;
805 case SRP_LOGIN_REJ: /* refused! */
806 dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
807 evt_struct->xfer_iu->srp.login_rej.reason);
808 /* Login failed. */
809 atomic_set(&hostdata->request_limit, -1);
810 return;
811 default:
812 dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
813 evt_struct->xfer_iu->srp.login_rsp.opcode);
814 /* Login failed. */
815 atomic_set(&hostdata->request_limit, -1);
816 return;
817 }
818
819 dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
820
821 /* Now we know what the real request-limit is.
822 * This value is set rather than added to request_limit because
823 * request_limit could have been set to -1 by this client.
824 */
825 atomic_set(&hostdata->request_limit,
826 evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
827
828 /* If we had any pending I/Os, kick them */
829 scsi_unblock_requests(hostdata->host);
830}
831
832/**
833 * send_srp_login: - Sends the srp login
834 * @hostdata: ibmvscsi_host_data of host
835 *
836 * Returns zero if successful.
837*/
838static int send_srp_login(struct ibmvscsi_host_data *hostdata)
839{
840 int rc;
841 unsigned long flags;
842 struct srp_login_req *login;
843 struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
844
845 BUG_ON(!evt_struct);
846 init_event_struct(evt_struct, login_rsp,
847 VIOSRP_SRP_FORMAT, login_timeout);
848
849 login = &evt_struct->iu.srp.login_req;
850 memset(login, 0, sizeof(*login));
851 login->opcode = SRP_LOGIN_REQ;
852 login->req_it_iu_len = sizeof(union srp_iu);
853 login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
854
855 spin_lock_irqsave(hostdata->host->host_lock, flags);
856 /* Start out with a request limit of 0, since this is negotiated in
857 * the login request we are just sending and login requests always
858 * get sent by the driver regardless of request_limit.
859 */
860 atomic_set(&hostdata->request_limit, 0);
861
862 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
863 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
864 dev_info(hostdata->dev, "sent SRP login\n");
865 return rc;
866};
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/**
Robert Jenningsc1988e32009-06-08 16:19:07 -0500869 * fast_fail_rsp: - Handle response to MAD enable fast fail
870 * @evt_struct: srp_event_struct with the response
871 *
872 * Used as a "done" callback by when sending enable fast fail. Gets called
873 * by ibmvscsi_handle_crq()
874 */
875static void fast_fail_rsp(struct srp_event_struct *evt_struct)
876{
877 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
878 u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
879
880 if (status == VIOSRP_MAD_NOT_SUPPORTED)
881 dev_err(hostdata->dev, "fast_fail not supported in server\n");
882 else if (status == VIOSRP_MAD_FAILED)
883 dev_err(hostdata->dev, "fast_fail request failed\n");
884 else if (status != VIOSRP_MAD_SUCCESS)
885 dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
886
887 send_srp_login(hostdata);
888}
889
890/**
891 * init_host - Start host initialization
892 * @hostdata: ibmvscsi_host_data of host
893 *
894 * Returns zero if successful.
895 */
896static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
897{
898 int rc;
899 unsigned long flags;
900 struct viosrp_fast_fail *fast_fail_mad;
901 struct srp_event_struct *evt_struct;
902
903 if (!fast_fail)
904 return send_srp_login(hostdata);
905
906 evt_struct = get_event_struct(&hostdata->pool);
907 BUG_ON(!evt_struct);
908
909 init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
910
911 fast_fail_mad = &evt_struct->iu.mad.fast_fail;
912 memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
913 fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
914 fast_fail_mad->common.length = sizeof(*fast_fail_mad);
915
916 spin_lock_irqsave(hostdata->host->host_lock, flags);
917 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
918 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
919 return rc;
920}
921
922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * adapter_info_rsp: - Handle response to MAD adapter info request
924 * @evt_struct: srp_event_struct with the response
925 *
926 * Used as a "done" callback by when sending adapter_info. Gets called
927 * by ibmvscsi_handle_crq()
928*/
929static void adapter_info_rsp(struct srp_event_struct *evt_struct)
930{
931 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
932 dma_unmap_single(hostdata->dev,
933 evt_struct->iu.mad.adapter_info.buffer,
934 evt_struct->iu.mad.adapter_info.common.length,
935 DMA_BIDIRECTIONAL);
936
937 if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
Brian King6c0a60e2007-06-13 17:12:19 -0500938 dev_err(hostdata->dev, "error %d getting adapter info\n",
939 evt_struct->xfer_iu->mad.adapter_info.common.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 } else {
Brian King6c0a60e2007-06-13 17:12:19 -0500941 dev_info(hostdata->dev, "host srp version: %s, "
942 "host partition %s (%d), OS %d, max io %u\n",
943 hostdata->madapter_info.srp_version,
944 hostdata->madapter_info.partition_name,
945 hostdata->madapter_info.partition_number,
946 hostdata->madapter_info.os_type,
947 hostdata->madapter_info.port_max_txu[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (hostdata->madapter_info.port_max_txu[0])
950 hostdata->host->max_sectors =
951 hostdata->madapter_info.port_max_txu[0] >> 9;
Dave C Boutcher154fb612005-09-13 10:09:02 -0500952
953 if (hostdata->madapter_info.os_type == 3 &&
954 strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
Brian King6c0a60e2007-06-13 17:12:19 -0500955 dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
956 hostdata->madapter_info.srp_version);
957 dev_err(hostdata->dev, "limiting scatterlists to %d\n",
958 MAX_INDIRECT_BUFS);
Dave C Boutcher154fb612005-09-13 10:09:02 -0500959 hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Brian King3507e132009-06-08 16:19:04 -0500962
Robert Jenningsc1988e32009-06-08 16:19:07 -0500963 enable_fast_fail(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/**
967 * send_mad_adapter_info: - Sends the mad adapter info request
968 * and stores the result so it can be retrieved with
969 * sysfs. We COULD consider causing a failure if the
970 * returned SRP version doesn't match ours.
971 * @hostdata: ibmvscsi_host_data of host
972 *
973 * Returns zero if successful.
974*/
975static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
976{
977 struct viosrp_adapter_info *req;
978 struct srp_event_struct *evt_struct;
Brian King06f923c2007-06-13 17:12:33 -0500979 unsigned long flags;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +0900980 dma_addr_t addr;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 evt_struct = get_event_struct(&hostdata->pool);
Brian King3507e132009-06-08 16:19:04 -0500983 BUG_ON(!evt_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 init_event_struct(evt_struct,
986 adapter_info_rsp,
987 VIOSRP_MAD_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -0500988 info_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 req = &evt_struct->iu.mad.adapter_info;
991 memset(req, 0x00, sizeof(*req));
992
993 req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
994 req->common.length = sizeof(hostdata->madapter_info);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +0900995 req->buffer = addr = dma_map_single(hostdata->dev,
996 &hostdata->madapter_info,
997 sizeof(hostdata->madapter_info),
998 DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001000 if (dma_mapping_error(hostdata->dev, req->buffer)) {
Robert Jennings7912a0a2008-07-24 04:35:27 +10001001 if (!firmware_has_feature(FW_FEATURE_CMO))
1002 dev_err(hostdata->dev,
1003 "Unable to map request_buffer for "
1004 "adapter_info!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 free_event_struct(&hostdata->pool, evt_struct);
1006 return;
1007 }
1008
Brian King06f923c2007-06-13 17:12:33 -05001009 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001010 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2)) {
Brian King6c0a60e2007-06-13 17:12:19 -05001011 dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001012 dma_unmap_single(hostdata->dev,
1013 addr,
1014 sizeof(hostdata->madapter_info),
1015 DMA_BIDIRECTIONAL);
1016 }
Brian King06f923c2007-06-13 17:12:33 -05001017 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018};
1019
1020/**
Brian King3507e132009-06-08 16:19:04 -05001021 * init_adapter: Start virtual adapter initialization sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 *
Brian King3507e132009-06-08 16:19:04 -05001023 */
1024static void init_adapter(struct ibmvscsi_host_data *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 send_mad_adapter_info(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * sync_completion: Signal that a synchronous command has completed
1031 * Note that after returning from this call, the evt_struct is freed.
1032 * the caller waiting on this completion shouldn't touch the evt_struct
1033 * again.
1034 */
1035static void sync_completion(struct srp_event_struct *evt_struct)
1036{
1037 /* copy the response back */
1038 if (evt_struct->sync_srp)
1039 *evt_struct->sync_srp = *evt_struct->xfer_iu;
1040
1041 complete(&evt_struct->comp);
1042}
1043
1044/**
1045 * ibmvscsi_abort: Abort a command...from scsi host template
1046 * send this over to the server and wait synchronously for the response
1047 */
1048static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
1049{
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001050 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct srp_tsk_mgmt *tsk_mgmt;
1052 struct srp_event_struct *evt;
1053 struct srp_event_struct *tmp_evt, *found_evt;
1054 union viosrp_iu srp_rsp;
1055 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001056 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 u16 lun = lun_from_dev(cmd->device);
Robert Jennings860784c2007-11-12 09:00:23 -06001058 unsigned long wait_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /* First, find this command in our sent list so we can figure
1061 * out the correct tag
1062 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001063 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001064 wait_switch = jiffies + (init_timeout * HZ);
1065 do {
1066 found_evt = NULL;
1067 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1068 if (tmp_evt->cmnd == cmd) {
1069 found_evt = tmp_evt;
1070 break;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Robert Jennings860784c2007-11-12 09:00:23 -06001074 if (!found_evt) {
1075 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1076 return SUCCESS;
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Robert Jennings860784c2007-11-12 09:00:23 -06001079 evt = get_event_struct(&hostdata->pool);
1080 if (evt == NULL) {
1081 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1082 sdev_printk(KERN_ERR, cmd->device,
1083 "failed to allocate abort event\n");
1084 return FAILED;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Robert Jennings860784c2007-11-12 09:00:23 -06001087 init_event_struct(evt,
1088 sync_completion,
1089 VIOSRP_SRP_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001090 abort_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Robert Jennings860784c2007-11-12 09:00:23 -06001092 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Robert Jennings860784c2007-11-12 09:00:23 -06001094 /* Set up an abort SRP command */
1095 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1096 tsk_mgmt->opcode = SRP_TSK_MGMT;
1097 tsk_mgmt->lun = ((u64) lun) << 48;
1098 tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
1099 tsk_mgmt->task_tag = (u64) found_evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Robert Jennings860784c2007-11-12 09:00:23 -06001101 evt->sync_srp = &srp_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Robert Jennings860784c2007-11-12 09:00:23 -06001103 init_completion(&evt->comp);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001104 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
Robert Jennings860784c2007-11-12 09:00:23 -06001105
1106 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1107 break;
1108
1109 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1110 msleep(10);
1111 spin_lock_irqsave(hostdata->host->host_lock, flags);
1112 } while (time_before(jiffies, wait_switch));
1113
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001114 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001115
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001116 if (rsp_rc != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001117 sdev_printk(KERN_ERR, cmd->device,
1118 "failed to send abort() event. rc=%d\n", rsp_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return FAILED;
1120 }
1121
Robert Jennings860784c2007-11-12 09:00:23 -06001122 sdev_printk(KERN_INFO, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001123 "aborting command. lun 0x%llx, tag 0x%llx\n",
Robert Jennings860784c2007-11-12 09:00:23 -06001124 (((u64) lun) << 48), (u64) found_evt);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001129 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001131 sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
1132 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return FAILED;
1134 }
1135
FUJITA Tomonorief265672006-03-26 03:57:14 +09001136 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1137 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 else
1139 rsp_rc = srp_rsp.srp.rsp.status;
1140
1141 if (rsp_rc) {
1142 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001143 sdev_printk(KERN_WARNING, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001144 "abort code %d for task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001145 rsp_rc, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return FAILED;
1147 }
1148
1149 /* Because we dropped the spinlock above, it's possible
1150 * The event is no longer in our list. Make sure it didn't
1151 * complete while we were aborting
1152 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001153 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 found_evt = NULL;
1155 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1156 if (tmp_evt->cmnd == cmd) {
1157 found_evt = tmp_evt;
1158 break;
1159 }
1160 }
1161
1162 if (found_evt == NULL) {
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001163 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Ingo Molnarfe333322009-01-06 14:26:03 +00001164 sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001165 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return SUCCESS;
1167 }
1168
Ingo Molnarfe333322009-01-06 14:26:03 +00001169 sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001170 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 cmd->result = (DID_ABORT << 16);
1173 list_del(&found_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001174 unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1175 found_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 free_event_struct(&found_evt->hostdata->pool, found_evt);
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001177 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 atomic_inc(&hostdata->request_limit);
1179 return SUCCESS;
1180}
1181
1182/**
1183 * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
1184 * template send this over to the server and wait synchronously for the
1185 * response
1186 */
1187static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1188{
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001189 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct srp_tsk_mgmt *tsk_mgmt;
1191 struct srp_event_struct *evt;
1192 struct srp_event_struct *tmp_evt, *pos;
1193 union viosrp_iu srp_rsp;
1194 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001195 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 u16 lun = lun_from_dev(cmd->device);
Robert Jennings860784c2007-11-12 09:00:23 -06001197 unsigned long wait_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001199 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001200 wait_switch = jiffies + (init_timeout * HZ);
1201 do {
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 reset event\n");
1207 return FAILED;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Robert Jennings860784c2007-11-12 09:00:23 -06001210 init_event_struct(evt,
1211 sync_completion,
1212 VIOSRP_SRP_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001213 reset_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Robert Jennings860784c2007-11-12 09:00:23 -06001215 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Robert Jennings860784c2007-11-12 09:00:23 -06001217 /* Set up a lun reset 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_LUN_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Robert Jennings860784c2007-11-12 09:00:23 -06001223 evt->sync_srp = &srp_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Robert Jennings860784c2007-11-12 09:00:23 -06001225 init_completion(&evt->comp);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001226 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
Robert Jennings860784c2007-11-12 09:00:23 -06001227
1228 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1229 break;
1230
1231 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1232 msleep(10);
1233 spin_lock_irqsave(hostdata->host->host_lock, flags);
1234 } while (time_before(jiffies, wait_switch));
1235
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001236 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001237
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001238 if (rsp_rc != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001239 sdev_printk(KERN_ERR, cmd->device,
1240 "failed to send reset event. rc=%d\n", rsp_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return FAILED;
1242 }
1243
Ingo Molnarfe333322009-01-06 14:26:03 +00001244 sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
Robert Jennings860784c2007-11-12 09:00:23 -06001245 (((u64) lun) << 48));
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001250 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001252 sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
1253 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return FAILED;
1255 }
1256
FUJITA Tomonorief265672006-03-26 03:57:14 +09001257 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1258 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 else
1260 rsp_rc = srp_rsp.srp.rsp.status;
1261
1262 if (rsp_rc) {
1263 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001264 sdev_printk(KERN_WARNING, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001265 "reset code %d for task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001266 rsp_rc, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return FAILED;
1268 }
1269
1270 /* We need to find all commands for this LUN that have not yet been
1271 * responded to, and fail them with DID_RESET
1272 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001273 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1275 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1276 if (tmp_evt->cmnd)
1277 tmp_evt->cmnd->result = (DID_RESET << 16);
1278 list_del(&tmp_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001279 unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1280 tmp_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 free_event_struct(&tmp_evt->hostdata->pool,
1282 tmp_evt);
1283 atomic_inc(&hostdata->request_limit);
1284 if (tmp_evt->cmnd_done)
1285 tmp_evt->cmnd_done(tmp_evt->cmnd);
1286 else if (tmp_evt->done)
1287 tmp_evt->done(tmp_evt);
1288 }
1289 }
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001290 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return SUCCESS;
1292}
1293
1294/**
Brian King3d0e91f2007-06-13 17:12:26 -05001295 * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
1296 * @cmd: struct scsi_cmnd having problems
1297*/
1298static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Brian King3d0e91f2007-06-13 17:12:26 -05001300 unsigned long wait_switch = 0;
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001301 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Brian King3d0e91f2007-06-13 17:12:26 -05001303 dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
1304
1305 ibmvscsi_reset_host(hostdata);
1306
1307 for (wait_switch = jiffies + (init_timeout * HZ);
1308 time_before(jiffies, wait_switch) &&
1309 atomic_read(&hostdata->request_limit) < 2;) {
1310
1311 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Brian King3d0e91f2007-06-13 17:12:26 -05001313
1314 if (atomic_read(&hostdata->request_limit) <= 0)
1315 return FAILED;
1316
1317 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320/**
1321 * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1322 * @crq: Command/Response queue
1323 * @hostdata: ibmvscsi_host_data of host
1324 *
1325*/
1326void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1327 struct ibmvscsi_host_data *hostdata)
1328{
Brian King6c0a60e2007-06-13 17:12:19 -05001329 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 unsigned long flags;
1331 struct srp_event_struct *evt_struct =
1332 (struct srp_event_struct *)crq->IU_data_ptr;
1333 switch (crq->valid) {
1334 case 0xC0: /* initialization */
1335 switch (crq->format) {
1336 case 0x01: /* Initialization message */
Brian King6c0a60e2007-06-13 17:12:19 -05001337 dev_info(hostdata->dev, "partner initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* Send back a response */
David Woodhoused3849d52007-09-22 08:29:36 +10001339 if ((rc = ibmvscsi_ops->send_crq(hostdata,
1340 0xC002000000000000LL, 0)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* Now login */
Brian King3507e132009-06-08 16:19:04 -05001342 init_adapter(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001344 dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
1347 break;
1348 case 0x02: /* Initialization response */
Brian King6c0a60e2007-06-13 17:12:19 -05001349 dev_info(hostdata->dev, "partner initialization complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* Now login */
Brian King3507e132009-06-08 16:19:04 -05001352 init_adapter(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 break;
1354 default:
Brian King6c0a60e2007-06-13 17:12:19 -05001355 dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 return;
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001358 case 0xFF: /* Hypervisor telling us the connection is closed */
1359 scsi_block_requests(hostdata->host);
Dave C Boutchercefbda22006-06-12 21:22:51 -05001360 atomic_set(&hostdata->request_limit, 0);
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001361 if (crq->format == 0x06) {
1362 /* We need to re-setup the interpartition connection */
Brian King6c0a60e2007-06-13 17:12:19 -05001363 dev_info(hostdata->dev, "Re-enabling adapter!\n");
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001364 purge_requests(hostdata, DID_REQUEUE);
David Woodhoused3849d52007-09-22 08:29:36 +10001365 if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
1366 hostdata)) ||
1367 (ibmvscsi_ops->send_crq(hostdata,
1368 0xC001000000000000LL, 0))) {
Dave C Boutchercefbda22006-06-12 21:22:51 -05001369 atomic_set(&hostdata->request_limit,
1370 -1);
Brian King6c0a60e2007-06-13 17:12:19 -05001371 dev_err(hostdata->dev, "error after enable\n");
Dave C Boutchercefbda22006-06-12 21:22:51 -05001372 }
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001373 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001374 dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
1375 crq->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001377 purge_requests(hostdata, DID_ERROR);
David Woodhoused3849d52007-09-22 08:29:36 +10001378 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
1379 hostdata)) ||
1380 (ibmvscsi_ops->send_crq(hostdata,
1381 0xC001000000000000LL, 0))) {
Dave C Boutchercefbda22006-06-12 21:22:51 -05001382 atomic_set(&hostdata->request_limit,
1383 -1);
Brian King6c0a60e2007-06-13 17:12:19 -05001384 dev_err(hostdata->dev, "error after reset\n");
Dave C Boutchercefbda22006-06-12 21:22:51 -05001385 }
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001386 }
1387 scsi_unblock_requests(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return;
1389 case 0x80: /* real payload */
1390 break;
1391 default:
Brian King6c0a60e2007-06-13 17:12:19 -05001392 dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
1393 crq->valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return;
1395 }
1396
1397 /* The only kind of payload CRQs we should get are responses to
1398 * things we send. Make sure this response is to something we
1399 * actually sent
1400 */
1401 if (!valid_event_struct(&hostdata->pool, evt_struct)) {
Brian King6c0a60e2007-06-13 17:12:19 -05001402 dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 (void *)crq->IU_data_ptr);
1404 return;
1405 }
1406
1407 if (atomic_read(&evt_struct->free)) {
Brian King6c0a60e2007-06-13 17:12:19 -05001408 dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
1409 (void *)crq->IU_data_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return;
1411 }
1412
1413 if (crq->format == VIOSRP_SRP_FORMAT)
FUJITA Tomonorief265672006-03-26 03:57:14 +09001414 atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 &hostdata->request_limit);
1416
Brian King3d0e91f2007-06-13 17:12:26 -05001417 del_timer(&evt_struct->timer);
1418
Brian Kingca616682008-05-19 10:27:56 -05001419 if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
Brian Kingc3a3b552008-04-25 16:58:29 -05001420 evt_struct->cmnd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (evt_struct->done)
1422 evt_struct->done(evt_struct);
1423 else
Brian King6c0a60e2007-06-13 17:12:19 -05001424 dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /*
1427 * Lock the host_lock before messing with these structures, since we
1428 * are running in a task context
1429 */
1430 spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1431 list_del(&evt_struct->list);
1432 free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1433 spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1434}
1435
1436/**
1437 * ibmvscsi_get_host_config: Send the command to the server to get host
1438 * configuration data. The data is opaque to us.
1439 */
1440static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1441 unsigned char *buffer, int length)
1442{
1443 struct viosrp_host_config *host_config;
1444 struct srp_event_struct *evt_struct;
Brian King06f923c2007-06-13 17:12:33 -05001445 unsigned long flags;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001446 dma_addr_t addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int rc;
1448
1449 evt_struct = get_event_struct(&hostdata->pool);
1450 if (!evt_struct) {
Brian King6c0a60e2007-06-13 17:12:19 -05001451 dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -1;
1453 }
1454
1455 init_event_struct(evt_struct,
1456 sync_completion,
1457 VIOSRP_MAD_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001458 info_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 host_config = &evt_struct->iu.mad.host_config;
1461
1462 /* Set up a lun reset SRP command */
1463 memset(host_config, 0x00, sizeof(*host_config));
1464 host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1465 host_config->common.length = length;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001466 host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
1467 length,
1468 DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001470 if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
Robert Jennings7912a0a2008-07-24 04:35:27 +10001471 if (!firmware_has_feature(FW_FEATURE_CMO))
1472 dev_err(hostdata->dev,
1473 "dma_mapping error getting host config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 free_event_struct(&hostdata->pool, evt_struct);
1475 return -1;
1476 }
1477
1478 init_completion(&evt_struct->comp);
Brian King06f923c2007-06-13 17:12:33 -05001479 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001480 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
Brian King06f923c2007-06-13 17:12:33 -05001481 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001482 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 wait_for_completion(&evt_struct->comp);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001484 dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 return rc;
1487}
1488
Robert Jennings0979c842007-03-29 12:30:40 -05001489/**
1490 * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
1491 * @sdev: struct scsi_device device to configure
1492 *
1493 * Enable allow_restart for a device if it is a disk. Adjust the
1494 * queue_depth here also as is required by the documentation for
1495 * struct scsi_host_template.
1496 */
1497static int ibmvscsi_slave_configure(struct scsi_device *sdev)
1498{
1499 struct Scsi_Host *shost = sdev->host;
1500 unsigned long lock_flags = 0;
1501
1502 spin_lock_irqsave(shost->host_lock, lock_flags);
Brian Kingd1a357f2007-10-25 16:06:34 -05001503 if (sdev->type == TYPE_DISK) {
Robert Jennings0979c842007-03-29 12:30:40 -05001504 sdev->allow_restart = 1;
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001505 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
Brian Kingd1a357f2007-10-25 16:06:34 -05001506 }
Robert Jennings0979c842007-03-29 12:30:40 -05001507 scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
1508 spin_unlock_irqrestore(shost->host_lock, lock_flags);
1509 return 0;
1510}
1511
Brian King742d25b2007-05-29 15:46:14 -05001512/**
1513 * ibmvscsi_change_queue_depth - Change the device's queue depth
1514 * @sdev: scsi device struct
1515 * @qdepth: depth to set
1516 *
1517 * Return value:
1518 * actual depth set
1519 **/
1520static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
1521{
1522 if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
1523 qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
1524
1525 scsi_adjust_queue_depth(sdev, 0, qdepth);
1526 return sdev->queue_depth;
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/* ------------------------------------------------------------
1530 * sysfs attributes
1531 */
Tony Jonesee959b02008-02-22 00:13:36 +01001532static ssize_t show_host_srp_version(struct device *dev,
1533 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Tony Jonesee959b02008-02-22 00:13:36 +01001535 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001536 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int len;
1538
1539 len = snprintf(buf, PAGE_SIZE, "%s\n",
1540 hostdata->madapter_info.srp_version);
1541 return len;
1542}
1543
Tony Jonesee959b02008-02-22 00:13:36 +01001544static struct device_attribute ibmvscsi_host_srp_version = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 .attr = {
1546 .name = "srp_version",
1547 .mode = S_IRUGO,
1548 },
1549 .show = show_host_srp_version,
1550};
1551
Tony Jonesee959b02008-02-22 00:13:36 +01001552static ssize_t show_host_partition_name(struct device *dev,
1553 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 char *buf)
1555{
Tony Jonesee959b02008-02-22 00:13:36 +01001556 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001557 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int len;
1559
1560 len = snprintf(buf, PAGE_SIZE, "%s\n",
1561 hostdata->madapter_info.partition_name);
1562 return len;
1563}
1564
Tony Jonesee959b02008-02-22 00:13:36 +01001565static struct device_attribute ibmvscsi_host_partition_name = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 .attr = {
1567 .name = "partition_name",
1568 .mode = S_IRUGO,
1569 },
1570 .show = show_host_partition_name,
1571};
1572
Tony Jonesee959b02008-02-22 00:13:36 +01001573static ssize_t show_host_partition_number(struct device *dev,
1574 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 char *buf)
1576{
Tony Jonesee959b02008-02-22 00:13:36 +01001577 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001578 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int len;
1580
1581 len = snprintf(buf, PAGE_SIZE, "%d\n",
1582 hostdata->madapter_info.partition_number);
1583 return len;
1584}
1585
Tony Jonesee959b02008-02-22 00:13:36 +01001586static struct device_attribute ibmvscsi_host_partition_number = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 .attr = {
1588 .name = "partition_number",
1589 .mode = S_IRUGO,
1590 },
1591 .show = show_host_partition_number,
1592};
1593
Tony Jonesee959b02008-02-22 00:13:36 +01001594static ssize_t show_host_mad_version(struct device *dev,
1595 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Tony Jonesee959b02008-02-22 00:13:36 +01001597 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001598 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 int len;
1600
1601 len = snprintf(buf, PAGE_SIZE, "%d\n",
1602 hostdata->madapter_info.mad_version);
1603 return len;
1604}
1605
Tony Jonesee959b02008-02-22 00:13:36 +01001606static struct device_attribute ibmvscsi_host_mad_version = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 .attr = {
1608 .name = "mad_version",
1609 .mode = S_IRUGO,
1610 },
1611 .show = show_host_mad_version,
1612};
1613
Tony Jonesee959b02008-02-22 00:13:36 +01001614static ssize_t show_host_os_type(struct device *dev,
1615 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Tony Jonesee959b02008-02-22 00:13:36 +01001617 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001618 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int len;
1620
1621 len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
1622 return len;
1623}
1624
Tony Jonesee959b02008-02-22 00:13:36 +01001625static struct device_attribute ibmvscsi_host_os_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 .attr = {
1627 .name = "os_type",
1628 .mode = S_IRUGO,
1629 },
1630 .show = show_host_os_type,
1631};
1632
Tony Jonesee959b02008-02-22 00:13:36 +01001633static ssize_t show_host_config(struct device *dev,
1634 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Tony Jonesee959b02008-02-22 00:13:36 +01001636 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001637 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /* returns null-terminated host config data */
1640 if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
1641 return strlen(buf);
1642 else
1643 return 0;
1644}
1645
Tony Jonesee959b02008-02-22 00:13:36 +01001646static struct device_attribute ibmvscsi_host_config = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 .attr = {
1648 .name = "config",
1649 .mode = S_IRUGO,
1650 },
1651 .show = show_host_config,
1652};
1653
Tony Jonesee959b02008-02-22 00:13:36 +01001654static struct device_attribute *ibmvscsi_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 &ibmvscsi_host_srp_version,
1656 &ibmvscsi_host_partition_name,
1657 &ibmvscsi_host_partition_number,
1658 &ibmvscsi_host_mad_version,
1659 &ibmvscsi_host_os_type,
1660 &ibmvscsi_host_config,
1661 NULL
1662};
1663
1664/* ------------------------------------------------------------
1665 * SCSI driver registration
1666 */
1667static struct scsi_host_template driver_template = {
1668 .module = THIS_MODULE,
1669 .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
1670 .proc_name = "ibmvscsi",
1671 .queuecommand = ibmvscsi_queuecommand,
1672 .eh_abort_handler = ibmvscsi_eh_abort_handler,
1673 .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
Brian King3d0e91f2007-06-13 17:12:26 -05001674 .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
Robert Jennings0979c842007-03-29 12:30:40 -05001675 .slave_configure = ibmvscsi_slave_configure,
Brian King742d25b2007-05-29 15:46:14 -05001676 .change_queue_depth = ibmvscsi_change_queue_depth,
Robert Jennings7912a0a2008-07-24 04:35:27 +10001677 .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
Robert Jenningsa897ff22007-03-28 12:45:46 -05001678 .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 .this_id = -1,
James Bottomley4dddbc22005-09-06 17:11:54 -05001680 .sg_tablesize = SG_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 .use_clustering = ENABLE_CLUSTERING,
1682 .shost_attrs = ibmvscsi_attrs,
1683};
1684
1685/**
Robert Jennings7912a0a2008-07-24 04:35:27 +10001686 * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
1687 *
1688 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1689 *
1690 * Return value:
1691 * Number of bytes of IO data the driver will need to perform well.
1692 */
1693static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
1694{
1695 /* iu_storage data allocated in initialize_event_pool */
Brian King4f10aae2008-12-17 17:19:33 -06001696 unsigned long desired_io = max_events * sizeof(union viosrp_iu);
Robert Jennings7912a0a2008-07-24 04:35:27 +10001697
1698 /* add io space for sg data */
Brian King004dd5e2008-08-15 10:48:47 -05001699 desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
Robert Jennings7912a0a2008-07-24 04:35:27 +10001700 IBMVSCSI_CMDS_PER_LUN_DEFAULT);
1701
1702 return desired_io;
1703}
1704
1705/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * Called by bus code for each adapter
1707 */
1708static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1709{
1710 struct ibmvscsi_host_data *hostdata;
1711 struct Scsi_Host *host;
1712 struct device *dev = &vdev->dev;
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001713 struct srp_rport_identifiers ids;
1714 struct srp_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 unsigned long wait_switch = 0;
Dave C Boutchercefbda22006-06-12 21:22:51 -05001716 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 vdev->dev.driver_data = NULL;
1719
1720 host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
1721 if (!host) {
Brian King6c0a60e2007-06-13 17:12:19 -05001722 dev_err(&vdev->dev, "couldn't allocate host data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 goto scsi_host_alloc_failed;
1724 }
1725
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001726 host->transportt = ibmvscsi_transport_template;
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001727 hostdata = shost_priv(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 memset(hostdata, 0x00, sizeof(*hostdata));
1729 INIT_LIST_HEAD(&hostdata->sent);
1730 hostdata->host = host;
1731 hostdata->dev = dev;
1732 atomic_set(&hostdata->request_limit, -1);
Robert Jennings7912a0a2008-07-24 04:35:27 +10001733 hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Brian King4f10aae2008-12-17 17:19:33 -06001735 rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
Dave C Boutchercefbda22006-06-12 21:22:51 -05001736 if (rc != 0 && rc != H_RESOURCE) {
Brian King6c0a60e2007-06-13 17:12:19 -05001737 dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto init_crq_failed;
1739 }
Brian King4f10aae2008-12-17 17:19:33 -06001740 if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001741 dev_err(&vdev->dev, "couldn't initialize event pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 goto init_pool_failed;
1743 }
1744
1745 host->max_lun = 8;
1746 host->max_id = max_id;
1747 host->max_channel = max_channel;
Brian Kingfbc56f02009-06-08 16:19:01 -05001748 host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 if (scsi_add_host(hostdata->host, hostdata->dev))
1751 goto add_host_failed;
1752
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001753 /* we don't have a proper target_port_id so let's use the fake one */
1754 memcpy(ids.port_id, hostdata->madapter_info.partition_name,
1755 sizeof(ids.port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001756 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001757 rport = srp_rport_add(host, &ids);
1758 if (IS_ERR(rport))
1759 goto add_srp_port_failed;
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* Try to send an initialization message. Note that this is allowed
1762 * to fail if the other end is not acive. In that case we don't
1763 * want to scan
1764 */
David Woodhoused3849d52007-09-22 08:29:36 +10001765 if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
Dave C Boutchercefbda22006-06-12 21:22:51 -05001766 || rc == H_RESOURCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /*
1768 * Wait around max init_timeout secs for the adapter to finish
1769 * initializing. When we are done initializing, we will have a
1770 * valid request_limit. We don't want Linux scanning before
1771 * we are ready.
1772 */
1773 for (wait_switch = jiffies + (init_timeout * HZ);
1774 time_before(jiffies, wait_switch) &&
1775 atomic_read(&hostdata->request_limit) < 2;) {
1776
1777 msleep(10);
1778 }
1779
1780 /* if we now have a valid request_limit, initiate a scan */
1781 if (atomic_read(&hostdata->request_limit) > 0)
1782 scsi_scan_host(host);
1783 }
1784
1785 vdev->dev.driver_data = hostdata;
1786 return 0;
1787
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001788 add_srp_port_failed:
1789 scsi_remove_host(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 add_host_failed:
1791 release_event_pool(&hostdata->pool, hostdata);
1792 init_pool_failed:
Brian King4f10aae2008-12-17 17:19:33 -06001793 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 init_crq_failed:
1795 scsi_host_put(host);
1796 scsi_host_alloc_failed:
1797 return -1;
1798}
1799
1800static int ibmvscsi_remove(struct vio_dev *vdev)
1801{
1802 struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
1803 release_event_pool(&hostdata->pool, hostdata);
David Woodhoused3849d52007-09-22 08:29:36 +10001804 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
Brian King4f10aae2008-12-17 17:19:33 -06001805 max_events);
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001806
1807 srp_remove_host(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 scsi_remove_host(hostdata->host);
1809 scsi_host_put(hostdata->host);
1810
1811 return 0;
1812}
1813
1814/**
1815 * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
1816 * support.
1817 */
1818static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
1819 {"vscsi", "IBM,v-scsi"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001820 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +10001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824static struct vio_driver ibmvscsi_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 .id_table = ibmvscsi_device_table,
1826 .probe = ibmvscsi_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001827 .remove = ibmvscsi_remove,
Robert Jennings7912a0a2008-07-24 04:35:27 +10001828 .get_desired_dma = ibmvscsi_get_desired_dma,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001829 .driver = {
1830 .name = "ibmvscsi",
Stephen Rothwell915124d2005-10-24 15:12:22 +10001831 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833};
1834
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001835static struct srp_function_template ibmvscsi_transport_functions = {
1836};
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838int __init ibmvscsi_module_init(void)
1839{
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001840 int ret;
1841
Brian King4f10aae2008-12-17 17:19:33 -06001842 /* Ensure we have two requests to do error recovery */
1843 driver_template.can_queue = max_requests;
1844 max_events = max_requests + 2;
1845
David Woodhoused3849d52007-09-22 08:29:36 +10001846 if (firmware_has_feature(FW_FEATURE_ISERIES))
1847 ibmvscsi_ops = &iseriesvscsi_ops;
1848 else if (firmware_has_feature(FW_FEATURE_VIO))
1849 ibmvscsi_ops = &rpavscsi_ops;
1850 else
1851 return -ENODEV;
1852
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001853 ibmvscsi_transport_template =
1854 srp_attach_transport(&ibmvscsi_transport_functions);
1855 if (!ibmvscsi_transport_template)
1856 return -ENOMEM;
1857
1858 ret = vio_register_driver(&ibmvscsi_driver);
1859 if (ret)
1860 srp_release_transport(ibmvscsi_transport_template);
1861 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864void __exit ibmvscsi_module_exit(void)
1865{
1866 vio_unregister_driver(&ibmvscsi_driver);
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001867 srp_release_transport(ibmvscsi_transport_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870module_init(ibmvscsi_module_init);
1871module_exit(ibmvscsi_module_exit);