xen-scsifront.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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. int err;
  547. if (info && current == info->curr) {
  548. err = xenbus_printf(XBT_NIL, info->dev->nodename,
  549. info->dev_state_path, "%d", XenbusStateConnected);
  550. if (err) {
  551. xenbus_dev_error(info->dev, err,
  552. "%s: writing dev_state_path", __func__);
  553. return err;
  554. }
  555. }
  556. return 0;
  557. }
  558. static void scsifront_sdev_destroy(struct scsi_device *sdev)
  559. {
  560. struct vscsifrnt_info *info = shost_priv(sdev->host);
  561. int err;
  562. if (info && current == info->curr) {
  563. err = xenbus_printf(XBT_NIL, info->dev->nodename,
  564. info->dev_state_path, "%d", XenbusStateClosed);
  565. if (err)
  566. xenbus_dev_error(info->dev, err,
  567. "%s: writing dev_state_path", __func__);
  568. }
  569. }
  570. static struct scsi_host_template scsifront_sht = {
  571. .module = THIS_MODULE,
  572. .name = "Xen SCSI frontend driver",
  573. .queuecommand = scsifront_queuecommand,
  574. .eh_abort_handler = scsifront_eh_abort_handler,
  575. .eh_device_reset_handler = scsifront_dev_reset_handler,
  576. .slave_configure = scsifront_sdev_configure,
  577. .slave_destroy = scsifront_sdev_destroy,
  578. .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
  579. .can_queue = VSCSIIF_MAX_REQS,
  580. .this_id = -1,
  581. .cmd_size = sizeof(struct vscsifrnt_shadow),
  582. .sg_tablesize = VSCSIIF_SG_TABLESIZE,
  583. .use_clustering = DISABLE_CLUSTERING,
  584. .proc_name = "scsifront",
  585. };
  586. static int scsifront_alloc_ring(struct vscsifrnt_info *info)
  587. {
  588. struct xenbus_device *dev = info->dev;
  589. struct vscsiif_sring *sring;
  590. grant_ref_t gref;
  591. int err = -ENOMEM;
  592. /***** Frontend to Backend ring start *****/
  593. sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL);
  594. if (!sring) {
  595. xenbus_dev_fatal(dev, err,
  596. "fail to allocate shared ring (Front to Back)");
  597. return err;
  598. }
  599. SHARED_RING_INIT(sring);
  600. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  601. err = xenbus_grant_ring(dev, sring, 1, &gref);
  602. if (err < 0) {
  603. free_page((unsigned long)sring);
  604. xenbus_dev_fatal(dev, err,
  605. "fail to grant shared ring (Front to Back)");
  606. return err;
  607. }
  608. info->ring_ref = gref;
  609. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  610. if (err) {
  611. xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
  612. goto free_gnttab;
  613. }
  614. err = bind_evtchn_to_irq(info->evtchn);
  615. if (err <= 0) {
  616. xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
  617. goto free_gnttab;
  618. }
  619. info->irq = err;
  620. err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
  621. IRQF_ONESHOT, "scsifront", info);
  622. if (err) {
  623. xenbus_dev_fatal(dev, err, "request_threaded_irq");
  624. goto free_irq;
  625. }
  626. return 0;
  627. /* free resource */
  628. free_irq:
  629. unbind_from_irqhandler(info->irq, info);
  630. free_gnttab:
  631. gnttab_end_foreign_access(info->ring_ref, 0,
  632. (unsigned long)info->ring.sring);
  633. return err;
  634. }
  635. static void scsifront_free_ring(struct vscsifrnt_info *info)
  636. {
  637. unbind_from_irqhandler(info->irq, info);
  638. gnttab_end_foreign_access(info->ring_ref, 0,
  639. (unsigned long)info->ring.sring);
  640. }
  641. static int scsifront_init_ring(struct vscsifrnt_info *info)
  642. {
  643. struct xenbus_device *dev = info->dev;
  644. struct xenbus_transaction xbt;
  645. int err;
  646. pr_debug("%s\n", __func__);
  647. err = scsifront_alloc_ring(info);
  648. if (err)
  649. return err;
  650. pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
  651. again:
  652. err = xenbus_transaction_start(&xbt);
  653. if (err)
  654. xenbus_dev_fatal(dev, err, "starting transaction");
  655. err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
  656. info->ring_ref);
  657. if (err) {
  658. xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
  659. goto fail;
  660. }
  661. err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  662. info->evtchn);
  663. if (err) {
  664. xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
  665. goto fail;
  666. }
  667. err = xenbus_transaction_end(xbt, 0);
  668. if (err) {
  669. if (err == -EAGAIN)
  670. goto again;
  671. xenbus_dev_fatal(dev, err, "completing transaction");
  672. goto free_sring;
  673. }
  674. return 0;
  675. fail:
  676. xenbus_transaction_end(xbt, 1);
  677. free_sring:
  678. scsifront_free_ring(info);
  679. return err;
  680. }
  681. static int scsifront_probe(struct xenbus_device *dev,
  682. const struct xenbus_device_id *id)
  683. {
  684. struct vscsifrnt_info *info;
  685. struct Scsi_Host *host;
  686. int err = -ENOMEM;
  687. char name[TASK_COMM_LEN];
  688. host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
  689. if (!host) {
  690. xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
  691. return err;
  692. }
  693. info = (struct vscsifrnt_info *)host->hostdata;
  694. dev_set_drvdata(&dev->dev, info);
  695. info->dev = dev;
  696. bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
  697. err = scsifront_init_ring(info);
  698. if (err) {
  699. scsi_host_put(host);
  700. return err;
  701. }
  702. init_waitqueue_head(&info->wq_sync);
  703. init_waitqueue_head(&info->wq_pause);
  704. spin_lock_init(&info->shadow_lock);
  705. snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
  706. host->max_id = VSCSIIF_MAX_TARGET;
  707. host->max_channel = 0;
  708. host->max_lun = VSCSIIF_MAX_LUN;
  709. host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
  710. host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
  711. err = scsi_add_host(host, &dev->dev);
  712. if (err) {
  713. dev_err(&dev->dev, "fail to add scsi host %d\n", err);
  714. goto free_sring;
  715. }
  716. info->host = host;
  717. info->host_active = 1;
  718. xenbus_switch_state(dev, XenbusStateInitialised);
  719. return 0;
  720. free_sring:
  721. scsifront_free_ring(info);
  722. scsi_host_put(host);
  723. return err;
  724. }
  725. static int scsifront_resume(struct xenbus_device *dev)
  726. {
  727. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  728. struct Scsi_Host *host = info->host;
  729. int err;
  730. spin_lock_irq(host->host_lock);
  731. /* Finish all still pending commands. */
  732. scsifront_finish_all(info);
  733. spin_unlock_irq(host->host_lock);
  734. /* Reconnect to dom0. */
  735. scsifront_free_ring(info);
  736. err = scsifront_init_ring(info);
  737. if (err) {
  738. dev_err(&dev->dev, "fail to resume %d\n", err);
  739. scsi_host_put(host);
  740. return err;
  741. }
  742. xenbus_switch_state(dev, XenbusStateInitialised);
  743. return 0;
  744. }
  745. static int scsifront_suspend(struct xenbus_device *dev)
  746. {
  747. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  748. struct Scsi_Host *host = info->host;
  749. int err = 0;
  750. /* No new commands for the backend. */
  751. spin_lock_irq(host->host_lock);
  752. info->pause = 1;
  753. while (info->callers && !err) {
  754. info->waiting_pause = 1;
  755. info->wait_ring_available = 0;
  756. spin_unlock_irq(host->host_lock);
  757. wake_up(&info->wq_sync);
  758. err = wait_event_interruptible(info->wq_pause,
  759. !info->waiting_pause);
  760. spin_lock_irq(host->host_lock);
  761. }
  762. spin_unlock_irq(host->host_lock);
  763. return err;
  764. }
  765. static int scsifront_remove(struct xenbus_device *dev)
  766. {
  767. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  768. pr_debug("%s: %s removed\n", __func__, dev->nodename);
  769. mutex_lock(&scsifront_mutex);
  770. if (info->host_active) {
  771. /* Scsi_host not yet removed */
  772. scsi_remove_host(info->host);
  773. info->host_active = 0;
  774. }
  775. mutex_unlock(&scsifront_mutex);
  776. scsifront_free_ring(info);
  777. scsi_host_put(info->host);
  778. return 0;
  779. }
  780. static void scsifront_disconnect(struct vscsifrnt_info *info)
  781. {
  782. struct xenbus_device *dev = info->dev;
  783. struct Scsi_Host *host = info->host;
  784. pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
  785. /*
  786. * When this function is executed, all devices of
  787. * Frontend have been deleted.
  788. * Therefore, it need not block I/O before remove_host.
  789. */
  790. mutex_lock(&scsifront_mutex);
  791. if (info->host_active) {
  792. scsi_remove_host(host);
  793. info->host_active = 0;
  794. }
  795. mutex_unlock(&scsifront_mutex);
  796. xenbus_frontend_closed(dev);
  797. }
  798. static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
  799. {
  800. struct xenbus_device *dev = info->dev;
  801. int i, err = 0;
  802. char str[64];
  803. char **dir;
  804. unsigned int dir_n = 0;
  805. unsigned int device_state;
  806. unsigned int hst, chn, tgt, lun;
  807. struct scsi_device *sdev;
  808. dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
  809. if (IS_ERR(dir))
  810. return;
  811. /* mark current task as the one allowed to modify device states */
  812. BUG_ON(info->curr);
  813. info->curr = current;
  814. for (i = 0; i < dir_n; i++) {
  815. /* read status */
  816. snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
  817. err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
  818. &device_state);
  819. if (XENBUS_EXIST_ERR(err))
  820. continue;
  821. /* virtual SCSI device */
  822. snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
  823. err = xenbus_scanf(XBT_NIL, dev->otherend, str,
  824. "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
  825. if (XENBUS_EXIST_ERR(err))
  826. continue;
  827. /*
  828. * Front device state path, used in slave_configure called
  829. * on successfull scsi_add_device, and in slave_destroy called
  830. * on remove of a device.
  831. */
  832. snprintf(info->dev_state_path, sizeof(info->dev_state_path),
  833. "vscsi-devs/%s/state", dir[i]);
  834. switch (op) {
  835. case VSCSIFRONT_OP_ADD_LUN:
  836. if (device_state != XenbusStateInitialised)
  837. break;
  838. if (scsi_add_device(info->host, chn, tgt, lun)) {
  839. dev_err(&dev->dev, "scsi_add_device\n");
  840. err = xenbus_printf(XBT_NIL, dev->nodename,
  841. info->dev_state_path,
  842. "%d", XenbusStateClosed);
  843. if (err)
  844. xenbus_dev_error(dev, err,
  845. "%s: writing dev_state_path", __func__);
  846. }
  847. break;
  848. case VSCSIFRONT_OP_DEL_LUN:
  849. if (device_state != XenbusStateClosing)
  850. break;
  851. sdev = scsi_device_lookup(info->host, chn, tgt, lun);
  852. if (sdev) {
  853. scsi_remove_device(sdev);
  854. scsi_device_put(sdev);
  855. }
  856. break;
  857. case VSCSIFRONT_OP_READD_LUN:
  858. if (device_state == XenbusStateConnected) {
  859. err = xenbus_printf(XBT_NIL, dev->nodename,
  860. info->dev_state_path,
  861. "%d", XenbusStateConnected);
  862. if (err)
  863. xenbus_dev_error(dev, err,
  864. "%s: writing dev_state_path", __func__);
  865. }
  866. break;
  867. default:
  868. break;
  869. }
  870. }
  871. info->curr = NULL;
  872. kfree(dir);
  873. }
  874. static void scsifront_read_backend_params(struct xenbus_device *dev,
  875. struct vscsifrnt_info *info)
  876. {
  877. unsigned int sg_grant, nr_segs;
  878. int ret;
  879. struct Scsi_Host *host = info->host;
  880. ret = xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg-grant", "%u",
  881. &sg_grant);
  882. if (ret != 1)
  883. sg_grant = 0;
  884. nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
  885. nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
  886. nr_segs = min_t(unsigned int, nr_segs,
  887. VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
  888. sizeof(struct scsiif_request_segment));
  889. if (!info->pause && sg_grant)
  890. dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
  891. else if (info->pause && nr_segs < host->sg_tablesize)
  892. dev_warn(&dev->dev,
  893. "SG entries decreased from %d to %u - device may not work properly anymore\n",
  894. host->sg_tablesize, nr_segs);
  895. host->sg_tablesize = nr_segs;
  896. host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
  897. }
  898. static void scsifront_backend_changed(struct xenbus_device *dev,
  899. enum xenbus_state backend_state)
  900. {
  901. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  902. pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
  903. switch (backend_state) {
  904. case XenbusStateUnknown:
  905. case XenbusStateInitialising:
  906. case XenbusStateInitWait:
  907. case XenbusStateInitialised:
  908. break;
  909. case XenbusStateConnected:
  910. scsifront_read_backend_params(dev, info);
  911. if (info->pause) {
  912. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
  913. xenbus_switch_state(dev, XenbusStateConnected);
  914. info->pause = 0;
  915. return;
  916. }
  917. if (xenbus_read_driver_state(dev->nodename) ==
  918. XenbusStateInitialised)
  919. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  920. if (dev->state != XenbusStateConnected)
  921. xenbus_switch_state(dev, XenbusStateConnected);
  922. break;
  923. case XenbusStateClosed:
  924. if (dev->state == XenbusStateClosed)
  925. break;
  926. /* Missed the backend's Closing state -- fallthrough */
  927. case XenbusStateClosing:
  928. scsifront_disconnect(info);
  929. break;
  930. case XenbusStateReconfiguring:
  931. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
  932. xenbus_switch_state(dev, XenbusStateReconfiguring);
  933. break;
  934. case XenbusStateReconfigured:
  935. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  936. xenbus_switch_state(dev, XenbusStateConnected);
  937. break;
  938. }
  939. }
  940. static const struct xenbus_device_id scsifront_ids[] = {
  941. { "vscsi" },
  942. { "" }
  943. };
  944. static struct xenbus_driver scsifront_driver = {
  945. .ids = scsifront_ids,
  946. .probe = scsifront_probe,
  947. .remove = scsifront_remove,
  948. .resume = scsifront_resume,
  949. .suspend = scsifront_suspend,
  950. .otherend_changed = scsifront_backend_changed,
  951. };
  952. static int __init scsifront_init(void)
  953. {
  954. if (!xen_domain())
  955. return -ENODEV;
  956. return xenbus_register_frontend(&scsifront_driver);
  957. }
  958. module_init(scsifront_init);
  959. static void __exit scsifront_exit(void)
  960. {
  961. xenbus_unregister_driver(&scsifront_driver);
  962. }
  963. module_exit(scsifront_exit);
  964. MODULE_DESCRIPTION("Xen SCSI frontend driver");
  965. MODULE_LICENSE("GPL");
  966. MODULE_ALIAS("xen:vscsi");
  967. MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");