ibmvscsi: Abort init sequence during error recovery
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / ibmvscsi / ibmvscsi.c
1 /* ------------------------------------------------------------
2  * ibmvscsi.c
3  * (C) Copyright IBM Corporation 1994, 2004
4  * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
5  *          Santiago Leon (santil@us.ibm.com)
6  *          Dave Boutcher (sleddog@us.ibm.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
21  * USA
22  *
23  * ------------------------------------------------------------
24  * Emulation of a SCSI host adapter for Virtual I/O devices
25  *
26  * This driver supports the SCSI adapter implemented by the IBM
27  * Power5 firmware.  That SCSI adapter is not a physical adapter,
28  * but allows Linux SCSI peripheral drivers to directly
29  * access devices in another logical partition on the physical system.
30  *
31  * The virtual adapter(s) are present in the open firmware device
32  * tree just like real adapters.
33  *
34  * One of the capabilities provided on these systems is the ability
35  * to DMA between partitions.  The architecture states that for VSCSI,
36  * the server side is allowed to DMA to and from the client.  The client
37  * is never trusted to DMA to or from the server directly.
38  *
39  * Messages are sent between partitions on a "Command/Response Queue" 
40  * (CRQ), which is just a buffer of 16 byte entries in the receiver's 
41  * Senders cannot access the buffer directly, but send messages by
42  * making a hypervisor call and passing in the 16 bytes.  The hypervisor
43  * puts the message in the next 16 byte space in round-robin fashion,
44  * turns on the high order bit of the message (the valid bit), and 
45  * generates an interrupt to the receiver (if interrupts are turned on.) 
46  * The receiver just turns off the valid bit when they have copied out
47  * the message.
48  *
49  * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
50  * (IU) (as defined in the T10 standard available at www.t10.org), gets 
51  * a DMA address for the message, and sends it to the server as the
52  * payload of a CRQ message.  The server DMAs the SRP IU and processes it,
53  * including doing any additional data transfers.  When it is done, it
54  * DMAs the SRP response back to the same address as the request came from,
55  * and sends a CRQ message back to inform the client that the request has
56  * completed.
57  *
58  * TODO: This is currently pretty tied to the IBM pSeries hypervisor
59  * 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>
67 #include <linux/slab.h>
68 #include <linux/of.h>
69 #include <linux/pm.h>
70 #include <linux/kthread.h>
71 #include <asm/firmware.h>
72 #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>
77 #include <scsi/scsi_transport_srp.h>
78 #include "ibmvscsi.h"
79
80 /* The values below are somewhat arbitrary default values, but 
81  * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
82  * Note that there are 3 bits of channel value, 6 bits of id, and
83  * 5 bits of LUN.
84  */
85 static int max_id = 64;
86 static int max_channel = 3;
87 static int init_timeout = 300;
88 static int login_timeout = 60;
89 static int info_timeout = 30;
90 static int abort_timeout = 60;
91 static int reset_timeout = 60;
92 static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
93 static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
94 static int fast_fail = 1;
95 static int client_reserve = 1;
96 static char partition_name[97] = "UNKNOWN";
97 static unsigned int partition_number = -1;
98
99 static struct scsi_transport_template *ibmvscsi_transport_template;
100
101 #define IBMVSCSI_VERSION "1.5.9"
102
103 MODULE_DESCRIPTION("IBM Virtual SCSI");
104 MODULE_AUTHOR("Dave Boutcher");
105 MODULE_LICENSE("GPL");
106 MODULE_VERSION(IBMVSCSI_VERSION);
107
108 module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
109 MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
110 module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
111 MODULE_PARM_DESC(max_channel, "Largest channel value");
112 module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
113 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
114 module_param_named(max_requests, max_requests, int, S_IRUGO);
115 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
116 module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
117 MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
118 module_param_named(client_reserve, client_reserve, int, S_IRUGO );
119 MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
120
121 static 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  */
135 static 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  */
152 static 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  */
178 static 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;
188         } else
189                 crq = NULL;
190         spin_unlock_irqrestore(&queue->lock, flags);
191
192         return crq;
193 }
194
195 /**
196  * ibmvscsi_send_crq: - Send a CRQ
197  * @hostdata:   the adapter
198  * @word1:      the first 64 bits of the data
199  * @word2:      the second 64 bits of the data
200  */
201 static int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata,
202                              u64 word1, u64 word2)
203 {
204         struct vio_dev *vdev = to_vio_dev(hostdata->dev);
205
206         return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
207 }
208
209 /**
210  * ibmvscsi_task: - Process srps asynchronously
211  * @data:       ibmvscsi_host_data of host
212  */
213 static void ibmvscsi_task(void *data)
214 {
215         struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
216         struct vio_dev *vdev = to_vio_dev(hostdata->dev);
217         struct viosrp_crq *crq;
218         int done = 0;
219
220         while (!done) {
221                 /* Pull all the valid messages off the CRQ */
222                 while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
223                         ibmvscsi_handle_crq(crq, hostdata);
224                         crq->valid = 0x00;
225                 }
226
227                 vio_enable_interrupts(vdev);
228                 crq = crq_queue_next_crq(&hostdata->queue);
229                 if (crq != NULL) {
230                         vio_disable_interrupts(vdev);
231                         ibmvscsi_handle_crq(crq, hostdata);
232                         crq->valid = 0x00;
233                 } else {
234                         done = 1;
235                 }
236         }
237 }
238
239 static void gather_partition_info(void)
240 {
241         struct device_node *rootdn;
242
243         const char *ppartition_name;
244         const unsigned int *p_number_ptr;
245
246         /* Retrieve information about this partition */
247         rootdn = of_find_node_by_path("/");
248         if (!rootdn) {
249                 return;
250         }
251
252         ppartition_name = of_get_property(rootdn, "ibm,partition-name", NULL);
253         if (ppartition_name)
254                 strncpy(partition_name, ppartition_name,
255                                 sizeof(partition_name));
256         p_number_ptr = of_get_property(rootdn, "ibm,partition-no", NULL);
257         if (p_number_ptr)
258                 partition_number = *p_number_ptr;
259         of_node_put(rootdn);
260 }
261
262 static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
263 {
264         memset(&hostdata->madapter_info, 0x00,
265                         sizeof(hostdata->madapter_info));
266
267         dev_info(hostdata->dev, "SRP_VERSION: %s\n", SRP_VERSION);
268         strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
269
270         strncpy(hostdata->madapter_info.partition_name, partition_name,
271                         sizeof(hostdata->madapter_info.partition_name));
272
273         hostdata->madapter_info.partition_number = partition_number;
274
275         hostdata->madapter_info.mad_version = 1;
276         hostdata->madapter_info.os_type = 2;
277 }
278
279 /**
280  * reset_crq_queue: - resets a crq after a failure
281  * @queue:      crq_queue to initialize and register
282  * @hostdata:   ibmvscsi_host_data of host
283  *
284  */
285 static int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
286                                     struct ibmvscsi_host_data *hostdata)
287 {
288         int rc = 0;
289         struct vio_dev *vdev = to_vio_dev(hostdata->dev);
290
291         /* Close the CRQ */
292         do {
293                 if (rc)
294                         msleep(100);
295                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
296         } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
297
298         /* Clean out the queue */
299         memset(queue->msgs, 0x00, PAGE_SIZE);
300         queue->cur = 0;
301
302         set_adapter_info(hostdata);
303
304         /* And re-open it again */
305         rc = plpar_hcall_norets(H_REG_CRQ,
306                                 vdev->unit_address,
307                                 queue->msg_token, PAGE_SIZE);
308         if (rc == 2) {
309                 /* Adapter is good, but other end is not ready */
310                 dev_warn(hostdata->dev, "Partner adapter not ready\n");
311         } else if (rc != 0) {
312                 dev_warn(hostdata->dev, "couldn't register crq--rc 0x%x\n", rc);
313         }
314         return rc;
315 }
316
317 /**
318  * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
319  * @queue:      crq_queue to initialize and register
320  * @hostdata:   ibmvscsi_host_data of host
321  *
322  * Allocates a page for messages, maps it for dma, and registers
323  * the crq with the hypervisor.
324  * Returns zero on success.
325  */
326 static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
327                                    struct ibmvscsi_host_data *hostdata,
328                                    int max_requests)
329 {
330         int rc;
331         int retrc;
332         struct vio_dev *vdev = to_vio_dev(hostdata->dev);
333
334         queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
335
336         if (!queue->msgs)
337                 goto malloc_failed;
338         queue->size = PAGE_SIZE / sizeof(*queue->msgs);
339
340         queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
341                                           queue->size * sizeof(*queue->msgs),
342                                           DMA_BIDIRECTIONAL);
343
344         if (dma_mapping_error(hostdata->dev, queue->msg_token))
345                 goto map_failed;
346
347         gather_partition_info();
348         set_adapter_info(hostdata);
349
350         retrc = rc = plpar_hcall_norets(H_REG_CRQ,
351                                 vdev->unit_address,
352                                 queue->msg_token, PAGE_SIZE);
353         if (rc == H_RESOURCE)
354                 /* maybe kexecing and resource is busy. try a reset */
355                 rc = ibmvscsi_reset_crq_queue(queue,
356                                               hostdata);
357
358         if (rc == 2) {
359                 /* Adapter is good, but other end is not ready */
360                 dev_warn(hostdata->dev, "Partner adapter not ready\n");
361                 retrc = 0;
362         } else if (rc != 0) {
363                 dev_warn(hostdata->dev, "Error %d opening adapter\n", rc);
364                 goto reg_crq_failed;
365         }
366
367         queue->cur = 0;
368         spin_lock_init(&queue->lock);
369
370         tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
371                      (unsigned long)hostdata);
372
373         if (request_irq(vdev->irq,
374                         ibmvscsi_handle_event,
375                         0, "ibmvscsi", (void *)hostdata) != 0) {
376                 dev_err(hostdata->dev, "couldn't register irq 0x%x\n",
377                         vdev->irq);
378                 goto req_irq_failed;
379         }
380
381         rc = vio_enable_interrupts(vdev);
382         if (rc != 0) {
383                 dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc);
384                 goto req_irq_failed;
385         }
386
387         return retrc;
388
389       req_irq_failed:
390         tasklet_kill(&hostdata->srp_task);
391         rc = 0;
392         do {
393                 if (rc)
394                         msleep(100);
395                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
396         } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
397       reg_crq_failed:
398         dma_unmap_single(hostdata->dev,
399                          queue->msg_token,
400                          queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
401       map_failed:
402         free_page((unsigned long)queue->msgs);
403       malloc_failed:
404         return -1;
405 }
406
407 /**
408  * reenable_crq_queue: - reenables a crq after
409  * @queue:      crq_queue to initialize and register
410  * @hostdata:   ibmvscsi_host_data of host
411  *
412  */
413 static int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
414                                        struct ibmvscsi_host_data *hostdata)
415 {
416         int rc = 0;
417         struct vio_dev *vdev = to_vio_dev(hostdata->dev);
418
419         /* Re-enable the CRQ */
420         do {
421                 if (rc)
422                         msleep(100);
423                 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
424         } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
425
426         if (rc)
427                 dev_err(hostdata->dev, "Error %d enabling adapter\n", rc);
428         return rc;
429 }
430
431 /* ------------------------------------------------------------
432  * Routines for the event pool and event structs
433  */
434 /**
435  * initialize_event_pool: - Allocates and initializes the event pool for a host
436  * @pool:       event_pool to be initialized
437  * @size:       Number of events in pool
438  * @hostdata:   ibmvscsi_host_data who owns the event pool
439  *
440  * Returns zero on success.
441 */
442 static int initialize_event_pool(struct event_pool *pool,
443                                  int size, struct ibmvscsi_host_data *hostdata)
444 {
445         int i;
446
447         pool->size = size;
448         pool->next = 0;
449         pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
450         if (!pool->events)
451                 return -ENOMEM;
452
453         pool->iu_storage =
454             dma_alloc_coherent(hostdata->dev,
455                                pool->size * sizeof(*pool->iu_storage),
456                                &pool->iu_token, 0);
457         if (!pool->iu_storage) {
458                 kfree(pool->events);
459                 return -ENOMEM;
460         }
461
462         for (i = 0; i < pool->size; ++i) {
463                 struct srp_event_struct *evt = &pool->events[i];
464                 memset(&evt->crq, 0x00, sizeof(evt->crq));
465                 atomic_set(&evt->free, 1);
466                 evt->crq.valid = 0x80;
467                 evt->crq.IU_length = sizeof(*evt->xfer_iu);
468                 evt->crq.IU_data_ptr = pool->iu_token + 
469                         sizeof(*evt->xfer_iu) * i;
470                 evt->xfer_iu = pool->iu_storage + i;
471                 evt->hostdata = hostdata;
472                 evt->ext_list = NULL;
473                 evt->ext_list_token = 0;
474         }
475
476         return 0;
477 }
478
479 /**
480  * release_event_pool: - Frees memory of an event pool of a host
481  * @pool:       event_pool to be released
482  * @hostdata:   ibmvscsi_host_data who owns the even pool
483  *
484  * Returns zero on success.
485 */
486 static void release_event_pool(struct event_pool *pool,
487                                struct ibmvscsi_host_data *hostdata)
488 {
489         int i, in_use = 0;
490         for (i = 0; i < pool->size; ++i) {
491                 if (atomic_read(&pool->events[i].free) != 1)
492                         ++in_use;
493                 if (pool->events[i].ext_list) {
494                         dma_free_coherent(hostdata->dev,
495                                   SG_ALL * sizeof(struct srp_direct_buf),
496                                   pool->events[i].ext_list,
497                                   pool->events[i].ext_list_token);
498                 }
499         }
500         if (in_use)
501                 dev_warn(hostdata->dev, "releasing event pool with %d "
502                          "events still in use?\n", in_use);
503         kfree(pool->events);
504         dma_free_coherent(hostdata->dev,
505                           pool->size * sizeof(*pool->iu_storage),
506                           pool->iu_storage, pool->iu_token);
507 }
508
509 /**
510  * valid_event_struct: - Determines if event is valid.
511  * @pool:       event_pool that contains the event
512  * @evt:        srp_event_struct to be checked for validity
513  *
514  * Returns zero if event is invalid, one otherwise.
515 */
516 static int valid_event_struct(struct event_pool *pool,
517                                 struct srp_event_struct *evt)
518 {
519         int index = evt - pool->events;
520         if (index < 0 || index >= pool->size)   /* outside of bounds */
521                 return 0;
522         if (evt != pool->events + index)        /* unaligned */
523                 return 0;
524         return 1;
525 }
526
527 /**
528  * ibmvscsi_free-event_struct: - Changes status of event to "free"
529  * @pool:       event_pool that contains the event
530  * @evt:        srp_event_struct to be modified
531  *
532 */
533 static void free_event_struct(struct event_pool *pool,
534                                        struct srp_event_struct *evt)
535 {
536         if (!valid_event_struct(pool, evt)) {
537                 dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
538                         "(not in pool %p)\n", evt, pool->events);
539                 return;
540         }
541         if (atomic_inc_return(&evt->free) != 1) {
542                 dev_err(evt->hostdata->dev, "Freeing event_struct %p "
543                         "which is not in use!\n", evt);
544                 return;
545         }
546 }
547
548 /**
549  * get_evt_struct: - Gets the next free event in pool
550  * @pool:       event_pool that contains the events to be searched
551  *
552  * Returns the next event in "free" state, and NULL if none are free.
553  * Note that no synchronization is done here, we assume the host_lock
554  * will syncrhonze things.
555 */
556 static struct srp_event_struct *get_event_struct(struct event_pool *pool)
557 {
558         int i;
559         int poolsize = pool->size;
560         int offset = pool->next;
561
562         for (i = 0; i < poolsize; i++) {
563                 offset = (offset + 1) % poolsize;
564                 if (!atomic_dec_if_positive(&pool->events[offset].free)) {
565                         pool->next = offset;
566                         return &pool->events[offset];
567                 }
568         }
569
570         printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
571         return NULL;
572 }
573
574 /**
575  * init_event_struct: Initialize fields in an event struct that are always 
576  *                    required.
577  * @evt:        The event
578  * @done:       Routine to call when the event is responded to
579  * @format:     SRP or MAD format
580  * @timeout:    timeout value set in the CRQ
581  */
582 static void init_event_struct(struct srp_event_struct *evt_struct,
583                               void (*done) (struct srp_event_struct *),
584                               u8 format,
585                               int timeout)
586 {
587         evt_struct->cmnd = NULL;
588         evt_struct->cmnd_done = NULL;
589         evt_struct->sync_srp = NULL;
590         evt_struct->crq.format = format;
591         evt_struct->crq.timeout = timeout;
592         evt_struct->done = done;
593 }
594
595 /* ------------------------------------------------------------
596  * Routines for receiving SCSI responses from the hosting partition
597  */
598
599 /**
600  * set_srp_direction: Set the fields in the srp related to data
601  *     direction and number of buffers based on the direction in
602  *     the scsi_cmnd and the number of buffers
603  */
604 static void set_srp_direction(struct scsi_cmnd *cmd,
605                               struct srp_cmd *srp_cmd, 
606                               int numbuf)
607 {
608         u8 fmt;
609
610         if (numbuf == 0)
611                 return;
612         
613         if (numbuf == 1)
614                 fmt = SRP_DATA_DESC_DIRECT;
615         else {
616                 fmt = SRP_DATA_DESC_INDIRECT;
617                 numbuf = min(numbuf, MAX_INDIRECT_BUFS);
618
619                 if (cmd->sc_data_direction == DMA_TO_DEVICE)
620                         srp_cmd->data_out_desc_cnt = numbuf;
621                 else
622                         srp_cmd->data_in_desc_cnt = numbuf;
623         }
624
625         if (cmd->sc_data_direction == DMA_TO_DEVICE)
626                 srp_cmd->buf_fmt = fmt << 4;
627         else
628                 srp_cmd->buf_fmt = fmt;
629 }
630
631 /**
632  * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
633  * @cmd:        srp_cmd whose additional_data member will be unmapped
634  * @dev:        device for which the memory is mapped
635  *
636 */
637 static void unmap_cmd_data(struct srp_cmd *cmd,
638                            struct srp_event_struct *evt_struct,
639                            struct device *dev)
640 {
641         u8 out_fmt, in_fmt;
642
643         out_fmt = cmd->buf_fmt >> 4;
644         in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
645
646         if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
647                 return;
648
649         if (evt_struct->cmnd)
650                 scsi_dma_unmap(evt_struct->cmnd);
651 }
652
653 static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
654                        struct srp_direct_buf *md)
655 {
656         int i;
657         struct scatterlist *sg;
658         u64 total_length = 0;
659
660         scsi_for_each_sg(cmd, sg, nseg, i) {
661                 struct srp_direct_buf *descr = md + i;
662                 descr->va = sg_dma_address(sg);
663                 descr->len = sg_dma_len(sg);
664                 descr->key = 0;
665                 total_length += sg_dma_len(sg);
666         }
667         return total_length;
668 }
669
670 /**
671  * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
672  * @cmd:        Scsi_Cmnd with the scatterlist
673  * @srp_cmd:    srp_cmd that contains the memory descriptor
674  * @dev:        device for which to map dma memory
675  *
676  * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
677  * Returns 1 on success.
678 */
679 static int map_sg_data(struct scsi_cmnd *cmd,
680                        struct srp_event_struct *evt_struct,
681                        struct srp_cmd *srp_cmd, struct device *dev)
682 {
683
684         int sg_mapped;
685         u64 total_length = 0;
686         struct srp_direct_buf *data =
687                 (struct srp_direct_buf *) srp_cmd->add_data;
688         struct srp_indirect_buf *indirect =
689                 (struct srp_indirect_buf *) data;
690
691         sg_mapped = scsi_dma_map(cmd);
692         if (!sg_mapped)
693                 return 1;
694         else if (sg_mapped < 0)
695                 return 0;
696
697         set_srp_direction(cmd, srp_cmd, sg_mapped);
698
699         /* special case; we can use a single direct descriptor */
700         if (sg_mapped == 1) {
701                 map_sg_list(cmd, sg_mapped, data);
702                 return 1;
703         }
704
705         indirect->table_desc.va = 0;
706         indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
707         indirect->table_desc.key = 0;
708
709         if (sg_mapped <= MAX_INDIRECT_BUFS) {
710                 total_length = map_sg_list(cmd, sg_mapped,
711                                            &indirect->desc_list[0]);
712                 indirect->len = total_length;
713                 return 1;
714         }
715
716         /* get indirect table */
717         if (!evt_struct->ext_list) {
718                 evt_struct->ext_list = (struct srp_direct_buf *)
719                         dma_alloc_coherent(dev,
720                                            SG_ALL * sizeof(struct srp_direct_buf),
721                                            &evt_struct->ext_list_token, 0);
722                 if (!evt_struct->ext_list) {
723                         if (!firmware_has_feature(FW_FEATURE_CMO))
724                                 sdev_printk(KERN_ERR, cmd->device,
725                                             "Can't allocate memory "
726                                             "for indirect table\n");
727                         scsi_dma_unmap(cmd);
728                         return 0;
729                 }
730         }
731
732         total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
733
734         indirect->len = total_length;
735         indirect->table_desc.va = evt_struct->ext_list_token;
736         indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
737         memcpy(indirect->desc_list, evt_struct->ext_list,
738                MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
739         return 1;
740 }
741
742 /**
743  * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
744  * @cmd:        struct scsi_cmnd with the memory to be mapped
745  * @srp_cmd:    srp_cmd that contains the memory descriptor
746  * @dev:        dma device for which to map dma memory
747  *
748  * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds 
749  * Returns 1 on success.
750 */
751 static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
752                                 struct srp_event_struct *evt_struct,
753                                 struct srp_cmd *srp_cmd, struct device *dev)
754 {
755         switch (cmd->sc_data_direction) {
756         case DMA_FROM_DEVICE:
757         case DMA_TO_DEVICE:
758                 break;
759         case DMA_NONE:
760                 return 1;
761         case DMA_BIDIRECTIONAL:
762                 sdev_printk(KERN_ERR, cmd->device,
763                             "Can't map DMA_BIDIRECTIONAL to read/write\n");
764                 return 0;
765         default:
766                 sdev_printk(KERN_ERR, cmd->device,
767                             "Unknown data direction 0x%02x; can't map!\n",
768                             cmd->sc_data_direction);
769                 return 0;
770         }
771
772         return map_sg_data(cmd, evt_struct, srp_cmd, dev);
773 }
774
775 /**
776  * purge_requests: Our virtual adapter just shut down.  purge any sent requests
777  * @hostdata:    the adapter
778  */
779 static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
780 {
781         struct srp_event_struct *evt;
782         unsigned long flags;
783
784         spin_lock_irqsave(hostdata->host->host_lock, flags);
785         while (!list_empty(&hostdata->sent)) {
786                 evt = list_first_entry(&hostdata->sent, struct srp_event_struct, list);
787                 list_del(&evt->list);
788                 del_timer(&evt->timer);
789
790                 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
791                 if (evt->cmnd) {
792                         evt->cmnd->result = (error_code << 16);
793                         unmap_cmd_data(&evt->iu.srp.cmd, evt,
794                                        evt->hostdata->dev);
795                         if (evt->cmnd_done)
796                                 evt->cmnd_done(evt->cmnd);
797                 } else if (evt->done && evt->crq.format != VIOSRP_MAD_FORMAT &&
798                            evt->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
799                         evt->done(evt);
800                 free_event_struct(&evt->hostdata->pool, evt);
801                 spin_lock_irqsave(hostdata->host->host_lock, flags);
802         }
803         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
804 }
805
806 /**
807  * ibmvscsi_reset_host - Reset the connection to the server
808  * @hostdata:   struct ibmvscsi_host_data to reset
809 */
810 static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
811 {
812         scsi_block_requests(hostdata->host);
813         atomic_set(&hostdata->request_limit, 0);
814
815         purge_requests(hostdata, DID_ERROR);
816         hostdata->reset_crq = 1;
817         wake_up(&hostdata->work_wait_q);
818 }
819
820 /**
821  * ibmvscsi_timeout - Internal command timeout handler
822  * @evt_struct: struct srp_event_struct that timed out
823  *
824  * Called when an internally generated command times out
825 */
826 static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
827 {
828         struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
829
830         dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
831                 evt_struct->iu.srp.cmd.opcode);
832
833         ibmvscsi_reset_host(hostdata);
834 }
835
836
837 /* ------------------------------------------------------------
838  * Routines for sending and receiving SRPs
839  */
840 /**
841  * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
842  * @evt_struct: evt_struct to be sent
843  * @hostdata:   ibmvscsi_host_data of host
844  * @timeout:    timeout in seconds - 0 means do not time command
845  *
846  * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
847  * Note that this routine assumes that host_lock is held for synchronization
848 */
849 static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
850                                    struct ibmvscsi_host_data *hostdata,
851                                    unsigned long timeout)
852 {
853         u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
854         int request_status = 0;
855         int rc;
856         int srp_req = 0;
857
858         /* If we have exhausted our request limit, just fail this request,
859          * unless it is for a reset or abort.
860          * Note that there are rare cases involving driver generated requests 
861          * (such as task management requests) that the mid layer may think we
862          * can handle more requests (can_queue) when we actually can't
863          */
864         if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
865                 srp_req = 1;
866                 request_status =
867                         atomic_dec_if_positive(&hostdata->request_limit);
868                 /* If request limit was -1 when we started, it is now even
869                  * less than that
870                  */
871                 if (request_status < -1)
872                         goto send_error;
873                 /* Otherwise, we may have run out of requests. */
874                 /* If request limit was 0 when we started the adapter is in the
875                  * process of performing a login with the server adapter, or
876                  * we may have run out of requests.
877                  */
878                 else if (request_status == -1 &&
879                          evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
880                         goto send_busy;
881                 /* Abort and reset calls should make it through.
882                  * Nothing except abort and reset should use the last two
883                  * slots unless we had two or less to begin with.
884                  */
885                 else if (request_status < 2 &&
886                          evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
887                         /* In the case that we have less than two requests
888                          * available, check the server limit as a combination
889                          * of the request limit and the number of requests
890                          * in-flight (the size of the send list).  If the
891                          * server limit is greater than 2, return busy so
892                          * that the last two are reserved for reset and abort.
893                          */
894                         int server_limit = request_status;
895                         struct srp_event_struct *tmp_evt;
896
897                         list_for_each_entry(tmp_evt, &hostdata->sent, list) {
898                                 server_limit++;
899                         }
900
901                         if (server_limit > 2)
902                                 goto send_busy;
903                 }
904         }
905
906         /* Copy the IU into the transfer area */
907         *evt_struct->xfer_iu = evt_struct->iu;
908         evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
909
910         /* Add this to the sent list.  We need to do this 
911          * before we actually send 
912          * in case it comes back REALLY fast
913          */
914         list_add_tail(&evt_struct->list, &hostdata->sent);
915
916         init_timer(&evt_struct->timer);
917         if (timeout) {
918                 evt_struct->timer.data = (unsigned long) evt_struct;
919                 evt_struct->timer.expires = jiffies + (timeout * HZ);
920                 evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
921                 add_timer(&evt_struct->timer);
922         }
923
924         if ((rc =
925              ibmvscsi_send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
926                 list_del(&evt_struct->list);
927                 del_timer(&evt_struct->timer);
928
929                 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
930                  * Firmware will send a CRQ with a transport event (0xFF) to
931                  * tell this client what has happened to the transport.  This
932                  * will be handled in ibmvscsi_handle_crq()
933                  */
934                 if (rc == H_CLOSED) {
935                         dev_warn(hostdata->dev, "send warning. "
936                                  "Receive queue closed, will retry.\n");
937                         goto send_busy;
938                 }
939                 dev_err(hostdata->dev, "send error %d\n", rc);
940                 if (srp_req)
941                         atomic_inc(&hostdata->request_limit);
942                 goto send_error;
943         }
944
945         return 0;
946
947  send_busy:
948         unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
949
950         free_event_struct(&hostdata->pool, evt_struct);
951         if (srp_req && request_status != -1)
952                 atomic_inc(&hostdata->request_limit);
953         return SCSI_MLQUEUE_HOST_BUSY;
954
955  send_error:
956         unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
957
958         if (evt_struct->cmnd != NULL) {
959                 evt_struct->cmnd->result = DID_ERROR << 16;
960                 evt_struct->cmnd_done(evt_struct->cmnd);
961         } else if (evt_struct->done)
962                 evt_struct->done(evt_struct);
963
964         free_event_struct(&hostdata->pool, evt_struct);
965         return 0;
966 }
967
968 /**
969  * handle_cmd_rsp: -  Handle responses from commands
970  * @evt_struct: srp_event_struct to be handled
971  *
972  * Used as a callback by when sending scsi cmds.
973  * Gets called by ibmvscsi_handle_crq()
974 */
975 static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
976 {
977         struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
978         struct scsi_cmnd *cmnd = evt_struct->cmnd;
979
980         if (unlikely(rsp->opcode != SRP_RSP)) {
981                 if (printk_ratelimit())
982                         dev_warn(evt_struct->hostdata->dev,
983                                  "bad SRP RSP type %d\n", rsp->opcode);
984         }
985         
986         if (cmnd) {
987                 cmnd->result |= rsp->status;
988                 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
989                         memcpy(cmnd->sense_buffer,
990                                rsp->data,
991                                rsp->sense_data_len);
992                 unmap_cmd_data(&evt_struct->iu.srp.cmd, 
993                                evt_struct, 
994                                evt_struct->hostdata->dev);
995
996                 if (rsp->flags & SRP_RSP_FLAG_DOOVER)
997                         scsi_set_resid(cmnd, rsp->data_out_res_cnt);
998                 else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
999                         scsi_set_resid(cmnd, rsp->data_in_res_cnt);
1000         }
1001
1002         if (evt_struct->cmnd_done)
1003                 evt_struct->cmnd_done(cmnd);
1004 }
1005
1006 /**
1007  * lun_from_dev: - Returns the lun of the scsi device
1008  * @dev:        struct scsi_device
1009  *
1010 */
1011 static inline u16 lun_from_dev(struct scsi_device *dev)
1012 {
1013         return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
1014 }
1015
1016 /**
1017  * ibmvscsi_queue: - The queuecommand function of the scsi template 
1018  * @cmd:        struct scsi_cmnd to be executed
1019  * @done:       Callback function to be called when cmd is completed
1020 */
1021 static int ibmvscsi_queuecommand_lck(struct scsi_cmnd *cmnd,
1022                                  void (*done) (struct scsi_cmnd *))
1023 {
1024         struct srp_cmd *srp_cmd;
1025         struct srp_event_struct *evt_struct;
1026         struct srp_indirect_buf *indirect;
1027         struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
1028         u16 lun = lun_from_dev(cmnd->device);
1029         u8 out_fmt, in_fmt;
1030
1031         cmnd->result = (DID_OK << 16);
1032         evt_struct = get_event_struct(&hostdata->pool);
1033         if (!evt_struct)
1034                 return SCSI_MLQUEUE_HOST_BUSY;
1035
1036         /* Set up the actual SRP IU */
1037         srp_cmd = &evt_struct->iu.srp.cmd;
1038         memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
1039         srp_cmd->opcode = SRP_CMD;
1040         memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
1041         srp_cmd->lun = ((u64) lun) << 48;
1042
1043         if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
1044                 if (!firmware_has_feature(FW_FEATURE_CMO))
1045                         sdev_printk(KERN_ERR, cmnd->device,
1046                                     "couldn't convert cmd to srp_cmd\n");
1047                 free_event_struct(&hostdata->pool, evt_struct);
1048                 return SCSI_MLQUEUE_HOST_BUSY;
1049         }
1050
1051         init_event_struct(evt_struct,
1052                           handle_cmd_rsp,
1053                           VIOSRP_SRP_FORMAT,
1054                           cmnd->request->timeout/HZ);
1055
1056         evt_struct->cmnd = cmnd;
1057         evt_struct->cmnd_done = done;
1058
1059         /* Fix up dma address of the buffer itself */
1060         indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
1061         out_fmt = srp_cmd->buf_fmt >> 4;
1062         in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
1063         if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
1064              out_fmt == SRP_DATA_DESC_INDIRECT) &&
1065             indirect->table_desc.va == 0) {
1066                 indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
1067                         offsetof(struct srp_cmd, add_data) +
1068                         offsetof(struct srp_indirect_buf, desc_list);
1069         }
1070
1071         return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
1072 }
1073
1074 static DEF_SCSI_QCMD(ibmvscsi_queuecommand)
1075
1076 /* ------------------------------------------------------------
1077  * Routines for driver initialization
1078  */
1079
1080 /**
1081  * map_persist_bufs: - Pre-map persistent data for adapter logins
1082  * @hostdata:   ibmvscsi_host_data of host
1083  *
1084  * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
1085  * Return 1 on error, 0 on success.
1086  */
1087 static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
1088 {
1089
1090         hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
1091                                              sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
1092
1093         if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
1094                 dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
1095                 return 1;
1096         }
1097
1098         hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
1099                                                      &hostdata->madapter_info,
1100                                                      sizeof(hostdata->madapter_info),
1101                                                      DMA_BIDIRECTIONAL);
1102         if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
1103                 dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
1104                 dma_unmap_single(hostdata->dev, hostdata->caps_addr,
1105                                  sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
1106                 return 1;
1107         }
1108
1109         return 0;
1110 }
1111
1112 /**
1113  * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
1114  * @hostdata:   ibmvscsi_host_data of host
1115  *
1116  * Unmap the capabilities and adapter info DMA buffers
1117  */
1118 static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
1119 {
1120         dma_unmap_single(hostdata->dev, hostdata->caps_addr,
1121                          sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
1122
1123         dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
1124                          sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
1125 }
1126
1127 /**
1128  * login_rsp: - Handle response to SRP login request
1129  * @evt_struct: srp_event_struct with the response
1130  *
1131  * Used as a "done" callback by when sending srp_login. Gets called
1132  * by ibmvscsi_handle_crq()
1133 */
1134 static void login_rsp(struct srp_event_struct *evt_struct)
1135 {
1136         struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1137         switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
1138         case SRP_LOGIN_RSP:     /* it worked! */
1139                 break;
1140         case SRP_LOGIN_REJ:     /* refused! */
1141                 dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
1142                          evt_struct->xfer_iu->srp.login_rej.reason);
1143                 /* Login failed.  */
1144                 atomic_set(&hostdata->request_limit, -1);
1145                 return;
1146         default:
1147                 dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
1148                         evt_struct->xfer_iu->srp.login_rsp.opcode);
1149                 /* Login failed.  */
1150                 atomic_set(&hostdata->request_limit, -1);
1151                 return;
1152         }
1153
1154         dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
1155         hostdata->client_migrated = 0;
1156
1157         /* Now we know what the real request-limit is.
1158          * This value is set rather than added to request_limit because
1159          * request_limit could have been set to -1 by this client.
1160          */
1161         atomic_set(&hostdata->request_limit,
1162                    evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
1163
1164         /* If we had any pending I/Os, kick them */
1165         scsi_unblock_requests(hostdata->host);
1166 }
1167
1168 /**
1169  * send_srp_login: - Sends the srp login
1170  * @hostdata:   ibmvscsi_host_data of host
1171  *
1172  * Returns zero if successful.
1173 */
1174 static int send_srp_login(struct ibmvscsi_host_data *hostdata)
1175 {
1176         int rc;
1177         unsigned long flags;
1178         struct srp_login_req *login;
1179         struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
1180
1181         BUG_ON(!evt_struct);
1182         init_event_struct(evt_struct, login_rsp,
1183                           VIOSRP_SRP_FORMAT, login_timeout);
1184
1185         login = &evt_struct->iu.srp.login_req;
1186         memset(login, 0, sizeof(*login));
1187         login->opcode = SRP_LOGIN_REQ;
1188         login->req_it_iu_len = sizeof(union srp_iu);
1189         login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
1190
1191         spin_lock_irqsave(hostdata->host->host_lock, flags);
1192         /* Start out with a request limit of 0, since this is negotiated in
1193          * the login request we are just sending and login requests always
1194          * get sent by the driver regardless of request_limit.
1195          */
1196         atomic_set(&hostdata->request_limit, 0);
1197
1198         rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
1199         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1200         dev_info(hostdata->dev, "sent SRP login\n");
1201         return rc;
1202 };
1203
1204 /**
1205  * capabilities_rsp: - Handle response to MAD adapter capabilities request
1206  * @evt_struct: srp_event_struct with the response
1207  *
1208  * Used as a "done" callback by when sending adapter_info.
1209  */
1210 static void capabilities_rsp(struct srp_event_struct *evt_struct)
1211 {
1212         struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1213
1214         if (evt_struct->xfer_iu->mad.capabilities.common.status) {
1215                 dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
1216                         evt_struct->xfer_iu->mad.capabilities.common.status);
1217         } else {
1218                 if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
1219                         dev_info(hostdata->dev, "Partition migration not supported\n");
1220
1221                 if (client_reserve) {
1222                         if (hostdata->caps.reserve.common.server_support ==
1223                             SERVER_SUPPORTS_CAP)
1224                                 dev_info(hostdata->dev, "Client reserve enabled\n");
1225                         else
1226                                 dev_info(hostdata->dev, "Client reserve not supported\n");
1227                 }
1228         }
1229
1230         send_srp_login(hostdata);
1231 }
1232
1233 /**
1234  * send_mad_capabilities: - Sends the mad capabilities request
1235  *      and stores the result so it can be retrieved with
1236  * @hostdata:   ibmvscsi_host_data of host
1237  */
1238 static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
1239 {
1240         struct viosrp_capabilities *req;
1241         struct srp_event_struct *evt_struct;
1242         unsigned long flags;
1243         struct device_node *of_node = hostdata->dev->of_node;
1244         const char *location;
1245
1246         evt_struct = get_event_struct(&hostdata->pool);
1247         BUG_ON(!evt_struct);
1248
1249         init_event_struct(evt_struct, capabilities_rsp,
1250                           VIOSRP_MAD_FORMAT, info_timeout);
1251
1252         req = &evt_struct->iu.mad.capabilities;
1253         memset(req, 0, sizeof(*req));
1254
1255         hostdata->caps.flags = CAP_LIST_SUPPORTED;
1256         if (hostdata->client_migrated)
1257                 hostdata->caps.flags |= CLIENT_MIGRATED;
1258
1259         strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
1260                 sizeof(hostdata->caps.name));
1261         hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
1262
1263         location = of_get_property(of_node, "ibm,loc-code", NULL);
1264         location = location ? location : dev_name(hostdata->dev);
1265         strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
1266         hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
1267
1268         req->common.type = VIOSRP_CAPABILITIES_TYPE;
1269         req->buffer = hostdata->caps_addr;
1270
1271         hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
1272         hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
1273         hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
1274         hostdata->caps.migration.ecl = 1;
1275
1276         if (client_reserve) {
1277                 hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
1278                 hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
1279                 hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
1280                 hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
1281                 req->common.length = sizeof(hostdata->caps);
1282         } else
1283                 req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);
1284
1285         spin_lock_irqsave(hostdata->host->host_lock, flags);
1286         if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1287                 dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
1288         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1289 };
1290
1291 /**
1292  * fast_fail_rsp: - Handle response to MAD enable fast fail
1293  * @evt_struct: srp_event_struct with the response
1294  *
1295  * Used as a "done" callback by when sending enable fast fail. Gets called
1296  * by ibmvscsi_handle_crq()
1297  */
1298 static void fast_fail_rsp(struct srp_event_struct *evt_struct)
1299 {
1300         struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1301         u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
1302
1303         if (status == VIOSRP_MAD_NOT_SUPPORTED)
1304                 dev_err(hostdata->dev, "fast_fail not supported in server\n");
1305         else if (status == VIOSRP_MAD_FAILED)
1306                 dev_err(hostdata->dev, "fast_fail request failed\n");
1307         else if (status != VIOSRP_MAD_SUCCESS)
1308                 dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
1309
1310         send_mad_capabilities(hostdata);
1311 }
1312
1313 /**
1314  * init_host - Start host initialization
1315  * @hostdata:   ibmvscsi_host_data of host
1316  *
1317  * Returns zero if successful.
1318  */
1319 static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
1320 {
1321         int rc;
1322         unsigned long flags;
1323         struct viosrp_fast_fail *fast_fail_mad;
1324         struct srp_event_struct *evt_struct;
1325
1326         if (!fast_fail) {
1327                 send_mad_capabilities(hostdata);
1328                 return 0;
1329         }
1330
1331         evt_struct = get_event_struct(&hostdata->pool);
1332         BUG_ON(!evt_struct);
1333
1334         init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
1335
1336         fast_fail_mad = &evt_struct->iu.mad.fast_fail;
1337         memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
1338         fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
1339         fast_fail_mad->common.length = sizeof(*fast_fail_mad);
1340
1341         spin_lock_irqsave(hostdata->host->host_lock, flags);
1342         rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1343         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1344         return rc;
1345 }
1346
1347 /**
1348  * adapter_info_rsp: - Handle response to MAD adapter info request
1349  * @evt_struct: srp_event_struct with the response
1350  *
1351  * Used as a "done" callback by when sending adapter_info. Gets called
1352  * by ibmvscsi_handle_crq()
1353 */
1354 static void adapter_info_rsp(struct srp_event_struct *evt_struct)
1355 {
1356         struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1357
1358         if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
1359                 dev_err(hostdata->dev, "error %d getting adapter info\n",
1360                         evt_struct->xfer_iu->mad.adapter_info.common.status);
1361         } else {
1362                 dev_info(hostdata->dev, "host srp version: %s, "
1363                          "host partition %s (%d), OS %d, max io %u\n",
1364                          hostdata->madapter_info.srp_version,
1365                          hostdata->madapter_info.partition_name,
1366                          hostdata->madapter_info.partition_number,
1367                          hostdata->madapter_info.os_type,
1368                          hostdata->madapter_info.port_max_txu[0]);
1369                 
1370                 if (hostdata->madapter_info.port_max_txu[0]) 
1371                         hostdata->host->max_sectors = 
1372                                 hostdata->madapter_info.port_max_txu[0] >> 9;
1373                 
1374                 if (hostdata->madapter_info.os_type == 3 &&
1375                     strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
1376                         dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
1377                                 hostdata->madapter_info.srp_version);
1378                         dev_err(hostdata->dev, "limiting scatterlists to %d\n",
1379                                 MAX_INDIRECT_BUFS);
1380                         hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
1381                 }
1382
1383                 if (hostdata->madapter_info.os_type == 3) {
1384                         enable_fast_fail(hostdata);
1385                         return;
1386                 }
1387         }
1388
1389         send_srp_login(hostdata);
1390 }
1391
1392 /**
1393  * send_mad_adapter_info: - Sends the mad adapter info request
1394  *      and stores the result so it can be retrieved with
1395  *      sysfs.  We COULD consider causing a failure if the
1396  *      returned SRP version doesn't match ours.
1397  * @hostdata:   ibmvscsi_host_data of host
1398  * 
1399  * Returns zero if successful.
1400 */
1401 static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
1402 {
1403         struct viosrp_adapter_info *req;
1404         struct srp_event_struct *evt_struct;
1405         unsigned long flags;
1406
1407         evt_struct = get_event_struct(&hostdata->pool);
1408         BUG_ON(!evt_struct);
1409
1410         init_event_struct(evt_struct,
1411                           adapter_info_rsp,
1412                           VIOSRP_MAD_FORMAT,
1413                           info_timeout);
1414         
1415         req = &evt_struct->iu.mad.adapter_info;
1416         memset(req, 0x00, sizeof(*req));
1417         
1418         req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
1419         req->common.length = sizeof(hostdata->madapter_info);
1420         req->buffer = hostdata->adapter_info_addr;
1421
1422         spin_lock_irqsave(hostdata->host->host_lock, flags);
1423         if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1424                 dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
1425         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1426 };
1427
1428 /**
1429  * init_adapter: Start virtual adapter initialization sequence
1430  *
1431  */
1432 static void init_adapter(struct ibmvscsi_host_data *hostdata)
1433 {
1434         send_mad_adapter_info(hostdata);
1435 }
1436
1437 /**
1438  * sync_completion: Signal that a synchronous command has completed
1439  * Note that after returning from this call, the evt_struct is freed.
1440  * the caller waiting on this completion shouldn't touch the evt_struct
1441  * again.
1442  */
1443 static void sync_completion(struct srp_event_struct *evt_struct)
1444 {
1445         /* copy the response back */
1446         if (evt_struct->sync_srp)
1447                 *evt_struct->sync_srp = *evt_struct->xfer_iu;
1448         
1449         complete(&evt_struct->comp);
1450 }
1451
1452 /**
1453  * ibmvscsi_abort: Abort a command...from scsi host template
1454  * send this over to the server and wait synchronously for the response
1455  */
1456 static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
1457 {
1458         struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1459         struct srp_tsk_mgmt *tsk_mgmt;
1460         struct srp_event_struct *evt;
1461         struct srp_event_struct *tmp_evt, *found_evt;
1462         union viosrp_iu srp_rsp;
1463         int rsp_rc;
1464         unsigned long flags;
1465         u16 lun = lun_from_dev(cmd->device);
1466         unsigned long wait_switch = 0;
1467
1468         /* First, find this command in our sent list so we can figure
1469          * out the correct tag
1470          */
1471         spin_lock_irqsave(hostdata->host->host_lock, flags);
1472         wait_switch = jiffies + (init_timeout * HZ);
1473         do {
1474                 found_evt = NULL;
1475                 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1476                         if (tmp_evt->cmnd == cmd) {
1477                                 found_evt = tmp_evt;
1478                                 break;
1479                         }
1480                 }
1481
1482                 if (!found_evt) {
1483                         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1484                         return SUCCESS;
1485                 }
1486
1487                 evt = get_event_struct(&hostdata->pool);
1488                 if (evt == NULL) {
1489                         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1490                         sdev_printk(KERN_ERR, cmd->device,
1491                                 "failed to allocate abort event\n");
1492                         return FAILED;
1493                 }
1494         
1495                 init_event_struct(evt,
1496                                   sync_completion,
1497                                   VIOSRP_SRP_FORMAT,
1498                                   abort_timeout);
1499
1500                 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1501         
1502                 /* Set up an abort SRP command */
1503                 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1504                 tsk_mgmt->opcode = SRP_TSK_MGMT;
1505                 tsk_mgmt->lun = ((u64) lun) << 48;
1506                 tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
1507                 tsk_mgmt->task_tag = (u64) found_evt;
1508
1509                 evt->sync_srp = &srp_rsp;
1510
1511                 init_completion(&evt->comp);
1512                 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
1513
1514                 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1515                         break;
1516
1517                 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1518                 msleep(10);
1519                 spin_lock_irqsave(hostdata->host->host_lock, flags);
1520         } while (time_before(jiffies, wait_switch));
1521
1522         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1523
1524         if (rsp_rc != 0) {
1525                 sdev_printk(KERN_ERR, cmd->device,
1526                             "failed to send abort() event. rc=%d\n", rsp_rc);
1527                 return FAILED;
1528         }
1529
1530         sdev_printk(KERN_INFO, cmd->device,
1531                     "aborting command. lun 0x%llx, tag 0x%llx\n",
1532                     (((u64) lun) << 48), (u64) found_evt);
1533
1534         wait_for_completion(&evt->comp);
1535
1536         /* make sure we got a good response */
1537         if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1538                 if (printk_ratelimit())
1539                         sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
1540                                     srp_rsp.srp.rsp.opcode);
1541                 return FAILED;
1542         }
1543
1544         if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1545                 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1546         else
1547                 rsp_rc = srp_rsp.srp.rsp.status;
1548
1549         if (rsp_rc) {
1550                 if (printk_ratelimit())
1551                         sdev_printk(KERN_WARNING, cmd->device,
1552                                     "abort code %d for task tag 0x%llx\n",
1553                                     rsp_rc, tsk_mgmt->task_tag);
1554                 return FAILED;
1555         }
1556
1557         /* Because we dropped the spinlock above, it's possible
1558          * The event is no longer in our list.  Make sure it didn't
1559          * complete while we were aborting
1560          */
1561         spin_lock_irqsave(hostdata->host->host_lock, flags);
1562         found_evt = NULL;
1563         list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1564                 if (tmp_evt->cmnd == cmd) {
1565                         found_evt = tmp_evt;
1566                         break;
1567                 }
1568         }
1569
1570         if (found_evt == NULL) {
1571                 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1572                 sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
1573                             tsk_mgmt->task_tag);
1574                 return SUCCESS;
1575         }
1576
1577         sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
1578                     tsk_mgmt->task_tag);
1579
1580         cmd->result = (DID_ABORT << 16);
1581         list_del(&found_evt->list);
1582         unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1583                        found_evt->hostdata->dev);
1584         free_event_struct(&found_evt->hostdata->pool, found_evt);
1585         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1586         atomic_inc(&hostdata->request_limit);
1587         return SUCCESS;
1588 }
1589
1590 /**
1591  * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host 
1592  * template send this over to the server and wait synchronously for the 
1593  * response
1594  */
1595 static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1596 {
1597         struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1598         struct srp_tsk_mgmt *tsk_mgmt;
1599         struct srp_event_struct *evt;
1600         struct srp_event_struct *tmp_evt, *pos;
1601         union viosrp_iu srp_rsp;
1602         int rsp_rc;
1603         unsigned long flags;
1604         u16 lun = lun_from_dev(cmd->device);
1605         unsigned long wait_switch = 0;
1606
1607         spin_lock_irqsave(hostdata->host->host_lock, flags);
1608         wait_switch = jiffies + (init_timeout * HZ);
1609         do {
1610                 evt = get_event_struct(&hostdata->pool);
1611                 if (evt == NULL) {
1612                         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1613                         sdev_printk(KERN_ERR, cmd->device,
1614                                 "failed to allocate reset event\n");
1615                         return FAILED;
1616                 }
1617         
1618                 init_event_struct(evt,
1619                                   sync_completion,
1620                                   VIOSRP_SRP_FORMAT,
1621                                   reset_timeout);
1622
1623                 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1624
1625                 /* Set up a lun reset SRP command */
1626                 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1627                 tsk_mgmt->opcode = SRP_TSK_MGMT;
1628                 tsk_mgmt->lun = ((u64) lun) << 48;
1629                 tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
1630
1631                 evt->sync_srp = &srp_rsp;
1632
1633                 init_completion(&evt->comp);
1634                 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
1635
1636                 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1637                         break;
1638
1639                 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1640                 msleep(10);
1641                 spin_lock_irqsave(hostdata->host->host_lock, flags);
1642         } while (time_before(jiffies, wait_switch));
1643
1644         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1645
1646         if (rsp_rc != 0) {
1647                 sdev_printk(KERN_ERR, cmd->device,
1648                             "failed to send reset event. rc=%d\n", rsp_rc);
1649                 return FAILED;
1650         }
1651
1652         sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
1653                     (((u64) lun) << 48));
1654
1655         wait_for_completion(&evt->comp);
1656
1657         /* make sure we got a good response */
1658         if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1659                 if (printk_ratelimit())
1660                         sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
1661                                     srp_rsp.srp.rsp.opcode);
1662                 return FAILED;
1663         }
1664
1665         if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1666                 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1667         else
1668                 rsp_rc = srp_rsp.srp.rsp.status;
1669
1670         if (rsp_rc) {
1671                 if (printk_ratelimit())
1672                         sdev_printk(KERN_WARNING, cmd->device,
1673                                     "reset code %d for task tag 0x%llx\n",
1674                                     rsp_rc, tsk_mgmt->task_tag);
1675                 return FAILED;
1676         }
1677
1678         /* We need to find all commands for this LUN that have not yet been
1679          * responded to, and fail them with DID_RESET
1680          */
1681         spin_lock_irqsave(hostdata->host->host_lock, flags);
1682         list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1683                 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1684                         if (tmp_evt->cmnd)
1685                                 tmp_evt->cmnd->result = (DID_RESET << 16);
1686                         list_del(&tmp_evt->list);
1687                         unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1688                                        tmp_evt->hostdata->dev);
1689                         free_event_struct(&tmp_evt->hostdata->pool,
1690                                                    tmp_evt);
1691                         atomic_inc(&hostdata->request_limit);
1692                         if (tmp_evt->cmnd_done)
1693                                 tmp_evt->cmnd_done(tmp_evt->cmnd);
1694                         else if (tmp_evt->done)
1695                                 tmp_evt->done(tmp_evt);
1696                 }
1697         }
1698         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1699         return SUCCESS;
1700 }
1701
1702 /**
1703  * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
1704  * @cmd:        struct scsi_cmnd having problems
1705 */
1706 static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
1707 {
1708         unsigned long wait_switch = 0;
1709         struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1710
1711         dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
1712
1713         ibmvscsi_reset_host(hostdata);
1714
1715         for (wait_switch = jiffies + (init_timeout * HZ);
1716              time_before(jiffies, wait_switch) &&
1717                      atomic_read(&hostdata->request_limit) < 2;) {
1718
1719                 msleep(10);
1720         }
1721
1722         if (atomic_read(&hostdata->request_limit) <= 0)
1723                 return FAILED;
1724
1725         return SUCCESS;
1726 }
1727
1728 /**
1729  * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
1730  * @crq:        Command/Response queue
1731  * @hostdata:   ibmvscsi_host_data of host
1732  *
1733 */
1734 static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1735                                 struct ibmvscsi_host_data *hostdata)
1736 {
1737         long rc;
1738         unsigned long flags;
1739         struct srp_event_struct *evt_struct =
1740             (struct srp_event_struct *)crq->IU_data_ptr;
1741         switch (crq->valid) {
1742         case 0xC0:              /* initialization */
1743                 switch (crq->format) {
1744                 case 0x01:      /* Initialization message */
1745                         dev_info(hostdata->dev, "partner initialized\n");
1746                         /* Send back a response */
1747                         rc = ibmvscsi_send_crq(hostdata, 0xC002000000000000LL, 0);
1748                         if (rc == 0) {
1749                                 /* Now login */
1750                                 init_adapter(hostdata);
1751                         } else {
1752                                 dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
1753                         }
1754
1755                         break;
1756                 case 0x02:      /* Initialization response */
1757                         dev_info(hostdata->dev, "partner initialization complete\n");
1758
1759                         /* Now login */
1760                         init_adapter(hostdata);
1761                         break;
1762                 default:
1763                         dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
1764                 }
1765                 return;
1766         case 0xFF:      /* Hypervisor telling us the connection is closed */
1767                 scsi_block_requests(hostdata->host);
1768                 atomic_set(&hostdata->request_limit, 0);
1769                 if (crq->format == 0x06) {
1770                         /* We need to re-setup the interpartition connection */
1771                         dev_info(hostdata->dev, "Re-enabling adapter!\n");
1772                         hostdata->client_migrated = 1;
1773                         hostdata->reenable_crq = 1;
1774                         purge_requests(hostdata, DID_REQUEUE);
1775                         wake_up(&hostdata->work_wait_q);
1776                 } else {
1777                         dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
1778                                 crq->format);
1779                         ibmvscsi_reset_host(hostdata);
1780                 }
1781                 return;
1782         case 0x80:              /* real payload */
1783                 break;
1784         default:
1785                 dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
1786                         crq->valid);
1787                 return;
1788         }
1789
1790         /* The only kind of payload CRQs we should get are responses to
1791          * things we send. Make sure this response is to something we
1792          * actually sent
1793          */
1794         if (!valid_event_struct(&hostdata->pool, evt_struct)) {
1795                 dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
1796                        (void *)crq->IU_data_ptr);
1797                 return;
1798         }
1799
1800         if (atomic_read(&evt_struct->free)) {
1801                 dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
1802                         (void *)crq->IU_data_ptr);
1803                 return;
1804         }
1805
1806         if (crq->format == VIOSRP_SRP_FORMAT)
1807                 atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
1808                            &hostdata->request_limit);
1809
1810         del_timer(&evt_struct->timer);
1811
1812         if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
1813                 evt_struct->cmnd->result = DID_ERROR << 16;
1814         if (evt_struct->done)
1815                 evt_struct->done(evt_struct);
1816         else
1817                 dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
1818
1819         /*
1820          * Lock the host_lock before messing with these structures, since we
1821          * are running in a task context
1822          */
1823         spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1824         list_del(&evt_struct->list);
1825         free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1826         spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1827 }
1828
1829 /**
1830  * ibmvscsi_get_host_config: Send the command to the server to get host
1831  * configuration data.  The data is opaque to us.
1832  */
1833 static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1834                                    unsigned char *buffer, int length)
1835 {
1836         struct viosrp_host_config *host_config;
1837         struct srp_event_struct *evt_struct;
1838         unsigned long flags;
1839         dma_addr_t addr;
1840         int rc;
1841
1842         evt_struct = get_event_struct(&hostdata->pool);
1843         if (!evt_struct) {
1844                 dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
1845                 return -1;
1846         }
1847
1848         init_event_struct(evt_struct,
1849                           sync_completion,
1850                           VIOSRP_MAD_FORMAT,
1851                           info_timeout);
1852
1853         host_config = &evt_struct->iu.mad.host_config;
1854
1855         /* The transport length field is only 16-bit */
1856         length = min(0xffff, length);
1857
1858         /* Set up a lun reset SRP command */
1859         memset(host_config, 0x00, sizeof(*host_config));
1860         host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1861         host_config->common.length = length;
1862         host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
1863                                                     length,
1864                                                     DMA_BIDIRECTIONAL);
1865
1866         if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
1867                 if (!firmware_has_feature(FW_FEATURE_CMO))
1868                         dev_err(hostdata->dev,
1869                                 "dma_mapping error getting host config\n");
1870                 free_event_struct(&hostdata->pool, evt_struct);
1871                 return -1;
1872         }
1873
1874         init_completion(&evt_struct->comp);
1875         spin_lock_irqsave(hostdata->host->host_lock, flags);
1876         rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1877         spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1878         if (rc == 0)
1879                 wait_for_completion(&evt_struct->comp);
1880         dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
1881
1882         return rc;
1883 }
1884
1885 /**
1886  * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
1887  * @sdev:       struct scsi_device device to configure
1888  *
1889  * Enable allow_restart for a device if it is a disk.  Adjust the
1890  * queue_depth here also as is required by the documentation for
1891  * struct scsi_host_template.
1892  */
1893 static int ibmvscsi_slave_configure(struct scsi_device *sdev)
1894 {
1895         struct Scsi_Host *shost = sdev->host;
1896         unsigned long lock_flags = 0;
1897
1898         spin_lock_irqsave(shost->host_lock, lock_flags);
1899         if (sdev->type == TYPE_DISK) {
1900                 sdev->allow_restart = 1;
1901                 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
1902         }
1903         spin_unlock_irqrestore(shost->host_lock, lock_flags);
1904         scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
1905         return 0;
1906 }
1907
1908 /**
1909  * ibmvscsi_change_queue_depth - Change the device's queue depth
1910  * @sdev:       scsi device struct
1911  * @qdepth:     depth to set
1912  * @reason:     calling context
1913  *
1914  * Return value:
1915  *      actual depth set
1916  **/
1917 static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
1918                                        int reason)
1919 {
1920         if (reason != SCSI_QDEPTH_DEFAULT)
1921                 return -EOPNOTSUPP;
1922
1923         if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
1924                 qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
1925
1926         scsi_adjust_queue_depth(sdev, 0, qdepth);
1927         return sdev->queue_depth;
1928 }
1929
1930 /* ------------------------------------------------------------
1931  * sysfs attributes
1932  */
1933 static ssize_t show_host_vhost_loc(struct device *dev,
1934                                    struct device_attribute *attr, char *buf)
1935 {
1936         struct Scsi_Host *shost = class_to_shost(dev);
1937         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1938         int len;
1939
1940         len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
1941                        hostdata->caps.loc);
1942         return len;
1943 }
1944
1945 static struct device_attribute ibmvscsi_host_vhost_loc = {
1946         .attr = {
1947                  .name = "vhost_loc",
1948                  .mode = S_IRUGO,
1949                  },
1950         .show = show_host_vhost_loc,
1951 };
1952
1953 static ssize_t show_host_vhost_name(struct device *dev,
1954                                     struct device_attribute *attr, char *buf)
1955 {
1956         struct Scsi_Host *shost = class_to_shost(dev);
1957         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1958         int len;
1959
1960         len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
1961                        hostdata->caps.name);
1962         return len;
1963 }
1964
1965 static struct device_attribute ibmvscsi_host_vhost_name = {
1966         .attr = {
1967                  .name = "vhost_name",
1968                  .mode = S_IRUGO,
1969                  },
1970         .show = show_host_vhost_name,
1971 };
1972
1973 static ssize_t show_host_srp_version(struct device *dev,
1974                                      struct device_attribute *attr, char *buf)
1975 {
1976         struct Scsi_Host *shost = class_to_shost(dev);
1977         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1978         int len;
1979
1980         len = snprintf(buf, PAGE_SIZE, "%s\n",
1981                        hostdata->madapter_info.srp_version);
1982         return len;
1983 }
1984
1985 static struct device_attribute ibmvscsi_host_srp_version = {
1986         .attr = {
1987                  .name = "srp_version",
1988                  .mode = S_IRUGO,
1989                  },
1990         .show = show_host_srp_version,
1991 };
1992
1993 static ssize_t show_host_partition_name(struct device *dev,
1994                                         struct device_attribute *attr,
1995                                         char *buf)
1996 {
1997         struct Scsi_Host *shost = class_to_shost(dev);
1998         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1999         int len;
2000
2001         len = snprintf(buf, PAGE_SIZE, "%s\n",
2002                        hostdata->madapter_info.partition_name);
2003         return len;
2004 }
2005
2006 static struct device_attribute ibmvscsi_host_partition_name = {
2007         .attr = {
2008                  .name = "partition_name",
2009                  .mode = S_IRUGO,
2010                  },
2011         .show = show_host_partition_name,
2012 };
2013
2014 static ssize_t show_host_partition_number(struct device *dev,
2015                                           struct device_attribute *attr,
2016                                           char *buf)
2017 {
2018         struct Scsi_Host *shost = class_to_shost(dev);
2019         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
2020         int len;
2021
2022         len = snprintf(buf, PAGE_SIZE, "%d\n",
2023                        hostdata->madapter_info.partition_number);
2024         return len;
2025 }
2026
2027 static struct device_attribute ibmvscsi_host_partition_number = {
2028         .attr = {
2029                  .name = "partition_number",
2030                  .mode = S_IRUGO,
2031                  },
2032         .show = show_host_partition_number,
2033 };
2034
2035 static ssize_t show_host_mad_version(struct device *dev,
2036                                      struct device_attribute *attr, char *buf)
2037 {
2038         struct Scsi_Host *shost = class_to_shost(dev);
2039         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
2040         int len;
2041
2042         len = snprintf(buf, PAGE_SIZE, "%d\n",
2043                        hostdata->madapter_info.mad_version);
2044         return len;
2045 }
2046
2047 static struct device_attribute ibmvscsi_host_mad_version = {
2048         .attr = {
2049                  .name = "mad_version",
2050                  .mode = S_IRUGO,
2051                  },
2052         .show = show_host_mad_version,
2053 };
2054
2055 static ssize_t show_host_os_type(struct device *dev,
2056                                  struct device_attribute *attr, char *buf)
2057 {
2058         struct Scsi_Host *shost = class_to_shost(dev);
2059         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
2060         int len;
2061
2062         len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
2063         return len;
2064 }
2065
2066 static struct device_attribute ibmvscsi_host_os_type = {
2067         .attr = {
2068                  .name = "os_type",
2069                  .mode = S_IRUGO,
2070                  },
2071         .show = show_host_os_type,
2072 };
2073
2074 static ssize_t show_host_config(struct device *dev,
2075                                 struct device_attribute *attr, char *buf)
2076 {
2077         struct Scsi_Host *shost = class_to_shost(dev);
2078         struct ibmvscsi_host_data *hostdata = shost_priv(shost);
2079
2080         /* returns null-terminated host config data */
2081         if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
2082                 return strlen(buf);
2083         else
2084                 return 0;
2085 }
2086
2087 static struct device_attribute ibmvscsi_host_config = {
2088         .attr = {
2089                  .name = "config",
2090                  .mode = S_IRUGO,
2091                  },
2092         .show = show_host_config,
2093 };
2094
2095 static struct device_attribute *ibmvscsi_attrs[] = {
2096         &ibmvscsi_host_vhost_loc,
2097         &ibmvscsi_host_vhost_name,
2098         &ibmvscsi_host_srp_version,
2099         &ibmvscsi_host_partition_name,
2100         &ibmvscsi_host_partition_number,
2101         &ibmvscsi_host_mad_version,
2102         &ibmvscsi_host_os_type,
2103         &ibmvscsi_host_config,
2104         NULL
2105 };
2106
2107 /* ------------------------------------------------------------
2108  * SCSI driver registration
2109  */
2110 static struct scsi_host_template driver_template = {
2111         .module = THIS_MODULE,
2112         .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
2113         .proc_name = "ibmvscsi",
2114         .queuecommand = ibmvscsi_queuecommand,
2115         .eh_abort_handler = ibmvscsi_eh_abort_handler,
2116         .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
2117         .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
2118         .slave_configure = ibmvscsi_slave_configure,
2119         .change_queue_depth = ibmvscsi_change_queue_depth,
2120         .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
2121         .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
2122         .this_id = -1,
2123         .sg_tablesize = SG_ALL,
2124         .use_clustering = ENABLE_CLUSTERING,
2125         .shost_attrs = ibmvscsi_attrs,
2126 };
2127
2128 /**
2129  * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
2130  *
2131  * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
2132  *
2133  * Return value:
2134  *      Number of bytes of IO data the driver will need to perform well.
2135  */
2136 static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
2137 {
2138         /* iu_storage data allocated in initialize_event_pool */
2139         unsigned long desired_io = max_events * sizeof(union viosrp_iu);
2140
2141         /* add io space for sg data */
2142         desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
2143                              IBMVSCSI_CMDS_PER_LUN_DEFAULT);
2144
2145         return desired_io;
2146 }
2147
2148 static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
2149 {
2150         int rc;
2151         char *action = "reset";
2152
2153         if (hostdata->reset_crq) {
2154                 smp_rmb();
2155                 hostdata->reset_crq = 0;
2156
2157                 rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
2158                 if (!rc)
2159                         rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
2160                 vio_enable_interrupts(to_vio_dev(hostdata->dev));
2161         } else if (hostdata->reenable_crq) {
2162                 smp_rmb();
2163                 action = "enable";
2164                 rc = ibmvscsi_reenable_crq_queue(&hostdata->queue, hostdata);
2165                 hostdata->reenable_crq = 0;
2166                 if (!rc)
2167                         rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
2168         } else
2169                 return;
2170
2171         if (rc) {
2172                 atomic_set(&hostdata->request_limit, -1);
2173                 dev_err(hostdata->dev, "error after %s\n", action);
2174         }
2175
2176         scsi_unblock_requests(hostdata->host);
2177 }
2178
2179 static int ibmvscsi_work_to_do(struct ibmvscsi_host_data *hostdata)
2180 {
2181         if (kthread_should_stop())
2182                 return 1;
2183         else if (hostdata->reset_crq) {
2184                 smp_rmb();
2185                 return 1;
2186         } else if (hostdata->reenable_crq) {
2187                 smp_rmb();
2188                 return 1;
2189         }
2190
2191         return 0;
2192 }
2193
2194 static int ibmvscsi_work(void *data)
2195 {
2196         struct ibmvscsi_host_data *hostdata = data;
2197         int rc;
2198
2199         set_user_nice(current, -20);
2200
2201         while (1) {
2202                 rc = wait_event_interruptible(hostdata->work_wait_q,
2203                                               ibmvscsi_work_to_do(hostdata));
2204
2205                 BUG_ON(rc);
2206
2207                 if (kthread_should_stop())
2208                         break;
2209
2210                 ibmvscsi_do_work(hostdata);
2211         }
2212
2213         return 0;
2214 }
2215
2216 /**
2217  * Called by bus code for each adapter
2218  */
2219 static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
2220 {
2221         struct ibmvscsi_host_data *hostdata;
2222         struct Scsi_Host *host;
2223         struct device *dev = &vdev->dev;
2224         struct srp_rport_identifiers ids;
2225         struct srp_rport *rport;
2226         unsigned long wait_switch = 0;
2227         int rc;
2228
2229         dev_set_drvdata(&vdev->dev, NULL);
2230
2231         host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
2232         if (!host) {
2233                 dev_err(&vdev->dev, "couldn't allocate host data\n");
2234                 goto scsi_host_alloc_failed;
2235         }
2236
2237         host->transportt = ibmvscsi_transport_template;
2238         hostdata = shost_priv(host);
2239         memset(hostdata, 0x00, sizeof(*hostdata));
2240         INIT_LIST_HEAD(&hostdata->sent);
2241         init_waitqueue_head(&hostdata->work_wait_q);
2242         hostdata->host = host;
2243         hostdata->dev = dev;
2244         atomic_set(&hostdata->request_limit, -1);
2245         hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
2246
2247         if (map_persist_bufs(hostdata)) {
2248                 dev_err(&vdev->dev, "couldn't map persistent buffers\n");
2249                 goto persist_bufs_failed;
2250         }
2251
2252         hostdata->work_thread = kthread_run(ibmvscsi_work, hostdata, "%s_%d",
2253                                             "ibmvscsi", host->host_no);
2254
2255         if (IS_ERR(hostdata->work_thread)) {
2256                 dev_err(&vdev->dev, "couldn't initialize kthread. rc=%ld\n",
2257                         PTR_ERR(hostdata->work_thread));
2258                 goto init_crq_failed;
2259         }
2260
2261         rc = ibmvscsi_init_crq_queue(&hostdata->queue, hostdata, max_events);
2262         if (rc != 0 && rc != H_RESOURCE) {
2263                 dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
2264                 goto kill_kthread;
2265         }
2266         if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
2267                 dev_err(&vdev->dev, "couldn't initialize event pool\n");
2268                 goto init_pool_failed;
2269         }
2270
2271         host->max_lun = 8;
2272         host->max_id = max_id;
2273         host->max_channel = max_channel;
2274         host->max_cmd_len = 16;
2275
2276         if (scsi_add_host(hostdata->host, hostdata->dev))
2277                 goto add_host_failed;
2278
2279         /* we don't have a proper target_port_id so let's use the fake one */
2280         memcpy(ids.port_id, hostdata->madapter_info.partition_name,
2281                sizeof(ids.port_id));
2282         ids.roles = SRP_RPORT_ROLE_TARGET;
2283         rport = srp_rport_add(host, &ids);
2284         if (IS_ERR(rport))
2285                 goto add_srp_port_failed;
2286
2287         /* Try to send an initialization message.  Note that this is allowed
2288          * to fail if the other end is not acive.  In that case we don't
2289          * want to scan
2290          */
2291         if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0
2292             || rc == H_RESOURCE) {
2293                 /*
2294                  * Wait around max init_timeout secs for the adapter to finish
2295                  * initializing. When we are done initializing, we will have a
2296                  * valid request_limit.  We don't want Linux scanning before
2297                  * we are ready.
2298                  */
2299                 for (wait_switch = jiffies + (init_timeout * HZ);
2300                      time_before(jiffies, wait_switch) &&
2301                      atomic_read(&hostdata->request_limit) < 2;) {
2302
2303                         msleep(10);
2304                 }
2305
2306                 /* if we now have a valid request_limit, initiate a scan */
2307                 if (atomic_read(&hostdata->request_limit) > 0)
2308                         scsi_scan_host(host);
2309         }
2310
2311         dev_set_drvdata(&vdev->dev, hostdata);
2312         return 0;
2313
2314       add_srp_port_failed:
2315         scsi_remove_host(hostdata->host);
2316       add_host_failed:
2317         release_event_pool(&hostdata->pool, hostdata);
2318       init_pool_failed:
2319         ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_events);
2320       kill_kthread:
2321       kthread_stop(hostdata->work_thread);
2322       init_crq_failed:
2323         unmap_persist_bufs(hostdata);
2324       persist_bufs_failed:
2325         scsi_host_put(host);
2326       scsi_host_alloc_failed:
2327         return -1;
2328 }
2329
2330 static int ibmvscsi_remove(struct vio_dev *vdev)
2331 {
2332         struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
2333         unmap_persist_bufs(hostdata);
2334         release_event_pool(&hostdata->pool, hostdata);
2335         ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
2336                                         max_events);
2337
2338         kthread_stop(hostdata->work_thread);
2339         srp_remove_host(hostdata->host);
2340         scsi_remove_host(hostdata->host);
2341         scsi_host_put(hostdata->host);
2342
2343         return 0;
2344 }
2345
2346 /**
2347  * ibmvscsi_resume: Resume from suspend
2348  * @dev:        device struct
2349  *
2350  * We may have lost an interrupt across suspend/resume, so kick the
2351  * interrupt handler
2352  */
2353 static int ibmvscsi_resume(struct device *dev)
2354 {
2355         struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
2356         vio_disable_interrupts(to_vio_dev(hostdata->dev));
2357         tasklet_schedule(&hostdata->srp_task);
2358
2359         return 0;
2360 }
2361
2362 /**
2363  * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we 
2364  * support.
2365  */
2366 static struct vio_device_id ibmvscsi_device_table[] = {
2367         {"vscsi", "IBM,v-scsi"},
2368         { "", "" }
2369 };
2370 MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
2371
2372 static struct dev_pm_ops ibmvscsi_pm_ops = {
2373         .resume = ibmvscsi_resume
2374 };
2375
2376 static struct vio_driver ibmvscsi_driver = {
2377         .id_table = ibmvscsi_device_table,
2378         .probe = ibmvscsi_probe,
2379         .remove = ibmvscsi_remove,
2380         .get_desired_dma = ibmvscsi_get_desired_dma,
2381         .name = "ibmvscsi",
2382         .pm = &ibmvscsi_pm_ops,
2383 };
2384
2385 static struct srp_function_template ibmvscsi_transport_functions = {
2386 };
2387
2388 int __init ibmvscsi_module_init(void)
2389 {
2390         int ret;
2391
2392         /* Ensure we have two requests to do error recovery */
2393         driver_template.can_queue = max_requests;
2394         max_events = max_requests + 2;
2395
2396         if (!firmware_has_feature(FW_FEATURE_VIO))
2397                 return -ENODEV;
2398
2399         ibmvscsi_transport_template =
2400                 srp_attach_transport(&ibmvscsi_transport_functions);
2401         if (!ibmvscsi_transport_template)
2402                 return -ENOMEM;
2403
2404         ret = vio_register_driver(&ibmvscsi_driver);
2405         if (ret)
2406                 srp_release_transport(ibmvscsi_transport_template);
2407         return ret;
2408 }
2409
2410 void __exit ibmvscsi_module_exit(void)
2411 {
2412         vio_unregister_driver(&ibmvscsi_driver);
2413         srp_release_transport(ibmvscsi_transport_template);
2414 }
2415
2416 module_init(ibmvscsi_module_init);
2417 module_exit(ibmvscsi_module_exit);