[SCSI] zfcp: replace current ERP logging with a more convenient version
[linux-2.6] / drivers / s390 / scsi / zfcp_qdio.c
1 /*
2  * zfcp device driver
3  *
4  * Setup and helper functions to access QDIO.
5  *
6  * Copyright IBM Corporation 2002, 2008
7  */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include "zfcp_ext.h"
13
14 /* FIXME(tune): free space should be one max. SBAL chain plus what? */
15 #define ZFCP_QDIO_PCI_INTERVAL  (QDIO_MAX_BUFFERS_PER_Q \
16                                 - (FSF_MAX_SBALS_PER_REQ + 4))
17 #define QBUFF_PER_PAGE          (PAGE_SIZE / sizeof(struct qdio_buffer))
18
19 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
20 {
21         int pos;
22
23         for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
24                 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
25                 if (!sbal[pos])
26                         return -ENOMEM;
27         }
28         for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
29                 if (pos % QBUFF_PER_PAGE)
30                         sbal[pos] = sbal[pos - 1] + 1;
31         return 0;
32 }
33
34 static struct qdio_buffer_element *
35 zfcp_qdio_sbale(struct zfcp_qdio_queue *q, int sbal_idx, int sbale_idx)
36 {
37         return &q->sbal[sbal_idx]->element[sbale_idx];
38 }
39
40 /**
41  * zfcp_qdio_free - free memory used by request- and resposne queue
42  * @adapter: pointer to the zfcp_adapter structure
43  */
44 void zfcp_qdio_free(struct zfcp_adapter *adapter)
45 {
46         struct qdio_buffer **sbal_req, **sbal_resp;
47         int p;
48
49         if (adapter->ccw_device)
50                 qdio_free(adapter->ccw_device);
51
52         sbal_req = adapter->req_q.sbal;
53         sbal_resp = adapter->resp_q.sbal;
54
55         for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
56                 free_page((unsigned long) sbal_req[p]);
57                 free_page((unsigned long) sbal_resp[p]);
58         }
59 }
60
61 static void zfcp_qdio_handler_error(struct zfcp_adapter *adapter, char *id)
62 {
63         dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
64
65         zfcp_erp_adapter_reopen(adapter,
66                                 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
67                                 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
68 }
69
70 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
71 {
72         int i, sbal_idx;
73
74         for (i = first; i < first + cnt; i++) {
75                 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
76                 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
77         }
78 }
79
80 /* this needs to be called prior to updating the queue fill level */
81 static void zfcp_qdio_account(struct zfcp_adapter *adapter)
82 {
83         ktime_t now;
84         s64 span;
85         int free, used;
86
87         spin_lock(&adapter->qdio_stat_lock);
88         now = ktime_get();
89         span = ktime_us_delta(now, adapter->req_q_time);
90         free = max(0, atomic_read(&adapter->req_q.count));
91         used = QDIO_MAX_BUFFERS_PER_Q - free;
92         adapter->req_q_util += used * span;
93         adapter->req_q_time = now;
94         spin_unlock(&adapter->qdio_stat_lock);
95 }
96
97 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
98                               int queue_no, int first, int count,
99                               unsigned long parm)
100 {
101         struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
102         struct zfcp_qdio_queue *queue = &adapter->req_q;
103
104         if (unlikely(qdio_err)) {
105                 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
106                 zfcp_qdio_handler_error(adapter, "qdireq1");
107                 return;
108         }
109
110         /* cleanup all SBALs being program-owned now */
111         zfcp_qdio_zero_sbals(queue->sbal, first, count);
112
113         zfcp_qdio_account(adapter);
114         atomic_add(count, &queue->count);
115         wake_up(&adapter->request_wq);
116 }
117
118 static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
119                                   unsigned long req_id, int sbal_idx)
120 {
121         struct zfcp_fsf_req *fsf_req;
122         unsigned long flags;
123
124         spin_lock_irqsave(&adapter->req_list_lock, flags);
125         fsf_req = zfcp_reqlist_find(adapter, req_id);
126
127         if (!fsf_req)
128                 /*
129                  * Unknown request means that we have potentially memory
130                  * corruption and must stop the machine immediatly.
131                  */
132                 panic("error: unknown request id (%lx) on adapter %s.\n",
133                       req_id, dev_name(&adapter->ccw_device->dev));
134
135         zfcp_reqlist_remove(adapter, fsf_req);
136         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
137
138         fsf_req->sbal_response = sbal_idx;
139         fsf_req->qdio_inb_usage = atomic_read(&adapter->resp_q.count);
140         zfcp_fsf_req_complete(fsf_req);
141 }
142
143 static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
144 {
145         struct zfcp_qdio_queue *queue = &adapter->resp_q;
146         struct ccw_device *cdev = adapter->ccw_device;
147         u8 count, start = queue->first;
148         unsigned int retval;
149
150         count = atomic_read(&queue->count) + processed;
151
152         retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
153
154         if (unlikely(retval)) {
155                 atomic_set(&queue->count, count);
156                 /* FIXME: Recover this with an adapter reopen? */
157         } else {
158                 queue->first += count;
159                 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
160                 atomic_set(&queue->count, 0);
161         }
162 }
163
164 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
165                                int queue_no, int first, int count,
166                                unsigned long parm)
167 {
168         struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
169         struct zfcp_qdio_queue *queue = &adapter->resp_q;
170         struct qdio_buffer_element *sbale;
171         int sbal_idx, sbale_idx, sbal_no;
172
173         if (unlikely(qdio_err)) {
174                 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
175                 zfcp_qdio_handler_error(adapter, "qdires1");
176                 return;
177         }
178
179         /*
180          * go through all SBALs from input queue currently
181          * returned by QDIO layer
182          */
183         for (sbal_no = 0; sbal_no < count; sbal_no++) {
184                 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
185
186                 /* go through all SBALEs of SBAL */
187                 for (sbale_idx = 0; sbale_idx < QDIO_MAX_ELEMENTS_PER_BUFFER;
188                      sbale_idx++) {
189                         sbale = zfcp_qdio_sbale(queue, sbal_idx, sbale_idx);
190                         zfcp_qdio_reqid_check(adapter,
191                                               (unsigned long) sbale->addr,
192                                               sbal_idx);
193                         if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
194                                 break;
195                 };
196
197                 if (unlikely(!(sbale->flags & SBAL_FLAGS_LAST_ENTRY)))
198                         dev_warn(&adapter->ccw_device->dev,
199                                  "A QDIO protocol error occurred, "
200                                  "operations continue\n");
201         }
202
203         /*
204          * put range of SBALs back to response queue
205          * (including SBALs which have already been free before)
206          */
207         zfcp_qdio_resp_put_back(adapter, count);
208 }
209
210 /**
211  * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
212  * @fsf_req: pointer to struct fsf_req
213  * Returns: pointer to qdio_buffer_element (SBALE) structure
214  */
215 struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_fsf_req *req)
216 {
217         return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last, 0);
218 }
219
220 /**
221  * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
222  * @fsf_req: pointer to struct fsf_req
223  * Returns: pointer to qdio_buffer_element (SBALE) structure
224  */
225 struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_fsf_req *req)
226 {
227         return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last,
228                                req->sbale_curr);
229 }
230
231 static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
232 {
233         int count = atomic_read(&fsf_req->adapter->req_q.count);
234         count = min(count, max_sbals);
235         fsf_req->sbal_limit = (fsf_req->sbal_first + count - 1)
236                                         % QDIO_MAX_BUFFERS_PER_Q;
237 }
238
239 static struct qdio_buffer_element *
240 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
241 {
242         struct qdio_buffer_element *sbale;
243
244         /* set last entry flag in current SBALE of current SBAL */
245         sbale = zfcp_qdio_sbale_curr(fsf_req);
246         sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
247
248         /* don't exceed last allowed SBAL */
249         if (fsf_req->sbal_last == fsf_req->sbal_limit)
250                 return NULL;
251
252         /* set chaining flag in first SBALE of current SBAL */
253         sbale = zfcp_qdio_sbale_req(fsf_req);
254         sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
255
256         /* calculate index of next SBAL */
257         fsf_req->sbal_last++;
258         fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
259
260         /* keep this requests number of SBALs up-to-date */
261         fsf_req->sbal_number++;
262
263         /* start at first SBALE of new SBAL */
264         fsf_req->sbale_curr = 0;
265
266         /* set storage-block type for new SBAL */
267         sbale = zfcp_qdio_sbale_curr(fsf_req);
268         sbale->flags |= sbtype;
269
270         return sbale;
271 }
272
273 static struct qdio_buffer_element *
274 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
275 {
276         if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
277                 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
278         fsf_req->sbale_curr++;
279         return zfcp_qdio_sbale_curr(fsf_req);
280 }
281
282 static void zfcp_qdio_undo_sbals(struct zfcp_fsf_req *fsf_req)
283 {
284         struct qdio_buffer **sbal = fsf_req->adapter->req_q.sbal;
285         int first = fsf_req->sbal_first;
286         int last = fsf_req->sbal_last;
287         int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
288                 QDIO_MAX_BUFFERS_PER_Q + 1;
289         zfcp_qdio_zero_sbals(sbal, first, count);
290 }
291
292 static int zfcp_qdio_fill_sbals(struct zfcp_fsf_req *fsf_req,
293                                 unsigned int sbtype, void *start_addr,
294                                 unsigned int total_length)
295 {
296         struct qdio_buffer_element *sbale;
297         unsigned long remaining, length;
298         void *addr;
299
300         /* split segment up */
301         for (addr = start_addr, remaining = total_length; remaining > 0;
302              addr += length, remaining -= length) {
303                 sbale = zfcp_qdio_sbale_next(fsf_req, sbtype);
304                 if (!sbale) {
305                         atomic_inc(&fsf_req->adapter->qdio_outb_full);
306                         zfcp_qdio_undo_sbals(fsf_req);
307                         return -EINVAL;
308                 }
309
310                 /* new piece must not exceed next page boundary */
311                 length = min(remaining,
312                              (PAGE_SIZE - ((unsigned long)addr &
313                                            (PAGE_SIZE - 1))));
314                 sbale->addr = addr;
315                 sbale->length = length;
316         }
317         return 0;
318 }
319
320 /**
321  * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
322  * @fsf_req: request to be processed
323  * @sbtype: SBALE flags
324  * @sg: scatter-gather list
325  * @max_sbals: upper bound for number of SBALs to be used
326  * Returns: number of bytes, or error (negativ)
327  */
328 int zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
329                             struct scatterlist *sg, int max_sbals)
330 {
331         struct qdio_buffer_element *sbale;
332         int retval, bytes = 0;
333
334         /* figure out last allowed SBAL */
335         zfcp_qdio_sbal_limit(fsf_req, max_sbals);
336
337         /* set storage-block type for this request */
338         sbale = zfcp_qdio_sbale_req(fsf_req);
339         sbale->flags |= sbtype;
340
341         for (; sg; sg = sg_next(sg)) {
342                 retval = zfcp_qdio_fill_sbals(fsf_req, sbtype, sg_virt(sg),
343                                               sg->length);
344                 if (retval < 0)
345                         return retval;
346                 bytes += sg->length;
347         }
348
349         /* assume that no other SBALEs are to follow in the same SBAL */
350         sbale = zfcp_qdio_sbale_curr(fsf_req);
351         sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
352
353         return bytes;
354 }
355
356 /**
357  * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
358  * @fsf_req: pointer to struct zfcp_fsf_req
359  * Returns: 0 on success, error otherwise
360  */
361 int zfcp_qdio_send(struct zfcp_fsf_req *fsf_req)
362 {
363         struct zfcp_adapter *adapter = fsf_req->adapter;
364         struct zfcp_qdio_queue *req_q = &adapter->req_q;
365         int first = fsf_req->sbal_first;
366         int count = fsf_req->sbal_number;
367         int retval, pci, pci_batch;
368         struct qdio_buffer_element *sbale;
369
370         /* acknowledgements for transferred buffers */
371         pci_batch = adapter->req_q_pci_batch + count;
372         if (unlikely(pci_batch >= ZFCP_QDIO_PCI_INTERVAL)) {
373                 pci_batch %= ZFCP_QDIO_PCI_INTERVAL;
374                 pci = first + count - (pci_batch + 1);
375                 pci %= QDIO_MAX_BUFFERS_PER_Q;
376                 sbale = zfcp_qdio_sbale(req_q, pci, 0);
377                 sbale->flags |= SBAL_FLAGS0_PCI;
378         }
379
380         zfcp_qdio_account(adapter);
381
382         retval = do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0, first,
383                          count);
384         if (unlikely(retval)) {
385                 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
386                 return retval;
387         }
388
389         /* account for transferred buffers */
390         atomic_sub(count, &req_q->count);
391         req_q->first += count;
392         req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
393         adapter->req_q_pci_batch = pci_batch;
394         return 0;
395 }
396
397 /**
398  * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
399  * @adapter: pointer to struct zfcp_adapter
400  * Returns: -ENOMEM on memory allocation error or return value from
401  *          qdio_allocate
402  */
403 int zfcp_qdio_allocate(struct zfcp_adapter *adapter)
404 {
405         struct qdio_initialize *init_data;
406
407         if (zfcp_qdio_buffers_enqueue(adapter->req_q.sbal) ||
408                    zfcp_qdio_buffers_enqueue(adapter->resp_q.sbal))
409                 return -ENOMEM;
410
411         init_data = &adapter->qdio_init_data;
412
413         init_data->cdev = adapter->ccw_device;
414         init_data->q_format = QDIO_ZFCP_QFMT;
415         memcpy(init_data->adapter_name, dev_name(&adapter->ccw_device->dev), 8);
416         ASCEBC(init_data->adapter_name, 8);
417         init_data->qib_param_field_format = 0;
418         init_data->qib_param_field = NULL;
419         init_data->input_slib_elements = NULL;
420         init_data->output_slib_elements = NULL;
421         init_data->no_input_qs = 1;
422         init_data->no_output_qs = 1;
423         init_data->input_handler = zfcp_qdio_int_resp;
424         init_data->output_handler = zfcp_qdio_int_req;
425         init_data->int_parm = (unsigned long) adapter;
426         init_data->flags = QDIO_INBOUND_0COPY_SBALS |
427                         QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
428         init_data->input_sbal_addr_array =
429                         (void **) (adapter->resp_q.sbal);
430         init_data->output_sbal_addr_array =
431                         (void **) (adapter->req_q.sbal);
432
433         return qdio_allocate(init_data);
434 }
435
436 /**
437  * zfcp_close_qdio - close qdio queues for an adapter
438  */
439 void zfcp_qdio_close(struct zfcp_adapter *adapter)
440 {
441         struct zfcp_qdio_queue *req_q;
442         int first, count;
443
444         if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
445                 return;
446
447         /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
448         req_q = &adapter->req_q;
449         spin_lock_bh(&adapter->req_q_lock);
450         atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
451         spin_unlock_bh(&adapter->req_q_lock);
452
453         qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
454
455         /* cleanup used outbound sbals */
456         count = atomic_read(&req_q->count);
457         if (count < QDIO_MAX_BUFFERS_PER_Q) {
458                 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
459                 count = QDIO_MAX_BUFFERS_PER_Q - count;
460                 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
461         }
462         req_q->first = 0;
463         atomic_set(&req_q->count, 0);
464         adapter->req_q_pci_batch = 0;
465         adapter->resp_q.first = 0;
466         atomic_set(&adapter->resp_q.count, 0);
467 }
468
469 /**
470  * zfcp_qdio_open - prepare and initialize response queue
471  * @adapter: pointer to struct zfcp_adapter
472  * Returns: 0 on success, otherwise -EIO
473  */
474 int zfcp_qdio_open(struct zfcp_adapter *adapter)
475 {
476         struct qdio_buffer_element *sbale;
477         int cc;
478
479         if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
480                 return -EIO;
481
482         if (qdio_establish(&adapter->qdio_init_data))
483                 goto failed_establish;
484
485         if (qdio_activate(adapter->ccw_device))
486                 goto failed_qdio;
487
488         for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
489                 sbale = &(adapter->resp_q.sbal[cc]->element[0]);
490                 sbale->length = 0;
491                 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
492                 sbale->addr = NULL;
493         }
494
495         if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0,
496                      QDIO_MAX_BUFFERS_PER_Q))
497                 goto failed_qdio;
498
499         /* set index of first avalable SBALS / number of available SBALS */
500         adapter->req_q.first = 0;
501         atomic_set(&adapter->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
502         adapter->req_q_pci_batch = 0;
503
504         return 0;
505
506 failed_qdio:
507         qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
508 failed_establish:
509         dev_err(&adapter->ccw_device->dev,
510                 "Setting up the QDIO connection to the FCP adapter failed\n");
511         return -EIO;
512 }