blob: d9c40ea73eef4864e6a477d2803b61309e037a1a [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
Christof Schmitt339f4f42010-07-16 15:37:43 +020033static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id,
34 unsigned int qdio_err)
Swen Schillig00bab912008-06-10 18:20:57 +020035{
Swen Schillig564e1c82009-08-18 15:43:19 +020036 struct zfcp_adapter *adapter = qdio->adapter;
37
Christof Schmittff3b24f2008-10-01 12:42:15 +020038 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
Swen Schillig00bab912008-06-10 18:20:57 +020039
Christof Schmitt339f4f42010-07-16 15:37:43 +020040 if (qdio_err & QDIO_ERROR_SLSB_STATE)
41 zfcp_qdio_siosl(adapter);
Swen Schillig00bab912008-06-10 18:20:57 +020042 zfcp_erp_adapter_reopen(adapter,
43 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
Swen Schilligea4a3a62010-12-02 15:16:16 +010044 ZFCP_STATUS_COMMON_ERP_FAILED, id);
Swen Schillig00bab912008-06-10 18:20:57 +020045}
46
Christof Schmitt5d4e2262008-07-02 10:56:34 +020047static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
48{
49 int i, sbal_idx;
50
51 for (i = first; i < first + cnt; i++) {
52 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
53 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
54 }
55}
56
Martin Peschke94506fd2009-03-02 13:08:56 +010057/* this needs to be called prior to updating the queue fill level */
Heiko Carstens41e05a12009-08-18 15:43:32 +020058static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
Martin Peschke94506fd2009-03-02 13:08:56 +010059{
Heiko Carstens41e05a12009-08-18 15:43:32 +020060 unsigned long long now, span;
Swen Schillig706eca42010-07-16 15:37:38 +020061 int used;
Martin Peschke94506fd2009-03-02 13:08:56 +010062
Heiko Carstens41e05a12009-08-18 15:43:32 +020063 now = get_clock_monotonic();
64 span = (now - qdio->req_q_time) >> 12;
Swen Schillig706eca42010-07-16 15:37:38 +020065 used = QDIO_MAX_BUFFERS_PER_Q - atomic_read(&qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020066 qdio->req_q_util += used * span;
67 qdio->req_q_time = now;
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,
Swen Schillig706eca42010-07-16 15:37:38 +020071 int queue_no, int idx, 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;
Swen Schillig00bab912008-06-10 18:20:57 +020075
Jan Glauber779e6e12008-07-17 17:16:48 +020076 if (unlikely(qdio_err)) {
Christof Schmitt339f4f42010-07-16 15:37:43 +020077 zfcp_qdio_handler_error(qdio, "qdireq1", qdio_err);
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
Christof Schmitt44a24cb2010-09-08 14:39:57 +020084 spin_lock_irq(&qdio->stat_lock);
Swen Schillig564e1c82009-08-18 15:43:19 +020085 zfcp_qdio_account(qdio);
Christof Schmitt44a24cb2010-09-08 14:39:57 +020086 spin_unlock_irq(&qdio->stat_lock);
Swen Schillig706eca42010-07-16 15:37:38 +020087 atomic_add(count, &qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020088 wake_up(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +020089}
90
Jan Glauber779e6e12008-07-17 17:16:48 +020091static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
Swen Schillig706eca42010-07-16 15:37:38 +020092 int queue_no, int idx, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020093 unsigned long parm)
94{
Swen Schillig564e1c82009-08-18 15:43:19 +020095 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schilligbd63eaf2009-08-18 15:43:13 +020096 int sbal_idx, sbal_no;
Swen Schillig00bab912008-06-10 18:20:57 +020097
Jan Glauber779e6e12008-07-17 17:16:48 +020098 if (unlikely(qdio_err)) {
Christof Schmitt339f4f42010-07-16 15:37:43 +020099 zfcp_qdio_handler_error(qdio, "qdires1", qdio_err);
Swen Schillig00bab912008-06-10 18:20:57 +0200100 return;
101 }
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /*
104 * go through all SBALs from input queue currently
105 * returned by QDIO layer
106 */
Swen Schillig00bab912008-06-10 18:20:57 +0200107 for (sbal_no = 0; sbal_no < count; sbal_no++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200108 sbal_idx = (idx + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* go through all SBALEs of SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200110 zfcp_fsf_reqid_check(qdio, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 /*
Swen Schillig706eca42010-07-16 15:37:38 +0200114 * put SBALs back to response queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Swen Schillig706eca42010-07-16 15:37:38 +0200116 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
Swen Schilligea4a3a62010-12-02 15:16:16 +0100117 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Swen Schillig44cc76f2008-10-01 12:42:16 +0200120static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200121zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200123 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200126 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200127 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200130 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return NULL;
132
133 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200134 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200135 sbale->sflags |= SBAL_SFLAGS0_MORE_SBALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200138 q_req->sbal_last++;
139 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200142 q_req->sbal_number++;
Swen Schillig01b04752010-07-16 15:37:37 +0200143 BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200146 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200149 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200150 sbale->sflags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 return sbale;
153}
154
Swen Schillig44cc76f2008-10-01 12:42:16 +0200155static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200156zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Christof Schmitt1674b402010-04-30 18:09:34 +0200158 if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
159 return zfcp_qdio_sbal_chain(qdio, q_req);
Swen Schillig42428f72009-08-18 15:43:18 +0200160 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200161 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/**
165 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
Christof Schmitt1674b402010-04-30 18:09:34 +0200166 * @qdio: pointer to struct zfcp_qdio
167 * @q_req: pointer to struct zfcp_qdio_req
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig00bab912008-06-10 18:20:57 +0200170 * Returns: number of bytes, or error (negativ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100172int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig01b04752010-07-16 15:37:37 +0200173 struct scatterlist *sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200175 struct qdio_buffer_element *sbale;
Christof Schmitt68322982010-04-30 18:09:33 +0200176 int bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Swen Schillig00bab912008-06-10 18:20:57 +0200178 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200179 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200180 sbale->sflags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Swen Schillig00bab912008-06-10 18:20:57 +0200182 for (; sg; sg = sg_next(sg)) {
Christof Schmitt1674b402010-04-30 18:09:34 +0200183 sbale = zfcp_qdio_sbale_next(qdio, q_req);
Christof Schmitt68322982010-04-30 18:09:33 +0200184 if (!sbale) {
185 atomic_inc(&qdio->req_q_full);
Swen Schillig706eca42010-07-16 15:37:38 +0200186 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
187 q_req->sbal_number);
Christof Schmitt68322982010-04-30 18:09:33 +0200188 return -EINVAL;
189 }
190
191 sbale->addr = sg_virt(sg);
192 sbale->length = sg->length;
193
Swen Schillig00bab912008-06-10 18:20:57 +0200194 bytes += sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Swen Schillig00bab912008-06-10 18:20:57 +0200196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return bytes;
198}
199
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200200static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
201{
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200202 spin_lock_irq(&qdio->req_q_lock);
Swen Schillig706eca42010-07-16 15:37:38 +0200203 if (atomic_read(&qdio->req_q_free) ||
Christof Schmittc2af7542010-06-21 10:11:32 +0200204 !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200205 return 1;
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200206 spin_unlock_irq(&qdio->req_q_lock);
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200207 return 0;
208}
209
210/**
211 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
212 * @qdio: pointer to struct zfcp_qdio
213 *
214 * The req_q_lock must be held by the caller of this function, and
215 * this function may only be called from process context; it will
216 * sleep when waiting for a free sbal.
217 *
218 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
219 */
220int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
221{
222 long ret;
223
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200224 spin_unlock_irq(&qdio->req_q_lock);
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200225 ret = wait_event_interruptible_timeout(qdio->req_q_wq,
226 zfcp_qdio_sbal_check(qdio), 5 * HZ);
Christof Schmittc2af7542010-06-21 10:11:32 +0200227
228 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
229 return -EIO;
230
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200231 if (ret > 0)
232 return 0;
Christof Schmittc2af7542010-06-21 10:11:32 +0200233
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200234 if (!ret) {
235 atomic_inc(&qdio->req_q_full);
236 /* assume hanging outbound queue, try queue recovery */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100237 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1");
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200238 }
239
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200240 spin_lock_irq(&qdio->req_q_lock);
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200241 return -EIO;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/**
Swen Schillig00bab912008-06-10 18:20:57 +0200245 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200246 * @qdio: pointer to struct zfcp_qdio
Christof Schmitt34c2b712010-02-17 11:18:59 +0100247 * @q_req: pointer to struct zfcp_qdio_req
Swen Schillig00bab912008-06-10 18:20:57 +0200248 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100250int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100252 int retval;
Swen Schillig706eca42010-07-16 15:37:38 +0200253 u8 sbal_number = q_req->sbal_number;
Swen Schillig00bab912008-06-10 18:20:57 +0200254
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200255 spin_lock(&qdio->stat_lock);
Swen Schillig564e1c82009-08-18 15:43:19 +0200256 zfcp_qdio_account(qdio);
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200257 spin_unlock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +0100258
Swen Schillig706eca42010-07-16 15:37:38 +0200259 retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
260 q_req->sbal_first, sbal_number);
261
Swen Schillig00bab912008-06-10 18:20:57 +0200262 if (unlikely(retval)) {
Swen Schillig706eca42010-07-16 15:37:38 +0200263 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
264 sbal_number);
Swen Schillig00bab912008-06-10 18:20:57 +0200265 return retval;
266 }
267
268 /* account for transferred buffers */
Swen Schillig706eca42010-07-16 15:37:38 +0200269 atomic_sub(sbal_number, &qdio->req_q_free);
270 qdio->req_q_idx += sbal_number;
271 qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;
272
Swen Schillig00bab912008-06-10 18:20:57 +0200273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Swen Schillig564e1c82009-08-18 15:43:19 +0200276
277static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
278 struct zfcp_qdio *qdio)
279{
Jan Glauberd36deae2010-09-07 21:14:39 +0000280 memset(id, 0, sizeof(*id));
Swen Schillig564e1c82009-08-18 15:43:19 +0200281 id->cdev = qdio->adapter->ccw_device;
282 id->q_format = QDIO_ZFCP_QFMT;
283 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
284 ASCEBC(id->adapter_name, 8);
Christof Schmittdcc18f42010-07-16 15:37:41 +0200285 id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
Swen Schillig564e1c82009-08-18 15:43:19 +0200286 id->no_input_qs = 1;
287 id->no_output_qs = 1;
288 id->input_handler = zfcp_qdio_int_resp;
289 id->output_handler = zfcp_qdio_int_req;
290 id->int_parm = (unsigned long) qdio;
Swen Schillig706eca42010-07-16 15:37:38 +0200291 id->input_sbal_addr_array = (void **) (qdio->res_q);
292 id->output_sbal_addr_array = (void **) (qdio->req_q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +0100293 id->scan_threshold =
294 QDIO_MAX_BUFFERS_PER_Q - ZFCP_QDIO_MAX_SBALS_PER_REQ * 2;
Swen Schillig564e1c82009-08-18 15:43:19 +0200295}
Christof Schmittdcc18f42010-07-16 15:37:41 +0200296
Swen Schillig00bab912008-06-10 18:20:57 +0200297/**
Swen Schillig00bab912008-06-10 18:20:57 +0200298 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
299 * @adapter: pointer to struct zfcp_adapter
300 * Returns: -ENOMEM on memory allocation error or return value from
301 * qdio_allocate
302 */
Swen Schilligd5a282a2009-08-18 15:43:22 +0200303static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200304{
Swen Schillig564e1c82009-08-18 15:43:19 +0200305 struct qdio_initialize init_data;
Swen Schillig00bab912008-06-10 18:20:57 +0200306
Swen Schillig706eca42010-07-16 15:37:38 +0200307 if (zfcp_qdio_buffers_enqueue(qdio->req_q) ||
308 zfcp_qdio_buffers_enqueue(qdio->res_q))
Swen Schillig00bab912008-06-10 18:20:57 +0200309 return -ENOMEM;
310
Swen Schillig564e1c82009-08-18 15:43:19 +0200311 zfcp_qdio_setup_init_data(&init_data, qdio);
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100312 init_waitqueue_head(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +0200313
Swen Schillig564e1c82009-08-18 15:43:19 +0200314 return qdio_allocate(&init_data);
Swen Schillig00bab912008-06-10 18:20:57 +0200315}
316
317/**
318 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200319 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200320 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200321void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200322{
Swen Schillig706eca42010-07-16 15:37:38 +0200323 struct zfcp_adapter *adapter = qdio->adapter;
324 int idx, count;
Swen Schillig00bab912008-06-10 18:20:57 +0200325
Swen Schillig706eca42010-07-16 15:37:38 +0200326 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200327 return;
328
329 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200330 spin_lock_irq(&qdio->req_q_lock);
Swen Schillig706eca42010-07-16 15:37:38 +0200331 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200332 spin_unlock_irq(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200333
Christof Schmittc2af7542010-06-21 10:11:32 +0200334 wake_up(&qdio->req_q_wq);
335
Swen Schillig706eca42010-07-16 15:37:38 +0200336 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200337
338 /* cleanup used outbound sbals */
Swen Schillig706eca42010-07-16 15:37:38 +0200339 count = atomic_read(&qdio->req_q_free);
Swen Schillig00bab912008-06-10 18:20:57 +0200340 if (count < QDIO_MAX_BUFFERS_PER_Q) {
Swen Schillig706eca42010-07-16 15:37:38 +0200341 idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200342 count = QDIO_MAX_BUFFERS_PER_Q - count;
Swen Schillig706eca42010-07-16 15:37:38 +0200343 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
Swen Schillig00bab912008-06-10 18:20:57 +0200344 }
Swen Schillig706eca42010-07-16 15:37:38 +0200345 qdio->req_q_idx = 0;
346 atomic_set(&qdio->req_q_free, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200347}
348
349/**
350 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200351 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200352 * Returns: 0 on success, otherwise -EIO
353 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200354int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200355{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200356 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200357 struct qdio_initialize init_data;
Swen Schillig706eca42010-07-16 15:37:38 +0200358 struct zfcp_adapter *adapter = qdio->adapter;
359 struct ccw_device *cdev = adapter->ccw_device;
Christof Schmittdcc18f42010-07-16 15:37:41 +0200360 struct qdio_ssqd_desc ssqd;
Swen Schillig00bab912008-06-10 18:20:57 +0200361 int cc;
362
Swen Schillig706eca42010-07-16 15:37:38 +0200363 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200364 return -EIO;
365
Christof Schmitt339f4f42010-07-16 15:37:43 +0200366 atomic_clear_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
367 &qdio->adapter->status);
368
Swen Schillig564e1c82009-08-18 15:43:19 +0200369 zfcp_qdio_setup_init_data(&init_data, qdio);
370
371 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200372 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200373
Christof Schmittdcc18f42010-07-16 15:37:41 +0200374 if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
375 goto failed_qdio;
376
377 if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
378 atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
379 &qdio->adapter->status);
380
Swen Schillig564e1c82009-08-18 15:43:19 +0200381 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200382 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200383
384 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200385 sbale = &(qdio->res_q[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200386 sbale->length = 0;
Jan Glauber3ec908782011-06-06 14:14:40 +0200387 sbale->eflags = SBAL_EFLAGS_LAST_ENTRY;
388 sbale->sflags = 0;
Swen Schillig00bab912008-06-10 18:20:57 +0200389 sbale->addr = NULL;
390 }
391
Swen Schillig706eca42010-07-16 15:37:38 +0200392 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200393 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200394
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300395 /* set index of first available SBALS / number of available SBALS */
Swen Schillig706eca42010-07-16 15:37:38 +0200396 qdio->req_q_idx = 0;
397 atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100398 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
Swen Schillig00bab912008-06-10 18:20:57 +0200399
400 return 0;
401
402failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200403 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200404failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200405 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200406 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200407 return -EIO;
408}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200409
410void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
411{
Swen Schilligd5a282a2009-08-18 15:43:22 +0200412 int p;
413
414 if (!qdio)
415 return;
416
417 if (qdio->adapter->ccw_device)
418 qdio_free(qdio->adapter->ccw_device);
419
Swen Schilligd5a282a2009-08-18 15:43:22 +0200420 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
Swen Schillig706eca42010-07-16 15:37:38 +0200421 free_page((unsigned long) qdio->req_q[p]);
422 free_page((unsigned long) qdio->res_q[p]);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200423 }
424
425 kfree(qdio);
426}
427
428int zfcp_qdio_setup(struct zfcp_adapter *adapter)
429{
430 struct zfcp_qdio *qdio;
431
432 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
433 if (!qdio)
434 return -ENOMEM;
435
436 qdio->adapter = adapter;
437
438 if (zfcp_qdio_allocate(qdio)) {
439 zfcp_qdio_destroy(qdio);
440 return -ENOMEM;
441 }
442
443 spin_lock_init(&qdio->req_q_lock);
444 spin_lock_init(&qdio->stat_lock);
445
446 adapter->qdio = qdio;
447 return 0;
448}
449
Christof Schmitt339f4f42010-07-16 15:37:43 +0200450/**
451 * zfcp_qdio_siosl - Trigger logging in FCP channel
452 * @adapter: The zfcp_adapter where to trigger logging
453 *
454 * Call the cio siosl function to trigger hardware logging. This
455 * wrapper function sets a flag to ensure hardware logging is only
456 * triggered once before going through qdio shutdown.
457 *
458 * The triggers are always run from qdio tasklet context, so no
459 * additional synchronization is necessary.
460 */
461void zfcp_qdio_siosl(struct zfcp_adapter *adapter)
462{
463 int rc;
464
465 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED)
466 return;
467
468 rc = ccw_device_siosl(adapter->ccw_device);
469 if (!rc)
470 atomic_set_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
471 &adapter->status);
472}