blob: 6c5228b627fc540bac927019bf0fb829bb9d4bf8 [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
Swen Schillig564e1c82009-08-18 15:43:19 +020037static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id)
Swen Schillig00bab912008-06-10 18:20:57 +020038{
Swen Schillig564e1c82009-08-18 15:43:19 +020039 struct zfcp_adapter *adapter = qdio->adapter;
40
Christof Schmittff3b24f2008-10-01 12:42:15 +020041 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
Swen Schillig00bab912008-06-10 18:20:57 +020042
43 zfcp_erp_adapter_reopen(adapter,
44 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
45 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
46}
47
Christof Schmitt5d4e2262008-07-02 10:56:34 +020048static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
49{
50 int i, sbal_idx;
51
52 for (i = first; i < first + cnt; i++) {
53 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
54 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
55 }
56}
57
Martin Peschke94506fd2009-03-02 13:08:56 +010058/* this needs to be called prior to updating the queue fill level */
Heiko Carstens41e05a12009-08-18 15:43:32 +020059static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
Martin Peschke94506fd2009-03-02 13:08:56 +010060{
Heiko Carstens41e05a12009-08-18 15:43:32 +020061 unsigned long long now, span;
Martin Peschke94506fd2009-03-02 13:08:56 +010062 int free, used;
63
Swen Schillig564e1c82009-08-18 15:43:19 +020064 spin_lock(&qdio->stat_lock);
Heiko Carstens41e05a12009-08-18 15:43:32 +020065 now = get_clock_monotonic();
66 span = (now - qdio->req_q_time) >> 12;
67 free = atomic_read(&qdio->req_q.count);
Martin Peschke94506fd2009-03-02 13:08:56 +010068 used = QDIO_MAX_BUFFERS_PER_Q - free;
Swen Schillig564e1c82009-08-18 15:43:19 +020069 qdio->req_q_util += used * span;
70 qdio->req_q_time = now;
71 spin_unlock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +010072}
73
Jan Glauber779e6e12008-07-17 17:16:48 +020074static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
75 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020076 unsigned long parm)
77{
Swen Schillig564e1c82009-08-18 15:43:19 +020078 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
79 struct zfcp_qdio_queue *queue = &qdio->req_q;
Swen Schillig00bab912008-06-10 18:20:57 +020080
Jan Glauber779e6e12008-07-17 17:16:48 +020081 if (unlikely(qdio_err)) {
Swen Schillig57717102009-08-18 15:43:21 +020082 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
83 count);
Swen Schillig564e1c82009-08-18 15:43:19 +020084 zfcp_qdio_handler_error(qdio, "qdireq1");
Swen Schillig00bab912008-06-10 18:20:57 +020085 return;
86 }
87
88 /* cleanup all SBALs being program-owned now */
89 zfcp_qdio_zero_sbals(queue->sbal, first, count);
90
Swen Schillig564e1c82009-08-18 15:43:19 +020091 zfcp_qdio_account(qdio);
Swen Schillig00bab912008-06-10 18:20:57 +020092 atomic_add(count, &queue->count);
Swen Schillig564e1c82009-08-18 15:43:19 +020093 wake_up(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +020094}
95
Swen Schillig564e1c82009-08-18 15:43:19 +020096static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Swen Schillig564e1c82009-08-18 15:43:19 +020098 struct zfcp_qdio_queue *queue = &qdio->resp_q;
99 struct ccw_device *cdev = qdio->adapter->ccw_device;
Swen Schillig00bab912008-06-10 18:20:57 +0200100 u8 count, start = queue->first;
101 unsigned int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Swen Schillig00bab912008-06-10 18:20:57 +0200103 count = atomic_read(&queue->count) + processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Jan Glauber779e6e12008-07-17 17:16:48 +0200105 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Swen Schillig00bab912008-06-10 18:20:57 +0200107 if (unlikely(retval)) {
108 atomic_set(&queue->count, count);
109 /* FIXME: Recover this with an adapter reopen? */
110 } else {
111 queue->first += count;
112 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
113 atomic_set(&queue->count, 0);
114 }
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Jan Glauber779e6e12008-07-17 17:16:48 +0200117static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
118 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +0200119 unsigned long parm)
120{
Swen Schillig564e1c82009-08-18 15:43:19 +0200121 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schilligbd63eaf2009-08-18 15:43:13 +0200122 int sbal_idx, sbal_no;
Swen Schillig00bab912008-06-10 18:20:57 +0200123
Jan Glauber779e6e12008-07-17 17:16:48 +0200124 if (unlikely(qdio_err)) {
Swen Schillig57717102009-08-18 15:43:21 +0200125 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
126 count);
Swen Schillig564e1c82009-08-18 15:43:19 +0200127 zfcp_qdio_handler_error(qdio, "qdires1");
Swen Schillig00bab912008-06-10 18:20:57 +0200128 return;
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /*
132 * go through all SBALs from input queue currently
133 * returned by QDIO layer
134 */
Swen Schillig00bab912008-06-10 18:20:57 +0200135 for (sbal_no = 0; sbal_no < count; sbal_no++) {
136 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* go through all SBALEs of SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200138 zfcp_fsf_reqid_check(qdio, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
141 /*
142 * put range of SBALs back to response queue
143 * (including SBALs which have already been free before)
144 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200145 zfcp_qdio_resp_put_back(qdio, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/**
Swen Schillig00bab912008-06-10 18:20:57 +0200149 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
Swen Schillig564e1c82009-08-18 15:43:19 +0200150 * @qdio: pointer to struct zfcp_qdio
Swen Schillig42428f72009-08-18 15:43:18 +0200151 * @q_rec: pointer to struct zfcp_queue_rec
Swen Schillig00bab912008-06-10 18:20:57 +0200152 * Returns: pointer to qdio_buffer_element (SBALE) structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200154struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200155 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Swen Schillig564e1c82009-08-18 15:43:19 +0200157 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/**
Swen Schillig00bab912008-06-10 18:20:57 +0200161 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
162 * @fsf_req: pointer to struct fsf_req
163 * Returns: pointer to qdio_buffer_element (SBALE) structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200165struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200166 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Swen Schillig564e1c82009-08-18 15:43:19 +0200168 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last,
Swen Schillig42428f72009-08-18 15:43:18 +0200169 q_req->sbale_curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Swen Schillig564e1c82009-08-18 15:43:19 +0200172static void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200173 struct zfcp_queue_req *q_req, int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Swen Schillig564e1c82009-08-18 15:43:19 +0200175 int count = atomic_read(&qdio->req_q.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 count = min(count, max_sbals);
Swen Schillig42428f72009-08-18 15:43:18 +0200177 q_req->sbal_limit = (q_req->sbal_first + count - 1)
Swen Schillig00bab912008-06-10 18:20:57 +0200178 % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Swen Schillig44cc76f2008-10-01 12:42:16 +0200181static struct qdio_buffer_element *
Swen Schillig564e1c82009-08-18 15:43:19 +0200182zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req,
Swen Schillig42428f72009-08-18 15:43:18 +0200183 unsigned long sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200185 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200188 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
190
191 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200192 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return NULL;
194
195 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200196 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
198
199 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200200 q_req->sbal_last++;
201 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200204 q_req->sbal_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200207 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200210 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sbale->flags |= sbtype;
212
213 return sbale;
214}
215
Swen Schillig44cc76f2008-10-01 12:42:16 +0200216static struct qdio_buffer_element *
Swen Schillig564e1c82009-08-18 15:43:19 +0200217zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req,
Swen Schillig42428f72009-08-18 15:43:18 +0200218 unsigned int sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Swen Schillig42428f72009-08-18 15:43:18 +0200220 if (q_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
Swen Schillig564e1c82009-08-18 15:43:19 +0200221 return zfcp_qdio_sbal_chain(qdio, q_req, sbtype);
Swen Schillig42428f72009-08-18 15:43:18 +0200222 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200223 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Swen Schillig564e1c82009-08-18 15:43:19 +0200226static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200227 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Swen Schillig564e1c82009-08-18 15:43:19 +0200229 struct qdio_buffer **sbal = qdio->req_q.sbal;
Swen Schillig42428f72009-08-18 15:43:18 +0200230 int first = q_req->sbal_first;
231 int last = q_req->sbal_last;
Swen Schillig00bab912008-06-10 18:20:57 +0200232 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
233 QDIO_MAX_BUFFERS_PER_Q + 1;
234 zfcp_qdio_zero_sbals(sbal, first, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Swen Schillig564e1c82009-08-18 15:43:19 +0200237static int zfcp_qdio_fill_sbals(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200238 struct zfcp_queue_req *q_req,
Swen Schillig00bab912008-06-10 18:20:57 +0200239 unsigned int sbtype, void *start_addr,
240 unsigned int total_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200242 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long remaining, length;
244 void *addr;
245
Swen Schillig00bab912008-06-10 18:20:57 +0200246 /* split segment up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 for (addr = start_addr, remaining = total_length; remaining > 0;
248 addr += length, remaining -= length) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200249 sbale = zfcp_qdio_sbale_next(qdio, q_req, sbtype);
Swen Schillig00bab912008-06-10 18:20:57 +0200250 if (!sbale) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200251 atomic_inc(&qdio->req_q_full);
252 zfcp_qdio_undo_sbals(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EINVAL;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Swen Schillig00bab912008-06-10 18:20:57 +0200256 /* new piece must not exceed next page boundary */
257 length = min(remaining,
258 (PAGE_SIZE - ((unsigned long)addr &
259 (PAGE_SIZE - 1))));
260 sbale->addr = addr;
261 sbale->length = length;
262 }
263 return 0;
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266/**
267 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
268 * @fsf_req: request to be processed
269 * @sbtype: SBALE flags
270 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig00bab912008-06-10 18:20:57 +0200272 * Returns: number of bytes, or error (negativ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200274int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200275 struct zfcp_queue_req *q_req,
276 unsigned long sbtype, struct scatterlist *sg,
277 int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200279 struct qdio_buffer_element *sbale;
Swen Schillig00bab912008-06-10 18:20:57 +0200280 int retval, bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* figure out last allowed SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200283 zfcp_qdio_sbal_limit(qdio, q_req, max_sbals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Swen Schillig00bab912008-06-10 18:20:57 +0200285 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200286 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 sbale->flags |= sbtype;
288
Swen Schillig00bab912008-06-10 18:20:57 +0200289 for (; sg; sg = sg_next(sg)) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200290 retval = zfcp_qdio_fill_sbals(qdio, q_req, sbtype,
Swen Schillig42428f72009-08-18 15:43:18 +0200291 sg_virt(sg), sg->length);
Swen Schillig00bab912008-06-10 18:20:57 +0200292 if (retval < 0)
293 return retval;
294 bytes += sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Swen Schillig00bab912008-06-10 18:20:57 +0200296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* assume that no other SBALEs are to follow in the same SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200298 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
Swen Schillig00bab912008-06-10 18:20:57 +0200300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return bytes;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/**
Swen Schillig00bab912008-06-10 18:20:57 +0200305 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200306 * @qdio: pointer to struct zfcp_qdio
307 * @q_req: pointer to struct zfcp_queue_req
Swen Schillig00bab912008-06-10 18:20:57 +0200308 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200310int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Swen Schillig564e1c82009-08-18 15:43:19 +0200312 struct zfcp_qdio_queue *req_q = &qdio->req_q;
Swen Schillig42428f72009-08-18 15:43:18 +0200313 int first = q_req->sbal_first;
314 int count = q_req->sbal_number;
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100315 int retval;
316 unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
Swen Schillig00bab912008-06-10 18:20:57 +0200317
Swen Schillig564e1c82009-08-18 15:43:19 +0200318 zfcp_qdio_account(qdio);
Martin Peschke94506fd2009-03-02 13:08:56 +0100319
Swen Schillig564e1c82009-08-18 15:43:19 +0200320 retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
321 count);
Swen Schillig00bab912008-06-10 18:20:57 +0200322 if (unlikely(retval)) {
323 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
324 return retval;
325 }
326
327 /* account for transferred buffers */
328 atomic_sub(count, &req_q->count);
329 req_q->first += count;
330 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Swen Schillig564e1c82009-08-18 15:43:19 +0200334
335static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
336 struct zfcp_qdio *qdio)
337{
338
339 id->cdev = qdio->adapter->ccw_device;
340 id->q_format = QDIO_ZFCP_QFMT;
341 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
342 ASCEBC(id->adapter_name, 8);
343 id->qib_param_field_format = 0;
344 id->qib_param_field = NULL;
345 id->input_slib_elements = NULL;
346 id->output_slib_elements = NULL;
347 id->no_input_qs = 1;
348 id->no_output_qs = 1;
349 id->input_handler = zfcp_qdio_int_resp;
350 id->output_handler = zfcp_qdio_int_req;
351 id->int_parm = (unsigned long) qdio;
352 id->flags = QDIO_INBOUND_0COPY_SBALS |
353 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
354 id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
355 id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);
356
357}
Swen Schillig00bab912008-06-10 18:20:57 +0200358/**
Swen Schillig00bab912008-06-10 18:20:57 +0200359 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
360 * @adapter: pointer to struct zfcp_adapter
361 * Returns: -ENOMEM on memory allocation error or return value from
362 * qdio_allocate
363 */
Swen Schilligd5a282a2009-08-18 15:43:22 +0200364static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200365{
Swen Schillig564e1c82009-08-18 15:43:19 +0200366 struct qdio_initialize init_data;
Swen Schillig00bab912008-06-10 18:20:57 +0200367
Swen Schillig564e1c82009-08-18 15:43:19 +0200368 if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
369 zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
Swen Schillig00bab912008-06-10 18:20:57 +0200370 return -ENOMEM;
371
Swen Schillig564e1c82009-08-18 15:43:19 +0200372 zfcp_qdio_setup_init_data(&init_data, qdio);
Swen Schillig00bab912008-06-10 18:20:57 +0200373
Swen Schillig564e1c82009-08-18 15:43:19 +0200374 return qdio_allocate(&init_data);
Swen Schillig00bab912008-06-10 18:20:57 +0200375}
376
377/**
378 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200379 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200380 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200381void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200382{
383 struct zfcp_qdio_queue *req_q;
384 int first, count;
385
Swen Schillig564e1c82009-08-18 15:43:19 +0200386 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200387 return;
388
389 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Swen Schillig564e1c82009-08-18 15:43:19 +0200390 req_q = &qdio->req_q;
391 spin_lock_bh(&qdio->req_q_lock);
392 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
393 spin_unlock_bh(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200394
Swen Schillig564e1c82009-08-18 15:43:19 +0200395 qdio_shutdown(qdio->adapter->ccw_device,
396 QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200397
398 /* cleanup used outbound sbals */
399 count = atomic_read(&req_q->count);
400 if (count < QDIO_MAX_BUFFERS_PER_Q) {
401 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
402 count = QDIO_MAX_BUFFERS_PER_Q - count;
403 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
404 }
405 req_q->first = 0;
406 atomic_set(&req_q->count, 0);
Swen Schillig564e1c82009-08-18 15:43:19 +0200407 qdio->resp_q.first = 0;
408 atomic_set(&qdio->resp_q.count, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200409}
410
411/**
412 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200413 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200414 * Returns: 0 on success, otherwise -EIO
415 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200416int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200417{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200418 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200419 struct qdio_initialize init_data;
420 struct ccw_device *cdev = qdio->adapter->ccw_device;
Swen Schillig00bab912008-06-10 18:20:57 +0200421 int cc;
422
Swen Schillig564e1c82009-08-18 15:43:19 +0200423 if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200424 return -EIO;
425
Swen Schillig564e1c82009-08-18 15:43:19 +0200426 zfcp_qdio_setup_init_data(&init_data, qdio);
427
428 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200429 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200430
Swen Schillig564e1c82009-08-18 15:43:19 +0200431 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200432 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200433
434 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200435 sbale = &(qdio->resp_q.sbal[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200436 sbale->length = 0;
437 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
438 sbale->addr = NULL;
439 }
440
Swen Schillig564e1c82009-08-18 15:43:19 +0200441 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200442 QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200443 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200444
445 /* set index of first avalable SBALS / number of available SBALS */
Swen Schillig564e1c82009-08-18 15:43:19 +0200446 qdio->req_q.first = 0;
447 atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
Swen Schillig00bab912008-06-10 18:20:57 +0200448
449 return 0;
450
451failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200452 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200453failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200454 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200455 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200456 return -EIO;
457}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200458
459void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
460{
461 struct qdio_buffer **sbal_req, **sbal_resp;
462 int p;
463
464 if (!qdio)
465 return;
466
467 if (qdio->adapter->ccw_device)
468 qdio_free(qdio->adapter->ccw_device);
469
470 sbal_req = qdio->req_q.sbal;
471 sbal_resp = qdio->resp_q.sbal;
472
473 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
474 free_page((unsigned long) sbal_req[p]);
475 free_page((unsigned long) sbal_resp[p]);
476 }
477
478 kfree(qdio);
479}
480
481int zfcp_qdio_setup(struct zfcp_adapter *adapter)
482{
483 struct zfcp_qdio *qdio;
484
485 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
486 if (!qdio)
487 return -ENOMEM;
488
489 qdio->adapter = adapter;
490
491 if (zfcp_qdio_allocate(qdio)) {
492 zfcp_qdio_destroy(qdio);
493 return -ENOMEM;
494 }
495
496 spin_lock_init(&qdio->req_q_lock);
497 spin_lock_init(&qdio->stat_lock);
498
499 adapter->qdio = qdio;
500 return 0;
501}
502