blob: 7ab1ac16a11f34f64eb717bb397dfd22adb3b8cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Swen Schillig00bab912008-06-10 18:20:57 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Swen Schillig00bab912008-06-10 18:20:57 +02004 * Setup and helper functions to access QDIO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Christof Schmitt1674b402010-04-30 18:09:34 +02006 * Copyright IBM Corporation 2002, 2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "zfcp_ext.h"
Christof Schmitt34c2b712010-02-17 11:18:59 +010014#include "zfcp_qdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Christof Schmitt5d4e2262008-07-02 10:56:34 +020016#define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Swen Schillig00bab912008-06-10 18:20:57 +020018static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
Swen Schilligb4e44592007-07-18 10:55:13 +020019{
20 int pos;
21
22 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
Swen Schillig00bab912008-06-10 18:20:57 +020023 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
24 if (!sbal[pos])
Swen Schilligb4e44592007-07-18 10:55:13 +020025 return -ENOMEM;
Swen Schilligb4e44592007-07-18 10:55:13 +020026 }
27 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
28 if (pos % QBUFF_PER_PAGE)
Swen Schillig00bab912008-06-10 18:20:57 +020029 sbal[pos] = sbal[pos - 1] + 1;
Swen Schilligb4e44592007-07-18 10:55:13 +020030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
Swen Schillig564e1c82009-08-18 15:43:19 +020033static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id)
Swen Schillig00bab912008-06-10 18:20:57 +020034{
Swen Schillig564e1c82009-08-18 15:43:19 +020035 struct zfcp_adapter *adapter = qdio->adapter;
36
Christof Schmittff3b24f2008-10-01 12:42:15 +020037 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
Swen Schillig00bab912008-06-10 18:20:57 +020038
39 zfcp_erp_adapter_reopen(adapter,
40 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
41 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
42}
43
Christof Schmitt5d4e2262008-07-02 10:56:34 +020044static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
45{
46 int i, sbal_idx;
47
48 for (i = first; i < first + cnt; i++) {
49 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
50 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
51 }
52}
53
Martin Peschke94506fd2009-03-02 13:08:56 +010054/* this needs to be called prior to updating the queue fill level */
Heiko Carstens41e05a12009-08-18 15:43:32 +020055static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
Martin Peschke94506fd2009-03-02 13:08:56 +010056{
Heiko Carstens41e05a12009-08-18 15:43:32 +020057 unsigned long long now, span;
Martin Peschke94506fd2009-03-02 13:08:56 +010058 int free, used;
59
Swen Schillig564e1c82009-08-18 15:43:19 +020060 spin_lock(&qdio->stat_lock);
Heiko Carstens41e05a12009-08-18 15:43:32 +020061 now = get_clock_monotonic();
62 span = (now - qdio->req_q_time) >> 12;
63 free = atomic_read(&qdio->req_q.count);
Martin Peschke94506fd2009-03-02 13:08:56 +010064 used = QDIO_MAX_BUFFERS_PER_Q - free;
Swen Schillig564e1c82009-08-18 15:43:19 +020065 qdio->req_q_util += used * span;
66 qdio->req_q_time = now;
67 spin_unlock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +010068}
69
Jan Glauber779e6e12008-07-17 17:16:48 +020070static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
71 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020072 unsigned long parm)
73{
Swen Schillig564e1c82009-08-18 15:43:19 +020074 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
75 struct zfcp_qdio_queue *queue = &qdio->req_q;
Swen Schillig00bab912008-06-10 18:20:57 +020076
Jan Glauber779e6e12008-07-17 17:16:48 +020077 if (unlikely(qdio_err)) {
Swen Schillig57717102009-08-18 15:43:21 +020078 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
79 count);
Swen Schillig564e1c82009-08-18 15:43:19 +020080 zfcp_qdio_handler_error(qdio, "qdireq1");
Swen Schillig00bab912008-06-10 18:20:57 +020081 return;
82 }
83
84 /* cleanup all SBALs being program-owned now */
85 zfcp_qdio_zero_sbals(queue->sbal, first, count);
86
Swen Schillig564e1c82009-08-18 15:43:19 +020087 zfcp_qdio_account(qdio);
Swen Schillig00bab912008-06-10 18:20:57 +020088 atomic_add(count, &queue->count);
Swen Schillig564e1c82009-08-18 15:43:19 +020089 wake_up(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +020090}
91
Swen Schillig564e1c82009-08-18 15:43:19 +020092static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Swen Schillig564e1c82009-08-18 15:43:19 +020094 struct zfcp_qdio_queue *queue = &qdio->resp_q;
95 struct ccw_device *cdev = qdio->adapter->ccw_device;
Swen Schillig00bab912008-06-10 18:20:57 +020096 u8 count, start = queue->first;
97 unsigned int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Swen Schillig00bab912008-06-10 18:20:57 +020099 count = atomic_read(&queue->count) + processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Jan Glauber779e6e12008-07-17 17:16:48 +0200101 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Swen Schillig00bab912008-06-10 18:20:57 +0200103 if (unlikely(retval)) {
104 atomic_set(&queue->count, count);
Christof Schmitt452b5052010-02-17 11:18:51 +0100105 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdrpb_1", NULL);
Swen Schillig00bab912008-06-10 18:20:57 +0200106 } else {
107 queue->first += count;
108 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
109 atomic_set(&queue->count, 0);
110 }
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jan Glauber779e6e12008-07-17 17:16:48 +0200113static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
114 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +0200115 unsigned long parm)
116{
Swen Schillig564e1c82009-08-18 15:43:19 +0200117 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schilligbd63eaf2009-08-18 15:43:13 +0200118 int sbal_idx, sbal_no;
Swen Schillig00bab912008-06-10 18:20:57 +0200119
Jan Glauber779e6e12008-07-17 17:16:48 +0200120 if (unlikely(qdio_err)) {
Swen Schillig57717102009-08-18 15:43:21 +0200121 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
122 count);
Swen Schillig564e1c82009-08-18 15:43:19 +0200123 zfcp_qdio_handler_error(qdio, "qdires1");
Swen Schillig00bab912008-06-10 18:20:57 +0200124 return;
125 }
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * go through all SBALs from input queue currently
129 * returned by QDIO layer
130 */
Swen Schillig00bab912008-06-10 18:20:57 +0200131 for (sbal_no = 0; sbal_no < count; sbal_no++) {
132 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* go through all SBALEs of SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200134 zfcp_fsf_reqid_check(qdio, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
137 /*
138 * put range of SBALs back to response queue
139 * (including SBALs which have already been free before)
140 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200141 zfcp_qdio_resp_put_back(qdio, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Swen Schillig44cc76f2008-10-01 12:42:16 +0200144static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200145zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200147 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200150 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
152
153 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200154 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return NULL;
156
157 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200158 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
160
161 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200162 q_req->sbal_last++;
163 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200166 q_req->sbal_number++;
Swen Schillig01b04752010-07-16 15:37:37 +0200167 BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200170 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200173 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Christof Schmitt1674b402010-04-30 18:09:34 +0200174 sbale->flags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return sbale;
177}
178
Swen Schillig44cc76f2008-10-01 12:42:16 +0200179static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200180zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Christof Schmitt1674b402010-04-30 18:09:34 +0200182 if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
183 return zfcp_qdio_sbal_chain(qdio, q_req);
Swen Schillig42428f72009-08-18 15:43:18 +0200184 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200185 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Swen Schillig564e1c82009-08-18 15:43:19 +0200188static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
Christof Schmitt34c2b712010-02-17 11:18:59 +0100189 struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Swen Schillig564e1c82009-08-18 15:43:19 +0200191 struct qdio_buffer **sbal = qdio->req_q.sbal;
Swen Schillig42428f72009-08-18 15:43:18 +0200192 int first = q_req->sbal_first;
193 int last = q_req->sbal_last;
Swen Schillig00bab912008-06-10 18:20:57 +0200194 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
195 QDIO_MAX_BUFFERS_PER_Q + 1;
196 zfcp_qdio_zero_sbals(sbal, first, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/**
200 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
Christof Schmitt1674b402010-04-30 18:09:34 +0200201 * @qdio: pointer to struct zfcp_qdio
202 * @q_req: pointer to struct zfcp_qdio_req
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig00bab912008-06-10 18:20:57 +0200205 * Returns: number of bytes, or error (negativ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100207int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig01b04752010-07-16 15:37:37 +0200208 struct scatterlist *sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200210 struct qdio_buffer_element *sbale;
Christof Schmitt68322982010-04-30 18:09:33 +0200211 int bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Swen Schillig00bab912008-06-10 18:20:57 +0200213 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200214 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Christof Schmitt1674b402010-04-30 18:09:34 +0200215 sbale->flags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Swen Schillig00bab912008-06-10 18:20:57 +0200217 for (; sg; sg = sg_next(sg)) {
Christof Schmitt1674b402010-04-30 18:09:34 +0200218 sbale = zfcp_qdio_sbale_next(qdio, q_req);
Christof Schmitt68322982010-04-30 18:09:33 +0200219 if (!sbale) {
220 atomic_inc(&qdio->req_q_full);
221 zfcp_qdio_undo_sbals(qdio, q_req);
222 return -EINVAL;
223 }
224
225 sbale->addr = sg_virt(sg);
226 sbale->length = sg->length;
227
Swen Schillig00bab912008-06-10 18:20:57 +0200228 bytes += sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Swen Schillig00bab912008-06-10 18:20:57 +0200230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* assume that no other SBALEs are to follow in the same SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200232 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
Swen Schillig00bab912008-06-10 18:20:57 +0200234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return bytes;
236}
237
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200238static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
239{
240 struct zfcp_qdio_queue *req_q = &qdio->req_q;
241
242 spin_lock_bh(&qdio->req_q_lock);
Christof Schmittc2af7542010-06-21 10:11:32 +0200243 if (atomic_read(&req_q->count) ||
244 !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200245 return 1;
246 spin_unlock_bh(&qdio->req_q_lock);
247 return 0;
248}
249
250/**
251 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
252 * @qdio: pointer to struct zfcp_qdio
253 *
254 * The req_q_lock must be held by the caller of this function, and
255 * this function may only be called from process context; it will
256 * sleep when waiting for a free sbal.
257 *
258 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
259 */
260int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
261{
262 long ret;
263
264 spin_unlock_bh(&qdio->req_q_lock);
265 ret = wait_event_interruptible_timeout(qdio->req_q_wq,
266 zfcp_qdio_sbal_check(qdio), 5 * HZ);
Christof Schmittc2af7542010-06-21 10:11:32 +0200267
268 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
269 return -EIO;
270
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200271 if (ret > 0)
272 return 0;
Christof Schmittc2af7542010-06-21 10:11:32 +0200273
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200274 if (!ret) {
275 atomic_inc(&qdio->req_q_full);
276 /* assume hanging outbound queue, try queue recovery */
277 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1", NULL);
278 }
279
280 spin_lock_bh(&qdio->req_q_lock);
281 return -EIO;
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/**
Swen Schillig00bab912008-06-10 18:20:57 +0200285 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200286 * @qdio: pointer to struct zfcp_qdio
Christof Schmitt34c2b712010-02-17 11:18:59 +0100287 * @q_req: pointer to struct zfcp_qdio_req
Swen Schillig00bab912008-06-10 18:20:57 +0200288 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100290int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Swen Schillig564e1c82009-08-18 15:43:19 +0200292 struct zfcp_qdio_queue *req_q = &qdio->req_q;
Swen Schillig42428f72009-08-18 15:43:18 +0200293 int first = q_req->sbal_first;
294 int count = q_req->sbal_number;
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100295 int retval;
296 unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
Swen Schillig00bab912008-06-10 18:20:57 +0200297
Swen Schillig564e1c82009-08-18 15:43:19 +0200298 zfcp_qdio_account(qdio);
Martin Peschke94506fd2009-03-02 13:08:56 +0100299
Swen Schillig564e1c82009-08-18 15:43:19 +0200300 retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
301 count);
Swen Schillig00bab912008-06-10 18:20:57 +0200302 if (unlikely(retval)) {
303 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
304 return retval;
305 }
306
307 /* account for transferred buffers */
308 atomic_sub(count, &req_q->count);
309 req_q->first += count;
310 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Swen Schillig564e1c82009-08-18 15:43:19 +0200314
315static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
316 struct zfcp_qdio *qdio)
317{
318
319 id->cdev = qdio->adapter->ccw_device;
320 id->q_format = QDIO_ZFCP_QFMT;
321 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
322 ASCEBC(id->adapter_name, 8);
323 id->qib_param_field_format = 0;
324 id->qib_param_field = NULL;
325 id->input_slib_elements = NULL;
326 id->output_slib_elements = NULL;
327 id->no_input_qs = 1;
328 id->no_output_qs = 1;
329 id->input_handler = zfcp_qdio_int_resp;
330 id->output_handler = zfcp_qdio_int_req;
331 id->int_parm = (unsigned long) qdio;
Swen Schillig564e1c82009-08-18 15:43:19 +0200332 id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
333 id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);
334
335}
Swen Schillig00bab912008-06-10 18:20:57 +0200336/**
Swen Schillig00bab912008-06-10 18:20:57 +0200337 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
338 * @adapter: pointer to struct zfcp_adapter
339 * Returns: -ENOMEM on memory allocation error or return value from
340 * qdio_allocate
341 */
Swen Schilligd5a282a2009-08-18 15:43:22 +0200342static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200343{
Swen Schillig564e1c82009-08-18 15:43:19 +0200344 struct qdio_initialize init_data;
Swen Schillig00bab912008-06-10 18:20:57 +0200345
Swen Schillig564e1c82009-08-18 15:43:19 +0200346 if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
347 zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
Swen Schillig00bab912008-06-10 18:20:57 +0200348 return -ENOMEM;
349
Swen Schillig564e1c82009-08-18 15:43:19 +0200350 zfcp_qdio_setup_init_data(&init_data, qdio);
Swen Schillig00bab912008-06-10 18:20:57 +0200351
Swen Schillig564e1c82009-08-18 15:43:19 +0200352 return qdio_allocate(&init_data);
Swen Schillig00bab912008-06-10 18:20:57 +0200353}
354
355/**
356 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200357 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200358 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200359void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200360{
361 struct zfcp_qdio_queue *req_q;
362 int first, count;
363
Swen Schillig564e1c82009-08-18 15:43:19 +0200364 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200365 return;
366
367 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Swen Schillig564e1c82009-08-18 15:43:19 +0200368 req_q = &qdio->req_q;
369 spin_lock_bh(&qdio->req_q_lock);
370 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
371 spin_unlock_bh(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200372
Christof Schmittc2af7542010-06-21 10:11:32 +0200373 wake_up(&qdio->req_q_wq);
374
Swen Schillig564e1c82009-08-18 15:43:19 +0200375 qdio_shutdown(qdio->adapter->ccw_device,
376 QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200377
378 /* cleanup used outbound sbals */
379 count = atomic_read(&req_q->count);
380 if (count < QDIO_MAX_BUFFERS_PER_Q) {
381 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
382 count = QDIO_MAX_BUFFERS_PER_Q - count;
383 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
384 }
385 req_q->first = 0;
386 atomic_set(&req_q->count, 0);
Swen Schillig564e1c82009-08-18 15:43:19 +0200387 qdio->resp_q.first = 0;
388 atomic_set(&qdio->resp_q.count, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200389}
390
391/**
392 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200393 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200394 * Returns: 0 on success, otherwise -EIO
395 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200396int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200397{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200398 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200399 struct qdio_initialize init_data;
400 struct ccw_device *cdev = qdio->adapter->ccw_device;
Swen Schillig00bab912008-06-10 18:20:57 +0200401 int cc;
402
Swen Schillig564e1c82009-08-18 15:43:19 +0200403 if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200404 return -EIO;
405
Swen Schillig564e1c82009-08-18 15:43:19 +0200406 zfcp_qdio_setup_init_data(&init_data, qdio);
407
408 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200409 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200410
Swen Schillig564e1c82009-08-18 15:43:19 +0200411 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200412 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200413
414 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200415 sbale = &(qdio->resp_q.sbal[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200416 sbale->length = 0;
417 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
418 sbale->addr = NULL;
419 }
420
Swen Schillig564e1c82009-08-18 15:43:19 +0200421 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200422 QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200423 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200424
425 /* set index of first avalable SBALS / number of available SBALS */
Swen Schillig564e1c82009-08-18 15:43:19 +0200426 qdio->req_q.first = 0;
427 atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
Swen Schillig00bab912008-06-10 18:20:57 +0200428
429 return 0;
430
431failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200432 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200433failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200434 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200435 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200436 return -EIO;
437}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200438
439void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
440{
441 struct qdio_buffer **sbal_req, **sbal_resp;
442 int p;
443
444 if (!qdio)
445 return;
446
447 if (qdio->adapter->ccw_device)
448 qdio_free(qdio->adapter->ccw_device);
449
450 sbal_req = qdio->req_q.sbal;
451 sbal_resp = qdio->resp_q.sbal;
452
453 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
454 free_page((unsigned long) sbal_req[p]);
455 free_page((unsigned long) sbal_resp[p]);
456 }
457
458 kfree(qdio);
459}
460
461int zfcp_qdio_setup(struct zfcp_adapter *adapter)
462{
463 struct zfcp_qdio *qdio;
464
465 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
466 if (!qdio)
467 return -ENOMEM;
468
469 qdio->adapter = adapter;
470
471 if (zfcp_qdio_allocate(qdio)) {
472 zfcp_qdio_destroy(qdio);
473 return -ENOMEM;
474 }
475
476 spin_lock_init(&qdio->req_q_lock);
477 spin_lock_init(&qdio->stat_lock);
478
479 adapter->qdio = qdio;
480 return 0;
481}
482