xen-scsifront.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /*
  2. * Xen SCSI frontend driver
  3. *
  4. * Copyright (c) 2008, FUJITSU Limited
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation; or, when distributed
  9. * separately from the Linux kernel or incorporated into other
  10. * software packages, subject to the following license:
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this source file (the "Software"), to deal in the Software without
  14. * restriction, including without limitation the rights to use, copy, modify,
  15. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  16. * and to permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  28. * IN THE SOFTWARE.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/device.h>
  33. #include <linux/wait.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/mutex.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/sched.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/pfn.h>
  40. #include <linux/slab.h>
  41. #include <linux/bitops.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi.h>
  45. #include <scsi/scsi_host.h>
  46. #include <xen/xen.h>
  47. #include <xen/xenbus.h>
  48. #include <xen/grant_table.h>
  49. #include <xen/events.h>
  50. #include <xen/page.h>
  51. #include <xen/interface/grant_table.h>
  52. #include <xen/interface/io/vscsiif.h>
  53. #include <xen/interface/io/protocols.h>
  54. #include <asm/xen/hypervisor.h>
  55. #define GRANT_INVALID_REF 0
  56. #define VSCSIFRONT_OP_ADD_LUN 1
  57. #define VSCSIFRONT_OP_DEL_LUN 2
  58. #define VSCSIFRONT_OP_READD_LUN 3
  59. /* Tuning point. */
  60. #define VSCSIIF_DEFAULT_CMD_PER_LUN 10
  61. #define VSCSIIF_MAX_TARGET 64
  62. #define VSCSIIF_MAX_LUN 255
  63. #define VSCSIIF_RING_SIZE __CONST_RING_SIZE(vscsiif, PAGE_SIZE)
  64. #define VSCSIIF_MAX_REQS VSCSIIF_RING_SIZE
  65. #define vscsiif_grants_sg(_sg) (PFN_UP((_sg) * \
  66. sizeof(struct scsiif_request_segment)))
  67. struct vscsifrnt_shadow {
  68. /* command between backend and frontend */
  69. unsigned char act;
  70. uint16_t rqid;
  71. unsigned int nr_grants; /* number of grants in gref[] */
  72. struct scsiif_request_segment *sg; /* scatter/gather elements */
  73. /* Do reset or abort function. */
  74. wait_queue_head_t wq_reset; /* reset work queue */
  75. int wait_reset; /* reset work queue condition */
  76. int32_t rslt_reset; /* reset response status: */
  77. /* SUCCESS or FAILED or: */
  78. #define RSLT_RESET_WAITING 0
  79. #define RSLT_RESET_ERR -1
  80. /* Requested struct scsi_cmnd is stored from kernel. */
  81. struct scsi_cmnd *sc;
  82. int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL];
  83. };
  84. struct vscsifrnt_info {
  85. struct xenbus_device *dev;
  86. struct Scsi_Host *host;
  87. int host_active;
  88. unsigned int evtchn;
  89. unsigned int irq;
  90. grant_ref_t ring_ref;
  91. struct vscsiif_front_ring ring;
  92. struct vscsiif_response ring_rsp;
  93. spinlock_t shadow_lock;
  94. DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS);
  95. struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS];
  96. /* Following items are protected by the host lock. */
  97. wait_queue_head_t wq_sync;
  98. wait_queue_head_t wq_pause;
  99. unsigned int wait_ring_available:1;
  100. unsigned int waiting_pause:1;
  101. unsigned int pause:1;
  102. unsigned callers;
  103. char dev_state_path[64];
  104. struct task_struct *curr;
  105. };
  106. static DEFINE_MUTEX(scsifront_mutex);
  107. static void scsifront_wake_up(struct vscsifrnt_info *info)
  108. {
  109. info->wait_ring_available = 0;
  110. wake_up(&info->wq_sync);
  111. }
  112. static int scsifront_get_rqid(struct vscsifrnt_info *info)
  113. {
  114. unsigned long flags;
  115. int free;
  116. spin_lock_irqsave(&info->shadow_lock, flags);
  117. free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
  118. __clear_bit(free, info->shadow_free_bitmap);
  119. spin_unlock_irqrestore(&info->shadow_lock, flags);
  120. return free;
  121. }
  122. static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
  123. {
  124. int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
  125. __set_bit(id, info->shadow_free_bitmap);
  126. info->shadow[id] = NULL;
  127. return empty || info->wait_ring_available;
  128. }
  129. static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
  130. {
  131. unsigned long flags;
  132. int kick;
  133. spin_lock_irqsave(&info->shadow_lock, flags);
  134. kick = _scsifront_put_rqid(info, id);
  135. spin_unlock_irqrestore(&info->shadow_lock, flags);
  136. if (kick)
  137. scsifront_wake_up(info);
  138. }
  139. static struct vscsiif_request *scsifront_pre_req(struct vscsifrnt_info *info)
  140. {
  141. struct vscsiif_front_ring *ring = &(info->ring);
  142. struct vscsiif_request *ring_req;
  143. uint32_t id;
  144. id = scsifront_get_rqid(info); /* use id in response */
  145. if (id >= VSCSIIF_MAX_REQS)
  146. return NULL;
  147. ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt);
  148. ring->req_prod_pvt++;
  149. ring_req->rqid = (uint16_t)id;
  150. return ring_req;
  151. }
  152. static void scsifront_do_request(struct vscsifrnt_info *info)
  153. {
  154. struct vscsiif_front_ring *ring = &(info->ring);
  155. int notify;
  156. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify);
  157. if (notify)
  158. notify_remote_via_irq(info->irq);
  159. }
  160. static void scsifront_gnttab_done(struct vscsifrnt_info *info, uint32_t id)
  161. {
  162. struct vscsifrnt_shadow *s = info->shadow[id];
  163. int i;
  164. if (s->sc->sc_data_direction == DMA_NONE)
  165. return;
  166. for (i = 0; i < s->nr_grants; i++) {
  167. if (unlikely(gnttab_query_foreign_access(s->gref[i]) != 0)) {
  168. shost_printk(KERN_ALERT, info->host, KBUILD_MODNAME
  169. "grant still in use by backend\n");
  170. BUG();
  171. }
  172. gnttab_end_foreign_access(s->gref[i], 0, 0UL);
  173. }
  174. kfree(s->sg);
  175. }
  176. static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
  177. struct vscsiif_response *ring_rsp)
  178. {
  179. struct scsi_cmnd *sc;
  180. uint32_t id;
  181. uint8_t sense_len;
  182. id = ring_rsp->rqid;
  183. sc = info->shadow[id]->sc;
  184. BUG_ON(sc == NULL);
  185. scsifront_gnttab_done(info, id);
  186. scsifront_put_rqid(info, id);
  187. sc->result = ring_rsp->rslt;
  188. scsi_set_resid(sc, ring_rsp->residual_len);
  189. sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
  190. ring_rsp->sense_len);
  191. if (sense_len)
  192. memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
  193. sc->scsi_done(sc);
  194. }
  195. static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
  196. struct vscsiif_response *ring_rsp)
  197. {
  198. uint16_t id = ring_rsp->rqid;
  199. unsigned long flags;
  200. struct vscsifrnt_shadow *shadow = info->shadow[id];
  201. int kick;
  202. spin_lock_irqsave(&info->shadow_lock, flags);
  203. shadow->wait_reset = 1;
  204. switch (shadow->rslt_reset) {
  205. case RSLT_RESET_WAITING:
  206. shadow->rslt_reset = ring_rsp->rslt;
  207. break;
  208. case RSLT_RESET_ERR:
  209. kick = _scsifront_put_rqid(info, id);
  210. spin_unlock_irqrestore(&info->shadow_lock, flags);
  211. kfree(shadow);
  212. if (kick)
  213. scsifront_wake_up(info);
  214. return;
  215. default:
  216. shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
  217. "bad reset state %d, possibly leaking %u\n",
  218. shadow->rslt_reset, id);
  219. break;
  220. }
  221. spin_unlock_irqrestore(&info->shadow_lock, flags);
  222. wake_up(&shadow->wq_reset);
  223. }
  224. static void scsifront_do_response(struct vscsifrnt_info *info,
  225. struct vscsiif_response *ring_rsp)
  226. {
  227. if (WARN(ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
  228. test_bit(ring_rsp->rqid, info->shadow_free_bitmap),
  229. "illegal rqid %u returned by backend!\n", ring_rsp->rqid))
  230. return;
  231. if (info->shadow[ring_rsp->rqid]->act == VSCSIIF_ACT_SCSI_CDB)
  232. scsifront_cdb_cmd_done(info, ring_rsp);
  233. else
  234. scsifront_sync_cmd_done(info, ring_rsp);
  235. }
  236. static int scsifront_ring_drain(struct vscsifrnt_info *info)
  237. {
  238. struct vscsiif_response *ring_rsp;
  239. RING_IDX i, rp;
  240. int more_to_do = 0;
  241. rp = info->ring.sring->rsp_prod;
  242. rmb(); /* ordering required respective to dom0 */
  243. for (i = info->ring.rsp_cons; i != rp; i++) {
  244. ring_rsp = RING_GET_RESPONSE(&info->ring, i);
  245. scsifront_do_response(info, ring_rsp);
  246. }
  247. info->ring.rsp_cons = i;
  248. if (i != info->ring.req_prod_pvt)
  249. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  250. else
  251. info->ring.sring->rsp_event = i + 1;
  252. return more_to_do;
  253. }
  254. static int scsifront_cmd_done(struct vscsifrnt_info *info)
  255. {
  256. int more_to_do;
  257. unsigned long flags;
  258. spin_lock_irqsave(info->host->host_lock, flags);
  259. more_to_do = scsifront_ring_drain(info);
  260. info->wait_ring_available = 0;
  261. spin_unlock_irqrestore(info->host->host_lock, flags);
  262. wake_up(&info->wq_sync);
  263. return more_to_do;
  264. }
  265. static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
  266. {
  267. struct vscsifrnt_info *info = dev_id;
  268. while (scsifront_cmd_done(info))
  269. /* Yield point for this unbounded loop. */
  270. cond_resched();
  271. return IRQ_HANDLED;
  272. }
  273. static void scsifront_finish_all(struct vscsifrnt_info *info)
  274. {
  275. unsigned i;
  276. struct vscsiif_response resp;
  277. scsifront_ring_drain(info);
  278. for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
  279. if (test_bit(i, info->shadow_free_bitmap))
  280. continue;
  281. resp.rqid = i;
  282. resp.sense_len = 0;
  283. resp.rslt = DID_RESET << 16;
  284. resp.residual_len = 0;
  285. scsifront_do_response(info, &resp);
  286. }
  287. }
  288. static int map_data_for_request(struct vscsifrnt_info *info,
  289. struct scsi_cmnd *sc,
  290. struct vscsiif_request *ring_req,
  291. struct vscsifrnt_shadow *shadow)
  292. {
  293. grant_ref_t gref_head;
  294. struct page *page;
  295. int err, ref, ref_cnt = 0;
  296. int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
  297. unsigned int i, off, len, bytes;
  298. unsigned int data_len = scsi_bufflen(sc);
  299. unsigned int data_grants = 0, seg_grants = 0;
  300. struct scatterlist *sg;
  301. struct scsiif_request_segment *seg;
  302. ring_req->nr_segments = 0;
  303. if (sc->sc_data_direction == DMA_NONE || !data_len)
  304. return 0;
  305. scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
  306. data_grants += PFN_UP(sg->offset + sg->length);
  307. if (data_grants > VSCSIIF_SG_TABLESIZE) {
  308. if (data_grants > info->host->sg_tablesize) {
  309. shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
  310. "Unable to map request_buffer for command!\n");
  311. return -E2BIG;
  312. }
  313. seg_grants = vscsiif_grants_sg(data_grants);
  314. shadow->sg = kcalloc(data_grants,
  315. sizeof(struct scsiif_request_segment), GFP_ATOMIC);
  316. if (!shadow->sg)
  317. return -ENOMEM;
  318. }
  319. seg = shadow->sg ? : ring_req->seg;
  320. err = gnttab_alloc_grant_references(seg_grants + data_grants,
  321. &gref_head);
  322. if (err) {
  323. kfree(shadow->sg);
  324. shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
  325. "gnttab_alloc_grant_references() error\n");
  326. return -ENOMEM;
  327. }
  328. if (seg_grants) {
  329. page = virt_to_page(seg);
  330. off = (unsigned long)seg & ~PAGE_MASK;
  331. len = sizeof(struct scsiif_request_segment) * data_grants;
  332. while (len > 0) {
  333. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  334. ref = gnttab_claim_grant_reference(&gref_head);
  335. BUG_ON(ref == -ENOSPC);
  336. gnttab_grant_foreign_access_ref(ref,
  337. info->dev->otherend_id,
  338. xen_page_to_gfn(page), 1);
  339. shadow->gref[ref_cnt] = ref;
  340. ring_req->seg[ref_cnt].gref = ref;
  341. ring_req->seg[ref_cnt].offset = (uint16_t)off;
  342. ring_req->seg[ref_cnt].length = (uint16_t)bytes;
  343. page++;
  344. len -= bytes;
  345. off = 0;
  346. ref_cnt++;
  347. }
  348. BUG_ON(seg_grants < ref_cnt);
  349. seg_grants = ref_cnt;
  350. }
  351. scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
  352. page = sg_page(sg);
  353. off = sg->offset;
  354. len = sg->length;
  355. while (len > 0 && data_len > 0) {
  356. /*
  357. * sg sends a scatterlist that is larger than
  358. * the data_len it wants transferred for certain
  359. * IO sizes.
  360. */
  361. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  362. bytes = min(bytes, data_len);
  363. ref = gnttab_claim_grant_reference(&gref_head);
  364. BUG_ON(ref == -ENOSPC);
  365. gnttab_grant_foreign_access_ref(ref,
  366. info->dev->otherend_id,
  367. xen_page_to_gfn(page),
  368. grant_ro);
  369. shadow->gref[ref_cnt] = ref;
  370. seg->gref = ref;
  371. seg->offset = (uint16_t)off;
  372. seg->length = (uint16_t)bytes;
  373. page++;
  374. seg++;
  375. len -= bytes;
  376. data_len -= bytes;
  377. off = 0;
  378. ref_cnt++;
  379. }
  380. }
  381. if (seg_grants)
  382. ring_req->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
  383. else
  384. ring_req->nr_segments = (uint8_t)ref_cnt;
  385. shadow->nr_grants = ref_cnt;
  386. return 0;
  387. }
  388. static struct vscsiif_request *scsifront_command2ring(
  389. struct vscsifrnt_info *info, struct scsi_cmnd *sc,
  390. struct vscsifrnt_shadow *shadow)
  391. {
  392. struct vscsiif_request *ring_req;
  393. memset(shadow, 0, sizeof(*shadow));
  394. ring_req = scsifront_pre_req(info);
  395. if (!ring_req)
  396. return NULL;
  397. info->shadow[ring_req->rqid] = shadow;
  398. shadow->rqid = ring_req->rqid;
  399. ring_req->id = sc->device->id;
  400. ring_req->lun = sc->device->lun;
  401. ring_req->channel = sc->device->channel;
  402. ring_req->cmd_len = sc->cmd_len;
  403. BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
  404. memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len);
  405. ring_req->sc_data_direction = (uint8_t)sc->sc_data_direction;
  406. ring_req->timeout_per_command = sc->request->timeout / HZ;
  407. return ring_req;
  408. }
  409. static int scsifront_enter(struct vscsifrnt_info *info)
  410. {
  411. if (info->pause)
  412. return 1;
  413. info->callers++;
  414. return 0;
  415. }
  416. static void scsifront_return(struct vscsifrnt_info *info)
  417. {
  418. info->callers--;
  419. if (info->callers)
  420. return;
  421. if (!info->waiting_pause)
  422. return;
  423. info->waiting_pause = 0;
  424. wake_up(&info->wq_pause);
  425. }
  426. static int scsifront_queuecommand(struct Scsi_Host *shost,
  427. struct scsi_cmnd *sc)
  428. {
  429. struct vscsifrnt_info *info = shost_priv(shost);
  430. struct vscsiif_request *ring_req;
  431. struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
  432. unsigned long flags;
  433. int err;
  434. uint16_t rqid;
  435. spin_lock_irqsave(shost->host_lock, flags);
  436. if (scsifront_enter(info)) {
  437. spin_unlock_irqrestore(shost->host_lock, flags);
  438. return SCSI_MLQUEUE_HOST_BUSY;
  439. }
  440. if (RING_FULL(&info->ring))
  441. goto busy;
  442. ring_req = scsifront_command2ring(info, sc, shadow);
  443. if (!ring_req)
  444. goto busy;
  445. sc->result = 0;
  446. rqid = ring_req->rqid;
  447. ring_req->act = VSCSIIF_ACT_SCSI_CDB;
  448. shadow->sc = sc;
  449. shadow->act = VSCSIIF_ACT_SCSI_CDB;
  450. err = map_data_for_request(info, sc, ring_req, shadow);
  451. if (err < 0) {
  452. pr_debug("%s: err %d\n", __func__, err);
  453. scsifront_put_rqid(info, rqid);
  454. scsifront_return(info);
  455. spin_unlock_irqrestore(shost->host_lock, flags);
  456. if (err == -ENOMEM)
  457. return SCSI_MLQUEUE_HOST_BUSY;
  458. sc->result = DID_ERROR << 16;
  459. sc->scsi_done(sc);
  460. return 0;
  461. }
  462. scsifront_do_request(info);
  463. scsifront_return(info);
  464. spin_unlock_irqrestore(shost->host_lock, flags);
  465. return 0;
  466. busy:
  467. scsifront_return(info);
  468. spin_unlock_irqrestore(shost->host_lock, flags);
  469. pr_debug("%s: busy\n", __func__);
  470. return SCSI_MLQUEUE_HOST_BUSY;
  471. }
  472. /*
  473. * Any exception handling (reset or abort) must be forwarded to the backend.
  474. * We have to wait until an answer is returned. This answer contains the
  475. * result to be returned to the requestor.
  476. */
  477. static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
  478. {
  479. struct Scsi_Host *host = sc->device->host;
  480. struct vscsifrnt_info *info = shost_priv(host);
  481. struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
  482. struct vscsiif_request *ring_req;
  483. int err = 0;
  484. shadow = kmalloc(sizeof(*shadow), GFP_NOIO);
  485. if (!shadow)
  486. return FAILED;
  487. spin_lock_irq(host->host_lock);
  488. for (;;) {
  489. if (!RING_FULL(&info->ring)) {
  490. ring_req = scsifront_command2ring(info, sc, shadow);
  491. if (ring_req)
  492. break;
  493. }
  494. if (err || info->pause) {
  495. spin_unlock_irq(host->host_lock);
  496. kfree(shadow);
  497. return FAILED;
  498. }
  499. info->wait_ring_available = 1;
  500. spin_unlock_irq(host->host_lock);
  501. err = wait_event_interruptible(info->wq_sync,
  502. !info->wait_ring_available);
  503. spin_lock_irq(host->host_lock);
  504. }
  505. if (scsifront_enter(info)) {
  506. spin_unlock_irq(host->host_lock);
  507. return FAILED;
  508. }
  509. ring_req->act = act;
  510. ring_req->ref_rqid = s->rqid;
  511. shadow->act = act;
  512. shadow->rslt_reset = RSLT_RESET_WAITING;
  513. init_waitqueue_head(&shadow->wq_reset);
  514. ring_req->nr_segments = 0;
  515. scsifront_do_request(info);
  516. spin_unlock_irq(host->host_lock);
  517. err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
  518. spin_lock_irq(host->host_lock);
  519. if (!err) {
  520. err = shadow->rslt_reset;
  521. scsifront_put_rqid(info, shadow->rqid);
  522. kfree(shadow);
  523. } else {
  524. spin_lock(&info->shadow_lock);
  525. shadow->rslt_reset = RSLT_RESET_ERR;
  526. spin_unlock(&info->shadow_lock);
  527. err = FAILED;
  528. }
  529. scsifront_return(info);
  530. spin_unlock_irq(host->host_lock);
  531. return err;
  532. }
  533. static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
  534. {
  535. pr_debug("%s\n", __func__);
  536. return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
  537. }
  538. static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
  539. {
  540. pr_debug("%s\n", __func__);
  541. return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
  542. }
  543. static int scsifront_sdev_configure(struct scsi_device *sdev)
  544. {
  545. struct vscsifrnt_info *info = shost_priv(sdev->host);
  546. if (info && current == info->curr)
  547. xenbus_printf(XBT_NIL, info->dev->nodename,
  548. info->dev_state_path, "%d", XenbusStateConnected);
  549. return 0;
  550. }
  551. static void scsifront_sdev_destroy(struct scsi_device *sdev)
  552. {
  553. struct vscsifrnt_info *info = shost_priv(sdev->host);
  554. if (info && current == info->curr)
  555. xenbus_printf(XBT_NIL, info->dev->nodename,
  556. info->dev_state_path, "%d", XenbusStateClosed);
  557. }
  558. static struct scsi_host_template scsifront_sht = {
  559. .module = THIS_MODULE,
  560. .name = "Xen SCSI frontend driver",
  561. .queuecommand = scsifront_queuecommand,
  562. .eh_abort_handler = scsifront_eh_abort_handler,
  563. .eh_device_reset_handler = scsifront_dev_reset_handler,
  564. .slave_configure = scsifront_sdev_configure,
  565. .slave_destroy = scsifront_sdev_destroy,
  566. .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
  567. .can_queue = VSCSIIF_MAX_REQS,
  568. .this_id = -1,
  569. .cmd_size = sizeof(struct vscsifrnt_shadow),
  570. .sg_tablesize = VSCSIIF_SG_TABLESIZE,
  571. .use_clustering = DISABLE_CLUSTERING,
  572. .proc_name = "scsifront",
  573. };
  574. static int scsifront_alloc_ring(struct vscsifrnt_info *info)
  575. {
  576. struct xenbus_device *dev = info->dev;
  577. struct vscsiif_sring *sring;
  578. grant_ref_t gref;
  579. int err = -ENOMEM;
  580. /***** Frontend to Backend ring start *****/
  581. sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL);
  582. if (!sring) {
  583. xenbus_dev_fatal(dev, err,
  584. "fail to allocate shared ring (Front to Back)");
  585. return err;
  586. }
  587. SHARED_RING_INIT(sring);
  588. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  589. err = xenbus_grant_ring(dev, sring, 1, &gref);
  590. if (err < 0) {
  591. free_page((unsigned long)sring);
  592. xenbus_dev_fatal(dev, err,
  593. "fail to grant shared ring (Front to Back)");
  594. return err;
  595. }
  596. info->ring_ref = gref;
  597. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  598. if (err) {
  599. xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
  600. goto free_gnttab;
  601. }
  602. err = bind_evtchn_to_irq(info->evtchn);
  603. if (err <= 0) {
  604. xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
  605. goto free_gnttab;
  606. }
  607. info->irq = err;
  608. err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
  609. IRQF_ONESHOT, "scsifront", info);
  610. if (err) {
  611. xenbus_dev_fatal(dev, err, "request_threaded_irq");
  612. goto free_irq;
  613. }
  614. return 0;
  615. /* free resource */
  616. free_irq:
  617. unbind_from_irqhandler(info->irq, info);
  618. free_gnttab:
  619. gnttab_end_foreign_access(info->ring_ref, 0,
  620. (unsigned long)info->ring.sring);
  621. return err;
  622. }
  623. static void scsifront_free_ring(struct vscsifrnt_info *info)
  624. {
  625. unbind_from_irqhandler(info->irq, info);
  626. gnttab_end_foreign_access(info->ring_ref, 0,
  627. (unsigned long)info->ring.sring);
  628. }
  629. static int scsifront_init_ring(struct vscsifrnt_info *info)
  630. {
  631. struct xenbus_device *dev = info->dev;
  632. struct xenbus_transaction xbt;
  633. int err;
  634. pr_debug("%s\n", __func__);
  635. err = scsifront_alloc_ring(info);
  636. if (err)
  637. return err;
  638. pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
  639. again:
  640. err = xenbus_transaction_start(&xbt);
  641. if (err)
  642. xenbus_dev_fatal(dev, err, "starting transaction");
  643. err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
  644. info->ring_ref);
  645. if (err) {
  646. xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
  647. goto fail;
  648. }
  649. err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  650. info->evtchn);
  651. if (err) {
  652. xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
  653. goto fail;
  654. }
  655. err = xenbus_transaction_end(xbt, 0);
  656. if (err) {
  657. if (err == -EAGAIN)
  658. goto again;
  659. xenbus_dev_fatal(dev, err, "completing transaction");
  660. goto free_sring;
  661. }
  662. return 0;
  663. fail:
  664. xenbus_transaction_end(xbt, 1);
  665. free_sring:
  666. scsifront_free_ring(info);
  667. return err;
  668. }
  669. static int scsifront_probe(struct xenbus_device *dev,
  670. const struct xenbus_device_id *id)
  671. {
  672. struct vscsifrnt_info *info;
  673. struct Scsi_Host *host;
  674. int err = -ENOMEM;
  675. char name[TASK_COMM_LEN];
  676. host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
  677. if (!host) {
  678. xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
  679. return err;
  680. }
  681. info = (struct vscsifrnt_info *)host->hostdata;
  682. dev_set_drvdata(&dev->dev, info);
  683. info->dev = dev;
  684. bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
  685. err = scsifront_init_ring(info);
  686. if (err) {
  687. scsi_host_put(host);
  688. return err;
  689. }
  690. init_waitqueue_head(&info->wq_sync);
  691. init_waitqueue_head(&info->wq_pause);
  692. spin_lock_init(&info->shadow_lock);
  693. snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
  694. host->max_id = VSCSIIF_MAX_TARGET;
  695. host->max_channel = 0;
  696. host->max_lun = VSCSIIF_MAX_LUN;
  697. host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
  698. host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
  699. err = scsi_add_host(host, &dev->dev);
  700. if (err) {
  701. dev_err(&dev->dev, "fail to add scsi host %d\n", err);
  702. goto free_sring;
  703. }
  704. info->host = host;
  705. info->host_active = 1;
  706. xenbus_switch_state(dev, XenbusStateInitialised);
  707. return 0;
  708. free_sring:
  709. scsifront_free_ring(info);
  710. scsi_host_put(host);
  711. return err;
  712. }
  713. static int scsifront_resume(struct xenbus_device *dev)
  714. {
  715. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  716. struct Scsi_Host *host = info->host;
  717. int err;
  718. spin_lock_irq(host->host_lock);
  719. /* Finish all still pending commands. */
  720. scsifront_finish_all(info);
  721. spin_unlock_irq(host->host_lock);
  722. /* Reconnect to dom0. */
  723. scsifront_free_ring(info);
  724. err = scsifront_init_ring(info);
  725. if (err) {
  726. dev_err(&dev->dev, "fail to resume %d\n", err);
  727. scsi_host_put(host);
  728. return err;
  729. }
  730. xenbus_switch_state(dev, XenbusStateInitialised);
  731. return 0;
  732. }
  733. static int scsifront_suspend(struct xenbus_device *dev)
  734. {
  735. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  736. struct Scsi_Host *host = info->host;
  737. int err = 0;
  738. /* No new commands for the backend. */
  739. spin_lock_irq(host->host_lock);
  740. info->pause = 1;
  741. while (info->callers && !err) {
  742. info->waiting_pause = 1;
  743. info->wait_ring_available = 0;
  744. spin_unlock_irq(host->host_lock);
  745. wake_up(&info->wq_sync);
  746. err = wait_event_interruptible(info->wq_pause,
  747. !info->waiting_pause);
  748. spin_lock_irq(host->host_lock);
  749. }
  750. spin_unlock_irq(host->host_lock);
  751. return err;
  752. }
  753. static int scsifront_remove(struct xenbus_device *dev)
  754. {
  755. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  756. pr_debug("%s: %s removed\n", __func__, dev->nodename);
  757. mutex_lock(&scsifront_mutex);
  758. if (info->host_active) {
  759. /* Scsi_host not yet removed */
  760. scsi_remove_host(info->host);
  761. info->host_active = 0;
  762. }
  763. mutex_unlock(&scsifront_mutex);
  764. scsifront_free_ring(info);
  765. scsi_host_put(info->host);
  766. return 0;
  767. }
  768. static void scsifront_disconnect(struct vscsifrnt_info *info)
  769. {
  770. struct xenbus_device *dev = info->dev;
  771. struct Scsi_Host *host = info->host;
  772. pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
  773. /*
  774. * When this function is executed, all devices of
  775. * Frontend have been deleted.
  776. * Therefore, it need not block I/O before remove_host.
  777. */
  778. mutex_lock(&scsifront_mutex);
  779. if (info->host_active) {
  780. scsi_remove_host(host);
  781. info->host_active = 0;
  782. }
  783. mutex_unlock(&scsifront_mutex);
  784. xenbus_frontend_closed(dev);
  785. }
  786. static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
  787. {
  788. struct xenbus_device *dev = info->dev;
  789. int i, err = 0;
  790. char str[64];
  791. char **dir;
  792. unsigned int dir_n = 0;
  793. unsigned int device_state;
  794. unsigned int hst, chn, tgt, lun;
  795. struct scsi_device *sdev;
  796. dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
  797. if (IS_ERR(dir))
  798. return;
  799. /* mark current task as the one allowed to modify device states */
  800. BUG_ON(info->curr);
  801. info->curr = current;
  802. for (i = 0; i < dir_n; i++) {
  803. /* read status */
  804. snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
  805. err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
  806. &device_state);
  807. if (XENBUS_EXIST_ERR(err))
  808. continue;
  809. /* virtual SCSI device */
  810. snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
  811. err = xenbus_scanf(XBT_NIL, dev->otherend, str,
  812. "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
  813. if (XENBUS_EXIST_ERR(err))
  814. continue;
  815. /*
  816. * Front device state path, used in slave_configure called
  817. * on successfull scsi_add_device, and in slave_destroy called
  818. * on remove of a device.
  819. */
  820. snprintf(info->dev_state_path, sizeof(info->dev_state_path),
  821. "vscsi-devs/%s/state", dir[i]);
  822. switch (op) {
  823. case VSCSIFRONT_OP_ADD_LUN:
  824. if (device_state != XenbusStateInitialised)
  825. break;
  826. if (scsi_add_device(info->host, chn, tgt, lun)) {
  827. dev_err(&dev->dev, "scsi_add_device\n");
  828. xenbus_printf(XBT_NIL, dev->nodename,
  829. info->dev_state_path,
  830. "%d", XenbusStateClosed);
  831. }
  832. break;
  833. case VSCSIFRONT_OP_DEL_LUN:
  834. if (device_state != XenbusStateClosing)
  835. break;
  836. sdev = scsi_device_lookup(info->host, chn, tgt, lun);
  837. if (sdev) {
  838. scsi_remove_device(sdev);
  839. scsi_device_put(sdev);
  840. }
  841. break;
  842. case VSCSIFRONT_OP_READD_LUN:
  843. if (device_state == XenbusStateConnected)
  844. xenbus_printf(XBT_NIL, dev->nodename,
  845. info->dev_state_path,
  846. "%d", XenbusStateConnected);
  847. break;
  848. default:
  849. break;
  850. }
  851. }
  852. info->curr = NULL;
  853. kfree(dir);
  854. }
  855. static void scsifront_read_backend_params(struct xenbus_device *dev,
  856. struct vscsifrnt_info *info)
  857. {
  858. unsigned int sg_grant, nr_segs;
  859. int ret;
  860. struct Scsi_Host *host = info->host;
  861. ret = xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg-grant", "%u",
  862. &sg_grant);
  863. if (ret != 1)
  864. sg_grant = 0;
  865. nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
  866. nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
  867. nr_segs = min_t(unsigned int, nr_segs,
  868. VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
  869. sizeof(struct scsiif_request_segment));
  870. if (!info->pause && sg_grant)
  871. dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
  872. else if (info->pause && nr_segs < host->sg_tablesize)
  873. dev_warn(&dev->dev,
  874. "SG entries decreased from %d to %u - device may not work properly anymore\n",
  875. host->sg_tablesize, nr_segs);
  876. host->sg_tablesize = nr_segs;
  877. host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
  878. }
  879. static void scsifront_backend_changed(struct xenbus_device *dev,
  880. enum xenbus_state backend_state)
  881. {
  882. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  883. pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
  884. switch (backend_state) {
  885. case XenbusStateUnknown:
  886. case XenbusStateInitialising:
  887. case XenbusStateInitWait:
  888. case XenbusStateInitialised:
  889. break;
  890. case XenbusStateConnected:
  891. scsifront_read_backend_params(dev, info);
  892. if (info->pause) {
  893. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
  894. xenbus_switch_state(dev, XenbusStateConnected);
  895. info->pause = 0;
  896. return;
  897. }
  898. if (xenbus_read_driver_state(dev->nodename) ==
  899. XenbusStateInitialised)
  900. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  901. if (dev->state != XenbusStateConnected)
  902. xenbus_switch_state(dev, XenbusStateConnected);
  903. break;
  904. case XenbusStateClosed:
  905. if (dev->state == XenbusStateClosed)
  906. break;
  907. /* Missed the backend's Closing state -- fallthrough */
  908. case XenbusStateClosing:
  909. scsifront_disconnect(info);
  910. break;
  911. case XenbusStateReconfiguring:
  912. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
  913. xenbus_switch_state(dev, XenbusStateReconfiguring);
  914. break;
  915. case XenbusStateReconfigured:
  916. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  917. xenbus_switch_state(dev, XenbusStateConnected);
  918. break;
  919. }
  920. }
  921. static const struct xenbus_device_id scsifront_ids[] = {
  922. { "vscsi" },
  923. { "" }
  924. };
  925. static struct xenbus_driver scsifront_driver = {
  926. .ids = scsifront_ids,
  927. .probe = scsifront_probe,
  928. .remove = scsifront_remove,
  929. .resume = scsifront_resume,
  930. .suspend = scsifront_suspend,
  931. .otherend_changed = scsifront_backend_changed,
  932. };
  933. static int __init scsifront_init(void)
  934. {
  935. if (!xen_domain())
  936. return -ENODEV;
  937. return xenbus_register_frontend(&scsifront_driver);
  938. }
  939. module_init(scsifront_init);
  940. static void __exit scsifront_exit(void)
  941. {
  942. xenbus_unregister_driver(&scsifront_driver);
  943. }
  944. module_exit(scsifront_exit);
  945. MODULE_DESCRIPTION("Xen SCSI frontend driver");
  946. MODULE_LICENSE("GPL");
  947. MODULE_ALIAS("xen:vscsi");
  948. MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");