blob: aceced8ec7e4aca7dff75975e6d0515e0c860485 [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;
Swen Schillig706eca42010-07-16 15:37:38 +020058 int used;
Martin Peschke94506fd2009-03-02 13:08:56 +010059
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;
Swen Schillig706eca42010-07-16 15:37:38 +020063 used = QDIO_MAX_BUFFERS_PER_Q - atomic_read(&qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020064 qdio->req_q_util += used * span;
65 qdio->req_q_time = now;
66 spin_unlock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +010067}
68
Jan Glauber779e6e12008-07-17 17:16:48 +020069static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
Swen Schillig706eca42010-07-16 15:37:38 +020070 int queue_no, int idx, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020071 unsigned long parm)
72{
Swen Schillig564e1c82009-08-18 15:43:19 +020073 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schillig00bab912008-06-10 18:20:57 +020074
Jan Glauber779e6e12008-07-17 17:16:48 +020075 if (unlikely(qdio_err)) {
Swen Schillig706eca42010-07-16 15:37:38 +020076 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, idx, count);
Swen Schillig564e1c82009-08-18 15:43:19 +020077 zfcp_qdio_handler_error(qdio, "qdireq1");
Swen Schillig00bab912008-06-10 18:20:57 +020078 return;
79 }
80
81 /* cleanup all SBALs being program-owned now */
Swen Schillig706eca42010-07-16 15:37:38 +020082 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
Swen Schillig00bab912008-06-10 18:20:57 +020083
Swen Schillig564e1c82009-08-18 15:43:19 +020084 zfcp_qdio_account(qdio);
Swen Schillig706eca42010-07-16 15:37:38 +020085 atomic_add(count, &qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020086 wake_up(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +020087}
88
Jan Glauber779e6e12008-07-17 17:16:48 +020089static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
Swen Schillig706eca42010-07-16 15:37:38 +020090 int queue_no, int idx, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020091 unsigned long parm)
92{
Swen Schillig564e1c82009-08-18 15:43:19 +020093 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schilligbd63eaf2009-08-18 15:43:13 +020094 int sbal_idx, sbal_no;
Swen Schillig00bab912008-06-10 18:20:57 +020095
Jan Glauber779e6e12008-07-17 17:16:48 +020096 if (unlikely(qdio_err)) {
Swen Schillig706eca42010-07-16 15:37:38 +020097 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, idx, count);
Swen Schillig564e1c82009-08-18 15:43:19 +020098 zfcp_qdio_handler_error(qdio, "qdires1");
Swen Schillig00bab912008-06-10 18:20:57 +020099 return;
100 }
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 /*
103 * go through all SBALs from input queue currently
104 * returned by QDIO layer
105 */
Swen Schillig00bab912008-06-10 18:20:57 +0200106 for (sbal_no = 0; sbal_no < count; sbal_no++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200107 sbal_idx = (idx + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* go through all SBALEs of SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200109 zfcp_fsf_reqid_check(qdio, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
112 /*
Swen Schillig706eca42010-07-16 15:37:38 +0200113 * put SBALs back to response queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Swen Schillig706eca42010-07-16 15:37:38 +0200115 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
116 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Swen Schillig44cc76f2008-10-01 12:42:16 +0200119static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200120zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200122 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200125 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
127
128 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200129 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return NULL;
131
132 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200133 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
135
136 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200137 q_req->sbal_last++;
138 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200141 q_req->sbal_number++;
Swen Schillig01b04752010-07-16 15:37:37 +0200142 BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200145 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200148 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Christof Schmitt1674b402010-04-30 18:09:34 +0200149 sbale->flags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return sbale;
152}
153
Swen Schillig44cc76f2008-10-01 12:42:16 +0200154static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200155zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Christof Schmitt1674b402010-04-30 18:09:34 +0200157 if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
158 return zfcp_qdio_sbal_chain(qdio, q_req);
Swen Schillig42428f72009-08-18 15:43:18 +0200159 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200160 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
Christof Schmitt1674b402010-04-30 18:09:34 +0200165 * @qdio: pointer to struct zfcp_qdio
166 * @q_req: pointer to struct zfcp_qdio_req
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig00bab912008-06-10 18:20:57 +0200169 * Returns: number of bytes, or error (negativ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100171int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig01b04752010-07-16 15:37:37 +0200172 struct scatterlist *sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200174 struct qdio_buffer_element *sbale;
Christof Schmitt68322982010-04-30 18:09:33 +0200175 int bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Swen Schillig00bab912008-06-10 18:20:57 +0200177 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200178 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Christof Schmitt1674b402010-04-30 18:09:34 +0200179 sbale->flags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Swen Schillig00bab912008-06-10 18:20:57 +0200181 for (; sg; sg = sg_next(sg)) {
Christof Schmitt1674b402010-04-30 18:09:34 +0200182 sbale = zfcp_qdio_sbale_next(qdio, q_req);
Christof Schmitt68322982010-04-30 18:09:33 +0200183 if (!sbale) {
184 atomic_inc(&qdio->req_q_full);
Swen Schillig706eca42010-07-16 15:37:38 +0200185 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
186 q_req->sbal_number);
Christof Schmitt68322982010-04-30 18:09:33 +0200187 return -EINVAL;
188 }
189
190 sbale->addr = sg_virt(sg);
191 sbale->length = sg->length;
192
Swen Schillig00bab912008-06-10 18:20:57 +0200193 bytes += sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Swen Schillig00bab912008-06-10 18:20:57 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return bytes;
197}
198
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200199static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
200{
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200201 spin_lock_bh(&qdio->req_q_lock);
Swen Schillig706eca42010-07-16 15:37:38 +0200202 if (atomic_read(&qdio->req_q_free) ||
Christof Schmittc2af7542010-06-21 10:11:32 +0200203 !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200204 return 1;
205 spin_unlock_bh(&qdio->req_q_lock);
206 return 0;
207}
208
209/**
210 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
211 * @qdio: pointer to struct zfcp_qdio
212 *
213 * The req_q_lock must be held by the caller of this function, and
214 * this function may only be called from process context; it will
215 * sleep when waiting for a free sbal.
216 *
217 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
218 */
219int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
220{
221 long ret;
222
223 spin_unlock_bh(&qdio->req_q_lock);
224 ret = wait_event_interruptible_timeout(qdio->req_q_wq,
225 zfcp_qdio_sbal_check(qdio), 5 * HZ);
Christof Schmittc2af7542010-06-21 10:11:32 +0200226
227 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
228 return -EIO;
229
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200230 if (ret > 0)
231 return 0;
Christof Schmittc2af7542010-06-21 10:11:32 +0200232
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200233 if (!ret) {
234 atomic_inc(&qdio->req_q_full);
235 /* assume hanging outbound queue, try queue recovery */
236 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1", NULL);
237 }
238
239 spin_lock_bh(&qdio->req_q_lock);
240 return -EIO;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/**
Swen Schillig00bab912008-06-10 18:20:57 +0200244 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200245 * @qdio: pointer to struct zfcp_qdio
Christof Schmitt34c2b712010-02-17 11:18:59 +0100246 * @q_req: pointer to struct zfcp_qdio_req
Swen Schillig00bab912008-06-10 18:20:57 +0200247 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100249int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100251 int retval;
Swen Schillig706eca42010-07-16 15:37:38 +0200252 u8 sbal_number = q_req->sbal_number;
Swen Schillig00bab912008-06-10 18:20:57 +0200253
Swen Schillig564e1c82009-08-18 15:43:19 +0200254 zfcp_qdio_account(qdio);
Martin Peschke94506fd2009-03-02 13:08:56 +0100255
Swen Schillig706eca42010-07-16 15:37:38 +0200256 retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
257 q_req->sbal_first, sbal_number);
258
Swen Schillig00bab912008-06-10 18:20:57 +0200259 if (unlikely(retval)) {
Swen Schillig706eca42010-07-16 15:37:38 +0200260 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
261 sbal_number);
Swen Schillig00bab912008-06-10 18:20:57 +0200262 return retval;
263 }
264
265 /* account for transferred buffers */
Swen Schillig706eca42010-07-16 15:37:38 +0200266 atomic_sub(sbal_number, &qdio->req_q_free);
267 qdio->req_q_idx += sbal_number;
268 qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;
269
Swen Schillig00bab912008-06-10 18:20:57 +0200270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Swen Schillig564e1c82009-08-18 15:43:19 +0200273
274static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
275 struct zfcp_qdio *qdio)
276{
277
278 id->cdev = qdio->adapter->ccw_device;
279 id->q_format = QDIO_ZFCP_QFMT;
280 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
281 ASCEBC(id->adapter_name, 8);
Christof Schmittdcc18f42010-07-16 15:37:41 +0200282 id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
Swen Schillig564e1c82009-08-18 15:43:19 +0200283 id->qib_param_field_format = 0;
284 id->qib_param_field = NULL;
285 id->input_slib_elements = NULL;
286 id->output_slib_elements = NULL;
287 id->no_input_qs = 1;
288 id->no_output_qs = 1;
289 id->input_handler = zfcp_qdio_int_resp;
290 id->output_handler = zfcp_qdio_int_req;
291 id->int_parm = (unsigned long) qdio;
Swen Schillig706eca42010-07-16 15:37:38 +0200292 id->input_sbal_addr_array = (void **) (qdio->res_q);
293 id->output_sbal_addr_array = (void **) (qdio->req_q);
Swen Schillig564e1c82009-08-18 15:43:19 +0200294}
Christof Schmittdcc18f42010-07-16 15:37:41 +0200295
Swen Schillig00bab912008-06-10 18:20:57 +0200296/**
Swen Schillig00bab912008-06-10 18:20:57 +0200297 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
298 * @adapter: pointer to struct zfcp_adapter
299 * Returns: -ENOMEM on memory allocation error or return value from
300 * qdio_allocate
301 */
Swen Schilligd5a282a2009-08-18 15:43:22 +0200302static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200303{
Swen Schillig564e1c82009-08-18 15:43:19 +0200304 struct qdio_initialize init_data;
Swen Schillig00bab912008-06-10 18:20:57 +0200305
Swen Schillig706eca42010-07-16 15:37:38 +0200306 if (zfcp_qdio_buffers_enqueue(qdio->req_q) ||
307 zfcp_qdio_buffers_enqueue(qdio->res_q))
Swen Schillig00bab912008-06-10 18:20:57 +0200308 return -ENOMEM;
309
Swen Schillig564e1c82009-08-18 15:43:19 +0200310 zfcp_qdio_setup_init_data(&init_data, qdio);
Swen Schillig00bab912008-06-10 18:20:57 +0200311
Swen Schillig564e1c82009-08-18 15:43:19 +0200312 return qdio_allocate(&init_data);
Swen Schillig00bab912008-06-10 18:20:57 +0200313}
314
315/**
316 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200317 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200318 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200319void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200320{
Swen Schillig706eca42010-07-16 15:37:38 +0200321 struct zfcp_adapter *adapter = qdio->adapter;
322 int idx, count;
Swen Schillig00bab912008-06-10 18:20:57 +0200323
Swen Schillig706eca42010-07-16 15:37:38 +0200324 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200325 return;
326
327 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Swen Schillig564e1c82009-08-18 15:43:19 +0200328 spin_lock_bh(&qdio->req_q_lock);
Swen Schillig706eca42010-07-16 15:37:38 +0200329 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
Swen Schillig564e1c82009-08-18 15:43:19 +0200330 spin_unlock_bh(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200331
Christof Schmittc2af7542010-06-21 10:11:32 +0200332 wake_up(&qdio->req_q_wq);
333
Swen Schillig706eca42010-07-16 15:37:38 +0200334 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200335
336 /* cleanup used outbound sbals */
Swen Schillig706eca42010-07-16 15:37:38 +0200337 count = atomic_read(&qdio->req_q_free);
Swen Schillig00bab912008-06-10 18:20:57 +0200338 if (count < QDIO_MAX_BUFFERS_PER_Q) {
Swen Schillig706eca42010-07-16 15:37:38 +0200339 idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200340 count = QDIO_MAX_BUFFERS_PER_Q - count;
Swen Schillig706eca42010-07-16 15:37:38 +0200341 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
Swen Schillig00bab912008-06-10 18:20:57 +0200342 }
Swen Schillig706eca42010-07-16 15:37:38 +0200343 qdio->req_q_idx = 0;
344 atomic_set(&qdio->req_q_free, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200345}
346
347/**
348 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200349 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200350 * Returns: 0 on success, otherwise -EIO
351 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200352int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200353{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200354 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200355 struct qdio_initialize init_data;
Swen Schillig706eca42010-07-16 15:37:38 +0200356 struct zfcp_adapter *adapter = qdio->adapter;
357 struct ccw_device *cdev = adapter->ccw_device;
Christof Schmittdcc18f42010-07-16 15:37:41 +0200358 struct qdio_ssqd_desc ssqd;
Swen Schillig00bab912008-06-10 18:20:57 +0200359 int cc;
360
Swen Schillig706eca42010-07-16 15:37:38 +0200361 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200362 return -EIO;
363
Swen Schillig564e1c82009-08-18 15:43:19 +0200364 zfcp_qdio_setup_init_data(&init_data, qdio);
365
366 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200367 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200368
Christof Schmittdcc18f42010-07-16 15:37:41 +0200369 if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
370 goto failed_qdio;
371
372 if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
373 atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
374 &qdio->adapter->status);
375
Swen Schillig564e1c82009-08-18 15:43:19 +0200376 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200377 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200378
379 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200380 sbale = &(qdio->res_q[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200381 sbale->length = 0;
382 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
383 sbale->addr = NULL;
384 }
385
Swen Schillig706eca42010-07-16 15:37:38 +0200386 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200387 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200388
389 /* set index of first avalable SBALS / number of available SBALS */
Swen Schillig706eca42010-07-16 15:37:38 +0200390 qdio->req_q_idx = 0;
391 atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
Swen Schillig00bab912008-06-10 18:20:57 +0200392
393 return 0;
394
395failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200396 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200397failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200398 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200399 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200400 return -EIO;
401}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200402
403void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
404{
Swen Schilligd5a282a2009-08-18 15:43:22 +0200405 int p;
406
407 if (!qdio)
408 return;
409
410 if (qdio->adapter->ccw_device)
411 qdio_free(qdio->adapter->ccw_device);
412
Swen Schilligd5a282a2009-08-18 15:43:22 +0200413 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
Swen Schillig706eca42010-07-16 15:37:38 +0200414 free_page((unsigned long) qdio->req_q[p]);
415 free_page((unsigned long) qdio->res_q[p]);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200416 }
417
418 kfree(qdio);
419}
420
421int zfcp_qdio_setup(struct zfcp_adapter *adapter)
422{
423 struct zfcp_qdio *qdio;
424
425 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
426 if (!qdio)
427 return -ENOMEM;
428
429 qdio->adapter = adapter;
430
431 if (zfcp_qdio_allocate(qdio)) {
432 zfcp_qdio_destroy(qdio);
433 return -ENOMEM;
434 }
435
436 spin_lock_init(&qdio->req_q_lock);
437 spin_lock_init(&qdio->stat_lock);
438
439 adapter->qdio = qdio;
440 return 0;
441}
442