[CONNECTOR]: Initialize subsystem earlier.
[linux-2.6/verdex.git] / drivers / scsi / ibmvscsi / rpa_vscsi.c
blob1a9992bdfef85ef944886e4a3f9fb334f34ed64f
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"
38 static char partition_name[97] = "UNKNOWN";
39 static unsigned int partition_number = -1;
41 /* ------------------------------------------------------------
42 * Routines for managing the command/response queue
44 /**
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
48 * @regs: pt_regs with registers
50 * Disables interrupts and schedules srp_task
51 * Always returns IRQ_HANDLED
53 static irqreturn_t ibmvscsi_handle_event(int irq,
54 void *dev_instance,
55 struct pt_regs *regs)
57 struct ibmvscsi_host_data *hostdata =
58 (struct ibmvscsi_host_data *)dev_instance;
59 vio_disable_interrupts(to_vio_dev(hostdata->dev));
60 tasklet_schedule(&hostdata->srp_task);
61 return IRQ_HANDLED;
64 /**
65 * release_crq_queue: - Deallocates data and unregisters CRQ
66 * @queue: crq_queue to initialize and register
67 * @host_data: ibmvscsi_host_data of host
69 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
70 * the crq with the hypervisor.
72 void ibmvscsi_release_crq_queue(struct crq_queue *queue,
73 struct ibmvscsi_host_data *hostdata,
74 int max_requests)
76 long rc;
77 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
78 free_irq(vdev->irq, (void *)hostdata);
79 tasklet_kill(&hostdata->srp_task);
80 do {
81 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
82 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
83 dma_unmap_single(hostdata->dev,
84 queue->msg_token,
85 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
86 free_page((unsigned long)queue->msgs);
89 /**
90 * crq_queue_next_crq: - Returns the next entry in message queue
91 * @queue: crq_queue to use
93 * Returns pointer to next entry in queue, or NULL if there are no new
94 * entried in the CRQ.
96 static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
98 struct viosrp_crq *crq;
99 unsigned long flags;
101 spin_lock_irqsave(&queue->lock, flags);
102 crq = &queue->msgs[queue->cur];
103 if (crq->valid & 0x80) {
104 if (++queue->cur == queue->size)
105 queue->cur = 0;
106 } else
107 crq = NULL;
108 spin_unlock_irqrestore(&queue->lock, flags);
110 return crq;
114 * ibmvscsi_send_crq: - Send a CRQ
115 * @hostdata: the adapter
116 * @word1: the first 64 bits of the data
117 * @word2: the second 64 bits of the data
119 int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata, u64 word1, u64 word2)
121 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
123 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
127 * ibmvscsi_task: - Process srps asynchronously
128 * @data: ibmvscsi_host_data of host
130 static void ibmvscsi_task(void *data)
132 struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
133 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
134 struct viosrp_crq *crq;
135 int done = 0;
137 while (!done) {
138 /* Pull all the valid messages off the CRQ */
139 while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
140 ibmvscsi_handle_crq(crq, hostdata);
141 crq->valid = 0x00;
144 vio_enable_interrupts(vdev);
145 if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
146 vio_disable_interrupts(vdev);
147 ibmvscsi_handle_crq(crq, hostdata);
148 crq->valid = 0x00;
149 } else {
150 done = 1;
155 static void gather_partition_info(void)
157 struct device_node *rootdn;
159 char *ppartition_name;
160 unsigned int *p_number_ptr;
162 /* Retrieve information about this partition */
163 rootdn = find_path_device("/");
164 if (!rootdn) {
165 return;
168 ppartition_name =
169 get_property(rootdn, "ibm,partition-name", NULL);
170 if (ppartition_name)
171 strncpy(partition_name, ppartition_name,
172 sizeof(partition_name));
173 p_number_ptr =
174 (unsigned int *)get_property(rootdn, "ibm,partition-no",
175 NULL);
176 if (p_number_ptr)
177 partition_number = *p_number_ptr;
180 static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
182 memset(&hostdata->madapter_info, 0x00,
183 sizeof(hostdata->madapter_info));
185 printk(KERN_INFO "rpa_vscsi: SPR_VERSION: %s\n", SRP_VERSION);
186 strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
188 strncpy(hostdata->madapter_info.partition_name, partition_name,
189 sizeof(hostdata->madapter_info.partition_name));
191 hostdata->madapter_info.partition_number = partition_number;
193 hostdata->madapter_info.mad_version = 1;
194 hostdata->madapter_info.os_type = 2;
198 * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
199 * @queue: crq_queue to initialize and register
200 * @hostdata: ibmvscsi_host_data of host
202 * Allocates a page for messages, maps it for dma, and registers
203 * the crq with the hypervisor.
204 * Returns zero on success.
206 int ibmvscsi_init_crq_queue(struct crq_queue *queue,
207 struct ibmvscsi_host_data *hostdata,
208 int max_requests)
210 int rc;
211 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
213 queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
215 if (!queue->msgs)
216 goto malloc_failed;
217 queue->size = PAGE_SIZE / sizeof(*queue->msgs);
219 queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
220 queue->size * sizeof(*queue->msgs),
221 DMA_BIDIRECTIONAL);
223 if (dma_mapping_error(queue->msg_token))
224 goto map_failed;
226 gather_partition_info();
227 set_adapter_info(hostdata);
229 rc = plpar_hcall_norets(H_REG_CRQ,
230 vdev->unit_address,
231 queue->msg_token, PAGE_SIZE);
232 if (rc == H_RESOURCE)
233 /* maybe kexecing and resource is busy. try a reset */
234 rc = ibmvscsi_reset_crq_queue(queue,
235 hostdata);
237 if (rc == 2) {
238 /* Adapter is good, but other end is not ready */
239 printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
240 } else if (rc != 0) {
241 printk(KERN_WARNING "ibmvscsi: Error %d opening adapter\n", rc);
242 goto reg_crq_failed;
245 if (request_irq(vdev->irq,
246 ibmvscsi_handle_event,
247 0, "ibmvscsi", (void *)hostdata) != 0) {
248 printk(KERN_ERR "ibmvscsi: couldn't register irq 0x%x\n",
249 vdev->irq);
250 goto req_irq_failed;
253 rc = vio_enable_interrupts(vdev);
254 if (rc != 0) {
255 printk(KERN_ERR "ibmvscsi: Error %d enabling interrupts!!!\n",
256 rc);
257 goto req_irq_failed;
260 queue->cur = 0;
261 spin_lock_init(&queue->lock);
263 tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
264 (unsigned long)hostdata);
266 return 0;
268 req_irq_failed:
269 do {
270 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
271 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
272 reg_crq_failed:
273 dma_unmap_single(hostdata->dev,
274 queue->msg_token,
275 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
276 map_failed:
277 free_page((unsigned long)queue->msgs);
278 malloc_failed:
279 return -1;
283 * reenable_crq_queue: - reenables a crq after
284 * @queue: crq_queue to initialize and register
285 * @hostdata: ibmvscsi_host_data of host
288 int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
289 struct ibmvscsi_host_data *hostdata)
291 int rc;
292 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
294 /* Re-enable the CRQ */
295 do {
296 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
297 } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
299 if (rc)
300 printk(KERN_ERR "ibmvscsi: Error %d enabling adapter\n", rc);
301 return rc;
305 * reset_crq_queue: - resets a crq after a failure
306 * @queue: crq_queue to initialize and register
307 * @hostdata: ibmvscsi_host_data of host
310 int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
311 struct ibmvscsi_host_data *hostdata)
313 int rc;
314 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
316 /* Close the CRQ */
317 do {
318 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
319 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
321 /* Clean out the queue */
322 memset(queue->msgs, 0x00, PAGE_SIZE);
323 queue->cur = 0;
325 set_adapter_info(hostdata);
327 /* And re-open it again */
328 rc = plpar_hcall_norets(H_REG_CRQ,
329 vdev->unit_address,
330 queue->msg_token, PAGE_SIZE);
331 if (rc == 2) {
332 /* Adapter is good, but other end is not ready */
333 printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
334 } else if (rc != 0) {
335 printk(KERN_WARNING
336 "ibmvscsi: couldn't register crq--rc 0x%x\n", rc);
338 return rc;