aoecmd.c 35 KB

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