blob: e475b7957c2d03167dbc3a027b36f2b4a74802cd [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>
Brian King126c5cc2009-06-08 16:19:08 -050073#include <linux/of.h>
David Woodhoused3849d52007-09-22 08:29:36 +100074#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/vio.h>
76#include <scsi/scsi.h>
77#include <scsi/scsi_cmnd.h>
78#include <scsi/scsi_host.h>
79#include <scsi/scsi_device.h>
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;
Brian King126c5cc2009-06-08 16:19:08 -050098static int client_reserve = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
FUJITA Tomonori4d680412007-06-27 16:32:50 +0900100static struct scsi_transport_template *ibmvscsi_transport_template;
101
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600102#define IBMVSCSI_VERSION "1.5.8"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
David Woodhoused3849d52007-09-22 08:29:36 +1000104static struct ibmvscsi_ops *ibmvscsi_ops;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106MODULE_DESCRIPTION("IBM Virtual SCSI");
107MODULE_AUTHOR("Dave Boutcher");
108MODULE_LICENSE("GPL");
109MODULE_VERSION(IBMVSCSI_VERSION);
110
111module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
112MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
113module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
114MODULE_PARM_DESC(max_channel, "Largest channel value");
115module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
116MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
Brian King21465ed2008-12-08 17:01:47 -0600117module_param_named(max_requests, max_requests, int, S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
Robert Jenningsc1988e32009-06-08 16:19:07 -0500119module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
120MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
Brian King126c5cc2009-06-08 16:19:08 -0500121module_param_named(client_reserve, client_reserve, int, S_IRUGO );
122MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/* ------------------------------------------------------------
125 * Routines for the event pool and event structs
126 */
127/**
128 * initialize_event_pool: - Allocates and initializes the event pool for a host
129 * @pool: event_pool to be initialized
130 * @size: Number of events in pool
131 * @hostdata: ibmvscsi_host_data who owns the event pool
132 *
133 * Returns zero on success.
134*/
135static int initialize_event_pool(struct event_pool *pool,
136 int size, struct ibmvscsi_host_data *hostdata)
137{
138 int i;
139
140 pool->size = size;
141 pool->next = 0;
FUJITA Tomonori4c021dd132006-04-07 19:10:03 +0900142 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!pool->events)
144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 pool->iu_storage =
147 dma_alloc_coherent(hostdata->dev,
148 pool->size * sizeof(*pool->iu_storage),
149 &pool->iu_token, 0);
150 if (!pool->iu_storage) {
151 kfree(pool->events);
152 return -ENOMEM;
153 }
154
155 for (i = 0; i < pool->size; ++i) {
156 struct srp_event_struct *evt = &pool->events[i];
157 memset(&evt->crq, 0x00, sizeof(evt->crq));
158 atomic_set(&evt->free, 1);
159 evt->crq.valid = 0x80;
160 evt->crq.IU_length = sizeof(*evt->xfer_iu);
161 evt->crq.IU_data_ptr = pool->iu_token +
162 sizeof(*evt->xfer_iu) * i;
163 evt->xfer_iu = pool->iu_storage + i;
164 evt->hostdata = hostdata;
James Bottomley4dddbc22005-09-06 17:11:54 -0500165 evt->ext_list = NULL;
166 evt->ext_list_token = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
169 return 0;
170}
171
172/**
173 * release_event_pool: - Frees memory of an event pool of a host
174 * @pool: event_pool to be released
175 * @hostdata: ibmvscsi_host_data who owns the even pool
176 *
177 * Returns zero on success.
178*/
179static void release_event_pool(struct event_pool *pool,
180 struct ibmvscsi_host_data *hostdata)
181{
182 int i, in_use = 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500183 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (atomic_read(&pool->events[i].free) != 1)
185 ++in_use;
James Bottomley4dddbc22005-09-06 17:11:54 -0500186 if (pool->events[i].ext_list) {
187 dma_free_coherent(hostdata->dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900188 SG_ALL * sizeof(struct srp_direct_buf),
James Bottomley4dddbc22005-09-06 17:11:54 -0500189 pool->events[i].ext_list,
190 pool->events[i].ext_list_token);
191 }
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (in_use)
Brian King6c0a60e2007-06-13 17:12:19 -0500194 dev_warn(hostdata->dev, "releasing event pool with %d "
195 "events still in use?\n", in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 kfree(pool->events);
197 dma_free_coherent(hostdata->dev,
198 pool->size * sizeof(*pool->iu_storage),
199 pool->iu_storage, pool->iu_token);
200}
201
202/**
203 * valid_event_struct: - Determines if event is valid.
204 * @pool: event_pool that contains the event
205 * @evt: srp_event_struct to be checked for validity
206 *
207 * Returns zero if event is invalid, one otherwise.
208*/
209static int valid_event_struct(struct event_pool *pool,
210 struct srp_event_struct *evt)
211{
212 int index = evt - pool->events;
213 if (index < 0 || index >= pool->size) /* outside of bounds */
214 return 0;
215 if (evt != pool->events + index) /* unaligned */
216 return 0;
217 return 1;
218}
219
220/**
221 * ibmvscsi_free-event_struct: - Changes status of event to "free"
222 * @pool: event_pool that contains the event
223 * @evt: srp_event_struct to be modified
224 *
225*/
226static void free_event_struct(struct event_pool *pool,
227 struct srp_event_struct *evt)
228{
229 if (!valid_event_struct(pool, evt)) {
Brian King6c0a60e2007-06-13 17:12:19 -0500230 dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
231 "(not in pool %p)\n", evt, pool->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
233 }
234 if (atomic_inc_return(&evt->free) != 1) {
Brian King6c0a60e2007-06-13 17:12:19 -0500235 dev_err(evt->hostdata->dev, "Freeing event_struct %p "
236 "which is not in use!\n", evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return;
238 }
239}
240
241/**
242 * get_evt_struct: - Gets the next free event in pool
243 * @pool: event_pool that contains the events to be searched
244 *
245 * Returns the next event in "free" state, and NULL if none are free.
246 * Note that no synchronization is done here, we assume the host_lock
247 * will syncrhonze things.
248*/
249static struct srp_event_struct *get_event_struct(struct event_pool *pool)
250{
251 int i;
252 int poolsize = pool->size;
253 int offset = pool->next;
254
255 for (i = 0; i < poolsize; i++) {
256 offset = (offset + 1) % poolsize;
257 if (!atomic_dec_if_positive(&pool->events[offset].free)) {
258 pool->next = offset;
259 return &pool->events[offset];
260 }
261 }
262
263 printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
264 return NULL;
265}
266
267/**
268 * init_event_struct: Initialize fields in an event struct that are always
269 * required.
270 * @evt: The event
271 * @done: Routine to call when the event is responded to
272 * @format: SRP or MAD format
273 * @timeout: timeout value set in the CRQ
274 */
275static void init_event_struct(struct srp_event_struct *evt_struct,
276 void (*done) (struct srp_event_struct *),
277 u8 format,
278 int timeout)
279{
280 evt_struct->cmnd = NULL;
281 evt_struct->cmnd_done = NULL;
282 evt_struct->sync_srp = NULL;
283 evt_struct->crq.format = format;
284 evt_struct->crq.timeout = timeout;
285 evt_struct->done = done;
286}
287
288/* ------------------------------------------------------------
289 * Routines for receiving SCSI responses from the hosting partition
290 */
291
292/**
293 * set_srp_direction: Set the fields in the srp related to data
294 * direction and number of buffers based on the direction in
295 * the scsi_cmnd and the number of buffers
296 */
297static void set_srp_direction(struct scsi_cmnd *cmd,
298 struct srp_cmd *srp_cmd,
299 int numbuf)
300{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900301 u8 fmt;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (numbuf == 0)
304 return;
305
FUJITA Tomonorief265672006-03-26 03:57:14 +0900306 if (numbuf == 1)
307 fmt = SRP_DATA_DESC_DIRECT;
308 else {
309 fmt = SRP_DATA_DESC_INDIRECT;
310 numbuf = min(numbuf, MAX_INDIRECT_BUFS);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (cmd->sc_data_direction == DMA_TO_DEVICE)
FUJITA Tomonorief265672006-03-26 03:57:14 +0900313 srp_cmd->data_out_desc_cnt = numbuf;
314 else
315 srp_cmd->data_in_desc_cnt = numbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
FUJITA Tomonorief265672006-03-26 03:57:14 +0900317
318 if (cmd->sc_data_direction == DMA_TO_DEVICE)
319 srp_cmd->buf_fmt = fmt << 4;
320 else
321 srp_cmd->buf_fmt = fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
FUJITA Tomonorief265672006-03-26 03:57:14 +0900324static void unmap_sg_list(int num_entries,
James Bottomley4dddbc22005-09-06 17:11:54 -0500325 struct device *dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900326 struct srp_direct_buf *md)
327{
James Bottomley4dddbc22005-09-06 17:11:54 -0500328 int i;
329
FUJITA Tomonorief265672006-03-26 03:57:14 +0900330 for (i = 0; i < num_entries; ++i)
331 dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
James Bottomley4dddbc22005-09-06 17:11:54 -0500332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
335 * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
336 * @cmd: srp_cmd whose additional_data member will be unmapped
337 * @dev: device for which the memory is mapped
338 *
339*/
James Bottomley4dddbc22005-09-06 17:11:54 -0500340static void unmap_cmd_data(struct srp_cmd *cmd,
341 struct srp_event_struct *evt_struct,
342 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900344 u8 out_fmt, in_fmt;
345
346 out_fmt = cmd->buf_fmt >> 4;
347 in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
348
349 if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900351 else if (out_fmt == SRP_DATA_DESC_DIRECT ||
352 in_fmt == SRP_DATA_DESC_DIRECT) {
353 struct srp_direct_buf *data =
354 (struct srp_direct_buf *) cmd->add_data;
355 dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 } else {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900357 struct srp_indirect_buf *indirect =
358 (struct srp_indirect_buf *) cmd->add_data;
359 int num_mapped = indirect->table_desc.len /
360 sizeof(struct srp_direct_buf);
James Bottomley4dddbc22005-09-06 17:11:54 -0500361
362 if (num_mapped <= MAX_INDIRECT_BUFS) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900363 unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
James Bottomley4dddbc22005-09-06 17:11:54 -0500364 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
James Bottomley4dddbc22005-09-06 17:11:54 -0500366
367 unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369}
370
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900371static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900372 struct srp_direct_buf *md)
James Bottomley4dddbc22005-09-06 17:11:54 -0500373{
374 int i;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900375 struct scatterlist *sg;
James Bottomley4dddbc22005-09-06 17:11:54 -0500376 u64 total_length = 0;
377
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900378 scsi_for_each_sg(cmd, sg, nseg, i) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900379 struct srp_direct_buf *descr = md + i;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900380 descr->va = sg_dma_address(sg);
381 descr->len = sg_dma_len(sg);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900382 descr->key = 0;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900383 total_length += sg_dma_len(sg);
James Bottomley4dddbc22005-09-06 17:11:54 -0500384 }
385 return total_length;
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/**
389 * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
390 * @cmd: Scsi_Cmnd with the scatterlist
391 * @srp_cmd: srp_cmd that contains the memory descriptor
392 * @dev: device for which to map dma memory
393 *
394 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
395 * Returns 1 on success.
396*/
397static int map_sg_data(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500398 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct srp_cmd *srp_cmd, struct device *dev)
400{
401
James Bottomley4dddbc22005-09-06 17:11:54 -0500402 int sg_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 u64 total_length = 0;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900404 struct srp_direct_buf *data =
405 (struct srp_direct_buf *) srp_cmd->add_data;
406 struct srp_indirect_buf *indirect =
407 (struct srp_indirect_buf *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900409 sg_mapped = scsi_dma_map(cmd);
410 if (!sg_mapped)
411 return 1;
412 else if (sg_mapped < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414
415 set_srp_direction(cmd, srp_cmd, sg_mapped);
416
417 /* special case; we can use a single direct descriptor */
418 if (sg_mapped == 1) {
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900419 map_sg_list(cmd, sg_mapped, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 1;
421 }
422
FUJITA Tomonorief265672006-03-26 03:57:14 +0900423 indirect->table_desc.va = 0;
424 indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
425 indirect->table_desc.key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
James Bottomley4dddbc22005-09-06 17:11:54 -0500427 if (sg_mapped <= MAX_INDIRECT_BUFS) {
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900428 total_length = map_sg_list(cmd, sg_mapped,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900429 &indirect->desc_list[0]);
430 indirect->len = total_length;
James Bottomley4dddbc22005-09-06 17:11:54 -0500431 return 1;
432 }
433
434 /* get indirect table */
435 if (!evt_struct->ext_list) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900436 evt_struct->ext_list = (struct srp_direct_buf *)
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900437 dma_alloc_coherent(dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900438 SG_ALL * sizeof(struct srp_direct_buf),
439 &evt_struct->ext_list_token, 0);
James Bottomley4dddbc22005-09-06 17:11:54 -0500440 if (!evt_struct->ext_list) {
Robert Jennings7912a0a2008-07-24 04:35:27 +1000441 if (!firmware_has_feature(FW_FEATURE_CMO))
442 sdev_printk(KERN_ERR, cmd->device,
443 "Can't allocate memory "
444 "for indirect table\n");
Robert Jenningse637d552009-01-22 13:40:09 -0600445 scsi_dma_unmap(cmd);
James Bottomley4dddbc22005-09-06 17:11:54 -0500446 return 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500447 }
448 }
449
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900450 total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
James Bottomley4dddbc22005-09-06 17:11:54 -0500451
FUJITA Tomonorief265672006-03-26 03:57:14 +0900452 indirect->len = total_length;
453 indirect->table_desc.va = evt_struct->ext_list_token;
454 indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
455 memcpy(indirect->desc_list, evt_struct->ext_list,
456 MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
James Bottomley4dddbc22005-09-06 17:11:54 -0500457 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
462 * @cmd: struct scsi_cmnd with the memory to be mapped
463 * @srp_cmd: srp_cmd that contains the memory descriptor
464 * @dev: dma device for which to map dma memory
465 *
466 * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
467 * Returns 1 on success.
468*/
469static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500470 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct srp_cmd *srp_cmd, struct device *dev)
472{
473 switch (cmd->sc_data_direction) {
474 case DMA_FROM_DEVICE:
475 case DMA_TO_DEVICE:
476 break;
477 case DMA_NONE:
478 return 1;
479 case DMA_BIDIRECTIONAL:
Brian King6c0a60e2007-06-13 17:12:19 -0500480 sdev_printk(KERN_ERR, cmd->device,
481 "Can't map DMA_BIDIRECTIONAL to read/write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483 default:
Brian King6c0a60e2007-06-13 17:12:19 -0500484 sdev_printk(KERN_ERR, cmd->device,
485 "Unknown data direction 0x%02x; can't map!\n",
486 cmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488 }
489
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900490 return map_sg_data(cmd, evt_struct, srp_cmd, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Brian King3d0e91f2007-06-13 17:12:26 -0500493/**
494 * purge_requests: Our virtual adapter just shut down. purge any sent requests
495 * @hostdata: the adapter
496 */
497static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
498{
499 struct srp_event_struct *tmp_evt, *pos;
500 unsigned long flags;
501
502 spin_lock_irqsave(hostdata->host->host_lock, flags);
503 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
504 list_del(&tmp_evt->list);
505 del_timer(&tmp_evt->timer);
506 if (tmp_evt->cmnd) {
507 tmp_evt->cmnd->result = (error_code << 16);
508 unmap_cmd_data(&tmp_evt->iu.srp.cmd,
509 tmp_evt,
510 tmp_evt->hostdata->dev);
511 if (tmp_evt->cmnd_done)
512 tmp_evt->cmnd_done(tmp_evt->cmnd);
513 } else if (tmp_evt->done)
514 tmp_evt->done(tmp_evt);
515 free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
516 }
517 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
518}
519
520/**
521 * ibmvscsi_reset_host - Reset the connection to the server
522 * @hostdata: struct ibmvscsi_host_data to reset
523*/
524static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
525{
526 scsi_block_requests(hostdata->host);
527 atomic_set(&hostdata->request_limit, 0);
528
529 purge_requests(hostdata, DID_ERROR);
David Woodhoused3849d52007-09-22 08:29:36 +1000530 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
531 (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
Brian King3d0e91f2007-06-13 17:12:26 -0500532 (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
533 atomic_set(&hostdata->request_limit, -1);
534 dev_err(hostdata->dev, "error after reset\n");
535 }
536
537 scsi_unblock_requests(hostdata->host);
538}
539
540/**
541 * ibmvscsi_timeout - Internal command timeout handler
542 * @evt_struct: struct srp_event_struct that timed out
543 *
544 * Called when an internally generated command times out
545*/
546static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
547{
548 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
549
550 dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
551 evt_struct->iu.srp.cmd.opcode);
552
553 ibmvscsi_reset_host(hostdata);
554}
555
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/* ------------------------------------------------------------
558 * Routines for sending and receiving SRPs
559 */
560/**
561 * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
562 * @evt_struct: evt_struct to be sent
563 * @hostdata: ibmvscsi_host_data of host
Brian King3d0e91f2007-06-13 17:12:26 -0500564 * @timeout: timeout in seconds - 0 means do not time command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *
566 * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
567 * Note that this routine assumes that host_lock is held for synchronization
568*/
569static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
Brian King3d0e91f2007-06-13 17:12:26 -0500570 struct ibmvscsi_host_data *hostdata,
571 unsigned long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
Robert Jennings3c887e82007-10-30 11:37:07 -0500574 int request_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int rc;
576
Robert Jenningsa897ff22007-03-28 12:45:46 -0500577 /* If we have exhausted our request limit, just fail this request,
578 * unless it is for a reset or abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * Note that there are rare cases involving driver generated requests
580 * (such as task management requests) that the mid layer may think we
581 * can handle more requests (can_queue) when we actually can't
582 */
Dave C Boutchercefbda22006-06-12 21:22:51 -0500583 if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
584 request_status =
585 atomic_dec_if_positive(&hostdata->request_limit);
586 /* If request limit was -1 when we started, it is now even
587 * less than that
588 */
589 if (request_status < -1)
590 goto send_error;
Robert Jenningsa897ff22007-03-28 12:45:46 -0500591 /* Otherwise, we may have run out of requests. */
Robert Jennings3c887e82007-10-30 11:37:07 -0500592 /* If request limit was 0 when we started the adapter is in the
593 * process of performing a login with the server adapter, or
594 * we may have run out of requests.
595 */
596 else if (request_status == -1 &&
597 evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
598 goto send_busy;
Robert Jenningsa897ff22007-03-28 12:45:46 -0500599 /* Abort and reset calls should make it through.
600 * Nothing except abort and reset should use the last two
601 * slots unless we had two or less to begin with.
602 */
603 else if (request_status < 2 &&
604 evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
605 /* In the case that we have less than two requests
606 * available, check the server limit as a combination
607 * of the request limit and the number of requests
608 * in-flight (the size of the send list). If the
609 * server limit is greater than 2, return busy so
610 * that the last two are reserved for reset and abort.
611 */
612 int server_limit = request_status;
613 struct srp_event_struct *tmp_evt;
614
615 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
616 server_limit++;
617 }
618
619 if (server_limit > 2)
620 goto send_busy;
621 }
Dave C Boutchercefbda22006-06-12 21:22:51 -0500622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Copy the IU into the transfer area */
625 *evt_struct->xfer_iu = evt_struct->iu;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900626 evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Add this to the sent list. We need to do this
629 * before we actually send
630 * in case it comes back REALLY fast
631 */
632 list_add_tail(&evt_struct->list, &hostdata->sent);
633
Brian King3d0e91f2007-06-13 17:12:26 -0500634 init_timer(&evt_struct->timer);
635 if (timeout) {
636 evt_struct->timer.data = (unsigned long) evt_struct;
637 evt_struct->timer.expires = jiffies + (timeout * HZ);
638 evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
639 add_timer(&evt_struct->timer);
640 }
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if ((rc =
David Woodhoused3849d52007-09-22 08:29:36 +1000643 ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 list_del(&evt_struct->list);
Brian King3d0e91f2007-06-13 17:12:26 -0500645 del_timer(&evt_struct->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Robert Jennings860784c2007-11-12 09:00:23 -0600647 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
648 * Firmware will send a CRQ with a transport event (0xFF) to
649 * tell this client what has happened to the transport. This
650 * will be handled in ibmvscsi_handle_crq()
651 */
652 if (rc == H_CLOSED) {
653 dev_warn(hostdata->dev, "send warning. "
654 "Receive queue closed, will retry.\n");
655 goto send_busy;
656 }
Brian King6c0a60e2007-06-13 17:12:19 -0500657 dev_err(hostdata->dev, "send error %d\n", rc);
Robert Jenningsa897ff22007-03-28 12:45:46 -0500658 atomic_inc(&hostdata->request_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto send_error;
660 }
661
662 return 0;
663
Dave C Boutchercefbda22006-06-12 21:22:51 -0500664 send_busy:
James Bottomley4dddbc22005-09-06 17:11:54 -0500665 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 free_event_struct(&hostdata->pool, evt_struct);
Robert Jennings3c887e82007-10-30 11:37:07 -0500668 if (request_status != -1)
669 atomic_inc(&hostdata->request_limit);
Robert Jenningsa897ff22007-03-28 12:45:46 -0500670 return SCSI_MLQUEUE_HOST_BUSY;
Dave C Boutchercefbda22006-06-12 21:22:51 -0500671
672 send_error:
673 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
674
675 if (evt_struct->cmnd != NULL) {
676 evt_struct->cmnd->result = DID_ERROR << 16;
677 evt_struct->cmnd_done(evt_struct->cmnd);
678 } else if (evt_struct->done)
679 evt_struct->done(evt_struct);
680
681 free_event_struct(&hostdata->pool, evt_struct);
682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685/**
686 * handle_cmd_rsp: - Handle responses from commands
687 * @evt_struct: srp_event_struct to be handled
688 *
689 * Used as a callback by when sending scsi cmds.
690 * Gets called by ibmvscsi_handle_crq()
691*/
692static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
693{
694 struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
695 struct scsi_cmnd *cmnd = evt_struct->cmnd;
696
FUJITA Tomonorief265672006-03-26 03:57:14 +0900697 if (unlikely(rsp->opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -0500699 dev_warn(evt_struct->hostdata->dev,
700 "bad SRP RSP type %d\n", rsp->opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 if (cmnd) {
Brian Kingc3a3b552008-04-25 16:58:29 -0500704 cmnd->result |= rsp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
706 memcpy(cmnd->sense_buffer,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900707 rsp->data,
708 rsp->sense_data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 unmap_cmd_data(&evt_struct->iu.srp.cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500710 evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 evt_struct->hostdata->dev);
712
FUJITA Tomonorief265672006-03-26 03:57:14 +0900713 if (rsp->flags & SRP_RSP_FLAG_DOOVER)
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900714 scsi_set_resid(cmnd, rsp->data_out_res_cnt);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900715 else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900716 scsi_set_resid(cmnd, rsp->data_in_res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 if (evt_struct->cmnd_done)
720 evt_struct->cmnd_done(cmnd);
721}
722
723/**
724 * lun_from_dev: - Returns the lun of the scsi device
725 * @dev: struct scsi_device
726 *
727*/
728static inline u16 lun_from_dev(struct scsi_device *dev)
729{
730 return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
731}
732
733/**
734 * ibmvscsi_queue: - The queuecommand function of the scsi template
735 * @cmd: struct scsi_cmnd to be executed
736 * @done: Callback function to be called when cmd is completed
737*/
738static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
739 void (*done) (struct scsi_cmnd *))
740{
741 struct srp_cmd *srp_cmd;
742 struct srp_event_struct *evt_struct;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900743 struct srp_indirect_buf *indirect;
FUJITA Tomonori7603e022007-07-23 09:28:40 +0900744 struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 u16 lun = lun_from_dev(cmnd->device);
FUJITA Tomonorief265672006-03-26 03:57:14 +0900746 u8 out_fmt, in_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Brian Kingc3a3b552008-04-25 16:58:29 -0500748 cmnd->result = (DID_OK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 evt_struct = get_event_struct(&hostdata->pool);
750 if (!evt_struct)
751 return SCSI_MLQUEUE_HOST_BUSY;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* Set up the actual SRP IU */
754 srp_cmd = &evt_struct->iu.srp.cmd;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900755 memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
756 srp_cmd->opcode = SRP_CMD;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300757 memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 srp_cmd->lun = ((u64) lun) << 48;
759
James Bottomley4dddbc22005-09-06 17:11:54 -0500760 if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
Robert Jennings7912a0a2008-07-24 04:35:27 +1000761 if (!firmware_has_feature(FW_FEATURE_CMO))
762 sdev_printk(KERN_ERR, cmnd->device,
763 "couldn't convert cmd to srp_cmd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 free_event_struct(&hostdata->pool, evt_struct);
765 return SCSI_MLQUEUE_HOST_BUSY;
766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 init_event_struct(evt_struct,
769 handle_cmd_rsp,
770 VIOSRP_SRP_FORMAT,
Jens Axboe242f9dc2008-09-14 05:55:09 -0700771 cmnd->request->timeout/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 evt_struct->cmnd = cmnd;
774 evt_struct->cmnd_done = done;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Fix up dma address of the buffer itself */
FUJITA Tomonorief265672006-03-26 03:57:14 +0900777 indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
778 out_fmt = srp_cmd->buf_fmt >> 4;
779 in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
780 if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
781 out_fmt == SRP_DATA_DESC_INDIRECT) &&
782 indirect->table_desc.va == 0) {
783 indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
784 offsetof(struct srp_cmd, add_data) +
785 offsetof(struct srp_indirect_buf, desc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Brian King3d0e91f2007-06-13 17:12:26 -0500788 return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791/* ------------------------------------------------------------
792 * Routines for driver initialization
793 */
Brian King3507e132009-06-08 16:19:04 -0500794
795/**
Brian King126c5cc2009-06-08 16:19:08 -0500796 * map_persist_bufs: - Pre-map persistent data for adapter logins
797 * @hostdata: ibmvscsi_host_data of host
798 *
799 * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
800 * Return 1 on error, 0 on success.
801 */
802static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
803{
804
805 hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
806 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
807
808 if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
809 dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
810 return 1;
811 }
812
813 hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
814 &hostdata->madapter_info,
815 sizeof(hostdata->madapter_info),
816 DMA_BIDIRECTIONAL);
817 if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
818 dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
819 dma_unmap_single(hostdata->dev, hostdata->caps_addr,
820 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
821 return 1;
822 }
823
824 return 0;
825}
826
827/**
828 * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
829 * @hostdata: ibmvscsi_host_data of host
830 *
831 * Unmap the capabilities and adapter info DMA buffers
832 */
833static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
834{
835 dma_unmap_single(hostdata->dev, hostdata->caps_addr,
836 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
837
838 dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
839 sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
840}
841
842/**
Brian King3507e132009-06-08 16:19:04 -0500843 * login_rsp: - Handle response to SRP login request
844 * @evt_struct: srp_event_struct with the response
845 *
846 * Used as a "done" callback by when sending srp_login. Gets called
847 * by ibmvscsi_handle_crq()
848*/
849static void login_rsp(struct srp_event_struct *evt_struct)
850{
851 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
852 switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
853 case SRP_LOGIN_RSP: /* it worked! */
854 break;
855 case SRP_LOGIN_REJ: /* refused! */
856 dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
857 evt_struct->xfer_iu->srp.login_rej.reason);
858 /* Login failed. */
859 atomic_set(&hostdata->request_limit, -1);
860 return;
861 default:
862 dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
863 evt_struct->xfer_iu->srp.login_rsp.opcode);
864 /* Login failed. */
865 atomic_set(&hostdata->request_limit, -1);
866 return;
867 }
868
869 dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
Brian King126c5cc2009-06-08 16:19:08 -0500870 hostdata->client_migrated = 0;
Brian King3507e132009-06-08 16:19:04 -0500871
872 /* Now we know what the real request-limit is.
873 * This value is set rather than added to request_limit because
874 * request_limit could have been set to -1 by this client.
875 */
876 atomic_set(&hostdata->request_limit,
877 evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
878
879 /* If we had any pending I/Os, kick them */
880 scsi_unblock_requests(hostdata->host);
881}
882
883/**
884 * send_srp_login: - Sends the srp login
885 * @hostdata: ibmvscsi_host_data of host
886 *
887 * Returns zero if successful.
888*/
889static int send_srp_login(struct ibmvscsi_host_data *hostdata)
890{
891 int rc;
892 unsigned long flags;
893 struct srp_login_req *login;
894 struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
895
896 BUG_ON(!evt_struct);
897 init_event_struct(evt_struct, login_rsp,
898 VIOSRP_SRP_FORMAT, login_timeout);
899
900 login = &evt_struct->iu.srp.login_req;
901 memset(login, 0, sizeof(*login));
902 login->opcode = SRP_LOGIN_REQ;
903 login->req_it_iu_len = sizeof(union srp_iu);
904 login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
905
906 spin_lock_irqsave(hostdata->host->host_lock, flags);
907 /* Start out with a request limit of 0, since this is negotiated in
908 * the login request we are just sending and login requests always
909 * get sent by the driver regardless of request_limit.
910 */
911 atomic_set(&hostdata->request_limit, 0);
912
913 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
914 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
915 dev_info(hostdata->dev, "sent SRP login\n");
916 return rc;
917};
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/**
Brian King126c5cc2009-06-08 16:19:08 -0500920 * capabilities_rsp: - Handle response to MAD adapter capabilities request
921 * @evt_struct: srp_event_struct with the response
922 *
923 * Used as a "done" callback by when sending adapter_info.
924 */
925static void capabilities_rsp(struct srp_event_struct *evt_struct)
926{
927 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
928
929 if (evt_struct->xfer_iu->mad.capabilities.common.status) {
930 dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
931 evt_struct->xfer_iu->mad.capabilities.common.status);
932 } else {
933 if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
934 dev_info(hostdata->dev, "Partition migration not supported\n");
935
936 if (client_reserve) {
937 if (hostdata->caps.reserve.common.server_support ==
938 SERVER_SUPPORTS_CAP)
939 dev_info(hostdata->dev, "Client reserve enabled\n");
940 else
941 dev_info(hostdata->dev, "Client reserve not supported\n");
942 }
943 }
944
945 send_srp_login(hostdata);
946}
947
948/**
949 * send_mad_capabilities: - Sends the mad capabilities request
950 * and stores the result so it can be retrieved with
951 * @hostdata: ibmvscsi_host_data of host
952 */
953static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
954{
955 struct viosrp_capabilities *req;
956 struct srp_event_struct *evt_struct;
957 unsigned long flags;
958 struct device_node *of_node = hostdata->dev->archdata.of_node;
959 const char *location;
960
961 evt_struct = get_event_struct(&hostdata->pool);
962 BUG_ON(!evt_struct);
963
964 init_event_struct(evt_struct, capabilities_rsp,
965 VIOSRP_MAD_FORMAT, info_timeout);
966
967 req = &evt_struct->iu.mad.capabilities;
968 memset(req, 0, sizeof(*req));
969
970 hostdata->caps.flags = CAP_LIST_SUPPORTED;
971 if (hostdata->client_migrated)
972 hostdata->caps.flags |= CLIENT_MIGRATED;
973
974 strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
975 sizeof(hostdata->caps.name));
976 hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
977
978 location = of_get_property(of_node, "ibm,loc-code", NULL);
979 location = location ? location : dev_name(hostdata->dev);
980 strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
981 hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
982
983 req->common.type = VIOSRP_CAPABILITIES_TYPE;
984 req->buffer = hostdata->caps_addr;
985
986 hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
987 hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
988 hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
989 hostdata->caps.migration.ecl = 1;
990
991 if (client_reserve) {
992 hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
993 hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
994 hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
995 hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
996 req->common.length = sizeof(hostdata->caps);
997 } else
998 req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);
999
1000 spin_lock_irqsave(hostdata->host->host_lock, flags);
1001 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1002 dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
1003 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1004};
1005
1006/**
Robert Jenningsc1988e32009-06-08 16:19:07 -05001007 * fast_fail_rsp: - Handle response to MAD enable fast fail
1008 * @evt_struct: srp_event_struct with the response
1009 *
1010 * Used as a "done" callback by when sending enable fast fail. Gets called
1011 * by ibmvscsi_handle_crq()
1012 */
1013static void fast_fail_rsp(struct srp_event_struct *evt_struct)
1014{
1015 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1016 u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
1017
1018 if (status == VIOSRP_MAD_NOT_SUPPORTED)
1019 dev_err(hostdata->dev, "fast_fail not supported in server\n");
1020 else if (status == VIOSRP_MAD_FAILED)
1021 dev_err(hostdata->dev, "fast_fail request failed\n");
1022 else if (status != VIOSRP_MAD_SUCCESS)
1023 dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
1024
Brian King126c5cc2009-06-08 16:19:08 -05001025 send_mad_capabilities(hostdata);
Robert Jenningsc1988e32009-06-08 16:19:07 -05001026}
1027
1028/**
1029 * init_host - Start host initialization
1030 * @hostdata: ibmvscsi_host_data of host
1031 *
1032 * Returns zero if successful.
1033 */
1034static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
1035{
1036 int rc;
1037 unsigned long flags;
1038 struct viosrp_fast_fail *fast_fail_mad;
1039 struct srp_event_struct *evt_struct;
1040
Brian King126c5cc2009-06-08 16:19:08 -05001041 if (!fast_fail) {
1042 send_mad_capabilities(hostdata);
1043 return 0;
1044 }
Robert Jenningsc1988e32009-06-08 16:19:07 -05001045
1046 evt_struct = get_event_struct(&hostdata->pool);
1047 BUG_ON(!evt_struct);
1048
1049 init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
1050
1051 fast_fail_mad = &evt_struct->iu.mad.fast_fail;
1052 memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
1053 fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
1054 fast_fail_mad->common.length = sizeof(*fast_fail_mad);
1055
1056 spin_lock_irqsave(hostdata->host->host_lock, flags);
1057 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1058 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1059 return rc;
1060}
1061
1062/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * adapter_info_rsp: - Handle response to MAD adapter info request
1064 * @evt_struct: srp_event_struct with the response
1065 *
1066 * Used as a "done" callback by when sending adapter_info. Gets called
1067 * by ibmvscsi_handle_crq()
1068*/
1069static void adapter_info_rsp(struct srp_event_struct *evt_struct)
1070{
1071 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
Brian King6c0a60e2007-06-13 17:12:19 -05001074 dev_err(hostdata->dev, "error %d getting adapter info\n",
1075 evt_struct->xfer_iu->mad.adapter_info.common.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001077 dev_info(hostdata->dev, "host srp version: %s, "
1078 "host partition %s (%d), OS %d, max io %u\n",
1079 hostdata->madapter_info.srp_version,
1080 hostdata->madapter_info.partition_name,
1081 hostdata->madapter_info.partition_number,
1082 hostdata->madapter_info.os_type,
1083 hostdata->madapter_info.port_max_txu[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (hostdata->madapter_info.port_max_txu[0])
1086 hostdata->host->max_sectors =
1087 hostdata->madapter_info.port_max_txu[0] >> 9;
Dave C Boutcher154fb612005-09-13 10:09:02 -05001088
1089 if (hostdata->madapter_info.os_type == 3 &&
1090 strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001091 dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
1092 hostdata->madapter_info.srp_version);
1093 dev_err(hostdata->dev, "limiting scatterlists to %d\n",
1094 MAX_INDIRECT_BUFS);
Dave C Boutcher154fb612005-09-13 10:09:02 -05001095 hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
1096 }
Brian Kinge08afeb2009-06-23 17:14:01 -05001097
1098 if (hostdata->madapter_info.os_type == 3) {
1099 enable_fast_fail(hostdata);
1100 return;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Brian King3507e132009-06-08 16:19:04 -05001103
Brian Kinge08afeb2009-06-23 17:14:01 -05001104 send_srp_login(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107/**
1108 * send_mad_adapter_info: - Sends the mad adapter info request
1109 * and stores the result so it can be retrieved with
1110 * sysfs. We COULD consider causing a failure if the
1111 * returned SRP version doesn't match ours.
1112 * @hostdata: ibmvscsi_host_data of host
1113 *
1114 * Returns zero if successful.
1115*/
1116static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
1117{
1118 struct viosrp_adapter_info *req;
1119 struct srp_event_struct *evt_struct;
Brian King06f923c2007-06-13 17:12:33 -05001120 unsigned long flags;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 evt_struct = get_event_struct(&hostdata->pool);
Brian King3507e132009-06-08 16:19:04 -05001123 BUG_ON(!evt_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 init_event_struct(evt_struct,
1126 adapter_info_rsp,
1127 VIOSRP_MAD_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001128 info_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 req = &evt_struct->iu.mad.adapter_info;
1131 memset(req, 0x00, sizeof(*req));
1132
1133 req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
1134 req->common.length = sizeof(hostdata->madapter_info);
Brian King126c5cc2009-06-08 16:19:08 -05001135 req->buffer = hostdata->adapter_info_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Brian King06f923c2007-06-13 17:12:33 -05001137 spin_lock_irqsave(hostdata->host->host_lock, flags);
Brian King126c5cc2009-06-08 16:19:08 -05001138 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
Brian King6c0a60e2007-06-13 17:12:19 -05001139 dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
Brian King06f923c2007-06-13 17:12:33 -05001140 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141};
1142
1143/**
Brian King3507e132009-06-08 16:19:04 -05001144 * init_adapter: Start virtual adapter initialization sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 *
Brian King3507e132009-06-08 16:19:04 -05001146 */
1147static void init_adapter(struct ibmvscsi_host_data *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 send_mad_adapter_info(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 * sync_completion: Signal that a synchronous command has completed
1154 * Note that after returning from this call, the evt_struct is freed.
1155 * the caller waiting on this completion shouldn't touch the evt_struct
1156 * again.
1157 */
1158static void sync_completion(struct srp_event_struct *evt_struct)
1159{
1160 /* copy the response back */
1161 if (evt_struct->sync_srp)
1162 *evt_struct->sync_srp = *evt_struct->xfer_iu;
1163
1164 complete(&evt_struct->comp);
1165}
1166
1167/**
1168 * ibmvscsi_abort: Abort a command...from scsi host template
1169 * send this over to the server and wait synchronously for the response
1170 */
1171static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
1172{
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001173 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct srp_tsk_mgmt *tsk_mgmt;
1175 struct srp_event_struct *evt;
1176 struct srp_event_struct *tmp_evt, *found_evt;
1177 union viosrp_iu srp_rsp;
1178 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001179 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 u16 lun = lun_from_dev(cmd->device);
Robert Jennings860784c2007-11-12 09:00:23 -06001181 unsigned long wait_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* First, find this command in our sent list so we can figure
1184 * out the correct tag
1185 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001186 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001187 wait_switch = jiffies + (init_timeout * HZ);
1188 do {
1189 found_evt = NULL;
1190 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1191 if (tmp_evt->cmnd == cmd) {
1192 found_evt = tmp_evt;
1193 break;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Robert Jennings860784c2007-11-12 09:00:23 -06001197 if (!found_evt) {
1198 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1199 return SUCCESS;
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Robert Jennings860784c2007-11-12 09:00:23 -06001202 evt = get_event_struct(&hostdata->pool);
1203 if (evt == NULL) {
1204 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1205 sdev_printk(KERN_ERR, cmd->device,
1206 "failed to allocate abort event\n");
1207 return FAILED;
1208 }
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 abort_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 an abort SRP command */
1218 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1219 tsk_mgmt->opcode = SRP_TSK_MGMT;
1220 tsk_mgmt->lun = ((u64) lun) << 48;
1221 tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
1222 tsk_mgmt->task_tag = (u64) found_evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Robert Jennings860784c2007-11-12 09:00:23 -06001224 evt->sync_srp = &srp_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Robert Jennings860784c2007-11-12 09:00:23 -06001226 init_completion(&evt->comp);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001227 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
Robert Jennings860784c2007-11-12 09:00:23 -06001228
1229 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1230 break;
1231
1232 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1233 msleep(10);
1234 spin_lock_irqsave(hostdata->host->host_lock, flags);
1235 } while (time_before(jiffies, wait_switch));
1236
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001237 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001238
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001239 if (rsp_rc != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001240 sdev_printk(KERN_ERR, cmd->device,
1241 "failed to send abort() event. rc=%d\n", rsp_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return FAILED;
1243 }
1244
Robert Jennings860784c2007-11-12 09:00:23 -06001245 sdev_printk(KERN_INFO, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001246 "aborting command. lun 0x%llx, tag 0x%llx\n",
Robert Jennings860784c2007-11-12 09:00:23 -06001247 (((u64) lun) << 48), (u64) found_evt);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001252 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001254 sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
1255 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return FAILED;
1257 }
1258
FUJITA Tomonorief265672006-03-26 03:57:14 +09001259 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1260 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 else
1262 rsp_rc = srp_rsp.srp.rsp.status;
1263
1264 if (rsp_rc) {
1265 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001266 sdev_printk(KERN_WARNING, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001267 "abort code %d for task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001268 rsp_rc, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return FAILED;
1270 }
1271
1272 /* Because we dropped the spinlock above, it's possible
1273 * The event is no longer in our list. Make sure it didn't
1274 * complete while we were aborting
1275 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001276 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 found_evt = NULL;
1278 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1279 if (tmp_evt->cmnd == cmd) {
1280 found_evt = tmp_evt;
1281 break;
1282 }
1283 }
1284
1285 if (found_evt == NULL) {
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001286 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Ingo Molnarfe333322009-01-06 14:26:03 +00001287 sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001288 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return SUCCESS;
1290 }
1291
Ingo Molnarfe333322009-01-06 14:26:03 +00001292 sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001293 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 cmd->result = (DID_ABORT << 16);
1296 list_del(&found_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001297 unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1298 found_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 free_event_struct(&found_evt->hostdata->pool, found_evt);
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001300 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 atomic_inc(&hostdata->request_limit);
1302 return SUCCESS;
1303}
1304
1305/**
1306 * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
1307 * template send this over to the server and wait synchronously for the
1308 * response
1309 */
1310static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1311{
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001312 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 struct srp_tsk_mgmt *tsk_mgmt;
1314 struct srp_event_struct *evt;
1315 struct srp_event_struct *tmp_evt, *pos;
1316 union viosrp_iu srp_rsp;
1317 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001318 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 u16 lun = lun_from_dev(cmd->device);
Robert Jennings860784c2007-11-12 09:00:23 -06001320 unsigned long wait_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001322 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001323 wait_switch = jiffies + (init_timeout * HZ);
1324 do {
1325 evt = get_event_struct(&hostdata->pool);
1326 if (evt == NULL) {
1327 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1328 sdev_printk(KERN_ERR, cmd->device,
1329 "failed to allocate reset event\n");
1330 return FAILED;
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Robert Jennings860784c2007-11-12 09:00:23 -06001333 init_event_struct(evt,
1334 sync_completion,
1335 VIOSRP_SRP_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001336 reset_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Robert Jennings860784c2007-11-12 09:00:23 -06001338 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Robert Jennings860784c2007-11-12 09:00:23 -06001340 /* Set up a lun reset SRP command */
1341 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1342 tsk_mgmt->opcode = SRP_TSK_MGMT;
1343 tsk_mgmt->lun = ((u64) lun) << 48;
1344 tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Robert Jennings860784c2007-11-12 09:00:23 -06001346 evt->sync_srp = &srp_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Robert Jennings860784c2007-11-12 09:00:23 -06001348 init_completion(&evt->comp);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001349 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
Robert Jennings860784c2007-11-12 09:00:23 -06001350
1351 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1352 break;
1353
1354 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1355 msleep(10);
1356 spin_lock_irqsave(hostdata->host->host_lock, flags);
1357 } while (time_before(jiffies, wait_switch));
1358
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001359 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001360
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001361 if (rsp_rc != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001362 sdev_printk(KERN_ERR, cmd->device,
1363 "failed to send reset event. rc=%d\n", rsp_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return FAILED;
1365 }
1366
Ingo Molnarfe333322009-01-06 14:26:03 +00001367 sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
Robert Jennings860784c2007-11-12 09:00:23 -06001368 (((u64) lun) << 48));
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001373 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001375 sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
1376 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return FAILED;
1378 }
1379
FUJITA Tomonorief265672006-03-26 03:57:14 +09001380 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1381 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 else
1383 rsp_rc = srp_rsp.srp.rsp.status;
1384
1385 if (rsp_rc) {
1386 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001387 sdev_printk(KERN_WARNING, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001388 "reset code %d for task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001389 rsp_rc, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return FAILED;
1391 }
1392
1393 /* We need to find all commands for this LUN that have not yet been
1394 * responded to, and fail them with DID_RESET
1395 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001396 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1398 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1399 if (tmp_evt->cmnd)
1400 tmp_evt->cmnd->result = (DID_RESET << 16);
1401 list_del(&tmp_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001402 unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1403 tmp_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 free_event_struct(&tmp_evt->hostdata->pool,
1405 tmp_evt);
1406 atomic_inc(&hostdata->request_limit);
1407 if (tmp_evt->cmnd_done)
1408 tmp_evt->cmnd_done(tmp_evt->cmnd);
1409 else if (tmp_evt->done)
1410 tmp_evt->done(tmp_evt);
1411 }
1412 }
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001413 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return SUCCESS;
1415}
1416
1417/**
Brian King3d0e91f2007-06-13 17:12:26 -05001418 * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
1419 * @cmd: struct scsi_cmnd having problems
1420*/
1421static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Brian King3d0e91f2007-06-13 17:12:26 -05001423 unsigned long wait_switch = 0;
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001424 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Brian King3d0e91f2007-06-13 17:12:26 -05001426 dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
1427
1428 ibmvscsi_reset_host(hostdata);
1429
1430 for (wait_switch = jiffies + (init_timeout * HZ);
1431 time_before(jiffies, wait_switch) &&
1432 atomic_read(&hostdata->request_limit) < 2;) {
1433
1434 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Brian King3d0e91f2007-06-13 17:12:26 -05001436
1437 if (atomic_read(&hostdata->request_limit) <= 0)
1438 return FAILED;
1439
1440 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443/**
1444 * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1445 * @crq: Command/Response queue
1446 * @hostdata: ibmvscsi_host_data of host
1447 *
1448*/
1449void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1450 struct ibmvscsi_host_data *hostdata)
1451{
Brian King6c0a60e2007-06-13 17:12:19 -05001452 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 unsigned long flags;
1454 struct srp_event_struct *evt_struct =
1455 (struct srp_event_struct *)crq->IU_data_ptr;
1456 switch (crq->valid) {
1457 case 0xC0: /* initialization */
1458 switch (crq->format) {
1459 case 0x01: /* Initialization message */
Brian King6c0a60e2007-06-13 17:12:19 -05001460 dev_info(hostdata->dev, "partner initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* Send back a response */
David Woodhoused3849d52007-09-22 08:29:36 +10001462 if ((rc = ibmvscsi_ops->send_crq(hostdata,
1463 0xC002000000000000LL, 0)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /* Now login */
Brian King3507e132009-06-08 16:19:04 -05001465 init_adapter(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001467 dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
1470 break;
1471 case 0x02: /* Initialization response */
Brian King6c0a60e2007-06-13 17:12:19 -05001472 dev_info(hostdata->dev, "partner initialization complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 /* Now login */
Brian King3507e132009-06-08 16:19:04 -05001475 init_adapter(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477 default:
Brian King6c0a60e2007-06-13 17:12:19 -05001478 dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 return;
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001481 case 0xFF: /* Hypervisor telling us the connection is closed */
1482 scsi_block_requests(hostdata->host);
Dave C Boutchercefbda22006-06-12 21:22:51 -05001483 atomic_set(&hostdata->request_limit, 0);
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001484 if (crq->format == 0x06) {
1485 /* We need to re-setup the interpartition connection */
Brian King6c0a60e2007-06-13 17:12:19 -05001486 dev_info(hostdata->dev, "Re-enabling adapter!\n");
Brian King126c5cc2009-06-08 16:19:08 -05001487 hostdata->client_migrated = 1;
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001488 purge_requests(hostdata, DID_REQUEUE);
David Woodhoused3849d52007-09-22 08:29:36 +10001489 if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
1490 hostdata)) ||
1491 (ibmvscsi_ops->send_crq(hostdata,
1492 0xC001000000000000LL, 0))) {
Dave C Boutchercefbda22006-06-12 21:22:51 -05001493 atomic_set(&hostdata->request_limit,
1494 -1);
Brian King6c0a60e2007-06-13 17:12:19 -05001495 dev_err(hostdata->dev, "error after enable\n");
Dave C Boutchercefbda22006-06-12 21:22:51 -05001496 }
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001497 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001498 dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
1499 crq->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001501 purge_requests(hostdata, DID_ERROR);
David Woodhoused3849d52007-09-22 08:29:36 +10001502 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
1503 hostdata)) ||
1504 (ibmvscsi_ops->send_crq(hostdata,
1505 0xC001000000000000LL, 0))) {
Dave C Boutchercefbda22006-06-12 21:22:51 -05001506 atomic_set(&hostdata->request_limit,
1507 -1);
Brian King6c0a60e2007-06-13 17:12:19 -05001508 dev_err(hostdata->dev, "error after reset\n");
Dave C Boutchercefbda22006-06-12 21:22:51 -05001509 }
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001510 }
1511 scsi_unblock_requests(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return;
1513 case 0x80: /* real payload */
1514 break;
1515 default:
Brian King6c0a60e2007-06-13 17:12:19 -05001516 dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
1517 crq->valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return;
1519 }
1520
1521 /* The only kind of payload CRQs we should get are responses to
1522 * things we send. Make sure this response is to something we
1523 * actually sent
1524 */
1525 if (!valid_event_struct(&hostdata->pool, evt_struct)) {
Brian King6c0a60e2007-06-13 17:12:19 -05001526 dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 (void *)crq->IU_data_ptr);
1528 return;
1529 }
1530
1531 if (atomic_read(&evt_struct->free)) {
Brian King6c0a60e2007-06-13 17:12:19 -05001532 dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
1533 (void *)crq->IU_data_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return;
1535 }
1536
1537 if (crq->format == VIOSRP_SRP_FORMAT)
FUJITA Tomonorief265672006-03-26 03:57:14 +09001538 atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 &hostdata->request_limit);
1540
Brian King3d0e91f2007-06-13 17:12:26 -05001541 del_timer(&evt_struct->timer);
1542
Brian Kingca616682008-05-19 10:27:56 -05001543 if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
Brian Kingc3a3b552008-04-25 16:58:29 -05001544 evt_struct->cmnd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (evt_struct->done)
1546 evt_struct->done(evt_struct);
1547 else
Brian King6c0a60e2007-06-13 17:12:19 -05001548 dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /*
1551 * Lock the host_lock before messing with these structures, since we
1552 * are running in a task context
1553 */
1554 spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1555 list_del(&evt_struct->list);
1556 free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1557 spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1558}
1559
1560/**
1561 * ibmvscsi_get_host_config: Send the command to the server to get host
1562 * configuration data. The data is opaque to us.
1563 */
1564static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1565 unsigned char *buffer, int length)
1566{
1567 struct viosrp_host_config *host_config;
1568 struct srp_event_struct *evt_struct;
Brian King06f923c2007-06-13 17:12:33 -05001569 unsigned long flags;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001570 dma_addr_t addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int rc;
1572
1573 evt_struct = get_event_struct(&hostdata->pool);
1574 if (!evt_struct) {
Brian King6c0a60e2007-06-13 17:12:19 -05001575 dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return -1;
1577 }
1578
1579 init_event_struct(evt_struct,
1580 sync_completion,
1581 VIOSRP_MAD_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001582 info_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 host_config = &evt_struct->iu.mad.host_config;
1585
1586 /* Set up a lun reset SRP command */
1587 memset(host_config, 0x00, sizeof(*host_config));
1588 host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1589 host_config->common.length = length;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001590 host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
1591 length,
1592 DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001594 if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
Robert Jennings7912a0a2008-07-24 04:35:27 +10001595 if (!firmware_has_feature(FW_FEATURE_CMO))
1596 dev_err(hostdata->dev,
1597 "dma_mapping error getting host config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 free_event_struct(&hostdata->pool, evt_struct);
1599 return -1;
1600 }
1601
1602 init_completion(&evt_struct->comp);
Brian King06f923c2007-06-13 17:12:33 -05001603 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001604 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
Brian King06f923c2007-06-13 17:12:33 -05001605 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001606 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 wait_for_completion(&evt_struct->comp);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001608 dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 return rc;
1611}
1612
Robert Jennings0979c842007-03-29 12:30:40 -05001613/**
1614 * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
1615 * @sdev: struct scsi_device device to configure
1616 *
1617 * Enable allow_restart for a device if it is a disk. Adjust the
1618 * queue_depth here also as is required by the documentation for
1619 * struct scsi_host_template.
1620 */
1621static int ibmvscsi_slave_configure(struct scsi_device *sdev)
1622{
1623 struct Scsi_Host *shost = sdev->host;
1624 unsigned long lock_flags = 0;
1625
1626 spin_lock_irqsave(shost->host_lock, lock_flags);
Brian Kingd1a357f2007-10-25 16:06:34 -05001627 if (sdev->type == TYPE_DISK) {
Robert Jennings0979c842007-03-29 12:30:40 -05001628 sdev->allow_restart = 1;
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001629 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
Brian Kingd1a357f2007-10-25 16:06:34 -05001630 }
Robert Jennings0979c842007-03-29 12:30:40 -05001631 scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
1632 spin_unlock_irqrestore(shost->host_lock, lock_flags);
1633 return 0;
1634}
1635
Brian King742d25b2007-05-29 15:46:14 -05001636/**
1637 * ibmvscsi_change_queue_depth - Change the device's queue depth
1638 * @sdev: scsi device struct
1639 * @qdepth: depth to set
Mike Christiee881a172009-10-15 17:46:39 -07001640 * @reason: calling context
Brian King742d25b2007-05-29 15:46:14 -05001641 *
1642 * Return value:
1643 * actual depth set
1644 **/
Mike Christiee881a172009-10-15 17:46:39 -07001645static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
1646 int reason)
Brian King742d25b2007-05-29 15:46:14 -05001647{
Mike Christiee881a172009-10-15 17:46:39 -07001648 if (reason != SCSI_QDEPTH_DEFAULT)
1649 return -EOPNOTSUPP;
1650
Brian King742d25b2007-05-29 15:46:14 -05001651 if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
1652 qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
1653
1654 scsi_adjust_queue_depth(sdev, 0, qdepth);
1655 return sdev->queue_depth;
1656}
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658/* ------------------------------------------------------------
1659 * sysfs attributes
1660 */
Brian King126c5cc2009-06-08 16:19:08 -05001661static ssize_t show_host_vhost_loc(struct device *dev,
1662 struct device_attribute *attr, char *buf)
1663{
1664 struct Scsi_Host *shost = class_to_shost(dev);
1665 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1666 int len;
1667
1668 len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
1669 hostdata->caps.loc);
1670 return len;
1671}
1672
1673static struct device_attribute ibmvscsi_host_vhost_loc = {
1674 .attr = {
1675 .name = "vhost_loc",
1676 .mode = S_IRUGO,
1677 },
1678 .show = show_host_vhost_loc,
1679};
1680
1681static ssize_t show_host_vhost_name(struct device *dev,
1682 struct device_attribute *attr, char *buf)
1683{
1684 struct Scsi_Host *shost = class_to_shost(dev);
1685 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1686 int len;
1687
1688 len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
1689 hostdata->caps.name);
1690 return len;
1691}
1692
1693static struct device_attribute ibmvscsi_host_vhost_name = {
1694 .attr = {
1695 .name = "vhost_name",
1696 .mode = S_IRUGO,
1697 },
1698 .show = show_host_vhost_name,
1699};
1700
Tony Jonesee959b02008-02-22 00:13:36 +01001701static ssize_t show_host_srp_version(struct device *dev,
1702 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Tony Jonesee959b02008-02-22 00:13:36 +01001704 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001705 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 int len;
1707
1708 len = snprintf(buf, PAGE_SIZE, "%s\n",
1709 hostdata->madapter_info.srp_version);
1710 return len;
1711}
1712
Tony Jonesee959b02008-02-22 00:13:36 +01001713static struct device_attribute ibmvscsi_host_srp_version = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .attr = {
1715 .name = "srp_version",
1716 .mode = S_IRUGO,
1717 },
1718 .show = show_host_srp_version,
1719};
1720
Tony Jonesee959b02008-02-22 00:13:36 +01001721static ssize_t show_host_partition_name(struct device *dev,
1722 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 char *buf)
1724{
Tony Jonesee959b02008-02-22 00:13:36 +01001725 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001726 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int len;
1728
1729 len = snprintf(buf, PAGE_SIZE, "%s\n",
1730 hostdata->madapter_info.partition_name);
1731 return len;
1732}
1733
Tony Jonesee959b02008-02-22 00:13:36 +01001734static struct device_attribute ibmvscsi_host_partition_name = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 .attr = {
1736 .name = "partition_name",
1737 .mode = S_IRUGO,
1738 },
1739 .show = show_host_partition_name,
1740};
1741
Tony Jonesee959b02008-02-22 00:13:36 +01001742static ssize_t show_host_partition_number(struct device *dev,
1743 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 char *buf)
1745{
Tony Jonesee959b02008-02-22 00:13:36 +01001746 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001747 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int len;
1749
1750 len = snprintf(buf, PAGE_SIZE, "%d\n",
1751 hostdata->madapter_info.partition_number);
1752 return len;
1753}
1754
Tony Jonesee959b02008-02-22 00:13:36 +01001755static struct device_attribute ibmvscsi_host_partition_number = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 .attr = {
1757 .name = "partition_number",
1758 .mode = S_IRUGO,
1759 },
1760 .show = show_host_partition_number,
1761};
1762
Tony Jonesee959b02008-02-22 00:13:36 +01001763static ssize_t show_host_mad_version(struct device *dev,
1764 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Tony Jonesee959b02008-02-22 00:13:36 +01001766 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001767 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int len;
1769
1770 len = snprintf(buf, PAGE_SIZE, "%d\n",
1771 hostdata->madapter_info.mad_version);
1772 return len;
1773}
1774
Tony Jonesee959b02008-02-22 00:13:36 +01001775static struct device_attribute ibmvscsi_host_mad_version = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 .attr = {
1777 .name = "mad_version",
1778 .mode = S_IRUGO,
1779 },
1780 .show = show_host_mad_version,
1781};
1782
Tony Jonesee959b02008-02-22 00:13:36 +01001783static ssize_t show_host_os_type(struct device *dev,
1784 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Tony Jonesee959b02008-02-22 00:13:36 +01001786 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001787 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int len;
1789
1790 len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
1791 return len;
1792}
1793
Tony Jonesee959b02008-02-22 00:13:36 +01001794static struct device_attribute ibmvscsi_host_os_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 .attr = {
1796 .name = "os_type",
1797 .mode = S_IRUGO,
1798 },
1799 .show = show_host_os_type,
1800};
1801
Tony Jonesee959b02008-02-22 00:13:36 +01001802static ssize_t show_host_config(struct device *dev,
1803 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Tony Jonesee959b02008-02-22 00:13:36 +01001805 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001806 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 /* returns null-terminated host config data */
1809 if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
1810 return strlen(buf);
1811 else
1812 return 0;
1813}
1814
Tony Jonesee959b02008-02-22 00:13:36 +01001815static struct device_attribute ibmvscsi_host_config = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 .attr = {
1817 .name = "config",
1818 .mode = S_IRUGO,
1819 },
1820 .show = show_host_config,
1821};
1822
Tony Jonesee959b02008-02-22 00:13:36 +01001823static struct device_attribute *ibmvscsi_attrs[] = {
Brian King126c5cc2009-06-08 16:19:08 -05001824 &ibmvscsi_host_vhost_loc,
1825 &ibmvscsi_host_vhost_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 &ibmvscsi_host_srp_version,
1827 &ibmvscsi_host_partition_name,
1828 &ibmvscsi_host_partition_number,
1829 &ibmvscsi_host_mad_version,
1830 &ibmvscsi_host_os_type,
1831 &ibmvscsi_host_config,
1832 NULL
1833};
1834
1835/* ------------------------------------------------------------
1836 * SCSI driver registration
1837 */
1838static struct scsi_host_template driver_template = {
1839 .module = THIS_MODULE,
1840 .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
1841 .proc_name = "ibmvscsi",
1842 .queuecommand = ibmvscsi_queuecommand,
1843 .eh_abort_handler = ibmvscsi_eh_abort_handler,
1844 .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
Brian King3d0e91f2007-06-13 17:12:26 -05001845 .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
Robert Jennings0979c842007-03-29 12:30:40 -05001846 .slave_configure = ibmvscsi_slave_configure,
Brian King742d25b2007-05-29 15:46:14 -05001847 .change_queue_depth = ibmvscsi_change_queue_depth,
Robert Jennings7912a0a2008-07-24 04:35:27 +10001848 .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
Robert Jenningsa897ff22007-03-28 12:45:46 -05001849 .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 .this_id = -1,
James Bottomley4dddbc22005-09-06 17:11:54 -05001851 .sg_tablesize = SG_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 .use_clustering = ENABLE_CLUSTERING,
1853 .shost_attrs = ibmvscsi_attrs,
1854};
1855
1856/**
Robert Jennings7912a0a2008-07-24 04:35:27 +10001857 * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
1858 *
1859 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1860 *
1861 * Return value:
1862 * Number of bytes of IO data the driver will need to perform well.
1863 */
1864static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
1865{
1866 /* iu_storage data allocated in initialize_event_pool */
Brian King4f10aae2008-12-17 17:19:33 -06001867 unsigned long desired_io = max_events * sizeof(union viosrp_iu);
Robert Jennings7912a0a2008-07-24 04:35:27 +10001868
1869 /* add io space for sg data */
Brian King004dd5e2008-08-15 10:48:47 -05001870 desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
Robert Jennings7912a0a2008-07-24 04:35:27 +10001871 IBMVSCSI_CMDS_PER_LUN_DEFAULT);
1872
1873 return desired_io;
1874}
1875
1876/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * Called by bus code for each adapter
1878 */
1879static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1880{
1881 struct ibmvscsi_host_data *hostdata;
1882 struct Scsi_Host *host;
1883 struct device *dev = &vdev->dev;
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001884 struct srp_rport_identifiers ids;
1885 struct srp_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 unsigned long wait_switch = 0;
Dave C Boutchercefbda22006-06-12 21:22:51 -05001887 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Greg Kroah-Hartman559fde72009-05-04 12:40:54 -07001889 dev_set_drvdata(&vdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
1892 if (!host) {
Brian King6c0a60e2007-06-13 17:12:19 -05001893 dev_err(&vdev->dev, "couldn't allocate host data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 goto scsi_host_alloc_failed;
1895 }
1896
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001897 host->transportt = ibmvscsi_transport_template;
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001898 hostdata = shost_priv(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 memset(hostdata, 0x00, sizeof(*hostdata));
1900 INIT_LIST_HEAD(&hostdata->sent);
1901 hostdata->host = host;
1902 hostdata->dev = dev;
1903 atomic_set(&hostdata->request_limit, -1);
Robert Jennings7912a0a2008-07-24 04:35:27 +10001904 hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Brian King126c5cc2009-06-08 16:19:08 -05001906 if (map_persist_bufs(hostdata)) {
1907 dev_err(&vdev->dev, "couldn't map persistent buffers\n");
1908 goto persist_bufs_failed;
1909 }
1910
Brian King4f10aae2008-12-17 17:19:33 -06001911 rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
Dave C Boutchercefbda22006-06-12 21:22:51 -05001912 if (rc != 0 && rc != H_RESOURCE) {
Brian King6c0a60e2007-06-13 17:12:19 -05001913 dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 goto init_crq_failed;
1915 }
Brian King4f10aae2008-12-17 17:19:33 -06001916 if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001917 dev_err(&vdev->dev, "couldn't initialize event pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 goto init_pool_failed;
1919 }
1920
1921 host->max_lun = 8;
1922 host->max_id = max_id;
1923 host->max_channel = max_channel;
Brian Kingfbc56f02009-06-08 16:19:01 -05001924 host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 if (scsi_add_host(hostdata->host, hostdata->dev))
1927 goto add_host_failed;
1928
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001929 /* we don't have a proper target_port_id so let's use the fake one */
1930 memcpy(ids.port_id, hostdata->madapter_info.partition_name,
1931 sizeof(ids.port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001932 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001933 rport = srp_rport_add(host, &ids);
1934 if (IS_ERR(rport))
1935 goto add_srp_port_failed;
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* Try to send an initialization message. Note that this is allowed
1938 * to fail if the other end is not acive. In that case we don't
1939 * want to scan
1940 */
David Woodhoused3849d52007-09-22 08:29:36 +10001941 if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
Dave C Boutchercefbda22006-06-12 21:22:51 -05001942 || rc == H_RESOURCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /*
1944 * Wait around max init_timeout secs for the adapter to finish
1945 * initializing. When we are done initializing, we will have a
1946 * valid request_limit. We don't want Linux scanning before
1947 * we are ready.
1948 */
1949 for (wait_switch = jiffies + (init_timeout * HZ);
1950 time_before(jiffies, wait_switch) &&
1951 atomic_read(&hostdata->request_limit) < 2;) {
1952
1953 msleep(10);
1954 }
1955
1956 /* if we now have a valid request_limit, initiate a scan */
1957 if (atomic_read(&hostdata->request_limit) > 0)
1958 scsi_scan_host(host);
1959 }
1960
Greg Kroah-Hartman559fde72009-05-04 12:40:54 -07001961 dev_set_drvdata(&vdev->dev, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return 0;
1963
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001964 add_srp_port_failed:
1965 scsi_remove_host(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 add_host_failed:
1967 release_event_pool(&hostdata->pool, hostdata);
1968 init_pool_failed:
Brian King4f10aae2008-12-17 17:19:33 -06001969 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 init_crq_failed:
Brian King126c5cc2009-06-08 16:19:08 -05001971 unmap_persist_bufs(hostdata);
1972 persist_bufs_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 scsi_host_put(host);
1974 scsi_host_alloc_failed:
1975 return -1;
1976}
1977
1978static int ibmvscsi_remove(struct vio_dev *vdev)
1979{
Greg Kroah-Hartman559fde72009-05-04 12:40:54 -07001980 struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
Brian King126c5cc2009-06-08 16:19:08 -05001981 unmap_persist_bufs(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 release_event_pool(&hostdata->pool, hostdata);
David Woodhoused3849d52007-09-22 08:29:36 +10001983 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
Brian King4f10aae2008-12-17 17:19:33 -06001984 max_events);
FUJITA Tomonori4d680412007-06-27 16:32:50 +09001985
1986 srp_remove_host(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 scsi_remove_host(hostdata->host);
1988 scsi_host_put(hostdata->host);
1989
1990 return 0;
1991}
1992
1993/**
1994 * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
1995 * support.
1996 */
1997static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
1998 {"vscsi", "IBM,v-scsi"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001999 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +10002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003static struct vio_driver ibmvscsi_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 .id_table = ibmvscsi_device_table,
2005 .probe = ibmvscsi_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10002006 .remove = ibmvscsi_remove,
Robert Jennings7912a0a2008-07-24 04:35:27 +10002007 .get_desired_dma = ibmvscsi_get_desired_dma,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10002008 .driver = {
2009 .name = "ibmvscsi",
Stephen Rothwell915124d2005-10-24 15:12:22 +10002010 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10002011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012};
2013
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002014static struct srp_function_template ibmvscsi_transport_functions = {
2015};
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017int __init ibmvscsi_module_init(void)
2018{
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002019 int ret;
2020
Brian King4f10aae2008-12-17 17:19:33 -06002021 /* Ensure we have two requests to do error recovery */
2022 driver_template.can_queue = max_requests;
2023 max_events = max_requests + 2;
2024
David Woodhoused3849d52007-09-22 08:29:36 +10002025 if (firmware_has_feature(FW_FEATURE_ISERIES))
2026 ibmvscsi_ops = &iseriesvscsi_ops;
2027 else if (firmware_has_feature(FW_FEATURE_VIO))
2028 ibmvscsi_ops = &rpavscsi_ops;
2029 else
2030 return -ENODEV;
2031
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002032 ibmvscsi_transport_template =
2033 srp_attach_transport(&ibmvscsi_transport_functions);
2034 if (!ibmvscsi_transport_template)
2035 return -ENOMEM;
2036
2037 ret = vio_register_driver(&ibmvscsi_driver);
2038 if (ret)
2039 srp_release_transport(ibmvscsi_transport_template);
2040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
2043void __exit ibmvscsi_module_exit(void)
2044{
2045 vio_unregister_driver(&ibmvscsi_driver);
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002046 srp_release_transport(ibmvscsi_transport_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049module_init(ibmvscsi_module_init);
2050module_exit(ibmvscsi_module_exit);