blob: dbfa312a7f50bb19e66812c310f22cd02fa4439b [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 *
Swen Schillig42428f72009-08-18 15:43:18 +02006 * Copyright IBM Corporation 2002, 2009
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 Schillig564e1c82009-08-18 15:43:19 +0200144static void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
Christof Schmitt34c2b712010-02-17 11:18:59 +0100145 struct zfcp_qdio_req *q_req, int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Swen Schillig564e1c82009-08-18 15:43:19 +0200147 int count = atomic_read(&qdio->req_q.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 count = min(count, max_sbals);
Swen Schillig42428f72009-08-18 15:43:18 +0200149 q_req->sbal_limit = (q_req->sbal_first + count - 1)
Swen Schillig00bab912008-06-10 18:20:57 +0200150 % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Swen Schillig44cc76f2008-10-01 12:42:16 +0200153static struct qdio_buffer_element *
Christof Schmitt34c2b712010-02-17 11:18:59 +0100154zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig42428f72009-08-18 15:43:18 +0200155 unsigned long sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200157 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200160 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
162
163 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200164 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return NULL;
166
167 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200168 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
170
171 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200172 q_req->sbal_last++;
173 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200176 q_req->sbal_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200179 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200182 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 sbale->flags |= sbtype;
184
185 return sbale;
186}
187
Swen Schillig44cc76f2008-10-01 12:42:16 +0200188static struct qdio_buffer_element *
Christof Schmitt34c2b712010-02-17 11:18:59 +0100189zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig42428f72009-08-18 15:43:18 +0200190 unsigned int sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Swen Schillig42428f72009-08-18 15:43:18 +0200192 if (q_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
Swen Schillig564e1c82009-08-18 15:43:19 +0200193 return zfcp_qdio_sbal_chain(qdio, q_req, sbtype);
Swen Schillig42428f72009-08-18 15:43:18 +0200194 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200195 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Swen Schillig564e1c82009-08-18 15:43:19 +0200198static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
Christof Schmitt34c2b712010-02-17 11:18:59 +0100199 struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Swen Schillig564e1c82009-08-18 15:43:19 +0200201 struct qdio_buffer **sbal = qdio->req_q.sbal;
Swen Schillig42428f72009-08-18 15:43:18 +0200202 int first = q_req->sbal_first;
203 int last = q_req->sbal_last;
Swen Schillig00bab912008-06-10 18:20:57 +0200204 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
205 QDIO_MAX_BUFFERS_PER_Q + 1;
206 zfcp_qdio_zero_sbals(sbal, first, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Swen Schillig564e1c82009-08-18 15:43:19 +0200209static int zfcp_qdio_fill_sbals(struct zfcp_qdio *qdio,
Christof Schmitt34c2b712010-02-17 11:18:59 +0100210 struct zfcp_qdio_req *q_req,
Swen Schillig00bab912008-06-10 18:20:57 +0200211 unsigned int sbtype, void *start_addr,
212 unsigned int total_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200214 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 unsigned long remaining, length;
216 void *addr;
217
Swen Schillig00bab912008-06-10 18:20:57 +0200218 /* split segment up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 for (addr = start_addr, remaining = total_length; remaining > 0;
220 addr += length, remaining -= length) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200221 sbale = zfcp_qdio_sbale_next(qdio, q_req, sbtype);
Swen Schillig00bab912008-06-10 18:20:57 +0200222 if (!sbale) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200223 atomic_inc(&qdio->req_q_full);
224 zfcp_qdio_undo_sbals(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EINVAL;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Swen Schillig00bab912008-06-10 18:20:57 +0200228 /* new piece must not exceed next page boundary */
229 length = min(remaining,
230 (PAGE_SIZE - ((unsigned long)addr &
231 (PAGE_SIZE - 1))));
232 sbale->addr = addr;
233 sbale->length = length;
234 }
235 return 0;
236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238/**
239 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
240 * @fsf_req: request to be processed
241 * @sbtype: SBALE flags
242 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig00bab912008-06-10 18:20:57 +0200244 * Returns: number of bytes, or error (negativ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100246int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig42428f72009-08-18 15:43:18 +0200247 unsigned long sbtype, struct scatterlist *sg,
248 int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200250 struct qdio_buffer_element *sbale;
Swen Schillig00bab912008-06-10 18:20:57 +0200251 int retval, bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* figure out last allowed SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200254 zfcp_qdio_sbal_limit(qdio, q_req, max_sbals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Swen Schillig00bab912008-06-10 18:20:57 +0200256 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200257 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sbale->flags |= sbtype;
259
Swen Schillig00bab912008-06-10 18:20:57 +0200260 for (; sg; sg = sg_next(sg)) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200261 retval = zfcp_qdio_fill_sbals(qdio, q_req, sbtype,
Swen Schillig42428f72009-08-18 15:43:18 +0200262 sg_virt(sg), sg->length);
Swen Schillig00bab912008-06-10 18:20:57 +0200263 if (retval < 0)
264 return retval;
265 bytes += sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Swen Schillig00bab912008-06-10 18:20:57 +0200267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* assume that no other SBALEs are to follow in the same SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200269 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
Swen Schillig00bab912008-06-10 18:20:57 +0200271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return bytes;
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/**
Swen Schillig00bab912008-06-10 18:20:57 +0200276 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200277 * @qdio: pointer to struct zfcp_qdio
Christof Schmitt34c2b712010-02-17 11:18:59 +0100278 * @q_req: pointer to struct zfcp_qdio_req
Swen Schillig00bab912008-06-10 18:20:57 +0200279 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100281int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Swen Schillig564e1c82009-08-18 15:43:19 +0200283 struct zfcp_qdio_queue *req_q = &qdio->req_q;
Swen Schillig42428f72009-08-18 15:43:18 +0200284 int first = q_req->sbal_first;
285 int count = q_req->sbal_number;
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100286 int retval;
287 unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
Swen Schillig00bab912008-06-10 18:20:57 +0200288
Swen Schillig564e1c82009-08-18 15:43:19 +0200289 zfcp_qdio_account(qdio);
Martin Peschke94506fd2009-03-02 13:08:56 +0100290
Swen Schillig564e1c82009-08-18 15:43:19 +0200291 retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
292 count);
Swen Schillig00bab912008-06-10 18:20:57 +0200293 if (unlikely(retval)) {
294 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
295 return retval;
296 }
297
298 /* account for transferred buffers */
299 atomic_sub(count, &req_q->count);
300 req_q->first += count;
301 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Swen Schillig564e1c82009-08-18 15:43:19 +0200305
306static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
307 struct zfcp_qdio *qdio)
308{
309
310 id->cdev = qdio->adapter->ccw_device;
311 id->q_format = QDIO_ZFCP_QFMT;
312 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
313 ASCEBC(id->adapter_name, 8);
314 id->qib_param_field_format = 0;
315 id->qib_param_field = NULL;
316 id->input_slib_elements = NULL;
317 id->output_slib_elements = NULL;
318 id->no_input_qs = 1;
319 id->no_output_qs = 1;
320 id->input_handler = zfcp_qdio_int_resp;
321 id->output_handler = zfcp_qdio_int_req;
322 id->int_parm = (unsigned long) qdio;
Swen Schillig564e1c82009-08-18 15:43:19 +0200323 id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
324 id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);
325
326}
Swen Schillig00bab912008-06-10 18:20:57 +0200327/**
Swen Schillig00bab912008-06-10 18:20:57 +0200328 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
329 * @adapter: pointer to struct zfcp_adapter
330 * Returns: -ENOMEM on memory allocation error or return value from
331 * qdio_allocate
332 */
Swen Schilligd5a282a2009-08-18 15:43:22 +0200333static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200334{
Swen Schillig564e1c82009-08-18 15:43:19 +0200335 struct qdio_initialize init_data;
Swen Schillig00bab912008-06-10 18:20:57 +0200336
Swen Schillig564e1c82009-08-18 15:43:19 +0200337 if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
338 zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
Swen Schillig00bab912008-06-10 18:20:57 +0200339 return -ENOMEM;
340
Swen Schillig564e1c82009-08-18 15:43:19 +0200341 zfcp_qdio_setup_init_data(&init_data, qdio);
Swen Schillig00bab912008-06-10 18:20:57 +0200342
Swen Schillig564e1c82009-08-18 15:43:19 +0200343 return qdio_allocate(&init_data);
Swen Schillig00bab912008-06-10 18:20:57 +0200344}
345
346/**
347 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200348 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200349 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200350void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200351{
352 struct zfcp_qdio_queue *req_q;
353 int first, count;
354
Swen Schillig564e1c82009-08-18 15:43:19 +0200355 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200356 return;
357
358 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Swen Schillig564e1c82009-08-18 15:43:19 +0200359 req_q = &qdio->req_q;
360 spin_lock_bh(&qdio->req_q_lock);
361 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
362 spin_unlock_bh(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200363
Swen Schillig564e1c82009-08-18 15:43:19 +0200364 qdio_shutdown(qdio->adapter->ccw_device,
365 QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200366
367 /* cleanup used outbound sbals */
368 count = atomic_read(&req_q->count);
369 if (count < QDIO_MAX_BUFFERS_PER_Q) {
370 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
371 count = QDIO_MAX_BUFFERS_PER_Q - count;
372 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
373 }
374 req_q->first = 0;
375 atomic_set(&req_q->count, 0);
Swen Schillig564e1c82009-08-18 15:43:19 +0200376 qdio->resp_q.first = 0;
377 atomic_set(&qdio->resp_q.count, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200378}
379
380/**
381 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200382 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200383 * Returns: 0 on success, otherwise -EIO
384 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200385int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200386{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200387 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200388 struct qdio_initialize init_data;
389 struct ccw_device *cdev = qdio->adapter->ccw_device;
Swen Schillig00bab912008-06-10 18:20:57 +0200390 int cc;
391
Swen Schillig564e1c82009-08-18 15:43:19 +0200392 if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200393 return -EIO;
394
Swen Schillig564e1c82009-08-18 15:43:19 +0200395 zfcp_qdio_setup_init_data(&init_data, qdio);
396
397 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200398 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200399
Swen Schillig564e1c82009-08-18 15:43:19 +0200400 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200401 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200402
403 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200404 sbale = &(qdio->resp_q.sbal[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200405 sbale->length = 0;
406 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
407 sbale->addr = NULL;
408 }
409
Swen Schillig564e1c82009-08-18 15:43:19 +0200410 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200411 QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200412 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200413
414 /* set index of first avalable SBALS / number of available SBALS */
Swen Schillig564e1c82009-08-18 15:43:19 +0200415 qdio->req_q.first = 0;
416 atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
Swen Schillig00bab912008-06-10 18:20:57 +0200417
418 return 0;
419
420failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200421 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200422failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200423 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200424 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200425 return -EIO;
426}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200427
428void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
429{
430 struct qdio_buffer **sbal_req, **sbal_resp;
431 int p;
432
433 if (!qdio)
434 return;
435
436 if (qdio->adapter->ccw_device)
437 qdio_free(qdio->adapter->ccw_device);
438
439 sbal_req = qdio->req_q.sbal;
440 sbal_resp = qdio->resp_q.sbal;
441
442 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
443 free_page((unsigned long) sbal_req[p]);
444 free_page((unsigned long) sbal_resp[p]);
445 }
446
447 kfree(qdio);
448}
449
450int zfcp_qdio_setup(struct zfcp_adapter *adapter)
451{
452 struct zfcp_qdio *qdio;
453
454 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
455 if (!qdio)
456 return -ENOMEM;
457
458 qdio->adapter = adapter;
459
460 if (zfcp_qdio_allocate(qdio)) {
461 zfcp_qdio_destroy(qdio);
462 return -ENOMEM;
463 }
464
465 spin_lock_init(&qdio->req_q_lock);
466 spin_lock_init(&qdio->stat_lock);
467
468 adapter->qdio = qdio;
469 return 0;
470}
471