Merge branch 'for-jens' of git://git.drbd.org/linux-2.6-drbd into for-linus
[linux-2.6-block.git] / drivers / scsi / ibmvscsi / ibmvscsi.c
CommitLineData
1da177e4
LT
1/* ------------------------------------------------------------
2 * ibmvscsi.c
3 * (C) Copyright IBM Corporation 1994, 2004
4 * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
5 * Santiago Leon (santil@us.ibm.com)
6 * Dave Boutcher (sleddog@us.ibm.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 *
23 * ------------------------------------------------------------
24 * Emulation of a SCSI host adapter for Virtual I/O devices
25 *
26 * This driver supports the SCSI adapter implemented by the IBM
27 * Power5 firmware. That SCSI adapter is not a physical adapter,
28 * but allows Linux SCSI peripheral drivers to directly
29 * access devices in another logical partition on the physical system.
30 *
31 * The virtual adapter(s) are present in the open firmware device
32 * tree just like real adapters.
33 *
34 * One of the capabilities provided on these systems is the ability
35 * to DMA between partitions. The architecture states that for VSCSI,
36 * the server side is allowed to DMA to and from the client. The client
37 * is never trusted to DMA to or from the server directly.
38 *
39 * Messages are sent between partitions on a "Command/Response Queue"
40 * (CRQ), which is just a buffer of 16 byte entries in the receiver's
41 * Senders cannot access the buffer directly, but send messages by
42 * making a hypervisor call and passing in the 16 bytes. The hypervisor
43 * puts the message in the next 16 byte space in round-robbin fashion,
44 * turns on the high order bit of the message (the valid bit), and
45 * generates an interrupt to the receiver (if interrupts are turned on.)
46 * The receiver just turns off the valid bit when they have copied out
47 * the message.
48 *
49 * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
50 * (IU) (as defined in the T10 standard available at www.t10.org), gets
51 * a DMA address for the message, and sends it to the server as the
52 * payload of a CRQ message. The server DMAs the SRP IU and processes it,
53 * including doing any additional data transfers. When it is done, it
54 * DMAs the SRP response back to the same address as the request came from,
55 * and sends a CRQ message back to inform the client that the request has
56 * completed.
57 *
58 * Note that some of the underlying infrastructure is different between
59 * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
60 * the older iSeries hypervisor models. To support both, some low level
61 * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
62 * The Makefile should pick one, not two, not zero, of these.
63 *
64 * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
65 * interfaces. It would be really nice to abstract this above an RDMA
66 * layer.
67 */
68
69#include <linux/module.h>
70#include <linux/moduleparam.h>
71#include <linux/dma-mapping.h>
72#include <linux/delay.h>
126c5cc3 73#include <linux/of.h>
d3849d51 74#include <asm/firmware.h>
1da177e4
LT
75#include <asm/vio.h>
76#include <scsi/scsi.h>
77#include <scsi/scsi_cmnd.h>
78#include <scsi/scsi_host.h>
79#include <scsi/scsi_device.h>
4d680419 80#include <scsi/scsi_transport_srp.h>
1da177e4
LT
81#include "ibmvscsi.h"
82
83/* The values below are somewhat arbitrary default values, but
84 * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
85 * Note that there are 3 bits of channel value, 6 bits of id, and
86 * 5 bits of LUN.
87 */
88static int max_id = 64;
89static int max_channel = 3;
e1a5ce5b
RJ
90static int init_timeout = 300;
91static int login_timeout = 60;
92static int info_timeout = 30;
93static int abort_timeout = 60;
94static int reset_timeout = 60;
a897ff2a 95static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
4f10aae0 96static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
c1988e31 97static int fast_fail = 1;
126c5cc3 98static int client_reserve = 1;
1da177e4 99
4d680419
FT
100static struct scsi_transport_template *ibmvscsi_transport_template;
101
2b541f8f 102#define IBMVSCSI_VERSION "1.5.8"
1da177e4 103
d3849d51
DW
104static struct ibmvscsi_ops *ibmvscsi_ops;
105
1da177e4
LT
106MODULE_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");
21465eda 117module_param_named(max_requests, max_requests, int, S_IRUGO);
1da177e4 118MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
c1988e31
RJ
119module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
120MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
126c5cc3
BK
121module_param_named(client_reserve, client_reserve, int, S_IRUGO );
122MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
1da177e4
LT
123
124/* ------------------------------------------------------------
125 * Routines for the event pool and event structs
126 */
127/**
128 * initialize_event_pool: - Allocates and initializes the event pool for a host
129 * @pool: event_pool to be initialized
130 * @size: Number of events in pool
131 * @hostdata: ibmvscsi_host_data who owns the event pool
132 *
133 * Returns zero on success.
134*/
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;
4c021dd1 142 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1da177e4
LT
143 if (!pool->events)
144 return -ENOMEM;
1da177e4
LT
145
146 pool->iu_storage =
147 dma_alloc_coherent(hostdata->dev,
148 pool->size * sizeof(*pool->iu_storage),
149 &pool->iu_token, 0);
150 if (!pool->iu_storage) {
151 kfree(pool->events);
152 return -ENOMEM;
153 }
154
155 for (i = 0; i < pool->size; ++i) {
156 struct srp_event_struct *evt = &pool->events[i];
157 memset(&evt->crq, 0x00, sizeof(evt->crq));
158 atomic_set(&evt->free, 1);
159 evt->crq.valid = 0x80;
160 evt->crq.IU_length = sizeof(*evt->xfer_iu);
161 evt->crq.IU_data_ptr = pool->iu_token +
162 sizeof(*evt->xfer_iu) * i;
163 evt->xfer_iu = pool->iu_storage + i;
164 evt->hostdata = hostdata;
4dddbc26
JB
165 evt->ext_list = NULL;
166 evt->ext_list_token = 0;
1da177e4
LT
167 }
168
169 return 0;
170}
171
172/**
173 * release_event_pool: - Frees memory of an event pool of a host
174 * @pool: event_pool to be released
175 * @hostdata: ibmvscsi_host_data who owns the even pool
176 *
177 * Returns zero on success.
178*/
179static void release_event_pool(struct event_pool *pool,
180 struct ibmvscsi_host_data *hostdata)
181{
182 int i, in_use = 0;
4dddbc26 183 for (i = 0; i < pool->size; ++i) {
1da177e4
LT
184 if (atomic_read(&pool->events[i].free) != 1)
185 ++in_use;
4dddbc26
JB
186 if (pool->events[i].ext_list) {
187 dma_free_coherent(hostdata->dev,
ef265673 188 SG_ALL * sizeof(struct srp_direct_buf),
4dddbc26
JB
189 pool->events[i].ext_list,
190 pool->events[i].ext_list_token);
191 }
192 }
1da177e4 193 if (in_use)
6c0a60ec
BK
194 dev_warn(hostdata->dev, "releasing event pool with %d "
195 "events still in use?\n", in_use);
1da177e4
LT
196 kfree(pool->events);
197 dma_free_coherent(hostdata->dev,
198 pool->size * sizeof(*pool->iu_storage),
199 pool->iu_storage, pool->iu_token);
200}
201
202/**
203 * valid_event_struct: - Determines if event is valid.
204 * @pool: event_pool that contains the event
205 * @evt: srp_event_struct to be checked for validity
206 *
207 * Returns zero if event is invalid, one otherwise.
208*/
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)) {
6c0a60ec
BK
230 dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
231 "(not in pool %p)\n", evt, pool->events);
1da177e4
LT
232 return;
233 }
234 if (atomic_inc_return(&evt->free) != 1) {
6c0a60ec
BK
235 dev_err(evt->hostdata->dev, "Freeing event_struct %p "
236 "which is not in use!\n", evt);
1da177e4
LT
237 return;
238 }
239}
240
241/**
242 * get_evt_struct: - Gets the next free event in pool
243 * @pool: event_pool that contains the events to be searched
244 *
245 * Returns the next event in "free" state, and NULL if none are free.
246 * Note that no synchronization is done here, we assume the host_lock
247 * will syncrhonze things.
248*/
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{
ef265673
FT
301 u8 fmt;
302
1da177e4
LT
303 if (numbuf == 0)
304 return;
305
ef265673
FT
306 if (numbuf == 1)
307 fmt = SRP_DATA_DESC_DIRECT;
308 else {
309 fmt = SRP_DATA_DESC_INDIRECT;
310 numbuf = min(numbuf, MAX_INDIRECT_BUFS);
311
1da177e4 312 if (cmd->sc_data_direction == DMA_TO_DEVICE)
ef265673
FT
313 srp_cmd->data_out_desc_cnt = numbuf;
314 else
315 srp_cmd->data_in_desc_cnt = numbuf;
1da177e4 316 }
ef265673
FT
317
318 if (cmd->sc_data_direction == DMA_TO_DEVICE)
319 srp_cmd->buf_fmt = fmt << 4;
320 else
321 srp_cmd->buf_fmt = fmt;
1da177e4
LT
322}
323
ef265673 324static void unmap_sg_list(int num_entries,
4dddbc26 325 struct device *dev,
ef265673
FT
326 struct srp_direct_buf *md)
327{
4dddbc26
JB
328 int i;
329
ef265673
FT
330 for (i = 0; i < num_entries; ++i)
331 dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
4dddbc26
JB
332}
333
1da177e4
LT
334/**
335 * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
336 * @cmd: srp_cmd whose additional_data member will be unmapped
337 * @dev: device for which the memory is mapped
338 *
339*/
4dddbc26
JB
340static void unmap_cmd_data(struct srp_cmd *cmd,
341 struct srp_event_struct *evt_struct,
342 struct device *dev)
1da177e4 343{
ef265673
FT
344 u8 out_fmt, in_fmt;
345
346 out_fmt = cmd->buf_fmt >> 4;
347 in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
348
349 if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
1da177e4 350 return;
ef265673
FT
351 else if (out_fmt == SRP_DATA_DESC_DIRECT ||
352 in_fmt == SRP_DATA_DESC_DIRECT) {
353 struct srp_direct_buf *data =
354 (struct srp_direct_buf *) cmd->add_data;
355 dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
1da177e4 356 } else {
ef265673
FT
357 struct srp_indirect_buf *indirect =
358 (struct srp_indirect_buf *) cmd->add_data;
359 int num_mapped = indirect->table_desc.len /
360 sizeof(struct srp_direct_buf);
4dddbc26
JB
361
362 if (num_mapped <= MAX_INDIRECT_BUFS) {
ef265673 363 unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
4dddbc26 364 return;
1da177e4 365 }
4dddbc26
JB
366
367 unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
1da177e4
LT
368 }
369}
370
9413d7b8 371static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
ef265673 372 struct srp_direct_buf *md)
4dddbc26
JB
373{
374 int i;
9413d7b8 375 struct scatterlist *sg;
4dddbc26
JB
376 u64 total_length = 0;
377
9413d7b8 378 scsi_for_each_sg(cmd, sg, nseg, i) {
ef265673 379 struct srp_direct_buf *descr = md + i;
9413d7b8
FT
380 descr->va = sg_dma_address(sg);
381 descr->len = sg_dma_len(sg);
ef265673 382 descr->key = 0;
9413d7b8 383 total_length += sg_dma_len(sg);
4dddbc26
JB
384 }
385 return total_length;
386}
387
1da177e4
LT
388/**
389 * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
390 * @cmd: Scsi_Cmnd with the scatterlist
391 * @srp_cmd: srp_cmd that contains the memory descriptor
392 * @dev: device for which to map dma memory
393 *
394 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
395 * Returns 1 on success.
396*/
397static int map_sg_data(struct scsi_cmnd *cmd,
4dddbc26 398 struct srp_event_struct *evt_struct,
1da177e4
LT
399 struct srp_cmd *srp_cmd, struct device *dev)
400{
401
4dddbc26 402 int sg_mapped;
1da177e4 403 u64 total_length = 0;
ef265673
FT
404 struct srp_direct_buf *data =
405 (struct srp_direct_buf *) srp_cmd->add_data;
406 struct srp_indirect_buf *indirect =
407 (struct srp_indirect_buf *) data;
1da177e4 408
9413d7b8
FT
409 sg_mapped = scsi_dma_map(cmd);
410 if (!sg_mapped)
411 return 1;
412 else if (sg_mapped < 0)
1da177e4
LT
413 return 0;
414
415 set_srp_direction(cmd, srp_cmd, sg_mapped);
416
417 /* special case; we can use a single direct descriptor */
418 if (sg_mapped == 1) {
9413d7b8 419 map_sg_list(cmd, sg_mapped, data);
1da177e4
LT
420 return 1;
421 }
422
ef265673
FT
423 indirect->table_desc.va = 0;
424 indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
425 indirect->table_desc.key = 0;
4dddbc26
JB
426
427 if (sg_mapped <= MAX_INDIRECT_BUFS) {
9413d7b8 428 total_length = map_sg_list(cmd, sg_mapped,
ef265673
FT
429 &indirect->desc_list[0]);
430 indirect->len = total_length;
4dddbc26 431 return 1;
1da177e4 432 }
1da177e4 433
4dddbc26
JB
434 /* get indirect table */
435 if (!evt_struct->ext_list) {
ef265673 436 evt_struct->ext_list = (struct srp_direct_buf *)
9413d7b8 437 dma_alloc_coherent(dev,
ef265673
FT
438 SG_ALL * sizeof(struct srp_direct_buf),
439 &evt_struct->ext_list_token, 0);
4dddbc26 440 if (!evt_struct->ext_list) {
7912a0ac
RJ
441 if (!firmware_has_feature(FW_FEATURE_CMO))
442 sdev_printk(KERN_ERR, cmd->device,
443 "Can't allocate memory "
444 "for indirect table\n");
e637d553 445 scsi_dma_unmap(cmd);
4dddbc26 446 return 0;
4dddbc26
JB
447 }
448 }
449
9413d7b8 450 total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
4dddbc26 451
ef265673
FT
452 indirect->len = total_length;
453 indirect->table_desc.va = evt_struct->ext_list_token;
454 indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
455 memcpy(indirect->desc_list, evt_struct->ext_list,
456 MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
4dddbc26 457 return 1;
1da177e4
LT
458}
459
1da177e4
LT
460/**
461 * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
462 * @cmd: struct scsi_cmnd with the memory to be mapped
463 * @srp_cmd: srp_cmd that contains the memory descriptor
464 * @dev: dma device for which to map dma memory
465 *
466 * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
467 * Returns 1 on success.
468*/
469static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
4dddbc26 470 struct srp_event_struct *evt_struct,
1da177e4
LT
471 struct srp_cmd *srp_cmd, struct device *dev)
472{
473 switch (cmd->sc_data_direction) {
474 case DMA_FROM_DEVICE:
475 case DMA_TO_DEVICE:
476 break;
477 case DMA_NONE:
478 return 1;
479 case DMA_BIDIRECTIONAL:
6c0a60ec
BK
480 sdev_printk(KERN_ERR, cmd->device,
481 "Can't map DMA_BIDIRECTIONAL to read/write\n");
1da177e4
LT
482 return 0;
483 default:
6c0a60ec
BK
484 sdev_printk(KERN_ERR, cmd->device,
485 "Unknown data direction 0x%02x; can't map!\n",
486 cmd->sc_data_direction);
1da177e4
LT
487 return 0;
488 }
489
9413d7b8 490 return map_sg_data(cmd, evt_struct, srp_cmd, dev);
1da177e4
LT
491}
492
3d0e91f7
BK
493/**
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);
d3849d51
DW
530 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
531 (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
3d0e91f7
BK
532 (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
533 atomic_set(&hostdata->request_limit, -1);
534 dev_err(hostdata->dev, "error after reset\n");
535 }
536
537 scsi_unblock_requests(hostdata->host);
538}
539
540/**
541 * ibmvscsi_timeout - Internal command timeout handler
542 * @evt_struct: struct srp_event_struct that timed out
543 *
544 * Called when an internally generated command times out
545*/
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
1da177e4
LT
557/* ------------------------------------------------------------
558 * Routines for sending and receiving SRPs
559 */
560/**
561 * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
562 * @evt_struct: evt_struct to be sent
563 * @hostdata: ibmvscsi_host_data of host
3d0e91f7 564 * @timeout: timeout in seconds - 0 means do not time command
1da177e4
LT
565 *
566 * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
567 * Note that this routine assumes that host_lock is held for synchronization
568*/
569static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
3d0e91f7
BK
570 struct ibmvscsi_host_data *hostdata,
571 unsigned long timeout)
1da177e4 572{
1da177e4 573 u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
3c887e8a 574 int request_status = 0;
1da177e4
LT
575 int rc;
576
a897ff2a
RJ
577 /* If we have exhausted our request limit, just fail this request,
578 * unless it is for a reset or abort.
1da177e4
LT
579 * Note that there are rare cases involving driver generated requests
580 * (such as task management requests) that the mid layer may think we
581 * can handle more requests (can_queue) when we actually can't
582 */
cefbda2d
DB
583 if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
584 request_status =
585 atomic_dec_if_positive(&hostdata->request_limit);
586 /* If request limit was -1 when we started, it is now even
587 * less than that
588 */
589 if (request_status < -1)
590 goto send_error;
a897ff2a 591 /* Otherwise, we may have run out of requests. */
3c887e8a
RJ
592 /* If request limit was 0 when we started the adapter is in the
593 * process of performing a login with the server adapter, or
594 * we may have run out of requests.
595 */
596 else if (request_status == -1 &&
597 evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
598 goto send_busy;
a897ff2a
RJ
599 /* Abort and reset calls should make it through.
600 * Nothing except abort and reset should use the last two
601 * slots unless we had two or less to begin with.
602 */
603 else if (request_status < 2 &&
604 evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
605 /* In the case that we have less than two requests
606 * available, check the server limit as a combination
607 * of the request limit and the number of requests
608 * in-flight (the size of the send list). If the
609 * server limit is greater than 2, return busy so
610 * that the last two are reserved for reset and abort.
611 */
612 int server_limit = request_status;
613 struct srp_event_struct *tmp_evt;
614
615 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
616 server_limit++;
617 }
618
619 if (server_limit > 2)
620 goto send_busy;
621 }
cefbda2d 622 }
1da177e4
LT
623
624 /* Copy the IU into the transfer area */
625 *evt_struct->xfer_iu = evt_struct->iu;
ef265673 626 evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
1da177e4
LT
627
628 /* Add this to the sent list. We need to do this
629 * before we actually send
630 * in case it comes back REALLY fast
631 */
632 list_add_tail(&evt_struct->list, &hostdata->sent);
633
3d0e91f7
BK
634 init_timer(&evt_struct->timer);
635 if (timeout) {
636 evt_struct->timer.data = (unsigned long) evt_struct;
637 evt_struct->timer.expires = jiffies + (timeout * HZ);
638 evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
639 add_timer(&evt_struct->timer);
640 }
641
1da177e4 642 if ((rc =
d3849d51 643 ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
1da177e4 644 list_del(&evt_struct->list);
3d0e91f7 645 del_timer(&evt_struct->timer);
1da177e4 646
860784c8
RJ
647 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
648 * Firmware will send a CRQ with a transport event (0xFF) to
649 * tell this client what has happened to the transport. This
650 * will be handled in ibmvscsi_handle_crq()
651 */
652 if (rc == H_CLOSED) {
653 dev_warn(hostdata->dev, "send warning. "
654 "Receive queue closed, will retry.\n");
655 goto send_busy;
656 }
6c0a60ec 657 dev_err(hostdata->dev, "send error %d\n", rc);
a897ff2a 658 atomic_inc(&hostdata->request_limit);
1da177e4
LT
659 goto send_error;
660 }
661
662 return 0;
663
cefbda2d 664 send_busy:
4dddbc26 665 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
1da177e4 666
1da177e4 667 free_event_struct(&hostdata->pool, evt_struct);
3c887e8a
RJ
668 if (request_status != -1)
669 atomic_inc(&hostdata->request_limit);
a897ff2a 670 return SCSI_MLQUEUE_HOST_BUSY;
cefbda2d
DB
671
672 send_error:
673 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
674
675 if (evt_struct->cmnd != NULL) {
676 evt_struct->cmnd->result = DID_ERROR << 16;
677 evt_struct->cmnd_done(evt_struct->cmnd);
678 } else if (evt_struct->done)
679 evt_struct->done(evt_struct);
680
681 free_event_struct(&hostdata->pool, evt_struct);
682 return 0;
1da177e4
LT
683}
684
685/**
686 * handle_cmd_rsp: - Handle responses from commands
687 * @evt_struct: srp_event_struct to be handled
688 *
689 * Used as a callback by when sending scsi cmds.
690 * Gets called by ibmvscsi_handle_crq()
691*/
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
ef265673 697 if (unlikely(rsp->opcode != SRP_RSP)) {
1da177e4 698 if (printk_ratelimit())
6c0a60ec
BK
699 dev_warn(evt_struct->hostdata->dev,
700 "bad SRP RSP type %d\n", rsp->opcode);
1da177e4
LT
701 }
702
703 if (cmnd) {
c3a3b55a 704 cmnd->result |= rsp->status;
1da177e4
LT
705 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
706 memcpy(cmnd->sense_buffer,
ef265673
FT
707 rsp->data,
708 rsp->sense_data_len);
1da177e4 709 unmap_cmd_data(&evt_struct->iu.srp.cmd,
4dddbc26 710 evt_struct,
1da177e4
LT
711 evt_struct->hostdata->dev);
712
ef265673 713 if (rsp->flags & SRP_RSP_FLAG_DOOVER)
9413d7b8 714 scsi_set_resid(cmnd, rsp->data_out_res_cnt);
ef265673 715 else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
9413d7b8 716 scsi_set_resid(cmnd, rsp->data_in_res_cnt);
1da177e4
LT
717 }
718
719 if (evt_struct->cmnd_done)
720 evt_struct->cmnd_done(cmnd);
721}
722
723/**
724 * lun_from_dev: - Returns the lun of the scsi device
725 * @dev: struct scsi_device
726 *
727*/
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;
ef265673 743 struct srp_indirect_buf *indirect;
7603e02e 744 struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
1da177e4 745 u16 lun = lun_from_dev(cmnd->device);
ef265673 746 u8 out_fmt, in_fmt;
1da177e4 747
c3a3b55a 748 cmnd->result = (DID_OK << 16);
1da177e4
LT
749 evt_struct = get_event_struct(&hostdata->pool);
750 if (!evt_struct)
751 return SCSI_MLQUEUE_HOST_BUSY;
752
1da177e4
LT
753 /* Set up the actual SRP IU */
754 srp_cmd = &evt_struct->iu.srp.cmd;
ef265673
FT
755 memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
756 srp_cmd->opcode = SRP_CMD;
64a87b24 757 memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
1da177e4
LT
758 srp_cmd->lun = ((u64) lun) << 48;
759
4dddbc26 760 if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
7912a0ac
RJ
761 if (!firmware_has_feature(FW_FEATURE_CMO))
762 sdev_printk(KERN_ERR, cmnd->device,
763 "couldn't convert cmd to srp_cmd\n");
1da177e4
LT
764 free_event_struct(&hostdata->pool, evt_struct);
765 return SCSI_MLQUEUE_HOST_BUSY;
766 }
767
4dddbc26
JB
768 init_event_struct(evt_struct,
769 handle_cmd_rsp,
770 VIOSRP_SRP_FORMAT,
242f9dcb 771 cmnd->request->timeout/HZ);
4dddbc26
JB
772
773 evt_struct->cmnd = cmnd;
774 evt_struct->cmnd_done = done;
775
1da177e4 776 /* Fix up dma address of the buffer itself */
ef265673
FT
777 indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
778 out_fmt = srp_cmd->buf_fmt >> 4;
779 in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
780 if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
781 out_fmt == SRP_DATA_DESC_INDIRECT) &&
782 indirect->table_desc.va == 0) {
783 indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
784 offsetof(struct srp_cmd, add_data) +
785 offsetof(struct srp_indirect_buf, desc_list);
1da177e4
LT
786 }
787
3d0e91f7 788 return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
1da177e4
LT
789}
790
791/* ------------------------------------------------------------
792 * Routines for driver initialization
793 */
3507e13f 794
126c5cc3
BK
795/**
796 * map_persist_bufs: - Pre-map persistent data for adapter logins
797 * @hostdata: ibmvscsi_host_data of host
798 *
799 * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
800 * Return 1 on error, 0 on success.
801 */
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
3507e13f
BK
842/**
843 * login_rsp: - Handle response to SRP login request
844 * @evt_struct: srp_event_struct with the response
845 *
846 * Used as a "done" callback by when sending srp_login. Gets called
847 * by ibmvscsi_handle_crq()
848*/
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");
126c5cc3 870 hostdata->client_migrated = 0;
3507e13f
BK
871
872 /* Now we know what the real request-limit is.
873 * This value is set rather than added to request_limit because
874 * request_limit could have been set to -1 by this client.
875 */
876 atomic_set(&hostdata->request_limit,
877 evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
878
879 /* If we had any pending I/Os, kick them */
880 scsi_unblock_requests(hostdata->host);
881}
882
883/**
884 * send_srp_login: - Sends the srp login
885 * @hostdata: ibmvscsi_host_data of host
886 *
887 * Returns zero if successful.
888*/
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
126c5cc3
BK
919/**
920 * capabilities_rsp: - Handle response to MAD adapter capabilities request
921 * @evt_struct: srp_event_struct with the response
922 *
923 * Used as a "done" callback by when sending adapter_info.
924 */
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
c1988e31
RJ
1006/**
1007 * fast_fail_rsp: - Handle response to MAD enable fast fail
1008 * @evt_struct: srp_event_struct with the response
1009 *
1010 * Used as a "done" callback by when sending enable fast fail. Gets called
1011 * by ibmvscsi_handle_crq()
1012 */
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
126c5cc3 1025 send_mad_capabilities(hostdata);
c1988e31
RJ
1026}
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
126c5cc3
BK
1041 if (!fast_fail) {
1042 send_mad_capabilities(hostdata);
1043 return 0;
1044 }
c1988e31
RJ
1045
1046 evt_struct = get_event_struct(&hostdata->pool);
1047 BUG_ON(!evt_struct);
1048
1049 init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
1050
1051 fast_fail_mad = &evt_struct->iu.mad.fast_fail;
1052 memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
1053 fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
1054 fast_fail_mad->common.length = sizeof(*fast_fail_mad);
1055
1056 spin_lock_irqsave(hostdata->host->host_lock, flags);
1057 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1058 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1059 return rc;
1060}
1061
1da177e4
LT
1062/**
1063 * adapter_info_rsp: - Handle response to MAD adapter info request
1064 * @evt_struct: srp_event_struct with the response
1065 *
1066 * Used as a "done" callback by when sending adapter_info. Gets called
1067 * by ibmvscsi_handle_crq()
1068*/
1069static void adapter_info_rsp(struct srp_event_struct *evt_struct)
1070{
1071 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1da177e4
LT
1072
1073 if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
6c0a60ec
BK
1074 dev_err(hostdata->dev, "error %d getting adapter info\n",
1075 evt_struct->xfer_iu->mad.adapter_info.common.status);
1da177e4 1076 } else {
6c0a60ec
BK
1077 dev_info(hostdata->dev, "host srp version: %s, "
1078 "host partition %s (%d), OS %d, max io %u\n",
1079 hostdata->madapter_info.srp_version,
1080 hostdata->madapter_info.partition_name,
1081 hostdata->madapter_info.partition_number,
1082 hostdata->madapter_info.os_type,
1083 hostdata->madapter_info.port_max_txu[0]);
1da177e4
LT
1084
1085 if (hostdata->madapter_info.port_max_txu[0])
1086 hostdata->host->max_sectors =
1087 hostdata->madapter_info.port_max_txu[0] >> 9;
154fb614
DB
1088
1089 if (hostdata->madapter_info.os_type == 3 &&
1090 strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
6c0a60ec
BK
1091 dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
1092 hostdata->madapter_info.srp_version);
1093 dev_err(hostdata->dev, "limiting scatterlists to %d\n",
1094 MAX_INDIRECT_BUFS);
154fb614
DB
1095 hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
1096 }
e08afeb7
BK
1097
1098 if (hostdata->madapter_info.os_type == 3) {
1099 enable_fast_fail(hostdata);
1100 return;
1101 }
1da177e4 1102 }
3507e13f 1103
e08afeb7 1104 send_srp_login(hostdata);
1da177e4
LT
1105}
1106
1107/**
1108 * send_mad_adapter_info: - Sends the mad adapter info request
1109 * and stores the result so it can be retrieved with
1110 * sysfs. We COULD consider causing a failure if the
1111 * returned SRP version doesn't match ours.
1112 * @hostdata: ibmvscsi_host_data of host
1113 *
1114 * Returns zero if successful.
1115*/
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;
06f923cb 1120 unsigned long flags;
e5dbfa66 1121
1da177e4 1122 evt_struct = get_event_struct(&hostdata->pool);
3507e13f 1123 BUG_ON(!evt_struct);
1da177e4
LT
1124
1125 init_event_struct(evt_struct,
1126 adapter_info_rsp,
1127 VIOSRP_MAD_FORMAT,
e1a5ce5b 1128 info_timeout);
1da177e4
LT
1129
1130 req = &evt_struct->iu.mad.adapter_info;
1131 memset(req, 0x00, sizeof(*req));
1132
1133 req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
1134 req->common.length = sizeof(hostdata->madapter_info);
126c5cc3 1135 req->buffer = hostdata->adapter_info_addr;
1da177e4 1136
06f923cb 1137 spin_lock_irqsave(hostdata->host->host_lock, flags);
126c5cc3 1138 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
6c0a60ec 1139 dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
06f923cb 1140 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1da177e4
LT
1141};
1142
1143/**
3507e13f 1144 * init_adapter: Start virtual adapter initialization sequence
1da177e4 1145 *
3507e13f
BK
1146 */
1147static void init_adapter(struct ibmvscsi_host_data *hostdata)
1da177e4 1148{
1da177e4 1149 send_mad_adapter_info(hostdata);
1da177e4
LT
1150}
1151
1da177e4
LT
1152/**
1153 * sync_completion: Signal that a synchronous command has completed
1154 * Note that after returning from this call, the evt_struct is freed.
1155 * the caller waiting on this completion shouldn't touch the evt_struct
1156 * again.
1157 */
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{
7603e02e 1173 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1da177e4
LT
1174 struct srp_tsk_mgmt *tsk_mgmt;
1175 struct srp_event_struct *evt;
1176 struct srp_event_struct *tmp_evt, *found_evt;
1177 union viosrp_iu srp_rsp;
1178 int rsp_rc;
be042f24 1179 unsigned long flags;
1da177e4 1180 u16 lun = lun_from_dev(cmd->device);
860784c8 1181 unsigned long wait_switch = 0;
1da177e4
LT
1182
1183 /* First, find this command in our sent list so we can figure
1184 * out the correct tag
1185 */
be042f24 1186 spin_lock_irqsave(hostdata->host->host_lock, flags);
860784c8
RJ
1187 wait_switch = jiffies + (init_timeout * HZ);
1188 do {
1189 found_evt = NULL;
1190 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1191 if (tmp_evt->cmnd == cmd) {
1192 found_evt = tmp_evt;
1193 break;
1194 }
1da177e4 1195 }
1da177e4 1196
860784c8
RJ
1197 if (!found_evt) {
1198 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1199 return SUCCESS;
1200 }
1da177e4 1201
860784c8
RJ
1202 evt = get_event_struct(&hostdata->pool);
1203 if (evt == NULL) {
1204 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1205 sdev_printk(KERN_ERR, cmd->device,
1206 "failed to allocate abort event\n");
1207 return FAILED;
1208 }
1da177e4 1209
860784c8
RJ
1210 init_event_struct(evt,
1211 sync_completion,
1212 VIOSRP_SRP_FORMAT,
e1a5ce5b 1213 abort_timeout);
1da177e4 1214
860784c8 1215 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1da177e4 1216
860784c8
RJ
1217 /* Set up an abort SRP command */
1218 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1219 tsk_mgmt->opcode = SRP_TSK_MGMT;
1220 tsk_mgmt->lun = ((u64) lun) << 48;
1221 tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
1222 tsk_mgmt->task_tag = (u64) found_evt;
1223
1224 evt->sync_srp = &srp_rsp;
1225
1226 init_completion(&evt->comp);
e1a5ce5b 1227 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
860784c8
RJ
1228
1229 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1230 break;
1231
1232 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1233 msleep(10);
1234 spin_lock_irqsave(hostdata->host->host_lock, flags);
1235 } while (time_before(jiffies, wait_switch));
1236
be042f24 1237 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
860784c8 1238
be042f24 1239 if (rsp_rc != 0) {
6c0a60ec
BK
1240 sdev_printk(KERN_ERR, cmd->device,
1241 "failed to send abort() event. rc=%d\n", rsp_rc);
1da177e4
LT
1242 return FAILED;
1243 }
1244
860784c8 1245 sdev_printk(KERN_INFO, cmd->device,
fe333321 1246 "aborting command. lun 0x%llx, tag 0x%llx\n",
860784c8
RJ
1247 (((u64) lun) << 48), (u64) found_evt);
1248
1da177e4 1249 wait_for_completion(&evt->comp);
1da177e4
LT
1250
1251 /* make sure we got a good response */
ef265673 1252 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1da177e4 1253 if (printk_ratelimit())
6c0a60ec
BK
1254 sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
1255 srp_rsp.srp.rsp.opcode);
1da177e4
LT
1256 return FAILED;
1257 }
1258
ef265673
FT
1259 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1260 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1da177e4
LT
1261 else
1262 rsp_rc = srp_rsp.srp.rsp.status;
1263
1264 if (rsp_rc) {
1265 if (printk_ratelimit())
6c0a60ec 1266 sdev_printk(KERN_WARNING, cmd->device,
fe333321 1267 "abort code %d for task tag 0x%llx\n",
6c0a60ec 1268 rsp_rc, tsk_mgmt->task_tag);
1da177e4
LT
1269 return FAILED;
1270 }
1271
1272 /* Because we dropped the spinlock above, it's possible
1273 * The event is no longer in our list. Make sure it didn't
1274 * complete while we were aborting
1275 */
be042f24 1276 spin_lock_irqsave(hostdata->host->host_lock, flags);
1da177e4
LT
1277 found_evt = NULL;
1278 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1279 if (tmp_evt->cmnd == cmd) {
1280 found_evt = tmp_evt;
1281 break;
1282 }
1283 }
1284
1285 if (found_evt == NULL) {
be042f24 1286 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
fe333321 1287 sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
6c0a60ec 1288 tsk_mgmt->task_tag);
1da177e4
LT
1289 return SUCCESS;
1290 }
1291
fe333321 1292 sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
6c0a60ec 1293 tsk_mgmt->task_tag);
1da177e4
LT
1294
1295 cmd->result = (DID_ABORT << 16);
1296 list_del(&found_evt->list);
4dddbc26
JB
1297 unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1298 found_evt->hostdata->dev);
1da177e4 1299 free_event_struct(&found_evt->hostdata->pool, found_evt);
be042f24 1300 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1da177e4
LT
1301 atomic_inc(&hostdata->request_limit);
1302 return SUCCESS;
1303}
1304
1305/**
1306 * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
1307 * template send this over to the server and wait synchronously for the
1308 * response
1309 */
1310static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1311{
7603e02e 1312 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1da177e4
LT
1313 struct srp_tsk_mgmt *tsk_mgmt;
1314 struct srp_event_struct *evt;
1315 struct srp_event_struct *tmp_evt, *pos;
1316 union viosrp_iu srp_rsp;
1317 int rsp_rc;
be042f24 1318 unsigned long flags;
1da177e4 1319 u16 lun = lun_from_dev(cmd->device);
860784c8 1320 unsigned long wait_switch = 0;
1da177e4 1321
be042f24 1322 spin_lock_irqsave(hostdata->host->host_lock, flags);
860784c8
RJ
1323 wait_switch = jiffies + (init_timeout * HZ);
1324 do {
1325 evt = get_event_struct(&hostdata->pool);
1326 if (evt == NULL) {
1327 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1328 sdev_printk(KERN_ERR, cmd->device,
1329 "failed to allocate reset event\n");
1330 return FAILED;
1331 }
1da177e4 1332
860784c8
RJ
1333 init_event_struct(evt,
1334 sync_completion,
1335 VIOSRP_SRP_FORMAT,
e1a5ce5b 1336 reset_timeout);
1da177e4 1337
860784c8 1338 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1da177e4 1339
860784c8
RJ
1340 /* Set up a lun reset SRP command */
1341 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1342 tsk_mgmt->opcode = SRP_TSK_MGMT;
1343 tsk_mgmt->lun = ((u64) lun) << 48;
1344 tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
1da177e4 1345
860784c8
RJ
1346 evt->sync_srp = &srp_rsp;
1347
1348 init_completion(&evt->comp);
e1a5ce5b 1349 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
860784c8
RJ
1350
1351 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1352 break;
1353
1354 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1355 msleep(10);
1356 spin_lock_irqsave(hostdata->host->host_lock, flags);
1357 } while (time_before(jiffies, wait_switch));
1da177e4 1358
be042f24 1359 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
860784c8 1360
be042f24 1361 if (rsp_rc != 0) {
6c0a60ec
BK
1362 sdev_printk(KERN_ERR, cmd->device,
1363 "failed to send reset event. rc=%d\n", rsp_rc);
1da177e4
LT
1364 return FAILED;
1365 }
1366
fe333321 1367 sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
860784c8
RJ
1368 (((u64) lun) << 48));
1369
1da177e4 1370 wait_for_completion(&evt->comp);
1da177e4
LT
1371
1372 /* make sure we got a good response */
ef265673 1373 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1da177e4 1374 if (printk_ratelimit())
6c0a60ec
BK
1375 sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
1376 srp_rsp.srp.rsp.opcode);
1da177e4
LT
1377 return FAILED;
1378 }
1379
ef265673
FT
1380 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1381 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1da177e4
LT
1382 else
1383 rsp_rc = srp_rsp.srp.rsp.status;
1384
1385 if (rsp_rc) {
1386 if (printk_ratelimit())
6c0a60ec 1387 sdev_printk(KERN_WARNING, cmd->device,
fe333321 1388 "reset code %d for task tag 0x%llx\n",
6c0a60ec 1389 rsp_rc, tsk_mgmt->task_tag);
1da177e4
LT
1390 return FAILED;
1391 }
1392
1393 /* We need to find all commands for this LUN that have not yet been
1394 * responded to, and fail them with DID_RESET
1395 */
be042f24 1396 spin_lock_irqsave(hostdata->host->host_lock, flags);
1da177e4
LT
1397 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1398 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1399 if (tmp_evt->cmnd)
1400 tmp_evt->cmnd->result = (DID_RESET << 16);
1401 list_del(&tmp_evt->list);
4dddbc26
JB
1402 unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1403 tmp_evt->hostdata->dev);
1da177e4
LT
1404 free_event_struct(&tmp_evt->hostdata->pool,
1405 tmp_evt);
1406 atomic_inc(&hostdata->request_limit);
1407 if (tmp_evt->cmnd_done)
1408 tmp_evt->cmnd_done(tmp_evt->cmnd);
1409 else if (tmp_evt->done)
1410 tmp_evt->done(tmp_evt);
1411 }
1412 }
be042f24 1413 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1da177e4
LT
1414 return SUCCESS;
1415}
1416
1417/**
3d0e91f7
BK
1418 * 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)
1da177e4 1422{
3d0e91f7 1423 unsigned long wait_switch = 0;
7603e02e 1424 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1da177e4 1425
3d0e91f7
BK
1426 dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
1427
1428 ibmvscsi_reset_host(hostdata);
1429
1430 for (wait_switch = jiffies + (init_timeout * HZ);
1431 time_before(jiffies, wait_switch) &&
1432 atomic_read(&hostdata->request_limit) < 2;) {
1433
1434 msleep(10);
1da177e4 1435 }
3d0e91f7
BK
1436
1437 if (atomic_read(&hostdata->request_limit) <= 0)
1438 return FAILED;
1439
1440 return SUCCESS;
1da177e4
LT
1441}
1442
1443/**
1444 * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1445 * @crq: Command/Response queue
1446 * @hostdata: ibmvscsi_host_data of host
1447 *
1448*/
1449void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1450 struct ibmvscsi_host_data *hostdata)
1451{
6c0a60ec 1452 long rc;
1da177e4
LT
1453 unsigned long flags;
1454 struct srp_event_struct *evt_struct =
1455 (struct srp_event_struct *)crq->IU_data_ptr;
1456 switch (crq->valid) {
1457 case 0xC0: /* initialization */
1458 switch (crq->format) {
1459 case 0x01: /* Initialization message */
6c0a60ec 1460 dev_info(hostdata->dev, "partner initialized\n");
1da177e4 1461 /* Send back a response */
d3849d51
DW
1462 if ((rc = ibmvscsi_ops->send_crq(hostdata,
1463 0xC002000000000000LL, 0)) == 0) {
1da177e4 1464 /* Now login */
3507e13f 1465 init_adapter(hostdata);
1da177e4 1466 } else {
6c0a60ec 1467 dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
1da177e4
LT
1468 }
1469
1470 break;
1471 case 0x02: /* Initialization response */
6c0a60ec 1472 dev_info(hostdata->dev, "partner initialization complete\n");
1da177e4
LT
1473
1474 /* Now login */
3507e13f 1475 init_adapter(hostdata);
1da177e4
LT
1476 break;
1477 default:
6c0a60ec 1478 dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
1da177e4
LT
1479 }
1480 return;
2b541f8f
DB
1481 case 0xFF: /* Hypervisor telling us the connection is closed */
1482 scsi_block_requests(hostdata->host);
cefbda2d 1483 atomic_set(&hostdata->request_limit, 0);
2b541f8f
DB
1484 if (crq->format == 0x06) {
1485 /* We need to re-setup the interpartition connection */
6c0a60ec 1486 dev_info(hostdata->dev, "Re-enabling adapter!\n");
126c5cc3 1487 hostdata->client_migrated = 1;
2b541f8f 1488 purge_requests(hostdata, DID_REQUEUE);
d3849d51
DW
1489 if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
1490 hostdata)) ||
1491 (ibmvscsi_ops->send_crq(hostdata,
1492 0xC001000000000000LL, 0))) {
cefbda2d
DB
1493 atomic_set(&hostdata->request_limit,
1494 -1);
6c0a60ec 1495 dev_err(hostdata->dev, "error after enable\n");
cefbda2d 1496 }
2b541f8f 1497 } else {
6c0a60ec
BK
1498 dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
1499 crq->format);
1da177e4 1500
2b541f8f 1501 purge_requests(hostdata, DID_ERROR);
d3849d51
DW
1502 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
1503 hostdata)) ||
1504 (ibmvscsi_ops->send_crq(hostdata,
1505 0xC001000000000000LL, 0))) {
cefbda2d
DB
1506 atomic_set(&hostdata->request_limit,
1507 -1);
6c0a60ec 1508 dev_err(hostdata->dev, "error after reset\n");
cefbda2d 1509 }
2b541f8f
DB
1510 }
1511 scsi_unblock_requests(hostdata->host);
1da177e4
LT
1512 return;
1513 case 0x80: /* real payload */
1514 break;
1515 default:
6c0a60ec
BK
1516 dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
1517 crq->valid);
1da177e4
LT
1518 return;
1519 }
1520
1521 /* The only kind of payload CRQs we should get are responses to
1522 * things we send. Make sure this response is to something we
1523 * actually sent
1524 */
1525 if (!valid_event_struct(&hostdata->pool, evt_struct)) {
6c0a60ec 1526 dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
1da177e4
LT
1527 (void *)crq->IU_data_ptr);
1528 return;
1529 }
1530
1531 if (atomic_read(&evt_struct->free)) {
6c0a60ec
BK
1532 dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
1533 (void *)crq->IU_data_ptr);
1da177e4
LT
1534 return;
1535 }
1536
1537 if (crq->format == VIOSRP_SRP_FORMAT)
ef265673 1538 atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
1da177e4
LT
1539 &hostdata->request_limit);
1540
3d0e91f7
BK
1541 del_timer(&evt_struct->timer);
1542
ca61668b 1543 if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
c3a3b55a 1544 evt_struct->cmnd->result = DID_ERROR << 16;
1da177e4
LT
1545 if (evt_struct->done)
1546 evt_struct->done(evt_struct);
1547 else
6c0a60ec 1548 dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
1da177e4
LT
1549
1550 /*
1551 * Lock the host_lock before messing with these structures, since we
1552 * are running in a task context
1553 */
1554 spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1555 list_del(&evt_struct->list);
1556 free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1557 spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1558}
1559
1560/**
1561 * ibmvscsi_get_host_config: Send the command to the server to get host
1562 * configuration data. The data is opaque to us.
1563 */
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;
06f923cb 1569 unsigned long flags;
e5dbfa66 1570 dma_addr_t addr;
1da177e4
LT
1571 int rc;
1572
1573 evt_struct = get_event_struct(&hostdata->pool);
1574 if (!evt_struct) {
6c0a60ec 1575 dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
1da177e4
LT
1576 return -1;
1577 }
1578
1579 init_event_struct(evt_struct,
1580 sync_completion,
1581 VIOSRP_MAD_FORMAT,
e1a5ce5b 1582 info_timeout);
1da177e4
LT
1583
1584 host_config = &evt_struct->iu.mad.host_config;
1585
1586 /* Set up a lun reset SRP command */
1587 memset(host_config, 0x00, sizeof(*host_config));
1588 host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1589 host_config->common.length = length;
e5dbfa66
FT
1590 host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
1591 length,
1592 DMA_BIDIRECTIONAL);
1da177e4 1593
8d8bb39b 1594 if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
7912a0ac
RJ
1595 if (!firmware_has_feature(FW_FEATURE_CMO))
1596 dev_err(hostdata->dev,
1597 "dma_mapping error getting host config\n");
1da177e4
LT
1598 free_event_struct(&hostdata->pool, evt_struct);
1599 return -1;
1600 }
1601
1602 init_completion(&evt_struct->comp);
06f923cb 1603 spin_lock_irqsave(hostdata->host->host_lock, flags);
e1a5ce5b 1604 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
06f923cb 1605 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
e5dbfa66 1606 if (rc == 0)
1da177e4 1607 wait_for_completion(&evt_struct->comp);
e5dbfa66 1608 dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
1da177e4
LT
1609
1610 return rc;
1611}
1612
0979c84b
RJ
1613/**
1614 * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
1615 * @sdev: struct scsi_device device to configure
1616 *
1617 * Enable allow_restart for a device if it is a disk. Adjust the
1618 * queue_depth here also as is required by the documentation for
1619 * struct scsi_host_template.
1620 */
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);
d1a357fc 1627 if (sdev->type == TYPE_DISK) {
0979c84b 1628 sdev->allow_restart = 1;
e1a5ce5b 1629 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
d1a357fc 1630 }
0979c84b
RJ
1631 scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
1632 spin_unlock_irqrestore(shost->host_lock, lock_flags);
1633 return 0;
1634}
1635
742d25b8
BK
1636/**
1637 * ibmvscsi_change_queue_depth - Change the device's queue depth
1638 * @sdev: scsi device struct
1639 * @qdepth: depth to set
e881a172 1640 * @reason: calling context
742d25b8
BK
1641 *
1642 * Return value:
1643 * actual depth set
1644 **/
e881a172
MC
1645static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
1646 int reason)
742d25b8 1647{
e881a172
MC
1648 if (reason != SCSI_QDEPTH_DEFAULT)
1649 return -EOPNOTSUPP;
1650
742d25b8
BK
1651 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
1da177e4
LT
1658/* ------------------------------------------------------------
1659 * sysfs attributes
1660 */
126c5cc3
BK
1661static 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
ee959b00
TJ
1701static ssize_t show_host_srp_version(struct device *dev,
1702 struct device_attribute *attr, char *buf)
1da177e4 1703{
ee959b00 1704 struct Scsi_Host *shost = class_to_shost(dev);
7603e02e 1705 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1da177e4
LT
1706 int len;
1707
1708 len = snprintf(buf, PAGE_SIZE, "%s\n",
1709 hostdata->madapter_info.srp_version);
1710 return len;
1711}
1712
ee959b00 1713static struct device_attribute ibmvscsi_host_srp_version = {
1da177e4
LT
1714 .attr = {
1715 .name = "srp_version",
1716 .mode = S_IRUGO,
1717 },
1718 .show = show_host_srp_version,
1719};
1720
ee959b00
TJ
1721static ssize_t show_host_partition_name(struct device *dev,
1722 struct device_attribute *attr,
1da177e4
LT
1723 char *buf)
1724{
ee959b00 1725 struct Scsi_Host *shost = class_to_shost(dev);
7603e02e 1726 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1da177e4
LT
1727 int len;
1728
1729 len = snprintf(buf, PAGE_SIZE, "%s\n",
1730 hostdata->madapter_info.partition_name);
1731 return len;
1732}
1733
ee959b00 1734static struct device_attribute ibmvscsi_host_partition_name = {
1da177e4
LT
1735 .attr = {
1736 .name = "partition_name",
1737 .mode = S_IRUGO,
1738 },
1739 .show = show_host_partition_name,
1740};
1741
ee959b00
TJ
1742static ssize_t show_host_partition_number(struct device *dev,
1743 struct device_attribute *attr,
1da177e4
LT
1744 char *buf)
1745{
ee959b00 1746 struct Scsi_Host *shost = class_to_shost(dev);
7603e02e 1747 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1da177e4
LT
1748 int len;
1749
1750 len = snprintf(buf, PAGE_SIZE, "%d\n",
1751 hostdata->madapter_info.partition_number);
1752 return len;
1753}
1754
ee959b00 1755static struct device_attribute ibmvscsi_host_partition_number = {
1da177e4
LT
1756 .attr = {
1757 .name = "partition_number",
1758 .mode = S_IRUGO,
1759 },
1760 .show = show_host_partition_number,
1761};
1762
ee959b00
TJ
1763static ssize_t show_host_mad_version(struct device *dev,
1764 struct device_attribute *attr, char *buf)
1da177e4 1765{
ee959b00 1766 struct Scsi_Host *shost = class_to_shost(dev);
7603e02e 1767 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1da177e4
LT
1768 int len;
1769
1770 len = snprintf(buf, PAGE_SIZE, "%d\n",
1771 hostdata->madapter_info.mad_version);
1772 return len;
1773}
1774
ee959b00 1775static struct device_attribute ibmvscsi_host_mad_version = {
1da177e4
LT
1776 .attr = {
1777 .name = "mad_version",
1778 .mode = S_IRUGO,
1779 },
1780 .show = show_host_mad_version,
1781};
1782
ee959b00
TJ
1783static ssize_t show_host_os_type(struct device *dev,
1784 struct device_attribute *attr, char *buf)
1da177e4 1785{
ee959b00 1786 struct Scsi_Host *shost = class_to_shost(dev);
7603e02e 1787 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1da177e4
LT
1788 int len;
1789
1790 len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
1791 return len;
1792}
1793
ee959b00 1794static struct device_attribute ibmvscsi_host_os_type = {
1da177e4
LT
1795 .attr = {
1796 .name = "os_type",
1797 .mode = S_IRUGO,
1798 },
1799 .show = show_host_os_type,
1800};
1801
ee959b00
TJ
1802static ssize_t show_host_config(struct device *dev,
1803 struct device_attribute *attr, char *buf)
1da177e4 1804{
ee959b00 1805 struct Scsi_Host *shost = class_to_shost(dev);
7603e02e 1806 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1da177e4
LT
1807
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
ee959b00 1815static struct device_attribute ibmvscsi_host_config = {
1da177e4
LT
1816 .attr = {
1817 .name = "config",
1818 .mode = S_IRUGO,
1819 },
1820 .show = show_host_config,
1821};
1822
ee959b00 1823static struct device_attribute *ibmvscsi_attrs[] = {
126c5cc3
BK
1824 &ibmvscsi_host_vhost_loc,
1825 &ibmvscsi_host_vhost_name,
1da177e4
LT
1826 &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,
3d0e91f7 1845 .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
0979c84b 1846 .slave_configure = ibmvscsi_slave_configure,
742d25b8 1847 .change_queue_depth = ibmvscsi_change_queue_depth,
7912a0ac 1848 .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
a897ff2a 1849 .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
1da177e4 1850 .this_id = -1,
4dddbc26 1851 .sg_tablesize = SG_ALL,
1da177e4
LT
1852 .use_clustering = ENABLE_CLUSTERING,
1853 .shost_attrs = ibmvscsi_attrs,
1854};
1855
7912a0ac
RJ
1856/**
1857 * 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 */
4f10aae0 1867 unsigned long desired_io = max_events * sizeof(union viosrp_iu);
7912a0ac
RJ
1868
1869 /* add io space for sg data */
004dd5e8 1870 desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
7912a0ac
RJ
1871 IBMVSCSI_CMDS_PER_LUN_DEFAULT);
1872
1873 return desired_io;
1874}
1875
1da177e4
LT
1876/**
1877 * 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;
4d680419
FT
1884 struct srp_rport_identifiers ids;
1885 struct srp_rport *rport;
1da177e4 1886 unsigned long wait_switch = 0;
cefbda2d 1887 int rc;
1da177e4 1888
559fde70 1889 dev_set_drvdata(&vdev->dev, NULL);
1da177e4
LT
1890
1891 host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
1892 if (!host) {
6c0a60ec 1893 dev_err(&vdev->dev, "couldn't allocate host data\n");
1da177e4
LT
1894 goto scsi_host_alloc_failed;
1895 }
1896
4d680419 1897 host->transportt = ibmvscsi_transport_template;
7603e02e 1898 hostdata = shost_priv(host);
1da177e4
LT
1899 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);
7912a0ac 1904 hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
1da177e4 1905
126c5cc3
BK
1906 if (map_persist_bufs(hostdata)) {
1907 dev_err(&vdev->dev, "couldn't map persistent buffers\n");
1908 goto persist_bufs_failed;
1909 }
1910
4f10aae0 1911 rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
cefbda2d 1912 if (rc != 0 && rc != H_RESOURCE) {
6c0a60ec 1913 dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
1da177e4
LT
1914 goto init_crq_failed;
1915 }
4f10aae0 1916 if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
6c0a60ec 1917 dev_err(&vdev->dev, "couldn't initialize event pool\n");
1da177e4
LT
1918 goto init_pool_failed;
1919 }
1920
1921 host->max_lun = 8;
1922 host->max_id = max_id;
1923 host->max_channel = max_channel;
fbc56f08 1924 host->max_cmd_len = 16;
1da177e4
LT
1925
1926 if (scsi_add_host(hostdata->host, hostdata->dev))
1927 goto add_host_failed;
1928
4d680419
FT
1929 /* 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));
aebd5e47 1932 ids.roles = SRP_RPORT_ROLE_TARGET;
4d680419
FT
1933 rport = srp_rport_add(host, &ids);
1934 if (IS_ERR(rport))
1935 goto add_srp_port_failed;
1936
1da177e4
LT
1937 /* 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 */
d3849d51 1941 if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
cefbda2d 1942 || rc == H_RESOURCE) {
1da177e4
LT
1943 /*
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
559fde70 1961 dev_set_drvdata(&vdev->dev, hostdata);
1da177e4
LT
1962 return 0;
1963
4d680419
FT
1964 add_srp_port_failed:
1965 scsi_remove_host(hostdata->host);
1da177e4
LT
1966 add_host_failed:
1967 release_event_pool(&hostdata->pool, hostdata);
1968 init_pool_failed:
4f10aae0 1969 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
1da177e4 1970 init_crq_failed:
126c5cc3
BK
1971 unmap_persist_bufs(hostdata);
1972 persist_bufs_failed:
1da177e4
LT
1973 scsi_host_put(host);
1974 scsi_host_alloc_failed:
1975 return -1;
1976}
1977
1978static int ibmvscsi_remove(struct vio_dev *vdev)
1979{
559fde70 1980 struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
126c5cc3 1981 unmap_persist_bufs(hostdata);
1da177e4 1982 release_event_pool(&hostdata->pool, hostdata);
d3849d51 1983 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
4f10aae0 1984 max_events);
4d680419
FT
1985
1986 srp_remove_host(hostdata->host);
1da177e4
LT
1987 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"},
fb120da6 1999 { "", "" }
1da177e4 2000};
1da177e4 2001MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
915124d8 2002
1da177e4 2003static struct vio_driver ibmvscsi_driver = {
1da177e4
LT
2004 .id_table = ibmvscsi_device_table,
2005 .probe = ibmvscsi_probe,
6fdf5392 2006 .remove = ibmvscsi_remove,
7912a0ac 2007 .get_desired_dma = ibmvscsi_get_desired_dma,
6fdf5392
SR
2008 .driver = {
2009 .name = "ibmvscsi",
915124d8 2010 .owner = THIS_MODULE,
6fdf5392 2011 }
1da177e4
LT
2012};
2013
4d680419
FT
2014static struct srp_function_template ibmvscsi_transport_functions = {
2015};
2016
1da177e4
LT
2017int __init ibmvscsi_module_init(void)
2018{
4d680419
FT
2019 int ret;
2020
4f10aae0
BK
2021 /* Ensure we have two requests to do error recovery */
2022 driver_template.can_queue = max_requests;
2023 max_events = max_requests + 2;
2024
d3849d51
DW
2025 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
4d680419
FT
2032 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;
1da177e4
LT
2041}
2042
2043void __exit ibmvscsi_module_exit(void)
2044{
2045 vio_unregister_driver(&ibmvscsi_driver);
4d680419 2046 srp_release_transport(ibmvscsi_transport_template);
1da177e4
LT
2047}
2048
2049module_init(ibmvscsi_module_init);
2050module_exit(ibmvscsi_module_exit);