aoecmd.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoecmd.c
  4. * Filesystem request handling methods
  5. */
  6. #include <linux/ata.h>
  7. #include <linux/slab.h>
  8. #include <linux/hdreg.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/genhd.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/kthread.h>
  16. #include <net/net_namespace.h>
  17. #include <asm/unaligned.h>
  18. #include <linux/uio.h>
  19. #include "aoe.h"
  20. #define MAXIOC (8192) /* default meant to avoid most soft lockups */
  21. static void ktcomplete(struct frame *, struct sk_buff *);
  22. static struct buf *nextbuf(struct aoedev *);
  23. static int aoe_deadsecs = 60 * 3;
  24. module_param(aoe_deadsecs, int, 0644);
  25. MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
  26. static int aoe_maxout = 16;
  27. module_param(aoe_maxout, int, 0644);
  28. MODULE_PARM_DESC(aoe_maxout,
  29. "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
  30. static wait_queue_head_t ktiowq;
  31. static struct ktstate kts;
  32. /* io completion queue */
  33. static struct {
  34. struct list_head head;
  35. spinlock_t lock;
  36. } iocq;
  37. static struct sk_buff *
  38. new_skb(ulong len)
  39. {
  40. struct sk_buff *skb;
  41. skb = alloc_skb(len, GFP_ATOMIC);
  42. if (skb) {
  43. skb_reset_mac_header(skb);
  44. skb_reset_network_header(skb);
  45. skb->protocol = __constant_htons(ETH_P_AOE);
  46. skb_checksum_none_assert(skb);
  47. }
  48. return skb;
  49. }
  50. static struct frame *
  51. getframe(struct aoedev *d, u32 tag)
  52. {
  53. struct frame *f;
  54. struct list_head *head, *pos, *nx;
  55. u32 n;
  56. n = tag % NFACTIVE;
  57. head = &d->factive[n];
  58. list_for_each_safe(pos, nx, head) {
  59. f = list_entry(pos, struct frame, head);
  60. if (f->tag == tag) {
  61. list_del(pos);
  62. return f;
  63. }
  64. }
  65. return NULL;
  66. }
  67. /*
  68. * Leave the top bit clear so we have tagspace for userland.
  69. * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
  70. * This driver reserves tag -1 to mean "unused frame."
  71. */
  72. static int
  73. newtag(struct aoedev *d)
  74. {
  75. register ulong n;
  76. n = jiffies & 0xffff;
  77. return n |= (++d->lasttag & 0x7fff) << 16;
  78. }
  79. static u32
  80. aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
  81. {
  82. u32 host_tag = newtag(d);
  83. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  84. memcpy(h->dst, t->addr, sizeof h->dst);
  85. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  86. h->verfl = AOE_HVER;
  87. h->major = cpu_to_be16(d->aoemajor);
  88. h->minor = d->aoeminor;
  89. h->cmd = AOECMD_ATA;
  90. h->tag = cpu_to_be32(host_tag);
  91. return host_tag;
  92. }
  93. static inline void
  94. put_lba(struct aoe_atahdr *ah, sector_t lba)
  95. {
  96. ah->lba0 = lba;
  97. ah->lba1 = lba >>= 8;
  98. ah->lba2 = lba >>= 8;
  99. ah->lba3 = lba >>= 8;
  100. ah->lba4 = lba >>= 8;
  101. ah->lba5 = lba >>= 8;
  102. }
  103. static struct aoeif *
  104. ifrotate(struct aoetgt *t)
  105. {
  106. struct aoeif *ifp;
  107. ifp = t->ifp;
  108. ifp++;
  109. if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
  110. ifp = t->ifs;
  111. if (ifp->nd == NULL)
  112. return NULL;
  113. return t->ifp = ifp;
  114. }
  115. static void
  116. skb_pool_put(struct aoedev *d, struct sk_buff *skb)
  117. {
  118. __skb_queue_tail(&d->skbpool, skb);
  119. }
  120. static struct sk_buff *
  121. skb_pool_get(struct aoedev *d)
  122. {
  123. struct sk_buff *skb = skb_peek(&d->skbpool);
  124. if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
  125. __skb_unlink(skb, &d->skbpool);
  126. return skb;
  127. }
  128. if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
  129. (skb = new_skb(ETH_ZLEN)))
  130. return skb;
  131. return NULL;
  132. }
  133. void
  134. aoe_freetframe(struct frame *f)
  135. {
  136. struct aoetgt *t;
  137. t = f->t;
  138. f->buf = NULL;
  139. f->bv = NULL;
  140. f->r_skb = NULL;
  141. list_add(&f->head, &t->ffree);
  142. }
  143. static struct frame *
  144. newtframe(struct aoedev *d, struct aoetgt *t)
  145. {
  146. struct frame *f;
  147. struct sk_buff *skb;
  148. struct list_head *pos;
  149. if (list_empty(&t->ffree)) {
  150. if (t->falloc >= NSKBPOOLMAX*2)
  151. return NULL;
  152. f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
  153. if (f == NULL)
  154. return NULL;
  155. t->falloc++;
  156. f->t = t;
  157. } else {
  158. pos = t->ffree.next;
  159. list_del(pos);
  160. f = list_entry(pos, struct frame, head);
  161. }
  162. skb = f->skb;
  163. if (skb == NULL) {
  164. f->skb = skb = new_skb(ETH_ZLEN);
  165. if (!skb) {
  166. bail: aoe_freetframe(f);
  167. return NULL;
  168. }
  169. }
  170. if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
  171. skb = skb_pool_get(d);
  172. if (skb == NULL)
  173. goto bail;
  174. skb_pool_put(d, f->skb);
  175. f->skb = skb;
  176. }
  177. skb->truesize -= skb->data_len;
  178. skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  179. skb_trim(skb, 0);
  180. return f;
  181. }
  182. static struct frame *
  183. newframe(struct aoedev *d)
  184. {
  185. struct frame *f;
  186. struct aoetgt *t, **tt;
  187. int totout = 0;
  188. if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
  189. printk(KERN_ERR "aoe: NULL TARGETS!\n");
  190. return NULL;
  191. }
  192. tt = d->tgt; /* last used target */
  193. for (;;) {
  194. tt++;
  195. if (tt >= &d->targets[NTARGETS] || !*tt)
  196. tt = d->targets;
  197. t = *tt;
  198. totout += t->nout;
  199. if (t->nout < t->maxout
  200. && t != d->htgt
  201. && t->ifp->nd) {
  202. f = newtframe(d, t);
  203. if (f) {
  204. ifrotate(t);
  205. d->tgt = tt;
  206. return f;
  207. }
  208. }
  209. if (tt == d->tgt) /* we've looped and found nada */
  210. break;
  211. }
  212. if (totout == 0) {
  213. d->kicked++;
  214. d->flags |= DEVFL_KICKME;
  215. }
  216. return NULL;
  217. }
  218. static void
  219. skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
  220. {
  221. int frag = 0;
  222. ulong fcnt;
  223. loop:
  224. fcnt = bv->bv_len - (off - bv->bv_offset);
  225. if (fcnt > cnt)
  226. fcnt = cnt;
  227. skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
  228. cnt -= fcnt;
  229. if (cnt <= 0)
  230. return;
  231. bv++;
  232. off = bv->bv_offset;
  233. goto loop;
  234. }
  235. static void
  236. fhash(struct frame *f)
  237. {
  238. struct aoedev *d = f->t->d;
  239. u32 n;
  240. n = f->tag % NFACTIVE;
  241. list_add_tail(&f->head, &d->factive[n]);
  242. }
  243. static int
  244. aoecmd_ata_rw(struct aoedev *d)
  245. {
  246. struct frame *f;
  247. struct aoe_hdr *h;
  248. struct aoe_atahdr *ah;
  249. struct buf *buf;
  250. struct aoetgt *t;
  251. struct sk_buff *skb;
  252. struct sk_buff_head queue;
  253. ulong bcnt, fbcnt;
  254. char writebit, extbit;
  255. writebit = 0x10;
  256. extbit = 0x4;
  257. buf = nextbuf(d);
  258. if (buf == NULL)
  259. return 0;
  260. f = newframe(d);
  261. if (f == NULL)
  262. return 0;
  263. t = *d->tgt;
  264. bcnt = d->maxbcnt;
  265. if (bcnt == 0)
  266. bcnt = DEFAULTBCNT;
  267. if (bcnt > buf->resid)
  268. bcnt = buf->resid;
  269. fbcnt = bcnt;
  270. f->bv = buf->bv;
  271. f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
  272. do {
  273. if (fbcnt < buf->bv_resid) {
  274. buf->bv_resid -= fbcnt;
  275. buf->resid -= fbcnt;
  276. break;
  277. }
  278. fbcnt -= buf->bv_resid;
  279. buf->resid -= buf->bv_resid;
  280. if (buf->resid == 0) {
  281. d->ip.buf = NULL;
  282. break;
  283. }
  284. buf->bv++;
  285. buf->bv_resid = buf->bv->bv_len;
  286. WARN_ON(buf->bv_resid == 0);
  287. } while (fbcnt);
  288. /* initialize the headers & frame */
  289. skb = f->skb;
  290. h = (struct aoe_hdr *) skb_mac_header(skb);
  291. ah = (struct aoe_atahdr *) (h+1);
  292. skb_put(skb, sizeof *h + sizeof *ah);
  293. memset(h, 0, skb->len);
  294. f->tag = aoehdr_atainit(d, t, h);
  295. fhash(f);
  296. t->nout++;
  297. f->waited = 0;
  298. f->buf = buf;
  299. f->bcnt = bcnt;
  300. f->lba = buf->sector;
  301. /* set up ata header */
  302. ah->scnt = bcnt >> 9;
  303. put_lba(ah, buf->sector);
  304. if (d->flags & DEVFL_EXT) {
  305. ah->aflags |= AOEAFL_EXT;
  306. } else {
  307. extbit = 0;
  308. ah->lba3 &= 0x0f;
  309. ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
  310. }
  311. if (bio_data_dir(buf->bio) == WRITE) {
  312. skb_fillup(skb, f->bv, f->bv_off, bcnt);
  313. ah->aflags |= AOEAFL_WRITE;
  314. skb->len += bcnt;
  315. skb->data_len = bcnt;
  316. skb->truesize += bcnt;
  317. t->wpkts++;
  318. } else {
  319. t->rpkts++;
  320. writebit = 0;
  321. }
  322. ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
  323. /* mark all tracking fields and load out */
  324. buf->nframesout += 1;
  325. buf->sector += bcnt >> 9;
  326. skb->dev = t->ifp->nd;
  327. skb = skb_clone(skb, GFP_ATOMIC);
  328. if (skb) {
  329. __skb_queue_head_init(&queue);
  330. __skb_queue_tail(&queue, skb);
  331. aoenet_xmit(&queue);
  332. }
  333. return 1;
  334. }
  335. /* some callers cannot sleep, and they can call this function,
  336. * transmitting the packets later, when interrupts are on
  337. */
  338. static void
  339. aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
  340. {
  341. struct aoe_hdr *h;
  342. struct aoe_cfghdr *ch;
  343. struct sk_buff *skb;
  344. struct net_device *ifp;
  345. rcu_read_lock();
  346. for_each_netdev_rcu(&init_net, ifp) {
  347. dev_hold(ifp);
  348. if (!is_aoe_netif(ifp))
  349. goto cont;
  350. skb = new_skb(sizeof *h + sizeof *ch);
  351. if (skb == NULL) {
  352. printk(KERN_INFO "aoe: skb alloc failure\n");
  353. goto cont;
  354. }
  355. skb_put(skb, sizeof *h + sizeof *ch);
  356. skb->dev = ifp;
  357. __skb_queue_tail(queue, skb);
  358. h = (struct aoe_hdr *) skb_mac_header(skb);
  359. memset(h, 0, sizeof *h + sizeof *ch);
  360. memset(h->dst, 0xff, sizeof h->dst);
  361. memcpy(h->src, ifp->dev_addr, sizeof h->src);
  362. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  363. h->verfl = AOE_HVER;
  364. h->major = cpu_to_be16(aoemajor);
  365. h->minor = aoeminor;
  366. h->cmd = AOECMD_CFG;
  367. cont:
  368. dev_put(ifp);
  369. }
  370. rcu_read_unlock();
  371. }
  372. static void
  373. resend(struct aoedev *d, struct frame *f)
  374. {
  375. struct sk_buff *skb;
  376. struct sk_buff_head queue;
  377. struct aoe_hdr *h;
  378. struct aoe_atahdr *ah;
  379. struct aoetgt *t;
  380. char buf[128];
  381. u32 n;
  382. t = f->t;
  383. n = newtag(d);
  384. skb = f->skb;
  385. if (ifrotate(t) == NULL) {
  386. /* probably can't happen, but set it up to fail anyway */
  387. pr_info("aoe: resend: no interfaces to rotate to.\n");
  388. ktcomplete(f, NULL);
  389. return;
  390. }
  391. h = (struct aoe_hdr *) skb_mac_header(skb);
  392. ah = (struct aoe_atahdr *) (h+1);
  393. snprintf(buf, sizeof buf,
  394. "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
  395. "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
  396. h->src, h->dst, t->nout);
  397. aoechr_error(buf);
  398. f->tag = n;
  399. fhash(f);
  400. h->tag = cpu_to_be32(n);
  401. memcpy(h->dst, t->addr, sizeof h->dst);
  402. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  403. skb->dev = t->ifp->nd;
  404. skb = skb_clone(skb, GFP_ATOMIC);
  405. if (skb == NULL)
  406. return;
  407. __skb_queue_head_init(&queue);
  408. __skb_queue_tail(&queue, skb);
  409. aoenet_xmit(&queue);
  410. }
  411. static int
  412. tsince(u32 tag)
  413. {
  414. int n;
  415. n = jiffies & 0xffff;
  416. n -= tag & 0xffff;
  417. if (n < 0)
  418. n += 1<<16;
  419. return n;
  420. }
  421. static struct aoeif *
  422. getif(struct aoetgt *t, struct net_device *nd)
  423. {
  424. struct aoeif *p, *e;
  425. p = t->ifs;
  426. e = p + NAOEIFS;
  427. for (; p < e; p++)
  428. if (p->nd == nd)
  429. return p;
  430. return NULL;
  431. }
  432. static void
  433. ejectif(struct aoetgt *t, struct aoeif *ifp)
  434. {
  435. struct aoeif *e;
  436. struct net_device *nd;
  437. ulong n;
  438. nd = ifp->nd;
  439. e = t->ifs + NAOEIFS - 1;
  440. n = (e - ifp) * sizeof *ifp;
  441. memmove(ifp, ifp+1, n);
  442. e->nd = NULL;
  443. dev_put(nd);
  444. }
  445. static int
  446. sthtith(struct aoedev *d)
  447. {
  448. struct frame *f, *nf;
  449. struct list_head *nx, *pos, *head;
  450. struct sk_buff *skb;
  451. struct aoetgt *ht = d->htgt;
  452. int i;
  453. for (i = 0; i < NFACTIVE; i++) {
  454. head = &d->factive[i];
  455. list_for_each_safe(pos, nx, head) {
  456. f = list_entry(pos, struct frame, head);
  457. if (f->t != ht)
  458. continue;
  459. nf = newframe(d);
  460. if (!nf)
  461. return 0;
  462. /* remove frame from active list */
  463. list_del(pos);
  464. /* reassign all pertinent bits to new outbound frame */
  465. skb = nf->skb;
  466. nf->skb = f->skb;
  467. nf->buf = f->buf;
  468. nf->bcnt = f->bcnt;
  469. nf->lba = f->lba;
  470. nf->bv = f->bv;
  471. nf->bv_off = f->bv_off;
  472. nf->waited = 0;
  473. f->skb = skb;
  474. aoe_freetframe(f);
  475. ht->nout--;
  476. nf->t->nout++;
  477. resend(d, nf);
  478. }
  479. }
  480. /* We've cleaned up the outstanding so take away his
  481. * interfaces so he won't be used. We should remove him from
  482. * the target array here, but cleaning up a target is
  483. * involved. PUNT!
  484. */
  485. memset(ht->ifs, 0, sizeof ht->ifs);
  486. d->htgt = NULL;
  487. return 1;
  488. }
  489. static inline unsigned char
  490. ata_scnt(unsigned char *packet) {
  491. struct aoe_hdr *h;
  492. struct aoe_atahdr *ah;
  493. h = (struct aoe_hdr *) packet;
  494. ah = (struct aoe_atahdr *) (h+1);
  495. return ah->scnt;
  496. }
  497. static void
  498. rexmit_timer(ulong vp)
  499. {
  500. struct aoedev *d;
  501. struct aoetgt *t, **tt, **te;
  502. struct aoeif *ifp;
  503. struct frame *f;
  504. struct list_head *head, *pos, *nx;
  505. LIST_HEAD(flist);
  506. register long timeout;
  507. ulong flags, n;
  508. int i;
  509. d = (struct aoedev *) vp;
  510. /* timeout is always ~150% of the moving average */
  511. timeout = d->rttavg;
  512. timeout += timeout >> 1;
  513. spin_lock_irqsave(&d->lock, flags);
  514. if (d->flags & DEVFL_TKILL) {
  515. spin_unlock_irqrestore(&d->lock, flags);
  516. return;
  517. }
  518. /* collect all frames to rexmit into flist */
  519. for (i = 0; i < NFACTIVE; i++) {
  520. head = &d->factive[i];
  521. list_for_each_safe(pos, nx, head) {
  522. f = list_entry(pos, struct frame, head);
  523. if (tsince(f->tag) < timeout)
  524. break; /* end of expired frames */
  525. /* move to flist for later processing */
  526. list_move_tail(pos, &flist);
  527. }
  528. }
  529. /* window check */
  530. tt = d->targets;
  531. te = tt + d->ntargets;
  532. for (; tt < te && (t = *tt); tt++) {
  533. if (t->nout == t->maxout
  534. && t->maxout < t->nframes
  535. && (jiffies - t->lastwadj)/HZ > 10) {
  536. t->maxout++;
  537. t->lastwadj = jiffies;
  538. }
  539. }
  540. if (!list_empty(&flist)) { /* retransmissions necessary */
  541. n = d->rttavg <<= 1;
  542. if (n > MAXTIMER)
  543. d->rttavg = MAXTIMER;
  544. }
  545. /* process expired frames */
  546. while (!list_empty(&flist)) {
  547. pos = flist.next;
  548. f = list_entry(pos, struct frame, head);
  549. n = f->waited += timeout;
  550. n /= HZ;
  551. if (n > aoe_deadsecs) {
  552. /* Waited too long. Device failure.
  553. * Hang all frames on first hash bucket for downdev
  554. * to clean up.
  555. */
  556. list_splice(&flist, &d->factive[0]);
  557. aoedev_downdev(d);
  558. break;
  559. }
  560. list_del(pos);
  561. t = f->t;
  562. if (n > aoe_deadsecs/2)
  563. d->htgt = t; /* see if another target can help */
  564. if (t->nout == t->maxout) {
  565. if (t->maxout > 1)
  566. t->maxout--;
  567. t->lastwadj = jiffies;
  568. }
  569. ifp = getif(t, f->skb->dev);
  570. if (ifp && ++ifp->lost > (t->nframes << 1)
  571. && (ifp != t->ifs || t->ifs[1].nd)) {
  572. ejectif(t, ifp);
  573. ifp = NULL;
  574. }
  575. resend(d, f);
  576. }
  577. if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
  578. d->flags &= ~DEVFL_KICKME;
  579. d->blkq->request_fn(d->blkq);
  580. }
  581. d->timer.expires = jiffies + TIMERTICK;
  582. add_timer(&d->timer);
  583. spin_unlock_irqrestore(&d->lock, flags);
  584. }
  585. static unsigned long
  586. rqbiocnt(struct request *r)
  587. {
  588. struct bio *bio;
  589. unsigned long n = 0;
  590. __rq_for_each_bio(bio, r)
  591. n++;
  592. return n;
  593. }
  594. /* This can be removed if we are certain that no users of the block
  595. * layer will ever use zero-count pages in bios. Otherwise we have to
  596. * protect against the put_page sometimes done by the network layer.
  597. *
  598. * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
  599. * discussion.
  600. *
  601. * We cannot use get_page in the workaround, because it insists on a
  602. * positive page count as a precondition. So we use _count directly.
  603. */
  604. static void
  605. bio_pageinc(struct bio *bio)
  606. {
  607. struct bio_vec *bv;
  608. struct page *page;
  609. int i;
  610. bio_for_each_segment(bv, bio, i) {
  611. page = bv->bv_page;
  612. /* Non-zero page count for non-head members of
  613. * compound pages is no longer allowed by the kernel,
  614. * but this has never been seen here.
  615. */
  616. if (unlikely(PageCompound(page)))
  617. if (compound_trans_head(page) != page) {
  618. pr_crit("page tail used for block I/O\n");
  619. BUG();
  620. }
  621. atomic_inc(&page->_count);
  622. }
  623. }
  624. static void
  625. bio_pagedec(struct bio *bio)
  626. {
  627. struct bio_vec *bv;
  628. int i;
  629. bio_for_each_segment(bv, bio, i)
  630. atomic_dec(&bv->bv_page->_count);
  631. }
  632. static void
  633. bufinit(struct buf *buf, struct request *rq, struct bio *bio)
  634. {
  635. struct bio_vec *bv;
  636. memset(buf, 0, sizeof(*buf));
  637. buf->rq = rq;
  638. buf->bio = bio;
  639. buf->resid = bio->bi_size;
  640. buf->sector = bio->bi_sector;
  641. bio_pageinc(bio);
  642. buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
  643. buf->bv_resid = bv->bv_len;
  644. WARN_ON(buf->bv_resid == 0);
  645. }
  646. static struct buf *
  647. nextbuf(struct aoedev *d)
  648. {
  649. struct request *rq;
  650. struct request_queue *q;
  651. struct buf *buf;
  652. struct bio *bio;
  653. q = d->blkq;
  654. if (q == NULL)
  655. return NULL; /* initializing */
  656. if (d->ip.buf)
  657. return d->ip.buf;
  658. rq = d->ip.rq;
  659. if (rq == NULL) {
  660. rq = blk_peek_request(q);
  661. if (rq == NULL)
  662. return NULL;
  663. blk_start_request(rq);
  664. d->ip.rq = rq;
  665. d->ip.nxbio = rq->bio;
  666. rq->special = (void *) rqbiocnt(rq);
  667. }
  668. buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
  669. if (buf == NULL) {
  670. pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
  671. return NULL;
  672. }
  673. bio = d->ip.nxbio;
  674. bufinit(buf, rq, bio);
  675. bio = bio->bi_next;
  676. d->ip.nxbio = bio;
  677. if (bio == NULL)
  678. d->ip.rq = NULL;
  679. return d->ip.buf = buf;
  680. }
  681. /* enters with d->lock held */
  682. void
  683. aoecmd_work(struct aoedev *d)
  684. {
  685. if (d->htgt && !sthtith(d))
  686. return;
  687. while (aoecmd_ata_rw(d))
  688. ;
  689. }
  690. /* this function performs work that has been deferred until sleeping is OK
  691. */
  692. void
  693. aoecmd_sleepwork(struct work_struct *work)
  694. {
  695. struct aoedev *d = container_of(work, struct aoedev, work);
  696. struct block_device *bd;
  697. u64 ssize;
  698. if (d->flags & DEVFL_GDALLOC)
  699. aoeblk_gdalloc(d);
  700. if (d->flags & DEVFL_NEWSIZE) {
  701. ssize = get_capacity(d->gd);
  702. bd = bdget_disk(d->gd, 0);
  703. if (bd) {
  704. mutex_lock(&bd->bd_inode->i_mutex);
  705. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  706. mutex_unlock(&bd->bd_inode->i_mutex);
  707. bdput(bd);
  708. }
  709. spin_lock_irq(&d->lock);
  710. d->flags |= DEVFL_UP;
  711. d->flags &= ~DEVFL_NEWSIZE;
  712. spin_unlock_irq(&d->lock);
  713. }
  714. }
  715. static void
  716. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  717. {
  718. u64 ssize;
  719. u16 n;
  720. /* word 83: command set supported */
  721. n = get_unaligned_le16(&id[83 << 1]);
  722. /* word 86: command set/feature enabled */
  723. n |= get_unaligned_le16(&id[86 << 1]);
  724. if (n & (1<<10)) { /* bit 10: LBA 48 */
  725. d->flags |= DEVFL_EXT;
  726. /* word 100: number lba48 sectors */
  727. ssize = get_unaligned_le64(&id[100 << 1]);
  728. /* set as in ide-disk.c:init_idedisk_capacity */
  729. d->geo.cylinders = ssize;
  730. d->geo.cylinders /= (255 * 63);
  731. d->geo.heads = 255;
  732. d->geo.sectors = 63;
  733. } else {
  734. d->flags &= ~DEVFL_EXT;
  735. /* number lba28 sectors */
  736. ssize = get_unaligned_le32(&id[60 << 1]);
  737. /* NOTE: obsolete in ATA 6 */
  738. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  739. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  740. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  741. }
  742. if (d->ssize != ssize)
  743. printk(KERN_INFO
  744. "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
  745. t->addr,
  746. d->aoemajor, d->aoeminor,
  747. d->fw_ver, (long long)ssize);
  748. d->ssize = ssize;
  749. d->geo.start = 0;
  750. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  751. return;
  752. if (d->gd != NULL) {
  753. set_capacity(d->gd, ssize);
  754. d->flags |= DEVFL_NEWSIZE;
  755. } else
  756. d->flags |= DEVFL_GDALLOC;
  757. schedule_work(&d->work);
  758. }
  759. static void
  760. calc_rttavg(struct aoedev *d, int rtt)
  761. {
  762. register long n;
  763. n = rtt;
  764. if (n < 0) {
  765. n = -rtt;
  766. if (n < MINTIMER)
  767. n = MINTIMER;
  768. else if (n > MAXTIMER)
  769. n = MAXTIMER;
  770. d->mintimer += (n - d->mintimer) >> 1;
  771. } else if (n < d->mintimer)
  772. n = d->mintimer;
  773. else if (n > MAXTIMER)
  774. n = MAXTIMER;
  775. /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
  776. n -= d->rttavg;
  777. d->rttavg += n >> 2;
  778. }
  779. static struct aoetgt *
  780. gettgt(struct aoedev *d, char *addr)
  781. {
  782. struct aoetgt **t, **e;
  783. t = d->targets;
  784. e = t + NTARGETS;
  785. for (; t < e && *t; t++)
  786. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  787. return *t;
  788. return NULL;
  789. }
  790. static void
  791. bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
  792. {
  793. ulong fcnt;
  794. char *p;
  795. int soff = 0;
  796. loop:
  797. fcnt = bv->bv_len - (off - bv->bv_offset);
  798. if (fcnt > cnt)
  799. fcnt = cnt;
  800. p = page_address(bv->bv_page) + off;
  801. skb_copy_bits(skb, soff, p, fcnt);
  802. soff += fcnt;
  803. cnt -= fcnt;
  804. if (cnt <= 0)
  805. return;
  806. bv++;
  807. off = bv->bv_offset;
  808. goto loop;
  809. }
  810. void
  811. aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
  812. {
  813. struct bio *bio;
  814. int bok;
  815. struct request_queue *q;
  816. q = d->blkq;
  817. if (rq == d->ip.rq)
  818. d->ip.rq = NULL;
  819. do {
  820. bio = rq->bio;
  821. bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
  822. } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
  823. /* cf. http://lkml.org/lkml/2006/10/31/28 */
  824. if (!fastfail)
  825. __blk_run_queue(q);
  826. }
  827. static void
  828. aoe_end_buf(struct aoedev *d, struct buf *buf)
  829. {
  830. struct request *rq;
  831. unsigned long n;
  832. if (buf == d->ip.buf)
  833. d->ip.buf = NULL;
  834. rq = buf->rq;
  835. bio_pagedec(buf->bio);
  836. mempool_free(buf, d->bufpool);
  837. n = (unsigned long) rq->special;
  838. rq->special = (void *) --n;
  839. if (n == 0)
  840. aoe_end_request(d, rq, 0);
  841. }
  842. static void
  843. ktiocomplete(struct frame *f)
  844. {
  845. struct aoe_hdr *hin, *hout;
  846. struct aoe_atahdr *ahin, *ahout;
  847. struct buf *buf;
  848. struct sk_buff *skb;
  849. struct aoetgt *t;
  850. struct aoeif *ifp;
  851. struct aoedev *d;
  852. long n;
  853. if (f == NULL)
  854. return;
  855. t = f->t;
  856. d = t->d;
  857. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  858. ahout = (struct aoe_atahdr *) (hout+1);
  859. buf = f->buf;
  860. skb = f->r_skb;
  861. if (skb == NULL)
  862. goto noskb; /* just fail the buf. */
  863. hin = (struct aoe_hdr *) skb->data;
  864. skb_pull(skb, sizeof(*hin));
  865. ahin = (struct aoe_atahdr *) skb->data;
  866. skb_pull(skb, sizeof(*ahin));
  867. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  868. pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  869. ahout->cmdstat, ahin->cmdstat,
  870. d->aoemajor, d->aoeminor);
  871. noskb: if (buf)
  872. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  873. goto badrsp;
  874. }
  875. n = ahout->scnt << 9;
  876. switch (ahout->cmdstat) {
  877. case ATA_CMD_PIO_READ:
  878. case ATA_CMD_PIO_READ_EXT:
  879. if (skb->len < n) {
  880. pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
  881. skb->len, n);
  882. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  883. break;
  884. }
  885. bvcpy(f->bv, f->bv_off, skb, n);
  886. case ATA_CMD_PIO_WRITE:
  887. case ATA_CMD_PIO_WRITE_EXT:
  888. spin_lock_irq(&d->lock);
  889. ifp = getif(t, skb->dev);
  890. if (ifp)
  891. ifp->lost = 0;
  892. if (d->htgt == t) /* I'll help myself, thank you. */
  893. d->htgt = NULL;
  894. spin_unlock_irq(&d->lock);
  895. break;
  896. case ATA_CMD_ID_ATA:
  897. if (skb->len < 512) {
  898. pr_info("aoe: runt data size in ataid. skb->len=%d\n",
  899. skb->len);
  900. break;
  901. }
  902. if (skb_linearize(skb))
  903. break;
  904. spin_lock_irq(&d->lock);
  905. ataid_complete(d, t, skb->data);
  906. spin_unlock_irq(&d->lock);
  907. break;
  908. default:
  909. pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  910. ahout->cmdstat,
  911. be16_to_cpu(get_unaligned(&hin->major)),
  912. hin->minor);
  913. }
  914. badrsp:
  915. spin_lock_irq(&d->lock);
  916. aoe_freetframe(f);
  917. if (buf && --buf->nframesout == 0 && buf->resid == 0)
  918. aoe_end_buf(d, buf);
  919. aoecmd_work(d);
  920. spin_unlock_irq(&d->lock);
  921. aoedev_put(d);
  922. dev_kfree_skb(skb);
  923. }
  924. /* Enters with iocq.lock held.
  925. * Returns true iff responses needing processing remain.
  926. */
  927. static int
  928. ktio(void)
  929. {
  930. struct frame *f;
  931. struct list_head *pos;
  932. int i;
  933. for (i = 0; ; ++i) {
  934. if (i == MAXIOC)
  935. return 1;
  936. if (list_empty(&iocq.head))
  937. return 0;
  938. pos = iocq.head.next;
  939. list_del(pos);
  940. spin_unlock_irq(&iocq.lock);
  941. f = list_entry(pos, struct frame, head);
  942. ktiocomplete(f);
  943. spin_lock_irq(&iocq.lock);
  944. }
  945. }
  946. static int
  947. kthread(void *vp)
  948. {
  949. struct ktstate *k;
  950. DECLARE_WAITQUEUE(wait, current);
  951. int more;
  952. k = vp;
  953. current->flags |= PF_NOFREEZE;
  954. set_user_nice(current, -10);
  955. complete(&k->rendez); /* tell spawner we're running */
  956. do {
  957. spin_lock_irq(k->lock);
  958. more = k->fn();
  959. if (!more) {
  960. add_wait_queue(k->waitq, &wait);
  961. __set_current_state(TASK_INTERRUPTIBLE);
  962. }
  963. spin_unlock_irq(k->lock);
  964. if (!more) {
  965. schedule();
  966. remove_wait_queue(k->waitq, &wait);
  967. } else
  968. cond_resched();
  969. } while (!kthread_should_stop());
  970. complete(&k->rendez); /* tell spawner we're stopping */
  971. return 0;
  972. }
  973. void
  974. aoe_ktstop(struct ktstate *k)
  975. {
  976. kthread_stop(k->task);
  977. wait_for_completion(&k->rendez);
  978. }
  979. int
  980. aoe_ktstart(struct ktstate *k)
  981. {
  982. struct task_struct *task;
  983. init_completion(&k->rendez);
  984. task = kthread_run(kthread, k, k->name);
  985. if (task == NULL || IS_ERR(task))
  986. return -ENOMEM;
  987. k->task = task;
  988. wait_for_completion(&k->rendez); /* allow kthread to start */
  989. init_completion(&k->rendez); /* for waiting for exit later */
  990. return 0;
  991. }
  992. /* pass it off to kthreads for processing */
  993. static void
  994. ktcomplete(struct frame *f, struct sk_buff *skb)
  995. {
  996. ulong flags;
  997. f->r_skb = skb;
  998. spin_lock_irqsave(&iocq.lock, flags);
  999. list_add_tail(&f->head, &iocq.head);
  1000. spin_unlock_irqrestore(&iocq.lock, flags);
  1001. wake_up(&ktiowq);
  1002. }
  1003. struct sk_buff *
  1004. aoecmd_ata_rsp(struct sk_buff *skb)
  1005. {
  1006. struct aoedev *d;
  1007. struct aoe_hdr *h;
  1008. struct frame *f;
  1009. struct aoetgt *t;
  1010. u32 n;
  1011. ulong flags;
  1012. char ebuf[128];
  1013. u16 aoemajor;
  1014. h = (struct aoe_hdr *) skb->data;
  1015. aoemajor = be16_to_cpu(get_unaligned(&h->major));
  1016. d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
  1017. if (d == NULL) {
  1018. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  1019. "for unknown device %d.%d\n",
  1020. aoemajor, h->minor);
  1021. aoechr_error(ebuf);
  1022. return skb;
  1023. }
  1024. spin_lock_irqsave(&d->lock, flags);
  1025. n = be32_to_cpu(get_unaligned(&h->tag));
  1026. f = getframe(d, n);
  1027. if (f == NULL) {
  1028. calc_rttavg(d, -tsince(n));
  1029. spin_unlock_irqrestore(&d->lock, flags);
  1030. aoedev_put(d);
  1031. snprintf(ebuf, sizeof ebuf,
  1032. "%15s e%d.%d tag=%08x@%08lx\n",
  1033. "unexpected rsp",
  1034. get_unaligned_be16(&h->major),
  1035. h->minor,
  1036. get_unaligned_be32(&h->tag),
  1037. jiffies);
  1038. aoechr_error(ebuf);
  1039. return skb;
  1040. }
  1041. t = f->t;
  1042. calc_rttavg(d, tsince(f->tag));
  1043. t->nout--;
  1044. aoecmd_work(d);
  1045. spin_unlock_irqrestore(&d->lock, flags);
  1046. ktcomplete(f, skb);
  1047. /*
  1048. * Note here that we do not perform an aoedev_put, as we are
  1049. * leaving this reference for the ktio to release.
  1050. */
  1051. return NULL;
  1052. }
  1053. void
  1054. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  1055. {
  1056. struct sk_buff_head queue;
  1057. __skb_queue_head_init(&queue);
  1058. aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
  1059. aoenet_xmit(&queue);
  1060. }
  1061. struct sk_buff *
  1062. aoecmd_ata_id(struct aoedev *d)
  1063. {
  1064. struct aoe_hdr *h;
  1065. struct aoe_atahdr *ah;
  1066. struct frame *f;
  1067. struct sk_buff *skb;
  1068. struct aoetgt *t;
  1069. f = newframe(d);
  1070. if (f == NULL)
  1071. return NULL;
  1072. t = *d->tgt;
  1073. /* initialize the headers & frame */
  1074. skb = f->skb;
  1075. h = (struct aoe_hdr *) skb_mac_header(skb);
  1076. ah = (struct aoe_atahdr *) (h+1);
  1077. skb_put(skb, sizeof *h + sizeof *ah);
  1078. memset(h, 0, skb->len);
  1079. f->tag = aoehdr_atainit(d, t, h);
  1080. fhash(f);
  1081. t->nout++;
  1082. f->waited = 0;
  1083. /* set up ata header */
  1084. ah->scnt = 1;
  1085. ah->cmdstat = ATA_CMD_ID_ATA;
  1086. ah->lba3 = 0xa0;
  1087. skb->dev = t->ifp->nd;
  1088. d->rttavg = MAXTIMER;
  1089. d->timer.function = rexmit_timer;
  1090. return skb_clone(skb, GFP_ATOMIC);
  1091. }
  1092. static struct aoetgt *
  1093. addtgt(struct aoedev *d, char *addr, ulong nframes)
  1094. {
  1095. struct aoetgt *t, **tt, **te;
  1096. tt = d->targets;
  1097. te = tt + NTARGETS;
  1098. for (; tt < te && *tt; tt++)
  1099. ;
  1100. if (tt == te) {
  1101. printk(KERN_INFO
  1102. "aoe: device addtgt failure; too many targets\n");
  1103. return NULL;
  1104. }
  1105. t = kzalloc(sizeof(*t), GFP_ATOMIC);
  1106. if (!t) {
  1107. printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
  1108. return NULL;
  1109. }
  1110. d->ntargets++;
  1111. t->nframes = nframes;
  1112. t->d = d;
  1113. memcpy(t->addr, addr, sizeof t->addr);
  1114. t->ifp = t->ifs;
  1115. t->maxout = t->nframes;
  1116. INIT_LIST_HEAD(&t->ffree);
  1117. return *tt = t;
  1118. }
  1119. static void
  1120. setdbcnt(struct aoedev *d)
  1121. {
  1122. struct aoetgt **t, **e;
  1123. int bcnt = 0;
  1124. t = d->targets;
  1125. e = t + NTARGETS;
  1126. for (; t < e && *t; t++)
  1127. if (bcnt == 0 || bcnt > (*t)->minbcnt)
  1128. bcnt = (*t)->minbcnt;
  1129. if (bcnt != d->maxbcnt) {
  1130. d->maxbcnt = bcnt;
  1131. pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
  1132. d->aoemajor, d->aoeminor, bcnt);
  1133. }
  1134. }
  1135. static void
  1136. setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
  1137. {
  1138. struct aoedev *d;
  1139. struct aoeif *p, *e;
  1140. int minbcnt;
  1141. d = t->d;
  1142. minbcnt = bcnt;
  1143. p = t->ifs;
  1144. e = p + NAOEIFS;
  1145. for (; p < e; p++) {
  1146. if (p->nd == NULL)
  1147. break; /* end of the valid interfaces */
  1148. if (p->nd == nd) {
  1149. p->bcnt = bcnt; /* we're updating */
  1150. nd = NULL;
  1151. } else if (minbcnt > p->bcnt)
  1152. minbcnt = p->bcnt; /* find the min interface */
  1153. }
  1154. if (nd) {
  1155. if (p == e) {
  1156. pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
  1157. return;
  1158. }
  1159. dev_hold(nd);
  1160. p->nd = nd;
  1161. p->bcnt = bcnt;
  1162. }
  1163. t->minbcnt = minbcnt;
  1164. setdbcnt(d);
  1165. }
  1166. void
  1167. aoecmd_cfg_rsp(struct sk_buff *skb)
  1168. {
  1169. struct aoedev *d;
  1170. struct aoe_hdr *h;
  1171. struct aoe_cfghdr *ch;
  1172. struct aoetgt *t;
  1173. ulong flags, aoemajor;
  1174. struct sk_buff *sl;
  1175. struct sk_buff_head queue;
  1176. u16 n;
  1177. sl = NULL;
  1178. h = (struct aoe_hdr *) skb_mac_header(skb);
  1179. ch = (struct aoe_cfghdr *) (h+1);
  1180. /*
  1181. * Enough people have their dip switches set backwards to
  1182. * warrant a loud message for this special case.
  1183. */
  1184. aoemajor = get_unaligned_be16(&h->major);
  1185. if (aoemajor == 0xfff) {
  1186. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  1187. "Check shelf dip switches.\n");
  1188. return;
  1189. }
  1190. if (aoemajor == 0xffff) {
  1191. pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
  1192. aoemajor, (int) h->minor);
  1193. return;
  1194. }
  1195. if (h->minor == 0xff) {
  1196. pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
  1197. aoemajor, (int) h->minor);
  1198. return;
  1199. }
  1200. n = be16_to_cpu(ch->bufcnt);
  1201. if (n > aoe_maxout) /* keep it reasonable */
  1202. n = aoe_maxout;
  1203. d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
  1204. if (d == NULL) {
  1205. pr_info("aoe: device allocation failure\n");
  1206. return;
  1207. }
  1208. spin_lock_irqsave(&d->lock, flags);
  1209. t = gettgt(d, h->src);
  1210. if (!t) {
  1211. t = addtgt(d, h->src, n);
  1212. if (!t)
  1213. goto bail;
  1214. }
  1215. n = skb->dev->mtu;
  1216. n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
  1217. n /= 512;
  1218. if (n > ch->scnt)
  1219. n = ch->scnt;
  1220. n = n ? n * 512 : DEFAULTBCNT;
  1221. setifbcnt(t, skb->dev, n);
  1222. /* don't change users' perspective */
  1223. if (d->nopen == 0) {
  1224. d->fw_ver = be16_to_cpu(ch->fwver);
  1225. sl = aoecmd_ata_id(d);
  1226. }
  1227. bail:
  1228. spin_unlock_irqrestore(&d->lock, flags);
  1229. aoedev_put(d);
  1230. if (sl) {
  1231. __skb_queue_head_init(&queue);
  1232. __skb_queue_tail(&queue, sl);
  1233. aoenet_xmit(&queue);
  1234. }
  1235. }
  1236. void
  1237. aoecmd_cleanslate(struct aoedev *d)
  1238. {
  1239. struct aoetgt **t, **te;
  1240. d->mintimer = MINTIMER;
  1241. d->maxbcnt = 0;
  1242. t = d->targets;
  1243. te = t + NTARGETS;
  1244. for (; t < te && *t; t++)
  1245. (*t)->maxout = (*t)->nframes;
  1246. }
  1247. void
  1248. aoe_failbuf(struct aoedev *d, struct buf *buf)
  1249. {
  1250. if (buf == NULL)
  1251. return;
  1252. buf->resid = 0;
  1253. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1254. if (buf->nframesout == 0)
  1255. aoe_end_buf(d, buf);
  1256. }
  1257. void
  1258. aoe_flush_iocq(void)
  1259. {
  1260. struct frame *f;
  1261. struct aoedev *d;
  1262. LIST_HEAD(flist);
  1263. struct list_head *pos;
  1264. struct sk_buff *skb;
  1265. ulong flags;
  1266. spin_lock_irqsave(&iocq.lock, flags);
  1267. list_splice_init(&iocq.head, &flist);
  1268. spin_unlock_irqrestore(&iocq.lock, flags);
  1269. while (!list_empty(&flist)) {
  1270. pos = flist.next;
  1271. list_del(pos);
  1272. f = list_entry(pos, struct frame, head);
  1273. d = f->t->d;
  1274. skb = f->r_skb;
  1275. spin_lock_irqsave(&d->lock, flags);
  1276. if (f->buf) {
  1277. f->buf->nframesout--;
  1278. aoe_failbuf(d, f->buf);
  1279. }
  1280. aoe_freetframe(f);
  1281. spin_unlock_irqrestore(&d->lock, flags);
  1282. dev_kfree_skb(skb);
  1283. aoedev_put(d);
  1284. }
  1285. }
  1286. int __init
  1287. aoecmd_init(void)
  1288. {
  1289. INIT_LIST_HEAD(&iocq.head);
  1290. spin_lock_init(&iocq.lock);
  1291. init_waitqueue_head(&ktiowq);
  1292. kts.name = "aoe_ktio";
  1293. kts.fn = ktio;
  1294. kts.waitq = &ktiowq;
  1295. kts.lock = &iocq.lock;
  1296. return aoe_ktstart(&kts);
  1297. }
  1298. void
  1299. aoecmd_exit(void)
  1300. {
  1301. aoe_ktstop(&kts);
  1302. aoe_flush_iocq();
  1303. }