aoecmd.c 35 KB

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