libiscsi.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <asm/unaligned.h>
  31. #include <net/tcp.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include <scsi/scsi_host.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/iscsi_proto.h>
  39. #include <scsi/scsi_transport.h>
  40. #include <scsi/scsi_transport_iscsi.h>
  41. #include <scsi/libiscsi.h>
  42. static int iscsi_dbg_lib_conn;
  43. module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
  44. S_IRUGO | S_IWUSR);
  45. MODULE_PARM_DESC(debug_libiscsi_conn,
  46. "Turn on debugging for connections in libiscsi module. "
  47. "Set to 1 to turn on, and zero to turn off. Default is off.");
  48. static int iscsi_dbg_lib_session;
  49. module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
  50. S_IRUGO | S_IWUSR);
  51. MODULE_PARM_DESC(debug_libiscsi_session,
  52. "Turn on debugging for sessions in libiscsi module. "
  53. "Set to 1 to turn on, and zero to turn off. Default is off.");
  54. static int iscsi_dbg_lib_eh;
  55. module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
  56. S_IRUGO | S_IWUSR);
  57. MODULE_PARM_DESC(debug_libiscsi_eh,
  58. "Turn on debugging for error handling in libiscsi module. "
  59. "Set to 1 to turn on, and zero to turn off. Default is off.");
  60. #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
  61. do { \
  62. if (iscsi_dbg_lib_conn) \
  63. iscsi_conn_printk(KERN_INFO, _conn, \
  64. "%s " dbg_fmt, \
  65. __func__, ##arg); \
  66. } while (0);
  67. #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
  68. do { \
  69. if (iscsi_dbg_lib_session) \
  70. iscsi_session_printk(KERN_INFO, _session, \
  71. "%s " dbg_fmt, \
  72. __func__, ##arg); \
  73. } while (0);
  74. #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
  75. do { \
  76. if (iscsi_dbg_lib_eh) \
  77. iscsi_session_printk(KERN_INFO, _session, \
  78. "%s " dbg_fmt, \
  79. __func__, ##arg); \
  80. } while (0);
  81. inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
  82. {
  83. struct Scsi_Host *shost = conn->session->host;
  84. struct iscsi_host *ihost = shost_priv(shost);
  85. if (ihost->workq)
  86. queue_work(ihost->workq, &conn->xmitwork);
  87. }
  88. EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
  89. static void __iscsi_update_cmdsn(struct iscsi_session *session,
  90. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  91. {
  92. /*
  93. * standard specifies this check for when to update expected and
  94. * max sequence numbers
  95. */
  96. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  97. return;
  98. if (exp_cmdsn != session->exp_cmdsn &&
  99. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  100. session->exp_cmdsn = exp_cmdsn;
  101. if (max_cmdsn != session->max_cmdsn &&
  102. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
  103. session->max_cmdsn = max_cmdsn;
  104. }
  105. void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  106. {
  107. __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
  108. be32_to_cpu(hdr->max_cmdsn));
  109. }
  110. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  111. /**
  112. * iscsi_prep_data_out_pdu - initialize Data-Out
  113. * @task: scsi command task
  114. * @r2t: R2T info
  115. * @hdr: iscsi data in pdu
  116. *
  117. * Notes:
  118. * Initialize Data-Out within this R2T sequence and finds
  119. * proper data_offset within this SCSI command.
  120. *
  121. * This function is called with connection lock taken.
  122. **/
  123. void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
  124. struct iscsi_data *hdr)
  125. {
  126. struct iscsi_conn *conn = task->conn;
  127. unsigned int left = r2t->data_length - r2t->sent;
  128. task->hdr_len = sizeof(struct iscsi_data);
  129. memset(hdr, 0, sizeof(struct iscsi_data));
  130. hdr->ttt = r2t->ttt;
  131. hdr->datasn = cpu_to_be32(r2t->datasn);
  132. r2t->datasn++;
  133. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  134. hdr->lun = task->lun;
  135. hdr->itt = task->hdr_itt;
  136. hdr->exp_statsn = r2t->exp_statsn;
  137. hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
  138. if (left > conn->max_xmit_dlength) {
  139. hton24(hdr->dlength, conn->max_xmit_dlength);
  140. r2t->data_count = conn->max_xmit_dlength;
  141. hdr->flags = 0;
  142. } else {
  143. hton24(hdr->dlength, left);
  144. r2t->data_count = left;
  145. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  146. }
  147. conn->dataout_pdus_cnt++;
  148. }
  149. EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
  150. static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
  151. {
  152. unsigned exp_len = task->hdr_len + len;
  153. if (exp_len > task->hdr_max) {
  154. WARN_ON(1);
  155. return -EINVAL;
  156. }
  157. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  158. task->hdr_len = exp_len;
  159. return 0;
  160. }
  161. /*
  162. * make an extended cdb AHS
  163. */
  164. static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
  165. {
  166. struct scsi_cmnd *cmd = task->sc;
  167. unsigned rlen, pad_len;
  168. unsigned short ahslength;
  169. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  170. int rc;
  171. ecdb_ahdr = iscsi_next_hdr(task);
  172. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  173. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  174. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  175. pad_len = iscsi_padding(rlen);
  176. rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
  177. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  178. if (rc)
  179. return rc;
  180. if (pad_len)
  181. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  182. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  183. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  184. ecdb_ahdr->reserved = 0;
  185. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  186. ISCSI_DBG_SESSION(task->conn->session,
  187. "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  188. "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
  189. "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
  190. task->hdr_len);
  191. return 0;
  192. }
  193. static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
  194. {
  195. struct scsi_cmnd *sc = task->sc;
  196. struct iscsi_rlength_ahdr *rlen_ahdr;
  197. int rc;
  198. rlen_ahdr = iscsi_next_hdr(task);
  199. rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
  200. if (rc)
  201. return rc;
  202. rlen_ahdr->ahslength =
  203. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  204. sizeof(rlen_ahdr->reserved));
  205. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  206. rlen_ahdr->reserved = 0;
  207. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  208. ISCSI_DBG_SESSION(task->conn->session,
  209. "bidi-in rlen_ahdr->read_length(%d) "
  210. "rlen_ahdr->ahslength(%d)\n",
  211. be32_to_cpu(rlen_ahdr->read_length),
  212. be16_to_cpu(rlen_ahdr->ahslength));
  213. return 0;
  214. }
  215. /**
  216. * iscsi_check_tmf_restrictions - check if a task is affected by TMF
  217. * @task: iscsi task
  218. * @opcode: opcode to check for
  219. *
  220. * During TMF a task has to be checked if it's affected.
  221. * All unrelated I/O can be passed through, but I/O to the
  222. * affected LUN should be restricted.
  223. * If 'fast_abort' is set we won't be sending any I/O to the
  224. * affected LUN.
  225. * Otherwise the target is waiting for all TTTs to be completed,
  226. * so we have to send all outstanding Data-Out PDUs to the target.
  227. */
  228. static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
  229. {
  230. struct iscsi_conn *conn = task->conn;
  231. struct iscsi_tm *tmf = &conn->tmhdr;
  232. u64 hdr_lun;
  233. if (conn->tmf_state == TMF_INITIAL)
  234. return 0;
  235. if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
  236. return 0;
  237. switch (ISCSI_TM_FUNC_VALUE(tmf)) {
  238. case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
  239. /*
  240. * Allow PDUs for unrelated LUNs
  241. */
  242. hdr_lun = scsilun_to_int(&tmf->lun);
  243. if (hdr_lun != task->sc->device->lun)
  244. return 0;
  245. /* fall through */
  246. case ISCSI_TM_FUNC_TARGET_WARM_RESET:
  247. /*
  248. * Fail all SCSI cmd PDUs
  249. */
  250. if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
  251. iscsi_conn_printk(KERN_INFO, conn,
  252. "task [op %x/%x itt "
  253. "0x%x/0x%x] "
  254. "rejected.\n",
  255. task->hdr->opcode, opcode,
  256. task->itt, task->hdr_itt);
  257. return -EACCES;
  258. }
  259. /*
  260. * And also all data-out PDUs in response to R2T
  261. * if fast_abort is set.
  262. */
  263. if (conn->session->fast_abort) {
  264. iscsi_conn_printk(KERN_INFO, conn,
  265. "task [op %x/%x itt "
  266. "0x%x/0x%x] fast abort.\n",
  267. task->hdr->opcode, opcode,
  268. task->itt, task->hdr_itt);
  269. return -EACCES;
  270. }
  271. break;
  272. case ISCSI_TM_FUNC_ABORT_TASK:
  273. /*
  274. * the caller has already checked if the task
  275. * they want to abort was in the pending queue so if
  276. * we are here the cmd pdu has gone out already, and
  277. * we will only hit this for data-outs
  278. */
  279. if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
  280. task->hdr_itt == tmf->rtt) {
  281. ISCSI_DBG_SESSION(conn->session,
  282. "Preventing task %x/%x from sending "
  283. "data-out due to abort task in "
  284. "progress\n", task->itt,
  285. task->hdr_itt);
  286. return -EACCES;
  287. }
  288. break;
  289. }
  290. return 0;
  291. }
  292. /**
  293. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  294. * @task: iscsi task
  295. *
  296. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  297. * fields like dlength or final based on how much data it sends
  298. */
  299. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
  300. {
  301. struct iscsi_conn *conn = task->conn;
  302. struct iscsi_session *session = conn->session;
  303. struct scsi_cmnd *sc = task->sc;
  304. struct iscsi_scsi_req *hdr;
  305. unsigned hdrlength, cmd_len, transfer_length;
  306. itt_t itt;
  307. int rc;
  308. rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
  309. if (rc)
  310. return rc;
  311. if (conn->session->tt->alloc_pdu) {
  312. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
  313. if (rc)
  314. return rc;
  315. }
  316. hdr = (struct iscsi_scsi_req *)task->hdr;
  317. itt = hdr->itt;
  318. memset(hdr, 0, sizeof(*hdr));
  319. if (session->tt->parse_pdu_itt)
  320. hdr->itt = task->hdr_itt = itt;
  321. else
  322. hdr->itt = task->hdr_itt = build_itt(task->itt,
  323. task->conn->session->age);
  324. task->hdr_len = 0;
  325. rc = iscsi_add_hdr(task, sizeof(*hdr));
  326. if (rc)
  327. return rc;
  328. hdr->opcode = ISCSI_OP_SCSI_CMD;
  329. hdr->flags = ISCSI_ATTR_SIMPLE;
  330. int_to_scsilun(sc->device->lun, &hdr->lun);
  331. task->lun = hdr->lun;
  332. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  333. cmd_len = sc->cmd_len;
  334. if (cmd_len < ISCSI_CDB_SIZE)
  335. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  336. else if (cmd_len > ISCSI_CDB_SIZE) {
  337. rc = iscsi_prep_ecdb_ahs(task);
  338. if (rc)
  339. return rc;
  340. cmd_len = ISCSI_CDB_SIZE;
  341. }
  342. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  343. task->imm_count = 0;
  344. if (scsi_bidi_cmnd(sc)) {
  345. hdr->flags |= ISCSI_FLAG_CMD_READ;
  346. rc = iscsi_prep_bidi_ahs(task);
  347. if (rc)
  348. return rc;
  349. }
  350. if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
  351. task->protected = true;
  352. transfer_length = scsi_transfer_length(sc);
  353. hdr->data_length = cpu_to_be32(transfer_length);
  354. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  355. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  356. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  357. /*
  358. * Write counters:
  359. *
  360. * imm_count bytes to be sent right after
  361. * SCSI PDU Header
  362. *
  363. * unsol_count bytes(as Data-Out) to be sent
  364. * without R2T ack right after
  365. * immediate data
  366. *
  367. * r2t data_length bytes to be sent via R2T ack's
  368. *
  369. * pad_count bytes to be sent as zero-padding
  370. */
  371. memset(r2t, 0, sizeof(*r2t));
  372. if (session->imm_data_en) {
  373. if (transfer_length >= session->first_burst)
  374. task->imm_count = min(session->first_burst,
  375. conn->max_xmit_dlength);
  376. else
  377. task->imm_count = min(transfer_length,
  378. conn->max_xmit_dlength);
  379. hton24(hdr->dlength, task->imm_count);
  380. } else
  381. zero_data(hdr->dlength);
  382. if (!session->initial_r2t_en) {
  383. r2t->data_length = min(session->first_burst,
  384. transfer_length) -
  385. task->imm_count;
  386. r2t->data_offset = task->imm_count;
  387. r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  388. r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
  389. }
  390. if (!task->unsol_r2t.data_length)
  391. /* No unsolicit Data-Out's */
  392. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  393. } else {
  394. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  395. zero_data(hdr->dlength);
  396. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  397. hdr->flags |= ISCSI_FLAG_CMD_READ;
  398. }
  399. /* calculate size of additional header segments (AHSs) */
  400. hdrlength = task->hdr_len - sizeof(*hdr);
  401. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  402. hdrlength /= ISCSI_PAD_LEN;
  403. WARN_ON(hdrlength >= 256);
  404. hdr->hlength = hdrlength & 0xFF;
  405. hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
  406. if (session->tt->init_task && session->tt->init_task(task))
  407. return -EIO;
  408. task->state = ISCSI_TASK_RUNNING;
  409. session->cmdsn++;
  410. conn->scsicmd_pdus_cnt++;
  411. ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
  412. "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
  413. scsi_bidi_cmnd(sc) ? "bidirectional" :
  414. sc->sc_data_direction == DMA_TO_DEVICE ?
  415. "write" : "read", conn->id, sc, sc->cmnd[0],
  416. task->itt, transfer_length,
  417. scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  418. session->cmdsn,
  419. session->max_cmdsn - session->exp_cmdsn + 1);
  420. return 0;
  421. }
  422. /**
  423. * iscsi_free_task - free a task
  424. * @task: iscsi cmd task
  425. *
  426. * Must be called with session back_lock.
  427. * This function returns the scsi command to scsi-ml or cleans
  428. * up mgmt tasks then returns the task to the pool.
  429. */
  430. static void iscsi_free_task(struct iscsi_task *task)
  431. {
  432. struct iscsi_conn *conn = task->conn;
  433. struct iscsi_session *session = conn->session;
  434. struct scsi_cmnd *sc = task->sc;
  435. int oldstate = task->state;
  436. ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
  437. task->itt, task->state, task->sc);
  438. session->tt->cleanup_task(task);
  439. task->state = ISCSI_TASK_FREE;
  440. task->sc = NULL;
  441. /*
  442. * login task is preallocated so do not free
  443. */
  444. if (conn->login_task == task)
  445. return;
  446. kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
  447. if (sc) {
  448. /* SCSI eh reuses commands to verify us */
  449. sc->SCp.ptr = NULL;
  450. /*
  451. * queue command may call this to free the task, so
  452. * it will decide how to return sc to scsi-ml.
  453. */
  454. if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
  455. sc->scsi_done(sc);
  456. }
  457. }
  458. void __iscsi_get_task(struct iscsi_task *task)
  459. {
  460. atomic_inc(&task->refcount);
  461. }
  462. EXPORT_SYMBOL_GPL(__iscsi_get_task);
  463. void __iscsi_put_task(struct iscsi_task *task)
  464. {
  465. if (atomic_dec_and_test(&task->refcount))
  466. iscsi_free_task(task);
  467. }
  468. EXPORT_SYMBOL_GPL(__iscsi_put_task);
  469. void iscsi_put_task(struct iscsi_task *task)
  470. {
  471. struct iscsi_session *session = task->conn->session;
  472. /* regular RX path uses back_lock */
  473. spin_lock_bh(&session->back_lock);
  474. __iscsi_put_task(task);
  475. spin_unlock_bh(&session->back_lock);
  476. }
  477. EXPORT_SYMBOL_GPL(iscsi_put_task);
  478. /**
  479. * iscsi_complete_task - finish a task
  480. * @task: iscsi cmd task
  481. * @state: state to complete task with
  482. *
  483. * Must be called with session back_lock.
  484. */
  485. static void iscsi_complete_task(struct iscsi_task *task, int state)
  486. {
  487. struct iscsi_conn *conn = task->conn;
  488. ISCSI_DBG_SESSION(conn->session,
  489. "complete task itt 0x%x state %d sc %p\n",
  490. task->itt, task->state, task->sc);
  491. if (task->state == ISCSI_TASK_COMPLETED ||
  492. task->state == ISCSI_TASK_ABRT_TMF ||
  493. task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
  494. task->state == ISCSI_TASK_REQUEUE_SCSIQ)
  495. return;
  496. WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
  497. task->state = state;
  498. spin_lock_bh(&conn->taskqueuelock);
  499. if (!list_empty(&task->running)) {
  500. pr_debug_once("%s while task on list", __func__);
  501. list_del_init(&task->running);
  502. }
  503. spin_unlock_bh(&conn->taskqueuelock);
  504. if (conn->task == task)
  505. conn->task = NULL;
  506. if (conn->ping_task == task)
  507. conn->ping_task = NULL;
  508. /* release get from queueing */
  509. __iscsi_put_task(task);
  510. }
  511. /**
  512. * iscsi_complete_scsi_task - finish scsi task normally
  513. * @task: iscsi task for scsi cmd
  514. * @exp_cmdsn: expected cmd sn in cpu format
  515. * @max_cmdsn: max cmd sn in cpu format
  516. *
  517. * This is used when drivers do not need or cannot perform
  518. * lower level pdu processing.
  519. *
  520. * Called with session back_lock
  521. */
  522. void iscsi_complete_scsi_task(struct iscsi_task *task,
  523. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  524. {
  525. struct iscsi_conn *conn = task->conn;
  526. ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
  527. conn->last_recv = jiffies;
  528. __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
  529. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  530. }
  531. EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
  532. /*
  533. * session back_lock must be held and if not called for a task that is
  534. * still pending or from the xmit thread, then xmit thread must
  535. * be suspended.
  536. */
  537. static void fail_scsi_task(struct iscsi_task *task, int err)
  538. {
  539. struct iscsi_conn *conn = task->conn;
  540. struct scsi_cmnd *sc;
  541. int state;
  542. /*
  543. * if a command completes and we get a successful tmf response
  544. * we will hit this because the scsi eh abort code does not take
  545. * a ref to the task.
  546. */
  547. sc = task->sc;
  548. if (!sc)
  549. return;
  550. if (task->state == ISCSI_TASK_PENDING) {
  551. /*
  552. * cmd never made it to the xmit thread, so we should not count
  553. * the cmd in the sequencing
  554. */
  555. conn->session->queued_cmdsn--;
  556. /* it was never sent so just complete like normal */
  557. state = ISCSI_TASK_COMPLETED;
  558. } else if (err == DID_TRANSPORT_DISRUPTED)
  559. state = ISCSI_TASK_ABRT_SESS_RECOV;
  560. else
  561. state = ISCSI_TASK_ABRT_TMF;
  562. sc->result = err << 16;
  563. if (!scsi_bidi_cmnd(sc))
  564. scsi_set_resid(sc, scsi_bufflen(sc));
  565. else {
  566. scsi_out(sc)->resid = scsi_out(sc)->length;
  567. scsi_in(sc)->resid = scsi_in(sc)->length;
  568. }
  569. /* regular RX path uses back_lock */
  570. spin_lock_bh(&conn->session->back_lock);
  571. iscsi_complete_task(task, state);
  572. spin_unlock_bh(&conn->session->back_lock);
  573. }
  574. static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
  575. struct iscsi_task *task)
  576. {
  577. struct iscsi_session *session = conn->session;
  578. struct iscsi_hdr *hdr = task->hdr;
  579. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  580. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  581. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  582. return -ENOTCONN;
  583. if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
  584. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  585. /*
  586. * pre-format CmdSN for outgoing PDU.
  587. */
  588. nop->cmdsn = cpu_to_be32(session->cmdsn);
  589. if (hdr->itt != RESERVED_ITT) {
  590. /*
  591. * TODO: We always use immediate for normal session pdus.
  592. * If we start to send tmfs or nops as non-immediate then
  593. * we should start checking the cmdsn numbers for mgmt tasks.
  594. *
  595. * During discovery sessions iscsid sends TEXT as non immediate,
  596. * but we always only send one PDU at a time.
  597. */
  598. if (conn->c_stage == ISCSI_CONN_STARTED &&
  599. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  600. session->queued_cmdsn++;
  601. session->cmdsn++;
  602. }
  603. }
  604. if (session->tt->init_task && session->tt->init_task(task))
  605. return -EIO;
  606. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  607. session->state = ISCSI_STATE_LOGGING_OUT;
  608. task->state = ISCSI_TASK_RUNNING;
  609. ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
  610. "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
  611. hdr->itt, task->data_count);
  612. return 0;
  613. }
  614. static struct iscsi_task *
  615. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  616. char *data, uint32_t data_size)
  617. {
  618. struct iscsi_session *session = conn->session;
  619. struct iscsi_host *ihost = shost_priv(session->host);
  620. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  621. struct iscsi_task *task;
  622. itt_t itt;
  623. if (session->state == ISCSI_STATE_TERMINATE)
  624. return NULL;
  625. if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
  626. /*
  627. * Login and Text are sent serially, in
  628. * request-followed-by-response sequence.
  629. * Same task can be used. Same ITT must be used.
  630. * Note that login_task is preallocated at conn_create().
  631. */
  632. if (conn->login_task->state != ISCSI_TASK_FREE) {
  633. iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
  634. "progress. Cannot start new task.\n");
  635. return NULL;
  636. }
  637. if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
  638. iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN);
  639. return NULL;
  640. }
  641. task = conn->login_task;
  642. } else {
  643. if (session->state != ISCSI_STATE_LOGGED_IN)
  644. return NULL;
  645. if (data_size != 0) {
  646. iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
  647. return NULL;
  648. }
  649. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  650. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  651. if (!kfifo_out(&session->cmdpool.queue,
  652. (void*)&task, sizeof(void*)))
  653. return NULL;
  654. }
  655. /*
  656. * released in complete pdu for task we expect a response for, and
  657. * released by the lld when it has transmitted the task for
  658. * pdus we do not expect a response for.
  659. */
  660. atomic_set(&task->refcount, 1);
  661. task->conn = conn;
  662. task->sc = NULL;
  663. INIT_LIST_HEAD(&task->running);
  664. task->state = ISCSI_TASK_PENDING;
  665. if (data_size) {
  666. memcpy(task->data, data, data_size);
  667. task->data_count = data_size;
  668. } else
  669. task->data_count = 0;
  670. if (conn->session->tt->alloc_pdu) {
  671. if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
  672. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
  673. "pdu for mgmt task.\n");
  674. goto free_task;
  675. }
  676. }
  677. itt = task->hdr->itt;
  678. task->hdr_len = sizeof(struct iscsi_hdr);
  679. memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
  680. if (hdr->itt != RESERVED_ITT) {
  681. if (session->tt->parse_pdu_itt)
  682. task->hdr->itt = itt;
  683. else
  684. task->hdr->itt = build_itt(task->itt,
  685. task->conn->session->age);
  686. }
  687. if (!ihost->workq) {
  688. if (iscsi_prep_mgmt_task(conn, task))
  689. goto free_task;
  690. if (session->tt->xmit_task(task))
  691. goto free_task;
  692. } else {
  693. spin_lock_bh(&conn->taskqueuelock);
  694. list_add_tail(&task->running, &conn->mgmtqueue);
  695. spin_unlock_bh(&conn->taskqueuelock);
  696. iscsi_conn_queue_work(conn);
  697. }
  698. return task;
  699. free_task:
  700. /* regular RX path uses back_lock */
  701. spin_lock(&session->back_lock);
  702. __iscsi_put_task(task);
  703. spin_unlock(&session->back_lock);
  704. return NULL;
  705. }
  706. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  707. char *data, uint32_t data_size)
  708. {
  709. struct iscsi_conn *conn = cls_conn->dd_data;
  710. struct iscsi_session *session = conn->session;
  711. int err = 0;
  712. spin_lock_bh(&session->frwd_lock);
  713. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  714. err = -EPERM;
  715. spin_unlock_bh(&session->frwd_lock);
  716. return err;
  717. }
  718. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  719. /**
  720. * iscsi_cmd_rsp - SCSI Command Response processing
  721. * @conn: iscsi connection
  722. * @hdr: iscsi header
  723. * @task: scsi command task
  724. * @data: cmd data buffer
  725. * @datalen: len of buffer
  726. *
  727. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  728. * then completes the command and task.
  729. **/
  730. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  731. struct iscsi_task *task, char *data,
  732. int datalen)
  733. {
  734. struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
  735. struct iscsi_session *session = conn->session;
  736. struct scsi_cmnd *sc = task->sc;
  737. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  738. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  739. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  740. if (task->protected) {
  741. sector_t sector;
  742. u8 ascq;
  743. /**
  744. * Transports that didn't implement check_protection
  745. * callback but still published T10-PI support to scsi-mid
  746. * deserve this BUG_ON.
  747. **/
  748. BUG_ON(!session->tt->check_protection);
  749. ascq = session->tt->check_protection(task, &sector);
  750. if (ascq) {
  751. sc->result = DRIVER_SENSE << 24 |
  752. SAM_STAT_CHECK_CONDITION;
  753. scsi_build_sense_buffer(1, sc->sense_buffer,
  754. ILLEGAL_REQUEST, 0x10, ascq);
  755. scsi_set_sense_information(sc->sense_buffer,
  756. SCSI_SENSE_BUFFERSIZE,
  757. sector);
  758. goto out;
  759. }
  760. }
  761. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  762. sc->result = DID_ERROR << 16;
  763. goto out;
  764. }
  765. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  766. uint16_t senselen;
  767. if (datalen < 2) {
  768. invalid_datalen:
  769. iscsi_conn_printk(KERN_ERR, conn,
  770. "Got CHECK_CONDITION but invalid data "
  771. "buffer size of %d\n", datalen);
  772. sc->result = DID_BAD_TARGET << 16;
  773. goto out;
  774. }
  775. senselen = get_unaligned_be16(data);
  776. if (datalen < senselen)
  777. goto invalid_datalen;
  778. memcpy(sc->sense_buffer, data + 2,
  779. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  780. ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
  781. min_t(uint16_t, senselen,
  782. SCSI_SENSE_BUFFERSIZE));
  783. }
  784. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  785. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  786. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  787. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  788. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  789. res_count <= scsi_in(sc)->length))
  790. scsi_in(sc)->resid = res_count;
  791. else
  792. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  793. }
  794. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  795. ISCSI_FLAG_CMD_OVERFLOW)) {
  796. int res_count = be32_to_cpu(rhdr->residual_count);
  797. if (res_count > 0 &&
  798. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  799. res_count <= scsi_bufflen(sc)))
  800. /* write side for bidi or uni-io set_resid */
  801. scsi_set_resid(sc, res_count);
  802. else
  803. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  804. }
  805. out:
  806. ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
  807. sc, sc->result, task->itt);
  808. conn->scsirsp_pdus_cnt++;
  809. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  810. }
  811. /**
  812. * iscsi_data_in_rsp - SCSI Data-In Response processing
  813. * @conn: iscsi connection
  814. * @hdr: iscsi pdu
  815. * @task: scsi command task
  816. **/
  817. static void
  818. iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  819. struct iscsi_task *task)
  820. {
  821. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
  822. struct scsi_cmnd *sc = task->sc;
  823. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  824. return;
  825. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
  826. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  827. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  828. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  829. ISCSI_FLAG_DATA_OVERFLOW)) {
  830. int res_count = be32_to_cpu(rhdr->residual_count);
  831. if (res_count > 0 &&
  832. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  833. res_count <= scsi_in(sc)->length))
  834. scsi_in(sc)->resid = res_count;
  835. else
  836. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  837. }
  838. ISCSI_DBG_SESSION(conn->session, "data in with status done "
  839. "[sc %p res %d itt 0x%x]\n",
  840. sc, sc->result, task->itt);
  841. conn->scsirsp_pdus_cnt++;
  842. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  843. }
  844. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  845. {
  846. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  847. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  848. conn->tmfrsp_pdus_cnt++;
  849. if (conn->tmf_state != TMF_QUEUED)
  850. return;
  851. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  852. conn->tmf_state = TMF_SUCCESS;
  853. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  854. conn->tmf_state = TMF_NOT_FOUND;
  855. else
  856. conn->tmf_state = TMF_FAILED;
  857. wake_up(&conn->ehwait);
  858. }
  859. static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  860. {
  861. struct iscsi_nopout hdr;
  862. struct iscsi_task *task;
  863. if (!rhdr && conn->ping_task)
  864. return -EINVAL;
  865. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  866. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  867. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  868. if (rhdr) {
  869. hdr.lun = rhdr->lun;
  870. hdr.ttt = rhdr->ttt;
  871. hdr.itt = RESERVED_ITT;
  872. } else
  873. hdr.ttt = RESERVED_ITT;
  874. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  875. if (!task) {
  876. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  877. return -EIO;
  878. } else if (!rhdr) {
  879. /* only track our nops */
  880. conn->ping_task = task;
  881. conn->last_ping = jiffies;
  882. }
  883. return 0;
  884. }
  885. static int iscsi_nop_out_rsp(struct iscsi_task *task,
  886. struct iscsi_nopin *nop, char *data, int datalen)
  887. {
  888. struct iscsi_conn *conn = task->conn;
  889. int rc = 0;
  890. if (conn->ping_task != task) {
  891. /*
  892. * If this is not in response to one of our
  893. * nops then it must be from userspace.
  894. */
  895. if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
  896. data, datalen))
  897. rc = ISCSI_ERR_CONN_FAILED;
  898. } else
  899. mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
  900. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  901. return rc;
  902. }
  903. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  904. char *data, int datalen)
  905. {
  906. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  907. struct iscsi_hdr rejected_pdu;
  908. int opcode, rc = 0;
  909. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  910. if (ntoh24(reject->dlength) > datalen ||
  911. ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
  912. iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
  913. "pdu. Invalid data length (pdu dlength "
  914. "%u, datalen %d\n", ntoh24(reject->dlength),
  915. datalen);
  916. return ISCSI_ERR_PROTO;
  917. }
  918. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  919. opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
  920. switch (reject->reason) {
  921. case ISCSI_REASON_DATA_DIGEST_ERROR:
  922. iscsi_conn_printk(KERN_ERR, conn,
  923. "pdu (op 0x%x itt 0x%x) rejected "
  924. "due to DataDigest error.\n",
  925. opcode, rejected_pdu.itt);
  926. break;
  927. case ISCSI_REASON_IMM_CMD_REJECT:
  928. iscsi_conn_printk(KERN_ERR, conn,
  929. "pdu (op 0x%x itt 0x%x) rejected. Too many "
  930. "immediate commands.\n",
  931. opcode, rejected_pdu.itt);
  932. /*
  933. * We only send one TMF at a time so if the target could not
  934. * handle it, then it should get fixed (RFC mandates that
  935. * a target can handle one immediate TMF per conn).
  936. *
  937. * For nops-outs, we could have sent more than one if
  938. * the target is sending us lots of nop-ins
  939. */
  940. if (opcode != ISCSI_OP_NOOP_OUT)
  941. return 0;
  942. if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
  943. /*
  944. * nop-out in response to target's nop-out rejected.
  945. * Just resend.
  946. */
  947. /* In RX path we are under back lock */
  948. spin_unlock(&conn->session->back_lock);
  949. spin_lock(&conn->session->frwd_lock);
  950. iscsi_send_nopout(conn,
  951. (struct iscsi_nopin*)&rejected_pdu);
  952. spin_unlock(&conn->session->frwd_lock);
  953. spin_lock(&conn->session->back_lock);
  954. } else {
  955. struct iscsi_task *task;
  956. /*
  957. * Our nop as ping got dropped. We know the target
  958. * and transport are ok so just clean up
  959. */
  960. task = iscsi_itt_to_task(conn, rejected_pdu.itt);
  961. if (!task) {
  962. iscsi_conn_printk(KERN_ERR, conn,
  963. "Invalid pdu reject. Could "
  964. "not lookup rejected task.\n");
  965. rc = ISCSI_ERR_BAD_ITT;
  966. } else
  967. rc = iscsi_nop_out_rsp(task,
  968. (struct iscsi_nopin*)&rejected_pdu,
  969. NULL, 0);
  970. }
  971. break;
  972. default:
  973. iscsi_conn_printk(KERN_ERR, conn,
  974. "pdu (op 0x%x itt 0x%x) rejected. Reason "
  975. "code 0x%x\n", rejected_pdu.opcode,
  976. rejected_pdu.itt, reject->reason);
  977. break;
  978. }
  979. return rc;
  980. }
  981. /**
  982. * iscsi_itt_to_task - look up task by itt
  983. * @conn: iscsi connection
  984. * @itt: itt
  985. *
  986. * This should be used for mgmt tasks like login and nops, or if
  987. * the LDD's itt space does not include the session age.
  988. *
  989. * The session back_lock must be held.
  990. */
  991. struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
  992. {
  993. struct iscsi_session *session = conn->session;
  994. int i;
  995. if (itt == RESERVED_ITT)
  996. return NULL;
  997. if (session->tt->parse_pdu_itt)
  998. session->tt->parse_pdu_itt(conn, itt, &i, NULL);
  999. else
  1000. i = get_itt(itt);
  1001. if (i >= session->cmds_max)
  1002. return NULL;
  1003. return session->cmds[i];
  1004. }
  1005. EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
  1006. /**
  1007. * __iscsi_complete_pdu - complete pdu
  1008. * @conn: iscsi conn
  1009. * @hdr: iscsi header
  1010. * @data: data buffer
  1011. * @datalen: len of data buffer
  1012. *
  1013. * Completes pdu processing by freeing any resources allocated at
  1014. * queuecommand or send generic. session back_lock must be held and verify
  1015. * itt must have been called.
  1016. */
  1017. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1018. char *data, int datalen)
  1019. {
  1020. struct iscsi_session *session = conn->session;
  1021. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  1022. struct iscsi_task *task;
  1023. uint32_t itt;
  1024. conn->last_recv = jiffies;
  1025. rc = iscsi_verify_itt(conn, hdr->itt);
  1026. if (rc)
  1027. return rc;
  1028. if (hdr->itt != RESERVED_ITT)
  1029. itt = get_itt(hdr->itt);
  1030. else
  1031. itt = ~0U;
  1032. ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
  1033. opcode, conn->id, itt, datalen);
  1034. if (itt == ~0U) {
  1035. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1036. switch(opcode) {
  1037. case ISCSI_OP_NOOP_IN:
  1038. if (datalen) {
  1039. rc = ISCSI_ERR_PROTO;
  1040. break;
  1041. }
  1042. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  1043. break;
  1044. /* In RX path we are under back lock */
  1045. spin_unlock(&session->back_lock);
  1046. spin_lock(&session->frwd_lock);
  1047. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  1048. spin_unlock(&session->frwd_lock);
  1049. spin_lock(&session->back_lock);
  1050. break;
  1051. case ISCSI_OP_REJECT:
  1052. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  1053. break;
  1054. case ISCSI_OP_ASYNC_EVENT:
  1055. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1056. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1057. rc = ISCSI_ERR_CONN_FAILED;
  1058. break;
  1059. default:
  1060. rc = ISCSI_ERR_BAD_OPCODE;
  1061. break;
  1062. }
  1063. goto out;
  1064. }
  1065. switch(opcode) {
  1066. case ISCSI_OP_SCSI_CMD_RSP:
  1067. case ISCSI_OP_SCSI_DATA_IN:
  1068. task = iscsi_itt_to_ctask(conn, hdr->itt);
  1069. if (!task)
  1070. return ISCSI_ERR_BAD_ITT;
  1071. task->last_xfer = jiffies;
  1072. break;
  1073. case ISCSI_OP_R2T:
  1074. /*
  1075. * LLD handles R2Ts if they need to.
  1076. */
  1077. return 0;
  1078. case ISCSI_OP_LOGOUT_RSP:
  1079. case ISCSI_OP_LOGIN_RSP:
  1080. case ISCSI_OP_TEXT_RSP:
  1081. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1082. case ISCSI_OP_NOOP_IN:
  1083. task = iscsi_itt_to_task(conn, hdr->itt);
  1084. if (!task)
  1085. return ISCSI_ERR_BAD_ITT;
  1086. break;
  1087. default:
  1088. return ISCSI_ERR_BAD_OPCODE;
  1089. }
  1090. switch(opcode) {
  1091. case ISCSI_OP_SCSI_CMD_RSP:
  1092. iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
  1093. break;
  1094. case ISCSI_OP_SCSI_DATA_IN:
  1095. iscsi_data_in_rsp(conn, hdr, task);
  1096. break;
  1097. case ISCSI_OP_LOGOUT_RSP:
  1098. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1099. if (datalen) {
  1100. rc = ISCSI_ERR_PROTO;
  1101. break;
  1102. }
  1103. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1104. goto recv_pdu;
  1105. case ISCSI_OP_LOGIN_RSP:
  1106. case ISCSI_OP_TEXT_RSP:
  1107. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1108. /*
  1109. * login related PDU's exp_statsn is handled in
  1110. * userspace
  1111. */
  1112. goto recv_pdu;
  1113. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1114. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1115. if (datalen) {
  1116. rc = ISCSI_ERR_PROTO;
  1117. break;
  1118. }
  1119. iscsi_tmf_rsp(conn, hdr);
  1120. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1121. break;
  1122. case ISCSI_OP_NOOP_IN:
  1123. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1124. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  1125. rc = ISCSI_ERR_PROTO;
  1126. break;
  1127. }
  1128. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1129. rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
  1130. data, datalen);
  1131. break;
  1132. default:
  1133. rc = ISCSI_ERR_BAD_OPCODE;
  1134. break;
  1135. }
  1136. out:
  1137. return rc;
  1138. recv_pdu:
  1139. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1140. rc = ISCSI_ERR_CONN_FAILED;
  1141. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1142. return rc;
  1143. }
  1144. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  1145. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1146. char *data, int datalen)
  1147. {
  1148. int rc;
  1149. spin_lock(&conn->session->back_lock);
  1150. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  1151. spin_unlock(&conn->session->back_lock);
  1152. return rc;
  1153. }
  1154. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  1155. int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
  1156. {
  1157. struct iscsi_session *session = conn->session;
  1158. int age = 0, i = 0;
  1159. if (itt == RESERVED_ITT)
  1160. return 0;
  1161. if (session->tt->parse_pdu_itt)
  1162. session->tt->parse_pdu_itt(conn, itt, &i, &age);
  1163. else {
  1164. i = get_itt(itt);
  1165. age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
  1166. }
  1167. if (age != session->age) {
  1168. iscsi_conn_printk(KERN_ERR, conn,
  1169. "received itt %x expected session age (%x)\n",
  1170. (__force u32)itt, session->age);
  1171. return ISCSI_ERR_BAD_ITT;
  1172. }
  1173. if (i >= session->cmds_max) {
  1174. iscsi_conn_printk(KERN_ERR, conn,
  1175. "received invalid itt index %u (max cmds "
  1176. "%u.\n", i, session->cmds_max);
  1177. return ISCSI_ERR_BAD_ITT;
  1178. }
  1179. return 0;
  1180. }
  1181. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  1182. /**
  1183. * iscsi_itt_to_ctask - look up ctask by itt
  1184. * @conn: iscsi connection
  1185. * @itt: itt
  1186. *
  1187. * This should be used for cmd tasks.
  1188. *
  1189. * The session back_lock must be held.
  1190. */
  1191. struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
  1192. {
  1193. struct iscsi_task *task;
  1194. if (iscsi_verify_itt(conn, itt))
  1195. return NULL;
  1196. task = iscsi_itt_to_task(conn, itt);
  1197. if (!task || !task->sc)
  1198. return NULL;
  1199. if (task->sc->SCp.phase != conn->session->age) {
  1200. iscsi_session_printk(KERN_ERR, conn->session,
  1201. "task's session age %d, expected %d\n",
  1202. task->sc->SCp.phase, conn->session->age);
  1203. return NULL;
  1204. }
  1205. return task;
  1206. }
  1207. EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
  1208. void iscsi_session_failure(struct iscsi_session *session,
  1209. enum iscsi_err err)
  1210. {
  1211. struct iscsi_conn *conn;
  1212. struct device *dev;
  1213. spin_lock_bh(&session->frwd_lock);
  1214. conn = session->leadconn;
  1215. if (session->state == ISCSI_STATE_TERMINATE || !conn) {
  1216. spin_unlock_bh(&session->frwd_lock);
  1217. return;
  1218. }
  1219. dev = get_device(&conn->cls_conn->dev);
  1220. spin_unlock_bh(&session->frwd_lock);
  1221. if (!dev)
  1222. return;
  1223. /*
  1224. * if the host is being removed bypass the connection
  1225. * recovery initialization because we are going to kill
  1226. * the session.
  1227. */
  1228. if (err == ISCSI_ERR_INVALID_HOST)
  1229. iscsi_conn_error_event(conn->cls_conn, err);
  1230. else
  1231. iscsi_conn_failure(conn, err);
  1232. put_device(dev);
  1233. }
  1234. EXPORT_SYMBOL_GPL(iscsi_session_failure);
  1235. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  1236. {
  1237. struct iscsi_session *session = conn->session;
  1238. spin_lock_bh(&session->frwd_lock);
  1239. if (session->state == ISCSI_STATE_FAILED) {
  1240. spin_unlock_bh(&session->frwd_lock);
  1241. return;
  1242. }
  1243. if (conn->stop_stage == 0)
  1244. session->state = ISCSI_STATE_FAILED;
  1245. spin_unlock_bh(&session->frwd_lock);
  1246. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1247. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1248. iscsi_conn_error_event(conn->cls_conn, err);
  1249. }
  1250. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  1251. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  1252. {
  1253. struct iscsi_session *session = conn->session;
  1254. /*
  1255. * Check for iSCSI window and take care of CmdSN wrap-around
  1256. */
  1257. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  1258. ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
  1259. "%u MaxCmdSN %u CmdSN %u/%u\n",
  1260. session->exp_cmdsn, session->max_cmdsn,
  1261. session->cmdsn, session->queued_cmdsn);
  1262. return -ENOSPC;
  1263. }
  1264. return 0;
  1265. }
  1266. static int iscsi_xmit_task(struct iscsi_conn *conn)
  1267. {
  1268. struct iscsi_task *task = conn->task;
  1269. int rc;
  1270. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
  1271. return -ENODATA;
  1272. __iscsi_get_task(task);
  1273. spin_unlock_bh(&conn->session->frwd_lock);
  1274. rc = conn->session->tt->xmit_task(task);
  1275. spin_lock_bh(&conn->session->frwd_lock);
  1276. if (!rc) {
  1277. /* done with this task */
  1278. task->last_xfer = jiffies;
  1279. conn->task = NULL;
  1280. }
  1281. /* regular RX path uses back_lock */
  1282. spin_lock(&conn->session->back_lock);
  1283. __iscsi_put_task(task);
  1284. spin_unlock(&conn->session->back_lock);
  1285. return rc;
  1286. }
  1287. /**
  1288. * iscsi_requeue_task - requeue task to run from session workqueue
  1289. * @task: task to requeue
  1290. *
  1291. * LLDs that need to run a task from the session workqueue should call
  1292. * this. The session frwd_lock must be held. This should only be called
  1293. * by software drivers.
  1294. */
  1295. void iscsi_requeue_task(struct iscsi_task *task)
  1296. {
  1297. struct iscsi_conn *conn = task->conn;
  1298. /*
  1299. * this may be on the requeue list already if the xmit_task callout
  1300. * is handling the r2ts while we are adding new ones
  1301. */
  1302. spin_lock_bh(&conn->taskqueuelock);
  1303. if (list_empty(&task->running))
  1304. list_add_tail(&task->running, &conn->requeue);
  1305. spin_unlock_bh(&conn->taskqueuelock);
  1306. iscsi_conn_queue_work(conn);
  1307. }
  1308. EXPORT_SYMBOL_GPL(iscsi_requeue_task);
  1309. /**
  1310. * iscsi_data_xmit - xmit any command into the scheduled connection
  1311. * @conn: iscsi connection
  1312. *
  1313. * Notes:
  1314. * The function can return -EAGAIN in which case the caller must
  1315. * re-schedule it again later or recover. '0' return code means
  1316. * successful xmit.
  1317. **/
  1318. static int iscsi_data_xmit(struct iscsi_conn *conn)
  1319. {
  1320. struct iscsi_task *task;
  1321. int rc = 0;
  1322. spin_lock_bh(&conn->session->frwd_lock);
  1323. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1324. ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
  1325. spin_unlock_bh(&conn->session->frwd_lock);
  1326. return -ENODATA;
  1327. }
  1328. if (conn->task) {
  1329. rc = iscsi_xmit_task(conn);
  1330. if (rc)
  1331. goto done;
  1332. }
  1333. /*
  1334. * process mgmt pdus like nops before commands since we should
  1335. * only have one nop-out as a ping from us and targets should not
  1336. * overflow us with nop-ins
  1337. */
  1338. spin_lock_bh(&conn->taskqueuelock);
  1339. check_mgmt:
  1340. while (!list_empty(&conn->mgmtqueue)) {
  1341. conn->task = list_entry(conn->mgmtqueue.next,
  1342. struct iscsi_task, running);
  1343. list_del_init(&conn->task->running);
  1344. spin_unlock_bh(&conn->taskqueuelock);
  1345. if (iscsi_prep_mgmt_task(conn, conn->task)) {
  1346. /* regular RX path uses back_lock */
  1347. spin_lock_bh(&conn->session->back_lock);
  1348. __iscsi_put_task(conn->task);
  1349. spin_unlock_bh(&conn->session->back_lock);
  1350. conn->task = NULL;
  1351. spin_lock_bh(&conn->taskqueuelock);
  1352. continue;
  1353. }
  1354. rc = iscsi_xmit_task(conn);
  1355. if (rc)
  1356. goto done;
  1357. spin_lock_bh(&conn->taskqueuelock);
  1358. }
  1359. /* process pending command queue */
  1360. while (!list_empty(&conn->cmdqueue)) {
  1361. conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
  1362. running);
  1363. list_del_init(&conn->task->running);
  1364. spin_unlock_bh(&conn->taskqueuelock);
  1365. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  1366. fail_scsi_task(conn->task, DID_IMM_RETRY);
  1367. spin_lock_bh(&conn->taskqueuelock);
  1368. continue;
  1369. }
  1370. rc = iscsi_prep_scsi_cmd_pdu(conn->task);
  1371. if (rc) {
  1372. if (rc == -ENOMEM || rc == -EACCES) {
  1373. spin_lock_bh(&conn->taskqueuelock);
  1374. list_add_tail(&conn->task->running,
  1375. &conn->cmdqueue);
  1376. conn->task = NULL;
  1377. spin_unlock_bh(&conn->taskqueuelock);
  1378. goto done;
  1379. } else
  1380. fail_scsi_task(conn->task, DID_ABORT);
  1381. spin_lock_bh(&conn->taskqueuelock);
  1382. continue;
  1383. }
  1384. rc = iscsi_xmit_task(conn);
  1385. if (rc)
  1386. goto done;
  1387. /*
  1388. * we could continuously get new task requests so
  1389. * we need to check the mgmt queue for nops that need to
  1390. * be sent to aviod starvation
  1391. */
  1392. spin_lock_bh(&conn->taskqueuelock);
  1393. if (!list_empty(&conn->mgmtqueue))
  1394. goto check_mgmt;
  1395. }
  1396. while (!list_empty(&conn->requeue)) {
  1397. /*
  1398. * we always do fastlogout - conn stop code will clean up.
  1399. */
  1400. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  1401. break;
  1402. task = list_entry(conn->requeue.next, struct iscsi_task,
  1403. running);
  1404. if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
  1405. break;
  1406. conn->task = task;
  1407. list_del_init(&conn->task->running);
  1408. conn->task->state = ISCSI_TASK_RUNNING;
  1409. spin_unlock_bh(&conn->taskqueuelock);
  1410. rc = iscsi_xmit_task(conn);
  1411. if (rc)
  1412. goto done;
  1413. spin_lock_bh(&conn->taskqueuelock);
  1414. if (!list_empty(&conn->mgmtqueue))
  1415. goto check_mgmt;
  1416. }
  1417. spin_unlock_bh(&conn->taskqueuelock);
  1418. spin_unlock_bh(&conn->session->frwd_lock);
  1419. return -ENODATA;
  1420. done:
  1421. spin_unlock_bh(&conn->session->frwd_lock);
  1422. return rc;
  1423. }
  1424. static void iscsi_xmitworker(struct work_struct *work)
  1425. {
  1426. struct iscsi_conn *conn =
  1427. container_of(work, struct iscsi_conn, xmitwork);
  1428. int rc;
  1429. /*
  1430. * serialize Xmit worker on a per-connection basis.
  1431. */
  1432. do {
  1433. rc = iscsi_data_xmit(conn);
  1434. } while (rc >= 0 || rc == -EAGAIN);
  1435. }
  1436. static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
  1437. struct scsi_cmnd *sc)
  1438. {
  1439. struct iscsi_task *task;
  1440. if (!kfifo_out(&conn->session->cmdpool.queue,
  1441. (void *) &task, sizeof(void *)))
  1442. return NULL;
  1443. sc->SCp.phase = conn->session->age;
  1444. sc->SCp.ptr = (char *) task;
  1445. atomic_set(&task->refcount, 1);
  1446. task->state = ISCSI_TASK_PENDING;
  1447. task->conn = conn;
  1448. task->sc = sc;
  1449. task->have_checked_conn = false;
  1450. task->last_timeout = jiffies;
  1451. task->last_xfer = jiffies;
  1452. task->protected = false;
  1453. INIT_LIST_HEAD(&task->running);
  1454. return task;
  1455. }
  1456. enum {
  1457. FAILURE_BAD_HOST = 1,
  1458. FAILURE_SESSION_FAILED,
  1459. FAILURE_SESSION_FREED,
  1460. FAILURE_WINDOW_CLOSED,
  1461. FAILURE_OOM,
  1462. FAILURE_SESSION_TERMINATE,
  1463. FAILURE_SESSION_IN_RECOVERY,
  1464. FAILURE_SESSION_RECOVERY_TIMEOUT,
  1465. FAILURE_SESSION_LOGGING_OUT,
  1466. FAILURE_SESSION_NOT_READY,
  1467. };
  1468. int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
  1469. {
  1470. struct iscsi_cls_session *cls_session;
  1471. struct iscsi_host *ihost;
  1472. int reason = 0;
  1473. struct iscsi_session *session;
  1474. struct iscsi_conn *conn;
  1475. struct iscsi_task *task = NULL;
  1476. sc->result = 0;
  1477. sc->SCp.ptr = NULL;
  1478. ihost = shost_priv(host);
  1479. cls_session = starget_to_session(scsi_target(sc->device));
  1480. session = cls_session->dd_data;
  1481. spin_lock_bh(&session->frwd_lock);
  1482. reason = iscsi_session_chkready(cls_session);
  1483. if (reason) {
  1484. sc->result = reason;
  1485. goto fault;
  1486. }
  1487. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1488. /*
  1489. * to handle the race between when we set the recovery state
  1490. * and block the session we requeue here (commands could
  1491. * be entering our queuecommand while a block is starting
  1492. * up because the block code is not locked)
  1493. */
  1494. switch (session->state) {
  1495. case ISCSI_STATE_FAILED:
  1496. /*
  1497. * cmds should fail during shutdown, if the session
  1498. * state is bad, allowing completion to happen
  1499. */
  1500. if (unlikely(system_state != SYSTEM_RUNNING)) {
  1501. reason = FAILURE_SESSION_FAILED;
  1502. sc->result = DID_NO_CONNECT << 16;
  1503. break;
  1504. }
  1505. case ISCSI_STATE_IN_RECOVERY:
  1506. reason = FAILURE_SESSION_IN_RECOVERY;
  1507. sc->result = DID_IMM_RETRY << 16;
  1508. break;
  1509. case ISCSI_STATE_LOGGING_OUT:
  1510. reason = FAILURE_SESSION_LOGGING_OUT;
  1511. sc->result = DID_IMM_RETRY << 16;
  1512. break;
  1513. case ISCSI_STATE_RECOVERY_FAILED:
  1514. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1515. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1516. break;
  1517. case ISCSI_STATE_TERMINATE:
  1518. reason = FAILURE_SESSION_TERMINATE;
  1519. sc->result = DID_NO_CONNECT << 16;
  1520. break;
  1521. default:
  1522. reason = FAILURE_SESSION_FREED;
  1523. sc->result = DID_NO_CONNECT << 16;
  1524. }
  1525. goto fault;
  1526. }
  1527. conn = session->leadconn;
  1528. if (!conn) {
  1529. reason = FAILURE_SESSION_FREED;
  1530. sc->result = DID_NO_CONNECT << 16;
  1531. goto fault;
  1532. }
  1533. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1534. reason = FAILURE_SESSION_IN_RECOVERY;
  1535. sc->result = DID_REQUEUE << 16;
  1536. goto fault;
  1537. }
  1538. if (iscsi_check_cmdsn_window_closed(conn)) {
  1539. reason = FAILURE_WINDOW_CLOSED;
  1540. goto reject;
  1541. }
  1542. task = iscsi_alloc_task(conn, sc);
  1543. if (!task) {
  1544. reason = FAILURE_OOM;
  1545. goto reject;
  1546. }
  1547. if (!ihost->workq) {
  1548. reason = iscsi_prep_scsi_cmd_pdu(task);
  1549. if (reason) {
  1550. if (reason == -ENOMEM || reason == -EACCES) {
  1551. reason = FAILURE_OOM;
  1552. goto prepd_reject;
  1553. } else {
  1554. sc->result = DID_ABORT << 16;
  1555. goto prepd_fault;
  1556. }
  1557. }
  1558. if (session->tt->xmit_task(task)) {
  1559. session->cmdsn--;
  1560. reason = FAILURE_SESSION_NOT_READY;
  1561. goto prepd_reject;
  1562. }
  1563. } else {
  1564. spin_lock_bh(&conn->taskqueuelock);
  1565. list_add_tail(&task->running, &conn->cmdqueue);
  1566. spin_unlock_bh(&conn->taskqueuelock);
  1567. iscsi_conn_queue_work(conn);
  1568. }
  1569. session->queued_cmdsn++;
  1570. spin_unlock_bh(&session->frwd_lock);
  1571. return 0;
  1572. prepd_reject:
  1573. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1574. reject:
  1575. spin_unlock_bh(&session->frwd_lock);
  1576. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1577. sc->cmnd[0], reason);
  1578. return SCSI_MLQUEUE_TARGET_BUSY;
  1579. prepd_fault:
  1580. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1581. fault:
  1582. spin_unlock_bh(&session->frwd_lock);
  1583. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1584. sc->cmnd[0], reason);
  1585. if (!scsi_bidi_cmnd(sc))
  1586. scsi_set_resid(sc, scsi_bufflen(sc));
  1587. else {
  1588. scsi_out(sc)->resid = scsi_out(sc)->length;
  1589. scsi_in(sc)->resid = scsi_in(sc)->length;
  1590. }
  1591. sc->scsi_done(sc);
  1592. return 0;
  1593. }
  1594. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1595. int iscsi_target_alloc(struct scsi_target *starget)
  1596. {
  1597. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1598. struct iscsi_session *session = cls_session->dd_data;
  1599. starget->can_queue = session->scsi_cmds_max;
  1600. return 0;
  1601. }
  1602. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1603. static void iscsi_tmf_timedout(unsigned long data)
  1604. {
  1605. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1606. struct iscsi_session *session = conn->session;
  1607. spin_lock(&session->frwd_lock);
  1608. if (conn->tmf_state == TMF_QUEUED) {
  1609. conn->tmf_state = TMF_TIMEDOUT;
  1610. ISCSI_DBG_EH(session, "tmf timedout\n");
  1611. /* unblock eh_abort() */
  1612. wake_up(&conn->ehwait);
  1613. }
  1614. spin_unlock(&session->frwd_lock);
  1615. }
  1616. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1617. struct iscsi_tm *hdr, int age,
  1618. int timeout)
  1619. {
  1620. struct iscsi_session *session = conn->session;
  1621. struct iscsi_task *task;
  1622. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1623. NULL, 0);
  1624. if (!task) {
  1625. spin_unlock_bh(&session->frwd_lock);
  1626. iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
  1627. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1628. spin_lock_bh(&session->frwd_lock);
  1629. return -EPERM;
  1630. }
  1631. conn->tmfcmd_pdus_cnt++;
  1632. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1633. conn->tmf_timer.function = iscsi_tmf_timedout;
  1634. conn->tmf_timer.data = (unsigned long)conn;
  1635. add_timer(&conn->tmf_timer);
  1636. ISCSI_DBG_EH(session, "tmf set timeout\n");
  1637. spin_unlock_bh(&session->frwd_lock);
  1638. mutex_unlock(&session->eh_mutex);
  1639. /*
  1640. * block eh thread until:
  1641. *
  1642. * 1) tmf response
  1643. * 2) tmf timeout
  1644. * 3) session is terminated or restarted or userspace has
  1645. * given up on recovery
  1646. */
  1647. wait_event_interruptible(conn->ehwait, age != session->age ||
  1648. session->state != ISCSI_STATE_LOGGED_IN ||
  1649. conn->tmf_state != TMF_QUEUED);
  1650. if (signal_pending(current))
  1651. flush_signals(current);
  1652. del_timer_sync(&conn->tmf_timer);
  1653. mutex_lock(&session->eh_mutex);
  1654. spin_lock_bh(&session->frwd_lock);
  1655. /* if the session drops it will clean up the task */
  1656. if (age != session->age ||
  1657. session->state != ISCSI_STATE_LOGGED_IN)
  1658. return -ENOTCONN;
  1659. return 0;
  1660. }
  1661. /*
  1662. * Fail commands. session lock held and recv side suspended and xmit
  1663. * thread flushed
  1664. */
  1665. static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
  1666. {
  1667. struct iscsi_task *task;
  1668. int i;
  1669. for (i = 0; i < conn->session->cmds_max; i++) {
  1670. task = conn->session->cmds[i];
  1671. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1672. continue;
  1673. if (lun != -1 && lun != task->sc->device->lun)
  1674. continue;
  1675. ISCSI_DBG_SESSION(conn->session,
  1676. "failing sc %p itt 0x%x state %d\n",
  1677. task->sc, task->itt, task->state);
  1678. fail_scsi_task(task, error);
  1679. }
  1680. }
  1681. /**
  1682. * iscsi_suspend_queue - suspend iscsi_queuecommand
  1683. * @conn: iscsi conn to stop queueing IO on
  1684. *
  1685. * This grabs the session frwd_lock to make sure no one is in
  1686. * xmit_task/queuecommand, and then sets suspend to prevent
  1687. * new commands from being queued. This only needs to be called
  1688. * by offload drivers that need to sync a path like ep disconnect
  1689. * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
  1690. * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
  1691. */
  1692. void iscsi_suspend_queue(struct iscsi_conn *conn)
  1693. {
  1694. spin_lock_bh(&conn->session->frwd_lock);
  1695. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1696. spin_unlock_bh(&conn->session->frwd_lock);
  1697. }
  1698. EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
  1699. /**
  1700. * iscsi_suspend_tx - suspend iscsi_data_xmit
  1701. * @conn: iscsi conn tp stop processing IO on.
  1702. *
  1703. * This function sets the suspend bit to prevent iscsi_data_xmit
  1704. * from sending new IO, and if work is queued on the xmit thread
  1705. * it will wait for it to be completed.
  1706. */
  1707. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1708. {
  1709. struct Scsi_Host *shost = conn->session->host;
  1710. struct iscsi_host *ihost = shost_priv(shost);
  1711. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1712. if (ihost->workq)
  1713. flush_workqueue(ihost->workq);
  1714. }
  1715. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1716. static void iscsi_start_tx(struct iscsi_conn *conn)
  1717. {
  1718. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1719. iscsi_conn_queue_work(conn);
  1720. }
  1721. /*
  1722. * We want to make sure a ping is in flight. It has timed out.
  1723. * And we are not busy processing a pdu that is making
  1724. * progress but got started before the ping and is taking a while
  1725. * to complete so the ping is just stuck behind it in a queue.
  1726. */
  1727. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1728. {
  1729. if (conn->ping_task &&
  1730. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1731. (conn->ping_timeout * HZ), jiffies))
  1732. return 1;
  1733. else
  1734. return 0;
  1735. }
  1736. static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
  1737. {
  1738. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1739. struct iscsi_task *task = NULL, *running_task;
  1740. struct iscsi_cls_session *cls_session;
  1741. struct iscsi_session *session;
  1742. struct iscsi_conn *conn;
  1743. int i;
  1744. cls_session = starget_to_session(scsi_target(sc->device));
  1745. session = cls_session->dd_data;
  1746. ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
  1747. spin_lock(&session->frwd_lock);
  1748. task = (struct iscsi_task *)sc->SCp.ptr;
  1749. if (!task) {
  1750. /*
  1751. * Raced with completion. Blk layer has taken ownership
  1752. * so let timeout code complete it now.
  1753. */
  1754. rc = BLK_EH_HANDLED;
  1755. goto done;
  1756. }
  1757. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1758. /*
  1759. * During shutdown, if session is prematurely disconnected,
  1760. * recovery won't happen and there will be hung cmds. Not
  1761. * handling cmds would trigger EH, also bad in this case.
  1762. * Instead, handle cmd, allow completion to happen and let
  1763. * upper layer to deal with the result.
  1764. */
  1765. if (unlikely(system_state != SYSTEM_RUNNING)) {
  1766. sc->result = DID_NO_CONNECT << 16;
  1767. ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
  1768. rc = BLK_EH_HANDLED;
  1769. goto done;
  1770. }
  1771. /*
  1772. * We are probably in the middle of iscsi recovery so let
  1773. * that complete and handle the error.
  1774. */
  1775. rc = BLK_EH_RESET_TIMER;
  1776. goto done;
  1777. }
  1778. conn = session->leadconn;
  1779. if (!conn) {
  1780. /* In the middle of shuting down */
  1781. rc = BLK_EH_RESET_TIMER;
  1782. goto done;
  1783. }
  1784. /*
  1785. * If we have sent (at least queued to the network layer) a pdu or
  1786. * recvd one for the task since the last timeout ask for
  1787. * more time. If on the next timeout we have not made progress
  1788. * we can check if it is the task or connection when we send the
  1789. * nop as a ping.
  1790. */
  1791. if (time_after(task->last_xfer, task->last_timeout)) {
  1792. ISCSI_DBG_EH(session, "Command making progress. Asking "
  1793. "scsi-ml for more time to complete. "
  1794. "Last data xfer at %lu. Last timeout was at "
  1795. "%lu\n.", task->last_xfer, task->last_timeout);
  1796. task->have_checked_conn = false;
  1797. rc = BLK_EH_RESET_TIMER;
  1798. goto done;
  1799. }
  1800. if (!conn->recv_timeout && !conn->ping_timeout)
  1801. goto done;
  1802. /*
  1803. * if the ping timedout then we are in the middle of cleaning up
  1804. * and can let the iscsi eh handle it
  1805. */
  1806. if (iscsi_has_ping_timed_out(conn)) {
  1807. rc = BLK_EH_RESET_TIMER;
  1808. goto done;
  1809. }
  1810. for (i = 0; i < conn->session->cmds_max; i++) {
  1811. running_task = conn->session->cmds[i];
  1812. if (!running_task->sc || running_task == task ||
  1813. running_task->state != ISCSI_TASK_RUNNING)
  1814. continue;
  1815. /*
  1816. * Only check if cmds started before this one have made
  1817. * progress, or this could never fail
  1818. */
  1819. if (time_after(running_task->sc->jiffies_at_alloc,
  1820. task->sc->jiffies_at_alloc))
  1821. continue;
  1822. if (time_after(running_task->last_xfer, task->last_timeout)) {
  1823. /*
  1824. * This task has not made progress, but a task
  1825. * started before us has transferred data since
  1826. * we started/last-checked. We could be queueing
  1827. * too many tasks or the LU is bad.
  1828. *
  1829. * If the device is bad the cmds ahead of us on
  1830. * other devs will complete, and this loop will
  1831. * eventually fail starting the scsi eh.
  1832. */
  1833. ISCSI_DBG_EH(session, "Command has not made progress "
  1834. "but commands ahead of it have. "
  1835. "Asking scsi-ml for more time to "
  1836. "complete. Our last xfer vs running task "
  1837. "last xfer %lu/%lu. Last check %lu.\n",
  1838. task->last_xfer, running_task->last_xfer,
  1839. task->last_timeout);
  1840. rc = BLK_EH_RESET_TIMER;
  1841. goto done;
  1842. }
  1843. }
  1844. /* Assumes nop timeout is shorter than scsi cmd timeout */
  1845. if (task->have_checked_conn)
  1846. goto done;
  1847. /*
  1848. * Checking the transport already or nop from a cmd timeout still
  1849. * running
  1850. */
  1851. if (conn->ping_task) {
  1852. task->have_checked_conn = true;
  1853. rc = BLK_EH_RESET_TIMER;
  1854. goto done;
  1855. }
  1856. /* Make sure there is a transport check done */
  1857. iscsi_send_nopout(conn, NULL);
  1858. task->have_checked_conn = true;
  1859. rc = BLK_EH_RESET_TIMER;
  1860. done:
  1861. if (task)
  1862. task->last_timeout = jiffies;
  1863. spin_unlock(&session->frwd_lock);
  1864. ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
  1865. "timer reset" : "shutdown or nh");
  1866. return rc;
  1867. }
  1868. static void iscsi_check_transport_timeouts(unsigned long data)
  1869. {
  1870. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1871. struct iscsi_session *session = conn->session;
  1872. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1873. spin_lock(&session->frwd_lock);
  1874. if (session->state != ISCSI_STATE_LOGGED_IN)
  1875. goto done;
  1876. recv_timeout = conn->recv_timeout;
  1877. if (!recv_timeout)
  1878. goto done;
  1879. recv_timeout *= HZ;
  1880. last_recv = conn->last_recv;
  1881. if (iscsi_has_ping_timed_out(conn)) {
  1882. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1883. "expired, recv timeout %d, last rx %lu, "
  1884. "last ping %lu, now %lu\n",
  1885. conn->ping_timeout, conn->recv_timeout,
  1886. last_recv, conn->last_ping, jiffies);
  1887. spin_unlock(&session->frwd_lock);
  1888. iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
  1889. return;
  1890. }
  1891. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1892. /* send a ping to try to provoke some traffic */
  1893. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  1894. if (iscsi_send_nopout(conn, NULL))
  1895. next_timeout = jiffies + (1 * HZ);
  1896. else
  1897. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1898. } else
  1899. next_timeout = last_recv + recv_timeout;
  1900. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  1901. mod_timer(&conn->transport_timer, next_timeout);
  1902. done:
  1903. spin_unlock(&session->frwd_lock);
  1904. }
  1905. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  1906. struct iscsi_tm *hdr)
  1907. {
  1908. memset(hdr, 0, sizeof(*hdr));
  1909. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1910. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1911. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1912. hdr->lun = task->lun;
  1913. hdr->rtt = task->hdr_itt;
  1914. hdr->refcmdsn = task->cmdsn;
  1915. }
  1916. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1917. {
  1918. struct iscsi_cls_session *cls_session;
  1919. struct iscsi_session *session;
  1920. struct iscsi_conn *conn;
  1921. struct iscsi_task *task;
  1922. struct iscsi_tm *hdr;
  1923. int age;
  1924. cls_session = starget_to_session(scsi_target(sc->device));
  1925. session = cls_session->dd_data;
  1926. ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
  1927. mutex_lock(&session->eh_mutex);
  1928. spin_lock_bh(&session->frwd_lock);
  1929. /*
  1930. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1931. * got the command.
  1932. */
  1933. if (!sc->SCp.ptr) {
  1934. ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
  1935. "it completed.\n");
  1936. spin_unlock_bh(&session->frwd_lock);
  1937. mutex_unlock(&session->eh_mutex);
  1938. return SUCCESS;
  1939. }
  1940. /*
  1941. * If we are not logged in or we have started a new session
  1942. * then let the host reset code handle this
  1943. */
  1944. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1945. sc->SCp.phase != session->age) {
  1946. spin_unlock_bh(&session->frwd_lock);
  1947. mutex_unlock(&session->eh_mutex);
  1948. ISCSI_DBG_EH(session, "failing abort due to dropped "
  1949. "session.\n");
  1950. return FAILED;
  1951. }
  1952. conn = session->leadconn;
  1953. conn->eh_abort_cnt++;
  1954. age = session->age;
  1955. task = (struct iscsi_task *)sc->SCp.ptr;
  1956. ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
  1957. sc, task->itt);
  1958. /* task completed before time out */
  1959. if (!task->sc) {
  1960. ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
  1961. goto success;
  1962. }
  1963. if (task->state == ISCSI_TASK_PENDING) {
  1964. fail_scsi_task(task, DID_ABORT);
  1965. goto success;
  1966. }
  1967. /* only have one tmf outstanding at a time */
  1968. if (conn->tmf_state != TMF_INITIAL)
  1969. goto failed;
  1970. conn->tmf_state = TMF_QUEUED;
  1971. hdr = &conn->tmhdr;
  1972. iscsi_prep_abort_task_pdu(task, hdr);
  1973. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
  1974. goto failed;
  1975. switch (conn->tmf_state) {
  1976. case TMF_SUCCESS:
  1977. spin_unlock_bh(&session->frwd_lock);
  1978. /*
  1979. * stop tx side incase the target had sent a abort rsp but
  1980. * the initiator was still writing out data.
  1981. */
  1982. iscsi_suspend_tx(conn);
  1983. /*
  1984. * we do not stop the recv side because targets have been
  1985. * good and have never sent us a successful tmf response
  1986. * then sent more data for the cmd.
  1987. */
  1988. spin_lock_bh(&session->frwd_lock);
  1989. fail_scsi_task(task, DID_ABORT);
  1990. conn->tmf_state = TMF_INITIAL;
  1991. memset(hdr, 0, sizeof(*hdr));
  1992. spin_unlock_bh(&session->frwd_lock);
  1993. iscsi_start_tx(conn);
  1994. goto success_unlocked;
  1995. case TMF_TIMEDOUT:
  1996. spin_unlock_bh(&session->frwd_lock);
  1997. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  1998. goto failed_unlocked;
  1999. case TMF_NOT_FOUND:
  2000. if (!sc->SCp.ptr) {
  2001. conn->tmf_state = TMF_INITIAL;
  2002. memset(hdr, 0, sizeof(*hdr));
  2003. /* task completed before tmf abort response */
  2004. ISCSI_DBG_EH(session, "sc completed while abort in "
  2005. "progress\n");
  2006. goto success;
  2007. }
  2008. /* fall through */
  2009. default:
  2010. conn->tmf_state = TMF_INITIAL;
  2011. goto failed;
  2012. }
  2013. success:
  2014. spin_unlock_bh(&session->frwd_lock);
  2015. success_unlocked:
  2016. ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
  2017. sc, task->itt);
  2018. mutex_unlock(&session->eh_mutex);
  2019. return SUCCESS;
  2020. failed:
  2021. spin_unlock_bh(&session->frwd_lock);
  2022. failed_unlocked:
  2023. ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
  2024. task ? task->itt : 0);
  2025. mutex_unlock(&session->eh_mutex);
  2026. return FAILED;
  2027. }
  2028. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  2029. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2030. {
  2031. memset(hdr, 0, sizeof(*hdr));
  2032. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2033. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2034. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2035. int_to_scsilun(sc->device->lun, &hdr->lun);
  2036. hdr->rtt = RESERVED_ITT;
  2037. }
  2038. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  2039. {
  2040. struct iscsi_cls_session *cls_session;
  2041. struct iscsi_session *session;
  2042. struct iscsi_conn *conn;
  2043. struct iscsi_tm *hdr;
  2044. int rc = FAILED;
  2045. cls_session = starget_to_session(scsi_target(sc->device));
  2046. session = cls_session->dd_data;
  2047. ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
  2048. sc->device->lun);
  2049. mutex_lock(&session->eh_mutex);
  2050. spin_lock_bh(&session->frwd_lock);
  2051. /*
  2052. * Just check if we are not logged in. We cannot check for
  2053. * the phase because the reset could come from a ioctl.
  2054. */
  2055. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2056. goto unlock;
  2057. conn = session->leadconn;
  2058. /* only have one tmf outstanding at a time */
  2059. if (conn->tmf_state != TMF_INITIAL)
  2060. goto unlock;
  2061. conn->tmf_state = TMF_QUEUED;
  2062. hdr = &conn->tmhdr;
  2063. iscsi_prep_lun_reset_pdu(sc, hdr);
  2064. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2065. session->lu_reset_timeout)) {
  2066. rc = FAILED;
  2067. goto unlock;
  2068. }
  2069. switch (conn->tmf_state) {
  2070. case TMF_SUCCESS:
  2071. break;
  2072. case TMF_TIMEDOUT:
  2073. spin_unlock_bh(&session->frwd_lock);
  2074. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2075. goto done;
  2076. default:
  2077. conn->tmf_state = TMF_INITIAL;
  2078. goto unlock;
  2079. }
  2080. rc = SUCCESS;
  2081. spin_unlock_bh(&session->frwd_lock);
  2082. iscsi_suspend_tx(conn);
  2083. spin_lock_bh(&session->frwd_lock);
  2084. memset(hdr, 0, sizeof(*hdr));
  2085. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  2086. conn->tmf_state = TMF_INITIAL;
  2087. spin_unlock_bh(&session->frwd_lock);
  2088. iscsi_start_tx(conn);
  2089. goto done;
  2090. unlock:
  2091. spin_unlock_bh(&session->frwd_lock);
  2092. done:
  2093. ISCSI_DBG_EH(session, "dev reset result = %s\n",
  2094. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2095. mutex_unlock(&session->eh_mutex);
  2096. return rc;
  2097. }
  2098. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  2099. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  2100. {
  2101. struct iscsi_session *session = cls_session->dd_data;
  2102. spin_lock_bh(&session->frwd_lock);
  2103. if (session->state != ISCSI_STATE_LOGGED_IN) {
  2104. session->state = ISCSI_STATE_RECOVERY_FAILED;
  2105. if (session->leadconn)
  2106. wake_up(&session->leadconn->ehwait);
  2107. }
  2108. spin_unlock_bh(&session->frwd_lock);
  2109. }
  2110. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  2111. /**
  2112. * iscsi_eh_session_reset - drop session and attempt relogin
  2113. * @sc: scsi command
  2114. *
  2115. * This function will wait for a relogin, session termination from
  2116. * userspace, or a recovery/replacement timeout.
  2117. */
  2118. int iscsi_eh_session_reset(struct scsi_cmnd *sc)
  2119. {
  2120. struct iscsi_cls_session *cls_session;
  2121. struct iscsi_session *session;
  2122. struct iscsi_conn *conn;
  2123. cls_session = starget_to_session(scsi_target(sc->device));
  2124. session = cls_session->dd_data;
  2125. conn = session->leadconn;
  2126. mutex_lock(&session->eh_mutex);
  2127. spin_lock_bh(&session->frwd_lock);
  2128. if (session->state == ISCSI_STATE_TERMINATE) {
  2129. failed:
  2130. ISCSI_DBG_EH(session,
  2131. "failing session reset: Could not log back into "
  2132. "%s, %s [age %d]\n", session->targetname,
  2133. conn->persistent_address, session->age);
  2134. spin_unlock_bh(&session->frwd_lock);
  2135. mutex_unlock(&session->eh_mutex);
  2136. return FAILED;
  2137. }
  2138. spin_unlock_bh(&session->frwd_lock);
  2139. mutex_unlock(&session->eh_mutex);
  2140. /*
  2141. * we drop the lock here but the leadconn cannot be destoyed while
  2142. * we are in the scsi eh
  2143. */
  2144. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2145. ISCSI_DBG_EH(session, "wait for relogin\n");
  2146. wait_event_interruptible(conn->ehwait,
  2147. session->state == ISCSI_STATE_TERMINATE ||
  2148. session->state == ISCSI_STATE_LOGGED_IN ||
  2149. session->state == ISCSI_STATE_RECOVERY_FAILED);
  2150. if (signal_pending(current))
  2151. flush_signals(current);
  2152. mutex_lock(&session->eh_mutex);
  2153. spin_lock_bh(&session->frwd_lock);
  2154. if (session->state == ISCSI_STATE_LOGGED_IN) {
  2155. ISCSI_DBG_EH(session,
  2156. "session reset succeeded for %s,%s\n",
  2157. session->targetname, conn->persistent_address);
  2158. } else
  2159. goto failed;
  2160. spin_unlock_bh(&session->frwd_lock);
  2161. mutex_unlock(&session->eh_mutex);
  2162. return SUCCESS;
  2163. }
  2164. EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
  2165. static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2166. {
  2167. memset(hdr, 0, sizeof(*hdr));
  2168. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2169. hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2170. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2171. hdr->rtt = RESERVED_ITT;
  2172. }
  2173. /**
  2174. * iscsi_eh_target_reset - reset target
  2175. * @sc: scsi command
  2176. *
  2177. * This will attempt to send a warm target reset.
  2178. */
  2179. static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  2180. {
  2181. struct iscsi_cls_session *cls_session;
  2182. struct iscsi_session *session;
  2183. struct iscsi_conn *conn;
  2184. struct iscsi_tm *hdr;
  2185. int rc = FAILED;
  2186. cls_session = starget_to_session(scsi_target(sc->device));
  2187. session = cls_session->dd_data;
  2188. ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
  2189. session->targetname);
  2190. mutex_lock(&session->eh_mutex);
  2191. spin_lock_bh(&session->frwd_lock);
  2192. /*
  2193. * Just check if we are not logged in. We cannot check for
  2194. * the phase because the reset could come from a ioctl.
  2195. */
  2196. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2197. goto unlock;
  2198. conn = session->leadconn;
  2199. /* only have one tmf outstanding at a time */
  2200. if (conn->tmf_state != TMF_INITIAL)
  2201. goto unlock;
  2202. conn->tmf_state = TMF_QUEUED;
  2203. hdr = &conn->tmhdr;
  2204. iscsi_prep_tgt_reset_pdu(sc, hdr);
  2205. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2206. session->tgt_reset_timeout)) {
  2207. rc = FAILED;
  2208. goto unlock;
  2209. }
  2210. switch (conn->tmf_state) {
  2211. case TMF_SUCCESS:
  2212. break;
  2213. case TMF_TIMEDOUT:
  2214. spin_unlock_bh(&session->frwd_lock);
  2215. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2216. goto done;
  2217. default:
  2218. conn->tmf_state = TMF_INITIAL;
  2219. goto unlock;
  2220. }
  2221. rc = SUCCESS;
  2222. spin_unlock_bh(&session->frwd_lock);
  2223. iscsi_suspend_tx(conn);
  2224. spin_lock_bh(&session->frwd_lock);
  2225. memset(hdr, 0, sizeof(*hdr));
  2226. fail_scsi_tasks(conn, -1, DID_ERROR);
  2227. conn->tmf_state = TMF_INITIAL;
  2228. spin_unlock_bh(&session->frwd_lock);
  2229. iscsi_start_tx(conn);
  2230. goto done;
  2231. unlock:
  2232. spin_unlock_bh(&session->frwd_lock);
  2233. done:
  2234. ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
  2235. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2236. mutex_unlock(&session->eh_mutex);
  2237. return rc;
  2238. }
  2239. /**
  2240. * iscsi_eh_recover_target - reset target and possibly the session
  2241. * @sc: scsi command
  2242. *
  2243. * This will attempt to send a warm target reset. If that fails,
  2244. * we will escalate to ERL0 session recovery.
  2245. */
  2246. int iscsi_eh_recover_target(struct scsi_cmnd *sc)
  2247. {
  2248. int rc;
  2249. rc = iscsi_eh_target_reset(sc);
  2250. if (rc == FAILED)
  2251. rc = iscsi_eh_session_reset(sc);
  2252. return rc;
  2253. }
  2254. EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
  2255. /*
  2256. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  2257. * should be accessed via kfifo_{get,put} on q->queue.
  2258. * Optionally, the caller can obtain the array of object pointers
  2259. * by passing in a non-NULL @items pointer
  2260. */
  2261. int
  2262. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  2263. {
  2264. int i, num_arrays = 1;
  2265. memset(q, 0, sizeof(*q));
  2266. q->max = max;
  2267. /* If the user passed an items pointer, he wants a copy of
  2268. * the array. */
  2269. if (items)
  2270. num_arrays++;
  2271. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  2272. if (q->pool == NULL)
  2273. return -ENOMEM;
  2274. kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
  2275. for (i = 0; i < max; i++) {
  2276. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  2277. if (q->pool[i] == NULL) {
  2278. q->max = i;
  2279. goto enomem;
  2280. }
  2281. kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
  2282. }
  2283. if (items) {
  2284. *items = q->pool + max;
  2285. memcpy(*items, q->pool, max * sizeof(void *));
  2286. }
  2287. return 0;
  2288. enomem:
  2289. iscsi_pool_free(q);
  2290. return -ENOMEM;
  2291. }
  2292. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  2293. void iscsi_pool_free(struct iscsi_pool *q)
  2294. {
  2295. int i;
  2296. for (i = 0; i < q->max; i++)
  2297. kfree(q->pool[i]);
  2298. kfree(q->pool);
  2299. }
  2300. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  2301. /**
  2302. * iscsi_host_add - add host to system
  2303. * @shost: scsi host
  2304. * @pdev: parent device
  2305. *
  2306. * This should be called by partial offload and software iscsi drivers
  2307. * to add a host to the system.
  2308. */
  2309. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  2310. {
  2311. if (!shost->can_queue)
  2312. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  2313. if (!shost->cmd_per_lun)
  2314. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  2315. if (!shost->transportt->eh_timed_out)
  2316. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  2317. return scsi_add_host(shost, pdev);
  2318. }
  2319. EXPORT_SYMBOL_GPL(iscsi_host_add);
  2320. /**
  2321. * iscsi_host_alloc - allocate a host and driver data
  2322. * @sht: scsi host template
  2323. * @dd_data_size: driver host data size
  2324. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  2325. *
  2326. * This should be called by partial offload and software iscsi drivers.
  2327. * To access the driver specific memory use the iscsi_host_priv() macro.
  2328. */
  2329. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  2330. int dd_data_size, bool xmit_can_sleep)
  2331. {
  2332. struct Scsi_Host *shost;
  2333. struct iscsi_host *ihost;
  2334. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  2335. if (!shost)
  2336. return NULL;
  2337. ihost = shost_priv(shost);
  2338. if (xmit_can_sleep) {
  2339. snprintf(ihost->workq_name, sizeof(ihost->workq_name),
  2340. "iscsi_q_%d", shost->host_no);
  2341. ihost->workq = create_singlethread_workqueue(ihost->workq_name);
  2342. if (!ihost->workq)
  2343. goto free_host;
  2344. }
  2345. spin_lock_init(&ihost->lock);
  2346. ihost->state = ISCSI_HOST_SETUP;
  2347. ihost->num_sessions = 0;
  2348. init_waitqueue_head(&ihost->session_removal_wq);
  2349. return shost;
  2350. free_host:
  2351. scsi_host_put(shost);
  2352. return NULL;
  2353. }
  2354. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  2355. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  2356. {
  2357. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  2358. }
  2359. /**
  2360. * iscsi_host_remove - remove host and sessions
  2361. * @shost: scsi host
  2362. *
  2363. * If there are any sessions left, this will initiate the removal and wait
  2364. * for the completion.
  2365. */
  2366. void iscsi_host_remove(struct Scsi_Host *shost)
  2367. {
  2368. struct iscsi_host *ihost = shost_priv(shost);
  2369. unsigned long flags;
  2370. spin_lock_irqsave(&ihost->lock, flags);
  2371. ihost->state = ISCSI_HOST_REMOVED;
  2372. spin_unlock_irqrestore(&ihost->lock, flags);
  2373. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  2374. wait_event_interruptible(ihost->session_removal_wq,
  2375. ihost->num_sessions == 0);
  2376. if (signal_pending(current))
  2377. flush_signals(current);
  2378. scsi_remove_host(shost);
  2379. if (ihost->workq)
  2380. destroy_workqueue(ihost->workq);
  2381. }
  2382. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  2383. void iscsi_host_free(struct Scsi_Host *shost)
  2384. {
  2385. struct iscsi_host *ihost = shost_priv(shost);
  2386. kfree(ihost->netdev);
  2387. kfree(ihost->hwaddress);
  2388. kfree(ihost->initiatorname);
  2389. scsi_host_put(shost);
  2390. }
  2391. EXPORT_SYMBOL_GPL(iscsi_host_free);
  2392. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  2393. {
  2394. struct iscsi_host *ihost = shost_priv(shost);
  2395. unsigned long flags;
  2396. shost = scsi_host_get(shost);
  2397. if (!shost) {
  2398. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  2399. "of session teardown event because host already "
  2400. "removed.\n");
  2401. return;
  2402. }
  2403. spin_lock_irqsave(&ihost->lock, flags);
  2404. ihost->num_sessions--;
  2405. if (ihost->num_sessions == 0)
  2406. wake_up(&ihost->session_removal_wq);
  2407. spin_unlock_irqrestore(&ihost->lock, flags);
  2408. scsi_host_put(shost);
  2409. }
  2410. /**
  2411. * iscsi_session_setup - create iscsi cls session and host and session
  2412. * @iscsit: iscsi transport template
  2413. * @shost: scsi host
  2414. * @cmds_max: session can queue
  2415. * @cmd_task_size: LLD task private data size
  2416. * @initial_cmdsn: initial CmdSN
  2417. *
  2418. * This can be used by software iscsi_transports that allocate
  2419. * a session per scsi host.
  2420. *
  2421. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  2422. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  2423. * for nop handling and login/logout requests.
  2424. */
  2425. struct iscsi_cls_session *
  2426. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  2427. uint16_t cmds_max, int dd_size, int cmd_task_size,
  2428. uint32_t initial_cmdsn, unsigned int id)
  2429. {
  2430. struct iscsi_host *ihost = shost_priv(shost);
  2431. struct iscsi_session *session;
  2432. struct iscsi_cls_session *cls_session;
  2433. int cmd_i, scsi_cmds, total_cmds = cmds_max;
  2434. unsigned long flags;
  2435. spin_lock_irqsave(&ihost->lock, flags);
  2436. if (ihost->state == ISCSI_HOST_REMOVED) {
  2437. spin_unlock_irqrestore(&ihost->lock, flags);
  2438. return NULL;
  2439. }
  2440. ihost->num_sessions++;
  2441. spin_unlock_irqrestore(&ihost->lock, flags);
  2442. if (!total_cmds)
  2443. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  2444. /*
  2445. * The iscsi layer needs some tasks for nop handling and tmfs,
  2446. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  2447. * + 1 command for scsi IO.
  2448. */
  2449. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  2450. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2451. "must be a power of two that is at least %d.\n",
  2452. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  2453. goto dec_session_count;
  2454. }
  2455. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  2456. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2457. "must be a power of 2 less than or equal to %d.\n",
  2458. cmds_max, ISCSI_TOTAL_CMDS_MAX);
  2459. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  2460. }
  2461. if (!is_power_of_2(total_cmds)) {
  2462. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2463. "must be a power of 2.\n", total_cmds);
  2464. total_cmds = rounddown_pow_of_two(total_cmds);
  2465. if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
  2466. return NULL;
  2467. printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
  2468. total_cmds);
  2469. }
  2470. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  2471. cls_session = iscsi_alloc_session(shost, iscsit,
  2472. sizeof(struct iscsi_session) +
  2473. dd_size);
  2474. if (!cls_session)
  2475. goto dec_session_count;
  2476. session = cls_session->dd_data;
  2477. session->cls_session = cls_session;
  2478. session->host = shost;
  2479. session->state = ISCSI_STATE_FREE;
  2480. session->fast_abort = 1;
  2481. session->tgt_reset_timeout = 30;
  2482. session->lu_reset_timeout = 15;
  2483. session->abort_timeout = 10;
  2484. session->scsi_cmds_max = scsi_cmds;
  2485. session->cmds_max = total_cmds;
  2486. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2487. session->exp_cmdsn = initial_cmdsn + 1;
  2488. session->max_cmdsn = initial_cmdsn + 1;
  2489. session->max_r2t = 1;
  2490. session->tt = iscsit;
  2491. session->dd_data = cls_session->dd_data + sizeof(*session);
  2492. mutex_init(&session->eh_mutex);
  2493. spin_lock_init(&session->frwd_lock);
  2494. spin_lock_init(&session->back_lock);
  2495. /* initialize SCSI PDU commands pool */
  2496. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2497. (void***)&session->cmds,
  2498. cmd_task_size + sizeof(struct iscsi_task)))
  2499. goto cmdpool_alloc_fail;
  2500. /* pre-format cmds pool with ITT */
  2501. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2502. struct iscsi_task *task = session->cmds[cmd_i];
  2503. if (cmd_task_size)
  2504. task->dd_data = &task[1];
  2505. task->itt = cmd_i;
  2506. task->state = ISCSI_TASK_FREE;
  2507. INIT_LIST_HEAD(&task->running);
  2508. }
  2509. if (!try_module_get(iscsit->owner))
  2510. goto module_get_fail;
  2511. if (iscsi_add_session(cls_session, id))
  2512. goto cls_session_fail;
  2513. return cls_session;
  2514. cls_session_fail:
  2515. module_put(iscsit->owner);
  2516. module_get_fail:
  2517. iscsi_pool_free(&session->cmdpool);
  2518. cmdpool_alloc_fail:
  2519. iscsi_free_session(cls_session);
  2520. dec_session_count:
  2521. iscsi_host_dec_session_cnt(shost);
  2522. return NULL;
  2523. }
  2524. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2525. /**
  2526. * iscsi_session_teardown - destroy session, host, and cls_session
  2527. * @cls_session: iscsi session
  2528. *
  2529. * The driver must have called iscsi_remove_session before
  2530. * calling this.
  2531. */
  2532. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2533. {
  2534. struct iscsi_session *session = cls_session->dd_data;
  2535. struct module *owner = cls_session->transport->owner;
  2536. struct Scsi_Host *shost = session->host;
  2537. iscsi_pool_free(&session->cmdpool);
  2538. kfree(session->password);
  2539. kfree(session->password_in);
  2540. kfree(session->username);
  2541. kfree(session->username_in);
  2542. kfree(session->targetname);
  2543. kfree(session->targetalias);
  2544. kfree(session->initiatorname);
  2545. kfree(session->boot_root);
  2546. kfree(session->boot_nic);
  2547. kfree(session->boot_target);
  2548. kfree(session->ifacename);
  2549. kfree(session->portal_type);
  2550. kfree(session->discovery_parent_type);
  2551. iscsi_destroy_session(cls_session);
  2552. iscsi_host_dec_session_cnt(shost);
  2553. module_put(owner);
  2554. }
  2555. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2556. /**
  2557. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2558. * @cls_session: iscsi_cls_session
  2559. * @dd_size: private driver data size
  2560. * @conn_idx: cid
  2561. */
  2562. struct iscsi_cls_conn *
  2563. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2564. uint32_t conn_idx)
  2565. {
  2566. struct iscsi_session *session = cls_session->dd_data;
  2567. struct iscsi_conn *conn;
  2568. struct iscsi_cls_conn *cls_conn;
  2569. char *data;
  2570. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  2571. conn_idx);
  2572. if (!cls_conn)
  2573. return NULL;
  2574. conn = cls_conn->dd_data;
  2575. memset(conn, 0, sizeof(*conn) + dd_size);
  2576. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2577. conn->session = session;
  2578. conn->cls_conn = cls_conn;
  2579. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2580. conn->id = conn_idx;
  2581. conn->exp_statsn = 0;
  2582. conn->tmf_state = TMF_INITIAL;
  2583. init_timer(&conn->transport_timer);
  2584. conn->transport_timer.data = (unsigned long)conn;
  2585. conn->transport_timer.function = iscsi_check_transport_timeouts;
  2586. INIT_LIST_HEAD(&conn->mgmtqueue);
  2587. INIT_LIST_HEAD(&conn->cmdqueue);
  2588. INIT_LIST_HEAD(&conn->requeue);
  2589. spin_lock_init(&conn->taskqueuelock);
  2590. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2591. /* allocate login_task used for the login/text sequences */
  2592. spin_lock_bh(&session->frwd_lock);
  2593. if (!kfifo_out(&session->cmdpool.queue,
  2594. (void*)&conn->login_task,
  2595. sizeof(void*))) {
  2596. spin_unlock_bh(&session->frwd_lock);
  2597. goto login_task_alloc_fail;
  2598. }
  2599. spin_unlock_bh(&session->frwd_lock);
  2600. data = (char *) __get_free_pages(GFP_KERNEL,
  2601. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2602. if (!data)
  2603. goto login_task_data_alloc_fail;
  2604. conn->login_task->data = conn->data = data;
  2605. init_timer(&conn->tmf_timer);
  2606. init_waitqueue_head(&conn->ehwait);
  2607. return cls_conn;
  2608. login_task_data_alloc_fail:
  2609. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2610. sizeof(void*));
  2611. login_task_alloc_fail:
  2612. iscsi_destroy_conn(cls_conn);
  2613. return NULL;
  2614. }
  2615. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2616. /**
  2617. * iscsi_conn_teardown - teardown iscsi connection
  2618. * cls_conn: iscsi class connection
  2619. *
  2620. * TODO: we may need to make this into a two step process
  2621. * like scsi-mls remove + put host
  2622. */
  2623. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2624. {
  2625. struct iscsi_conn *conn = cls_conn->dd_data;
  2626. struct iscsi_session *session = conn->session;
  2627. del_timer_sync(&conn->transport_timer);
  2628. mutex_lock(&session->eh_mutex);
  2629. spin_lock_bh(&session->frwd_lock);
  2630. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2631. if (session->leadconn == conn) {
  2632. /*
  2633. * leading connection? then give up on recovery.
  2634. */
  2635. session->state = ISCSI_STATE_TERMINATE;
  2636. wake_up(&conn->ehwait);
  2637. }
  2638. spin_unlock_bh(&session->frwd_lock);
  2639. /* flush queued up work because we free the connection below */
  2640. iscsi_suspend_tx(conn);
  2641. spin_lock_bh(&session->frwd_lock);
  2642. free_pages((unsigned long) conn->data,
  2643. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2644. kfree(conn->persistent_address);
  2645. kfree(conn->local_ipaddr);
  2646. /* regular RX path uses back_lock */
  2647. spin_lock_bh(&session->back_lock);
  2648. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2649. sizeof(void*));
  2650. spin_unlock_bh(&session->back_lock);
  2651. if (session->leadconn == conn)
  2652. session->leadconn = NULL;
  2653. spin_unlock_bh(&session->frwd_lock);
  2654. mutex_unlock(&session->eh_mutex);
  2655. iscsi_destroy_conn(cls_conn);
  2656. }
  2657. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2658. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2659. {
  2660. struct iscsi_conn *conn = cls_conn->dd_data;
  2661. struct iscsi_session *session = conn->session;
  2662. if (!session) {
  2663. iscsi_conn_printk(KERN_ERR, conn,
  2664. "can't start unbound connection\n");
  2665. return -EPERM;
  2666. }
  2667. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2668. session->first_burst > session->max_burst) {
  2669. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2670. "first_burst %d max_burst %d\n",
  2671. session->first_burst, session->max_burst);
  2672. return -EINVAL;
  2673. }
  2674. if (conn->ping_timeout && !conn->recv_timeout) {
  2675. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2676. "zero. Using 5 seconds\n.");
  2677. conn->recv_timeout = 5;
  2678. }
  2679. if (conn->recv_timeout && !conn->ping_timeout) {
  2680. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2681. "zero. Using 5 seconds.\n");
  2682. conn->ping_timeout = 5;
  2683. }
  2684. spin_lock_bh(&session->frwd_lock);
  2685. conn->c_stage = ISCSI_CONN_STARTED;
  2686. session->state = ISCSI_STATE_LOGGED_IN;
  2687. session->queued_cmdsn = session->cmdsn;
  2688. conn->last_recv = jiffies;
  2689. conn->last_ping = jiffies;
  2690. if (conn->recv_timeout && conn->ping_timeout)
  2691. mod_timer(&conn->transport_timer,
  2692. jiffies + (conn->recv_timeout * HZ));
  2693. switch(conn->stop_stage) {
  2694. case STOP_CONN_RECOVER:
  2695. /*
  2696. * unblock eh_abort() if it is blocked. re-try all
  2697. * commands after successful recovery
  2698. */
  2699. conn->stop_stage = 0;
  2700. conn->tmf_state = TMF_INITIAL;
  2701. session->age++;
  2702. if (session->age == 16)
  2703. session->age = 0;
  2704. break;
  2705. case STOP_CONN_TERM:
  2706. conn->stop_stage = 0;
  2707. break;
  2708. default:
  2709. break;
  2710. }
  2711. spin_unlock_bh(&session->frwd_lock);
  2712. iscsi_unblock_session(session->cls_session);
  2713. wake_up(&conn->ehwait);
  2714. return 0;
  2715. }
  2716. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2717. static void
  2718. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2719. {
  2720. struct iscsi_task *task;
  2721. int i, state;
  2722. for (i = 0; i < conn->session->cmds_max; i++) {
  2723. task = conn->session->cmds[i];
  2724. if (task->sc)
  2725. continue;
  2726. if (task->state == ISCSI_TASK_FREE)
  2727. continue;
  2728. ISCSI_DBG_SESSION(conn->session,
  2729. "failing mgmt itt 0x%x state %d\n",
  2730. task->itt, task->state);
  2731. state = ISCSI_TASK_ABRT_SESS_RECOV;
  2732. if (task->state == ISCSI_TASK_PENDING)
  2733. state = ISCSI_TASK_COMPLETED;
  2734. iscsi_complete_task(task, state);
  2735. }
  2736. }
  2737. static void iscsi_start_session_recovery(struct iscsi_session *session,
  2738. struct iscsi_conn *conn, int flag)
  2739. {
  2740. int old_stop_stage;
  2741. mutex_lock(&session->eh_mutex);
  2742. spin_lock_bh(&session->frwd_lock);
  2743. if (conn->stop_stage == STOP_CONN_TERM) {
  2744. spin_unlock_bh(&session->frwd_lock);
  2745. mutex_unlock(&session->eh_mutex);
  2746. return;
  2747. }
  2748. /*
  2749. * When this is called for the in_login state, we only want to clean
  2750. * up the login task and connection. We do not need to block and set
  2751. * the recovery state again
  2752. */
  2753. if (flag == STOP_CONN_TERM)
  2754. session->state = ISCSI_STATE_TERMINATE;
  2755. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2756. session->state = ISCSI_STATE_IN_RECOVERY;
  2757. old_stop_stage = conn->stop_stage;
  2758. conn->stop_stage = flag;
  2759. spin_unlock_bh(&session->frwd_lock);
  2760. del_timer_sync(&conn->transport_timer);
  2761. iscsi_suspend_tx(conn);
  2762. spin_lock_bh(&session->frwd_lock);
  2763. conn->c_stage = ISCSI_CONN_STOPPED;
  2764. spin_unlock_bh(&session->frwd_lock);
  2765. /*
  2766. * for connection level recovery we should not calculate
  2767. * header digest. conn->hdr_size used for optimization
  2768. * in hdr_extract() and will be re-negotiated at
  2769. * set_param() time.
  2770. */
  2771. if (flag == STOP_CONN_RECOVER) {
  2772. conn->hdrdgst_en = 0;
  2773. conn->datadgst_en = 0;
  2774. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2775. old_stop_stage != STOP_CONN_RECOVER) {
  2776. ISCSI_DBG_SESSION(session, "blocking session\n");
  2777. iscsi_block_session(session->cls_session);
  2778. }
  2779. }
  2780. /*
  2781. * flush queues.
  2782. */
  2783. spin_lock_bh(&session->frwd_lock);
  2784. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  2785. fail_mgmt_tasks(session, conn);
  2786. memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
  2787. spin_unlock_bh(&session->frwd_lock);
  2788. mutex_unlock(&session->eh_mutex);
  2789. }
  2790. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2791. {
  2792. struct iscsi_conn *conn = cls_conn->dd_data;
  2793. struct iscsi_session *session = conn->session;
  2794. switch (flag) {
  2795. case STOP_CONN_RECOVER:
  2796. case STOP_CONN_TERM:
  2797. iscsi_start_session_recovery(session, conn, flag);
  2798. break;
  2799. default:
  2800. iscsi_conn_printk(KERN_ERR, conn,
  2801. "invalid stop flag %d\n", flag);
  2802. }
  2803. }
  2804. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2805. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2806. struct iscsi_cls_conn *cls_conn, int is_leading)
  2807. {
  2808. struct iscsi_session *session = cls_session->dd_data;
  2809. struct iscsi_conn *conn = cls_conn->dd_data;
  2810. spin_lock_bh(&session->frwd_lock);
  2811. if (is_leading)
  2812. session->leadconn = conn;
  2813. spin_unlock_bh(&session->frwd_lock);
  2814. /*
  2815. * Unblock xmitworker(), Login Phase will pass through.
  2816. */
  2817. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2818. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2819. return 0;
  2820. }
  2821. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2822. int iscsi_switch_str_param(char **param, char *new_val_buf)
  2823. {
  2824. char *new_val;
  2825. if (*param) {
  2826. if (!strcmp(*param, new_val_buf))
  2827. return 0;
  2828. }
  2829. new_val = kstrdup(new_val_buf, GFP_NOIO);
  2830. if (!new_val)
  2831. return -ENOMEM;
  2832. kfree(*param);
  2833. *param = new_val;
  2834. return 0;
  2835. }
  2836. EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
  2837. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2838. enum iscsi_param param, char *buf, int buflen)
  2839. {
  2840. struct iscsi_conn *conn = cls_conn->dd_data;
  2841. struct iscsi_session *session = conn->session;
  2842. int val;
  2843. switch(param) {
  2844. case ISCSI_PARAM_FAST_ABORT:
  2845. sscanf(buf, "%d", &session->fast_abort);
  2846. break;
  2847. case ISCSI_PARAM_ABORT_TMO:
  2848. sscanf(buf, "%d", &session->abort_timeout);
  2849. break;
  2850. case ISCSI_PARAM_LU_RESET_TMO:
  2851. sscanf(buf, "%d", &session->lu_reset_timeout);
  2852. break;
  2853. case ISCSI_PARAM_TGT_RESET_TMO:
  2854. sscanf(buf, "%d", &session->tgt_reset_timeout);
  2855. break;
  2856. case ISCSI_PARAM_PING_TMO:
  2857. sscanf(buf, "%d", &conn->ping_timeout);
  2858. break;
  2859. case ISCSI_PARAM_RECV_TMO:
  2860. sscanf(buf, "%d", &conn->recv_timeout);
  2861. break;
  2862. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2863. sscanf(buf, "%d", &conn->max_recv_dlength);
  2864. break;
  2865. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2866. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2867. break;
  2868. case ISCSI_PARAM_HDRDGST_EN:
  2869. sscanf(buf, "%d", &conn->hdrdgst_en);
  2870. break;
  2871. case ISCSI_PARAM_DATADGST_EN:
  2872. sscanf(buf, "%d", &conn->datadgst_en);
  2873. break;
  2874. case ISCSI_PARAM_INITIAL_R2T_EN:
  2875. sscanf(buf, "%d", &session->initial_r2t_en);
  2876. break;
  2877. case ISCSI_PARAM_MAX_R2T:
  2878. sscanf(buf, "%hu", &session->max_r2t);
  2879. break;
  2880. case ISCSI_PARAM_IMM_DATA_EN:
  2881. sscanf(buf, "%d", &session->imm_data_en);
  2882. break;
  2883. case ISCSI_PARAM_FIRST_BURST:
  2884. sscanf(buf, "%d", &session->first_burst);
  2885. break;
  2886. case ISCSI_PARAM_MAX_BURST:
  2887. sscanf(buf, "%d", &session->max_burst);
  2888. break;
  2889. case ISCSI_PARAM_PDU_INORDER_EN:
  2890. sscanf(buf, "%d", &session->pdu_inorder_en);
  2891. break;
  2892. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2893. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2894. break;
  2895. case ISCSI_PARAM_ERL:
  2896. sscanf(buf, "%d", &session->erl);
  2897. break;
  2898. case ISCSI_PARAM_EXP_STATSN:
  2899. sscanf(buf, "%u", &conn->exp_statsn);
  2900. break;
  2901. case ISCSI_PARAM_USERNAME:
  2902. return iscsi_switch_str_param(&session->username, buf);
  2903. case ISCSI_PARAM_USERNAME_IN:
  2904. return iscsi_switch_str_param(&session->username_in, buf);
  2905. case ISCSI_PARAM_PASSWORD:
  2906. return iscsi_switch_str_param(&session->password, buf);
  2907. case ISCSI_PARAM_PASSWORD_IN:
  2908. return iscsi_switch_str_param(&session->password_in, buf);
  2909. case ISCSI_PARAM_TARGET_NAME:
  2910. return iscsi_switch_str_param(&session->targetname, buf);
  2911. case ISCSI_PARAM_TARGET_ALIAS:
  2912. return iscsi_switch_str_param(&session->targetalias, buf);
  2913. case ISCSI_PARAM_TPGT:
  2914. sscanf(buf, "%d", &session->tpgt);
  2915. break;
  2916. case ISCSI_PARAM_PERSISTENT_PORT:
  2917. sscanf(buf, "%d", &conn->persistent_port);
  2918. break;
  2919. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2920. return iscsi_switch_str_param(&conn->persistent_address, buf);
  2921. case ISCSI_PARAM_IFACE_NAME:
  2922. return iscsi_switch_str_param(&session->ifacename, buf);
  2923. case ISCSI_PARAM_INITIATOR_NAME:
  2924. return iscsi_switch_str_param(&session->initiatorname, buf);
  2925. case ISCSI_PARAM_BOOT_ROOT:
  2926. return iscsi_switch_str_param(&session->boot_root, buf);
  2927. case ISCSI_PARAM_BOOT_NIC:
  2928. return iscsi_switch_str_param(&session->boot_nic, buf);
  2929. case ISCSI_PARAM_BOOT_TARGET:
  2930. return iscsi_switch_str_param(&session->boot_target, buf);
  2931. case ISCSI_PARAM_PORTAL_TYPE:
  2932. return iscsi_switch_str_param(&session->portal_type, buf);
  2933. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  2934. return iscsi_switch_str_param(&session->discovery_parent_type,
  2935. buf);
  2936. case ISCSI_PARAM_DISCOVERY_SESS:
  2937. sscanf(buf, "%d", &val);
  2938. session->discovery_sess = !!val;
  2939. break;
  2940. case ISCSI_PARAM_LOCAL_IPADDR:
  2941. return iscsi_switch_str_param(&conn->local_ipaddr, buf);
  2942. default:
  2943. return -ENOSYS;
  2944. }
  2945. return 0;
  2946. }
  2947. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2948. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2949. enum iscsi_param param, char *buf)
  2950. {
  2951. struct iscsi_session *session = cls_session->dd_data;
  2952. int len;
  2953. switch(param) {
  2954. case ISCSI_PARAM_FAST_ABORT:
  2955. len = sprintf(buf, "%d\n", session->fast_abort);
  2956. break;
  2957. case ISCSI_PARAM_ABORT_TMO:
  2958. len = sprintf(buf, "%d\n", session->abort_timeout);
  2959. break;
  2960. case ISCSI_PARAM_LU_RESET_TMO:
  2961. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2962. break;
  2963. case ISCSI_PARAM_TGT_RESET_TMO:
  2964. len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
  2965. break;
  2966. case ISCSI_PARAM_INITIAL_R2T_EN:
  2967. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2968. break;
  2969. case ISCSI_PARAM_MAX_R2T:
  2970. len = sprintf(buf, "%hu\n", session->max_r2t);
  2971. break;
  2972. case ISCSI_PARAM_IMM_DATA_EN:
  2973. len = sprintf(buf, "%d\n", session->imm_data_en);
  2974. break;
  2975. case ISCSI_PARAM_FIRST_BURST:
  2976. len = sprintf(buf, "%u\n", session->first_burst);
  2977. break;
  2978. case ISCSI_PARAM_MAX_BURST:
  2979. len = sprintf(buf, "%u\n", session->max_burst);
  2980. break;
  2981. case ISCSI_PARAM_PDU_INORDER_EN:
  2982. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2983. break;
  2984. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2985. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2986. break;
  2987. case ISCSI_PARAM_DEF_TASKMGMT_TMO:
  2988. len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
  2989. break;
  2990. case ISCSI_PARAM_ERL:
  2991. len = sprintf(buf, "%d\n", session->erl);
  2992. break;
  2993. case ISCSI_PARAM_TARGET_NAME:
  2994. len = sprintf(buf, "%s\n", session->targetname);
  2995. break;
  2996. case ISCSI_PARAM_TARGET_ALIAS:
  2997. len = sprintf(buf, "%s\n", session->targetalias);
  2998. break;
  2999. case ISCSI_PARAM_TPGT:
  3000. len = sprintf(buf, "%d\n", session->tpgt);
  3001. break;
  3002. case ISCSI_PARAM_USERNAME:
  3003. len = sprintf(buf, "%s\n", session->username);
  3004. break;
  3005. case ISCSI_PARAM_USERNAME_IN:
  3006. len = sprintf(buf, "%s\n", session->username_in);
  3007. break;
  3008. case ISCSI_PARAM_PASSWORD:
  3009. len = sprintf(buf, "%s\n", session->password);
  3010. break;
  3011. case ISCSI_PARAM_PASSWORD_IN:
  3012. len = sprintf(buf, "%s\n", session->password_in);
  3013. break;
  3014. case ISCSI_PARAM_IFACE_NAME:
  3015. len = sprintf(buf, "%s\n", session->ifacename);
  3016. break;
  3017. case ISCSI_PARAM_INITIATOR_NAME:
  3018. len = sprintf(buf, "%s\n", session->initiatorname);
  3019. break;
  3020. case ISCSI_PARAM_BOOT_ROOT:
  3021. len = sprintf(buf, "%s\n", session->boot_root);
  3022. break;
  3023. case ISCSI_PARAM_BOOT_NIC:
  3024. len = sprintf(buf, "%s\n", session->boot_nic);
  3025. break;
  3026. case ISCSI_PARAM_BOOT_TARGET:
  3027. len = sprintf(buf, "%s\n", session->boot_target);
  3028. break;
  3029. case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
  3030. len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
  3031. break;
  3032. case ISCSI_PARAM_DISCOVERY_SESS:
  3033. len = sprintf(buf, "%u\n", session->discovery_sess);
  3034. break;
  3035. case ISCSI_PARAM_PORTAL_TYPE:
  3036. len = sprintf(buf, "%s\n", session->portal_type);
  3037. break;
  3038. case ISCSI_PARAM_CHAP_AUTH_EN:
  3039. len = sprintf(buf, "%u\n", session->chap_auth_en);
  3040. break;
  3041. case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
  3042. len = sprintf(buf, "%u\n", session->discovery_logout_en);
  3043. break;
  3044. case ISCSI_PARAM_BIDI_CHAP_EN:
  3045. len = sprintf(buf, "%u\n", session->bidi_chap_en);
  3046. break;
  3047. case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
  3048. len = sprintf(buf, "%u\n", session->discovery_auth_optional);
  3049. break;
  3050. case ISCSI_PARAM_DEF_TIME2WAIT:
  3051. len = sprintf(buf, "%d\n", session->time2wait);
  3052. break;
  3053. case ISCSI_PARAM_DEF_TIME2RETAIN:
  3054. len = sprintf(buf, "%d\n", session->time2retain);
  3055. break;
  3056. case ISCSI_PARAM_TSID:
  3057. len = sprintf(buf, "%u\n", session->tsid);
  3058. break;
  3059. case ISCSI_PARAM_ISID:
  3060. len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
  3061. session->isid[0], session->isid[1],
  3062. session->isid[2], session->isid[3],
  3063. session->isid[4], session->isid[5]);
  3064. break;
  3065. case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
  3066. len = sprintf(buf, "%u\n", session->discovery_parent_idx);
  3067. break;
  3068. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  3069. if (session->discovery_parent_type)
  3070. len = sprintf(buf, "%s\n",
  3071. session->discovery_parent_type);
  3072. else
  3073. len = sprintf(buf, "\n");
  3074. break;
  3075. default:
  3076. return -ENOSYS;
  3077. }
  3078. return len;
  3079. }
  3080. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  3081. int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  3082. enum iscsi_param param, char *buf)
  3083. {
  3084. struct sockaddr_in6 *sin6 = NULL;
  3085. struct sockaddr_in *sin = NULL;
  3086. int len;
  3087. switch (addr->ss_family) {
  3088. case AF_INET:
  3089. sin = (struct sockaddr_in *)addr;
  3090. break;
  3091. case AF_INET6:
  3092. sin6 = (struct sockaddr_in6 *)addr;
  3093. break;
  3094. default:
  3095. return -EINVAL;
  3096. }
  3097. switch (param) {
  3098. case ISCSI_PARAM_CONN_ADDRESS:
  3099. case ISCSI_HOST_PARAM_IPADDRESS:
  3100. if (sin)
  3101. len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
  3102. else
  3103. len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
  3104. break;
  3105. case ISCSI_PARAM_CONN_PORT:
  3106. case ISCSI_PARAM_LOCAL_PORT:
  3107. if (sin)
  3108. len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
  3109. else
  3110. len = sprintf(buf, "%hu\n",
  3111. be16_to_cpu(sin6->sin6_port));
  3112. break;
  3113. default:
  3114. return -EINVAL;
  3115. }
  3116. return len;
  3117. }
  3118. EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
  3119. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  3120. enum iscsi_param param, char *buf)
  3121. {
  3122. struct iscsi_conn *conn = cls_conn->dd_data;
  3123. int len;
  3124. switch(param) {
  3125. case ISCSI_PARAM_PING_TMO:
  3126. len = sprintf(buf, "%u\n", conn->ping_timeout);
  3127. break;
  3128. case ISCSI_PARAM_RECV_TMO:
  3129. len = sprintf(buf, "%u\n", conn->recv_timeout);
  3130. break;
  3131. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  3132. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  3133. break;
  3134. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  3135. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  3136. break;
  3137. case ISCSI_PARAM_HDRDGST_EN:
  3138. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  3139. break;
  3140. case ISCSI_PARAM_DATADGST_EN:
  3141. len = sprintf(buf, "%d\n", conn->datadgst_en);
  3142. break;
  3143. case ISCSI_PARAM_IFMARKER_EN:
  3144. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  3145. break;
  3146. case ISCSI_PARAM_OFMARKER_EN:
  3147. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  3148. break;
  3149. case ISCSI_PARAM_EXP_STATSN:
  3150. len = sprintf(buf, "%u\n", conn->exp_statsn);
  3151. break;
  3152. case ISCSI_PARAM_PERSISTENT_PORT:
  3153. len = sprintf(buf, "%d\n", conn->persistent_port);
  3154. break;
  3155. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  3156. len = sprintf(buf, "%s\n", conn->persistent_address);
  3157. break;
  3158. case ISCSI_PARAM_STATSN:
  3159. len = sprintf(buf, "%u\n", conn->statsn);
  3160. break;
  3161. case ISCSI_PARAM_MAX_SEGMENT_SIZE:
  3162. len = sprintf(buf, "%u\n", conn->max_segment_size);
  3163. break;
  3164. case ISCSI_PARAM_KEEPALIVE_TMO:
  3165. len = sprintf(buf, "%u\n", conn->keepalive_tmo);
  3166. break;
  3167. case ISCSI_PARAM_LOCAL_PORT:
  3168. len = sprintf(buf, "%u\n", conn->local_port);
  3169. break;
  3170. case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
  3171. len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
  3172. break;
  3173. case ISCSI_PARAM_TCP_NAGLE_DISABLE:
  3174. len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
  3175. break;
  3176. case ISCSI_PARAM_TCP_WSF_DISABLE:
  3177. len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
  3178. break;
  3179. case ISCSI_PARAM_TCP_TIMER_SCALE:
  3180. len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
  3181. break;
  3182. case ISCSI_PARAM_TCP_TIMESTAMP_EN:
  3183. len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
  3184. break;
  3185. case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
  3186. len = sprintf(buf, "%u\n", conn->fragment_disable);
  3187. break;
  3188. case ISCSI_PARAM_IPV4_TOS:
  3189. len = sprintf(buf, "%u\n", conn->ipv4_tos);
  3190. break;
  3191. case ISCSI_PARAM_IPV6_TC:
  3192. len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
  3193. break;
  3194. case ISCSI_PARAM_IPV6_FLOW_LABEL:
  3195. len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
  3196. break;
  3197. case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
  3198. len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
  3199. break;
  3200. case ISCSI_PARAM_TCP_XMIT_WSF:
  3201. len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
  3202. break;
  3203. case ISCSI_PARAM_TCP_RECV_WSF:
  3204. len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
  3205. break;
  3206. case ISCSI_PARAM_LOCAL_IPADDR:
  3207. len = sprintf(buf, "%s\n", conn->local_ipaddr);
  3208. break;
  3209. default:
  3210. return -ENOSYS;
  3211. }
  3212. return len;
  3213. }
  3214. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  3215. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3216. char *buf)
  3217. {
  3218. struct iscsi_host *ihost = shost_priv(shost);
  3219. int len;
  3220. switch (param) {
  3221. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3222. len = sprintf(buf, "%s\n", ihost->netdev);
  3223. break;
  3224. case ISCSI_HOST_PARAM_HWADDRESS:
  3225. len = sprintf(buf, "%s\n", ihost->hwaddress);
  3226. break;
  3227. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3228. len = sprintf(buf, "%s\n", ihost->initiatorname);
  3229. break;
  3230. default:
  3231. return -ENOSYS;
  3232. }
  3233. return len;
  3234. }
  3235. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  3236. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3237. char *buf, int buflen)
  3238. {
  3239. struct iscsi_host *ihost = shost_priv(shost);
  3240. switch (param) {
  3241. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3242. return iscsi_switch_str_param(&ihost->netdev, buf);
  3243. case ISCSI_HOST_PARAM_HWADDRESS:
  3244. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  3245. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3246. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  3247. default:
  3248. return -ENOSYS;
  3249. }
  3250. return 0;
  3251. }
  3252. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  3253. MODULE_AUTHOR("Mike Christie");
  3254. MODULE_DESCRIPTION("iSCSI library functions");
  3255. MODULE_LICENSE("GPL");