aoecmd.c 23 KB

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