[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / scsi / ibmvscsi / rpa_vscsi.c
blob8bf5652f106090ab75ec922a747e11bf0d1b2ad0
1 /* ------------------------------------------------------------
2 * rpa_vscsi.c
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
20 * USA
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.
30 #include <asm/vio.h>
31 #include <asm/prom.h>
32 #include <asm/iommu.h>
33 #include <asm/hvcall.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/interrupt.h>
36 #include "ibmvscsi.h"
37 #include "srp.h"
39 static char partition_name[97] = "UNKNOWN";
40 static unsigned int partition_number = -1;
42 /* ------------------------------------------------------------
43 * Routines for managing the command/response queue
45 /**
46 * ibmvscsi_handle_event: - Interrupt handler for crq events
47 * @irq: number of irq to handle, not used
48 * @dev_instance: ibmvscsi_host_data of host that received interrupt
49 * @regs: pt_regs with registers
51 * Disables interrupts and schedules srp_task
52 * Always returns IRQ_HANDLED
54 static irqreturn_t ibmvscsi_handle_event(int irq,
55 void *dev_instance,
56 struct pt_regs *regs)
58 struct ibmvscsi_host_data *hostdata =
59 (struct ibmvscsi_host_data *)dev_instance;
60 vio_disable_interrupts(to_vio_dev(hostdata->dev));
61 tasklet_schedule(&hostdata->srp_task);
62 return IRQ_HANDLED;
65 /**
66 * release_crq_queue: - Deallocates data and unregisters CRQ
67 * @queue: crq_queue to initialize and register
68 * @host_data: ibmvscsi_host_data of host
70 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
71 * the crq with the hypervisor.
73 void ibmvscsi_release_crq_queue(struct crq_queue *queue,
74 struct ibmvscsi_host_data *hostdata,
75 int max_requests)
77 long rc;
78 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
79 free_irq(vdev->irq, (void *)hostdata);
80 tasklet_kill(&hostdata->srp_task);
81 do {
82 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
83 } while ((rc == H_Busy) || (H_isLongBusy(rc)));
84 dma_unmap_single(hostdata->dev,
85 queue->msg_token,
86 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
87 free_page((unsigned long)queue->msgs);
90 /**
91 * crq_queue_next_crq: - Returns the next entry in message queue
92 * @queue: crq_queue to use
94 * Returns pointer to next entry in queue, or NULL if there are no new
95 * entried in the CRQ.
97 static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
99 struct viosrp_crq *crq;
100 unsigned long flags;
102 spin_lock_irqsave(&queue->lock, flags);
103 crq = &queue->msgs[queue->cur];
104 if (crq->valid & 0x80) {
105 if (++queue->cur == queue->size)
106 queue->cur = 0;
107 } else
108 crq = NULL;
109 spin_unlock_irqrestore(&queue->lock, flags);
111 return crq;
115 * ibmvscsi_send_crq: - Send a CRQ
116 * @hostdata: the adapter
117 * @word1: the first 64 bits of the data
118 * @word2: the second 64 bits of the data
120 int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata, u64 word1, u64 word2)
122 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
124 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
128 * ibmvscsi_task: - Process srps asynchronously
129 * @data: ibmvscsi_host_data of host
131 static void ibmvscsi_task(void *data)
133 struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
134 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
135 struct viosrp_crq *crq;
136 int done = 0;
138 while (!done) {
139 /* Pull all the valid messages off the CRQ */
140 while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
141 ibmvscsi_handle_crq(crq, hostdata);
142 crq->valid = 0x00;
145 vio_enable_interrupts(vdev);
146 if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
147 vio_disable_interrupts(vdev);
148 ibmvscsi_handle_crq(crq, hostdata);
149 crq->valid = 0x00;
150 } else {
151 done = 1;
156 static void gather_partition_info(void)
158 struct device_node *rootdn;
160 char *ppartition_name;
161 unsigned int *p_number_ptr;
163 /* Retrieve information about this partition */
164 rootdn = find_path_device("/");
165 if (!rootdn) {
166 return;
169 ppartition_name =
170 get_property(rootdn, "ibm,partition-name", NULL);
171 if (ppartition_name)
172 strncpy(partition_name, ppartition_name,
173 sizeof(partition_name));
174 p_number_ptr =
175 (unsigned int *)get_property(rootdn, "ibm,partition-no",
176 NULL);
177 if (p_number_ptr)
178 partition_number = *p_number_ptr;
181 static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
183 memset(&hostdata->madapter_info, 0x00,
184 sizeof(hostdata->madapter_info));
186 printk(KERN_INFO "rpa_vscsi: SPR_VERSION: %s\n", SRP_VERSION);
187 strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
189 strncpy(hostdata->madapter_info.partition_name, partition_name,
190 sizeof(hostdata->madapter_info.partition_name));
192 hostdata->madapter_info.partition_number = partition_number;
194 hostdata->madapter_info.mad_version = 1;
195 hostdata->madapter_info.os_type = 2;
199 * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
200 * @queue: crq_queue to initialize and register
201 * @hostdata: ibmvscsi_host_data of host
203 * Allocates a page for messages, maps it for dma, and registers
204 * the crq with the hypervisor.
205 * Returns zero on success.
207 int ibmvscsi_init_crq_queue(struct crq_queue *queue,
208 struct ibmvscsi_host_data *hostdata,
209 int max_requests)
211 int rc;
212 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
214 queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
216 if (!queue->msgs)
217 goto malloc_failed;
218 queue->size = PAGE_SIZE / sizeof(*queue->msgs);
220 queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
221 queue->size * sizeof(*queue->msgs),
222 DMA_BIDIRECTIONAL);
224 if (dma_mapping_error(queue->msg_token))
225 goto map_failed;
227 gather_partition_info();
228 set_adapter_info(hostdata);
230 rc = plpar_hcall_norets(H_REG_CRQ,
231 vdev->unit_address,
232 queue->msg_token, PAGE_SIZE);
233 if (rc == 2) {
234 /* Adapter is good, but other end is not ready */
235 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);
238 goto reg_crq_failed;
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",
245 vdev->irq);
246 goto req_irq_failed;
249 rc = vio_enable_interrupts(vdev);
250 if (rc != 0) {
251 printk(KERN_ERR "ibmvscsi: Error %d enabling interrupts!!!\n",
252 rc);
253 goto req_irq_failed;
256 queue->cur = 0;
257 spin_lock_init(&queue->lock);
259 tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
260 (unsigned long)hostdata);
262 return 0;
264 req_irq_failed:
265 do {
266 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
267 } while ((rc == H_Busy) || (H_isLongBusy(rc)));
268 reg_crq_failed:
269 dma_unmap_single(hostdata->dev,
270 queue->msg_token,
271 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
272 map_failed:
273 free_page((unsigned long)queue->msgs);
274 malloc_failed:
275 return -1;
279 * reset_crq_queue: - resets a crq after a failure
280 * @queue: crq_queue to initialize and register
281 * @hostdata: ibmvscsi_host_data of host
284 void ibmvscsi_reset_crq_queue(struct crq_queue *queue,
285 struct ibmvscsi_host_data *hostdata)
287 int rc;
288 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
290 /* Close the CRQ */
291 do {
292 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
293 } while ((rc == H_Busy) || (H_isLongBusy(rc)));
295 /* Clean out the queue */
296 memset(queue->msgs, 0x00, PAGE_SIZE);
297 queue->cur = 0;
299 set_adapter_info(hostdata);
301 /* And re-open it again */
302 rc = plpar_hcall_norets(H_REG_CRQ,
303 vdev->unit_address,
304 queue->msg_token, PAGE_SIZE);
305 if (rc == 2) {
306 /* Adapter is good, but other end is not ready */
307 printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
308 } else if (rc != 0) {
309 printk(KERN_WARNING
310 "ibmvscsi: couldn't register crq--rc 0x%x\n", rc);