blob: adfef9db6f1e2c279002315abc3d936adc622027 [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
Bart Van Assche9b7dac02009-12-04 20:43:37 +010043 * puts the message in the next 16 byte space in round-robin fashion,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 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 *
Stephen Rothwell78347992012-03-07 18:35:38 +000058 * TODO: This is currently pretty tied to the IBM pSeries hypervisor
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * interfaces. It would be really nice to abstract this above an RDMA
60 * layer.
61 */
62
63#include <linux/module.h>
64#include <linux/moduleparam.h>
65#include <linux/dma-mapping.h>
66#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Brian King126c5cc2009-06-08 16:19:08 -050068#include <linux/of.h>
Brian King64355b92010-02-21 10:37:57 -060069#include <linux/pm.h>
Brian King0f33ece2010-06-17 13:56:00 -050070#include <linux/kthread.h>
David Woodhoused3849d52007-09-22 08:29:36 +100071#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/vio.h>
73#include <scsi/scsi.h>
74#include <scsi/scsi_cmnd.h>
75#include <scsi/scsi_host.h>
76#include <scsi/scsi_device.h>
FUJITA Tomonori4d680412007-06-27 16:32:50 +090077#include <scsi/scsi_transport_srp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include "ibmvscsi.h"
79
80/* The values below are somewhat arbitrary default values, but
81 * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
82 * Note that there are 3 bits of channel value, 6 bits of id, and
83 * 5 bits of LUN.
84 */
85static int max_id = 64;
86static int max_channel = 3;
Robert Jenningse1a5ce52009-06-08 16:19:03 -050087static int init_timeout = 300;
88static int login_timeout = 60;
89static int info_timeout = 30;
90static int abort_timeout = 60;
91static int reset_timeout = 60;
Robert Jenningsa897ff22007-03-28 12:45:46 -050092static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
Brian King4f10aae2008-12-17 17:19:33 -060093static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
Robert Jenningsc1988e32009-06-08 16:19:07 -050094static int fast_fail = 1;
Brian King126c5cc2009-06-08 16:19:08 -050095static int client_reserve = 1;
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +100096static char partition_name[97] = "UNKNOWN";
97static unsigned int partition_number = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
FUJITA Tomonori4d680412007-06-27 16:32:50 +090099static struct scsi_transport_template *ibmvscsi_transport_template;
100
Brian Kingaac31182010-06-17 13:56:04 -0500101#define IBMVSCSI_VERSION "1.5.9"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103MODULE_DESCRIPTION("IBM Virtual SCSI");
104MODULE_AUTHOR("Dave Boutcher");
105MODULE_LICENSE("GPL");
106MODULE_VERSION(IBMVSCSI_VERSION);
107
108module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
Laurent Viviere3678a02015-11-09 17:49:08 +0100109MODULE_PARM_DESC(max_id, "Largest ID value for each channel [Default=64]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
Laurent Viviere3678a02015-11-09 17:49:08 +0100111MODULE_PARM_DESC(max_channel, "Largest channel value [Default=3]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
113MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
Brian King21465ed2008-12-08 17:01:47 -0600114module_param_named(max_requests, max_requests, int, S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
Robert Jenningsc1988e32009-06-08 16:19:07 -0500116module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
117MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
Brian King126c5cc2009-06-08 16:19:08 -0500118module_param_named(client_reserve, client_reserve, int, S_IRUGO );
119MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +1000121static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
122 struct ibmvscsi_host_data *hostdata);
123
124/* ------------------------------------------------------------
125 * Routines for managing the command/response queue
126 */
127/**
128 * ibmvscsi_handle_event: - Interrupt handler for crq events
129 * @irq: number of irq to handle, not used
130 * @dev_instance: ibmvscsi_host_data of host that received interrupt
131 *
132 * Disables interrupts and schedules srp_task
133 * Always returns IRQ_HANDLED
134 */
135static irqreturn_t ibmvscsi_handle_event(int irq, void *dev_instance)
136{
137 struct ibmvscsi_host_data *hostdata =
138 (struct ibmvscsi_host_data *)dev_instance;
139 vio_disable_interrupts(to_vio_dev(hostdata->dev));
140 tasklet_schedule(&hostdata->srp_task);
141 return IRQ_HANDLED;
142}
143
144/**
145 * release_crq_queue: - Deallocates data and unregisters CRQ
146 * @queue: crq_queue to initialize and register
147 * @host_data: ibmvscsi_host_data of host
148 *
149 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
150 * the crq with the hypervisor.
151 */
152static void ibmvscsi_release_crq_queue(struct crq_queue *queue,
153 struct ibmvscsi_host_data *hostdata,
154 int max_requests)
155{
156 long rc = 0;
157 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
158 free_irq(vdev->irq, (void *)hostdata);
159 tasklet_kill(&hostdata->srp_task);
160 do {
161 if (rc)
162 msleep(100);
163 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
164 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
165 dma_unmap_single(hostdata->dev,
166 queue->msg_token,
167 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
168 free_page((unsigned long)queue->msgs);
169}
170
171/**
172 * crq_queue_next_crq: - Returns the next entry in message queue
173 * @queue: crq_queue to use
174 *
175 * Returns pointer to next entry in queue, or NULL if there are no new
176 * entried in the CRQ.
177 */
178static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
179{
180 struct viosrp_crq *crq;
181 unsigned long flags;
182
183 spin_lock_irqsave(&queue->lock, flags);
184 crq = &queue->msgs[queue->cur];
185 if (crq->valid & 0x80) {
186 if (++queue->cur == queue->size)
187 queue->cur = 0;
Brian King7114aae2014-05-23 10:52:11 -0500188
189 /* Ensure the read of the valid bit occurs before reading any
190 * other bits of the CRQ entry
191 */
192 rmb();
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +1000193 } else
194 crq = NULL;
195 spin_unlock_irqrestore(&queue->lock, flags);
196
197 return crq;
198}
199
200/**
201 * ibmvscsi_send_crq: - Send a CRQ
202 * @hostdata: the adapter
203 * @word1: the first 64 bits of the data
204 * @word2: the second 64 bits of the data
205 */
206static int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata,
207 u64 word1, u64 word2)
208{
209 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
210
Brian King7114aae2014-05-23 10:52:11 -0500211 /*
212 * Ensure the command buffer is flushed to memory before handing it
213 * over to the VIOS to prevent it from fetching any stale data.
214 */
215 mb();
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +1000216 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
217}
218
219/**
220 * ibmvscsi_task: - Process srps asynchronously
221 * @data: ibmvscsi_host_data of host
222 */
223static void ibmvscsi_task(void *data)
224{
225 struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
226 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
227 struct viosrp_crq *crq;
228 int done = 0;
229
230 while (!done) {
231 /* Pull all the valid messages off the CRQ */
232 while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
233 ibmvscsi_handle_crq(crq, hostdata);
234 crq->valid = 0x00;
235 }
236
237 vio_enable_interrupts(vdev);
238 crq = crq_queue_next_crq(&hostdata->queue);
239 if (crq != NULL) {
240 vio_disable_interrupts(vdev);
241 ibmvscsi_handle_crq(crq, hostdata);
242 crq->valid = 0x00;
243 } else {
244 done = 1;
245 }
246 }
247}
248
249static void gather_partition_info(void)
250{
251 struct device_node *rootdn;
252
253 const char *ppartition_name;
Anton Blanchard72264eb2013-09-03 10:04:47 +1000254 const __be32 *p_number_ptr;
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +1000255
256 /* Retrieve information about this partition */
257 rootdn = of_find_node_by_path("/");
258 if (!rootdn) {
259 return;
260 }
261
262 ppartition_name = of_get_property(rootdn, "ibm,partition-name", NULL);
263 if (ppartition_name)
264 strncpy(partition_name, ppartition_name,
265 sizeof(partition_name));
266 p_number_ptr = of_get_property(rootdn, "ibm,partition-no", NULL);
267 if (p_number_ptr)
Anton Blanchard72264eb2013-09-03 10:04:47 +1000268 partition_number = of_read_number(p_number_ptr, 1);
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +1000269 of_node_put(rootdn);
270}
271
272static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
273{
274 memset(&hostdata->madapter_info, 0x00,
275 sizeof(hostdata->madapter_info));
276
277 dev_info(hostdata->dev, "SRP_VERSION: %s\n", SRP_VERSION);
278 strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
279
280 strncpy(hostdata->madapter_info.partition_name, partition_name,
281 sizeof(hostdata->madapter_info.partition_name));
282
Anton Blanchard72264eb2013-09-03 10:04:47 +1000283 hostdata->madapter_info.partition_number =
284 cpu_to_be32(partition_number);
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +1000285
Anton Blanchard72264eb2013-09-03 10:04:47 +1000286 hostdata->madapter_info.mad_version = cpu_to_be32(1);
287 hostdata->madapter_info.os_type = cpu_to_be32(2);
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +1000288}
289
290/**
291 * reset_crq_queue: - resets a crq after a failure
292 * @queue: crq_queue to initialize and register
293 * @hostdata: ibmvscsi_host_data of host
294 *
295 */
296static int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
297 struct ibmvscsi_host_data *hostdata)
298{
299 int rc = 0;
300 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
301
302 /* Close the CRQ */
303 do {
304 if (rc)
305 msleep(100);
306 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
307 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
308
309 /* Clean out the queue */
310 memset(queue->msgs, 0x00, PAGE_SIZE);
311 queue->cur = 0;
312
313 set_adapter_info(hostdata);
314
315 /* And re-open it again */
316 rc = plpar_hcall_norets(H_REG_CRQ,
317 vdev->unit_address,
318 queue->msg_token, PAGE_SIZE);
319 if (rc == 2) {
320 /* Adapter is good, but other end is not ready */
321 dev_warn(hostdata->dev, "Partner adapter not ready\n");
322 } else if (rc != 0) {
323 dev_warn(hostdata->dev, "couldn't register crq--rc 0x%x\n", rc);
324 }
325 return rc;
326}
327
328/**
329 * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
330 * @queue: crq_queue to initialize and register
331 * @hostdata: ibmvscsi_host_data of host
332 *
333 * Allocates a page for messages, maps it for dma, and registers
334 * the crq with the hypervisor.
335 * Returns zero on success.
336 */
337static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
338 struct ibmvscsi_host_data *hostdata,
339 int max_requests)
340{
341 int rc;
342 int retrc;
343 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
344
345 queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
346
347 if (!queue->msgs)
348 goto malloc_failed;
349 queue->size = PAGE_SIZE / sizeof(*queue->msgs);
350
351 queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
352 queue->size * sizeof(*queue->msgs),
353 DMA_BIDIRECTIONAL);
354
355 if (dma_mapping_error(hostdata->dev, queue->msg_token))
356 goto map_failed;
357
358 gather_partition_info();
359 set_adapter_info(hostdata);
360
361 retrc = rc = plpar_hcall_norets(H_REG_CRQ,
362 vdev->unit_address,
363 queue->msg_token, PAGE_SIZE);
364 if (rc == H_RESOURCE)
365 /* maybe kexecing and resource is busy. try a reset */
366 rc = ibmvscsi_reset_crq_queue(queue,
367 hostdata);
368
369 if (rc == 2) {
370 /* Adapter is good, but other end is not ready */
371 dev_warn(hostdata->dev, "Partner adapter not ready\n");
372 retrc = 0;
373 } else if (rc != 0) {
374 dev_warn(hostdata->dev, "Error %d opening adapter\n", rc);
375 goto reg_crq_failed;
376 }
377
378 queue->cur = 0;
379 spin_lock_init(&queue->lock);
380
381 tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
382 (unsigned long)hostdata);
383
384 if (request_irq(vdev->irq,
385 ibmvscsi_handle_event,
386 0, "ibmvscsi", (void *)hostdata) != 0) {
387 dev_err(hostdata->dev, "couldn't register irq 0x%x\n",
388 vdev->irq);
389 goto req_irq_failed;
390 }
391
392 rc = vio_enable_interrupts(vdev);
393 if (rc != 0) {
394 dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc);
395 goto req_irq_failed;
396 }
397
398 return retrc;
399
400 req_irq_failed:
401 tasklet_kill(&hostdata->srp_task);
402 rc = 0;
403 do {
404 if (rc)
405 msleep(100);
406 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
407 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
408 reg_crq_failed:
409 dma_unmap_single(hostdata->dev,
410 queue->msg_token,
411 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
412 map_failed:
413 free_page((unsigned long)queue->msgs);
414 malloc_failed:
415 return -1;
416}
417
418/**
419 * reenable_crq_queue: - reenables a crq after
420 * @queue: crq_queue to initialize and register
421 * @hostdata: ibmvscsi_host_data of host
422 *
423 */
424static int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
425 struct ibmvscsi_host_data *hostdata)
426{
427 int rc = 0;
428 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
429
430 /* Re-enable the CRQ */
431 do {
432 if (rc)
433 msleep(100);
434 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
435 } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
436
437 if (rc)
438 dev_err(hostdata->dev, "Error %d enabling adapter\n", rc);
439 return rc;
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/* ------------------------------------------------------------
443 * Routines for the event pool and event structs
444 */
445/**
446 * initialize_event_pool: - Allocates and initializes the event pool for a host
447 * @pool: event_pool to be initialized
448 * @size: Number of events in pool
449 * @hostdata: ibmvscsi_host_data who owns the event pool
450 *
451 * Returns zero on success.
452*/
453static int initialize_event_pool(struct event_pool *pool,
454 int size, struct ibmvscsi_host_data *hostdata)
455{
456 int i;
457
458 pool->size = size;
459 pool->next = 0;
FUJITA Tomonori4c021dd132006-04-07 19:10:03 +0900460 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!pool->events)
462 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 pool->iu_storage =
465 dma_alloc_coherent(hostdata->dev,
466 pool->size * sizeof(*pool->iu_storage),
467 &pool->iu_token, 0);
468 if (!pool->iu_storage) {
469 kfree(pool->events);
470 return -ENOMEM;
471 }
472
473 for (i = 0; i < pool->size; ++i) {
474 struct srp_event_struct *evt = &pool->events[i];
475 memset(&evt->crq, 0x00, sizeof(evt->crq));
476 atomic_set(&evt->free, 1);
477 evt->crq.valid = 0x80;
Anton Blanchard72264eb2013-09-03 10:04:47 +1000478 evt->crq.IU_length = cpu_to_be16(sizeof(*evt->xfer_iu));
479 evt->crq.IU_data_ptr = cpu_to_be64(pool->iu_token +
480 sizeof(*evt->xfer_iu) * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 evt->xfer_iu = pool->iu_storage + i;
482 evt->hostdata = hostdata;
James Bottomley4dddbc22005-09-06 17:11:54 -0500483 evt->ext_list = NULL;
484 evt->ext_list_token = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 return 0;
488}
489
490/**
491 * release_event_pool: - Frees memory of an event pool of a host
492 * @pool: event_pool to be released
493 * @hostdata: ibmvscsi_host_data who owns the even pool
494 *
495 * Returns zero on success.
496*/
497static void release_event_pool(struct event_pool *pool,
498 struct ibmvscsi_host_data *hostdata)
499{
500 int i, in_use = 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500501 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (atomic_read(&pool->events[i].free) != 1)
503 ++in_use;
James Bottomley4dddbc22005-09-06 17:11:54 -0500504 if (pool->events[i].ext_list) {
505 dma_free_coherent(hostdata->dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900506 SG_ALL * sizeof(struct srp_direct_buf),
James Bottomley4dddbc22005-09-06 17:11:54 -0500507 pool->events[i].ext_list,
508 pool->events[i].ext_list_token);
509 }
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (in_use)
Brian King6c0a60e2007-06-13 17:12:19 -0500512 dev_warn(hostdata->dev, "releasing event pool with %d "
513 "events still in use?\n", in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 kfree(pool->events);
515 dma_free_coherent(hostdata->dev,
516 pool->size * sizeof(*pool->iu_storage),
517 pool->iu_storage, pool->iu_token);
518}
519
520/**
521 * valid_event_struct: - Determines if event is valid.
522 * @pool: event_pool that contains the event
523 * @evt: srp_event_struct to be checked for validity
524 *
525 * Returns zero if event is invalid, one otherwise.
526*/
527static int valid_event_struct(struct event_pool *pool,
528 struct srp_event_struct *evt)
529{
530 int index = evt - pool->events;
531 if (index < 0 || index >= pool->size) /* outside of bounds */
532 return 0;
533 if (evt != pool->events + index) /* unaligned */
534 return 0;
535 return 1;
536}
537
538/**
539 * ibmvscsi_free-event_struct: - Changes status of event to "free"
540 * @pool: event_pool that contains the event
541 * @evt: srp_event_struct to be modified
542 *
543*/
544static void free_event_struct(struct event_pool *pool,
545 struct srp_event_struct *evt)
546{
547 if (!valid_event_struct(pool, evt)) {
Brian King6c0a60e2007-06-13 17:12:19 -0500548 dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
549 "(not in pool %p)\n", evt, pool->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return;
551 }
552 if (atomic_inc_return(&evt->free) != 1) {
Brian King6c0a60e2007-06-13 17:12:19 -0500553 dev_err(evt->hostdata->dev, "Freeing event_struct %p "
554 "which is not in use!\n", evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return;
556 }
557}
558
559/**
560 * get_evt_struct: - Gets the next free event in pool
561 * @pool: event_pool that contains the events to be searched
562 *
563 * Returns the next event in "free" state, and NULL if none are free.
564 * Note that no synchronization is done here, we assume the host_lock
565 * will syncrhonze things.
566*/
567static struct srp_event_struct *get_event_struct(struct event_pool *pool)
568{
569 int i;
570 int poolsize = pool->size;
571 int offset = pool->next;
572
573 for (i = 0; i < poolsize; i++) {
574 offset = (offset + 1) % poolsize;
575 if (!atomic_dec_if_positive(&pool->events[offset].free)) {
576 pool->next = offset;
577 return &pool->events[offset];
578 }
579 }
580
581 printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
582 return NULL;
583}
584
585/**
586 * init_event_struct: Initialize fields in an event struct that are always
587 * required.
588 * @evt: The event
589 * @done: Routine to call when the event is responded to
590 * @format: SRP or MAD format
591 * @timeout: timeout value set in the CRQ
592 */
593static void init_event_struct(struct srp_event_struct *evt_struct,
594 void (*done) (struct srp_event_struct *),
595 u8 format,
596 int timeout)
597{
598 evt_struct->cmnd = NULL;
599 evt_struct->cmnd_done = NULL;
600 evt_struct->sync_srp = NULL;
601 evt_struct->crq.format = format;
Anton Blanchard72264eb2013-09-03 10:04:47 +1000602 evt_struct->crq.timeout = cpu_to_be16(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 evt_struct->done = done;
604}
605
606/* ------------------------------------------------------------
607 * Routines for receiving SCSI responses from the hosting partition
608 */
609
610/**
611 * set_srp_direction: Set the fields in the srp related to data
612 * direction and number of buffers based on the direction in
613 * the scsi_cmnd and the number of buffers
614 */
615static void set_srp_direction(struct scsi_cmnd *cmd,
616 struct srp_cmd *srp_cmd,
617 int numbuf)
618{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900619 u8 fmt;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (numbuf == 0)
622 return;
623
FUJITA Tomonorief265672006-03-26 03:57:14 +0900624 if (numbuf == 1)
625 fmt = SRP_DATA_DESC_DIRECT;
626 else {
627 fmt = SRP_DATA_DESC_INDIRECT;
628 numbuf = min(numbuf, MAX_INDIRECT_BUFS);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (cmd->sc_data_direction == DMA_TO_DEVICE)
FUJITA Tomonorief265672006-03-26 03:57:14 +0900631 srp_cmd->data_out_desc_cnt = numbuf;
632 else
633 srp_cmd->data_in_desc_cnt = numbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
FUJITA Tomonorief265672006-03-26 03:57:14 +0900635
636 if (cmd->sc_data_direction == DMA_TO_DEVICE)
637 srp_cmd->buf_fmt = fmt << 4;
638 else
639 srp_cmd->buf_fmt = fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/**
643 * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
644 * @cmd: srp_cmd whose additional_data member will be unmapped
645 * @dev: device for which the memory is mapped
646 *
647*/
James Bottomley4dddbc22005-09-06 17:11:54 -0500648static void unmap_cmd_data(struct srp_cmd *cmd,
649 struct srp_event_struct *evt_struct,
650 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
FUJITA Tomonorief265672006-03-26 03:57:14 +0900652 u8 out_fmt, in_fmt;
653
654 out_fmt = cmd->buf_fmt >> 4;
655 in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
656
657 if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return;
James Bottomley4dddbc22005-09-06 17:11:54 -0500659
FUJITA Tomonoria71fa1f2010-04-02 15:50:24 +0900660 if (evt_struct->cmnd)
661 scsi_dma_unmap(evt_struct->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900664static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900665 struct srp_direct_buf *md)
James Bottomley4dddbc22005-09-06 17:11:54 -0500666{
667 int i;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900668 struct scatterlist *sg;
James Bottomley4dddbc22005-09-06 17:11:54 -0500669 u64 total_length = 0;
670
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900671 scsi_for_each_sg(cmd, sg, nseg, i) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900672 struct srp_direct_buf *descr = md + i;
Anton Blanchard72264eb2013-09-03 10:04:47 +1000673 descr->va = cpu_to_be64(sg_dma_address(sg));
674 descr->len = cpu_to_be32(sg_dma_len(sg));
FUJITA Tomonorief265672006-03-26 03:57:14 +0900675 descr->key = 0;
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900676 total_length += sg_dma_len(sg);
James Bottomley4dddbc22005-09-06 17:11:54 -0500677 }
678 return total_length;
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/**
682 * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
683 * @cmd: Scsi_Cmnd with the scatterlist
684 * @srp_cmd: srp_cmd that contains the memory descriptor
685 * @dev: device for which to map dma memory
686 *
687 * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
688 * Returns 1 on success.
689*/
690static int map_sg_data(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500691 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct srp_cmd *srp_cmd, struct device *dev)
693{
694
James Bottomley4dddbc22005-09-06 17:11:54 -0500695 int sg_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 u64 total_length = 0;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900697 struct srp_direct_buf *data =
698 (struct srp_direct_buf *) srp_cmd->add_data;
699 struct srp_indirect_buf *indirect =
700 (struct srp_indirect_buf *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900702 sg_mapped = scsi_dma_map(cmd);
703 if (!sg_mapped)
704 return 1;
705 else if (sg_mapped < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707
708 set_srp_direction(cmd, srp_cmd, sg_mapped);
709
710 /* special case; we can use a single direct descriptor */
711 if (sg_mapped == 1) {
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900712 map_sg_list(cmd, sg_mapped, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 1;
714 }
715
FUJITA Tomonorief265672006-03-26 03:57:14 +0900716 indirect->table_desc.va = 0;
Anton Blanchard72264eb2013-09-03 10:04:47 +1000717 indirect->table_desc.len = cpu_to_be32(sg_mapped *
718 sizeof(struct srp_direct_buf));
FUJITA Tomonorief265672006-03-26 03:57:14 +0900719 indirect->table_desc.key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
James Bottomley4dddbc22005-09-06 17:11:54 -0500721 if (sg_mapped <= MAX_INDIRECT_BUFS) {
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900722 total_length = map_sg_list(cmd, sg_mapped,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900723 &indirect->desc_list[0]);
Anton Blanchard72264eb2013-09-03 10:04:47 +1000724 indirect->len = cpu_to_be32(total_length);
James Bottomley4dddbc22005-09-06 17:11:54 -0500725 return 1;
726 }
727
728 /* get indirect table */
729 if (!evt_struct->ext_list) {
FUJITA Tomonorief265672006-03-26 03:57:14 +0900730 evt_struct->ext_list = (struct srp_direct_buf *)
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900731 dma_alloc_coherent(dev,
FUJITA Tomonorief265672006-03-26 03:57:14 +0900732 SG_ALL * sizeof(struct srp_direct_buf),
733 &evt_struct->ext_list_token, 0);
James Bottomley4dddbc22005-09-06 17:11:54 -0500734 if (!evt_struct->ext_list) {
Robert Jennings7912a0a2008-07-24 04:35:27 +1000735 if (!firmware_has_feature(FW_FEATURE_CMO))
736 sdev_printk(KERN_ERR, cmd->device,
737 "Can't allocate memory "
738 "for indirect table\n");
Robert Jenningse637d552009-01-22 13:40:09 -0600739 scsi_dma_unmap(cmd);
James Bottomley4dddbc22005-09-06 17:11:54 -0500740 return 0;
James Bottomley4dddbc22005-09-06 17:11:54 -0500741 }
742 }
743
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900744 total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
James Bottomley4dddbc22005-09-06 17:11:54 -0500745
Anton Blanchard72264eb2013-09-03 10:04:47 +1000746 indirect->len = cpu_to_be32(total_length);
747 indirect->table_desc.va = cpu_to_be64(evt_struct->ext_list_token);
748 indirect->table_desc.len = cpu_to_be32(sg_mapped *
749 sizeof(indirect->desc_list[0]));
FUJITA Tomonorief265672006-03-26 03:57:14 +0900750 memcpy(indirect->desc_list, evt_struct->ext_list,
751 MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
James Bottomley4dddbc22005-09-06 17:11:54 -0500752 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
757 * @cmd: struct scsi_cmnd with the memory to be mapped
758 * @srp_cmd: srp_cmd that contains the memory descriptor
759 * @dev: dma device for which to map dma memory
760 *
761 * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
762 * Returns 1 on success.
763*/
764static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -0500765 struct srp_event_struct *evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct srp_cmd *srp_cmd, struct device *dev)
767{
768 switch (cmd->sc_data_direction) {
769 case DMA_FROM_DEVICE:
770 case DMA_TO_DEVICE:
771 break;
772 case DMA_NONE:
773 return 1;
774 case DMA_BIDIRECTIONAL:
Brian King6c0a60e2007-06-13 17:12:19 -0500775 sdev_printk(KERN_ERR, cmd->device,
776 "Can't map DMA_BIDIRECTIONAL to read/write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778 default:
Brian King6c0a60e2007-06-13 17:12:19 -0500779 sdev_printk(KERN_ERR, cmd->device,
780 "Unknown data direction 0x%02x; can't map!\n",
781 cmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783 }
784
FUJITA Tomonori9413d7b2007-05-26 00:32:58 +0900785 return map_sg_data(cmd, evt_struct, srp_cmd, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Brian King3d0e91f2007-06-13 17:12:26 -0500788/**
789 * purge_requests: Our virtual adapter just shut down. purge any sent requests
790 * @hostdata: the adapter
791 */
792static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
793{
Brian King1117ef82010-06-17 13:56:02 -0500794 struct srp_event_struct *evt;
Brian King3d0e91f2007-06-13 17:12:26 -0500795 unsigned long flags;
796
797 spin_lock_irqsave(hostdata->host->host_lock, flags);
Brian King1117ef82010-06-17 13:56:02 -0500798 while (!list_empty(&hostdata->sent)) {
799 evt = list_first_entry(&hostdata->sent, struct srp_event_struct, list);
800 list_del(&evt->list);
801 del_timer(&evt->timer);
802
803 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
804 if (evt->cmnd) {
805 evt->cmnd->result = (error_code << 16);
806 unmap_cmd_data(&evt->iu.srp.cmd, evt,
807 evt->hostdata->dev);
808 if (evt->cmnd_done)
809 evt->cmnd_done(evt->cmnd);
Brian King9ee75592014-05-23 10:52:10 -0500810 } else if (evt->done && evt->crq.format != VIOSRP_MAD_FORMAT &&
811 evt->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
Brian King1117ef82010-06-17 13:56:02 -0500812 evt->done(evt);
813 free_event_struct(&evt->hostdata->pool, evt);
814 spin_lock_irqsave(hostdata->host->host_lock, flags);
Brian King3d0e91f2007-06-13 17:12:26 -0500815 }
816 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
817}
818
819/**
820 * ibmvscsi_reset_host - Reset the connection to the server
821 * @hostdata: struct ibmvscsi_host_data to reset
822*/
823static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
824{
825 scsi_block_requests(hostdata->host);
826 atomic_set(&hostdata->request_limit, 0);
827
828 purge_requests(hostdata, DID_ERROR);
Brian King0f33ece2010-06-17 13:56:00 -0500829 hostdata->reset_crq = 1;
830 wake_up(&hostdata->work_wait_q);
Brian King3d0e91f2007-06-13 17:12:26 -0500831}
832
833/**
834 * ibmvscsi_timeout - Internal command timeout handler
835 * @evt_struct: struct srp_event_struct that timed out
836 *
837 * Called when an internally generated command times out
838*/
839static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
840{
841 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
842
843 dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
844 evt_struct->iu.srp.cmd.opcode);
845
846 ibmvscsi_reset_host(hostdata);
847}
848
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/* ------------------------------------------------------------
851 * Routines for sending and receiving SRPs
852 */
853/**
854 * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
855 * @evt_struct: evt_struct to be sent
856 * @hostdata: ibmvscsi_host_data of host
Brian King3d0e91f2007-06-13 17:12:26 -0500857 * @timeout: timeout in seconds - 0 means do not time command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *
859 * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
860 * Note that this routine assumes that host_lock is held for synchronization
861*/
862static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
Brian King3d0e91f2007-06-13 17:12:26 -0500863 struct ibmvscsi_host_data *hostdata,
864 unsigned long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Anton Blanchard72264eb2013-09-03 10:04:47 +1000866 __be64 *crq_as_u64 = (__be64 *)&evt_struct->crq;
Robert Jennings3c887e82007-10-30 11:37:07 -0500867 int request_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int rc;
Brian Kingf3a9c4d2010-06-17 13:56:03 -0500869 int srp_req = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Robert Jenningsa897ff22007-03-28 12:45:46 -0500871 /* If we have exhausted our request limit, just fail this request,
872 * unless it is for a reset or abort.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * Note that there are rare cases involving driver generated requests
874 * (such as task management requests) that the mid layer may think we
875 * can handle more requests (can_queue) when we actually can't
876 */
Dave C Boutchercefbda22006-06-12 21:22:51 -0500877 if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
Brian Kingf3a9c4d2010-06-17 13:56:03 -0500878 srp_req = 1;
Dave C Boutchercefbda22006-06-12 21:22:51 -0500879 request_status =
880 atomic_dec_if_positive(&hostdata->request_limit);
881 /* If request limit was -1 when we started, it is now even
882 * less than that
883 */
884 if (request_status < -1)
885 goto send_error;
Robert Jenningsa897ff22007-03-28 12:45:46 -0500886 /* Otherwise, we may have run out of requests. */
Robert Jennings3c887e82007-10-30 11:37:07 -0500887 /* If request limit was 0 when we started the adapter is in the
888 * process of performing a login with the server adapter, or
889 * we may have run out of requests.
890 */
891 else if (request_status == -1 &&
892 evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
893 goto send_busy;
Robert Jenningsa897ff22007-03-28 12:45:46 -0500894 /* Abort and reset calls should make it through.
895 * Nothing except abort and reset should use the last two
896 * slots unless we had two or less to begin with.
897 */
898 else if (request_status < 2 &&
899 evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
900 /* In the case that we have less than two requests
901 * available, check the server limit as a combination
902 * of the request limit and the number of requests
903 * in-flight (the size of the send list). If the
904 * server limit is greater than 2, return busy so
905 * that the last two are reserved for reset and abort.
906 */
907 int server_limit = request_status;
908 struct srp_event_struct *tmp_evt;
909
910 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
911 server_limit++;
912 }
913
914 if (server_limit > 2)
915 goto send_busy;
916 }
Dave C Boutchercefbda22006-06-12 21:22:51 -0500917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* Copy the IU into the transfer area */
920 *evt_struct->xfer_iu = evt_struct->iu;
FUJITA Tomonorief265672006-03-26 03:57:14 +0900921 evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Add this to the sent list. We need to do this
924 * before we actually send
925 * in case it comes back REALLY fast
926 */
927 list_add_tail(&evt_struct->list, &hostdata->sent);
928
Brian King3d0e91f2007-06-13 17:12:26 -0500929 init_timer(&evt_struct->timer);
930 if (timeout) {
931 evt_struct->timer.data = (unsigned long) evt_struct;
932 evt_struct->timer.expires = jiffies + (timeout * HZ);
933 evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
934 add_timer(&evt_struct->timer);
935 }
936
Anton Blanchard72264eb2013-09-03 10:04:47 +1000937 rc = ibmvscsi_send_crq(hostdata, be64_to_cpu(crq_as_u64[0]),
938 be64_to_cpu(crq_as_u64[1]));
939 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 list_del(&evt_struct->list);
Brian King3d0e91f2007-06-13 17:12:26 -0500941 del_timer(&evt_struct->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Robert Jennings860784c2007-11-12 09:00:23 -0600943 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
944 * Firmware will send a CRQ with a transport event (0xFF) to
945 * tell this client what has happened to the transport. This
946 * will be handled in ibmvscsi_handle_crq()
947 */
948 if (rc == H_CLOSED) {
949 dev_warn(hostdata->dev, "send warning. "
950 "Receive queue closed, will retry.\n");
951 goto send_busy;
952 }
Brian King6c0a60e2007-06-13 17:12:19 -0500953 dev_err(hostdata->dev, "send error %d\n", rc);
Brian Kingf3a9c4d2010-06-17 13:56:03 -0500954 if (srp_req)
955 atomic_inc(&hostdata->request_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto send_error;
957 }
958
959 return 0;
960
Dave C Boutchercefbda22006-06-12 21:22:51 -0500961 send_busy:
James Bottomley4dddbc22005-09-06 17:11:54 -0500962 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 free_event_struct(&hostdata->pool, evt_struct);
Brian Kingf3a9c4d2010-06-17 13:56:03 -0500965 if (srp_req && request_status != -1)
Robert Jennings3c887e82007-10-30 11:37:07 -0500966 atomic_inc(&hostdata->request_limit);
Robert Jenningsa897ff22007-03-28 12:45:46 -0500967 return SCSI_MLQUEUE_HOST_BUSY;
Dave C Boutchercefbda22006-06-12 21:22:51 -0500968
969 send_error:
970 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
971
972 if (evt_struct->cmnd != NULL) {
973 evt_struct->cmnd->result = DID_ERROR << 16;
974 evt_struct->cmnd_done(evt_struct->cmnd);
975 } else if (evt_struct->done)
976 evt_struct->done(evt_struct);
977
978 free_event_struct(&hostdata->pool, evt_struct);
979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/**
983 * handle_cmd_rsp: - Handle responses from commands
984 * @evt_struct: srp_event_struct to be handled
985 *
986 * Used as a callback by when sending scsi cmds.
987 * Gets called by ibmvscsi_handle_crq()
988*/
989static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
990{
991 struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
992 struct scsi_cmnd *cmnd = evt_struct->cmnd;
993
FUJITA Tomonorief265672006-03-26 03:57:14 +0900994 if (unlikely(rsp->opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -0500996 dev_warn(evt_struct->hostdata->dev,
997 "bad SRP RSP type %d\n", rsp->opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
1000 if (cmnd) {
Brian Kingc3a3b552008-04-25 16:58:29 -05001001 cmnd->result |= rsp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
1003 memcpy(cmnd->sense_buffer,
FUJITA Tomonorief265672006-03-26 03:57:14 +09001004 rsp->data,
Anton Blanchard72264eb2013-09-03 10:04:47 +10001005 be32_to_cpu(rsp->sense_data_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unmap_cmd_data(&evt_struct->iu.srp.cmd,
James Bottomley4dddbc22005-09-06 17:11:54 -05001007 evt_struct,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 evt_struct->hostdata->dev);
1009
FUJITA Tomonorief265672006-03-26 03:57:14 +09001010 if (rsp->flags & SRP_RSP_FLAG_DOOVER)
Anton Blanchard72264eb2013-09-03 10:04:47 +10001011 scsi_set_resid(cmnd,
1012 be32_to_cpu(rsp->data_out_res_cnt));
FUJITA Tomonorief265672006-03-26 03:57:14 +09001013 else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
Anton Blanchard72264eb2013-09-03 10:04:47 +10001014 scsi_set_resid(cmnd, be32_to_cpu(rsp->data_in_res_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
1017 if (evt_struct->cmnd_done)
1018 evt_struct->cmnd_done(cmnd);
1019}
1020
1021/**
1022 * lun_from_dev: - Returns the lun of the scsi device
1023 * @dev: struct scsi_device
1024 *
1025*/
1026static inline u16 lun_from_dev(struct scsi_device *dev)
1027{
1028 return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
1029}
1030
1031/**
1032 * ibmvscsi_queue: - The queuecommand function of the scsi template
1033 * @cmd: struct scsi_cmnd to be executed
1034 * @done: Callback function to be called when cmd is completed
1035*/
Jeff Garzikf2812332010-11-16 02:10:29 -05001036static int ibmvscsi_queuecommand_lck(struct scsi_cmnd *cmnd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 void (*done) (struct scsi_cmnd *))
1038{
1039 struct srp_cmd *srp_cmd;
1040 struct srp_event_struct *evt_struct;
FUJITA Tomonorief265672006-03-26 03:57:14 +09001041 struct srp_indirect_buf *indirect;
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001042 struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 u16 lun = lun_from_dev(cmnd->device);
FUJITA Tomonorief265672006-03-26 03:57:14 +09001044 u8 out_fmt, in_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Brian Kingc3a3b552008-04-25 16:58:29 -05001046 cmnd->result = (DID_OK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 evt_struct = get_event_struct(&hostdata->pool);
1048 if (!evt_struct)
1049 return SCSI_MLQUEUE_HOST_BUSY;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* Set up the actual SRP IU */
1052 srp_cmd = &evt_struct->iu.srp.cmd;
FUJITA Tomonorief265672006-03-26 03:57:14 +09001053 memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
1054 srp_cmd->opcode = SRP_CMD;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001055 memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
Bart Van Assche985aa492015-05-18 13:27:14 +02001056 int_to_scsilun(lun, &srp_cmd->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
James Bottomley4dddbc22005-09-06 17:11:54 -05001058 if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
Robert Jennings7912a0a2008-07-24 04:35:27 +10001059 if (!firmware_has_feature(FW_FEATURE_CMO))
1060 sdev_printk(KERN_ERR, cmnd->device,
1061 "couldn't convert cmd to srp_cmd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 free_event_struct(&hostdata->pool, evt_struct);
1063 return SCSI_MLQUEUE_HOST_BUSY;
1064 }
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 init_event_struct(evt_struct,
1067 handle_cmd_rsp,
1068 VIOSRP_SRP_FORMAT,
Jens Axboe242f9dc2008-09-14 05:55:09 -07001069 cmnd->request->timeout/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 evt_struct->cmnd = cmnd;
1072 evt_struct->cmnd_done = done;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Fix up dma address of the buffer itself */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001075 indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
1076 out_fmt = srp_cmd->buf_fmt >> 4;
1077 in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
1078 if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
1079 out_fmt == SRP_DATA_DESC_INDIRECT) &&
1080 indirect->table_desc.va == 0) {
Anton Blanchard72264eb2013-09-03 10:04:47 +10001081 indirect->table_desc.va =
1082 cpu_to_be64(be64_to_cpu(evt_struct->crq.IU_data_ptr) +
FUJITA Tomonorief265672006-03-26 03:57:14 +09001083 offsetof(struct srp_cmd, add_data) +
Anton Blanchard72264eb2013-09-03 10:04:47 +10001084 offsetof(struct srp_indirect_buf, desc_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
Brian King3d0e91f2007-06-13 17:12:26 -05001087 return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Jeff Garzikf2812332010-11-16 02:10:29 -05001090static DEF_SCSI_QCMD(ibmvscsi_queuecommand)
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/* ------------------------------------------------------------
1093 * Routines for driver initialization
1094 */
Brian King3507e132009-06-08 16:19:04 -05001095
1096/**
Brian King126c5cc2009-06-08 16:19:08 -05001097 * map_persist_bufs: - Pre-map persistent data for adapter logins
1098 * @hostdata: ibmvscsi_host_data of host
1099 *
1100 * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
1101 * Return 1 on error, 0 on success.
1102 */
1103static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
1104{
1105
1106 hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
1107 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
1108
1109 if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
1110 dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
1111 return 1;
1112 }
1113
1114 hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
1115 &hostdata->madapter_info,
1116 sizeof(hostdata->madapter_info),
1117 DMA_BIDIRECTIONAL);
1118 if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
1119 dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
1120 dma_unmap_single(hostdata->dev, hostdata->caps_addr,
1121 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
1122 return 1;
1123 }
1124
1125 return 0;
1126}
1127
1128/**
1129 * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
1130 * @hostdata: ibmvscsi_host_data of host
1131 *
1132 * Unmap the capabilities and adapter info DMA buffers
1133 */
1134static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
1135{
1136 dma_unmap_single(hostdata->dev, hostdata->caps_addr,
1137 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
1138
1139 dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
1140 sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
1141}
1142
1143/**
Brian King3507e132009-06-08 16:19:04 -05001144 * login_rsp: - Handle response to SRP login request
1145 * @evt_struct: srp_event_struct with the response
1146 *
1147 * Used as a "done" callback by when sending srp_login. Gets called
1148 * by ibmvscsi_handle_crq()
1149*/
1150static void login_rsp(struct srp_event_struct *evt_struct)
1151{
1152 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1153 switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
1154 case SRP_LOGIN_RSP: /* it worked! */
1155 break;
1156 case SRP_LOGIN_REJ: /* refused! */
1157 dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
1158 evt_struct->xfer_iu->srp.login_rej.reason);
1159 /* Login failed. */
1160 atomic_set(&hostdata->request_limit, -1);
1161 return;
1162 default:
1163 dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
1164 evt_struct->xfer_iu->srp.login_rsp.opcode);
1165 /* Login failed. */
1166 atomic_set(&hostdata->request_limit, -1);
1167 return;
1168 }
1169
1170 dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
Brian King126c5cc2009-06-08 16:19:08 -05001171 hostdata->client_migrated = 0;
Brian King3507e132009-06-08 16:19:04 -05001172
1173 /* Now we know what the real request-limit is.
1174 * This value is set rather than added to request_limit because
1175 * request_limit could have been set to -1 by this client.
1176 */
1177 atomic_set(&hostdata->request_limit,
Anton Blanchard72264eb2013-09-03 10:04:47 +10001178 be32_to_cpu(evt_struct->xfer_iu->srp.login_rsp.req_lim_delta));
Brian King3507e132009-06-08 16:19:04 -05001179
1180 /* If we had any pending I/Os, kick them */
1181 scsi_unblock_requests(hostdata->host);
1182}
1183
1184/**
1185 * send_srp_login: - Sends the srp login
1186 * @hostdata: ibmvscsi_host_data of host
1187 *
1188 * Returns zero if successful.
1189*/
1190static int send_srp_login(struct ibmvscsi_host_data *hostdata)
1191{
1192 int rc;
1193 unsigned long flags;
1194 struct srp_login_req *login;
1195 struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
1196
1197 BUG_ON(!evt_struct);
1198 init_event_struct(evt_struct, login_rsp,
1199 VIOSRP_SRP_FORMAT, login_timeout);
1200
1201 login = &evt_struct->iu.srp.login_req;
1202 memset(login, 0, sizeof(*login));
1203 login->opcode = SRP_LOGIN_REQ;
Anton Blanchard72264eb2013-09-03 10:04:47 +10001204 login->req_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
1205 login->req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
1206 SRP_BUF_FORMAT_INDIRECT);
Brian King3507e132009-06-08 16:19:04 -05001207
1208 spin_lock_irqsave(hostdata->host->host_lock, flags);
1209 /* Start out with a request limit of 0, since this is negotiated in
1210 * the login request we are just sending and login requests always
1211 * get sent by the driver regardless of request_limit.
1212 */
1213 atomic_set(&hostdata->request_limit, 0);
1214
1215 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
1216 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1217 dev_info(hostdata->dev, "sent SRP login\n");
1218 return rc;
1219};
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/**
Brian King126c5cc2009-06-08 16:19:08 -05001222 * capabilities_rsp: - Handle response to MAD adapter capabilities request
1223 * @evt_struct: srp_event_struct with the response
1224 *
1225 * Used as a "done" callback by when sending adapter_info.
1226 */
1227static void capabilities_rsp(struct srp_event_struct *evt_struct)
1228{
1229 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1230
1231 if (evt_struct->xfer_iu->mad.capabilities.common.status) {
1232 dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
1233 evt_struct->xfer_iu->mad.capabilities.common.status);
1234 } else {
Anton Blanchard72264eb2013-09-03 10:04:47 +10001235 if (hostdata->caps.migration.common.server_support !=
1236 cpu_to_be16(SERVER_SUPPORTS_CAP))
Brian King126c5cc2009-06-08 16:19:08 -05001237 dev_info(hostdata->dev, "Partition migration not supported\n");
1238
1239 if (client_reserve) {
1240 if (hostdata->caps.reserve.common.server_support ==
Anton Blanchard72264eb2013-09-03 10:04:47 +10001241 cpu_to_be16(SERVER_SUPPORTS_CAP))
Brian King126c5cc2009-06-08 16:19:08 -05001242 dev_info(hostdata->dev, "Client reserve enabled\n");
1243 else
1244 dev_info(hostdata->dev, "Client reserve not supported\n");
1245 }
1246 }
1247
1248 send_srp_login(hostdata);
1249}
1250
1251/**
1252 * send_mad_capabilities: - Sends the mad capabilities request
1253 * and stores the result so it can be retrieved with
1254 * @hostdata: ibmvscsi_host_data of host
1255 */
1256static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
1257{
1258 struct viosrp_capabilities *req;
1259 struct srp_event_struct *evt_struct;
1260 unsigned long flags;
Grant Likely61c7a082010-04-13 16:12:29 -07001261 struct device_node *of_node = hostdata->dev->of_node;
Brian King126c5cc2009-06-08 16:19:08 -05001262 const char *location;
1263
1264 evt_struct = get_event_struct(&hostdata->pool);
1265 BUG_ON(!evt_struct);
1266
1267 init_event_struct(evt_struct, capabilities_rsp,
1268 VIOSRP_MAD_FORMAT, info_timeout);
1269
1270 req = &evt_struct->iu.mad.capabilities;
1271 memset(req, 0, sizeof(*req));
1272
Anton Blanchard72264eb2013-09-03 10:04:47 +10001273 hostdata->caps.flags = cpu_to_be32(CAP_LIST_SUPPORTED);
Brian King126c5cc2009-06-08 16:19:08 -05001274 if (hostdata->client_migrated)
Anton Blanchard72264eb2013-09-03 10:04:47 +10001275 hostdata->caps.flags |= cpu_to_be32(CLIENT_MIGRATED);
Brian King126c5cc2009-06-08 16:19:08 -05001276
1277 strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
1278 sizeof(hostdata->caps.name));
1279 hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
1280
1281 location = of_get_property(of_node, "ibm,loc-code", NULL);
1282 location = location ? location : dev_name(hostdata->dev);
1283 strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
1284 hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
1285
Anton Blanchard72264eb2013-09-03 10:04:47 +10001286 req->common.type = cpu_to_be32(VIOSRP_CAPABILITIES_TYPE);
1287 req->buffer = cpu_to_be64(hostdata->caps_addr);
Brian King126c5cc2009-06-08 16:19:08 -05001288
Anton Blanchard72264eb2013-09-03 10:04:47 +10001289 hostdata->caps.migration.common.cap_type =
1290 cpu_to_be32(MIGRATION_CAPABILITIES);
1291 hostdata->caps.migration.common.length =
1292 cpu_to_be16(sizeof(hostdata->caps.migration));
1293 hostdata->caps.migration.common.server_support =
1294 cpu_to_be16(SERVER_SUPPORTS_CAP);
1295 hostdata->caps.migration.ecl = cpu_to_be32(1);
Brian King126c5cc2009-06-08 16:19:08 -05001296
1297 if (client_reserve) {
Anton Blanchard72264eb2013-09-03 10:04:47 +10001298 hostdata->caps.reserve.common.cap_type =
1299 cpu_to_be32(RESERVATION_CAPABILITIES);
1300 hostdata->caps.reserve.common.length =
1301 cpu_to_be16(sizeof(hostdata->caps.reserve));
1302 hostdata->caps.reserve.common.server_support =
1303 cpu_to_be16(SERVER_SUPPORTS_CAP);
1304 hostdata->caps.reserve.type =
1305 cpu_to_be32(CLIENT_RESERVE_SCSI_2);
1306 req->common.length =
1307 cpu_to_be16(sizeof(hostdata->caps));
Brian King126c5cc2009-06-08 16:19:08 -05001308 } else
Anton Blanchard72264eb2013-09-03 10:04:47 +10001309 req->common.length = cpu_to_be16(sizeof(hostdata->caps) -
1310 sizeof(hostdata->caps.reserve));
Brian King126c5cc2009-06-08 16:19:08 -05001311
1312 spin_lock_irqsave(hostdata->host->host_lock, flags);
1313 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1314 dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
1315 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1316};
1317
1318/**
Robert Jenningsc1988e32009-06-08 16:19:07 -05001319 * fast_fail_rsp: - Handle response to MAD enable fast fail
1320 * @evt_struct: srp_event_struct with the response
1321 *
1322 * Used as a "done" callback by when sending enable fast fail. Gets called
1323 * by ibmvscsi_handle_crq()
1324 */
1325static void fast_fail_rsp(struct srp_event_struct *evt_struct)
1326{
1327 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
Anton Blanchard72264eb2013-09-03 10:04:47 +10001328 u16 status = be16_to_cpu(evt_struct->xfer_iu->mad.fast_fail.common.status);
Robert Jenningsc1988e32009-06-08 16:19:07 -05001329
1330 if (status == VIOSRP_MAD_NOT_SUPPORTED)
1331 dev_err(hostdata->dev, "fast_fail not supported in server\n");
1332 else if (status == VIOSRP_MAD_FAILED)
1333 dev_err(hostdata->dev, "fast_fail request failed\n");
1334 else if (status != VIOSRP_MAD_SUCCESS)
1335 dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
1336
Brian King126c5cc2009-06-08 16:19:08 -05001337 send_mad_capabilities(hostdata);
Robert Jenningsc1988e32009-06-08 16:19:07 -05001338}
1339
1340/**
1341 * init_host - Start host initialization
1342 * @hostdata: ibmvscsi_host_data of host
1343 *
1344 * Returns zero if successful.
1345 */
1346static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
1347{
1348 int rc;
1349 unsigned long flags;
1350 struct viosrp_fast_fail *fast_fail_mad;
1351 struct srp_event_struct *evt_struct;
1352
Brian King126c5cc2009-06-08 16:19:08 -05001353 if (!fast_fail) {
1354 send_mad_capabilities(hostdata);
1355 return 0;
1356 }
Robert Jenningsc1988e32009-06-08 16:19:07 -05001357
1358 evt_struct = get_event_struct(&hostdata->pool);
1359 BUG_ON(!evt_struct);
1360
1361 init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
1362
1363 fast_fail_mad = &evt_struct->iu.mad.fast_fail;
1364 memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
Anton Blanchard72264eb2013-09-03 10:04:47 +10001365 fast_fail_mad->common.type = cpu_to_be32(VIOSRP_ENABLE_FAST_FAIL);
1366 fast_fail_mad->common.length = cpu_to_be16(sizeof(*fast_fail_mad));
Robert Jenningsc1988e32009-06-08 16:19:07 -05001367
1368 spin_lock_irqsave(hostdata->host->host_lock, flags);
1369 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1370 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1371 return rc;
1372}
1373
1374/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * adapter_info_rsp: - Handle response to MAD adapter info request
1376 * @evt_struct: srp_event_struct with the response
1377 *
1378 * Used as a "done" callback by when sending adapter_info. Gets called
1379 * by ibmvscsi_handle_crq()
1380*/
1381static void adapter_info_rsp(struct srp_event_struct *evt_struct)
1382{
1383 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
Brian King6c0a60e2007-06-13 17:12:19 -05001386 dev_err(hostdata->dev, "error %d getting adapter info\n",
1387 evt_struct->xfer_iu->mad.adapter_info.common.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001389 dev_info(hostdata->dev, "host srp version: %s, "
1390 "host partition %s (%d), OS %d, max io %u\n",
1391 hostdata->madapter_info.srp_version,
1392 hostdata->madapter_info.partition_name,
Anton Blanchard72264eb2013-09-03 10:04:47 +10001393 be32_to_cpu(hostdata->madapter_info.partition_number),
1394 be32_to_cpu(hostdata->madapter_info.os_type),
1395 be32_to_cpu(hostdata->madapter_info.port_max_txu[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if (hostdata->madapter_info.port_max_txu[0])
1398 hostdata->host->max_sectors =
Anton Blanchard72264eb2013-09-03 10:04:47 +10001399 be32_to_cpu(hostdata->madapter_info.port_max_txu[0]) >> 9;
Dave C Boutcher154fb612005-09-13 10:09:02 -05001400
Anton Blanchard72264eb2013-09-03 10:04:47 +10001401 if (be32_to_cpu(hostdata->madapter_info.os_type) == 3 &&
Dave C Boutcher154fb612005-09-13 10:09:02 -05001402 strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001403 dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
1404 hostdata->madapter_info.srp_version);
1405 dev_err(hostdata->dev, "limiting scatterlists to %d\n",
1406 MAX_INDIRECT_BUFS);
Dave C Boutcher154fb612005-09-13 10:09:02 -05001407 hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
1408 }
Brian Kinge08afeb2009-06-23 17:14:01 -05001409
Anton Blanchard72264eb2013-09-03 10:04:47 +10001410 if (be32_to_cpu(hostdata->madapter_info.os_type) == 3) {
Brian Kinge08afeb2009-06-23 17:14:01 -05001411 enable_fast_fail(hostdata);
1412 return;
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Brian King3507e132009-06-08 16:19:04 -05001415
Brian Kinge08afeb2009-06-23 17:14:01 -05001416 send_srp_login(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/**
1420 * send_mad_adapter_info: - Sends the mad adapter info request
1421 * and stores the result so it can be retrieved with
1422 * sysfs. We COULD consider causing a failure if the
1423 * returned SRP version doesn't match ours.
1424 * @hostdata: ibmvscsi_host_data of host
1425 *
1426 * Returns zero if successful.
1427*/
1428static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
1429{
1430 struct viosrp_adapter_info *req;
1431 struct srp_event_struct *evt_struct;
Brian King06f923c2007-06-13 17:12:33 -05001432 unsigned long flags;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 evt_struct = get_event_struct(&hostdata->pool);
Brian King3507e132009-06-08 16:19:04 -05001435 BUG_ON(!evt_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 init_event_struct(evt_struct,
1438 adapter_info_rsp,
1439 VIOSRP_MAD_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001440 info_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 req = &evt_struct->iu.mad.adapter_info;
1443 memset(req, 0x00, sizeof(*req));
1444
Anton Blanchard72264eb2013-09-03 10:04:47 +10001445 req->common.type = cpu_to_be32(VIOSRP_ADAPTER_INFO_TYPE);
1446 req->common.length = cpu_to_be16(sizeof(hostdata->madapter_info));
1447 req->buffer = cpu_to_be64(hostdata->adapter_info_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Brian King06f923c2007-06-13 17:12:33 -05001449 spin_lock_irqsave(hostdata->host->host_lock, flags);
Brian King126c5cc2009-06-08 16:19:08 -05001450 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
Brian King6c0a60e2007-06-13 17:12:19 -05001451 dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
Brian King06f923c2007-06-13 17:12:33 -05001452 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453};
1454
1455/**
Brian King3507e132009-06-08 16:19:04 -05001456 * init_adapter: Start virtual adapter initialization sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
Brian King3507e132009-06-08 16:19:04 -05001458 */
1459static void init_adapter(struct ibmvscsi_host_data *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 send_mad_adapter_info(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 * sync_completion: Signal that a synchronous command has completed
1466 * Note that after returning from this call, the evt_struct is freed.
1467 * the caller waiting on this completion shouldn't touch the evt_struct
1468 * again.
1469 */
1470static void sync_completion(struct srp_event_struct *evt_struct)
1471{
1472 /* copy the response back */
1473 if (evt_struct->sync_srp)
1474 *evt_struct->sync_srp = *evt_struct->xfer_iu;
1475
1476 complete(&evt_struct->comp);
1477}
1478
1479/**
1480 * ibmvscsi_abort: Abort a command...from scsi host template
1481 * send this over to the server and wait synchronously for the response
1482 */
1483static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
1484{
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001485 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct srp_tsk_mgmt *tsk_mgmt;
1487 struct srp_event_struct *evt;
1488 struct srp_event_struct *tmp_evt, *found_evt;
1489 union viosrp_iu srp_rsp;
1490 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001491 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 u16 lun = lun_from_dev(cmd->device);
Robert Jennings860784c2007-11-12 09:00:23 -06001493 unsigned long wait_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /* First, find this command in our sent list so we can figure
1496 * out the correct tag
1497 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001498 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001499 wait_switch = jiffies + (init_timeout * HZ);
1500 do {
1501 found_evt = NULL;
1502 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1503 if (tmp_evt->cmnd == cmd) {
1504 found_evt = tmp_evt;
1505 break;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Robert Jennings860784c2007-11-12 09:00:23 -06001509 if (!found_evt) {
1510 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1511 return SUCCESS;
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Robert Jennings860784c2007-11-12 09:00:23 -06001514 evt = get_event_struct(&hostdata->pool);
1515 if (evt == NULL) {
1516 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1517 sdev_printk(KERN_ERR, cmd->device,
1518 "failed to allocate abort event\n");
1519 return FAILED;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Robert Jennings860784c2007-11-12 09:00:23 -06001522 init_event_struct(evt,
1523 sync_completion,
1524 VIOSRP_SRP_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001525 abort_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Robert Jennings860784c2007-11-12 09:00:23 -06001527 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Robert Jennings860784c2007-11-12 09:00:23 -06001529 /* Set up an abort SRP command */
1530 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1531 tsk_mgmt->opcode = SRP_TSK_MGMT;
Bart Van Assche985aa492015-05-18 13:27:14 +02001532 int_to_scsilun(lun, &tsk_mgmt->lun);
Robert Jennings860784c2007-11-12 09:00:23 -06001533 tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
1534 tsk_mgmt->task_tag = (u64) found_evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Robert Jennings860784c2007-11-12 09:00:23 -06001536 evt->sync_srp = &srp_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Robert Jennings860784c2007-11-12 09:00:23 -06001538 init_completion(&evt->comp);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001539 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
Robert Jennings860784c2007-11-12 09:00:23 -06001540
1541 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1542 break;
1543
1544 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1545 msleep(10);
1546 spin_lock_irqsave(hostdata->host->host_lock, flags);
1547 } while (time_before(jiffies, wait_switch));
1548
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001549 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001550
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001551 if (rsp_rc != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001552 sdev_printk(KERN_ERR, cmd->device,
1553 "failed to send abort() event. rc=%d\n", rsp_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return FAILED;
1555 }
1556
Robert Jennings860784c2007-11-12 09:00:23 -06001557 sdev_printk(KERN_INFO, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001558 "aborting command. lun 0x%llx, tag 0x%llx\n",
Robert Jennings860784c2007-11-12 09:00:23 -06001559 (((u64) lun) << 48), (u64) found_evt);
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001564 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001566 sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
1567 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return FAILED;
1569 }
1570
FUJITA Tomonorief265672006-03-26 03:57:14 +09001571 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1572 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 else
1574 rsp_rc = srp_rsp.srp.rsp.status;
1575
1576 if (rsp_rc) {
1577 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001578 sdev_printk(KERN_WARNING, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001579 "abort code %d for task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001580 rsp_rc, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return FAILED;
1582 }
1583
1584 /* Because we dropped the spinlock above, it's possible
1585 * The event is no longer in our list. Make sure it didn't
1586 * complete while we were aborting
1587 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001588 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 found_evt = NULL;
1590 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1591 if (tmp_evt->cmnd == cmd) {
1592 found_evt = tmp_evt;
1593 break;
1594 }
1595 }
1596
1597 if (found_evt == NULL) {
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001598 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Ingo Molnarfe333322009-01-06 14:26:03 +00001599 sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001600 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return SUCCESS;
1602 }
1603
Ingo Molnarfe333322009-01-06 14:26:03 +00001604 sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001605 tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 cmd->result = (DID_ABORT << 16);
1608 list_del(&found_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001609 unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1610 found_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 free_event_struct(&found_evt->hostdata->pool, found_evt);
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001612 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 atomic_inc(&hostdata->request_limit);
1614 return SUCCESS;
1615}
1616
1617/**
1618 * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
1619 * template send this over to the server and wait synchronously for the
1620 * response
1621 */
1622static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1623{
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001624 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct srp_tsk_mgmt *tsk_mgmt;
1626 struct srp_event_struct *evt;
1627 struct srp_event_struct *tmp_evt, *pos;
1628 union viosrp_iu srp_rsp;
1629 int rsp_rc;
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001630 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 u16 lun = lun_from_dev(cmd->device);
Robert Jennings860784c2007-11-12 09:00:23 -06001632 unsigned long wait_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001634 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001635 wait_switch = jiffies + (init_timeout * HZ);
1636 do {
1637 evt = get_event_struct(&hostdata->pool);
1638 if (evt == NULL) {
1639 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1640 sdev_printk(KERN_ERR, cmd->device,
1641 "failed to allocate reset event\n");
1642 return FAILED;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Robert Jennings860784c2007-11-12 09:00:23 -06001645 init_event_struct(evt,
1646 sync_completion,
1647 VIOSRP_SRP_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001648 reset_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Robert Jennings860784c2007-11-12 09:00:23 -06001650 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Robert Jennings860784c2007-11-12 09:00:23 -06001652 /* Set up a lun reset SRP command */
1653 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1654 tsk_mgmt->opcode = SRP_TSK_MGMT;
Bart Van Assche985aa492015-05-18 13:27:14 +02001655 int_to_scsilun(lun, &tsk_mgmt->lun);
Robert Jennings860784c2007-11-12 09:00:23 -06001656 tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Robert Jennings860784c2007-11-12 09:00:23 -06001658 evt->sync_srp = &srp_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Robert Jennings860784c2007-11-12 09:00:23 -06001660 init_completion(&evt->comp);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001661 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
Robert Jennings860784c2007-11-12 09:00:23 -06001662
1663 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1664 break;
1665
1666 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1667 msleep(10);
1668 spin_lock_irqsave(hostdata->host->host_lock, flags);
1669 } while (time_before(jiffies, wait_switch));
1670
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001671 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Robert Jennings860784c2007-11-12 09:00:23 -06001672
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001673 if (rsp_rc != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05001674 sdev_printk(KERN_ERR, cmd->device,
1675 "failed to send reset event. rc=%d\n", rsp_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return FAILED;
1677 }
1678
Ingo Molnarfe333322009-01-06 14:26:03 +00001679 sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
Robert Jennings860784c2007-11-12 09:00:23 -06001680 (((u64) lun) << 48));
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 wait_for_completion(&evt->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /* make sure we got a good response */
FUJITA Tomonorief265672006-03-26 03:57:14 +09001685 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001687 sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
1688 srp_rsp.srp.rsp.opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return FAILED;
1690 }
1691
FUJITA Tomonorief265672006-03-26 03:57:14 +09001692 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1693 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 else
1695 rsp_rc = srp_rsp.srp.rsp.status;
1696
1697 if (rsp_rc) {
1698 if (printk_ratelimit())
Brian King6c0a60e2007-06-13 17:12:19 -05001699 sdev_printk(KERN_WARNING, cmd->device,
Ingo Molnarfe333322009-01-06 14:26:03 +00001700 "reset code %d for task tag 0x%llx\n",
Brian King6c0a60e2007-06-13 17:12:19 -05001701 rsp_rc, tsk_mgmt->task_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return FAILED;
1703 }
1704
1705 /* We need to find all commands for this LUN that have not yet been
1706 * responded to, and fail them with DID_RESET
1707 */
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001708 spin_lock_irqsave(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1710 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1711 if (tmp_evt->cmnd)
1712 tmp_evt->cmnd->result = (DID_RESET << 16);
1713 list_del(&tmp_evt->list);
James Bottomley4dddbc22005-09-06 17:11:54 -05001714 unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1715 tmp_evt->hostdata->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 free_event_struct(&tmp_evt->hostdata->pool,
1717 tmp_evt);
1718 atomic_inc(&hostdata->request_limit);
1719 if (tmp_evt->cmnd_done)
1720 tmp_evt->cmnd_done(tmp_evt->cmnd);
1721 else if (tmp_evt->done)
1722 tmp_evt->done(tmp_evt);
1723 }
1724 }
Dave C Boutcherbe042f22005-08-15 16:52:58 -05001725 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return SUCCESS;
1727}
1728
1729/**
Brian King3d0e91f2007-06-13 17:12:26 -05001730 * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
1731 * @cmd: struct scsi_cmnd having problems
1732*/
1733static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Brian King3d0e91f2007-06-13 17:12:26 -05001735 unsigned long wait_switch = 0;
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001736 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Brian King3d0e91f2007-06-13 17:12:26 -05001738 dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
1739
1740 ibmvscsi_reset_host(hostdata);
1741
1742 for (wait_switch = jiffies + (init_timeout * HZ);
1743 time_before(jiffies, wait_switch) &&
1744 atomic_read(&hostdata->request_limit) < 2;) {
1745
1746 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Brian King3d0e91f2007-06-13 17:12:26 -05001748
1749 if (atomic_read(&hostdata->request_limit) <= 0)
1750 return FAILED;
1751
1752 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
1755/**
1756 * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1757 * @crq: Command/Response queue
1758 * @hostdata: ibmvscsi_host_data of host
1759 *
1760*/
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10001761static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1762 struct ibmvscsi_host_data *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Brian King6c0a60e2007-06-13 17:12:19 -05001764 long rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 unsigned long flags;
Anton Blanchard72264eb2013-09-03 10:04:47 +10001766 /* The hypervisor copies our tag value here so no byteswapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct srp_event_struct *evt_struct =
Anton Blanchard72264eb2013-09-03 10:04:47 +10001768 (__force struct srp_event_struct *)crq->IU_data_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 switch (crq->valid) {
1770 case 0xC0: /* initialization */
1771 switch (crq->format) {
1772 case 0x01: /* Initialization message */
Brian King6c0a60e2007-06-13 17:12:19 -05001773 dev_info(hostdata->dev, "partner initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* Send back a response */
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10001775 rc = ibmvscsi_send_crq(hostdata, 0xC002000000000000LL, 0);
1776 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* Now login */
Brian King3507e132009-06-08 16:19:04 -05001778 init_adapter(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001780 dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782
1783 break;
1784 case 0x02: /* Initialization response */
Brian King6c0a60e2007-06-13 17:12:19 -05001785 dev_info(hostdata->dev, "partner initialization complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 /* Now login */
Brian King3507e132009-06-08 16:19:04 -05001788 init_adapter(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 break;
1790 default:
Brian King6c0a60e2007-06-13 17:12:19 -05001791 dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 return;
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001794 case 0xFF: /* Hypervisor telling us the connection is closed */
1795 scsi_block_requests(hostdata->host);
Dave C Boutchercefbda22006-06-12 21:22:51 -05001796 atomic_set(&hostdata->request_limit, 0);
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001797 if (crq->format == 0x06) {
1798 /* We need to re-setup the interpartition connection */
Brian King6c0a60e2007-06-13 17:12:19 -05001799 dev_info(hostdata->dev, "Re-enabling adapter!\n");
Brian King126c5cc2009-06-08 16:19:08 -05001800 hostdata->client_migrated = 1;
Brian King0f33ece2010-06-17 13:56:00 -05001801 hostdata->reenable_crq = 1;
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001802 purge_requests(hostdata, DID_REQUEUE);
Brian King0f33ece2010-06-17 13:56:00 -05001803 wake_up(&hostdata->work_wait_q);
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001804 } else {
Brian King6c0a60e2007-06-13 17:12:19 -05001805 dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
1806 crq->format);
Brian King0f33ece2010-06-17 13:56:00 -05001807 ibmvscsi_reset_host(hostdata);
Dave C Boutcher2b541f82006-01-19 13:34:44 -06001808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return;
1810 case 0x80: /* real payload */
1811 break;
1812 default:
Brian King6c0a60e2007-06-13 17:12:19 -05001813 dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
1814 crq->valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return;
1816 }
1817
1818 /* The only kind of payload CRQs we should get are responses to
1819 * things we send. Make sure this response is to something we
1820 * actually sent
1821 */
1822 if (!valid_event_struct(&hostdata->pool, evt_struct)) {
Brian King6c0a60e2007-06-13 17:12:19 -05001823 dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
Anton Blanchard72264eb2013-09-03 10:04:47 +10001824 evt_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return;
1826 }
1827
1828 if (atomic_read(&evt_struct->free)) {
Brian King6c0a60e2007-06-13 17:12:19 -05001829 dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
Anton Blanchard72264eb2013-09-03 10:04:47 +10001830 evt_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return;
1832 }
1833
1834 if (crq->format == VIOSRP_SRP_FORMAT)
Anton Blanchard72264eb2013-09-03 10:04:47 +10001835 atomic_add(be32_to_cpu(evt_struct->xfer_iu->srp.rsp.req_lim_delta),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 &hostdata->request_limit);
1837
Brian King3d0e91f2007-06-13 17:12:26 -05001838 del_timer(&evt_struct->timer);
1839
Brian Kingca616682008-05-19 10:27:56 -05001840 if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
Brian Kingc3a3b552008-04-25 16:58:29 -05001841 evt_struct->cmnd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (evt_struct->done)
1843 evt_struct->done(evt_struct);
1844 else
Brian King6c0a60e2007-06-13 17:12:19 -05001845 dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /*
1848 * Lock the host_lock before messing with these structures, since we
1849 * are running in a task context
1850 */
1851 spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1852 list_del(&evt_struct->list);
1853 free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1854 spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1855}
1856
1857/**
1858 * ibmvscsi_get_host_config: Send the command to the server to get host
1859 * configuration data. The data is opaque to us.
1860 */
1861static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1862 unsigned char *buffer, int length)
1863{
1864 struct viosrp_host_config *host_config;
1865 struct srp_event_struct *evt_struct;
Brian King06f923c2007-06-13 17:12:33 -05001866 unsigned long flags;
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001867 dma_addr_t addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int rc;
1869
1870 evt_struct = get_event_struct(&hostdata->pool);
1871 if (!evt_struct) {
Brian King6c0a60e2007-06-13 17:12:19 -05001872 dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return -1;
1874 }
1875
1876 init_event_struct(evt_struct,
1877 sync_completion,
1878 VIOSRP_MAD_FORMAT,
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001879 info_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 host_config = &evt_struct->iu.mad.host_config;
1882
Benjamin Herrenschmidt225c5692012-07-30 11:33:05 +10001883 /* The transport length field is only 16-bit */
1884 length = min(0xffff, length);
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /* Set up a lun reset SRP command */
1887 memset(host_config, 0x00, sizeof(*host_config));
Anton Blanchard72264eb2013-09-03 10:04:47 +10001888 host_config->common.type = cpu_to_be32(VIOSRP_HOST_CONFIG_TYPE);
1889 host_config->common.length = cpu_to_be16(length);
1890 addr = dma_map_single(hostdata->dev, buffer, length, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Anton Blanchard72264eb2013-09-03 10:04:47 +10001892 if (dma_mapping_error(hostdata->dev, addr)) {
Robert Jennings7912a0a2008-07-24 04:35:27 +10001893 if (!firmware_has_feature(FW_FEATURE_CMO))
1894 dev_err(hostdata->dev,
1895 "dma_mapping error getting host config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 free_event_struct(&hostdata->pool, evt_struct);
1897 return -1;
1898 }
1899
Anton Blanchard72264eb2013-09-03 10:04:47 +10001900 host_config->buffer = cpu_to_be64(addr);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 init_completion(&evt_struct->comp);
Brian King06f923c2007-06-13 17:12:33 -05001903 spin_lock_irqsave(hostdata->host->host_lock, flags);
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001904 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
Brian King06f923c2007-06-13 17:12:33 -05001905 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001906 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 wait_for_completion(&evt_struct->comp);
FUJITA Tomonorie5dbfa62006-04-14 13:52:18 +09001908 dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 return rc;
1911}
1912
Robert Jennings0979c842007-03-29 12:30:40 -05001913/**
1914 * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
1915 * @sdev: struct scsi_device device to configure
1916 *
1917 * Enable allow_restart for a device if it is a disk. Adjust the
1918 * queue_depth here also as is required by the documentation for
1919 * struct scsi_host_template.
1920 */
1921static int ibmvscsi_slave_configure(struct scsi_device *sdev)
1922{
1923 struct Scsi_Host *shost = sdev->host;
1924 unsigned long lock_flags = 0;
1925
1926 spin_lock_irqsave(shost->host_lock, lock_flags);
Brian Kingd1a357f2007-10-25 16:06:34 -05001927 if (sdev->type == TYPE_DISK) {
Robert Jennings0979c842007-03-29 12:30:40 -05001928 sdev->allow_restart = 1;
Robert Jenningse1a5ce52009-06-08 16:19:03 -05001929 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
Brian Kingd1a357f2007-10-25 16:06:34 -05001930 }
Robert Jennings0979c842007-03-29 12:30:40 -05001931 spin_unlock_irqrestore(shost->host_lock, lock_flags);
1932 return 0;
1933}
1934
Brian King742d25b2007-05-29 15:46:14 -05001935/**
1936 * ibmvscsi_change_queue_depth - Change the device's queue depth
1937 * @sdev: scsi device struct
1938 * @qdepth: depth to set
Mike Christiee881a172009-10-15 17:46:39 -07001939 * @reason: calling context
Brian King742d25b2007-05-29 15:46:14 -05001940 *
1941 * Return value:
1942 * actual depth set
1943 **/
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001944static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
Brian King742d25b2007-05-29 15:46:14 -05001945{
1946 if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
1947 qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001948 return scsi_change_queue_depth(sdev, qdepth);
Brian King742d25b2007-05-29 15:46:14 -05001949}
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/* ------------------------------------------------------------
1952 * sysfs attributes
1953 */
Brian King126c5cc2009-06-08 16:19:08 -05001954static ssize_t show_host_vhost_loc(struct device *dev,
1955 struct device_attribute *attr, char *buf)
1956{
1957 struct Scsi_Host *shost = class_to_shost(dev);
1958 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1959 int len;
1960
1961 len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
1962 hostdata->caps.loc);
1963 return len;
1964}
1965
1966static struct device_attribute ibmvscsi_host_vhost_loc = {
1967 .attr = {
1968 .name = "vhost_loc",
1969 .mode = S_IRUGO,
1970 },
1971 .show = show_host_vhost_loc,
1972};
1973
1974static ssize_t show_host_vhost_name(struct device *dev,
1975 struct device_attribute *attr, char *buf)
1976{
1977 struct Scsi_Host *shost = class_to_shost(dev);
1978 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1979 int len;
1980
1981 len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
1982 hostdata->caps.name);
1983 return len;
1984}
1985
1986static struct device_attribute ibmvscsi_host_vhost_name = {
1987 .attr = {
1988 .name = "vhost_name",
1989 .mode = S_IRUGO,
1990 },
1991 .show = show_host_vhost_name,
1992};
1993
Tony Jonesee959b02008-02-22 00:13:36 +01001994static ssize_t show_host_srp_version(struct device *dev,
1995 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Tony Jonesee959b02008-02-22 00:13:36 +01001997 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09001998 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int len;
2000
2001 len = snprintf(buf, PAGE_SIZE, "%s\n",
2002 hostdata->madapter_info.srp_version);
2003 return len;
2004}
2005
Tony Jonesee959b02008-02-22 00:13:36 +01002006static struct device_attribute ibmvscsi_host_srp_version = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 .attr = {
2008 .name = "srp_version",
2009 .mode = S_IRUGO,
2010 },
2011 .show = show_host_srp_version,
2012};
2013
Tony Jonesee959b02008-02-22 00:13:36 +01002014static ssize_t show_host_partition_name(struct device *dev,
2015 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 char *buf)
2017{
Tony Jonesee959b02008-02-22 00:13:36 +01002018 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09002019 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int len;
2021
2022 len = snprintf(buf, PAGE_SIZE, "%s\n",
2023 hostdata->madapter_info.partition_name);
2024 return len;
2025}
2026
Tony Jonesee959b02008-02-22 00:13:36 +01002027static struct device_attribute ibmvscsi_host_partition_name = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 .attr = {
2029 .name = "partition_name",
2030 .mode = S_IRUGO,
2031 },
2032 .show = show_host_partition_name,
2033};
2034
Tony Jonesee959b02008-02-22 00:13:36 +01002035static ssize_t show_host_partition_number(struct device *dev,
2036 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 char *buf)
2038{
Tony Jonesee959b02008-02-22 00:13:36 +01002039 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09002040 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int len;
2042
2043 len = snprintf(buf, PAGE_SIZE, "%d\n",
2044 hostdata->madapter_info.partition_number);
2045 return len;
2046}
2047
Tony Jonesee959b02008-02-22 00:13:36 +01002048static struct device_attribute ibmvscsi_host_partition_number = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 .attr = {
2050 .name = "partition_number",
2051 .mode = S_IRUGO,
2052 },
2053 .show = show_host_partition_number,
2054};
2055
Tony Jonesee959b02008-02-22 00:13:36 +01002056static ssize_t show_host_mad_version(struct device *dev,
2057 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Tony Jonesee959b02008-02-22 00:13:36 +01002059 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09002060 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 int len;
2062
2063 len = snprintf(buf, PAGE_SIZE, "%d\n",
2064 hostdata->madapter_info.mad_version);
2065 return len;
2066}
2067
Tony Jonesee959b02008-02-22 00:13:36 +01002068static struct device_attribute ibmvscsi_host_mad_version = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 .attr = {
2070 .name = "mad_version",
2071 .mode = S_IRUGO,
2072 },
2073 .show = show_host_mad_version,
2074};
2075
Tony Jonesee959b02008-02-22 00:13:36 +01002076static ssize_t show_host_os_type(struct device *dev,
2077 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Tony Jonesee959b02008-02-22 00:13:36 +01002079 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09002080 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 int len;
2082
2083 len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
2084 return len;
2085}
2086
Tony Jonesee959b02008-02-22 00:13:36 +01002087static struct device_attribute ibmvscsi_host_os_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 .attr = {
2089 .name = "os_type",
2090 .mode = S_IRUGO,
2091 },
2092 .show = show_host_os_type,
2093};
2094
Tony Jonesee959b02008-02-22 00:13:36 +01002095static ssize_t show_host_config(struct device *dev,
2096 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Tony Jonesee959b02008-02-22 00:13:36 +01002098 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori7603e022007-07-23 09:28:40 +09002099 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 /* returns null-terminated host config data */
2102 if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
2103 return strlen(buf);
2104 else
2105 return 0;
2106}
2107
Tony Jonesee959b02008-02-22 00:13:36 +01002108static struct device_attribute ibmvscsi_host_config = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 .attr = {
2110 .name = "config",
2111 .mode = S_IRUGO,
2112 },
2113 .show = show_host_config,
2114};
2115
Tony Jonesee959b02008-02-22 00:13:36 +01002116static struct device_attribute *ibmvscsi_attrs[] = {
Brian King126c5cc2009-06-08 16:19:08 -05002117 &ibmvscsi_host_vhost_loc,
2118 &ibmvscsi_host_vhost_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 &ibmvscsi_host_srp_version,
2120 &ibmvscsi_host_partition_name,
2121 &ibmvscsi_host_partition_number,
2122 &ibmvscsi_host_mad_version,
2123 &ibmvscsi_host_os_type,
2124 &ibmvscsi_host_config,
2125 NULL
2126};
2127
2128/* ------------------------------------------------------------
2129 * SCSI driver registration
2130 */
2131static struct scsi_host_template driver_template = {
2132 .module = THIS_MODULE,
2133 .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
2134 .proc_name = "ibmvscsi",
2135 .queuecommand = ibmvscsi_queuecommand,
2136 .eh_abort_handler = ibmvscsi_eh_abort_handler,
2137 .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
Brian King3d0e91f2007-06-13 17:12:26 -05002138 .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
Robert Jennings0979c842007-03-29 12:30:40 -05002139 .slave_configure = ibmvscsi_slave_configure,
Brian King742d25b2007-05-29 15:46:14 -05002140 .change_queue_depth = ibmvscsi_change_queue_depth,
Robert Jennings7912a0a2008-07-24 04:35:27 +10002141 .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
Robert Jenningsa897ff22007-03-28 12:45:46 -05002142 .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 .this_id = -1,
James Bottomley4dddbc22005-09-06 17:11:54 -05002144 .sg_tablesize = SG_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 .use_clustering = ENABLE_CLUSTERING,
2146 .shost_attrs = ibmvscsi_attrs,
2147};
2148
2149/**
Robert Jennings7912a0a2008-07-24 04:35:27 +10002150 * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
2151 *
2152 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
2153 *
2154 * Return value:
2155 * Number of bytes of IO data the driver will need to perform well.
2156 */
2157static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
2158{
2159 /* iu_storage data allocated in initialize_event_pool */
Brian King4f10aae2008-12-17 17:19:33 -06002160 unsigned long desired_io = max_events * sizeof(union viosrp_iu);
Robert Jennings7912a0a2008-07-24 04:35:27 +10002161
2162 /* add io space for sg data */
Brian King004dd5e2008-08-15 10:48:47 -05002163 desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
Robert Jennings7912a0a2008-07-24 04:35:27 +10002164 IBMVSCSI_CMDS_PER_LUN_DEFAULT);
2165
2166 return desired_io;
2167}
2168
Brian King0f33ece2010-06-17 13:56:00 -05002169static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
2170{
2171 int rc;
2172 char *action = "reset";
2173
2174 if (hostdata->reset_crq) {
2175 smp_rmb();
2176 hostdata->reset_crq = 0;
2177
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002178 rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
Brian King0f33ece2010-06-17 13:56:00 -05002179 if (!rc)
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002180 rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
Brian King201aed62011-04-27 10:27:08 -05002181 vio_enable_interrupts(to_vio_dev(hostdata->dev));
Brian King0f33ece2010-06-17 13:56:00 -05002182 } else if (hostdata->reenable_crq) {
2183 smp_rmb();
2184 action = "enable";
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002185 rc = ibmvscsi_reenable_crq_queue(&hostdata->queue, hostdata);
Brian King0f33ece2010-06-17 13:56:00 -05002186 hostdata->reenable_crq = 0;
2187 if (!rc)
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002188 rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
Brian King0f33ece2010-06-17 13:56:00 -05002189 } else
2190 return;
2191
2192 if (rc) {
2193 atomic_set(&hostdata->request_limit, -1);
2194 dev_err(hostdata->dev, "error after %s\n", action);
2195 }
2196
2197 scsi_unblock_requests(hostdata->host);
2198}
2199
2200static int ibmvscsi_work_to_do(struct ibmvscsi_host_data *hostdata)
2201{
2202 if (kthread_should_stop())
2203 return 1;
2204 else if (hostdata->reset_crq) {
2205 smp_rmb();
2206 return 1;
2207 } else if (hostdata->reenable_crq) {
2208 smp_rmb();
2209 return 1;
2210 }
2211
2212 return 0;
2213}
2214
2215static int ibmvscsi_work(void *data)
2216{
2217 struct ibmvscsi_host_data *hostdata = data;
2218 int rc;
2219
Dongsheng Yang8698a742014-03-11 18:09:12 +08002220 set_user_nice(current, MIN_NICE);
Brian King0f33ece2010-06-17 13:56:00 -05002221
2222 while (1) {
2223 rc = wait_event_interruptible(hostdata->work_wait_q,
2224 ibmvscsi_work_to_do(hostdata));
2225
2226 BUG_ON(rc);
2227
2228 if (kthread_should_stop())
2229 break;
2230
2231 ibmvscsi_do_work(hostdata);
2232 }
2233
2234 return 0;
2235}
2236
Robert Jennings7912a0a2008-07-24 04:35:27 +10002237/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * Called by bus code for each adapter
2239 */
2240static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
2241{
2242 struct ibmvscsi_host_data *hostdata;
2243 struct Scsi_Host *host;
2244 struct device *dev = &vdev->dev;
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002245 struct srp_rport_identifiers ids;
2246 struct srp_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 unsigned long wait_switch = 0;
Dave C Boutchercefbda22006-06-12 21:22:51 -05002248 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Greg Kroah-Hartman559fde72009-05-04 12:40:54 -07002250 dev_set_drvdata(&vdev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
2253 if (!host) {
Brian King6c0a60e2007-06-13 17:12:19 -05002254 dev_err(&vdev->dev, "couldn't allocate host data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 goto scsi_host_alloc_failed;
2256 }
2257
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002258 host->transportt = ibmvscsi_transport_template;
FUJITA Tomonori7603e022007-07-23 09:28:40 +09002259 hostdata = shost_priv(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 memset(hostdata, 0x00, sizeof(*hostdata));
2261 INIT_LIST_HEAD(&hostdata->sent);
Brian King0f33ece2010-06-17 13:56:00 -05002262 init_waitqueue_head(&hostdata->work_wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 hostdata->host = host;
2264 hostdata->dev = dev;
2265 atomic_set(&hostdata->request_limit, -1);
Robert Jennings7912a0a2008-07-24 04:35:27 +10002266 hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Brian King126c5cc2009-06-08 16:19:08 -05002268 if (map_persist_bufs(hostdata)) {
2269 dev_err(&vdev->dev, "couldn't map persistent buffers\n");
2270 goto persist_bufs_failed;
2271 }
2272
Brian King0f33ece2010-06-17 13:56:00 -05002273 hostdata->work_thread = kthread_run(ibmvscsi_work, hostdata, "%s_%d",
2274 "ibmvscsi", host->host_no);
2275
2276 if (IS_ERR(hostdata->work_thread)) {
2277 dev_err(&vdev->dev, "couldn't initialize kthread. rc=%ld\n",
2278 PTR_ERR(hostdata->work_thread));
2279 goto init_crq_failed;
2280 }
2281
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002282 rc = ibmvscsi_init_crq_queue(&hostdata->queue, hostdata, max_events);
Dave C Boutchercefbda22006-06-12 21:22:51 -05002283 if (rc != 0 && rc != H_RESOURCE) {
Brian King6c0a60e2007-06-13 17:12:19 -05002284 dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
Brian King0f33ece2010-06-17 13:56:00 -05002285 goto kill_kthread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
Brian King4f10aae2008-12-17 17:19:33 -06002287 if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
Brian King6c0a60e2007-06-13 17:12:19 -05002288 dev_err(&vdev->dev, "couldn't initialize event pool\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 goto init_pool_failed;
2290 }
2291
Laurent Vivier3467a142015-11-09 17:49:09 +01002292 host->max_lun = IBMVSCSI_MAX_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 host->max_id = max_id;
2294 host->max_channel = max_channel;
Brian Kingfbc56f02009-06-08 16:19:01 -05002295 host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Laurent Viviere3678a02015-11-09 17:49:08 +01002297 dev_info(dev,
2298 "Maximum ID: %d Maximum LUN: %llu Maximum Channel: %d\n",
2299 host->max_id, host->max_lun, host->max_channel);
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (scsi_add_host(hostdata->host, hostdata->dev))
2302 goto add_host_failed;
2303
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002304 /* we don't have a proper target_port_id so let's use the fake one */
2305 memcpy(ids.port_id, hostdata->madapter_info.partition_name,
2306 sizeof(ids.port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002307 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002308 rport = srp_rport_add(host, &ids);
2309 if (IS_ERR(rport))
2310 goto add_srp_port_failed;
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* Try to send an initialization message. Note that this is allowed
2313 * to fail if the other end is not acive. In that case we don't
2314 * want to scan
2315 */
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002316 if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0
Dave C Boutchercefbda22006-06-12 21:22:51 -05002317 || rc == H_RESOURCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /*
2319 * Wait around max init_timeout secs for the adapter to finish
2320 * initializing. When we are done initializing, we will have a
2321 * valid request_limit. We don't want Linux scanning before
2322 * we are ready.
2323 */
2324 for (wait_switch = jiffies + (init_timeout * HZ);
2325 time_before(jiffies, wait_switch) &&
2326 atomic_read(&hostdata->request_limit) < 2;) {
2327
2328 msleep(10);
2329 }
2330
2331 /* if we now have a valid request_limit, initiate a scan */
2332 if (atomic_read(&hostdata->request_limit) > 0)
2333 scsi_scan_host(host);
2334 }
2335
Greg Kroah-Hartman559fde72009-05-04 12:40:54 -07002336 dev_set_drvdata(&vdev->dev, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return 0;
2338
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002339 add_srp_port_failed:
2340 scsi_remove_host(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 add_host_failed:
2342 release_event_pool(&hostdata->pool, hostdata);
2343 init_pool_failed:
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002344 ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_events);
Brian King0f33ece2010-06-17 13:56:00 -05002345 kill_kthread:
2346 kthread_stop(hostdata->work_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 init_crq_failed:
Brian King126c5cc2009-06-08 16:19:08 -05002348 unmap_persist_bufs(hostdata);
2349 persist_bufs_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 scsi_host_put(host);
2351 scsi_host_alloc_failed:
2352 return -1;
2353}
2354
2355static int ibmvscsi_remove(struct vio_dev *vdev)
2356{
Greg Kroah-Hartman559fde72009-05-04 12:40:54 -07002357 struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
Brian King126c5cc2009-06-08 16:19:08 -05002358 unmap_persist_bufs(hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 release_event_pool(&hostdata->pool, hostdata);
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002360 ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
Brian King4f10aae2008-12-17 17:19:33 -06002361 max_events);
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002362
Brian King0f33ece2010-06-17 13:56:00 -05002363 kthread_stop(hostdata->work_thread);
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002364 srp_remove_host(hostdata->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 scsi_remove_host(hostdata->host);
2366 scsi_host_put(hostdata->host);
2367
2368 return 0;
2369}
2370
2371/**
Brian King64355b92010-02-21 10:37:57 -06002372 * ibmvscsi_resume: Resume from suspend
2373 * @dev: device struct
2374 *
2375 * We may have lost an interrupt across suspend/resume, so kick the
2376 * interrupt handler
2377 */
2378static int ibmvscsi_resume(struct device *dev)
2379{
2380 struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002381 vio_disable_interrupts(to_vio_dev(hostdata->dev));
2382 tasklet_schedule(&hostdata->srp_task);
2383
2384 return 0;
Brian King64355b92010-02-21 10:37:57 -06002385}
2386
2387/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
2389 * support.
2390 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08002391static struct vio_device_id ibmvscsi_device_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 {"vscsi", "IBM,v-scsi"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10002393 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +10002396
Brian King64355b92010-02-21 10:37:57 -06002397static struct dev_pm_ops ibmvscsi_pm_ops = {
2398 .resume = ibmvscsi_resume
2399};
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401static struct vio_driver ibmvscsi_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 .id_table = ibmvscsi_device_table,
2403 .probe = ibmvscsi_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10002404 .remove = ibmvscsi_remove,
Robert Jennings7912a0a2008-07-24 04:35:27 +10002405 .get_desired_dma = ibmvscsi_get_desired_dma,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00002406 .name = "ibmvscsi",
2407 .pm = &ibmvscsi_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408};
2409
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002410static struct srp_function_template ibmvscsi_transport_functions = {
2411};
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413int __init ibmvscsi_module_init(void)
2414{
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002415 int ret;
2416
Brian King4f10aae2008-12-17 17:19:33 -06002417 /* Ensure we have two requests to do error recovery */
2418 driver_template.can_queue = max_requests;
2419 max_events = max_requests + 2;
2420
Benjamin Herrenschmidt9965c2f2012-07-30 11:32:26 +10002421 if (!firmware_has_feature(FW_FEATURE_VIO))
David Woodhoused3849d52007-09-22 08:29:36 +10002422 return -ENODEV;
2423
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002424 ibmvscsi_transport_template =
2425 srp_attach_transport(&ibmvscsi_transport_functions);
2426 if (!ibmvscsi_transport_template)
2427 return -ENOMEM;
2428
2429 ret = vio_register_driver(&ibmvscsi_driver);
2430 if (ret)
2431 srp_release_transport(ibmvscsi_transport_template);
2432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
2435void __exit ibmvscsi_module_exit(void)
2436{
2437 vio_unregister_driver(&ibmvscsi_driver);
FUJITA Tomonori4d680412007-06-27 16:32:50 +09002438 srp_release_transport(ibmvscsi_transport_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
2441module_init(ibmvscsi_module_init);
2442module_exit(ibmvscsi_module_exit);