1 /* ------------------------------------------------------------
3 * (C) Copyright IBM Corporation 1994, 2003
4 * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
5 * Santiago Leon (santil@us.ibm.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * ------------------------------------------------------------
23 * RPA-specific functions of the SCSI host adapter for Virtual I/O devices
25 * This driver allows the Linux SCSI peripheral drivers to directly
26 * access devices in the hosting partition, either on an iSeries
27 * hypervisor system or a converged hypervisor system.
32 #include <asm/iommu.h>
33 #include <asm/hvcall.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/interrupt.h>
38 static char partition_name
[97] = "UNKNOWN";
39 static unsigned int partition_number
= -1;
41 /* ------------------------------------------------------------
42 * Routines for managing the command/response queue
45 * ibmvscsi_handle_event: - Interrupt handler for crq events
46 * @irq: number of irq to handle, not used
47 * @dev_instance: ibmvscsi_host_data of host that received interrupt
49 * Disables interrupts and schedules srp_task
50 * Always returns IRQ_HANDLED
52 static irqreturn_t
ibmvscsi_handle_event(int irq
, void *dev_instance
)
54 struct ibmvscsi_host_data
*hostdata
=
55 (struct ibmvscsi_host_data
*)dev_instance
;
56 vio_disable_interrupts(to_vio_dev(hostdata
->dev
));
57 tasklet_schedule(&hostdata
->srp_task
);
62 * release_crq_queue: - Deallocates data and unregisters CRQ
63 * @queue: crq_queue to initialize and register
64 * @host_data: ibmvscsi_host_data of host
66 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
67 * the crq with the hypervisor.
69 void ibmvscsi_release_crq_queue(struct crq_queue
*queue
,
70 struct ibmvscsi_host_data
*hostdata
,
74 struct vio_dev
*vdev
= to_vio_dev(hostdata
->dev
);
75 free_irq(vdev
->irq
, (void *)hostdata
);
76 tasklet_kill(&hostdata
->srp_task
);
78 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
79 } while ((rc
== H_BUSY
) || (H_IS_LONG_BUSY(rc
)));
80 dma_unmap_single(hostdata
->dev
,
82 queue
->size
* sizeof(*queue
->msgs
), DMA_BIDIRECTIONAL
);
83 free_page((unsigned long)queue
->msgs
);
87 * crq_queue_next_crq: - Returns the next entry in message queue
88 * @queue: crq_queue to use
90 * Returns pointer to next entry in queue, or NULL if there are no new
93 static struct viosrp_crq
*crq_queue_next_crq(struct crq_queue
*queue
)
95 struct viosrp_crq
*crq
;
98 spin_lock_irqsave(&queue
->lock
, flags
);
99 crq
= &queue
->msgs
[queue
->cur
];
100 if (crq
->valid
& 0x80) {
101 if (++queue
->cur
== queue
->size
)
105 spin_unlock_irqrestore(&queue
->lock
, flags
);
111 * ibmvscsi_send_crq: - Send a CRQ
112 * @hostdata: the adapter
113 * @word1: the first 64 bits of the data
114 * @word2: the second 64 bits of the data
116 int ibmvscsi_send_crq(struct ibmvscsi_host_data
*hostdata
, u64 word1
, u64 word2
)
118 struct vio_dev
*vdev
= to_vio_dev(hostdata
->dev
);
120 return plpar_hcall_norets(H_SEND_CRQ
, vdev
->unit_address
, word1
, word2
);
124 * ibmvscsi_task: - Process srps asynchronously
125 * @data: ibmvscsi_host_data of host
127 static void ibmvscsi_task(void *data
)
129 struct ibmvscsi_host_data
*hostdata
= (struct ibmvscsi_host_data
*)data
;
130 struct vio_dev
*vdev
= to_vio_dev(hostdata
->dev
);
131 struct viosrp_crq
*crq
;
135 /* Pull all the valid messages off the CRQ */
136 while ((crq
= crq_queue_next_crq(&hostdata
->queue
)) != NULL
) {
137 ibmvscsi_handle_crq(crq
, hostdata
);
141 vio_enable_interrupts(vdev
);
142 if ((crq
= crq_queue_next_crq(&hostdata
->queue
)) != NULL
) {
143 vio_disable_interrupts(vdev
);
144 ibmvscsi_handle_crq(crq
, hostdata
);
152 static void gather_partition_info(void)
154 struct device_node
*rootdn
;
156 const char *ppartition_name
;
157 const unsigned int *p_number_ptr
;
159 /* Retrieve information about this partition */
160 rootdn
= find_path_device("/");
165 ppartition_name
= get_property(rootdn
, "ibm,partition-name", NULL
);
167 strncpy(partition_name
, ppartition_name
,
168 sizeof(partition_name
));
169 p_number_ptr
= get_property(rootdn
, "ibm,partition-no", NULL
);
171 partition_number
= *p_number_ptr
;
174 static void set_adapter_info(struct ibmvscsi_host_data
*hostdata
)
176 memset(&hostdata
->madapter_info
, 0x00,
177 sizeof(hostdata
->madapter_info
));
179 printk(KERN_INFO
"rpa_vscsi: SPR_VERSION: %s\n", SRP_VERSION
);
180 strcpy(hostdata
->madapter_info
.srp_version
, SRP_VERSION
);
182 strncpy(hostdata
->madapter_info
.partition_name
, partition_name
,
183 sizeof(hostdata
->madapter_info
.partition_name
));
185 hostdata
->madapter_info
.partition_number
= partition_number
;
187 hostdata
->madapter_info
.mad_version
= 1;
188 hostdata
->madapter_info
.os_type
= 2;
192 * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
193 * @queue: crq_queue to initialize and register
194 * @hostdata: ibmvscsi_host_data of host
196 * Allocates a page for messages, maps it for dma, and registers
197 * the crq with the hypervisor.
198 * Returns zero on success.
200 int ibmvscsi_init_crq_queue(struct crq_queue
*queue
,
201 struct ibmvscsi_host_data
*hostdata
,
206 struct vio_dev
*vdev
= to_vio_dev(hostdata
->dev
);
208 queue
->msgs
= (struct viosrp_crq
*)get_zeroed_page(GFP_KERNEL
);
212 queue
->size
= PAGE_SIZE
/ sizeof(*queue
->msgs
);
214 queue
->msg_token
= dma_map_single(hostdata
->dev
, queue
->msgs
,
215 queue
->size
* sizeof(*queue
->msgs
),
218 if (dma_mapping_error(queue
->msg_token
))
221 gather_partition_info();
222 set_adapter_info(hostdata
);
224 retrc
= rc
= plpar_hcall_norets(H_REG_CRQ
,
226 queue
->msg_token
, PAGE_SIZE
);
227 if (rc
== H_RESOURCE
)
228 /* maybe kexecing and resource is busy. try a reset */
229 rc
= ibmvscsi_reset_crq_queue(queue
,
233 /* Adapter is good, but other end is not ready */
234 printk(KERN_WARNING
"ibmvscsi: Partner adapter not ready\n");
236 } else if (rc
!= 0) {
237 printk(KERN_WARNING
"ibmvscsi: Error %d opening adapter\n", rc
);
241 if (request_irq(vdev
->irq
,
242 ibmvscsi_handle_event
,
243 0, "ibmvscsi", (void *)hostdata
) != 0) {
244 printk(KERN_ERR
"ibmvscsi: couldn't register irq 0x%x\n",
249 rc
= vio_enable_interrupts(vdev
);
251 printk(KERN_ERR
"ibmvscsi: Error %d enabling interrupts!!!\n",
257 spin_lock_init(&queue
->lock
);
259 tasklet_init(&hostdata
->srp_task
, (void *)ibmvscsi_task
,
260 (unsigned long)hostdata
);
266 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
267 } while ((rc
== H_BUSY
) || (H_IS_LONG_BUSY(rc
)));
269 dma_unmap_single(hostdata
->dev
,
271 queue
->size
* sizeof(*queue
->msgs
), DMA_BIDIRECTIONAL
);
273 free_page((unsigned long)queue
->msgs
);
279 * reenable_crq_queue: - reenables a crq after
280 * @queue: crq_queue to initialize and register
281 * @hostdata: ibmvscsi_host_data of host
284 int ibmvscsi_reenable_crq_queue(struct crq_queue
*queue
,
285 struct ibmvscsi_host_data
*hostdata
)
288 struct vio_dev
*vdev
= to_vio_dev(hostdata
->dev
);
290 /* Re-enable the CRQ */
292 rc
= plpar_hcall_norets(H_ENABLE_CRQ
, vdev
->unit_address
);
293 } while ((rc
== H_IN_PROGRESS
) || (rc
== H_BUSY
) || (H_IS_LONG_BUSY(rc
)));
296 printk(KERN_ERR
"ibmvscsi: Error %d enabling adapter\n", rc
);
301 * reset_crq_queue: - resets a crq after a failure
302 * @queue: crq_queue to initialize and register
303 * @hostdata: ibmvscsi_host_data of host
306 int ibmvscsi_reset_crq_queue(struct crq_queue
*queue
,
307 struct ibmvscsi_host_data
*hostdata
)
310 struct vio_dev
*vdev
= to_vio_dev(hostdata
->dev
);
314 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
315 } while ((rc
== H_BUSY
) || (H_IS_LONG_BUSY(rc
)));
317 /* Clean out the queue */
318 memset(queue
->msgs
, 0x00, PAGE_SIZE
);
321 set_adapter_info(hostdata
);
323 /* And re-open it again */
324 rc
= plpar_hcall_norets(H_REG_CRQ
,
326 queue
->msg_token
, PAGE_SIZE
);
328 /* Adapter is good, but other end is not ready */
329 printk(KERN_WARNING
"ibmvscsi: Partner adapter not ready\n");
330 } else if (rc
!= 0) {
332 "ibmvscsi: couldn't register crq--rc 0x%x\n", rc
);