blob: 01b8ac641eb89d5460cb7383a24c865230e4c45e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------
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)
6 *
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.
11 *
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.
16 *
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
21 *
22 * ------------------------------------------------------------
23 * RPA-specific functions of the SCSI host adapter for Virtual I/O devices
24 *
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.
28 */
29
30#include <asm/vio.h>
Stephen Rothwell71d276d2005-08-17 16:41:44 +100031#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/iommu.h>
33#include <asm/hvcall.h>
34#include <linux/dma-mapping.h>
35#include <linux/interrupt.h>
36#include "ibmvscsi.h"
Linda Xieb4687ca2005-06-27 17:01:48 -050037
38static char partition_name[97] = "UNKNOWN";
39static unsigned int partition_number = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* ------------------------------------------------------------
42 * Routines for managing the command/response queue
43 */
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
49 *
50 * Disables interrupts and schedules srp_task
51 * Always returns IRQ_HANDLED
52 */
53static irqreturn_t ibmvscsi_handle_event(int irq,
54 void *dev_instance,
55 struct pt_regs *regs)
56{
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;
62}
63
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
68 *
69 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
70 * the crq with the hypervisor.
71 */
72void ibmvscsi_release_crq_queue(struct crq_queue *queue,
73 struct ibmvscsi_host_data *hostdata,
74 int max_requests)
75{
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);
Segher Boessenkool706c8c92006-03-30 14:49:40 +020082 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 dma_unmap_single(hostdata->dev,
84 queue->msg_token,
85 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
86 free_page((unsigned long)queue->msgs);
87}
88
89/**
90 * crq_queue_next_crq: - Returns the next entry in message queue
91 * @queue: crq_queue to use
92 *
93 * Returns pointer to next entry in queue, or NULL if there are no new
94 * entried in the CRQ.
95 */
96static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
97{
98 struct viosrp_crq *crq;
99 unsigned long flags;
100
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);
109
110 return crq;
111}
112
113/**
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
118 */
119int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata, u64 word1, u64 word2)
120{
121 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
122
123 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
124}
125
126/**
127 * ibmvscsi_task: - Process srps asynchronously
128 * @data: ibmvscsi_host_data of host
129 */
130static void ibmvscsi_task(void *data)
131{
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;
136
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;
142 }
143
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;
151 }
152 }
153}
154
Linda Xieb4687ca2005-06-27 17:01:48 -0500155static void gather_partition_info(void)
156{
157 struct device_node *rootdn;
158
Jeremy Kerr294ef162006-07-12 15:40:51 +1000159 const char *ppartition_name;
160 const unsigned int *p_number_ptr;
Linda Xieb4687ca2005-06-27 17:01:48 -0500161
162 /* Retrieve information about this partition */
163 rootdn = find_path_device("/");
164 if (!rootdn) {
165 return;
166 }
167
Jeremy Kerr294ef162006-07-12 15:40:51 +1000168 ppartition_name = get_property(rootdn, "ibm,partition-name", NULL);
Linda Xieb4687ca2005-06-27 17:01:48 -0500169 if (ppartition_name)
170 strncpy(partition_name, ppartition_name,
171 sizeof(partition_name));
Jeremy Kerr294ef162006-07-12 15:40:51 +1000172 p_number_ptr = get_property(rootdn, "ibm,partition-no", NULL);
Linda Xieb4687ca2005-06-27 17:01:48 -0500173 if (p_number_ptr)
174 partition_number = *p_number_ptr;
175}
176
177static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
178{
179 memset(&hostdata->madapter_info, 0x00,
180 sizeof(hostdata->madapter_info));
181
182 printk(KERN_INFO "rpa_vscsi: SPR_VERSION: %s\n", SRP_VERSION);
183 strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
184
185 strncpy(hostdata->madapter_info.partition_name, partition_name,
186 sizeof(hostdata->madapter_info.partition_name));
187
188 hostdata->madapter_info.partition_number = partition_number;
189
190 hostdata->madapter_info.mad_version = 1;
191 hostdata->madapter_info.os_type = 2;
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/**
195 * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
196 * @queue: crq_queue to initialize and register
197 * @hostdata: ibmvscsi_host_data of host
198 *
199 * Allocates a page for messages, maps it for dma, and registers
200 * the crq with the hypervisor.
201 * Returns zero on success.
202 */
203int ibmvscsi_init_crq_queue(struct crq_queue *queue,
204 struct ibmvscsi_host_data *hostdata,
205 int max_requests)
206{
207 int rc;
Dave C Boutchercefbda22006-06-12 21:22:51 -0500208 int retrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
210
211 queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
212
213 if (!queue->msgs)
214 goto malloc_failed;
215 queue->size = PAGE_SIZE / sizeof(*queue->msgs);
216
217 queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
218 queue->size * sizeof(*queue->msgs),
219 DMA_BIDIRECTIONAL);
220
221 if (dma_mapping_error(queue->msg_token))
222 goto map_failed;
223
Linda Xieb4687ca2005-06-27 17:01:48 -0500224 gather_partition_info();
225 set_adapter_info(hostdata);
226
Dave C Boutchercefbda22006-06-12 21:22:51 -0500227 retrc = rc = plpar_hcall_norets(H_REG_CRQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 vdev->unit_address,
229 queue->msg_token, PAGE_SIZE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200230 if (rc == H_RESOURCE)
Dave C Boutcherbb585962005-11-15 09:53:00 -0600231 /* maybe kexecing and resource is busy. try a reset */
232 rc = ibmvscsi_reset_crq_queue(queue,
233 hostdata);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (rc == 2) {
236 /* Adapter is good, but other end is not ready */
237 printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
Dave C Boutcherae0fda02006-07-06 22:08:49 -0500238 retrc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 } else if (rc != 0) {
240 printk(KERN_WARNING "ibmvscsi: Error %d opening adapter\n", rc);
241 goto reg_crq_failed;
242 }
243
244 if (request_irq(vdev->irq,
245 ibmvscsi_handle_event,
246 0, "ibmvscsi", (void *)hostdata) != 0) {
247 printk(KERN_ERR "ibmvscsi: couldn't register irq 0x%x\n",
248 vdev->irq);
249 goto req_irq_failed;
250 }
251
252 rc = vio_enable_interrupts(vdev);
253 if (rc != 0) {
254 printk(KERN_ERR "ibmvscsi: Error %d enabling interrupts!!!\n",
255 rc);
256 goto req_irq_failed;
257 }
258
259 queue->cur = 0;
260 spin_lock_init(&queue->lock);
261
262 tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
263 (unsigned long)hostdata);
264
Dave C Boutchercefbda22006-06-12 21:22:51 -0500265 return retrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 req_irq_failed:
268 do {
269 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200270 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 reg_crq_failed:
272 dma_unmap_single(hostdata->dev,
273 queue->msg_token,
274 queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
275 map_failed:
276 free_page((unsigned long)queue->msgs);
277 malloc_failed:
278 return -1;
279}
280
281/**
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600282 * reenable_crq_queue: - reenables a crq after
283 * @queue: crq_queue to initialize and register
284 * @hostdata: ibmvscsi_host_data of host
285 *
286 */
287int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
288 struct ibmvscsi_host_data *hostdata)
289{
290 int rc;
291 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
292
293 /* Re-enable the CRQ */
294 do {
295 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200296 } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
Dave C Boutcher2b541f82006-01-19 13:34:44 -0600297
298 if (rc)
299 printk(KERN_ERR "ibmvscsi: Error %d enabling adapter\n", rc);
300 return rc;
301}
302
303/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * reset_crq_queue: - resets a crq after a failure
305 * @queue: crq_queue to initialize and register
306 * @hostdata: ibmvscsi_host_data of host
307 *
308 */
Dave C Boutcherbb585962005-11-15 09:53:00 -0600309int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct ibmvscsi_host_data *hostdata)
311{
312 int rc;
313 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
314
315 /* Close the CRQ */
316 do {
317 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200318 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* Clean out the queue */
321 memset(queue->msgs, 0x00, PAGE_SIZE);
322 queue->cur = 0;
323
Linda Xieb4687ca2005-06-27 17:01:48 -0500324 set_adapter_info(hostdata);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* And re-open it again */
327 rc = plpar_hcall_norets(H_REG_CRQ,
328 vdev->unit_address,
329 queue->msg_token, PAGE_SIZE);
330 if (rc == 2) {
331 /* Adapter is good, but other end is not ready */
332 printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
333 } else if (rc != 0) {
334 printk(KERN_WARNING
335 "ibmvscsi: couldn't register crq--rc 0x%x\n", rc);
336 }
Dave C Boutcherbb585962005-11-15 09:53:00 -0600337 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}