aoecmd.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoecmd.c
  4. * Filesystem request handling methods
  5. */
  6. #include <linux/hdreg.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/genhd.h>
  11. #include <linux/moduleparam.h>
  12. #include <net/net_namespace.h>
  13. #include <asm/unaligned.h>
  14. #include "aoe.h"
  15. static int aoe_deadsecs = 60 * 3;
  16. module_param(aoe_deadsecs, int, 0644);
  17. MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
  18. static int aoe_maxout = 16;
  19. module_param(aoe_maxout, int, 0644);
  20. MODULE_PARM_DESC(aoe_maxout,
  21. "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
  22. static struct sk_buff *
  23. new_skb(ulong len)
  24. {
  25. struct sk_buff *skb;
  26. skb = alloc_skb(len, GFP_ATOMIC);
  27. if (skb) {
  28. skb_reset_mac_header(skb);
  29. skb_reset_network_header(skb);
  30. skb->protocol = __constant_htons(ETH_P_AOE);
  31. skb->priority = 0;
  32. skb->next = skb->prev = NULL;
  33. /* tell the network layer not to perform IP checksums
  34. * or to get the NIC to do it
  35. */
  36. skb->ip_summed = CHECKSUM_NONE;
  37. }
  38. return skb;
  39. }
  40. static struct frame *
  41. getframe(struct aoetgt *t, int tag)
  42. {
  43. struct frame *f, *e;
  44. f = t->frames;
  45. e = f + t->nframes;
  46. for (; f<e; f++)
  47. if (f->tag == tag)
  48. return f;
  49. return NULL;
  50. }
  51. /*
  52. * Leave the top bit clear so we have tagspace for userland.
  53. * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
  54. * This driver reserves tag -1 to mean "unused frame."
  55. */
  56. static int
  57. newtag(struct aoetgt *t)
  58. {
  59. register ulong n;
  60. n = jiffies & 0xffff;
  61. return n |= (++t->lasttag & 0x7fff) << 16;
  62. }
  63. static int
  64. aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
  65. {
  66. u32 host_tag = newtag(t);
  67. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  68. memcpy(h->dst, t->addr, sizeof h->dst);
  69. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  70. h->verfl = AOE_HVER;
  71. h->major = cpu_to_be16(d->aoemajor);
  72. h->minor = d->aoeminor;
  73. h->cmd = AOECMD_ATA;
  74. h->tag = cpu_to_be32(host_tag);
  75. return host_tag;
  76. }
  77. static inline void
  78. put_lba(struct aoe_atahdr *ah, sector_t lba)
  79. {
  80. ah->lba0 = lba;
  81. ah->lba1 = lba >>= 8;
  82. ah->lba2 = lba >>= 8;
  83. ah->lba3 = lba >>= 8;
  84. ah->lba4 = lba >>= 8;
  85. ah->lba5 = lba >>= 8;
  86. }
  87. static void
  88. ifrotate(struct aoetgt *t)
  89. {
  90. t->ifp++;
  91. if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
  92. t->ifp = t->ifs;
  93. if (t->ifp->nd == NULL) {
  94. printk(KERN_INFO "aoe: no interface to rotate to\n");
  95. BUG();
  96. }
  97. }
  98. static void
  99. skb_pool_put(struct aoedev *d, struct sk_buff *skb)
  100. {
  101. if (!d->skbpool_hd)
  102. d->skbpool_hd = skb;
  103. else
  104. d->skbpool_tl->next = skb;
  105. d->skbpool_tl = skb;
  106. }
  107. static struct sk_buff *
  108. skb_pool_get(struct aoedev *d)
  109. {
  110. struct sk_buff *skb;
  111. skb = d->skbpool_hd;
  112. if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
  113. d->skbpool_hd = skb->next;
  114. skb->next = NULL;
  115. return skb;
  116. }
  117. if (d->nskbpool < NSKBPOOLMAX
  118. && (skb = new_skb(ETH_ZLEN))) {
  119. d->nskbpool++;
  120. return skb;
  121. }
  122. return NULL;
  123. }
  124. /* freeframe is where we do our load balancing so it's a little hairy. */
  125. static struct frame *
  126. freeframe(struct aoedev *d)
  127. {
  128. struct frame *f, *e, *rf;
  129. struct aoetgt **t;
  130. struct sk_buff *skb;
  131. if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
  132. printk(KERN_ERR "aoe: NULL TARGETS!\n");
  133. return NULL;
  134. }
  135. t = d->tgt;
  136. t++;
  137. if (t >= &d->targets[NTARGETS] || !*t)
  138. t = d->targets;
  139. for (;;) {
  140. if ((*t)->nout < (*t)->maxout
  141. && t != d->htgt
  142. && (*t)->ifp->nd) {
  143. rf = NULL;
  144. f = (*t)->frames;
  145. e = f + (*t)->nframes;
  146. for (; f < e; f++) {
  147. if (f->tag != FREETAG)
  148. continue;
  149. skb = f->skb;
  150. if (!skb
  151. && !(f->skb = skb = new_skb(ETH_ZLEN)))
  152. continue;
  153. if (atomic_read(&skb_shinfo(skb)->dataref)
  154. != 1) {
  155. if (!rf)
  156. rf = f;
  157. continue;
  158. }
  159. gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  160. skb_trim(skb, 0);
  161. d->tgt = t;
  162. ifrotate(*t);
  163. return f;
  164. }
  165. /* Work can be done, but the network layer is
  166. holding our precious packets. Try to grab
  167. one from the pool. */
  168. f = rf;
  169. if (f == NULL) { /* more paranoia */
  170. printk(KERN_ERR
  171. "aoe: freeframe: %s.\n",
  172. "unexpected null rf");
  173. d->flags |= DEVFL_KICKME;
  174. return NULL;
  175. }
  176. skb = skb_pool_get(d);
  177. if (skb) {
  178. skb_pool_put(d, f->skb);
  179. f->skb = skb;
  180. goto gotone;
  181. }
  182. (*t)->dataref++;
  183. if ((*t)->nout == 0)
  184. d->flags |= DEVFL_KICKME;
  185. }
  186. if (t == d->tgt) /* we've looped and found nada */
  187. break;
  188. t++;
  189. if (t >= &d->targets[NTARGETS] || !*t)
  190. t = d->targets;
  191. }
  192. return NULL;
  193. }
  194. static int
  195. aoecmd_ata_rw(struct aoedev *d)
  196. {
  197. struct frame *f;
  198. struct aoe_hdr *h;
  199. struct aoe_atahdr *ah;
  200. struct buf *buf;
  201. struct bio_vec *bv;
  202. struct aoetgt *t;
  203. struct sk_buff *skb;
  204. ulong bcnt;
  205. char writebit, extbit;
  206. writebit = 0x10;
  207. extbit = 0x4;
  208. f = freeframe(d);
  209. if (f == NULL)
  210. return 0;
  211. t = *d->tgt;
  212. buf = d->inprocess;
  213. bv = buf->bv;
  214. bcnt = t->ifp->maxbcnt;
  215. if (bcnt == 0)
  216. bcnt = DEFAULTBCNT;
  217. if (bcnt > buf->bv_resid)
  218. bcnt = buf->bv_resid;
  219. /* initialize the headers & frame */
  220. skb = f->skb;
  221. h = (struct aoe_hdr *) skb_mac_header(skb);
  222. ah = (struct aoe_atahdr *) (h+1);
  223. skb_put(skb, sizeof *h + sizeof *ah);
  224. memset(h, 0, skb->len);
  225. f->tag = aoehdr_atainit(d, t, h);
  226. t->nout++;
  227. f->waited = 0;
  228. f->buf = buf;
  229. f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
  230. f->bcnt = bcnt;
  231. f->lba = buf->sector;
  232. /* set up ata header */
  233. ah->scnt = bcnt >> 9;
  234. put_lba(ah, buf->sector);
  235. if (d->flags & DEVFL_EXT) {
  236. ah->aflags |= AOEAFL_EXT;
  237. } else {
  238. extbit = 0;
  239. ah->lba3 &= 0x0f;
  240. ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
  241. }
  242. if (bio_data_dir(buf->bio) == WRITE) {
  243. skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
  244. ah->aflags |= AOEAFL_WRITE;
  245. skb->len += bcnt;
  246. skb->data_len = bcnt;
  247. t->wpkts++;
  248. } else {
  249. t->rpkts++;
  250. writebit = 0;
  251. }
  252. ah->cmdstat = WIN_READ | writebit | extbit;
  253. /* mark all tracking fields and load out */
  254. buf->nframesout += 1;
  255. buf->bv_off += bcnt;
  256. buf->bv_resid -= bcnt;
  257. buf->resid -= bcnt;
  258. buf->sector += bcnt >> 9;
  259. if (buf->resid == 0) {
  260. d->inprocess = NULL;
  261. } else if (buf->bv_resid == 0) {
  262. buf->bv = ++bv;
  263. buf->bv_resid = bv->bv_len;
  264. WARN_ON(buf->bv_resid == 0);
  265. buf->bv_off = bv->bv_offset;
  266. }
  267. skb->dev = t->ifp->nd;
  268. skb = skb_clone(skb, GFP_ATOMIC);
  269. if (skb) {
  270. if (d->sendq_hd)
  271. d->sendq_tl->next = skb;
  272. else
  273. d->sendq_hd = skb;
  274. d->sendq_tl = skb;
  275. }
  276. return 1;
  277. }
  278. /* some callers cannot sleep, and they can call this function,
  279. * transmitting the packets later, when interrupts are on
  280. */
  281. static struct sk_buff *
  282. aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
  283. {
  284. struct aoe_hdr *h;
  285. struct aoe_cfghdr *ch;
  286. struct sk_buff *skb, *sl, *sl_tail;
  287. struct net_device *ifp;
  288. sl = sl_tail = NULL;
  289. read_lock(&dev_base_lock);
  290. for_each_netdev(&init_net, ifp) {
  291. dev_hold(ifp);
  292. if (!is_aoe_netif(ifp))
  293. goto cont;
  294. skb = new_skb(sizeof *h + sizeof *ch);
  295. if (skb == NULL) {
  296. printk(KERN_INFO "aoe: skb alloc failure\n");
  297. goto cont;
  298. }
  299. skb_put(skb, sizeof *h + sizeof *ch);
  300. skb->dev = ifp;
  301. if (sl_tail == NULL)
  302. sl_tail = skb;
  303. h = (struct aoe_hdr *) skb_mac_header(skb);
  304. memset(h, 0, sizeof *h + sizeof *ch);
  305. memset(h->dst, 0xff, sizeof h->dst);
  306. memcpy(h->src, ifp->dev_addr, sizeof h->src);
  307. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  308. h->verfl = AOE_HVER;
  309. h->major = cpu_to_be16(aoemajor);
  310. h->minor = aoeminor;
  311. h->cmd = AOECMD_CFG;
  312. skb->next = sl;
  313. sl = skb;
  314. cont:
  315. dev_put(ifp);
  316. }
  317. read_unlock(&dev_base_lock);
  318. if (tail != NULL)
  319. *tail = sl_tail;
  320. return sl;
  321. }
  322. static void
  323. resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
  324. {
  325. struct sk_buff *skb;
  326. struct aoe_hdr *h;
  327. struct aoe_atahdr *ah;
  328. char buf[128];
  329. u32 n;
  330. ifrotate(t);
  331. n = newtag(t);
  332. skb = f->skb;
  333. h = (struct aoe_hdr *) skb_mac_header(skb);
  334. ah = (struct aoe_atahdr *) (h+1);
  335. snprintf(buf, sizeof buf,
  336. "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x "
  337. "s=%012llx d=%012llx nout=%d\n",
  338. "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
  339. mac_addr(h->src),
  340. mac_addr(h->dst), t->nout);
  341. aoechr_error(buf);
  342. f->tag = n;
  343. h->tag = cpu_to_be32(n);
  344. memcpy(h->dst, t->addr, sizeof h->dst);
  345. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  346. switch (ah->cmdstat) {
  347. default:
  348. break;
  349. case WIN_READ:
  350. case WIN_READ_EXT:
  351. case WIN_WRITE:
  352. case WIN_WRITE_EXT:
  353. put_lba(ah, f->lba);
  354. n = f->bcnt;
  355. if (n > DEFAULTBCNT)
  356. n = DEFAULTBCNT;
  357. ah->scnt = n >> 9;
  358. if (ah->aflags & AOEAFL_WRITE) {
  359. skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
  360. offset_in_page(f->bufaddr), n);
  361. skb->len = sizeof *h + sizeof *ah + n;
  362. skb->data_len = n;
  363. }
  364. }
  365. skb->dev = t->ifp->nd;
  366. skb = skb_clone(skb, GFP_ATOMIC);
  367. if (skb == NULL)
  368. return;
  369. if (d->sendq_hd)
  370. d->sendq_tl->next = skb;
  371. else
  372. d->sendq_hd = skb;
  373. d->sendq_tl = skb;
  374. }
  375. static int
  376. tsince(int tag)
  377. {
  378. int n;
  379. n = jiffies & 0xffff;
  380. n -= tag & 0xffff;
  381. if (n < 0)
  382. n += 1<<16;
  383. return n;
  384. }
  385. static struct aoeif *
  386. getif(struct aoetgt *t, struct net_device *nd)
  387. {
  388. struct aoeif *p, *e;
  389. p = t->ifs;
  390. e = p + NAOEIFS;
  391. for (; p < e; p++)
  392. if (p->nd == nd)
  393. return p;
  394. return NULL;
  395. }
  396. static struct aoeif *
  397. addif(struct aoetgt *t, struct net_device *nd)
  398. {
  399. struct aoeif *p;
  400. p = getif(t, NULL);
  401. if (!p)
  402. return NULL;
  403. p->nd = nd;
  404. p->maxbcnt = DEFAULTBCNT;
  405. p->lost = 0;
  406. p->lostjumbo = 0;
  407. return p;
  408. }
  409. static void
  410. ejectif(struct aoetgt *t, struct aoeif *ifp)
  411. {
  412. struct aoeif *e;
  413. ulong n;
  414. e = t->ifs + NAOEIFS - 1;
  415. n = (e - ifp) * sizeof *ifp;
  416. memmove(ifp, ifp+1, n);
  417. e->nd = NULL;
  418. }
  419. static int
  420. sthtith(struct aoedev *d)
  421. {
  422. struct frame *f, *e, *nf;
  423. struct sk_buff *skb;
  424. struct aoetgt *ht = *d->htgt;
  425. f = ht->frames;
  426. e = f + ht->nframes;
  427. for (; f < e; f++) {
  428. if (f->tag == FREETAG)
  429. continue;
  430. nf = freeframe(d);
  431. if (!nf)
  432. return 0;
  433. skb = nf->skb;
  434. *nf = *f;
  435. f->skb = skb;
  436. f->tag = FREETAG;
  437. nf->waited = 0;
  438. ht->nout--;
  439. (*d->tgt)->nout++;
  440. resend(d, *d->tgt, nf);
  441. }
  442. /* he's clean, he's useless. take away his interfaces */
  443. memset(ht->ifs, 0, sizeof ht->ifs);
  444. d->htgt = NULL;
  445. return 1;
  446. }
  447. static inline unsigned char
  448. ata_scnt(unsigned char *packet) {
  449. struct aoe_hdr *h;
  450. struct aoe_atahdr *ah;
  451. h = (struct aoe_hdr *) packet;
  452. ah = (struct aoe_atahdr *) (h+1);
  453. return ah->scnt;
  454. }
  455. static void
  456. rexmit_timer(ulong vp)
  457. {
  458. struct aoedev *d;
  459. struct aoetgt *t, **tt, **te;
  460. struct aoeif *ifp;
  461. struct frame *f, *e;
  462. struct sk_buff *sl;
  463. register long timeout;
  464. ulong flags, n;
  465. d = (struct aoedev *) vp;
  466. sl = NULL;
  467. /* timeout is always ~150% of the moving average */
  468. timeout = d->rttavg;
  469. timeout += timeout >> 1;
  470. spin_lock_irqsave(&d->lock, flags);
  471. if (d->flags & DEVFL_TKILL) {
  472. spin_unlock_irqrestore(&d->lock, flags);
  473. return;
  474. }
  475. tt = d->targets;
  476. te = tt + NTARGETS;
  477. for (; tt < te && *tt; tt++) {
  478. t = *tt;
  479. f = t->frames;
  480. e = f + t->nframes;
  481. for (; f < e; f++) {
  482. if (f->tag == FREETAG
  483. || tsince(f->tag) < timeout)
  484. continue;
  485. n = f->waited += timeout;
  486. n /= HZ;
  487. if (n > aoe_deadsecs) {
  488. /* waited too long. device failure. */
  489. aoedev_downdev(d);
  490. break;
  491. }
  492. if (n > HELPWAIT /* see if another target can help */
  493. && (tt != d->targets || d->targets[1]))
  494. d->htgt = tt;
  495. if (t->nout == t->maxout) {
  496. if (t->maxout > 1)
  497. t->maxout--;
  498. t->lastwadj = jiffies;
  499. }
  500. ifp = getif(t, f->skb->dev);
  501. if (ifp && ++ifp->lost > (t->nframes << 1)
  502. && (ifp != t->ifs || t->ifs[1].nd)) {
  503. ejectif(t, ifp);
  504. ifp = NULL;
  505. }
  506. if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
  507. && ifp && ++ifp->lostjumbo > (t->nframes << 1)
  508. && ifp->maxbcnt != DEFAULTBCNT) {
  509. printk(KERN_INFO
  510. "aoe: e%ld.%d: "
  511. "too many lost jumbo on "
  512. "%s:%012llx - "
  513. "falling back to %d frames.\n",
  514. d->aoemajor, d->aoeminor,
  515. ifp->nd->name, mac_addr(t->addr),
  516. DEFAULTBCNT);
  517. ifp->maxbcnt = 0;
  518. }
  519. resend(d, t, f);
  520. }
  521. /* window check */
  522. if (t->nout == t->maxout
  523. && t->maxout < t->nframes
  524. && (jiffies - t->lastwadj)/HZ > 10) {
  525. t->maxout++;
  526. t->lastwadj = jiffies;
  527. }
  528. }
  529. if (d->sendq_hd) {
  530. n = d->rttavg <<= 1;
  531. if (n > MAXTIMER)
  532. d->rttavg = MAXTIMER;
  533. }
  534. if (d->flags & DEVFL_KICKME || d->htgt) {
  535. d->flags &= ~DEVFL_KICKME;
  536. aoecmd_work(d);
  537. }
  538. sl = d->sendq_hd;
  539. d->sendq_hd = d->sendq_tl = NULL;
  540. d->timer.expires = jiffies + TIMERTICK;
  541. add_timer(&d->timer);
  542. spin_unlock_irqrestore(&d->lock, flags);
  543. aoenet_xmit(sl);
  544. }
  545. /* enters with d->lock held */
  546. void
  547. aoecmd_work(struct aoedev *d)
  548. {
  549. struct buf *buf;
  550. loop:
  551. if (d->htgt && !sthtith(d))
  552. return;
  553. if (d->inprocess == NULL) {
  554. if (list_empty(&d->bufq))
  555. return;
  556. buf = container_of(d->bufq.next, struct buf, bufs);
  557. list_del(d->bufq.next);
  558. d->inprocess = buf;
  559. }
  560. if (aoecmd_ata_rw(d))
  561. goto loop;
  562. }
  563. /* this function performs work that has been deferred until sleeping is OK
  564. */
  565. void
  566. aoecmd_sleepwork(struct work_struct *work)
  567. {
  568. struct aoedev *d = container_of(work, struct aoedev, work);
  569. if (d->flags & DEVFL_GDALLOC)
  570. aoeblk_gdalloc(d);
  571. if (d->flags & DEVFL_NEWSIZE) {
  572. struct block_device *bd;
  573. unsigned long flags;
  574. u64 ssize;
  575. ssize = d->gd->capacity;
  576. bd = bdget_disk(d->gd, 0);
  577. if (bd) {
  578. mutex_lock(&bd->bd_inode->i_mutex);
  579. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  580. mutex_unlock(&bd->bd_inode->i_mutex);
  581. bdput(bd);
  582. }
  583. spin_lock_irqsave(&d->lock, flags);
  584. d->flags |= DEVFL_UP;
  585. d->flags &= ~DEVFL_NEWSIZE;
  586. spin_unlock_irqrestore(&d->lock, flags);
  587. }
  588. }
  589. static void
  590. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  591. {
  592. u64 ssize;
  593. u16 n;
  594. /* word 83: command set supported */
  595. n = get_unaligned_le16(&id[83 << 1]);
  596. /* word 86: command set/feature enabled */
  597. n |= get_unaligned_le16(&id[86 << 1]);
  598. if (n & (1<<10)) { /* bit 10: LBA 48 */
  599. d->flags |= DEVFL_EXT;
  600. /* word 100: number lba48 sectors */
  601. ssize = get_unaligned_le64(&id[100 << 1]);
  602. /* set as in ide-disk.c:init_idedisk_capacity */
  603. d->geo.cylinders = ssize;
  604. d->geo.cylinders /= (255 * 63);
  605. d->geo.heads = 255;
  606. d->geo.sectors = 63;
  607. } else {
  608. d->flags &= ~DEVFL_EXT;
  609. /* number lba28 sectors */
  610. ssize = get_unaligned_le32(&id[60 << 1]);
  611. /* NOTE: obsolete in ATA 6 */
  612. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  613. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  614. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  615. }
  616. if (d->ssize != ssize)
  617. printk(KERN_INFO
  618. "aoe: %012llx e%ld.%d v%04x has %llu sectors\n",
  619. mac_addr(t->addr),
  620. d->aoemajor, d->aoeminor,
  621. d->fw_ver, (long long)ssize);
  622. d->ssize = ssize;
  623. d->geo.start = 0;
  624. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  625. return;
  626. if (d->gd != NULL) {
  627. d->gd->capacity = ssize;
  628. d->flags |= DEVFL_NEWSIZE;
  629. } else
  630. d->flags |= DEVFL_GDALLOC;
  631. schedule_work(&d->work);
  632. }
  633. static void
  634. calc_rttavg(struct aoedev *d, int rtt)
  635. {
  636. register long n;
  637. n = rtt;
  638. if (n < 0) {
  639. n = -rtt;
  640. if (n < MINTIMER)
  641. n = MINTIMER;
  642. else if (n > MAXTIMER)
  643. n = MAXTIMER;
  644. d->mintimer += (n - d->mintimer) >> 1;
  645. } else if (n < d->mintimer)
  646. n = d->mintimer;
  647. else if (n > MAXTIMER)
  648. n = MAXTIMER;
  649. /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
  650. n -= d->rttavg;
  651. d->rttavg += n >> 2;
  652. }
  653. static struct aoetgt *
  654. gettgt(struct aoedev *d, char *addr)
  655. {
  656. struct aoetgt **t, **e;
  657. t = d->targets;
  658. e = t + NTARGETS;
  659. for (; t < e && *t; t++)
  660. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  661. return *t;
  662. return NULL;
  663. }
  664. static inline void
  665. diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
  666. {
  667. unsigned long n_sect = bio->bi_size >> 9;
  668. const int rw = bio_data_dir(bio);
  669. all_stat_inc(disk, ios[rw], sector);
  670. all_stat_add(disk, ticks[rw], duration, sector);
  671. all_stat_add(disk, sectors[rw], n_sect, sector);
  672. all_stat_add(disk, io_ticks, duration, sector);
  673. }
  674. void
  675. aoecmd_ata_rsp(struct sk_buff *skb)
  676. {
  677. struct aoedev *d;
  678. struct aoe_hdr *hin, *hout;
  679. struct aoe_atahdr *ahin, *ahout;
  680. struct frame *f;
  681. struct buf *buf;
  682. struct sk_buff *sl;
  683. struct aoetgt *t;
  684. struct aoeif *ifp;
  685. register long n;
  686. ulong flags;
  687. char ebuf[128];
  688. u16 aoemajor;
  689. hin = (struct aoe_hdr *) skb_mac_header(skb);
  690. aoemajor = get_unaligned_be16(&hin->major);
  691. d = aoedev_by_aoeaddr(aoemajor, hin->minor);
  692. if (d == NULL) {
  693. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  694. "for unknown device %d.%d\n",
  695. aoemajor, hin->minor);
  696. aoechr_error(ebuf);
  697. return;
  698. }
  699. spin_lock_irqsave(&d->lock, flags);
  700. n = get_unaligned_be32(&hin->tag);
  701. t = gettgt(d, hin->src);
  702. if (t == NULL) {
  703. printk(KERN_INFO "aoe: can't find target e%ld.%d:%012llx\n",
  704. d->aoemajor, d->aoeminor, mac_addr(hin->src));
  705. spin_unlock_irqrestore(&d->lock, flags);
  706. return;
  707. }
  708. f = getframe(t, n);
  709. if (f == NULL) {
  710. calc_rttavg(d, -tsince(n));
  711. spin_unlock_irqrestore(&d->lock, flags);
  712. snprintf(ebuf, sizeof ebuf,
  713. "%15s e%d.%d tag=%08x@%08lx\n",
  714. "unexpected rsp",
  715. get_unaligned_be16(&hin->major),
  716. hin->minor,
  717. get_unaligned_be32(&hin->tag),
  718. jiffies);
  719. aoechr_error(ebuf);
  720. return;
  721. }
  722. calc_rttavg(d, tsince(f->tag));
  723. ahin = (struct aoe_atahdr *) (hin+1);
  724. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  725. ahout = (struct aoe_atahdr *) (hout+1);
  726. buf = f->buf;
  727. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  728. printk(KERN_ERR
  729. "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  730. ahout->cmdstat, ahin->cmdstat,
  731. d->aoemajor, d->aoeminor);
  732. if (buf)
  733. buf->flags |= BUFFL_FAIL;
  734. } else {
  735. if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
  736. d->htgt = NULL;
  737. n = ahout->scnt << 9;
  738. switch (ahout->cmdstat) {
  739. case WIN_READ:
  740. case WIN_READ_EXT:
  741. if (skb->len - sizeof *hin - sizeof *ahin < n) {
  742. printk(KERN_ERR
  743. "aoe: %s. skb->len=%d need=%ld\n",
  744. "runt data size in read", skb->len, n);
  745. /* fail frame f? just returning will rexmit. */
  746. spin_unlock_irqrestore(&d->lock, flags);
  747. return;
  748. }
  749. memcpy(f->bufaddr, ahin+1, n);
  750. case WIN_WRITE:
  751. case WIN_WRITE_EXT:
  752. ifp = getif(t, skb->dev);
  753. if (ifp) {
  754. ifp->lost = 0;
  755. if (n > DEFAULTBCNT)
  756. ifp->lostjumbo = 0;
  757. }
  758. if (f->bcnt -= n) {
  759. f->lba += n >> 9;
  760. f->bufaddr += n;
  761. resend(d, t, f);
  762. goto xmit;
  763. }
  764. break;
  765. case WIN_IDENTIFY:
  766. if (skb->len - sizeof *hin - sizeof *ahin < 512) {
  767. printk(KERN_INFO
  768. "aoe: runt data size in ataid. skb->len=%d\n",
  769. skb->len);
  770. spin_unlock_irqrestore(&d->lock, flags);
  771. return;
  772. }
  773. ataid_complete(d, t, (char *) (ahin+1));
  774. break;
  775. default:
  776. printk(KERN_INFO
  777. "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  778. ahout->cmdstat,
  779. get_unaligned_be16(&hin->major),
  780. hin->minor);
  781. }
  782. }
  783. if (buf && --buf->nframesout == 0 && buf->resid == 0) {
  784. diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
  785. n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
  786. bio_endio(buf->bio, n);
  787. mempool_free(buf, d->bufpool);
  788. }
  789. f->buf = NULL;
  790. f->tag = FREETAG;
  791. t->nout--;
  792. aoecmd_work(d);
  793. xmit:
  794. sl = d->sendq_hd;
  795. d->sendq_hd = d->sendq_tl = NULL;
  796. spin_unlock_irqrestore(&d->lock, flags);
  797. aoenet_xmit(sl);
  798. }
  799. void
  800. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  801. {
  802. struct sk_buff *sl;
  803. sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
  804. aoenet_xmit(sl);
  805. }
  806. struct sk_buff *
  807. aoecmd_ata_id(struct aoedev *d)
  808. {
  809. struct aoe_hdr *h;
  810. struct aoe_atahdr *ah;
  811. struct frame *f;
  812. struct sk_buff *skb;
  813. struct aoetgt *t;
  814. f = freeframe(d);
  815. if (f == NULL)
  816. return NULL;
  817. t = *d->tgt;
  818. /* initialize the headers & frame */
  819. skb = f->skb;
  820. h = (struct aoe_hdr *) skb_mac_header(skb);
  821. ah = (struct aoe_atahdr *) (h+1);
  822. skb_put(skb, sizeof *h + sizeof *ah);
  823. memset(h, 0, skb->len);
  824. f->tag = aoehdr_atainit(d, t, h);
  825. t->nout++;
  826. f->waited = 0;
  827. /* set up ata header */
  828. ah->scnt = 1;
  829. ah->cmdstat = WIN_IDENTIFY;
  830. ah->lba3 = 0xa0;
  831. skb->dev = t->ifp->nd;
  832. d->rttavg = MAXTIMER;
  833. d->timer.function = rexmit_timer;
  834. return skb_clone(skb, GFP_ATOMIC);
  835. }
  836. static struct aoetgt *
  837. addtgt(struct aoedev *d, char *addr, ulong nframes)
  838. {
  839. struct aoetgt *t, **tt, **te;
  840. struct frame *f, *e;
  841. tt = d->targets;
  842. te = tt + NTARGETS;
  843. for (; tt < te && *tt; tt++)
  844. ;
  845. if (tt == te) {
  846. printk(KERN_INFO
  847. "aoe: device addtgt failure; too many targets\n");
  848. return NULL;
  849. }
  850. t = kcalloc(1, sizeof *t, GFP_ATOMIC);
  851. f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
  852. if (!t || !f) {
  853. kfree(f);
  854. kfree(t);
  855. printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
  856. return NULL;
  857. }
  858. t->nframes = nframes;
  859. t->frames = f;
  860. e = f + nframes;
  861. for (; f < e; f++)
  862. f->tag = FREETAG;
  863. memcpy(t->addr, addr, sizeof t->addr);
  864. t->ifp = t->ifs;
  865. t->maxout = t->nframes;
  866. return *tt = t;
  867. }
  868. void
  869. aoecmd_cfg_rsp(struct sk_buff *skb)
  870. {
  871. struct aoedev *d;
  872. struct aoe_hdr *h;
  873. struct aoe_cfghdr *ch;
  874. struct aoetgt *t;
  875. struct aoeif *ifp;
  876. ulong flags, sysminor, aoemajor;
  877. struct sk_buff *sl;
  878. u16 n;
  879. h = (struct aoe_hdr *) skb_mac_header(skb);
  880. ch = (struct aoe_cfghdr *) (h+1);
  881. /*
  882. * Enough people have their dip switches set backwards to
  883. * warrant a loud message for this special case.
  884. */
  885. aoemajor = be16_to_cpu(get_unaligned(&h->major));
  886. if (aoemajor == 0xfff) {
  887. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  888. "Check shelf dip switches.\n");
  889. return;
  890. }
  891. sysminor = SYSMINOR(aoemajor, h->minor);
  892. if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
  893. printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
  894. aoemajor, (int) h->minor);
  895. return;
  896. }
  897. n = be16_to_cpu(ch->bufcnt);
  898. if (n > aoe_maxout) /* keep it reasonable */
  899. n = aoe_maxout;
  900. d = aoedev_by_sysminor_m(sysminor);
  901. if (d == NULL) {
  902. printk(KERN_INFO "aoe: device sysminor_m failure\n");
  903. return;
  904. }
  905. spin_lock_irqsave(&d->lock, flags);
  906. t = gettgt(d, h->src);
  907. if (!t) {
  908. t = addtgt(d, h->src, n);
  909. if (!t) {
  910. spin_unlock_irqrestore(&d->lock, flags);
  911. return;
  912. }
  913. }
  914. ifp = getif(t, skb->dev);
  915. if (!ifp) {
  916. ifp = addif(t, skb->dev);
  917. if (!ifp) {
  918. printk(KERN_INFO
  919. "aoe: device addif failure; "
  920. "too many interfaces?\n");
  921. spin_unlock_irqrestore(&d->lock, flags);
  922. return;
  923. }
  924. }
  925. if (ifp->maxbcnt) {
  926. n = ifp->nd->mtu;
  927. n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
  928. n /= 512;
  929. if (n > ch->scnt)
  930. n = ch->scnt;
  931. n = n ? n * 512 : DEFAULTBCNT;
  932. if (n != ifp->maxbcnt) {
  933. printk(KERN_INFO
  934. "aoe: e%ld.%d: setting %d%s%s:%012llx\n",
  935. d->aoemajor, d->aoeminor, n,
  936. " byte data frames on ", ifp->nd->name,
  937. mac_addr(t->addr));
  938. ifp->maxbcnt = n;
  939. }
  940. }
  941. /* don't change users' perspective */
  942. if (d->nopen) {
  943. spin_unlock_irqrestore(&d->lock, flags);
  944. return;
  945. }
  946. d->fw_ver = be16_to_cpu(ch->fwver);
  947. sl = aoecmd_ata_id(d);
  948. spin_unlock_irqrestore(&d->lock, flags);
  949. aoenet_xmit(sl);
  950. }
  951. void
  952. aoecmd_cleanslate(struct aoedev *d)
  953. {
  954. struct aoetgt **t, **te;
  955. struct aoeif *p, *e;
  956. d->mintimer = MINTIMER;
  957. t = d->targets;
  958. te = t + NTARGETS;
  959. for (; t < te && *t; t++) {
  960. (*t)->maxout = (*t)->nframes;
  961. p = (*t)->ifs;
  962. e = p + NAOEIFS;
  963. for (; p < e; p++) {
  964. p->lostjumbo = 0;
  965. p->lost = 0;
  966. p->maxbcnt = DEFAULTBCNT;
  967. }
  968. }
  969. }