scsi_transport_spi.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /*
  2. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/ctype.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mutex.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/slab.h>
  29. #include <scsi/scsi.h>
  30. #include "scsi_priv.h"
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_transport.h>
  36. #include <scsi/scsi_transport_spi.h>
  37. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  38. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  39. * on" attributes */
  40. #define SPI_HOST_ATTRS 1
  41. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  42. #define DV_LOOPS 3
  43. #define DV_TIMEOUT (10*HZ)
  44. #define DV_RETRIES 3 /* should only need at most
  45. * two cc/ua clears */
  46. /* Our blacklist flags */
  47. enum {
  48. SPI_BLIST_NOIUS = 0x1,
  49. };
  50. /* blacklist table, modelled on scsi_devinfo.c */
  51. static struct {
  52. char *vendor;
  53. char *model;
  54. unsigned flags;
  55. } spi_static_device_list[] __initdata = {
  56. {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
  57. {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
  58. {NULL, NULL, 0}
  59. };
  60. /* Private data accessors (keep these out of the header file) */
  61. #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
  62. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  63. struct spi_internal {
  64. struct scsi_transport_template t;
  65. struct spi_function_template *f;
  66. };
  67. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  68. static const int ppr_to_ps[] = {
  69. /* The PPR values 0-6 are reserved, fill them in when
  70. * the committee defines them */
  71. -1, /* 0x00 */
  72. -1, /* 0x01 */
  73. -1, /* 0x02 */
  74. -1, /* 0x03 */
  75. -1, /* 0x04 */
  76. -1, /* 0x05 */
  77. -1, /* 0x06 */
  78. 3125, /* 0x07 */
  79. 6250, /* 0x08 */
  80. 12500, /* 0x09 */
  81. 25000, /* 0x0a */
  82. 30300, /* 0x0b */
  83. 50000, /* 0x0c */
  84. };
  85. /* The PPR values at which you calculate the period in ns by multiplying
  86. * by 4 */
  87. #define SPI_STATIC_PPR 0x0c
  88. static int sprint_frac(char *dest, int value, int denom)
  89. {
  90. int frac = value % denom;
  91. int result = sprintf(dest, "%d", value / denom);
  92. if (frac == 0)
  93. return result;
  94. dest[result++] = '.';
  95. do {
  96. denom /= 10;
  97. sprintf(dest + result, "%d", frac / denom);
  98. result++;
  99. frac %= denom;
  100. } while (frac);
  101. dest[result++] = '\0';
  102. return result;
  103. }
  104. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  105. enum dma_data_direction dir,
  106. void *buffer, unsigned bufflen,
  107. struct scsi_sense_hdr *sshdr)
  108. {
  109. int i, result;
  110. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  111. for(i = 0; i < DV_RETRIES; i++) {
  112. result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
  113. sense, DV_TIMEOUT, /* retries */ 1,
  114. REQ_FAILFAST_DEV |
  115. REQ_FAILFAST_TRANSPORT |
  116. REQ_FAILFAST_DRIVER,
  117. NULL);
  118. if (driver_byte(result) & DRIVER_SENSE) {
  119. struct scsi_sense_hdr sshdr_tmp;
  120. if (!sshdr)
  121. sshdr = &sshdr_tmp;
  122. if (scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE,
  123. sshdr)
  124. && sshdr->sense_key == UNIT_ATTENTION)
  125. continue;
  126. }
  127. break;
  128. }
  129. return result;
  130. }
  131. static struct {
  132. enum spi_signal_type value;
  133. char *name;
  134. } signal_types[] = {
  135. { SPI_SIGNAL_UNKNOWN, "unknown" },
  136. { SPI_SIGNAL_SE, "SE" },
  137. { SPI_SIGNAL_LVD, "LVD" },
  138. { SPI_SIGNAL_HVD, "HVD" },
  139. };
  140. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  141. {
  142. int i;
  143. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  144. if (type == signal_types[i].value)
  145. return signal_types[i].name;
  146. }
  147. return NULL;
  148. }
  149. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  150. {
  151. int i, len;
  152. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  153. len = strlen(signal_types[i].name);
  154. if (strncmp(name, signal_types[i].name, len) == 0 &&
  155. (name[len] == '\n' || name[len] == '\0'))
  156. return signal_types[i].value;
  157. }
  158. return SPI_SIGNAL_UNKNOWN;
  159. }
  160. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  161. struct device *cdev)
  162. {
  163. struct Scsi_Host *shost = dev_to_shost(dev);
  164. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  165. return 0;
  166. }
  167. static int spi_host_configure(struct transport_container *tc,
  168. struct device *dev,
  169. struct device *cdev);
  170. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  171. "spi_host",
  172. spi_host_setup,
  173. NULL,
  174. spi_host_configure);
  175. static int spi_host_match(struct attribute_container *cont,
  176. struct device *dev)
  177. {
  178. struct Scsi_Host *shost;
  179. if (!scsi_is_host_device(dev))
  180. return 0;
  181. shost = dev_to_shost(dev);
  182. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  183. != &spi_host_class.class)
  184. return 0;
  185. return &shost->transportt->host_attrs.ac == cont;
  186. }
  187. static int spi_target_configure(struct transport_container *tc,
  188. struct device *dev,
  189. struct device *cdev);
  190. static int spi_device_configure(struct transport_container *tc,
  191. struct device *dev,
  192. struct device *cdev)
  193. {
  194. struct scsi_device *sdev = to_scsi_device(dev);
  195. struct scsi_target *starget = sdev->sdev_target;
  196. unsigned bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
  197. &sdev->inquiry[16],
  198. SCSI_DEVINFO_SPI);
  199. /* Populate the target capability fields with the values
  200. * gleaned from the device inquiry */
  201. spi_support_sync(starget) = scsi_device_sync(sdev);
  202. spi_support_wide(starget) = scsi_device_wide(sdev);
  203. spi_support_dt(starget) = scsi_device_dt(sdev);
  204. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  205. spi_support_ius(starget) = scsi_device_ius(sdev);
  206. if (bflags & SPI_BLIST_NOIUS) {
  207. dev_info(dev, "Information Units disabled by blacklist\n");
  208. spi_support_ius(starget) = 0;
  209. }
  210. spi_support_qas(starget) = scsi_device_qas(sdev);
  211. return 0;
  212. }
  213. static int spi_setup_transport_attrs(struct transport_container *tc,
  214. struct device *dev,
  215. struct device *cdev)
  216. {
  217. struct scsi_target *starget = to_scsi_target(dev);
  218. spi_period(starget) = -1; /* illegal value */
  219. spi_min_period(starget) = 0;
  220. spi_offset(starget) = 0; /* async */
  221. spi_max_offset(starget) = 255;
  222. spi_width(starget) = 0; /* narrow */
  223. spi_max_width(starget) = 1;
  224. spi_iu(starget) = 0; /* no IU */
  225. spi_max_iu(starget) = 1;
  226. spi_dt(starget) = 0; /* ST */
  227. spi_qas(starget) = 0;
  228. spi_max_qas(starget) = 1;
  229. spi_wr_flow(starget) = 0;
  230. spi_rd_strm(starget) = 0;
  231. spi_rti(starget) = 0;
  232. spi_pcomp_en(starget) = 0;
  233. spi_hold_mcs(starget) = 0;
  234. spi_dv_pending(starget) = 0;
  235. spi_dv_in_progress(starget) = 0;
  236. spi_initial_dv(starget) = 0;
  237. mutex_init(&spi_dv_mutex(starget));
  238. return 0;
  239. }
  240. #define spi_transport_show_simple(field, format_string) \
  241. \
  242. static ssize_t \
  243. show_spi_transport_##field(struct device *dev, \
  244. struct device_attribute *attr, char *buf) \
  245. { \
  246. struct scsi_target *starget = transport_class_to_starget(dev); \
  247. struct spi_transport_attrs *tp; \
  248. \
  249. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  250. return snprintf(buf, 20, format_string, tp->field); \
  251. }
  252. #define spi_transport_store_simple(field, format_string) \
  253. \
  254. static ssize_t \
  255. store_spi_transport_##field(struct device *dev, \
  256. struct device_attribute *attr, \
  257. const char *buf, size_t count) \
  258. { \
  259. int val; \
  260. struct scsi_target *starget = transport_class_to_starget(dev); \
  261. struct spi_transport_attrs *tp; \
  262. \
  263. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  264. val = simple_strtoul(buf, NULL, 0); \
  265. tp->field = val; \
  266. return count; \
  267. }
  268. #define spi_transport_show_function(field, format_string) \
  269. \
  270. static ssize_t \
  271. show_spi_transport_##field(struct device *dev, \
  272. struct device_attribute *attr, char *buf) \
  273. { \
  274. struct scsi_target *starget = transport_class_to_starget(dev); \
  275. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  276. struct spi_transport_attrs *tp; \
  277. struct spi_internal *i = to_spi_internal(shost->transportt); \
  278. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  279. if (i->f->get_##field) \
  280. i->f->get_##field(starget); \
  281. return snprintf(buf, 20, format_string, tp->field); \
  282. }
  283. #define spi_transport_store_function(field, format_string) \
  284. static ssize_t \
  285. store_spi_transport_##field(struct device *dev, \
  286. struct device_attribute *attr, \
  287. const char *buf, size_t count) \
  288. { \
  289. int val; \
  290. struct scsi_target *starget = transport_class_to_starget(dev); \
  291. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  292. struct spi_internal *i = to_spi_internal(shost->transportt); \
  293. \
  294. if (!i->f->set_##field) \
  295. return -EINVAL; \
  296. val = simple_strtoul(buf, NULL, 0); \
  297. i->f->set_##field(starget, val); \
  298. return count; \
  299. }
  300. #define spi_transport_store_max(field, format_string) \
  301. static ssize_t \
  302. store_spi_transport_##field(struct device *dev, \
  303. struct device_attribute *attr, \
  304. const char *buf, size_t count) \
  305. { \
  306. int val; \
  307. struct scsi_target *starget = transport_class_to_starget(dev); \
  308. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  309. struct spi_internal *i = to_spi_internal(shost->transportt); \
  310. struct spi_transport_attrs *tp \
  311. = (struct spi_transport_attrs *)&starget->starget_data; \
  312. \
  313. if (i->f->set_##field) \
  314. return -EINVAL; \
  315. val = simple_strtoul(buf, NULL, 0); \
  316. if (val > tp->max_##field) \
  317. val = tp->max_##field; \
  318. i->f->set_##field(starget, val); \
  319. return count; \
  320. }
  321. #define spi_transport_rd_attr(field, format_string) \
  322. spi_transport_show_function(field, format_string) \
  323. spi_transport_store_function(field, format_string) \
  324. static DEVICE_ATTR(field, S_IRUGO, \
  325. show_spi_transport_##field, \
  326. store_spi_transport_##field);
  327. #define spi_transport_simple_attr(field, format_string) \
  328. spi_transport_show_simple(field, format_string) \
  329. spi_transport_store_simple(field, format_string) \
  330. static DEVICE_ATTR(field, S_IRUGO, \
  331. show_spi_transport_##field, \
  332. store_spi_transport_##field);
  333. #define spi_transport_max_attr(field, format_string) \
  334. spi_transport_show_function(field, format_string) \
  335. spi_transport_store_max(field, format_string) \
  336. spi_transport_simple_attr(max_##field, format_string) \
  337. static DEVICE_ATTR(field, S_IRUGO, \
  338. show_spi_transport_##field, \
  339. store_spi_transport_##field);
  340. /* The Parallel SCSI Tranport Attributes: */
  341. spi_transport_max_attr(offset, "%d\n");
  342. spi_transport_max_attr(width, "%d\n");
  343. spi_transport_max_attr(iu, "%d\n");
  344. spi_transport_rd_attr(dt, "%d\n");
  345. spi_transport_max_attr(qas, "%d\n");
  346. spi_transport_rd_attr(wr_flow, "%d\n");
  347. spi_transport_rd_attr(rd_strm, "%d\n");
  348. spi_transport_rd_attr(rti, "%d\n");
  349. spi_transport_rd_attr(pcomp_en, "%d\n");
  350. spi_transport_rd_attr(hold_mcs, "%d\n");
  351. /* we only care about the first child device that's a real SCSI device
  352. * so we return 1 to terminate the iteration when we find it */
  353. static int child_iter(struct device *dev, void *data)
  354. {
  355. if (!scsi_is_sdev_device(dev))
  356. return 0;
  357. spi_dv_device(to_scsi_device(dev));
  358. return 1;
  359. }
  360. static ssize_t
  361. store_spi_revalidate(struct device *dev, struct device_attribute *attr,
  362. const char *buf, size_t count)
  363. {
  364. struct scsi_target *starget = transport_class_to_starget(dev);
  365. device_for_each_child(&starget->dev, NULL, child_iter);
  366. return count;
  367. }
  368. static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  369. /* Translate the period into ns according to the current spec
  370. * for SDTR/PPR messages */
  371. static int period_to_str(char *buf, int period)
  372. {
  373. int len, picosec;
  374. if (period < 0 || period > 0xff) {
  375. picosec = -1;
  376. } else if (period <= SPI_STATIC_PPR) {
  377. picosec = ppr_to_ps[period];
  378. } else {
  379. picosec = period * 4000;
  380. }
  381. if (picosec == -1) {
  382. len = sprintf(buf, "reserved");
  383. } else {
  384. len = sprint_frac(buf, picosec, 1000);
  385. }
  386. return len;
  387. }
  388. static ssize_t
  389. show_spi_transport_period_helper(char *buf, int period)
  390. {
  391. int len = period_to_str(buf, period);
  392. buf[len++] = '\n';
  393. buf[len] = '\0';
  394. return len;
  395. }
  396. static ssize_t
  397. store_spi_transport_period_helper(struct device *dev, const char *buf,
  398. size_t count, int *periodp)
  399. {
  400. int j, picosec, period = -1;
  401. char *endp;
  402. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  403. if (*endp == '.') {
  404. int mult = 100;
  405. do {
  406. endp++;
  407. if (!isdigit(*endp))
  408. break;
  409. picosec += (*endp - '0') * mult;
  410. mult /= 10;
  411. } while (mult > 0);
  412. }
  413. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  414. if (ppr_to_ps[j] < picosec)
  415. continue;
  416. period = j;
  417. break;
  418. }
  419. if (period == -1)
  420. period = picosec / 4000;
  421. if (period > 0xff)
  422. period = 0xff;
  423. *periodp = period;
  424. return count;
  425. }
  426. static ssize_t
  427. show_spi_transport_period(struct device *dev,
  428. struct device_attribute *attr, char *buf)
  429. {
  430. struct scsi_target *starget = transport_class_to_starget(dev);
  431. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  432. struct spi_internal *i = to_spi_internal(shost->transportt);
  433. struct spi_transport_attrs *tp =
  434. (struct spi_transport_attrs *)&starget->starget_data;
  435. if (i->f->get_period)
  436. i->f->get_period(starget);
  437. return show_spi_transport_period_helper(buf, tp->period);
  438. }
  439. static ssize_t
  440. store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
  441. const char *buf, size_t count)
  442. {
  443. struct scsi_target *starget = transport_class_to_starget(cdev);
  444. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  445. struct spi_internal *i = to_spi_internal(shost->transportt);
  446. struct spi_transport_attrs *tp =
  447. (struct spi_transport_attrs *)&starget->starget_data;
  448. int period, retval;
  449. if (!i->f->set_period)
  450. return -EINVAL;
  451. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  452. if (period < tp->min_period)
  453. period = tp->min_period;
  454. i->f->set_period(starget, period);
  455. return retval;
  456. }
  457. static DEVICE_ATTR(period, S_IRUGO,
  458. show_spi_transport_period,
  459. store_spi_transport_period);
  460. static ssize_t
  461. show_spi_transport_min_period(struct device *cdev,
  462. struct device_attribute *attr, char *buf)
  463. {
  464. struct scsi_target *starget = transport_class_to_starget(cdev);
  465. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  466. struct spi_internal *i = to_spi_internal(shost->transportt);
  467. struct spi_transport_attrs *tp =
  468. (struct spi_transport_attrs *)&starget->starget_data;
  469. if (!i->f->set_period)
  470. return -EINVAL;
  471. return show_spi_transport_period_helper(buf, tp->min_period);
  472. }
  473. static ssize_t
  474. store_spi_transport_min_period(struct device *cdev,
  475. struct device_attribute *attr,
  476. const char *buf, size_t count)
  477. {
  478. struct scsi_target *starget = transport_class_to_starget(cdev);
  479. struct spi_transport_attrs *tp =
  480. (struct spi_transport_attrs *)&starget->starget_data;
  481. return store_spi_transport_period_helper(cdev, buf, count,
  482. &tp->min_period);
  483. }
  484. static DEVICE_ATTR(min_period, S_IRUGO,
  485. show_spi_transport_min_period,
  486. store_spi_transport_min_period);
  487. static ssize_t show_spi_host_signalling(struct device *cdev,
  488. struct device_attribute *attr,
  489. char *buf)
  490. {
  491. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  492. struct spi_internal *i = to_spi_internal(shost->transportt);
  493. if (i->f->get_signalling)
  494. i->f->get_signalling(shost);
  495. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  496. }
  497. static ssize_t store_spi_host_signalling(struct device *dev,
  498. struct device_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. struct Scsi_Host *shost = transport_class_to_shost(dev);
  502. struct spi_internal *i = to_spi_internal(shost->transportt);
  503. enum spi_signal_type type = spi_signal_to_value(buf);
  504. if (!i->f->set_signalling)
  505. return -EINVAL;
  506. if (type != SPI_SIGNAL_UNKNOWN)
  507. i->f->set_signalling(shost, type);
  508. return count;
  509. }
  510. static DEVICE_ATTR(signalling, S_IRUGO,
  511. show_spi_host_signalling,
  512. store_spi_host_signalling);
  513. static ssize_t show_spi_host_width(struct device *cdev,
  514. struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  518. return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow");
  519. }
  520. static DEVICE_ATTR(host_width, S_IRUGO,
  521. show_spi_host_width, NULL);
  522. static ssize_t show_spi_host_hba_id(struct device *cdev,
  523. struct device_attribute *attr,
  524. char *buf)
  525. {
  526. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  527. return sprintf(buf, "%d\n", shost->this_id);
  528. }
  529. static DEVICE_ATTR(hba_id, S_IRUGO,
  530. show_spi_host_hba_id, NULL);
  531. #define DV_SET(x, y) \
  532. if(i->f->set_##x) \
  533. i->f->set_##x(sdev->sdev_target, y)
  534. enum spi_compare_returns {
  535. SPI_COMPARE_SUCCESS,
  536. SPI_COMPARE_FAILURE,
  537. SPI_COMPARE_SKIP_TEST,
  538. };
  539. /* This is for read/write Domain Validation: If the device supports
  540. * an echo buffer, we do read/write tests to it */
  541. static enum spi_compare_returns
  542. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  543. u8 *ptr, const int retries)
  544. {
  545. int len = ptr - buffer;
  546. int j, k, r, result;
  547. unsigned int pattern = 0x0000ffff;
  548. struct scsi_sense_hdr sshdr;
  549. const char spi_write_buffer[] = {
  550. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  551. };
  552. const char spi_read_buffer[] = {
  553. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  554. };
  555. /* set up the pattern buffer. Doesn't matter if we spill
  556. * slightly beyond since that's where the read buffer is */
  557. for (j = 0; j < len; ) {
  558. /* fill the buffer with counting (test a) */
  559. for ( ; j < min(len, 32); j++)
  560. buffer[j] = j;
  561. k = j;
  562. /* fill the buffer with alternating words of 0x0 and
  563. * 0xffff (test b) */
  564. for ( ; j < min(len, k + 32); j += 2) {
  565. u16 *word = (u16 *)&buffer[j];
  566. *word = (j & 0x02) ? 0x0000 : 0xffff;
  567. }
  568. k = j;
  569. /* fill with crosstalk (alternating 0x5555 0xaaa)
  570. * (test c) */
  571. for ( ; j < min(len, k + 32); j += 2) {
  572. u16 *word = (u16 *)&buffer[j];
  573. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  574. }
  575. k = j;
  576. /* fill with shifting bits (test d) */
  577. for ( ; j < min(len, k + 32); j += 4) {
  578. u32 *word = (unsigned int *)&buffer[j];
  579. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  580. *word = pattern;
  581. pattern = (pattern << 1) | roll;
  582. }
  583. /* don't bother with random data (test e) */
  584. }
  585. for (r = 0; r < retries; r++) {
  586. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  587. buffer, len, &sshdr);
  588. if(result || !scsi_device_online(sdev)) {
  589. scsi_device_set_state(sdev, SDEV_QUIESCE);
  590. if (scsi_sense_valid(&sshdr)
  591. && sshdr.sense_key == ILLEGAL_REQUEST
  592. /* INVALID FIELD IN CDB */
  593. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  594. /* This would mean that the drive lied
  595. * to us about supporting an echo
  596. * buffer (unfortunately some Western
  597. * Digital drives do precisely this)
  598. */
  599. return SPI_COMPARE_SKIP_TEST;
  600. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  601. return SPI_COMPARE_FAILURE;
  602. }
  603. memset(ptr, 0, len);
  604. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  605. ptr, len, NULL);
  606. scsi_device_set_state(sdev, SDEV_QUIESCE);
  607. if (memcmp(buffer, ptr, len) != 0)
  608. return SPI_COMPARE_FAILURE;
  609. }
  610. return SPI_COMPARE_SUCCESS;
  611. }
  612. /* This is for the simplest form of Domain Validation: a read test
  613. * on the inquiry data from the device */
  614. static enum spi_compare_returns
  615. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  616. u8 *ptr, const int retries)
  617. {
  618. int r, result;
  619. const int len = sdev->inquiry_len;
  620. const char spi_inquiry[] = {
  621. INQUIRY, 0, 0, 0, len, 0
  622. };
  623. for (r = 0; r < retries; r++) {
  624. memset(ptr, 0, len);
  625. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  626. ptr, len, NULL);
  627. if(result || !scsi_device_online(sdev)) {
  628. scsi_device_set_state(sdev, SDEV_QUIESCE);
  629. return SPI_COMPARE_FAILURE;
  630. }
  631. /* If we don't have the inquiry data already, the
  632. * first read gets it */
  633. if (ptr == buffer) {
  634. ptr += len;
  635. --r;
  636. continue;
  637. }
  638. if (memcmp(buffer, ptr, len) != 0)
  639. /* failure */
  640. return SPI_COMPARE_FAILURE;
  641. }
  642. return SPI_COMPARE_SUCCESS;
  643. }
  644. static enum spi_compare_returns
  645. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  646. enum spi_compare_returns
  647. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  648. {
  649. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  650. struct scsi_target *starget = sdev->sdev_target;
  651. int period = 0, prevperiod = 0;
  652. enum spi_compare_returns retval;
  653. for (;;) {
  654. int newperiod;
  655. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  656. if (retval == SPI_COMPARE_SUCCESS
  657. || retval == SPI_COMPARE_SKIP_TEST)
  658. break;
  659. /* OK, retrain, fallback */
  660. if (i->f->get_iu)
  661. i->f->get_iu(starget);
  662. if (i->f->get_qas)
  663. i->f->get_qas(starget);
  664. if (i->f->get_period)
  665. i->f->get_period(sdev->sdev_target);
  666. /* Here's the fallback sequence; first try turning off
  667. * IU, then QAS (if we can control them), then finally
  668. * fall down the periods */
  669. if (i->f->set_iu && spi_iu(starget)) {
  670. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
  671. DV_SET(iu, 0);
  672. } else if (i->f->set_qas && spi_qas(starget)) {
  673. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
  674. DV_SET(qas, 0);
  675. } else {
  676. newperiod = spi_period(starget);
  677. period = newperiod > period ? newperiod : period;
  678. if (period < 0x0d)
  679. period++;
  680. else
  681. period += period >> 1;
  682. if (unlikely(period > 0xff || period == prevperiod)) {
  683. /* Total failure; set to async and return */
  684. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  685. DV_SET(offset, 0);
  686. return SPI_COMPARE_FAILURE;
  687. }
  688. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  689. DV_SET(period, period);
  690. prevperiod = period;
  691. }
  692. }
  693. return retval;
  694. }
  695. static int
  696. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  697. {
  698. int l, result;
  699. /* first off do a test unit ready. This can error out
  700. * because of reservations or some other reason. If it
  701. * fails, the device won't let us write to the echo buffer
  702. * so just return failure */
  703. const char spi_test_unit_ready[] = {
  704. TEST_UNIT_READY, 0, 0, 0, 0, 0
  705. };
  706. const char spi_read_buffer_descriptor[] = {
  707. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  708. };
  709. /* We send a set of three TURs to clear any outstanding
  710. * unit attention conditions if they exist (Otherwise the
  711. * buffer tests won't be happy). If the TUR still fails
  712. * (reservation conflict, device not ready, etc) just
  713. * skip the write tests */
  714. for (l = 0; ; l++) {
  715. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  716. NULL, 0, NULL);
  717. if(result) {
  718. if(l >= 3)
  719. return 0;
  720. } else {
  721. /* TUR succeeded */
  722. break;
  723. }
  724. }
  725. result = spi_execute(sdev, spi_read_buffer_descriptor,
  726. DMA_FROM_DEVICE, buffer, 4, NULL);
  727. if (result)
  728. /* Device has no echo buffer */
  729. return 0;
  730. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  731. }
  732. static void
  733. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  734. {
  735. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  736. struct scsi_target *starget = sdev->sdev_target;
  737. struct Scsi_Host *shost = sdev->host;
  738. int len = sdev->inquiry_len;
  739. int min_period = spi_min_period(starget);
  740. int max_width = spi_max_width(starget);
  741. /* first set us up for narrow async */
  742. DV_SET(offset, 0);
  743. DV_SET(width, 0);
  744. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  745. != SPI_COMPARE_SUCCESS) {
  746. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  747. /* FIXME: should probably offline the device here? */
  748. return;
  749. }
  750. if (!spi_support_wide(starget)) {
  751. spi_max_width(starget) = 0;
  752. max_width = 0;
  753. }
  754. /* test width */
  755. if (i->f->set_width && max_width) {
  756. i->f->set_width(starget, 1);
  757. if (spi_dv_device_compare_inquiry(sdev, buffer,
  758. buffer + len,
  759. DV_LOOPS)
  760. != SPI_COMPARE_SUCCESS) {
  761. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  762. i->f->set_width(starget, 0);
  763. /* Make sure we don't force wide back on by asking
  764. * for a transfer period that requires it */
  765. max_width = 0;
  766. if (min_period < 10)
  767. min_period = 10;
  768. }
  769. }
  770. if (!i->f->set_period)
  771. return;
  772. /* device can't handle synchronous */
  773. if (!spi_support_sync(starget) && !spi_support_dt(starget))
  774. return;
  775. /* len == -1 is the signal that we need to ascertain the
  776. * presence of an echo buffer before trying to use it. len ==
  777. * 0 means we don't have an echo buffer */
  778. len = -1;
  779. retry:
  780. /* now set up to the maximum */
  781. DV_SET(offset, spi_max_offset(starget));
  782. DV_SET(period, min_period);
  783. /* try QAS requests; this should be harmless to set if the
  784. * target supports it */
  785. if (spi_support_qas(starget) && spi_max_qas(starget)) {
  786. DV_SET(qas, 1);
  787. } else {
  788. DV_SET(qas, 0);
  789. }
  790. if (spi_support_ius(starget) && spi_max_iu(starget) &&
  791. min_period < 9) {
  792. /* This u320 (or u640). Set IU transfers */
  793. DV_SET(iu, 1);
  794. /* Then set the optional parameters */
  795. DV_SET(rd_strm, 1);
  796. DV_SET(wr_flow, 1);
  797. DV_SET(rti, 1);
  798. if (min_period == 8)
  799. DV_SET(pcomp_en, 1);
  800. } else {
  801. DV_SET(iu, 0);
  802. }
  803. /* now that we've done all this, actually check the bus
  804. * signal type (if known). Some devices are stupid on
  805. * a SE bus and still claim they can try LVD only settings */
  806. if (i->f->get_signalling)
  807. i->f->get_signalling(shost);
  808. if (spi_signalling(shost) == SPI_SIGNAL_SE ||
  809. spi_signalling(shost) == SPI_SIGNAL_HVD ||
  810. !spi_support_dt(starget)) {
  811. DV_SET(dt, 0);
  812. } else {
  813. DV_SET(dt, 1);
  814. }
  815. /* set width last because it will pull all the other
  816. * parameters down to required values */
  817. DV_SET(width, max_width);
  818. /* Do the read only INQUIRY tests */
  819. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  820. spi_dv_device_compare_inquiry);
  821. /* See if we actually managed to negotiate and sustain DT */
  822. if (i->f->get_dt)
  823. i->f->get_dt(starget);
  824. /* see if the device has an echo buffer. If it does we can do
  825. * the SPI pattern write tests. Because of some broken
  826. * devices, we *only* try this on a device that has actually
  827. * negotiated DT */
  828. if (len == -1 && spi_dt(starget))
  829. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  830. if (len <= 0) {
  831. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  832. return;
  833. }
  834. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  835. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  836. len = SPI_MAX_ECHO_BUFFER_SIZE;
  837. }
  838. if (spi_dv_retrain(sdev, buffer, buffer + len,
  839. spi_dv_device_echo_buffer)
  840. == SPI_COMPARE_SKIP_TEST) {
  841. /* OK, the stupid drive can't do a write echo buffer
  842. * test after all, fall back to the read tests */
  843. len = 0;
  844. goto retry;
  845. }
  846. }
  847. /** spi_dv_device - Do Domain Validation on the device
  848. * @sdev: scsi device to validate
  849. *
  850. * Performs the domain validation on the given device in the
  851. * current execution thread. Since DV operations may sleep,
  852. * the current thread must have user context. Also no SCSI
  853. * related locks that would deadlock I/O issued by the DV may
  854. * be held.
  855. */
  856. void
  857. spi_dv_device(struct scsi_device *sdev)
  858. {
  859. struct scsi_target *starget = sdev->sdev_target;
  860. u8 *buffer;
  861. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  862. if (unlikely(scsi_device_get(sdev)))
  863. return;
  864. if (unlikely(spi_dv_in_progress(starget)))
  865. return;
  866. spi_dv_in_progress(starget) = 1;
  867. buffer = kzalloc(len, GFP_KERNEL);
  868. if (unlikely(!buffer))
  869. goto out_put;
  870. /* We need to verify that the actual device will quiesce; the
  871. * later target quiesce is just a nice to have */
  872. if (unlikely(scsi_device_quiesce(sdev)))
  873. goto out_free;
  874. scsi_target_quiesce(starget);
  875. spi_dv_pending(starget) = 1;
  876. mutex_lock(&spi_dv_mutex(starget));
  877. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  878. spi_dv_device_internal(sdev, buffer);
  879. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  880. mutex_unlock(&spi_dv_mutex(starget));
  881. spi_dv_pending(starget) = 0;
  882. scsi_target_resume(starget);
  883. spi_initial_dv(starget) = 1;
  884. out_free:
  885. kfree(buffer);
  886. out_put:
  887. spi_dv_in_progress(starget) = 0;
  888. scsi_device_put(sdev);
  889. }
  890. EXPORT_SYMBOL(spi_dv_device);
  891. struct work_queue_wrapper {
  892. struct work_struct work;
  893. struct scsi_device *sdev;
  894. };
  895. static void
  896. spi_dv_device_work_wrapper(struct work_struct *work)
  897. {
  898. struct work_queue_wrapper *wqw =
  899. container_of(work, struct work_queue_wrapper, work);
  900. struct scsi_device *sdev = wqw->sdev;
  901. kfree(wqw);
  902. spi_dv_device(sdev);
  903. spi_dv_pending(sdev->sdev_target) = 0;
  904. scsi_device_put(sdev);
  905. }
  906. /**
  907. * spi_schedule_dv_device - schedule domain validation to occur on the device
  908. * @sdev: The device to validate
  909. *
  910. * Identical to spi_dv_device() above, except that the DV will be
  911. * scheduled to occur in a workqueue later. All memory allocations
  912. * are atomic, so may be called from any context including those holding
  913. * SCSI locks.
  914. */
  915. void
  916. spi_schedule_dv_device(struct scsi_device *sdev)
  917. {
  918. struct work_queue_wrapper *wqw =
  919. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  920. if (unlikely(!wqw))
  921. return;
  922. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  923. kfree(wqw);
  924. return;
  925. }
  926. /* Set pending early (dv_device doesn't check it, only sets it) */
  927. spi_dv_pending(sdev->sdev_target) = 1;
  928. if (unlikely(scsi_device_get(sdev))) {
  929. kfree(wqw);
  930. spi_dv_pending(sdev->sdev_target) = 0;
  931. return;
  932. }
  933. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
  934. wqw->sdev = sdev;
  935. schedule_work(&wqw->work);
  936. }
  937. EXPORT_SYMBOL(spi_schedule_dv_device);
  938. /**
  939. * spi_display_xfer_agreement - Print the current target transfer agreement
  940. * @starget: The target for which to display the agreement
  941. *
  942. * Each SPI port is required to maintain a transfer agreement for each
  943. * other port on the bus. This function prints a one-line summary of
  944. * the current agreement; more detailed information is available in sysfs.
  945. */
  946. void spi_display_xfer_agreement(struct scsi_target *starget)
  947. {
  948. struct spi_transport_attrs *tp;
  949. tp = (struct spi_transport_attrs *)&starget->starget_data;
  950. if (tp->offset > 0 && tp->period > 0) {
  951. unsigned int picosec, kb100;
  952. char *scsi = "FAST-?";
  953. char tmp[8];
  954. if (tp->period <= SPI_STATIC_PPR) {
  955. picosec = ppr_to_ps[tp->period];
  956. switch (tp->period) {
  957. case 7: scsi = "FAST-320"; break;
  958. case 8: scsi = "FAST-160"; break;
  959. case 9: scsi = "FAST-80"; break;
  960. case 10:
  961. case 11: scsi = "FAST-40"; break;
  962. case 12: scsi = "FAST-20"; break;
  963. }
  964. } else {
  965. picosec = tp->period * 4000;
  966. if (tp->period < 25)
  967. scsi = "FAST-20";
  968. else if (tp->period < 50)
  969. scsi = "FAST-10";
  970. else
  971. scsi = "FAST-5";
  972. }
  973. kb100 = (10000000 + picosec / 2) / picosec;
  974. if (tp->width)
  975. kb100 *= 2;
  976. sprint_frac(tmp, picosec, 1000);
  977. dev_info(&starget->dev,
  978. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  979. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  980. tp->dt ? "DT" : "ST",
  981. tp->iu ? " IU" : "",
  982. tp->qas ? " QAS" : "",
  983. tp->rd_strm ? " RDSTRM" : "",
  984. tp->rti ? " RTI" : "",
  985. tp->wr_flow ? " WRFLOW" : "",
  986. tp->pcomp_en ? " PCOMP" : "",
  987. tp->hold_mcs ? " HMCS" : "",
  988. tmp, tp->offset);
  989. } else {
  990. dev_info(&starget->dev, "%sasynchronous\n",
  991. tp->width ? "wide " : "");
  992. }
  993. }
  994. EXPORT_SYMBOL(spi_display_xfer_agreement);
  995. int spi_populate_width_msg(unsigned char *msg, int width)
  996. {
  997. msg[0] = EXTENDED_MESSAGE;
  998. msg[1] = 2;
  999. msg[2] = EXTENDED_WDTR;
  1000. msg[3] = width;
  1001. return 4;
  1002. }
  1003. EXPORT_SYMBOL_GPL(spi_populate_width_msg);
  1004. int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
  1005. {
  1006. msg[0] = EXTENDED_MESSAGE;
  1007. msg[1] = 3;
  1008. msg[2] = EXTENDED_SDTR;
  1009. msg[3] = period;
  1010. msg[4] = offset;
  1011. return 5;
  1012. }
  1013. EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
  1014. int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
  1015. int width, int options)
  1016. {
  1017. msg[0] = EXTENDED_MESSAGE;
  1018. msg[1] = 6;
  1019. msg[2] = EXTENDED_PPR;
  1020. msg[3] = period;
  1021. msg[4] = 0;
  1022. msg[5] = offset;
  1023. msg[6] = width;
  1024. msg[7] = options;
  1025. return 8;
  1026. }
  1027. EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
  1028. #ifdef CONFIG_SCSI_CONSTANTS
  1029. static const char * const one_byte_msgs[] = {
  1030. /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
  1031. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  1032. /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
  1033. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  1034. /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
  1035. /* 0x0f */ "Initiate Recovery", "Release Recovery",
  1036. /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
  1037. /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
  1038. };
  1039. static const char * const two_byte_msgs[] = {
  1040. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  1041. /* 0x23 */ "Ignore Wide Residue", "ACA"
  1042. };
  1043. static const char * const extended_msgs[] = {
  1044. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  1045. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  1046. /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
  1047. };
  1048. static void print_nego(const unsigned char *msg, int per, int off, int width)
  1049. {
  1050. if (per) {
  1051. char buf[20];
  1052. period_to_str(buf, msg[per]);
  1053. printk("period = %s ns ", buf);
  1054. }
  1055. if (off)
  1056. printk("offset = %d ", msg[off]);
  1057. if (width)
  1058. printk("width = %d ", 8 << msg[width]);
  1059. }
  1060. static void print_ptr(const unsigned char *msg, int msb, const char *desc)
  1061. {
  1062. int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
  1063. msg[msb+3];
  1064. printk("%s = %d ", desc, ptr);
  1065. }
  1066. int spi_print_msg(const unsigned char *msg)
  1067. {
  1068. int len = 1, i;
  1069. if (msg[0] == EXTENDED_MESSAGE) {
  1070. len = 2 + msg[1];
  1071. if (len == 2)
  1072. len += 256;
  1073. if (msg[2] < ARRAY_SIZE(extended_msgs))
  1074. printk ("%s ", extended_msgs[msg[2]]);
  1075. else
  1076. printk ("Extended Message, reserved code (0x%02x) ",
  1077. (int) msg[2]);
  1078. switch (msg[2]) {
  1079. case EXTENDED_MODIFY_DATA_POINTER:
  1080. print_ptr(msg, 3, "pointer");
  1081. break;
  1082. case EXTENDED_SDTR:
  1083. print_nego(msg, 3, 4, 0);
  1084. break;
  1085. case EXTENDED_WDTR:
  1086. print_nego(msg, 0, 0, 3);
  1087. break;
  1088. case EXTENDED_PPR:
  1089. print_nego(msg, 3, 5, 6);
  1090. break;
  1091. case EXTENDED_MODIFY_BIDI_DATA_PTR:
  1092. print_ptr(msg, 3, "out");
  1093. print_ptr(msg, 7, "in");
  1094. break;
  1095. default:
  1096. for (i = 2; i < len; ++i)
  1097. printk("%02x ", msg[i]);
  1098. }
  1099. /* Identify */
  1100. } else if (msg[0] & 0x80) {
  1101. printk("Identify disconnect %sallowed %s %d ",
  1102. (msg[0] & 0x40) ? "" : "not ",
  1103. (msg[0] & 0x20) ? "target routine" : "lun",
  1104. msg[0] & 0x7);
  1105. /* Normal One byte */
  1106. } else if (msg[0] < 0x1f) {
  1107. if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
  1108. printk("%s ", one_byte_msgs[msg[0]]);
  1109. else
  1110. printk("reserved (%02x) ", msg[0]);
  1111. } else if (msg[0] == 0x55) {
  1112. printk("QAS Request ");
  1113. /* Two byte */
  1114. } else if (msg[0] <= 0x2f) {
  1115. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  1116. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  1117. msg[1]);
  1118. else
  1119. printk("reserved two byte (%02x %02x) ",
  1120. msg[0], msg[1]);
  1121. len = 2;
  1122. } else
  1123. printk("reserved ");
  1124. return len;
  1125. }
  1126. EXPORT_SYMBOL(spi_print_msg);
  1127. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  1128. int spi_print_msg(const unsigned char *msg)
  1129. {
  1130. int len = 1, i;
  1131. if (msg[0] == EXTENDED_MESSAGE) {
  1132. len = 2 + msg[1];
  1133. if (len == 2)
  1134. len += 256;
  1135. for (i = 0; i < len; ++i)
  1136. printk("%02x ", msg[i]);
  1137. /* Identify */
  1138. } else if (msg[0] & 0x80) {
  1139. printk("%02x ", msg[0]);
  1140. /* Normal One byte */
  1141. } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
  1142. printk("%02x ", msg[0]);
  1143. /* Two byte */
  1144. } else if (msg[0] <= 0x2f) {
  1145. printk("%02x %02x", msg[0], msg[1]);
  1146. len = 2;
  1147. } else
  1148. printk("%02x ", msg[0]);
  1149. return len;
  1150. }
  1151. EXPORT_SYMBOL(spi_print_msg);
  1152. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1153. static int spi_device_match(struct attribute_container *cont,
  1154. struct device *dev)
  1155. {
  1156. struct scsi_device *sdev;
  1157. struct Scsi_Host *shost;
  1158. struct spi_internal *i;
  1159. if (!scsi_is_sdev_device(dev))
  1160. return 0;
  1161. sdev = to_scsi_device(dev);
  1162. shost = sdev->host;
  1163. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1164. != &spi_host_class.class)
  1165. return 0;
  1166. /* Note: this class has no device attributes, so it has
  1167. * no per-HBA allocation and thus we don't need to distinguish
  1168. * the attribute containers for the device */
  1169. i = to_spi_internal(shost->transportt);
  1170. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1171. return 0;
  1172. return 1;
  1173. }
  1174. static int spi_target_match(struct attribute_container *cont,
  1175. struct device *dev)
  1176. {
  1177. struct Scsi_Host *shost;
  1178. struct scsi_target *starget;
  1179. struct spi_internal *i;
  1180. if (!scsi_is_target_device(dev))
  1181. return 0;
  1182. shost = dev_to_shost(dev->parent);
  1183. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1184. != &spi_host_class.class)
  1185. return 0;
  1186. i = to_spi_internal(shost->transportt);
  1187. starget = to_scsi_target(dev);
  1188. if (i->f->deny_binding && i->f->deny_binding(starget))
  1189. return 0;
  1190. return &i->t.target_attrs.ac == cont;
  1191. }
  1192. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1193. "spi_transport",
  1194. spi_setup_transport_attrs,
  1195. NULL,
  1196. spi_target_configure);
  1197. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1198. spi_device_match,
  1199. spi_device_configure);
  1200. static struct attribute *host_attributes[] = {
  1201. &dev_attr_signalling.attr,
  1202. &dev_attr_host_width.attr,
  1203. &dev_attr_hba_id.attr,
  1204. NULL
  1205. };
  1206. static struct attribute_group host_attribute_group = {
  1207. .attrs = host_attributes,
  1208. };
  1209. static int spi_host_configure(struct transport_container *tc,
  1210. struct device *dev,
  1211. struct device *cdev)
  1212. {
  1213. struct kobject *kobj = &cdev->kobj;
  1214. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1215. struct spi_internal *si = to_spi_internal(shost->transportt);
  1216. struct attribute *attr = &dev_attr_signalling.attr;
  1217. int rc = 0;
  1218. if (si->f->set_signalling)
  1219. rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
  1220. return rc;
  1221. }
  1222. /* returns true if we should be showing the variable. Also
  1223. * overloads the return by setting 1<<1 if the attribute should
  1224. * be writeable */
  1225. #define TARGET_ATTRIBUTE_HELPER(name) \
  1226. (si->f->show_##name ? S_IRUGO : 0) | \
  1227. (si->f->set_##name ? S_IWUSR : 0)
  1228. static umode_t target_attribute_is_visible(struct kobject *kobj,
  1229. struct attribute *attr, int i)
  1230. {
  1231. struct device *cdev = container_of(kobj, struct device, kobj);
  1232. struct scsi_target *starget = transport_class_to_starget(cdev);
  1233. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1234. struct spi_internal *si = to_spi_internal(shost->transportt);
  1235. if (attr == &dev_attr_period.attr &&
  1236. spi_support_sync(starget))
  1237. return TARGET_ATTRIBUTE_HELPER(period);
  1238. else if (attr == &dev_attr_min_period.attr &&
  1239. spi_support_sync(starget))
  1240. return TARGET_ATTRIBUTE_HELPER(period);
  1241. else if (attr == &dev_attr_offset.attr &&
  1242. spi_support_sync(starget))
  1243. return TARGET_ATTRIBUTE_HELPER(offset);
  1244. else if (attr == &dev_attr_max_offset.attr &&
  1245. spi_support_sync(starget))
  1246. return TARGET_ATTRIBUTE_HELPER(offset);
  1247. else if (attr == &dev_attr_width.attr &&
  1248. spi_support_wide(starget))
  1249. return TARGET_ATTRIBUTE_HELPER(width);
  1250. else if (attr == &dev_attr_max_width.attr &&
  1251. spi_support_wide(starget))
  1252. return TARGET_ATTRIBUTE_HELPER(width);
  1253. else if (attr == &dev_attr_iu.attr &&
  1254. spi_support_ius(starget))
  1255. return TARGET_ATTRIBUTE_HELPER(iu);
  1256. else if (attr == &dev_attr_max_iu.attr &&
  1257. spi_support_ius(starget))
  1258. return TARGET_ATTRIBUTE_HELPER(iu);
  1259. else if (attr == &dev_attr_dt.attr &&
  1260. spi_support_dt(starget))
  1261. return TARGET_ATTRIBUTE_HELPER(dt);
  1262. else if (attr == &dev_attr_qas.attr &&
  1263. spi_support_qas(starget))
  1264. return TARGET_ATTRIBUTE_HELPER(qas);
  1265. else if (attr == &dev_attr_max_qas.attr &&
  1266. spi_support_qas(starget))
  1267. return TARGET_ATTRIBUTE_HELPER(qas);
  1268. else if (attr == &dev_attr_wr_flow.attr &&
  1269. spi_support_ius(starget))
  1270. return TARGET_ATTRIBUTE_HELPER(wr_flow);
  1271. else if (attr == &dev_attr_rd_strm.attr &&
  1272. spi_support_ius(starget))
  1273. return TARGET_ATTRIBUTE_HELPER(rd_strm);
  1274. else if (attr == &dev_attr_rti.attr &&
  1275. spi_support_ius(starget))
  1276. return TARGET_ATTRIBUTE_HELPER(rti);
  1277. else if (attr == &dev_attr_pcomp_en.attr &&
  1278. spi_support_ius(starget))
  1279. return TARGET_ATTRIBUTE_HELPER(pcomp_en);
  1280. else if (attr == &dev_attr_hold_mcs.attr &&
  1281. spi_support_ius(starget))
  1282. return TARGET_ATTRIBUTE_HELPER(hold_mcs);
  1283. else if (attr == &dev_attr_revalidate.attr)
  1284. return S_IWUSR;
  1285. return 0;
  1286. }
  1287. static struct attribute *target_attributes[] = {
  1288. &dev_attr_period.attr,
  1289. &dev_attr_min_period.attr,
  1290. &dev_attr_offset.attr,
  1291. &dev_attr_max_offset.attr,
  1292. &dev_attr_width.attr,
  1293. &dev_attr_max_width.attr,
  1294. &dev_attr_iu.attr,
  1295. &dev_attr_max_iu.attr,
  1296. &dev_attr_dt.attr,
  1297. &dev_attr_qas.attr,
  1298. &dev_attr_max_qas.attr,
  1299. &dev_attr_wr_flow.attr,
  1300. &dev_attr_rd_strm.attr,
  1301. &dev_attr_rti.attr,
  1302. &dev_attr_pcomp_en.attr,
  1303. &dev_attr_hold_mcs.attr,
  1304. &dev_attr_revalidate.attr,
  1305. NULL
  1306. };
  1307. static struct attribute_group target_attribute_group = {
  1308. .attrs = target_attributes,
  1309. .is_visible = target_attribute_is_visible,
  1310. };
  1311. static int spi_target_configure(struct transport_container *tc,
  1312. struct device *dev,
  1313. struct device *cdev)
  1314. {
  1315. struct kobject *kobj = &cdev->kobj;
  1316. /* force an update based on parameters read from the device */
  1317. sysfs_update_group(kobj, &target_attribute_group);
  1318. return 0;
  1319. }
  1320. struct scsi_transport_template *
  1321. spi_attach_transport(struct spi_function_template *ft)
  1322. {
  1323. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1324. GFP_KERNEL);
  1325. if (unlikely(!i))
  1326. return NULL;
  1327. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1328. i->t.target_attrs.ac.grp = &target_attribute_group;
  1329. i->t.target_attrs.ac.match = spi_target_match;
  1330. transport_container_register(&i->t.target_attrs);
  1331. i->t.target_size = sizeof(struct spi_transport_attrs);
  1332. i->t.host_attrs.ac.class = &spi_host_class.class;
  1333. i->t.host_attrs.ac.grp = &host_attribute_group;
  1334. i->t.host_attrs.ac.match = spi_host_match;
  1335. transport_container_register(&i->t.host_attrs);
  1336. i->t.host_size = sizeof(struct spi_host_attrs);
  1337. i->f = ft;
  1338. return &i->t;
  1339. }
  1340. EXPORT_SYMBOL(spi_attach_transport);
  1341. void spi_release_transport(struct scsi_transport_template *t)
  1342. {
  1343. struct spi_internal *i = to_spi_internal(t);
  1344. transport_container_unregister(&i->t.target_attrs);
  1345. transport_container_unregister(&i->t.host_attrs);
  1346. kfree(i);
  1347. }
  1348. EXPORT_SYMBOL(spi_release_transport);
  1349. static __init int spi_transport_init(void)
  1350. {
  1351. int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
  1352. "SCSI Parallel Transport Class");
  1353. if (!error) {
  1354. int i;
  1355. for (i = 0; spi_static_device_list[i].vendor; i++)
  1356. scsi_dev_info_list_add_keyed(1, /* compatible */
  1357. spi_static_device_list[i].vendor,
  1358. spi_static_device_list[i].model,
  1359. NULL,
  1360. spi_static_device_list[i].flags,
  1361. SCSI_DEVINFO_SPI);
  1362. }
  1363. error = transport_class_register(&spi_transport_class);
  1364. if (error)
  1365. return error;
  1366. error = anon_transport_class_register(&spi_device_class);
  1367. return transport_class_register(&spi_host_class);
  1368. }
  1369. static void __exit spi_transport_exit(void)
  1370. {
  1371. transport_class_unregister(&spi_transport_class);
  1372. anon_transport_class_unregister(&spi_device_class);
  1373. transport_class_unregister(&spi_host_class);
  1374. scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
  1375. }
  1376. MODULE_AUTHOR("Martin Hicks");
  1377. MODULE_DESCRIPTION("SPI Transport Attributes");
  1378. MODULE_LICENSE("GPL");
  1379. module_init(spi_transport_init);
  1380. module_exit(spi_transport_exit);