aoecmd.c 37 KB

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