aoecmd.c 23 KB

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