blob: e118874976f088d29706e5ecb2f3ca2fa6de8bc2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "zfcp_ext.h"
13
Christof Schmitt5d4e2262008-07-02 10:56:34 +020014#define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Swen Schillig00bab912008-06-10 18:20:57 +020016static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
Swen Schilligb4e44592007-07-18 10:55:13 +020017{
18 int pos;
19
20 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
Swen Schillig00bab912008-06-10 18:20:57 +020021 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
22 if (!sbal[pos])
Swen Schilligb4e44592007-07-18 10:55:13 +020023 return -ENOMEM;
Swen Schilligb4e44592007-07-18 10:55:13 +020024 }
25 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
26 if (pos % QBUFF_PER_PAGE)
Swen Schillig00bab912008-06-10 18:20:57 +020027 sbal[pos] = sbal[pos - 1] + 1;
Swen Schilligb4e44592007-07-18 10:55:13 +020028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30
Swen Schillig44cc76f2008-10-01 12:42:16 +020031static struct qdio_buffer_element *
Swen Schillig00bab912008-06-10 18:20:57 +020032zfcp_qdio_sbale(struct zfcp_qdio_queue *q, int sbal_idx, int sbale_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Swen Schillig00bab912008-06-10 18:20:57 +020034 return &q->sbal[sbal_idx]->element[sbale_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Volker Sameskefea9d6c2006-08-02 11:05:16 +020037/**
Swen Schillig00bab912008-06-10 18:20:57 +020038 * zfcp_qdio_free - free memory used by request- and resposne queue
39 * @adapter: pointer to the zfcp_adapter structure
Volker Sameskefea9d6c2006-08-02 11:05:16 +020040 */
Swen Schillig00bab912008-06-10 18:20:57 +020041void zfcp_qdio_free(struct zfcp_adapter *adapter)
42{
43 struct qdio_buffer **sbal_req, **sbal_resp;
44 int p;
45
46 if (adapter->ccw_device)
47 qdio_free(adapter->ccw_device);
48
49 sbal_req = adapter->req_q.sbal;
50 sbal_resp = adapter->resp_q.sbal;
51
52 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
53 free_page((unsigned long) sbal_req[p]);
54 free_page((unsigned long) sbal_resp[p]);
55 }
56}
57
Swen Schillig5ffd51a2009-03-02 13:09:04 +010058static void zfcp_qdio_handler_error(struct zfcp_adapter *adapter, char *id)
Swen Schillig00bab912008-06-10 18:20:57 +020059{
Christof Schmittff3b24f2008-10-01 12:42:15 +020060 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
Swen Schillig00bab912008-06-10 18:20:57 +020061
62 zfcp_erp_adapter_reopen(adapter,
63 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
64 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
65}
66
Christof Schmitt5d4e2262008-07-02 10:56:34 +020067static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
68{
69 int i, sbal_idx;
70
71 for (i = first; i < first + cnt; i++) {
72 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
73 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
74 }
75}
76
Martin Peschke94506fd2009-03-02 13:08:56 +010077/* this needs to be called prior to updating the queue fill level */
78static void zfcp_qdio_account(struct zfcp_adapter *adapter)
79{
80 ktime_t now;
81 s64 span;
82 int free, used;
83
84 spin_lock(&adapter->qdio_stat_lock);
85 now = ktime_get();
86 span = ktime_us_delta(now, adapter->req_q_time);
87 free = max(0, atomic_read(&adapter->req_q.count));
88 used = QDIO_MAX_BUFFERS_PER_Q - free;
89 adapter->req_q_util += used * span;
90 adapter->req_q_time = now;
91 spin_unlock(&adapter->qdio_stat_lock);
92}
93
Jan Glauber779e6e12008-07-17 17:16:48 +020094static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
95 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020096 unsigned long parm)
97{
98 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
99 struct zfcp_qdio_queue *queue = &adapter->req_q;
100
Jan Glauber779e6e12008-07-17 17:16:48 +0200101 if (unlikely(qdio_err)) {
102 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100103 zfcp_qdio_handler_error(adapter, "qdireq1");
Swen Schillig00bab912008-06-10 18:20:57 +0200104 return;
105 }
106
107 /* cleanup all SBALs being program-owned now */
108 zfcp_qdio_zero_sbals(queue->sbal, first, count);
109
Martin Peschke94506fd2009-03-02 13:08:56 +0100110 zfcp_qdio_account(adapter);
Swen Schillig00bab912008-06-10 18:20:57 +0200111 atomic_add(count, &queue->count);
112 wake_up(&adapter->request_wq);
113}
114
Swen Schillig00bab912008-06-10 18:20:57 +0200115static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Swen Schillig00bab912008-06-10 18:20:57 +0200117 struct zfcp_qdio_queue *queue = &adapter->resp_q;
118 struct ccw_device *cdev = adapter->ccw_device;
119 u8 count, start = queue->first;
120 unsigned int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Swen Schillig00bab912008-06-10 18:20:57 +0200122 count = atomic_read(&queue->count) + processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jan Glauber779e6e12008-07-17 17:16:48 +0200124 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Swen Schillig00bab912008-06-10 18:20:57 +0200126 if (unlikely(retval)) {
127 atomic_set(&queue->count, count);
128 /* FIXME: Recover this with an adapter reopen? */
129 } else {
130 queue->first += count;
131 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
132 atomic_set(&queue->count, 0);
133 }
134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jan Glauber779e6e12008-07-17 17:16:48 +0200136static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
137 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +0200138 unsigned long parm)
139{
140 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
Swen Schilligbd63eaf2009-08-18 15:43:13 +0200141 int sbal_idx, sbal_no;
Swen Schillig00bab912008-06-10 18:20:57 +0200142
Jan Glauber779e6e12008-07-17 17:16:48 +0200143 if (unlikely(qdio_err)) {
144 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100145 zfcp_qdio_handler_error(adapter, "qdires1");
Swen Schillig00bab912008-06-10 18:20:57 +0200146 return;
147 }
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * go through all SBALs from input queue currently
151 * returned by QDIO layer
152 */
Swen Schillig00bab912008-06-10 18:20:57 +0200153 for (sbal_no = 0; sbal_no < count; sbal_no++) {
154 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* go through all SBALEs of SBAL */
Swen Schilligbd63eaf2009-08-18 15:43:13 +0200156 zfcp_fsf_reqid_check(adapter, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159 /*
160 * put range of SBALs back to response queue
161 * (including SBALs which have already been free before)
162 */
Swen Schillig00bab912008-06-10 18:20:57 +0200163 zfcp_qdio_resp_put_back(adapter, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/**
Swen Schillig00bab912008-06-10 18:20:57 +0200167 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
Swen Schillig42428f72009-08-18 15:43:18 +0200168 * @adapter: pointer to struct zfcp_adapter
169 * @q_rec: pointer to struct zfcp_queue_rec
Swen Schillig00bab912008-06-10 18:20:57 +0200170 * Returns: pointer to qdio_buffer_element (SBALE) structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Swen Schillig42428f72009-08-18 15:43:18 +0200172struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_adapter *adapter,
173 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Swen Schillig42428f72009-08-18 15:43:18 +0200175 return zfcp_qdio_sbale(&adapter->req_q, q_req->sbal_last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/**
Swen Schillig00bab912008-06-10 18:20:57 +0200179 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
180 * @fsf_req: pointer to struct fsf_req
181 * Returns: pointer to qdio_buffer_element (SBALE) structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Swen Schillig42428f72009-08-18 15:43:18 +0200183struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_adapter *adapter,
184 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Swen Schillig42428f72009-08-18 15:43:18 +0200186 return zfcp_qdio_sbale(&adapter->req_q, q_req->sbal_last,
187 q_req->sbale_curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Swen Schillig42428f72009-08-18 15:43:18 +0200190static void zfcp_qdio_sbal_limit(struct zfcp_adapter *adapter,
191 struct zfcp_queue_req *q_req, int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Swen Schillig42428f72009-08-18 15:43:18 +0200193 int count = atomic_read(&adapter->req_q.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 count = min(count, max_sbals);
Swen Schillig42428f72009-08-18 15:43:18 +0200195 q_req->sbal_limit = (q_req->sbal_first + count - 1)
Swen Schillig00bab912008-06-10 18:20:57 +0200196 % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Swen Schillig44cc76f2008-10-01 12:42:16 +0200199static struct qdio_buffer_element *
Swen Schillig42428f72009-08-18 15:43:18 +0200200zfcp_qdio_sbal_chain(struct zfcp_adapter *adapter, struct zfcp_queue_req *q_req,
201 unsigned long sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200203 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200206 sbale = zfcp_qdio_sbale_curr(adapter, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
208
209 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200210 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return NULL;
212
213 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200214 sbale = zfcp_qdio_sbale_req(adapter, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
216
217 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200218 q_req->sbal_last++;
219 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200222 q_req->sbal_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200225 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* set storage-block type for new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200228 sbale = zfcp_qdio_sbale_curr(adapter, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 sbale->flags |= sbtype;
230
231 return sbale;
232}
233
Swen Schillig44cc76f2008-10-01 12:42:16 +0200234static struct qdio_buffer_element *
Swen Schillig42428f72009-08-18 15:43:18 +0200235zfcp_qdio_sbale_next(struct zfcp_adapter *adapter, struct zfcp_queue_req *q_req,
236 unsigned int sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Swen Schillig42428f72009-08-18 15:43:18 +0200238 if (q_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
239 return zfcp_qdio_sbal_chain(adapter, q_req, sbtype);
240 q_req->sbale_curr++;
241 return zfcp_qdio_sbale_curr(adapter, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Swen Schillig42428f72009-08-18 15:43:18 +0200244static void zfcp_qdio_undo_sbals(struct zfcp_adapter *adapter,
245 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Swen Schillig42428f72009-08-18 15:43:18 +0200247 struct qdio_buffer **sbal = adapter->req_q.sbal;
248 int first = q_req->sbal_first;
249 int last = q_req->sbal_last;
Swen Schillig00bab912008-06-10 18:20:57 +0200250 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
251 QDIO_MAX_BUFFERS_PER_Q + 1;
252 zfcp_qdio_zero_sbals(sbal, first, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Swen Schillig42428f72009-08-18 15:43:18 +0200255static int zfcp_qdio_fill_sbals(struct zfcp_adapter *adapter,
256 struct zfcp_queue_req *q_req,
Swen Schillig00bab912008-06-10 18:20:57 +0200257 unsigned int sbtype, void *start_addr,
258 unsigned int total_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200260 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 unsigned long remaining, length;
262 void *addr;
263
Swen Schillig00bab912008-06-10 18:20:57 +0200264 /* split segment up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 for (addr = start_addr, remaining = total_length; remaining > 0;
266 addr += length, remaining -= length) {
Swen Schillig42428f72009-08-18 15:43:18 +0200267 sbale = zfcp_qdio_sbale_next(adapter, q_req, sbtype);
Swen Schillig00bab912008-06-10 18:20:57 +0200268 if (!sbale) {
Swen Schillig42428f72009-08-18 15:43:18 +0200269 atomic_inc(&adapter->qdio_outb_full);
270 zfcp_qdio_undo_sbals(adapter, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EINVAL;
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Swen Schillig00bab912008-06-10 18:20:57 +0200274 /* new piece must not exceed next page boundary */
275 length = min(remaining,
276 (PAGE_SIZE - ((unsigned long)addr &
277 (PAGE_SIZE - 1))));
278 sbale->addr = addr;
279 sbale->length = length;
280 }
281 return 0;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/**
285 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
286 * @fsf_req: request to be processed
287 * @sbtype: SBALE flags
288 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig00bab912008-06-10 18:20:57 +0200290 * Returns: number of bytes, or error (negativ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Swen Schillig42428f72009-08-18 15:43:18 +0200292int zfcp_qdio_sbals_from_sg(struct zfcp_adapter *adapter,
293 struct zfcp_queue_req *q_req,
294 unsigned long sbtype, struct scatterlist *sg,
295 int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200297 struct qdio_buffer_element *sbale;
Swen Schillig00bab912008-06-10 18:20:57 +0200298 int retval, bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* figure out last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200301 zfcp_qdio_sbal_limit(adapter, q_req, max_sbals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Swen Schillig00bab912008-06-10 18:20:57 +0200303 /* set storage-block type for this request */
Swen Schillig42428f72009-08-18 15:43:18 +0200304 sbale = zfcp_qdio_sbale_req(adapter, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 sbale->flags |= sbtype;
306
Swen Schillig00bab912008-06-10 18:20:57 +0200307 for (; sg; sg = sg_next(sg)) {
Swen Schillig42428f72009-08-18 15:43:18 +0200308 retval = zfcp_qdio_fill_sbals(adapter, q_req, sbtype,
309 sg_virt(sg), sg->length);
Swen Schillig00bab912008-06-10 18:20:57 +0200310 if (retval < 0)
311 return retval;
312 bytes += sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Swen Schillig00bab912008-06-10 18:20:57 +0200314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* assume that no other SBALEs are to follow in the same SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200316 sbale = zfcp_qdio_sbale_curr(adapter, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
Swen Schillig00bab912008-06-10 18:20:57 +0200318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return bytes;
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/**
Swen Schillig00bab912008-06-10 18:20:57 +0200323 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
324 * @fsf_req: pointer to struct zfcp_fsf_req
325 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Swen Schillig42428f72009-08-18 15:43:18 +0200327int zfcp_qdio_send(struct zfcp_adapter *adapter, struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Swen Schillig00bab912008-06-10 18:20:57 +0200329 struct zfcp_qdio_queue *req_q = &adapter->req_q;
Swen Schillig42428f72009-08-18 15:43:18 +0200330 int first = q_req->sbal_first;
331 int count = q_req->sbal_number;
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100332 int retval;
333 unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
Swen Schillig00bab912008-06-10 18:20:57 +0200334
Martin Peschke94506fd2009-03-02 13:08:56 +0100335 zfcp_qdio_account(adapter);
336
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100337 retval = do_QDIO(adapter->ccw_device, qdio_flags, 0, first, count);
Swen Schillig00bab912008-06-10 18:20:57 +0200338 if (unlikely(retval)) {
339 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
340 return retval;
341 }
342
343 /* account for transferred buffers */
344 atomic_sub(count, &req_q->count);
345 req_q->first += count;
346 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Swen Schillig00bab912008-06-10 18:20:57 +0200350/**
Swen Schillig00bab912008-06-10 18:20:57 +0200351 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
352 * @adapter: pointer to struct zfcp_adapter
353 * Returns: -ENOMEM on memory allocation error or return value from
354 * qdio_allocate
355 */
356int zfcp_qdio_allocate(struct zfcp_adapter *adapter)
357{
358 struct qdio_initialize *init_data;
359
360 if (zfcp_qdio_buffers_enqueue(adapter->req_q.sbal) ||
361 zfcp_qdio_buffers_enqueue(adapter->resp_q.sbal))
362 return -ENOMEM;
363
364 init_data = &adapter->qdio_init_data;
365
366 init_data->cdev = adapter->ccw_device;
367 init_data->q_format = QDIO_ZFCP_QFMT;
Christof Schmittb225cf92008-12-19 16:57:00 +0100368 memcpy(init_data->adapter_name, dev_name(&adapter->ccw_device->dev), 8);
Swen Schillig00bab912008-06-10 18:20:57 +0200369 ASCEBC(init_data->adapter_name, 8);
370 init_data->qib_param_field_format = 0;
371 init_data->qib_param_field = NULL;
372 init_data->input_slib_elements = NULL;
373 init_data->output_slib_elements = NULL;
Swen Schillig00bab912008-06-10 18:20:57 +0200374 init_data->no_input_qs = 1;
375 init_data->no_output_qs = 1;
376 init_data->input_handler = zfcp_qdio_int_resp;
377 init_data->output_handler = zfcp_qdio_int_req;
378 init_data->int_parm = (unsigned long) adapter;
379 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
380 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
381 init_data->input_sbal_addr_array =
382 (void **) (adapter->resp_q.sbal);
383 init_data->output_sbal_addr_array =
384 (void **) (adapter->req_q.sbal);
385
386 return qdio_allocate(init_data);
387}
388
389/**
390 * zfcp_close_qdio - close qdio queues for an adapter
391 */
392void zfcp_qdio_close(struct zfcp_adapter *adapter)
393{
394 struct zfcp_qdio_queue *req_q;
395 int first, count;
396
Swen Schillig44cc76f2008-10-01 12:42:16 +0200397 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200398 return;
399
400 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
401 req_q = &adapter->req_q;
Christof Schmitt04062892008-10-01 12:42:20 +0200402 spin_lock_bh(&adapter->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200403 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
Christof Schmitt04062892008-10-01 12:42:20 +0200404 spin_unlock_bh(&adapter->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200405
Jan Glauber779e6e12008-07-17 17:16:48 +0200406 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200407
408 /* cleanup used outbound sbals */
409 count = atomic_read(&req_q->count);
410 if (count < QDIO_MAX_BUFFERS_PER_Q) {
411 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
412 count = QDIO_MAX_BUFFERS_PER_Q - count;
413 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
414 }
415 req_q->first = 0;
416 atomic_set(&req_q->count, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200417 adapter->resp_q.first = 0;
418 atomic_set(&adapter->resp_q.count, 0);
419}
420
421/**
422 * zfcp_qdio_open - prepare and initialize response queue
423 * @adapter: pointer to struct zfcp_adapter
424 * Returns: 0 on success, otherwise -EIO
425 */
426int zfcp_qdio_open(struct zfcp_adapter *adapter)
427{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200428 struct qdio_buffer_element *sbale;
Swen Schillig00bab912008-06-10 18:20:57 +0200429 int cc;
430
Swen Schillig44cc76f2008-10-01 12:42:16 +0200431 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200432 return -EIO;
433
Christof Schmittff3b24f2008-10-01 12:42:15 +0200434 if (qdio_establish(&adapter->qdio_init_data))
435 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200436
Christof Schmittff3b24f2008-10-01 12:42:15 +0200437 if (qdio_activate(adapter->ccw_device))
Swen Schillig00bab912008-06-10 18:20:57 +0200438 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200439
440 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
441 sbale = &(adapter->resp_q.sbal[cc]->element[0]);
442 sbale->length = 0;
443 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
444 sbale->addr = NULL;
445 }
446
447 if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200448 QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200449 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200450
451 /* set index of first avalable SBALS / number of available SBALS */
452 adapter->req_q.first = 0;
453 atomic_set(&adapter->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
Swen Schillig00bab912008-06-10 18:20:57 +0200454
455 return 0;
456
457failed_qdio:
Jan Glauber779e6e12008-07-17 17:16:48 +0200458 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200459failed_establish:
460 dev_err(&adapter->ccw_device->dev,
461 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200462 return -EIO;
463}